• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2018 Intel Corporation. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/of_net.h>
6 #include <linux/pci.h>
7 #include <linux/bpf.h>
8 #include <generated/utsrelease.h>
9 
10 /* Local includes */
11 #include "i40e.h"
12 #include "i40e_diag.h"
13 #include "i40e_xsk.h"
14 #include <net/udp_tunnel.h>
15 #include <net/xdp_sock_drv.h>
16 /* All i40e tracepoints are defined by the include below, which
17  * must be included exactly once across the whole kernel with
18  * CREATE_TRACE_POINTS defined
19  */
20 #define CREATE_TRACE_POINTS
21 #include "i40e_trace.h"
22 
23 const char i40e_driver_name[] = "i40e";
24 static const char i40e_driver_string[] =
25 			"Intel(R) Ethernet Connection XL710 Network Driver";
26 
27 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
28 
29 /* a bit of forward declarations */
30 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
31 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
32 static int i40e_add_vsi(struct i40e_vsi *vsi);
33 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
34 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
35 static int i40e_setup_misc_vector(struct i40e_pf *pf);
36 static void i40e_determine_queue_usage(struct i40e_pf *pf);
37 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
38 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
39 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
40 				   bool lock_acquired);
41 static int i40e_reset(struct i40e_pf *pf);
42 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
43 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
44 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
45 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
46 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
47 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
48 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
49 static int i40e_get_capabilities(struct i40e_pf *pf,
50 				 enum i40e_admin_queue_opc list_type);
51 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
52 
53 /* i40e_pci_tbl - PCI Device ID Table
54  *
55  * Last entry must be all 0s
56  *
57  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
58  *   Class, Class Mask, private data (not used) }
59  */
60 static const struct pci_device_id i40e_pci_tbl[] = {
61 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
85 	/* required last entry */
86 	{0, }
87 };
88 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
89 
90 #define I40E_MAX_VF_COUNT 128
91 static int debug = -1;
92 module_param(debug, uint, 0);
93 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
94 
95 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
96 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
97 MODULE_LICENSE("GPL v2");
98 
99 static struct workqueue_struct *i40e_wq;
100 
netdev_hw_addr_refcnt(struct i40e_mac_filter * f,struct net_device * netdev,int delta)101 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
102 				  struct net_device *netdev, int delta)
103 {
104 	struct netdev_hw_addr *ha;
105 
106 	if (!f || !netdev)
107 		return;
108 
109 	netdev_for_each_mc_addr(ha, netdev) {
110 		if (ether_addr_equal(ha->addr, f->macaddr)) {
111 			ha->refcount += delta;
112 			if (ha->refcount <= 0)
113 				ha->refcount = 1;
114 			break;
115 		}
116 	}
117 }
118 
119 /**
120  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
121  * @hw:   pointer to the HW structure
122  * @mem:  ptr to mem struct to fill out
123  * @size: size of memory requested
124  * @alignment: what to align the allocation to
125  **/
i40e_allocate_dma_mem_d(struct i40e_hw * hw,struct i40e_dma_mem * mem,u64 size,u32 alignment)126 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
127 			    u64 size, u32 alignment)
128 {
129 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
130 
131 	mem->size = ALIGN(size, alignment);
132 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
133 				     GFP_KERNEL);
134 	if (!mem->va)
135 		return -ENOMEM;
136 
137 	return 0;
138 }
139 
140 /**
141  * i40e_free_dma_mem_d - OS specific memory free for shared code
142  * @hw:   pointer to the HW structure
143  * @mem:  ptr to mem struct to free
144  **/
i40e_free_dma_mem_d(struct i40e_hw * hw,struct i40e_dma_mem * mem)145 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
146 {
147 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
148 
149 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
150 	mem->va = NULL;
151 	mem->pa = 0;
152 	mem->size = 0;
153 
154 	return 0;
155 }
156 
157 /**
158  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
159  * @hw:   pointer to the HW structure
160  * @mem:  ptr to mem struct to fill out
161  * @size: size of memory requested
162  **/
i40e_allocate_virt_mem_d(struct i40e_hw * hw,struct i40e_virt_mem * mem,u32 size)163 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
164 			     u32 size)
165 {
166 	mem->size = size;
167 	mem->va = kzalloc(size, GFP_KERNEL);
168 
169 	if (!mem->va)
170 		return -ENOMEM;
171 
172 	return 0;
173 }
174 
175 /**
176  * i40e_free_virt_mem_d - OS specific memory free for shared code
177  * @hw:   pointer to the HW structure
178  * @mem:  ptr to mem struct to free
179  **/
i40e_free_virt_mem_d(struct i40e_hw * hw,struct i40e_virt_mem * mem)180 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
181 {
182 	/* it's ok to kfree a NULL pointer */
183 	kfree(mem->va);
184 	mem->va = NULL;
185 	mem->size = 0;
186 
187 	return 0;
188 }
189 
190 /**
191  * i40e_get_lump - find a lump of free generic resource
192  * @pf: board private structure
193  * @pile: the pile of resource to search
194  * @needed: the number of items needed
195  * @id: an owner id to stick on the items assigned
196  *
197  * Returns the base item index of the lump, or negative for error
198  **/
i40e_get_lump(struct i40e_pf * pf,struct i40e_lump_tracking * pile,u16 needed,u16 id)199 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
200 			 u16 needed, u16 id)
201 {
202 	int ret = -ENOMEM;
203 	int i, j;
204 
205 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
206 		dev_info(&pf->pdev->dev,
207 			 "param err: pile=%s needed=%d id=0x%04x\n",
208 			 pile ? "<valid>" : "<null>", needed, id);
209 		return -EINVAL;
210 	}
211 
212 	/* Allocate last queue in the pile for FDIR VSI queue
213 	 * so it doesn't fragment the qp_pile
214 	 */
215 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
216 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
217 			dev_err(&pf->pdev->dev,
218 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
219 				pile->num_entries - 1);
220 			return -ENOMEM;
221 		}
222 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
223 		return pile->num_entries - 1;
224 	}
225 
226 	i = 0;
227 	while (i < pile->num_entries) {
228 		/* skip already allocated entries */
229 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
230 			i++;
231 			continue;
232 		}
233 
234 		/* do we have enough in this lump? */
235 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
236 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
237 				break;
238 		}
239 
240 		if (j == needed) {
241 			/* there was enough, so assign it to the requestor */
242 			for (j = 0; j < needed; j++)
243 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
244 			ret = i;
245 			break;
246 		}
247 
248 		/* not enough, so skip over it and continue looking */
249 		i += j;
250 	}
251 
252 	return ret;
253 }
254 
255 /**
256  * i40e_put_lump - return a lump of generic resource
257  * @pile: the pile of resource to search
258  * @index: the base item index
259  * @id: the owner id of the items assigned
260  *
261  * Returns the count of items in the lump
262  **/
i40e_put_lump(struct i40e_lump_tracking * pile,u16 index,u16 id)263 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
264 {
265 	int valid_id = (id | I40E_PILE_VALID_BIT);
266 	int count = 0;
267 	u16 i;
268 
269 	if (!pile || index >= pile->num_entries)
270 		return -EINVAL;
271 
272 	for (i = index;
273 	     i < pile->num_entries && pile->list[i] == valid_id;
274 	     i++) {
275 		pile->list[i] = 0;
276 		count++;
277 	}
278 
279 
280 	return count;
281 }
282 
283 /**
284  * i40e_find_vsi_from_id - searches for the vsi with the given id
285  * @pf: the pf structure to search for the vsi
286  * @id: id of the vsi it is searching for
287  **/
i40e_find_vsi_from_id(struct i40e_pf * pf,u16 id)288 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
289 {
290 	int i;
291 
292 	for (i = 0; i < pf->num_alloc_vsi; i++)
293 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
294 			return pf->vsi[i];
295 
296 	return NULL;
297 }
298 
299 /**
300  * i40e_service_event_schedule - Schedule the service task to wake up
301  * @pf: board private structure
302  *
303  * If not already scheduled, this puts the task into the work queue
304  **/
i40e_service_event_schedule(struct i40e_pf * pf)305 void i40e_service_event_schedule(struct i40e_pf *pf)
306 {
307 	if ((!test_bit(__I40E_DOWN, pf->state) &&
308 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
309 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
310 		queue_work(i40e_wq, &pf->service_task);
311 }
312 
313 /**
314  * i40e_tx_timeout - Respond to a Tx Hang
315  * @netdev: network interface device structure
316  * @txqueue: queue number timing out
317  *
318  * If any port has noticed a Tx timeout, it is likely that the whole
319  * device is munged, not just the one netdev port, so go for the full
320  * reset.
321  **/
i40e_tx_timeout(struct net_device * netdev,unsigned int txqueue)322 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
323 {
324 	struct i40e_netdev_priv *np = netdev_priv(netdev);
325 	struct i40e_vsi *vsi = np->vsi;
326 	struct i40e_pf *pf = vsi->back;
327 	struct i40e_ring *tx_ring = NULL;
328 	unsigned int i;
329 	u32 head, val;
330 
331 	pf->tx_timeout_count++;
332 
333 	/* with txqueue index, find the tx_ring struct */
334 	for (i = 0; i < vsi->num_queue_pairs; i++) {
335 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
336 			if (txqueue ==
337 			    vsi->tx_rings[i]->queue_index) {
338 				tx_ring = vsi->tx_rings[i];
339 				break;
340 			}
341 		}
342 	}
343 
344 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
345 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
346 	else if (time_before(jiffies,
347 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
348 		return;   /* don't do any new action before the next timeout */
349 
350 	/* don't kick off another recovery if one is already pending */
351 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
352 		return;
353 
354 	if (tx_ring) {
355 		head = i40e_get_head(tx_ring);
356 		/* Read interrupt register */
357 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
358 			val = rd32(&pf->hw,
359 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
360 						tx_ring->vsi->base_vector - 1));
361 		else
362 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
363 
364 		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",
365 			    vsi->seid, txqueue, tx_ring->next_to_clean,
366 			    head, tx_ring->next_to_use,
367 			    readl(tx_ring->tail), val);
368 	}
369 
370 	pf->tx_timeout_last_recovery = jiffies;
371 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
372 		    pf->tx_timeout_recovery_level, txqueue);
373 
374 	switch (pf->tx_timeout_recovery_level) {
375 	case 1:
376 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
377 		break;
378 	case 2:
379 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
380 		break;
381 	case 3:
382 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
383 		break;
384 	default:
385 		netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
386 		break;
387 	}
388 
389 	i40e_service_event_schedule(pf);
390 	pf->tx_timeout_recovery_level++;
391 }
392 
393 /**
394  * i40e_get_vsi_stats_struct - Get System Network Statistics
395  * @vsi: the VSI we care about
396  *
397  * Returns the address of the device statistics structure.
398  * The statistics are actually updated from the service task.
399  **/
i40e_get_vsi_stats_struct(struct i40e_vsi * vsi)400 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
401 {
402 	return &vsi->net_stats;
403 }
404 
405 /**
406  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
407  * @ring: Tx ring to get statistics from
408  * @stats: statistics entry to be updated
409  **/
i40e_get_netdev_stats_struct_tx(struct i40e_ring * ring,struct rtnl_link_stats64 * stats)410 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
411 					    struct rtnl_link_stats64 *stats)
412 {
413 	u64 bytes, packets;
414 	unsigned int start;
415 
416 	do {
417 		start = u64_stats_fetch_begin_irq(&ring->syncp);
418 		packets = ring->stats.packets;
419 		bytes   = ring->stats.bytes;
420 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
421 
422 	stats->tx_packets += packets;
423 	stats->tx_bytes   += bytes;
424 }
425 
426 /**
427  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
428  * @netdev: network interface device structure
429  * @stats: data structure to store statistics
430  *
431  * Returns the address of the device statistics structure.
432  * The statistics are actually updated from the service task.
433  **/
i40e_get_netdev_stats_struct(struct net_device * netdev,struct rtnl_link_stats64 * stats)434 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
435 				  struct rtnl_link_stats64 *stats)
436 {
437 	struct i40e_netdev_priv *np = netdev_priv(netdev);
438 	struct i40e_vsi *vsi = np->vsi;
439 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
440 	struct i40e_ring *ring;
441 	int i;
442 
443 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
444 		return;
445 
446 	if (!vsi->tx_rings)
447 		return;
448 
449 	rcu_read_lock();
450 	for (i = 0; i < vsi->num_queue_pairs; i++) {
451 		u64 bytes, packets;
452 		unsigned int start;
453 
454 		ring = READ_ONCE(vsi->tx_rings[i]);
455 		if (!ring)
456 			continue;
457 		i40e_get_netdev_stats_struct_tx(ring, stats);
458 
459 		if (i40e_enabled_xdp_vsi(vsi)) {
460 			ring = READ_ONCE(vsi->xdp_rings[i]);
461 			if (!ring)
462 				continue;
463 			i40e_get_netdev_stats_struct_tx(ring, stats);
464 		}
465 
466 		ring = READ_ONCE(vsi->rx_rings[i]);
467 		if (!ring)
468 			continue;
469 		do {
470 			start   = u64_stats_fetch_begin_irq(&ring->syncp);
471 			packets = ring->stats.packets;
472 			bytes   = ring->stats.bytes;
473 		} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
474 
475 		stats->rx_packets += packets;
476 		stats->rx_bytes   += bytes;
477 
478 	}
479 	rcu_read_unlock();
480 
481 	/* following stats updated by i40e_watchdog_subtask() */
482 	stats->multicast	= vsi_stats->multicast;
483 	stats->tx_errors	= vsi_stats->tx_errors;
484 	stats->tx_dropped	= vsi_stats->tx_dropped;
485 	stats->rx_errors	= vsi_stats->rx_errors;
486 	stats->rx_dropped	= vsi_stats->rx_dropped;
487 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
488 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
489 }
490 
491 /**
492  * i40e_vsi_reset_stats - Resets all stats of the given vsi
493  * @vsi: the VSI to have its stats reset
494  **/
i40e_vsi_reset_stats(struct i40e_vsi * vsi)495 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
496 {
497 	struct rtnl_link_stats64 *ns;
498 	int i;
499 
500 	if (!vsi)
501 		return;
502 
503 	ns = i40e_get_vsi_stats_struct(vsi);
504 	memset(ns, 0, sizeof(*ns));
505 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
506 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
507 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
508 	if (vsi->rx_rings && vsi->rx_rings[0]) {
509 		for (i = 0; i < vsi->num_queue_pairs; i++) {
510 			memset(&vsi->rx_rings[i]->stats, 0,
511 			       sizeof(vsi->rx_rings[i]->stats));
512 			memset(&vsi->rx_rings[i]->rx_stats, 0,
513 			       sizeof(vsi->rx_rings[i]->rx_stats));
514 			memset(&vsi->tx_rings[i]->stats, 0,
515 			       sizeof(vsi->tx_rings[i]->stats));
516 			memset(&vsi->tx_rings[i]->tx_stats, 0,
517 			       sizeof(vsi->tx_rings[i]->tx_stats));
518 		}
519 	}
520 	vsi->stat_offsets_loaded = false;
521 }
522 
523 /**
524  * i40e_pf_reset_stats - Reset all of the stats for the given PF
525  * @pf: the PF to be reset
526  **/
i40e_pf_reset_stats(struct i40e_pf * pf)527 void i40e_pf_reset_stats(struct i40e_pf *pf)
528 {
529 	int i;
530 
531 	memset(&pf->stats, 0, sizeof(pf->stats));
532 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
533 	pf->stat_offsets_loaded = false;
534 
535 	for (i = 0; i < I40E_MAX_VEB; i++) {
536 		if (pf->veb[i]) {
537 			memset(&pf->veb[i]->stats, 0,
538 			       sizeof(pf->veb[i]->stats));
539 			memset(&pf->veb[i]->stats_offsets, 0,
540 			       sizeof(pf->veb[i]->stats_offsets));
541 			memset(&pf->veb[i]->tc_stats, 0,
542 			       sizeof(pf->veb[i]->tc_stats));
543 			memset(&pf->veb[i]->tc_stats_offsets, 0,
544 			       sizeof(pf->veb[i]->tc_stats_offsets));
545 			pf->veb[i]->stat_offsets_loaded = false;
546 		}
547 	}
548 	pf->hw_csum_rx_error = 0;
549 }
550 
551 /**
552  * i40e_stat_update48 - read and update a 48 bit stat from the chip
553  * @hw: ptr to the hardware info
554  * @hireg: the high 32 bit reg to read
555  * @loreg: the low 32 bit reg to read
556  * @offset_loaded: has the initial offset been loaded yet
557  * @offset: ptr to current offset value
558  * @stat: ptr to the stat
559  *
560  * Since the device stats are not reset at PFReset, they likely will not
561  * be zeroed when the driver starts.  We'll save the first values read
562  * and use them as offsets to be subtracted from the raw values in order
563  * to report stats that count from zero.  In the process, we also manage
564  * the potential roll-over.
565  **/
i40e_stat_update48(struct i40e_hw * hw,u32 hireg,u32 loreg,bool offset_loaded,u64 * offset,u64 * stat)566 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
567 			       bool offset_loaded, u64 *offset, u64 *stat)
568 {
569 	u64 new_data;
570 
571 	if (hw->device_id == I40E_DEV_ID_QEMU) {
572 		new_data = rd32(hw, loreg);
573 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
574 	} else {
575 		new_data = rd64(hw, loreg);
576 	}
577 	if (!offset_loaded)
578 		*offset = new_data;
579 	if (likely(new_data >= *offset))
580 		*stat = new_data - *offset;
581 	else
582 		*stat = (new_data + BIT_ULL(48)) - *offset;
583 	*stat &= 0xFFFFFFFFFFFFULL;
584 }
585 
586 /**
587  * i40e_stat_update32 - read and update a 32 bit stat from the chip
588  * @hw: ptr to the hardware info
589  * @reg: the hw reg to read
590  * @offset_loaded: has the initial offset been loaded yet
591  * @offset: ptr to current offset value
592  * @stat: ptr to the stat
593  **/
i40e_stat_update32(struct i40e_hw * hw,u32 reg,bool offset_loaded,u64 * offset,u64 * stat)594 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
595 			       bool offset_loaded, u64 *offset, u64 *stat)
596 {
597 	u32 new_data;
598 
599 	new_data = rd32(hw, reg);
600 	if (!offset_loaded)
601 		*offset = new_data;
602 	if (likely(new_data >= *offset))
603 		*stat = (u32)(new_data - *offset);
604 	else
605 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
606 }
607 
608 /**
609  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
610  * @hw: ptr to the hardware info
611  * @reg: the hw reg to read and clear
612  * @stat: ptr to the stat
613  **/
i40e_stat_update_and_clear32(struct i40e_hw * hw,u32 reg,u64 * stat)614 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
615 {
616 	u32 new_data = rd32(hw, reg);
617 
618 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
619 	*stat += new_data;
620 }
621 
622 /**
623  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
624  * @vsi: the VSI to be updated
625  **/
i40e_update_eth_stats(struct i40e_vsi * vsi)626 void i40e_update_eth_stats(struct i40e_vsi *vsi)
627 {
628 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
629 	struct i40e_pf *pf = vsi->back;
630 	struct i40e_hw *hw = &pf->hw;
631 	struct i40e_eth_stats *oes;
632 	struct i40e_eth_stats *es;     /* device's eth stats */
633 
634 	es = &vsi->eth_stats;
635 	oes = &vsi->eth_stats_offsets;
636 
637 	/* Gather up the stats that the hw collects */
638 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
639 			   vsi->stat_offsets_loaded,
640 			   &oes->tx_errors, &es->tx_errors);
641 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
642 			   vsi->stat_offsets_loaded,
643 			   &oes->rx_discards, &es->rx_discards);
644 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
645 			   vsi->stat_offsets_loaded,
646 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
647 
648 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
649 			   I40E_GLV_GORCL(stat_idx),
650 			   vsi->stat_offsets_loaded,
651 			   &oes->rx_bytes, &es->rx_bytes);
652 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
653 			   I40E_GLV_UPRCL(stat_idx),
654 			   vsi->stat_offsets_loaded,
655 			   &oes->rx_unicast, &es->rx_unicast);
656 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
657 			   I40E_GLV_MPRCL(stat_idx),
658 			   vsi->stat_offsets_loaded,
659 			   &oes->rx_multicast, &es->rx_multicast);
660 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
661 			   I40E_GLV_BPRCL(stat_idx),
662 			   vsi->stat_offsets_loaded,
663 			   &oes->rx_broadcast, &es->rx_broadcast);
664 
665 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
666 			   I40E_GLV_GOTCL(stat_idx),
667 			   vsi->stat_offsets_loaded,
668 			   &oes->tx_bytes, &es->tx_bytes);
669 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
670 			   I40E_GLV_UPTCL(stat_idx),
671 			   vsi->stat_offsets_loaded,
672 			   &oes->tx_unicast, &es->tx_unicast);
673 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
674 			   I40E_GLV_MPTCL(stat_idx),
675 			   vsi->stat_offsets_loaded,
676 			   &oes->tx_multicast, &es->tx_multicast);
677 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
678 			   I40E_GLV_BPTCL(stat_idx),
679 			   vsi->stat_offsets_loaded,
680 			   &oes->tx_broadcast, &es->tx_broadcast);
681 	vsi->stat_offsets_loaded = true;
682 }
683 
684 /**
685  * i40e_update_veb_stats - Update Switch component statistics
686  * @veb: the VEB being updated
687  **/
i40e_update_veb_stats(struct i40e_veb * veb)688 void i40e_update_veb_stats(struct i40e_veb *veb)
689 {
690 	struct i40e_pf *pf = veb->pf;
691 	struct i40e_hw *hw = &pf->hw;
692 	struct i40e_eth_stats *oes;
693 	struct i40e_eth_stats *es;     /* device's eth stats */
694 	struct i40e_veb_tc_stats *veb_oes;
695 	struct i40e_veb_tc_stats *veb_es;
696 	int i, idx = 0;
697 
698 	idx = veb->stats_idx;
699 	es = &veb->stats;
700 	oes = &veb->stats_offsets;
701 	veb_es = &veb->tc_stats;
702 	veb_oes = &veb->tc_stats_offsets;
703 
704 	/* Gather up the stats that the hw collects */
705 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
706 			   veb->stat_offsets_loaded,
707 			   &oes->tx_discards, &es->tx_discards);
708 	if (hw->revision_id > 0)
709 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
710 				   veb->stat_offsets_loaded,
711 				   &oes->rx_unknown_protocol,
712 				   &es->rx_unknown_protocol);
713 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
714 			   veb->stat_offsets_loaded,
715 			   &oes->rx_bytes, &es->rx_bytes);
716 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
717 			   veb->stat_offsets_loaded,
718 			   &oes->rx_unicast, &es->rx_unicast);
719 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
720 			   veb->stat_offsets_loaded,
721 			   &oes->rx_multicast, &es->rx_multicast);
722 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
723 			   veb->stat_offsets_loaded,
724 			   &oes->rx_broadcast, &es->rx_broadcast);
725 
726 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
727 			   veb->stat_offsets_loaded,
728 			   &oes->tx_bytes, &es->tx_bytes);
729 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
730 			   veb->stat_offsets_loaded,
731 			   &oes->tx_unicast, &es->tx_unicast);
732 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
733 			   veb->stat_offsets_loaded,
734 			   &oes->tx_multicast, &es->tx_multicast);
735 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
736 			   veb->stat_offsets_loaded,
737 			   &oes->tx_broadcast, &es->tx_broadcast);
738 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
739 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
740 				   I40E_GLVEBTC_RPCL(i, idx),
741 				   veb->stat_offsets_loaded,
742 				   &veb_oes->tc_rx_packets[i],
743 				   &veb_es->tc_rx_packets[i]);
744 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
745 				   I40E_GLVEBTC_RBCL(i, idx),
746 				   veb->stat_offsets_loaded,
747 				   &veb_oes->tc_rx_bytes[i],
748 				   &veb_es->tc_rx_bytes[i]);
749 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
750 				   I40E_GLVEBTC_TPCL(i, idx),
751 				   veb->stat_offsets_loaded,
752 				   &veb_oes->tc_tx_packets[i],
753 				   &veb_es->tc_tx_packets[i]);
754 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
755 				   I40E_GLVEBTC_TBCL(i, idx),
756 				   veb->stat_offsets_loaded,
757 				   &veb_oes->tc_tx_bytes[i],
758 				   &veb_es->tc_tx_bytes[i]);
759 	}
760 	veb->stat_offsets_loaded = true;
761 }
762 
763 /**
764  * i40e_update_vsi_stats - Update the vsi statistics counters.
765  * @vsi: the VSI to be updated
766  *
767  * There are a few instances where we store the same stat in a
768  * couple of different structs.  This is partly because we have
769  * the netdev stats that need to be filled out, which is slightly
770  * different from the "eth_stats" defined by the chip and used in
771  * VF communications.  We sort it out here.
772  **/
i40e_update_vsi_stats(struct i40e_vsi * vsi)773 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
774 {
775 	struct i40e_pf *pf = vsi->back;
776 	struct rtnl_link_stats64 *ons;
777 	struct rtnl_link_stats64 *ns;   /* netdev stats */
778 	struct i40e_eth_stats *oes;
779 	struct i40e_eth_stats *es;     /* device's eth stats */
780 	u64 tx_restart, tx_busy;
781 	struct i40e_ring *p;
782 	u64 rx_page, rx_buf;
783 	u64 bytes, packets;
784 	unsigned int start;
785 	u64 tx_linearize;
786 	u64 tx_force_wb;
787 	u64 rx_p, rx_b;
788 	u64 tx_p, tx_b;
789 	u16 q;
790 
791 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
792 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
793 		return;
794 
795 	ns = i40e_get_vsi_stats_struct(vsi);
796 	ons = &vsi->net_stats_offsets;
797 	es = &vsi->eth_stats;
798 	oes = &vsi->eth_stats_offsets;
799 
800 	/* Gather up the netdev and vsi stats that the driver collects
801 	 * on the fly during packet processing
802 	 */
803 	rx_b = rx_p = 0;
804 	tx_b = tx_p = 0;
805 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
806 	rx_page = 0;
807 	rx_buf = 0;
808 	rcu_read_lock();
809 	for (q = 0; q < vsi->num_queue_pairs; q++) {
810 		/* locate Tx ring */
811 		p = READ_ONCE(vsi->tx_rings[q]);
812 		if (!p)
813 			continue;
814 
815 		do {
816 			start = u64_stats_fetch_begin_irq(&p->syncp);
817 			packets = p->stats.packets;
818 			bytes = p->stats.bytes;
819 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
820 		tx_b += bytes;
821 		tx_p += packets;
822 		tx_restart += p->tx_stats.restart_queue;
823 		tx_busy += p->tx_stats.tx_busy;
824 		tx_linearize += p->tx_stats.tx_linearize;
825 		tx_force_wb += p->tx_stats.tx_force_wb;
826 
827 		/* locate Rx ring */
828 		p = READ_ONCE(vsi->rx_rings[q]);
829 		if (!p)
830 			continue;
831 
832 		do {
833 			start = u64_stats_fetch_begin_irq(&p->syncp);
834 			packets = p->stats.packets;
835 			bytes = p->stats.bytes;
836 		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
837 		rx_b += bytes;
838 		rx_p += packets;
839 		rx_buf += p->rx_stats.alloc_buff_failed;
840 		rx_page += p->rx_stats.alloc_page_failed;
841 
842 		if (i40e_enabled_xdp_vsi(vsi)) {
843 			/* locate XDP ring */
844 			p = READ_ONCE(vsi->xdp_rings[q]);
845 			if (!p)
846 				continue;
847 
848 			do {
849 				start = u64_stats_fetch_begin_irq(&p->syncp);
850 				packets = p->stats.packets;
851 				bytes = p->stats.bytes;
852 			} while (u64_stats_fetch_retry_irq(&p->syncp, start));
853 			tx_b += bytes;
854 			tx_p += packets;
855 			tx_restart += p->tx_stats.restart_queue;
856 			tx_busy += p->tx_stats.tx_busy;
857 			tx_linearize += p->tx_stats.tx_linearize;
858 			tx_force_wb += p->tx_stats.tx_force_wb;
859 		}
860 	}
861 	rcu_read_unlock();
862 	vsi->tx_restart = tx_restart;
863 	vsi->tx_busy = tx_busy;
864 	vsi->tx_linearize = tx_linearize;
865 	vsi->tx_force_wb = tx_force_wb;
866 	vsi->rx_page_failed = rx_page;
867 	vsi->rx_buf_failed = rx_buf;
868 
869 	ns->rx_packets = rx_p;
870 	ns->rx_bytes = rx_b;
871 	ns->tx_packets = tx_p;
872 	ns->tx_bytes = tx_b;
873 
874 	/* update netdev stats from eth stats */
875 	i40e_update_eth_stats(vsi);
876 	ons->tx_errors = oes->tx_errors;
877 	ns->tx_errors = es->tx_errors;
878 	ons->multicast = oes->rx_multicast;
879 	ns->multicast = es->rx_multicast;
880 	ons->rx_dropped = oes->rx_discards;
881 	ns->rx_dropped = es->rx_discards;
882 	ons->tx_dropped = oes->tx_discards;
883 	ns->tx_dropped = es->tx_discards;
884 
885 	/* pull in a couple PF stats if this is the main vsi */
886 	if (vsi == pf->vsi[pf->lan_vsi]) {
887 		ns->rx_crc_errors = pf->stats.crc_errors;
888 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
889 		ns->rx_length_errors = pf->stats.rx_length_errors;
890 	}
891 }
892 
893 /**
894  * i40e_update_pf_stats - Update the PF statistics counters.
895  * @pf: the PF to be updated
896  **/
i40e_update_pf_stats(struct i40e_pf * pf)897 static void i40e_update_pf_stats(struct i40e_pf *pf)
898 {
899 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
900 	struct i40e_hw_port_stats *nsd = &pf->stats;
901 	struct i40e_hw *hw = &pf->hw;
902 	u32 val;
903 	int i;
904 
905 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
906 			   I40E_GLPRT_GORCL(hw->port),
907 			   pf->stat_offsets_loaded,
908 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
909 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
910 			   I40E_GLPRT_GOTCL(hw->port),
911 			   pf->stat_offsets_loaded,
912 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
913 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
914 			   pf->stat_offsets_loaded,
915 			   &osd->eth.rx_discards,
916 			   &nsd->eth.rx_discards);
917 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
918 			   I40E_GLPRT_UPRCL(hw->port),
919 			   pf->stat_offsets_loaded,
920 			   &osd->eth.rx_unicast,
921 			   &nsd->eth.rx_unicast);
922 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
923 			   I40E_GLPRT_MPRCL(hw->port),
924 			   pf->stat_offsets_loaded,
925 			   &osd->eth.rx_multicast,
926 			   &nsd->eth.rx_multicast);
927 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
928 			   I40E_GLPRT_BPRCL(hw->port),
929 			   pf->stat_offsets_loaded,
930 			   &osd->eth.rx_broadcast,
931 			   &nsd->eth.rx_broadcast);
932 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
933 			   I40E_GLPRT_UPTCL(hw->port),
934 			   pf->stat_offsets_loaded,
935 			   &osd->eth.tx_unicast,
936 			   &nsd->eth.tx_unicast);
937 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
938 			   I40E_GLPRT_MPTCL(hw->port),
939 			   pf->stat_offsets_loaded,
940 			   &osd->eth.tx_multicast,
941 			   &nsd->eth.tx_multicast);
942 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
943 			   I40E_GLPRT_BPTCL(hw->port),
944 			   pf->stat_offsets_loaded,
945 			   &osd->eth.tx_broadcast,
946 			   &nsd->eth.tx_broadcast);
947 
948 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
949 			   pf->stat_offsets_loaded,
950 			   &osd->tx_dropped_link_down,
951 			   &nsd->tx_dropped_link_down);
952 
953 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
954 			   pf->stat_offsets_loaded,
955 			   &osd->crc_errors, &nsd->crc_errors);
956 
957 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
958 			   pf->stat_offsets_loaded,
959 			   &osd->illegal_bytes, &nsd->illegal_bytes);
960 
961 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
962 			   pf->stat_offsets_loaded,
963 			   &osd->mac_local_faults,
964 			   &nsd->mac_local_faults);
965 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
966 			   pf->stat_offsets_loaded,
967 			   &osd->mac_remote_faults,
968 			   &nsd->mac_remote_faults);
969 
970 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
971 			   pf->stat_offsets_loaded,
972 			   &osd->rx_length_errors,
973 			   &nsd->rx_length_errors);
974 
975 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
976 			   pf->stat_offsets_loaded,
977 			   &osd->link_xon_rx, &nsd->link_xon_rx);
978 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
979 			   pf->stat_offsets_loaded,
980 			   &osd->link_xon_tx, &nsd->link_xon_tx);
981 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
982 			   pf->stat_offsets_loaded,
983 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
984 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
985 			   pf->stat_offsets_loaded,
986 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
987 
988 	for (i = 0; i < 8; i++) {
989 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
990 				   pf->stat_offsets_loaded,
991 				   &osd->priority_xoff_rx[i],
992 				   &nsd->priority_xoff_rx[i]);
993 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
994 				   pf->stat_offsets_loaded,
995 				   &osd->priority_xon_rx[i],
996 				   &nsd->priority_xon_rx[i]);
997 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
998 				   pf->stat_offsets_loaded,
999 				   &osd->priority_xon_tx[i],
1000 				   &nsd->priority_xon_tx[i]);
1001 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1002 				   pf->stat_offsets_loaded,
1003 				   &osd->priority_xoff_tx[i],
1004 				   &nsd->priority_xoff_tx[i]);
1005 		i40e_stat_update32(hw,
1006 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1007 				   pf->stat_offsets_loaded,
1008 				   &osd->priority_xon_2_xoff[i],
1009 				   &nsd->priority_xon_2_xoff[i]);
1010 	}
1011 
1012 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1013 			   I40E_GLPRT_PRC64L(hw->port),
1014 			   pf->stat_offsets_loaded,
1015 			   &osd->rx_size_64, &nsd->rx_size_64);
1016 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1017 			   I40E_GLPRT_PRC127L(hw->port),
1018 			   pf->stat_offsets_loaded,
1019 			   &osd->rx_size_127, &nsd->rx_size_127);
1020 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1021 			   I40E_GLPRT_PRC255L(hw->port),
1022 			   pf->stat_offsets_loaded,
1023 			   &osd->rx_size_255, &nsd->rx_size_255);
1024 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1025 			   I40E_GLPRT_PRC511L(hw->port),
1026 			   pf->stat_offsets_loaded,
1027 			   &osd->rx_size_511, &nsd->rx_size_511);
1028 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1029 			   I40E_GLPRT_PRC1023L(hw->port),
1030 			   pf->stat_offsets_loaded,
1031 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1032 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1033 			   I40E_GLPRT_PRC1522L(hw->port),
1034 			   pf->stat_offsets_loaded,
1035 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1036 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1037 			   I40E_GLPRT_PRC9522L(hw->port),
1038 			   pf->stat_offsets_loaded,
1039 			   &osd->rx_size_big, &nsd->rx_size_big);
1040 
1041 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1042 			   I40E_GLPRT_PTC64L(hw->port),
1043 			   pf->stat_offsets_loaded,
1044 			   &osd->tx_size_64, &nsd->tx_size_64);
1045 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1046 			   I40E_GLPRT_PTC127L(hw->port),
1047 			   pf->stat_offsets_loaded,
1048 			   &osd->tx_size_127, &nsd->tx_size_127);
1049 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1050 			   I40E_GLPRT_PTC255L(hw->port),
1051 			   pf->stat_offsets_loaded,
1052 			   &osd->tx_size_255, &nsd->tx_size_255);
1053 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1054 			   I40E_GLPRT_PTC511L(hw->port),
1055 			   pf->stat_offsets_loaded,
1056 			   &osd->tx_size_511, &nsd->tx_size_511);
1057 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1058 			   I40E_GLPRT_PTC1023L(hw->port),
1059 			   pf->stat_offsets_loaded,
1060 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1061 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1062 			   I40E_GLPRT_PTC1522L(hw->port),
1063 			   pf->stat_offsets_loaded,
1064 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1065 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1066 			   I40E_GLPRT_PTC9522L(hw->port),
1067 			   pf->stat_offsets_loaded,
1068 			   &osd->tx_size_big, &nsd->tx_size_big);
1069 
1070 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1071 			   pf->stat_offsets_loaded,
1072 			   &osd->rx_undersize, &nsd->rx_undersize);
1073 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1074 			   pf->stat_offsets_loaded,
1075 			   &osd->rx_fragments, &nsd->rx_fragments);
1076 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1077 			   pf->stat_offsets_loaded,
1078 			   &osd->rx_oversize, &nsd->rx_oversize);
1079 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1080 			   pf->stat_offsets_loaded,
1081 			   &osd->rx_jabber, &nsd->rx_jabber);
1082 
1083 	/* FDIR stats */
1084 	i40e_stat_update_and_clear32(hw,
1085 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1086 			&nsd->fd_atr_match);
1087 	i40e_stat_update_and_clear32(hw,
1088 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1089 			&nsd->fd_sb_match);
1090 	i40e_stat_update_and_clear32(hw,
1091 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1092 			&nsd->fd_atr_tunnel_match);
1093 
1094 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1095 	nsd->tx_lpi_status =
1096 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1097 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1098 	nsd->rx_lpi_status =
1099 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1100 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1101 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1102 			   pf->stat_offsets_loaded,
1103 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1104 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1105 			   pf->stat_offsets_loaded,
1106 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1107 
1108 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1109 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1110 		nsd->fd_sb_status = true;
1111 	else
1112 		nsd->fd_sb_status = false;
1113 
1114 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1115 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1116 		nsd->fd_atr_status = true;
1117 	else
1118 		nsd->fd_atr_status = false;
1119 
1120 	pf->stat_offsets_loaded = true;
1121 }
1122 
1123 /**
1124  * i40e_update_stats - Update the various statistics counters.
1125  * @vsi: the VSI to be updated
1126  *
1127  * Update the various stats for this VSI and its related entities.
1128  **/
i40e_update_stats(struct i40e_vsi * vsi)1129 void i40e_update_stats(struct i40e_vsi *vsi)
1130 {
1131 	struct i40e_pf *pf = vsi->back;
1132 
1133 	if (vsi == pf->vsi[pf->lan_vsi])
1134 		i40e_update_pf_stats(pf);
1135 
1136 	i40e_update_vsi_stats(vsi);
1137 }
1138 
1139 /**
1140  * i40e_count_filters - counts VSI mac filters
1141  * @vsi: the VSI to be searched
1142  *
1143  * Returns count of mac filters
1144  **/
i40e_count_filters(struct i40e_vsi * vsi)1145 int i40e_count_filters(struct i40e_vsi *vsi)
1146 {
1147 	struct i40e_mac_filter *f;
1148 	struct hlist_node *h;
1149 	int bkt;
1150 	int cnt = 0;
1151 
1152 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1153 		++cnt;
1154 
1155 	return cnt;
1156 }
1157 
1158 /**
1159  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1160  * @vsi: the VSI to be searched
1161  * @macaddr: the MAC address
1162  * @vlan: the vlan
1163  *
1164  * Returns ptr to the filter object or NULL
1165  **/
i40e_find_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1166 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1167 						const u8 *macaddr, s16 vlan)
1168 {
1169 	struct i40e_mac_filter *f;
1170 	u64 key;
1171 
1172 	if (!vsi || !macaddr)
1173 		return NULL;
1174 
1175 	key = i40e_addr_to_hkey(macaddr);
1176 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1177 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1178 		    (vlan == f->vlan))
1179 			return f;
1180 	}
1181 	return NULL;
1182 }
1183 
1184 /**
1185  * i40e_find_mac - Find a mac addr in the macvlan filters list
1186  * @vsi: the VSI to be searched
1187  * @macaddr: the MAC address we are searching for
1188  *
1189  * Returns the first filter with the provided MAC address or NULL if
1190  * MAC address was not found
1191  **/
i40e_find_mac(struct i40e_vsi * vsi,const u8 * macaddr)1192 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1193 {
1194 	struct i40e_mac_filter *f;
1195 	u64 key;
1196 
1197 	if (!vsi || !macaddr)
1198 		return NULL;
1199 
1200 	key = i40e_addr_to_hkey(macaddr);
1201 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1202 		if ((ether_addr_equal(macaddr, f->macaddr)))
1203 			return f;
1204 	}
1205 	return NULL;
1206 }
1207 
1208 /**
1209  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1210  * @vsi: the VSI to be searched
1211  *
1212  * Returns true if VSI is in vlan mode or false otherwise
1213  **/
i40e_is_vsi_in_vlan(struct i40e_vsi * vsi)1214 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1215 {
1216 	/* If we have a PVID, always operate in VLAN mode */
1217 	if (vsi->info.pvid)
1218 		return true;
1219 
1220 	/* We need to operate in VLAN mode whenever we have any filters with
1221 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1222 	 * time, incurring search cost repeatedly. However, we can notice two
1223 	 * things:
1224 	 *
1225 	 * 1) the only place where we can gain a VLAN filter is in
1226 	 *    i40e_add_filter.
1227 	 *
1228 	 * 2) the only place where filters are actually removed is in
1229 	 *    i40e_sync_filters_subtask.
1230 	 *
1231 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1232 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1233 	 * we have to perform the full search after deleting filters in
1234 	 * i40e_sync_filters_subtask, but we already have to search
1235 	 * filters here and can perform the check at the same time. This
1236 	 * results in avoiding embedding a loop for VLAN mode inside another
1237 	 * loop over all the filters, and should maintain correctness as noted
1238 	 * above.
1239 	 */
1240 	return vsi->has_vlan_filter;
1241 }
1242 
1243 /**
1244  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1245  * @vsi: the VSI to configure
1246  * @tmp_add_list: list of filters ready to be added
1247  * @tmp_del_list: list of filters ready to be deleted
1248  * @vlan_filters: the number of active VLAN filters
1249  *
1250  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1251  * behave as expected. If we have any active VLAN filters remaining or about
1252  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1253  * so that they only match against untagged traffic. If we no longer have any
1254  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1255  * so that they match against both tagged and untagged traffic. In this way,
1256  * we ensure that we correctly receive the desired traffic. This ensures that
1257  * when we have an active VLAN we will receive only untagged traffic and
1258  * traffic matching active VLANs. If we have no active VLANs then we will
1259  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1260  *
1261  * Finally, in a similar fashion, this function also corrects filters when
1262  * there is an active PVID assigned to this VSI.
1263  *
1264  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1265  *
1266  * This function is only expected to be called from within
1267  * i40e_sync_vsi_filters.
1268  *
1269  * NOTE: This function expects to be called while under the
1270  * mac_filter_hash_lock
1271  */
i40e_correct_mac_vlan_filters(struct i40e_vsi * vsi,struct hlist_head * tmp_add_list,struct hlist_head * tmp_del_list,int vlan_filters)1272 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1273 					 struct hlist_head *tmp_add_list,
1274 					 struct hlist_head *tmp_del_list,
1275 					 int vlan_filters)
1276 {
1277 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1278 	struct i40e_mac_filter *f, *add_head;
1279 	struct i40e_new_mac_filter *new;
1280 	struct hlist_node *h;
1281 	int bkt, new_vlan;
1282 
1283 	/* To determine if a particular filter needs to be replaced we
1284 	 * have the three following conditions:
1285 	 *
1286 	 * a) if we have a PVID assigned, then all filters which are
1287 	 *    not marked as VLAN=PVID must be replaced with filters that
1288 	 *    are.
1289 	 * b) otherwise, if we have any active VLANS, all filters
1290 	 *    which are marked as VLAN=-1 must be replaced with
1291 	 *    filters marked as VLAN=0
1292 	 * c) finally, if we do not have any active VLANS, all filters
1293 	 *    which are marked as VLAN=0 must be replaced with filters
1294 	 *    marked as VLAN=-1
1295 	 */
1296 
1297 	/* Update the filters about to be added in place */
1298 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1299 		if (pvid && new->f->vlan != pvid)
1300 			new->f->vlan = pvid;
1301 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1302 			new->f->vlan = 0;
1303 		else if (!vlan_filters && new->f->vlan == 0)
1304 			new->f->vlan = I40E_VLAN_ANY;
1305 	}
1306 
1307 	/* Update the remaining active filters */
1308 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1309 		/* Combine the checks for whether a filter needs to be changed
1310 		 * and then determine the new VLAN inside the if block, in
1311 		 * order to avoid duplicating code for adding the new filter
1312 		 * then deleting the old filter.
1313 		 */
1314 		if ((pvid && f->vlan != pvid) ||
1315 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1316 		    (!vlan_filters && f->vlan == 0)) {
1317 			/* Determine the new vlan we will be adding */
1318 			if (pvid)
1319 				new_vlan = pvid;
1320 			else if (vlan_filters)
1321 				new_vlan = 0;
1322 			else
1323 				new_vlan = I40E_VLAN_ANY;
1324 
1325 			/* Create the new filter */
1326 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1327 			if (!add_head)
1328 				return -ENOMEM;
1329 
1330 			/* Create a temporary i40e_new_mac_filter */
1331 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1332 			if (!new)
1333 				return -ENOMEM;
1334 
1335 			new->f = add_head;
1336 			new->state = add_head->state;
1337 
1338 			/* Add the new filter to the tmp list */
1339 			hlist_add_head(&new->hlist, tmp_add_list);
1340 
1341 			/* Put the original filter into the delete list */
1342 			f->state = I40E_FILTER_REMOVE;
1343 			hash_del(&f->hlist);
1344 			hlist_add_head(&f->hlist, tmp_del_list);
1345 		}
1346 	}
1347 
1348 	vsi->has_vlan_filter = !!vlan_filters;
1349 
1350 	return 0;
1351 }
1352 
1353 /**
1354  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1355  * @vsi: the PF Main VSI - inappropriate for any other VSI
1356  * @macaddr: the MAC address
1357  *
1358  * Remove whatever filter the firmware set up so the driver can manage
1359  * its own filtering intelligently.
1360  **/
i40e_rm_default_mac_filter(struct i40e_vsi * vsi,u8 * macaddr)1361 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1362 {
1363 	struct i40e_aqc_remove_macvlan_element_data element;
1364 	struct i40e_pf *pf = vsi->back;
1365 
1366 	/* Only appropriate for the PF main VSI */
1367 	if (vsi->type != I40E_VSI_MAIN)
1368 		return;
1369 
1370 	memset(&element, 0, sizeof(element));
1371 	ether_addr_copy(element.mac_addr, macaddr);
1372 	element.vlan_tag = 0;
1373 	/* Ignore error returns, some firmware does it this way... */
1374 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1375 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1376 
1377 	memset(&element, 0, sizeof(element));
1378 	ether_addr_copy(element.mac_addr, macaddr);
1379 	element.vlan_tag = 0;
1380 	/* ...and some firmware does it this way. */
1381 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1382 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1383 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1384 }
1385 
1386 /**
1387  * i40e_add_filter - Add a mac/vlan filter to the VSI
1388  * @vsi: the VSI to be searched
1389  * @macaddr: the MAC address
1390  * @vlan: the vlan
1391  *
1392  * Returns ptr to the filter object or NULL when no memory available.
1393  *
1394  * NOTE: This function is expected to be called with mac_filter_hash_lock
1395  * being held.
1396  **/
i40e_add_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1397 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1398 					const u8 *macaddr, s16 vlan)
1399 {
1400 	struct i40e_mac_filter *f;
1401 	u64 key;
1402 
1403 	if (!vsi || !macaddr)
1404 		return NULL;
1405 
1406 	f = i40e_find_filter(vsi, macaddr, vlan);
1407 	if (!f) {
1408 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1409 		if (!f)
1410 			return NULL;
1411 
1412 		/* Update the boolean indicating if we need to function in
1413 		 * VLAN mode.
1414 		 */
1415 		if (vlan >= 0)
1416 			vsi->has_vlan_filter = true;
1417 
1418 		ether_addr_copy(f->macaddr, macaddr);
1419 		f->vlan = vlan;
1420 		f->state = I40E_FILTER_NEW;
1421 		INIT_HLIST_NODE(&f->hlist);
1422 
1423 		key = i40e_addr_to_hkey(macaddr);
1424 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1425 
1426 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1427 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1428 	}
1429 
1430 	/* If we're asked to add a filter that has been marked for removal, it
1431 	 * is safe to simply restore it to active state. __i40e_del_filter
1432 	 * will have simply deleted any filters which were previously marked
1433 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1434 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1435 	 * task, just restore this filter to the ACTIVE state so that the
1436 	 * sync task leaves it in place
1437 	 */
1438 	if (f->state == I40E_FILTER_REMOVE)
1439 		f->state = I40E_FILTER_ACTIVE;
1440 
1441 	return f;
1442 }
1443 
1444 /**
1445  * __i40e_del_filter - Remove a specific filter from the VSI
1446  * @vsi: VSI to remove from
1447  * @f: the filter to remove from the list
1448  *
1449  * This function should be called instead of i40e_del_filter only if you know
1450  * the exact filter you will remove already, such as via i40e_find_filter or
1451  * i40e_find_mac.
1452  *
1453  * NOTE: This function is expected to be called with mac_filter_hash_lock
1454  * being held.
1455  * ANOTHER NOTE: This function MUST be called from within the context of
1456  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1457  * instead of list_for_each_entry().
1458  **/
__i40e_del_filter(struct i40e_vsi * vsi,struct i40e_mac_filter * f)1459 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1460 {
1461 	if (!f)
1462 		return;
1463 
1464 	/* If the filter was never added to firmware then we can just delete it
1465 	 * directly and we don't want to set the status to remove or else an
1466 	 * admin queue command will unnecessarily fire.
1467 	 */
1468 	if ((f->state == I40E_FILTER_FAILED) ||
1469 	    (f->state == I40E_FILTER_NEW)) {
1470 		hash_del(&f->hlist);
1471 		kfree(f);
1472 	} else {
1473 		f->state = I40E_FILTER_REMOVE;
1474 	}
1475 
1476 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1477 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1478 }
1479 
1480 /**
1481  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1482  * @vsi: the VSI to be searched
1483  * @macaddr: the MAC address
1484  * @vlan: the VLAN
1485  *
1486  * NOTE: This function is expected to be called with mac_filter_hash_lock
1487  * being held.
1488  * ANOTHER NOTE: This function MUST be called from within the context of
1489  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1490  * instead of list_for_each_entry().
1491  **/
i40e_del_filter(struct i40e_vsi * vsi,const u8 * macaddr,s16 vlan)1492 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1493 {
1494 	struct i40e_mac_filter *f;
1495 
1496 	if (!vsi || !macaddr)
1497 		return;
1498 
1499 	f = i40e_find_filter(vsi, macaddr, vlan);
1500 	__i40e_del_filter(vsi, f);
1501 }
1502 
1503 /**
1504  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1505  * @vsi: the VSI to be searched
1506  * @macaddr: the mac address to be filtered
1507  *
1508  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1509  * go through all the macvlan filters and add a macvlan filter for each
1510  * unique vlan that already exists. If a PVID has been assigned, instead only
1511  * add the macaddr to that VLAN.
1512  *
1513  * Returns last filter added on success, else NULL
1514  **/
i40e_add_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1515 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1516 					    const u8 *macaddr)
1517 {
1518 	struct i40e_mac_filter *f, *add = NULL;
1519 	struct hlist_node *h;
1520 	int bkt;
1521 
1522 	if (vsi->info.pvid)
1523 		return i40e_add_filter(vsi, macaddr,
1524 				       le16_to_cpu(vsi->info.pvid));
1525 
1526 	if (!i40e_is_vsi_in_vlan(vsi))
1527 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1528 
1529 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1530 		if (f->state == I40E_FILTER_REMOVE)
1531 			continue;
1532 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1533 		if (!add)
1534 			return NULL;
1535 	}
1536 
1537 	return add;
1538 }
1539 
1540 /**
1541  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1542  * @vsi: the VSI to be searched
1543  * @macaddr: the mac address to be removed
1544  *
1545  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1546  * associated with.
1547  *
1548  * Returns 0 for success, or error
1549  **/
i40e_del_mac_filter(struct i40e_vsi * vsi,const u8 * macaddr)1550 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1551 {
1552 	struct i40e_mac_filter *f;
1553 	struct hlist_node *h;
1554 	bool found = false;
1555 	int bkt;
1556 
1557 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1558 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1559 		if (ether_addr_equal(macaddr, f->macaddr)) {
1560 			__i40e_del_filter(vsi, f);
1561 			found = true;
1562 		}
1563 	}
1564 
1565 	if (found)
1566 		return 0;
1567 	else
1568 		return -ENOENT;
1569 }
1570 
1571 /**
1572  * i40e_set_mac - NDO callback to set mac address
1573  * @netdev: network interface device structure
1574  * @p: pointer to an address structure
1575  *
1576  * Returns 0 on success, negative on failure
1577  **/
i40e_set_mac(struct net_device * netdev,void * p)1578 static int i40e_set_mac(struct net_device *netdev, void *p)
1579 {
1580 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1581 	struct i40e_vsi *vsi = np->vsi;
1582 	struct i40e_pf *pf = vsi->back;
1583 	struct i40e_hw *hw = &pf->hw;
1584 	struct sockaddr *addr = p;
1585 
1586 	if (!is_valid_ether_addr(addr->sa_data))
1587 		return -EADDRNOTAVAIL;
1588 
1589 	if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1590 		netdev_info(netdev, "already using mac address %pM\n",
1591 			    addr->sa_data);
1592 		return 0;
1593 	}
1594 
1595 	if (test_bit(__I40E_DOWN, pf->state) ||
1596 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1597 		return -EADDRNOTAVAIL;
1598 
1599 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1600 		netdev_info(netdev, "returning to hw mac address %pM\n",
1601 			    hw->mac.addr);
1602 	else
1603 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1604 
1605 	/* Copy the address first, so that we avoid a possible race with
1606 	 * .set_rx_mode().
1607 	 * - Remove old address from MAC filter
1608 	 * - Copy new address
1609 	 * - Add new address to MAC filter
1610 	 */
1611 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1612 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1613 	ether_addr_copy(netdev->dev_addr, addr->sa_data);
1614 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1615 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1616 
1617 	if (vsi->type == I40E_VSI_MAIN) {
1618 		i40e_status ret;
1619 
1620 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1621 						addr->sa_data, NULL);
1622 		if (ret)
1623 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1624 				    i40e_stat_str(hw, ret),
1625 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1626 	}
1627 
1628 	/* schedule our worker thread which will take care of
1629 	 * applying the new filter changes
1630 	 */
1631 	i40e_service_event_schedule(pf);
1632 	return 0;
1633 }
1634 
1635 /**
1636  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1637  * @vsi: vsi structure
1638  * @seed: RSS hash seed
1639  * @lut: pointer to lookup table of lut_size
1640  * @lut_size: size of the lookup table
1641  **/
i40e_config_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)1642 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1643 			      u8 *lut, u16 lut_size)
1644 {
1645 	struct i40e_pf *pf = vsi->back;
1646 	struct i40e_hw *hw = &pf->hw;
1647 	int ret = 0;
1648 
1649 	if (seed) {
1650 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1651 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1652 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1653 		if (ret) {
1654 			dev_info(&pf->pdev->dev,
1655 				 "Cannot set RSS key, err %s aq_err %s\n",
1656 				 i40e_stat_str(hw, ret),
1657 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1658 			return ret;
1659 		}
1660 	}
1661 	if (lut) {
1662 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1663 
1664 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1665 		if (ret) {
1666 			dev_info(&pf->pdev->dev,
1667 				 "Cannot set RSS lut, err %s aq_err %s\n",
1668 				 i40e_stat_str(hw, ret),
1669 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1670 			return ret;
1671 		}
1672 	}
1673 	return ret;
1674 }
1675 
1676 /**
1677  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1678  * @vsi: VSI structure
1679  **/
i40e_vsi_config_rss(struct i40e_vsi * vsi)1680 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1681 {
1682 	struct i40e_pf *pf = vsi->back;
1683 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1684 	u8 *lut;
1685 	int ret;
1686 
1687 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1688 		return 0;
1689 	if (!vsi->rss_size)
1690 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1691 				      vsi->num_queue_pairs);
1692 	if (!vsi->rss_size)
1693 		return -EINVAL;
1694 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1695 	if (!lut)
1696 		return -ENOMEM;
1697 
1698 	/* Use the user configured hash keys and lookup table if there is one,
1699 	 * otherwise use default
1700 	 */
1701 	if (vsi->rss_lut_user)
1702 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1703 	else
1704 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1705 	if (vsi->rss_hkey_user)
1706 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1707 	else
1708 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1709 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1710 	kfree(lut);
1711 	return ret;
1712 }
1713 
1714 /**
1715  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1716  * @vsi: the VSI being configured,
1717  * @ctxt: VSI context structure
1718  * @enabled_tc: number of traffic classes to enable
1719  *
1720  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1721  **/
i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc)1722 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1723 					   struct i40e_vsi_context *ctxt,
1724 					   u8 enabled_tc)
1725 {
1726 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1727 	int i, override_q, pow, num_qps, ret;
1728 	u8 netdev_tc = 0, offset = 0;
1729 
1730 	if (vsi->type != I40E_VSI_MAIN)
1731 		return -EINVAL;
1732 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1733 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1734 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1735 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1736 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1737 
1738 	/* find the next higher power-of-2 of num queue pairs */
1739 	pow = ilog2(num_qps);
1740 	if (!is_power_of_2(num_qps))
1741 		pow++;
1742 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1743 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1744 
1745 	/* Setup queue offset/count for all TCs for given VSI */
1746 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1747 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1748 		/* See if the given TC is enabled for the given VSI */
1749 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1750 			offset = vsi->mqprio_qopt.qopt.offset[i];
1751 			qcount = vsi->mqprio_qopt.qopt.count[i];
1752 			if (qcount > max_qcount)
1753 				max_qcount = qcount;
1754 			vsi->tc_config.tc_info[i].qoffset = offset;
1755 			vsi->tc_config.tc_info[i].qcount = qcount;
1756 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1757 		} else {
1758 			/* TC is not enabled so set the offset to
1759 			 * default queue and allocate one queue
1760 			 * for the given TC.
1761 			 */
1762 			vsi->tc_config.tc_info[i].qoffset = 0;
1763 			vsi->tc_config.tc_info[i].qcount = 1;
1764 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1765 		}
1766 	}
1767 
1768 	/* Set actual Tx/Rx queue pairs */
1769 	vsi->num_queue_pairs = offset + qcount;
1770 
1771 	/* Setup queue TC[0].qmap for given VSI context */
1772 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1773 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1774 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1775 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1776 
1777 	/* Reconfigure RSS for main VSI with max queue count */
1778 	vsi->rss_size = max_qcount;
1779 	ret = i40e_vsi_config_rss(vsi);
1780 	if (ret) {
1781 		dev_info(&vsi->back->pdev->dev,
1782 			 "Failed to reconfig rss for num_queues (%u)\n",
1783 			 max_qcount);
1784 		return ret;
1785 	}
1786 	vsi->reconfig_rss = true;
1787 	dev_dbg(&vsi->back->pdev->dev,
1788 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1789 
1790 	/* Find queue count available for channel VSIs and starting offset
1791 	 * for channel VSIs
1792 	 */
1793 	override_q = vsi->mqprio_qopt.qopt.count[0];
1794 	if (override_q && override_q < vsi->num_queue_pairs) {
1795 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1796 		vsi->next_base_queue = override_q;
1797 	}
1798 	return 0;
1799 }
1800 
1801 /**
1802  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1803  * @vsi: the VSI being setup
1804  * @ctxt: VSI context structure
1805  * @enabled_tc: Enabled TCs bitmap
1806  * @is_add: True if called before Add VSI
1807  *
1808  * Setup VSI queue mapping for enabled traffic classes.
1809  **/
i40e_vsi_setup_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt,u8 enabled_tc,bool is_add)1810 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1811 				     struct i40e_vsi_context *ctxt,
1812 				     u8 enabled_tc,
1813 				     bool is_add)
1814 {
1815 	struct i40e_pf *pf = vsi->back;
1816 	u16 num_tc_qps = 0;
1817 	u16 sections = 0;
1818 	u8 netdev_tc = 0;
1819 	u16 numtc = 1;
1820 	u16 qcount;
1821 	u8 offset;
1822 	u16 qmap;
1823 	int i;
1824 
1825 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1826 	offset = 0;
1827 	/* zero out queue mapping, it will get updated on the end of the function */
1828 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
1829 
1830 	if (vsi->type == I40E_VSI_MAIN) {
1831 		/* This code helps add more queue to the VSI if we have
1832 		 * more cores than RSS can support, the higher cores will
1833 		 * be served by ATR or other filters. Furthermore, the
1834 		 * non-zero req_queue_pairs says that user requested a new
1835 		 * queue count via ethtool's set_channels, so use this
1836 		 * value for queues distribution across traffic classes
1837 		 */
1838 		if (vsi->req_queue_pairs > 0)
1839 			vsi->num_queue_pairs = vsi->req_queue_pairs;
1840 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1841 			vsi->num_queue_pairs = pf->num_lan_msix;
1842 	}
1843 
1844 	/* Number of queues per enabled TC */
1845 	if (vsi->type == I40E_VSI_MAIN ||
1846 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
1847 		num_tc_qps = vsi->num_queue_pairs;
1848 	else
1849 		num_tc_qps = vsi->alloc_queue_pairs;
1850 
1851 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1852 		/* Find numtc from enabled TC bitmap */
1853 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1854 			if (enabled_tc & BIT(i)) /* TC is enabled */
1855 				numtc++;
1856 		}
1857 		if (!numtc) {
1858 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1859 			numtc = 1;
1860 		}
1861 		num_tc_qps = num_tc_qps / numtc;
1862 		num_tc_qps = min_t(int, num_tc_qps,
1863 				   i40e_pf_get_max_q_per_tc(pf));
1864 	}
1865 
1866 	vsi->tc_config.numtc = numtc;
1867 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1868 
1869 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
1870 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
1871 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
1872 
1873 	/* Setup queue offset/count for all TCs for given VSI */
1874 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1875 		/* See if the given TC is enabled for the given VSI */
1876 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1877 			/* TC is enabled */
1878 			int pow, num_qps;
1879 
1880 			switch (vsi->type) {
1881 			case I40E_VSI_MAIN:
1882 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
1883 				    I40E_FLAG_FD_ATR_ENABLED)) ||
1884 				    vsi->tc_config.enabled_tc != 1) {
1885 					qcount = min_t(int, pf->alloc_rss_size,
1886 						       num_tc_qps);
1887 					break;
1888 				}
1889 				fallthrough;
1890 			case I40E_VSI_FDIR:
1891 			case I40E_VSI_SRIOV:
1892 			case I40E_VSI_VMDQ2:
1893 			default:
1894 				qcount = num_tc_qps;
1895 				WARN_ON(i != 0);
1896 				break;
1897 			}
1898 			vsi->tc_config.tc_info[i].qoffset = offset;
1899 			vsi->tc_config.tc_info[i].qcount = qcount;
1900 
1901 			/* find the next higher power-of-2 of num queue pairs */
1902 			num_qps = qcount;
1903 			pow = 0;
1904 			while (num_qps && (BIT_ULL(pow) < qcount)) {
1905 				pow++;
1906 				num_qps >>= 1;
1907 			}
1908 
1909 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1910 			qmap =
1911 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1912 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1913 
1914 			offset += qcount;
1915 		} else {
1916 			/* TC is not enabled so set the offset to
1917 			 * default queue and allocate one queue
1918 			 * for the given TC.
1919 			 */
1920 			vsi->tc_config.tc_info[i].qoffset = 0;
1921 			vsi->tc_config.tc_info[i].qcount = 1;
1922 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1923 
1924 			qmap = 0;
1925 		}
1926 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1927 	}
1928 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
1929 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
1930 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
1931 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
1932 		vsi->num_queue_pairs = offset;
1933 
1934 	/* Scheduler section valid can only be set for ADD VSI */
1935 	if (is_add) {
1936 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1937 
1938 		ctxt->info.up_enable_bits = enabled_tc;
1939 	}
1940 	if (vsi->type == I40E_VSI_SRIOV) {
1941 		ctxt->info.mapping_flags |=
1942 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1943 		for (i = 0; i < vsi->num_queue_pairs; i++)
1944 			ctxt->info.queue_mapping[i] =
1945 					       cpu_to_le16(vsi->base_queue + i);
1946 	} else {
1947 		ctxt->info.mapping_flags |=
1948 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1949 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1950 	}
1951 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1952 }
1953 
1954 /**
1955  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
1956  * @netdev: the netdevice
1957  * @addr: address to add
1958  *
1959  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
1960  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1961  */
i40e_addr_sync(struct net_device * netdev,const u8 * addr)1962 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
1963 {
1964 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1965 	struct i40e_vsi *vsi = np->vsi;
1966 
1967 	if (i40e_add_mac_filter(vsi, addr))
1968 		return 0;
1969 	else
1970 		return -ENOMEM;
1971 }
1972 
1973 /**
1974  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
1975  * @netdev: the netdevice
1976  * @addr: address to add
1977  *
1978  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
1979  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
1980  */
i40e_addr_unsync(struct net_device * netdev,const u8 * addr)1981 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
1982 {
1983 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1984 	struct i40e_vsi *vsi = np->vsi;
1985 
1986 	/* Under some circumstances, we might receive a request to delete
1987 	 * our own device address from our uc list. Because we store the
1988 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
1989 	 * such requests and not delete our device address from this list.
1990 	 */
1991 	if (ether_addr_equal(addr, netdev->dev_addr))
1992 		return 0;
1993 
1994 	i40e_del_mac_filter(vsi, addr);
1995 
1996 	return 0;
1997 }
1998 
1999 /**
2000  * i40e_set_rx_mode - NDO callback to set the netdev filters
2001  * @netdev: network interface device structure
2002  **/
i40e_set_rx_mode(struct net_device * netdev)2003 static void i40e_set_rx_mode(struct net_device *netdev)
2004 {
2005 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2006 	struct i40e_vsi *vsi = np->vsi;
2007 
2008 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2009 
2010 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2011 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2012 
2013 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2014 
2015 	/* check for other flag changes */
2016 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2017 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2018 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2019 	}
2020 }
2021 
2022 /**
2023  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2024  * @vsi: Pointer to VSI struct
2025  * @from: Pointer to list which contains MAC filter entries - changes to
2026  *        those entries needs to be undone.
2027  *
2028  * MAC filter entries from this list were slated for deletion.
2029  **/
i40e_undo_del_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2030 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2031 					 struct hlist_head *from)
2032 {
2033 	struct i40e_mac_filter *f;
2034 	struct hlist_node *h;
2035 
2036 	hlist_for_each_entry_safe(f, h, from, hlist) {
2037 		u64 key = i40e_addr_to_hkey(f->macaddr);
2038 
2039 		/* Move the element back into MAC filter list*/
2040 		hlist_del(&f->hlist);
2041 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2042 	}
2043 }
2044 
2045 /**
2046  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2047  * @vsi: Pointer to vsi struct
2048  * @from: Pointer to list which contains MAC filter entries - changes to
2049  *        those entries needs to be undone.
2050  *
2051  * MAC filter entries from this list were slated for addition.
2052  **/
i40e_undo_add_filter_entries(struct i40e_vsi * vsi,struct hlist_head * from)2053 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2054 					 struct hlist_head *from)
2055 {
2056 	struct i40e_new_mac_filter *new;
2057 	struct hlist_node *h;
2058 
2059 	hlist_for_each_entry_safe(new, h, from, hlist) {
2060 		/* We can simply free the wrapper structure */
2061 		hlist_del(&new->hlist);
2062 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2063 		kfree(new);
2064 	}
2065 }
2066 
2067 /**
2068  * i40e_next_entry - Get the next non-broadcast filter from a list
2069  * @next: pointer to filter in list
2070  *
2071  * Returns the next non-broadcast filter in the list. Required so that we
2072  * ignore broadcast filters within the list, since these are not handled via
2073  * the normal firmware update path.
2074  */
2075 static
i40e_next_filter(struct i40e_new_mac_filter * next)2076 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2077 {
2078 	hlist_for_each_entry_continue(next, hlist) {
2079 		if (!is_broadcast_ether_addr(next->f->macaddr))
2080 			return next;
2081 	}
2082 
2083 	return NULL;
2084 }
2085 
2086 /**
2087  * i40e_update_filter_state - Update filter state based on return data
2088  * from firmware
2089  * @count: Number of filters added
2090  * @add_list: return data from fw
2091  * @add_head: pointer to first filter in current batch
2092  *
2093  * MAC filter entries from list were slated to be added to device. Returns
2094  * number of successful filters. Note that 0 does NOT mean success!
2095  **/
2096 static int
i40e_update_filter_state(int count,struct i40e_aqc_add_macvlan_element_data * add_list,struct i40e_new_mac_filter * add_head)2097 i40e_update_filter_state(int count,
2098 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2099 			 struct i40e_new_mac_filter *add_head)
2100 {
2101 	int retval = 0;
2102 	int i;
2103 
2104 	for (i = 0; i < count; i++) {
2105 		/* Always check status of each filter. We don't need to check
2106 		 * the firmware return status because we pre-set the filter
2107 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2108 		 * request to the adminq. Thus, if it no longer matches then
2109 		 * we know the filter is active.
2110 		 */
2111 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2112 			add_head->state = I40E_FILTER_FAILED;
2113 		} else {
2114 			add_head->state = I40E_FILTER_ACTIVE;
2115 			retval++;
2116 		}
2117 
2118 		add_head = i40e_next_filter(add_head);
2119 		if (!add_head)
2120 			break;
2121 	}
2122 
2123 	return retval;
2124 }
2125 
2126 /**
2127  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2128  * @vsi: ptr to the VSI
2129  * @vsi_name: name to display in messages
2130  * @list: the list of filters to send to firmware
2131  * @num_del: the number of filters to delete
2132  * @retval: Set to -EIO on failure to delete
2133  *
2134  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2135  * *retval instead of a return value so that success does not force ret_val to
2136  * be set to 0. This ensures that a sequence of calls to this function
2137  * preserve the previous value of *retval on successful delete.
2138  */
2139 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)2140 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2141 			  struct i40e_aqc_remove_macvlan_element_data *list,
2142 			  int num_del, int *retval)
2143 {
2144 	struct i40e_hw *hw = &vsi->back->hw;
2145 	i40e_status aq_ret;
2146 	int aq_err;
2147 
2148 	aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
2149 	aq_err = hw->aq.asq_last_status;
2150 
2151 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2152 	if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
2153 		*retval = -EIO;
2154 		dev_info(&vsi->back->pdev->dev,
2155 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2156 			 vsi_name, i40e_stat_str(hw, aq_ret),
2157 			 i40e_aq_str(hw, aq_err));
2158 	}
2159 }
2160 
2161 /**
2162  * i40e_aqc_add_filters - Request firmware to add a set of filters
2163  * @vsi: ptr to the VSI
2164  * @vsi_name: name to display in messages
2165  * @list: the list of filters to send to firmware
2166  * @add_head: Position in the add hlist
2167  * @num_add: the number of filters to add
2168  *
2169  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2170  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2171  * space for more filters.
2172  */
2173 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)2174 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2175 			  struct i40e_aqc_add_macvlan_element_data *list,
2176 			  struct i40e_new_mac_filter *add_head,
2177 			  int num_add)
2178 {
2179 	struct i40e_hw *hw = &vsi->back->hw;
2180 	int aq_err, fcnt;
2181 
2182 	i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
2183 	aq_err = hw->aq.asq_last_status;
2184 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2185 
2186 	if (fcnt != num_add) {
2187 		if (vsi->type == I40E_VSI_MAIN) {
2188 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2189 			dev_warn(&vsi->back->pdev->dev,
2190 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2191 				 i40e_aq_str(hw, aq_err), vsi_name);
2192 		} else if (vsi->type == I40E_VSI_SRIOV ||
2193 			   vsi->type == I40E_VSI_VMDQ1 ||
2194 			   vsi->type == I40E_VSI_VMDQ2) {
2195 			dev_warn(&vsi->back->pdev->dev,
2196 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2197 				 i40e_aq_str(hw, aq_err), vsi_name, vsi_name);
2198 		} else {
2199 			dev_warn(&vsi->back->pdev->dev,
2200 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2201 				 i40e_aq_str(hw, aq_err), vsi_name, vsi->type);
2202 		}
2203 	}
2204 }
2205 
2206 /**
2207  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2208  * @vsi: pointer to the VSI
2209  * @vsi_name: the VSI name
2210  * @f: filter data
2211  *
2212  * This function sets or clears the promiscuous broadcast flags for VLAN
2213  * filters in order to properly receive broadcast frames. Assumes that only
2214  * broadcast filters are passed.
2215  *
2216  * Returns status indicating success or failure;
2217  **/
2218 static i40e_status
i40e_aqc_broadcast_filter(struct i40e_vsi * vsi,const char * vsi_name,struct i40e_mac_filter * f)2219 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2220 			  struct i40e_mac_filter *f)
2221 {
2222 	bool enable = f->state == I40E_FILTER_NEW;
2223 	struct i40e_hw *hw = &vsi->back->hw;
2224 	i40e_status aq_ret;
2225 
2226 	if (f->vlan == I40E_VLAN_ANY) {
2227 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2228 						   vsi->seid,
2229 						   enable,
2230 						   NULL);
2231 	} else {
2232 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2233 							    vsi->seid,
2234 							    enable,
2235 							    f->vlan,
2236 							    NULL);
2237 	}
2238 
2239 	if (aq_ret) {
2240 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2241 		dev_warn(&vsi->back->pdev->dev,
2242 			 "Error %s, forcing overflow promiscuous on %s\n",
2243 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2244 			 vsi_name);
2245 	}
2246 
2247 	return aq_ret;
2248 }
2249 
2250 /**
2251  * i40e_set_promiscuous - set promiscuous mode
2252  * @pf: board private structure
2253  * @promisc: promisc on or off
2254  *
2255  * There are different ways of setting promiscuous mode on a PF depending on
2256  * what state/environment we're in.  This identifies and sets it appropriately.
2257  * Returns 0 on success.
2258  **/
i40e_set_promiscuous(struct i40e_pf * pf,bool promisc)2259 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2260 {
2261 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2262 	struct i40e_hw *hw = &pf->hw;
2263 	i40e_status aq_ret;
2264 
2265 	if (vsi->type == I40E_VSI_MAIN &&
2266 	    pf->lan_veb != I40E_NO_VEB &&
2267 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2268 		/* set defport ON for Main VSI instead of true promisc
2269 		 * this way we will get all unicast/multicast and VLAN
2270 		 * promisc behavior but will not get VF or VMDq traffic
2271 		 * replicated on the Main VSI.
2272 		 */
2273 		if (promisc)
2274 			aq_ret = i40e_aq_set_default_vsi(hw,
2275 							 vsi->seid,
2276 							 NULL);
2277 		else
2278 			aq_ret = i40e_aq_clear_default_vsi(hw,
2279 							   vsi->seid,
2280 							   NULL);
2281 		if (aq_ret) {
2282 			dev_info(&pf->pdev->dev,
2283 				 "Set default VSI failed, err %s, aq_err %s\n",
2284 				 i40e_stat_str(hw, aq_ret),
2285 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2286 		}
2287 	} else {
2288 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2289 						  hw,
2290 						  vsi->seid,
2291 						  promisc, NULL,
2292 						  true);
2293 		if (aq_ret) {
2294 			dev_info(&pf->pdev->dev,
2295 				 "set unicast promisc failed, err %s, aq_err %s\n",
2296 				 i40e_stat_str(hw, aq_ret),
2297 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2298 		}
2299 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2300 						  hw,
2301 						  vsi->seid,
2302 						  promisc, NULL);
2303 		if (aq_ret) {
2304 			dev_info(&pf->pdev->dev,
2305 				 "set multicast promisc failed, err %s, aq_err %s\n",
2306 				 i40e_stat_str(hw, aq_ret),
2307 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2308 		}
2309 	}
2310 
2311 	if (!aq_ret)
2312 		pf->cur_promisc = promisc;
2313 
2314 	return aq_ret;
2315 }
2316 
2317 /**
2318  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2319  * @vsi: ptr to the VSI
2320  *
2321  * Push any outstanding VSI filter changes through the AdminQ.
2322  *
2323  * Returns 0 or error value
2324  **/
i40e_sync_vsi_filters(struct i40e_vsi * vsi)2325 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2326 {
2327 	struct hlist_head tmp_add_list, tmp_del_list;
2328 	struct i40e_mac_filter *f;
2329 	struct i40e_new_mac_filter *new, *add_head = NULL;
2330 	struct i40e_hw *hw = &vsi->back->hw;
2331 	bool old_overflow, new_overflow;
2332 	unsigned int failed_filters = 0;
2333 	unsigned int vlan_filters = 0;
2334 	char vsi_name[16] = "PF";
2335 	int filter_list_len = 0;
2336 	i40e_status aq_ret = 0;
2337 	u32 changed_flags = 0;
2338 	struct hlist_node *h;
2339 	struct i40e_pf *pf;
2340 	int num_add = 0;
2341 	int num_del = 0;
2342 	int retval = 0;
2343 	u16 cmd_flags;
2344 	int list_size;
2345 	int bkt;
2346 
2347 	/* empty array typed pointers, kcalloc later */
2348 	struct i40e_aqc_add_macvlan_element_data *add_list;
2349 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2350 
2351 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2352 		usleep_range(1000, 2000);
2353 	pf = vsi->back;
2354 
2355 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2356 
2357 	if (vsi->netdev) {
2358 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2359 		vsi->current_netdev_flags = vsi->netdev->flags;
2360 	}
2361 
2362 	INIT_HLIST_HEAD(&tmp_add_list);
2363 	INIT_HLIST_HEAD(&tmp_del_list);
2364 
2365 	if (vsi->type == I40E_VSI_SRIOV)
2366 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2367 	else if (vsi->type != I40E_VSI_MAIN)
2368 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2369 
2370 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2371 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2372 
2373 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2374 		/* Create a list of filters to delete. */
2375 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2376 			if (f->state == I40E_FILTER_REMOVE) {
2377 				/* Move the element into temporary del_list */
2378 				hash_del(&f->hlist);
2379 				hlist_add_head(&f->hlist, &tmp_del_list);
2380 
2381 				/* Avoid counting removed filters */
2382 				continue;
2383 			}
2384 			if (f->state == I40E_FILTER_NEW) {
2385 				/* Create a temporary i40e_new_mac_filter */
2386 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2387 				if (!new)
2388 					goto err_no_memory_locked;
2389 
2390 				/* Store pointer to the real filter */
2391 				new->f = f;
2392 				new->state = f->state;
2393 
2394 				/* Add it to the hash list */
2395 				hlist_add_head(&new->hlist, &tmp_add_list);
2396 			}
2397 
2398 			/* Count the number of active (current and new) VLAN
2399 			 * filters we have now. Does not count filters which
2400 			 * are marked for deletion.
2401 			 */
2402 			if (f->vlan > 0)
2403 				vlan_filters++;
2404 		}
2405 
2406 		retval = i40e_correct_mac_vlan_filters(vsi,
2407 						       &tmp_add_list,
2408 						       &tmp_del_list,
2409 						       vlan_filters);
2410 
2411 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2412 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2413 
2414 		if (retval)
2415 			goto err_no_memory_locked;
2416 
2417 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2418 	}
2419 
2420 	/* Now process 'del_list' outside the lock */
2421 	if (!hlist_empty(&tmp_del_list)) {
2422 		filter_list_len = hw->aq.asq_buf_size /
2423 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2424 		list_size = filter_list_len *
2425 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2426 		del_list = kzalloc(list_size, GFP_ATOMIC);
2427 		if (!del_list)
2428 			goto err_no_memory;
2429 
2430 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2431 			cmd_flags = 0;
2432 
2433 			/* handle broadcast filters by updating the broadcast
2434 			 * promiscuous flag and release filter list.
2435 			 */
2436 			if (is_broadcast_ether_addr(f->macaddr)) {
2437 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2438 
2439 				hlist_del(&f->hlist);
2440 				kfree(f);
2441 				continue;
2442 			}
2443 
2444 			/* add to delete list */
2445 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2446 			if (f->vlan == I40E_VLAN_ANY) {
2447 				del_list[num_del].vlan_tag = 0;
2448 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2449 			} else {
2450 				del_list[num_del].vlan_tag =
2451 					cpu_to_le16((u16)(f->vlan));
2452 			}
2453 
2454 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2455 			del_list[num_del].flags = cmd_flags;
2456 			num_del++;
2457 
2458 			/* flush a full buffer */
2459 			if (num_del == filter_list_len) {
2460 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2461 						     num_del, &retval);
2462 				memset(del_list, 0, list_size);
2463 				num_del = 0;
2464 			}
2465 			/* Release memory for MAC filter entries which were
2466 			 * synced up with HW.
2467 			 */
2468 			hlist_del(&f->hlist);
2469 			kfree(f);
2470 		}
2471 
2472 		if (num_del) {
2473 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2474 					     num_del, &retval);
2475 		}
2476 
2477 		kfree(del_list);
2478 		del_list = NULL;
2479 	}
2480 
2481 	if (!hlist_empty(&tmp_add_list)) {
2482 		/* Do all the adds now. */
2483 		filter_list_len = hw->aq.asq_buf_size /
2484 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2485 		list_size = filter_list_len *
2486 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2487 		add_list = kzalloc(list_size, GFP_ATOMIC);
2488 		if (!add_list)
2489 			goto err_no_memory;
2490 
2491 		num_add = 0;
2492 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2493 			/* handle broadcast filters by updating the broadcast
2494 			 * promiscuous flag instead of adding a MAC filter.
2495 			 */
2496 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2497 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2498 							      new->f))
2499 					new->state = I40E_FILTER_FAILED;
2500 				else
2501 					new->state = I40E_FILTER_ACTIVE;
2502 				continue;
2503 			}
2504 
2505 			/* add to add array */
2506 			if (num_add == 0)
2507 				add_head = new;
2508 			cmd_flags = 0;
2509 			ether_addr_copy(add_list[num_add].mac_addr,
2510 					new->f->macaddr);
2511 			if (new->f->vlan == I40E_VLAN_ANY) {
2512 				add_list[num_add].vlan_tag = 0;
2513 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2514 			} else {
2515 				add_list[num_add].vlan_tag =
2516 					cpu_to_le16((u16)(new->f->vlan));
2517 			}
2518 			add_list[num_add].queue_number = 0;
2519 			/* set invalid match method for later detection */
2520 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2521 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2522 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2523 			num_add++;
2524 
2525 			/* flush a full buffer */
2526 			if (num_add == filter_list_len) {
2527 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2528 						     add_head, num_add);
2529 				memset(add_list, 0, list_size);
2530 				num_add = 0;
2531 			}
2532 		}
2533 		if (num_add) {
2534 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2535 					     num_add);
2536 		}
2537 		/* Now move all of the filters from the temp add list back to
2538 		 * the VSI's list.
2539 		 */
2540 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2541 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2542 			/* Only update the state if we're still NEW */
2543 			if (new->f->state == I40E_FILTER_NEW)
2544 				new->f->state = new->state;
2545 			hlist_del(&new->hlist);
2546 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2547 			kfree(new);
2548 		}
2549 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2550 		kfree(add_list);
2551 		add_list = NULL;
2552 	}
2553 
2554 	/* Determine the number of active and failed filters. */
2555 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2556 	vsi->active_filters = 0;
2557 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2558 		if (f->state == I40E_FILTER_ACTIVE)
2559 			vsi->active_filters++;
2560 		else if (f->state == I40E_FILTER_FAILED)
2561 			failed_filters++;
2562 	}
2563 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2564 
2565 	/* Check if we are able to exit overflow promiscuous mode. We can
2566 	 * safely exit if we didn't just enter, we no longer have any failed
2567 	 * filters, and we have reduced filters below the threshold value.
2568 	 */
2569 	if (old_overflow && !failed_filters &&
2570 	    vsi->active_filters < vsi->promisc_threshold) {
2571 		dev_info(&pf->pdev->dev,
2572 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2573 			 vsi_name);
2574 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2575 		vsi->promisc_threshold = 0;
2576 	}
2577 
2578 	/* if the VF is not trusted do not do promisc */
2579 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2580 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2581 		goto out;
2582 	}
2583 
2584 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2585 
2586 	/* If we are entering overflow promiscuous, we need to calculate a new
2587 	 * threshold for when we are safe to exit
2588 	 */
2589 	if (!old_overflow && new_overflow)
2590 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2591 
2592 	/* check for changes in promiscuous modes */
2593 	if (changed_flags & IFF_ALLMULTI) {
2594 		bool cur_multipromisc;
2595 
2596 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2597 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2598 							       vsi->seid,
2599 							       cur_multipromisc,
2600 							       NULL);
2601 		if (aq_ret) {
2602 			retval = i40e_aq_rc_to_posix(aq_ret,
2603 						     hw->aq.asq_last_status);
2604 			dev_info(&pf->pdev->dev,
2605 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2606 				 vsi_name,
2607 				 i40e_stat_str(hw, aq_ret),
2608 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2609 		} else {
2610 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2611 				 cur_multipromisc ? "entering" : "leaving");
2612 		}
2613 	}
2614 
2615 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2616 		bool cur_promisc;
2617 
2618 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2619 			       new_overflow);
2620 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2621 		if (aq_ret) {
2622 			retval = i40e_aq_rc_to_posix(aq_ret,
2623 						     hw->aq.asq_last_status);
2624 			dev_info(&pf->pdev->dev,
2625 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2626 				 cur_promisc ? "on" : "off",
2627 				 vsi_name,
2628 				 i40e_stat_str(hw, aq_ret),
2629 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2630 		}
2631 	}
2632 out:
2633 	/* if something went wrong then set the changed flag so we try again */
2634 	if (retval)
2635 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2636 
2637 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2638 	return retval;
2639 
2640 err_no_memory:
2641 	/* Restore elements on the temporary add and delete lists */
2642 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2643 err_no_memory_locked:
2644 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2645 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2646 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2647 
2648 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2649 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2650 	return -ENOMEM;
2651 }
2652 
2653 /**
2654  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2655  * @pf: board private structure
2656  **/
i40e_sync_filters_subtask(struct i40e_pf * pf)2657 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2658 {
2659 	int v;
2660 
2661 	if (!pf)
2662 		return;
2663 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2664 		return;
2665 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2666 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2667 		return;
2668 	}
2669 
2670 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2671 		if (pf->vsi[v] &&
2672 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2673 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2674 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2675 
2676 			if (ret) {
2677 				/* come back and try again later */
2678 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2679 					pf->state);
2680 				break;
2681 			}
2682 		}
2683 	}
2684 }
2685 
2686 /**
2687  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2688  * @vsi: the vsi
2689  **/
i40e_max_xdp_frame_size(struct i40e_vsi * vsi)2690 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2691 {
2692 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2693 		return I40E_RXBUFFER_2048;
2694 	else
2695 		return I40E_RXBUFFER_3072;
2696 }
2697 
2698 /**
2699  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2700  * @netdev: network interface device structure
2701  * @new_mtu: new value for maximum frame size
2702  *
2703  * Returns 0 on success, negative on failure
2704  **/
i40e_change_mtu(struct net_device * netdev,int new_mtu)2705 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2706 {
2707 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2708 	struct i40e_vsi *vsi = np->vsi;
2709 	struct i40e_pf *pf = vsi->back;
2710 
2711 	if (i40e_enabled_xdp_vsi(vsi)) {
2712 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2713 
2714 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2715 			return -EINVAL;
2716 	}
2717 
2718 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2719 		   netdev->mtu, new_mtu);
2720 	netdev->mtu = new_mtu;
2721 	if (netif_running(netdev))
2722 		i40e_vsi_reinit_locked(vsi);
2723 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2724 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2725 	return 0;
2726 }
2727 
2728 /**
2729  * i40e_ioctl - Access the hwtstamp interface
2730  * @netdev: network interface device structure
2731  * @ifr: interface request data
2732  * @cmd: ioctl command
2733  **/
i40e_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)2734 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2735 {
2736 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2737 	struct i40e_pf *pf = np->vsi->back;
2738 
2739 	switch (cmd) {
2740 	case SIOCGHWTSTAMP:
2741 		return i40e_ptp_get_ts_config(pf, ifr);
2742 	case SIOCSHWTSTAMP:
2743 		return i40e_ptp_set_ts_config(pf, ifr);
2744 	default:
2745 		return -EOPNOTSUPP;
2746 	}
2747 }
2748 
2749 /**
2750  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2751  * @vsi: the vsi being adjusted
2752  **/
i40e_vlan_stripping_enable(struct i40e_vsi * vsi)2753 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2754 {
2755 	struct i40e_vsi_context ctxt;
2756 	i40e_status ret;
2757 
2758 	/* Don't modify stripping options if a port VLAN is active */
2759 	if (vsi->info.pvid)
2760 		return;
2761 
2762 	if ((vsi->info.valid_sections &
2763 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2764 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2765 		return;  /* already enabled */
2766 
2767 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2768 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2769 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2770 
2771 	ctxt.seid = vsi->seid;
2772 	ctxt.info = vsi->info;
2773 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2774 	if (ret) {
2775 		dev_info(&vsi->back->pdev->dev,
2776 			 "update vlan stripping failed, err %s aq_err %s\n",
2777 			 i40e_stat_str(&vsi->back->hw, ret),
2778 			 i40e_aq_str(&vsi->back->hw,
2779 				     vsi->back->hw.aq.asq_last_status));
2780 	}
2781 }
2782 
2783 /**
2784  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2785  * @vsi: the vsi being adjusted
2786  **/
i40e_vlan_stripping_disable(struct i40e_vsi * vsi)2787 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2788 {
2789 	struct i40e_vsi_context ctxt;
2790 	i40e_status ret;
2791 
2792 	/* Don't modify stripping options if a port VLAN is active */
2793 	if (vsi->info.pvid)
2794 		return;
2795 
2796 	if ((vsi->info.valid_sections &
2797 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2798 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
2799 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
2800 		return;  /* already disabled */
2801 
2802 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2803 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2804 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
2805 
2806 	ctxt.seid = vsi->seid;
2807 	ctxt.info = vsi->info;
2808 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2809 	if (ret) {
2810 		dev_info(&vsi->back->pdev->dev,
2811 			 "update vlan stripping failed, err %s aq_err %s\n",
2812 			 i40e_stat_str(&vsi->back->hw, ret),
2813 			 i40e_aq_str(&vsi->back->hw,
2814 				     vsi->back->hw.aq.asq_last_status));
2815 	}
2816 }
2817 
2818 /**
2819  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
2820  * @vsi: the vsi being configured
2821  * @vid: vlan id to be added (0 = untagged only , -1 = any)
2822  *
2823  * This is a helper function for adding a new MAC/VLAN filter with the
2824  * specified VLAN for each existing MAC address already in the hash table.
2825  * This function does *not* perform any accounting to update filters based on
2826  * VLAN mode.
2827  *
2828  * NOTE: this function expects to be called while under the
2829  * mac_filter_hash_lock
2830  **/
i40e_add_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)2831 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2832 {
2833 	struct i40e_mac_filter *f, *add_f;
2834 	struct hlist_node *h;
2835 	int bkt;
2836 
2837 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2838 		if (f->state == I40E_FILTER_REMOVE)
2839 			continue;
2840 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
2841 		if (!add_f) {
2842 			dev_info(&vsi->back->pdev->dev,
2843 				 "Could not add vlan filter %d for %pM\n",
2844 				 vid, f->macaddr);
2845 			return -ENOMEM;
2846 		}
2847 	}
2848 
2849 	return 0;
2850 }
2851 
2852 /**
2853  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
2854  * @vsi: the VSI being configured
2855  * @vid: VLAN id to be added
2856  **/
i40e_vsi_add_vlan(struct i40e_vsi * vsi,u16 vid)2857 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
2858 {
2859 	int err;
2860 
2861 	if (vsi->info.pvid)
2862 		return -EINVAL;
2863 
2864 	/* The network stack will attempt to add VID=0, with the intention to
2865 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
2866 	 * these packets by default when configured to receive untagged
2867 	 * packets, so we don't need to add a filter for this case.
2868 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
2869 	 * receive *only* tagged traffic and stops receiving untagged traffic.
2870 	 * Thus, we do not want to actually add a filter for VID=0
2871 	 */
2872 	if (!vid)
2873 		return 0;
2874 
2875 	/* Locked once because all functions invoked below iterates list*/
2876 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2877 	err = i40e_add_vlan_all_mac(vsi, vid);
2878 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2879 	if (err)
2880 		return err;
2881 
2882 	/* schedule our worker thread which will take care of
2883 	 * applying the new filter changes
2884 	 */
2885 	i40e_service_event_schedule(vsi->back);
2886 	return 0;
2887 }
2888 
2889 /**
2890  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
2891  * @vsi: the vsi being configured
2892  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
2893  *
2894  * This function should be used to remove all VLAN filters which match the
2895  * given VID. It does not schedule the service event and does not take the
2896  * mac_filter_hash_lock so it may be combined with other operations under
2897  * a single invocation of the mac_filter_hash_lock.
2898  *
2899  * NOTE: this function expects to be called while under the
2900  * mac_filter_hash_lock
2901  */
i40e_rm_vlan_all_mac(struct i40e_vsi * vsi,s16 vid)2902 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
2903 {
2904 	struct i40e_mac_filter *f;
2905 	struct hlist_node *h;
2906 	int bkt;
2907 
2908 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2909 		if (f->vlan == vid)
2910 			__i40e_del_filter(vsi, f);
2911 	}
2912 }
2913 
2914 /**
2915  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
2916  * @vsi: the VSI being configured
2917  * @vid: VLAN id to be removed
2918  **/
i40e_vsi_kill_vlan(struct i40e_vsi * vsi,u16 vid)2919 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
2920 {
2921 	if (!vid || vsi->info.pvid)
2922 		return;
2923 
2924 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2925 	i40e_rm_vlan_all_mac(vsi, vid);
2926 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2927 
2928 	/* schedule our worker thread which will take care of
2929 	 * applying the new filter changes
2930 	 */
2931 	i40e_service_event_schedule(vsi->back);
2932 }
2933 
2934 /**
2935  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
2936  * @netdev: network interface to be adjusted
2937  * @proto: unused protocol value
2938  * @vid: vlan id to be added
2939  *
2940  * net_device_ops implementation for adding vlan ids
2941  **/
i40e_vlan_rx_add_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)2942 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
2943 				__always_unused __be16 proto, u16 vid)
2944 {
2945 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2946 	struct i40e_vsi *vsi = np->vsi;
2947 	int ret = 0;
2948 
2949 	if (vid >= VLAN_N_VID)
2950 		return -EINVAL;
2951 
2952 	ret = i40e_vsi_add_vlan(vsi, vid);
2953 	if (!ret)
2954 		set_bit(vid, vsi->active_vlans);
2955 
2956 	return ret;
2957 }
2958 
2959 /**
2960  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
2961  * @netdev: network interface to be adjusted
2962  * @proto: unused protocol value
2963  * @vid: vlan id to be added
2964  **/
i40e_vlan_rx_add_vid_up(struct net_device * netdev,__always_unused __be16 proto,u16 vid)2965 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
2966 				    __always_unused __be16 proto, u16 vid)
2967 {
2968 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2969 	struct i40e_vsi *vsi = np->vsi;
2970 
2971 	if (vid >= VLAN_N_VID)
2972 		return;
2973 	set_bit(vid, vsi->active_vlans);
2974 }
2975 
2976 /**
2977  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
2978  * @netdev: network interface to be adjusted
2979  * @proto: unused protocol value
2980  * @vid: vlan id to be removed
2981  *
2982  * net_device_ops implementation for removing vlan ids
2983  **/
i40e_vlan_rx_kill_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)2984 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
2985 				 __always_unused __be16 proto, u16 vid)
2986 {
2987 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2988 	struct i40e_vsi *vsi = np->vsi;
2989 
2990 	/* return code is ignored as there is nothing a user
2991 	 * can do about failure to remove and a log message was
2992 	 * already printed from the other function
2993 	 */
2994 	i40e_vsi_kill_vlan(vsi, vid);
2995 
2996 	clear_bit(vid, vsi->active_vlans);
2997 
2998 	return 0;
2999 }
3000 
3001 /**
3002  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3003  * @vsi: the vsi being brought back up
3004  **/
i40e_restore_vlan(struct i40e_vsi * vsi)3005 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3006 {
3007 	u16 vid;
3008 
3009 	if (!vsi->netdev)
3010 		return;
3011 
3012 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3013 		i40e_vlan_stripping_enable(vsi);
3014 	else
3015 		i40e_vlan_stripping_disable(vsi);
3016 
3017 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3018 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3019 					vid);
3020 }
3021 
3022 /**
3023  * i40e_vsi_add_pvid - Add pvid for the VSI
3024  * @vsi: the vsi being adjusted
3025  * @vid: the vlan id to set as a PVID
3026  **/
i40e_vsi_add_pvid(struct i40e_vsi * vsi,u16 vid)3027 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3028 {
3029 	struct i40e_vsi_context ctxt;
3030 	i40e_status ret;
3031 
3032 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3033 	vsi->info.pvid = cpu_to_le16(vid);
3034 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3035 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3036 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3037 
3038 	ctxt.seid = vsi->seid;
3039 	ctxt.info = vsi->info;
3040 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3041 	if (ret) {
3042 		dev_info(&vsi->back->pdev->dev,
3043 			 "add pvid failed, err %s aq_err %s\n",
3044 			 i40e_stat_str(&vsi->back->hw, ret),
3045 			 i40e_aq_str(&vsi->back->hw,
3046 				     vsi->back->hw.aq.asq_last_status));
3047 		return -ENOENT;
3048 	}
3049 
3050 	return 0;
3051 }
3052 
3053 /**
3054  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3055  * @vsi: the vsi being adjusted
3056  *
3057  * Just use the vlan_rx_register() service to put it back to normal
3058  **/
i40e_vsi_remove_pvid(struct i40e_vsi * vsi)3059 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3060 {
3061 	vsi->info.pvid = 0;
3062 
3063 	i40e_vlan_stripping_disable(vsi);
3064 }
3065 
3066 /**
3067  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3068  * @vsi: ptr to the VSI
3069  *
3070  * If this function returns with an error, then it's possible one or
3071  * more of the rings is populated (while the rest are not).  It is the
3072  * callers duty to clean those orphaned rings.
3073  *
3074  * Return 0 on success, negative on failure
3075  **/
i40e_vsi_setup_tx_resources(struct i40e_vsi * vsi)3076 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3077 {
3078 	int i, err = 0;
3079 
3080 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3081 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3082 
3083 	if (!i40e_enabled_xdp_vsi(vsi))
3084 		return err;
3085 
3086 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3087 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3088 
3089 	return err;
3090 }
3091 
3092 /**
3093  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3094  * @vsi: ptr to the VSI
3095  *
3096  * Free VSI's transmit software resources
3097  **/
i40e_vsi_free_tx_resources(struct i40e_vsi * vsi)3098 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3099 {
3100 	int i;
3101 
3102 	if (vsi->tx_rings) {
3103 		for (i = 0; i < vsi->num_queue_pairs; i++)
3104 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3105 				i40e_free_tx_resources(vsi->tx_rings[i]);
3106 	}
3107 
3108 	if (vsi->xdp_rings) {
3109 		for (i = 0; i < vsi->num_queue_pairs; i++)
3110 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3111 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3112 	}
3113 }
3114 
3115 /**
3116  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3117  * @vsi: ptr to the VSI
3118  *
3119  * If this function returns with an error, then it's possible one or
3120  * more of the rings is populated (while the rest are not).  It is the
3121  * callers duty to clean those orphaned rings.
3122  *
3123  * Return 0 on success, negative on failure
3124  **/
i40e_vsi_setup_rx_resources(struct i40e_vsi * vsi)3125 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3126 {
3127 	int i, err = 0;
3128 
3129 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3130 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3131 	return err;
3132 }
3133 
3134 /**
3135  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3136  * @vsi: ptr to the VSI
3137  *
3138  * Free all receive software resources
3139  **/
i40e_vsi_free_rx_resources(struct i40e_vsi * vsi)3140 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3141 {
3142 	int i;
3143 
3144 	if (!vsi->rx_rings)
3145 		return;
3146 
3147 	for (i = 0; i < vsi->num_queue_pairs; i++)
3148 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3149 			i40e_free_rx_resources(vsi->rx_rings[i]);
3150 }
3151 
3152 /**
3153  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3154  * @ring: The Tx ring to configure
3155  *
3156  * This enables/disables XPS for a given Tx descriptor ring
3157  * based on the TCs enabled for the VSI that ring belongs to.
3158  **/
i40e_config_xps_tx_ring(struct i40e_ring * ring)3159 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3160 {
3161 	int cpu;
3162 
3163 	if (!ring->q_vector || !ring->netdev || ring->ch)
3164 		return;
3165 
3166 	/* We only initialize XPS once, so as not to overwrite user settings */
3167 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3168 		return;
3169 
3170 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3171 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3172 			    ring->queue_index);
3173 }
3174 
3175 /**
3176  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3177  * @ring: The Tx or Rx ring
3178  *
3179  * Returns the AF_XDP buffer pool or NULL.
3180  **/
i40e_xsk_pool(struct i40e_ring * ring)3181 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3182 {
3183 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3184 	int qid = ring->queue_index;
3185 
3186 	if (ring_is_xdp(ring))
3187 		qid -= ring->vsi->alloc_queue_pairs;
3188 
3189 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3190 		return NULL;
3191 
3192 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3193 }
3194 
3195 /**
3196  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3197  * @ring: The Tx ring to configure
3198  *
3199  * Configure the Tx descriptor ring in the HMC context.
3200  **/
i40e_configure_tx_ring(struct i40e_ring * ring)3201 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3202 {
3203 	struct i40e_vsi *vsi = ring->vsi;
3204 	u16 pf_q = vsi->base_queue + ring->queue_index;
3205 	struct i40e_hw *hw = &vsi->back->hw;
3206 	struct i40e_hmc_obj_txq tx_ctx;
3207 	i40e_status err = 0;
3208 	u32 qtx_ctl = 0;
3209 
3210 	if (ring_is_xdp(ring))
3211 		ring->xsk_pool = i40e_xsk_pool(ring);
3212 
3213 	/* some ATR related tx ring init */
3214 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3215 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3216 		ring->atr_count = 0;
3217 	} else {
3218 		ring->atr_sample_rate = 0;
3219 	}
3220 
3221 	/* configure XPS */
3222 	i40e_config_xps_tx_ring(ring);
3223 
3224 	/* clear the context structure first */
3225 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3226 
3227 	tx_ctx.new_context = 1;
3228 	tx_ctx.base = (ring->dma / 128);
3229 	tx_ctx.qlen = ring->count;
3230 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3231 					       I40E_FLAG_FD_ATR_ENABLED));
3232 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3233 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3234 	if (vsi->type != I40E_VSI_FDIR)
3235 		tx_ctx.head_wb_ena = 1;
3236 	tx_ctx.head_wb_addr = ring->dma +
3237 			      (ring->count * sizeof(struct i40e_tx_desc));
3238 
3239 	/* As part of VSI creation/update, FW allocates certain
3240 	 * Tx arbitration queue sets for each TC enabled for
3241 	 * the VSI. The FW returns the handles to these queue
3242 	 * sets as part of the response buffer to Add VSI,
3243 	 * Update VSI, etc. AQ commands. It is expected that
3244 	 * these queue set handles be associated with the Tx
3245 	 * queues by the driver as part of the TX queue context
3246 	 * initialization. This has to be done regardless of
3247 	 * DCB as by default everything is mapped to TC0.
3248 	 */
3249 
3250 	if (ring->ch)
3251 		tx_ctx.rdylist =
3252 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3253 
3254 	else
3255 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3256 
3257 	tx_ctx.rdylist_act = 0;
3258 
3259 	/* clear the context in the HMC */
3260 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3261 	if (err) {
3262 		dev_info(&vsi->back->pdev->dev,
3263 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3264 			 ring->queue_index, pf_q, err);
3265 		return -ENOMEM;
3266 	}
3267 
3268 	/* set the context in the HMC */
3269 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3270 	if (err) {
3271 		dev_info(&vsi->back->pdev->dev,
3272 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3273 			 ring->queue_index, pf_q, err);
3274 		return -ENOMEM;
3275 	}
3276 
3277 	/* Now associate this queue with this PCI function */
3278 	if (ring->ch) {
3279 		if (ring->ch->type == I40E_VSI_VMDQ2)
3280 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3281 		else
3282 			return -EINVAL;
3283 
3284 		qtx_ctl |= (ring->ch->vsi_number <<
3285 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3286 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3287 	} else {
3288 		if (vsi->type == I40E_VSI_VMDQ2) {
3289 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3290 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3291 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3292 		} else {
3293 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3294 		}
3295 	}
3296 
3297 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3298 		    I40E_QTX_CTL_PF_INDX_MASK);
3299 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3300 	i40e_flush(hw);
3301 
3302 	/* cache tail off for easier writes later */
3303 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3304 
3305 	return 0;
3306 }
3307 
3308 /**
3309  * i40e_configure_rx_ring - Configure a receive ring context
3310  * @ring: The Rx ring to configure
3311  *
3312  * Configure the Rx descriptor ring in the HMC context.
3313  **/
i40e_configure_rx_ring(struct i40e_ring * ring)3314 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3315 {
3316 	struct i40e_vsi *vsi = ring->vsi;
3317 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3318 	u16 pf_q = vsi->base_queue + ring->queue_index;
3319 	struct i40e_hw *hw = &vsi->back->hw;
3320 	struct i40e_hmc_obj_rxq rx_ctx;
3321 	i40e_status err = 0;
3322 	bool ok;
3323 	int ret;
3324 
3325 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3326 
3327 	/* clear the context structure first */
3328 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3329 
3330 	if (ring->vsi->type == I40E_VSI_MAIN)
3331 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3332 
3333 	kfree(ring->rx_bi);
3334 	ring->xsk_pool = i40e_xsk_pool(ring);
3335 	if (ring->xsk_pool) {
3336 		ret = i40e_alloc_rx_bi_zc(ring);
3337 		if (ret)
3338 			return ret;
3339 		ring->rx_buf_len =
3340 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3341 		/* For AF_XDP ZC, we disallow packets to span on
3342 		 * multiple buffers, thus letting us skip that
3343 		 * handling in the fast-path.
3344 		 */
3345 		chain_len = 1;
3346 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3347 						 MEM_TYPE_XSK_BUFF_POOL,
3348 						 NULL);
3349 		if (ret)
3350 			return ret;
3351 		dev_info(&vsi->back->pdev->dev,
3352 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3353 			 ring->queue_index);
3354 
3355 	} else {
3356 		ret = i40e_alloc_rx_bi(ring);
3357 		if (ret)
3358 			return ret;
3359 		ring->rx_buf_len = vsi->rx_buf_len;
3360 		if (ring->vsi->type == I40E_VSI_MAIN) {
3361 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3362 							 MEM_TYPE_PAGE_SHARED,
3363 							 NULL);
3364 			if (ret)
3365 				return ret;
3366 		}
3367 	}
3368 
3369 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3370 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3371 
3372 	rx_ctx.base = (ring->dma / 128);
3373 	rx_ctx.qlen = ring->count;
3374 
3375 	/* use 16 byte descriptors */
3376 	rx_ctx.dsize = 0;
3377 
3378 	/* descriptor type is always zero
3379 	 * rx_ctx.dtype = 0;
3380 	 */
3381 	rx_ctx.hsplit_0 = 0;
3382 
3383 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3384 	if (hw->revision_id == 0)
3385 		rx_ctx.lrxqthresh = 0;
3386 	else
3387 		rx_ctx.lrxqthresh = 1;
3388 	rx_ctx.crcstrip = 1;
3389 	rx_ctx.l2tsel = 1;
3390 	/* this controls whether VLAN is stripped from inner headers */
3391 	rx_ctx.showiv = 0;
3392 	/* set the prefena field to 1 because the manual says to */
3393 	rx_ctx.prefena = 1;
3394 
3395 	/* clear the context in the HMC */
3396 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3397 	if (err) {
3398 		dev_info(&vsi->back->pdev->dev,
3399 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3400 			 ring->queue_index, pf_q, err);
3401 		return -ENOMEM;
3402 	}
3403 
3404 	/* set the context in the HMC */
3405 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3406 	if (err) {
3407 		dev_info(&vsi->back->pdev->dev,
3408 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3409 			 ring->queue_index, pf_q, err);
3410 		return -ENOMEM;
3411 	}
3412 
3413 	/* configure Rx buffer alignment */
3414 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3415 		clear_ring_build_skb_enabled(ring);
3416 	else
3417 		set_ring_build_skb_enabled(ring);
3418 
3419 	/* cache tail for quicker writes, and clear the reg before use */
3420 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3421 	writel(0, ring->tail);
3422 
3423 	if (ring->xsk_pool) {
3424 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3425 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3426 	} else {
3427 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3428 	}
3429 	if (!ok) {
3430 		/* Log this in case the user has forgotten to give the kernel
3431 		 * any buffers, even later in the application.
3432 		 */
3433 		dev_info(&vsi->back->pdev->dev,
3434 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3435 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3436 			 ring->queue_index, pf_q);
3437 	}
3438 
3439 	return 0;
3440 }
3441 
3442 /**
3443  * i40e_vsi_configure_tx - Configure the VSI for Tx
3444  * @vsi: VSI structure describing this set of rings and resources
3445  *
3446  * Configure the Tx VSI for operation.
3447  **/
i40e_vsi_configure_tx(struct i40e_vsi * vsi)3448 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3449 {
3450 	int err = 0;
3451 	u16 i;
3452 
3453 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3454 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3455 
3456 	if (err || !i40e_enabled_xdp_vsi(vsi))
3457 		return err;
3458 
3459 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3460 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3461 
3462 	return err;
3463 }
3464 
3465 /**
3466  * i40e_vsi_configure_rx - Configure the VSI for Rx
3467  * @vsi: the VSI being configured
3468  *
3469  * Configure the Rx VSI for operation.
3470  **/
i40e_vsi_configure_rx(struct i40e_vsi * vsi)3471 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3472 {
3473 	int err = 0;
3474 	u16 i;
3475 
3476 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3477 		vsi->max_frame = I40E_MAX_RXBUFFER;
3478 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3479 #if (PAGE_SIZE < 8192)
3480 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3481 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3482 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3483 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3484 #endif
3485 	} else {
3486 		vsi->max_frame = I40E_MAX_RXBUFFER;
3487 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3488 						       I40E_RXBUFFER_2048;
3489 	}
3490 
3491 	/* set up individual rings */
3492 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3493 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3494 
3495 	return err;
3496 }
3497 
3498 /**
3499  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3500  * @vsi: ptr to the VSI
3501  **/
i40e_vsi_config_dcb_rings(struct i40e_vsi * vsi)3502 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3503 {
3504 	struct i40e_ring *tx_ring, *rx_ring;
3505 	u16 qoffset, qcount;
3506 	int i, n;
3507 
3508 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3509 		/* Reset the TC information */
3510 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3511 			rx_ring = vsi->rx_rings[i];
3512 			tx_ring = vsi->tx_rings[i];
3513 			rx_ring->dcb_tc = 0;
3514 			tx_ring->dcb_tc = 0;
3515 		}
3516 		return;
3517 	}
3518 
3519 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3520 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3521 			continue;
3522 
3523 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3524 		qcount = vsi->tc_config.tc_info[n].qcount;
3525 		for (i = qoffset; i < (qoffset + qcount); i++) {
3526 			rx_ring = vsi->rx_rings[i];
3527 			tx_ring = vsi->tx_rings[i];
3528 			rx_ring->dcb_tc = n;
3529 			tx_ring->dcb_tc = n;
3530 		}
3531 	}
3532 }
3533 
3534 /**
3535  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3536  * @vsi: ptr to the VSI
3537  **/
i40e_set_vsi_rx_mode(struct i40e_vsi * vsi)3538 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3539 {
3540 	if (vsi->netdev)
3541 		i40e_set_rx_mode(vsi->netdev);
3542 }
3543 
3544 /**
3545  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3546  * @vsi: Pointer to the targeted VSI
3547  *
3548  * This function replays the hlist on the hw where all the SB Flow Director
3549  * filters were saved.
3550  **/
i40e_fdir_filter_restore(struct i40e_vsi * vsi)3551 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3552 {
3553 	struct i40e_fdir_filter *filter;
3554 	struct i40e_pf *pf = vsi->back;
3555 	struct hlist_node *node;
3556 
3557 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3558 		return;
3559 
3560 	/* Reset FDir counters as we're replaying all existing filters */
3561 	pf->fd_tcp4_filter_cnt = 0;
3562 	pf->fd_udp4_filter_cnt = 0;
3563 	pf->fd_sctp4_filter_cnt = 0;
3564 	pf->fd_ip4_filter_cnt = 0;
3565 
3566 	hlist_for_each_entry_safe(filter, node,
3567 				  &pf->fdir_filter_list, fdir_node) {
3568 		i40e_add_del_fdir(vsi, filter, true);
3569 	}
3570 }
3571 
3572 /**
3573  * i40e_vsi_configure - Set up the VSI for action
3574  * @vsi: the VSI being configured
3575  **/
i40e_vsi_configure(struct i40e_vsi * vsi)3576 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3577 {
3578 	int err;
3579 
3580 	i40e_set_vsi_rx_mode(vsi);
3581 	i40e_restore_vlan(vsi);
3582 	i40e_vsi_config_dcb_rings(vsi);
3583 	err = i40e_vsi_configure_tx(vsi);
3584 	if (!err)
3585 		err = i40e_vsi_configure_rx(vsi);
3586 
3587 	return err;
3588 }
3589 
3590 /**
3591  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3592  * @vsi: the VSI being configured
3593  **/
i40e_vsi_configure_msix(struct i40e_vsi * vsi)3594 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3595 {
3596 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3597 	struct i40e_pf *pf = vsi->back;
3598 	struct i40e_hw *hw = &pf->hw;
3599 	u16 vector;
3600 	int i, q;
3601 	u32 qp;
3602 
3603 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3604 	 * and PFINT_LNKLSTn registers, e.g.:
3605 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3606 	 */
3607 	qp = vsi->base_queue;
3608 	vector = vsi->base_vector;
3609 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3610 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3611 
3612 		q_vector->rx.next_update = jiffies + 1;
3613 		q_vector->rx.target_itr =
3614 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3615 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3616 		     q_vector->rx.target_itr >> 1);
3617 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3618 
3619 		q_vector->tx.next_update = jiffies + 1;
3620 		q_vector->tx.target_itr =
3621 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3622 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3623 		     q_vector->tx.target_itr >> 1);
3624 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3625 
3626 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3627 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3628 
3629 		/* Linked list for the queuepairs assigned to this vector */
3630 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3631 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3632 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3633 			u32 val;
3634 
3635 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3636 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3637 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3638 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3639 			      (I40E_QUEUE_TYPE_TX <<
3640 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3641 
3642 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3643 
3644 			if (has_xdp) {
3645 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3646 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3647 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3648 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3649 				      (I40E_QUEUE_TYPE_TX <<
3650 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3651 
3652 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3653 			}
3654 
3655 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3656 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3657 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3658 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3659 			      (I40E_QUEUE_TYPE_RX <<
3660 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3661 
3662 			/* Terminate the linked list */
3663 			if (q == (q_vector->num_ringpairs - 1))
3664 				val |= (I40E_QUEUE_END_OF_LIST <<
3665 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3666 
3667 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3668 			qp++;
3669 		}
3670 	}
3671 
3672 	i40e_flush(hw);
3673 }
3674 
3675 /**
3676  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3677  * @pf: pointer to private device data structure
3678  **/
i40e_enable_misc_int_causes(struct i40e_pf * pf)3679 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3680 {
3681 	struct i40e_hw *hw = &pf->hw;
3682 	u32 val;
3683 
3684 	/* clear things first */
3685 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3686 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3687 
3688 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3689 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3690 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3691 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3692 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3693 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3694 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3695 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3696 
3697 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3698 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3699 
3700 	if (pf->flags & I40E_FLAG_PTP)
3701 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3702 
3703 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3704 
3705 	/* SW_ITR_IDX = 0, but don't change INTENA */
3706 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3707 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3708 
3709 	/* OTHER_ITR_IDX = 0 */
3710 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3711 }
3712 
3713 /**
3714  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3715  * @vsi: the VSI being configured
3716  **/
i40e_configure_msi_and_legacy(struct i40e_vsi * vsi)3717 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3718 {
3719 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3720 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3721 	struct i40e_pf *pf = vsi->back;
3722 	struct i40e_hw *hw = &pf->hw;
3723 	u32 val;
3724 
3725 	/* set the ITR configuration */
3726 	q_vector->rx.next_update = jiffies + 1;
3727 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3728 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3729 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3730 	q_vector->tx.next_update = jiffies + 1;
3731 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3732 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3733 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3734 
3735 	i40e_enable_misc_int_causes(pf);
3736 
3737 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3738 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3739 
3740 	/* Associate the queue pair to the vector and enable the queue int */
3741 	val = I40E_QINT_RQCTL_CAUSE_ENA_MASK		       |
3742 	      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT)  |
3743 	      (nextqp	   << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
3744 	      (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3745 
3746 	wr32(hw, I40E_QINT_RQCTL(0), val);
3747 
3748 	if (i40e_enabled_xdp_vsi(vsi)) {
3749 		val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		     |
3750 		      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
3751 		      (I40E_QUEUE_TYPE_TX
3752 		       << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3753 
3754 		wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3755 	}
3756 
3757 	val = I40E_QINT_TQCTL_CAUSE_ENA_MASK		      |
3758 	      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3759 	      (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3760 
3761 	wr32(hw, I40E_QINT_TQCTL(0), val);
3762 	i40e_flush(hw);
3763 }
3764 
3765 /**
3766  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
3767  * @pf: board private structure
3768  **/
i40e_irq_dynamic_disable_icr0(struct i40e_pf * pf)3769 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
3770 {
3771 	struct i40e_hw *hw = &pf->hw;
3772 
3773 	wr32(hw, I40E_PFINT_DYN_CTL0,
3774 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
3775 	i40e_flush(hw);
3776 }
3777 
3778 /**
3779  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
3780  * @pf: board private structure
3781  **/
i40e_irq_dynamic_enable_icr0(struct i40e_pf * pf)3782 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
3783 {
3784 	struct i40e_hw *hw = &pf->hw;
3785 	u32 val;
3786 
3787 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
3788 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
3789 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
3790 
3791 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
3792 	i40e_flush(hw);
3793 }
3794 
3795 /**
3796  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
3797  * @irq: interrupt number
3798  * @data: pointer to a q_vector
3799  **/
i40e_msix_clean_rings(int irq,void * data)3800 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
3801 {
3802 	struct i40e_q_vector *q_vector = data;
3803 
3804 	if (!q_vector->tx.ring && !q_vector->rx.ring)
3805 		return IRQ_HANDLED;
3806 
3807 	napi_schedule_irqoff(&q_vector->napi);
3808 
3809 	return IRQ_HANDLED;
3810 }
3811 
3812 /**
3813  * i40e_irq_affinity_notify - Callback for affinity changes
3814  * @notify: context as to what irq was changed
3815  * @mask: the new affinity mask
3816  *
3817  * This is a callback function used by the irq_set_affinity_notifier function
3818  * so that we may register to receive changes to the irq affinity masks.
3819  **/
i40e_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)3820 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
3821 				     const cpumask_t *mask)
3822 {
3823 	struct i40e_q_vector *q_vector =
3824 		container_of(notify, struct i40e_q_vector, affinity_notify);
3825 
3826 	cpumask_copy(&q_vector->affinity_mask, mask);
3827 }
3828 
3829 /**
3830  * i40e_irq_affinity_release - Callback for affinity notifier release
3831  * @ref: internal core kernel usage
3832  *
3833  * This is a callback function used by the irq_set_affinity_notifier function
3834  * to inform the current notification subscriber that they will no longer
3835  * receive notifications.
3836  **/
i40e_irq_affinity_release(struct kref * ref)3837 static void i40e_irq_affinity_release(struct kref *ref) {}
3838 
3839 /**
3840  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
3841  * @vsi: the VSI being configured
3842  * @basename: name for the vector
3843  *
3844  * Allocates MSI-X vectors and requests interrupts from the kernel.
3845  **/
i40e_vsi_request_irq_msix(struct i40e_vsi * vsi,char * basename)3846 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
3847 {
3848 	int q_vectors = vsi->num_q_vectors;
3849 	struct i40e_pf *pf = vsi->back;
3850 	int base = vsi->base_vector;
3851 	int rx_int_idx = 0;
3852 	int tx_int_idx = 0;
3853 	int vector, err;
3854 	int irq_num;
3855 	int cpu;
3856 
3857 	for (vector = 0; vector < q_vectors; vector++) {
3858 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
3859 
3860 		irq_num = pf->msix_entries[base + vector].vector;
3861 
3862 		if (q_vector->tx.ring && q_vector->rx.ring) {
3863 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3864 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
3865 			tx_int_idx++;
3866 		} else if (q_vector->rx.ring) {
3867 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3868 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
3869 		} else if (q_vector->tx.ring) {
3870 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
3871 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
3872 		} else {
3873 			/* skip this unused q_vector */
3874 			continue;
3875 		}
3876 		err = request_irq(irq_num,
3877 				  vsi->irq_handler,
3878 				  0,
3879 				  q_vector->name,
3880 				  q_vector);
3881 		if (err) {
3882 			dev_info(&pf->pdev->dev,
3883 				 "MSIX request_irq failed, error: %d\n", err);
3884 			goto free_queue_irqs;
3885 		}
3886 
3887 		/* register for affinity change notifications */
3888 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
3889 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
3890 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
3891 		/* Spread affinity hints out across online CPUs.
3892 		 *
3893 		 * get_cpu_mask returns a static constant mask with
3894 		 * a permanent lifetime so it's ok to pass to
3895 		 * irq_set_affinity_hint without making a copy.
3896 		 */
3897 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
3898 		irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
3899 	}
3900 
3901 	vsi->irqs_ready = true;
3902 	return 0;
3903 
3904 free_queue_irqs:
3905 	while (vector) {
3906 		vector--;
3907 		irq_num = pf->msix_entries[base + vector].vector;
3908 		irq_set_affinity_notifier(irq_num, NULL);
3909 		irq_set_affinity_hint(irq_num, NULL);
3910 		free_irq(irq_num, &vsi->q_vectors[vector]);
3911 	}
3912 	return err;
3913 }
3914 
3915 /**
3916  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
3917  * @vsi: the VSI being un-configured
3918  **/
i40e_vsi_disable_irq(struct i40e_vsi * vsi)3919 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
3920 {
3921 	struct i40e_pf *pf = vsi->back;
3922 	struct i40e_hw *hw = &pf->hw;
3923 	int base = vsi->base_vector;
3924 	int i;
3925 
3926 	/* disable interrupt causation from each queue */
3927 	for (i = 0; i < vsi->num_queue_pairs; i++) {
3928 		u32 val;
3929 
3930 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
3931 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
3932 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
3933 
3934 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
3935 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
3936 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
3937 
3938 		if (!i40e_enabled_xdp_vsi(vsi))
3939 			continue;
3940 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
3941 	}
3942 
3943 	/* disable each interrupt */
3944 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3945 		for (i = vsi->base_vector;
3946 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
3947 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
3948 
3949 		i40e_flush(hw);
3950 		for (i = 0; i < vsi->num_q_vectors; i++)
3951 			synchronize_irq(pf->msix_entries[i + base].vector);
3952 	} else {
3953 		/* Legacy and MSI mode - this stops all interrupt handling */
3954 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
3955 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
3956 		i40e_flush(hw);
3957 		synchronize_irq(pf->pdev->irq);
3958 	}
3959 }
3960 
3961 /**
3962  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
3963  * @vsi: the VSI being configured
3964  **/
i40e_vsi_enable_irq(struct i40e_vsi * vsi)3965 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
3966 {
3967 	struct i40e_pf *pf = vsi->back;
3968 	int i;
3969 
3970 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3971 		for (i = 0; i < vsi->num_q_vectors; i++)
3972 			i40e_irq_dynamic_enable(vsi, i);
3973 	} else {
3974 		i40e_irq_dynamic_enable_icr0(pf);
3975 	}
3976 
3977 	i40e_flush(&pf->hw);
3978 	return 0;
3979 }
3980 
3981 /**
3982  * i40e_free_misc_vector - Free the vector that handles non-queue events
3983  * @pf: board private structure
3984  **/
i40e_free_misc_vector(struct i40e_pf * pf)3985 static void i40e_free_misc_vector(struct i40e_pf *pf)
3986 {
3987 	/* Disable ICR 0 */
3988 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
3989 	i40e_flush(&pf->hw);
3990 
3991 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
3992 		synchronize_irq(pf->msix_entries[0].vector);
3993 		free_irq(pf->msix_entries[0].vector, pf);
3994 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
3995 	}
3996 }
3997 
3998 /**
3999  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4000  * @irq: interrupt number
4001  * @data: pointer to a q_vector
4002  *
4003  * This is the handler used for all MSI/Legacy interrupts, and deals
4004  * with both queue and non-queue interrupts.  This is also used in
4005  * MSIX mode to handle the non-queue interrupts.
4006  **/
i40e_intr(int irq,void * data)4007 static irqreturn_t i40e_intr(int irq, void *data)
4008 {
4009 	struct i40e_pf *pf = (struct i40e_pf *)data;
4010 	struct i40e_hw *hw = &pf->hw;
4011 	irqreturn_t ret = IRQ_NONE;
4012 	u32 icr0, icr0_remaining;
4013 	u32 val, ena_mask;
4014 
4015 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4016 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4017 
4018 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4019 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4020 		goto enable_intr;
4021 
4022 	/* if interrupt but no bits showing, must be SWINT */
4023 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4024 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4025 		pf->sw_int_count++;
4026 
4027 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4028 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4029 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4030 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4031 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4032 	}
4033 
4034 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4035 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4036 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4037 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4038 
4039 		/* We do not have a way to disarm Queue causes while leaving
4040 		 * interrupt enabled for all other causes, ideally
4041 		 * interrupt should be disabled while we are in NAPI but
4042 		 * this is not a performance path and napi_schedule()
4043 		 * can deal with rescheduling.
4044 		 */
4045 		if (!test_bit(__I40E_DOWN, pf->state))
4046 			napi_schedule_irqoff(&q_vector->napi);
4047 	}
4048 
4049 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4050 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4051 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4052 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4053 	}
4054 
4055 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4056 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4057 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4058 	}
4059 
4060 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4061 		/* disable any further VFLR event notifications */
4062 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4063 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4064 
4065 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4066 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4067 		} else {
4068 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4069 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4070 		}
4071 	}
4072 
4073 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4074 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4075 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4076 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4077 		val = rd32(hw, I40E_GLGEN_RSTAT);
4078 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4079 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4080 		if (val == I40E_RESET_CORER) {
4081 			pf->corer_count++;
4082 		} else if (val == I40E_RESET_GLOBR) {
4083 			pf->globr_count++;
4084 		} else if (val == I40E_RESET_EMPR) {
4085 			pf->empr_count++;
4086 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4087 		}
4088 	}
4089 
4090 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4091 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4092 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4093 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4094 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4095 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4096 	}
4097 
4098 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4099 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4100 
4101 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
4102 			icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4103 			i40e_ptp_tx_hwtstamp(pf);
4104 		}
4105 	}
4106 
4107 	/* If a critical error is pending we have no choice but to reset the
4108 	 * device.
4109 	 * Report and mask out any remaining unexpected interrupts.
4110 	 */
4111 	icr0_remaining = icr0 & ena_mask;
4112 	if (icr0_remaining) {
4113 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4114 			 icr0_remaining);
4115 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4116 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4117 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4118 			dev_info(&pf->pdev->dev, "device will be reset\n");
4119 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4120 			i40e_service_event_schedule(pf);
4121 		}
4122 		ena_mask &= ~icr0_remaining;
4123 	}
4124 	ret = IRQ_HANDLED;
4125 
4126 enable_intr:
4127 	/* re-enable interrupt causes */
4128 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4129 	if (!test_bit(__I40E_DOWN, pf->state) ||
4130 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4131 		i40e_service_event_schedule(pf);
4132 		i40e_irq_dynamic_enable_icr0(pf);
4133 	}
4134 
4135 	return ret;
4136 }
4137 
4138 /**
4139  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4140  * @tx_ring:  tx ring to clean
4141  * @budget:   how many cleans we're allowed
4142  *
4143  * Returns true if there's any budget left (e.g. the clean is finished)
4144  **/
i40e_clean_fdir_tx_irq(struct i40e_ring * tx_ring,int budget)4145 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4146 {
4147 	struct i40e_vsi *vsi = tx_ring->vsi;
4148 	u16 i = tx_ring->next_to_clean;
4149 	struct i40e_tx_buffer *tx_buf;
4150 	struct i40e_tx_desc *tx_desc;
4151 
4152 	tx_buf = &tx_ring->tx_bi[i];
4153 	tx_desc = I40E_TX_DESC(tx_ring, i);
4154 	i -= tx_ring->count;
4155 
4156 	do {
4157 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4158 
4159 		/* if next_to_watch is not set then there is no work pending */
4160 		if (!eop_desc)
4161 			break;
4162 
4163 		/* prevent any other reads prior to eop_desc */
4164 		smp_rmb();
4165 
4166 		/* if the descriptor isn't done, no work yet to do */
4167 		if (!(eop_desc->cmd_type_offset_bsz &
4168 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4169 			break;
4170 
4171 		/* clear next_to_watch to prevent false hangs */
4172 		tx_buf->next_to_watch = NULL;
4173 
4174 		tx_desc->buffer_addr = 0;
4175 		tx_desc->cmd_type_offset_bsz = 0;
4176 		/* move past filter desc */
4177 		tx_buf++;
4178 		tx_desc++;
4179 		i++;
4180 		if (unlikely(!i)) {
4181 			i -= tx_ring->count;
4182 			tx_buf = tx_ring->tx_bi;
4183 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4184 		}
4185 		/* unmap skb header data */
4186 		dma_unmap_single(tx_ring->dev,
4187 				 dma_unmap_addr(tx_buf, dma),
4188 				 dma_unmap_len(tx_buf, len),
4189 				 DMA_TO_DEVICE);
4190 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4191 			kfree(tx_buf->raw_buf);
4192 
4193 		tx_buf->raw_buf = NULL;
4194 		tx_buf->tx_flags = 0;
4195 		tx_buf->next_to_watch = NULL;
4196 		dma_unmap_len_set(tx_buf, len, 0);
4197 		tx_desc->buffer_addr = 0;
4198 		tx_desc->cmd_type_offset_bsz = 0;
4199 
4200 		/* move us past the eop_desc for start of next FD desc */
4201 		tx_buf++;
4202 		tx_desc++;
4203 		i++;
4204 		if (unlikely(!i)) {
4205 			i -= tx_ring->count;
4206 			tx_buf = tx_ring->tx_bi;
4207 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4208 		}
4209 
4210 		/* update budget accounting */
4211 		budget--;
4212 	} while (likely(budget));
4213 
4214 	i += tx_ring->count;
4215 	tx_ring->next_to_clean = i;
4216 
4217 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4218 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4219 
4220 	return budget > 0;
4221 }
4222 
4223 /**
4224  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4225  * @irq: interrupt number
4226  * @data: pointer to a q_vector
4227  **/
i40e_fdir_clean_ring(int irq,void * data)4228 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4229 {
4230 	struct i40e_q_vector *q_vector = data;
4231 	struct i40e_vsi *vsi;
4232 
4233 	if (!q_vector->tx.ring)
4234 		return IRQ_HANDLED;
4235 
4236 	vsi = q_vector->tx.ring->vsi;
4237 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4238 
4239 	return IRQ_HANDLED;
4240 }
4241 
4242 /**
4243  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4244  * @vsi: the VSI being configured
4245  * @v_idx: vector index
4246  * @qp_idx: queue pair index
4247  **/
i40e_map_vector_to_qp(struct i40e_vsi * vsi,int v_idx,int qp_idx)4248 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4249 {
4250 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4251 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4252 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4253 
4254 	tx_ring->q_vector = q_vector;
4255 	tx_ring->next = q_vector->tx.ring;
4256 	q_vector->tx.ring = tx_ring;
4257 	q_vector->tx.count++;
4258 
4259 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4260 	if (i40e_enabled_xdp_vsi(vsi)) {
4261 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4262 
4263 		xdp_ring->q_vector = q_vector;
4264 		xdp_ring->next = q_vector->tx.ring;
4265 		q_vector->tx.ring = xdp_ring;
4266 		q_vector->tx.count++;
4267 	}
4268 
4269 	rx_ring->q_vector = q_vector;
4270 	rx_ring->next = q_vector->rx.ring;
4271 	q_vector->rx.ring = rx_ring;
4272 	q_vector->rx.count++;
4273 }
4274 
4275 /**
4276  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4277  * @vsi: the VSI being configured
4278  *
4279  * This function maps descriptor rings to the queue-specific vectors
4280  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4281  * one vector per queue pair, but on a constrained vector budget, we
4282  * group the queue pairs as "efficiently" as possible.
4283  **/
i40e_vsi_map_rings_to_vectors(struct i40e_vsi * vsi)4284 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4285 {
4286 	int qp_remaining = vsi->num_queue_pairs;
4287 	int q_vectors = vsi->num_q_vectors;
4288 	int num_ringpairs;
4289 	int v_start = 0;
4290 	int qp_idx = 0;
4291 
4292 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4293 	 * group them so there are multiple queues per vector.
4294 	 * It is also important to go through all the vectors available to be
4295 	 * sure that if we don't use all the vectors, that the remaining vectors
4296 	 * are cleared. This is especially important when decreasing the
4297 	 * number of queues in use.
4298 	 */
4299 	for (; v_start < q_vectors; v_start++) {
4300 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4301 
4302 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4303 
4304 		q_vector->num_ringpairs = num_ringpairs;
4305 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4306 
4307 		q_vector->rx.count = 0;
4308 		q_vector->tx.count = 0;
4309 		q_vector->rx.ring = NULL;
4310 		q_vector->tx.ring = NULL;
4311 
4312 		while (num_ringpairs--) {
4313 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4314 			qp_idx++;
4315 			qp_remaining--;
4316 		}
4317 	}
4318 }
4319 
4320 /**
4321  * i40e_vsi_request_irq - Request IRQ from the OS
4322  * @vsi: the VSI being configured
4323  * @basename: name for the vector
4324  **/
i40e_vsi_request_irq(struct i40e_vsi * vsi,char * basename)4325 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4326 {
4327 	struct i40e_pf *pf = vsi->back;
4328 	int err;
4329 
4330 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4331 		err = i40e_vsi_request_irq_msix(vsi, basename);
4332 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4333 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4334 				  pf->int_name, pf);
4335 	else
4336 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4337 				  pf->int_name, pf);
4338 
4339 	if (err)
4340 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4341 
4342 	return err;
4343 }
4344 
4345 #ifdef CONFIG_NET_POLL_CONTROLLER
4346 /**
4347  * i40e_netpoll - A Polling 'interrupt' handler
4348  * @netdev: network interface device structure
4349  *
4350  * This is used by netconsole to send skbs without having to re-enable
4351  * interrupts.  It's not called while the normal interrupt routine is executing.
4352  **/
i40e_netpoll(struct net_device * netdev)4353 static void i40e_netpoll(struct net_device *netdev)
4354 {
4355 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4356 	struct i40e_vsi *vsi = np->vsi;
4357 	struct i40e_pf *pf = vsi->back;
4358 	int i;
4359 
4360 	/* if interface is down do nothing */
4361 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4362 		return;
4363 
4364 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4365 		for (i = 0; i < vsi->num_q_vectors; i++)
4366 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4367 	} else {
4368 		i40e_intr(pf->pdev->irq, netdev);
4369 	}
4370 }
4371 #endif
4372 
4373 #define I40E_QTX_ENA_WAIT_COUNT 50
4374 
4375 /**
4376  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4377  * @pf: the PF being configured
4378  * @pf_q: the PF queue
4379  * @enable: enable or disable state of the queue
4380  *
4381  * This routine will wait for the given Tx queue of the PF to reach the
4382  * enabled or disabled state.
4383  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4384  * multiple retries; else will return 0 in case of success.
4385  **/
i40e_pf_txq_wait(struct i40e_pf * pf,int pf_q,bool enable)4386 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4387 {
4388 	int i;
4389 	u32 tx_reg;
4390 
4391 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4392 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4393 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4394 			break;
4395 
4396 		usleep_range(10, 20);
4397 	}
4398 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4399 		return -ETIMEDOUT;
4400 
4401 	return 0;
4402 }
4403 
4404 /**
4405  * i40e_control_tx_q - Start or stop a particular Tx queue
4406  * @pf: the PF structure
4407  * @pf_q: the PF queue to configure
4408  * @enable: start or stop the queue
4409  *
4410  * This function enables or disables a single queue. Note that any delay
4411  * required after the operation is expected to be handled by the caller of
4412  * this function.
4413  **/
i40e_control_tx_q(struct i40e_pf * pf,int pf_q,bool enable)4414 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4415 {
4416 	struct i40e_hw *hw = &pf->hw;
4417 	u32 tx_reg;
4418 	int i;
4419 
4420 	/* warn the TX unit of coming changes */
4421 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4422 	if (!enable)
4423 		usleep_range(10, 20);
4424 
4425 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4426 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4427 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4428 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4429 			break;
4430 		usleep_range(1000, 2000);
4431 	}
4432 
4433 	/* Skip if the queue is already in the requested state */
4434 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4435 		return;
4436 
4437 	/* turn on/off the queue */
4438 	if (enable) {
4439 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4440 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4441 	} else {
4442 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4443 	}
4444 
4445 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4446 }
4447 
4448 /**
4449  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4450  * @seid: VSI SEID
4451  * @pf: the PF structure
4452  * @pf_q: the PF queue to configure
4453  * @is_xdp: true if the queue is used for XDP
4454  * @enable: start or stop the queue
4455  **/
i40e_control_wait_tx_q(int seid,struct i40e_pf * pf,int pf_q,bool is_xdp,bool enable)4456 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4457 			   bool is_xdp, bool enable)
4458 {
4459 	int ret;
4460 
4461 	i40e_control_tx_q(pf, pf_q, enable);
4462 
4463 	/* wait for the change to finish */
4464 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4465 	if (ret) {
4466 		dev_info(&pf->pdev->dev,
4467 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4468 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4469 			 (enable ? "en" : "dis"));
4470 	}
4471 
4472 	return ret;
4473 }
4474 
4475 /**
4476  * i40e_vsi_enable_tx - Start a VSI's rings
4477  * @vsi: the VSI being configured
4478  **/
i40e_vsi_enable_tx(struct i40e_vsi * vsi)4479 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4480 {
4481 	struct i40e_pf *pf = vsi->back;
4482 	int i, pf_q, ret = 0;
4483 
4484 	pf_q = vsi->base_queue;
4485 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4486 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4487 					     pf_q,
4488 					     false /*is xdp*/, true);
4489 		if (ret)
4490 			break;
4491 
4492 		if (!i40e_enabled_xdp_vsi(vsi))
4493 			continue;
4494 
4495 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4496 					     pf_q + vsi->alloc_queue_pairs,
4497 					     true /*is xdp*/, true);
4498 		if (ret)
4499 			break;
4500 	}
4501 	return ret;
4502 }
4503 
4504 /**
4505  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4506  * @pf: the PF being configured
4507  * @pf_q: the PF queue
4508  * @enable: enable or disable state of the queue
4509  *
4510  * This routine will wait for the given Rx queue of the PF to reach the
4511  * enabled or disabled state.
4512  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4513  * multiple retries; else will return 0 in case of success.
4514  **/
i40e_pf_rxq_wait(struct i40e_pf * pf,int pf_q,bool enable)4515 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4516 {
4517 	int i;
4518 	u32 rx_reg;
4519 
4520 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4521 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4522 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4523 			break;
4524 
4525 		usleep_range(10, 20);
4526 	}
4527 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4528 		return -ETIMEDOUT;
4529 
4530 	return 0;
4531 }
4532 
4533 /**
4534  * i40e_control_rx_q - Start or stop a particular Rx queue
4535  * @pf: the PF structure
4536  * @pf_q: the PF queue to configure
4537  * @enable: start or stop the queue
4538  *
4539  * This function enables or disables a single queue. Note that
4540  * any delay required after the operation is expected to be
4541  * handled by the caller of this function.
4542  **/
i40e_control_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4543 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4544 {
4545 	struct i40e_hw *hw = &pf->hw;
4546 	u32 rx_reg;
4547 	int i;
4548 
4549 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4550 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4551 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4552 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4553 			break;
4554 		usleep_range(1000, 2000);
4555 	}
4556 
4557 	/* Skip if the queue is already in the requested state */
4558 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4559 		return;
4560 
4561 	/* turn on/off the queue */
4562 	if (enable)
4563 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4564 	else
4565 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4566 
4567 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4568 }
4569 
4570 /**
4571  * i40e_control_wait_rx_q
4572  * @pf: the PF structure
4573  * @pf_q: queue being configured
4574  * @enable: start or stop the rings
4575  *
4576  * This function enables or disables a single queue along with waiting
4577  * for the change to finish. The caller of this function should handle
4578  * the delays needed in the case of disabling queues.
4579  **/
i40e_control_wait_rx_q(struct i40e_pf * pf,int pf_q,bool enable)4580 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4581 {
4582 	int ret = 0;
4583 
4584 	i40e_control_rx_q(pf, pf_q, enable);
4585 
4586 	/* wait for the change to finish */
4587 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4588 	if (ret)
4589 		return ret;
4590 
4591 	return ret;
4592 }
4593 
4594 /**
4595  * i40e_vsi_enable_rx - Start a VSI's rings
4596  * @vsi: the VSI being configured
4597  **/
i40e_vsi_enable_rx(struct i40e_vsi * vsi)4598 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4599 {
4600 	struct i40e_pf *pf = vsi->back;
4601 	int i, pf_q, ret = 0;
4602 
4603 	pf_q = vsi->base_queue;
4604 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4605 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4606 		if (ret) {
4607 			dev_info(&pf->pdev->dev,
4608 				 "VSI seid %d Rx ring %d enable timeout\n",
4609 				 vsi->seid, pf_q);
4610 			break;
4611 		}
4612 	}
4613 
4614 	return ret;
4615 }
4616 
4617 /**
4618  * i40e_vsi_start_rings - Start a VSI's rings
4619  * @vsi: the VSI being configured
4620  **/
i40e_vsi_start_rings(struct i40e_vsi * vsi)4621 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4622 {
4623 	int ret = 0;
4624 
4625 	/* do rx first for enable and last for disable */
4626 	ret = i40e_vsi_enable_rx(vsi);
4627 	if (ret)
4628 		return ret;
4629 	ret = i40e_vsi_enable_tx(vsi);
4630 
4631 	return ret;
4632 }
4633 
4634 #define I40E_DISABLE_TX_GAP_MSEC	50
4635 
4636 /**
4637  * i40e_vsi_stop_rings - Stop a VSI's rings
4638  * @vsi: the VSI being configured
4639  **/
i40e_vsi_stop_rings(struct i40e_vsi * vsi)4640 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4641 {
4642 	struct i40e_pf *pf = vsi->back;
4643 	int pf_q, err, q_end;
4644 
4645 	/* When port TX is suspended, don't wait */
4646 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4647 		return i40e_vsi_stop_rings_no_wait(vsi);
4648 
4649 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4650 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4651 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4652 
4653 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4654 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4655 		if (err)
4656 			dev_info(&pf->pdev->dev,
4657 				 "VSI seid %d Rx ring %d dissable timeout\n",
4658 				 vsi->seid, pf_q);
4659 	}
4660 
4661 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4662 	pf_q = vsi->base_queue;
4663 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4664 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4665 
4666 	i40e_vsi_wait_queues_disabled(vsi);
4667 }
4668 
4669 /**
4670  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4671  * @vsi: the VSI being shutdown
4672  *
4673  * This function stops all the rings for a VSI but does not delay to verify
4674  * that rings have been disabled. It is expected that the caller is shutting
4675  * down multiple VSIs at once and will delay together for all the VSIs after
4676  * initiating the shutdown. This is particularly useful for shutting down lots
4677  * of VFs together. Otherwise, a large delay can be incurred while configuring
4678  * each VSI in serial.
4679  **/
i40e_vsi_stop_rings_no_wait(struct i40e_vsi * vsi)4680 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4681 {
4682 	struct i40e_pf *pf = vsi->back;
4683 	int i, pf_q;
4684 
4685 	pf_q = vsi->base_queue;
4686 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4687 		i40e_control_tx_q(pf, pf_q, false);
4688 		i40e_control_rx_q(pf, pf_q, false);
4689 	}
4690 }
4691 
4692 /**
4693  * i40e_vsi_free_irq - Free the irq association with the OS
4694  * @vsi: the VSI being configured
4695  **/
i40e_vsi_free_irq(struct i40e_vsi * vsi)4696 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4697 {
4698 	struct i40e_pf *pf = vsi->back;
4699 	struct i40e_hw *hw = &pf->hw;
4700 	int base = vsi->base_vector;
4701 	u32 val, qp;
4702 	int i;
4703 
4704 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4705 		if (!vsi->q_vectors)
4706 			return;
4707 
4708 		if (!vsi->irqs_ready)
4709 			return;
4710 
4711 		vsi->irqs_ready = false;
4712 		for (i = 0; i < vsi->num_q_vectors; i++) {
4713 			int irq_num;
4714 			u16 vector;
4715 
4716 			vector = i + base;
4717 			irq_num = pf->msix_entries[vector].vector;
4718 
4719 			/* free only the irqs that were actually requested */
4720 			if (!vsi->q_vectors[i] ||
4721 			    !vsi->q_vectors[i]->num_ringpairs)
4722 				continue;
4723 
4724 			/* clear the affinity notifier in the IRQ descriptor */
4725 			irq_set_affinity_notifier(irq_num, NULL);
4726 			/* remove our suggested affinity mask for this IRQ */
4727 			irq_set_affinity_hint(irq_num, NULL);
4728 			synchronize_irq(irq_num);
4729 			free_irq(irq_num, vsi->q_vectors[i]);
4730 
4731 			/* Tear down the interrupt queue link list
4732 			 *
4733 			 * We know that they come in pairs and always
4734 			 * the Rx first, then the Tx.  To clear the
4735 			 * link list, stick the EOL value into the
4736 			 * next_q field of the registers.
4737 			 */
4738 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4739 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4740 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4741 			val |= I40E_QUEUE_END_OF_LIST
4742 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4743 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4744 
4745 			while (qp != I40E_QUEUE_END_OF_LIST) {
4746 				u32 next;
4747 
4748 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4749 
4750 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4751 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4752 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4753 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4754 
4755 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4756 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4757 
4758 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4759 
4760 				val = rd32(hw, I40E_QINT_TQCTL(qp));
4761 
4762 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
4763 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
4764 
4765 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4766 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4767 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4768 					 I40E_QINT_TQCTL_INTEVENT_MASK);
4769 
4770 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4771 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4772 
4773 				wr32(hw, I40E_QINT_TQCTL(qp), val);
4774 				qp = next;
4775 			}
4776 		}
4777 	} else {
4778 		free_irq(pf->pdev->irq, pf);
4779 
4780 		val = rd32(hw, I40E_PFINT_LNKLST0);
4781 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4782 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4783 		val |= I40E_QUEUE_END_OF_LIST
4784 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
4785 		wr32(hw, I40E_PFINT_LNKLST0, val);
4786 
4787 		val = rd32(hw, I40E_QINT_RQCTL(qp));
4788 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4789 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4790 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4791 			 I40E_QINT_RQCTL_INTEVENT_MASK);
4792 
4793 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4794 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4795 
4796 		wr32(hw, I40E_QINT_RQCTL(qp), val);
4797 
4798 		val = rd32(hw, I40E_QINT_TQCTL(qp));
4799 
4800 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
4801 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
4802 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
4803 			 I40E_QINT_TQCTL_INTEVENT_MASK);
4804 
4805 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
4806 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
4807 
4808 		wr32(hw, I40E_QINT_TQCTL(qp), val);
4809 	}
4810 }
4811 
4812 /**
4813  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
4814  * @vsi: the VSI being configured
4815  * @v_idx: Index of vector to be freed
4816  *
4817  * This function frees the memory allocated to the q_vector.  In addition if
4818  * NAPI is enabled it will delete any references to the NAPI struct prior
4819  * to freeing the q_vector.
4820  **/
i40e_free_q_vector(struct i40e_vsi * vsi,int v_idx)4821 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
4822 {
4823 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4824 	struct i40e_ring *ring;
4825 
4826 	if (!q_vector)
4827 		return;
4828 
4829 	/* disassociate q_vector from rings */
4830 	i40e_for_each_ring(ring, q_vector->tx)
4831 		ring->q_vector = NULL;
4832 
4833 	i40e_for_each_ring(ring, q_vector->rx)
4834 		ring->q_vector = NULL;
4835 
4836 	/* only VSI w/ an associated netdev is set up w/ NAPI */
4837 	if (vsi->netdev)
4838 		netif_napi_del(&q_vector->napi);
4839 
4840 	vsi->q_vectors[v_idx] = NULL;
4841 
4842 	kfree_rcu(q_vector, rcu);
4843 }
4844 
4845 /**
4846  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
4847  * @vsi: the VSI being un-configured
4848  *
4849  * This frees the memory allocated to the q_vectors and
4850  * deletes references to the NAPI struct.
4851  **/
i40e_vsi_free_q_vectors(struct i40e_vsi * vsi)4852 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
4853 {
4854 	int v_idx;
4855 
4856 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
4857 		i40e_free_q_vector(vsi, v_idx);
4858 }
4859 
4860 /**
4861  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
4862  * @pf: board private structure
4863  **/
i40e_reset_interrupt_capability(struct i40e_pf * pf)4864 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
4865 {
4866 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
4867 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4868 		pci_disable_msix(pf->pdev);
4869 		kfree(pf->msix_entries);
4870 		pf->msix_entries = NULL;
4871 		kfree(pf->irq_pile);
4872 		pf->irq_pile = NULL;
4873 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
4874 		pci_disable_msi(pf->pdev);
4875 	}
4876 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
4877 }
4878 
4879 /**
4880  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
4881  * @pf: board private structure
4882  *
4883  * We go through and clear interrupt specific resources and reset the structure
4884  * to pre-load conditions
4885  **/
i40e_clear_interrupt_scheme(struct i40e_pf * pf)4886 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
4887 {
4888 	int i;
4889 
4890 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
4891 		i40e_free_misc_vector(pf);
4892 
4893 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
4894 		      I40E_IWARP_IRQ_PILE_ID);
4895 
4896 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
4897 	for (i = 0; i < pf->num_alloc_vsi; i++)
4898 		if (pf->vsi[i])
4899 			i40e_vsi_free_q_vectors(pf->vsi[i]);
4900 	i40e_reset_interrupt_capability(pf);
4901 }
4902 
4903 /**
4904  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
4905  * @vsi: the VSI being configured
4906  **/
i40e_napi_enable_all(struct i40e_vsi * vsi)4907 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
4908 {
4909 	int q_idx;
4910 
4911 	if (!vsi->netdev)
4912 		return;
4913 
4914 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4915 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4916 
4917 		if (q_vector->rx.ring || q_vector->tx.ring)
4918 			napi_enable(&q_vector->napi);
4919 	}
4920 }
4921 
4922 /**
4923  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
4924  * @vsi: the VSI being configured
4925  **/
i40e_napi_disable_all(struct i40e_vsi * vsi)4926 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
4927 {
4928 	int q_idx;
4929 
4930 	if (!vsi->netdev)
4931 		return;
4932 
4933 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
4934 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
4935 
4936 		if (q_vector->rx.ring || q_vector->tx.ring)
4937 			napi_disable(&q_vector->napi);
4938 	}
4939 }
4940 
4941 /**
4942  * i40e_vsi_close - Shut down a VSI
4943  * @vsi: the vsi to be quelled
4944  **/
i40e_vsi_close(struct i40e_vsi * vsi)4945 static void i40e_vsi_close(struct i40e_vsi *vsi)
4946 {
4947 	struct i40e_pf *pf = vsi->back;
4948 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
4949 		i40e_down(vsi);
4950 	i40e_vsi_free_irq(vsi);
4951 	i40e_vsi_free_tx_resources(vsi);
4952 	i40e_vsi_free_rx_resources(vsi);
4953 	vsi->current_netdev_flags = 0;
4954 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
4955 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4956 		set_bit(__I40E_CLIENT_RESET, pf->state);
4957 }
4958 
4959 /**
4960  * i40e_quiesce_vsi - Pause a given VSI
4961  * @vsi: the VSI being paused
4962  **/
i40e_quiesce_vsi(struct i40e_vsi * vsi)4963 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
4964 {
4965 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4966 		return;
4967 
4968 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
4969 	if (vsi->netdev && netif_running(vsi->netdev))
4970 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
4971 	else
4972 		i40e_vsi_close(vsi);
4973 }
4974 
4975 /**
4976  * i40e_unquiesce_vsi - Resume a given VSI
4977  * @vsi: the VSI being resumed
4978  **/
i40e_unquiesce_vsi(struct i40e_vsi * vsi)4979 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
4980 {
4981 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
4982 		return;
4983 
4984 	if (vsi->netdev && netif_running(vsi->netdev))
4985 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
4986 	else
4987 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
4988 }
4989 
4990 /**
4991  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
4992  * @pf: the PF
4993  **/
i40e_pf_quiesce_all_vsi(struct i40e_pf * pf)4994 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
4995 {
4996 	int v;
4997 
4998 	for (v = 0; v < pf->num_alloc_vsi; v++) {
4999 		if (pf->vsi[v])
5000 			i40e_quiesce_vsi(pf->vsi[v]);
5001 	}
5002 }
5003 
5004 /**
5005  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5006  * @pf: the PF
5007  **/
i40e_pf_unquiesce_all_vsi(struct i40e_pf * pf)5008 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5009 {
5010 	int v;
5011 
5012 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5013 		if (pf->vsi[v])
5014 			i40e_unquiesce_vsi(pf->vsi[v]);
5015 	}
5016 }
5017 
5018 /**
5019  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5020  * @vsi: the VSI being configured
5021  *
5022  * Wait until all queues on a given VSI have been disabled.
5023  **/
i40e_vsi_wait_queues_disabled(struct i40e_vsi * vsi)5024 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5025 {
5026 	struct i40e_pf *pf = vsi->back;
5027 	int i, pf_q, ret;
5028 
5029 	pf_q = vsi->base_queue;
5030 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5031 		/* Check and wait for the Tx queue */
5032 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5033 		if (ret) {
5034 			dev_info(&pf->pdev->dev,
5035 				 "VSI seid %d Tx ring %d disable timeout\n",
5036 				 vsi->seid, pf_q);
5037 			return ret;
5038 		}
5039 
5040 		if (!i40e_enabled_xdp_vsi(vsi))
5041 			goto wait_rx;
5042 
5043 		/* Check and wait for the XDP Tx queue */
5044 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5045 				       false);
5046 		if (ret) {
5047 			dev_info(&pf->pdev->dev,
5048 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5049 				 vsi->seid, pf_q);
5050 			return ret;
5051 		}
5052 wait_rx:
5053 		/* Check and wait for the Rx queue */
5054 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5055 		if (ret) {
5056 			dev_info(&pf->pdev->dev,
5057 				 "VSI seid %d Rx ring %d disable timeout\n",
5058 				 vsi->seid, pf_q);
5059 			return ret;
5060 		}
5061 	}
5062 
5063 	return 0;
5064 }
5065 
5066 #ifdef CONFIG_I40E_DCB
5067 /**
5068  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5069  * @pf: the PF
5070  *
5071  * This function waits for the queues to be in disabled state for all the
5072  * VSIs that are managed by this PF.
5073  **/
i40e_pf_wait_queues_disabled(struct i40e_pf * pf)5074 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5075 {
5076 	int v, ret = 0;
5077 
5078 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5079 		if (pf->vsi[v]) {
5080 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5081 			if (ret)
5082 				break;
5083 		}
5084 	}
5085 
5086 	return ret;
5087 }
5088 
5089 #endif
5090 
5091 /**
5092  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5093  * @pf: pointer to PF
5094  *
5095  * Get TC map for ISCSI PF type that will include iSCSI TC
5096  * and LAN TC.
5097  **/
i40e_get_iscsi_tc_map(struct i40e_pf * pf)5098 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5099 {
5100 	struct i40e_dcb_app_priority_table app;
5101 	struct i40e_hw *hw = &pf->hw;
5102 	u8 enabled_tc = 1; /* TC0 is always enabled */
5103 	u8 tc, i;
5104 	/* Get the iSCSI APP TLV */
5105 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5106 
5107 	for (i = 0; i < dcbcfg->numapps; i++) {
5108 		app = dcbcfg->app[i];
5109 		if (app.selector == I40E_APP_SEL_TCPIP &&
5110 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5111 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5112 			enabled_tc |= BIT(tc);
5113 			break;
5114 		}
5115 	}
5116 
5117 	return enabled_tc;
5118 }
5119 
5120 /**
5121  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5122  * @dcbcfg: the corresponding DCBx configuration structure
5123  *
5124  * Return the number of TCs from given DCBx configuration
5125  **/
i40e_dcb_get_num_tc(struct i40e_dcbx_config * dcbcfg)5126 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5127 {
5128 	int i, tc_unused = 0;
5129 	u8 num_tc = 0;
5130 	u8 ret = 0;
5131 
5132 	/* Scan the ETS Config Priority Table to find
5133 	 * traffic class enabled for a given priority
5134 	 * and create a bitmask of enabled TCs
5135 	 */
5136 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5137 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5138 
5139 	/* Now scan the bitmask to check for
5140 	 * contiguous TCs starting with TC0
5141 	 */
5142 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5143 		if (num_tc & BIT(i)) {
5144 			if (!tc_unused) {
5145 				ret++;
5146 			} else {
5147 				pr_err("Non-contiguous TC - Disabling DCB\n");
5148 				return 1;
5149 			}
5150 		} else {
5151 			tc_unused = 1;
5152 		}
5153 	}
5154 
5155 	/* There is always at least TC0 */
5156 	if (!ret)
5157 		ret = 1;
5158 
5159 	return ret;
5160 }
5161 
5162 /**
5163  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5164  * @dcbcfg: the corresponding DCBx configuration structure
5165  *
5166  * Query the current DCB configuration and return the number of
5167  * traffic classes enabled from the given DCBX config
5168  **/
i40e_dcb_get_enabled_tc(struct i40e_dcbx_config * dcbcfg)5169 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5170 {
5171 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5172 	u8 enabled_tc = 1;
5173 	u8 i;
5174 
5175 	for (i = 0; i < num_tc; i++)
5176 		enabled_tc |= BIT(i);
5177 
5178 	return enabled_tc;
5179 }
5180 
5181 /**
5182  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5183  * @pf: PF being queried
5184  *
5185  * Query the current MQPRIO configuration and return the number of
5186  * traffic classes enabled.
5187  **/
i40e_mqprio_get_enabled_tc(struct i40e_pf * pf)5188 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5189 {
5190 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5191 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5192 	u8 enabled_tc = 1, i;
5193 
5194 	for (i = 1; i < num_tc; i++)
5195 		enabled_tc |= BIT(i);
5196 	return enabled_tc;
5197 }
5198 
5199 /**
5200  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5201  * @pf: PF being queried
5202  *
5203  * Return number of traffic classes enabled for the given PF
5204  **/
i40e_pf_get_num_tc(struct i40e_pf * pf)5205 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5206 {
5207 	struct i40e_hw *hw = &pf->hw;
5208 	u8 i, enabled_tc = 1;
5209 	u8 num_tc = 0;
5210 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5211 
5212 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5213 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5214 
5215 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5216 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5217 		return 1;
5218 
5219 	/* SFP mode will be enabled for all TCs on port */
5220 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5221 		return i40e_dcb_get_num_tc(dcbcfg);
5222 
5223 	/* MFP mode return count of enabled TCs for this PF */
5224 	if (pf->hw.func_caps.iscsi)
5225 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5226 	else
5227 		return 1; /* Only TC0 */
5228 
5229 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5230 		if (enabled_tc & BIT(i))
5231 			num_tc++;
5232 	}
5233 	return num_tc;
5234 }
5235 
5236 /**
5237  * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
5238  * @pf: PF being queried
5239  *
5240  * Return a bitmap for enabled traffic classes for this PF.
5241  **/
i40e_pf_get_tc_map(struct i40e_pf * pf)5242 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5243 {
5244 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5245 		return i40e_mqprio_get_enabled_tc(pf);
5246 
5247 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5248 	 * default TC
5249 	 */
5250 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5251 		return I40E_DEFAULT_TRAFFIC_CLASS;
5252 
5253 	/* SFP mode we want PF to be enabled for all TCs */
5254 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5255 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5256 
5257 	/* MFP enabled and iSCSI PF type */
5258 	if (pf->hw.func_caps.iscsi)
5259 		return i40e_get_iscsi_tc_map(pf);
5260 	else
5261 		return I40E_DEFAULT_TRAFFIC_CLASS;
5262 }
5263 
5264 /**
5265  * i40e_vsi_get_bw_info - Query VSI BW Information
5266  * @vsi: the VSI being queried
5267  *
5268  * Returns 0 on success, negative value on failure
5269  **/
i40e_vsi_get_bw_info(struct i40e_vsi * vsi)5270 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5271 {
5272 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5273 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5274 	struct i40e_pf *pf = vsi->back;
5275 	struct i40e_hw *hw = &pf->hw;
5276 	i40e_status ret;
5277 	u32 tc_bw_max;
5278 	int i;
5279 
5280 	/* Get the VSI level BW configuration */
5281 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5282 	if (ret) {
5283 		dev_info(&pf->pdev->dev,
5284 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5285 			 i40e_stat_str(&pf->hw, ret),
5286 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5287 		return -EINVAL;
5288 	}
5289 
5290 	/* Get the VSI level BW configuration per TC */
5291 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5292 					       NULL);
5293 	if (ret) {
5294 		dev_info(&pf->pdev->dev,
5295 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5296 			 i40e_stat_str(&pf->hw, ret),
5297 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5298 		return -EINVAL;
5299 	}
5300 
5301 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5302 		dev_info(&pf->pdev->dev,
5303 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5304 			 bw_config.tc_valid_bits,
5305 			 bw_ets_config.tc_valid_bits);
5306 		/* Still continuing */
5307 	}
5308 
5309 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5310 	vsi->bw_max_quanta = bw_config.max_bw;
5311 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5312 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5313 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5314 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5315 		vsi->bw_ets_limit_credits[i] =
5316 					le16_to_cpu(bw_ets_config.credits[i]);
5317 		/* 3 bits out of 4 for each TC */
5318 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5319 	}
5320 
5321 	return 0;
5322 }
5323 
5324 /**
5325  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5326  * @vsi: the VSI being configured
5327  * @enabled_tc: TC bitmap
5328  * @bw_share: BW shared credits per TC
5329  *
5330  * Returns 0 on success, negative value on failure
5331  **/
i40e_vsi_configure_bw_alloc(struct i40e_vsi * vsi,u8 enabled_tc,u8 * bw_share)5332 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5333 				       u8 *bw_share)
5334 {
5335 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5336 	struct i40e_pf *pf = vsi->back;
5337 	i40e_status ret;
5338 	int i;
5339 
5340 	/* There is no need to reset BW when mqprio mode is on.  */
5341 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5342 		return 0;
5343 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5344 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5345 		if (ret)
5346 			dev_info(&pf->pdev->dev,
5347 				 "Failed to reset tx rate for vsi->seid %u\n",
5348 				 vsi->seid);
5349 		return ret;
5350 	}
5351 	bw_data.tc_valid_bits = enabled_tc;
5352 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5353 		bw_data.tc_bw_credits[i] = bw_share[i];
5354 
5355 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5356 	if (ret) {
5357 		dev_info(&pf->pdev->dev,
5358 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5359 			 pf->hw.aq.asq_last_status);
5360 		return -EINVAL;
5361 	}
5362 
5363 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5364 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5365 
5366 	return 0;
5367 }
5368 
5369 /**
5370  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5371  * @vsi: the VSI being configured
5372  * @enabled_tc: TC map to be enabled
5373  *
5374  **/
i40e_vsi_config_netdev_tc(struct i40e_vsi * vsi,u8 enabled_tc)5375 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5376 {
5377 	struct net_device *netdev = vsi->netdev;
5378 	struct i40e_pf *pf = vsi->back;
5379 	struct i40e_hw *hw = &pf->hw;
5380 	u8 netdev_tc = 0;
5381 	int i;
5382 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5383 
5384 	if (!netdev)
5385 		return;
5386 
5387 	if (!enabled_tc) {
5388 		netdev_reset_tc(netdev);
5389 		return;
5390 	}
5391 
5392 	/* Set up actual enabled TCs on the VSI */
5393 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5394 		return;
5395 
5396 	/* set per TC queues for the VSI */
5397 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5398 		/* Only set TC queues for enabled tcs
5399 		 *
5400 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5401 		 * enabled_tc bitmap would be 0x00001001; the driver
5402 		 * will set the numtc for netdev as 2 that will be
5403 		 * referenced by the netdev layer as TC 0 and 1.
5404 		 */
5405 		if (vsi->tc_config.enabled_tc & BIT(i))
5406 			netdev_set_tc_queue(netdev,
5407 					vsi->tc_config.tc_info[i].netdev_tc,
5408 					vsi->tc_config.tc_info[i].qcount,
5409 					vsi->tc_config.tc_info[i].qoffset);
5410 	}
5411 
5412 	if (pf->flags & I40E_FLAG_TC_MQPRIO)
5413 		return;
5414 
5415 	/* Assign UP2TC map for the VSI */
5416 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5417 		/* Get the actual TC# for the UP */
5418 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5419 		/* Get the mapped netdev TC# for the UP */
5420 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5421 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5422 	}
5423 }
5424 
5425 /**
5426  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5427  * @vsi: the VSI being configured
5428  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5429  **/
i40e_vsi_update_queue_map(struct i40e_vsi * vsi,struct i40e_vsi_context * ctxt)5430 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5431 				      struct i40e_vsi_context *ctxt)
5432 {
5433 	/* copy just the sections touched not the entire info
5434 	 * since not all sections are valid as returned by
5435 	 * update vsi params
5436 	 */
5437 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5438 	memcpy(&vsi->info.queue_mapping,
5439 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5440 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5441 	       sizeof(vsi->info.tc_mapping));
5442 }
5443 
5444 /**
5445  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5446  * @vsi: the VSI being reconfigured
5447  * @vsi_offset: offset from main VF VSI
5448  */
i40e_update_adq_vsi_queues(struct i40e_vsi * vsi,int vsi_offset)5449 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5450 {
5451 	struct i40e_vsi_context ctxt = {};
5452 	struct i40e_pf *pf;
5453 	struct i40e_hw *hw;
5454 	int ret;
5455 
5456 	if (!vsi)
5457 		return I40E_ERR_PARAM;
5458 	pf = vsi->back;
5459 	hw = &pf->hw;
5460 
5461 	ctxt.seid = vsi->seid;
5462 	ctxt.pf_num = hw->pf_id;
5463 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5464 	ctxt.uplink_seid = vsi->uplink_seid;
5465 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5466 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5467 	ctxt.info = vsi->info;
5468 
5469 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5470 				 false);
5471 	if (vsi->reconfig_rss) {
5472 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5473 				      vsi->num_queue_pairs);
5474 		ret = i40e_vsi_config_rss(vsi);
5475 		if (ret) {
5476 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5477 			return ret;
5478 		}
5479 		vsi->reconfig_rss = false;
5480 	}
5481 
5482 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5483 	if (ret) {
5484 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5485 			 i40e_stat_str(hw, ret),
5486 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5487 		return ret;
5488 	}
5489 	/* update the local VSI info with updated queue map */
5490 	i40e_vsi_update_queue_map(vsi, &ctxt);
5491 	vsi->info.valid_sections = 0;
5492 
5493 	return ret;
5494 }
5495 
5496 /**
5497  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5498  * @vsi: VSI to be configured
5499  * @enabled_tc: TC bitmap
5500  *
5501  * This configures a particular VSI for TCs that are mapped to the
5502  * given TC bitmap. It uses default bandwidth share for TCs across
5503  * VSIs to configure TC for a particular VSI.
5504  *
5505  * NOTE:
5506  * It is expected that the VSI queues have been quisced before calling
5507  * this function.
5508  **/
i40e_vsi_config_tc(struct i40e_vsi * vsi,u8 enabled_tc)5509 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5510 {
5511 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5512 	struct i40e_pf *pf = vsi->back;
5513 	struct i40e_hw *hw = &pf->hw;
5514 	struct i40e_vsi_context ctxt;
5515 	int ret = 0;
5516 	int i;
5517 
5518 	/* Check if enabled_tc is same as existing or new TCs */
5519 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5520 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5521 		return ret;
5522 
5523 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5524 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5525 		if (enabled_tc & BIT(i))
5526 			bw_share[i] = 1;
5527 	}
5528 
5529 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5530 	if (ret) {
5531 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5532 
5533 		dev_info(&pf->pdev->dev,
5534 			 "Failed configuring TC map %d for VSI %d\n",
5535 			 enabled_tc, vsi->seid);
5536 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5537 						  &bw_config, NULL);
5538 		if (ret) {
5539 			dev_info(&pf->pdev->dev,
5540 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5541 				 i40e_stat_str(hw, ret),
5542 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5543 			goto out;
5544 		}
5545 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5546 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5547 
5548 			if (!valid_tc)
5549 				valid_tc = bw_config.tc_valid_bits;
5550 			/* Always enable TC0, no matter what */
5551 			valid_tc |= 1;
5552 			dev_info(&pf->pdev->dev,
5553 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5554 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5555 			enabled_tc = valid_tc;
5556 		}
5557 
5558 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5559 		if (ret) {
5560 			dev_err(&pf->pdev->dev,
5561 				"Unable to  configure TC map %d for VSI %d\n",
5562 				enabled_tc, vsi->seid);
5563 			goto out;
5564 		}
5565 	}
5566 
5567 	/* Update Queue Pairs Mapping for currently enabled UPs */
5568 	ctxt.seid = vsi->seid;
5569 	ctxt.pf_num = vsi->back->hw.pf_id;
5570 	ctxt.vf_num = 0;
5571 	ctxt.uplink_seid = vsi->uplink_seid;
5572 	ctxt.info = vsi->info;
5573 	if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
5574 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5575 		if (ret)
5576 			goto out;
5577 	} else {
5578 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5579 	}
5580 
5581 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5582 	 * queues changed.
5583 	 */
5584 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5585 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5586 				      vsi->num_queue_pairs);
5587 		ret = i40e_vsi_config_rss(vsi);
5588 		if (ret) {
5589 			dev_info(&vsi->back->pdev->dev,
5590 				 "Failed to reconfig rss for num_queues\n");
5591 			return ret;
5592 		}
5593 		vsi->reconfig_rss = false;
5594 	}
5595 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5596 		ctxt.info.valid_sections |=
5597 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5598 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5599 	}
5600 
5601 	/* Update the VSI after updating the VSI queue-mapping
5602 	 * information
5603 	 */
5604 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5605 	if (ret) {
5606 		dev_info(&pf->pdev->dev,
5607 			 "Update vsi tc config failed, err %s aq_err %s\n",
5608 			 i40e_stat_str(hw, ret),
5609 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5610 		goto out;
5611 	}
5612 	/* update the local VSI info with updated queue map */
5613 	i40e_vsi_update_queue_map(vsi, &ctxt);
5614 	vsi->info.valid_sections = 0;
5615 
5616 	/* Update current VSI BW information */
5617 	ret = i40e_vsi_get_bw_info(vsi);
5618 	if (ret) {
5619 		dev_info(&pf->pdev->dev,
5620 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5621 			 i40e_stat_str(hw, ret),
5622 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5623 		goto out;
5624 	}
5625 
5626 	/* Update the netdev TC setup */
5627 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5628 out:
5629 	return ret;
5630 }
5631 
5632 /**
5633  * i40e_get_link_speed - Returns link speed for the interface
5634  * @vsi: VSI to be configured
5635  *
5636  **/
i40e_get_link_speed(struct i40e_vsi * vsi)5637 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5638 {
5639 	struct i40e_pf *pf = vsi->back;
5640 
5641 	switch (pf->hw.phy.link_info.link_speed) {
5642 	case I40E_LINK_SPEED_40GB:
5643 		return 40000;
5644 	case I40E_LINK_SPEED_25GB:
5645 		return 25000;
5646 	case I40E_LINK_SPEED_20GB:
5647 		return 20000;
5648 	case I40E_LINK_SPEED_10GB:
5649 		return 10000;
5650 	case I40E_LINK_SPEED_1GB:
5651 		return 1000;
5652 	default:
5653 		return -EINVAL;
5654 	}
5655 }
5656 
5657 /**
5658  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5659  * @vsi: VSI to be configured
5660  * @seid: seid of the channel/VSI
5661  * @max_tx_rate: max TX rate to be configured as BW limit
5662  *
5663  * Helper function to set BW limit for a given VSI
5664  **/
i40e_set_bw_limit(struct i40e_vsi * vsi,u16 seid,u64 max_tx_rate)5665 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5666 {
5667 	struct i40e_pf *pf = vsi->back;
5668 	u64 credits = 0;
5669 	int speed = 0;
5670 	int ret = 0;
5671 
5672 	speed = i40e_get_link_speed(vsi);
5673 	if (max_tx_rate > speed) {
5674 		dev_err(&pf->pdev->dev,
5675 			"Invalid max tx rate %llu specified for VSI seid %d.",
5676 			max_tx_rate, seid);
5677 		return -EINVAL;
5678 	}
5679 	if (max_tx_rate && max_tx_rate < 50) {
5680 		dev_warn(&pf->pdev->dev,
5681 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5682 		max_tx_rate = 50;
5683 	}
5684 
5685 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5686 	credits = max_tx_rate;
5687 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5688 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5689 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5690 	if (ret)
5691 		dev_err(&pf->pdev->dev,
5692 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5693 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5694 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5695 	return ret;
5696 }
5697 
5698 /**
5699  * i40e_remove_queue_channels - Remove queue channels for the TCs
5700  * @vsi: VSI to be configured
5701  *
5702  * Remove queue channels for the TCs
5703  **/
i40e_remove_queue_channels(struct i40e_vsi * vsi)5704 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5705 {
5706 	enum i40e_admin_queue_err last_aq_status;
5707 	struct i40e_cloud_filter *cfilter;
5708 	struct i40e_channel *ch, *ch_tmp;
5709 	struct i40e_pf *pf = vsi->back;
5710 	struct hlist_node *node;
5711 	int ret, i;
5712 
5713 	/* Reset rss size that was stored when reconfiguring rss for
5714 	 * channel VSIs with non-power-of-2 queue count.
5715 	 */
5716 	vsi->current_rss_size = 0;
5717 
5718 	/* perform cleanup for channels if they exist */
5719 	if (list_empty(&vsi->ch_list))
5720 		return;
5721 
5722 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5723 		struct i40e_vsi *p_vsi;
5724 
5725 		list_del(&ch->list);
5726 		p_vsi = ch->parent_vsi;
5727 		if (!p_vsi || !ch->initialized) {
5728 			kfree(ch);
5729 			continue;
5730 		}
5731 		/* Reset queue contexts */
5732 		for (i = 0; i < ch->num_queue_pairs; i++) {
5733 			struct i40e_ring *tx_ring, *rx_ring;
5734 			u16 pf_q;
5735 
5736 			pf_q = ch->base_queue + i;
5737 			tx_ring = vsi->tx_rings[pf_q];
5738 			tx_ring->ch = NULL;
5739 
5740 			rx_ring = vsi->rx_rings[pf_q];
5741 			rx_ring->ch = NULL;
5742 		}
5743 
5744 		/* Reset BW configured for this VSI via mqprio */
5745 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
5746 		if (ret)
5747 			dev_info(&vsi->back->pdev->dev,
5748 				 "Failed to reset tx rate for ch->seid %u\n",
5749 				 ch->seid);
5750 
5751 		/* delete cloud filters associated with this channel */
5752 		hlist_for_each_entry_safe(cfilter, node,
5753 					  &pf->cloud_filter_list, cloud_node) {
5754 			if (cfilter->seid != ch->seid)
5755 				continue;
5756 
5757 			hash_del(&cfilter->cloud_node);
5758 			if (cfilter->dst_port)
5759 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
5760 									cfilter,
5761 									false);
5762 			else
5763 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
5764 								false);
5765 			last_aq_status = pf->hw.aq.asq_last_status;
5766 			if (ret)
5767 				dev_info(&pf->pdev->dev,
5768 					 "Failed to delete cloud filter, err %s aq_err %s\n",
5769 					 i40e_stat_str(&pf->hw, ret),
5770 					 i40e_aq_str(&pf->hw, last_aq_status));
5771 			kfree(cfilter);
5772 		}
5773 
5774 		/* delete VSI from FW */
5775 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
5776 					     NULL);
5777 		if (ret)
5778 			dev_err(&vsi->back->pdev->dev,
5779 				"unable to remove channel (%d) for parent VSI(%d)\n",
5780 				ch->seid, p_vsi->seid);
5781 		kfree(ch);
5782 	}
5783 	INIT_LIST_HEAD(&vsi->ch_list);
5784 }
5785 
5786 /**
5787  * i40e_get_max_queues_for_channel
5788  * @vsi: ptr to VSI to which channels are associated with
5789  *
5790  * Helper function which returns max value among the queue counts set on the
5791  * channels/TCs created.
5792  **/
i40e_get_max_queues_for_channel(struct i40e_vsi * vsi)5793 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
5794 {
5795 	struct i40e_channel *ch, *ch_tmp;
5796 	int max = 0;
5797 
5798 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5799 		if (!ch->initialized)
5800 			continue;
5801 		if (ch->num_queue_pairs > max)
5802 			max = ch->num_queue_pairs;
5803 	}
5804 
5805 	return max;
5806 }
5807 
5808 /**
5809  * i40e_validate_num_queues - validate num_queues w.r.t channel
5810  * @pf: ptr to PF device
5811  * @num_queues: number of queues
5812  * @vsi: the parent VSI
5813  * @reconfig_rss: indicates should the RSS be reconfigured or not
5814  *
5815  * This function validates number of queues in the context of new channel
5816  * which is being established and determines if RSS should be reconfigured
5817  * or not for parent VSI.
5818  **/
i40e_validate_num_queues(struct i40e_pf * pf,int num_queues,struct i40e_vsi * vsi,bool * reconfig_rss)5819 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
5820 				    struct i40e_vsi *vsi, bool *reconfig_rss)
5821 {
5822 	int max_ch_queues;
5823 
5824 	if (!reconfig_rss)
5825 		return -EINVAL;
5826 
5827 	*reconfig_rss = false;
5828 	if (vsi->current_rss_size) {
5829 		if (num_queues > vsi->current_rss_size) {
5830 			dev_dbg(&pf->pdev->dev,
5831 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
5832 				num_queues, vsi->current_rss_size);
5833 			return -EINVAL;
5834 		} else if ((num_queues < vsi->current_rss_size) &&
5835 			   (!is_power_of_2(num_queues))) {
5836 			dev_dbg(&pf->pdev->dev,
5837 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
5838 				num_queues, vsi->current_rss_size);
5839 			return -EINVAL;
5840 		}
5841 	}
5842 
5843 	if (!is_power_of_2(num_queues)) {
5844 		/* Find the max num_queues configured for channel if channel
5845 		 * exist.
5846 		 * if channel exist, then enforce 'num_queues' to be more than
5847 		 * max ever queues configured for channel.
5848 		 */
5849 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
5850 		if (num_queues < max_ch_queues) {
5851 			dev_dbg(&pf->pdev->dev,
5852 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
5853 				num_queues, max_ch_queues);
5854 			return -EINVAL;
5855 		}
5856 		*reconfig_rss = true;
5857 	}
5858 
5859 	return 0;
5860 }
5861 
5862 /**
5863  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
5864  * @vsi: the VSI being setup
5865  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
5866  *
5867  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
5868  **/
i40e_vsi_reconfig_rss(struct i40e_vsi * vsi,u16 rss_size)5869 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
5870 {
5871 	struct i40e_pf *pf = vsi->back;
5872 	u8 seed[I40E_HKEY_ARRAY_SIZE];
5873 	struct i40e_hw *hw = &pf->hw;
5874 	int local_rss_size;
5875 	u8 *lut;
5876 	int ret;
5877 
5878 	if (!vsi->rss_size)
5879 		return -EINVAL;
5880 
5881 	if (rss_size > vsi->rss_size)
5882 		return -EINVAL;
5883 
5884 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
5885 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
5886 	if (!lut)
5887 		return -ENOMEM;
5888 
5889 	/* Ignoring user configured lut if there is one */
5890 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
5891 
5892 	/* Use user configured hash key if there is one, otherwise
5893 	 * use default.
5894 	 */
5895 	if (vsi->rss_hkey_user)
5896 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
5897 	else
5898 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
5899 
5900 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
5901 	if (ret) {
5902 		dev_info(&pf->pdev->dev,
5903 			 "Cannot set RSS lut, err %s aq_err %s\n",
5904 			 i40e_stat_str(hw, ret),
5905 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5906 		kfree(lut);
5907 		return ret;
5908 	}
5909 	kfree(lut);
5910 
5911 	/* Do the update w.r.t. storing rss_size */
5912 	if (!vsi->orig_rss_size)
5913 		vsi->orig_rss_size = vsi->rss_size;
5914 	vsi->current_rss_size = local_rss_size;
5915 
5916 	return ret;
5917 }
5918 
5919 /**
5920  * i40e_channel_setup_queue_map - Setup a channel queue map
5921  * @pf: ptr to PF device
5922  * @ctxt: VSI context structure
5923  * @ch: ptr to channel structure
5924  *
5925  * Setup queue map for a specific channel
5926  **/
i40e_channel_setup_queue_map(struct i40e_pf * pf,struct i40e_vsi_context * ctxt,struct i40e_channel * ch)5927 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
5928 					 struct i40e_vsi_context *ctxt,
5929 					 struct i40e_channel *ch)
5930 {
5931 	u16 qcount, qmap, sections = 0;
5932 	u8 offset = 0;
5933 	int pow;
5934 
5935 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
5936 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
5937 
5938 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
5939 	ch->num_queue_pairs = qcount;
5940 
5941 	/* find the next higher power-of-2 of num queue pairs */
5942 	pow = ilog2(qcount);
5943 	if (!is_power_of_2(qcount))
5944 		pow++;
5945 
5946 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
5947 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
5948 
5949 	/* Setup queue TC[0].qmap for given VSI context */
5950 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
5951 
5952 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
5953 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
5954 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
5955 	ctxt->info.valid_sections |= cpu_to_le16(sections);
5956 }
5957 
5958 /**
5959  * i40e_add_channel - add a channel by adding VSI
5960  * @pf: ptr to PF device
5961  * @uplink_seid: underlying HW switching element (VEB) ID
5962  * @ch: ptr to channel structure
5963  *
5964  * Add a channel (VSI) using add_vsi and queue_map
5965  **/
i40e_add_channel(struct i40e_pf * pf,u16 uplink_seid,struct i40e_channel * ch)5966 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
5967 			    struct i40e_channel *ch)
5968 {
5969 	struct i40e_hw *hw = &pf->hw;
5970 	struct i40e_vsi_context ctxt;
5971 	u8 enabled_tc = 0x1; /* TC0 enabled */
5972 	int ret;
5973 
5974 	if (ch->type != I40E_VSI_VMDQ2) {
5975 		dev_info(&pf->pdev->dev,
5976 			 "add new vsi failed, ch->type %d\n", ch->type);
5977 		return -EINVAL;
5978 	}
5979 
5980 	memset(&ctxt, 0, sizeof(ctxt));
5981 	ctxt.pf_num = hw->pf_id;
5982 	ctxt.vf_num = 0;
5983 	ctxt.uplink_seid = uplink_seid;
5984 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5985 	if (ch->type == I40E_VSI_VMDQ2)
5986 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
5987 
5988 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
5989 		ctxt.info.valid_sections |=
5990 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
5991 		ctxt.info.switch_id =
5992 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
5993 	}
5994 
5995 	/* Set queue map for a given VSI context */
5996 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
5997 
5998 	/* Now time to create VSI */
5999 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6000 	if (ret) {
6001 		dev_info(&pf->pdev->dev,
6002 			 "add new vsi failed, err %s aq_err %s\n",
6003 			 i40e_stat_str(&pf->hw, ret),
6004 			 i40e_aq_str(&pf->hw,
6005 				     pf->hw.aq.asq_last_status));
6006 		return -ENOENT;
6007 	}
6008 
6009 	/* Success, update channel, set enabled_tc only if the channel
6010 	 * is not a macvlan
6011 	 */
6012 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6013 	ch->seid = ctxt.seid;
6014 	ch->vsi_number = ctxt.vsi_number;
6015 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6016 
6017 	/* copy just the sections touched not the entire info
6018 	 * since not all sections are valid as returned by
6019 	 * update vsi params
6020 	 */
6021 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6022 	memcpy(&ch->info.queue_mapping,
6023 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6024 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6025 	       sizeof(ctxt.info.tc_mapping));
6026 
6027 	return 0;
6028 }
6029 
i40e_channel_config_bw(struct i40e_vsi * vsi,struct i40e_channel * ch,u8 * bw_share)6030 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6031 				  u8 *bw_share)
6032 {
6033 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6034 	i40e_status ret;
6035 	int i;
6036 
6037 	bw_data.tc_valid_bits = ch->enabled_tc;
6038 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6039 		bw_data.tc_bw_credits[i] = bw_share[i];
6040 
6041 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6042 				       &bw_data, NULL);
6043 	if (ret) {
6044 		dev_info(&vsi->back->pdev->dev,
6045 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6046 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6047 		return -EINVAL;
6048 	}
6049 
6050 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6051 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6052 
6053 	return 0;
6054 }
6055 
6056 /**
6057  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6058  * @pf: ptr to PF device
6059  * @vsi: the VSI being setup
6060  * @ch: ptr to channel structure
6061  *
6062  * Configure TX rings associated with channel (VSI) since queues are being
6063  * from parent VSI.
6064  **/
i40e_channel_config_tx_ring(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6065 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6066 				       struct i40e_vsi *vsi,
6067 				       struct i40e_channel *ch)
6068 {
6069 	i40e_status ret;
6070 	int i;
6071 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6072 
6073 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6074 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6075 		if (ch->enabled_tc & BIT(i))
6076 			bw_share[i] = 1;
6077 	}
6078 
6079 	/* configure BW for new VSI */
6080 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6081 	if (ret) {
6082 		dev_info(&vsi->back->pdev->dev,
6083 			 "Failed configuring TC map %d for channel (seid %u)\n",
6084 			 ch->enabled_tc, ch->seid);
6085 		return ret;
6086 	}
6087 
6088 	for (i = 0; i < ch->num_queue_pairs; i++) {
6089 		struct i40e_ring *tx_ring, *rx_ring;
6090 		u16 pf_q;
6091 
6092 		pf_q = ch->base_queue + i;
6093 
6094 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6095 		 * context
6096 		 */
6097 		tx_ring = vsi->tx_rings[pf_q];
6098 		tx_ring->ch = ch;
6099 
6100 		/* Get the RX ring ptr */
6101 		rx_ring = vsi->rx_rings[pf_q];
6102 		rx_ring->ch = ch;
6103 	}
6104 
6105 	return 0;
6106 }
6107 
6108 /**
6109  * i40e_setup_hw_channel - setup new channel
6110  * @pf: ptr to PF device
6111  * @vsi: the VSI being setup
6112  * @ch: ptr to channel structure
6113  * @uplink_seid: underlying HW switching element (VEB) ID
6114  * @type: type of channel to be created (VMDq2/VF)
6115  *
6116  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6117  * and configures TX rings accordingly
6118  **/
i40e_setup_hw_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch,u16 uplink_seid,u8 type)6119 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6120 					struct i40e_vsi *vsi,
6121 					struct i40e_channel *ch,
6122 					u16 uplink_seid, u8 type)
6123 {
6124 	int ret;
6125 
6126 	ch->initialized = false;
6127 	ch->base_queue = vsi->next_base_queue;
6128 	ch->type = type;
6129 
6130 	/* Proceed with creation of channel (VMDq2) VSI */
6131 	ret = i40e_add_channel(pf, uplink_seid, ch);
6132 	if (ret) {
6133 		dev_info(&pf->pdev->dev,
6134 			 "failed to add_channel using uplink_seid %u\n",
6135 			 uplink_seid);
6136 		return ret;
6137 	}
6138 
6139 	/* Mark the successful creation of channel */
6140 	ch->initialized = true;
6141 
6142 	/* Reconfigure TX queues using QTX_CTL register */
6143 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6144 	if (ret) {
6145 		dev_info(&pf->pdev->dev,
6146 			 "failed to configure TX rings for channel %u\n",
6147 			 ch->seid);
6148 		return ret;
6149 	}
6150 
6151 	/* update 'next_base_queue' */
6152 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6153 	dev_dbg(&pf->pdev->dev,
6154 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6155 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6156 		ch->num_queue_pairs,
6157 		vsi->next_base_queue);
6158 	return ret;
6159 }
6160 
6161 /**
6162  * i40e_setup_channel - setup new channel using uplink element
6163  * @pf: ptr to PF device
6164  * @vsi: pointer to the VSI to set up the channel within
6165  * @ch: ptr to channel structure
6166  *
6167  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6168  * and uplink switching element (uplink_seid)
6169  **/
i40e_setup_channel(struct i40e_pf * pf,struct i40e_vsi * vsi,struct i40e_channel * ch)6170 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6171 			       struct i40e_channel *ch)
6172 {
6173 	u8 vsi_type;
6174 	u16 seid;
6175 	int ret;
6176 
6177 	if (vsi->type == I40E_VSI_MAIN) {
6178 		vsi_type = I40E_VSI_VMDQ2;
6179 	} else {
6180 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6181 			vsi->type);
6182 		return false;
6183 	}
6184 
6185 	/* underlying switching element */
6186 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6187 
6188 	/* create channel (VSI), configure TX rings */
6189 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6190 	if (ret) {
6191 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6192 		return false;
6193 	}
6194 
6195 	return ch->initialized ? true : false;
6196 }
6197 
6198 /**
6199  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6200  * @vsi: ptr to VSI which has PF backing
6201  *
6202  * Sets up switch mode correctly if it needs to be changed and perform
6203  * what are allowed modes.
6204  **/
i40e_validate_and_set_switch_mode(struct i40e_vsi * vsi)6205 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6206 {
6207 	u8 mode;
6208 	struct i40e_pf *pf = vsi->back;
6209 	struct i40e_hw *hw = &pf->hw;
6210 	int ret;
6211 
6212 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6213 	if (ret)
6214 		return -EINVAL;
6215 
6216 	if (hw->dev_caps.switch_mode) {
6217 		/* if switch mode is set, support mode2 (non-tunneled for
6218 		 * cloud filter) for now
6219 		 */
6220 		u32 switch_mode = hw->dev_caps.switch_mode &
6221 				  I40E_SWITCH_MODE_MASK;
6222 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6223 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6224 				return 0;
6225 			dev_err(&pf->pdev->dev,
6226 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6227 				hw->dev_caps.switch_mode);
6228 			return -EINVAL;
6229 		}
6230 	}
6231 
6232 	/* Set Bit 7 to be valid */
6233 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6234 
6235 	/* Set L4type for TCP support */
6236 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6237 
6238 	/* Set cloud filter mode */
6239 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6240 
6241 	/* Prep mode field for set_switch_config */
6242 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6243 					pf->last_sw_conf_valid_flags,
6244 					mode, NULL);
6245 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6246 		dev_err(&pf->pdev->dev,
6247 			"couldn't set switch config bits, err %s aq_err %s\n",
6248 			i40e_stat_str(hw, ret),
6249 			i40e_aq_str(hw,
6250 				    hw->aq.asq_last_status));
6251 
6252 	return ret;
6253 }
6254 
6255 /**
6256  * i40e_create_queue_channel - function to create channel
6257  * @vsi: VSI to be configured
6258  * @ch: ptr to channel (it contains channel specific params)
6259  *
6260  * This function creates channel (VSI) using num_queues specified by user,
6261  * reconfigs RSS if needed.
6262  **/
i40e_create_queue_channel(struct i40e_vsi * vsi,struct i40e_channel * ch)6263 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6264 			      struct i40e_channel *ch)
6265 {
6266 	struct i40e_pf *pf = vsi->back;
6267 	bool reconfig_rss;
6268 	int err;
6269 
6270 	if (!ch)
6271 		return -EINVAL;
6272 
6273 	if (!ch->num_queue_pairs) {
6274 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6275 			ch->num_queue_pairs);
6276 		return -EINVAL;
6277 	}
6278 
6279 	/* validate user requested num_queues for channel */
6280 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6281 				       &reconfig_rss);
6282 	if (err) {
6283 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6284 			 ch->num_queue_pairs);
6285 		return -EINVAL;
6286 	}
6287 
6288 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6289 	 * VSI to be added switch to VEB mode.
6290 	 */
6291 
6292 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6293 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6294 
6295 		if (vsi->type == I40E_VSI_MAIN) {
6296 			if (pf->flags & I40E_FLAG_TC_MQPRIO)
6297 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6298 			else
6299 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6300 		}
6301 		/* now onwards for main VSI, number of queues will be value
6302 		 * of TC0's queue count
6303 		 */
6304 	}
6305 
6306 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6307 	 * it should be more than num_queues
6308 	 */
6309 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6310 		dev_dbg(&pf->pdev->dev,
6311 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6312 			vsi->cnt_q_avail, ch->num_queue_pairs);
6313 		return -EINVAL;
6314 	}
6315 
6316 	/* reconfig_rss only if vsi type is MAIN_VSI */
6317 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6318 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6319 		if (err) {
6320 			dev_info(&pf->pdev->dev,
6321 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6322 				 ch->num_queue_pairs);
6323 			return -EINVAL;
6324 		}
6325 	}
6326 
6327 	if (!i40e_setup_channel(pf, vsi, ch)) {
6328 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6329 		return -EINVAL;
6330 	}
6331 
6332 	dev_info(&pf->pdev->dev,
6333 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6334 		 ch->seid, ch->num_queue_pairs);
6335 
6336 	/* configure VSI for BW limit */
6337 	if (ch->max_tx_rate) {
6338 		u64 credits = ch->max_tx_rate;
6339 
6340 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6341 			return -EINVAL;
6342 
6343 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6344 		dev_dbg(&pf->pdev->dev,
6345 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6346 			ch->max_tx_rate,
6347 			credits,
6348 			ch->seid);
6349 	}
6350 
6351 	/* in case of VF, this will be main SRIOV VSI */
6352 	ch->parent_vsi = vsi;
6353 
6354 	/* and update main_vsi's count for queue_available to use */
6355 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6356 
6357 	return 0;
6358 }
6359 
6360 /**
6361  * i40e_configure_queue_channels - Add queue channel for the given TCs
6362  * @vsi: VSI to be configured
6363  *
6364  * Configures queue channel mapping to the given TCs
6365  **/
i40e_configure_queue_channels(struct i40e_vsi * vsi)6366 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6367 {
6368 	struct i40e_channel *ch;
6369 	u64 max_rate = 0;
6370 	int ret = 0, i;
6371 
6372 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6373 	vsi->tc_seid_map[0] = vsi->seid;
6374 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6375 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6376 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6377 			if (!ch) {
6378 				ret = -ENOMEM;
6379 				goto err_free;
6380 			}
6381 
6382 			INIT_LIST_HEAD(&ch->list);
6383 			ch->num_queue_pairs =
6384 				vsi->tc_config.tc_info[i].qcount;
6385 			ch->base_queue =
6386 				vsi->tc_config.tc_info[i].qoffset;
6387 
6388 			/* Bandwidth limit through tc interface is in bytes/s,
6389 			 * change to Mbit/s
6390 			 */
6391 			max_rate = vsi->mqprio_qopt.max_rate[i];
6392 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6393 			ch->max_tx_rate = max_rate;
6394 
6395 			list_add_tail(&ch->list, &vsi->ch_list);
6396 
6397 			ret = i40e_create_queue_channel(vsi, ch);
6398 			if (ret) {
6399 				dev_err(&vsi->back->pdev->dev,
6400 					"Failed creating queue channel with TC%d: queues %d\n",
6401 					i, ch->num_queue_pairs);
6402 				goto err_free;
6403 			}
6404 			vsi->tc_seid_map[i] = ch->seid;
6405 		}
6406 	}
6407 	return ret;
6408 
6409 err_free:
6410 	i40e_remove_queue_channels(vsi);
6411 	return ret;
6412 }
6413 
6414 /**
6415  * i40e_veb_config_tc - Configure TCs for given VEB
6416  * @veb: given VEB
6417  * @enabled_tc: TC bitmap
6418  *
6419  * Configures given TC bitmap for VEB (switching) element
6420  **/
i40e_veb_config_tc(struct i40e_veb * veb,u8 enabled_tc)6421 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6422 {
6423 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6424 	struct i40e_pf *pf = veb->pf;
6425 	int ret = 0;
6426 	int i;
6427 
6428 	/* No TCs or already enabled TCs just return */
6429 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6430 		return ret;
6431 
6432 	bw_data.tc_valid_bits = enabled_tc;
6433 	/* bw_data.absolute_credits is not set (relative) */
6434 
6435 	/* Enable ETS TCs with equal BW Share for now */
6436 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6437 		if (enabled_tc & BIT(i))
6438 			bw_data.tc_bw_share_credits[i] = 1;
6439 	}
6440 
6441 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6442 						   &bw_data, NULL);
6443 	if (ret) {
6444 		dev_info(&pf->pdev->dev,
6445 			 "VEB bw config failed, err %s aq_err %s\n",
6446 			 i40e_stat_str(&pf->hw, ret),
6447 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6448 		goto out;
6449 	}
6450 
6451 	/* Update the BW information */
6452 	ret = i40e_veb_get_bw_info(veb);
6453 	if (ret) {
6454 		dev_info(&pf->pdev->dev,
6455 			 "Failed getting veb bw config, err %s aq_err %s\n",
6456 			 i40e_stat_str(&pf->hw, ret),
6457 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6458 	}
6459 
6460 out:
6461 	return ret;
6462 }
6463 
6464 #ifdef CONFIG_I40E_DCB
6465 /**
6466  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6467  * @pf: PF struct
6468  *
6469  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6470  * the caller would've quiesce all the VSIs before calling
6471  * this function
6472  **/
i40e_dcb_reconfigure(struct i40e_pf * pf)6473 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6474 {
6475 	u8 tc_map = 0;
6476 	int ret;
6477 	u8 v;
6478 
6479 	/* Enable the TCs available on PF to all VEBs */
6480 	tc_map = i40e_pf_get_tc_map(pf);
6481 	for (v = 0; v < I40E_MAX_VEB; v++) {
6482 		if (!pf->veb[v])
6483 			continue;
6484 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6485 		if (ret) {
6486 			dev_info(&pf->pdev->dev,
6487 				 "Failed configuring TC for VEB seid=%d\n",
6488 				 pf->veb[v]->seid);
6489 			/* Will try to configure as many components */
6490 		}
6491 	}
6492 
6493 	/* Update each VSI */
6494 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6495 		if (!pf->vsi[v])
6496 			continue;
6497 
6498 		/* - Enable all TCs for the LAN VSI
6499 		 * - For all others keep them at TC0 for now
6500 		 */
6501 		if (v == pf->lan_vsi)
6502 			tc_map = i40e_pf_get_tc_map(pf);
6503 		else
6504 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6505 
6506 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6507 		if (ret) {
6508 			dev_info(&pf->pdev->dev,
6509 				 "Failed configuring TC for VSI seid=%d\n",
6510 				 pf->vsi[v]->seid);
6511 			/* Will try to configure as many components */
6512 		} else {
6513 			/* Re-configure VSI vectors based on updated TC map */
6514 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6515 			if (pf->vsi[v]->netdev)
6516 				i40e_dcbnl_set_all(pf->vsi[v]);
6517 		}
6518 	}
6519 }
6520 
6521 /**
6522  * i40e_resume_port_tx - Resume port Tx
6523  * @pf: PF struct
6524  *
6525  * Resume a port's Tx and issue a PF reset in case of failure to
6526  * resume.
6527  **/
i40e_resume_port_tx(struct i40e_pf * pf)6528 static int i40e_resume_port_tx(struct i40e_pf *pf)
6529 {
6530 	struct i40e_hw *hw = &pf->hw;
6531 	int ret;
6532 
6533 	ret = i40e_aq_resume_port_tx(hw, NULL);
6534 	if (ret) {
6535 		dev_info(&pf->pdev->dev,
6536 			 "Resume Port Tx failed, err %s aq_err %s\n",
6537 			  i40e_stat_str(&pf->hw, ret),
6538 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6539 		/* Schedule PF reset to recover */
6540 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6541 		i40e_service_event_schedule(pf);
6542 	}
6543 
6544 	return ret;
6545 }
6546 
6547 /**
6548  * i40e_init_pf_dcb - Initialize DCB configuration
6549  * @pf: PF being configured
6550  *
6551  * Query the current DCB configuration and cache it
6552  * in the hardware structure
6553  **/
i40e_init_pf_dcb(struct i40e_pf * pf)6554 static int i40e_init_pf_dcb(struct i40e_pf *pf)
6555 {
6556 	struct i40e_hw *hw = &pf->hw;
6557 	int err = 0;
6558 
6559 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
6560 	 * Also do not enable DCBx if FW LLDP agent is disabled
6561 	 */
6562 	if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
6563 	    (pf->flags & I40E_FLAG_DISABLE_FW_LLDP)) {
6564 		dev_info(&pf->pdev->dev, "DCB is not supported or FW LLDP is disabled\n");
6565 		err = I40E_NOT_SUPPORTED;
6566 		goto out;
6567 	}
6568 
6569 	err = i40e_init_dcb(hw, true);
6570 	if (!err) {
6571 		/* Device/Function is not DCBX capable */
6572 		if ((!hw->func_caps.dcb) ||
6573 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
6574 			dev_info(&pf->pdev->dev,
6575 				 "DCBX offload is not supported or is disabled for this PF.\n");
6576 		} else {
6577 			/* When status is not DISABLED then DCBX in FW */
6578 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
6579 				       DCB_CAP_DCBX_VER_IEEE;
6580 
6581 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
6582 			/* Enable DCB tagging only when more than one TC
6583 			 * or explicitly disable if only one TC
6584 			 */
6585 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
6586 				pf->flags |= I40E_FLAG_DCB_ENABLED;
6587 			else
6588 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6589 			dev_dbg(&pf->pdev->dev,
6590 				"DCBX offload is supported for this PF.\n");
6591 		}
6592 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
6593 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
6594 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
6595 	} else {
6596 		dev_info(&pf->pdev->dev,
6597 			 "Query for DCB configuration failed, err %s aq_err %s\n",
6598 			 i40e_stat_str(&pf->hw, err),
6599 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6600 	}
6601 
6602 out:
6603 	return err;
6604 }
6605 #endif /* CONFIG_I40E_DCB */
6606 
6607 /**
6608  * i40e_print_link_message - print link up or down
6609  * @vsi: the VSI for which link needs a message
6610  * @isup: true of link is up, false otherwise
6611  */
i40e_print_link_message(struct i40e_vsi * vsi,bool isup)6612 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
6613 {
6614 	enum i40e_aq_link_speed new_speed;
6615 	struct i40e_pf *pf = vsi->back;
6616 	char *speed = "Unknown";
6617 	char *fc = "Unknown";
6618 	char *fec = "";
6619 	char *req_fec = "";
6620 	char *an = "";
6621 
6622 	if (isup)
6623 		new_speed = pf->hw.phy.link_info.link_speed;
6624 	else
6625 		new_speed = I40E_LINK_SPEED_UNKNOWN;
6626 
6627 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
6628 		return;
6629 	vsi->current_isup = isup;
6630 	vsi->current_speed = new_speed;
6631 	if (!isup) {
6632 		netdev_info(vsi->netdev, "NIC Link is Down\n");
6633 		return;
6634 	}
6635 
6636 	/* Warn user if link speed on NPAR enabled partition is not at
6637 	 * least 10GB
6638 	 */
6639 	if (pf->hw.func_caps.npar_enable &&
6640 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
6641 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
6642 		netdev_warn(vsi->netdev,
6643 			    "The partition detected link speed that is less than 10Gbps\n");
6644 
6645 	switch (pf->hw.phy.link_info.link_speed) {
6646 	case I40E_LINK_SPEED_40GB:
6647 		speed = "40 G";
6648 		break;
6649 	case I40E_LINK_SPEED_20GB:
6650 		speed = "20 G";
6651 		break;
6652 	case I40E_LINK_SPEED_25GB:
6653 		speed = "25 G";
6654 		break;
6655 	case I40E_LINK_SPEED_10GB:
6656 		speed = "10 G";
6657 		break;
6658 	case I40E_LINK_SPEED_5GB:
6659 		speed = "5 G";
6660 		break;
6661 	case I40E_LINK_SPEED_2_5GB:
6662 		speed = "2.5 G";
6663 		break;
6664 	case I40E_LINK_SPEED_1GB:
6665 		speed = "1000 M";
6666 		break;
6667 	case I40E_LINK_SPEED_100MB:
6668 		speed = "100 M";
6669 		break;
6670 	default:
6671 		break;
6672 	}
6673 
6674 	switch (pf->hw.fc.current_mode) {
6675 	case I40E_FC_FULL:
6676 		fc = "RX/TX";
6677 		break;
6678 	case I40E_FC_TX_PAUSE:
6679 		fc = "TX";
6680 		break;
6681 	case I40E_FC_RX_PAUSE:
6682 		fc = "RX";
6683 		break;
6684 	default:
6685 		fc = "None";
6686 		break;
6687 	}
6688 
6689 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
6690 		req_fec = "None";
6691 		fec = "None";
6692 		an = "False";
6693 
6694 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6695 			an = "True";
6696 
6697 		if (pf->hw.phy.link_info.fec_info &
6698 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6699 			fec = "CL74 FC-FEC/BASE-R";
6700 		else if (pf->hw.phy.link_info.fec_info &
6701 			 I40E_AQ_CONFIG_FEC_RS_ENA)
6702 			fec = "CL108 RS-FEC";
6703 
6704 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
6705 		 * both RS and FC are requested
6706 		 */
6707 		if (vsi->back->hw.phy.link_info.req_fec_info &
6708 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
6709 			if (vsi->back->hw.phy.link_info.req_fec_info &
6710 			    I40E_AQ_REQUEST_FEC_RS)
6711 				req_fec = "CL108 RS-FEC";
6712 			else
6713 				req_fec = "CL74 FC-FEC/BASE-R";
6714 		}
6715 		netdev_info(vsi->netdev,
6716 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
6717 			    speed, req_fec, fec, an, fc);
6718 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
6719 		req_fec = "None";
6720 		fec = "None";
6721 		an = "False";
6722 
6723 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
6724 			an = "True";
6725 
6726 		if (pf->hw.phy.link_info.fec_info &
6727 		    I40E_AQ_CONFIG_FEC_KR_ENA)
6728 			fec = "CL74 FC-FEC/BASE-R";
6729 
6730 		if (pf->hw.phy.link_info.req_fec_info &
6731 		    I40E_AQ_REQUEST_FEC_KR)
6732 			req_fec = "CL74 FC-FEC/BASE-R";
6733 
6734 		netdev_info(vsi->netdev,
6735 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
6736 			    speed, req_fec, fec, an, fc);
6737 	} else {
6738 		netdev_info(vsi->netdev,
6739 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
6740 			    speed, fc);
6741 	}
6742 
6743 }
6744 
6745 /**
6746  * i40e_up_complete - Finish the last steps of bringing up a connection
6747  * @vsi: the VSI being configured
6748  **/
i40e_up_complete(struct i40e_vsi * vsi)6749 static int i40e_up_complete(struct i40e_vsi *vsi)
6750 {
6751 	struct i40e_pf *pf = vsi->back;
6752 	int err;
6753 
6754 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
6755 		i40e_vsi_configure_msix(vsi);
6756 	else
6757 		i40e_configure_msi_and_legacy(vsi);
6758 
6759 	/* start rings */
6760 	err = i40e_vsi_start_rings(vsi);
6761 	if (err)
6762 		return err;
6763 
6764 	clear_bit(__I40E_VSI_DOWN, vsi->state);
6765 	i40e_napi_enable_all(vsi);
6766 	i40e_vsi_enable_irq(vsi);
6767 
6768 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
6769 	    (vsi->netdev)) {
6770 		i40e_print_link_message(vsi, true);
6771 		netif_tx_start_all_queues(vsi->netdev);
6772 		netif_carrier_on(vsi->netdev);
6773 	}
6774 
6775 	/* replay FDIR SB filters */
6776 	if (vsi->type == I40E_VSI_FDIR) {
6777 		/* reset fd counters */
6778 		pf->fd_add_err = 0;
6779 		pf->fd_atr_cnt = 0;
6780 		i40e_fdir_filter_restore(vsi);
6781 	}
6782 
6783 	/* On the next run of the service_task, notify any clients of the new
6784 	 * opened netdev
6785 	 */
6786 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
6787 	i40e_service_event_schedule(pf);
6788 
6789 	return 0;
6790 }
6791 
6792 /**
6793  * i40e_vsi_reinit_locked - Reset the VSI
6794  * @vsi: the VSI being configured
6795  *
6796  * Rebuild the ring structs after some configuration
6797  * has changed, e.g. MTU size.
6798  **/
i40e_vsi_reinit_locked(struct i40e_vsi * vsi)6799 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
6800 {
6801 	struct i40e_pf *pf = vsi->back;
6802 
6803 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
6804 		usleep_range(1000, 2000);
6805 	i40e_down(vsi);
6806 
6807 	i40e_up(vsi);
6808 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
6809 }
6810 
6811 /**
6812  * i40e_force_link_state - Force the link status
6813  * @pf: board private structure
6814  * @is_up: whether the link state should be forced up or down
6815  **/
i40e_force_link_state(struct i40e_pf * pf,bool is_up)6816 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
6817 {
6818 	struct i40e_aq_get_phy_abilities_resp abilities;
6819 	struct i40e_aq_set_phy_config config = {0};
6820 	bool non_zero_phy_type = is_up;
6821 	struct i40e_hw *hw = &pf->hw;
6822 	i40e_status err;
6823 	u64 mask;
6824 	u8 speed;
6825 
6826 	/* Card might've been put in an unstable state by other drivers
6827 	 * and applications, which causes incorrect speed values being
6828 	 * set on startup. In order to clear speed registers, we call
6829 	 * get_phy_capabilities twice, once to get initial state of
6830 	 * available speeds, and once to get current PHY config.
6831 	 */
6832 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
6833 					   NULL);
6834 	if (err) {
6835 		dev_err(&pf->pdev->dev,
6836 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6837 			i40e_stat_str(hw, err),
6838 			i40e_aq_str(hw, hw->aq.asq_last_status));
6839 		return err;
6840 	}
6841 	speed = abilities.link_speed;
6842 
6843 	/* Get the current phy config */
6844 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
6845 					   NULL);
6846 	if (err) {
6847 		dev_err(&pf->pdev->dev,
6848 			"failed to get phy cap., ret =  %s last_status =  %s\n",
6849 			i40e_stat_str(hw, err),
6850 			i40e_aq_str(hw, hw->aq.asq_last_status));
6851 		return err;
6852 	}
6853 
6854 	/* If link needs to go up, but was not forced to go down,
6855 	 * and its speed values are OK, no need for a flap
6856 	 * if non_zero_phy_type was set, still need to force up
6857 	 */
6858 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
6859 		non_zero_phy_type = true;
6860 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
6861 		return I40E_SUCCESS;
6862 
6863 	/* To force link we need to set bits for all supported PHY types,
6864 	 * but there are now more than 32, so we need to split the bitmap
6865 	 * across two fields.
6866 	 */
6867 	mask = I40E_PHY_TYPES_BITMASK;
6868 	config.phy_type =
6869 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
6870 	config.phy_type_ext =
6871 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
6872 	/* Copy the old settings, except of phy_type */
6873 	config.abilities = abilities.abilities;
6874 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
6875 		if (is_up)
6876 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
6877 		else
6878 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
6879 	}
6880 	if (abilities.link_speed != 0)
6881 		config.link_speed = abilities.link_speed;
6882 	else
6883 		config.link_speed = speed;
6884 	config.eee_capability = abilities.eee_capability;
6885 	config.eeer = abilities.eeer_val;
6886 	config.low_power_ctrl = abilities.d3_lpan;
6887 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
6888 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
6889 	err = i40e_aq_set_phy_config(hw, &config, NULL);
6890 
6891 	if (err) {
6892 		dev_err(&pf->pdev->dev,
6893 			"set phy config ret =  %s last_status =  %s\n",
6894 			i40e_stat_str(&pf->hw, err),
6895 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6896 		return err;
6897 	}
6898 
6899 	/* Update the link info */
6900 	err = i40e_update_link_info(hw);
6901 	if (err) {
6902 		/* Wait a little bit (on 40G cards it sometimes takes a really
6903 		 * long time for link to come back from the atomic reset)
6904 		 * and try once more
6905 		 */
6906 		msleep(1000);
6907 		i40e_update_link_info(hw);
6908 	}
6909 
6910 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
6911 
6912 	return I40E_SUCCESS;
6913 }
6914 
6915 /**
6916  * i40e_up - Bring the connection back up after being down
6917  * @vsi: the VSI being configured
6918  **/
i40e_up(struct i40e_vsi * vsi)6919 int i40e_up(struct i40e_vsi *vsi)
6920 {
6921 	int err;
6922 
6923 	if (vsi->type == I40E_VSI_MAIN &&
6924 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
6925 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
6926 		i40e_force_link_state(vsi->back, true);
6927 
6928 	err = i40e_vsi_configure(vsi);
6929 	if (!err)
6930 		err = i40e_up_complete(vsi);
6931 
6932 	return err;
6933 }
6934 
6935 /**
6936  * i40e_down - Shutdown the connection processing
6937  * @vsi: the VSI being stopped
6938  **/
i40e_down(struct i40e_vsi * vsi)6939 void i40e_down(struct i40e_vsi *vsi)
6940 {
6941 	int i;
6942 
6943 	/* It is assumed that the caller of this function
6944 	 * sets the vsi->state __I40E_VSI_DOWN bit.
6945 	 */
6946 	if (vsi->netdev) {
6947 		netif_carrier_off(vsi->netdev);
6948 		netif_tx_disable(vsi->netdev);
6949 	}
6950 	i40e_vsi_disable_irq(vsi);
6951 	i40e_vsi_stop_rings(vsi);
6952 	if (vsi->type == I40E_VSI_MAIN &&
6953 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
6954 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
6955 		i40e_force_link_state(vsi->back, false);
6956 	i40e_napi_disable_all(vsi);
6957 
6958 	for (i = 0; i < vsi->num_queue_pairs; i++) {
6959 		i40e_clean_tx_ring(vsi->tx_rings[i]);
6960 		if (i40e_enabled_xdp_vsi(vsi)) {
6961 			/* Make sure that in-progress ndo_xdp_xmit and
6962 			 * ndo_xsk_wakeup calls are completed.
6963 			 */
6964 			synchronize_rcu();
6965 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
6966 		}
6967 		i40e_clean_rx_ring(vsi->rx_rings[i]);
6968 	}
6969 
6970 }
6971 
6972 /**
6973  * i40e_validate_mqprio_qopt- validate queue mapping info
6974  * @vsi: the VSI being configured
6975  * @mqprio_qopt: queue parametrs
6976  **/
i40e_validate_mqprio_qopt(struct i40e_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)6977 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
6978 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
6979 {
6980 	u64 sum_max_rate = 0;
6981 	u64 max_rate = 0;
6982 	int i;
6983 
6984 	if (mqprio_qopt->qopt.offset[0] != 0 ||
6985 	    mqprio_qopt->qopt.num_tc < 1 ||
6986 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
6987 		return -EINVAL;
6988 	for (i = 0; ; i++) {
6989 		if (!mqprio_qopt->qopt.count[i])
6990 			return -EINVAL;
6991 		if (mqprio_qopt->min_rate[i]) {
6992 			dev_err(&vsi->back->pdev->dev,
6993 				"Invalid min tx rate (greater than 0) specified\n");
6994 			return -EINVAL;
6995 		}
6996 		max_rate = mqprio_qopt->max_rate[i];
6997 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6998 		sum_max_rate += max_rate;
6999 
7000 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7001 			break;
7002 		if (mqprio_qopt->qopt.offset[i + 1] !=
7003 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7004 			return -EINVAL;
7005 	}
7006 	if (vsi->num_queue_pairs <
7007 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7008 		dev_err(&vsi->back->pdev->dev,
7009 			"Failed to create traffic channel, insufficient number of queues.\n");
7010 		return -EINVAL;
7011 	}
7012 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7013 		dev_err(&vsi->back->pdev->dev,
7014 			"Invalid max tx rate specified\n");
7015 		return -EINVAL;
7016 	}
7017 	return 0;
7018 }
7019 
7020 /**
7021  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7022  * @vsi: the VSI being configured
7023  **/
i40e_vsi_set_default_tc_config(struct i40e_vsi * vsi)7024 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7025 {
7026 	u16 qcount;
7027 	int i;
7028 
7029 	/* Only TC0 is enabled */
7030 	vsi->tc_config.numtc = 1;
7031 	vsi->tc_config.enabled_tc = 1;
7032 	qcount = min_t(int, vsi->alloc_queue_pairs,
7033 		       i40e_pf_get_max_q_per_tc(vsi->back));
7034 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7035 		/* For the TC that is not enabled set the offset to to default
7036 		 * queue and allocate one queue for the given TC.
7037 		 */
7038 		vsi->tc_config.tc_info[i].qoffset = 0;
7039 		if (i == 0)
7040 			vsi->tc_config.tc_info[i].qcount = qcount;
7041 		else
7042 			vsi->tc_config.tc_info[i].qcount = 1;
7043 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7044 	}
7045 }
7046 
7047 /**
7048  * i40e_del_macvlan_filter
7049  * @hw: pointer to the HW structure
7050  * @seid: seid of the channel VSI
7051  * @macaddr: the mac address to apply as a filter
7052  * @aq_err: store the admin Q error
7053  *
7054  * This function deletes a mac filter on the channel VSI which serves as the
7055  * macvlan. Returns 0 on success.
7056  **/
i40e_del_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7057 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7058 					   const u8 *macaddr, int *aq_err)
7059 {
7060 	struct i40e_aqc_remove_macvlan_element_data element;
7061 	i40e_status status;
7062 
7063 	memset(&element, 0, sizeof(element));
7064 	ether_addr_copy(element.mac_addr, macaddr);
7065 	element.vlan_tag = 0;
7066 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7067 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7068 	*aq_err = hw->aq.asq_last_status;
7069 
7070 	return status;
7071 }
7072 
7073 /**
7074  * i40e_add_macvlan_filter
7075  * @hw: pointer to the HW structure
7076  * @seid: seid of the channel VSI
7077  * @macaddr: the mac address to apply as a filter
7078  * @aq_err: store the admin Q error
7079  *
7080  * This function adds a mac filter on the channel VSI which serves as the
7081  * macvlan. Returns 0 on success.
7082  **/
i40e_add_macvlan_filter(struct i40e_hw * hw,u16 seid,const u8 * macaddr,int * aq_err)7083 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7084 					   const u8 *macaddr, int *aq_err)
7085 {
7086 	struct i40e_aqc_add_macvlan_element_data element;
7087 	i40e_status status;
7088 	u16 cmd_flags = 0;
7089 
7090 	ether_addr_copy(element.mac_addr, macaddr);
7091 	element.vlan_tag = 0;
7092 	element.queue_number = 0;
7093 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7094 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7095 	element.flags = cpu_to_le16(cmd_flags);
7096 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7097 	*aq_err = hw->aq.asq_last_status;
7098 
7099 	return status;
7100 }
7101 
7102 /**
7103  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7104  * @vsi: the VSI we want to access
7105  * @ch: the channel we want to access
7106  */
i40e_reset_ch_rings(struct i40e_vsi * vsi,struct i40e_channel * ch)7107 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7108 {
7109 	struct i40e_ring *tx_ring, *rx_ring;
7110 	u16 pf_q;
7111 	int i;
7112 
7113 	for (i = 0; i < ch->num_queue_pairs; i++) {
7114 		pf_q = ch->base_queue + i;
7115 		tx_ring = vsi->tx_rings[pf_q];
7116 		tx_ring->ch = NULL;
7117 		rx_ring = vsi->rx_rings[pf_q];
7118 		rx_ring->ch = NULL;
7119 	}
7120 }
7121 
7122 /**
7123  * i40e_free_macvlan_channels
7124  * @vsi: the VSI we want to access
7125  *
7126  * This function frees the Qs of the channel VSI from
7127  * the stack and also deletes the channel VSIs which
7128  * serve as macvlans.
7129  */
i40e_free_macvlan_channels(struct i40e_vsi * vsi)7130 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7131 {
7132 	struct i40e_channel *ch, *ch_tmp;
7133 	int ret;
7134 
7135 	if (list_empty(&vsi->macvlan_list))
7136 		return;
7137 
7138 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7139 		struct i40e_vsi *parent_vsi;
7140 
7141 		if (i40e_is_channel_macvlan(ch)) {
7142 			i40e_reset_ch_rings(vsi, ch);
7143 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7144 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7145 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7146 			kfree(ch->fwd);
7147 			ch->fwd = NULL;
7148 		}
7149 
7150 		list_del(&ch->list);
7151 		parent_vsi = ch->parent_vsi;
7152 		if (!parent_vsi || !ch->initialized) {
7153 			kfree(ch);
7154 			continue;
7155 		}
7156 
7157 		/* remove the VSI */
7158 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7159 					     NULL);
7160 		if (ret)
7161 			dev_err(&vsi->back->pdev->dev,
7162 				"unable to remove channel (%d) for parent VSI(%d)\n",
7163 				ch->seid, parent_vsi->seid);
7164 		kfree(ch);
7165 	}
7166 	vsi->macvlan_cnt = 0;
7167 }
7168 
7169 /**
7170  * i40e_fwd_ring_up - bring the macvlan device up
7171  * @vsi: the VSI we want to access
7172  * @vdev: macvlan netdevice
7173  * @fwd: the private fwd structure
7174  */
i40e_fwd_ring_up(struct i40e_vsi * vsi,struct net_device * vdev,struct i40e_fwd_adapter * fwd)7175 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7176 			    struct i40e_fwd_adapter *fwd)
7177 {
7178 	int ret = 0, num_tc = 1,  i, aq_err;
7179 	struct i40e_channel *ch, *ch_tmp;
7180 	struct i40e_pf *pf = vsi->back;
7181 	struct i40e_hw *hw = &pf->hw;
7182 
7183 	if (list_empty(&vsi->macvlan_list))
7184 		return -EINVAL;
7185 
7186 	/* Go through the list and find an available channel */
7187 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7188 		if (!i40e_is_channel_macvlan(ch)) {
7189 			ch->fwd = fwd;
7190 			/* record configuration for macvlan interface in vdev */
7191 			for (i = 0; i < num_tc; i++)
7192 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7193 							     i,
7194 							     ch->num_queue_pairs,
7195 							     ch->base_queue);
7196 			for (i = 0; i < ch->num_queue_pairs; i++) {
7197 				struct i40e_ring *tx_ring, *rx_ring;
7198 				u16 pf_q;
7199 
7200 				pf_q = ch->base_queue + i;
7201 
7202 				/* Get to TX ring ptr */
7203 				tx_ring = vsi->tx_rings[pf_q];
7204 				tx_ring->ch = ch;
7205 
7206 				/* Get the RX ring ptr */
7207 				rx_ring = vsi->rx_rings[pf_q];
7208 				rx_ring->ch = ch;
7209 			}
7210 			break;
7211 		}
7212 	}
7213 
7214 	/* Guarantee all rings are updated before we update the
7215 	 * MAC address filter.
7216 	 */
7217 	wmb();
7218 
7219 	/* Add a mac filter */
7220 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7221 	if (ret) {
7222 		/* if we cannot add the MAC rule then disable the offload */
7223 		macvlan_release_l2fw_offload(vdev);
7224 		for (i = 0; i < ch->num_queue_pairs; i++) {
7225 			struct i40e_ring *rx_ring;
7226 			u16 pf_q;
7227 
7228 			pf_q = ch->base_queue + i;
7229 			rx_ring = vsi->rx_rings[pf_q];
7230 			rx_ring->netdev = NULL;
7231 		}
7232 		dev_info(&pf->pdev->dev,
7233 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7234 			  i40e_stat_str(hw, ret),
7235 			  i40e_aq_str(hw, aq_err));
7236 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7237 	}
7238 
7239 	return ret;
7240 }
7241 
7242 /**
7243  * i40e_setup_macvlans - create the channels which will be macvlans
7244  * @vsi: the VSI we want to access
7245  * @macvlan_cnt: no. of macvlans to be setup
7246  * @qcnt: no. of Qs per macvlan
7247  * @vdev: macvlan netdevice
7248  */
i40e_setup_macvlans(struct i40e_vsi * vsi,u16 macvlan_cnt,u16 qcnt,struct net_device * vdev)7249 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7250 			       struct net_device *vdev)
7251 {
7252 	struct i40e_pf *pf = vsi->back;
7253 	struct i40e_hw *hw = &pf->hw;
7254 	struct i40e_vsi_context ctxt;
7255 	u16 sections, qmap, num_qps;
7256 	struct i40e_channel *ch;
7257 	int i, pow, ret = 0;
7258 	u8 offset = 0;
7259 
7260 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7261 		return -EINVAL;
7262 
7263 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7264 
7265 	/* find the next higher power-of-2 of num queue pairs */
7266 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7267 
7268 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7269 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7270 
7271 	/* Setup context bits for the main VSI */
7272 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7273 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7274 	memset(&ctxt, 0, sizeof(ctxt));
7275 	ctxt.seid = vsi->seid;
7276 	ctxt.pf_num = vsi->back->hw.pf_id;
7277 	ctxt.vf_num = 0;
7278 	ctxt.uplink_seid = vsi->uplink_seid;
7279 	ctxt.info = vsi->info;
7280 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7281 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7282 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7283 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7284 
7285 	/* Reconfigure RSS for main VSI with new max queue count */
7286 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7287 	ret = i40e_vsi_config_rss(vsi);
7288 	if (ret) {
7289 		dev_info(&pf->pdev->dev,
7290 			 "Failed to reconfig RSS for num_queues (%u)\n",
7291 			 vsi->rss_size);
7292 		return ret;
7293 	}
7294 	vsi->reconfig_rss = true;
7295 	dev_dbg(&vsi->back->pdev->dev,
7296 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7297 	vsi->next_base_queue = num_qps;
7298 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7299 
7300 	/* Update the VSI after updating the VSI queue-mapping
7301 	 * information
7302 	 */
7303 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7304 	if (ret) {
7305 		dev_info(&pf->pdev->dev,
7306 			 "Update vsi tc config failed, err %s aq_err %s\n",
7307 			 i40e_stat_str(hw, ret),
7308 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7309 		return ret;
7310 	}
7311 	/* update the local VSI info with updated queue map */
7312 	i40e_vsi_update_queue_map(vsi, &ctxt);
7313 	vsi->info.valid_sections = 0;
7314 
7315 	/* Create channels for macvlans */
7316 	INIT_LIST_HEAD(&vsi->macvlan_list);
7317 	for (i = 0; i < macvlan_cnt; i++) {
7318 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7319 		if (!ch) {
7320 			ret = -ENOMEM;
7321 			goto err_free;
7322 		}
7323 		INIT_LIST_HEAD(&ch->list);
7324 		ch->num_queue_pairs = qcnt;
7325 		if (!i40e_setup_channel(pf, vsi, ch)) {
7326 			ret = -EINVAL;
7327 			kfree(ch);
7328 			goto err_free;
7329 		}
7330 		ch->parent_vsi = vsi;
7331 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7332 		vsi->macvlan_cnt++;
7333 		list_add_tail(&ch->list, &vsi->macvlan_list);
7334 	}
7335 
7336 	return ret;
7337 
7338 err_free:
7339 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7340 	i40e_free_macvlan_channels(vsi);
7341 
7342 	return ret;
7343 }
7344 
7345 /**
7346  * i40e_fwd_add - configure macvlans
7347  * @netdev: net device to configure
7348  * @vdev: macvlan netdevice
7349  **/
i40e_fwd_add(struct net_device * netdev,struct net_device * vdev)7350 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7351 {
7352 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7353 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7354 	struct i40e_vsi *vsi = np->vsi;
7355 	struct i40e_pf *pf = vsi->back;
7356 	struct i40e_fwd_adapter *fwd;
7357 	int avail_macvlan, ret;
7358 
7359 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7360 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7361 		return ERR_PTR(-EINVAL);
7362 	}
7363 	if ((pf->flags & I40E_FLAG_TC_MQPRIO)) {
7364 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7365 		return ERR_PTR(-EINVAL);
7366 	}
7367 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7368 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7369 		return ERR_PTR(-EINVAL);
7370 	}
7371 
7372 	/* The macvlan device has to be a single Q device so that the
7373 	 * tc_to_txq field can be reused to pick the tx queue.
7374 	 */
7375 	if (netif_is_multiqueue(vdev))
7376 		return ERR_PTR(-ERANGE);
7377 
7378 	if (!vsi->macvlan_cnt) {
7379 		/* reserve bit 0 for the pf device */
7380 		set_bit(0, vsi->fwd_bitmask);
7381 
7382 		/* Try to reserve as many queues as possible for macvlans. First
7383 		 * reserve 3/4th of max vectors, then half, then quarter and
7384 		 * calculate Qs per macvlan as you go
7385 		 */
7386 		vectors = pf->num_lan_msix;
7387 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7388 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7389 			q_per_macvlan = 4;
7390 			macvlan_cnt = (vectors - 32) / 4;
7391 		} else if (vectors <= 64 && vectors > 32) {
7392 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7393 			q_per_macvlan = 2;
7394 			macvlan_cnt = (vectors - 16) / 2;
7395 		} else if (vectors <= 32 && vectors > 16) {
7396 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7397 			q_per_macvlan = 1;
7398 			macvlan_cnt = vectors - 16;
7399 		} else if (vectors <= 16 && vectors > 8) {
7400 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7401 			q_per_macvlan = 1;
7402 			macvlan_cnt = vectors - 8;
7403 		} else {
7404 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7405 			q_per_macvlan = 1;
7406 			macvlan_cnt = vectors - 1;
7407 		}
7408 
7409 		if (macvlan_cnt == 0)
7410 			return ERR_PTR(-EBUSY);
7411 
7412 		/* Quiesce VSI queues */
7413 		i40e_quiesce_vsi(vsi);
7414 
7415 		/* sets up the macvlans but does not "enable" them */
7416 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
7417 					  vdev);
7418 		if (ret)
7419 			return ERR_PTR(ret);
7420 
7421 		/* Unquiesce VSI */
7422 		i40e_unquiesce_vsi(vsi);
7423 	}
7424 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
7425 					    vsi->macvlan_cnt);
7426 	if (avail_macvlan >= I40E_MAX_MACVLANS)
7427 		return ERR_PTR(-EBUSY);
7428 
7429 	/* create the fwd struct */
7430 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
7431 	if (!fwd)
7432 		return ERR_PTR(-ENOMEM);
7433 
7434 	set_bit(avail_macvlan, vsi->fwd_bitmask);
7435 	fwd->bit_no = avail_macvlan;
7436 	netdev_set_sb_channel(vdev, avail_macvlan);
7437 	fwd->netdev = vdev;
7438 
7439 	if (!netif_running(netdev))
7440 		return fwd;
7441 
7442 	/* Set fwd ring up */
7443 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
7444 	if (ret) {
7445 		/* unbind the queues and drop the subordinate channel config */
7446 		netdev_unbind_sb_channel(netdev, vdev);
7447 		netdev_set_sb_channel(vdev, 0);
7448 
7449 		kfree(fwd);
7450 		return ERR_PTR(-EINVAL);
7451 	}
7452 
7453 	return fwd;
7454 }
7455 
7456 /**
7457  * i40e_del_all_macvlans - Delete all the mac filters on the channels
7458  * @vsi: the VSI we want to access
7459  */
i40e_del_all_macvlans(struct i40e_vsi * vsi)7460 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
7461 {
7462 	struct i40e_channel *ch, *ch_tmp;
7463 	struct i40e_pf *pf = vsi->back;
7464 	struct i40e_hw *hw = &pf->hw;
7465 	int aq_err, ret = 0;
7466 
7467 	if (list_empty(&vsi->macvlan_list))
7468 		return;
7469 
7470 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7471 		if (i40e_is_channel_macvlan(ch)) {
7472 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7473 						      i40e_channel_mac(ch),
7474 						      &aq_err);
7475 			if (!ret) {
7476 				/* Reset queue contexts */
7477 				i40e_reset_ch_rings(vsi, ch);
7478 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7479 				netdev_unbind_sb_channel(vsi->netdev,
7480 							 ch->fwd->netdev);
7481 				netdev_set_sb_channel(ch->fwd->netdev, 0);
7482 				kfree(ch->fwd);
7483 				ch->fwd = NULL;
7484 			}
7485 		}
7486 	}
7487 }
7488 
7489 /**
7490  * i40e_fwd_del - delete macvlan interfaces
7491  * @netdev: net device to configure
7492  * @vdev: macvlan netdevice
7493  */
i40e_fwd_del(struct net_device * netdev,void * vdev)7494 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
7495 {
7496 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7497 	struct i40e_fwd_adapter *fwd = vdev;
7498 	struct i40e_channel *ch, *ch_tmp;
7499 	struct i40e_vsi *vsi = np->vsi;
7500 	struct i40e_pf *pf = vsi->back;
7501 	struct i40e_hw *hw = &pf->hw;
7502 	int aq_err, ret = 0;
7503 
7504 	/* Find the channel associated with the macvlan and del mac filter */
7505 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7506 		if (i40e_is_channel_macvlan(ch) &&
7507 		    ether_addr_equal(i40e_channel_mac(ch),
7508 				     fwd->netdev->dev_addr)) {
7509 			ret = i40e_del_macvlan_filter(hw, ch->seid,
7510 						      i40e_channel_mac(ch),
7511 						      &aq_err);
7512 			if (!ret) {
7513 				/* Reset queue contexts */
7514 				i40e_reset_ch_rings(vsi, ch);
7515 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7516 				netdev_unbind_sb_channel(netdev, fwd->netdev);
7517 				netdev_set_sb_channel(fwd->netdev, 0);
7518 				kfree(ch->fwd);
7519 				ch->fwd = NULL;
7520 			} else {
7521 				dev_info(&pf->pdev->dev,
7522 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
7523 					  i40e_stat_str(hw, ret),
7524 					  i40e_aq_str(hw, aq_err));
7525 			}
7526 			break;
7527 		}
7528 	}
7529 }
7530 
7531 /**
7532  * i40e_setup_tc - configure multiple traffic classes
7533  * @netdev: net device to configure
7534  * @type_data: tc offload data
7535  **/
i40e_setup_tc(struct net_device * netdev,void * type_data)7536 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
7537 {
7538 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
7539 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7540 	struct i40e_vsi *vsi = np->vsi;
7541 	struct i40e_pf *pf = vsi->back;
7542 	u8 enabled_tc = 0, num_tc, hw;
7543 	bool need_reset = false;
7544 	int old_queue_pairs;
7545 	int ret = -EINVAL;
7546 	u16 mode;
7547 	int i;
7548 
7549 	old_queue_pairs = vsi->num_queue_pairs;
7550 	num_tc = mqprio_qopt->qopt.num_tc;
7551 	hw = mqprio_qopt->qopt.hw;
7552 	mode = mqprio_qopt->mode;
7553 	if (!hw) {
7554 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7555 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
7556 		goto config_tc;
7557 	}
7558 
7559 	/* Check if MFP enabled */
7560 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
7561 		netdev_info(netdev,
7562 			    "Configuring TC not supported in MFP mode\n");
7563 		return ret;
7564 	}
7565 	switch (mode) {
7566 	case TC_MQPRIO_MODE_DCB:
7567 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
7568 
7569 		/* Check if DCB enabled to continue */
7570 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7571 			netdev_info(netdev,
7572 				    "DCB is not enabled for adapter\n");
7573 			return ret;
7574 		}
7575 
7576 		/* Check whether tc count is within enabled limit */
7577 		if (num_tc > i40e_pf_get_num_tc(pf)) {
7578 			netdev_info(netdev,
7579 				    "TC count greater than enabled on link for adapter\n");
7580 			return ret;
7581 		}
7582 		break;
7583 	case TC_MQPRIO_MODE_CHANNEL:
7584 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
7585 			netdev_info(netdev,
7586 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
7587 			return ret;
7588 		}
7589 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
7590 			return ret;
7591 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
7592 		if (ret)
7593 			return ret;
7594 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
7595 		       sizeof(*mqprio_qopt));
7596 		pf->flags |= I40E_FLAG_TC_MQPRIO;
7597 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7598 		break;
7599 	default:
7600 		return -EINVAL;
7601 	}
7602 
7603 config_tc:
7604 	/* Generate TC map for number of tc requested */
7605 	for (i = 0; i < num_tc; i++)
7606 		enabled_tc |= BIT(i);
7607 
7608 	/* Requesting same TC configuration as already enabled */
7609 	if (enabled_tc == vsi->tc_config.enabled_tc &&
7610 	    mode != TC_MQPRIO_MODE_CHANNEL)
7611 		return 0;
7612 
7613 	/* Quiesce VSI queues */
7614 	i40e_quiesce_vsi(vsi);
7615 
7616 	if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
7617 		i40e_remove_queue_channels(vsi);
7618 
7619 	/* Configure VSI for enabled TCs */
7620 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
7621 	if (ret) {
7622 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
7623 			    vsi->seid);
7624 		need_reset = true;
7625 		goto exit;
7626 	} else if (enabled_tc &&
7627 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
7628 		netdev_info(netdev,
7629 			    "Failed to create channel. Override queues (%u) not power of 2\n",
7630 			    vsi->tc_config.tc_info[0].qcount);
7631 		ret = -EINVAL;
7632 		need_reset = true;
7633 		goto exit;
7634 	}
7635 
7636 	dev_info(&vsi->back->pdev->dev,
7637 		 "Setup channel (id:%u) utilizing num_queues %d\n",
7638 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
7639 
7640 	if (pf->flags & I40E_FLAG_TC_MQPRIO) {
7641 		if (vsi->mqprio_qopt.max_rate[0]) {
7642 			u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
7643 
7644 			do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
7645 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
7646 			if (!ret) {
7647 				u64 credits = max_tx_rate;
7648 
7649 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
7650 				dev_dbg(&vsi->back->pdev->dev,
7651 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
7652 					max_tx_rate,
7653 					credits,
7654 					vsi->seid);
7655 			} else {
7656 				need_reset = true;
7657 				goto exit;
7658 			}
7659 		}
7660 		ret = i40e_configure_queue_channels(vsi);
7661 		if (ret) {
7662 			vsi->num_queue_pairs = old_queue_pairs;
7663 			netdev_info(netdev,
7664 				    "Failed configuring queue channels\n");
7665 			need_reset = true;
7666 			goto exit;
7667 		}
7668 	}
7669 
7670 exit:
7671 	/* Reset the configuration data to defaults, only TC0 is enabled */
7672 	if (need_reset) {
7673 		i40e_vsi_set_default_tc_config(vsi);
7674 		need_reset = false;
7675 	}
7676 
7677 	/* Unquiesce VSI */
7678 	i40e_unquiesce_vsi(vsi);
7679 	return ret;
7680 }
7681 
7682 /**
7683  * i40e_set_cld_element - sets cloud filter element data
7684  * @filter: cloud filter rule
7685  * @cld: ptr to cloud filter element data
7686  *
7687  * This is helper function to copy data into cloud filter element
7688  **/
7689 static inline void
i40e_set_cld_element(struct i40e_cloud_filter * filter,struct i40e_aqc_cloud_filters_element_data * cld)7690 i40e_set_cld_element(struct i40e_cloud_filter *filter,
7691 		     struct i40e_aqc_cloud_filters_element_data *cld)
7692 {
7693 	u32 ipa;
7694 	int i;
7695 
7696 	memset(cld, 0, sizeof(*cld));
7697 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
7698 	ether_addr_copy(cld->inner_mac, filter->src_mac);
7699 
7700 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
7701 		return;
7702 
7703 	if (filter->n_proto == ETH_P_IPV6) {
7704 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
7705 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
7706 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
7707 
7708 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
7709 		}
7710 	} else {
7711 		ipa = be32_to_cpu(filter->dst_ipv4);
7712 
7713 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
7714 	}
7715 
7716 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
7717 
7718 	/* tenant_id is not supported by FW now, once the support is enabled
7719 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
7720 	 */
7721 	if (filter->tenant_id)
7722 		return;
7723 }
7724 
7725 /**
7726  * i40e_add_del_cloud_filter - Add/del cloud filter
7727  * @vsi: pointer to VSI
7728  * @filter: cloud filter rule
7729  * @add: if true, add, if false, delete
7730  *
7731  * Add or delete a cloud filter for a specific flow spec.
7732  * Returns 0 if the filter were successfully added.
7733  **/
i40e_add_del_cloud_filter(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)7734 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
7735 			      struct i40e_cloud_filter *filter, bool add)
7736 {
7737 	struct i40e_aqc_cloud_filters_element_data cld_filter;
7738 	struct i40e_pf *pf = vsi->back;
7739 	int ret;
7740 	static const u16 flag_table[128] = {
7741 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
7742 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
7743 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
7744 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
7745 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
7746 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
7747 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
7748 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
7749 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
7750 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
7751 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
7752 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
7753 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
7754 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
7755 	};
7756 
7757 	if (filter->flags >= ARRAY_SIZE(flag_table))
7758 		return I40E_ERR_CONFIG;
7759 
7760 	memset(&cld_filter, 0, sizeof(cld_filter));
7761 
7762 	/* copy element needed to add cloud filter from filter */
7763 	i40e_set_cld_element(filter, &cld_filter);
7764 
7765 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
7766 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
7767 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
7768 
7769 	if (filter->n_proto == ETH_P_IPV6)
7770 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7771 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7772 	else
7773 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
7774 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7775 
7776 	if (add)
7777 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
7778 						&cld_filter, 1);
7779 	else
7780 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
7781 						&cld_filter, 1);
7782 	if (ret)
7783 		dev_dbg(&pf->pdev->dev,
7784 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
7785 			add ? "add" : "delete", filter->dst_port, ret,
7786 			pf->hw.aq.asq_last_status);
7787 	else
7788 		dev_info(&pf->pdev->dev,
7789 			 "%s cloud filter for VSI: %d\n",
7790 			 add ? "Added" : "Deleted", filter->seid);
7791 	return ret;
7792 }
7793 
7794 /**
7795  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
7796  * @vsi: pointer to VSI
7797  * @filter: cloud filter rule
7798  * @add: if true, add, if false, delete
7799  *
7800  * Add or delete a cloud filter for a specific flow spec using big buffer.
7801  * Returns 0 if the filter were successfully added.
7802  **/
i40e_add_del_cloud_filter_big_buf(struct i40e_vsi * vsi,struct i40e_cloud_filter * filter,bool add)7803 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
7804 				      struct i40e_cloud_filter *filter,
7805 				      bool add)
7806 {
7807 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
7808 	struct i40e_pf *pf = vsi->back;
7809 	int ret;
7810 
7811 	/* Both (src/dst) valid mac_addr are not supported */
7812 	if ((is_valid_ether_addr(filter->dst_mac) &&
7813 	     is_valid_ether_addr(filter->src_mac)) ||
7814 	    (is_multicast_ether_addr(filter->dst_mac) &&
7815 	     is_multicast_ether_addr(filter->src_mac)))
7816 		return -EOPNOTSUPP;
7817 
7818 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
7819 	 * ports are not supported via big buffer now.
7820 	 */
7821 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
7822 		return -EOPNOTSUPP;
7823 
7824 	/* adding filter using src_port/src_ip is not supported at this stage */
7825 	if (filter->src_port ||
7826 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
7827 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
7828 		return -EOPNOTSUPP;
7829 
7830 	memset(&cld_filter, 0, sizeof(cld_filter));
7831 
7832 	/* copy element needed to add cloud filter from filter */
7833 	i40e_set_cld_element(filter, &cld_filter.element);
7834 
7835 	if (is_valid_ether_addr(filter->dst_mac) ||
7836 	    is_valid_ether_addr(filter->src_mac) ||
7837 	    is_multicast_ether_addr(filter->dst_mac) ||
7838 	    is_multicast_ether_addr(filter->src_mac)) {
7839 		/* MAC + IP : unsupported mode */
7840 		if (filter->dst_ipv4)
7841 			return -EOPNOTSUPP;
7842 
7843 		/* since we validated that L4 port must be valid before
7844 		 * we get here, start with respective "flags" value
7845 		 * and update if vlan is present or not
7846 		 */
7847 		cld_filter.element.flags =
7848 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
7849 
7850 		if (filter->vlan_id) {
7851 			cld_filter.element.flags =
7852 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
7853 		}
7854 
7855 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
7856 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
7857 		cld_filter.element.flags =
7858 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
7859 		if (filter->n_proto == ETH_P_IPV6)
7860 			cld_filter.element.flags |=
7861 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
7862 		else
7863 			cld_filter.element.flags |=
7864 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
7865 	} else {
7866 		dev_err(&pf->pdev->dev,
7867 			"either mac or ip has to be valid for cloud filter\n");
7868 		return -EINVAL;
7869 	}
7870 
7871 	/* Now copy L4 port in Byte 6..7 in general fields */
7872 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
7873 						be16_to_cpu(filter->dst_port);
7874 
7875 	if (add) {
7876 		/* Validate current device switch mode, change if necessary */
7877 		ret = i40e_validate_and_set_switch_mode(vsi);
7878 		if (ret) {
7879 			dev_err(&pf->pdev->dev,
7880 				"failed to set switch mode, ret %d\n",
7881 				ret);
7882 			return ret;
7883 		}
7884 
7885 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
7886 						   &cld_filter, 1);
7887 	} else {
7888 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
7889 						   &cld_filter, 1);
7890 	}
7891 
7892 	if (ret)
7893 		dev_dbg(&pf->pdev->dev,
7894 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
7895 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
7896 	else
7897 		dev_info(&pf->pdev->dev,
7898 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
7899 			 add ? "add" : "delete", filter->seid,
7900 			 ntohs(filter->dst_port));
7901 	return ret;
7902 }
7903 
7904 /**
7905  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
7906  * @vsi: Pointer to VSI
7907  * @f: Pointer to struct flow_cls_offload
7908  * @filter: Pointer to cloud filter structure
7909  *
7910  **/
i40e_parse_cls_flower(struct i40e_vsi * vsi,struct flow_cls_offload * f,struct i40e_cloud_filter * filter)7911 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
7912 				 struct flow_cls_offload *f,
7913 				 struct i40e_cloud_filter *filter)
7914 {
7915 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
7916 	struct flow_dissector *dissector = rule->match.dissector;
7917 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
7918 	struct i40e_pf *pf = vsi->back;
7919 	u8 field_flags = 0;
7920 
7921 	if (dissector->used_keys &
7922 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
7923 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
7924 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
7925 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
7926 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
7927 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
7928 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
7929 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
7930 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
7931 			dissector->used_keys);
7932 		return -EOPNOTSUPP;
7933 	}
7934 
7935 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
7936 		struct flow_match_enc_keyid match;
7937 
7938 		flow_rule_match_enc_keyid(rule, &match);
7939 		if (match.mask->keyid != 0)
7940 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
7941 
7942 		filter->tenant_id = be32_to_cpu(match.key->keyid);
7943 	}
7944 
7945 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
7946 		struct flow_match_basic match;
7947 
7948 		flow_rule_match_basic(rule, &match);
7949 		n_proto_key = ntohs(match.key->n_proto);
7950 		n_proto_mask = ntohs(match.mask->n_proto);
7951 
7952 		if (n_proto_key == ETH_P_ALL) {
7953 			n_proto_key = 0;
7954 			n_proto_mask = 0;
7955 		}
7956 		filter->n_proto = n_proto_key & n_proto_mask;
7957 		filter->ip_proto = match.key->ip_proto;
7958 	}
7959 
7960 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
7961 		struct flow_match_eth_addrs match;
7962 
7963 		flow_rule_match_eth_addrs(rule, &match);
7964 
7965 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
7966 		if (!is_zero_ether_addr(match.mask->dst)) {
7967 			if (is_broadcast_ether_addr(match.mask->dst)) {
7968 				field_flags |= I40E_CLOUD_FIELD_OMAC;
7969 			} else {
7970 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
7971 					match.mask->dst);
7972 				return I40E_ERR_CONFIG;
7973 			}
7974 		}
7975 
7976 		if (!is_zero_ether_addr(match.mask->src)) {
7977 			if (is_broadcast_ether_addr(match.mask->src)) {
7978 				field_flags |= I40E_CLOUD_FIELD_IMAC;
7979 			} else {
7980 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
7981 					match.mask->src);
7982 				return I40E_ERR_CONFIG;
7983 			}
7984 		}
7985 		ether_addr_copy(filter->dst_mac, match.key->dst);
7986 		ether_addr_copy(filter->src_mac, match.key->src);
7987 	}
7988 
7989 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
7990 		struct flow_match_vlan match;
7991 
7992 		flow_rule_match_vlan(rule, &match);
7993 		if (match.mask->vlan_id) {
7994 			if (match.mask->vlan_id == VLAN_VID_MASK) {
7995 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
7996 
7997 			} else {
7998 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
7999 					match.mask->vlan_id);
8000 				return I40E_ERR_CONFIG;
8001 			}
8002 		}
8003 
8004 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8005 	}
8006 
8007 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8008 		struct flow_match_control match;
8009 
8010 		flow_rule_match_control(rule, &match);
8011 		addr_type = match.key->addr_type;
8012 	}
8013 
8014 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8015 		struct flow_match_ipv4_addrs match;
8016 
8017 		flow_rule_match_ipv4_addrs(rule, &match);
8018 		if (match.mask->dst) {
8019 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8020 				field_flags |= I40E_CLOUD_FIELD_IIP;
8021 			} else {
8022 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8023 					&match.mask->dst);
8024 				return I40E_ERR_CONFIG;
8025 			}
8026 		}
8027 
8028 		if (match.mask->src) {
8029 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8030 				field_flags |= I40E_CLOUD_FIELD_IIP;
8031 			} else {
8032 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8033 					&match.mask->src);
8034 				return I40E_ERR_CONFIG;
8035 			}
8036 		}
8037 
8038 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8039 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8040 			return I40E_ERR_CONFIG;
8041 		}
8042 		filter->dst_ipv4 = match.key->dst;
8043 		filter->src_ipv4 = match.key->src;
8044 	}
8045 
8046 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8047 		struct flow_match_ipv6_addrs match;
8048 
8049 		flow_rule_match_ipv6_addrs(rule, &match);
8050 
8051 		/* src and dest IPV6 address should not be LOOPBACK
8052 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8053 		 */
8054 		if (ipv6_addr_loopback(&match.key->dst) ||
8055 		    ipv6_addr_loopback(&match.key->src)) {
8056 			dev_err(&pf->pdev->dev,
8057 				"Bad ipv6, addr is LOOPBACK\n");
8058 			return I40E_ERR_CONFIG;
8059 		}
8060 		if (!ipv6_addr_any(&match.mask->dst) ||
8061 		    !ipv6_addr_any(&match.mask->src))
8062 			field_flags |= I40E_CLOUD_FIELD_IIP;
8063 
8064 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8065 		       sizeof(filter->src_ipv6));
8066 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8067 		       sizeof(filter->dst_ipv6));
8068 	}
8069 
8070 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8071 		struct flow_match_ports match;
8072 
8073 		flow_rule_match_ports(rule, &match);
8074 		if (match.mask->src) {
8075 			if (match.mask->src == cpu_to_be16(0xffff)) {
8076 				field_flags |= I40E_CLOUD_FIELD_IIP;
8077 			} else {
8078 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8079 					be16_to_cpu(match.mask->src));
8080 				return I40E_ERR_CONFIG;
8081 			}
8082 		}
8083 
8084 		if (match.mask->dst) {
8085 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8086 				field_flags |= I40E_CLOUD_FIELD_IIP;
8087 			} else {
8088 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8089 					be16_to_cpu(match.mask->dst));
8090 				return I40E_ERR_CONFIG;
8091 			}
8092 		}
8093 
8094 		filter->dst_port = match.key->dst;
8095 		filter->src_port = match.key->src;
8096 
8097 		switch (filter->ip_proto) {
8098 		case IPPROTO_TCP:
8099 		case IPPROTO_UDP:
8100 			break;
8101 		default:
8102 			dev_err(&pf->pdev->dev,
8103 				"Only UDP and TCP transport are supported\n");
8104 			return -EINVAL;
8105 		}
8106 	}
8107 	filter->flags = field_flags;
8108 	return 0;
8109 }
8110 
8111 /**
8112  * i40e_handle_tclass: Forward to a traffic class on the device
8113  * @vsi: Pointer to VSI
8114  * @tc: traffic class index on the device
8115  * @filter: Pointer to cloud filter structure
8116  *
8117  **/
i40e_handle_tclass(struct i40e_vsi * vsi,u32 tc,struct i40e_cloud_filter * filter)8118 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8119 			      struct i40e_cloud_filter *filter)
8120 {
8121 	struct i40e_channel *ch, *ch_tmp;
8122 
8123 	/* direct to a traffic class on the same device */
8124 	if (tc == 0) {
8125 		filter->seid = vsi->seid;
8126 		return 0;
8127 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8128 		if (!filter->dst_port) {
8129 			dev_err(&vsi->back->pdev->dev,
8130 				"Specify destination port to direct to traffic class that is not default\n");
8131 			return -EINVAL;
8132 		}
8133 		if (list_empty(&vsi->ch_list))
8134 			return -EINVAL;
8135 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8136 					 list) {
8137 			if (ch->seid == vsi->tc_seid_map[tc])
8138 				filter->seid = ch->seid;
8139 		}
8140 		return 0;
8141 	}
8142 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8143 	return -EINVAL;
8144 }
8145 
8146 /**
8147  * i40e_configure_clsflower - Configure tc flower filters
8148  * @vsi: Pointer to VSI
8149  * @cls_flower: Pointer to struct flow_cls_offload
8150  *
8151  **/
i40e_configure_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8152 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8153 				    struct flow_cls_offload *cls_flower)
8154 {
8155 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8156 	struct i40e_cloud_filter *filter = NULL;
8157 	struct i40e_pf *pf = vsi->back;
8158 	int err = 0;
8159 
8160 	if (tc < 0) {
8161 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8162 		return -EOPNOTSUPP;
8163 	}
8164 
8165 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8166 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8167 		return -EBUSY;
8168 
8169 	if (pf->fdir_pf_active_filters ||
8170 	    (!hlist_empty(&pf->fdir_filter_list))) {
8171 		dev_err(&vsi->back->pdev->dev,
8172 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8173 		return -EINVAL;
8174 	}
8175 
8176 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8177 		dev_err(&vsi->back->pdev->dev,
8178 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8179 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8180 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8181 	}
8182 
8183 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8184 	if (!filter)
8185 		return -ENOMEM;
8186 
8187 	filter->cookie = cls_flower->cookie;
8188 
8189 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8190 	if (err < 0)
8191 		goto err;
8192 
8193 	err = i40e_handle_tclass(vsi, tc, filter);
8194 	if (err < 0)
8195 		goto err;
8196 
8197 	/* Add cloud filter */
8198 	if (filter->dst_port)
8199 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8200 	else
8201 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8202 
8203 	if (err) {
8204 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8205 			err);
8206 		goto err;
8207 	}
8208 
8209 	/* add filter to the ordered list */
8210 	INIT_HLIST_NODE(&filter->cloud_node);
8211 
8212 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8213 
8214 	pf->num_cloud_filters++;
8215 
8216 	return err;
8217 err:
8218 	kfree(filter);
8219 	return err;
8220 }
8221 
8222 /**
8223  * i40e_find_cloud_filter - Find the could filter in the list
8224  * @vsi: Pointer to VSI
8225  * @cookie: filter specific cookie
8226  *
8227  **/
i40e_find_cloud_filter(struct i40e_vsi * vsi,unsigned long * cookie)8228 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8229 							unsigned long *cookie)
8230 {
8231 	struct i40e_cloud_filter *filter = NULL;
8232 	struct hlist_node *node2;
8233 
8234 	hlist_for_each_entry_safe(filter, node2,
8235 				  &vsi->back->cloud_filter_list, cloud_node)
8236 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8237 			return filter;
8238 	return NULL;
8239 }
8240 
8241 /**
8242  * i40e_delete_clsflower - Remove tc flower filters
8243  * @vsi: Pointer to VSI
8244  * @cls_flower: Pointer to struct flow_cls_offload
8245  *
8246  **/
i40e_delete_clsflower(struct i40e_vsi * vsi,struct flow_cls_offload * cls_flower)8247 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8248 				 struct flow_cls_offload *cls_flower)
8249 {
8250 	struct i40e_cloud_filter *filter = NULL;
8251 	struct i40e_pf *pf = vsi->back;
8252 	int err = 0;
8253 
8254 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8255 
8256 	if (!filter)
8257 		return -EINVAL;
8258 
8259 	hash_del(&filter->cloud_node);
8260 
8261 	if (filter->dst_port)
8262 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8263 	else
8264 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8265 
8266 	kfree(filter);
8267 	if (err) {
8268 		dev_err(&pf->pdev->dev,
8269 			"Failed to delete cloud filter, err %s\n",
8270 			i40e_stat_str(&pf->hw, err));
8271 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8272 	}
8273 
8274 	pf->num_cloud_filters--;
8275 	if (!pf->num_cloud_filters)
8276 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8277 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8278 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8279 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8280 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8281 		}
8282 	return 0;
8283 }
8284 
8285 /**
8286  * i40e_setup_tc_cls_flower - flower classifier offloads
8287  * @np: net device to configure
8288  * @cls_flower: offload data
8289  **/
i40e_setup_tc_cls_flower(struct i40e_netdev_priv * np,struct flow_cls_offload * cls_flower)8290 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8291 				    struct flow_cls_offload *cls_flower)
8292 {
8293 	struct i40e_vsi *vsi = np->vsi;
8294 
8295 	switch (cls_flower->command) {
8296 	case FLOW_CLS_REPLACE:
8297 		return i40e_configure_clsflower(vsi, cls_flower);
8298 	case FLOW_CLS_DESTROY:
8299 		return i40e_delete_clsflower(vsi, cls_flower);
8300 	case FLOW_CLS_STATS:
8301 		return -EOPNOTSUPP;
8302 	default:
8303 		return -EOPNOTSUPP;
8304 	}
8305 }
8306 
i40e_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)8307 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8308 				  void *cb_priv)
8309 {
8310 	struct i40e_netdev_priv *np = cb_priv;
8311 
8312 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8313 		return -EOPNOTSUPP;
8314 
8315 	switch (type) {
8316 	case TC_SETUP_CLSFLOWER:
8317 		return i40e_setup_tc_cls_flower(np, type_data);
8318 
8319 	default:
8320 		return -EOPNOTSUPP;
8321 	}
8322 }
8323 
8324 static LIST_HEAD(i40e_block_cb_list);
8325 
__i40e_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)8326 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8327 			   void *type_data)
8328 {
8329 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8330 
8331 	switch (type) {
8332 	case TC_SETUP_QDISC_MQPRIO:
8333 		return i40e_setup_tc(netdev, type_data);
8334 	case TC_SETUP_BLOCK:
8335 		return flow_block_cb_setup_simple(type_data,
8336 						  &i40e_block_cb_list,
8337 						  i40e_setup_tc_block_cb,
8338 						  np, np, true);
8339 	default:
8340 		return -EOPNOTSUPP;
8341 	}
8342 }
8343 
8344 /**
8345  * i40e_open - Called when a network interface is made active
8346  * @netdev: network interface device structure
8347  *
8348  * The open entry point is called when a network interface is made
8349  * active by the system (IFF_UP).  At this point all resources needed
8350  * for transmit and receive operations are allocated, the interrupt
8351  * handler is registered with the OS, the netdev watchdog subtask is
8352  * enabled, and the stack is notified that the interface is ready.
8353  *
8354  * Returns 0 on success, negative value on failure
8355  **/
i40e_open(struct net_device * netdev)8356 int i40e_open(struct net_device *netdev)
8357 {
8358 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8359 	struct i40e_vsi *vsi = np->vsi;
8360 	struct i40e_pf *pf = vsi->back;
8361 	int err;
8362 
8363 	/* disallow open during test or if eeprom is broken */
8364 	if (test_bit(__I40E_TESTING, pf->state) ||
8365 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8366 		return -EBUSY;
8367 
8368 	netif_carrier_off(netdev);
8369 
8370 	if (i40e_force_link_state(pf, true))
8371 		return -EAGAIN;
8372 
8373 	err = i40e_vsi_open(vsi);
8374 	if (err)
8375 		return err;
8376 
8377 	/* configure global TSO hardware offload settings */
8378 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8379 						       TCP_FLAG_FIN) >> 16);
8380 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8381 						       TCP_FLAG_FIN |
8382 						       TCP_FLAG_CWR) >> 16);
8383 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8384 
8385 	udp_tunnel_get_rx_info(netdev);
8386 
8387 	return 0;
8388 }
8389 
8390 /**
8391  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8392  * @vsi: vsi structure
8393  *
8394  * This updates netdev's number of tx/rx queues
8395  *
8396  * Returns status of setting tx/rx queues
8397  **/
i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi * vsi)8398 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8399 {
8400 	int ret;
8401 
8402 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8403 					   vsi->num_queue_pairs);
8404 	if (ret)
8405 		return ret;
8406 
8407 	return netif_set_real_num_tx_queues(vsi->netdev,
8408 					    vsi->num_queue_pairs);
8409 }
8410 
8411 /**
8412  * i40e_vsi_open -
8413  * @vsi: the VSI to open
8414  *
8415  * Finish initialization of the VSI.
8416  *
8417  * Returns 0 on success, negative value on failure
8418  *
8419  * Note: expects to be called while under rtnl_lock()
8420  **/
i40e_vsi_open(struct i40e_vsi * vsi)8421 int i40e_vsi_open(struct i40e_vsi *vsi)
8422 {
8423 	struct i40e_pf *pf = vsi->back;
8424 	char int_name[I40E_INT_NAME_STR_LEN];
8425 	int err;
8426 
8427 	/* allocate descriptors */
8428 	err = i40e_vsi_setup_tx_resources(vsi);
8429 	if (err)
8430 		goto err_setup_tx;
8431 	err = i40e_vsi_setup_rx_resources(vsi);
8432 	if (err)
8433 		goto err_setup_rx;
8434 
8435 	err = i40e_vsi_configure(vsi);
8436 	if (err)
8437 		goto err_setup_rx;
8438 
8439 	if (vsi->netdev) {
8440 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
8441 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
8442 		err = i40e_vsi_request_irq(vsi, int_name);
8443 		if (err)
8444 			goto err_setup_rx;
8445 
8446 		/* Notify the stack of the actual queue counts. */
8447 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
8448 		if (err)
8449 			goto err_set_queues;
8450 
8451 	} else if (vsi->type == I40E_VSI_FDIR) {
8452 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
8453 			 dev_driver_string(&pf->pdev->dev),
8454 			 dev_name(&pf->pdev->dev));
8455 		err = i40e_vsi_request_irq(vsi, int_name);
8456 		if (err)
8457 			goto err_setup_rx;
8458 
8459 	} else {
8460 		err = -EINVAL;
8461 		goto err_setup_rx;
8462 	}
8463 
8464 	err = i40e_up_complete(vsi);
8465 	if (err)
8466 		goto err_up_complete;
8467 
8468 	return 0;
8469 
8470 err_up_complete:
8471 	i40e_down(vsi);
8472 err_set_queues:
8473 	i40e_vsi_free_irq(vsi);
8474 err_setup_rx:
8475 	i40e_vsi_free_rx_resources(vsi);
8476 err_setup_tx:
8477 	i40e_vsi_free_tx_resources(vsi);
8478 	if (vsi == pf->vsi[pf->lan_vsi])
8479 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
8480 
8481 	return err;
8482 }
8483 
8484 /**
8485  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
8486  * @pf: Pointer to PF
8487  *
8488  * This function destroys the hlist where all the Flow Director
8489  * filters were saved.
8490  **/
i40e_fdir_filter_exit(struct i40e_pf * pf)8491 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
8492 {
8493 	struct i40e_fdir_filter *filter;
8494 	struct i40e_flex_pit *pit_entry, *tmp;
8495 	struct hlist_node *node2;
8496 
8497 	hlist_for_each_entry_safe(filter, node2,
8498 				  &pf->fdir_filter_list, fdir_node) {
8499 		hlist_del(&filter->fdir_node);
8500 		kfree(filter);
8501 	}
8502 
8503 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
8504 		list_del(&pit_entry->list);
8505 		kfree(pit_entry);
8506 	}
8507 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
8508 
8509 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
8510 		list_del(&pit_entry->list);
8511 		kfree(pit_entry);
8512 	}
8513 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
8514 
8515 	pf->fdir_pf_active_filters = 0;
8516 	pf->fd_tcp4_filter_cnt = 0;
8517 	pf->fd_udp4_filter_cnt = 0;
8518 	pf->fd_sctp4_filter_cnt = 0;
8519 	pf->fd_ip4_filter_cnt = 0;
8520 
8521 	/* Reprogram the default input set for TCP/IPv4 */
8522 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8523 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8524 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8525 
8526 	/* Reprogram the default input set for UDP/IPv4 */
8527 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
8528 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8529 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8530 
8531 	/* Reprogram the default input set for SCTP/IPv4 */
8532 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
8533 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8534 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8535 
8536 	/* Reprogram the default input set for Other/IPv4 */
8537 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
8538 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8539 
8540 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
8541 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
8542 }
8543 
8544 /**
8545  * i40e_cloud_filter_exit - Cleans up the cloud filters
8546  * @pf: Pointer to PF
8547  *
8548  * This function destroys the hlist where all the cloud filters
8549  * were saved.
8550  **/
i40e_cloud_filter_exit(struct i40e_pf * pf)8551 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
8552 {
8553 	struct i40e_cloud_filter *cfilter;
8554 	struct hlist_node *node;
8555 
8556 	hlist_for_each_entry_safe(cfilter, node,
8557 				  &pf->cloud_filter_list, cloud_node) {
8558 		hlist_del(&cfilter->cloud_node);
8559 		kfree(cfilter);
8560 	}
8561 	pf->num_cloud_filters = 0;
8562 
8563 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8564 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8565 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8566 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8567 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8568 	}
8569 }
8570 
8571 /**
8572  * i40e_close - Disables a network interface
8573  * @netdev: network interface device structure
8574  *
8575  * The close entry point is called when an interface is de-activated
8576  * by the OS.  The hardware is still under the driver's control, but
8577  * this netdev interface is disabled.
8578  *
8579  * Returns 0, this is not allowed to fail
8580  **/
i40e_close(struct net_device * netdev)8581 int i40e_close(struct net_device *netdev)
8582 {
8583 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8584 	struct i40e_vsi *vsi = np->vsi;
8585 
8586 	i40e_vsi_close(vsi);
8587 
8588 	return 0;
8589 }
8590 
8591 /**
8592  * i40e_do_reset - Start a PF or Core Reset sequence
8593  * @pf: board private structure
8594  * @reset_flags: which reset is requested
8595  * @lock_acquired: indicates whether or not the lock has been acquired
8596  * before this function was called.
8597  *
8598  * The essential difference in resets is that the PF Reset
8599  * doesn't clear the packet buffers, doesn't reset the PE
8600  * firmware, and doesn't bother the other PFs on the chip.
8601  **/
i40e_do_reset(struct i40e_pf * pf,u32 reset_flags,bool lock_acquired)8602 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
8603 {
8604 	u32 val;
8605 
8606 	/* do the biggest reset indicated */
8607 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
8608 
8609 		/* Request a Global Reset
8610 		 *
8611 		 * This will start the chip's countdown to the actual full
8612 		 * chip reset event, and a warning interrupt to be sent
8613 		 * to all PFs, including the requestor.  Our handler
8614 		 * for the warning interrupt will deal with the shutdown
8615 		 * and recovery of the switch setup.
8616 		 */
8617 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
8618 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8619 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
8620 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8621 
8622 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
8623 
8624 		/* Request a Core Reset
8625 		 *
8626 		 * Same as Global Reset, except does *not* include the MAC/PHY
8627 		 */
8628 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
8629 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
8630 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
8631 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
8632 		i40e_flush(&pf->hw);
8633 
8634 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
8635 
8636 		/* Request a PF Reset
8637 		 *
8638 		 * Resets only the PF-specific registers
8639 		 *
8640 		 * This goes directly to the tear-down and rebuild of
8641 		 * the switch, since we need to do all the recovery as
8642 		 * for the Core Reset.
8643 		 */
8644 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
8645 		i40e_handle_reset_warning(pf, lock_acquired);
8646 
8647 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
8648 		/* Request a PF Reset
8649 		 *
8650 		 * Resets PF and reinitializes PFs VSI.
8651 		 */
8652 		i40e_prep_for_reset(pf, lock_acquired);
8653 		i40e_reset_and_rebuild(pf, true, lock_acquired);
8654 		dev_info(&pf->pdev->dev,
8655 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
8656 			 "FW LLDP is disabled\n" :
8657 			 "FW LLDP is enabled\n");
8658 
8659 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
8660 		int v;
8661 
8662 		/* Find the VSI(s) that requested a re-init */
8663 		dev_info(&pf->pdev->dev,
8664 			 "VSI reinit requested\n");
8665 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8666 			struct i40e_vsi *vsi = pf->vsi[v];
8667 
8668 			if (vsi != NULL &&
8669 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
8670 					       vsi->state))
8671 				i40e_vsi_reinit_locked(pf->vsi[v]);
8672 		}
8673 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
8674 		int v;
8675 
8676 		/* Find the VSI(s) that needs to be brought down */
8677 		dev_info(&pf->pdev->dev, "VSI down requested\n");
8678 		for (v = 0; v < pf->num_alloc_vsi; v++) {
8679 			struct i40e_vsi *vsi = pf->vsi[v];
8680 
8681 			if (vsi != NULL &&
8682 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
8683 					       vsi->state)) {
8684 				set_bit(__I40E_VSI_DOWN, vsi->state);
8685 				i40e_down(vsi);
8686 			}
8687 		}
8688 	} else {
8689 		dev_info(&pf->pdev->dev,
8690 			 "bad reset request 0x%08x\n", reset_flags);
8691 	}
8692 }
8693 
8694 #ifdef CONFIG_I40E_DCB
8695 /**
8696  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
8697  * @pf: board private structure
8698  * @old_cfg: current DCB config
8699  * @new_cfg: new DCB config
8700  **/
i40e_dcb_need_reconfig(struct i40e_pf * pf,struct i40e_dcbx_config * old_cfg,struct i40e_dcbx_config * new_cfg)8701 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
8702 			    struct i40e_dcbx_config *old_cfg,
8703 			    struct i40e_dcbx_config *new_cfg)
8704 {
8705 	bool need_reconfig = false;
8706 
8707 	/* Check if ETS configuration has changed */
8708 	if (memcmp(&new_cfg->etscfg,
8709 		   &old_cfg->etscfg,
8710 		   sizeof(new_cfg->etscfg))) {
8711 		/* If Priority Table has changed reconfig is needed */
8712 		if (memcmp(&new_cfg->etscfg.prioritytable,
8713 			   &old_cfg->etscfg.prioritytable,
8714 			   sizeof(new_cfg->etscfg.prioritytable))) {
8715 			need_reconfig = true;
8716 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
8717 		}
8718 
8719 		if (memcmp(&new_cfg->etscfg.tcbwtable,
8720 			   &old_cfg->etscfg.tcbwtable,
8721 			   sizeof(new_cfg->etscfg.tcbwtable)))
8722 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
8723 
8724 		if (memcmp(&new_cfg->etscfg.tsatable,
8725 			   &old_cfg->etscfg.tsatable,
8726 			   sizeof(new_cfg->etscfg.tsatable)))
8727 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
8728 	}
8729 
8730 	/* Check if PFC configuration has changed */
8731 	if (memcmp(&new_cfg->pfc,
8732 		   &old_cfg->pfc,
8733 		   sizeof(new_cfg->pfc))) {
8734 		need_reconfig = true;
8735 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
8736 	}
8737 
8738 	/* Check if APP Table has changed */
8739 	if (memcmp(&new_cfg->app,
8740 		   &old_cfg->app,
8741 		   sizeof(new_cfg->app))) {
8742 		need_reconfig = true;
8743 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
8744 	}
8745 
8746 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
8747 	return need_reconfig;
8748 }
8749 
8750 /**
8751  * i40e_handle_lldp_event - Handle LLDP Change MIB event
8752  * @pf: board private structure
8753  * @e: event info posted on ARQ
8754  **/
i40e_handle_lldp_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)8755 static int i40e_handle_lldp_event(struct i40e_pf *pf,
8756 				  struct i40e_arq_event_info *e)
8757 {
8758 	struct i40e_aqc_lldp_get_mib *mib =
8759 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
8760 	struct i40e_hw *hw = &pf->hw;
8761 	struct i40e_dcbx_config tmp_dcbx_cfg;
8762 	bool need_reconfig = false;
8763 	int ret = 0;
8764 	u8 type;
8765 
8766 	/* Not DCB capable or capability disabled */
8767 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
8768 		return ret;
8769 
8770 	/* Ignore if event is not for Nearest Bridge */
8771 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
8772 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
8773 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
8774 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
8775 		return ret;
8776 
8777 	/* Check MIB Type and return if event for Remote MIB update */
8778 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
8779 	dev_dbg(&pf->pdev->dev,
8780 		"LLDP event mib type %s\n", type ? "remote" : "local");
8781 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
8782 		/* Update the remote cached instance and return */
8783 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
8784 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
8785 				&hw->remote_dcbx_config);
8786 		goto exit;
8787 	}
8788 
8789 	/* Store the old configuration */
8790 	tmp_dcbx_cfg = hw->local_dcbx_config;
8791 
8792 	/* Reset the old DCBx configuration data */
8793 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
8794 	/* Get updated DCBX data from firmware */
8795 	ret = i40e_get_dcb_config(&pf->hw);
8796 	if (ret) {
8797 		dev_info(&pf->pdev->dev,
8798 			 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
8799 			 i40e_stat_str(&pf->hw, ret),
8800 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
8801 		goto exit;
8802 	}
8803 
8804 	/* No change detected in DCBX configs */
8805 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
8806 		    sizeof(tmp_dcbx_cfg))) {
8807 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
8808 		goto exit;
8809 	}
8810 
8811 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
8812 					       &hw->local_dcbx_config);
8813 
8814 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
8815 
8816 	if (!need_reconfig)
8817 		goto exit;
8818 
8819 	/* Enable DCB tagging only when more than one TC */
8820 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
8821 		pf->flags |= I40E_FLAG_DCB_ENABLED;
8822 	else
8823 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8824 
8825 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
8826 	/* Reconfiguration needed quiesce all VSIs */
8827 	i40e_pf_quiesce_all_vsi(pf);
8828 
8829 	/* Changes in configuration update VEB/VSI */
8830 	i40e_dcb_reconfigure(pf);
8831 
8832 	ret = i40e_resume_port_tx(pf);
8833 
8834 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
8835 	/* In case of error no point in resuming VSIs */
8836 	if (ret)
8837 		goto exit;
8838 
8839 	/* Wait for the PF's queues to be disabled */
8840 	ret = i40e_pf_wait_queues_disabled(pf);
8841 	if (ret) {
8842 		/* Schedule PF reset to recover */
8843 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
8844 		i40e_service_event_schedule(pf);
8845 	} else {
8846 		i40e_pf_unquiesce_all_vsi(pf);
8847 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
8848 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
8849 	}
8850 
8851 exit:
8852 	return ret;
8853 }
8854 #endif /* CONFIG_I40E_DCB */
8855 
8856 /**
8857  * i40e_do_reset_safe - Protected reset path for userland calls.
8858  * @pf: board private structure
8859  * @reset_flags: which reset is requested
8860  *
8861  **/
i40e_do_reset_safe(struct i40e_pf * pf,u32 reset_flags)8862 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
8863 {
8864 	rtnl_lock();
8865 	i40e_do_reset(pf, reset_flags, true);
8866 	rtnl_unlock();
8867 }
8868 
8869 /**
8870  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
8871  * @pf: board private structure
8872  * @e: event info posted on ARQ
8873  *
8874  * Handler for LAN Queue Overflow Event generated by the firmware for PF
8875  * and VF queues
8876  **/
i40e_handle_lan_overflow_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)8877 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
8878 					   struct i40e_arq_event_info *e)
8879 {
8880 	struct i40e_aqc_lan_overflow *data =
8881 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
8882 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
8883 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
8884 	struct i40e_hw *hw = &pf->hw;
8885 	struct i40e_vf *vf;
8886 	u16 vf_id;
8887 
8888 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
8889 		queue, qtx_ctl);
8890 
8891 	/* Queue belongs to VF, find the VF and issue VF reset */
8892 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
8893 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
8894 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
8895 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
8896 		vf_id -= hw->func_caps.vf_base_id;
8897 		vf = &pf->vf[vf_id];
8898 		i40e_vc_notify_vf_reset(vf);
8899 		/* Allow VF to process pending reset notification */
8900 		msleep(20);
8901 		i40e_reset_vf(vf, false);
8902 	}
8903 }
8904 
8905 /**
8906  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
8907  * @pf: board private structure
8908  **/
i40e_get_cur_guaranteed_fd_count(struct i40e_pf * pf)8909 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
8910 {
8911 	u32 val, fcnt_prog;
8912 
8913 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8914 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
8915 	return fcnt_prog;
8916 }
8917 
8918 /**
8919  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
8920  * @pf: board private structure
8921  **/
i40e_get_current_fd_count(struct i40e_pf * pf)8922 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
8923 {
8924 	u32 val, fcnt_prog;
8925 
8926 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
8927 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
8928 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
8929 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
8930 	return fcnt_prog;
8931 }
8932 
8933 /**
8934  * i40e_get_global_fd_count - Get total FD filters programmed on device
8935  * @pf: board private structure
8936  **/
i40e_get_global_fd_count(struct i40e_pf * pf)8937 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
8938 {
8939 	u32 val, fcnt_prog;
8940 
8941 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
8942 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
8943 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
8944 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
8945 	return fcnt_prog;
8946 }
8947 
8948 /**
8949  * i40e_reenable_fdir_sb - Restore FDir SB capability
8950  * @pf: board private structure
8951  **/
i40e_reenable_fdir_sb(struct i40e_pf * pf)8952 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
8953 {
8954 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
8955 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
8956 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8957 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
8958 }
8959 
8960 /**
8961  * i40e_reenable_fdir_atr - Restore FDir ATR capability
8962  * @pf: board private structure
8963  **/
i40e_reenable_fdir_atr(struct i40e_pf * pf)8964 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
8965 {
8966 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
8967 		/* ATR uses the same filtering logic as SB rules. It only
8968 		 * functions properly if the input set mask is at the default
8969 		 * settings. It is safe to restore the default input set
8970 		 * because there are no active TCPv4 filter rules.
8971 		 */
8972 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
8973 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
8974 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
8975 
8976 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
8977 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
8978 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
8979 	}
8980 }
8981 
8982 /**
8983  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
8984  * @pf: board private structure
8985  * @filter: FDir filter to remove
8986  */
i40e_delete_invalid_filter(struct i40e_pf * pf,struct i40e_fdir_filter * filter)8987 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
8988 				       struct i40e_fdir_filter *filter)
8989 {
8990 	/* Update counters */
8991 	pf->fdir_pf_active_filters--;
8992 	pf->fd_inv = 0;
8993 
8994 	switch (filter->flow_type) {
8995 	case TCP_V4_FLOW:
8996 		pf->fd_tcp4_filter_cnt--;
8997 		break;
8998 	case UDP_V4_FLOW:
8999 		pf->fd_udp4_filter_cnt--;
9000 		break;
9001 	case SCTP_V4_FLOW:
9002 		pf->fd_sctp4_filter_cnt--;
9003 		break;
9004 	case IP_USER_FLOW:
9005 		switch (filter->ip4_proto) {
9006 		case IPPROTO_TCP:
9007 			pf->fd_tcp4_filter_cnt--;
9008 			break;
9009 		case IPPROTO_UDP:
9010 			pf->fd_udp4_filter_cnt--;
9011 			break;
9012 		case IPPROTO_SCTP:
9013 			pf->fd_sctp4_filter_cnt--;
9014 			break;
9015 		case IPPROTO_IP:
9016 			pf->fd_ip4_filter_cnt--;
9017 			break;
9018 		}
9019 		break;
9020 	}
9021 
9022 	/* Remove the filter from the list and free memory */
9023 	hlist_del(&filter->fdir_node);
9024 	kfree(filter);
9025 }
9026 
9027 /**
9028  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9029  * @pf: board private structure
9030  **/
i40e_fdir_check_and_reenable(struct i40e_pf * pf)9031 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9032 {
9033 	struct i40e_fdir_filter *filter;
9034 	u32 fcnt_prog, fcnt_avail;
9035 	struct hlist_node *node;
9036 
9037 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9038 		return;
9039 
9040 	/* Check if we have enough room to re-enable FDir SB capability. */
9041 	fcnt_prog = i40e_get_global_fd_count(pf);
9042 	fcnt_avail = pf->fdir_pf_filter_count;
9043 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9044 	    (pf->fd_add_err == 0) ||
9045 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9046 		i40e_reenable_fdir_sb(pf);
9047 
9048 	/* We should wait for even more space before re-enabling ATR.
9049 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9050 	 * rules active.
9051 	 */
9052 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9053 	    (pf->fd_tcp4_filter_cnt == 0))
9054 		i40e_reenable_fdir_atr(pf);
9055 
9056 	/* if hw had a problem adding a filter, delete it */
9057 	if (pf->fd_inv > 0) {
9058 		hlist_for_each_entry_safe(filter, node,
9059 					  &pf->fdir_filter_list, fdir_node)
9060 			if (filter->fd_id == pf->fd_inv)
9061 				i40e_delete_invalid_filter(pf, filter);
9062 	}
9063 }
9064 
9065 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9066 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9067 /**
9068  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9069  * @pf: board private structure
9070  **/
i40e_fdir_flush_and_replay(struct i40e_pf * pf)9071 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9072 {
9073 	unsigned long min_flush_time;
9074 	int flush_wait_retry = 50;
9075 	bool disable_atr = false;
9076 	int fd_room;
9077 	int reg;
9078 
9079 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9080 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9081 		return;
9082 
9083 	/* If the flush is happening too quick and we have mostly SB rules we
9084 	 * should not re-enable ATR for some time.
9085 	 */
9086 	min_flush_time = pf->fd_flush_timestamp +
9087 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9088 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9089 
9090 	if (!(time_after(jiffies, min_flush_time)) &&
9091 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9092 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9093 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9094 		disable_atr = true;
9095 	}
9096 
9097 	pf->fd_flush_timestamp = jiffies;
9098 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9099 	/* flush all filters */
9100 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9101 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9102 	i40e_flush(&pf->hw);
9103 	pf->fd_flush_cnt++;
9104 	pf->fd_add_err = 0;
9105 	do {
9106 		/* Check FD flush status every 5-6msec */
9107 		usleep_range(5000, 6000);
9108 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9109 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9110 			break;
9111 	} while (flush_wait_retry--);
9112 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9113 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9114 	} else {
9115 		/* replay sideband filters */
9116 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9117 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9118 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9119 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9120 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9121 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9122 	}
9123 }
9124 
9125 /**
9126  * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
9127  * @pf: board private structure
9128  **/
i40e_get_current_atr_cnt(struct i40e_pf * pf)9129 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9130 {
9131 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9132 }
9133 
9134 /**
9135  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9136  * @pf: board private structure
9137  **/
i40e_fdir_reinit_subtask(struct i40e_pf * pf)9138 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9139 {
9140 
9141 	/* if interface is down do nothing */
9142 	if (test_bit(__I40E_DOWN, pf->state))
9143 		return;
9144 
9145 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9146 		i40e_fdir_flush_and_replay(pf);
9147 
9148 	i40e_fdir_check_and_reenable(pf);
9149 
9150 }
9151 
9152 /**
9153  * i40e_vsi_link_event - notify VSI of a link event
9154  * @vsi: vsi to be notified
9155  * @link_up: link up or down
9156  **/
i40e_vsi_link_event(struct i40e_vsi * vsi,bool link_up)9157 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9158 {
9159 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9160 		return;
9161 
9162 	switch (vsi->type) {
9163 	case I40E_VSI_MAIN:
9164 		if (!vsi->netdev || !vsi->netdev_registered)
9165 			break;
9166 
9167 		if (link_up) {
9168 			netif_carrier_on(vsi->netdev);
9169 			netif_tx_wake_all_queues(vsi->netdev);
9170 		} else {
9171 			netif_carrier_off(vsi->netdev);
9172 			netif_tx_stop_all_queues(vsi->netdev);
9173 		}
9174 		break;
9175 
9176 	case I40E_VSI_SRIOV:
9177 	case I40E_VSI_VMDQ2:
9178 	case I40E_VSI_CTRL:
9179 	case I40E_VSI_IWARP:
9180 	case I40E_VSI_MIRROR:
9181 	default:
9182 		/* there is no notification for other VSIs */
9183 		break;
9184 	}
9185 }
9186 
9187 /**
9188  * i40e_veb_link_event - notify elements on the veb of a link event
9189  * @veb: veb to be notified
9190  * @link_up: link up or down
9191  **/
i40e_veb_link_event(struct i40e_veb * veb,bool link_up)9192 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9193 {
9194 	struct i40e_pf *pf;
9195 	int i;
9196 
9197 	if (!veb || !veb->pf)
9198 		return;
9199 	pf = veb->pf;
9200 
9201 	/* depth first... */
9202 	for (i = 0; i < I40E_MAX_VEB; i++)
9203 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9204 			i40e_veb_link_event(pf->veb[i], link_up);
9205 
9206 	/* ... now the local VSIs */
9207 	for (i = 0; i < pf->num_alloc_vsi; i++)
9208 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9209 			i40e_vsi_link_event(pf->vsi[i], link_up);
9210 }
9211 
9212 /**
9213  * i40e_link_event - Update netif_carrier status
9214  * @pf: board private structure
9215  **/
i40e_link_event(struct i40e_pf * pf)9216 static void i40e_link_event(struct i40e_pf *pf)
9217 {
9218 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9219 	u8 new_link_speed, old_link_speed;
9220 	i40e_status status;
9221 	bool new_link, old_link;
9222 
9223 	/* set this to force the get_link_status call to refresh state */
9224 	pf->hw.phy.get_link_info = true;
9225 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9226 	status = i40e_get_link_status(&pf->hw, &new_link);
9227 
9228 	/* On success, disable temp link polling */
9229 	if (status == I40E_SUCCESS) {
9230 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9231 	} else {
9232 		/* Enable link polling temporarily until i40e_get_link_status
9233 		 * returns I40E_SUCCESS
9234 		 */
9235 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9236 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9237 			status);
9238 		return;
9239 	}
9240 
9241 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9242 	new_link_speed = pf->hw.phy.link_info.link_speed;
9243 
9244 	if (new_link == old_link &&
9245 	    new_link_speed == old_link_speed &&
9246 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9247 	     new_link == netif_carrier_ok(vsi->netdev)))
9248 		return;
9249 
9250 	i40e_print_link_message(vsi, new_link);
9251 
9252 	/* Notify the base of the switch tree connected to
9253 	 * the link.  Floating VEBs are not notified.
9254 	 */
9255 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9256 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9257 	else
9258 		i40e_vsi_link_event(vsi, new_link);
9259 
9260 	if (pf->vf)
9261 		i40e_vc_notify_link_state(pf);
9262 
9263 	if (pf->flags & I40E_FLAG_PTP)
9264 		i40e_ptp_set_increment(pf);
9265 }
9266 
9267 /**
9268  * i40e_watchdog_subtask - periodic checks not using event driven response
9269  * @pf: board private structure
9270  **/
i40e_watchdog_subtask(struct i40e_pf * pf)9271 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9272 {
9273 	int i;
9274 
9275 	/* if interface is down do nothing */
9276 	if (test_bit(__I40E_DOWN, pf->state) ||
9277 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9278 		return;
9279 
9280 	/* make sure we don't do these things too often */
9281 	if (time_before(jiffies, (pf->service_timer_previous +
9282 				  pf->service_timer_period)))
9283 		return;
9284 	pf->service_timer_previous = jiffies;
9285 
9286 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9287 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9288 		i40e_link_event(pf);
9289 
9290 	/* Update the stats for active netdevs so the network stack
9291 	 * can look at updated numbers whenever it cares to
9292 	 */
9293 	for (i = 0; i < pf->num_alloc_vsi; i++)
9294 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9295 			i40e_update_stats(pf->vsi[i]);
9296 
9297 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9298 		/* Update the stats for the active switching components */
9299 		for (i = 0; i < I40E_MAX_VEB; i++)
9300 			if (pf->veb[i])
9301 				i40e_update_veb_stats(pf->veb[i]);
9302 	}
9303 
9304 	i40e_ptp_rx_hang(pf);
9305 	i40e_ptp_tx_hang(pf);
9306 }
9307 
9308 /**
9309  * i40e_reset_subtask - Set up for resetting the device and driver
9310  * @pf: board private structure
9311  **/
i40e_reset_subtask(struct i40e_pf * pf)9312 static void i40e_reset_subtask(struct i40e_pf *pf)
9313 {
9314 	u32 reset_flags = 0;
9315 
9316 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
9317 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
9318 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
9319 	}
9320 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
9321 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
9322 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9323 	}
9324 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
9325 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
9326 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
9327 	}
9328 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
9329 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
9330 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
9331 	}
9332 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
9333 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
9334 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
9335 	}
9336 
9337 	/* If there's a recovery already waiting, it takes
9338 	 * precedence before starting a new reset sequence.
9339 	 */
9340 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
9341 		i40e_prep_for_reset(pf, false);
9342 		i40e_reset(pf);
9343 		i40e_rebuild(pf, false, false);
9344 	}
9345 
9346 	/* If we're already down or resetting, just bail */
9347 	if (reset_flags &&
9348 	    !test_bit(__I40E_DOWN, pf->state) &&
9349 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
9350 		i40e_do_reset(pf, reset_flags, false);
9351 	}
9352 }
9353 
9354 /**
9355  * i40e_handle_link_event - Handle link event
9356  * @pf: board private structure
9357  * @e: event info posted on ARQ
9358  **/
i40e_handle_link_event(struct i40e_pf * pf,struct i40e_arq_event_info * e)9359 static void i40e_handle_link_event(struct i40e_pf *pf,
9360 				   struct i40e_arq_event_info *e)
9361 {
9362 	struct i40e_aqc_get_link_status *status =
9363 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
9364 
9365 	/* Do a new status request to re-enable LSE reporting
9366 	 * and load new status information into the hw struct
9367 	 * This completely ignores any state information
9368 	 * in the ARQ event info, instead choosing to always
9369 	 * issue the AQ update link status command.
9370 	 */
9371 	i40e_link_event(pf);
9372 
9373 	/* Check if module meets thermal requirements */
9374 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
9375 		dev_err(&pf->pdev->dev,
9376 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
9377 		dev_err(&pf->pdev->dev,
9378 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9379 	} else {
9380 		/* check for unqualified module, if link is down, suppress
9381 		 * the message if link was forced to be down.
9382 		 */
9383 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
9384 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
9385 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
9386 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
9387 			dev_err(&pf->pdev->dev,
9388 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
9389 			dev_err(&pf->pdev->dev,
9390 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
9391 		}
9392 	}
9393 }
9394 
9395 /**
9396  * i40e_clean_adminq_subtask - Clean the AdminQ rings
9397  * @pf: board private structure
9398  **/
i40e_clean_adminq_subtask(struct i40e_pf * pf)9399 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
9400 {
9401 	struct i40e_arq_event_info event;
9402 	struct i40e_hw *hw = &pf->hw;
9403 	u16 pending, i = 0;
9404 	i40e_status ret;
9405 	u16 opcode;
9406 	u32 oldval;
9407 	u32 val;
9408 
9409 	/* Do not run clean AQ when PF reset fails */
9410 	if (test_bit(__I40E_RESET_FAILED, pf->state))
9411 		return;
9412 
9413 	/* check for error indications */
9414 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
9415 	oldval = val;
9416 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
9417 		if (hw->debug_mask & I40E_DEBUG_AQ)
9418 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
9419 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
9420 	}
9421 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
9422 		if (hw->debug_mask & I40E_DEBUG_AQ)
9423 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
9424 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
9425 		pf->arq_overflows++;
9426 	}
9427 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
9428 		if (hw->debug_mask & I40E_DEBUG_AQ)
9429 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
9430 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
9431 	}
9432 	if (oldval != val)
9433 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
9434 
9435 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
9436 	oldval = val;
9437 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
9438 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9439 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
9440 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
9441 	}
9442 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
9443 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9444 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
9445 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
9446 	}
9447 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
9448 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
9449 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
9450 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
9451 	}
9452 	if (oldval != val)
9453 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
9454 
9455 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
9456 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
9457 	if (!event.msg_buf)
9458 		return;
9459 
9460 	do {
9461 		ret = i40e_clean_arq_element(hw, &event, &pending);
9462 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
9463 			break;
9464 		else if (ret) {
9465 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
9466 			break;
9467 		}
9468 
9469 		opcode = le16_to_cpu(event.desc.opcode);
9470 		switch (opcode) {
9471 
9472 		case i40e_aqc_opc_get_link_status:
9473 			i40e_handle_link_event(pf, &event);
9474 			break;
9475 		case i40e_aqc_opc_send_msg_to_pf:
9476 			ret = i40e_vc_process_vf_msg(pf,
9477 					le16_to_cpu(event.desc.retval),
9478 					le32_to_cpu(event.desc.cookie_high),
9479 					le32_to_cpu(event.desc.cookie_low),
9480 					event.msg_buf,
9481 					event.msg_len);
9482 			break;
9483 		case i40e_aqc_opc_lldp_update_mib:
9484 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
9485 #ifdef CONFIG_I40E_DCB
9486 			rtnl_lock();
9487 			ret = i40e_handle_lldp_event(pf, &event);
9488 			rtnl_unlock();
9489 #endif /* CONFIG_I40E_DCB */
9490 			break;
9491 		case i40e_aqc_opc_event_lan_overflow:
9492 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
9493 			i40e_handle_lan_overflow_event(pf, &event);
9494 			break;
9495 		case i40e_aqc_opc_send_msg_to_peer:
9496 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
9497 			break;
9498 		case i40e_aqc_opc_nvm_erase:
9499 		case i40e_aqc_opc_nvm_update:
9500 		case i40e_aqc_opc_oem_post_update:
9501 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
9502 				   "ARQ NVM operation 0x%04x completed\n",
9503 				   opcode);
9504 			break;
9505 		default:
9506 			dev_info(&pf->pdev->dev,
9507 				 "ARQ: Unknown event 0x%04x ignored\n",
9508 				 opcode);
9509 			break;
9510 		}
9511 	} while (i++ < pf->adminq_work_limit);
9512 
9513 	if (i < pf->adminq_work_limit)
9514 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
9515 
9516 	/* re-enable Admin queue interrupt cause */
9517 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
9518 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
9519 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
9520 	i40e_flush(hw);
9521 
9522 	kfree(event.msg_buf);
9523 }
9524 
9525 /**
9526  * i40e_verify_eeprom - make sure eeprom is good to use
9527  * @pf: board private structure
9528  **/
i40e_verify_eeprom(struct i40e_pf * pf)9529 static void i40e_verify_eeprom(struct i40e_pf *pf)
9530 {
9531 	int err;
9532 
9533 	err = i40e_diag_eeprom_test(&pf->hw);
9534 	if (err) {
9535 		/* retry in case of garbage read */
9536 		err = i40e_diag_eeprom_test(&pf->hw);
9537 		if (err) {
9538 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
9539 				 err);
9540 			set_bit(__I40E_BAD_EEPROM, pf->state);
9541 		}
9542 	}
9543 
9544 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
9545 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
9546 		clear_bit(__I40E_BAD_EEPROM, pf->state);
9547 	}
9548 }
9549 
9550 /**
9551  * i40e_enable_pf_switch_lb
9552  * @pf: pointer to the PF structure
9553  *
9554  * enable switch loop back or die - no point in a return value
9555  **/
i40e_enable_pf_switch_lb(struct i40e_pf * pf)9556 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
9557 {
9558 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9559 	struct i40e_vsi_context ctxt;
9560 	int ret;
9561 
9562 	ctxt.seid = pf->main_vsi_seid;
9563 	ctxt.pf_num = pf->hw.pf_id;
9564 	ctxt.vf_num = 0;
9565 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9566 	if (ret) {
9567 		dev_info(&pf->pdev->dev,
9568 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9569 			 i40e_stat_str(&pf->hw, ret),
9570 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9571 		return;
9572 	}
9573 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9574 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9575 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9576 
9577 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9578 	if (ret) {
9579 		dev_info(&pf->pdev->dev,
9580 			 "update vsi switch failed, err %s aq_err %s\n",
9581 			 i40e_stat_str(&pf->hw, ret),
9582 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9583 	}
9584 }
9585 
9586 /**
9587  * i40e_disable_pf_switch_lb
9588  * @pf: pointer to the PF structure
9589  *
9590  * disable switch loop back or die - no point in a return value
9591  **/
i40e_disable_pf_switch_lb(struct i40e_pf * pf)9592 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
9593 {
9594 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9595 	struct i40e_vsi_context ctxt;
9596 	int ret;
9597 
9598 	ctxt.seid = pf->main_vsi_seid;
9599 	ctxt.pf_num = pf->hw.pf_id;
9600 	ctxt.vf_num = 0;
9601 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
9602 	if (ret) {
9603 		dev_info(&pf->pdev->dev,
9604 			 "couldn't get PF vsi config, err %s aq_err %s\n",
9605 			 i40e_stat_str(&pf->hw, ret),
9606 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9607 		return;
9608 	}
9609 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
9610 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
9611 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
9612 
9613 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
9614 	if (ret) {
9615 		dev_info(&pf->pdev->dev,
9616 			 "update vsi switch failed, err %s aq_err %s\n",
9617 			 i40e_stat_str(&pf->hw, ret),
9618 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
9619 	}
9620 }
9621 
9622 /**
9623  * i40e_config_bridge_mode - Configure the HW bridge mode
9624  * @veb: pointer to the bridge instance
9625  *
9626  * Configure the loop back mode for the LAN VSI that is downlink to the
9627  * specified HW bridge instance. It is expected this function is called
9628  * when a new HW bridge is instantiated.
9629  **/
i40e_config_bridge_mode(struct i40e_veb * veb)9630 static void i40e_config_bridge_mode(struct i40e_veb *veb)
9631 {
9632 	struct i40e_pf *pf = veb->pf;
9633 
9634 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
9635 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
9636 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
9637 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
9638 		i40e_disable_pf_switch_lb(pf);
9639 	else
9640 		i40e_enable_pf_switch_lb(pf);
9641 }
9642 
9643 /**
9644  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
9645  * @veb: pointer to the VEB instance
9646  *
9647  * This is a recursive function that first builds the attached VSIs then
9648  * recurses in to build the next layer of VEB.  We track the connections
9649  * through our own index numbers because the seid's from the HW could
9650  * change across the reset.
9651  **/
i40e_reconstitute_veb(struct i40e_veb * veb)9652 static int i40e_reconstitute_veb(struct i40e_veb *veb)
9653 {
9654 	struct i40e_vsi *ctl_vsi = NULL;
9655 	struct i40e_pf *pf = veb->pf;
9656 	int v, veb_idx;
9657 	int ret;
9658 
9659 	/* build VSI that owns this VEB, temporarily attached to base VEB */
9660 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
9661 		if (pf->vsi[v] &&
9662 		    pf->vsi[v]->veb_idx == veb->idx &&
9663 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
9664 			ctl_vsi = pf->vsi[v];
9665 			break;
9666 		}
9667 	}
9668 	if (!ctl_vsi) {
9669 		dev_info(&pf->pdev->dev,
9670 			 "missing owner VSI for veb_idx %d\n", veb->idx);
9671 		ret = -ENOENT;
9672 		goto end_reconstitute;
9673 	}
9674 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
9675 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
9676 	ret = i40e_add_vsi(ctl_vsi);
9677 	if (ret) {
9678 		dev_info(&pf->pdev->dev,
9679 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
9680 			 veb->idx, ret);
9681 		goto end_reconstitute;
9682 	}
9683 	i40e_vsi_reset_stats(ctl_vsi);
9684 
9685 	/* create the VEB in the switch and move the VSI onto the VEB */
9686 	ret = i40e_add_veb(veb, ctl_vsi);
9687 	if (ret)
9688 		goto end_reconstitute;
9689 
9690 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
9691 		veb->bridge_mode = BRIDGE_MODE_VEB;
9692 	else
9693 		veb->bridge_mode = BRIDGE_MODE_VEPA;
9694 	i40e_config_bridge_mode(veb);
9695 
9696 	/* create the remaining VSIs attached to this VEB */
9697 	for (v = 0; v < pf->num_alloc_vsi; v++) {
9698 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
9699 			continue;
9700 
9701 		if (pf->vsi[v]->veb_idx == veb->idx) {
9702 			struct i40e_vsi *vsi = pf->vsi[v];
9703 
9704 			vsi->uplink_seid = veb->seid;
9705 			ret = i40e_add_vsi(vsi);
9706 			if (ret) {
9707 				dev_info(&pf->pdev->dev,
9708 					 "rebuild of vsi_idx %d failed: %d\n",
9709 					 v, ret);
9710 				goto end_reconstitute;
9711 			}
9712 			i40e_vsi_reset_stats(vsi);
9713 		}
9714 	}
9715 
9716 	/* create any VEBs attached to this VEB - RECURSION */
9717 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
9718 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
9719 			pf->veb[veb_idx]->uplink_seid = veb->seid;
9720 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
9721 			if (ret)
9722 				break;
9723 		}
9724 	}
9725 
9726 end_reconstitute:
9727 	return ret;
9728 }
9729 
9730 /**
9731  * i40e_get_capabilities - get info about the HW
9732  * @pf: the PF struct
9733  * @list_type: AQ capability to be queried
9734  **/
i40e_get_capabilities(struct i40e_pf * pf,enum i40e_admin_queue_opc list_type)9735 static int i40e_get_capabilities(struct i40e_pf *pf,
9736 				 enum i40e_admin_queue_opc list_type)
9737 {
9738 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
9739 	u16 data_size;
9740 	int buf_len;
9741 	int err;
9742 
9743 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
9744 	do {
9745 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
9746 		if (!cap_buf)
9747 			return -ENOMEM;
9748 
9749 		/* this loads the data into the hw struct for us */
9750 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
9751 						    &data_size, list_type,
9752 						    NULL);
9753 		/* data loaded, buffer no longer needed */
9754 		kfree(cap_buf);
9755 
9756 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
9757 			/* retry with a larger buffer */
9758 			buf_len = data_size;
9759 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
9760 			dev_info(&pf->pdev->dev,
9761 				 "capability discovery failed, err %s aq_err %s\n",
9762 				 i40e_stat_str(&pf->hw, err),
9763 				 i40e_aq_str(&pf->hw,
9764 					     pf->hw.aq.asq_last_status));
9765 			return -ENODEV;
9766 		}
9767 	} while (err);
9768 
9769 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
9770 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
9771 			dev_info(&pf->pdev->dev,
9772 				 "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",
9773 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
9774 				 pf->hw.func_caps.num_msix_vectors,
9775 				 pf->hw.func_caps.num_msix_vectors_vf,
9776 				 pf->hw.func_caps.fd_filters_guaranteed,
9777 				 pf->hw.func_caps.fd_filters_best_effort,
9778 				 pf->hw.func_caps.num_tx_qp,
9779 				 pf->hw.func_caps.num_vsis);
9780 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
9781 			dev_info(&pf->pdev->dev,
9782 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
9783 				 pf->hw.dev_caps.switch_mode,
9784 				 pf->hw.dev_caps.valid_functions);
9785 			dev_info(&pf->pdev->dev,
9786 				 "SR-IOV=%d, num_vfs for all function=%u\n",
9787 				 pf->hw.dev_caps.sr_iov_1_1,
9788 				 pf->hw.dev_caps.num_vfs);
9789 			dev_info(&pf->pdev->dev,
9790 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
9791 				 pf->hw.dev_caps.num_vsis,
9792 				 pf->hw.dev_caps.num_rx_qp,
9793 				 pf->hw.dev_caps.num_tx_qp);
9794 		}
9795 	}
9796 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
9797 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
9798 		       + pf->hw.func_caps.num_vfs)
9799 		if (pf->hw.revision_id == 0 &&
9800 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
9801 			dev_info(&pf->pdev->dev,
9802 				 "got num_vsis %d, setting num_vsis to %d\n",
9803 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
9804 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
9805 		}
9806 	}
9807 	return 0;
9808 }
9809 
9810 static int i40e_vsi_clear(struct i40e_vsi *vsi);
9811 
9812 /**
9813  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
9814  * @pf: board private structure
9815  **/
i40e_fdir_sb_setup(struct i40e_pf * pf)9816 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
9817 {
9818 	struct i40e_vsi *vsi;
9819 
9820 	/* quick workaround for an NVM issue that leaves a critical register
9821 	 * uninitialized
9822 	 */
9823 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
9824 		static const u32 hkey[] = {
9825 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
9826 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
9827 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
9828 			0x95b3a76d};
9829 		int i;
9830 
9831 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
9832 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
9833 	}
9834 
9835 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
9836 		return;
9837 
9838 	/* find existing VSI and see if it needs configuring */
9839 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9840 
9841 	/* create a new VSI if none exists */
9842 	if (!vsi) {
9843 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
9844 				     pf->vsi[pf->lan_vsi]->seid, 0);
9845 		if (!vsi) {
9846 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
9847 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
9848 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
9849 			return;
9850 		}
9851 	}
9852 
9853 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
9854 }
9855 
9856 /**
9857  * i40e_fdir_teardown - release the Flow Director resources
9858  * @pf: board private structure
9859  **/
i40e_fdir_teardown(struct i40e_pf * pf)9860 static void i40e_fdir_teardown(struct i40e_pf *pf)
9861 {
9862 	struct i40e_vsi *vsi;
9863 
9864 	i40e_fdir_filter_exit(pf);
9865 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
9866 	if (vsi)
9867 		i40e_vsi_release(vsi);
9868 }
9869 
9870 /**
9871  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
9872  * @vsi: PF main vsi
9873  * @seid: seid of main or channel VSIs
9874  *
9875  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
9876  * existed before reset
9877  **/
i40e_rebuild_cloud_filters(struct i40e_vsi * vsi,u16 seid)9878 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
9879 {
9880 	struct i40e_cloud_filter *cfilter;
9881 	struct i40e_pf *pf = vsi->back;
9882 	struct hlist_node *node;
9883 	i40e_status ret;
9884 
9885 	/* Add cloud filters back if they exist */
9886 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
9887 				  cloud_node) {
9888 		if (cfilter->seid != seid)
9889 			continue;
9890 
9891 		if (cfilter->dst_port)
9892 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
9893 								true);
9894 		else
9895 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
9896 
9897 		if (ret) {
9898 			dev_dbg(&pf->pdev->dev,
9899 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
9900 				i40e_stat_str(&pf->hw, ret),
9901 				i40e_aq_str(&pf->hw,
9902 					    pf->hw.aq.asq_last_status));
9903 			return ret;
9904 		}
9905 	}
9906 	return 0;
9907 }
9908 
9909 /**
9910  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
9911  * @vsi: PF main vsi
9912  *
9913  * Rebuilds channel VSIs if they existed before reset
9914  **/
i40e_rebuild_channels(struct i40e_vsi * vsi)9915 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
9916 {
9917 	struct i40e_channel *ch, *ch_tmp;
9918 	i40e_status ret;
9919 
9920 	if (list_empty(&vsi->ch_list))
9921 		return 0;
9922 
9923 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
9924 		if (!ch->initialized)
9925 			break;
9926 		/* Proceed with creation of channel (VMDq2) VSI */
9927 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
9928 		if (ret) {
9929 			dev_info(&vsi->back->pdev->dev,
9930 				 "failed to rebuild channels using uplink_seid %u\n",
9931 				 vsi->uplink_seid);
9932 			return ret;
9933 		}
9934 		/* Reconfigure TX queues using QTX_CTL register */
9935 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
9936 		if (ret) {
9937 			dev_info(&vsi->back->pdev->dev,
9938 				 "failed to configure TX rings for channel %u\n",
9939 				 ch->seid);
9940 			return ret;
9941 		}
9942 		/* update 'next_base_queue' */
9943 		vsi->next_base_queue = vsi->next_base_queue +
9944 							ch->num_queue_pairs;
9945 		if (ch->max_tx_rate) {
9946 			u64 credits = ch->max_tx_rate;
9947 
9948 			if (i40e_set_bw_limit(vsi, ch->seid,
9949 					      ch->max_tx_rate))
9950 				return -EINVAL;
9951 
9952 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
9953 			dev_dbg(&vsi->back->pdev->dev,
9954 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
9955 				ch->max_tx_rate,
9956 				credits,
9957 				ch->seid);
9958 		}
9959 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
9960 		if (ret) {
9961 			dev_dbg(&vsi->back->pdev->dev,
9962 				"Failed to rebuild cloud filters for channel VSI %u\n",
9963 				ch->seid);
9964 			return ret;
9965 		}
9966 	}
9967 	return 0;
9968 }
9969 
9970 /**
9971  * i40e_prep_for_reset - prep for the core to reset
9972  * @pf: board private structure
9973  * @lock_acquired: indicates whether or not the lock has been acquired
9974  * before this function was called.
9975  *
9976  * Close up the VFs and other things in prep for PF Reset.
9977   **/
i40e_prep_for_reset(struct i40e_pf * pf,bool lock_acquired)9978 static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
9979 {
9980 	struct i40e_hw *hw = &pf->hw;
9981 	i40e_status ret = 0;
9982 	u32 v;
9983 
9984 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
9985 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
9986 		return;
9987 	if (i40e_check_asq_alive(&pf->hw))
9988 		i40e_vc_notify_reset(pf);
9989 
9990 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
9991 
9992 	/* quiesce the VSIs and their queues that are not already DOWN */
9993 	/* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
9994 	if (!lock_acquired)
9995 		rtnl_lock();
9996 	i40e_pf_quiesce_all_vsi(pf);
9997 	if (!lock_acquired)
9998 		rtnl_unlock();
9999 
10000 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10001 		if (pf->vsi[v])
10002 			pf->vsi[v]->seid = 0;
10003 	}
10004 
10005 	i40e_shutdown_adminq(&pf->hw);
10006 
10007 	/* call shutdown HMC */
10008 	if (hw->hmc.hmc_obj) {
10009 		ret = i40e_shutdown_lan_hmc(hw);
10010 		if (ret)
10011 			dev_warn(&pf->pdev->dev,
10012 				 "shutdown_lan_hmc failed: %d\n", ret);
10013 	}
10014 
10015 	/* Save the current PTP time so that we can restore the time after the
10016 	 * reset completes.
10017 	 */
10018 	i40e_ptp_save_hw_time(pf);
10019 }
10020 
10021 /**
10022  * i40e_send_version - update firmware with driver version
10023  * @pf: PF struct
10024  */
i40e_send_version(struct i40e_pf * pf)10025 static void i40e_send_version(struct i40e_pf *pf)
10026 {
10027 	struct i40e_driver_version dv;
10028 
10029 	dv.major_version = 0xff;
10030 	dv.minor_version = 0xff;
10031 	dv.build_version = 0xff;
10032 	dv.subbuild_version = 0;
10033 	strlcpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10034 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10035 }
10036 
10037 /**
10038  * i40e_get_oem_version - get OEM specific version information
10039  * @hw: pointer to the hardware structure
10040  **/
i40e_get_oem_version(struct i40e_hw * hw)10041 static void i40e_get_oem_version(struct i40e_hw *hw)
10042 {
10043 	u16 block_offset = 0xffff;
10044 	u16 block_length = 0;
10045 	u16 capabilities = 0;
10046 	u16 gen_snap = 0;
10047 	u16 release = 0;
10048 
10049 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10050 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10051 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10052 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10053 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10054 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10055 #define I40E_NVM_OEM_LENGTH			3
10056 
10057 	/* Check if pointer to OEM version block is valid. */
10058 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10059 	if (block_offset == 0xffff)
10060 		return;
10061 
10062 	/* Check if OEM version block has correct length. */
10063 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10064 			   &block_length);
10065 	if (block_length < I40E_NVM_OEM_LENGTH)
10066 		return;
10067 
10068 	/* Check if OEM version format is as expected. */
10069 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10070 			   &capabilities);
10071 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10072 		return;
10073 
10074 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10075 			   &gen_snap);
10076 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10077 			   &release);
10078 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10079 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10080 }
10081 
10082 /**
10083  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10084  * @pf: board private structure
10085  **/
i40e_reset(struct i40e_pf * pf)10086 static int i40e_reset(struct i40e_pf *pf)
10087 {
10088 	struct i40e_hw *hw = &pf->hw;
10089 	i40e_status ret;
10090 
10091 	ret = i40e_pf_reset(hw);
10092 	if (ret) {
10093 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10094 		set_bit(__I40E_RESET_FAILED, pf->state);
10095 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10096 	} else {
10097 		pf->pfr_count++;
10098 	}
10099 	return ret;
10100 }
10101 
10102 /**
10103  * i40e_rebuild - rebuild using a saved config
10104  * @pf: board private structure
10105  * @reinit: if the Main VSI needs to re-initialized.
10106  * @lock_acquired: indicates whether or not the lock has been acquired
10107  * before this function was called.
10108  **/
i40e_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)10109 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10110 {
10111 	int old_recovery_mode_bit = test_bit(__I40E_RECOVERY_MODE, pf->state);
10112 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10113 	struct i40e_hw *hw = &pf->hw;
10114 	i40e_status ret;
10115 	u32 val;
10116 	int v;
10117 
10118 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10119 	    i40e_check_recovery_mode(pf)) {
10120 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10121 	}
10122 
10123 	if (test_bit(__I40E_DOWN, pf->state) &&
10124 	    !test_bit(__I40E_RECOVERY_MODE, pf->state) &&
10125 	    !old_recovery_mode_bit)
10126 		goto clear_recovery;
10127 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10128 
10129 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10130 	ret = i40e_init_adminq(&pf->hw);
10131 	if (ret) {
10132 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10133 			 i40e_stat_str(&pf->hw, ret),
10134 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10135 		goto clear_recovery;
10136 	}
10137 	i40e_get_oem_version(&pf->hw);
10138 
10139 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10140 		/* The following delay is necessary for firmware update. */
10141 		mdelay(1000);
10142 	}
10143 
10144 	/* re-verify the eeprom if we just had an EMP reset */
10145 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10146 		i40e_verify_eeprom(pf);
10147 
10148 	/* if we are going out of or into recovery mode we have to act
10149 	 * accordingly with regard to resources initialization
10150 	 * and deinitialization
10151 	 */
10152 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) ||
10153 	    old_recovery_mode_bit) {
10154 		if (i40e_get_capabilities(pf,
10155 					  i40e_aqc_opc_list_func_capabilities))
10156 			goto end_unlock;
10157 
10158 		if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10159 			/* we're staying in recovery mode so we'll reinitialize
10160 			 * misc vector here
10161 			 */
10162 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10163 				goto end_unlock;
10164 		} else {
10165 			if (!lock_acquired)
10166 				rtnl_lock();
10167 			/* we're going out of recovery mode so we'll free
10168 			 * the IRQ allocated specifically for recovery mode
10169 			 * and restore the interrupt scheme
10170 			 */
10171 			free_irq(pf->pdev->irq, pf);
10172 			i40e_clear_interrupt_scheme(pf);
10173 			if (i40e_restore_interrupt_scheme(pf))
10174 				goto end_unlock;
10175 		}
10176 
10177 		/* tell the firmware that we're starting */
10178 		i40e_send_version(pf);
10179 
10180 		/* bail out in case recovery mode was detected, as there is
10181 		 * no need for further configuration.
10182 		 */
10183 		goto end_unlock;
10184 	}
10185 
10186 	i40e_clear_pxe_mode(hw);
10187 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10188 	if (ret)
10189 		goto end_core_reset;
10190 
10191 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10192 				hw->func_caps.num_rx_qp, 0, 0);
10193 	if (ret) {
10194 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10195 		goto end_core_reset;
10196 	}
10197 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10198 	if (ret) {
10199 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10200 		goto end_core_reset;
10201 	}
10202 
10203 	/* Enable FW to write a default DCB config on link-up */
10204 	i40e_aq_set_dcb_parameters(hw, true, NULL);
10205 
10206 #ifdef CONFIG_I40E_DCB
10207 	ret = i40e_init_pf_dcb(pf);
10208 	if (ret) {
10209 		dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
10210 		pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10211 		/* Continue without DCB enabled */
10212 	}
10213 #endif /* CONFIG_I40E_DCB */
10214 	/* do basic switch setup */
10215 	if (!lock_acquired)
10216 		rtnl_lock();
10217 	ret = i40e_setup_pf_switch(pf, reinit, true);
10218 	if (ret)
10219 		goto end_unlock;
10220 
10221 	/* The driver only wants link up/down and module qualification
10222 	 * reports from firmware.  Note the negative logic.
10223 	 */
10224 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10225 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10226 					 I40E_AQ_EVENT_MEDIA_NA |
10227 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10228 	if (ret)
10229 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10230 			 i40e_stat_str(&pf->hw, ret),
10231 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10232 
10233 	/* Rebuild the VSIs and VEBs that existed before reset.
10234 	 * They are still in our local switch element arrays, so only
10235 	 * need to rebuild the switch model in the HW.
10236 	 *
10237 	 * If there were VEBs but the reconstitution failed, we'll try
10238 	 * try to recover minimal use by getting the basic PF VSI working.
10239 	 */
10240 	if (vsi->uplink_seid != pf->mac_seid) {
10241 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10242 		/* find the one VEB connected to the MAC, and find orphans */
10243 		for (v = 0; v < I40E_MAX_VEB; v++) {
10244 			if (!pf->veb[v])
10245 				continue;
10246 
10247 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10248 			    pf->veb[v]->uplink_seid == 0) {
10249 				ret = i40e_reconstitute_veb(pf->veb[v]);
10250 
10251 				if (!ret)
10252 					continue;
10253 
10254 				/* If Main VEB failed, we're in deep doodoo,
10255 				 * so give up rebuilding the switch and set up
10256 				 * for minimal rebuild of PF VSI.
10257 				 * If orphan failed, we'll report the error
10258 				 * but try to keep going.
10259 				 */
10260 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10261 					dev_info(&pf->pdev->dev,
10262 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10263 						 ret);
10264 					vsi->uplink_seid = pf->mac_seid;
10265 					break;
10266 				} else if (pf->veb[v]->uplink_seid == 0) {
10267 					dev_info(&pf->pdev->dev,
10268 						 "rebuild of orphan VEB failed: %d\n",
10269 						 ret);
10270 				}
10271 			}
10272 		}
10273 	}
10274 
10275 	if (vsi->uplink_seid == pf->mac_seid) {
10276 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10277 		/* no VEB, so rebuild only the Main VSI */
10278 		ret = i40e_add_vsi(vsi);
10279 		if (ret) {
10280 			dev_info(&pf->pdev->dev,
10281 				 "rebuild of Main VSI failed: %d\n", ret);
10282 			goto end_unlock;
10283 		}
10284 	}
10285 
10286 	if (vsi->mqprio_qopt.max_rate[0]) {
10287 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
10288 		u64 credits = 0;
10289 
10290 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
10291 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10292 		if (ret)
10293 			goto end_unlock;
10294 
10295 		credits = max_tx_rate;
10296 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10297 		dev_dbg(&vsi->back->pdev->dev,
10298 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10299 			max_tx_rate,
10300 			credits,
10301 			vsi->seid);
10302 	}
10303 
10304 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10305 	if (ret)
10306 		goto end_unlock;
10307 
10308 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
10309 	 * for this main VSI if they exist
10310 	 */
10311 	ret = i40e_rebuild_channels(vsi);
10312 	if (ret)
10313 		goto end_unlock;
10314 
10315 	/* Reconfigure hardware for allowing smaller MSS in the case
10316 	 * of TSO, so that we avoid the MDD being fired and causing
10317 	 * a reset in the case of small MSS+TSO.
10318 	 */
10319 #define I40E_REG_MSS          0x000E64DC
10320 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
10321 #define I40E_64BYTE_MSS       0x400000
10322 	val = rd32(hw, I40E_REG_MSS);
10323 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
10324 		val &= ~I40E_REG_MSS_MIN_MASK;
10325 		val |= I40E_64BYTE_MSS;
10326 		wr32(hw, I40E_REG_MSS, val);
10327 	}
10328 
10329 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
10330 		msleep(75);
10331 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
10332 		if (ret)
10333 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
10334 				 i40e_stat_str(&pf->hw, ret),
10335 				 i40e_aq_str(&pf->hw,
10336 					     pf->hw.aq.asq_last_status));
10337 	}
10338 	/* reinit the misc interrupt */
10339 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10340 		ret = i40e_setup_misc_vector(pf);
10341 
10342 	/* Add a filter to drop all Flow control frames from any VSI from being
10343 	 * transmitted. By doing so we stop a malicious VF from sending out
10344 	 * PAUSE or PFC frames and potentially controlling traffic for other
10345 	 * PF/VF VSIs.
10346 	 * The FW can still send Flow control frames if enabled.
10347 	 */
10348 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
10349 						       pf->main_vsi_seid);
10350 
10351 	/* restart the VSIs that were rebuilt and running before the reset */
10352 	i40e_pf_unquiesce_all_vsi(pf);
10353 
10354 	/* Release the RTNL lock before we start resetting VFs */
10355 	if (!lock_acquired)
10356 		rtnl_unlock();
10357 
10358 	/* Restore promiscuous settings */
10359 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
10360 	if (ret)
10361 		dev_warn(&pf->pdev->dev,
10362 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
10363 			 pf->cur_promisc ? "on" : "off",
10364 			 i40e_stat_str(&pf->hw, ret),
10365 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10366 
10367 	i40e_reset_all_vfs(pf, true);
10368 
10369 	/* tell the firmware that we're starting */
10370 	i40e_send_version(pf);
10371 
10372 	/* We've already released the lock, so don't do it again */
10373 	goto end_core_reset;
10374 
10375 end_unlock:
10376 	if (!lock_acquired)
10377 		rtnl_unlock();
10378 end_core_reset:
10379 	clear_bit(__I40E_RESET_FAILED, pf->state);
10380 clear_recovery:
10381 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10382 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
10383 }
10384 
10385 /**
10386  * i40e_reset_and_rebuild - reset and rebuild using a saved config
10387  * @pf: board private structure
10388  * @reinit: if the Main VSI needs to re-initialized.
10389  * @lock_acquired: indicates whether or not the lock has been acquired
10390  * before this function was called.
10391  **/
i40e_reset_and_rebuild(struct i40e_pf * pf,bool reinit,bool lock_acquired)10392 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
10393 				   bool lock_acquired)
10394 {
10395 	int ret;
10396 	/* Now we wait for GRST to settle out.
10397 	 * We don't have to delete the VEBs or VSIs from the hw switch
10398 	 * because the reset will make them disappear.
10399 	 */
10400 	ret = i40e_reset(pf);
10401 	if (!ret)
10402 		i40e_rebuild(pf, reinit, lock_acquired);
10403 }
10404 
10405 /**
10406  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
10407  * @pf: board private structure
10408  *
10409  * Close up the VFs and other things in prep for a Core Reset,
10410  * then get ready to rebuild the world.
10411  * @lock_acquired: indicates whether or not the lock has been acquired
10412  * before this function was called.
10413  **/
i40e_handle_reset_warning(struct i40e_pf * pf,bool lock_acquired)10414 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
10415 {
10416 	i40e_prep_for_reset(pf, lock_acquired);
10417 	i40e_reset_and_rebuild(pf, false, lock_acquired);
10418 }
10419 
10420 /**
10421  * i40e_handle_mdd_event
10422  * @pf: pointer to the PF structure
10423  *
10424  * Called from the MDD irq handler to identify possibly malicious vfs
10425  **/
i40e_handle_mdd_event(struct i40e_pf * pf)10426 static void i40e_handle_mdd_event(struct i40e_pf *pf)
10427 {
10428 	struct i40e_hw *hw = &pf->hw;
10429 	bool mdd_detected = false;
10430 	struct i40e_vf *vf;
10431 	u32 reg;
10432 	int i;
10433 
10434 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
10435 		return;
10436 
10437 	/* find what triggered the MDD event */
10438 	reg = rd32(hw, I40E_GL_MDET_TX);
10439 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
10440 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
10441 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
10442 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
10443 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
10444 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
10445 				I40E_GL_MDET_TX_EVENT_SHIFT;
10446 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
10447 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
10448 				pf->hw.func_caps.base_queue;
10449 		if (netif_msg_tx_err(pf))
10450 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
10451 				 event, queue, pf_num, vf_num);
10452 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
10453 		mdd_detected = true;
10454 	}
10455 	reg = rd32(hw, I40E_GL_MDET_RX);
10456 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
10457 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
10458 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
10459 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
10460 				I40E_GL_MDET_RX_EVENT_SHIFT;
10461 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
10462 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
10463 				pf->hw.func_caps.base_queue;
10464 		if (netif_msg_rx_err(pf))
10465 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
10466 				 event, queue, func);
10467 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
10468 		mdd_detected = true;
10469 	}
10470 
10471 	if (mdd_detected) {
10472 		reg = rd32(hw, I40E_PF_MDET_TX);
10473 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
10474 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
10475 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
10476 		}
10477 		reg = rd32(hw, I40E_PF_MDET_RX);
10478 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
10479 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
10480 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
10481 		}
10482 	}
10483 
10484 	/* see if one of the VFs needs its hand slapped */
10485 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
10486 		vf = &(pf->vf[i]);
10487 		reg = rd32(hw, I40E_VP_MDET_TX(i));
10488 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
10489 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
10490 			vf->num_mdd_events++;
10491 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
10492 				 i);
10493 			dev_info(&pf->pdev->dev,
10494 				 "Use PF Control I/F to re-enable the VF\n");
10495 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10496 		}
10497 
10498 		reg = rd32(hw, I40E_VP_MDET_RX(i));
10499 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
10500 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
10501 			vf->num_mdd_events++;
10502 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
10503 				 i);
10504 			dev_info(&pf->pdev->dev,
10505 				 "Use PF Control I/F to re-enable the VF\n");
10506 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
10507 		}
10508 	}
10509 
10510 	/* re-enable mdd interrupt cause */
10511 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
10512 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
10513 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
10514 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
10515 	i40e_flush(hw);
10516 }
10517 
10518 /**
10519  * i40e_service_task - Run the driver's async subtasks
10520  * @work: pointer to work_struct containing our data
10521  **/
i40e_service_task(struct work_struct * work)10522 static void i40e_service_task(struct work_struct *work)
10523 {
10524 	struct i40e_pf *pf = container_of(work,
10525 					  struct i40e_pf,
10526 					  service_task);
10527 	unsigned long start_time = jiffies;
10528 
10529 	/* don't bother with service tasks if a reset is in progress */
10530 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
10531 	    test_bit(__I40E_SUSPENDED, pf->state))
10532 		return;
10533 
10534 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
10535 		return;
10536 
10537 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10538 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
10539 		i40e_sync_filters_subtask(pf);
10540 		i40e_reset_subtask(pf);
10541 		i40e_handle_mdd_event(pf);
10542 		i40e_vc_process_vflr_event(pf);
10543 		i40e_watchdog_subtask(pf);
10544 		i40e_fdir_reinit_subtask(pf);
10545 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
10546 			/* Client subtask will reopen next time through. */
10547 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
10548 							   true);
10549 		} else {
10550 			i40e_client_subtask(pf);
10551 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
10552 					       pf->state))
10553 				i40e_notify_client_of_l2_param_changes(
10554 								pf->vsi[pf->lan_vsi]);
10555 		}
10556 		i40e_sync_filters_subtask(pf);
10557 	} else {
10558 		i40e_reset_subtask(pf);
10559 	}
10560 
10561 	i40e_clean_adminq_subtask(pf);
10562 
10563 	/* flush memory to make sure state is correct before next watchdog */
10564 	smp_mb__before_atomic();
10565 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
10566 
10567 	/* If the tasks have taken longer than one timer cycle or there
10568 	 * is more work to be done, reschedule the service task now
10569 	 * rather than wait for the timer to tick again.
10570 	 */
10571 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
10572 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
10573 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
10574 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
10575 		i40e_service_event_schedule(pf);
10576 }
10577 
10578 /**
10579  * i40e_service_timer - timer callback
10580  * @t: timer list pointer
10581  **/
i40e_service_timer(struct timer_list * t)10582 static void i40e_service_timer(struct timer_list *t)
10583 {
10584 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
10585 
10586 	mod_timer(&pf->service_timer,
10587 		  round_jiffies(jiffies + pf->service_timer_period));
10588 	i40e_service_event_schedule(pf);
10589 }
10590 
10591 /**
10592  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
10593  * @vsi: the VSI being configured
10594  **/
i40e_set_num_rings_in_vsi(struct i40e_vsi * vsi)10595 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
10596 {
10597 	struct i40e_pf *pf = vsi->back;
10598 
10599 	switch (vsi->type) {
10600 	case I40E_VSI_MAIN:
10601 		vsi->alloc_queue_pairs = pf->num_lan_qps;
10602 		if (!vsi->num_tx_desc)
10603 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10604 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10605 		if (!vsi->num_rx_desc)
10606 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10607 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10608 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
10609 			vsi->num_q_vectors = pf->num_lan_msix;
10610 		else
10611 			vsi->num_q_vectors = 1;
10612 
10613 		break;
10614 
10615 	case I40E_VSI_FDIR:
10616 		vsi->alloc_queue_pairs = 1;
10617 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10618 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10619 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
10620 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
10621 		vsi->num_q_vectors = pf->num_fdsb_msix;
10622 		break;
10623 
10624 	case I40E_VSI_VMDQ2:
10625 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
10626 		if (!vsi->num_tx_desc)
10627 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10628 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10629 		if (!vsi->num_rx_desc)
10630 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10631 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10632 		vsi->num_q_vectors = pf->num_vmdq_msix;
10633 		break;
10634 
10635 	case I40E_VSI_SRIOV:
10636 		vsi->alloc_queue_pairs = pf->num_vf_qps;
10637 		if (!vsi->num_tx_desc)
10638 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10639 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10640 		if (!vsi->num_rx_desc)
10641 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
10642 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
10643 		break;
10644 
10645 	default:
10646 		WARN_ON(1);
10647 		return -ENODATA;
10648 	}
10649 
10650 	return 0;
10651 }
10652 
10653 /**
10654  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
10655  * @vsi: VSI pointer
10656  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
10657  *
10658  * On error: returns error code (negative)
10659  * On success: returns 0
10660  **/
i40e_vsi_alloc_arrays(struct i40e_vsi * vsi,bool alloc_qvectors)10661 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
10662 {
10663 	struct i40e_ring **next_rings;
10664 	int size;
10665 	int ret = 0;
10666 
10667 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
10668 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
10669 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
10670 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
10671 	if (!vsi->tx_rings)
10672 		return -ENOMEM;
10673 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
10674 	if (i40e_enabled_xdp_vsi(vsi)) {
10675 		vsi->xdp_rings = next_rings;
10676 		next_rings += vsi->alloc_queue_pairs;
10677 	}
10678 	vsi->rx_rings = next_rings;
10679 
10680 	if (alloc_qvectors) {
10681 		/* allocate memory for q_vector pointers */
10682 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
10683 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
10684 		if (!vsi->q_vectors) {
10685 			ret = -ENOMEM;
10686 			goto err_vectors;
10687 		}
10688 	}
10689 	return ret;
10690 
10691 err_vectors:
10692 	kfree(vsi->tx_rings);
10693 	return ret;
10694 }
10695 
10696 /**
10697  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
10698  * @pf: board private structure
10699  * @type: type of VSI
10700  *
10701  * On error: returns error code (negative)
10702  * On success: returns vsi index in PF (positive)
10703  **/
i40e_vsi_mem_alloc(struct i40e_pf * pf,enum i40e_vsi_type type)10704 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
10705 {
10706 	int ret = -ENODEV;
10707 	struct i40e_vsi *vsi;
10708 	int vsi_idx;
10709 	int i;
10710 
10711 	/* Need to protect the allocation of the VSIs at the PF level */
10712 	mutex_lock(&pf->switch_mutex);
10713 
10714 	/* VSI list may be fragmented if VSI creation/destruction has
10715 	 * been happening.  We can afford to do a quick scan to look
10716 	 * for any free VSIs in the list.
10717 	 *
10718 	 * find next empty vsi slot, looping back around if necessary
10719 	 */
10720 	i = pf->next_vsi;
10721 	while (i < pf->num_alloc_vsi && pf->vsi[i])
10722 		i++;
10723 	if (i >= pf->num_alloc_vsi) {
10724 		i = 0;
10725 		while (i < pf->next_vsi && pf->vsi[i])
10726 			i++;
10727 	}
10728 
10729 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
10730 		vsi_idx = i;             /* Found one! */
10731 	} else {
10732 		ret = -ENODEV;
10733 		goto unlock_pf;  /* out of VSI slots! */
10734 	}
10735 	pf->next_vsi = ++i;
10736 
10737 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
10738 	if (!vsi) {
10739 		ret = -ENOMEM;
10740 		goto unlock_pf;
10741 	}
10742 	vsi->type = type;
10743 	vsi->back = pf;
10744 	set_bit(__I40E_VSI_DOWN, vsi->state);
10745 	vsi->flags = 0;
10746 	vsi->idx = vsi_idx;
10747 	vsi->int_rate_limit = 0;
10748 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
10749 				pf->rss_table_size : 64;
10750 	vsi->netdev_registered = false;
10751 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
10752 	hash_init(vsi->mac_filter_hash);
10753 	vsi->irqs_ready = false;
10754 
10755 	if (type == I40E_VSI_MAIN) {
10756 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
10757 		if (!vsi->af_xdp_zc_qps)
10758 			goto err_rings;
10759 	}
10760 
10761 	ret = i40e_set_num_rings_in_vsi(vsi);
10762 	if (ret)
10763 		goto err_rings;
10764 
10765 	ret = i40e_vsi_alloc_arrays(vsi, true);
10766 	if (ret)
10767 		goto err_rings;
10768 
10769 	/* Setup default MSIX irq handler for VSI */
10770 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
10771 
10772 	/* Initialize VSI lock */
10773 	spin_lock_init(&vsi->mac_filter_hash_lock);
10774 	pf->vsi[vsi_idx] = vsi;
10775 	ret = vsi_idx;
10776 	goto unlock_pf;
10777 
10778 err_rings:
10779 	bitmap_free(vsi->af_xdp_zc_qps);
10780 	pf->next_vsi = i - 1;
10781 	kfree(vsi);
10782 unlock_pf:
10783 	mutex_unlock(&pf->switch_mutex);
10784 	return ret;
10785 }
10786 
10787 /**
10788  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
10789  * @vsi: VSI pointer
10790  * @free_qvectors: a bool to specify if q_vectors need to be freed.
10791  *
10792  * On error: returns error code (negative)
10793  * On success: returns 0
10794  **/
i40e_vsi_free_arrays(struct i40e_vsi * vsi,bool free_qvectors)10795 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
10796 {
10797 	/* free the ring and vector containers */
10798 	if (free_qvectors) {
10799 		kfree(vsi->q_vectors);
10800 		vsi->q_vectors = NULL;
10801 	}
10802 	kfree(vsi->tx_rings);
10803 	vsi->tx_rings = NULL;
10804 	vsi->rx_rings = NULL;
10805 	vsi->xdp_rings = NULL;
10806 }
10807 
10808 /**
10809  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
10810  * and lookup table
10811  * @vsi: Pointer to VSI structure
10812  */
i40e_clear_rss_config_user(struct i40e_vsi * vsi)10813 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
10814 {
10815 	if (!vsi)
10816 		return;
10817 
10818 	kfree(vsi->rss_hkey_user);
10819 	vsi->rss_hkey_user = NULL;
10820 
10821 	kfree(vsi->rss_lut_user);
10822 	vsi->rss_lut_user = NULL;
10823 }
10824 
10825 /**
10826  * i40e_vsi_clear - Deallocate the VSI provided
10827  * @vsi: the VSI being un-configured
10828  **/
i40e_vsi_clear(struct i40e_vsi * vsi)10829 static int i40e_vsi_clear(struct i40e_vsi *vsi)
10830 {
10831 	struct i40e_pf *pf;
10832 
10833 	if (!vsi)
10834 		return 0;
10835 
10836 	if (!vsi->back)
10837 		goto free_vsi;
10838 	pf = vsi->back;
10839 
10840 	mutex_lock(&pf->switch_mutex);
10841 	if (!pf->vsi[vsi->idx]) {
10842 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
10843 			vsi->idx, vsi->idx, vsi->type);
10844 		goto unlock_vsi;
10845 	}
10846 
10847 	if (pf->vsi[vsi->idx] != vsi) {
10848 		dev_err(&pf->pdev->dev,
10849 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
10850 			pf->vsi[vsi->idx]->idx,
10851 			pf->vsi[vsi->idx]->type,
10852 			vsi->idx, vsi->type);
10853 		goto unlock_vsi;
10854 	}
10855 
10856 	/* updates the PF for this cleared vsi */
10857 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
10858 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
10859 
10860 	bitmap_free(vsi->af_xdp_zc_qps);
10861 	i40e_vsi_free_arrays(vsi, true);
10862 	i40e_clear_rss_config_user(vsi);
10863 
10864 	pf->vsi[vsi->idx] = NULL;
10865 	if (vsi->idx < pf->next_vsi)
10866 		pf->next_vsi = vsi->idx;
10867 
10868 unlock_vsi:
10869 	mutex_unlock(&pf->switch_mutex);
10870 free_vsi:
10871 	kfree(vsi);
10872 
10873 	return 0;
10874 }
10875 
10876 /**
10877  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
10878  * @vsi: the VSI being cleaned
10879  **/
i40e_vsi_clear_rings(struct i40e_vsi * vsi)10880 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
10881 {
10882 	int i;
10883 
10884 	if (vsi->tx_rings && vsi->tx_rings[0]) {
10885 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10886 			kfree_rcu(vsi->tx_rings[i], rcu);
10887 			WRITE_ONCE(vsi->tx_rings[i], NULL);
10888 			WRITE_ONCE(vsi->rx_rings[i], NULL);
10889 			if (vsi->xdp_rings)
10890 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
10891 		}
10892 	}
10893 }
10894 
10895 /**
10896  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
10897  * @vsi: the VSI being configured
10898  **/
i40e_alloc_rings(struct i40e_vsi * vsi)10899 static int i40e_alloc_rings(struct i40e_vsi *vsi)
10900 {
10901 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
10902 	struct i40e_pf *pf = vsi->back;
10903 	struct i40e_ring *ring;
10904 
10905 	/* Set basic values in the rings to be used later during open() */
10906 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
10907 		/* allocate space for both Tx and Rx in one shot */
10908 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
10909 		if (!ring)
10910 			goto err_out;
10911 
10912 		ring->queue_index = i;
10913 		ring->reg_idx = vsi->base_queue + i;
10914 		ring->ring_active = false;
10915 		ring->vsi = vsi;
10916 		ring->netdev = vsi->netdev;
10917 		ring->dev = &pf->pdev->dev;
10918 		ring->count = vsi->num_tx_desc;
10919 		ring->size = 0;
10920 		ring->dcb_tc = 0;
10921 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10922 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10923 		ring->itr_setting = pf->tx_itr_default;
10924 		WRITE_ONCE(vsi->tx_rings[i], ring++);
10925 
10926 		if (!i40e_enabled_xdp_vsi(vsi))
10927 			goto setup_rx;
10928 
10929 		ring->queue_index = vsi->alloc_queue_pairs + i;
10930 		ring->reg_idx = vsi->base_queue + ring->queue_index;
10931 		ring->ring_active = false;
10932 		ring->vsi = vsi;
10933 		ring->netdev = NULL;
10934 		ring->dev = &pf->pdev->dev;
10935 		ring->count = vsi->num_tx_desc;
10936 		ring->size = 0;
10937 		ring->dcb_tc = 0;
10938 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
10939 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
10940 		set_ring_xdp(ring);
10941 		ring->itr_setting = pf->tx_itr_default;
10942 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
10943 
10944 setup_rx:
10945 		ring->queue_index = i;
10946 		ring->reg_idx = vsi->base_queue + i;
10947 		ring->ring_active = false;
10948 		ring->vsi = vsi;
10949 		ring->netdev = vsi->netdev;
10950 		ring->dev = &pf->pdev->dev;
10951 		ring->count = vsi->num_rx_desc;
10952 		ring->size = 0;
10953 		ring->dcb_tc = 0;
10954 		ring->itr_setting = pf->rx_itr_default;
10955 		WRITE_ONCE(vsi->rx_rings[i], ring);
10956 	}
10957 
10958 	return 0;
10959 
10960 err_out:
10961 	i40e_vsi_clear_rings(vsi);
10962 	return -ENOMEM;
10963 }
10964 
10965 /**
10966  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
10967  * @pf: board private structure
10968  * @vectors: the number of MSI-X vectors to request
10969  *
10970  * Returns the number of vectors reserved, or error
10971  **/
i40e_reserve_msix_vectors(struct i40e_pf * pf,int vectors)10972 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
10973 {
10974 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
10975 					I40E_MIN_MSIX, vectors);
10976 	if (vectors < 0) {
10977 		dev_info(&pf->pdev->dev,
10978 			 "MSI-X vector reservation failed: %d\n", vectors);
10979 		vectors = 0;
10980 	}
10981 
10982 	return vectors;
10983 }
10984 
10985 /**
10986  * i40e_init_msix - Setup the MSIX capability
10987  * @pf: board private structure
10988  *
10989  * Work with the OS to set up the MSIX vectors needed.
10990  *
10991  * Returns the number of vectors reserved or negative on failure
10992  **/
i40e_init_msix(struct i40e_pf * pf)10993 static int i40e_init_msix(struct i40e_pf *pf)
10994 {
10995 	struct i40e_hw *hw = &pf->hw;
10996 	int cpus, extra_vectors;
10997 	int vectors_left;
10998 	int v_budget, i;
10999 	int v_actual;
11000 	int iwarp_requested = 0;
11001 
11002 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11003 		return -ENODEV;
11004 
11005 	/* The number of vectors we'll request will be comprised of:
11006 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11007 	 *   - The number of LAN queue pairs
11008 	 *	- Queues being used for RSS.
11009 	 *		We don't need as many as max_rss_size vectors.
11010 	 *		use rss_size instead in the calculation since that
11011 	 *		is governed by number of cpus in the system.
11012 	 *	- assumes symmetric Tx/Rx pairing
11013 	 *   - The number of VMDq pairs
11014 	 *   - The CPU count within the NUMA node if iWARP is enabled
11015 	 * Once we count this up, try the request.
11016 	 *
11017 	 * If we can't get what we want, we'll simplify to nearly nothing
11018 	 * and try again.  If that still fails, we punt.
11019 	 */
11020 	vectors_left = hw->func_caps.num_msix_vectors;
11021 	v_budget = 0;
11022 
11023 	/* reserve one vector for miscellaneous handler */
11024 	if (vectors_left) {
11025 		v_budget++;
11026 		vectors_left--;
11027 	}
11028 
11029 	/* reserve some vectors for the main PF traffic queues. Initially we
11030 	 * only reserve at most 50% of the available vectors, in the case that
11031 	 * the number of online CPUs is large. This ensures that we can enable
11032 	 * extra features as well. Once we've enabled the other features, we
11033 	 * will use any remaining vectors to reach as close as we can to the
11034 	 * number of online CPUs.
11035 	 */
11036 	cpus = num_online_cpus();
11037 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11038 	vectors_left -= pf->num_lan_msix;
11039 
11040 	/* reserve one vector for sideband flow director */
11041 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11042 		if (vectors_left) {
11043 			pf->num_fdsb_msix = 1;
11044 			v_budget++;
11045 			vectors_left--;
11046 		} else {
11047 			pf->num_fdsb_msix = 0;
11048 		}
11049 	}
11050 
11051 	/* can we reserve enough for iWARP? */
11052 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11053 		iwarp_requested = pf->num_iwarp_msix;
11054 
11055 		if (!vectors_left)
11056 			pf->num_iwarp_msix = 0;
11057 		else if (vectors_left < pf->num_iwarp_msix)
11058 			pf->num_iwarp_msix = 1;
11059 		v_budget += pf->num_iwarp_msix;
11060 		vectors_left -= pf->num_iwarp_msix;
11061 	}
11062 
11063 	/* any vectors left over go for VMDq support */
11064 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11065 		if (!vectors_left) {
11066 			pf->num_vmdq_msix = 0;
11067 			pf->num_vmdq_qps = 0;
11068 		} else {
11069 			int vmdq_vecs_wanted =
11070 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11071 			int vmdq_vecs =
11072 				min_t(int, vectors_left, vmdq_vecs_wanted);
11073 
11074 			/* if we're short on vectors for what's desired, we limit
11075 			 * the queues per vmdq.  If this is still more than are
11076 			 * available, the user will need to change the number of
11077 			 * queues/vectors used by the PF later with the ethtool
11078 			 * channels command
11079 			 */
11080 			if (vectors_left < vmdq_vecs_wanted) {
11081 				pf->num_vmdq_qps = 1;
11082 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11083 				vmdq_vecs = min_t(int,
11084 						  vectors_left,
11085 						  vmdq_vecs_wanted);
11086 			}
11087 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11088 
11089 			v_budget += vmdq_vecs;
11090 			vectors_left -= vmdq_vecs;
11091 		}
11092 	}
11093 
11094 	/* On systems with a large number of SMP cores, we previously limited
11095 	 * the number of vectors for num_lan_msix to be at most 50% of the
11096 	 * available vectors, to allow for other features. Now, we add back
11097 	 * the remaining vectors. However, we ensure that the total
11098 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11099 	 * calculate the number of vectors we can add without going over the
11100 	 * cap of CPUs. For systems with a small number of CPUs this will be
11101 	 * zero.
11102 	 */
11103 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11104 	pf->num_lan_msix += extra_vectors;
11105 	vectors_left -= extra_vectors;
11106 
11107 	WARN(vectors_left < 0,
11108 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11109 
11110 	v_budget += pf->num_lan_msix;
11111 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11112 				   GFP_KERNEL);
11113 	if (!pf->msix_entries)
11114 		return -ENOMEM;
11115 
11116 	for (i = 0; i < v_budget; i++)
11117 		pf->msix_entries[i].entry = i;
11118 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11119 
11120 	if (v_actual < I40E_MIN_MSIX) {
11121 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11122 		kfree(pf->msix_entries);
11123 		pf->msix_entries = NULL;
11124 		pci_disable_msix(pf->pdev);
11125 		return -ENODEV;
11126 
11127 	} else if (v_actual == I40E_MIN_MSIX) {
11128 		/* Adjust for minimal MSIX use */
11129 		pf->num_vmdq_vsis = 0;
11130 		pf->num_vmdq_qps = 0;
11131 		pf->num_lan_qps = 1;
11132 		pf->num_lan_msix = 1;
11133 
11134 	} else if (v_actual != v_budget) {
11135 		/* If we have limited resources, we will start with no vectors
11136 		 * for the special features and then allocate vectors to some
11137 		 * of these features based on the policy and at the end disable
11138 		 * the features that did not get any vectors.
11139 		 */
11140 		int vec;
11141 
11142 		dev_info(&pf->pdev->dev,
11143 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11144 			 v_actual, v_budget);
11145 		/* reserve the misc vector */
11146 		vec = v_actual - 1;
11147 
11148 		/* Scale vector usage down */
11149 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11150 		pf->num_vmdq_vsis = 1;
11151 		pf->num_vmdq_qps = 1;
11152 
11153 		/* partition out the remaining vectors */
11154 		switch (vec) {
11155 		case 2:
11156 			pf->num_lan_msix = 1;
11157 			break;
11158 		case 3:
11159 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11160 				pf->num_lan_msix = 1;
11161 				pf->num_iwarp_msix = 1;
11162 			} else {
11163 				pf->num_lan_msix = 2;
11164 			}
11165 			break;
11166 		default:
11167 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11168 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11169 						 iwarp_requested);
11170 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11171 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11172 			} else {
11173 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11174 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11175 			}
11176 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11177 				pf->num_fdsb_msix = 1;
11178 				vec--;
11179 			}
11180 			pf->num_lan_msix = min_t(int,
11181 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11182 							      pf->num_lan_msix);
11183 			pf->num_lan_qps = pf->num_lan_msix;
11184 			break;
11185 		}
11186 	}
11187 
11188 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11189 	    (pf->num_fdsb_msix == 0)) {
11190 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11191 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11192 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11193 	}
11194 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11195 	    (pf->num_vmdq_msix == 0)) {
11196 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11197 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11198 	}
11199 
11200 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11201 	    (pf->num_iwarp_msix == 0)) {
11202 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11203 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11204 	}
11205 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11206 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11207 		   pf->num_lan_msix,
11208 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11209 		   pf->num_fdsb_msix,
11210 		   pf->num_iwarp_msix);
11211 
11212 	return v_actual;
11213 }
11214 
11215 /**
11216  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11217  * @vsi: the VSI being configured
11218  * @v_idx: index of the vector in the vsi struct
11219  *
11220  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11221  **/
i40e_vsi_alloc_q_vector(struct i40e_vsi * vsi,int v_idx)11222 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11223 {
11224 	struct i40e_q_vector *q_vector;
11225 
11226 	/* allocate q_vector */
11227 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11228 	if (!q_vector)
11229 		return -ENOMEM;
11230 
11231 	q_vector->vsi = vsi;
11232 	q_vector->v_idx = v_idx;
11233 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11234 
11235 	if (vsi->netdev)
11236 		netif_napi_add(vsi->netdev, &q_vector->napi,
11237 			       i40e_napi_poll, NAPI_POLL_WEIGHT);
11238 
11239 	/* tie q_vector and vsi together */
11240 	vsi->q_vectors[v_idx] = q_vector;
11241 
11242 	return 0;
11243 }
11244 
11245 /**
11246  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11247  * @vsi: the VSI being configured
11248  *
11249  * We allocate one q_vector per queue interrupt.  If allocation fails we
11250  * return -ENOMEM.
11251  **/
i40e_vsi_alloc_q_vectors(struct i40e_vsi * vsi)11252 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11253 {
11254 	struct i40e_pf *pf = vsi->back;
11255 	int err, v_idx, num_q_vectors;
11256 
11257 	/* if not MSIX, give the one vector only to the LAN VSI */
11258 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11259 		num_q_vectors = vsi->num_q_vectors;
11260 	else if (vsi == pf->vsi[pf->lan_vsi])
11261 		num_q_vectors = 1;
11262 	else
11263 		return -EINVAL;
11264 
11265 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11266 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11267 		if (err)
11268 			goto err_out;
11269 	}
11270 
11271 	return 0;
11272 
11273 err_out:
11274 	while (v_idx--)
11275 		i40e_free_q_vector(vsi, v_idx);
11276 
11277 	return err;
11278 }
11279 
11280 /**
11281  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11282  * @pf: board private structure to initialize
11283  **/
i40e_init_interrupt_scheme(struct i40e_pf * pf)11284 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11285 {
11286 	int vectors = 0;
11287 	ssize_t size;
11288 
11289 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11290 		vectors = i40e_init_msix(pf);
11291 		if (vectors < 0) {
11292 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11293 				       I40E_FLAG_IWARP_ENABLED	|
11294 				       I40E_FLAG_RSS_ENABLED	|
11295 				       I40E_FLAG_DCB_CAPABLE	|
11296 				       I40E_FLAG_DCB_ENABLED	|
11297 				       I40E_FLAG_SRIOV_ENABLED	|
11298 				       I40E_FLAG_FD_SB_ENABLED	|
11299 				       I40E_FLAG_FD_ATR_ENABLED	|
11300 				       I40E_FLAG_VMDQ_ENABLED);
11301 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11302 
11303 			/* rework the queue expectations without MSIX */
11304 			i40e_determine_queue_usage(pf);
11305 		}
11306 	}
11307 
11308 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
11309 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
11310 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
11311 		vectors = pci_enable_msi(pf->pdev);
11312 		if (vectors < 0) {
11313 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
11314 				 vectors);
11315 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
11316 		}
11317 		vectors = 1;  /* one MSI or Legacy vector */
11318 	}
11319 
11320 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
11321 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
11322 
11323 	/* set up vector assignment tracking */
11324 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
11325 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
11326 	if (!pf->irq_pile)
11327 		return -ENOMEM;
11328 
11329 	pf->irq_pile->num_entries = vectors;
11330 
11331 	/* track first vector for misc interrupts, ignore return */
11332 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
11333 
11334 	return 0;
11335 }
11336 
11337 /**
11338  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
11339  * @pf: private board data structure
11340  *
11341  * Restore the interrupt scheme that was cleared when we suspended the
11342  * device. This should be called during resume to re-allocate the q_vectors
11343  * and reacquire IRQs.
11344  */
i40e_restore_interrupt_scheme(struct i40e_pf * pf)11345 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
11346 {
11347 	int err, i;
11348 
11349 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
11350 	 * scheme. We need to re-enabled them here in order to attempt to
11351 	 * re-acquire the MSI or MSI-X vectors
11352 	 */
11353 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
11354 
11355 	err = i40e_init_interrupt_scheme(pf);
11356 	if (err)
11357 		return err;
11358 
11359 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
11360 	 * rings together again.
11361 	 */
11362 	for (i = 0; i < pf->num_alloc_vsi; i++) {
11363 		if (pf->vsi[i]) {
11364 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
11365 			if (err)
11366 				goto err_unwind;
11367 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
11368 		}
11369 	}
11370 
11371 	err = i40e_setup_misc_vector(pf);
11372 	if (err)
11373 		goto err_unwind;
11374 
11375 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
11376 		i40e_client_update_msix_info(pf);
11377 
11378 	return 0;
11379 
11380 err_unwind:
11381 	while (i--) {
11382 		if (pf->vsi[i])
11383 			i40e_vsi_free_q_vectors(pf->vsi[i]);
11384 	}
11385 
11386 	return err;
11387 }
11388 
11389 /**
11390  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
11391  * non queue events in recovery mode
11392  * @pf: board private structure
11393  *
11394  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
11395  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
11396  * This is handled differently than in recovery mode since no Tx/Rx resources
11397  * are being allocated.
11398  **/
i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf * pf)11399 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
11400 {
11401 	int err;
11402 
11403 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11404 		err = i40e_setup_misc_vector(pf);
11405 
11406 		if (err) {
11407 			dev_info(&pf->pdev->dev,
11408 				 "MSI-X misc vector request failed, error %d\n",
11409 				 err);
11410 			return err;
11411 		}
11412 	} else {
11413 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
11414 
11415 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
11416 				  pf->int_name, pf);
11417 
11418 		if (err) {
11419 			dev_info(&pf->pdev->dev,
11420 				 "MSI/legacy misc vector request failed, error %d\n",
11421 				 err);
11422 			return err;
11423 		}
11424 		i40e_enable_misc_int_causes(pf);
11425 		i40e_irq_dynamic_enable_icr0(pf);
11426 	}
11427 
11428 	return 0;
11429 }
11430 
11431 /**
11432  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
11433  * @pf: board private structure
11434  *
11435  * This sets up the handler for MSIX 0, which is used to manage the
11436  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
11437  * when in MSI or Legacy interrupt mode.
11438  **/
i40e_setup_misc_vector(struct i40e_pf * pf)11439 static int i40e_setup_misc_vector(struct i40e_pf *pf)
11440 {
11441 	struct i40e_hw *hw = &pf->hw;
11442 	int err = 0;
11443 
11444 	/* Only request the IRQ once, the first time through. */
11445 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
11446 		err = request_irq(pf->msix_entries[0].vector,
11447 				  i40e_intr, 0, pf->int_name, pf);
11448 		if (err) {
11449 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
11450 			dev_info(&pf->pdev->dev,
11451 				 "request_irq for %s failed: %d\n",
11452 				 pf->int_name, err);
11453 			return -EFAULT;
11454 		}
11455 	}
11456 
11457 	i40e_enable_misc_int_causes(pf);
11458 
11459 	/* associate no queues to the misc vector */
11460 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
11461 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
11462 
11463 	i40e_flush(hw);
11464 
11465 	i40e_irq_dynamic_enable_icr0(pf);
11466 
11467 	return err;
11468 }
11469 
11470 /**
11471  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
11472  * @vsi: Pointer to vsi structure
11473  * @seed: Buffter to store the hash keys
11474  * @lut: Buffer to store the lookup table entries
11475  * @lut_size: Size of buffer to store the lookup table entries
11476  *
11477  * Return 0 on success, negative on failure
11478  */
i40e_get_rss_aq(struct i40e_vsi * vsi,const u8 * seed,u8 * lut,u16 lut_size)11479 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
11480 			   u8 *lut, u16 lut_size)
11481 {
11482 	struct i40e_pf *pf = vsi->back;
11483 	struct i40e_hw *hw = &pf->hw;
11484 	int ret = 0;
11485 
11486 	if (seed) {
11487 		ret = i40e_aq_get_rss_key(hw, vsi->id,
11488 			(struct i40e_aqc_get_set_rss_key_data *)seed);
11489 		if (ret) {
11490 			dev_info(&pf->pdev->dev,
11491 				 "Cannot get RSS key, err %s aq_err %s\n",
11492 				 i40e_stat_str(&pf->hw, ret),
11493 				 i40e_aq_str(&pf->hw,
11494 					     pf->hw.aq.asq_last_status));
11495 			return ret;
11496 		}
11497 	}
11498 
11499 	if (lut) {
11500 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
11501 
11502 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
11503 		if (ret) {
11504 			dev_info(&pf->pdev->dev,
11505 				 "Cannot get RSS lut, err %s aq_err %s\n",
11506 				 i40e_stat_str(&pf->hw, ret),
11507 				 i40e_aq_str(&pf->hw,
11508 					     pf->hw.aq.asq_last_status));
11509 			return ret;
11510 		}
11511 	}
11512 
11513 	return ret;
11514 }
11515 
11516 /**
11517  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
11518  * @vsi: Pointer to vsi structure
11519  * @seed: RSS hash seed
11520  * @lut: Lookup table
11521  * @lut_size: Lookup table size
11522  *
11523  * Returns 0 on success, negative on failure
11524  **/
i40e_config_rss_reg(struct i40e_vsi * vsi,const u8 * seed,const u8 * lut,u16 lut_size)11525 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
11526 			       const u8 *lut, u16 lut_size)
11527 {
11528 	struct i40e_pf *pf = vsi->back;
11529 	struct i40e_hw *hw = &pf->hw;
11530 	u16 vf_id = vsi->vf_id;
11531 	u8 i;
11532 
11533 	/* Fill out hash function seed */
11534 	if (seed) {
11535 		u32 *seed_dw = (u32 *)seed;
11536 
11537 		if (vsi->type == I40E_VSI_MAIN) {
11538 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11539 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
11540 		} else if (vsi->type == I40E_VSI_SRIOV) {
11541 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
11542 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
11543 		} else {
11544 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
11545 		}
11546 	}
11547 
11548 	if (lut) {
11549 		u32 *lut_dw = (u32 *)lut;
11550 
11551 		if (vsi->type == I40E_VSI_MAIN) {
11552 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
11553 				return -EINVAL;
11554 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11555 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
11556 		} else if (vsi->type == I40E_VSI_SRIOV) {
11557 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
11558 				return -EINVAL;
11559 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
11560 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
11561 		} else {
11562 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
11563 		}
11564 	}
11565 	i40e_flush(hw);
11566 
11567 	return 0;
11568 }
11569 
11570 /**
11571  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
11572  * @vsi: Pointer to VSI structure
11573  * @seed: Buffer to store the keys
11574  * @lut: Buffer to store the lookup table entries
11575  * @lut_size: Size of buffer to store the lookup table entries
11576  *
11577  * Returns 0 on success, negative on failure
11578  */
i40e_get_rss_reg(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)11579 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
11580 			    u8 *lut, u16 lut_size)
11581 {
11582 	struct i40e_pf *pf = vsi->back;
11583 	struct i40e_hw *hw = &pf->hw;
11584 	u16 i;
11585 
11586 	if (seed) {
11587 		u32 *seed_dw = (u32 *)seed;
11588 
11589 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
11590 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
11591 	}
11592 	if (lut) {
11593 		u32 *lut_dw = (u32 *)lut;
11594 
11595 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
11596 			return -EINVAL;
11597 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
11598 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
11599 	}
11600 
11601 	return 0;
11602 }
11603 
11604 /**
11605  * i40e_config_rss - Configure RSS keys and lut
11606  * @vsi: Pointer to VSI structure
11607  * @seed: RSS hash seed
11608  * @lut: Lookup table
11609  * @lut_size: Lookup table size
11610  *
11611  * Returns 0 on success, negative on failure
11612  */
i40e_config_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)11613 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11614 {
11615 	struct i40e_pf *pf = vsi->back;
11616 
11617 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11618 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
11619 	else
11620 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
11621 }
11622 
11623 /**
11624  * i40e_get_rss - Get RSS keys and lut
11625  * @vsi: Pointer to VSI structure
11626  * @seed: Buffer to store the keys
11627  * @lut: Buffer to store the lookup table entries
11628  * @lut_size: Size of buffer to store the lookup table entries
11629  *
11630  * Returns 0 on success, negative on failure
11631  */
i40e_get_rss(struct i40e_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)11632 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
11633 {
11634 	struct i40e_pf *pf = vsi->back;
11635 
11636 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
11637 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
11638 	else
11639 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
11640 }
11641 
11642 /**
11643  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
11644  * @pf: Pointer to board private structure
11645  * @lut: Lookup table
11646  * @rss_table_size: Lookup table size
11647  * @rss_size: Range of queue number for hashing
11648  */
i40e_fill_rss_lut(struct i40e_pf * pf,u8 * lut,u16 rss_table_size,u16 rss_size)11649 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
11650 		       u16 rss_table_size, u16 rss_size)
11651 {
11652 	u16 i;
11653 
11654 	for (i = 0; i < rss_table_size; i++)
11655 		lut[i] = i % rss_size;
11656 }
11657 
11658 /**
11659  * i40e_pf_config_rss - Prepare for RSS if used
11660  * @pf: board private structure
11661  **/
i40e_pf_config_rss(struct i40e_pf * pf)11662 static int i40e_pf_config_rss(struct i40e_pf *pf)
11663 {
11664 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11665 	u8 seed[I40E_HKEY_ARRAY_SIZE];
11666 	u8 *lut;
11667 	struct i40e_hw *hw = &pf->hw;
11668 	u32 reg_val;
11669 	u64 hena;
11670 	int ret;
11671 
11672 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
11673 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
11674 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
11675 	hena |= i40e_pf_get_default_rss_hena(pf);
11676 
11677 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
11678 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
11679 
11680 	/* Determine the RSS table size based on the hardware capabilities */
11681 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
11682 	reg_val = (pf->rss_table_size == 512) ?
11683 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
11684 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
11685 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
11686 
11687 	/* Determine the RSS size of the VSI */
11688 	if (!vsi->rss_size) {
11689 		u16 qcount;
11690 		/* If the firmware does something weird during VSI init, we
11691 		 * could end up with zero TCs. Check for that to avoid
11692 		 * divide-by-zero. It probably won't pass traffic, but it also
11693 		 * won't panic.
11694 		 */
11695 		qcount = vsi->num_queue_pairs /
11696 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
11697 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11698 	}
11699 	if (!vsi->rss_size)
11700 		return -EINVAL;
11701 
11702 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
11703 	if (!lut)
11704 		return -ENOMEM;
11705 
11706 	/* Use user configured lut if there is one, otherwise use default */
11707 	if (vsi->rss_lut_user)
11708 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
11709 	else
11710 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
11711 
11712 	/* Use user configured hash key if there is one, otherwise
11713 	 * use default.
11714 	 */
11715 	if (vsi->rss_hkey_user)
11716 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
11717 	else
11718 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
11719 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
11720 	kfree(lut);
11721 
11722 	return ret;
11723 }
11724 
11725 /**
11726  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
11727  * @pf: board private structure
11728  * @queue_count: the requested queue count for rss.
11729  *
11730  * returns 0 if rss is not enabled, if enabled returns the final rss queue
11731  * count which may be different from the requested queue count.
11732  * Note: expects to be called while under rtnl_lock()
11733  **/
i40e_reconfig_rss_queues(struct i40e_pf * pf,int queue_count)11734 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
11735 {
11736 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
11737 	int new_rss_size;
11738 
11739 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
11740 		return 0;
11741 
11742 	queue_count = min_t(int, queue_count, num_online_cpus());
11743 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
11744 
11745 	if (queue_count != vsi->num_queue_pairs) {
11746 		u16 qcount;
11747 
11748 		vsi->req_queue_pairs = queue_count;
11749 		i40e_prep_for_reset(pf, true);
11750 
11751 		pf->alloc_rss_size = new_rss_size;
11752 
11753 		i40e_reset_and_rebuild(pf, true, true);
11754 
11755 		/* Discard the user configured hash keys and lut, if less
11756 		 * queues are enabled.
11757 		 */
11758 		if (queue_count < vsi->rss_size) {
11759 			i40e_clear_rss_config_user(vsi);
11760 			dev_dbg(&pf->pdev->dev,
11761 				"discard user configured hash keys and lut\n");
11762 		}
11763 
11764 		/* Reset vsi->rss_size, as number of enabled queues changed */
11765 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
11766 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
11767 
11768 		i40e_pf_config_rss(pf);
11769 	}
11770 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
11771 		 vsi->req_queue_pairs, pf->rss_size_max);
11772 	return pf->alloc_rss_size;
11773 }
11774 
11775 /**
11776  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
11777  * @pf: board private structure
11778  **/
i40e_get_partition_bw_setting(struct i40e_pf * pf)11779 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
11780 {
11781 	i40e_status status;
11782 	bool min_valid, max_valid;
11783 	u32 max_bw, min_bw;
11784 
11785 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
11786 					   &min_valid, &max_valid);
11787 
11788 	if (!status) {
11789 		if (min_valid)
11790 			pf->min_bw = min_bw;
11791 		if (max_valid)
11792 			pf->max_bw = max_bw;
11793 	}
11794 
11795 	return status;
11796 }
11797 
11798 /**
11799  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
11800  * @pf: board private structure
11801  **/
i40e_set_partition_bw_setting(struct i40e_pf * pf)11802 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
11803 {
11804 	struct i40e_aqc_configure_partition_bw_data bw_data;
11805 	i40e_status status;
11806 
11807 	memset(&bw_data, 0, sizeof(bw_data));
11808 
11809 	/* Set the valid bit for this PF */
11810 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
11811 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
11812 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
11813 
11814 	/* Set the new bandwidths */
11815 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
11816 
11817 	return status;
11818 }
11819 
11820 /**
11821  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
11822  * @pf: board private structure
11823  **/
i40e_commit_partition_bw_setting(struct i40e_pf * pf)11824 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
11825 {
11826 	/* Commit temporary BW setting to permanent NVM image */
11827 	enum i40e_admin_queue_err last_aq_status;
11828 	i40e_status ret;
11829 	u16 nvm_word;
11830 
11831 	if (pf->hw.partition_id != 1) {
11832 		dev_info(&pf->pdev->dev,
11833 			 "Commit BW only works on partition 1! This is partition %d",
11834 			 pf->hw.partition_id);
11835 		ret = I40E_NOT_SUPPORTED;
11836 		goto bw_commit_out;
11837 	}
11838 
11839 	/* Acquire NVM for read access */
11840 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
11841 	last_aq_status = pf->hw.aq.asq_last_status;
11842 	if (ret) {
11843 		dev_info(&pf->pdev->dev,
11844 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
11845 			 i40e_stat_str(&pf->hw, ret),
11846 			 i40e_aq_str(&pf->hw, last_aq_status));
11847 		goto bw_commit_out;
11848 	}
11849 
11850 	/* Read word 0x10 of NVM - SW compatibility word 1 */
11851 	ret = i40e_aq_read_nvm(&pf->hw,
11852 			       I40E_SR_NVM_CONTROL_WORD,
11853 			       0x10, sizeof(nvm_word), &nvm_word,
11854 			       false, NULL);
11855 	/* Save off last admin queue command status before releasing
11856 	 * the NVM
11857 	 */
11858 	last_aq_status = pf->hw.aq.asq_last_status;
11859 	i40e_release_nvm(&pf->hw);
11860 	if (ret) {
11861 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
11862 			 i40e_stat_str(&pf->hw, ret),
11863 			 i40e_aq_str(&pf->hw, last_aq_status));
11864 		goto bw_commit_out;
11865 	}
11866 
11867 	/* Wait a bit for NVM release to complete */
11868 	msleep(50);
11869 
11870 	/* Acquire NVM for write access */
11871 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
11872 	last_aq_status = pf->hw.aq.asq_last_status;
11873 	if (ret) {
11874 		dev_info(&pf->pdev->dev,
11875 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
11876 			 i40e_stat_str(&pf->hw, ret),
11877 			 i40e_aq_str(&pf->hw, last_aq_status));
11878 		goto bw_commit_out;
11879 	}
11880 	/* Write it back out unchanged to initiate update NVM,
11881 	 * which will force a write of the shadow (alt) RAM to
11882 	 * the NVM - thus storing the bandwidth values permanently.
11883 	 */
11884 	ret = i40e_aq_update_nvm(&pf->hw,
11885 				 I40E_SR_NVM_CONTROL_WORD,
11886 				 0x10, sizeof(nvm_word),
11887 				 &nvm_word, true, 0, NULL);
11888 	/* Save off last admin queue command status before releasing
11889 	 * the NVM
11890 	 */
11891 	last_aq_status = pf->hw.aq.asq_last_status;
11892 	i40e_release_nvm(&pf->hw);
11893 	if (ret)
11894 		dev_info(&pf->pdev->dev,
11895 			 "BW settings NOT SAVED, err %s aq_err %s\n",
11896 			 i40e_stat_str(&pf->hw, ret),
11897 			 i40e_aq_str(&pf->hw, last_aq_status));
11898 bw_commit_out:
11899 
11900 	return ret;
11901 }
11902 
11903 /**
11904  * i40e_is_total_port_shutdown_enabled - read NVM and return value
11905  * if total port shutdown feature is enabled for this PF
11906  * @pf: board private structure
11907  **/
i40e_is_total_port_shutdown_enabled(struct i40e_pf * pf)11908 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
11909 {
11910 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
11911 #define I40E_FEATURES_ENABLE_PTR		0x2A
11912 #define I40E_CURRENT_SETTING_PTR		0x2B
11913 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
11914 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
11915 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
11916 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
11917 	i40e_status read_status = I40E_SUCCESS;
11918 	u16 sr_emp_sr_settings_ptr = 0;
11919 	u16 features_enable = 0;
11920 	u16 link_behavior = 0;
11921 	bool ret = false;
11922 
11923 	read_status = i40e_read_nvm_word(&pf->hw,
11924 					 I40E_SR_EMP_SR_SETTINGS_PTR,
11925 					 &sr_emp_sr_settings_ptr);
11926 	if (read_status)
11927 		goto err_nvm;
11928 	read_status = i40e_read_nvm_word(&pf->hw,
11929 					 sr_emp_sr_settings_ptr +
11930 					 I40E_FEATURES_ENABLE_PTR,
11931 					 &features_enable);
11932 	if (read_status)
11933 		goto err_nvm;
11934 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
11935 		read_status = i40e_read_nvm_module_data(&pf->hw,
11936 							I40E_SR_EMP_SR_SETTINGS_PTR,
11937 							I40E_CURRENT_SETTING_PTR,
11938 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
11939 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
11940 							&link_behavior);
11941 		if (read_status)
11942 			goto err_nvm;
11943 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
11944 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
11945 	}
11946 	return ret;
11947 
11948 err_nvm:
11949 	dev_warn(&pf->pdev->dev,
11950 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
11951 		 i40e_stat_str(&pf->hw, read_status));
11952 	return ret;
11953 }
11954 
11955 /**
11956  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
11957  * @pf: board private structure to initialize
11958  *
11959  * i40e_sw_init initializes the Adapter private data structure.
11960  * Fields are initialized based on PCI device information and
11961  * OS network device settings (MTU size).
11962  **/
i40e_sw_init(struct i40e_pf * pf)11963 static int i40e_sw_init(struct i40e_pf *pf)
11964 {
11965 	int err = 0;
11966 	int size;
11967 	u16 pow;
11968 
11969 	/* Set default capability flags */
11970 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
11971 		    I40E_FLAG_MSI_ENABLED     |
11972 		    I40E_FLAG_MSIX_ENABLED;
11973 
11974 	/* Set default ITR */
11975 	pf->rx_itr_default = I40E_ITR_RX_DEF;
11976 	pf->tx_itr_default = I40E_ITR_TX_DEF;
11977 
11978 	/* Depending on PF configurations, it is possible that the RSS
11979 	 * maximum might end up larger than the available queues
11980 	 */
11981 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
11982 	pf->alloc_rss_size = 1;
11983 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
11984 	pf->rss_size_max = min_t(int, pf->rss_size_max,
11985 				 pf->hw.func_caps.num_tx_qp);
11986 
11987 	/* find the next higher power-of-2 of num cpus */
11988 	pow = roundup_pow_of_two(num_online_cpus());
11989 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
11990 
11991 	if (pf->hw.func_caps.rss) {
11992 		pf->flags |= I40E_FLAG_RSS_ENABLED;
11993 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
11994 					   num_online_cpus());
11995 	}
11996 
11997 	/* MFP mode enabled */
11998 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
11999 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12000 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12001 		if (i40e_get_partition_bw_setting(pf)) {
12002 			dev_warn(&pf->pdev->dev,
12003 				 "Could not get partition bw settings\n");
12004 		} else {
12005 			dev_info(&pf->pdev->dev,
12006 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12007 				 pf->min_bw, pf->max_bw);
12008 
12009 			/* nudge the Tx scheduler */
12010 			i40e_set_partition_bw_setting(pf);
12011 		}
12012 	}
12013 
12014 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12015 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12016 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12017 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12018 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12019 		    pf->hw.num_partitions > 1)
12020 			dev_info(&pf->pdev->dev,
12021 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12022 		else
12023 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12024 		pf->fdir_pf_filter_count =
12025 				 pf->hw.func_caps.fd_filters_guaranteed;
12026 		pf->hw.fdir_shared_filter_count =
12027 				 pf->hw.func_caps.fd_filters_best_effort;
12028 	}
12029 
12030 	if (pf->hw.mac.type == I40E_MAC_X722) {
12031 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12032 				    I40E_HW_128_QP_RSS_CAPABLE |
12033 				    I40E_HW_ATR_EVICT_CAPABLE |
12034 				    I40E_HW_WB_ON_ITR_CAPABLE |
12035 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12036 				    I40E_HW_NO_PCI_LINK_CHECK |
12037 				    I40E_HW_USE_SET_LLDP_MIB |
12038 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12039 				    I40E_HW_PTP_L4_CAPABLE |
12040 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12041 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12042 
12043 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12044 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12045 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12046 			dev_warn(&pf->pdev->dev,
12047 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12048 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12049 		}
12050 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12051 		   ((pf->hw.aq.api_maj_ver == 1) &&
12052 		    (pf->hw.aq.api_min_ver > 4))) {
12053 		/* Supported in FW API version higher than 1.4 */
12054 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12055 	}
12056 
12057 	/* Enable HW ATR eviction if possible */
12058 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12059 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12060 
12061 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12062 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12063 	    (pf->hw.aq.fw_maj_ver < 4))) {
12064 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12065 		/* No DCB support  for FW < v4.33 */
12066 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12067 	}
12068 
12069 	/* Disable FW LLDP if FW < v4.3 */
12070 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12071 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12072 	    (pf->hw.aq.fw_maj_ver < 4)))
12073 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12074 
12075 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12076 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12077 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12078 	    (pf->hw.aq.fw_maj_ver >= 5)))
12079 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12080 
12081 	/* Enable PTP L4 if FW > v6.0 */
12082 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12083 	    pf->hw.aq.fw_maj_ver >= 6)
12084 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12085 
12086 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12087 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12088 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12089 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12090 	}
12091 
12092 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12093 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12094 		/* IWARP needs one extra vector for CQP just like MISC.*/
12095 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12096 	}
12097 	/* Stopping FW LLDP engine is supported on XL710 and X722
12098 	 * starting from FW versions determined in i40e_init_adminq.
12099 	 * Stopping the FW LLDP engine is not supported on XL710
12100 	 * if NPAR is functioning so unset this hw flag in this case.
12101 	 */
12102 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12103 	    pf->hw.func_caps.npar_enable &&
12104 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12105 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12106 
12107 #ifdef CONFIG_PCI_IOV
12108 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12109 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12110 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12111 		pf->num_req_vfs = min_t(int,
12112 					pf->hw.func_caps.num_vfs,
12113 					I40E_MAX_VF_COUNT);
12114 	}
12115 #endif /* CONFIG_PCI_IOV */
12116 	pf->eeprom_version = 0xDEAD;
12117 	pf->lan_veb = I40E_NO_VEB;
12118 	pf->lan_vsi = I40E_NO_VSI;
12119 
12120 	/* By default FW has this off for performance reasons */
12121 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12122 
12123 	/* set up queue assignment tracking */
12124 	size = sizeof(struct i40e_lump_tracking)
12125 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12126 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12127 	if (!pf->qp_pile) {
12128 		err = -ENOMEM;
12129 		goto sw_init_done;
12130 	}
12131 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12132 
12133 	pf->tx_timeout_recovery_level = 1;
12134 
12135 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12136 	    i40e_is_total_port_shutdown_enabled(pf)) {
12137 		/* Link down on close must be on when total port shutdown
12138 		 * is enabled for a given port
12139 		 */
12140 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12141 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12142 		dev_info(&pf->pdev->dev,
12143 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12144 	}
12145 	mutex_init(&pf->switch_mutex);
12146 
12147 sw_init_done:
12148 	return err;
12149 }
12150 
12151 /**
12152  * i40e_set_ntuple - set the ntuple feature flag and take action
12153  * @pf: board private structure to initialize
12154  * @features: the feature set that the stack is suggesting
12155  *
12156  * returns a bool to indicate if reset needs to happen
12157  **/
i40e_set_ntuple(struct i40e_pf * pf,netdev_features_t features)12158 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12159 {
12160 	bool need_reset = false;
12161 
12162 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12163 	 * the state changed, we need to reset.
12164 	 */
12165 	if (features & NETIF_F_NTUPLE) {
12166 		/* Enable filters and mark for reset */
12167 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12168 			need_reset = true;
12169 		/* enable FD_SB only if there is MSI-X vector and no cloud
12170 		 * filters exist
12171 		 */
12172 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12173 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12174 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12175 		}
12176 	} else {
12177 		/* turn off filters, mark for reset and clear SW filter list */
12178 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12179 			need_reset = true;
12180 			i40e_fdir_filter_exit(pf);
12181 		}
12182 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12183 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12184 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12185 
12186 		/* reset fd counters */
12187 		pf->fd_add_err = 0;
12188 		pf->fd_atr_cnt = 0;
12189 		/* if ATR was auto disabled it can be re-enabled. */
12190 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12191 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12192 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12193 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12194 	}
12195 	return need_reset;
12196 }
12197 
12198 /**
12199  * i40e_clear_rss_lut - clear the rx hash lookup table
12200  * @vsi: the VSI being configured
12201  **/
i40e_clear_rss_lut(struct i40e_vsi * vsi)12202 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12203 {
12204 	struct i40e_pf *pf = vsi->back;
12205 	struct i40e_hw *hw = &pf->hw;
12206 	u16 vf_id = vsi->vf_id;
12207 	u8 i;
12208 
12209 	if (vsi->type == I40E_VSI_MAIN) {
12210 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12211 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12212 	} else if (vsi->type == I40E_VSI_SRIOV) {
12213 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12214 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12215 	} else {
12216 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12217 	}
12218 }
12219 
12220 /**
12221  * i40e_set_features - set the netdev feature flags
12222  * @netdev: ptr to the netdev being adjusted
12223  * @features: the feature set that the stack is suggesting
12224  * Note: expects to be called while under rtnl_lock()
12225  **/
i40e_set_features(struct net_device * netdev,netdev_features_t features)12226 static int i40e_set_features(struct net_device *netdev,
12227 			     netdev_features_t features)
12228 {
12229 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12230 	struct i40e_vsi *vsi = np->vsi;
12231 	struct i40e_pf *pf = vsi->back;
12232 	bool need_reset;
12233 
12234 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12235 		i40e_pf_config_rss(pf);
12236 	else if (!(features & NETIF_F_RXHASH) &&
12237 		 netdev->features & NETIF_F_RXHASH)
12238 		i40e_clear_rss_lut(vsi);
12239 
12240 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12241 		i40e_vlan_stripping_enable(vsi);
12242 	else
12243 		i40e_vlan_stripping_disable(vsi);
12244 
12245 	if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12246 		dev_err(&pf->pdev->dev,
12247 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12248 		return -EINVAL;
12249 	}
12250 
12251 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12252 		i40e_del_all_macvlans(vsi);
12253 
12254 	need_reset = i40e_set_ntuple(pf, features);
12255 
12256 	if (need_reset)
12257 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12258 
12259 	return 0;
12260 }
12261 
i40e_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)12262 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12263 				    unsigned int table, unsigned int idx,
12264 				    struct udp_tunnel_info *ti)
12265 {
12266 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12267 	struct i40e_hw *hw = &np->vsi->back->hw;
12268 	u8 type, filter_index;
12269 	i40e_status ret;
12270 
12271 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12272 						   I40E_AQC_TUNNEL_TYPE_NGE;
12273 
12274 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12275 				     NULL);
12276 	if (ret) {
12277 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12278 			    i40e_stat_str(hw, ret),
12279 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12280 		return -EIO;
12281 	}
12282 
12283 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12284 	return 0;
12285 }
12286 
i40e_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)12287 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12288 				      unsigned int table, unsigned int idx,
12289 				      struct udp_tunnel_info *ti)
12290 {
12291 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12292 	struct i40e_hw *hw = &np->vsi->back->hw;
12293 	i40e_status ret;
12294 
12295 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12296 	if (ret) {
12297 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
12298 			    i40e_stat_str(hw, ret),
12299 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12300 		return -EIO;
12301 	}
12302 
12303 	return 0;
12304 }
12305 
i40e_get_phys_port_id(struct net_device * netdev,struct netdev_phys_item_id * ppid)12306 static int i40e_get_phys_port_id(struct net_device *netdev,
12307 				 struct netdev_phys_item_id *ppid)
12308 {
12309 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12310 	struct i40e_pf *pf = np->vsi->back;
12311 	struct i40e_hw *hw = &pf->hw;
12312 
12313 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
12314 		return -EOPNOTSUPP;
12315 
12316 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
12317 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
12318 
12319 	return 0;
12320 }
12321 
12322 /**
12323  * i40e_ndo_fdb_add - add an entry to the hardware database
12324  * @ndm: the input from the stack
12325  * @tb: pointer to array of nladdr (unused)
12326  * @dev: the net device pointer
12327  * @addr: the MAC address entry being added
12328  * @vid: VLAN ID
12329  * @flags: instructions from stack about fdb operation
12330  * @extack: netlink extended ack, unused currently
12331  */
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)12332 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
12333 			    struct net_device *dev,
12334 			    const unsigned char *addr, u16 vid,
12335 			    u16 flags,
12336 			    struct netlink_ext_ack *extack)
12337 {
12338 	struct i40e_netdev_priv *np = netdev_priv(dev);
12339 	struct i40e_pf *pf = np->vsi->back;
12340 	int err = 0;
12341 
12342 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
12343 		return -EOPNOTSUPP;
12344 
12345 	if (vid) {
12346 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
12347 		return -EINVAL;
12348 	}
12349 
12350 	/* Hardware does not support aging addresses so if a
12351 	 * ndm_state is given only allow permanent addresses
12352 	 */
12353 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
12354 		netdev_info(dev, "FDB only supports static addresses\n");
12355 		return -EINVAL;
12356 	}
12357 
12358 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
12359 		err = dev_uc_add_excl(dev, addr);
12360 	else if (is_multicast_ether_addr(addr))
12361 		err = dev_mc_add_excl(dev, addr);
12362 	else
12363 		err = -EINVAL;
12364 
12365 	/* Only return duplicate errors if NLM_F_EXCL is set */
12366 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
12367 		err = 0;
12368 
12369 	return err;
12370 }
12371 
12372 /**
12373  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
12374  * @dev: the netdev being configured
12375  * @nlh: RTNL message
12376  * @flags: bridge flags
12377  * @extack: netlink extended ack
12378  *
12379  * Inserts a new hardware bridge if not already created and
12380  * enables the bridging mode requested (VEB or VEPA). If the
12381  * hardware bridge has already been inserted and the request
12382  * is to change the mode then that requires a PF reset to
12383  * allow rebuild of the components with required hardware
12384  * bridge mode enabled.
12385  *
12386  * Note: expects to be called while under rtnl_lock()
12387  **/
i40e_ndo_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 flags,struct netlink_ext_ack * extack)12388 static int i40e_ndo_bridge_setlink(struct net_device *dev,
12389 				   struct nlmsghdr *nlh,
12390 				   u16 flags,
12391 				   struct netlink_ext_ack *extack)
12392 {
12393 	struct i40e_netdev_priv *np = netdev_priv(dev);
12394 	struct i40e_vsi *vsi = np->vsi;
12395 	struct i40e_pf *pf = vsi->back;
12396 	struct i40e_veb *veb = NULL;
12397 	struct nlattr *attr, *br_spec;
12398 	int i, rem;
12399 
12400 	/* Only for PF VSI for now */
12401 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12402 		return -EOPNOTSUPP;
12403 
12404 	/* Find the HW bridge for PF VSI */
12405 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12406 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12407 			veb = pf->veb[i];
12408 	}
12409 
12410 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
12411 
12412 	nla_for_each_nested(attr, br_spec, rem) {
12413 		__u16 mode;
12414 
12415 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
12416 			continue;
12417 
12418 		mode = nla_get_u16(attr);
12419 		if ((mode != BRIDGE_MODE_VEPA) &&
12420 		    (mode != BRIDGE_MODE_VEB))
12421 			return -EINVAL;
12422 
12423 		/* Insert a new HW bridge */
12424 		if (!veb) {
12425 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
12426 					     vsi->tc_config.enabled_tc);
12427 			if (veb) {
12428 				veb->bridge_mode = mode;
12429 				i40e_config_bridge_mode(veb);
12430 			} else {
12431 				/* No Bridge HW offload available */
12432 				return -ENOENT;
12433 			}
12434 			break;
12435 		} else if (mode != veb->bridge_mode) {
12436 			/* Existing HW bridge but different mode needs reset */
12437 			veb->bridge_mode = mode;
12438 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
12439 			if (mode == BRIDGE_MODE_VEB)
12440 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
12441 			else
12442 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
12443 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12444 			break;
12445 		}
12446 	}
12447 
12448 	return 0;
12449 }
12450 
12451 /**
12452  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
12453  * @skb: skb buff
12454  * @pid: process id
12455  * @seq: RTNL message seq #
12456  * @dev: the netdev being configured
12457  * @filter_mask: unused
12458  * @nlflags: netlink flags passed in
12459  *
12460  * Return the mode in which the hardware bridge is operating in
12461  * i.e VEB or VEPA.
12462  **/
i40e_ndo_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 __always_unused filter_mask,int nlflags)12463 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
12464 				   struct net_device *dev,
12465 				   u32 __always_unused filter_mask,
12466 				   int nlflags)
12467 {
12468 	struct i40e_netdev_priv *np = netdev_priv(dev);
12469 	struct i40e_vsi *vsi = np->vsi;
12470 	struct i40e_pf *pf = vsi->back;
12471 	struct i40e_veb *veb = NULL;
12472 	int i;
12473 
12474 	/* Only for PF VSI for now */
12475 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
12476 		return -EOPNOTSUPP;
12477 
12478 	/* Find the HW bridge for the PF VSI */
12479 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
12480 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
12481 			veb = pf->veb[i];
12482 	}
12483 
12484 	if (!veb)
12485 		return 0;
12486 
12487 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
12488 				       0, 0, nlflags, filter_mask, NULL);
12489 }
12490 
12491 /**
12492  * i40e_features_check - Validate encapsulated packet conforms to limits
12493  * @skb: skb buff
12494  * @dev: This physical port's netdev
12495  * @features: Offload features that the stack believes apply
12496  **/
i40e_features_check(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)12497 static netdev_features_t i40e_features_check(struct sk_buff *skb,
12498 					     struct net_device *dev,
12499 					     netdev_features_t features)
12500 {
12501 	size_t len;
12502 
12503 	/* No point in doing any of this if neither checksum nor GSO are
12504 	 * being requested for this frame.  We can rule out both by just
12505 	 * checking for CHECKSUM_PARTIAL
12506 	 */
12507 	if (skb->ip_summed != CHECKSUM_PARTIAL)
12508 		return features;
12509 
12510 	/* We cannot support GSO if the MSS is going to be less than
12511 	 * 64 bytes.  If it is then we need to drop support for GSO.
12512 	 */
12513 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
12514 		features &= ~NETIF_F_GSO_MASK;
12515 
12516 	/* MACLEN can support at most 63 words */
12517 	len = skb_network_header(skb) - skb->data;
12518 	if (len & ~(63 * 2))
12519 		goto out_err;
12520 
12521 	/* IPLEN and EIPLEN can support at most 127 dwords */
12522 	len = skb_transport_header(skb) - skb_network_header(skb);
12523 	if (len & ~(127 * 4))
12524 		goto out_err;
12525 
12526 	if (skb->encapsulation) {
12527 		/* L4TUNLEN can support 127 words */
12528 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
12529 		if (len & ~(127 * 2))
12530 			goto out_err;
12531 
12532 		/* IPLEN can support at most 127 dwords */
12533 		len = skb_inner_transport_header(skb) -
12534 		      skb_inner_network_header(skb);
12535 		if (len & ~(127 * 4))
12536 			goto out_err;
12537 	}
12538 
12539 	/* No need to validate L4LEN as TCP is the only protocol with a
12540 	 * a flexible value and we support all possible values supported
12541 	 * by TCP, which is at most 15 dwords
12542 	 */
12543 
12544 	return features;
12545 out_err:
12546 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
12547 }
12548 
12549 /**
12550  * i40e_xdp_setup - add/remove an XDP program
12551  * @vsi: VSI to changed
12552  * @prog: XDP program
12553  **/
i40e_xdp_setup(struct i40e_vsi * vsi,struct bpf_prog * prog)12554 static int i40e_xdp_setup(struct i40e_vsi *vsi,
12555 			  struct bpf_prog *prog)
12556 {
12557 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
12558 	struct i40e_pf *pf = vsi->back;
12559 	struct bpf_prog *old_prog;
12560 	bool need_reset;
12561 	int i;
12562 
12563 	/* Don't allow frames that span over multiple buffers */
12564 	if (frame_size > vsi->rx_buf_len)
12565 		return -EINVAL;
12566 
12567 	if (!i40e_enabled_xdp_vsi(vsi) && !prog)
12568 		return 0;
12569 
12570 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
12571 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
12572 
12573 	if (need_reset)
12574 		i40e_prep_for_reset(pf, true);
12575 
12576 	old_prog = xchg(&vsi->xdp_prog, prog);
12577 
12578 	if (need_reset) {
12579 		if (!prog)
12580 			/* Wait until ndo_xsk_wakeup completes. */
12581 			synchronize_rcu();
12582 		i40e_reset_and_rebuild(pf, true, true);
12583 	}
12584 
12585 	for (i = 0; i < vsi->num_queue_pairs; i++)
12586 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
12587 
12588 	if (old_prog)
12589 		bpf_prog_put(old_prog);
12590 
12591 	/* Kick start the NAPI context if there is an AF_XDP socket open
12592 	 * on that queue id. This so that receiving will start.
12593 	 */
12594 	if (need_reset && prog)
12595 		for (i = 0; i < vsi->num_queue_pairs; i++)
12596 			if (vsi->xdp_rings[i]->xsk_pool)
12597 				(void)i40e_xsk_wakeup(vsi->netdev, i,
12598 						      XDP_WAKEUP_RX);
12599 
12600 	return 0;
12601 }
12602 
12603 /**
12604  * i40e_enter_busy_conf - Enters busy config state
12605  * @vsi: vsi
12606  *
12607  * Returns 0 on success, <0 for failure.
12608  **/
i40e_enter_busy_conf(struct i40e_vsi * vsi)12609 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
12610 {
12611 	struct i40e_pf *pf = vsi->back;
12612 	int timeout = 50;
12613 
12614 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
12615 		timeout--;
12616 		if (!timeout)
12617 			return -EBUSY;
12618 		usleep_range(1000, 2000);
12619 	}
12620 
12621 	return 0;
12622 }
12623 
12624 /**
12625  * i40e_exit_busy_conf - Exits busy config state
12626  * @vsi: vsi
12627  **/
i40e_exit_busy_conf(struct i40e_vsi * vsi)12628 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
12629 {
12630 	struct i40e_pf *pf = vsi->back;
12631 
12632 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
12633 }
12634 
12635 /**
12636  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
12637  * @vsi: vsi
12638  * @queue_pair: queue pair
12639  **/
i40e_queue_pair_reset_stats(struct i40e_vsi * vsi,int queue_pair)12640 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
12641 {
12642 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
12643 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
12644 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
12645 	       sizeof(vsi->tx_rings[queue_pair]->stats));
12646 	if (i40e_enabled_xdp_vsi(vsi)) {
12647 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
12648 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
12649 	}
12650 }
12651 
12652 /**
12653  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
12654  * @vsi: vsi
12655  * @queue_pair: queue pair
12656  **/
i40e_queue_pair_clean_rings(struct i40e_vsi * vsi,int queue_pair)12657 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
12658 {
12659 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
12660 	if (i40e_enabled_xdp_vsi(vsi)) {
12661 		/* Make sure that in-progress ndo_xdp_xmit calls are
12662 		 * completed.
12663 		 */
12664 		synchronize_rcu();
12665 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
12666 	}
12667 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
12668 }
12669 
12670 /**
12671  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
12672  * @vsi: vsi
12673  * @queue_pair: queue pair
12674  * @enable: true for enable, false for disable
12675  **/
i40e_queue_pair_toggle_napi(struct i40e_vsi * vsi,int queue_pair,bool enable)12676 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
12677 					bool enable)
12678 {
12679 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12680 	struct i40e_q_vector *q_vector = rxr->q_vector;
12681 
12682 	if (!vsi->netdev)
12683 		return;
12684 
12685 	/* All rings in a qp belong to the same qvector. */
12686 	if (q_vector->rx.ring || q_vector->tx.ring) {
12687 		if (enable)
12688 			napi_enable(&q_vector->napi);
12689 		else
12690 			napi_disable(&q_vector->napi);
12691 	}
12692 }
12693 
12694 /**
12695  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
12696  * @vsi: vsi
12697  * @queue_pair: queue pair
12698  * @enable: true for enable, false for disable
12699  *
12700  * Returns 0 on success, <0 on failure.
12701  **/
i40e_queue_pair_toggle_rings(struct i40e_vsi * vsi,int queue_pair,bool enable)12702 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
12703 					bool enable)
12704 {
12705 	struct i40e_pf *pf = vsi->back;
12706 	int pf_q, ret = 0;
12707 
12708 	pf_q = vsi->base_queue + queue_pair;
12709 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
12710 				     false /*is xdp*/, enable);
12711 	if (ret) {
12712 		dev_info(&pf->pdev->dev,
12713 			 "VSI seid %d Tx ring %d %sable timeout\n",
12714 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12715 		return ret;
12716 	}
12717 
12718 	i40e_control_rx_q(pf, pf_q, enable);
12719 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
12720 	if (ret) {
12721 		dev_info(&pf->pdev->dev,
12722 			 "VSI seid %d Rx ring %d %sable timeout\n",
12723 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12724 		return ret;
12725 	}
12726 
12727 	/* Due to HW errata, on Rx disable only, the register can
12728 	 * indicate done before it really is. Needs 50ms to be sure
12729 	 */
12730 	if (!enable)
12731 		mdelay(50);
12732 
12733 	if (!i40e_enabled_xdp_vsi(vsi))
12734 		return ret;
12735 
12736 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
12737 				     pf_q + vsi->alloc_queue_pairs,
12738 				     true /*is xdp*/, enable);
12739 	if (ret) {
12740 		dev_info(&pf->pdev->dev,
12741 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
12742 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
12743 	}
12744 
12745 	return ret;
12746 }
12747 
12748 /**
12749  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
12750  * @vsi: vsi
12751  * @queue_pair: queue_pair
12752  **/
i40e_queue_pair_enable_irq(struct i40e_vsi * vsi,int queue_pair)12753 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
12754 {
12755 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12756 	struct i40e_pf *pf = vsi->back;
12757 	struct i40e_hw *hw = &pf->hw;
12758 
12759 	/* All rings in a qp belong to the same qvector. */
12760 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
12761 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
12762 	else
12763 		i40e_irq_dynamic_enable_icr0(pf);
12764 
12765 	i40e_flush(hw);
12766 }
12767 
12768 /**
12769  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
12770  * @vsi: vsi
12771  * @queue_pair: queue_pair
12772  **/
i40e_queue_pair_disable_irq(struct i40e_vsi * vsi,int queue_pair)12773 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
12774 {
12775 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
12776 	struct i40e_pf *pf = vsi->back;
12777 	struct i40e_hw *hw = &pf->hw;
12778 
12779 	/* For simplicity, instead of removing the qp interrupt causes
12780 	 * from the interrupt linked list, we simply disable the interrupt, and
12781 	 * leave the list intact.
12782 	 *
12783 	 * All rings in a qp belong to the same qvector.
12784 	 */
12785 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12786 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
12787 
12788 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
12789 		i40e_flush(hw);
12790 		synchronize_irq(pf->msix_entries[intpf].vector);
12791 	} else {
12792 		/* Legacy and MSI mode - this stops all interrupt handling */
12793 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
12794 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
12795 		i40e_flush(hw);
12796 		synchronize_irq(pf->pdev->irq);
12797 	}
12798 }
12799 
12800 /**
12801  * i40e_queue_pair_disable - Disables a queue pair
12802  * @vsi: vsi
12803  * @queue_pair: queue pair
12804  *
12805  * Returns 0 on success, <0 on failure.
12806  **/
i40e_queue_pair_disable(struct i40e_vsi * vsi,int queue_pair)12807 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
12808 {
12809 	int err;
12810 
12811 	err = i40e_enter_busy_conf(vsi);
12812 	if (err)
12813 		return err;
12814 
12815 	i40e_queue_pair_disable_irq(vsi, queue_pair);
12816 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
12817 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
12818 	i40e_queue_pair_clean_rings(vsi, queue_pair);
12819 	i40e_queue_pair_reset_stats(vsi, queue_pair);
12820 
12821 	return err;
12822 }
12823 
12824 /**
12825  * i40e_queue_pair_enable - Enables a queue pair
12826  * @vsi: vsi
12827  * @queue_pair: queue pair
12828  *
12829  * Returns 0 on success, <0 on failure.
12830  **/
i40e_queue_pair_enable(struct i40e_vsi * vsi,int queue_pair)12831 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
12832 {
12833 	int err;
12834 
12835 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
12836 	if (err)
12837 		return err;
12838 
12839 	if (i40e_enabled_xdp_vsi(vsi)) {
12840 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
12841 		if (err)
12842 			return err;
12843 	}
12844 
12845 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
12846 	if (err)
12847 		return err;
12848 
12849 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
12850 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
12851 	i40e_queue_pair_enable_irq(vsi, queue_pair);
12852 
12853 	i40e_exit_busy_conf(vsi);
12854 
12855 	return err;
12856 }
12857 
12858 /**
12859  * i40e_xdp - implements ndo_bpf for i40e
12860  * @dev: netdevice
12861  * @xdp: XDP command
12862  **/
i40e_xdp(struct net_device * dev,struct netdev_bpf * xdp)12863 static int i40e_xdp(struct net_device *dev,
12864 		    struct netdev_bpf *xdp)
12865 {
12866 	struct i40e_netdev_priv *np = netdev_priv(dev);
12867 	struct i40e_vsi *vsi = np->vsi;
12868 
12869 	if (vsi->type != I40E_VSI_MAIN)
12870 		return -EINVAL;
12871 
12872 	switch (xdp->command) {
12873 	case XDP_SETUP_PROG:
12874 		return i40e_xdp_setup(vsi, xdp->prog);
12875 	case XDP_SETUP_XSK_POOL:
12876 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
12877 					   xdp->xsk.queue_id);
12878 	default:
12879 		return -EINVAL;
12880 	}
12881 }
12882 
12883 static const struct net_device_ops i40e_netdev_ops = {
12884 	.ndo_open		= i40e_open,
12885 	.ndo_stop		= i40e_close,
12886 	.ndo_start_xmit		= i40e_lan_xmit_frame,
12887 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
12888 	.ndo_set_rx_mode	= i40e_set_rx_mode,
12889 	.ndo_validate_addr	= eth_validate_addr,
12890 	.ndo_set_mac_address	= i40e_set_mac,
12891 	.ndo_change_mtu		= i40e_change_mtu,
12892 	.ndo_do_ioctl		= i40e_ioctl,
12893 	.ndo_tx_timeout		= i40e_tx_timeout,
12894 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
12895 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
12896 #ifdef CONFIG_NET_POLL_CONTROLLER
12897 	.ndo_poll_controller	= i40e_netpoll,
12898 #endif
12899 	.ndo_setup_tc		= __i40e_setup_tc,
12900 	.ndo_select_queue	= i40e_lan_select_queue,
12901 	.ndo_set_features	= i40e_set_features,
12902 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
12903 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
12904 	.ndo_get_vf_stats	= i40e_get_vf_stats,
12905 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
12906 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
12907 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
12908 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
12909 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
12910 	.ndo_udp_tunnel_add	= udp_tunnel_nic_add_port,
12911 	.ndo_udp_tunnel_del	= udp_tunnel_nic_del_port,
12912 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
12913 	.ndo_fdb_add		= i40e_ndo_fdb_add,
12914 	.ndo_features_check	= i40e_features_check,
12915 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
12916 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
12917 	.ndo_bpf		= i40e_xdp,
12918 	.ndo_xdp_xmit		= i40e_xdp_xmit,
12919 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
12920 	.ndo_dfwd_add_station	= i40e_fwd_add,
12921 	.ndo_dfwd_del_station	= i40e_fwd_del,
12922 };
12923 
12924 /**
12925  * i40e_config_netdev - Setup the netdev flags
12926  * @vsi: the VSI being configured
12927  *
12928  * Returns 0 on success, negative value on failure
12929  **/
i40e_config_netdev(struct i40e_vsi * vsi)12930 static int i40e_config_netdev(struct i40e_vsi *vsi)
12931 {
12932 	struct i40e_pf *pf = vsi->back;
12933 	struct i40e_hw *hw = &pf->hw;
12934 	struct i40e_netdev_priv *np;
12935 	struct net_device *netdev;
12936 	u8 broadcast[ETH_ALEN];
12937 	u8 mac_addr[ETH_ALEN];
12938 	int etherdev_size;
12939 	netdev_features_t hw_enc_features;
12940 	netdev_features_t hw_features;
12941 
12942 	etherdev_size = sizeof(struct i40e_netdev_priv);
12943 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
12944 	if (!netdev)
12945 		return -ENOMEM;
12946 
12947 	vsi->netdev = netdev;
12948 	np = netdev_priv(netdev);
12949 	np->vsi = vsi;
12950 
12951 	hw_enc_features = NETIF_F_SG			|
12952 			  NETIF_F_IP_CSUM		|
12953 			  NETIF_F_IPV6_CSUM		|
12954 			  NETIF_F_HIGHDMA		|
12955 			  NETIF_F_SOFT_FEATURES		|
12956 			  NETIF_F_TSO			|
12957 			  NETIF_F_TSO_ECN		|
12958 			  NETIF_F_TSO6			|
12959 			  NETIF_F_GSO_GRE		|
12960 			  NETIF_F_GSO_GRE_CSUM		|
12961 			  NETIF_F_GSO_PARTIAL		|
12962 			  NETIF_F_GSO_IPXIP4		|
12963 			  NETIF_F_GSO_IPXIP6		|
12964 			  NETIF_F_GSO_UDP_TUNNEL	|
12965 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
12966 			  NETIF_F_GSO_UDP_L4		|
12967 			  NETIF_F_SCTP_CRC		|
12968 			  NETIF_F_RXHASH		|
12969 			  NETIF_F_RXCSUM		|
12970 			  0;
12971 
12972 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
12973 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
12974 
12975 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
12976 
12977 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
12978 
12979 	netdev->hw_enc_features |= hw_enc_features;
12980 
12981 	/* record features VLANs can make use of */
12982 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
12983 
12984 	/* enable macvlan offloads */
12985 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
12986 
12987 	hw_features = hw_enc_features		|
12988 		      NETIF_F_HW_VLAN_CTAG_TX	|
12989 		      NETIF_F_HW_VLAN_CTAG_RX;
12990 
12991 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
12992 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
12993 
12994 	netdev->hw_features |= hw_features;
12995 
12996 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
12997 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
12998 
12999 	if (vsi->type == I40E_VSI_MAIN) {
13000 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13001 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13002 		/* The following steps are necessary for two reasons. First,
13003 		 * some older NVM configurations load a default MAC-VLAN
13004 		 * filter that will accept any tagged packet, and we want to
13005 		 * replace this with a normal filter. Additionally, it is
13006 		 * possible our MAC address was provided by the platform using
13007 		 * Open Firmware or similar.
13008 		 *
13009 		 * Thus, we need to remove the default filter and install one
13010 		 * specific to the MAC address.
13011 		 */
13012 		i40e_rm_default_mac_filter(vsi, mac_addr);
13013 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13014 		i40e_add_mac_filter(vsi, mac_addr);
13015 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13016 	} else {
13017 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13018 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13019 		 * the end, which is 4 bytes long, so force truncation of the
13020 		 * original name by IFNAMSIZ - 4
13021 		 */
13022 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13023 			 IFNAMSIZ - 4,
13024 			 pf->vsi[pf->lan_vsi]->netdev->name);
13025 		eth_random_addr(mac_addr);
13026 
13027 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13028 		i40e_add_mac_filter(vsi, mac_addr);
13029 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13030 	}
13031 
13032 	/* Add the broadcast filter so that we initially will receive
13033 	 * broadcast packets. Note that when a new VLAN is first added the
13034 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13035 	 * specific filters as part of transitioning into "vlan" operation.
13036 	 * When more VLANs are added, the driver will copy each existing MAC
13037 	 * filter and add it for the new VLAN.
13038 	 *
13039 	 * Broadcast filters are handled specially by
13040 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13041 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13042 	 * filter. The subtask will update the correct broadcast promiscuous
13043 	 * bits as VLANs become active or inactive.
13044 	 */
13045 	eth_broadcast_addr(broadcast);
13046 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13047 	i40e_add_mac_filter(vsi, broadcast);
13048 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13049 
13050 	ether_addr_copy(netdev->dev_addr, mac_addr);
13051 	ether_addr_copy(netdev->perm_addr, mac_addr);
13052 
13053 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13054 	netdev->neigh_priv_len = sizeof(u32) * 4;
13055 
13056 	netdev->priv_flags |= IFF_UNICAST_FLT;
13057 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13058 	/* Setup netdev TC information */
13059 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13060 
13061 	netdev->netdev_ops = &i40e_netdev_ops;
13062 	netdev->watchdog_timeo = 5 * HZ;
13063 	i40e_set_ethtool_ops(netdev);
13064 
13065 	/* MTU range: 68 - 9706 */
13066 	netdev->min_mtu = ETH_MIN_MTU;
13067 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13068 
13069 	return 0;
13070 }
13071 
13072 /**
13073  * i40e_vsi_delete - Delete a VSI from the switch
13074  * @vsi: the VSI being removed
13075  *
13076  * Returns 0 on success, negative value on failure
13077  **/
i40e_vsi_delete(struct i40e_vsi * vsi)13078 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13079 {
13080 	/* remove default VSI is not allowed */
13081 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13082 		return;
13083 
13084 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13085 }
13086 
13087 /**
13088  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13089  * @vsi: the VSI being queried
13090  *
13091  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13092  **/
i40e_is_vsi_uplink_mode_veb(struct i40e_vsi * vsi)13093 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13094 {
13095 	struct i40e_veb *veb;
13096 	struct i40e_pf *pf = vsi->back;
13097 
13098 	/* Uplink is not a bridge so default to VEB */
13099 	if (vsi->veb_idx >= I40E_MAX_VEB)
13100 		return 1;
13101 
13102 	veb = pf->veb[vsi->veb_idx];
13103 	if (!veb) {
13104 		dev_info(&pf->pdev->dev,
13105 			 "There is no veb associated with the bridge\n");
13106 		return -ENOENT;
13107 	}
13108 
13109 	/* Uplink is a bridge in VEPA mode */
13110 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13111 		return 0;
13112 	} else {
13113 		/* Uplink is a bridge in VEB mode */
13114 		return 1;
13115 	}
13116 
13117 	/* VEPA is now default bridge, so return 0 */
13118 	return 0;
13119 }
13120 
13121 /**
13122  * i40e_add_vsi - Add a VSI to the switch
13123  * @vsi: the VSI being configured
13124  *
13125  * This initializes a VSI context depending on the VSI type to be added and
13126  * passes it down to the add_vsi aq command.
13127  **/
i40e_add_vsi(struct i40e_vsi * vsi)13128 static int i40e_add_vsi(struct i40e_vsi *vsi)
13129 {
13130 	int ret = -ENODEV;
13131 	struct i40e_pf *pf = vsi->back;
13132 	struct i40e_hw *hw = &pf->hw;
13133 	struct i40e_vsi_context ctxt;
13134 	struct i40e_mac_filter *f;
13135 	struct hlist_node *h;
13136 	int bkt;
13137 
13138 	u8 enabled_tc = 0x1; /* TC0 enabled */
13139 	int f_count = 0;
13140 
13141 	memset(&ctxt, 0, sizeof(ctxt));
13142 	switch (vsi->type) {
13143 	case I40E_VSI_MAIN:
13144 		/* The PF's main VSI is already setup as part of the
13145 		 * device initialization, so we'll not bother with
13146 		 * the add_vsi call, but we will retrieve the current
13147 		 * VSI context.
13148 		 */
13149 		ctxt.seid = pf->main_vsi_seid;
13150 		ctxt.pf_num = pf->hw.pf_id;
13151 		ctxt.vf_num = 0;
13152 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13153 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13154 		if (ret) {
13155 			dev_info(&pf->pdev->dev,
13156 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13157 				 i40e_stat_str(&pf->hw, ret),
13158 				 i40e_aq_str(&pf->hw,
13159 					     pf->hw.aq.asq_last_status));
13160 			return -ENOENT;
13161 		}
13162 		vsi->info = ctxt.info;
13163 		vsi->info.valid_sections = 0;
13164 
13165 		vsi->seid = ctxt.seid;
13166 		vsi->id = ctxt.vsi_number;
13167 
13168 		enabled_tc = i40e_pf_get_tc_map(pf);
13169 
13170 		/* Source pruning is enabled by default, so the flag is
13171 		 * negative logic - if it's set, we need to fiddle with
13172 		 * the VSI to disable source pruning.
13173 		 */
13174 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13175 			memset(&ctxt, 0, sizeof(ctxt));
13176 			ctxt.seid = pf->main_vsi_seid;
13177 			ctxt.pf_num = pf->hw.pf_id;
13178 			ctxt.vf_num = 0;
13179 			ctxt.info.valid_sections |=
13180 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13181 			ctxt.info.switch_id =
13182 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13183 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13184 			if (ret) {
13185 				dev_info(&pf->pdev->dev,
13186 					 "update vsi failed, err %s aq_err %s\n",
13187 					 i40e_stat_str(&pf->hw, ret),
13188 					 i40e_aq_str(&pf->hw,
13189 						     pf->hw.aq.asq_last_status));
13190 				ret = -ENOENT;
13191 				goto err;
13192 			}
13193 		}
13194 
13195 		/* MFP mode setup queue map and update VSI */
13196 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13197 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13198 			memset(&ctxt, 0, sizeof(ctxt));
13199 			ctxt.seid = pf->main_vsi_seid;
13200 			ctxt.pf_num = pf->hw.pf_id;
13201 			ctxt.vf_num = 0;
13202 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13203 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13204 			if (ret) {
13205 				dev_info(&pf->pdev->dev,
13206 					 "update vsi failed, err %s aq_err %s\n",
13207 					 i40e_stat_str(&pf->hw, ret),
13208 					 i40e_aq_str(&pf->hw,
13209 						    pf->hw.aq.asq_last_status));
13210 				ret = -ENOENT;
13211 				goto err;
13212 			}
13213 			/* update the local VSI info queue map */
13214 			i40e_vsi_update_queue_map(vsi, &ctxt);
13215 			vsi->info.valid_sections = 0;
13216 		} else {
13217 			/* Default/Main VSI is only enabled for TC0
13218 			 * reconfigure it to enable all TCs that are
13219 			 * available on the port in SFP mode.
13220 			 * For MFP case the iSCSI PF would use this
13221 			 * flow to enable LAN+iSCSI TC.
13222 			 */
13223 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13224 			if (ret) {
13225 				/* Single TC condition is not fatal,
13226 				 * message and continue
13227 				 */
13228 				dev_info(&pf->pdev->dev,
13229 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13230 					 enabled_tc,
13231 					 i40e_stat_str(&pf->hw, ret),
13232 					 i40e_aq_str(&pf->hw,
13233 						    pf->hw.aq.asq_last_status));
13234 			}
13235 		}
13236 		break;
13237 
13238 	case I40E_VSI_FDIR:
13239 		ctxt.pf_num = hw->pf_id;
13240 		ctxt.vf_num = 0;
13241 		ctxt.uplink_seid = vsi->uplink_seid;
13242 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13243 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13244 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13245 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13246 			ctxt.info.valid_sections |=
13247 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13248 			ctxt.info.switch_id =
13249 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13250 		}
13251 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13252 		break;
13253 
13254 	case I40E_VSI_VMDQ2:
13255 		ctxt.pf_num = hw->pf_id;
13256 		ctxt.vf_num = 0;
13257 		ctxt.uplink_seid = vsi->uplink_seid;
13258 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13259 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13260 
13261 		/* This VSI is connected to VEB so the switch_id
13262 		 * should be set to zero by default.
13263 		 */
13264 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13265 			ctxt.info.valid_sections |=
13266 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13267 			ctxt.info.switch_id =
13268 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13269 		}
13270 
13271 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13272 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13273 		break;
13274 
13275 	case I40E_VSI_SRIOV:
13276 		ctxt.pf_num = hw->pf_id;
13277 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
13278 		ctxt.uplink_seid = vsi->uplink_seid;
13279 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13280 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
13281 
13282 		/* This VSI is connected to VEB so the switch_id
13283 		 * should be set to zero by default.
13284 		 */
13285 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13286 			ctxt.info.valid_sections |=
13287 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13288 			ctxt.info.switch_id =
13289 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13290 		}
13291 
13292 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
13293 			ctxt.info.valid_sections |=
13294 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
13295 			ctxt.info.queueing_opt_flags |=
13296 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
13297 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
13298 		}
13299 
13300 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
13301 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
13302 		if (pf->vf[vsi->vf_id].spoofchk) {
13303 			ctxt.info.valid_sections |=
13304 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
13305 			ctxt.info.sec_flags |=
13306 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
13307 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
13308 		}
13309 		/* Setup the VSI tx/rx queue map for TC0 only for now */
13310 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13311 		break;
13312 
13313 	case I40E_VSI_IWARP:
13314 		/* send down message to iWARP */
13315 		break;
13316 
13317 	default:
13318 		return -ENODEV;
13319 	}
13320 
13321 	if (vsi->type != I40E_VSI_MAIN) {
13322 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
13323 		if (ret) {
13324 			dev_info(&vsi->back->pdev->dev,
13325 				 "add vsi failed, err %s aq_err %s\n",
13326 				 i40e_stat_str(&pf->hw, ret),
13327 				 i40e_aq_str(&pf->hw,
13328 					     pf->hw.aq.asq_last_status));
13329 			ret = -ENOENT;
13330 			goto err;
13331 		}
13332 		vsi->info = ctxt.info;
13333 		vsi->info.valid_sections = 0;
13334 		vsi->seid = ctxt.seid;
13335 		vsi->id = ctxt.vsi_number;
13336 	}
13337 
13338 	vsi->active_filters = 0;
13339 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
13340 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13341 	/* If macvlan filters already exist, force them to get loaded */
13342 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
13343 		f->state = I40E_FILTER_NEW;
13344 		f_count++;
13345 	}
13346 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13347 
13348 	if (f_count) {
13349 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
13350 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
13351 	}
13352 
13353 	/* Update VSI BW information */
13354 	ret = i40e_vsi_get_bw_info(vsi);
13355 	if (ret) {
13356 		dev_info(&pf->pdev->dev,
13357 			 "couldn't get vsi bw info, err %s aq_err %s\n",
13358 			 i40e_stat_str(&pf->hw, ret),
13359 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
13360 		/* VSI is already added so not tearing that up */
13361 		ret = 0;
13362 	}
13363 
13364 err:
13365 	return ret;
13366 }
13367 
13368 /**
13369  * i40e_vsi_release - Delete a VSI and free its resources
13370  * @vsi: the VSI being removed
13371  *
13372  * Returns 0 on success or < 0 on error
13373  **/
i40e_vsi_release(struct i40e_vsi * vsi)13374 int i40e_vsi_release(struct i40e_vsi *vsi)
13375 {
13376 	struct i40e_mac_filter *f;
13377 	struct hlist_node *h;
13378 	struct i40e_veb *veb = NULL;
13379 	struct i40e_pf *pf;
13380 	u16 uplink_seid;
13381 	int i, n, bkt;
13382 
13383 	pf = vsi->back;
13384 
13385 	/* release of a VEB-owner or last VSI is not allowed */
13386 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
13387 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
13388 			 vsi->seid, vsi->uplink_seid);
13389 		return -ENODEV;
13390 	}
13391 	if (vsi == pf->vsi[pf->lan_vsi] &&
13392 	    !test_bit(__I40E_DOWN, pf->state)) {
13393 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
13394 		return -ENODEV;
13395 	}
13396 	set_bit(__I40E_VSI_RELEASING, vsi->state);
13397 	uplink_seid = vsi->uplink_seid;
13398 	if (vsi->type != I40E_VSI_SRIOV) {
13399 		if (vsi->netdev_registered) {
13400 			vsi->netdev_registered = false;
13401 			if (vsi->netdev) {
13402 				/* results in a call to i40e_close() */
13403 				unregister_netdev(vsi->netdev);
13404 			}
13405 		} else {
13406 			i40e_vsi_close(vsi);
13407 		}
13408 		i40e_vsi_disable_irq(vsi);
13409 	}
13410 
13411 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13412 
13413 	/* clear the sync flag on all filters */
13414 	if (vsi->netdev) {
13415 		__dev_uc_unsync(vsi->netdev, NULL);
13416 		__dev_mc_unsync(vsi->netdev, NULL);
13417 	}
13418 
13419 	/* make sure any remaining filters are marked for deletion */
13420 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
13421 		__i40e_del_filter(vsi, f);
13422 
13423 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13424 
13425 	i40e_sync_vsi_filters(vsi);
13426 
13427 	i40e_vsi_delete(vsi);
13428 	i40e_vsi_free_q_vectors(vsi);
13429 	if (vsi->netdev) {
13430 		free_netdev(vsi->netdev);
13431 		vsi->netdev = NULL;
13432 	}
13433 	i40e_vsi_clear_rings(vsi);
13434 	i40e_vsi_clear(vsi);
13435 
13436 	/* If this was the last thing on the VEB, except for the
13437 	 * controlling VSI, remove the VEB, which puts the controlling
13438 	 * VSI onto the next level down in the switch.
13439 	 *
13440 	 * Well, okay, there's one more exception here: don't remove
13441 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
13442 	 * from up the network stack.
13443 	 */
13444 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
13445 		if (pf->vsi[i] &&
13446 		    pf->vsi[i]->uplink_seid == uplink_seid &&
13447 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13448 			n++;      /* count the VSIs */
13449 		}
13450 	}
13451 	for (i = 0; i < I40E_MAX_VEB; i++) {
13452 		if (!pf->veb[i])
13453 			continue;
13454 		if (pf->veb[i]->uplink_seid == uplink_seid)
13455 			n++;     /* count the VEBs */
13456 		if (pf->veb[i]->seid == uplink_seid)
13457 			veb = pf->veb[i];
13458 	}
13459 	if (n == 0 && veb && veb->uplink_seid != 0)
13460 		i40e_veb_release(veb);
13461 
13462 	return 0;
13463 }
13464 
13465 /**
13466  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
13467  * @vsi: ptr to the VSI
13468  *
13469  * This should only be called after i40e_vsi_mem_alloc() which allocates the
13470  * corresponding SW VSI structure and initializes num_queue_pairs for the
13471  * newly allocated VSI.
13472  *
13473  * Returns 0 on success or negative on failure
13474  **/
i40e_vsi_setup_vectors(struct i40e_vsi * vsi)13475 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
13476 {
13477 	int ret = -ENOENT;
13478 	struct i40e_pf *pf = vsi->back;
13479 
13480 	if (vsi->q_vectors[0]) {
13481 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
13482 			 vsi->seid);
13483 		return -EEXIST;
13484 	}
13485 
13486 	if (vsi->base_vector) {
13487 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
13488 			 vsi->seid, vsi->base_vector);
13489 		return -EEXIST;
13490 	}
13491 
13492 	ret = i40e_vsi_alloc_q_vectors(vsi);
13493 	if (ret) {
13494 		dev_info(&pf->pdev->dev,
13495 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
13496 			 vsi->num_q_vectors, vsi->seid, ret);
13497 		vsi->num_q_vectors = 0;
13498 		goto vector_setup_out;
13499 	}
13500 
13501 	/* In Legacy mode, we do not have to get any other vector since we
13502 	 * piggyback on the misc/ICR0 for queue interrupts.
13503 	*/
13504 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
13505 		return ret;
13506 	if (vsi->num_q_vectors)
13507 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
13508 						 vsi->num_q_vectors, vsi->idx);
13509 	if (vsi->base_vector < 0) {
13510 		dev_info(&pf->pdev->dev,
13511 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
13512 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
13513 		i40e_vsi_free_q_vectors(vsi);
13514 		ret = -ENOENT;
13515 		goto vector_setup_out;
13516 	}
13517 
13518 vector_setup_out:
13519 	return ret;
13520 }
13521 
13522 /**
13523  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
13524  * @vsi: pointer to the vsi.
13525  *
13526  * This re-allocates a vsi's queue resources.
13527  *
13528  * Returns pointer to the successfully allocated and configured VSI sw struct
13529  * on success, otherwise returns NULL on failure.
13530  **/
i40e_vsi_reinit_setup(struct i40e_vsi * vsi)13531 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
13532 {
13533 	u16 alloc_queue_pairs;
13534 	struct i40e_pf *pf;
13535 	u8 enabled_tc;
13536 	int ret;
13537 
13538 	if (!vsi)
13539 		return NULL;
13540 
13541 	pf = vsi->back;
13542 
13543 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
13544 	i40e_vsi_clear_rings(vsi);
13545 
13546 	i40e_vsi_free_arrays(vsi, false);
13547 	i40e_set_num_rings_in_vsi(vsi);
13548 	ret = i40e_vsi_alloc_arrays(vsi, false);
13549 	if (ret)
13550 		goto err_vsi;
13551 
13552 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13553 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13554 
13555 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13556 	if (ret < 0) {
13557 		dev_info(&pf->pdev->dev,
13558 			 "failed to get tracking for %d queues for VSI %d err %d\n",
13559 			 alloc_queue_pairs, vsi->seid, ret);
13560 		goto err_vsi;
13561 	}
13562 	vsi->base_queue = ret;
13563 
13564 	/* Update the FW view of the VSI. Force a reset of TC and queue
13565 	 * layout configurations.
13566 	 */
13567 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
13568 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
13569 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
13570 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
13571 	if (vsi->type == I40E_VSI_MAIN)
13572 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
13573 
13574 	/* assign it some queues */
13575 	ret = i40e_alloc_rings(vsi);
13576 	if (ret)
13577 		goto err_rings;
13578 
13579 	/* map all of the rings to the q_vectors */
13580 	i40e_vsi_map_rings_to_vectors(vsi);
13581 	return vsi;
13582 
13583 err_rings:
13584 	i40e_vsi_free_q_vectors(vsi);
13585 	if (vsi->netdev_registered) {
13586 		vsi->netdev_registered = false;
13587 		unregister_netdev(vsi->netdev);
13588 		free_netdev(vsi->netdev);
13589 		vsi->netdev = NULL;
13590 	}
13591 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13592 err_vsi:
13593 	i40e_vsi_clear(vsi);
13594 	return NULL;
13595 }
13596 
13597 /**
13598  * i40e_vsi_setup - Set up a VSI by a given type
13599  * @pf: board private structure
13600  * @type: VSI type
13601  * @uplink_seid: the switch element to link to
13602  * @param1: usage depends upon VSI type. For VF types, indicates VF id
13603  *
13604  * This allocates the sw VSI structure and its queue resources, then add a VSI
13605  * to the identified VEB.
13606  *
13607  * Returns pointer to the successfully allocated and configure VSI sw struct on
13608  * success, otherwise returns NULL on failure.
13609  **/
i40e_vsi_setup(struct i40e_pf * pf,u8 type,u16 uplink_seid,u32 param1)13610 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
13611 				u16 uplink_seid, u32 param1)
13612 {
13613 	struct i40e_vsi *vsi = NULL;
13614 	struct i40e_veb *veb = NULL;
13615 	u16 alloc_queue_pairs;
13616 	int ret, i;
13617 	int v_idx;
13618 
13619 	/* The requested uplink_seid must be either
13620 	 *     - the PF's port seid
13621 	 *              no VEB is needed because this is the PF
13622 	 *              or this is a Flow Director special case VSI
13623 	 *     - seid of an existing VEB
13624 	 *     - seid of a VSI that owns an existing VEB
13625 	 *     - seid of a VSI that doesn't own a VEB
13626 	 *              a new VEB is created and the VSI becomes the owner
13627 	 *     - seid of the PF VSI, which is what creates the first VEB
13628 	 *              this is a special case of the previous
13629 	 *
13630 	 * Find which uplink_seid we were given and create a new VEB if needed
13631 	 */
13632 	for (i = 0; i < I40E_MAX_VEB; i++) {
13633 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
13634 			veb = pf->veb[i];
13635 			break;
13636 		}
13637 	}
13638 
13639 	if (!veb && uplink_seid != pf->mac_seid) {
13640 
13641 		for (i = 0; i < pf->num_alloc_vsi; i++) {
13642 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
13643 				vsi = pf->vsi[i];
13644 				break;
13645 			}
13646 		}
13647 		if (!vsi) {
13648 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
13649 				 uplink_seid);
13650 			return NULL;
13651 		}
13652 
13653 		if (vsi->uplink_seid == pf->mac_seid)
13654 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
13655 					     vsi->tc_config.enabled_tc);
13656 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
13657 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13658 					     vsi->tc_config.enabled_tc);
13659 		if (veb) {
13660 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
13661 				dev_info(&vsi->back->pdev->dev,
13662 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
13663 				return NULL;
13664 			}
13665 			/* We come up by default in VEPA mode if SRIOV is not
13666 			 * already enabled, in which case we can't force VEPA
13667 			 * mode.
13668 			 */
13669 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
13670 				veb->bridge_mode = BRIDGE_MODE_VEPA;
13671 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13672 			}
13673 			i40e_config_bridge_mode(veb);
13674 		}
13675 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13676 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13677 				veb = pf->veb[i];
13678 		}
13679 		if (!veb) {
13680 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
13681 			return NULL;
13682 		}
13683 
13684 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
13685 		uplink_seid = veb->seid;
13686 	}
13687 
13688 	/* get vsi sw struct */
13689 	v_idx = i40e_vsi_mem_alloc(pf, type);
13690 	if (v_idx < 0)
13691 		goto err_alloc;
13692 	vsi = pf->vsi[v_idx];
13693 	if (!vsi)
13694 		goto err_alloc;
13695 	vsi->type = type;
13696 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
13697 
13698 	if (type == I40E_VSI_MAIN)
13699 		pf->lan_vsi = v_idx;
13700 	else if (type == I40E_VSI_SRIOV)
13701 		vsi->vf_id = param1;
13702 	/* assign it some queues */
13703 	alloc_queue_pairs = vsi->alloc_queue_pairs *
13704 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
13705 
13706 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
13707 	if (ret < 0) {
13708 		dev_info(&pf->pdev->dev,
13709 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
13710 			 alloc_queue_pairs, vsi->seid, ret);
13711 		goto err_vsi;
13712 	}
13713 	vsi->base_queue = ret;
13714 
13715 	/* get a VSI from the hardware */
13716 	vsi->uplink_seid = uplink_seid;
13717 	ret = i40e_add_vsi(vsi);
13718 	if (ret)
13719 		goto err_vsi;
13720 
13721 	switch (vsi->type) {
13722 	/* setup the netdev if needed */
13723 	case I40E_VSI_MAIN:
13724 	case I40E_VSI_VMDQ2:
13725 		ret = i40e_config_netdev(vsi);
13726 		if (ret)
13727 			goto err_netdev;
13728 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
13729 		if (ret)
13730 			goto err_netdev;
13731 		ret = register_netdev(vsi->netdev);
13732 		if (ret)
13733 			goto err_netdev;
13734 		vsi->netdev_registered = true;
13735 		netif_carrier_off(vsi->netdev);
13736 #ifdef CONFIG_I40E_DCB
13737 		/* Setup DCB netlink interface */
13738 		i40e_dcbnl_setup(vsi);
13739 #endif /* CONFIG_I40E_DCB */
13740 		fallthrough;
13741 	case I40E_VSI_FDIR:
13742 		/* set up vectors and rings if needed */
13743 		ret = i40e_vsi_setup_vectors(vsi);
13744 		if (ret)
13745 			goto err_msix;
13746 
13747 		ret = i40e_alloc_rings(vsi);
13748 		if (ret)
13749 			goto err_rings;
13750 
13751 		/* map all of the rings to the q_vectors */
13752 		i40e_vsi_map_rings_to_vectors(vsi);
13753 
13754 		i40e_vsi_reset_stats(vsi);
13755 		break;
13756 	default:
13757 		/* no netdev or rings for the other VSI types */
13758 		break;
13759 	}
13760 
13761 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
13762 	    (vsi->type == I40E_VSI_VMDQ2)) {
13763 		ret = i40e_vsi_config_rss(vsi);
13764 	}
13765 	return vsi;
13766 
13767 err_rings:
13768 	i40e_vsi_free_q_vectors(vsi);
13769 err_msix:
13770 	if (vsi->netdev_registered) {
13771 		vsi->netdev_registered = false;
13772 		unregister_netdev(vsi->netdev);
13773 		free_netdev(vsi->netdev);
13774 		vsi->netdev = NULL;
13775 	}
13776 err_netdev:
13777 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
13778 err_vsi:
13779 	i40e_vsi_clear(vsi);
13780 err_alloc:
13781 	return NULL;
13782 }
13783 
13784 /**
13785  * i40e_veb_get_bw_info - Query VEB BW information
13786  * @veb: the veb to query
13787  *
13788  * Query the Tx scheduler BW configuration data for given VEB
13789  **/
i40e_veb_get_bw_info(struct i40e_veb * veb)13790 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
13791 {
13792 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
13793 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
13794 	struct i40e_pf *pf = veb->pf;
13795 	struct i40e_hw *hw = &pf->hw;
13796 	u32 tc_bw_max;
13797 	int ret = 0;
13798 	int i;
13799 
13800 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
13801 						  &bw_data, NULL);
13802 	if (ret) {
13803 		dev_info(&pf->pdev->dev,
13804 			 "query veb bw config failed, err %s aq_err %s\n",
13805 			 i40e_stat_str(&pf->hw, ret),
13806 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13807 		goto out;
13808 	}
13809 
13810 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
13811 						   &ets_data, NULL);
13812 	if (ret) {
13813 		dev_info(&pf->pdev->dev,
13814 			 "query veb bw ets config failed, err %s aq_err %s\n",
13815 			 i40e_stat_str(&pf->hw, ret),
13816 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
13817 		goto out;
13818 	}
13819 
13820 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
13821 	veb->bw_max_quanta = ets_data.tc_bw_max;
13822 	veb->is_abs_credits = bw_data.absolute_credits_enable;
13823 	veb->enabled_tc = ets_data.tc_valid_bits;
13824 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
13825 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
13826 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
13827 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
13828 		veb->bw_tc_limit_credits[i] =
13829 					le16_to_cpu(bw_data.tc_bw_limits[i]);
13830 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
13831 	}
13832 
13833 out:
13834 	return ret;
13835 }
13836 
13837 /**
13838  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
13839  * @pf: board private structure
13840  *
13841  * On error: returns error code (negative)
13842  * On success: returns vsi index in PF (positive)
13843  **/
i40e_veb_mem_alloc(struct i40e_pf * pf)13844 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
13845 {
13846 	int ret = -ENOENT;
13847 	struct i40e_veb *veb;
13848 	int i;
13849 
13850 	/* Need to protect the allocation of switch elements at the PF level */
13851 	mutex_lock(&pf->switch_mutex);
13852 
13853 	/* VEB list may be fragmented if VEB creation/destruction has
13854 	 * been happening.  We can afford to do a quick scan to look
13855 	 * for any free slots in the list.
13856 	 *
13857 	 * find next empty veb slot, looping back around if necessary
13858 	 */
13859 	i = 0;
13860 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
13861 		i++;
13862 	if (i >= I40E_MAX_VEB) {
13863 		ret = -ENOMEM;
13864 		goto err_alloc_veb;  /* out of VEB slots! */
13865 	}
13866 
13867 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
13868 	if (!veb) {
13869 		ret = -ENOMEM;
13870 		goto err_alloc_veb;
13871 	}
13872 	veb->pf = pf;
13873 	veb->idx = i;
13874 	veb->enabled_tc = 1;
13875 
13876 	pf->veb[i] = veb;
13877 	ret = i;
13878 err_alloc_veb:
13879 	mutex_unlock(&pf->switch_mutex);
13880 	return ret;
13881 }
13882 
13883 /**
13884  * i40e_switch_branch_release - Delete a branch of the switch tree
13885  * @branch: where to start deleting
13886  *
13887  * This uses recursion to find the tips of the branch to be
13888  * removed, deleting until we get back to and can delete this VEB.
13889  **/
i40e_switch_branch_release(struct i40e_veb * branch)13890 static void i40e_switch_branch_release(struct i40e_veb *branch)
13891 {
13892 	struct i40e_pf *pf = branch->pf;
13893 	u16 branch_seid = branch->seid;
13894 	u16 veb_idx = branch->idx;
13895 	int i;
13896 
13897 	/* release any VEBs on this VEB - RECURSION */
13898 	for (i = 0; i < I40E_MAX_VEB; i++) {
13899 		if (!pf->veb[i])
13900 			continue;
13901 		if (pf->veb[i]->uplink_seid == branch->seid)
13902 			i40e_switch_branch_release(pf->veb[i]);
13903 	}
13904 
13905 	/* Release the VSIs on this VEB, but not the owner VSI.
13906 	 *
13907 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
13908 	 *       the VEB itself, so don't use (*branch) after this loop.
13909 	 */
13910 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13911 		if (!pf->vsi[i])
13912 			continue;
13913 		if (pf->vsi[i]->uplink_seid == branch_seid &&
13914 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
13915 			i40e_vsi_release(pf->vsi[i]);
13916 		}
13917 	}
13918 
13919 	/* There's one corner case where the VEB might not have been
13920 	 * removed, so double check it here and remove it if needed.
13921 	 * This case happens if the veb was created from the debugfs
13922 	 * commands and no VSIs were added to it.
13923 	 */
13924 	if (pf->veb[veb_idx])
13925 		i40e_veb_release(pf->veb[veb_idx]);
13926 }
13927 
13928 /**
13929  * i40e_veb_clear - remove veb struct
13930  * @veb: the veb to remove
13931  **/
i40e_veb_clear(struct i40e_veb * veb)13932 static void i40e_veb_clear(struct i40e_veb *veb)
13933 {
13934 	if (!veb)
13935 		return;
13936 
13937 	if (veb->pf) {
13938 		struct i40e_pf *pf = veb->pf;
13939 
13940 		mutex_lock(&pf->switch_mutex);
13941 		if (pf->veb[veb->idx] == veb)
13942 			pf->veb[veb->idx] = NULL;
13943 		mutex_unlock(&pf->switch_mutex);
13944 	}
13945 
13946 	kfree(veb);
13947 }
13948 
13949 /**
13950  * i40e_veb_release - Delete a VEB and free its resources
13951  * @veb: the VEB being removed
13952  **/
i40e_veb_release(struct i40e_veb * veb)13953 void i40e_veb_release(struct i40e_veb *veb)
13954 {
13955 	struct i40e_vsi *vsi = NULL;
13956 	struct i40e_pf *pf;
13957 	int i, n = 0;
13958 
13959 	pf = veb->pf;
13960 
13961 	/* find the remaining VSI and check for extras */
13962 	for (i = 0; i < pf->num_alloc_vsi; i++) {
13963 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
13964 			n++;
13965 			vsi = pf->vsi[i];
13966 		}
13967 	}
13968 	if (n != 1) {
13969 		dev_info(&pf->pdev->dev,
13970 			 "can't remove VEB %d with %d VSIs left\n",
13971 			 veb->seid, n);
13972 		return;
13973 	}
13974 
13975 	/* move the remaining VSI to uplink veb */
13976 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
13977 	if (veb->uplink_seid) {
13978 		vsi->uplink_seid = veb->uplink_seid;
13979 		if (veb->uplink_seid == pf->mac_seid)
13980 			vsi->veb_idx = I40E_NO_VEB;
13981 		else
13982 			vsi->veb_idx = veb->veb_idx;
13983 	} else {
13984 		/* floating VEB */
13985 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
13986 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
13987 	}
13988 
13989 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
13990 	i40e_veb_clear(veb);
13991 }
13992 
13993 /**
13994  * i40e_add_veb - create the VEB in the switch
13995  * @veb: the VEB to be instantiated
13996  * @vsi: the controlling VSI
13997  **/
i40e_add_veb(struct i40e_veb * veb,struct i40e_vsi * vsi)13998 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
13999 {
14000 	struct i40e_pf *pf = veb->pf;
14001 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14002 	int ret;
14003 
14004 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14005 			      veb->enabled_tc, false,
14006 			      &veb->seid, enable_stats, NULL);
14007 
14008 	/* get a VEB from the hardware */
14009 	if (ret) {
14010 		dev_info(&pf->pdev->dev,
14011 			 "couldn't add VEB, err %s aq_err %s\n",
14012 			 i40e_stat_str(&pf->hw, ret),
14013 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14014 		return -EPERM;
14015 	}
14016 
14017 	/* get statistics counter */
14018 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14019 					 &veb->stats_idx, NULL, NULL, NULL);
14020 	if (ret) {
14021 		dev_info(&pf->pdev->dev,
14022 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14023 			 i40e_stat_str(&pf->hw, ret),
14024 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14025 		return -EPERM;
14026 	}
14027 	ret = i40e_veb_get_bw_info(veb);
14028 	if (ret) {
14029 		dev_info(&pf->pdev->dev,
14030 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14031 			 i40e_stat_str(&pf->hw, ret),
14032 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14033 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14034 		return -ENOENT;
14035 	}
14036 
14037 	vsi->uplink_seid = veb->seid;
14038 	vsi->veb_idx = veb->idx;
14039 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14040 
14041 	return 0;
14042 }
14043 
14044 /**
14045  * i40e_veb_setup - Set up a VEB
14046  * @pf: board private structure
14047  * @flags: VEB setup flags
14048  * @uplink_seid: the switch element to link to
14049  * @vsi_seid: the initial VSI seid
14050  * @enabled_tc: Enabled TC bit-map
14051  *
14052  * This allocates the sw VEB structure and links it into the switch
14053  * It is possible and legal for this to be a duplicate of an already
14054  * existing VEB.  It is also possible for both uplink and vsi seids
14055  * to be zero, in order to create a floating VEB.
14056  *
14057  * Returns pointer to the successfully allocated VEB sw struct on
14058  * success, otherwise returns NULL on failure.
14059  **/
i40e_veb_setup(struct i40e_pf * pf,u16 flags,u16 uplink_seid,u16 vsi_seid,u8 enabled_tc)14060 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14061 				u16 uplink_seid, u16 vsi_seid,
14062 				u8 enabled_tc)
14063 {
14064 	struct i40e_veb *veb, *uplink_veb = NULL;
14065 	int vsi_idx, veb_idx;
14066 	int ret;
14067 
14068 	/* if one seid is 0, the other must be 0 to create a floating relay */
14069 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14070 	    (uplink_seid + vsi_seid != 0)) {
14071 		dev_info(&pf->pdev->dev,
14072 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14073 			 uplink_seid, vsi_seid);
14074 		return NULL;
14075 	}
14076 
14077 	/* make sure there is such a vsi and uplink */
14078 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14079 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14080 			break;
14081 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14082 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14083 			 vsi_seid);
14084 		return NULL;
14085 	}
14086 
14087 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14088 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14089 			if (pf->veb[veb_idx] &&
14090 			    pf->veb[veb_idx]->seid == uplink_seid) {
14091 				uplink_veb = pf->veb[veb_idx];
14092 				break;
14093 			}
14094 		}
14095 		if (!uplink_veb) {
14096 			dev_info(&pf->pdev->dev,
14097 				 "uplink seid %d not found\n", uplink_seid);
14098 			return NULL;
14099 		}
14100 	}
14101 
14102 	/* get veb sw struct */
14103 	veb_idx = i40e_veb_mem_alloc(pf);
14104 	if (veb_idx < 0)
14105 		goto err_alloc;
14106 	veb = pf->veb[veb_idx];
14107 	veb->flags = flags;
14108 	veb->uplink_seid = uplink_seid;
14109 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14110 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14111 
14112 	/* create the VEB in the switch */
14113 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14114 	if (ret)
14115 		goto err_veb;
14116 	if (vsi_idx == pf->lan_vsi)
14117 		pf->lan_veb = veb->idx;
14118 
14119 	return veb;
14120 
14121 err_veb:
14122 	i40e_veb_clear(veb);
14123 err_alloc:
14124 	return NULL;
14125 }
14126 
14127 /**
14128  * i40e_setup_pf_switch_element - set PF vars based on switch type
14129  * @pf: board private structure
14130  * @ele: element we are building info from
14131  * @num_reported: total number of elements
14132  * @printconfig: should we print the contents
14133  *
14134  * helper function to assist in extracting a few useful SEID values.
14135  **/
i40e_setup_pf_switch_element(struct i40e_pf * pf,struct i40e_aqc_switch_config_element_resp * ele,u16 num_reported,bool printconfig)14136 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14137 				struct i40e_aqc_switch_config_element_resp *ele,
14138 				u16 num_reported, bool printconfig)
14139 {
14140 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14141 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14142 	u8 element_type = ele->element_type;
14143 	u16 seid = le16_to_cpu(ele->seid);
14144 
14145 	if (printconfig)
14146 		dev_info(&pf->pdev->dev,
14147 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14148 			 element_type, seid, uplink_seid, downlink_seid);
14149 
14150 	switch (element_type) {
14151 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14152 		pf->mac_seid = seid;
14153 		break;
14154 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14155 		/* Main VEB? */
14156 		if (uplink_seid != pf->mac_seid)
14157 			break;
14158 		if (pf->lan_veb >= I40E_MAX_VEB) {
14159 			int v;
14160 
14161 			/* find existing or else empty VEB */
14162 			for (v = 0; v < I40E_MAX_VEB; v++) {
14163 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14164 					pf->lan_veb = v;
14165 					break;
14166 				}
14167 			}
14168 			if (pf->lan_veb >= I40E_MAX_VEB) {
14169 				v = i40e_veb_mem_alloc(pf);
14170 				if (v < 0)
14171 					break;
14172 				pf->lan_veb = v;
14173 			}
14174 		}
14175 		if (pf->lan_veb >= I40E_MAX_VEB)
14176 			break;
14177 
14178 		pf->veb[pf->lan_veb]->seid = seid;
14179 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14180 		pf->veb[pf->lan_veb]->pf = pf;
14181 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14182 		break;
14183 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14184 		if (num_reported != 1)
14185 			break;
14186 		/* This is immediately after a reset so we can assume this is
14187 		 * the PF's VSI
14188 		 */
14189 		pf->mac_seid = uplink_seid;
14190 		pf->pf_seid = downlink_seid;
14191 		pf->main_vsi_seid = seid;
14192 		if (printconfig)
14193 			dev_info(&pf->pdev->dev,
14194 				 "pf_seid=%d main_vsi_seid=%d\n",
14195 				 pf->pf_seid, pf->main_vsi_seid);
14196 		break;
14197 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14198 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14199 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14200 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14201 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14202 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14203 		/* ignore these for now */
14204 		break;
14205 	default:
14206 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14207 			 element_type, seid);
14208 		break;
14209 	}
14210 }
14211 
14212 /**
14213  * i40e_fetch_switch_configuration - Get switch config from firmware
14214  * @pf: board private structure
14215  * @printconfig: should we print the contents
14216  *
14217  * Get the current switch configuration from the device and
14218  * extract a few useful SEID values.
14219  **/
i40e_fetch_switch_configuration(struct i40e_pf * pf,bool printconfig)14220 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14221 {
14222 	struct i40e_aqc_get_switch_config_resp *sw_config;
14223 	u16 next_seid = 0;
14224 	int ret = 0;
14225 	u8 *aq_buf;
14226 	int i;
14227 
14228 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14229 	if (!aq_buf)
14230 		return -ENOMEM;
14231 
14232 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14233 	do {
14234 		u16 num_reported, num_total;
14235 
14236 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14237 						I40E_AQ_LARGE_BUF,
14238 						&next_seid, NULL);
14239 		if (ret) {
14240 			dev_info(&pf->pdev->dev,
14241 				 "get switch config failed err %s aq_err %s\n",
14242 				 i40e_stat_str(&pf->hw, ret),
14243 				 i40e_aq_str(&pf->hw,
14244 					     pf->hw.aq.asq_last_status));
14245 			kfree(aq_buf);
14246 			return -ENOENT;
14247 		}
14248 
14249 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14250 		num_total = le16_to_cpu(sw_config->header.num_total);
14251 
14252 		if (printconfig)
14253 			dev_info(&pf->pdev->dev,
14254 				 "header: %d reported %d total\n",
14255 				 num_reported, num_total);
14256 
14257 		for (i = 0; i < num_reported; i++) {
14258 			struct i40e_aqc_switch_config_element_resp *ele =
14259 				&sw_config->element[i];
14260 
14261 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14262 						     printconfig);
14263 		}
14264 	} while (next_seid != 0);
14265 
14266 	kfree(aq_buf);
14267 	return ret;
14268 }
14269 
14270 /**
14271  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14272  * @pf: board private structure
14273  * @reinit: if the Main VSI needs to re-initialized.
14274  * @lock_acquired: indicates whether or not the lock has been acquired
14275  *
14276  * Returns 0 on success, negative value on failure
14277  **/
i40e_setup_pf_switch(struct i40e_pf * pf,bool reinit,bool lock_acquired)14278 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14279 {
14280 	u16 flags = 0;
14281 	int ret;
14282 
14283 	/* find out what's out there already */
14284 	ret = i40e_fetch_switch_configuration(pf, false);
14285 	if (ret) {
14286 		dev_info(&pf->pdev->dev,
14287 			 "couldn't fetch switch config, err %s aq_err %s\n",
14288 			 i40e_stat_str(&pf->hw, ret),
14289 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14290 		return ret;
14291 	}
14292 	i40e_pf_reset_stats(pf);
14293 
14294 	/* set the switch config bit for the whole device to
14295 	 * support limited promisc or true promisc
14296 	 * when user requests promisc. The default is limited
14297 	 * promisc.
14298 	*/
14299 
14300 	if ((pf->hw.pf_id == 0) &&
14301 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
14302 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14303 		pf->last_sw_conf_flags = flags;
14304 	}
14305 
14306 	if (pf->hw.pf_id == 0) {
14307 		u16 valid_flags;
14308 
14309 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
14310 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
14311 						NULL);
14312 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
14313 			dev_info(&pf->pdev->dev,
14314 				 "couldn't set switch config bits, err %s aq_err %s\n",
14315 				 i40e_stat_str(&pf->hw, ret),
14316 				 i40e_aq_str(&pf->hw,
14317 					     pf->hw.aq.asq_last_status));
14318 			/* not a fatal problem, just keep going */
14319 		}
14320 		pf->last_sw_conf_valid_flags = valid_flags;
14321 	}
14322 
14323 	/* first time setup */
14324 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
14325 		struct i40e_vsi *vsi = NULL;
14326 		u16 uplink_seid;
14327 
14328 		/* Set up the PF VSI associated with the PF's main VSI
14329 		 * that is already in the HW switch
14330 		 */
14331 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
14332 			uplink_seid = pf->veb[pf->lan_veb]->seid;
14333 		else
14334 			uplink_seid = pf->mac_seid;
14335 		if (pf->lan_vsi == I40E_NO_VSI)
14336 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
14337 		else if (reinit)
14338 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
14339 		if (!vsi) {
14340 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
14341 			i40e_cloud_filter_exit(pf);
14342 			i40e_fdir_teardown(pf);
14343 			return -EAGAIN;
14344 		}
14345 	} else {
14346 		/* force a reset of TC and queue layout configurations */
14347 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14348 
14349 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14350 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14351 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14352 	}
14353 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
14354 
14355 	i40e_fdir_sb_setup(pf);
14356 
14357 	/* Setup static PF queue filter control settings */
14358 	ret = i40e_setup_pf_filter_control(pf);
14359 	if (ret) {
14360 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
14361 			 ret);
14362 		/* Failure here should not stop continuing other steps */
14363 	}
14364 
14365 	/* enable RSS in the HW, even for only one queue, as the stack can use
14366 	 * the hash
14367 	 */
14368 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
14369 		i40e_pf_config_rss(pf);
14370 
14371 	/* fill in link information and enable LSE reporting */
14372 	i40e_link_event(pf);
14373 
14374 	/* Initialize user-specific link properties */
14375 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
14376 				  I40E_AQ_AN_COMPLETED) ? true : false);
14377 
14378 	i40e_ptp_init(pf);
14379 
14380 	if (!lock_acquired)
14381 		rtnl_lock();
14382 
14383 	/* repopulate tunnel port filters */
14384 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
14385 
14386 	if (!lock_acquired)
14387 		rtnl_unlock();
14388 
14389 	return ret;
14390 }
14391 
14392 /**
14393  * i40e_determine_queue_usage - Work out queue distribution
14394  * @pf: board private structure
14395  **/
i40e_determine_queue_usage(struct i40e_pf * pf)14396 static void i40e_determine_queue_usage(struct i40e_pf *pf)
14397 {
14398 	int queues_left;
14399 	int q_max;
14400 
14401 	pf->num_lan_qps = 0;
14402 
14403 	/* Find the max queues to be put into basic use.  We'll always be
14404 	 * using TC0, whether or not DCB is running, and TC0 will get the
14405 	 * big RSS set.
14406 	 */
14407 	queues_left = pf->hw.func_caps.num_tx_qp;
14408 
14409 	if ((queues_left == 1) ||
14410 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
14411 		/* one qp for PF, no queues for anything else */
14412 		queues_left = 0;
14413 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14414 
14415 		/* make sure all the fancies are disabled */
14416 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14417 			       I40E_FLAG_IWARP_ENABLED	|
14418 			       I40E_FLAG_FD_SB_ENABLED	|
14419 			       I40E_FLAG_FD_ATR_ENABLED	|
14420 			       I40E_FLAG_DCB_CAPABLE	|
14421 			       I40E_FLAG_DCB_ENABLED	|
14422 			       I40E_FLAG_SRIOV_ENABLED	|
14423 			       I40E_FLAG_VMDQ_ENABLED);
14424 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14425 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
14426 				  I40E_FLAG_FD_SB_ENABLED |
14427 				  I40E_FLAG_FD_ATR_ENABLED |
14428 				  I40E_FLAG_DCB_CAPABLE))) {
14429 		/* one qp for PF */
14430 		pf->alloc_rss_size = pf->num_lan_qps = 1;
14431 		queues_left -= pf->num_lan_qps;
14432 
14433 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
14434 			       I40E_FLAG_IWARP_ENABLED	|
14435 			       I40E_FLAG_FD_SB_ENABLED	|
14436 			       I40E_FLAG_FD_ATR_ENABLED	|
14437 			       I40E_FLAG_DCB_ENABLED	|
14438 			       I40E_FLAG_VMDQ_ENABLED);
14439 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14440 	} else {
14441 		/* Not enough queues for all TCs */
14442 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
14443 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
14444 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
14445 					I40E_FLAG_DCB_ENABLED);
14446 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
14447 		}
14448 
14449 		/* limit lan qps to the smaller of qps, cpus or msix */
14450 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
14451 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
14452 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
14453 		pf->num_lan_qps = q_max;
14454 
14455 		queues_left -= pf->num_lan_qps;
14456 	}
14457 
14458 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14459 		if (queues_left > 1) {
14460 			queues_left -= 1; /* save 1 queue for FD */
14461 		} else {
14462 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
14463 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
14464 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
14465 		}
14466 	}
14467 
14468 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
14469 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
14470 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
14471 					(queues_left / pf->num_vf_qps));
14472 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
14473 	}
14474 
14475 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
14476 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
14477 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
14478 					  (queues_left / pf->num_vmdq_qps));
14479 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
14480 	}
14481 
14482 	pf->queues_left = queues_left;
14483 	dev_dbg(&pf->pdev->dev,
14484 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
14485 		pf->hw.func_caps.num_tx_qp,
14486 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
14487 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
14488 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
14489 		queues_left);
14490 }
14491 
14492 /**
14493  * i40e_setup_pf_filter_control - Setup PF static filter control
14494  * @pf: PF to be setup
14495  *
14496  * i40e_setup_pf_filter_control sets up a PF's initial filter control
14497  * settings. If PE/FCoE are enabled then it will also set the per PF
14498  * based filter sizes required for them. It also enables Flow director,
14499  * ethertype and macvlan type filter settings for the pf.
14500  *
14501  * Returns 0 on success, negative on failure
14502  **/
i40e_setup_pf_filter_control(struct i40e_pf * pf)14503 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
14504 {
14505 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
14506 
14507 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
14508 
14509 	/* Flow Director is enabled */
14510 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
14511 		settings->enable_fdir = true;
14512 
14513 	/* Ethtype and MACVLAN filters enabled for PF */
14514 	settings->enable_ethtype = true;
14515 	settings->enable_macvlan = true;
14516 
14517 	if (i40e_set_filter_control(&pf->hw, settings))
14518 		return -ENOENT;
14519 
14520 	return 0;
14521 }
14522 
14523 #define INFO_STRING_LEN 255
14524 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
i40e_print_features(struct i40e_pf * pf)14525 static void i40e_print_features(struct i40e_pf *pf)
14526 {
14527 	struct i40e_hw *hw = &pf->hw;
14528 	char *buf;
14529 	int i;
14530 
14531 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
14532 	if (!buf)
14533 		return;
14534 
14535 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
14536 #ifdef CONFIG_PCI_IOV
14537 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
14538 #endif
14539 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
14540 		      pf->hw.func_caps.num_vsis,
14541 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
14542 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
14543 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
14544 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
14545 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
14546 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
14547 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
14548 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
14549 	}
14550 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
14551 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
14552 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
14553 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
14554 	if (pf->flags & I40E_FLAG_PTP)
14555 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
14556 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
14557 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
14558 	else
14559 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
14560 
14561 	dev_info(&pf->pdev->dev, "%s\n", buf);
14562 	kfree(buf);
14563 	WARN_ON(i > INFO_STRING_LEN);
14564 }
14565 
14566 /**
14567  * i40e_get_platform_mac_addr - get platform-specific MAC address
14568  * @pdev: PCI device information struct
14569  * @pf: board private structure
14570  *
14571  * Look up the MAC address for the device. First we'll try
14572  * eth_platform_get_mac_address, which will check Open Firmware, or arch
14573  * specific fallback. Otherwise, we'll default to the stored value in
14574  * firmware.
14575  **/
i40e_get_platform_mac_addr(struct pci_dev * pdev,struct i40e_pf * pf)14576 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
14577 {
14578 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
14579 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
14580 }
14581 
14582 /**
14583  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
14584  * @fec_cfg: FEC option to set in flags
14585  * @flags: ptr to flags in which we set FEC option
14586  **/
i40e_set_fec_in_flags(u8 fec_cfg,u32 * flags)14587 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
14588 {
14589 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
14590 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
14591 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
14592 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
14593 		*flags |= I40E_FLAG_RS_FEC;
14594 		*flags &= ~I40E_FLAG_BASE_R_FEC;
14595 	}
14596 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
14597 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
14598 		*flags |= I40E_FLAG_BASE_R_FEC;
14599 		*flags &= ~I40E_FLAG_RS_FEC;
14600 	}
14601 	if (fec_cfg == 0)
14602 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
14603 }
14604 
14605 /**
14606  * i40e_check_recovery_mode - check if we are running transition firmware
14607  * @pf: board private structure
14608  *
14609  * Check registers indicating the firmware runs in recovery mode. Sets the
14610  * appropriate driver state.
14611  *
14612  * Returns true if the recovery mode was detected, false otherwise
14613  **/
i40e_check_recovery_mode(struct i40e_pf * pf)14614 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
14615 {
14616 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
14617 
14618 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
14619 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
14620 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
14621 		set_bit(__I40E_RECOVERY_MODE, pf->state);
14622 
14623 		return true;
14624 	}
14625 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
14626 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
14627 
14628 	return false;
14629 }
14630 
14631 /**
14632  * i40e_pf_loop_reset - perform reset in a loop.
14633  * @pf: board private structure
14634  *
14635  * This function is useful when a NIC is about to enter recovery mode.
14636  * When a NIC's internal data structures are corrupted the NIC's
14637  * firmware is going to enter recovery mode.
14638  * Right after a POR it takes about 7 minutes for firmware to enter
14639  * recovery mode. Until that time a NIC is in some kind of intermediate
14640  * state. After that time period the NIC almost surely enters
14641  * recovery mode. The only way for a driver to detect intermediate
14642  * state is to issue a series of pf-resets and check a return value.
14643  * If a PF reset returns success then the firmware could be in recovery
14644  * mode so the caller of this code needs to check for recovery mode
14645  * if this function returns success. There is a little chance that
14646  * firmware will hang in intermediate state forever.
14647  * Since waiting 7 minutes is quite a lot of time this function waits
14648  * 10 seconds and then gives up by returning an error.
14649  *
14650  * Return 0 on success, negative on failure.
14651  **/
i40e_pf_loop_reset(struct i40e_pf * pf)14652 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
14653 {
14654 	/* wait max 10 seconds for PF reset to succeed */
14655 	const unsigned long time_end = jiffies + 10 * HZ;
14656 
14657 	struct i40e_hw *hw = &pf->hw;
14658 	i40e_status ret;
14659 
14660 	ret = i40e_pf_reset(hw);
14661 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
14662 		usleep_range(10000, 20000);
14663 		ret = i40e_pf_reset(hw);
14664 	}
14665 
14666 	if (ret == I40E_SUCCESS)
14667 		pf->pfr_count++;
14668 	else
14669 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
14670 
14671 	return ret;
14672 }
14673 
14674 /**
14675  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
14676  * @pf: board private structure
14677  *
14678  * Check FW registers to determine if FW issued unexpected EMP Reset.
14679  * Every time when unexpected EMP Reset occurs the FW increments
14680  * a counter of unexpected EMP Resets. When the counter reaches 10
14681  * the FW should enter the Recovery mode
14682  *
14683  * Returns true if FW issued unexpected EMP Reset
14684  **/
i40e_check_fw_empr(struct i40e_pf * pf)14685 static bool i40e_check_fw_empr(struct i40e_pf *pf)
14686 {
14687 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
14688 			   I40E_GL_FWSTS_FWS1B_MASK;
14689 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
14690 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
14691 }
14692 
14693 /**
14694  * i40e_handle_resets - handle EMP resets and PF resets
14695  * @pf: board private structure
14696  *
14697  * Handle both EMP resets and PF resets and conclude whether there are
14698  * any issues regarding these resets. If there are any issues then
14699  * generate log entry.
14700  *
14701  * Return 0 if NIC is healthy or negative value when there are issues
14702  * with resets
14703  **/
i40e_handle_resets(struct i40e_pf * pf)14704 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
14705 {
14706 	const i40e_status pfr = i40e_pf_loop_reset(pf);
14707 	const bool is_empr = i40e_check_fw_empr(pf);
14708 
14709 	if (is_empr || pfr != I40E_SUCCESS)
14710 		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");
14711 
14712 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
14713 }
14714 
14715 /**
14716  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
14717  * @pf: board private structure
14718  * @hw: ptr to the hardware info
14719  *
14720  * This function does a minimal setup of all subsystems needed for running
14721  * recovery mode.
14722  *
14723  * Returns 0 on success, negative on failure
14724  **/
i40e_init_recovery_mode(struct i40e_pf * pf,struct i40e_hw * hw)14725 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
14726 {
14727 	struct i40e_vsi *vsi;
14728 	int err;
14729 	int v_idx;
14730 
14731 	pci_save_state(pf->pdev);
14732 
14733 	/* set up periodic task facility */
14734 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
14735 	pf->service_timer_period = HZ;
14736 
14737 	INIT_WORK(&pf->service_task, i40e_service_task);
14738 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
14739 
14740 	err = i40e_init_interrupt_scheme(pf);
14741 	if (err)
14742 		goto err_switch_setup;
14743 
14744 	/* The number of VSIs reported by the FW is the minimum guaranteed
14745 	 * to us; HW supports far more and we share the remaining pool with
14746 	 * the other PFs. We allocate space for more than the guarantee with
14747 	 * the understanding that we might not get them all later.
14748 	 */
14749 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
14750 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
14751 	else
14752 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
14753 
14754 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
14755 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
14756 			  GFP_KERNEL);
14757 	if (!pf->vsi) {
14758 		err = -ENOMEM;
14759 		goto err_switch_setup;
14760 	}
14761 
14762 	/* We allocate one VSI which is needed as absolute minimum
14763 	 * in order to register the netdev
14764 	 */
14765 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
14766 	if (v_idx < 0) {
14767 		err = v_idx;
14768 		goto err_switch_setup;
14769 	}
14770 	pf->lan_vsi = v_idx;
14771 	vsi = pf->vsi[v_idx];
14772 	if (!vsi) {
14773 		err = -EFAULT;
14774 		goto err_switch_setup;
14775 	}
14776 	vsi->alloc_queue_pairs = 1;
14777 	err = i40e_config_netdev(vsi);
14778 	if (err)
14779 		goto err_switch_setup;
14780 	err = register_netdev(vsi->netdev);
14781 	if (err)
14782 		goto err_switch_setup;
14783 	vsi->netdev_registered = true;
14784 	i40e_dbg_pf_init(pf);
14785 
14786 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
14787 	if (err)
14788 		goto err_switch_setup;
14789 
14790 	/* tell the firmware that we're starting */
14791 	i40e_send_version(pf);
14792 
14793 	/* since everything's happy, start the service_task timer */
14794 	mod_timer(&pf->service_timer,
14795 		  round_jiffies(jiffies + pf->service_timer_period));
14796 
14797 	return 0;
14798 
14799 err_switch_setup:
14800 	i40e_reset_interrupt_capability(pf);
14801 	del_timer_sync(&pf->service_timer);
14802 	i40e_shutdown_adminq(hw);
14803 	iounmap(hw->hw_addr);
14804 	pci_disable_pcie_error_reporting(pf->pdev);
14805 	pci_release_mem_regions(pf->pdev);
14806 	pci_disable_device(pf->pdev);
14807 	kfree(pf);
14808 
14809 	return err;
14810 }
14811 
14812 /**
14813  * i40e_probe - Device initialization routine
14814  * @pdev: PCI device information struct
14815  * @ent: entry in i40e_pci_tbl
14816  *
14817  * i40e_probe initializes a PF identified by a pci_dev structure.
14818  * The OS initialization, configuring of the PF private structure,
14819  * and a hardware reset occur.
14820  *
14821  * Returns 0 on success, negative on failure
14822  **/
i40e_probe(struct pci_dev * pdev,const struct pci_device_id * ent)14823 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
14824 {
14825 	struct i40e_aq_get_phy_abilities_resp abilities;
14826 	struct i40e_pf *pf;
14827 	struct i40e_hw *hw;
14828 	static u16 pfs_found;
14829 	u16 wol_nvm_bits;
14830 	u16 link_status;
14831 	int err;
14832 	u32 val;
14833 	u32 i;
14834 
14835 	err = pci_enable_device_mem(pdev);
14836 	if (err)
14837 		return err;
14838 
14839 	/* set up for high or low dma */
14840 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
14841 	if (err) {
14842 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
14843 		if (err) {
14844 			dev_err(&pdev->dev,
14845 				"DMA configuration failed: 0x%x\n", err);
14846 			goto err_dma;
14847 		}
14848 	}
14849 
14850 	/* set up pci connections */
14851 	err = pci_request_mem_regions(pdev, i40e_driver_name);
14852 	if (err) {
14853 		dev_info(&pdev->dev,
14854 			 "pci_request_selected_regions failed %d\n", err);
14855 		goto err_pci_reg;
14856 	}
14857 
14858 	pci_enable_pcie_error_reporting(pdev);
14859 	pci_set_master(pdev);
14860 
14861 	/* Now that we have a PCI connection, we need to do the
14862 	 * low level device setup.  This is primarily setting up
14863 	 * the Admin Queue structures and then querying for the
14864 	 * device's current profile information.
14865 	 */
14866 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
14867 	if (!pf) {
14868 		err = -ENOMEM;
14869 		goto err_pf_alloc;
14870 	}
14871 	pf->next_vsi = 0;
14872 	pf->pdev = pdev;
14873 	set_bit(__I40E_DOWN, pf->state);
14874 
14875 	hw = &pf->hw;
14876 	hw->back = pf;
14877 
14878 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
14879 				I40E_MAX_CSR_SPACE);
14880 	/* We believe that the highest register to read is
14881 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
14882 	 * is not less than that before mapping to prevent a
14883 	 * kernel panic.
14884 	 */
14885 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
14886 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
14887 			pf->ioremap_len);
14888 		err = -ENOMEM;
14889 		goto err_ioremap;
14890 	}
14891 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
14892 	if (!hw->hw_addr) {
14893 		err = -EIO;
14894 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
14895 			 (unsigned int)pci_resource_start(pdev, 0),
14896 			 pf->ioremap_len, err);
14897 		goto err_ioremap;
14898 	}
14899 	hw->vendor_id = pdev->vendor;
14900 	hw->device_id = pdev->device;
14901 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
14902 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
14903 	hw->subsystem_device_id = pdev->subsystem_device;
14904 	hw->bus.device = PCI_SLOT(pdev->devfn);
14905 	hw->bus.func = PCI_FUNC(pdev->devfn);
14906 	hw->bus.bus_id = pdev->bus->number;
14907 	pf->instance = pfs_found;
14908 
14909 	/* Select something other than the 802.1ad ethertype for the
14910 	 * switch to use internally and drop on ingress.
14911 	 */
14912 	hw->switch_tag = 0xffff;
14913 	hw->first_tag = ETH_P_8021AD;
14914 	hw->second_tag = ETH_P_8021Q;
14915 
14916 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
14917 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
14918 	INIT_LIST_HEAD(&pf->ddp_old_prof);
14919 
14920 	/* set up the locks for the AQ, do this only once in probe
14921 	 * and destroy them only once in remove
14922 	 */
14923 	mutex_init(&hw->aq.asq_mutex);
14924 	mutex_init(&hw->aq.arq_mutex);
14925 
14926 	pf->msg_enable = netif_msg_init(debug,
14927 					NETIF_MSG_DRV |
14928 					NETIF_MSG_PROBE |
14929 					NETIF_MSG_LINK);
14930 	if (debug < -1)
14931 		pf->hw.debug_mask = debug;
14932 
14933 	/* do a special CORER for clearing PXE mode once at init */
14934 	if (hw->revision_id == 0 &&
14935 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
14936 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
14937 		i40e_flush(hw);
14938 		msleep(200);
14939 		pf->corer_count++;
14940 
14941 		i40e_clear_pxe_mode(hw);
14942 	}
14943 
14944 	/* Reset here to make sure all is clean and to define PF 'n' */
14945 	i40e_clear_hw(hw);
14946 
14947 	err = i40e_set_mac_type(hw);
14948 	if (err) {
14949 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
14950 			 err);
14951 		goto err_pf_reset;
14952 	}
14953 
14954 	err = i40e_handle_resets(pf);
14955 	if (err)
14956 		goto err_pf_reset;
14957 
14958 	i40e_check_recovery_mode(pf);
14959 
14960 	hw->aq.num_arq_entries = I40E_AQ_LEN;
14961 	hw->aq.num_asq_entries = I40E_AQ_LEN;
14962 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14963 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
14964 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
14965 
14966 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
14967 		 "%s-%s:misc",
14968 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
14969 
14970 	err = i40e_init_shared_code(hw);
14971 	if (err) {
14972 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
14973 			 err);
14974 		goto err_pf_reset;
14975 	}
14976 
14977 	/* set up a default setting for link flow control */
14978 	pf->hw.fc.requested_mode = I40E_FC_NONE;
14979 
14980 	err = i40e_init_adminq(hw);
14981 	if (err) {
14982 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
14983 			dev_info(&pdev->dev,
14984 				 "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",
14985 				 hw->aq.api_maj_ver,
14986 				 hw->aq.api_min_ver,
14987 				 I40E_FW_API_VERSION_MAJOR,
14988 				 I40E_FW_MINOR_VERSION(hw));
14989 		else
14990 			dev_info(&pdev->dev,
14991 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
14992 
14993 		goto err_pf_reset;
14994 	}
14995 	i40e_get_oem_version(hw);
14996 
14997 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
14998 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
14999 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15000 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15001 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15002 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15003 
15004 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15005 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15006 		dev_dbg(&pdev->dev,
15007 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15008 			 hw->aq.api_maj_ver,
15009 			 hw->aq.api_min_ver,
15010 			 I40E_FW_API_VERSION_MAJOR,
15011 			 I40E_FW_MINOR_VERSION(hw));
15012 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15013 		dev_info(&pdev->dev,
15014 			 "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",
15015 			 hw->aq.api_maj_ver,
15016 			 hw->aq.api_min_ver,
15017 			 I40E_FW_API_VERSION_MAJOR,
15018 			 I40E_FW_MINOR_VERSION(hw));
15019 
15020 	i40e_verify_eeprom(pf);
15021 
15022 	/* Rev 0 hardware was never productized */
15023 	if (hw->revision_id < 1)
15024 		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");
15025 
15026 	i40e_clear_pxe_mode(hw);
15027 
15028 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15029 	if (err)
15030 		goto err_adminq_setup;
15031 
15032 	err = i40e_sw_init(pf);
15033 	if (err) {
15034 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15035 		goto err_sw_init;
15036 	}
15037 
15038 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15039 		return i40e_init_recovery_mode(pf, hw);
15040 
15041 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15042 				hw->func_caps.num_rx_qp, 0, 0);
15043 	if (err) {
15044 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15045 		goto err_init_lan_hmc;
15046 	}
15047 
15048 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15049 	if (err) {
15050 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15051 		err = -ENOENT;
15052 		goto err_configure_lan_hmc;
15053 	}
15054 
15055 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15056 	 * Ignore error return codes because if it was already disabled via
15057 	 * hardware settings this will fail
15058 	 */
15059 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15060 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15061 		i40e_aq_stop_lldp(hw, true, false, NULL);
15062 	}
15063 
15064 	/* allow a platform config to override the HW addr */
15065 	i40e_get_platform_mac_addr(pdev, pf);
15066 
15067 	if (!is_valid_ether_addr(hw->mac.addr)) {
15068 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15069 		err = -EIO;
15070 		goto err_mac_addr;
15071 	}
15072 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15073 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15074 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15075 	if (is_valid_ether_addr(hw->mac.port_addr))
15076 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15077 
15078 	pci_set_drvdata(pdev, pf);
15079 	pci_save_state(pdev);
15080 
15081 	dev_info(&pdev->dev,
15082 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15083 			"FW LLDP is disabled\n" :
15084 			"FW LLDP is enabled\n");
15085 
15086 	/* Enable FW to write default DCB config on link-up */
15087 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15088 
15089 #ifdef CONFIG_I40E_DCB
15090 	err = i40e_init_pf_dcb(pf);
15091 	if (err) {
15092 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15093 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15094 		/* Continue without DCB enabled */
15095 	}
15096 #endif /* CONFIG_I40E_DCB */
15097 
15098 	/* set up periodic task facility */
15099 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15100 	pf->service_timer_period = HZ;
15101 
15102 	INIT_WORK(&pf->service_task, i40e_service_task);
15103 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15104 
15105 	/* NVM bit on means WoL disabled for the port */
15106 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15107 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15108 		pf->wol_en = false;
15109 	else
15110 		pf->wol_en = true;
15111 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15112 
15113 	/* set up the main switch operations */
15114 	i40e_determine_queue_usage(pf);
15115 	err = i40e_init_interrupt_scheme(pf);
15116 	if (err)
15117 		goto err_switch_setup;
15118 
15119 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15120 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15121 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15122 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15123 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15124 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15125 						    UDP_TUNNEL_TYPE_GENEVE;
15126 
15127 	/* The number of VSIs reported by the FW is the minimum guaranteed
15128 	 * to us; HW supports far more and we share the remaining pool with
15129 	 * the other PFs. We allocate space for more than the guarantee with
15130 	 * the understanding that we might not get them all later.
15131 	 */
15132 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15133 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15134 	else
15135 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15136 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15137 		dev_warn(&pf->pdev->dev,
15138 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15139 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15140 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15141 	}
15142 
15143 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15144 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15145 			  GFP_KERNEL);
15146 	if (!pf->vsi) {
15147 		err = -ENOMEM;
15148 		goto err_switch_setup;
15149 	}
15150 
15151 #ifdef CONFIG_PCI_IOV
15152 	/* prep for VF support */
15153 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15154 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15155 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15156 		if (pci_num_vf(pdev))
15157 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15158 	}
15159 #endif
15160 	err = i40e_setup_pf_switch(pf, false, false);
15161 	if (err) {
15162 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15163 		goto err_vsis;
15164 	}
15165 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15166 
15167 	/* if FDIR VSI was set up, start it now */
15168 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15169 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15170 			i40e_vsi_open(pf->vsi[i]);
15171 			break;
15172 		}
15173 	}
15174 
15175 	/* The driver only wants link up/down and module qualification
15176 	 * reports from firmware.  Note the negative logic.
15177 	 */
15178 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15179 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15180 					 I40E_AQ_EVENT_MEDIA_NA |
15181 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15182 	if (err)
15183 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15184 			 i40e_stat_str(&pf->hw, err),
15185 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15186 
15187 	/* Reconfigure hardware for allowing smaller MSS in the case
15188 	 * of TSO, so that we avoid the MDD being fired and causing
15189 	 * a reset in the case of small MSS+TSO.
15190 	 */
15191 	val = rd32(hw, I40E_REG_MSS);
15192 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15193 		val &= ~I40E_REG_MSS_MIN_MASK;
15194 		val |= I40E_64BYTE_MSS;
15195 		wr32(hw, I40E_REG_MSS, val);
15196 	}
15197 
15198 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15199 		msleep(75);
15200 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15201 		if (err)
15202 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15203 				 i40e_stat_str(&pf->hw, err),
15204 				 i40e_aq_str(&pf->hw,
15205 					     pf->hw.aq.asq_last_status));
15206 	}
15207 	/* The main driver is (mostly) up and happy. We need to set this state
15208 	 * before setting up the misc vector or we get a race and the vector
15209 	 * ends up disabled forever.
15210 	 */
15211 	clear_bit(__I40E_DOWN, pf->state);
15212 
15213 	/* In case of MSIX we are going to setup the misc vector right here
15214 	 * to handle admin queue events etc. In case of legacy and MSI
15215 	 * the misc functionality and queue processing is combined in
15216 	 * the same vector and that gets setup at open.
15217 	 */
15218 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15219 		err = i40e_setup_misc_vector(pf);
15220 		if (err) {
15221 			dev_info(&pdev->dev,
15222 				 "setup of misc vector failed: %d\n", err);
15223 			i40e_cloud_filter_exit(pf);
15224 			i40e_fdir_teardown(pf);
15225 			goto err_vsis;
15226 		}
15227 	}
15228 
15229 #ifdef CONFIG_PCI_IOV
15230 	/* prep for VF support */
15231 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15232 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15233 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15234 		/* disable link interrupts for VFs */
15235 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15236 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15237 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15238 		i40e_flush(hw);
15239 
15240 		if (pci_num_vf(pdev)) {
15241 			dev_info(&pdev->dev,
15242 				 "Active VFs found, allocating resources.\n");
15243 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15244 			if (err)
15245 				dev_info(&pdev->dev,
15246 					 "Error %d allocating resources for existing VFs\n",
15247 					 err);
15248 		}
15249 	}
15250 #endif /* CONFIG_PCI_IOV */
15251 
15252 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15253 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
15254 						      pf->num_iwarp_msix,
15255 						      I40E_IWARP_IRQ_PILE_ID);
15256 		if (pf->iwarp_base_vector < 0) {
15257 			dev_info(&pdev->dev,
15258 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
15259 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
15260 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
15261 		}
15262 	}
15263 
15264 	i40e_dbg_pf_init(pf);
15265 
15266 	/* tell the firmware that we're starting */
15267 	i40e_send_version(pf);
15268 
15269 	/* since everything's happy, start the service_task timer */
15270 	mod_timer(&pf->service_timer,
15271 		  round_jiffies(jiffies + pf->service_timer_period));
15272 
15273 	/* add this PF to client device list and launch a client service task */
15274 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15275 		err = i40e_lan_add_device(pf);
15276 		if (err)
15277 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
15278 				 err);
15279 	}
15280 
15281 #define PCI_SPEED_SIZE 8
15282 #define PCI_WIDTH_SIZE 8
15283 	/* Devices on the IOSF bus do not have this information
15284 	 * and will report PCI Gen 1 x 1 by default so don't bother
15285 	 * checking them.
15286 	 */
15287 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
15288 		char speed[PCI_SPEED_SIZE] = "Unknown";
15289 		char width[PCI_WIDTH_SIZE] = "Unknown";
15290 
15291 		/* Get the negotiated link width and speed from PCI config
15292 		 * space
15293 		 */
15294 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
15295 					  &link_status);
15296 
15297 		i40e_set_pci_config_data(hw, link_status);
15298 
15299 		switch (hw->bus.speed) {
15300 		case i40e_bus_speed_8000:
15301 			strlcpy(speed, "8.0", PCI_SPEED_SIZE); break;
15302 		case i40e_bus_speed_5000:
15303 			strlcpy(speed, "5.0", PCI_SPEED_SIZE); break;
15304 		case i40e_bus_speed_2500:
15305 			strlcpy(speed, "2.5", PCI_SPEED_SIZE); break;
15306 		default:
15307 			break;
15308 		}
15309 		switch (hw->bus.width) {
15310 		case i40e_bus_width_pcie_x8:
15311 			strlcpy(width, "8", PCI_WIDTH_SIZE); break;
15312 		case i40e_bus_width_pcie_x4:
15313 			strlcpy(width, "4", PCI_WIDTH_SIZE); break;
15314 		case i40e_bus_width_pcie_x2:
15315 			strlcpy(width, "2", PCI_WIDTH_SIZE); break;
15316 		case i40e_bus_width_pcie_x1:
15317 			strlcpy(width, "1", PCI_WIDTH_SIZE); break;
15318 		default:
15319 			break;
15320 		}
15321 
15322 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
15323 			 speed, width);
15324 
15325 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
15326 		    hw->bus.speed < i40e_bus_speed_8000) {
15327 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
15328 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
15329 		}
15330 	}
15331 
15332 	/* get the requested speeds from the fw */
15333 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
15334 	if (err)
15335 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
15336 			i40e_stat_str(&pf->hw, err),
15337 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15338 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
15339 
15340 	/* set the FEC config due to the board capabilities */
15341 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
15342 
15343 	/* get the supported phy types from the fw */
15344 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
15345 	if (err)
15346 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
15347 			i40e_stat_str(&pf->hw, err),
15348 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15349 
15350 	/* make sure the MFS hasn't been set lower than the default */
15351 #define MAX_FRAME_SIZE_DEFAULT 0x2600
15352 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
15353 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
15354 	if (val < MAX_FRAME_SIZE_DEFAULT)
15355 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
15356 			 i, val);
15357 
15358 	/* Add a filter to drop all Flow control frames from any VSI from being
15359 	 * transmitted. By doing so we stop a malicious VF from sending out
15360 	 * PAUSE or PFC frames and potentially controlling traffic for other
15361 	 * PF/VF VSIs.
15362 	 * The FW can still send Flow control frames if enabled.
15363 	 */
15364 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
15365 						       pf->main_vsi_seid);
15366 
15367 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
15368 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
15369 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
15370 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
15371 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
15372 	/* print a string summarizing features */
15373 	i40e_print_features(pf);
15374 
15375 	return 0;
15376 
15377 	/* Unwind what we've done if something failed in the setup */
15378 err_vsis:
15379 	set_bit(__I40E_DOWN, pf->state);
15380 	i40e_clear_interrupt_scheme(pf);
15381 	kfree(pf->vsi);
15382 err_switch_setup:
15383 	i40e_reset_interrupt_capability(pf);
15384 	del_timer_sync(&pf->service_timer);
15385 err_mac_addr:
15386 err_configure_lan_hmc:
15387 	(void)i40e_shutdown_lan_hmc(hw);
15388 err_init_lan_hmc:
15389 	kfree(pf->qp_pile);
15390 err_sw_init:
15391 err_adminq_setup:
15392 err_pf_reset:
15393 	iounmap(hw->hw_addr);
15394 err_ioremap:
15395 	kfree(pf);
15396 err_pf_alloc:
15397 	pci_disable_pcie_error_reporting(pdev);
15398 	pci_release_mem_regions(pdev);
15399 err_pci_reg:
15400 err_dma:
15401 	pci_disable_device(pdev);
15402 	return err;
15403 }
15404 
15405 /**
15406  * i40e_remove - Device removal routine
15407  * @pdev: PCI device information struct
15408  *
15409  * i40e_remove is called by the PCI subsystem to alert the driver
15410  * that is should release a PCI device.  This could be caused by a
15411  * Hot-Plug event, or because the driver is going to be removed from
15412  * memory.
15413  **/
i40e_remove(struct pci_dev * pdev)15414 static void i40e_remove(struct pci_dev *pdev)
15415 {
15416 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15417 	struct i40e_hw *hw = &pf->hw;
15418 	i40e_status ret_code;
15419 	int i;
15420 
15421 	i40e_dbg_pf_exit(pf);
15422 
15423 	i40e_ptp_stop(pf);
15424 
15425 	/* Disable RSS in hw */
15426 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
15427 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
15428 
15429 	while (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
15430 		usleep_range(1000, 2000);
15431 
15432 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
15433 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
15434 		i40e_free_vfs(pf);
15435 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
15436 	}
15437 	/* no more scheduling of any task */
15438 	set_bit(__I40E_SUSPENDED, pf->state);
15439 	set_bit(__I40E_DOWN, pf->state);
15440 	if (pf->service_timer.function)
15441 		del_timer_sync(&pf->service_timer);
15442 	if (pf->service_task.func)
15443 		cancel_work_sync(&pf->service_task);
15444 
15445 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
15446 		struct i40e_vsi *vsi = pf->vsi[0];
15447 
15448 		/* We know that we have allocated only one vsi for this PF,
15449 		 * it was just for registering netdevice, so the interface
15450 		 * could be visible in the 'ifconfig' output
15451 		 */
15452 		unregister_netdev(vsi->netdev);
15453 		free_netdev(vsi->netdev);
15454 
15455 		goto unmap;
15456 	}
15457 
15458 	/* Client close must be called explicitly here because the timer
15459 	 * has been stopped.
15460 	 */
15461 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15462 
15463 	i40e_fdir_teardown(pf);
15464 
15465 	/* If there is a switch structure or any orphans, remove them.
15466 	 * This will leave only the PF's VSI remaining.
15467 	 */
15468 	for (i = 0; i < I40E_MAX_VEB; i++) {
15469 		if (!pf->veb[i])
15470 			continue;
15471 
15472 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
15473 		    pf->veb[i]->uplink_seid == 0)
15474 			i40e_switch_branch_release(pf->veb[i]);
15475 	}
15476 
15477 	/* Now we can shutdown the PF's VSI, just before we kill
15478 	 * adminq and hmc.
15479 	 */
15480 	if (pf->vsi[pf->lan_vsi])
15481 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
15482 
15483 	i40e_cloud_filter_exit(pf);
15484 
15485 	/* remove attached clients */
15486 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
15487 		ret_code = i40e_lan_del_device(pf);
15488 		if (ret_code)
15489 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
15490 				 ret_code);
15491 	}
15492 
15493 	/* shutdown and destroy the HMC */
15494 	if (hw->hmc.hmc_obj) {
15495 		ret_code = i40e_shutdown_lan_hmc(hw);
15496 		if (ret_code)
15497 			dev_warn(&pdev->dev,
15498 				 "Failed to destroy the HMC resources: %d\n",
15499 				 ret_code);
15500 	}
15501 
15502 unmap:
15503 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15504 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15505 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15506 		free_irq(pf->pdev->irq, pf);
15507 
15508 	/* shutdown the adminq */
15509 	i40e_shutdown_adminq(hw);
15510 
15511 	/* destroy the locks only once, here */
15512 	mutex_destroy(&hw->aq.arq_mutex);
15513 	mutex_destroy(&hw->aq.asq_mutex);
15514 
15515 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
15516 	rtnl_lock();
15517 	i40e_clear_interrupt_scheme(pf);
15518 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15519 		if (pf->vsi[i]) {
15520 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
15521 				i40e_vsi_clear_rings(pf->vsi[i]);
15522 			i40e_vsi_clear(pf->vsi[i]);
15523 			pf->vsi[i] = NULL;
15524 		}
15525 	}
15526 	rtnl_unlock();
15527 
15528 	for (i = 0; i < I40E_MAX_VEB; i++) {
15529 		kfree(pf->veb[i]);
15530 		pf->veb[i] = NULL;
15531 	}
15532 
15533 	kfree(pf->qp_pile);
15534 	kfree(pf->vsi);
15535 
15536 	iounmap(hw->hw_addr);
15537 	kfree(pf);
15538 	pci_release_mem_regions(pdev);
15539 
15540 	pci_disable_pcie_error_reporting(pdev);
15541 	pci_disable_device(pdev);
15542 }
15543 
15544 /**
15545  * i40e_pci_error_detected - warning that something funky happened in PCI land
15546  * @pdev: PCI device information struct
15547  * @error: the type of PCI error
15548  *
15549  * Called to warn that something happened and the error handling steps
15550  * are in progress.  Allows the driver to quiesce things, be ready for
15551  * remediation.
15552  **/
i40e_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t error)15553 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
15554 						pci_channel_state_t error)
15555 {
15556 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15557 
15558 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
15559 
15560 	if (!pf) {
15561 		dev_info(&pdev->dev,
15562 			 "Cannot recover - error happened during device probe\n");
15563 		return PCI_ERS_RESULT_DISCONNECT;
15564 	}
15565 
15566 	/* shutdown all operations */
15567 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15568 		i40e_prep_for_reset(pf, false);
15569 
15570 	/* Request a slot reset */
15571 	return PCI_ERS_RESULT_NEED_RESET;
15572 }
15573 
15574 /**
15575  * i40e_pci_error_slot_reset - a PCI slot reset just happened
15576  * @pdev: PCI device information struct
15577  *
15578  * Called to find if the driver can work with the device now that
15579  * the pci slot has been reset.  If a basic connection seems good
15580  * (registers are readable and have sane content) then return a
15581  * happy little PCI_ERS_RESULT_xxx.
15582  **/
i40e_pci_error_slot_reset(struct pci_dev * pdev)15583 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
15584 {
15585 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15586 	pci_ers_result_t result;
15587 	u32 reg;
15588 
15589 	dev_dbg(&pdev->dev, "%s\n", __func__);
15590 	if (pci_enable_device_mem(pdev)) {
15591 		dev_info(&pdev->dev,
15592 			 "Cannot re-enable PCI device after reset.\n");
15593 		result = PCI_ERS_RESULT_DISCONNECT;
15594 	} else {
15595 		pci_set_master(pdev);
15596 		pci_restore_state(pdev);
15597 		pci_save_state(pdev);
15598 		pci_wake_from_d3(pdev, false);
15599 
15600 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
15601 		if (reg == 0)
15602 			result = PCI_ERS_RESULT_RECOVERED;
15603 		else
15604 			result = PCI_ERS_RESULT_DISCONNECT;
15605 	}
15606 
15607 	return result;
15608 }
15609 
15610 /**
15611  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
15612  * @pdev: PCI device information struct
15613  */
i40e_pci_error_reset_prepare(struct pci_dev * pdev)15614 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
15615 {
15616 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15617 
15618 	i40e_prep_for_reset(pf, false);
15619 }
15620 
15621 /**
15622  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
15623  * @pdev: PCI device information struct
15624  */
i40e_pci_error_reset_done(struct pci_dev * pdev)15625 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
15626 {
15627 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15628 
15629 	i40e_reset_and_rebuild(pf, false, false);
15630 }
15631 
15632 /**
15633  * i40e_pci_error_resume - restart operations after PCI error recovery
15634  * @pdev: PCI device information struct
15635  *
15636  * Called to allow the driver to bring things back up after PCI error
15637  * and/or reset recovery has finished.
15638  **/
i40e_pci_error_resume(struct pci_dev * pdev)15639 static void i40e_pci_error_resume(struct pci_dev *pdev)
15640 {
15641 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15642 
15643 	dev_dbg(&pdev->dev, "%s\n", __func__);
15644 	if (test_bit(__I40E_SUSPENDED, pf->state))
15645 		return;
15646 
15647 	i40e_handle_reset_warning(pf, false);
15648 }
15649 
15650 /**
15651  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
15652  * using the mac_address_write admin q function
15653  * @pf: pointer to i40e_pf struct
15654  **/
i40e_enable_mc_magic_wake(struct i40e_pf * pf)15655 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
15656 {
15657 	struct i40e_hw *hw = &pf->hw;
15658 	i40e_status ret;
15659 	u8 mac_addr[6];
15660 	u16 flags = 0;
15661 
15662 	/* Get current MAC address in case it's an LAA */
15663 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
15664 		ether_addr_copy(mac_addr,
15665 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
15666 	} else {
15667 		dev_err(&pf->pdev->dev,
15668 			"Failed to retrieve MAC address; using default\n");
15669 		ether_addr_copy(mac_addr, hw->mac.addr);
15670 	}
15671 
15672 	/* The FW expects the mac address write cmd to first be called with
15673 	 * one of these flags before calling it again with the multicast
15674 	 * enable flags.
15675 	 */
15676 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
15677 
15678 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
15679 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
15680 
15681 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
15682 	if (ret) {
15683 		dev_err(&pf->pdev->dev,
15684 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
15685 		return;
15686 	}
15687 
15688 	flags = I40E_AQC_MC_MAG_EN
15689 			| I40E_AQC_WOL_PRESERVE_ON_PFR
15690 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
15691 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
15692 	if (ret)
15693 		dev_err(&pf->pdev->dev,
15694 			"Failed to enable Multicast Magic Packet wake up\n");
15695 }
15696 
15697 /**
15698  * i40e_shutdown - PCI callback for shutting down
15699  * @pdev: PCI device information struct
15700  **/
i40e_shutdown(struct pci_dev * pdev)15701 static void i40e_shutdown(struct pci_dev *pdev)
15702 {
15703 	struct i40e_pf *pf = pci_get_drvdata(pdev);
15704 	struct i40e_hw *hw = &pf->hw;
15705 
15706 	set_bit(__I40E_SUSPENDED, pf->state);
15707 	set_bit(__I40E_DOWN, pf->state);
15708 
15709 	del_timer_sync(&pf->service_timer);
15710 	cancel_work_sync(&pf->service_task);
15711 	i40e_cloud_filter_exit(pf);
15712 	i40e_fdir_teardown(pf);
15713 
15714 	/* Client close must be called explicitly here because the timer
15715 	 * has been stopped.
15716 	 */
15717 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15718 
15719 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15720 		i40e_enable_mc_magic_wake(pf);
15721 
15722 	i40e_prep_for_reset(pf, false);
15723 
15724 	wr32(hw, I40E_PFPM_APM,
15725 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15726 	wr32(hw, I40E_PFPM_WUFC,
15727 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15728 
15729 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
15730 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
15731 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
15732 		free_irq(pf->pdev->irq, pf);
15733 
15734 	/* Since we're going to destroy queues during the
15735 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15736 	 * whole section
15737 	 */
15738 	rtnl_lock();
15739 	i40e_clear_interrupt_scheme(pf);
15740 	rtnl_unlock();
15741 
15742 	if (system_state == SYSTEM_POWER_OFF) {
15743 		pci_wake_from_d3(pdev, pf->wol_en);
15744 		pci_set_power_state(pdev, PCI_D3hot);
15745 	}
15746 }
15747 
15748 /**
15749  * i40e_suspend - PM callback for moving to D3
15750  * @dev: generic device information structure
15751  **/
i40e_suspend(struct device * dev)15752 static int __maybe_unused i40e_suspend(struct device *dev)
15753 {
15754 	struct i40e_pf *pf = dev_get_drvdata(dev);
15755 	struct i40e_hw *hw = &pf->hw;
15756 
15757 	/* If we're already suspended, then there is nothing to do */
15758 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
15759 		return 0;
15760 
15761 	set_bit(__I40E_DOWN, pf->state);
15762 
15763 	/* Ensure service task will not be running */
15764 	del_timer_sync(&pf->service_timer);
15765 	cancel_work_sync(&pf->service_task);
15766 
15767 	/* Client close must be called explicitly here because the timer
15768 	 * has been stopped.
15769 	 */
15770 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
15771 
15772 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
15773 		i40e_enable_mc_magic_wake(pf);
15774 
15775 	/* Since we're going to destroy queues during the
15776 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
15777 	 * whole section
15778 	 */
15779 	rtnl_lock();
15780 
15781 	i40e_prep_for_reset(pf, true);
15782 
15783 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
15784 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
15785 
15786 	/* Clear the interrupt scheme and release our IRQs so that the system
15787 	 * can safely hibernate even when there are a large number of CPUs.
15788 	 * Otherwise hibernation might fail when mapping all the vectors back
15789 	 * to CPU0.
15790 	 */
15791 	i40e_clear_interrupt_scheme(pf);
15792 
15793 	rtnl_unlock();
15794 
15795 	return 0;
15796 }
15797 
15798 /**
15799  * i40e_resume - PM callback for waking up from D3
15800  * @dev: generic device information structure
15801  **/
i40e_resume(struct device * dev)15802 static int __maybe_unused i40e_resume(struct device *dev)
15803 {
15804 	struct i40e_pf *pf = dev_get_drvdata(dev);
15805 	int err;
15806 
15807 	/* If we're not suspended, then there is nothing to do */
15808 	if (!test_bit(__I40E_SUSPENDED, pf->state))
15809 		return 0;
15810 
15811 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
15812 	 * since we're going to be restoring queues
15813 	 */
15814 	rtnl_lock();
15815 
15816 	/* We cleared the interrupt scheme when we suspended, so we need to
15817 	 * restore it now to resume device functionality.
15818 	 */
15819 	err = i40e_restore_interrupt_scheme(pf);
15820 	if (err) {
15821 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
15822 			err);
15823 	}
15824 
15825 	clear_bit(__I40E_DOWN, pf->state);
15826 	i40e_reset_and_rebuild(pf, false, true);
15827 
15828 	rtnl_unlock();
15829 
15830 	/* Clear suspended state last after everything is recovered */
15831 	clear_bit(__I40E_SUSPENDED, pf->state);
15832 
15833 	/* Restart the service task */
15834 	mod_timer(&pf->service_timer,
15835 		  round_jiffies(jiffies + pf->service_timer_period));
15836 
15837 	return 0;
15838 }
15839 
15840 static const struct pci_error_handlers i40e_err_handler = {
15841 	.error_detected = i40e_pci_error_detected,
15842 	.slot_reset = i40e_pci_error_slot_reset,
15843 	.reset_prepare = i40e_pci_error_reset_prepare,
15844 	.reset_done = i40e_pci_error_reset_done,
15845 	.resume = i40e_pci_error_resume,
15846 };
15847 
15848 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
15849 
15850 static struct pci_driver i40e_driver = {
15851 	.name     = i40e_driver_name,
15852 	.id_table = i40e_pci_tbl,
15853 	.probe    = i40e_probe,
15854 	.remove   = i40e_remove,
15855 	.driver   = {
15856 		.pm = &i40e_pm_ops,
15857 	},
15858 	.shutdown = i40e_shutdown,
15859 	.err_handler = &i40e_err_handler,
15860 	.sriov_configure = i40e_pci_sriov_configure,
15861 };
15862 
15863 /**
15864  * i40e_init_module - Driver registration routine
15865  *
15866  * i40e_init_module is the first routine called when the driver is
15867  * loaded. All it does is register with the PCI subsystem.
15868  **/
i40e_init_module(void)15869 static int __init i40e_init_module(void)
15870 {
15871 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
15872 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
15873 
15874 	/* There is no need to throttle the number of active tasks because
15875 	 * each device limits its own task using a state bit for scheduling
15876 	 * the service task, and the device tasks do not interfere with each
15877 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
15878 	 * since we need to be able to guarantee forward progress even under
15879 	 * memory pressure.
15880 	 */
15881 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
15882 	if (!i40e_wq) {
15883 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
15884 		return -ENOMEM;
15885 	}
15886 
15887 	i40e_dbg_init();
15888 	return pci_register_driver(&i40e_driver);
15889 }
15890 module_init(i40e_init_module);
15891 
15892 /**
15893  * i40e_exit_module - Driver exit cleanup routine
15894  *
15895  * i40e_exit_module is called just before the driver is removed
15896  * from memory.
15897  **/
i40e_exit_module(void)15898 static void __exit i40e_exit_module(void)
15899 {
15900 	pci_unregister_driver(&i40e_driver);
15901 	destroy_workqueue(i40e_wq);
15902 	i40e_dbg_exit();
15903 }
15904 module_exit(i40e_exit_module);
15905