xref: /openbmc/linux/drivers/net/ethernet/intel/ice/ice_main.c (revision 44ad3baf1cca483e418b6aadf2d3994f69e0f16a)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2018-2023, Intel Corporation. */
3 
4 /* Intel(R) Ethernet Connection E800 Series Linux Driver */
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <generated/utsrelease.h>
9 #include <linux/crash_dump.h>
10 #include "ice.h"
11 #include "ice_base.h"
12 #include "ice_lib.h"
13 #include "ice_fltr.h"
14 #include "ice_dcb_lib.h"
15 #include "ice_dcb_nl.h"
16 #include "ice_devlink.h"
17 /* Including ice_trace.h with CREATE_TRACE_POINTS defined will generate the
18  * ice tracepoint functions. This must be done exactly once across the
19  * ice driver.
20  */
21 #define CREATE_TRACE_POINTS
22 #include "ice_trace.h"
23 #include "ice_eswitch.h"
24 #include "ice_tc_lib.h"
25 #include "ice_vsi_vlan_ops.h"
26 #include <net/xdp_sock_drv.h>
27 
28 #define DRV_SUMMARY	"Intel(R) Ethernet Connection E800 Series Linux Driver"
29 static const char ice_driver_string[] = DRV_SUMMARY;
30 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
31 
32 /* DDP Package file located in firmware search paths (e.g. /lib/firmware/) */
33 #define ICE_DDP_PKG_PATH	"intel/ice/ddp/"
34 #define ICE_DDP_PKG_FILE	ICE_DDP_PKG_PATH "ice.pkg"
35 
36 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
37 MODULE_DESCRIPTION(DRV_SUMMARY);
38 MODULE_LICENSE("GPL v2");
39 MODULE_FIRMWARE(ICE_DDP_PKG_FILE);
40 
41 static int debug = -1;
42 module_param(debug, int, 0644);
43 #ifndef CONFIG_DYNAMIC_DEBUG
44 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
45 #else
46 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
47 #endif /* !CONFIG_DYNAMIC_DEBUG */
48 
49 DEFINE_STATIC_KEY_FALSE(ice_xdp_locking_key);
50 EXPORT_SYMBOL(ice_xdp_locking_key);
51 
52 /**
53  * ice_hw_to_dev - Get device pointer from the hardware structure
54  * @hw: pointer to the device HW structure
55  *
56  * Used to access the device pointer from compilation units which can't easily
57  * include the definition of struct ice_pf without leading to circular header
58  * dependencies.
59  */
ice_hw_to_dev(struct ice_hw * hw)60 struct device *ice_hw_to_dev(struct ice_hw *hw)
61 {
62 	struct ice_pf *pf = container_of(hw, struct ice_pf, hw);
63 
64 	return &pf->pdev->dev;
65 }
66 
67 static struct workqueue_struct *ice_wq;
68 struct workqueue_struct *ice_lag_wq;
69 static const struct net_device_ops ice_netdev_safe_mode_ops;
70 static const struct net_device_ops ice_netdev_ops;
71 
72 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type);
73 
74 static void ice_vsi_release_all(struct ice_pf *pf);
75 
76 static int ice_rebuild_channels(struct ice_pf *pf);
77 static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_adv_fltr);
78 
79 static int
80 ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
81 		     void *cb_priv, enum tc_setup_type type, void *type_data,
82 		     void *data,
83 		     void (*cleanup)(struct flow_block_cb *block_cb));
84 
netif_is_ice(const struct net_device * dev)85 bool netif_is_ice(const struct net_device *dev)
86 {
87 	return dev && (dev->netdev_ops == &ice_netdev_ops ||
88 		       dev->netdev_ops == &ice_netdev_safe_mode_ops);
89 }
90 
91 /**
92  * ice_get_tx_pending - returns number of Tx descriptors not processed
93  * @ring: the ring of descriptors
94  */
ice_get_tx_pending(struct ice_tx_ring * ring)95 static u16 ice_get_tx_pending(struct ice_tx_ring *ring)
96 {
97 	u16 head, tail;
98 
99 	head = ring->next_to_clean;
100 	tail = ring->next_to_use;
101 
102 	if (head != tail)
103 		return (head < tail) ?
104 			tail - head : (tail + ring->count - head);
105 	return 0;
106 }
107 
108 /**
109  * ice_check_for_hang_subtask - check for and recover hung queues
110  * @pf: pointer to PF struct
111  */
ice_check_for_hang_subtask(struct ice_pf * pf)112 static void ice_check_for_hang_subtask(struct ice_pf *pf)
113 {
114 	struct ice_vsi *vsi = NULL;
115 	struct ice_hw *hw;
116 	unsigned int i;
117 	int packets;
118 	u32 v;
119 
120 	ice_for_each_vsi(pf, v)
121 		if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
122 			vsi = pf->vsi[v];
123 			break;
124 		}
125 
126 	if (!vsi || test_bit(ICE_VSI_DOWN, vsi->state))
127 		return;
128 
129 	if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
130 		return;
131 
132 	hw = &vsi->back->hw;
133 
134 	ice_for_each_txq(vsi, i) {
135 		struct ice_tx_ring *tx_ring = vsi->tx_rings[i];
136 		struct ice_ring_stats *ring_stats;
137 
138 		if (!tx_ring)
139 			continue;
140 		if (ice_ring_ch_enabled(tx_ring))
141 			continue;
142 
143 		ring_stats = tx_ring->ring_stats;
144 		if (!ring_stats)
145 			continue;
146 
147 		if (tx_ring->desc) {
148 			/* If packet counter has not changed the queue is
149 			 * likely stalled, so force an interrupt for this
150 			 * queue.
151 			 *
152 			 * prev_pkt would be negative if there was no
153 			 * pending work.
154 			 */
155 			packets = ring_stats->stats.pkts & INT_MAX;
156 			if (ring_stats->tx_stats.prev_pkt == packets) {
157 				/* Trigger sw interrupt to revive the queue */
158 				ice_trigger_sw_intr(hw, tx_ring->q_vector);
159 				continue;
160 			}
161 
162 			/* Memory barrier between read of packet count and call
163 			 * to ice_get_tx_pending()
164 			 */
165 			smp_rmb();
166 			ring_stats->tx_stats.prev_pkt =
167 			    ice_get_tx_pending(tx_ring) ? packets : -1;
168 		}
169 	}
170 }
171 
172 /**
173  * ice_init_mac_fltr - Set initial MAC filters
174  * @pf: board private structure
175  *
176  * Set initial set of MAC filters for PF VSI; configure filters for permanent
177  * address and broadcast address. If an error is encountered, netdevice will be
178  * unregistered.
179  */
ice_init_mac_fltr(struct ice_pf * pf)180 static int ice_init_mac_fltr(struct ice_pf *pf)
181 {
182 	struct ice_vsi *vsi;
183 	u8 *perm_addr;
184 
185 	vsi = ice_get_main_vsi(pf);
186 	if (!vsi)
187 		return -EINVAL;
188 
189 	perm_addr = vsi->port_info->mac.perm_addr;
190 	return ice_fltr_add_mac_and_broadcast(vsi, perm_addr, ICE_FWD_TO_VSI);
191 }
192 
193 /**
194  * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
195  * @netdev: the net device on which the sync is happening
196  * @addr: MAC address to sync
197  *
198  * This is a callback function which is called by the in kernel device sync
199  * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
200  * populates the tmp_sync_list, which is later used by ice_add_mac to add the
201  * MAC filters from the hardware.
202  */
ice_add_mac_to_sync_list(struct net_device * netdev,const u8 * addr)203 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
204 {
205 	struct ice_netdev_priv *np = netdev_priv(netdev);
206 	struct ice_vsi *vsi = np->vsi;
207 
208 	if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr,
209 				     ICE_FWD_TO_VSI))
210 		return -EINVAL;
211 
212 	return 0;
213 }
214 
215 /**
216  * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
217  * @netdev: the net device on which the unsync is happening
218  * @addr: MAC address to unsync
219  *
220  * This is a callback function which is called by the in kernel device unsync
221  * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
222  * populates the tmp_unsync_list, which is later used by ice_remove_mac to
223  * delete the MAC filters from the hardware.
224  */
ice_add_mac_to_unsync_list(struct net_device * netdev,const u8 * addr)225 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
226 {
227 	struct ice_netdev_priv *np = netdev_priv(netdev);
228 	struct ice_vsi *vsi = np->vsi;
229 
230 	/* Under some circumstances, we might receive a request to delete our
231 	 * own device address from our uc list. Because we store the device
232 	 * address in the VSI's MAC filter list, we need to ignore such
233 	 * requests and not delete our device address from this list.
234 	 */
235 	if (ether_addr_equal(addr, netdev->dev_addr))
236 		return 0;
237 
238 	if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr,
239 				     ICE_FWD_TO_VSI))
240 		return -EINVAL;
241 
242 	return 0;
243 }
244 
245 /**
246  * ice_vsi_fltr_changed - check if filter state changed
247  * @vsi: VSI to be checked
248  *
249  * returns true if filter state has changed, false otherwise.
250  */
ice_vsi_fltr_changed(struct ice_vsi * vsi)251 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
252 {
253 	return test_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state) ||
254 	       test_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
255 }
256 
257 /**
258  * ice_set_promisc - Enable promiscuous mode for a given PF
259  * @vsi: the VSI being configured
260  * @promisc_m: mask of promiscuous config bits
261  *
262  */
ice_set_promisc(struct ice_vsi * vsi,u8 promisc_m)263 static int ice_set_promisc(struct ice_vsi *vsi, u8 promisc_m)
264 {
265 	int status;
266 
267 	if (vsi->type != ICE_VSI_PF)
268 		return 0;
269 
270 	if (ice_vsi_has_non_zero_vlans(vsi)) {
271 		promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
272 		status = ice_fltr_set_vlan_vsi_promisc(&vsi->back->hw, vsi,
273 						       promisc_m);
274 	} else {
275 		status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
276 						  promisc_m, 0);
277 	}
278 	if (status && status != -EEXIST)
279 		return status;
280 
281 	netdev_dbg(vsi->netdev, "set promisc filter bits for VSI %i: 0x%x\n",
282 		   vsi->vsi_num, promisc_m);
283 	return 0;
284 }
285 
286 /**
287  * ice_clear_promisc - Disable promiscuous mode for a given PF
288  * @vsi: the VSI being configured
289  * @promisc_m: mask of promiscuous config bits
290  *
291  */
ice_clear_promisc(struct ice_vsi * vsi,u8 promisc_m)292 static int ice_clear_promisc(struct ice_vsi *vsi, u8 promisc_m)
293 {
294 	int status;
295 
296 	if (vsi->type != ICE_VSI_PF)
297 		return 0;
298 
299 	if (ice_vsi_has_non_zero_vlans(vsi)) {
300 		promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
301 		status = ice_fltr_clear_vlan_vsi_promisc(&vsi->back->hw, vsi,
302 							 promisc_m);
303 	} else {
304 		status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
305 						    promisc_m, 0);
306 	}
307 
308 	netdev_dbg(vsi->netdev, "clear promisc filter bits for VSI %i: 0x%x\n",
309 		   vsi->vsi_num, promisc_m);
310 	return status;
311 }
312 
313 /**
314  * ice_vsi_sync_fltr - Update the VSI filter list to the HW
315  * @vsi: ptr to the VSI
316  *
317  * Push any outstanding VSI filter changes through the AdminQ.
318  */
ice_vsi_sync_fltr(struct ice_vsi * vsi)319 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
320 {
321 	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
322 	struct device *dev = ice_pf_to_dev(vsi->back);
323 	struct net_device *netdev = vsi->netdev;
324 	bool promisc_forced_on = false;
325 	struct ice_pf *pf = vsi->back;
326 	struct ice_hw *hw = &pf->hw;
327 	u32 changed_flags = 0;
328 	int err;
329 
330 	if (!vsi->netdev)
331 		return -EINVAL;
332 
333 	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
334 		usleep_range(1000, 2000);
335 
336 	changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
337 	vsi->current_netdev_flags = vsi->netdev->flags;
338 
339 	INIT_LIST_HEAD(&vsi->tmp_sync_list);
340 	INIT_LIST_HEAD(&vsi->tmp_unsync_list);
341 
342 	if (ice_vsi_fltr_changed(vsi)) {
343 		clear_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
344 		clear_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
345 
346 		/* grab the netdev's addr_list_lock */
347 		netif_addr_lock_bh(netdev);
348 		__dev_uc_sync(netdev, ice_add_mac_to_sync_list,
349 			      ice_add_mac_to_unsync_list);
350 		__dev_mc_sync(netdev, ice_add_mac_to_sync_list,
351 			      ice_add_mac_to_unsync_list);
352 		/* our temp lists are populated. release lock */
353 		netif_addr_unlock_bh(netdev);
354 	}
355 
356 	/* Remove MAC addresses in the unsync list */
357 	err = ice_fltr_remove_mac_list(vsi, &vsi->tmp_unsync_list);
358 	ice_fltr_free_list(dev, &vsi->tmp_unsync_list);
359 	if (err) {
360 		netdev_err(netdev, "Failed to delete MAC filters\n");
361 		/* if we failed because of alloc failures, just bail */
362 		if (err == -ENOMEM)
363 			goto out;
364 	}
365 
366 	/* Add MAC addresses in the sync list */
367 	err = ice_fltr_add_mac_list(vsi, &vsi->tmp_sync_list);
368 	ice_fltr_free_list(dev, &vsi->tmp_sync_list);
369 	/* If filter is added successfully or already exists, do not go into
370 	 * 'if' condition and report it as error. Instead continue processing
371 	 * rest of the function.
372 	 */
373 	if (err && err != -EEXIST) {
374 		netdev_err(netdev, "Failed to add MAC filters\n");
375 		/* If there is no more space for new umac filters, VSI
376 		 * should go into promiscuous mode. There should be some
377 		 * space reserved for promiscuous filters.
378 		 */
379 		if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
380 		    !test_and_set_bit(ICE_FLTR_OVERFLOW_PROMISC,
381 				      vsi->state)) {
382 			promisc_forced_on = true;
383 			netdev_warn(netdev, "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
384 				    vsi->vsi_num);
385 		} else {
386 			goto out;
387 		}
388 	}
389 	err = 0;
390 	/* check for changes in promiscuous modes */
391 	if (changed_flags & IFF_ALLMULTI) {
392 		if (vsi->current_netdev_flags & IFF_ALLMULTI) {
393 			err = ice_set_promisc(vsi, ICE_MCAST_PROMISC_BITS);
394 			if (err) {
395 				vsi->current_netdev_flags &= ~IFF_ALLMULTI;
396 				goto out_promisc;
397 			}
398 		} else {
399 			/* !(vsi->current_netdev_flags & IFF_ALLMULTI) */
400 			err = ice_clear_promisc(vsi, ICE_MCAST_PROMISC_BITS);
401 			if (err) {
402 				vsi->current_netdev_flags |= IFF_ALLMULTI;
403 				goto out_promisc;
404 			}
405 		}
406 	}
407 
408 	if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
409 	    test_bit(ICE_VSI_PROMISC_CHANGED, vsi->state)) {
410 		clear_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
411 		if (vsi->current_netdev_flags & IFF_PROMISC) {
412 			/* Apply Rx filter rule to get traffic from wire */
413 			if (!ice_is_dflt_vsi_in_use(vsi->port_info)) {
414 				err = ice_set_dflt_vsi(vsi);
415 				if (err && err != -EEXIST) {
416 					netdev_err(netdev, "Error %d setting default VSI %i Rx rule\n",
417 						   err, vsi->vsi_num);
418 					vsi->current_netdev_flags &=
419 						~IFF_PROMISC;
420 					goto out_promisc;
421 				}
422 				err = 0;
423 				vlan_ops->dis_rx_filtering(vsi);
424 
425 				/* promiscuous mode implies allmulticast so
426 				 * that VSIs that are in promiscuous mode are
427 				 * subscribed to multicast packets coming to
428 				 * the port
429 				 */
430 				err = ice_set_promisc(vsi,
431 						      ICE_MCAST_PROMISC_BITS);
432 				if (err)
433 					goto out_promisc;
434 			}
435 		} else {
436 			/* Clear Rx filter to remove traffic from wire */
437 			if (ice_is_vsi_dflt_vsi(vsi)) {
438 				err = ice_clear_dflt_vsi(vsi);
439 				if (err) {
440 					netdev_err(netdev, "Error %d clearing default VSI %i Rx rule\n",
441 						   err, vsi->vsi_num);
442 					vsi->current_netdev_flags |=
443 						IFF_PROMISC;
444 					goto out_promisc;
445 				}
446 				if (vsi->netdev->features &
447 				    NETIF_F_HW_VLAN_CTAG_FILTER)
448 					vlan_ops->ena_rx_filtering(vsi);
449 			}
450 
451 			/* disable allmulti here, but only if allmulti is not
452 			 * still enabled for the netdev
453 			 */
454 			if (!(vsi->current_netdev_flags & IFF_ALLMULTI)) {
455 				err = ice_clear_promisc(vsi,
456 							ICE_MCAST_PROMISC_BITS);
457 				if (err) {
458 					netdev_err(netdev, "Error %d clearing multicast promiscuous on VSI %i\n",
459 						   err, vsi->vsi_num);
460 				}
461 			}
462 		}
463 	}
464 	goto exit;
465 
466 out_promisc:
467 	set_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
468 	goto exit;
469 out:
470 	/* if something went wrong then set the changed flag so we try again */
471 	set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
472 	set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
473 exit:
474 	clear_bit(ICE_CFG_BUSY, vsi->state);
475 	return err;
476 }
477 
478 /**
479  * ice_sync_fltr_subtask - Sync the VSI filter list with HW
480  * @pf: board private structure
481  */
ice_sync_fltr_subtask(struct ice_pf * pf)482 static void ice_sync_fltr_subtask(struct ice_pf *pf)
483 {
484 	int v;
485 
486 	if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
487 		return;
488 
489 	clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
490 
491 	ice_for_each_vsi(pf, v)
492 		if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
493 		    ice_vsi_sync_fltr(pf->vsi[v])) {
494 			/* come back and try again later */
495 			set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
496 			break;
497 		}
498 }
499 
500 /**
501  * ice_pf_dis_all_vsi - Pause all VSIs on a PF
502  * @pf: the PF
503  * @locked: is the rtnl_lock already held
504  */
ice_pf_dis_all_vsi(struct ice_pf * pf,bool locked)505 static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
506 {
507 	int node;
508 	int v;
509 
510 	ice_for_each_vsi(pf, v)
511 		if (pf->vsi[v])
512 			ice_dis_vsi(pf->vsi[v], locked);
513 
514 	for (node = 0; node < ICE_MAX_PF_AGG_NODES; node++)
515 		pf->pf_agg_node[node].num_vsis = 0;
516 
517 	for (node = 0; node < ICE_MAX_VF_AGG_NODES; node++)
518 		pf->vf_agg_node[node].num_vsis = 0;
519 }
520 
521 /**
522  * ice_prepare_for_reset - prep for reset
523  * @pf: board private structure
524  * @reset_type: reset type requested
525  *
526  * Inform or close all dependent features in prep for reset.
527  */
528 static void
ice_prepare_for_reset(struct ice_pf * pf,enum ice_reset_req reset_type)529 ice_prepare_for_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
530 {
531 	struct ice_hw *hw = &pf->hw;
532 	struct ice_vsi *vsi;
533 	struct ice_vf *vf;
534 	unsigned int bkt;
535 
536 	dev_dbg(ice_pf_to_dev(pf), "reset_type=%d\n", reset_type);
537 
538 	/* already prepared for reset */
539 	if (test_bit(ICE_PREPARED_FOR_RESET, pf->state))
540 		return;
541 
542 	synchronize_irq(pf->oicr_irq.virq);
543 
544 	ice_unplug_aux_dev(pf);
545 
546 	/* Notify VFs of impending reset */
547 	if (ice_check_sq_alive(hw, &hw->mailboxq))
548 		ice_vc_notify_reset(pf);
549 
550 	/* Disable VFs until reset is completed */
551 	mutex_lock(&pf->vfs.table_lock);
552 	ice_for_each_vf(pf, bkt, vf)
553 		ice_set_vf_state_dis(vf);
554 	mutex_unlock(&pf->vfs.table_lock);
555 
556 	if (ice_is_eswitch_mode_switchdev(pf)) {
557 		rtnl_lock();
558 		ice_eswitch_br_fdb_flush(pf->eswitch.br_offloads->bridge);
559 		rtnl_unlock();
560 	}
561 
562 	/* release ADQ specific HW and SW resources */
563 	vsi = ice_get_main_vsi(pf);
564 	if (!vsi)
565 		goto skip;
566 
567 	/* to be on safe side, reset orig_rss_size so that normal flow
568 	 * of deciding rss_size can take precedence
569 	 */
570 	vsi->orig_rss_size = 0;
571 
572 	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
573 		if (reset_type == ICE_RESET_PFR) {
574 			vsi->old_ena_tc = vsi->all_enatc;
575 			vsi->old_numtc = vsi->all_numtc;
576 		} else {
577 			ice_remove_q_channels(vsi, true);
578 
579 			/* for other reset type, do not support channel rebuild
580 			 * hence reset needed info
581 			 */
582 			vsi->old_ena_tc = 0;
583 			vsi->all_enatc = 0;
584 			vsi->old_numtc = 0;
585 			vsi->all_numtc = 0;
586 			vsi->req_txq = 0;
587 			vsi->req_rxq = 0;
588 			clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
589 			memset(&vsi->mqprio_qopt, 0, sizeof(vsi->mqprio_qopt));
590 		}
591 	}
592 
593 	if (vsi->netdev)
594 		netif_device_detach(vsi->netdev);
595 skip:
596 
597 	/* clear SW filtering DB */
598 	ice_clear_hw_tbls(hw);
599 	/* disable the VSIs and their queues that are not already DOWN */
600 	set_bit(ICE_VSI_REBUILD_PENDING, ice_get_main_vsi(pf)->state);
601 	ice_pf_dis_all_vsi(pf, false);
602 
603 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
604 		ice_ptp_prepare_for_reset(pf);
605 
606 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
607 		ice_gnss_exit(pf);
608 
609 	if (hw->port_info)
610 		ice_sched_clear_port(hw->port_info);
611 
612 	ice_shutdown_all_ctrlq(hw);
613 
614 	set_bit(ICE_PREPARED_FOR_RESET, pf->state);
615 }
616 
617 /**
618  * ice_do_reset - Initiate one of many types of resets
619  * @pf: board private structure
620  * @reset_type: reset type requested before this function was called.
621  */
ice_do_reset(struct ice_pf * pf,enum ice_reset_req reset_type)622 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
623 {
624 	struct device *dev = ice_pf_to_dev(pf);
625 	struct ice_hw *hw = &pf->hw;
626 
627 	dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
628 
629 	if (pf->lag && pf->lag->bonded && reset_type == ICE_RESET_PFR) {
630 		dev_dbg(dev, "PFR on a bonded interface, promoting to CORER\n");
631 		reset_type = ICE_RESET_CORER;
632 	}
633 
634 	ice_prepare_for_reset(pf, reset_type);
635 
636 	/* trigger the reset */
637 	if (ice_reset(hw, reset_type)) {
638 		dev_err(dev, "reset %d failed\n", reset_type);
639 		set_bit(ICE_RESET_FAILED, pf->state);
640 		clear_bit(ICE_RESET_OICR_RECV, pf->state);
641 		clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
642 		clear_bit(ICE_PFR_REQ, pf->state);
643 		clear_bit(ICE_CORER_REQ, pf->state);
644 		clear_bit(ICE_GLOBR_REQ, pf->state);
645 		wake_up(&pf->reset_wait_queue);
646 		return;
647 	}
648 
649 	/* PFR is a bit of a special case because it doesn't result in an OICR
650 	 * interrupt. So for PFR, rebuild after the reset and clear the reset-
651 	 * associated state bits.
652 	 */
653 	if (reset_type == ICE_RESET_PFR) {
654 		pf->pfr_count++;
655 		ice_rebuild(pf, reset_type);
656 		clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
657 		clear_bit(ICE_PFR_REQ, pf->state);
658 		wake_up(&pf->reset_wait_queue);
659 		ice_reset_all_vfs(pf);
660 	}
661 }
662 
663 /**
664  * ice_reset_subtask - Set up for resetting the device and driver
665  * @pf: board private structure
666  */
ice_reset_subtask(struct ice_pf * pf)667 static void ice_reset_subtask(struct ice_pf *pf)
668 {
669 	enum ice_reset_req reset_type = ICE_RESET_INVAL;
670 
671 	/* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
672 	 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
673 	 * of reset is pending and sets bits in pf->state indicating the reset
674 	 * type and ICE_RESET_OICR_RECV. So, if the latter bit is set
675 	 * prepare for pending reset if not already (for PF software-initiated
676 	 * global resets the software should already be prepared for it as
677 	 * indicated by ICE_PREPARED_FOR_RESET; for global resets initiated
678 	 * by firmware or software on other PFs, that bit is not set so prepare
679 	 * for the reset now), poll for reset done, rebuild and return.
680 	 */
681 	if (test_bit(ICE_RESET_OICR_RECV, pf->state)) {
682 		/* Perform the largest reset requested */
683 		if (test_and_clear_bit(ICE_CORER_RECV, pf->state))
684 			reset_type = ICE_RESET_CORER;
685 		if (test_and_clear_bit(ICE_GLOBR_RECV, pf->state))
686 			reset_type = ICE_RESET_GLOBR;
687 		if (test_and_clear_bit(ICE_EMPR_RECV, pf->state))
688 			reset_type = ICE_RESET_EMPR;
689 		/* return if no valid reset type requested */
690 		if (reset_type == ICE_RESET_INVAL)
691 			return;
692 		ice_prepare_for_reset(pf, reset_type);
693 
694 		/* make sure we are ready to rebuild */
695 		if (ice_check_reset(&pf->hw)) {
696 			set_bit(ICE_RESET_FAILED, pf->state);
697 		} else {
698 			/* done with reset. start rebuild */
699 			pf->hw.reset_ongoing = false;
700 			ice_rebuild(pf, reset_type);
701 			/* clear bit to resume normal operations, but
702 			 * ICE_NEEDS_RESTART bit is set in case rebuild failed
703 			 */
704 			clear_bit(ICE_RESET_OICR_RECV, pf->state);
705 			clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
706 			clear_bit(ICE_PFR_REQ, pf->state);
707 			clear_bit(ICE_CORER_REQ, pf->state);
708 			clear_bit(ICE_GLOBR_REQ, pf->state);
709 			wake_up(&pf->reset_wait_queue);
710 			ice_reset_all_vfs(pf);
711 		}
712 
713 		return;
714 	}
715 
716 	/* No pending resets to finish processing. Check for new resets */
717 	if (test_bit(ICE_PFR_REQ, pf->state)) {
718 		reset_type = ICE_RESET_PFR;
719 		if (pf->lag && pf->lag->bonded) {
720 			dev_dbg(ice_pf_to_dev(pf), "PFR on a bonded interface, promoting to CORER\n");
721 			reset_type = ICE_RESET_CORER;
722 		}
723 	}
724 	if (test_bit(ICE_CORER_REQ, pf->state))
725 		reset_type = ICE_RESET_CORER;
726 	if (test_bit(ICE_GLOBR_REQ, pf->state))
727 		reset_type = ICE_RESET_GLOBR;
728 	/* If no valid reset type requested just return */
729 	if (reset_type == ICE_RESET_INVAL)
730 		return;
731 
732 	/* reset if not already down or busy */
733 	if (!test_bit(ICE_DOWN, pf->state) &&
734 	    !test_bit(ICE_CFG_BUSY, pf->state)) {
735 		ice_do_reset(pf, reset_type);
736 	}
737 }
738 
739 /**
740  * ice_print_topo_conflict - print topology conflict message
741  * @vsi: the VSI whose topology status is being checked
742  */
ice_print_topo_conflict(struct ice_vsi * vsi)743 static void ice_print_topo_conflict(struct ice_vsi *vsi)
744 {
745 	switch (vsi->port_info->phy.link_info.topo_media_conflict) {
746 	case ICE_AQ_LINK_TOPO_CONFLICT:
747 	case ICE_AQ_LINK_MEDIA_CONFLICT:
748 	case ICE_AQ_LINK_TOPO_UNREACH_PRT:
749 	case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT:
750 	case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA:
751 		netdev_info(vsi->netdev, "Potential misconfiguration of the Ethernet port detected. If it was not intended, please use the Intel (R) Ethernet Port Configuration Tool to address the issue.\n");
752 		break;
753 	case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA:
754 		if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, vsi->back->flags))
755 			netdev_warn(vsi->netdev, "An unsupported module type was detected. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules\n");
756 		else
757 			netdev_err(vsi->netdev, "Rx/Tx is disabled on this device because an unsupported module type was detected. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
758 		break;
759 	default:
760 		break;
761 	}
762 }
763 
764 /**
765  * ice_print_link_msg - print link up or down message
766  * @vsi: the VSI whose link status is being queried
767  * @isup: boolean for if the link is now up or down
768  */
ice_print_link_msg(struct ice_vsi * vsi,bool isup)769 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
770 {
771 	struct ice_aqc_get_phy_caps_data *caps;
772 	const char *an_advertised;
773 	const char *fec_req;
774 	const char *speed;
775 	const char *fec;
776 	const char *fc;
777 	const char *an;
778 	int status;
779 
780 	if (!vsi)
781 		return;
782 
783 	if (vsi->current_isup == isup)
784 		return;
785 
786 	vsi->current_isup = isup;
787 
788 	if (!isup) {
789 		netdev_info(vsi->netdev, "NIC Link is Down\n");
790 		return;
791 	}
792 
793 	switch (vsi->port_info->phy.link_info.link_speed) {
794 	case ICE_AQ_LINK_SPEED_100GB:
795 		speed = "100 G";
796 		break;
797 	case ICE_AQ_LINK_SPEED_50GB:
798 		speed = "50 G";
799 		break;
800 	case ICE_AQ_LINK_SPEED_40GB:
801 		speed = "40 G";
802 		break;
803 	case ICE_AQ_LINK_SPEED_25GB:
804 		speed = "25 G";
805 		break;
806 	case ICE_AQ_LINK_SPEED_20GB:
807 		speed = "20 G";
808 		break;
809 	case ICE_AQ_LINK_SPEED_10GB:
810 		speed = "10 G";
811 		break;
812 	case ICE_AQ_LINK_SPEED_5GB:
813 		speed = "5 G";
814 		break;
815 	case ICE_AQ_LINK_SPEED_2500MB:
816 		speed = "2.5 G";
817 		break;
818 	case ICE_AQ_LINK_SPEED_1000MB:
819 		speed = "1 G";
820 		break;
821 	case ICE_AQ_LINK_SPEED_100MB:
822 		speed = "100 M";
823 		break;
824 	default:
825 		speed = "Unknown ";
826 		break;
827 	}
828 
829 	switch (vsi->port_info->fc.current_mode) {
830 	case ICE_FC_FULL:
831 		fc = "Rx/Tx";
832 		break;
833 	case ICE_FC_TX_PAUSE:
834 		fc = "Tx";
835 		break;
836 	case ICE_FC_RX_PAUSE:
837 		fc = "Rx";
838 		break;
839 	case ICE_FC_NONE:
840 		fc = "None";
841 		break;
842 	default:
843 		fc = "Unknown";
844 		break;
845 	}
846 
847 	/* Get FEC mode based on negotiated link info */
848 	switch (vsi->port_info->phy.link_info.fec_info) {
849 	case ICE_AQ_LINK_25G_RS_528_FEC_EN:
850 	case ICE_AQ_LINK_25G_RS_544_FEC_EN:
851 		fec = "RS-FEC";
852 		break;
853 	case ICE_AQ_LINK_25G_KR_FEC_EN:
854 		fec = "FC-FEC/BASE-R";
855 		break;
856 	default:
857 		fec = "NONE";
858 		break;
859 	}
860 
861 	/* check if autoneg completed, might be false due to not supported */
862 	if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
863 		an = "True";
864 	else
865 		an = "False";
866 
867 	/* Get FEC mode requested based on PHY caps last SW configuration */
868 	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
869 	if (!caps) {
870 		fec_req = "Unknown";
871 		an_advertised = "Unknown";
872 		goto done;
873 	}
874 
875 	status = ice_aq_get_phy_caps(vsi->port_info, false,
876 				     ICE_AQC_REPORT_ACTIVE_CFG, caps, NULL);
877 	if (status)
878 		netdev_info(vsi->netdev, "Get phy capability failed.\n");
879 
880 	an_advertised = ice_is_phy_caps_an_enabled(caps) ? "On" : "Off";
881 
882 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
883 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
884 		fec_req = "RS-FEC";
885 	else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
886 		 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
887 		fec_req = "FC-FEC/BASE-R";
888 	else
889 		fec_req = "NONE";
890 
891 	kfree(caps);
892 
893 done:
894 	netdev_info(vsi->netdev, "NIC Link is up %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg Advertised: %s, Autoneg Negotiated: %s, Flow Control: %s\n",
895 		    speed, fec_req, fec, an_advertised, an, fc);
896 	ice_print_topo_conflict(vsi);
897 }
898 
899 /**
900  * ice_vsi_link_event - update the VSI's netdev
901  * @vsi: the VSI on which the link event occurred
902  * @link_up: whether or not the VSI needs to be set up or down
903  */
ice_vsi_link_event(struct ice_vsi * vsi,bool link_up)904 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
905 {
906 	if (!vsi)
907 		return;
908 
909 	if (test_bit(ICE_VSI_DOWN, vsi->state) || !vsi->netdev)
910 		return;
911 
912 	if (vsi->type == ICE_VSI_PF) {
913 		if (link_up == netif_carrier_ok(vsi->netdev))
914 			return;
915 
916 		if (link_up) {
917 			netif_carrier_on(vsi->netdev);
918 			netif_tx_wake_all_queues(vsi->netdev);
919 		} else {
920 			netif_carrier_off(vsi->netdev);
921 			netif_tx_stop_all_queues(vsi->netdev);
922 		}
923 	}
924 }
925 
926 /**
927  * ice_set_dflt_mib - send a default config MIB to the FW
928  * @pf: private PF struct
929  *
930  * This function sends a default configuration MIB to the FW.
931  *
932  * If this function errors out at any point, the driver is still able to
933  * function.  The main impact is that LFC may not operate as expected.
934  * Therefore an error state in this function should be treated with a DBG
935  * message and continue on with driver rebuild/reenable.
936  */
ice_set_dflt_mib(struct ice_pf * pf)937 static void ice_set_dflt_mib(struct ice_pf *pf)
938 {
939 	struct device *dev = ice_pf_to_dev(pf);
940 	u8 mib_type, *buf, *lldpmib = NULL;
941 	u16 len, typelen, offset = 0;
942 	struct ice_lldp_org_tlv *tlv;
943 	struct ice_hw *hw = &pf->hw;
944 	u32 ouisubtype;
945 
946 	mib_type = SET_LOCAL_MIB_TYPE_LOCAL_MIB;
947 	lldpmib = kzalloc(ICE_LLDPDU_SIZE, GFP_KERNEL);
948 	if (!lldpmib) {
949 		dev_dbg(dev, "%s Failed to allocate MIB memory\n",
950 			__func__);
951 		return;
952 	}
953 
954 	/* Add ETS CFG TLV */
955 	tlv = (struct ice_lldp_org_tlv *)lldpmib;
956 	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
957 		   ICE_IEEE_ETS_TLV_LEN);
958 	tlv->typelen = htons(typelen);
959 	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
960 		      ICE_IEEE_SUBTYPE_ETS_CFG);
961 	tlv->ouisubtype = htonl(ouisubtype);
962 
963 	buf = tlv->tlvinfo;
964 	buf[0] = 0;
965 
966 	/* ETS CFG all UPs map to TC 0. Next 4 (1 - 4) Octets = 0.
967 	 * Octets 5 - 12 are BW values, set octet 5 to 100% BW.
968 	 * Octets 13 - 20 are TSA values - leave as zeros
969 	 */
970 	buf[5] = 0x64;
971 	len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
972 	offset += len + 2;
973 	tlv = (struct ice_lldp_org_tlv *)
974 		((char *)tlv + sizeof(tlv->typelen) + len);
975 
976 	/* Add ETS REC TLV */
977 	buf = tlv->tlvinfo;
978 	tlv->typelen = htons(typelen);
979 
980 	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
981 		      ICE_IEEE_SUBTYPE_ETS_REC);
982 	tlv->ouisubtype = htonl(ouisubtype);
983 
984 	/* First octet of buf is reserved
985 	 * Octets 1 - 4 map UP to TC - all UPs map to zero
986 	 * Octets 5 - 12 are BW values - set TC 0 to 100%.
987 	 * Octets 13 - 20 are TSA value - leave as zeros
988 	 */
989 	buf[5] = 0x64;
990 	offset += len + 2;
991 	tlv = (struct ice_lldp_org_tlv *)
992 		((char *)tlv + sizeof(tlv->typelen) + len);
993 
994 	/* Add PFC CFG TLV */
995 	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
996 		   ICE_IEEE_PFC_TLV_LEN);
997 	tlv->typelen = htons(typelen);
998 
999 	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
1000 		      ICE_IEEE_SUBTYPE_PFC_CFG);
1001 	tlv->ouisubtype = htonl(ouisubtype);
1002 
1003 	/* Octet 1 left as all zeros - PFC disabled */
1004 	buf[0] = 0x08;
1005 	len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
1006 	offset += len + 2;
1007 
1008 	if (ice_aq_set_lldp_mib(hw, mib_type, (void *)lldpmib, offset, NULL))
1009 		dev_dbg(dev, "%s Failed to set default LLDP MIB\n", __func__);
1010 
1011 	kfree(lldpmib);
1012 }
1013 
1014 /**
1015  * ice_check_phy_fw_load - check if PHY FW load failed
1016  * @pf: pointer to PF struct
1017  * @link_cfg_err: bitmap from the link info structure
1018  *
1019  * check if external PHY FW load failed and print an error message if it did
1020  */
ice_check_phy_fw_load(struct ice_pf * pf,u8 link_cfg_err)1021 static void ice_check_phy_fw_load(struct ice_pf *pf, u8 link_cfg_err)
1022 {
1023 	if (!(link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE)) {
1024 		clear_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
1025 		return;
1026 	}
1027 
1028 	if (test_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags))
1029 		return;
1030 
1031 	if (link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE) {
1032 		dev_err(ice_pf_to_dev(pf), "Device failed to load the FW for the external PHY. Please download and install the latest NVM for your device and try again\n");
1033 		set_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
1034 	}
1035 }
1036 
1037 /**
1038  * ice_check_module_power
1039  * @pf: pointer to PF struct
1040  * @link_cfg_err: bitmap from the link info structure
1041  *
1042  * check module power level returned by a previous call to aq_get_link_info
1043  * and print error messages if module power level is not supported
1044  */
ice_check_module_power(struct ice_pf * pf,u8 link_cfg_err)1045 static void ice_check_module_power(struct ice_pf *pf, u8 link_cfg_err)
1046 {
1047 	/* if module power level is supported, clear the flag */
1048 	if (!(link_cfg_err & (ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT |
1049 			      ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED))) {
1050 		clear_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1051 		return;
1052 	}
1053 
1054 	/* if ICE_FLAG_MOD_POWER_UNSUPPORTED was previously set and the
1055 	 * above block didn't clear this bit, there's nothing to do
1056 	 */
1057 	if (test_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags))
1058 		return;
1059 
1060 	if (link_cfg_err & ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT) {
1061 		dev_err(ice_pf_to_dev(pf), "The installed module is incompatible with the device's NVM image. Cannot start link\n");
1062 		set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1063 	} else if (link_cfg_err & ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED) {
1064 		dev_err(ice_pf_to_dev(pf), "The module's power requirements exceed the device's power supply. Cannot start link\n");
1065 		set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1066 	}
1067 }
1068 
1069 /**
1070  * ice_check_link_cfg_err - check if link configuration failed
1071  * @pf: pointer to the PF struct
1072  * @link_cfg_err: bitmap from the link info structure
1073  *
1074  * print if any link configuration failure happens due to the value in the
1075  * link_cfg_err parameter in the link info structure
1076  */
ice_check_link_cfg_err(struct ice_pf * pf,u8 link_cfg_err)1077 static void ice_check_link_cfg_err(struct ice_pf *pf, u8 link_cfg_err)
1078 {
1079 	ice_check_module_power(pf, link_cfg_err);
1080 	ice_check_phy_fw_load(pf, link_cfg_err);
1081 }
1082 
1083 /**
1084  * ice_link_event - process the link event
1085  * @pf: PF that the link event is associated with
1086  * @pi: port_info for the port that the link event is associated with
1087  * @link_up: true if the physical link is up and false if it is down
1088  * @link_speed: current link speed received from the link event
1089  *
1090  * Returns 0 on success and negative on failure
1091  */
1092 static int
ice_link_event(struct ice_pf * pf,struct ice_port_info * pi,bool link_up,u16 link_speed)1093 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
1094 	       u16 link_speed)
1095 {
1096 	struct device *dev = ice_pf_to_dev(pf);
1097 	struct ice_phy_info *phy_info;
1098 	struct ice_vsi *vsi;
1099 	u16 old_link_speed;
1100 	bool old_link;
1101 	int status;
1102 
1103 	phy_info = &pi->phy;
1104 	phy_info->link_info_old = phy_info->link_info;
1105 
1106 	old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
1107 	old_link_speed = phy_info->link_info_old.link_speed;
1108 
1109 	/* update the link info structures and re-enable link events,
1110 	 * don't bail on failure due to other book keeping needed
1111 	 */
1112 	status = ice_update_link_info(pi);
1113 	if (status)
1114 		dev_dbg(dev, "Failed to update link status on port %d, err %d aq_err %s\n",
1115 			pi->lport, status,
1116 			ice_aq_str(pi->hw->adminq.sq_last_status));
1117 
1118 	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
1119 
1120 	/* Check if the link state is up after updating link info, and treat
1121 	 * this event as an UP event since the link is actually UP now.
1122 	 */
1123 	if (phy_info->link_info.link_info & ICE_AQ_LINK_UP)
1124 		link_up = true;
1125 
1126 	vsi = ice_get_main_vsi(pf);
1127 	if (!vsi || !vsi->port_info)
1128 		return -EINVAL;
1129 
1130 	/* turn off PHY if media was removed */
1131 	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
1132 	    !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
1133 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
1134 		ice_set_link(vsi, false);
1135 	}
1136 
1137 	/* if the old link up/down and speed is the same as the new */
1138 	if (link_up == old_link && link_speed == old_link_speed)
1139 		return 0;
1140 
1141 	ice_ptp_link_change(pf, pf->hw.pf_id, link_up);
1142 
1143 	if (ice_is_dcb_active(pf)) {
1144 		if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
1145 			ice_dcb_rebuild(pf);
1146 	} else {
1147 		if (link_up)
1148 			ice_set_dflt_mib(pf);
1149 	}
1150 	ice_vsi_link_event(vsi, link_up);
1151 	ice_print_link_msg(vsi, link_up);
1152 
1153 	ice_vc_notify_link_state(pf);
1154 
1155 	return 0;
1156 }
1157 
1158 /**
1159  * ice_watchdog_subtask - periodic tasks not using event driven scheduling
1160  * @pf: board private structure
1161  */
ice_watchdog_subtask(struct ice_pf * pf)1162 static void ice_watchdog_subtask(struct ice_pf *pf)
1163 {
1164 	int i;
1165 
1166 	/* if interface is down do nothing */
1167 	if (test_bit(ICE_DOWN, pf->state) ||
1168 	    test_bit(ICE_CFG_BUSY, pf->state))
1169 		return;
1170 
1171 	/* make sure we don't do these things too often */
1172 	if (time_before(jiffies,
1173 			pf->serv_tmr_prev + pf->serv_tmr_period))
1174 		return;
1175 
1176 	pf->serv_tmr_prev = jiffies;
1177 
1178 	/* Update the stats for active netdevs so the network stack
1179 	 * can look at updated numbers whenever it cares to
1180 	 */
1181 	ice_update_pf_stats(pf);
1182 	ice_for_each_vsi(pf, i)
1183 		if (pf->vsi[i] && pf->vsi[i]->netdev)
1184 			ice_update_vsi_stats(pf->vsi[i]);
1185 }
1186 
1187 /**
1188  * ice_init_link_events - enable/initialize link events
1189  * @pi: pointer to the port_info instance
1190  *
1191  * Returns -EIO on failure, 0 on success
1192  */
ice_init_link_events(struct ice_port_info * pi)1193 static int ice_init_link_events(struct ice_port_info *pi)
1194 {
1195 	u16 mask;
1196 
1197 	mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
1198 		       ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL |
1199 		       ICE_AQ_LINK_EVENT_PHY_FW_LOAD_FAIL));
1200 
1201 	if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
1202 		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to set link event mask for port %d\n",
1203 			pi->lport);
1204 		return -EIO;
1205 	}
1206 
1207 	if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
1208 		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to enable link events for port %d\n",
1209 			pi->lport);
1210 		return -EIO;
1211 	}
1212 
1213 	return 0;
1214 }
1215 
1216 /**
1217  * ice_handle_link_event - handle link event via ARQ
1218  * @pf: PF that the link event is associated with
1219  * @event: event structure containing link status info
1220  */
1221 static int
ice_handle_link_event(struct ice_pf * pf,struct ice_rq_event_info * event)1222 ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1223 {
1224 	struct ice_aqc_get_link_status_data *link_data;
1225 	struct ice_port_info *port_info;
1226 	int status;
1227 
1228 	link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
1229 	port_info = pf->hw.port_info;
1230 	if (!port_info)
1231 		return -EINVAL;
1232 
1233 	status = ice_link_event(pf, port_info,
1234 				!!(link_data->link_info & ICE_AQ_LINK_UP),
1235 				le16_to_cpu(link_data->link_speed));
1236 	if (status)
1237 		dev_dbg(ice_pf_to_dev(pf), "Could not process link event, error %d\n",
1238 			status);
1239 
1240 	return status;
1241 }
1242 
1243 /**
1244  * ice_aq_prep_for_event - Prepare to wait for an AdminQ event from firmware
1245  * @pf: pointer to the PF private structure
1246  * @task: intermediate helper storage and identifier for waiting
1247  * @opcode: the opcode to wait for
1248  *
1249  * Prepares to wait for a specific AdminQ completion event on the ARQ for
1250  * a given PF. Actual wait would be done by a call to ice_aq_wait_for_event().
1251  *
1252  * Calls are separated to allow caller registering for event before sending
1253  * the command, which mitigates a race between registering and FW responding.
1254  *
1255  * To obtain only the descriptor contents, pass an task->event with null
1256  * msg_buf. If the complete data buffer is desired, allocate the
1257  * task->event.msg_buf with enough space ahead of time.
1258  */
ice_aq_prep_for_event(struct ice_pf * pf,struct ice_aq_task * task,u16 opcode)1259 void ice_aq_prep_for_event(struct ice_pf *pf, struct ice_aq_task *task,
1260 			   u16 opcode)
1261 {
1262 	INIT_HLIST_NODE(&task->entry);
1263 	task->opcode = opcode;
1264 	task->state = ICE_AQ_TASK_WAITING;
1265 
1266 	spin_lock_bh(&pf->aq_wait_lock);
1267 	hlist_add_head(&task->entry, &pf->aq_wait_list);
1268 	spin_unlock_bh(&pf->aq_wait_lock);
1269 }
1270 
1271 /**
1272  * ice_aq_wait_for_event - Wait for an AdminQ event from firmware
1273  * @pf: pointer to the PF private structure
1274  * @task: ptr prepared by ice_aq_prep_for_event()
1275  * @timeout: how long to wait, in jiffies
1276  *
1277  * Waits for a specific AdminQ completion event on the ARQ for a given PF. The
1278  * current thread will be put to sleep until the specified event occurs or
1279  * until the given timeout is reached.
1280  *
1281  * Returns: zero on success, or a negative error code on failure.
1282  */
ice_aq_wait_for_event(struct ice_pf * pf,struct ice_aq_task * task,unsigned long timeout)1283 int ice_aq_wait_for_event(struct ice_pf *pf, struct ice_aq_task *task,
1284 			  unsigned long timeout)
1285 {
1286 	enum ice_aq_task_state *state = &task->state;
1287 	struct device *dev = ice_pf_to_dev(pf);
1288 	unsigned long start = jiffies;
1289 	long ret;
1290 	int err;
1291 
1292 	ret = wait_event_interruptible_timeout(pf->aq_wait_queue,
1293 					       *state != ICE_AQ_TASK_WAITING,
1294 					       timeout);
1295 	switch (*state) {
1296 	case ICE_AQ_TASK_NOT_PREPARED:
1297 		WARN(1, "call to %s without ice_aq_prep_for_event()", __func__);
1298 		err = -EINVAL;
1299 		break;
1300 	case ICE_AQ_TASK_WAITING:
1301 		err = ret < 0 ? ret : -ETIMEDOUT;
1302 		break;
1303 	case ICE_AQ_TASK_CANCELED:
1304 		err = ret < 0 ? ret : -ECANCELED;
1305 		break;
1306 	case ICE_AQ_TASK_COMPLETE:
1307 		err = ret < 0 ? ret : 0;
1308 		break;
1309 	default:
1310 		WARN(1, "Unexpected AdminQ wait task state %u", *state);
1311 		err = -EINVAL;
1312 		break;
1313 	}
1314 
1315 	dev_dbg(dev, "Waited %u msecs (max %u msecs) for firmware response to op 0x%04x\n",
1316 		jiffies_to_msecs(jiffies - start),
1317 		jiffies_to_msecs(timeout),
1318 		task->opcode);
1319 
1320 	spin_lock_bh(&pf->aq_wait_lock);
1321 	hlist_del(&task->entry);
1322 	spin_unlock_bh(&pf->aq_wait_lock);
1323 
1324 	return err;
1325 }
1326 
1327 /**
1328  * ice_aq_check_events - Check if any thread is waiting for an AdminQ event
1329  * @pf: pointer to the PF private structure
1330  * @opcode: the opcode of the event
1331  * @event: the event to check
1332  *
1333  * Loops over the current list of pending threads waiting for an AdminQ event.
1334  * For each matching task, copy the contents of the event into the task
1335  * structure and wake up the thread.
1336  *
1337  * If multiple threads wait for the same opcode, they will all be woken up.
1338  *
1339  * Note that event->msg_buf will only be duplicated if the event has a buffer
1340  * with enough space already allocated. Otherwise, only the descriptor and
1341  * message length will be copied.
1342  *
1343  * Returns: true if an event was found, false otherwise
1344  */
ice_aq_check_events(struct ice_pf * pf,u16 opcode,struct ice_rq_event_info * event)1345 static void ice_aq_check_events(struct ice_pf *pf, u16 opcode,
1346 				struct ice_rq_event_info *event)
1347 {
1348 	struct ice_rq_event_info *task_ev;
1349 	struct ice_aq_task *task;
1350 	bool found = false;
1351 
1352 	spin_lock_bh(&pf->aq_wait_lock);
1353 	hlist_for_each_entry(task, &pf->aq_wait_list, entry) {
1354 		if (task->state != ICE_AQ_TASK_WAITING)
1355 			continue;
1356 		if (task->opcode != opcode)
1357 			continue;
1358 
1359 		task_ev = &task->event;
1360 		memcpy(&task_ev->desc, &event->desc, sizeof(event->desc));
1361 		task_ev->msg_len = event->msg_len;
1362 
1363 		/* Only copy the data buffer if a destination was set */
1364 		if (task_ev->msg_buf && task_ev->buf_len >= event->buf_len) {
1365 			memcpy(task_ev->msg_buf, event->msg_buf,
1366 			       event->buf_len);
1367 			task_ev->buf_len = event->buf_len;
1368 		}
1369 
1370 		task->state = ICE_AQ_TASK_COMPLETE;
1371 		found = true;
1372 	}
1373 	spin_unlock_bh(&pf->aq_wait_lock);
1374 
1375 	if (found)
1376 		wake_up(&pf->aq_wait_queue);
1377 }
1378 
1379 /**
1380  * ice_aq_cancel_waiting_tasks - Immediately cancel all waiting tasks
1381  * @pf: the PF private structure
1382  *
1383  * Set all waiting tasks to ICE_AQ_TASK_CANCELED, and wake up their threads.
1384  * This will then cause ice_aq_wait_for_event to exit with -ECANCELED.
1385  */
ice_aq_cancel_waiting_tasks(struct ice_pf * pf)1386 static void ice_aq_cancel_waiting_tasks(struct ice_pf *pf)
1387 {
1388 	struct ice_aq_task *task;
1389 
1390 	spin_lock_bh(&pf->aq_wait_lock);
1391 	hlist_for_each_entry(task, &pf->aq_wait_list, entry)
1392 		task->state = ICE_AQ_TASK_CANCELED;
1393 	spin_unlock_bh(&pf->aq_wait_lock);
1394 
1395 	wake_up(&pf->aq_wait_queue);
1396 }
1397 
1398 #define ICE_MBX_OVERFLOW_WATERMARK 64
1399 
1400 /**
1401  * __ice_clean_ctrlq - helper function to clean controlq rings
1402  * @pf: ptr to struct ice_pf
1403  * @q_type: specific Control queue type
1404  */
__ice_clean_ctrlq(struct ice_pf * pf,enum ice_ctl_q q_type)1405 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
1406 {
1407 	struct device *dev = ice_pf_to_dev(pf);
1408 	struct ice_rq_event_info event;
1409 	struct ice_hw *hw = &pf->hw;
1410 	struct ice_ctl_q_info *cq;
1411 	u16 pending, i = 0;
1412 	const char *qtype;
1413 	u32 oldval, val;
1414 
1415 	/* Do not clean control queue if/when PF reset fails */
1416 	if (test_bit(ICE_RESET_FAILED, pf->state))
1417 		return 0;
1418 
1419 	switch (q_type) {
1420 	case ICE_CTL_Q_ADMIN:
1421 		cq = &hw->adminq;
1422 		qtype = "Admin";
1423 		break;
1424 	case ICE_CTL_Q_SB:
1425 		cq = &hw->sbq;
1426 		qtype = "Sideband";
1427 		break;
1428 	case ICE_CTL_Q_MAILBOX:
1429 		cq = &hw->mailboxq;
1430 		qtype = "Mailbox";
1431 		/* we are going to try to detect a malicious VF, so set the
1432 		 * state to begin detection
1433 		 */
1434 		hw->mbx_snapshot.mbx_buf.state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT;
1435 		break;
1436 	default:
1437 		dev_warn(dev, "Unknown control queue type 0x%x\n", q_type);
1438 		return 0;
1439 	}
1440 
1441 	/* check for error indications - PF_xx_AxQLEN register layout for
1442 	 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
1443 	 */
1444 	val = rd32(hw, cq->rq.len);
1445 	if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1446 		   PF_FW_ARQLEN_ARQCRIT_M)) {
1447 		oldval = val;
1448 		if (val & PF_FW_ARQLEN_ARQVFE_M)
1449 			dev_dbg(dev, "%s Receive Queue VF Error detected\n",
1450 				qtype);
1451 		if (val & PF_FW_ARQLEN_ARQOVFL_M) {
1452 			dev_dbg(dev, "%s Receive Queue Overflow Error detected\n",
1453 				qtype);
1454 		}
1455 		if (val & PF_FW_ARQLEN_ARQCRIT_M)
1456 			dev_dbg(dev, "%s Receive Queue Critical Error detected\n",
1457 				qtype);
1458 		val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1459 			 PF_FW_ARQLEN_ARQCRIT_M);
1460 		if (oldval != val)
1461 			wr32(hw, cq->rq.len, val);
1462 	}
1463 
1464 	val = rd32(hw, cq->sq.len);
1465 	if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1466 		   PF_FW_ATQLEN_ATQCRIT_M)) {
1467 		oldval = val;
1468 		if (val & PF_FW_ATQLEN_ATQVFE_M)
1469 			dev_dbg(dev, "%s Send Queue VF Error detected\n",
1470 				qtype);
1471 		if (val & PF_FW_ATQLEN_ATQOVFL_M) {
1472 			dev_dbg(dev, "%s Send Queue Overflow Error detected\n",
1473 				qtype);
1474 		}
1475 		if (val & PF_FW_ATQLEN_ATQCRIT_M)
1476 			dev_dbg(dev, "%s Send Queue Critical Error detected\n",
1477 				qtype);
1478 		val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1479 			 PF_FW_ATQLEN_ATQCRIT_M);
1480 		if (oldval != val)
1481 			wr32(hw, cq->sq.len, val);
1482 	}
1483 
1484 	event.buf_len = cq->rq_buf_size;
1485 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1486 	if (!event.msg_buf)
1487 		return 0;
1488 
1489 	do {
1490 		struct ice_mbx_data data = {};
1491 		u16 opcode;
1492 		int ret;
1493 
1494 		ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1495 		if (ret == -EALREADY)
1496 			break;
1497 		if (ret) {
1498 			dev_err(dev, "%s Receive Queue event error %d\n", qtype,
1499 				ret);
1500 			break;
1501 		}
1502 
1503 		opcode = le16_to_cpu(event.desc.opcode);
1504 
1505 		/* Notify any thread that might be waiting for this event */
1506 		ice_aq_check_events(pf, opcode, &event);
1507 
1508 		switch (opcode) {
1509 		case ice_aqc_opc_get_link_status:
1510 			if (ice_handle_link_event(pf, &event))
1511 				dev_err(dev, "Could not handle link event\n");
1512 			break;
1513 		case ice_aqc_opc_event_lan_overflow:
1514 			ice_vf_lan_overflow_event(pf, &event);
1515 			break;
1516 		case ice_mbx_opc_send_msg_to_pf:
1517 			if (ice_is_feature_supported(pf, ICE_F_MBX_LIMIT)) {
1518 				ice_vc_process_vf_msg(pf, &event, NULL);
1519 				ice_mbx_vf_dec_trig_e830(hw, &event);
1520 			} else {
1521 				u16 val = hw->mailboxq.num_rq_entries;
1522 
1523 				data.max_num_msgs_mbx = val;
1524 				val = ICE_MBX_OVERFLOW_WATERMARK;
1525 				data.async_watermark_val = val;
1526 				data.num_msg_proc = i;
1527 				data.num_pending_arq = pending;
1528 
1529 				ice_vc_process_vf_msg(pf, &event, &data);
1530 			}
1531 			break;
1532 		case ice_aqc_opc_fw_logging:
1533 			ice_output_fw_log(hw, &event.desc, event.msg_buf);
1534 			break;
1535 		case ice_aqc_opc_lldp_set_mib_change:
1536 			ice_dcb_process_lldp_set_mib_change(pf, &event);
1537 			break;
1538 		default:
1539 			dev_dbg(dev, "%s Receive Queue unknown event 0x%04x ignored\n",
1540 				qtype, opcode);
1541 			break;
1542 		}
1543 	} while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1544 
1545 	kfree(event.msg_buf);
1546 
1547 	return pending && (i == ICE_DFLT_IRQ_WORK);
1548 }
1549 
1550 /**
1551  * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1552  * @hw: pointer to hardware info
1553  * @cq: control queue information
1554  *
1555  * returns true if there are pending messages in a queue, false if there aren't
1556  */
ice_ctrlq_pending(struct ice_hw * hw,struct ice_ctl_q_info * cq)1557 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1558 {
1559 	u16 ntu;
1560 
1561 	ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1562 	return cq->rq.next_to_clean != ntu;
1563 }
1564 
1565 /**
1566  * ice_clean_adminq_subtask - clean the AdminQ rings
1567  * @pf: board private structure
1568  */
ice_clean_adminq_subtask(struct ice_pf * pf)1569 static void ice_clean_adminq_subtask(struct ice_pf *pf)
1570 {
1571 	struct ice_hw *hw = &pf->hw;
1572 
1573 	if (!test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
1574 		return;
1575 
1576 	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1577 		return;
1578 
1579 	clear_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
1580 
1581 	/* There might be a situation where new messages arrive to a control
1582 	 * queue between processing the last message and clearing the
1583 	 * EVENT_PENDING bit. So before exiting, check queue head again (using
1584 	 * ice_ctrlq_pending) and process new messages if any.
1585 	 */
1586 	if (ice_ctrlq_pending(hw, &hw->adminq))
1587 		__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1588 
1589 	ice_flush(hw);
1590 }
1591 
1592 /**
1593  * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1594  * @pf: board private structure
1595  */
ice_clean_mailboxq_subtask(struct ice_pf * pf)1596 static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1597 {
1598 	struct ice_hw *hw = &pf->hw;
1599 
1600 	if (!test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state))
1601 		return;
1602 
1603 	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1604 		return;
1605 
1606 	clear_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1607 
1608 	if (ice_ctrlq_pending(hw, &hw->mailboxq))
1609 		__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1610 
1611 	ice_flush(hw);
1612 }
1613 
1614 /**
1615  * ice_clean_sbq_subtask - clean the Sideband Queue rings
1616  * @pf: board private structure
1617  */
ice_clean_sbq_subtask(struct ice_pf * pf)1618 static void ice_clean_sbq_subtask(struct ice_pf *pf)
1619 {
1620 	struct ice_hw *hw = &pf->hw;
1621 
1622 	/* Nothing to do here if sideband queue is not supported */
1623 	if (!ice_is_sbq_supported(hw)) {
1624 		clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1625 		return;
1626 	}
1627 
1628 	if (!test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state))
1629 		return;
1630 
1631 	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_SB))
1632 		return;
1633 
1634 	clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1635 
1636 	if (ice_ctrlq_pending(hw, &hw->sbq))
1637 		__ice_clean_ctrlq(pf, ICE_CTL_Q_SB);
1638 
1639 	ice_flush(hw);
1640 }
1641 
1642 /**
1643  * ice_service_task_schedule - schedule the service task to wake up
1644  * @pf: board private structure
1645  *
1646  * If not already scheduled, this puts the task into the work queue.
1647  */
ice_service_task_schedule(struct ice_pf * pf)1648 void ice_service_task_schedule(struct ice_pf *pf)
1649 {
1650 	if (!test_bit(ICE_SERVICE_DIS, pf->state) &&
1651 	    !test_and_set_bit(ICE_SERVICE_SCHED, pf->state) &&
1652 	    !test_bit(ICE_NEEDS_RESTART, pf->state))
1653 		queue_work(ice_wq, &pf->serv_task);
1654 }
1655 
1656 /**
1657  * ice_service_task_complete - finish up the service task
1658  * @pf: board private structure
1659  */
ice_service_task_complete(struct ice_pf * pf)1660 static void ice_service_task_complete(struct ice_pf *pf)
1661 {
1662 	WARN_ON(!test_bit(ICE_SERVICE_SCHED, pf->state));
1663 
1664 	/* force memory (pf->state) to sync before next service task */
1665 	smp_mb__before_atomic();
1666 	clear_bit(ICE_SERVICE_SCHED, pf->state);
1667 }
1668 
1669 /**
1670  * ice_service_task_stop - stop service task and cancel works
1671  * @pf: board private structure
1672  *
1673  * Return 0 if the ICE_SERVICE_DIS bit was not already set,
1674  * 1 otherwise.
1675  */
ice_service_task_stop(struct ice_pf * pf)1676 static int ice_service_task_stop(struct ice_pf *pf)
1677 {
1678 	int ret;
1679 
1680 	ret = test_and_set_bit(ICE_SERVICE_DIS, pf->state);
1681 
1682 	if (pf->serv_tmr.function)
1683 		del_timer_sync(&pf->serv_tmr);
1684 	if (pf->serv_task.func)
1685 		cancel_work_sync(&pf->serv_task);
1686 
1687 	clear_bit(ICE_SERVICE_SCHED, pf->state);
1688 	return ret;
1689 }
1690 
1691 /**
1692  * ice_service_task_restart - restart service task and schedule works
1693  * @pf: board private structure
1694  *
1695  * This function is needed for suspend and resume works (e.g WoL scenario)
1696  */
ice_service_task_restart(struct ice_pf * pf)1697 static void ice_service_task_restart(struct ice_pf *pf)
1698 {
1699 	clear_bit(ICE_SERVICE_DIS, pf->state);
1700 	ice_service_task_schedule(pf);
1701 }
1702 
1703 /**
1704  * ice_service_timer - timer callback to schedule service task
1705  * @t: pointer to timer_list
1706  */
ice_service_timer(struct timer_list * t)1707 static void ice_service_timer(struct timer_list *t)
1708 {
1709 	struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1710 
1711 	mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1712 	ice_service_task_schedule(pf);
1713 }
1714 
1715 /**
1716  * ice_handle_mdd_event - handle malicious driver detect event
1717  * @pf: pointer to the PF structure
1718  *
1719  * Called from service task. OICR interrupt handler indicates MDD event.
1720  * VF MDD logging is guarded by net_ratelimit. Additional PF and VF log
1721  * messages are wrapped by netif_msg_[rx|tx]_err. Since VF Rx MDD events
1722  * disable the queue, the PF can be configured to reset the VF using ethtool
1723  * private flag mdd-auto-reset-vf.
1724  */
ice_handle_mdd_event(struct ice_pf * pf)1725 static void ice_handle_mdd_event(struct ice_pf *pf)
1726 {
1727 	struct device *dev = ice_pf_to_dev(pf);
1728 	struct ice_hw *hw = &pf->hw;
1729 	struct ice_vf *vf;
1730 	unsigned int bkt;
1731 	u32 reg;
1732 
1733 	if (!test_and_clear_bit(ICE_MDD_EVENT_PENDING, pf->state)) {
1734 		/* Since the VF MDD event logging is rate limited, check if
1735 		 * there are pending MDD events.
1736 		 */
1737 		ice_print_vfs_mdd_events(pf);
1738 		return;
1739 	}
1740 
1741 	/* find what triggered an MDD event */
1742 	reg = rd32(hw, GL_MDET_TX_PQM);
1743 	if (reg & GL_MDET_TX_PQM_VALID_M) {
1744 		u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1745 				GL_MDET_TX_PQM_PF_NUM_S;
1746 		u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
1747 				GL_MDET_TX_PQM_VF_NUM_S;
1748 		u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1749 				GL_MDET_TX_PQM_MAL_TYPE_S;
1750 		u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
1751 				GL_MDET_TX_PQM_QNUM_S);
1752 
1753 		if (netif_msg_tx_err(pf))
1754 			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1755 				 event, queue, pf_num, vf_num);
1756 		wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1757 	}
1758 
1759 	reg = rd32(hw, GL_MDET_TX_TCLAN_BY_MAC(hw));
1760 	if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1761 		u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1762 				GL_MDET_TX_TCLAN_PF_NUM_S;
1763 		u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
1764 				GL_MDET_TX_TCLAN_VF_NUM_S;
1765 		u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1766 				GL_MDET_TX_TCLAN_MAL_TYPE_S;
1767 		u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1768 				GL_MDET_TX_TCLAN_QNUM_S);
1769 
1770 		if (netif_msg_tx_err(pf))
1771 			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1772 				 event, queue, pf_num, vf_num);
1773 		wr32(hw, GL_MDET_TX_TCLAN_BY_MAC(hw), U32_MAX);
1774 	}
1775 
1776 	reg = rd32(hw, GL_MDET_RX);
1777 	if (reg & GL_MDET_RX_VALID_M) {
1778 		u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
1779 				GL_MDET_RX_PF_NUM_S;
1780 		u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
1781 				GL_MDET_RX_VF_NUM_S;
1782 		u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
1783 				GL_MDET_RX_MAL_TYPE_S;
1784 		u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
1785 				GL_MDET_RX_QNUM_S);
1786 
1787 		if (netif_msg_rx_err(pf))
1788 			dev_info(dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1789 				 event, queue, pf_num, vf_num);
1790 		wr32(hw, GL_MDET_RX, 0xffffffff);
1791 	}
1792 
1793 	/* check to see if this PF caused an MDD event */
1794 	reg = rd32(hw, PF_MDET_TX_PQM);
1795 	if (reg & PF_MDET_TX_PQM_VALID_M) {
1796 		wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1797 		if (netif_msg_tx_err(pf))
1798 			dev_info(dev, "Malicious Driver Detection event TX_PQM detected on PF\n");
1799 	}
1800 
1801 	reg = rd32(hw, PF_MDET_TX_TCLAN_BY_MAC(hw));
1802 	if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1803 		wr32(hw, PF_MDET_TX_TCLAN_BY_MAC(hw), 0xffff);
1804 		if (netif_msg_tx_err(pf))
1805 			dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on PF\n");
1806 	}
1807 
1808 	reg = rd32(hw, PF_MDET_RX);
1809 	if (reg & PF_MDET_RX_VALID_M) {
1810 		wr32(hw, PF_MDET_RX, 0xFFFF);
1811 		if (netif_msg_rx_err(pf))
1812 			dev_info(dev, "Malicious Driver Detection event RX detected on PF\n");
1813 	}
1814 
1815 	/* Check to see if one of the VFs caused an MDD event, and then
1816 	 * increment counters and set print pending
1817 	 */
1818 	mutex_lock(&pf->vfs.table_lock);
1819 	ice_for_each_vf(pf, bkt, vf) {
1820 		reg = rd32(hw, VP_MDET_TX_PQM(vf->vf_id));
1821 		if (reg & VP_MDET_TX_PQM_VALID_M) {
1822 			wr32(hw, VP_MDET_TX_PQM(vf->vf_id), 0xFFFF);
1823 			vf->mdd_tx_events.count++;
1824 			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1825 			if (netif_msg_tx_err(pf))
1826 				dev_info(dev, "Malicious Driver Detection event TX_PQM detected on VF %d\n",
1827 					 vf->vf_id);
1828 		}
1829 
1830 		reg = rd32(hw, VP_MDET_TX_TCLAN(vf->vf_id));
1831 		if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1832 			wr32(hw, VP_MDET_TX_TCLAN(vf->vf_id), 0xFFFF);
1833 			vf->mdd_tx_events.count++;
1834 			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1835 			if (netif_msg_tx_err(pf))
1836 				dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on VF %d\n",
1837 					 vf->vf_id);
1838 		}
1839 
1840 		reg = rd32(hw, VP_MDET_TX_TDPU(vf->vf_id));
1841 		if (reg & VP_MDET_TX_TDPU_VALID_M) {
1842 			wr32(hw, VP_MDET_TX_TDPU(vf->vf_id), 0xFFFF);
1843 			vf->mdd_tx_events.count++;
1844 			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1845 			if (netif_msg_tx_err(pf))
1846 				dev_info(dev, "Malicious Driver Detection event TX_TDPU detected on VF %d\n",
1847 					 vf->vf_id);
1848 		}
1849 
1850 		reg = rd32(hw, VP_MDET_RX(vf->vf_id));
1851 		if (reg & VP_MDET_RX_VALID_M) {
1852 			wr32(hw, VP_MDET_RX(vf->vf_id), 0xFFFF);
1853 			vf->mdd_rx_events.count++;
1854 			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1855 			if (netif_msg_rx_err(pf))
1856 				dev_info(dev, "Malicious Driver Detection event RX detected on VF %d\n",
1857 					 vf->vf_id);
1858 
1859 			/* Since the queue is disabled on VF Rx MDD events, the
1860 			 * PF can be configured to reset the VF through ethtool
1861 			 * private flag mdd-auto-reset-vf.
1862 			 */
1863 			if (test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
1864 				/* VF MDD event counters will be cleared by
1865 				 * reset, so print the event prior to reset.
1866 				 */
1867 				ice_print_vf_rx_mdd_event(vf);
1868 				ice_reset_vf(vf, ICE_VF_RESET_LOCK);
1869 			}
1870 		}
1871 	}
1872 	mutex_unlock(&pf->vfs.table_lock);
1873 
1874 	ice_print_vfs_mdd_events(pf);
1875 }
1876 
1877 /**
1878  * ice_force_phys_link_state - Force the physical link state
1879  * @vsi: VSI to force the physical link state to up/down
1880  * @link_up: true/false indicates to set the physical link to up/down
1881  *
1882  * Force the physical link state by getting the current PHY capabilities from
1883  * hardware and setting the PHY config based on the determined capabilities. If
1884  * link changes a link event will be triggered because both the Enable Automatic
1885  * Link Update and LESM Enable bits are set when setting the PHY capabilities.
1886  *
1887  * Returns 0 on success, negative on failure
1888  */
ice_force_phys_link_state(struct ice_vsi * vsi,bool link_up)1889 static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
1890 {
1891 	struct ice_aqc_get_phy_caps_data *pcaps;
1892 	struct ice_aqc_set_phy_cfg_data *cfg;
1893 	struct ice_port_info *pi;
1894 	struct device *dev;
1895 	int retcode;
1896 
1897 	if (!vsi || !vsi->port_info || !vsi->back)
1898 		return -EINVAL;
1899 	if (vsi->type != ICE_VSI_PF)
1900 		return 0;
1901 
1902 	dev = ice_pf_to_dev(vsi->back);
1903 
1904 	pi = vsi->port_info;
1905 
1906 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1907 	if (!pcaps)
1908 		return -ENOMEM;
1909 
1910 	retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
1911 				      NULL);
1912 	if (retcode) {
1913 		dev_err(dev, "Failed to get phy capabilities, VSI %d error %d\n",
1914 			vsi->vsi_num, retcode);
1915 		retcode = -EIO;
1916 		goto out;
1917 	}
1918 
1919 	/* No change in link */
1920 	if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
1921 	    link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
1922 		goto out;
1923 
1924 	/* Use the current user PHY configuration. The current user PHY
1925 	 * configuration is initialized during probe from PHY capabilities
1926 	 * software mode, and updated on set PHY configuration.
1927 	 */
1928 	cfg = kmemdup(&pi->phy.curr_user_phy_cfg, sizeof(*cfg), GFP_KERNEL);
1929 	if (!cfg) {
1930 		retcode = -ENOMEM;
1931 		goto out;
1932 	}
1933 
1934 	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1935 	if (link_up)
1936 		cfg->caps |= ICE_AQ_PHY_ENA_LINK;
1937 	else
1938 		cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
1939 
1940 	retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
1941 	if (retcode) {
1942 		dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
1943 			vsi->vsi_num, retcode);
1944 		retcode = -EIO;
1945 	}
1946 
1947 	kfree(cfg);
1948 out:
1949 	kfree(pcaps);
1950 	return retcode;
1951 }
1952 
1953 /**
1954  * ice_init_nvm_phy_type - Initialize the NVM PHY type
1955  * @pi: port info structure
1956  *
1957  * Initialize nvm_phy_type_[low|high] for link lenient mode support
1958  */
ice_init_nvm_phy_type(struct ice_port_info * pi)1959 static int ice_init_nvm_phy_type(struct ice_port_info *pi)
1960 {
1961 	struct ice_aqc_get_phy_caps_data *pcaps;
1962 	struct ice_pf *pf = pi->hw->back;
1963 	int err;
1964 
1965 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1966 	if (!pcaps)
1967 		return -ENOMEM;
1968 
1969 	err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA,
1970 				  pcaps, NULL);
1971 
1972 	if (err) {
1973 		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
1974 		goto out;
1975 	}
1976 
1977 	pf->nvm_phy_type_hi = pcaps->phy_type_high;
1978 	pf->nvm_phy_type_lo = pcaps->phy_type_low;
1979 
1980 out:
1981 	kfree(pcaps);
1982 	return err;
1983 }
1984 
1985 /**
1986  * ice_init_link_dflt_override - Initialize link default override
1987  * @pi: port info structure
1988  *
1989  * Initialize link default override and PHY total port shutdown during probe
1990  */
ice_init_link_dflt_override(struct ice_port_info * pi)1991 static void ice_init_link_dflt_override(struct ice_port_info *pi)
1992 {
1993 	struct ice_link_default_override_tlv *ldo;
1994 	struct ice_pf *pf = pi->hw->back;
1995 
1996 	ldo = &pf->link_dflt_override;
1997 	if (ice_get_link_default_override(ldo, pi))
1998 		return;
1999 
2000 	if (!(ldo->options & ICE_LINK_OVERRIDE_PORT_DIS))
2001 		return;
2002 
2003 	/* Enable Total Port Shutdown (override/replace link-down-on-close
2004 	 * ethtool private flag) for ports with Port Disable bit set.
2005 	 */
2006 	set_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
2007 	set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
2008 }
2009 
2010 /**
2011  * ice_init_phy_cfg_dflt_override - Initialize PHY cfg default override settings
2012  * @pi: port info structure
2013  *
2014  * If default override is enabled, initialize the user PHY cfg speed and FEC
2015  * settings using the default override mask from the NVM.
2016  *
2017  * The PHY should only be configured with the default override settings the
2018  * first time media is available. The ICE_LINK_DEFAULT_OVERRIDE_PENDING state
2019  * is used to indicate that the user PHY cfg default override is initialized
2020  * and the PHY has not been configured with the default override settings. The
2021  * state is set here, and cleared in ice_configure_phy the first time the PHY is
2022  * configured.
2023  *
2024  * This function should be called only if the FW doesn't support default
2025  * configuration mode, as reported by ice_fw_supports_report_dflt_cfg.
2026  */
ice_init_phy_cfg_dflt_override(struct ice_port_info * pi)2027 static void ice_init_phy_cfg_dflt_override(struct ice_port_info *pi)
2028 {
2029 	struct ice_link_default_override_tlv *ldo;
2030 	struct ice_aqc_set_phy_cfg_data *cfg;
2031 	struct ice_phy_info *phy = &pi->phy;
2032 	struct ice_pf *pf = pi->hw->back;
2033 
2034 	ldo = &pf->link_dflt_override;
2035 
2036 	/* If link default override is enabled, use to mask NVM PHY capabilities
2037 	 * for speed and FEC default configuration.
2038 	 */
2039 	cfg = &phy->curr_user_phy_cfg;
2040 
2041 	if (ldo->phy_type_low || ldo->phy_type_high) {
2042 		cfg->phy_type_low = pf->nvm_phy_type_lo &
2043 				    cpu_to_le64(ldo->phy_type_low);
2044 		cfg->phy_type_high = pf->nvm_phy_type_hi &
2045 				     cpu_to_le64(ldo->phy_type_high);
2046 	}
2047 	cfg->link_fec_opt = ldo->fec_options;
2048 	phy->curr_user_fec_req = ICE_FEC_AUTO;
2049 
2050 	set_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING, pf->state);
2051 }
2052 
2053 /**
2054  * ice_init_phy_user_cfg - Initialize the PHY user configuration
2055  * @pi: port info structure
2056  *
2057  * Initialize the current user PHY configuration, speed, FEC, and FC requested
2058  * mode to default. The PHY defaults are from get PHY capabilities topology
2059  * with media so call when media is first available. An error is returned if
2060  * called when media is not available. The PHY initialization completed state is
2061  * set here.
2062  *
2063  * These configurations are used when setting PHY
2064  * configuration. The user PHY configuration is updated on set PHY
2065  * configuration. Returns 0 on success, negative on failure
2066  */
ice_init_phy_user_cfg(struct ice_port_info * pi)2067 static int ice_init_phy_user_cfg(struct ice_port_info *pi)
2068 {
2069 	struct ice_aqc_get_phy_caps_data *pcaps;
2070 	struct ice_phy_info *phy = &pi->phy;
2071 	struct ice_pf *pf = pi->hw->back;
2072 	int err;
2073 
2074 	if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
2075 		return -EIO;
2076 
2077 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2078 	if (!pcaps)
2079 		return -ENOMEM;
2080 
2081 	if (ice_fw_supports_report_dflt_cfg(pi->hw))
2082 		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2083 					  pcaps, NULL);
2084 	else
2085 		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2086 					  pcaps, NULL);
2087 	if (err) {
2088 		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
2089 		goto err_out;
2090 	}
2091 
2092 	ice_copy_phy_caps_to_cfg(pi, pcaps, &pi->phy.curr_user_phy_cfg);
2093 
2094 	/* check if lenient mode is supported and enabled */
2095 	if (ice_fw_supports_link_override(pi->hw) &&
2096 	    !(pcaps->module_compliance_enforcement &
2097 	      ICE_AQC_MOD_ENFORCE_STRICT_MODE)) {
2098 		set_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags);
2099 
2100 		/* if the FW supports default PHY configuration mode, then the driver
2101 		 * does not have to apply link override settings. If not,
2102 		 * initialize user PHY configuration with link override values
2103 		 */
2104 		if (!ice_fw_supports_report_dflt_cfg(pi->hw) &&
2105 		    (pf->link_dflt_override.options & ICE_LINK_OVERRIDE_EN)) {
2106 			ice_init_phy_cfg_dflt_override(pi);
2107 			goto out;
2108 		}
2109 	}
2110 
2111 	/* if link default override is not enabled, set user flow control and
2112 	 * FEC settings based on what get_phy_caps returned
2113 	 */
2114 	phy->curr_user_fec_req = ice_caps_to_fec_mode(pcaps->caps,
2115 						      pcaps->link_fec_options);
2116 	phy->curr_user_fc_req = ice_caps_to_fc_mode(pcaps->caps);
2117 
2118 out:
2119 	phy->curr_user_speed_req = ICE_AQ_LINK_SPEED_M;
2120 	set_bit(ICE_PHY_INIT_COMPLETE, pf->state);
2121 err_out:
2122 	kfree(pcaps);
2123 	return err;
2124 }
2125 
2126 /**
2127  * ice_configure_phy - configure PHY
2128  * @vsi: VSI of PHY
2129  *
2130  * Set the PHY configuration. If the current PHY configuration is the same as
2131  * the curr_user_phy_cfg, then do nothing to avoid link flap. Otherwise
2132  * configure the based get PHY capabilities for topology with media.
2133  */
ice_configure_phy(struct ice_vsi * vsi)2134 static int ice_configure_phy(struct ice_vsi *vsi)
2135 {
2136 	struct device *dev = ice_pf_to_dev(vsi->back);
2137 	struct ice_port_info *pi = vsi->port_info;
2138 	struct ice_aqc_get_phy_caps_data *pcaps;
2139 	struct ice_aqc_set_phy_cfg_data *cfg;
2140 	struct ice_phy_info *phy = &pi->phy;
2141 	struct ice_pf *pf = vsi->back;
2142 	int err;
2143 
2144 	/* Ensure we have media as we cannot configure a medialess port */
2145 	if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
2146 		return -ENOMEDIUM;
2147 
2148 	ice_print_topo_conflict(vsi);
2149 
2150 	if (!test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags) &&
2151 	    phy->link_info.topo_media_conflict == ICE_AQ_LINK_TOPO_UNSUPP_MEDIA)
2152 		return -EPERM;
2153 
2154 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))
2155 		return ice_force_phys_link_state(vsi, true);
2156 
2157 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2158 	if (!pcaps)
2159 		return -ENOMEM;
2160 
2161 	/* Get current PHY config */
2162 	err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
2163 				  NULL);
2164 	if (err) {
2165 		dev_err(dev, "Failed to get PHY configuration, VSI %d error %d\n",
2166 			vsi->vsi_num, err);
2167 		goto done;
2168 	}
2169 
2170 	/* If PHY enable link is configured and configuration has not changed,
2171 	 * there's nothing to do
2172 	 */
2173 	if (pcaps->caps & ICE_AQC_PHY_EN_LINK &&
2174 	    ice_phy_caps_equals_cfg(pcaps, &phy->curr_user_phy_cfg))
2175 		goto done;
2176 
2177 	/* Use PHY topology as baseline for configuration */
2178 	memset(pcaps, 0, sizeof(*pcaps));
2179 	if (ice_fw_supports_report_dflt_cfg(pi->hw))
2180 		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2181 					  pcaps, NULL);
2182 	else
2183 		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2184 					  pcaps, NULL);
2185 	if (err) {
2186 		dev_err(dev, "Failed to get PHY caps, VSI %d error %d\n",
2187 			vsi->vsi_num, err);
2188 		goto done;
2189 	}
2190 
2191 	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
2192 	if (!cfg) {
2193 		err = -ENOMEM;
2194 		goto done;
2195 	}
2196 
2197 	ice_copy_phy_caps_to_cfg(pi, pcaps, cfg);
2198 
2199 	/* Speed - If default override pending, use curr_user_phy_cfg set in
2200 	 * ice_init_phy_user_cfg_ldo.
2201 	 */
2202 	if (test_and_clear_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING,
2203 			       vsi->back->state)) {
2204 		cfg->phy_type_low = phy->curr_user_phy_cfg.phy_type_low;
2205 		cfg->phy_type_high = phy->curr_user_phy_cfg.phy_type_high;
2206 	} else {
2207 		u64 phy_low = 0, phy_high = 0;
2208 
2209 		ice_update_phy_type(&phy_low, &phy_high,
2210 				    pi->phy.curr_user_speed_req);
2211 		cfg->phy_type_low = pcaps->phy_type_low & cpu_to_le64(phy_low);
2212 		cfg->phy_type_high = pcaps->phy_type_high &
2213 				     cpu_to_le64(phy_high);
2214 	}
2215 
2216 	/* Can't provide what was requested; use PHY capabilities */
2217 	if (!cfg->phy_type_low && !cfg->phy_type_high) {
2218 		cfg->phy_type_low = pcaps->phy_type_low;
2219 		cfg->phy_type_high = pcaps->phy_type_high;
2220 	}
2221 
2222 	/* FEC */
2223 	ice_cfg_phy_fec(pi, cfg, phy->curr_user_fec_req);
2224 
2225 	/* Can't provide what was requested; use PHY capabilities */
2226 	if (cfg->link_fec_opt !=
2227 	    (cfg->link_fec_opt & pcaps->link_fec_options)) {
2228 		cfg->caps |= pcaps->caps & ICE_AQC_PHY_EN_AUTO_FEC;
2229 		cfg->link_fec_opt = pcaps->link_fec_options;
2230 	}
2231 
2232 	/* Flow Control - always supported; no need to check against
2233 	 * capabilities
2234 	 */
2235 	ice_cfg_phy_fc(pi, cfg, phy->curr_user_fc_req);
2236 
2237 	/* Enable link and link update */
2238 	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT | ICE_AQ_PHY_ENA_LINK;
2239 
2240 	err = ice_aq_set_phy_cfg(&pf->hw, pi, cfg, NULL);
2241 	if (err)
2242 		dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
2243 			vsi->vsi_num, err);
2244 
2245 	kfree(cfg);
2246 done:
2247 	kfree(pcaps);
2248 	return err;
2249 }
2250 
2251 /**
2252  * ice_check_media_subtask - Check for media
2253  * @pf: pointer to PF struct
2254  *
2255  * If media is available, then initialize PHY user configuration if it is not
2256  * been, and configure the PHY if the interface is up.
2257  */
ice_check_media_subtask(struct ice_pf * pf)2258 static void ice_check_media_subtask(struct ice_pf *pf)
2259 {
2260 	struct ice_port_info *pi;
2261 	struct ice_vsi *vsi;
2262 	int err;
2263 
2264 	/* No need to check for media if it's already present */
2265 	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags))
2266 		return;
2267 
2268 	vsi = ice_get_main_vsi(pf);
2269 	if (!vsi)
2270 		return;
2271 
2272 	/* Refresh link info and check if media is present */
2273 	pi = vsi->port_info;
2274 	err = ice_update_link_info(pi);
2275 	if (err)
2276 		return;
2277 
2278 	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
2279 
2280 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
2281 		if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state))
2282 			ice_init_phy_user_cfg(pi);
2283 
2284 		/* PHY settings are reset on media insertion, reconfigure
2285 		 * PHY to preserve settings.
2286 		 */
2287 		if (test_bit(ICE_VSI_DOWN, vsi->state) &&
2288 		    test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
2289 			return;
2290 
2291 		err = ice_configure_phy(vsi);
2292 		if (!err)
2293 			clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
2294 
2295 		/* A Link Status Event will be generated; the event handler
2296 		 * will complete bringing the interface up
2297 		 */
2298 	}
2299 }
2300 
2301 /**
2302  * ice_service_task - manage and run subtasks
2303  * @work: pointer to work_struct contained by the PF struct
2304  */
ice_service_task(struct work_struct * work)2305 static void ice_service_task(struct work_struct *work)
2306 {
2307 	struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
2308 	unsigned long start_time = jiffies;
2309 
2310 	/* subtasks */
2311 
2312 	/* process reset requests first */
2313 	ice_reset_subtask(pf);
2314 
2315 	/* bail if a reset/recovery cycle is pending or rebuild failed */
2316 	if (ice_is_reset_in_progress(pf->state) ||
2317 	    test_bit(ICE_SUSPENDED, pf->state) ||
2318 	    test_bit(ICE_NEEDS_RESTART, pf->state)) {
2319 		ice_service_task_complete(pf);
2320 		return;
2321 	}
2322 
2323 	if (test_and_clear_bit(ICE_AUX_ERR_PENDING, pf->state)) {
2324 		struct iidc_event *event;
2325 
2326 		event = kzalloc(sizeof(*event), GFP_KERNEL);
2327 		if (event) {
2328 			set_bit(IIDC_EVENT_CRIT_ERR, event->type);
2329 			/* report the entire OICR value to AUX driver */
2330 			swap(event->reg, pf->oicr_err_reg);
2331 			ice_send_event_to_aux(pf, event);
2332 			kfree(event);
2333 		}
2334 	}
2335 
2336 	/* unplug aux dev per request, if an unplug request came in
2337 	 * while processing a plug request, this will handle it
2338 	 */
2339 	if (test_and_clear_bit(ICE_FLAG_UNPLUG_AUX_DEV, pf->flags))
2340 		ice_unplug_aux_dev(pf);
2341 
2342 	/* Plug aux device per request */
2343 	if (test_and_clear_bit(ICE_FLAG_PLUG_AUX_DEV, pf->flags))
2344 		ice_plug_aux_dev(pf);
2345 
2346 	if (test_and_clear_bit(ICE_FLAG_MTU_CHANGED, pf->flags)) {
2347 		struct iidc_event *event;
2348 
2349 		event = kzalloc(sizeof(*event), GFP_KERNEL);
2350 		if (event) {
2351 			set_bit(IIDC_EVENT_AFTER_MTU_CHANGE, event->type);
2352 			ice_send_event_to_aux(pf, event);
2353 			kfree(event);
2354 		}
2355 	}
2356 
2357 	ice_clean_adminq_subtask(pf);
2358 	ice_check_media_subtask(pf);
2359 	ice_check_for_hang_subtask(pf);
2360 	ice_sync_fltr_subtask(pf);
2361 	ice_handle_mdd_event(pf);
2362 	ice_watchdog_subtask(pf);
2363 
2364 	if (ice_is_safe_mode(pf)) {
2365 		ice_service_task_complete(pf);
2366 		return;
2367 	}
2368 
2369 	ice_process_vflr_event(pf);
2370 	ice_clean_mailboxq_subtask(pf);
2371 	ice_clean_sbq_subtask(pf);
2372 	ice_sync_arfs_fltrs(pf);
2373 	ice_flush_fdir_ctx(pf);
2374 
2375 	/* Clear ICE_SERVICE_SCHED flag to allow scheduling next event */
2376 	ice_service_task_complete(pf);
2377 
2378 	/* If the tasks have taken longer than one service timer period
2379 	 * or there is more work to be done, reset the service timer to
2380 	 * schedule the service task now.
2381 	 */
2382 	if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
2383 	    test_bit(ICE_MDD_EVENT_PENDING, pf->state) ||
2384 	    test_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
2385 	    test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
2386 	    test_bit(ICE_FD_VF_FLUSH_CTX, pf->state) ||
2387 	    test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state) ||
2388 	    test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
2389 		mod_timer(&pf->serv_tmr, jiffies);
2390 }
2391 
2392 /**
2393  * ice_set_ctrlq_len - helper function to set controlq length
2394  * @hw: pointer to the HW instance
2395  */
ice_set_ctrlq_len(struct ice_hw * hw)2396 static void ice_set_ctrlq_len(struct ice_hw *hw)
2397 {
2398 	hw->adminq.num_rq_entries = ICE_AQ_LEN;
2399 	hw->adminq.num_sq_entries = ICE_AQ_LEN;
2400 	hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
2401 	hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
2402 	hw->mailboxq.num_rq_entries = PF_MBX_ARQLEN_ARQLEN_M;
2403 	hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN;
2404 	hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2405 	hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2406 	hw->sbq.num_rq_entries = ICE_SBQ_LEN;
2407 	hw->sbq.num_sq_entries = ICE_SBQ_LEN;
2408 	hw->sbq.rq_buf_size = ICE_SBQ_MAX_BUF_LEN;
2409 	hw->sbq.sq_buf_size = ICE_SBQ_MAX_BUF_LEN;
2410 }
2411 
2412 /**
2413  * ice_schedule_reset - schedule a reset
2414  * @pf: board private structure
2415  * @reset: reset being requested
2416  */
ice_schedule_reset(struct ice_pf * pf,enum ice_reset_req reset)2417 int ice_schedule_reset(struct ice_pf *pf, enum ice_reset_req reset)
2418 {
2419 	struct device *dev = ice_pf_to_dev(pf);
2420 
2421 	/* bail out if earlier reset has failed */
2422 	if (test_bit(ICE_RESET_FAILED, pf->state)) {
2423 		dev_dbg(dev, "earlier reset has failed\n");
2424 		return -EIO;
2425 	}
2426 	/* bail if reset/recovery already in progress */
2427 	if (ice_is_reset_in_progress(pf->state)) {
2428 		dev_dbg(dev, "Reset already in progress\n");
2429 		return -EBUSY;
2430 	}
2431 
2432 	switch (reset) {
2433 	case ICE_RESET_PFR:
2434 		set_bit(ICE_PFR_REQ, pf->state);
2435 		break;
2436 	case ICE_RESET_CORER:
2437 		set_bit(ICE_CORER_REQ, pf->state);
2438 		break;
2439 	case ICE_RESET_GLOBR:
2440 		set_bit(ICE_GLOBR_REQ, pf->state);
2441 		break;
2442 	default:
2443 		return -EINVAL;
2444 	}
2445 
2446 	ice_service_task_schedule(pf);
2447 	return 0;
2448 }
2449 
2450 /**
2451  * ice_irq_affinity_notify - Callback for affinity changes
2452  * @notify: context as to what irq was changed
2453  * @mask: the new affinity mask
2454  *
2455  * This is a callback function used by the irq_set_affinity_notifier function
2456  * so that we may register to receive changes to the irq affinity masks.
2457  */
2458 static void
ice_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)2459 ice_irq_affinity_notify(struct irq_affinity_notify *notify,
2460 			const cpumask_t *mask)
2461 {
2462 	struct ice_q_vector *q_vector =
2463 		container_of(notify, struct ice_q_vector, affinity_notify);
2464 
2465 	cpumask_copy(&q_vector->affinity_mask, mask);
2466 }
2467 
2468 /**
2469  * ice_irq_affinity_release - Callback for affinity notifier release
2470  * @ref: internal core kernel usage
2471  *
2472  * This is a callback function used by the irq_set_affinity_notifier function
2473  * to inform the current notification subscriber that they will no longer
2474  * receive notifications.
2475  */
ice_irq_affinity_release(struct kref __always_unused * ref)2476 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
2477 
2478 /**
2479  * ice_vsi_ena_irq - Enable IRQ for the given VSI
2480  * @vsi: the VSI being configured
2481  */
ice_vsi_ena_irq(struct ice_vsi * vsi)2482 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
2483 {
2484 	struct ice_hw *hw = &vsi->back->hw;
2485 	int i;
2486 
2487 	ice_for_each_q_vector(vsi, i)
2488 		ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
2489 
2490 	ice_flush(hw);
2491 	return 0;
2492 }
2493 
2494 /**
2495  * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
2496  * @vsi: the VSI being configured
2497  * @basename: name for the vector
2498  */
ice_vsi_req_irq_msix(struct ice_vsi * vsi,char * basename)2499 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
2500 {
2501 	int q_vectors = vsi->num_q_vectors;
2502 	struct ice_pf *pf = vsi->back;
2503 	struct device *dev;
2504 	int rx_int_idx = 0;
2505 	int tx_int_idx = 0;
2506 	int vector, err;
2507 	int irq_num;
2508 
2509 	dev = ice_pf_to_dev(pf);
2510 	for (vector = 0; vector < q_vectors; vector++) {
2511 		struct ice_q_vector *q_vector = vsi->q_vectors[vector];
2512 
2513 		irq_num = q_vector->irq.virq;
2514 
2515 		if (q_vector->tx.tx_ring && q_vector->rx.rx_ring) {
2516 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2517 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2518 			tx_int_idx++;
2519 		} else if (q_vector->rx.rx_ring) {
2520 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2521 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
2522 		} else if (q_vector->tx.tx_ring) {
2523 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2524 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
2525 		} else {
2526 			/* skip this unused q_vector */
2527 			continue;
2528 		}
2529 		if (vsi->type == ICE_VSI_CTRL && vsi->vf)
2530 			err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2531 					       IRQF_SHARED, q_vector->name,
2532 					       q_vector);
2533 		else
2534 			err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2535 					       0, q_vector->name, q_vector);
2536 		if (err) {
2537 			netdev_err(vsi->netdev, "MSIX request_irq failed, error: %d\n",
2538 				   err);
2539 			goto free_q_irqs;
2540 		}
2541 
2542 		/* register for affinity change notifications */
2543 		if (!IS_ENABLED(CONFIG_RFS_ACCEL)) {
2544 			struct irq_affinity_notify *affinity_notify;
2545 
2546 			affinity_notify = &q_vector->affinity_notify;
2547 			affinity_notify->notify = ice_irq_affinity_notify;
2548 			affinity_notify->release = ice_irq_affinity_release;
2549 			irq_set_affinity_notifier(irq_num, affinity_notify);
2550 		}
2551 
2552 		/* assign the mask for this irq */
2553 		irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
2554 	}
2555 
2556 	err = ice_set_cpu_rx_rmap(vsi);
2557 	if (err) {
2558 		netdev_err(vsi->netdev, "Failed to setup CPU RMAP on VSI %u: %pe\n",
2559 			   vsi->vsi_num, ERR_PTR(err));
2560 		goto free_q_irqs;
2561 	}
2562 
2563 	vsi->irqs_ready = true;
2564 	return 0;
2565 
2566 free_q_irqs:
2567 	while (vector--) {
2568 		irq_num = vsi->q_vectors[vector]->irq.virq;
2569 		if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2570 			irq_set_affinity_notifier(irq_num, NULL);
2571 		irq_set_affinity_hint(irq_num, NULL);
2572 		devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]);
2573 	}
2574 	return err;
2575 }
2576 
2577 /**
2578  * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
2579  * @vsi: VSI to setup Tx rings used by XDP
2580  *
2581  * Return 0 on success and negative value on error
2582  */
ice_xdp_alloc_setup_rings(struct ice_vsi * vsi)2583 static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
2584 {
2585 	struct device *dev = ice_pf_to_dev(vsi->back);
2586 	struct ice_tx_desc *tx_desc;
2587 	int i, j;
2588 
2589 	ice_for_each_xdp_txq(vsi, i) {
2590 		u16 xdp_q_idx = vsi->alloc_txq + i;
2591 		struct ice_ring_stats *ring_stats;
2592 		struct ice_tx_ring *xdp_ring;
2593 
2594 		xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
2595 		if (!xdp_ring)
2596 			goto free_xdp_rings;
2597 
2598 		ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
2599 		if (!ring_stats) {
2600 			ice_free_tx_ring(xdp_ring);
2601 			goto free_xdp_rings;
2602 		}
2603 
2604 		xdp_ring->ring_stats = ring_stats;
2605 		xdp_ring->q_index = xdp_q_idx;
2606 		xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
2607 		xdp_ring->vsi = vsi;
2608 		xdp_ring->netdev = NULL;
2609 		xdp_ring->dev = dev;
2610 		xdp_ring->count = vsi->num_tx_desc;
2611 		WRITE_ONCE(vsi->xdp_rings[i], xdp_ring);
2612 		if (ice_setup_tx_ring(xdp_ring))
2613 			goto free_xdp_rings;
2614 		ice_set_ring_xdp(xdp_ring);
2615 		spin_lock_init(&xdp_ring->tx_lock);
2616 		for (j = 0; j < xdp_ring->count; j++) {
2617 			tx_desc = ICE_TX_DESC(xdp_ring, j);
2618 			tx_desc->cmd_type_offset_bsz = 0;
2619 		}
2620 	}
2621 
2622 	return 0;
2623 
2624 free_xdp_rings:
2625 	for (; i >= 0; i--) {
2626 		if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc) {
2627 			kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
2628 			vsi->xdp_rings[i]->ring_stats = NULL;
2629 			ice_free_tx_ring(vsi->xdp_rings[i]);
2630 		}
2631 	}
2632 	return -ENOMEM;
2633 }
2634 
2635 /**
2636  * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
2637  * @vsi: VSI to set the bpf prog on
2638  * @prog: the bpf prog pointer
2639  */
ice_vsi_assign_bpf_prog(struct ice_vsi * vsi,struct bpf_prog * prog)2640 static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
2641 {
2642 	struct bpf_prog *old_prog;
2643 	int i;
2644 
2645 	old_prog = xchg(&vsi->xdp_prog, prog);
2646 	ice_for_each_rxq(vsi, i)
2647 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
2648 
2649 	if (old_prog)
2650 		bpf_prog_put(old_prog);
2651 }
2652 
2653 /**
2654  * ice_unmap_xdp_rings - Unmap XDP rings from interrupt vectors
2655  * @vsi: the VSI with XDP rings being unmapped
2656  */
ice_unmap_xdp_rings(struct ice_vsi * vsi)2657 static void ice_unmap_xdp_rings(struct ice_vsi *vsi)
2658 {
2659 	int v_idx;
2660 
2661 	ice_for_each_q_vector(vsi, v_idx) {
2662 		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2663 		struct ice_tx_ring *ring;
2664 
2665 		ice_for_each_tx_ring(ring, q_vector->tx)
2666 			if (!ring->tx_buf || !ice_ring_is_xdp(ring))
2667 				break;
2668 
2669 		/* restore the value of last node prior to XDP setup */
2670 		q_vector->tx.tx_ring = ring;
2671 	}
2672 }
2673 
2674 /**
2675  * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
2676  * @vsi: VSI to bring up Tx rings used by XDP
2677  * @prog: bpf program that will be assigned to VSI
2678  * @cfg_type: create from scratch or restore the existing configuration
2679  *
2680  * Return 0 on success and negative value on error
2681  */
ice_prepare_xdp_rings(struct ice_vsi * vsi,struct bpf_prog * prog,enum ice_xdp_cfg cfg_type)2682 int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog,
2683 			  enum ice_xdp_cfg cfg_type)
2684 {
2685 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2686 	int xdp_rings_rem = vsi->num_xdp_txq;
2687 	struct ice_pf *pf = vsi->back;
2688 	struct ice_qs_cfg xdp_qs_cfg = {
2689 		.qs_mutex = &pf->avail_q_mutex,
2690 		.pf_map = pf->avail_txqs,
2691 		.pf_map_size = pf->max_pf_txqs,
2692 		.q_count = vsi->num_xdp_txq,
2693 		.scatter_count = ICE_MAX_SCATTER_TXQS,
2694 		.vsi_map = vsi->txq_map,
2695 		.vsi_map_offset = vsi->alloc_txq,
2696 		.mapping_mode = ICE_VSI_MAP_CONTIG
2697 	};
2698 	struct device *dev;
2699 	int i, v_idx;
2700 	int status;
2701 
2702 	dev = ice_pf_to_dev(pf);
2703 	vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq,
2704 				      sizeof(*vsi->xdp_rings), GFP_KERNEL);
2705 	if (!vsi->xdp_rings)
2706 		return -ENOMEM;
2707 
2708 	vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
2709 	if (__ice_vsi_get_qs(&xdp_qs_cfg))
2710 		goto err_map_xdp;
2711 
2712 	if (static_key_enabled(&ice_xdp_locking_key))
2713 		netdev_warn(vsi->netdev,
2714 			    "Could not allocate one XDP Tx ring per CPU, XDP_TX/XDP_REDIRECT actions will be slower\n");
2715 
2716 	if (ice_xdp_alloc_setup_rings(vsi))
2717 		goto clear_xdp_rings;
2718 
2719 	/* follow the logic from ice_vsi_map_rings_to_vectors */
2720 	ice_for_each_q_vector(vsi, v_idx) {
2721 		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2722 		int xdp_rings_per_v, q_id, q_base;
2723 
2724 		xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
2725 					       vsi->num_q_vectors - v_idx);
2726 		q_base = vsi->num_xdp_txq - xdp_rings_rem;
2727 
2728 		for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
2729 			struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_id];
2730 
2731 			xdp_ring->q_vector = q_vector;
2732 			xdp_ring->next = q_vector->tx.tx_ring;
2733 			q_vector->tx.tx_ring = xdp_ring;
2734 		}
2735 		xdp_rings_rem -= xdp_rings_per_v;
2736 	}
2737 
2738 	ice_for_each_rxq(vsi, i) {
2739 		if (static_key_enabled(&ice_xdp_locking_key)) {
2740 			vsi->rx_rings[i]->xdp_ring = vsi->xdp_rings[i % vsi->num_xdp_txq];
2741 		} else {
2742 			struct ice_q_vector *q_vector = vsi->rx_rings[i]->q_vector;
2743 			struct ice_tx_ring *ring;
2744 
2745 			ice_for_each_tx_ring(ring, q_vector->tx) {
2746 				if (ice_ring_is_xdp(ring)) {
2747 					vsi->rx_rings[i]->xdp_ring = ring;
2748 					break;
2749 				}
2750 			}
2751 		}
2752 		ice_tx_xsk_pool(vsi, i);
2753 	}
2754 
2755 	/* omit the scheduler update if in reset path; XDP queues will be
2756 	 * taken into account at the end of ice_vsi_rebuild, where
2757 	 * ice_cfg_vsi_lan is being called
2758 	 */
2759 	if (cfg_type == ICE_XDP_CFG_PART)
2760 		return 0;
2761 
2762 	/* tell the Tx scheduler that right now we have
2763 	 * additional queues
2764 	 */
2765 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2766 		max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
2767 
2768 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2769 				 max_txqs);
2770 	if (status) {
2771 		dev_err(dev, "Failed VSI LAN queue config for XDP, error: %d\n",
2772 			status);
2773 		goto unmap_xdp_rings;
2774 	}
2775 
2776 	/* assign the prog only when it's not already present on VSI;
2777 	 * this flow is a subject of both ethtool -L and ndo_bpf flows;
2778 	 * VSI rebuild that happens under ethtool -L can expose us to
2779 	 * the bpf_prog refcount issues as we would be swapping same
2780 	 * bpf_prog pointers from vsi->xdp_prog and calling bpf_prog_put
2781 	 * on it as it would be treated as an 'old_prog'; for ndo_bpf
2782 	 * this is not harmful as dev_xdp_install bumps the refcount
2783 	 * before calling the op exposed by the driver;
2784 	 */
2785 	if (!ice_is_xdp_ena_vsi(vsi))
2786 		ice_vsi_assign_bpf_prog(vsi, prog);
2787 
2788 	return 0;
2789 unmap_xdp_rings:
2790 	ice_unmap_xdp_rings(vsi);
2791 clear_xdp_rings:
2792 	ice_for_each_xdp_txq(vsi, i)
2793 		if (vsi->xdp_rings[i]) {
2794 			kfree_rcu(vsi->xdp_rings[i], rcu);
2795 			vsi->xdp_rings[i] = NULL;
2796 		}
2797 
2798 err_map_xdp:
2799 	mutex_lock(&pf->avail_q_mutex);
2800 	ice_for_each_xdp_txq(vsi, i) {
2801 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2802 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2803 	}
2804 	mutex_unlock(&pf->avail_q_mutex);
2805 
2806 	devm_kfree(dev, vsi->xdp_rings);
2807 	vsi->xdp_rings = NULL;
2808 
2809 	return -ENOMEM;
2810 }
2811 
2812 /**
2813  * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
2814  * @vsi: VSI to remove XDP rings
2815  * @cfg_type: disable XDP permanently or allow it to be restored later
2816  *
2817  * Detach XDP rings from irq vectors, clean up the PF bitmap and free
2818  * resources
2819  */
ice_destroy_xdp_rings(struct ice_vsi * vsi,enum ice_xdp_cfg cfg_type)2820 int ice_destroy_xdp_rings(struct ice_vsi *vsi, enum ice_xdp_cfg cfg_type)
2821 {
2822 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2823 	struct ice_pf *pf = vsi->back;
2824 	int i;
2825 
2826 	/* q_vectors are freed in reset path so there's no point in detaching
2827 	 * rings
2828 	 */
2829 	if (cfg_type == ICE_XDP_CFG_PART)
2830 		goto free_qmap;
2831 
2832 	ice_unmap_xdp_rings(vsi);
2833 
2834 free_qmap:
2835 	mutex_lock(&pf->avail_q_mutex);
2836 	ice_for_each_xdp_txq(vsi, i) {
2837 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2838 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2839 	}
2840 	mutex_unlock(&pf->avail_q_mutex);
2841 
2842 	ice_for_each_xdp_txq(vsi, i)
2843 		if (vsi->xdp_rings[i]) {
2844 			if (vsi->xdp_rings[i]->desc) {
2845 				synchronize_rcu();
2846 				ice_free_tx_ring(vsi->xdp_rings[i]);
2847 			}
2848 			kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
2849 			vsi->xdp_rings[i]->ring_stats = NULL;
2850 			kfree_rcu(vsi->xdp_rings[i], rcu);
2851 			vsi->xdp_rings[i] = NULL;
2852 		}
2853 
2854 	devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
2855 	vsi->xdp_rings = NULL;
2856 
2857 	if (static_key_enabled(&ice_xdp_locking_key))
2858 		static_branch_dec(&ice_xdp_locking_key);
2859 
2860 	if (cfg_type == ICE_XDP_CFG_PART)
2861 		return 0;
2862 
2863 	ice_vsi_assign_bpf_prog(vsi, NULL);
2864 
2865 	/* notify Tx scheduler that we destroyed XDP queues and bring
2866 	 * back the old number of child nodes
2867 	 */
2868 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2869 		max_txqs[i] = vsi->num_txq;
2870 
2871 	/* change number of XDP Tx queues to 0 */
2872 	vsi->num_xdp_txq = 0;
2873 
2874 	return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2875 			       max_txqs);
2876 }
2877 
2878 /**
2879  * ice_vsi_rx_napi_schedule - Schedule napi on RX queues from VSI
2880  * @vsi: VSI to schedule napi on
2881  */
ice_vsi_rx_napi_schedule(struct ice_vsi * vsi)2882 static void ice_vsi_rx_napi_schedule(struct ice_vsi *vsi)
2883 {
2884 	int i;
2885 
2886 	ice_for_each_rxq(vsi, i) {
2887 		struct ice_rx_ring *rx_ring = vsi->rx_rings[i];
2888 
2889 		if (rx_ring->xsk_pool)
2890 			napi_schedule(&rx_ring->q_vector->napi);
2891 	}
2892 }
2893 
2894 /**
2895  * ice_vsi_determine_xdp_res - figure out how many Tx qs can XDP have
2896  * @vsi: VSI to determine the count of XDP Tx qs
2897  *
2898  * returns 0 if Tx qs count is higher than at least half of CPU count,
2899  * -ENOMEM otherwise
2900  */
ice_vsi_determine_xdp_res(struct ice_vsi * vsi)2901 int ice_vsi_determine_xdp_res(struct ice_vsi *vsi)
2902 {
2903 	u16 avail = ice_get_avail_txq_count(vsi->back);
2904 	u16 cpus = num_possible_cpus();
2905 
2906 	if (avail < cpus / 2)
2907 		return -ENOMEM;
2908 
2909 	vsi->num_xdp_txq = min_t(u16, avail, cpus);
2910 
2911 	if (vsi->num_xdp_txq < cpus)
2912 		static_branch_inc(&ice_xdp_locking_key);
2913 
2914 	return 0;
2915 }
2916 
2917 /**
2918  * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2919  * @vsi: Pointer to VSI structure
2920  */
ice_max_xdp_frame_size(struct ice_vsi * vsi)2921 static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
2922 {
2923 	if (test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
2924 		return ICE_RXBUF_1664;
2925 	else
2926 		return ICE_RXBUF_3072;
2927 }
2928 
2929 /**
2930  * ice_xdp_setup_prog - Add or remove XDP eBPF program
2931  * @vsi: VSI to setup XDP for
2932  * @prog: XDP program
2933  * @extack: netlink extended ack
2934  */
2935 static int
ice_xdp_setup_prog(struct ice_vsi * vsi,struct bpf_prog * prog,struct netlink_ext_ack * extack)2936 ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
2937 		   struct netlink_ext_ack *extack)
2938 {
2939 	unsigned int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
2940 	int ret = 0, xdp_ring_err = 0;
2941 	bool if_running;
2942 
2943 	if (prog && !prog->aux->xdp_has_frags) {
2944 		if (frame_size > ice_max_xdp_frame_size(vsi)) {
2945 			NL_SET_ERR_MSG_MOD(extack,
2946 					   "MTU is too large for linear frames and XDP prog does not support frags");
2947 			return -EOPNOTSUPP;
2948 		}
2949 	}
2950 
2951 	/* hot swap progs and avoid toggling link */
2952 	if (ice_is_xdp_ena_vsi(vsi) == !!prog ||
2953 	    test_bit(ICE_VSI_REBUILD_PENDING, vsi->state)) {
2954 		ice_vsi_assign_bpf_prog(vsi, prog);
2955 		return 0;
2956 	}
2957 
2958 	if_running = netif_running(vsi->netdev) &&
2959 		     !test_and_set_bit(ICE_VSI_DOWN, vsi->state);
2960 
2961 	/* need to stop netdev while setting up the program for Rx rings */
2962 	if (if_running) {
2963 		ret = ice_down(vsi);
2964 		if (ret) {
2965 			NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
2966 			return ret;
2967 		}
2968 	}
2969 
2970 	if (!ice_is_xdp_ena_vsi(vsi) && prog) {
2971 		xdp_ring_err = ice_vsi_determine_xdp_res(vsi);
2972 		if (xdp_ring_err) {
2973 			NL_SET_ERR_MSG_MOD(extack, "Not enough Tx resources for XDP");
2974 			goto resume_if;
2975 		} else {
2976 			xdp_ring_err = ice_prepare_xdp_rings(vsi, prog,
2977 							     ICE_XDP_CFG_FULL);
2978 			if (xdp_ring_err) {
2979 				NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
2980 				goto resume_if;
2981 			}
2982 		}
2983 		xdp_features_set_redirect_target(vsi->netdev, true);
2984 		/* reallocate Rx queues that are used for zero-copy */
2985 		xdp_ring_err = ice_realloc_zc_buf(vsi, true);
2986 		if (xdp_ring_err)
2987 			NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Rx resources failed");
2988 	} else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
2989 		xdp_features_clear_redirect_target(vsi->netdev);
2990 		xdp_ring_err = ice_destroy_xdp_rings(vsi, ICE_XDP_CFG_FULL);
2991 		if (xdp_ring_err)
2992 			NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
2993 		/* reallocate Rx queues that were used for zero-copy */
2994 		xdp_ring_err = ice_realloc_zc_buf(vsi, false);
2995 		if (xdp_ring_err)
2996 			NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Rx resources failed");
2997 	}
2998 
2999 resume_if:
3000 	if (if_running)
3001 		ret = ice_up(vsi);
3002 
3003 	if (!ret && prog)
3004 		ice_vsi_rx_napi_schedule(vsi);
3005 
3006 	return (ret || xdp_ring_err) ? -ENOMEM : 0;
3007 }
3008 
3009 /**
3010  * ice_xdp_safe_mode - XDP handler for safe mode
3011  * @dev: netdevice
3012  * @xdp: XDP command
3013  */
ice_xdp_safe_mode(struct net_device __always_unused * dev,struct netdev_bpf * xdp)3014 static int ice_xdp_safe_mode(struct net_device __always_unused *dev,
3015 			     struct netdev_bpf *xdp)
3016 {
3017 	NL_SET_ERR_MSG_MOD(xdp->extack,
3018 			   "Please provide working DDP firmware package in order to use XDP\n"
3019 			   "Refer to Documentation/networking/device_drivers/ethernet/intel/ice.rst");
3020 	return -EOPNOTSUPP;
3021 }
3022 
3023 /**
3024  * ice_xdp - implements XDP handler
3025  * @dev: netdevice
3026  * @xdp: XDP command
3027  */
ice_xdp(struct net_device * dev,struct netdev_bpf * xdp)3028 static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3029 {
3030 	struct ice_netdev_priv *np = netdev_priv(dev);
3031 	struct ice_vsi *vsi = np->vsi;
3032 	int ret;
3033 
3034 	if (vsi->type != ICE_VSI_PF) {
3035 		NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF VSI");
3036 		return -EINVAL;
3037 	}
3038 
3039 	mutex_lock(&vsi->xdp_state_lock);
3040 
3041 	switch (xdp->command) {
3042 	case XDP_SETUP_PROG:
3043 		ret = ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
3044 		break;
3045 	case XDP_SETUP_XSK_POOL:
3046 		ret = ice_xsk_pool_setup(vsi, xdp->xsk.pool, xdp->xsk.queue_id);
3047 		break;
3048 	default:
3049 		ret = -EINVAL;
3050 	}
3051 
3052 	mutex_unlock(&vsi->xdp_state_lock);
3053 	return ret;
3054 }
3055 
3056 /**
3057  * ice_ena_misc_vector - enable the non-queue interrupts
3058  * @pf: board private structure
3059  */
ice_ena_misc_vector(struct ice_pf * pf)3060 static void ice_ena_misc_vector(struct ice_pf *pf)
3061 {
3062 	struct ice_hw *hw = &pf->hw;
3063 	u32 val;
3064 
3065 	/* Disable anti-spoof detection interrupt to prevent spurious event
3066 	 * interrupts during a function reset. Anti-spoof functionally is
3067 	 * still supported.
3068 	 */
3069 	val = rd32(hw, GL_MDCK_TX_TDPU);
3070 	val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
3071 	wr32(hw, GL_MDCK_TX_TDPU, val);
3072 
3073 	/* clear things first */
3074 	wr32(hw, PFINT_OICR_ENA, 0);	/* disable all */
3075 	rd32(hw, PFINT_OICR);		/* read to clear */
3076 
3077 	val = (PFINT_OICR_ECC_ERR_M |
3078 	       PFINT_OICR_MAL_DETECT_M |
3079 	       PFINT_OICR_GRST_M |
3080 	       PFINT_OICR_PCI_EXCEPTION_M |
3081 	       PFINT_OICR_VFLR_M |
3082 	       PFINT_OICR_HMC_ERR_M |
3083 	       PFINT_OICR_PE_PUSH_M |
3084 	       PFINT_OICR_PE_CRITERR_M);
3085 
3086 	wr32(hw, PFINT_OICR_ENA, val);
3087 
3088 	/* SW_ITR_IDX = 0, but don't change INTENA */
3089 	wr32(hw, GLINT_DYN_CTL(pf->oicr_irq.index),
3090 	     GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
3091 }
3092 
3093 /**
3094  * ice_misc_intr - misc interrupt handler
3095  * @irq: interrupt number
3096  * @data: pointer to a q_vector
3097  */
ice_misc_intr(int __always_unused irq,void * data)3098 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
3099 {
3100 	struct ice_pf *pf = (struct ice_pf *)data;
3101 	struct ice_hw *hw = &pf->hw;
3102 	struct device *dev;
3103 	u32 oicr, ena_mask;
3104 
3105 	dev = ice_pf_to_dev(pf);
3106 	set_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
3107 	set_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
3108 	set_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
3109 
3110 	oicr = rd32(hw, PFINT_OICR);
3111 	ena_mask = rd32(hw, PFINT_OICR_ENA);
3112 
3113 	if (oicr & PFINT_OICR_SWINT_M) {
3114 		ena_mask &= ~PFINT_OICR_SWINT_M;
3115 		pf->sw_int_count++;
3116 	}
3117 
3118 	if (oicr & PFINT_OICR_MAL_DETECT_M) {
3119 		ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
3120 		set_bit(ICE_MDD_EVENT_PENDING, pf->state);
3121 	}
3122 	if (oicr & PFINT_OICR_VFLR_M) {
3123 		/* disable any further VFLR event notifications */
3124 		if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
3125 			u32 reg = rd32(hw, PFINT_OICR_ENA);
3126 
3127 			reg &= ~PFINT_OICR_VFLR_M;
3128 			wr32(hw, PFINT_OICR_ENA, reg);
3129 		} else {
3130 			ena_mask &= ~PFINT_OICR_VFLR_M;
3131 			set_bit(ICE_VFLR_EVENT_PENDING, pf->state);
3132 		}
3133 	}
3134 
3135 	if (oicr & PFINT_OICR_GRST_M) {
3136 		u32 reset;
3137 
3138 		/* we have a reset warning */
3139 		ena_mask &= ~PFINT_OICR_GRST_M;
3140 		reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
3141 			GLGEN_RSTAT_RESET_TYPE_S;
3142 
3143 		if (reset == ICE_RESET_CORER)
3144 			pf->corer_count++;
3145 		else if (reset == ICE_RESET_GLOBR)
3146 			pf->globr_count++;
3147 		else if (reset == ICE_RESET_EMPR)
3148 			pf->empr_count++;
3149 		else
3150 			dev_dbg(dev, "Invalid reset type %d\n", reset);
3151 
3152 		/* If a reset cycle isn't already in progress, we set a bit in
3153 		 * pf->state so that the service task can start a reset/rebuild.
3154 		 */
3155 		if (!test_and_set_bit(ICE_RESET_OICR_RECV, pf->state)) {
3156 			if (reset == ICE_RESET_CORER)
3157 				set_bit(ICE_CORER_RECV, pf->state);
3158 			else if (reset == ICE_RESET_GLOBR)
3159 				set_bit(ICE_GLOBR_RECV, pf->state);
3160 			else
3161 				set_bit(ICE_EMPR_RECV, pf->state);
3162 
3163 			/* There are couple of different bits at play here.
3164 			 * hw->reset_ongoing indicates whether the hardware is
3165 			 * in reset. This is set to true when a reset interrupt
3166 			 * is received and set back to false after the driver
3167 			 * has determined that the hardware is out of reset.
3168 			 *
3169 			 * ICE_RESET_OICR_RECV in pf->state indicates
3170 			 * that a post reset rebuild is required before the
3171 			 * driver is operational again. This is set above.
3172 			 *
3173 			 * As this is the start of the reset/rebuild cycle, set
3174 			 * both to indicate that.
3175 			 */
3176 			hw->reset_ongoing = true;
3177 		}
3178 	}
3179 
3180 	if (oicr & PFINT_OICR_TSYN_TX_M) {
3181 		ena_mask &= ~PFINT_OICR_TSYN_TX_M;
3182 		if (!hw->reset_ongoing)
3183 			set_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread);
3184 	}
3185 
3186 	if (oicr & PFINT_OICR_TSYN_EVNT_M) {
3187 		u8 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3188 		u32 gltsyn_stat = rd32(hw, GLTSYN_STAT(tmr_idx));
3189 
3190 		ena_mask &= ~PFINT_OICR_TSYN_EVNT_M;
3191 
3192 		if (hw->func_caps.ts_func_info.src_tmr_owned) {
3193 			/* Save EVENTs from GLTSYN register */
3194 			pf->ptp.ext_ts_irq |= gltsyn_stat &
3195 					      (GLTSYN_STAT_EVENT0_M |
3196 					       GLTSYN_STAT_EVENT1_M |
3197 					       GLTSYN_STAT_EVENT2_M);
3198 
3199 			set_bit(ICE_MISC_THREAD_EXTTS_EVENT, pf->misc_thread);
3200 		}
3201 	}
3202 
3203 #define ICE_AUX_CRIT_ERR (PFINT_OICR_PE_CRITERR_M | PFINT_OICR_HMC_ERR_M | PFINT_OICR_PE_PUSH_M)
3204 	if (oicr & ICE_AUX_CRIT_ERR) {
3205 		pf->oicr_err_reg |= oicr;
3206 		set_bit(ICE_AUX_ERR_PENDING, pf->state);
3207 		ena_mask &= ~ICE_AUX_CRIT_ERR;
3208 	}
3209 
3210 	/* Report any remaining unexpected interrupts */
3211 	oicr &= ena_mask;
3212 	if (oicr) {
3213 		dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
3214 		/* If a critical error is pending there is no choice but to
3215 		 * reset the device.
3216 		 */
3217 		if (oicr & (PFINT_OICR_PCI_EXCEPTION_M |
3218 			    PFINT_OICR_ECC_ERR_M)) {
3219 			set_bit(ICE_PFR_REQ, pf->state);
3220 		}
3221 	}
3222 
3223 	return IRQ_WAKE_THREAD;
3224 }
3225 
3226 /**
3227  * ice_misc_intr_thread_fn - misc interrupt thread function
3228  * @irq: interrupt number
3229  * @data: pointer to a q_vector
3230  */
ice_misc_intr_thread_fn(int __always_unused irq,void * data)3231 static irqreturn_t ice_misc_intr_thread_fn(int __always_unused irq, void *data)
3232 {
3233 	struct ice_pf *pf = data;
3234 	struct ice_hw *hw;
3235 
3236 	hw = &pf->hw;
3237 
3238 	if (ice_is_reset_in_progress(pf->state))
3239 		return IRQ_HANDLED;
3240 
3241 	ice_service_task_schedule(pf);
3242 
3243 	if (test_and_clear_bit(ICE_MISC_THREAD_EXTTS_EVENT, pf->misc_thread))
3244 		ice_ptp_extts_event(pf);
3245 
3246 	if (test_and_clear_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread)) {
3247 		/* Process outstanding Tx timestamps. If there is more work,
3248 		 * re-arm the interrupt to trigger again.
3249 		 */
3250 		if (ice_ptp_process_ts(pf) == ICE_TX_TSTAMP_WORK_PENDING) {
3251 			wr32(hw, PFINT_OICR, PFINT_OICR_TSYN_TX_M);
3252 			ice_flush(hw);
3253 		}
3254 	}
3255 
3256 	ice_irq_dynamic_ena(hw, NULL, NULL);
3257 
3258 	return IRQ_HANDLED;
3259 }
3260 
3261 /**
3262  * ice_dis_ctrlq_interrupts - disable control queue interrupts
3263  * @hw: pointer to HW structure
3264  */
ice_dis_ctrlq_interrupts(struct ice_hw * hw)3265 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
3266 {
3267 	/* disable Admin queue Interrupt causes */
3268 	wr32(hw, PFINT_FW_CTL,
3269 	     rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
3270 
3271 	/* disable Mailbox queue Interrupt causes */
3272 	wr32(hw, PFINT_MBX_CTL,
3273 	     rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
3274 
3275 	wr32(hw, PFINT_SB_CTL,
3276 	     rd32(hw, PFINT_SB_CTL) & ~PFINT_SB_CTL_CAUSE_ENA_M);
3277 
3278 	/* disable Control queue Interrupt causes */
3279 	wr32(hw, PFINT_OICR_CTL,
3280 	     rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
3281 
3282 	ice_flush(hw);
3283 }
3284 
3285 /**
3286  * ice_free_irq_msix_misc - Unroll misc vector setup
3287  * @pf: board private structure
3288  */
ice_free_irq_msix_misc(struct ice_pf * pf)3289 static void ice_free_irq_msix_misc(struct ice_pf *pf)
3290 {
3291 	int misc_irq_num = pf->oicr_irq.virq;
3292 	struct ice_hw *hw = &pf->hw;
3293 
3294 	ice_dis_ctrlq_interrupts(hw);
3295 
3296 	/* disable OICR interrupt */
3297 	wr32(hw, PFINT_OICR_ENA, 0);
3298 	ice_flush(hw);
3299 
3300 	synchronize_irq(misc_irq_num);
3301 	devm_free_irq(ice_pf_to_dev(pf), misc_irq_num, pf);
3302 
3303 	ice_free_irq(pf, pf->oicr_irq);
3304 }
3305 
3306 /**
3307  * ice_ena_ctrlq_interrupts - enable control queue interrupts
3308  * @hw: pointer to HW structure
3309  * @reg_idx: HW vector index to associate the control queue interrupts with
3310  */
ice_ena_ctrlq_interrupts(struct ice_hw * hw,u16 reg_idx)3311 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
3312 {
3313 	u32 val;
3314 
3315 	val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
3316 	       PFINT_OICR_CTL_CAUSE_ENA_M);
3317 	wr32(hw, PFINT_OICR_CTL, val);
3318 
3319 	/* enable Admin queue Interrupt causes */
3320 	val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
3321 	       PFINT_FW_CTL_CAUSE_ENA_M);
3322 	wr32(hw, PFINT_FW_CTL, val);
3323 
3324 	/* enable Mailbox queue Interrupt causes */
3325 	val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
3326 	       PFINT_MBX_CTL_CAUSE_ENA_M);
3327 	wr32(hw, PFINT_MBX_CTL, val);
3328 
3329 	/* This enables Sideband queue Interrupt causes */
3330 	val = ((reg_idx & PFINT_SB_CTL_MSIX_INDX_M) |
3331 	       PFINT_SB_CTL_CAUSE_ENA_M);
3332 	wr32(hw, PFINT_SB_CTL, val);
3333 
3334 	ice_flush(hw);
3335 }
3336 
3337 /**
3338  * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
3339  * @pf: board private structure
3340  *
3341  * This sets up the handler for MSIX 0, which is used to manage the
3342  * non-queue interrupts, e.g. AdminQ and errors. This is not used
3343  * when in MSI or Legacy interrupt mode.
3344  */
ice_req_irq_msix_misc(struct ice_pf * pf)3345 static int ice_req_irq_msix_misc(struct ice_pf *pf)
3346 {
3347 	struct device *dev = ice_pf_to_dev(pf);
3348 	struct ice_hw *hw = &pf->hw;
3349 	struct msi_map oicr_irq;
3350 	int err = 0;
3351 
3352 	if (!pf->int_name[0])
3353 		snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
3354 			 dev_driver_string(dev), dev_name(dev));
3355 
3356 	/* Do not request IRQ but do enable OICR interrupt since settings are
3357 	 * lost during reset. Note that this function is called only during
3358 	 * rebuild path and not while reset is in progress.
3359 	 */
3360 	if (ice_is_reset_in_progress(pf->state))
3361 		goto skip_req_irq;
3362 
3363 	/* reserve one vector in irq_tracker for misc interrupts */
3364 	oicr_irq = ice_alloc_irq(pf, false);
3365 	if (oicr_irq.index < 0)
3366 		return oicr_irq.index;
3367 
3368 	pf->oicr_irq = oicr_irq;
3369 	err = devm_request_threaded_irq(dev, pf->oicr_irq.virq, ice_misc_intr,
3370 					ice_misc_intr_thread_fn, 0,
3371 					pf->int_name, pf);
3372 	if (err) {
3373 		dev_err(dev, "devm_request_threaded_irq for %s failed: %d\n",
3374 			pf->int_name, err);
3375 		ice_free_irq(pf, pf->oicr_irq);
3376 		return err;
3377 	}
3378 
3379 skip_req_irq:
3380 	ice_ena_misc_vector(pf);
3381 
3382 	ice_ena_ctrlq_interrupts(hw, pf->oicr_irq.index);
3383 	wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_irq.index),
3384 	     ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
3385 
3386 	ice_flush(hw);
3387 	ice_irq_dynamic_ena(hw, NULL, NULL);
3388 
3389 	return 0;
3390 }
3391 
3392 /**
3393  * ice_napi_add - register NAPI handler for the VSI
3394  * @vsi: VSI for which NAPI handler is to be registered
3395  *
3396  * This function is only called in the driver's load path. Registering the NAPI
3397  * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
3398  * reset/rebuild, etc.)
3399  */
ice_napi_add(struct ice_vsi * vsi)3400 static void ice_napi_add(struct ice_vsi *vsi)
3401 {
3402 	int v_idx;
3403 
3404 	if (!vsi->netdev)
3405 		return;
3406 
3407 	ice_for_each_q_vector(vsi, v_idx)
3408 		netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
3409 			       ice_napi_poll);
3410 }
3411 
3412 /**
3413  * ice_set_ops - set netdev and ethtools ops for the given netdev
3414  * @vsi: the VSI associated with the new netdev
3415  */
ice_set_ops(struct ice_vsi * vsi)3416 static void ice_set_ops(struct ice_vsi *vsi)
3417 {
3418 	struct net_device *netdev = vsi->netdev;
3419 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
3420 
3421 	if (ice_is_safe_mode(pf)) {
3422 		netdev->netdev_ops = &ice_netdev_safe_mode_ops;
3423 		ice_set_ethtool_safe_mode_ops(netdev);
3424 		return;
3425 	}
3426 
3427 	netdev->netdev_ops = &ice_netdev_ops;
3428 	netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
3429 	ice_set_ethtool_ops(netdev);
3430 
3431 	if (vsi->type != ICE_VSI_PF)
3432 		return;
3433 
3434 	netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
3435 			       NETDEV_XDP_ACT_XSK_ZEROCOPY |
3436 			       NETDEV_XDP_ACT_RX_SG;
3437 	netdev->xdp_zc_max_segs = ICE_MAX_BUF_TXD;
3438 }
3439 
3440 /**
3441  * ice_set_netdev_features - set features for the given netdev
3442  * @netdev: netdev instance
3443  */
ice_set_netdev_features(struct net_device * netdev)3444 static void ice_set_netdev_features(struct net_device *netdev)
3445 {
3446 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
3447 	bool is_dvm_ena = ice_is_dvm_ena(&pf->hw);
3448 	netdev_features_t csumo_features;
3449 	netdev_features_t vlano_features;
3450 	netdev_features_t dflt_features;
3451 	netdev_features_t tso_features;
3452 
3453 	if (ice_is_safe_mode(pf)) {
3454 		/* safe mode */
3455 		netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
3456 		netdev->hw_features = netdev->features;
3457 		return;
3458 	}
3459 
3460 	dflt_features = NETIF_F_SG	|
3461 			NETIF_F_HIGHDMA	|
3462 			NETIF_F_NTUPLE	|
3463 			NETIF_F_RXHASH;
3464 
3465 	csumo_features = NETIF_F_RXCSUM	  |
3466 			 NETIF_F_IP_CSUM  |
3467 			 NETIF_F_SCTP_CRC |
3468 			 NETIF_F_IPV6_CSUM;
3469 
3470 	vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
3471 			 NETIF_F_HW_VLAN_CTAG_TX     |
3472 			 NETIF_F_HW_VLAN_CTAG_RX;
3473 
3474 	/* Enable CTAG/STAG filtering by default in Double VLAN Mode (DVM) */
3475 	if (is_dvm_ena)
3476 		vlano_features |= NETIF_F_HW_VLAN_STAG_FILTER;
3477 
3478 	tso_features = NETIF_F_TSO			|
3479 		       NETIF_F_TSO_ECN			|
3480 		       NETIF_F_TSO6			|
3481 		       NETIF_F_GSO_GRE			|
3482 		       NETIF_F_GSO_UDP_TUNNEL		|
3483 		       NETIF_F_GSO_GRE_CSUM		|
3484 		       NETIF_F_GSO_UDP_TUNNEL_CSUM	|
3485 		       NETIF_F_GSO_PARTIAL		|
3486 		       NETIF_F_GSO_IPXIP4		|
3487 		       NETIF_F_GSO_IPXIP6		|
3488 		       NETIF_F_GSO_UDP_L4;
3489 
3490 	netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3491 					NETIF_F_GSO_GRE_CSUM;
3492 	/* set features that user can change */
3493 	netdev->hw_features = dflt_features | csumo_features |
3494 			      vlano_features | tso_features;
3495 
3496 	/* add support for HW_CSUM on packets with MPLS header */
3497 	netdev->mpls_features =  NETIF_F_HW_CSUM |
3498 				 NETIF_F_TSO     |
3499 				 NETIF_F_TSO6;
3500 
3501 	/* enable features */
3502 	netdev->features |= netdev->hw_features;
3503 
3504 	netdev->hw_features |= NETIF_F_HW_TC;
3505 	netdev->hw_features |= NETIF_F_LOOPBACK;
3506 
3507 	/* encap and VLAN devices inherit default, csumo and tso features */
3508 	netdev->hw_enc_features |= dflt_features | csumo_features |
3509 				   tso_features;
3510 	netdev->vlan_features |= dflt_features | csumo_features |
3511 				 tso_features;
3512 
3513 	/* advertise support but don't enable by default since only one type of
3514 	 * VLAN offload can be enabled at a time (i.e. CTAG or STAG). When one
3515 	 * type turns on the other has to be turned off. This is enforced by the
3516 	 * ice_fix_features() ndo callback.
3517 	 */
3518 	if (is_dvm_ena)
3519 		netdev->hw_features |= NETIF_F_HW_VLAN_STAG_RX |
3520 			NETIF_F_HW_VLAN_STAG_TX;
3521 
3522 	/* Leave CRC / FCS stripping enabled by default, but allow the value to
3523 	 * be changed at runtime
3524 	 */
3525 	netdev->hw_features |= NETIF_F_RXFCS;
3526 
3527 	netif_set_tso_max_size(netdev, ICE_MAX_TSO_SIZE);
3528 }
3529 
3530 /**
3531  * ice_fill_rss_lut - Fill the RSS lookup table with default values
3532  * @lut: Lookup table
3533  * @rss_table_size: Lookup table size
3534  * @rss_size: Range of queue number for hashing
3535  */
ice_fill_rss_lut(u8 * lut,u16 rss_table_size,u16 rss_size)3536 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
3537 {
3538 	u16 i;
3539 
3540 	for (i = 0; i < rss_table_size; i++)
3541 		lut[i] = i % rss_size;
3542 }
3543 
3544 /**
3545  * ice_pf_vsi_setup - Set up a PF VSI
3546  * @pf: board private structure
3547  * @pi: pointer to the port_info instance
3548  *
3549  * Returns pointer to the successfully allocated VSI software struct
3550  * on success, otherwise returns NULL on failure.
3551  */
3552 static struct ice_vsi *
ice_pf_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi)3553 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3554 {
3555 	struct ice_vsi_cfg_params params = {};
3556 
3557 	params.type = ICE_VSI_PF;
3558 	params.pi = pi;
3559 	params.flags = ICE_VSI_FLAG_INIT;
3560 
3561 	return ice_vsi_setup(pf, &params);
3562 }
3563 
3564 static struct ice_vsi *
ice_chnl_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi,struct ice_channel * ch)3565 ice_chnl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
3566 		   struct ice_channel *ch)
3567 {
3568 	struct ice_vsi_cfg_params params = {};
3569 
3570 	params.type = ICE_VSI_CHNL;
3571 	params.pi = pi;
3572 	params.ch = ch;
3573 	params.flags = ICE_VSI_FLAG_INIT;
3574 
3575 	return ice_vsi_setup(pf, &params);
3576 }
3577 
3578 /**
3579  * ice_ctrl_vsi_setup - Set up a control VSI
3580  * @pf: board private structure
3581  * @pi: pointer to the port_info instance
3582  *
3583  * Returns pointer to the successfully allocated VSI software struct
3584  * on success, otherwise returns NULL on failure.
3585  */
3586 static struct ice_vsi *
ice_ctrl_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi)3587 ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3588 {
3589 	struct ice_vsi_cfg_params params = {};
3590 
3591 	params.type = ICE_VSI_CTRL;
3592 	params.pi = pi;
3593 	params.flags = ICE_VSI_FLAG_INIT;
3594 
3595 	return ice_vsi_setup(pf, &params);
3596 }
3597 
3598 /**
3599  * ice_lb_vsi_setup - Set up a loopback VSI
3600  * @pf: board private structure
3601  * @pi: pointer to the port_info instance
3602  *
3603  * Returns pointer to the successfully allocated VSI software struct
3604  * on success, otherwise returns NULL on failure.
3605  */
3606 struct ice_vsi *
ice_lb_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi)3607 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3608 {
3609 	struct ice_vsi_cfg_params params = {};
3610 
3611 	params.type = ICE_VSI_LB;
3612 	params.pi = pi;
3613 	params.flags = ICE_VSI_FLAG_INIT;
3614 
3615 	return ice_vsi_setup(pf, &params);
3616 }
3617 
3618 /**
3619  * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
3620  * @netdev: network interface to be adjusted
3621  * @proto: VLAN TPID
3622  * @vid: VLAN ID to be added
3623  *
3624  * net_device_ops implementation for adding VLAN IDs
3625  */
3626 static int
ice_vlan_rx_add_vid(struct net_device * netdev,__be16 proto,u16 vid)3627 ice_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
3628 {
3629 	struct ice_netdev_priv *np = netdev_priv(netdev);
3630 	struct ice_vsi_vlan_ops *vlan_ops;
3631 	struct ice_vsi *vsi = np->vsi;
3632 	struct ice_vlan vlan;
3633 	int ret;
3634 
3635 	/* VLAN 0 is added by default during load/reset */
3636 	if (!vid)
3637 		return 0;
3638 
3639 	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3640 		usleep_range(1000, 2000);
3641 
3642 	/* Add multicast promisc rule for the VLAN ID to be added if
3643 	 * all-multicast is currently enabled.
3644 	 */
3645 	if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3646 		ret = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3647 					       ICE_MCAST_VLAN_PROMISC_BITS,
3648 					       vid);
3649 		if (ret)
3650 			goto finish;
3651 	}
3652 
3653 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3654 
3655 	/* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
3656 	 * packets aren't pruned by the device's internal switch on Rx
3657 	 */
3658 	vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3659 	ret = vlan_ops->add_vlan(vsi, &vlan);
3660 	if (ret)
3661 		goto finish;
3662 
3663 	/* If all-multicast is currently enabled and this VLAN ID is only one
3664 	 * besides VLAN-0 we have to update look-up type of multicast promisc
3665 	 * rule for VLAN-0 from ICE_SW_LKUP_PROMISC to ICE_SW_LKUP_PROMISC_VLAN.
3666 	 */
3667 	if ((vsi->current_netdev_flags & IFF_ALLMULTI) &&
3668 	    ice_vsi_num_non_zero_vlans(vsi) == 1) {
3669 		ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3670 					   ICE_MCAST_PROMISC_BITS, 0);
3671 		ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3672 					 ICE_MCAST_VLAN_PROMISC_BITS, 0);
3673 	}
3674 
3675 finish:
3676 	clear_bit(ICE_CFG_BUSY, vsi->state);
3677 
3678 	return ret;
3679 }
3680 
3681 /**
3682  * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
3683  * @netdev: network interface to be adjusted
3684  * @proto: VLAN TPID
3685  * @vid: VLAN ID to be removed
3686  *
3687  * net_device_ops implementation for removing VLAN IDs
3688  */
3689 static int
ice_vlan_rx_kill_vid(struct net_device * netdev,__be16 proto,u16 vid)3690 ice_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
3691 {
3692 	struct ice_netdev_priv *np = netdev_priv(netdev);
3693 	struct ice_vsi_vlan_ops *vlan_ops;
3694 	struct ice_vsi *vsi = np->vsi;
3695 	struct ice_vlan vlan;
3696 	int ret;
3697 
3698 	/* don't allow removal of VLAN 0 */
3699 	if (!vid)
3700 		return 0;
3701 
3702 	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3703 		usleep_range(1000, 2000);
3704 
3705 	ret = ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3706 				    ICE_MCAST_VLAN_PROMISC_BITS, vid);
3707 	if (ret) {
3708 		netdev_err(netdev, "Error clearing multicast promiscuous mode on VSI %i\n",
3709 			   vsi->vsi_num);
3710 		vsi->current_netdev_flags |= IFF_ALLMULTI;
3711 	}
3712 
3713 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3714 
3715 	/* Make sure VLAN delete is successful before updating VLAN
3716 	 * information
3717 	 */
3718 	vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3719 	ret = vlan_ops->del_vlan(vsi, &vlan);
3720 	if (ret)
3721 		goto finish;
3722 
3723 	/* Remove multicast promisc rule for the removed VLAN ID if
3724 	 * all-multicast is enabled.
3725 	 */
3726 	if (vsi->current_netdev_flags & IFF_ALLMULTI)
3727 		ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3728 					   ICE_MCAST_VLAN_PROMISC_BITS, vid);
3729 
3730 	if (!ice_vsi_has_non_zero_vlans(vsi)) {
3731 		/* Update look-up type of multicast promisc rule for VLAN 0
3732 		 * from ICE_SW_LKUP_PROMISC_VLAN to ICE_SW_LKUP_PROMISC when
3733 		 * all-multicast is enabled and VLAN 0 is the only VLAN rule.
3734 		 */
3735 		if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3736 			ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3737 						   ICE_MCAST_VLAN_PROMISC_BITS,
3738 						   0);
3739 			ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3740 						 ICE_MCAST_PROMISC_BITS, 0);
3741 		}
3742 	}
3743 
3744 finish:
3745 	clear_bit(ICE_CFG_BUSY, vsi->state);
3746 
3747 	return ret;
3748 }
3749 
3750 /**
3751  * ice_rep_indr_tc_block_unbind
3752  * @cb_priv: indirection block private data
3753  */
ice_rep_indr_tc_block_unbind(void * cb_priv)3754 static void ice_rep_indr_tc_block_unbind(void *cb_priv)
3755 {
3756 	struct ice_indr_block_priv *indr_priv = cb_priv;
3757 
3758 	list_del(&indr_priv->list);
3759 	kfree(indr_priv);
3760 }
3761 
3762 /**
3763  * ice_tc_indir_block_unregister - Unregister TC indirect block notifications
3764  * @vsi: VSI struct which has the netdev
3765  */
ice_tc_indir_block_unregister(struct ice_vsi * vsi)3766 static void ice_tc_indir_block_unregister(struct ice_vsi *vsi)
3767 {
3768 	struct ice_netdev_priv *np = netdev_priv(vsi->netdev);
3769 
3770 	flow_indr_dev_unregister(ice_indr_setup_tc_cb, np,
3771 				 ice_rep_indr_tc_block_unbind);
3772 }
3773 
3774 /**
3775  * ice_tc_indir_block_register - Register TC indirect block notifications
3776  * @vsi: VSI struct which has the netdev
3777  *
3778  * Returns 0 on success, negative value on failure
3779  */
ice_tc_indir_block_register(struct ice_vsi * vsi)3780 static int ice_tc_indir_block_register(struct ice_vsi *vsi)
3781 {
3782 	struct ice_netdev_priv *np;
3783 
3784 	if (!vsi || !vsi->netdev)
3785 		return -EINVAL;
3786 
3787 	np = netdev_priv(vsi->netdev);
3788 
3789 	INIT_LIST_HEAD(&np->tc_indr_block_priv_list);
3790 	return flow_indr_dev_register(ice_indr_setup_tc_cb, np);
3791 }
3792 
3793 /**
3794  * ice_get_avail_q_count - Get count of queues in use
3795  * @pf_qmap: bitmap to get queue use count from
3796  * @lock: pointer to a mutex that protects access to pf_qmap
3797  * @size: size of the bitmap
3798  */
3799 static u16
ice_get_avail_q_count(unsigned long * pf_qmap,struct mutex * lock,u16 size)3800 ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
3801 {
3802 	unsigned long bit;
3803 	u16 count = 0;
3804 
3805 	mutex_lock(lock);
3806 	for_each_clear_bit(bit, pf_qmap, size)
3807 		count++;
3808 	mutex_unlock(lock);
3809 
3810 	return count;
3811 }
3812 
3813 /**
3814  * ice_get_avail_txq_count - Get count of Tx queues in use
3815  * @pf: pointer to an ice_pf instance
3816  */
ice_get_avail_txq_count(struct ice_pf * pf)3817 u16 ice_get_avail_txq_count(struct ice_pf *pf)
3818 {
3819 	return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
3820 				     pf->max_pf_txqs);
3821 }
3822 
3823 /**
3824  * ice_get_avail_rxq_count - Get count of Rx queues in use
3825  * @pf: pointer to an ice_pf instance
3826  */
ice_get_avail_rxq_count(struct ice_pf * pf)3827 u16 ice_get_avail_rxq_count(struct ice_pf *pf)
3828 {
3829 	return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
3830 				     pf->max_pf_rxqs);
3831 }
3832 
3833 /**
3834  * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3835  * @pf: board private structure to initialize
3836  */
ice_deinit_pf(struct ice_pf * pf)3837 static void ice_deinit_pf(struct ice_pf *pf)
3838 {
3839 	ice_service_task_stop(pf);
3840 	mutex_destroy(&pf->lag_mutex);
3841 	mutex_destroy(&pf->adev_mutex);
3842 	mutex_destroy(&pf->sw_mutex);
3843 	mutex_destroy(&pf->tc_mutex);
3844 	mutex_destroy(&pf->avail_q_mutex);
3845 	mutex_destroy(&pf->vfs.table_lock);
3846 
3847 	if (pf->avail_txqs) {
3848 		bitmap_free(pf->avail_txqs);
3849 		pf->avail_txqs = NULL;
3850 	}
3851 
3852 	if (pf->avail_rxqs) {
3853 		bitmap_free(pf->avail_rxqs);
3854 		pf->avail_rxqs = NULL;
3855 	}
3856 
3857 	if (pf->ptp.clock)
3858 		ptp_clock_unregister(pf->ptp.clock);
3859 }
3860 
3861 /**
3862  * ice_set_pf_caps - set PFs capability flags
3863  * @pf: pointer to the PF instance
3864  */
ice_set_pf_caps(struct ice_pf * pf)3865 static void ice_set_pf_caps(struct ice_pf *pf)
3866 {
3867 	struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
3868 
3869 	clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3870 	if (func_caps->common_cap.rdma)
3871 		set_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3872 	clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3873 	if (func_caps->common_cap.dcb)
3874 		set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3875 	clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3876 	if (func_caps->common_cap.sr_iov_1_1) {
3877 		set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3878 		pf->vfs.num_supported = min_t(int, func_caps->num_allocd_vfs,
3879 					      ICE_MAX_SRIOV_VFS);
3880 	}
3881 	clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
3882 	if (func_caps->common_cap.rss_table_size)
3883 		set_bit(ICE_FLAG_RSS_ENA, pf->flags);
3884 
3885 	clear_bit(ICE_FLAG_FD_ENA, pf->flags);
3886 	if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
3887 		u16 unused;
3888 
3889 		/* ctrl_vsi_idx will be set to a valid value when flow director
3890 		 * is setup by ice_init_fdir
3891 		 */
3892 		pf->ctrl_vsi_idx = ICE_NO_VSI;
3893 		set_bit(ICE_FLAG_FD_ENA, pf->flags);
3894 		/* force guaranteed filter pool for PF */
3895 		ice_alloc_fd_guar_item(&pf->hw, &unused,
3896 				       func_caps->fd_fltr_guar);
3897 		/* force shared filter pool for PF */
3898 		ice_alloc_fd_shrd_item(&pf->hw, &unused,
3899 				       func_caps->fd_fltr_best_effort);
3900 	}
3901 
3902 	clear_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
3903 	if (func_caps->common_cap.ieee_1588 &&
3904 	    !(pf->hw.mac_type == ICE_MAC_E830))
3905 		set_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
3906 
3907 	pf->max_pf_txqs = func_caps->common_cap.num_txq;
3908 	pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
3909 }
3910 
3911 /**
3912  * ice_init_pf - Initialize general software structures (struct ice_pf)
3913  * @pf: board private structure to initialize
3914  */
ice_init_pf(struct ice_pf * pf)3915 static int ice_init_pf(struct ice_pf *pf)
3916 {
3917 	ice_set_pf_caps(pf);
3918 
3919 	mutex_init(&pf->sw_mutex);
3920 	mutex_init(&pf->tc_mutex);
3921 	mutex_init(&pf->adev_mutex);
3922 	mutex_init(&pf->lag_mutex);
3923 
3924 	INIT_HLIST_HEAD(&pf->aq_wait_list);
3925 	spin_lock_init(&pf->aq_wait_lock);
3926 	init_waitqueue_head(&pf->aq_wait_queue);
3927 
3928 	init_waitqueue_head(&pf->reset_wait_queue);
3929 
3930 	/* setup service timer and periodic service task */
3931 	timer_setup(&pf->serv_tmr, ice_service_timer, 0);
3932 	pf->serv_tmr_period = HZ;
3933 	INIT_WORK(&pf->serv_task, ice_service_task);
3934 	clear_bit(ICE_SERVICE_SCHED, pf->state);
3935 
3936 	mutex_init(&pf->avail_q_mutex);
3937 	pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
3938 	if (!pf->avail_txqs)
3939 		return -ENOMEM;
3940 
3941 	pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
3942 	if (!pf->avail_rxqs) {
3943 		bitmap_free(pf->avail_txqs);
3944 		pf->avail_txqs = NULL;
3945 		return -ENOMEM;
3946 	}
3947 
3948 	mutex_init(&pf->vfs.table_lock);
3949 	hash_init(pf->vfs.table);
3950 	if (ice_is_feature_supported(pf, ICE_F_MBX_LIMIT))
3951 		wr32(&pf->hw, E830_MBX_PF_IN_FLIGHT_VF_MSGS_THRESH,
3952 		     ICE_MBX_OVERFLOW_WATERMARK);
3953 	else
3954 		ice_mbx_init_snapshot(&pf->hw);
3955 
3956 	return 0;
3957 }
3958 
3959 /**
3960  * ice_is_wol_supported - check if WoL is supported
3961  * @hw: pointer to hardware info
3962  *
3963  * Check if WoL is supported based on the HW configuration.
3964  * Returns true if NVM supports and enables WoL for this port, false otherwise
3965  */
ice_is_wol_supported(struct ice_hw * hw)3966 bool ice_is_wol_supported(struct ice_hw *hw)
3967 {
3968 	u16 wol_ctrl;
3969 
3970 	/* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
3971 	 * word) indicates WoL is not supported on the corresponding PF ID.
3972 	 */
3973 	if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
3974 		return false;
3975 
3976 	return !(BIT(hw->port_info->lport) & wol_ctrl);
3977 }
3978 
3979 /**
3980  * ice_vsi_recfg_qs - Change the number of queues on a VSI
3981  * @vsi: VSI being changed
3982  * @new_rx: new number of Rx queues
3983  * @new_tx: new number of Tx queues
3984  * @locked: is adev device_lock held
3985  *
3986  * Only change the number of queues if new_tx, or new_rx is non-0.
3987  *
3988  * Returns 0 on success.
3989  */
ice_vsi_recfg_qs(struct ice_vsi * vsi,int new_rx,int new_tx,bool locked)3990 int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx, bool locked)
3991 {
3992 	struct ice_pf *pf = vsi->back;
3993 	int i, err = 0, timeout = 50;
3994 
3995 	if (!new_rx && !new_tx)
3996 		return -EINVAL;
3997 
3998 	while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
3999 		timeout--;
4000 		if (!timeout)
4001 			return -EBUSY;
4002 		usleep_range(1000, 2000);
4003 	}
4004 
4005 	if (new_tx)
4006 		vsi->req_txq = (u16)new_tx;
4007 	if (new_rx)
4008 		vsi->req_rxq = (u16)new_rx;
4009 
4010 	/* set for the next time the netdev is started */
4011 	if (!netif_running(vsi->netdev)) {
4012 		err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
4013 		if (err)
4014 			goto rebuild_err;
4015 		dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
4016 		goto done;
4017 	}
4018 
4019 	ice_vsi_close(vsi);
4020 	err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
4021 	if (err)
4022 		goto rebuild_err;
4023 
4024 	ice_for_each_traffic_class(i) {
4025 		if (vsi->tc_cfg.ena_tc & BIT(i))
4026 			netdev_set_tc_queue(vsi->netdev,
4027 					    vsi->tc_cfg.tc_info[i].netdev_tc,
4028 					    vsi->tc_cfg.tc_info[i].qcount_tx,
4029 					    vsi->tc_cfg.tc_info[i].qoffset);
4030 	}
4031 	ice_pf_dcb_recfg(pf, locked);
4032 	ice_vsi_open(vsi);
4033 	goto done;
4034 
4035 rebuild_err:
4036 	dev_err(ice_pf_to_dev(pf), "Error during VSI rebuild: %d. Unload and reload the driver.\n",
4037 		err);
4038 done:
4039 	clear_bit(ICE_CFG_BUSY, pf->state);
4040 	return err;
4041 }
4042 
4043 /**
4044  * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
4045  * @pf: PF to configure
4046  *
4047  * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
4048  * VSI can still Tx/Rx VLAN tagged packets.
4049  */
ice_set_safe_mode_vlan_cfg(struct ice_pf * pf)4050 static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
4051 {
4052 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
4053 	struct ice_vsi_ctx *ctxt;
4054 	struct ice_hw *hw;
4055 	int status;
4056 
4057 	if (!vsi)
4058 		return;
4059 
4060 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
4061 	if (!ctxt)
4062 		return;
4063 
4064 	hw = &pf->hw;
4065 	ctxt->info = vsi->info;
4066 
4067 	ctxt->info.valid_sections =
4068 		cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
4069 			    ICE_AQ_VSI_PROP_SECURITY_VALID |
4070 			    ICE_AQ_VSI_PROP_SW_VALID);
4071 
4072 	/* disable VLAN anti-spoof */
4073 	ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4074 				  ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4075 
4076 	/* disable VLAN pruning and keep all other settings */
4077 	ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
4078 
4079 	/* allow all VLANs on Tx and don't strip on Rx */
4080 	ctxt->info.inner_vlan_flags = ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL |
4081 		ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING;
4082 
4083 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4084 	if (status) {
4085 		dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %d aq_err %s\n",
4086 			status, ice_aq_str(hw->adminq.sq_last_status));
4087 	} else {
4088 		vsi->info.sec_flags = ctxt->info.sec_flags;
4089 		vsi->info.sw_flags2 = ctxt->info.sw_flags2;
4090 		vsi->info.inner_vlan_flags = ctxt->info.inner_vlan_flags;
4091 	}
4092 
4093 	kfree(ctxt);
4094 }
4095 
4096 /**
4097  * ice_log_pkg_init - log result of DDP package load
4098  * @hw: pointer to hardware info
4099  * @state: state of package load
4100  */
ice_log_pkg_init(struct ice_hw * hw,enum ice_ddp_state state)4101 static void ice_log_pkg_init(struct ice_hw *hw, enum ice_ddp_state state)
4102 {
4103 	struct ice_pf *pf = hw->back;
4104 	struct device *dev;
4105 
4106 	dev = ice_pf_to_dev(pf);
4107 
4108 	switch (state) {
4109 	case ICE_DDP_PKG_SUCCESS:
4110 		dev_info(dev, "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
4111 			 hw->active_pkg_name,
4112 			 hw->active_pkg_ver.major,
4113 			 hw->active_pkg_ver.minor,
4114 			 hw->active_pkg_ver.update,
4115 			 hw->active_pkg_ver.draft);
4116 		break;
4117 	case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
4118 		dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
4119 			 hw->active_pkg_name,
4120 			 hw->active_pkg_ver.major,
4121 			 hw->active_pkg_ver.minor,
4122 			 hw->active_pkg_ver.update,
4123 			 hw->active_pkg_ver.draft);
4124 		break;
4125 	case ICE_DDP_PKG_ALREADY_LOADED_NOT_SUPPORTED:
4126 		dev_err(dev, "The device has a DDP package that is not supported by the driver.  The device has package '%s' version %d.%d.x.x.  The driver requires version %d.%d.x.x.  Entering Safe Mode.\n",
4127 			hw->active_pkg_name,
4128 			hw->active_pkg_ver.major,
4129 			hw->active_pkg_ver.minor,
4130 			ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4131 		break;
4132 	case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
4133 		dev_info(dev, "The driver could not load the DDP package file because a compatible DDP package is already present on the device.  The device has package '%s' version %d.%d.%d.%d.  The package file found by the driver: '%s' version %d.%d.%d.%d.\n",
4134 			 hw->active_pkg_name,
4135 			 hw->active_pkg_ver.major,
4136 			 hw->active_pkg_ver.minor,
4137 			 hw->active_pkg_ver.update,
4138 			 hw->active_pkg_ver.draft,
4139 			 hw->pkg_name,
4140 			 hw->pkg_ver.major,
4141 			 hw->pkg_ver.minor,
4142 			 hw->pkg_ver.update,
4143 			 hw->pkg_ver.draft);
4144 		break;
4145 	case ICE_DDP_PKG_FW_MISMATCH:
4146 		dev_err(dev, "The firmware loaded on the device is not compatible with the DDP package.  Please update the device's NVM.  Entering safe mode.\n");
4147 		break;
4148 	case ICE_DDP_PKG_INVALID_FILE:
4149 		dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
4150 		break;
4151 	case ICE_DDP_PKG_FILE_VERSION_TOO_HIGH:
4152 		dev_err(dev, "The DDP package file version is higher than the driver supports.  Please use an updated driver.  Entering Safe Mode.\n");
4153 		break;
4154 	case ICE_DDP_PKG_FILE_VERSION_TOO_LOW:
4155 		dev_err(dev, "The DDP package file version is lower than the driver supports.  The driver requires version %d.%d.x.x.  Please use an updated DDP Package file.  Entering Safe Mode.\n",
4156 			ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4157 		break;
4158 	case ICE_DDP_PKG_FILE_SIGNATURE_INVALID:
4159 		dev_err(dev, "The DDP package could not be loaded because its signature is not valid.  Please use a valid DDP Package.  Entering Safe Mode.\n");
4160 		break;
4161 	case ICE_DDP_PKG_FILE_REVISION_TOO_LOW:
4162 		dev_err(dev, "The DDP Package could not be loaded because its security revision is too low.  Please use an updated DDP Package.  Entering Safe Mode.\n");
4163 		break;
4164 	case ICE_DDP_PKG_LOAD_ERROR:
4165 		dev_err(dev, "An error occurred on the device while loading the DDP package.  The device will be reset.\n");
4166 		/* poll for reset to complete */
4167 		if (ice_check_reset(hw))
4168 			dev_err(dev, "Error resetting device. Please reload the driver\n");
4169 		break;
4170 	case ICE_DDP_PKG_ERR:
4171 	default:
4172 		dev_err(dev, "An unknown error occurred when loading the DDP package.  Entering Safe Mode.\n");
4173 		break;
4174 	}
4175 }
4176 
4177 /**
4178  * ice_load_pkg - load/reload the DDP Package file
4179  * @firmware: firmware structure when firmware requested or NULL for reload
4180  * @pf: pointer to the PF instance
4181  *
4182  * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
4183  * initialize HW tables.
4184  */
4185 static void
ice_load_pkg(const struct firmware * firmware,struct ice_pf * pf)4186 ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
4187 {
4188 	enum ice_ddp_state state = ICE_DDP_PKG_ERR;
4189 	struct device *dev = ice_pf_to_dev(pf);
4190 	struct ice_hw *hw = &pf->hw;
4191 
4192 	/* Load DDP Package */
4193 	if (firmware && !hw->pkg_copy) {
4194 		state = ice_copy_and_init_pkg(hw, firmware->data,
4195 					      firmware->size);
4196 		ice_log_pkg_init(hw, state);
4197 	} else if (!firmware && hw->pkg_copy) {
4198 		/* Reload package during rebuild after CORER/GLOBR reset */
4199 		state = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
4200 		ice_log_pkg_init(hw, state);
4201 	} else {
4202 		dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
4203 	}
4204 
4205 	if (!ice_is_init_pkg_successful(state)) {
4206 		/* Safe Mode */
4207 		clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4208 		return;
4209 	}
4210 
4211 	/* Successful download package is the precondition for advanced
4212 	 * features, hence setting the ICE_FLAG_ADV_FEATURES flag
4213 	 */
4214 	set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4215 }
4216 
4217 /**
4218  * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
4219  * @pf: pointer to the PF structure
4220  *
4221  * There is no error returned here because the driver should be able to handle
4222  * 128 Byte cache lines, so we only print a warning in case issues are seen,
4223  * specifically with Tx.
4224  */
ice_verify_cacheline_size(struct ice_pf * pf)4225 static void ice_verify_cacheline_size(struct ice_pf *pf)
4226 {
4227 	if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
4228 		dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
4229 			 ICE_CACHE_LINE_BYTES);
4230 }
4231 
4232 /**
4233  * ice_send_version - update firmware with driver version
4234  * @pf: PF struct
4235  *
4236  * Returns 0 on success, else error code
4237  */
ice_send_version(struct ice_pf * pf)4238 static int ice_send_version(struct ice_pf *pf)
4239 {
4240 	struct ice_driver_ver dv;
4241 
4242 	dv.major_ver = 0xff;
4243 	dv.minor_ver = 0xff;
4244 	dv.build_ver = 0xff;
4245 	dv.subbuild_ver = 0;
4246 	strscpy((char *)dv.driver_string, UTS_RELEASE,
4247 		sizeof(dv.driver_string));
4248 	return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
4249 }
4250 
4251 /**
4252  * ice_init_fdir - Initialize flow director VSI and configuration
4253  * @pf: pointer to the PF instance
4254  *
4255  * returns 0 on success, negative on error
4256  */
ice_init_fdir(struct ice_pf * pf)4257 static int ice_init_fdir(struct ice_pf *pf)
4258 {
4259 	struct device *dev = ice_pf_to_dev(pf);
4260 	struct ice_vsi *ctrl_vsi;
4261 	int err;
4262 
4263 	/* Side Band Flow Director needs to have a control VSI.
4264 	 * Allocate it and store it in the PF.
4265 	 */
4266 	ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
4267 	if (!ctrl_vsi) {
4268 		dev_dbg(dev, "could not create control VSI\n");
4269 		return -ENOMEM;
4270 	}
4271 
4272 	err = ice_vsi_open_ctrl(ctrl_vsi);
4273 	if (err) {
4274 		dev_dbg(dev, "could not open control VSI\n");
4275 		goto err_vsi_open;
4276 	}
4277 
4278 	mutex_init(&pf->hw.fdir_fltr_lock);
4279 
4280 	err = ice_fdir_create_dflt_rules(pf);
4281 	if (err)
4282 		goto err_fdir_rule;
4283 
4284 	return 0;
4285 
4286 err_fdir_rule:
4287 	ice_fdir_release_flows(&pf->hw);
4288 	ice_vsi_close(ctrl_vsi);
4289 err_vsi_open:
4290 	ice_vsi_release(ctrl_vsi);
4291 	if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4292 		pf->vsi[pf->ctrl_vsi_idx] = NULL;
4293 		pf->ctrl_vsi_idx = ICE_NO_VSI;
4294 	}
4295 	return err;
4296 }
4297 
ice_deinit_fdir(struct ice_pf * pf)4298 static void ice_deinit_fdir(struct ice_pf *pf)
4299 {
4300 	struct ice_vsi *vsi = ice_get_ctrl_vsi(pf);
4301 
4302 	if (!vsi)
4303 		return;
4304 
4305 	ice_vsi_manage_fdir(vsi, false);
4306 	ice_vsi_release(vsi);
4307 	if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4308 		pf->vsi[pf->ctrl_vsi_idx] = NULL;
4309 		pf->ctrl_vsi_idx = ICE_NO_VSI;
4310 	}
4311 
4312 	mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
4313 }
4314 
4315 /**
4316  * ice_get_opt_fw_name - return optional firmware file name or NULL
4317  * @pf: pointer to the PF instance
4318  */
ice_get_opt_fw_name(struct ice_pf * pf)4319 static char *ice_get_opt_fw_name(struct ice_pf *pf)
4320 {
4321 	/* Optional firmware name same as default with additional dash
4322 	 * followed by a EUI-64 identifier (PCIe Device Serial Number)
4323 	 */
4324 	struct pci_dev *pdev = pf->pdev;
4325 	char *opt_fw_filename;
4326 	u64 dsn;
4327 
4328 	/* Determine the name of the optional file using the DSN (two
4329 	 * dwords following the start of the DSN Capability).
4330 	 */
4331 	dsn = pci_get_dsn(pdev);
4332 	if (!dsn)
4333 		return NULL;
4334 
4335 	opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
4336 	if (!opt_fw_filename)
4337 		return NULL;
4338 
4339 	snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
4340 		 ICE_DDP_PKG_PATH, dsn);
4341 
4342 	return opt_fw_filename;
4343 }
4344 
4345 /**
4346  * ice_request_fw - Device initialization routine
4347  * @pf: pointer to the PF instance
4348  */
ice_request_fw(struct ice_pf * pf)4349 static void ice_request_fw(struct ice_pf *pf)
4350 {
4351 	char *opt_fw_filename = ice_get_opt_fw_name(pf);
4352 	const struct firmware *firmware = NULL;
4353 	struct device *dev = ice_pf_to_dev(pf);
4354 	int err = 0;
4355 
4356 	/* optional device-specific DDP (if present) overrides the default DDP
4357 	 * package file. kernel logs a debug message if the file doesn't exist,
4358 	 * and warning messages for other errors.
4359 	 */
4360 	if (opt_fw_filename) {
4361 		err = firmware_request_nowarn(&firmware, opt_fw_filename, dev);
4362 		if (err) {
4363 			kfree(opt_fw_filename);
4364 			goto dflt_pkg_load;
4365 		}
4366 
4367 		/* request for firmware was successful. Download to device */
4368 		ice_load_pkg(firmware, pf);
4369 		kfree(opt_fw_filename);
4370 		release_firmware(firmware);
4371 		return;
4372 	}
4373 
4374 dflt_pkg_load:
4375 	err = request_firmware(&firmware, ICE_DDP_PKG_FILE, dev);
4376 	if (err) {
4377 		dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
4378 		return;
4379 	}
4380 
4381 	/* request for firmware was successful. Download to device */
4382 	ice_load_pkg(firmware, pf);
4383 	release_firmware(firmware);
4384 }
4385 
4386 /**
4387  * ice_print_wake_reason - show the wake up cause in the log
4388  * @pf: pointer to the PF struct
4389  */
ice_print_wake_reason(struct ice_pf * pf)4390 static void ice_print_wake_reason(struct ice_pf *pf)
4391 {
4392 	u32 wus = pf->wakeup_reason;
4393 	const char *wake_str;
4394 
4395 	/* if no wake event, nothing to print */
4396 	if (!wus)
4397 		return;
4398 
4399 	if (wus & PFPM_WUS_LNKC_M)
4400 		wake_str = "Link\n";
4401 	else if (wus & PFPM_WUS_MAG_M)
4402 		wake_str = "Magic Packet\n";
4403 	else if (wus & PFPM_WUS_MNG_M)
4404 		wake_str = "Management\n";
4405 	else if (wus & PFPM_WUS_FW_RST_WK_M)
4406 		wake_str = "Firmware Reset\n";
4407 	else
4408 		wake_str = "Unknown\n";
4409 
4410 	dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
4411 }
4412 
4413 /**
4414  * ice_register_netdev - register netdev
4415  * @vsi: pointer to the VSI struct
4416  */
ice_register_netdev(struct ice_vsi * vsi)4417 static int ice_register_netdev(struct ice_vsi *vsi)
4418 {
4419 	int err;
4420 
4421 	if (!vsi || !vsi->netdev)
4422 		return -EIO;
4423 
4424 	err = register_netdev(vsi->netdev);
4425 	if (err)
4426 		return err;
4427 
4428 	set_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4429 	netif_carrier_off(vsi->netdev);
4430 	netif_tx_stop_all_queues(vsi->netdev);
4431 
4432 	return 0;
4433 }
4434 
ice_unregister_netdev(struct ice_vsi * vsi)4435 static void ice_unregister_netdev(struct ice_vsi *vsi)
4436 {
4437 	if (!vsi || !vsi->netdev)
4438 		return;
4439 
4440 	unregister_netdev(vsi->netdev);
4441 	clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4442 }
4443 
4444 /**
4445  * ice_cfg_netdev - Allocate, configure and register a netdev
4446  * @vsi: the VSI associated with the new netdev
4447  *
4448  * Returns 0 on success, negative value on failure
4449  */
ice_cfg_netdev(struct ice_vsi * vsi)4450 static int ice_cfg_netdev(struct ice_vsi *vsi)
4451 {
4452 	struct ice_netdev_priv *np;
4453 	struct net_device *netdev;
4454 	u8 mac_addr[ETH_ALEN];
4455 
4456 	netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
4457 				    vsi->alloc_rxq);
4458 	if (!netdev)
4459 		return -ENOMEM;
4460 
4461 	set_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4462 	vsi->netdev = netdev;
4463 	np = netdev_priv(netdev);
4464 	np->vsi = vsi;
4465 
4466 	ice_set_netdev_features(netdev);
4467 	ice_set_ops(vsi);
4468 
4469 	if (vsi->type == ICE_VSI_PF) {
4470 		SET_NETDEV_DEV(netdev, ice_pf_to_dev(vsi->back));
4471 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
4472 		eth_hw_addr_set(netdev, mac_addr);
4473 	}
4474 
4475 	netdev->priv_flags |= IFF_UNICAST_FLT;
4476 
4477 	/* Setup netdev TC information */
4478 	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
4479 
4480 	netdev->max_mtu = ICE_MAX_MTU;
4481 
4482 	return 0;
4483 }
4484 
ice_decfg_netdev(struct ice_vsi * vsi)4485 static void ice_decfg_netdev(struct ice_vsi *vsi)
4486 {
4487 	clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4488 	free_netdev(vsi->netdev);
4489 	vsi->netdev = NULL;
4490 }
4491 
ice_start_eth(struct ice_vsi * vsi)4492 static int ice_start_eth(struct ice_vsi *vsi)
4493 {
4494 	int err;
4495 
4496 	err = ice_init_mac_fltr(vsi->back);
4497 	if (err)
4498 		return err;
4499 
4500 	err = ice_vsi_open(vsi);
4501 	if (err)
4502 		ice_fltr_remove_all(vsi);
4503 
4504 	return err;
4505 }
4506 
ice_stop_eth(struct ice_vsi * vsi)4507 static void ice_stop_eth(struct ice_vsi *vsi)
4508 {
4509 	ice_fltr_remove_all(vsi);
4510 	ice_vsi_close(vsi);
4511 }
4512 
ice_init_eth(struct ice_pf * pf)4513 static int ice_init_eth(struct ice_pf *pf)
4514 {
4515 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
4516 	int err;
4517 
4518 	if (!vsi)
4519 		return -EINVAL;
4520 
4521 	/* init channel list */
4522 	INIT_LIST_HEAD(&vsi->ch_list);
4523 
4524 	err = ice_cfg_netdev(vsi);
4525 	if (err)
4526 		return err;
4527 	/* Setup DCB netlink interface */
4528 	ice_dcbnl_setup(vsi);
4529 
4530 	err = ice_init_mac_fltr(pf);
4531 	if (err)
4532 		goto err_init_mac_fltr;
4533 
4534 	err = ice_devlink_create_pf_port(pf);
4535 	if (err)
4536 		goto err_devlink_create_pf_port;
4537 
4538 	SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
4539 
4540 	err = ice_register_netdev(vsi);
4541 	if (err)
4542 		goto err_register_netdev;
4543 
4544 	err = ice_tc_indir_block_register(vsi);
4545 	if (err)
4546 		goto err_tc_indir_block_register;
4547 
4548 	ice_napi_add(vsi);
4549 
4550 	return 0;
4551 
4552 err_tc_indir_block_register:
4553 	ice_unregister_netdev(vsi);
4554 err_register_netdev:
4555 	ice_devlink_destroy_pf_port(pf);
4556 err_devlink_create_pf_port:
4557 err_init_mac_fltr:
4558 	ice_decfg_netdev(vsi);
4559 	return err;
4560 }
4561 
ice_deinit_eth(struct ice_pf * pf)4562 static void ice_deinit_eth(struct ice_pf *pf)
4563 {
4564 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
4565 
4566 	if (!vsi)
4567 		return;
4568 
4569 	ice_vsi_close(vsi);
4570 	ice_unregister_netdev(vsi);
4571 	ice_devlink_destroy_pf_port(pf);
4572 	ice_tc_indir_block_unregister(vsi);
4573 	ice_decfg_netdev(vsi);
4574 }
4575 
4576 /**
4577  * ice_wait_for_fw - wait for full FW readiness
4578  * @hw: pointer to the hardware structure
4579  * @timeout: milliseconds that can elapse before timing out
4580  */
ice_wait_for_fw(struct ice_hw * hw,u32 timeout)4581 static int ice_wait_for_fw(struct ice_hw *hw, u32 timeout)
4582 {
4583 	int fw_loading;
4584 	u32 elapsed = 0;
4585 
4586 	while (elapsed <= timeout) {
4587 		fw_loading = rd32(hw, GL_MNG_FWSM) & GL_MNG_FWSM_FW_LOADING_M;
4588 
4589 		/* firmware was not yet loaded, we have to wait more */
4590 		if (fw_loading) {
4591 			elapsed += 100;
4592 			msleep(100);
4593 			continue;
4594 		}
4595 		return 0;
4596 	}
4597 
4598 	return -ETIMEDOUT;
4599 }
4600 
ice_init_dev(struct ice_pf * pf)4601 static int ice_init_dev(struct ice_pf *pf)
4602 {
4603 	struct device *dev = ice_pf_to_dev(pf);
4604 	struct ice_hw *hw = &pf->hw;
4605 	int err;
4606 
4607 	err = ice_init_hw(hw);
4608 	if (err) {
4609 		dev_err(dev, "ice_init_hw failed: %d\n", err);
4610 		return err;
4611 	}
4612 
4613 	/* Some cards require longer initialization times
4614 	 * due to necessity of loading FW from an external source.
4615 	 * This can take even half a minute.
4616 	 */
4617 	if (ice_is_pf_c827(hw)) {
4618 		err = ice_wait_for_fw(hw, 30000);
4619 		if (err) {
4620 			dev_err(dev, "ice_wait_for_fw timed out");
4621 			return err;
4622 		}
4623 	}
4624 
4625 	ice_init_feature_support(pf);
4626 
4627 	ice_request_fw(pf);
4628 
4629 	/* if ice_request_fw fails, ICE_FLAG_ADV_FEATURES bit won't be
4630 	 * set in pf->state, which will cause ice_is_safe_mode to return
4631 	 * true
4632 	 */
4633 	if (ice_is_safe_mode(pf)) {
4634 		/* we already got function/device capabilities but these don't
4635 		 * reflect what the driver needs to do in safe mode. Instead of
4636 		 * adding conditional logic everywhere to ignore these
4637 		 * device/function capabilities, override them.
4638 		 */
4639 		ice_set_safe_mode_caps(hw);
4640 	}
4641 
4642 	err = ice_init_pf(pf);
4643 	if (err) {
4644 		dev_err(dev, "ice_init_pf failed: %d\n", err);
4645 		goto err_init_pf;
4646 	}
4647 
4648 	pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4649 	pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4650 	pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
4651 	pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
4652 	if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4653 		pf->hw.udp_tunnel_nic.tables[0].n_entries =
4654 			pf->hw.tnl.valid_count[TNL_VXLAN];
4655 		pf->hw.udp_tunnel_nic.tables[0].tunnel_types =
4656 			UDP_TUNNEL_TYPE_VXLAN;
4657 	}
4658 	if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4659 		pf->hw.udp_tunnel_nic.tables[1].n_entries =
4660 			pf->hw.tnl.valid_count[TNL_GENEVE];
4661 		pf->hw.udp_tunnel_nic.tables[1].tunnel_types =
4662 			UDP_TUNNEL_TYPE_GENEVE;
4663 	}
4664 
4665 	err = ice_init_interrupt_scheme(pf);
4666 	if (err) {
4667 		dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4668 		err = -EIO;
4669 		goto err_init_interrupt_scheme;
4670 	}
4671 
4672 	/* In case of MSIX we are going to setup the misc vector right here
4673 	 * to handle admin queue events etc. In case of legacy and MSI
4674 	 * the misc functionality and queue processing is combined in
4675 	 * the same vector and that gets setup at open.
4676 	 */
4677 	err = ice_req_irq_msix_misc(pf);
4678 	if (err) {
4679 		dev_err(dev, "setup of misc vector failed: %d\n", err);
4680 		goto err_req_irq_msix_misc;
4681 	}
4682 
4683 	return 0;
4684 
4685 err_req_irq_msix_misc:
4686 	ice_clear_interrupt_scheme(pf);
4687 err_init_interrupt_scheme:
4688 	ice_deinit_pf(pf);
4689 err_init_pf:
4690 	ice_deinit_hw(hw);
4691 	return err;
4692 }
4693 
ice_deinit_dev(struct ice_pf * pf)4694 static void ice_deinit_dev(struct ice_pf *pf)
4695 {
4696 	ice_free_irq_msix_misc(pf);
4697 	ice_deinit_pf(pf);
4698 	ice_deinit_hw(&pf->hw);
4699 
4700 	/* Service task is already stopped, so call reset directly. */
4701 	ice_reset(&pf->hw, ICE_RESET_PFR);
4702 	pci_wait_for_pending_transaction(pf->pdev);
4703 	ice_clear_interrupt_scheme(pf);
4704 }
4705 
ice_init_features(struct ice_pf * pf)4706 static void ice_init_features(struct ice_pf *pf)
4707 {
4708 	struct device *dev = ice_pf_to_dev(pf);
4709 
4710 	if (ice_is_safe_mode(pf))
4711 		return;
4712 
4713 	/* initialize DDP driven features */
4714 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4715 		ice_ptp_init(pf);
4716 
4717 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
4718 		ice_gnss_init(pf);
4719 
4720 	/* Note: Flow director init failure is non-fatal to load */
4721 	if (ice_init_fdir(pf))
4722 		dev_err(dev, "could not initialize flow director\n");
4723 
4724 	/* Note: DCB init failure is non-fatal to load */
4725 	if (ice_init_pf_dcb(pf, false)) {
4726 		clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4727 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4728 	} else {
4729 		ice_cfg_lldp_mib_change(&pf->hw, true);
4730 	}
4731 
4732 	if (ice_init_lag(pf))
4733 		dev_warn(dev, "Failed to init link aggregation support\n");
4734 }
4735 
ice_deinit_features(struct ice_pf * pf)4736 static void ice_deinit_features(struct ice_pf *pf)
4737 {
4738 	if (ice_is_safe_mode(pf))
4739 		return;
4740 
4741 	ice_deinit_lag(pf);
4742 	if (test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags))
4743 		ice_cfg_lldp_mib_change(&pf->hw, false);
4744 	ice_deinit_fdir(pf);
4745 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
4746 		ice_gnss_exit(pf);
4747 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4748 		ice_ptp_release(pf);
4749 }
4750 
ice_init_wakeup(struct ice_pf * pf)4751 static void ice_init_wakeup(struct ice_pf *pf)
4752 {
4753 	/* Save wakeup reason register for later use */
4754 	pf->wakeup_reason = rd32(&pf->hw, PFPM_WUS);
4755 
4756 	/* check for a power management event */
4757 	ice_print_wake_reason(pf);
4758 
4759 	/* clear wake status, all bits */
4760 	wr32(&pf->hw, PFPM_WUS, U32_MAX);
4761 
4762 	/* Disable WoL at init, wait for user to enable */
4763 	device_set_wakeup_enable(ice_pf_to_dev(pf), false);
4764 }
4765 
ice_init_link(struct ice_pf * pf)4766 static int ice_init_link(struct ice_pf *pf)
4767 {
4768 	struct device *dev = ice_pf_to_dev(pf);
4769 	int err;
4770 
4771 	err = ice_init_link_events(pf->hw.port_info);
4772 	if (err) {
4773 		dev_err(dev, "ice_init_link_events failed: %d\n", err);
4774 		return err;
4775 	}
4776 
4777 	/* not a fatal error if this fails */
4778 	err = ice_init_nvm_phy_type(pf->hw.port_info);
4779 	if (err)
4780 		dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4781 
4782 	/* not a fatal error if this fails */
4783 	err = ice_update_link_info(pf->hw.port_info);
4784 	if (err)
4785 		dev_err(dev, "ice_update_link_info failed: %d\n", err);
4786 
4787 	ice_init_link_dflt_override(pf->hw.port_info);
4788 
4789 	ice_check_link_cfg_err(pf,
4790 			       pf->hw.port_info->phy.link_info.link_cfg_err);
4791 
4792 	/* if media available, initialize PHY settings */
4793 	if (pf->hw.port_info->phy.link_info.link_info &
4794 	    ICE_AQ_MEDIA_AVAILABLE) {
4795 		/* not a fatal error if this fails */
4796 		err = ice_init_phy_user_cfg(pf->hw.port_info);
4797 		if (err)
4798 			dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4799 
4800 		if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4801 			struct ice_vsi *vsi = ice_get_main_vsi(pf);
4802 
4803 			if (vsi)
4804 				ice_configure_phy(vsi);
4805 		}
4806 	} else {
4807 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4808 	}
4809 
4810 	return err;
4811 }
4812 
ice_init_pf_sw(struct ice_pf * pf)4813 static int ice_init_pf_sw(struct ice_pf *pf)
4814 {
4815 	bool dvm = ice_is_dvm_ena(&pf->hw);
4816 	struct ice_vsi *vsi;
4817 	int err;
4818 
4819 	/* create switch struct for the switch element created by FW on boot */
4820 	pf->first_sw = kzalloc(sizeof(*pf->first_sw), GFP_KERNEL);
4821 	if (!pf->first_sw)
4822 		return -ENOMEM;
4823 
4824 	if (pf->hw.evb_veb)
4825 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4826 	else
4827 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
4828 
4829 	pf->first_sw->pf = pf;
4830 
4831 	/* record the sw_id available for later use */
4832 	pf->first_sw->sw_id = pf->hw.port_info->sw_id;
4833 
4834 	err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
4835 	if (err)
4836 		goto err_aq_set_port_params;
4837 
4838 	vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
4839 	if (!vsi) {
4840 		err = -ENOMEM;
4841 		goto err_pf_vsi_setup;
4842 	}
4843 
4844 	return 0;
4845 
4846 err_pf_vsi_setup:
4847 err_aq_set_port_params:
4848 	kfree(pf->first_sw);
4849 	return err;
4850 }
4851 
ice_deinit_pf_sw(struct ice_pf * pf)4852 static void ice_deinit_pf_sw(struct ice_pf *pf)
4853 {
4854 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
4855 
4856 	if (!vsi)
4857 		return;
4858 
4859 	ice_vsi_release(vsi);
4860 	kfree(pf->first_sw);
4861 }
4862 
ice_alloc_vsis(struct ice_pf * pf)4863 static int ice_alloc_vsis(struct ice_pf *pf)
4864 {
4865 	struct device *dev = ice_pf_to_dev(pf);
4866 
4867 	pf->num_alloc_vsi = pf->hw.func_caps.guar_num_vsi;
4868 	if (!pf->num_alloc_vsi)
4869 		return -EIO;
4870 
4871 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
4872 		dev_warn(dev,
4873 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
4874 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
4875 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
4876 	}
4877 
4878 	pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
4879 			       GFP_KERNEL);
4880 	if (!pf->vsi)
4881 		return -ENOMEM;
4882 
4883 	pf->vsi_stats = devm_kcalloc(dev, pf->num_alloc_vsi,
4884 				     sizeof(*pf->vsi_stats), GFP_KERNEL);
4885 	if (!pf->vsi_stats) {
4886 		devm_kfree(dev, pf->vsi);
4887 		return -ENOMEM;
4888 	}
4889 
4890 	return 0;
4891 }
4892 
ice_dealloc_vsis(struct ice_pf * pf)4893 static void ice_dealloc_vsis(struct ice_pf *pf)
4894 {
4895 	devm_kfree(ice_pf_to_dev(pf), pf->vsi_stats);
4896 	pf->vsi_stats = NULL;
4897 
4898 	pf->num_alloc_vsi = 0;
4899 	devm_kfree(ice_pf_to_dev(pf), pf->vsi);
4900 	pf->vsi = NULL;
4901 }
4902 
ice_init_devlink(struct ice_pf * pf)4903 static int ice_init_devlink(struct ice_pf *pf)
4904 {
4905 	int err;
4906 
4907 	err = ice_devlink_register_params(pf);
4908 	if (err)
4909 		return err;
4910 
4911 	ice_devlink_init_regions(pf);
4912 	ice_devlink_register(pf);
4913 
4914 	return 0;
4915 }
4916 
ice_deinit_devlink(struct ice_pf * pf)4917 static void ice_deinit_devlink(struct ice_pf *pf)
4918 {
4919 	ice_devlink_unregister(pf);
4920 	ice_devlink_destroy_regions(pf);
4921 	ice_devlink_unregister_params(pf);
4922 }
4923 
ice_init(struct ice_pf * pf)4924 static int ice_init(struct ice_pf *pf)
4925 {
4926 	int err;
4927 
4928 	err = ice_init_dev(pf);
4929 	if (err)
4930 		return err;
4931 
4932 	err = ice_alloc_vsis(pf);
4933 	if (err)
4934 		goto err_alloc_vsis;
4935 
4936 	err = ice_init_pf_sw(pf);
4937 	if (err)
4938 		goto err_init_pf_sw;
4939 
4940 	ice_init_wakeup(pf);
4941 
4942 	err = ice_init_link(pf);
4943 	if (err)
4944 		goto err_init_link;
4945 
4946 	err = ice_send_version(pf);
4947 	if (err)
4948 		goto err_init_link;
4949 
4950 	ice_verify_cacheline_size(pf);
4951 
4952 	if (ice_is_safe_mode(pf))
4953 		ice_set_safe_mode_vlan_cfg(pf);
4954 	else
4955 		/* print PCI link speed and width */
4956 		pcie_print_link_status(pf->pdev);
4957 
4958 	/* ready to go, so clear down state bit */
4959 	clear_bit(ICE_DOWN, pf->state);
4960 	clear_bit(ICE_SERVICE_DIS, pf->state);
4961 
4962 	/* since everything is good, start the service timer */
4963 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4964 
4965 	return 0;
4966 
4967 err_init_link:
4968 	ice_deinit_pf_sw(pf);
4969 err_init_pf_sw:
4970 	ice_dealloc_vsis(pf);
4971 err_alloc_vsis:
4972 	ice_deinit_dev(pf);
4973 	return err;
4974 }
4975 
ice_deinit(struct ice_pf * pf)4976 static void ice_deinit(struct ice_pf *pf)
4977 {
4978 	set_bit(ICE_SERVICE_DIS, pf->state);
4979 	set_bit(ICE_DOWN, pf->state);
4980 
4981 	ice_deinit_pf_sw(pf);
4982 	ice_dealloc_vsis(pf);
4983 	ice_deinit_dev(pf);
4984 }
4985 
4986 /**
4987  * ice_load - load pf by init hw and starting VSI
4988  * @pf: pointer to the pf instance
4989  */
ice_load(struct ice_pf * pf)4990 int ice_load(struct ice_pf *pf)
4991 {
4992 	struct ice_vsi_cfg_params params = {};
4993 	struct ice_vsi *vsi;
4994 	int err;
4995 
4996 	err = ice_init_dev(pf);
4997 	if (err)
4998 		return err;
4999 
5000 	vsi = ice_get_main_vsi(pf);
5001 
5002 	params = ice_vsi_to_params(vsi);
5003 	params.flags = ICE_VSI_FLAG_INIT;
5004 
5005 	rtnl_lock();
5006 	err = ice_vsi_cfg(vsi, &params);
5007 	if (err)
5008 		goto err_vsi_cfg;
5009 
5010 	err = ice_start_eth(ice_get_main_vsi(pf));
5011 	if (err)
5012 		goto err_start_eth;
5013 	rtnl_unlock();
5014 
5015 	err = ice_init_rdma(pf);
5016 	if (err)
5017 		goto err_init_rdma;
5018 
5019 	ice_init_features(pf);
5020 	ice_service_task_restart(pf);
5021 
5022 	clear_bit(ICE_DOWN, pf->state);
5023 
5024 	return 0;
5025 
5026 err_init_rdma:
5027 	ice_vsi_close(ice_get_main_vsi(pf));
5028 	rtnl_lock();
5029 err_start_eth:
5030 	ice_vsi_decfg(ice_get_main_vsi(pf));
5031 err_vsi_cfg:
5032 	rtnl_unlock();
5033 	ice_deinit_dev(pf);
5034 	return err;
5035 }
5036 
5037 /**
5038  * ice_unload - unload pf by stopping VSI and deinit hw
5039  * @pf: pointer to the pf instance
5040  */
ice_unload(struct ice_pf * pf)5041 void ice_unload(struct ice_pf *pf)
5042 {
5043 	ice_deinit_features(pf);
5044 	ice_deinit_rdma(pf);
5045 	rtnl_lock();
5046 	ice_stop_eth(ice_get_main_vsi(pf));
5047 	ice_vsi_decfg(ice_get_main_vsi(pf));
5048 	rtnl_unlock();
5049 	ice_deinit_dev(pf);
5050 }
5051 
5052 /**
5053  * ice_probe - Device initialization routine
5054  * @pdev: PCI device information struct
5055  * @ent: entry in ice_pci_tbl
5056  *
5057  * Returns 0 on success, negative on failure
5058  */
5059 static int
ice_probe(struct pci_dev * pdev,const struct pci_device_id __always_unused * ent)5060 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
5061 {
5062 	struct device *dev = &pdev->dev;
5063 	struct ice_pf *pf;
5064 	struct ice_hw *hw;
5065 	int err;
5066 
5067 	if (pdev->is_virtfn) {
5068 		dev_err(dev, "can't probe a virtual function\n");
5069 		return -EINVAL;
5070 	}
5071 
5072 	/* when under a kdump kernel initiate a reset before enabling the
5073 	 * device in order to clear out any pending DMA transactions. These
5074 	 * transactions can cause some systems to machine check when doing
5075 	 * the pcim_enable_device() below.
5076 	 */
5077 	if (is_kdump_kernel()) {
5078 		pci_save_state(pdev);
5079 		pci_clear_master(pdev);
5080 		err = pcie_flr(pdev);
5081 		if (err)
5082 			return err;
5083 		pci_restore_state(pdev);
5084 	}
5085 
5086 	/* this driver uses devres, see
5087 	 * Documentation/driver-api/driver-model/devres.rst
5088 	 */
5089 	err = pcim_enable_device(pdev);
5090 	if (err)
5091 		return err;
5092 
5093 	err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), dev_driver_string(dev));
5094 	if (err) {
5095 		dev_err(dev, "BAR0 I/O map error %d\n", err);
5096 		return err;
5097 	}
5098 
5099 	pf = ice_allocate_pf(dev);
5100 	if (!pf)
5101 		return -ENOMEM;
5102 
5103 	/* initialize Auxiliary index to invalid value */
5104 	pf->aux_idx = -1;
5105 
5106 	/* set up for high or low DMA */
5107 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
5108 	if (err) {
5109 		dev_err(dev, "DMA configuration failed: 0x%x\n", err);
5110 		return err;
5111 	}
5112 
5113 	pci_set_master(pdev);
5114 
5115 	pf->pdev = pdev;
5116 	pci_set_drvdata(pdev, pf);
5117 	set_bit(ICE_DOWN, pf->state);
5118 	/* Disable service task until DOWN bit is cleared */
5119 	set_bit(ICE_SERVICE_DIS, pf->state);
5120 
5121 	hw = &pf->hw;
5122 	hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
5123 	pci_save_state(pdev);
5124 
5125 	hw->back = pf;
5126 	hw->port_info = NULL;
5127 	hw->vendor_id = pdev->vendor;
5128 	hw->device_id = pdev->device;
5129 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
5130 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
5131 	hw->subsystem_device_id = pdev->subsystem_device;
5132 	hw->bus.device = PCI_SLOT(pdev->devfn);
5133 	hw->bus.func = PCI_FUNC(pdev->devfn);
5134 	ice_set_ctrlq_len(hw);
5135 
5136 	pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
5137 
5138 #ifndef CONFIG_DYNAMIC_DEBUG
5139 	if (debug < -1)
5140 		hw->debug_mask = debug;
5141 #endif
5142 
5143 	err = ice_init(pf);
5144 	if (err)
5145 		goto err_init;
5146 
5147 	err = ice_init_eth(pf);
5148 	if (err)
5149 		goto err_init_eth;
5150 
5151 	err = ice_init_rdma(pf);
5152 	if (err)
5153 		goto err_init_rdma;
5154 
5155 	err = ice_init_devlink(pf);
5156 	if (err)
5157 		goto err_init_devlink;
5158 
5159 	ice_init_features(pf);
5160 
5161 	return 0;
5162 
5163 err_init_devlink:
5164 	ice_deinit_rdma(pf);
5165 err_init_rdma:
5166 	ice_deinit_eth(pf);
5167 err_init_eth:
5168 	ice_deinit(pf);
5169 err_init:
5170 	pci_disable_device(pdev);
5171 	return err;
5172 }
5173 
5174 /**
5175  * ice_set_wake - enable or disable Wake on LAN
5176  * @pf: pointer to the PF struct
5177  *
5178  * Simple helper for WoL control
5179  */
ice_set_wake(struct ice_pf * pf)5180 static void ice_set_wake(struct ice_pf *pf)
5181 {
5182 	struct ice_hw *hw = &pf->hw;
5183 	bool wol = pf->wol_ena;
5184 
5185 	/* clear wake state, otherwise new wake events won't fire */
5186 	wr32(hw, PFPM_WUS, U32_MAX);
5187 
5188 	/* enable / disable APM wake up, no RMW needed */
5189 	wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
5190 
5191 	/* set magic packet filter enabled */
5192 	wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
5193 }
5194 
5195 /**
5196  * ice_setup_mc_magic_wake - setup device to wake on multicast magic packet
5197  * @pf: pointer to the PF struct
5198  *
5199  * Issue firmware command to enable multicast magic wake, making
5200  * sure that any locally administered address (LAA) is used for
5201  * wake, and that PF reset doesn't undo the LAA.
5202  */
ice_setup_mc_magic_wake(struct ice_pf * pf)5203 static void ice_setup_mc_magic_wake(struct ice_pf *pf)
5204 {
5205 	struct device *dev = ice_pf_to_dev(pf);
5206 	struct ice_hw *hw = &pf->hw;
5207 	u8 mac_addr[ETH_ALEN];
5208 	struct ice_vsi *vsi;
5209 	int status;
5210 	u8 flags;
5211 
5212 	if (!pf->wol_ena)
5213 		return;
5214 
5215 	vsi = ice_get_main_vsi(pf);
5216 	if (!vsi)
5217 		return;
5218 
5219 	/* Get current MAC address in case it's an LAA */
5220 	if (vsi->netdev)
5221 		ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
5222 	else
5223 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
5224 
5225 	flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
5226 		ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
5227 		ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
5228 
5229 	status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
5230 	if (status)
5231 		dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %d aq_err %s\n",
5232 			status, ice_aq_str(hw->adminq.sq_last_status));
5233 }
5234 
5235 /**
5236  * ice_remove - Device removal routine
5237  * @pdev: PCI device information struct
5238  */
ice_remove(struct pci_dev * pdev)5239 static void ice_remove(struct pci_dev *pdev)
5240 {
5241 	struct ice_pf *pf = pci_get_drvdata(pdev);
5242 	int i;
5243 
5244 	for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
5245 		if (!ice_is_reset_in_progress(pf->state))
5246 			break;
5247 		msleep(100);
5248 	}
5249 
5250 	if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
5251 		set_bit(ICE_VF_RESETS_DISABLED, pf->state);
5252 		ice_free_vfs(pf);
5253 	}
5254 
5255 	ice_service_task_stop(pf);
5256 	ice_aq_cancel_waiting_tasks(pf);
5257 	set_bit(ICE_DOWN, pf->state);
5258 
5259 	if (!ice_is_safe_mode(pf))
5260 		ice_remove_arfs(pf);
5261 	ice_deinit_features(pf);
5262 	ice_deinit_devlink(pf);
5263 	ice_deinit_rdma(pf);
5264 	ice_deinit_eth(pf);
5265 	ice_deinit(pf);
5266 
5267 	ice_vsi_release_all(pf);
5268 
5269 	ice_setup_mc_magic_wake(pf);
5270 	ice_set_wake(pf);
5271 
5272 	pci_disable_device(pdev);
5273 }
5274 
5275 /**
5276  * ice_shutdown - PCI callback for shutting down device
5277  * @pdev: PCI device information struct
5278  */
ice_shutdown(struct pci_dev * pdev)5279 static void ice_shutdown(struct pci_dev *pdev)
5280 {
5281 	struct ice_pf *pf = pci_get_drvdata(pdev);
5282 
5283 	ice_remove(pdev);
5284 
5285 	if (system_state == SYSTEM_POWER_OFF) {
5286 		pci_wake_from_d3(pdev, pf->wol_ena);
5287 		pci_set_power_state(pdev, PCI_D3hot);
5288 	}
5289 }
5290 
5291 #ifdef CONFIG_PM
5292 /**
5293  * ice_prepare_for_shutdown - prep for PCI shutdown
5294  * @pf: board private structure
5295  *
5296  * Inform or close all dependent features in prep for PCI device shutdown
5297  */
ice_prepare_for_shutdown(struct ice_pf * pf)5298 static void ice_prepare_for_shutdown(struct ice_pf *pf)
5299 {
5300 	struct ice_hw *hw = &pf->hw;
5301 	u32 v;
5302 
5303 	/* Notify VFs of impending reset */
5304 	if (ice_check_sq_alive(hw, &hw->mailboxq))
5305 		ice_vc_notify_reset(pf);
5306 
5307 	dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
5308 
5309 	/* disable the VSIs and their queues that are not already DOWN */
5310 	ice_pf_dis_all_vsi(pf, false);
5311 
5312 	ice_for_each_vsi(pf, v)
5313 		if (pf->vsi[v])
5314 			pf->vsi[v]->vsi_num = 0;
5315 
5316 	ice_shutdown_all_ctrlq(hw);
5317 }
5318 
5319 /**
5320  * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
5321  * @pf: board private structure to reinitialize
5322  *
5323  * This routine reinitialize interrupt scheme that was cleared during
5324  * power management suspend callback.
5325  *
5326  * This should be called during resume routine to re-allocate the q_vectors
5327  * and reacquire interrupts.
5328  */
ice_reinit_interrupt_scheme(struct ice_pf * pf)5329 static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
5330 {
5331 	struct device *dev = ice_pf_to_dev(pf);
5332 	int ret, v;
5333 
5334 	/* Since we clear MSIX flag during suspend, we need to
5335 	 * set it back during resume...
5336 	 */
5337 
5338 	ret = ice_init_interrupt_scheme(pf);
5339 	if (ret) {
5340 		dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
5341 		return ret;
5342 	}
5343 
5344 	/* Remap vectors and rings, after successful re-init interrupts */
5345 	ice_for_each_vsi(pf, v) {
5346 		if (!pf->vsi[v])
5347 			continue;
5348 
5349 		ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
5350 		if (ret)
5351 			goto err_reinit;
5352 		ice_vsi_map_rings_to_vectors(pf->vsi[v]);
5353 	}
5354 
5355 	ret = ice_req_irq_msix_misc(pf);
5356 	if (ret) {
5357 		dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
5358 			ret);
5359 		goto err_reinit;
5360 	}
5361 
5362 	return 0;
5363 
5364 err_reinit:
5365 	while (v--)
5366 		if (pf->vsi[v])
5367 			ice_vsi_free_q_vectors(pf->vsi[v]);
5368 
5369 	return ret;
5370 }
5371 
5372 /**
5373  * ice_suspend
5374  * @dev: generic device information structure
5375  *
5376  * Power Management callback to quiesce the device and prepare
5377  * for D3 transition.
5378  */
ice_suspend(struct device * dev)5379 static int __maybe_unused ice_suspend(struct device *dev)
5380 {
5381 	struct pci_dev *pdev = to_pci_dev(dev);
5382 	struct ice_pf *pf;
5383 	int disabled, v;
5384 
5385 	pf = pci_get_drvdata(pdev);
5386 
5387 	if (!ice_pf_state_is_nominal(pf)) {
5388 		dev_err(dev, "Device is not ready, no need to suspend it\n");
5389 		return -EBUSY;
5390 	}
5391 
5392 	/* Stop watchdog tasks until resume completion.
5393 	 * Even though it is most likely that the service task is
5394 	 * disabled if the device is suspended or down, the service task's
5395 	 * state is controlled by a different state bit, and we should
5396 	 * store and honor whatever state that bit is in at this point.
5397 	 */
5398 	disabled = ice_service_task_stop(pf);
5399 
5400 	ice_deinit_rdma(pf);
5401 
5402 	/* Already suspended?, then there is nothing to do */
5403 	if (test_and_set_bit(ICE_SUSPENDED, pf->state)) {
5404 		if (!disabled)
5405 			ice_service_task_restart(pf);
5406 		return 0;
5407 	}
5408 
5409 	if (test_bit(ICE_DOWN, pf->state) ||
5410 	    ice_is_reset_in_progress(pf->state)) {
5411 		dev_err(dev, "can't suspend device in reset or already down\n");
5412 		if (!disabled)
5413 			ice_service_task_restart(pf);
5414 		return 0;
5415 	}
5416 
5417 	ice_setup_mc_magic_wake(pf);
5418 
5419 	ice_prepare_for_shutdown(pf);
5420 
5421 	ice_set_wake(pf);
5422 
5423 	/* Free vectors, clear the interrupt scheme and release IRQs
5424 	 * for proper hibernation, especially with large number of CPUs.
5425 	 * Otherwise hibernation might fail when mapping all the vectors back
5426 	 * to CPU0.
5427 	 */
5428 	ice_free_irq_msix_misc(pf);
5429 	ice_for_each_vsi(pf, v) {
5430 		if (!pf->vsi[v])
5431 			continue;
5432 		ice_vsi_free_q_vectors(pf->vsi[v]);
5433 	}
5434 	ice_clear_interrupt_scheme(pf);
5435 
5436 	pci_save_state(pdev);
5437 	pci_wake_from_d3(pdev, pf->wol_ena);
5438 	pci_set_power_state(pdev, PCI_D3hot);
5439 	return 0;
5440 }
5441 
5442 /**
5443  * ice_resume - PM callback for waking up from D3
5444  * @dev: generic device information structure
5445  */
ice_resume(struct device * dev)5446 static int __maybe_unused ice_resume(struct device *dev)
5447 {
5448 	struct pci_dev *pdev = to_pci_dev(dev);
5449 	enum ice_reset_req reset_type;
5450 	struct ice_pf *pf;
5451 	struct ice_hw *hw;
5452 	int ret;
5453 
5454 	pci_set_power_state(pdev, PCI_D0);
5455 	pci_restore_state(pdev);
5456 	pci_save_state(pdev);
5457 
5458 	if (!pci_device_is_present(pdev))
5459 		return -ENODEV;
5460 
5461 	ret = pci_enable_device_mem(pdev);
5462 	if (ret) {
5463 		dev_err(dev, "Cannot enable device after suspend\n");
5464 		return ret;
5465 	}
5466 
5467 	pf = pci_get_drvdata(pdev);
5468 	hw = &pf->hw;
5469 
5470 	pf->wakeup_reason = rd32(hw, PFPM_WUS);
5471 	ice_print_wake_reason(pf);
5472 
5473 	/* We cleared the interrupt scheme when we suspended, so we need to
5474 	 * restore it now to resume device functionality.
5475 	 */
5476 	ret = ice_reinit_interrupt_scheme(pf);
5477 	if (ret)
5478 		dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
5479 
5480 	ret = ice_init_rdma(pf);
5481 	if (ret)
5482 		dev_err(dev, "Reinitialize RDMA during resume failed: %d\n",
5483 			ret);
5484 
5485 	clear_bit(ICE_DOWN, pf->state);
5486 	/* Now perform PF reset and rebuild */
5487 	reset_type = ICE_RESET_PFR;
5488 	/* re-enable service task for reset, but allow reset to schedule it */
5489 	clear_bit(ICE_SERVICE_DIS, pf->state);
5490 
5491 	if (ice_schedule_reset(pf, reset_type))
5492 		dev_err(dev, "Reset during resume failed.\n");
5493 
5494 	clear_bit(ICE_SUSPENDED, pf->state);
5495 	ice_service_task_restart(pf);
5496 
5497 	/* Restart the service task */
5498 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5499 
5500 	return 0;
5501 }
5502 #endif /* CONFIG_PM */
5503 
5504 /**
5505  * ice_pci_err_detected - warning that PCI error has been detected
5506  * @pdev: PCI device information struct
5507  * @err: the type of PCI error
5508  *
5509  * Called to warn that something happened on the PCI bus and the error handling
5510  * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
5511  */
5512 static pci_ers_result_t
ice_pci_err_detected(struct pci_dev * pdev,pci_channel_state_t err)5513 ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
5514 {
5515 	struct ice_pf *pf = pci_get_drvdata(pdev);
5516 
5517 	if (!pf) {
5518 		dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
5519 			__func__, err);
5520 		return PCI_ERS_RESULT_DISCONNECT;
5521 	}
5522 
5523 	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5524 		ice_service_task_stop(pf);
5525 
5526 		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5527 			set_bit(ICE_PFR_REQ, pf->state);
5528 			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5529 		}
5530 	}
5531 
5532 	return PCI_ERS_RESULT_NEED_RESET;
5533 }
5534 
5535 /**
5536  * ice_pci_err_slot_reset - a PCI slot reset has just happened
5537  * @pdev: PCI device information struct
5538  *
5539  * Called to determine if the driver can recover from the PCI slot reset by
5540  * using a register read to determine if the device is recoverable.
5541  */
ice_pci_err_slot_reset(struct pci_dev * pdev)5542 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
5543 {
5544 	struct ice_pf *pf = pci_get_drvdata(pdev);
5545 	pci_ers_result_t result;
5546 	int err;
5547 	u32 reg;
5548 
5549 	err = pci_enable_device_mem(pdev);
5550 	if (err) {
5551 		dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
5552 			err);
5553 		result = PCI_ERS_RESULT_DISCONNECT;
5554 	} else {
5555 		pci_set_master(pdev);
5556 		pci_restore_state(pdev);
5557 		pci_save_state(pdev);
5558 		pci_wake_from_d3(pdev, false);
5559 
5560 		/* Check for life */
5561 		reg = rd32(&pf->hw, GLGEN_RTRIG);
5562 		if (!reg)
5563 			result = PCI_ERS_RESULT_RECOVERED;
5564 		else
5565 			result = PCI_ERS_RESULT_DISCONNECT;
5566 	}
5567 
5568 	return result;
5569 }
5570 
5571 /**
5572  * ice_pci_err_resume - restart operations after PCI error recovery
5573  * @pdev: PCI device information struct
5574  *
5575  * Called to allow the driver to bring things back up after PCI error and/or
5576  * reset recovery have finished
5577  */
ice_pci_err_resume(struct pci_dev * pdev)5578 static void ice_pci_err_resume(struct pci_dev *pdev)
5579 {
5580 	struct ice_pf *pf = pci_get_drvdata(pdev);
5581 
5582 	if (!pf) {
5583 		dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
5584 			__func__);
5585 		return;
5586 	}
5587 
5588 	if (test_bit(ICE_SUSPENDED, pf->state)) {
5589 		dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
5590 			__func__);
5591 		return;
5592 	}
5593 
5594 	ice_restore_all_vfs_msi_state(pdev);
5595 
5596 	ice_do_reset(pf, ICE_RESET_PFR);
5597 	ice_service_task_restart(pf);
5598 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5599 }
5600 
5601 /**
5602  * ice_pci_err_reset_prepare - prepare device driver for PCI reset
5603  * @pdev: PCI device information struct
5604  */
ice_pci_err_reset_prepare(struct pci_dev * pdev)5605 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
5606 {
5607 	struct ice_pf *pf = pci_get_drvdata(pdev);
5608 
5609 	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5610 		ice_service_task_stop(pf);
5611 
5612 		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5613 			set_bit(ICE_PFR_REQ, pf->state);
5614 			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5615 		}
5616 	}
5617 }
5618 
5619 /**
5620  * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
5621  * @pdev: PCI device information struct
5622  */
ice_pci_err_reset_done(struct pci_dev * pdev)5623 static void ice_pci_err_reset_done(struct pci_dev *pdev)
5624 {
5625 	ice_pci_err_resume(pdev);
5626 }
5627 
5628 /* ice_pci_tbl - PCI Device ID Table
5629  *
5630  * Wildcard entries (PCI_ANY_ID) should come last
5631  * Last entry must be all 0s
5632  *
5633  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
5634  *   Class, Class Mask, private data (not used) }
5635  */
5636 static const struct pci_device_id ice_pci_tbl[] = {
5637 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
5638 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
5639 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
5640 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE), 0 },
5641 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP), 0 },
5642 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP), 0 },
5643 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE), 0 },
5644 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP), 0 },
5645 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP), 0 },
5646 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T), 0 },
5647 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII), 0 },
5648 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE), 0 },
5649 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP), 0 },
5650 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP), 0 },
5651 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T), 0 },
5652 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII), 0 },
5653 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE), 0 },
5654 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP), 0 },
5655 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T), 0 },
5656 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII), 0 },
5657 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE), 0 },
5658 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP), 0 },
5659 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T), 0 },
5660 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE), 0 },
5661 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP), 0 },
5662 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822_SI_DFLT), 0 },
5663 	/* required last entry */
5664 	{ 0, }
5665 };
5666 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
5667 
5668 static __maybe_unused SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
5669 
5670 static const struct pci_error_handlers ice_pci_err_handler = {
5671 	.error_detected = ice_pci_err_detected,
5672 	.slot_reset = ice_pci_err_slot_reset,
5673 	.reset_prepare = ice_pci_err_reset_prepare,
5674 	.reset_done = ice_pci_err_reset_done,
5675 	.resume = ice_pci_err_resume
5676 };
5677 
5678 static struct pci_driver ice_driver = {
5679 	.name = KBUILD_MODNAME,
5680 	.id_table = ice_pci_tbl,
5681 	.probe = ice_probe,
5682 	.remove = ice_remove,
5683 #ifdef CONFIG_PM
5684 	.driver.pm = &ice_pm_ops,
5685 #endif /* CONFIG_PM */
5686 	.shutdown = ice_shutdown,
5687 	.sriov_configure = ice_sriov_configure,
5688 	.err_handler = &ice_pci_err_handler
5689 };
5690 
5691 /**
5692  * ice_module_init - Driver registration routine
5693  *
5694  * ice_module_init is the first routine called when the driver is
5695  * loaded. All it does is register with the PCI subsystem.
5696  */
ice_module_init(void)5697 static int __init ice_module_init(void)
5698 {
5699 	int status = -ENOMEM;
5700 
5701 	pr_info("%s\n", ice_driver_string);
5702 	pr_info("%s\n", ice_copyright);
5703 
5704 	ice_wq = alloc_workqueue("%s", 0, 0, KBUILD_MODNAME);
5705 	if (!ice_wq) {
5706 		pr_err("Failed to create workqueue\n");
5707 		return status;
5708 	}
5709 
5710 	ice_lag_wq = alloc_ordered_workqueue("ice_lag_wq", 0);
5711 	if (!ice_lag_wq) {
5712 		pr_err("Failed to create LAG workqueue\n");
5713 		goto err_dest_wq;
5714 	}
5715 
5716 	status = pci_register_driver(&ice_driver);
5717 	if (status) {
5718 		pr_err("failed to register PCI driver, err %d\n", status);
5719 		goto err_dest_lag_wq;
5720 	}
5721 
5722 	return 0;
5723 
5724 err_dest_lag_wq:
5725 	destroy_workqueue(ice_lag_wq);
5726 err_dest_wq:
5727 	destroy_workqueue(ice_wq);
5728 	return status;
5729 }
5730 module_init(ice_module_init);
5731 
5732 /**
5733  * ice_module_exit - Driver exit cleanup routine
5734  *
5735  * ice_module_exit is called just before the driver is removed
5736  * from memory.
5737  */
ice_module_exit(void)5738 static void __exit ice_module_exit(void)
5739 {
5740 	pci_unregister_driver(&ice_driver);
5741 	destroy_workqueue(ice_wq);
5742 	destroy_workqueue(ice_lag_wq);
5743 	pr_info("module unloaded\n");
5744 }
5745 module_exit(ice_module_exit);
5746 
5747 /**
5748  * ice_set_mac_address - NDO callback to set MAC address
5749  * @netdev: network interface device structure
5750  * @pi: pointer to an address structure
5751  *
5752  * Returns 0 on success, negative on failure
5753  */
ice_set_mac_address(struct net_device * netdev,void * pi)5754 static int ice_set_mac_address(struct net_device *netdev, void *pi)
5755 {
5756 	struct ice_netdev_priv *np = netdev_priv(netdev);
5757 	struct ice_vsi *vsi = np->vsi;
5758 	struct ice_pf *pf = vsi->back;
5759 	struct ice_hw *hw = &pf->hw;
5760 	struct sockaddr *addr = pi;
5761 	u8 old_mac[ETH_ALEN];
5762 	u8 flags = 0;
5763 	u8 *mac;
5764 	int err;
5765 
5766 	mac = (u8 *)addr->sa_data;
5767 
5768 	if (!is_valid_ether_addr(mac))
5769 		return -EADDRNOTAVAIL;
5770 
5771 	if (test_bit(ICE_DOWN, pf->state) ||
5772 	    ice_is_reset_in_progress(pf->state)) {
5773 		netdev_err(netdev, "can't set mac %pM. device not ready\n",
5774 			   mac);
5775 		return -EBUSY;
5776 	}
5777 
5778 	if (ice_chnl_dmac_fltr_cnt(pf)) {
5779 		netdev_err(netdev, "can't set mac %pM. Device has tc-flower filters, delete all of them and try again\n",
5780 			   mac);
5781 		return -EAGAIN;
5782 	}
5783 
5784 	netif_addr_lock_bh(netdev);
5785 	ether_addr_copy(old_mac, netdev->dev_addr);
5786 	/* change the netdev's MAC address */
5787 	eth_hw_addr_set(netdev, mac);
5788 	netif_addr_unlock_bh(netdev);
5789 
5790 	/* Clean up old MAC filter. Not an error if old filter doesn't exist */
5791 	err = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
5792 	if (err && err != -ENOENT) {
5793 		err = -EADDRNOTAVAIL;
5794 		goto err_update_filters;
5795 	}
5796 
5797 	/* Add filter for new MAC. If filter exists, return success */
5798 	err = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
5799 	if (err == -EEXIST) {
5800 		/* Although this MAC filter is already present in hardware it's
5801 		 * possible in some cases (e.g. bonding) that dev_addr was
5802 		 * modified outside of the driver and needs to be restored back
5803 		 * to this value.
5804 		 */
5805 		netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
5806 
5807 		return 0;
5808 	} else if (err) {
5809 		/* error if the new filter addition failed */
5810 		err = -EADDRNOTAVAIL;
5811 	}
5812 
5813 err_update_filters:
5814 	if (err) {
5815 		netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
5816 			   mac);
5817 		netif_addr_lock_bh(netdev);
5818 		eth_hw_addr_set(netdev, old_mac);
5819 		netif_addr_unlock_bh(netdev);
5820 		return err;
5821 	}
5822 
5823 	netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
5824 		   netdev->dev_addr);
5825 
5826 	/* write new MAC address to the firmware */
5827 	flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
5828 	err = ice_aq_manage_mac_write(hw, mac, flags, NULL);
5829 	if (err) {
5830 		netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n",
5831 			   mac, err);
5832 	}
5833 	return 0;
5834 }
5835 
5836 /**
5837  * ice_set_rx_mode - NDO callback to set the netdev filters
5838  * @netdev: network interface device structure
5839  */
ice_set_rx_mode(struct net_device * netdev)5840 static void ice_set_rx_mode(struct net_device *netdev)
5841 {
5842 	struct ice_netdev_priv *np = netdev_priv(netdev);
5843 	struct ice_vsi *vsi = np->vsi;
5844 
5845 	if (!vsi || ice_is_switchdev_running(vsi->back))
5846 		return;
5847 
5848 	/* Set the flags to synchronize filters
5849 	 * ndo_set_rx_mode may be triggered even without a change in netdev
5850 	 * flags
5851 	 */
5852 	set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
5853 	set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
5854 	set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
5855 
5856 	/* schedule our worker thread which will take care of
5857 	 * applying the new filter changes
5858 	 */
5859 	ice_service_task_schedule(vsi->back);
5860 }
5861 
5862 /**
5863  * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
5864  * @netdev: network interface device structure
5865  * @queue_index: Queue ID
5866  * @maxrate: maximum bandwidth in Mbps
5867  */
5868 static int
ice_set_tx_maxrate(struct net_device * netdev,int queue_index,u32 maxrate)5869 ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
5870 {
5871 	struct ice_netdev_priv *np = netdev_priv(netdev);
5872 	struct ice_vsi *vsi = np->vsi;
5873 	u16 q_handle;
5874 	int status;
5875 	u8 tc;
5876 
5877 	/* Validate maxrate requested is within permitted range */
5878 	if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
5879 		netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
5880 			   maxrate, queue_index);
5881 		return -EINVAL;
5882 	}
5883 
5884 	q_handle = vsi->tx_rings[queue_index]->q_handle;
5885 	tc = ice_dcb_get_tc(vsi, queue_index);
5886 
5887 	vsi = ice_locate_vsi_using_queue(vsi, queue_index);
5888 	if (!vsi) {
5889 		netdev_err(netdev, "Invalid VSI for given queue %d\n",
5890 			   queue_index);
5891 		return -EINVAL;
5892 	}
5893 
5894 	/* Set BW back to default, when user set maxrate to 0 */
5895 	if (!maxrate)
5896 		status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
5897 					       q_handle, ICE_MAX_BW);
5898 	else
5899 		status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
5900 					  q_handle, ICE_MAX_BW, maxrate * 1000);
5901 	if (status)
5902 		netdev_err(netdev, "Unable to set Tx max rate, error %d\n",
5903 			   status);
5904 
5905 	return status;
5906 }
5907 
5908 /**
5909  * ice_fdb_add - add an entry to the hardware database
5910  * @ndm: the input from the stack
5911  * @tb: pointer to array of nladdr (unused)
5912  * @dev: the net device pointer
5913  * @addr: the MAC address entry being added
5914  * @vid: VLAN ID
5915  * @flags: instructions from stack about fdb operation
5916  * @extack: netlink extended ack
5917  */
5918 static int
ice_fdb_add(struct ndmsg * ndm,struct nlattr __always_unused * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u16 flags,struct netlink_ext_ack __always_unused * extack)5919 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
5920 	    struct net_device *dev, const unsigned char *addr, u16 vid,
5921 	    u16 flags, struct netlink_ext_ack __always_unused *extack)
5922 {
5923 	int err;
5924 
5925 	if (vid) {
5926 		netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
5927 		return -EINVAL;
5928 	}
5929 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
5930 		netdev_err(dev, "FDB only supports static addresses\n");
5931 		return -EINVAL;
5932 	}
5933 
5934 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
5935 		err = dev_uc_add_excl(dev, addr);
5936 	else if (is_multicast_ether_addr(addr))
5937 		err = dev_mc_add_excl(dev, addr);
5938 	else
5939 		err = -EINVAL;
5940 
5941 	/* Only return duplicate errors if NLM_F_EXCL is set */
5942 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
5943 		err = 0;
5944 
5945 	return err;
5946 }
5947 
5948 /**
5949  * ice_fdb_del - delete an entry from the hardware database
5950  * @ndm: the input from the stack
5951  * @tb: pointer to array of nladdr (unused)
5952  * @dev: the net device pointer
5953  * @addr: the MAC address entry being added
5954  * @vid: VLAN ID
5955  * @extack: netlink extended ack
5956  */
5957 static int
ice_fdb_del(struct ndmsg * ndm,__always_unused struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,__always_unused u16 vid,struct netlink_ext_ack * extack)5958 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
5959 	    struct net_device *dev, const unsigned char *addr,
5960 	    __always_unused u16 vid, struct netlink_ext_ack *extack)
5961 {
5962 	int err;
5963 
5964 	if (ndm->ndm_state & NUD_PERMANENT) {
5965 		netdev_err(dev, "FDB only supports static addresses\n");
5966 		return -EINVAL;
5967 	}
5968 
5969 	if (is_unicast_ether_addr(addr))
5970 		err = dev_uc_del(dev, addr);
5971 	else if (is_multicast_ether_addr(addr))
5972 		err = dev_mc_del(dev, addr);
5973 	else
5974 		err = -EINVAL;
5975 
5976 	return err;
5977 }
5978 
5979 #define NETIF_VLAN_OFFLOAD_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
5980 					 NETIF_F_HW_VLAN_CTAG_TX | \
5981 					 NETIF_F_HW_VLAN_STAG_RX | \
5982 					 NETIF_F_HW_VLAN_STAG_TX)
5983 
5984 #define NETIF_VLAN_STRIPPING_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
5985 					 NETIF_F_HW_VLAN_STAG_RX)
5986 
5987 #define NETIF_VLAN_FILTERING_FEATURES	(NETIF_F_HW_VLAN_CTAG_FILTER | \
5988 					 NETIF_F_HW_VLAN_STAG_FILTER)
5989 
5990 /**
5991  * ice_fix_features - fix the netdev features flags based on device limitations
5992  * @netdev: ptr to the netdev that flags are being fixed on
5993  * @features: features that need to be checked and possibly fixed
5994  *
5995  * Make sure any fixups are made to features in this callback. This enables the
5996  * driver to not have to check unsupported configurations throughout the driver
5997  * because that's the responsiblity of this callback.
5998  *
5999  * Single VLAN Mode (SVM) Supported Features:
6000  *	NETIF_F_HW_VLAN_CTAG_FILTER
6001  *	NETIF_F_HW_VLAN_CTAG_RX
6002  *	NETIF_F_HW_VLAN_CTAG_TX
6003  *
6004  * Double VLAN Mode (DVM) Supported Features:
6005  *	NETIF_F_HW_VLAN_CTAG_FILTER
6006  *	NETIF_F_HW_VLAN_CTAG_RX
6007  *	NETIF_F_HW_VLAN_CTAG_TX
6008  *
6009  *	NETIF_F_HW_VLAN_STAG_FILTER
6010  *	NETIF_HW_VLAN_STAG_RX
6011  *	NETIF_HW_VLAN_STAG_TX
6012  *
6013  * Features that need fixing:
6014  *	Cannot simultaneously enable CTAG and STAG stripping and/or insertion.
6015  *	These are mutually exlusive as the VSI context cannot support multiple
6016  *	VLAN ethertypes simultaneously for stripping and/or insertion. If this
6017  *	is not done, then default to clearing the requested STAG offload
6018  *	settings.
6019  *
6020  *	All supported filtering has to be enabled or disabled together. For
6021  *	example, in DVM, CTAG and STAG filtering have to be enabled and disabled
6022  *	together. If this is not done, then default to VLAN filtering disabled.
6023  *	These are mutually exclusive as there is currently no way to
6024  *	enable/disable VLAN filtering based on VLAN ethertype when using VLAN
6025  *	prune rules.
6026  */
6027 static netdev_features_t
ice_fix_features(struct net_device * netdev,netdev_features_t features)6028 ice_fix_features(struct net_device *netdev, netdev_features_t features)
6029 {
6030 	struct ice_netdev_priv *np = netdev_priv(netdev);
6031 	netdev_features_t req_vlan_fltr, cur_vlan_fltr;
6032 	bool cur_ctag, cur_stag, req_ctag, req_stag;
6033 
6034 	cur_vlan_fltr = netdev->features & NETIF_VLAN_FILTERING_FEATURES;
6035 	cur_ctag = cur_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6036 	cur_stag = cur_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
6037 
6038 	req_vlan_fltr = features & NETIF_VLAN_FILTERING_FEATURES;
6039 	req_ctag = req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6040 	req_stag = req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
6041 
6042 	if (req_vlan_fltr != cur_vlan_fltr) {
6043 		if (ice_is_dvm_ena(&np->vsi->back->hw)) {
6044 			if (req_ctag && req_stag) {
6045 				features |= NETIF_VLAN_FILTERING_FEATURES;
6046 			} else if (!req_ctag && !req_stag) {
6047 				features &= ~NETIF_VLAN_FILTERING_FEATURES;
6048 			} else if ((!cur_ctag && req_ctag && !cur_stag) ||
6049 				   (!cur_stag && req_stag && !cur_ctag)) {
6050 				features |= NETIF_VLAN_FILTERING_FEATURES;
6051 				netdev_warn(netdev,  "802.1Q and 802.1ad VLAN filtering must be either both on or both off. VLAN filtering has been enabled for both types.\n");
6052 			} else if ((cur_ctag && !req_ctag && cur_stag) ||
6053 				   (cur_stag && !req_stag && cur_ctag)) {
6054 				features &= ~NETIF_VLAN_FILTERING_FEATURES;
6055 				netdev_warn(netdev,  "802.1Q and 802.1ad VLAN filtering must be either both on or both off. VLAN filtering has been disabled for both types.\n");
6056 			}
6057 		} else {
6058 			if (req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER)
6059 				netdev_warn(netdev, "cannot support requested 802.1ad filtering setting in SVM mode\n");
6060 
6061 			if (req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER)
6062 				features |= NETIF_F_HW_VLAN_CTAG_FILTER;
6063 		}
6064 	}
6065 
6066 	if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
6067 	    (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))) {
6068 		netdev_warn(netdev, "cannot support CTAG and STAG VLAN stripping and/or insertion simultaneously since CTAG and STAG offloads are mutually exclusive, clearing STAG offload settings\n");
6069 		features &= ~(NETIF_F_HW_VLAN_STAG_RX |
6070 			      NETIF_F_HW_VLAN_STAG_TX);
6071 	}
6072 
6073 	if (!(netdev->features & NETIF_F_RXFCS) &&
6074 	    (features & NETIF_F_RXFCS) &&
6075 	    (features & NETIF_VLAN_STRIPPING_FEATURES) &&
6076 	    !ice_vsi_has_non_zero_vlans(np->vsi)) {
6077 		netdev_warn(netdev, "Disabling VLAN stripping as FCS/CRC stripping is also disabled and there is no VLAN configured\n");
6078 		features &= ~NETIF_VLAN_STRIPPING_FEATURES;
6079 	}
6080 
6081 	return features;
6082 }
6083 
6084 /**
6085  * ice_set_vlan_offload_features - set VLAN offload features for the PF VSI
6086  * @vsi: PF's VSI
6087  * @features: features used to determine VLAN offload settings
6088  *
6089  * First, determine the vlan_ethertype based on the VLAN offload bits in
6090  * features. Then determine if stripping and insertion should be enabled or
6091  * disabled. Finally enable or disable VLAN stripping and insertion.
6092  */
6093 static int
ice_set_vlan_offload_features(struct ice_vsi * vsi,netdev_features_t features)6094 ice_set_vlan_offload_features(struct ice_vsi *vsi, netdev_features_t features)
6095 {
6096 	bool enable_stripping = true, enable_insertion = true;
6097 	struct ice_vsi_vlan_ops *vlan_ops;
6098 	int strip_err = 0, insert_err = 0;
6099 	u16 vlan_ethertype = 0;
6100 
6101 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6102 
6103 	if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
6104 		vlan_ethertype = ETH_P_8021AD;
6105 	else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
6106 		vlan_ethertype = ETH_P_8021Q;
6107 
6108 	if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
6109 		enable_stripping = false;
6110 	if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
6111 		enable_insertion = false;
6112 
6113 	if (enable_stripping)
6114 		strip_err = vlan_ops->ena_stripping(vsi, vlan_ethertype);
6115 	else
6116 		strip_err = vlan_ops->dis_stripping(vsi);
6117 
6118 	if (enable_insertion)
6119 		insert_err = vlan_ops->ena_insertion(vsi, vlan_ethertype);
6120 	else
6121 		insert_err = vlan_ops->dis_insertion(vsi);
6122 
6123 	if (strip_err || insert_err)
6124 		return -EIO;
6125 
6126 	return 0;
6127 }
6128 
6129 /**
6130  * ice_set_vlan_filtering_features - set VLAN filtering features for the PF VSI
6131  * @vsi: PF's VSI
6132  * @features: features used to determine VLAN filtering settings
6133  *
6134  * Enable or disable Rx VLAN filtering based on the VLAN filtering bits in the
6135  * features.
6136  */
6137 static int
ice_set_vlan_filtering_features(struct ice_vsi * vsi,netdev_features_t features)6138 ice_set_vlan_filtering_features(struct ice_vsi *vsi, netdev_features_t features)
6139 {
6140 	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6141 	int err = 0;
6142 
6143 	/* support Single VLAN Mode (SVM) and Double VLAN Mode (DVM) by checking
6144 	 * if either bit is set
6145 	 */
6146 	if (features &
6147 	    (NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER))
6148 		err = vlan_ops->ena_rx_filtering(vsi);
6149 	else
6150 		err = vlan_ops->dis_rx_filtering(vsi);
6151 
6152 	return err;
6153 }
6154 
6155 /**
6156  * ice_set_vlan_features - set VLAN settings based on suggested feature set
6157  * @netdev: ptr to the netdev being adjusted
6158  * @features: the feature set that the stack is suggesting
6159  *
6160  * Only update VLAN settings if the requested_vlan_features are different than
6161  * the current_vlan_features.
6162  */
6163 static int
ice_set_vlan_features(struct net_device * netdev,netdev_features_t features)6164 ice_set_vlan_features(struct net_device *netdev, netdev_features_t features)
6165 {
6166 	netdev_features_t current_vlan_features, requested_vlan_features;
6167 	struct ice_netdev_priv *np = netdev_priv(netdev);
6168 	struct ice_vsi *vsi = np->vsi;
6169 	int err;
6170 
6171 	current_vlan_features = netdev->features & NETIF_VLAN_OFFLOAD_FEATURES;
6172 	requested_vlan_features = features & NETIF_VLAN_OFFLOAD_FEATURES;
6173 	if (current_vlan_features ^ requested_vlan_features) {
6174 		if ((features & NETIF_F_RXFCS) &&
6175 		    (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6176 			dev_err(ice_pf_to_dev(vsi->back),
6177 				"To enable VLAN stripping, you must first enable FCS/CRC stripping\n");
6178 			return -EIO;
6179 		}
6180 
6181 		err = ice_set_vlan_offload_features(vsi, features);
6182 		if (err)
6183 			return err;
6184 	}
6185 
6186 	current_vlan_features = netdev->features &
6187 		NETIF_VLAN_FILTERING_FEATURES;
6188 	requested_vlan_features = features & NETIF_VLAN_FILTERING_FEATURES;
6189 	if (current_vlan_features ^ requested_vlan_features) {
6190 		err = ice_set_vlan_filtering_features(vsi, features);
6191 		if (err)
6192 			return err;
6193 	}
6194 
6195 	return 0;
6196 }
6197 
6198 /**
6199  * ice_set_loopback - turn on/off loopback mode on underlying PF
6200  * @vsi: ptr to VSI
6201  * @ena: flag to indicate the on/off setting
6202  */
ice_set_loopback(struct ice_vsi * vsi,bool ena)6203 static int ice_set_loopback(struct ice_vsi *vsi, bool ena)
6204 {
6205 	bool if_running = netif_running(vsi->netdev);
6206 	int ret;
6207 
6208 	if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
6209 		ret = ice_down(vsi);
6210 		if (ret) {
6211 			netdev_err(vsi->netdev, "Preparing device to toggle loopback failed\n");
6212 			return ret;
6213 		}
6214 	}
6215 	ret = ice_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
6216 	if (ret)
6217 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
6218 	if (if_running)
6219 		ret = ice_up(vsi);
6220 
6221 	return ret;
6222 }
6223 
6224 /**
6225  * ice_set_features - set the netdev feature flags
6226  * @netdev: ptr to the netdev being adjusted
6227  * @features: the feature set that the stack is suggesting
6228  */
6229 static int
ice_set_features(struct net_device * netdev,netdev_features_t features)6230 ice_set_features(struct net_device *netdev, netdev_features_t features)
6231 {
6232 	netdev_features_t changed = netdev->features ^ features;
6233 	struct ice_netdev_priv *np = netdev_priv(netdev);
6234 	struct ice_vsi *vsi = np->vsi;
6235 	struct ice_pf *pf = vsi->back;
6236 	int ret = 0;
6237 
6238 	/* Don't set any netdev advanced features with device in Safe Mode */
6239 	if (ice_is_safe_mode(pf)) {
6240 		dev_err(ice_pf_to_dev(pf),
6241 			"Device is in Safe Mode - not enabling advanced netdev features\n");
6242 		return ret;
6243 	}
6244 
6245 	/* Do not change setting during reset */
6246 	if (ice_is_reset_in_progress(pf->state)) {
6247 		dev_err(ice_pf_to_dev(pf),
6248 			"Device is resetting, changing advanced netdev features temporarily unavailable.\n");
6249 		return -EBUSY;
6250 	}
6251 
6252 	/* Multiple features can be changed in one call so keep features in
6253 	 * separate if/else statements to guarantee each feature is checked
6254 	 */
6255 	if (changed & NETIF_F_RXHASH)
6256 		ice_vsi_manage_rss_lut(vsi, !!(features & NETIF_F_RXHASH));
6257 
6258 	ret = ice_set_vlan_features(netdev, features);
6259 	if (ret)
6260 		return ret;
6261 
6262 	/* Turn on receive of FCS aka CRC, and after setting this
6263 	 * flag the packet data will have the 4 byte CRC appended
6264 	 */
6265 	if (changed & NETIF_F_RXFCS) {
6266 		if ((features & NETIF_F_RXFCS) &&
6267 		    (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6268 			dev_err(ice_pf_to_dev(vsi->back),
6269 				"To disable FCS/CRC stripping, you must first disable VLAN stripping\n");
6270 			return -EIO;
6271 		}
6272 
6273 		ice_vsi_cfg_crc_strip(vsi, !!(features & NETIF_F_RXFCS));
6274 		ret = ice_down_up(vsi);
6275 		if (ret)
6276 			return ret;
6277 	}
6278 
6279 	if (changed & NETIF_F_NTUPLE) {
6280 		bool ena = !!(features & NETIF_F_NTUPLE);
6281 
6282 		ice_vsi_manage_fdir(vsi, ena);
6283 		ena ? ice_init_arfs(vsi) : ice_clear_arfs(vsi);
6284 	}
6285 
6286 	/* don't turn off hw_tc_offload when ADQ is already enabled */
6287 	if (!(features & NETIF_F_HW_TC) && ice_is_adq_active(pf)) {
6288 		dev_err(ice_pf_to_dev(pf), "ADQ is active, can't turn hw_tc_offload off\n");
6289 		return -EACCES;
6290 	}
6291 
6292 	if (changed & NETIF_F_HW_TC) {
6293 		bool ena = !!(features & NETIF_F_HW_TC);
6294 
6295 		ena ? set_bit(ICE_FLAG_CLS_FLOWER, pf->flags) :
6296 		      clear_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
6297 	}
6298 
6299 	if (changed & NETIF_F_LOOPBACK)
6300 		ret = ice_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
6301 
6302 	return ret;
6303 }
6304 
6305 /**
6306  * ice_vsi_vlan_setup - Setup VLAN offload properties on a PF VSI
6307  * @vsi: VSI to setup VLAN properties for
6308  */
ice_vsi_vlan_setup(struct ice_vsi * vsi)6309 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
6310 {
6311 	int err;
6312 
6313 	err = ice_set_vlan_offload_features(vsi, vsi->netdev->features);
6314 	if (err)
6315 		return err;
6316 
6317 	err = ice_set_vlan_filtering_features(vsi, vsi->netdev->features);
6318 	if (err)
6319 		return err;
6320 
6321 	return ice_vsi_add_vlan_zero(vsi);
6322 }
6323 
6324 /**
6325  * ice_vsi_cfg_lan - Setup the VSI lan related config
6326  * @vsi: the VSI being configured
6327  *
6328  * Return 0 on success and negative value on error
6329  */
ice_vsi_cfg_lan(struct ice_vsi * vsi)6330 int ice_vsi_cfg_lan(struct ice_vsi *vsi)
6331 {
6332 	int err;
6333 
6334 	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
6335 		ice_set_rx_mode(vsi->netdev);
6336 
6337 		err = ice_vsi_vlan_setup(vsi);
6338 		if (err)
6339 			return err;
6340 	}
6341 	ice_vsi_cfg_dcb_rings(vsi);
6342 
6343 	err = ice_vsi_cfg_lan_txqs(vsi);
6344 	if (!err && ice_is_xdp_ena_vsi(vsi))
6345 		err = ice_vsi_cfg_xdp_txqs(vsi);
6346 	if (!err)
6347 		err = ice_vsi_cfg_rxqs(vsi);
6348 
6349 	return err;
6350 }
6351 
6352 /* THEORY OF MODERATION:
6353  * The ice driver hardware works differently than the hardware that DIMLIB was
6354  * originally made for. ice hardware doesn't have packet count limits that
6355  * can trigger an interrupt, but it *does* have interrupt rate limit support,
6356  * which is hard-coded to a limit of 250,000 ints/second.
6357  * If not using dynamic moderation, the INTRL value can be modified
6358  * by ethtool rx-usecs-high.
6359  */
6360 struct ice_dim {
6361 	/* the throttle rate for interrupts, basically worst case delay before
6362 	 * an initial interrupt fires, value is stored in microseconds.
6363 	 */
6364 	u16 itr;
6365 };
6366 
6367 /* Make a different profile for Rx that doesn't allow quite so aggressive
6368  * moderation at the high end (it maxes out at 126us or about 8k interrupts a
6369  * second.
6370  */
6371 static const struct ice_dim rx_profile[] = {
6372 	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6373 	{8},    /* 125,000 ints/s */
6374 	{16},   /*  62,500 ints/s */
6375 	{62},   /*  16,129 ints/s */
6376 	{126}   /*   7,936 ints/s */
6377 };
6378 
6379 /* The transmit profile, which has the same sorts of values
6380  * as the previous struct
6381  */
6382 static const struct ice_dim tx_profile[] = {
6383 	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6384 	{8},    /* 125,000 ints/s */
6385 	{40},   /*  16,125 ints/s */
6386 	{128},  /*   7,812 ints/s */
6387 	{256}   /*   3,906 ints/s */
6388 };
6389 
ice_tx_dim_work(struct work_struct * work)6390 static void ice_tx_dim_work(struct work_struct *work)
6391 {
6392 	struct ice_ring_container *rc;
6393 	struct dim *dim;
6394 	u16 itr;
6395 
6396 	dim = container_of(work, struct dim, work);
6397 	rc = dim->priv;
6398 
6399 	WARN_ON(dim->profile_ix >= ARRAY_SIZE(tx_profile));
6400 
6401 	/* look up the values in our local table */
6402 	itr = tx_profile[dim->profile_ix].itr;
6403 
6404 	ice_trace(tx_dim_work, container_of(rc, struct ice_q_vector, tx), dim);
6405 	ice_write_itr(rc, itr);
6406 
6407 	dim->state = DIM_START_MEASURE;
6408 }
6409 
ice_rx_dim_work(struct work_struct * work)6410 static void ice_rx_dim_work(struct work_struct *work)
6411 {
6412 	struct ice_ring_container *rc;
6413 	struct dim *dim;
6414 	u16 itr;
6415 
6416 	dim = container_of(work, struct dim, work);
6417 	rc = dim->priv;
6418 
6419 	WARN_ON(dim->profile_ix >= ARRAY_SIZE(rx_profile));
6420 
6421 	/* look up the values in our local table */
6422 	itr = rx_profile[dim->profile_ix].itr;
6423 
6424 	ice_trace(rx_dim_work, container_of(rc, struct ice_q_vector, rx), dim);
6425 	ice_write_itr(rc, itr);
6426 
6427 	dim->state = DIM_START_MEASURE;
6428 }
6429 
6430 #define ICE_DIM_DEFAULT_PROFILE_IX 1
6431 
6432 /**
6433  * ice_init_moderation - set up interrupt moderation
6434  * @q_vector: the vector containing rings to be configured
6435  *
6436  * Set up interrupt moderation registers, with the intent to do the right thing
6437  * when called from reset or from probe, and whether or not dynamic moderation
6438  * is enabled or not. Take special care to write all the registers in both
6439  * dynamic moderation mode or not in order to make sure hardware is in a known
6440  * state.
6441  */
ice_init_moderation(struct ice_q_vector * q_vector)6442 static void ice_init_moderation(struct ice_q_vector *q_vector)
6443 {
6444 	struct ice_ring_container *rc;
6445 	bool tx_dynamic, rx_dynamic;
6446 
6447 	rc = &q_vector->tx;
6448 	INIT_WORK(&rc->dim.work, ice_tx_dim_work);
6449 	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6450 	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6451 	rc->dim.priv = rc;
6452 	tx_dynamic = ITR_IS_DYNAMIC(rc);
6453 
6454 	/* set the initial TX ITR to match the above */
6455 	ice_write_itr(rc, tx_dynamic ?
6456 		      tx_profile[rc->dim.profile_ix].itr : rc->itr_setting);
6457 
6458 	rc = &q_vector->rx;
6459 	INIT_WORK(&rc->dim.work, ice_rx_dim_work);
6460 	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6461 	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6462 	rc->dim.priv = rc;
6463 	rx_dynamic = ITR_IS_DYNAMIC(rc);
6464 
6465 	/* set the initial RX ITR to match the above */
6466 	ice_write_itr(rc, rx_dynamic ? rx_profile[rc->dim.profile_ix].itr :
6467 				       rc->itr_setting);
6468 
6469 	ice_set_q_vector_intrl(q_vector);
6470 }
6471 
6472 /**
6473  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
6474  * @vsi: the VSI being configured
6475  */
ice_napi_enable_all(struct ice_vsi * vsi)6476 static void ice_napi_enable_all(struct ice_vsi *vsi)
6477 {
6478 	int q_idx;
6479 
6480 	if (!vsi->netdev)
6481 		return;
6482 
6483 	ice_for_each_q_vector(vsi, q_idx) {
6484 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6485 
6486 		ice_init_moderation(q_vector);
6487 
6488 		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6489 			napi_enable(&q_vector->napi);
6490 	}
6491 }
6492 
6493 /**
6494  * ice_up_complete - Finish the last steps of bringing up a connection
6495  * @vsi: The VSI being configured
6496  *
6497  * Return 0 on success and negative value on error
6498  */
ice_up_complete(struct ice_vsi * vsi)6499 static int ice_up_complete(struct ice_vsi *vsi)
6500 {
6501 	struct ice_pf *pf = vsi->back;
6502 	int err;
6503 
6504 	ice_vsi_cfg_msix(vsi);
6505 
6506 	/* Enable only Rx rings, Tx rings were enabled by the FW when the
6507 	 * Tx queue group list was configured and the context bits were
6508 	 * programmed using ice_vsi_cfg_txqs
6509 	 */
6510 	err = ice_vsi_start_all_rx_rings(vsi);
6511 	if (err)
6512 		return err;
6513 
6514 	clear_bit(ICE_VSI_DOWN, vsi->state);
6515 	ice_napi_enable_all(vsi);
6516 	ice_vsi_ena_irq(vsi);
6517 
6518 	if (vsi->port_info &&
6519 	    (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
6520 	    vsi->netdev && vsi->type == ICE_VSI_PF) {
6521 		ice_print_link_msg(vsi, true);
6522 		netif_tx_start_all_queues(vsi->netdev);
6523 		netif_carrier_on(vsi->netdev);
6524 		ice_ptp_link_change(pf, pf->hw.pf_id, true);
6525 	}
6526 
6527 	/* Perform an initial read of the statistics registers now to
6528 	 * set the baseline so counters are ready when interface is up
6529 	 */
6530 	ice_update_eth_stats(vsi);
6531 
6532 	if (vsi->type == ICE_VSI_PF)
6533 		ice_service_task_schedule(pf);
6534 
6535 	return 0;
6536 }
6537 
6538 /**
6539  * ice_up - Bring the connection back up after being down
6540  * @vsi: VSI being configured
6541  */
ice_up(struct ice_vsi * vsi)6542 int ice_up(struct ice_vsi *vsi)
6543 {
6544 	int err;
6545 
6546 	err = ice_vsi_cfg_lan(vsi);
6547 	if (!err)
6548 		err = ice_up_complete(vsi);
6549 
6550 	return err;
6551 }
6552 
6553 /**
6554  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
6555  * @syncp: pointer to u64_stats_sync
6556  * @stats: stats that pkts and bytes count will be taken from
6557  * @pkts: packets stats counter
6558  * @bytes: bytes stats counter
6559  *
6560  * This function fetches stats from the ring considering the atomic operations
6561  * that needs to be performed to read u64 values in 32 bit machine.
6562  */
6563 void
ice_fetch_u64_stats_per_ring(struct u64_stats_sync * syncp,struct ice_q_stats stats,u64 * pkts,u64 * bytes)6564 ice_fetch_u64_stats_per_ring(struct u64_stats_sync *syncp,
6565 			     struct ice_q_stats stats, u64 *pkts, u64 *bytes)
6566 {
6567 	unsigned int start;
6568 
6569 	do {
6570 		start = u64_stats_fetch_begin(syncp);
6571 		*pkts = stats.pkts;
6572 		*bytes = stats.bytes;
6573 	} while (u64_stats_fetch_retry(syncp, start));
6574 }
6575 
6576 /**
6577  * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
6578  * @vsi: the VSI to be updated
6579  * @vsi_stats: the stats struct to be updated
6580  * @rings: rings to work on
6581  * @count: number of rings
6582  */
6583 static void
ice_update_vsi_tx_ring_stats(struct ice_vsi * vsi,struct rtnl_link_stats64 * vsi_stats,struct ice_tx_ring ** rings,u16 count)6584 ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi,
6585 			     struct rtnl_link_stats64 *vsi_stats,
6586 			     struct ice_tx_ring **rings, u16 count)
6587 {
6588 	u16 i;
6589 
6590 	for (i = 0; i < count; i++) {
6591 		struct ice_tx_ring *ring;
6592 		u64 pkts = 0, bytes = 0;
6593 
6594 		ring = READ_ONCE(rings[i]);
6595 		if (!ring || !ring->ring_stats)
6596 			continue;
6597 		ice_fetch_u64_stats_per_ring(&ring->ring_stats->syncp,
6598 					     ring->ring_stats->stats, &pkts,
6599 					     &bytes);
6600 		vsi_stats->tx_packets += pkts;
6601 		vsi_stats->tx_bytes += bytes;
6602 		vsi->tx_restart += ring->ring_stats->tx_stats.restart_q;
6603 		vsi->tx_busy += ring->ring_stats->tx_stats.tx_busy;
6604 		vsi->tx_linearize += ring->ring_stats->tx_stats.tx_linearize;
6605 	}
6606 }
6607 
6608 /**
6609  * ice_update_vsi_ring_stats - Update VSI stats counters
6610  * @vsi: the VSI to be updated
6611  */
ice_update_vsi_ring_stats(struct ice_vsi * vsi)6612 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
6613 {
6614 	struct rtnl_link_stats64 *net_stats, *stats_prev;
6615 	struct rtnl_link_stats64 *vsi_stats;
6616 	struct ice_pf *pf = vsi->back;
6617 	u64 pkts, bytes;
6618 	int i;
6619 
6620 	vsi_stats = kzalloc(sizeof(*vsi_stats), GFP_ATOMIC);
6621 	if (!vsi_stats)
6622 		return;
6623 
6624 	/* reset non-netdev (extended) stats */
6625 	vsi->tx_restart = 0;
6626 	vsi->tx_busy = 0;
6627 	vsi->tx_linearize = 0;
6628 	vsi->rx_buf_failed = 0;
6629 	vsi->rx_page_failed = 0;
6630 
6631 	rcu_read_lock();
6632 
6633 	/* update Tx rings counters */
6634 	ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->tx_rings,
6635 				     vsi->num_txq);
6636 
6637 	/* update Rx rings counters */
6638 	ice_for_each_rxq(vsi, i) {
6639 		struct ice_rx_ring *ring = READ_ONCE(vsi->rx_rings[i]);
6640 		struct ice_ring_stats *ring_stats;
6641 
6642 		ring_stats = ring->ring_stats;
6643 		ice_fetch_u64_stats_per_ring(&ring_stats->syncp,
6644 					     ring_stats->stats, &pkts,
6645 					     &bytes);
6646 		vsi_stats->rx_packets += pkts;
6647 		vsi_stats->rx_bytes += bytes;
6648 		vsi->rx_buf_failed += ring_stats->rx_stats.alloc_buf_failed;
6649 		vsi->rx_page_failed += ring_stats->rx_stats.alloc_page_failed;
6650 	}
6651 
6652 	/* update XDP Tx rings counters */
6653 	if (ice_is_xdp_ena_vsi(vsi))
6654 		ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->xdp_rings,
6655 					     vsi->num_xdp_txq);
6656 
6657 	rcu_read_unlock();
6658 
6659 	net_stats = &vsi->net_stats;
6660 	stats_prev = &vsi->net_stats_prev;
6661 
6662 	/* Update netdev counters, but keep in mind that values could start at
6663 	 * random value after PF reset. And as we increase the reported stat by
6664 	 * diff of Prev-Cur, we need to be sure that Prev is valid. If it's not,
6665 	 * let's skip this round.
6666 	 */
6667 	if (likely(pf->stat_prev_loaded)) {
6668 		net_stats->tx_packets += vsi_stats->tx_packets - stats_prev->tx_packets;
6669 		net_stats->tx_bytes += vsi_stats->tx_bytes - stats_prev->tx_bytes;
6670 		net_stats->rx_packets += vsi_stats->rx_packets - stats_prev->rx_packets;
6671 		net_stats->rx_bytes += vsi_stats->rx_bytes - stats_prev->rx_bytes;
6672 	}
6673 
6674 	stats_prev->tx_packets = vsi_stats->tx_packets;
6675 	stats_prev->tx_bytes = vsi_stats->tx_bytes;
6676 	stats_prev->rx_packets = vsi_stats->rx_packets;
6677 	stats_prev->rx_bytes = vsi_stats->rx_bytes;
6678 
6679 	kfree(vsi_stats);
6680 }
6681 
6682 /**
6683  * ice_update_vsi_stats - Update VSI stats counters
6684  * @vsi: the VSI to be updated
6685  */
ice_update_vsi_stats(struct ice_vsi * vsi)6686 void ice_update_vsi_stats(struct ice_vsi *vsi)
6687 {
6688 	struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
6689 	struct ice_eth_stats *cur_es = &vsi->eth_stats;
6690 	struct ice_pf *pf = vsi->back;
6691 
6692 	if (test_bit(ICE_VSI_DOWN, vsi->state) ||
6693 	    test_bit(ICE_CFG_BUSY, pf->state))
6694 		return;
6695 
6696 	/* get stats as recorded by Tx/Rx rings */
6697 	ice_update_vsi_ring_stats(vsi);
6698 
6699 	/* get VSI stats as recorded by the hardware */
6700 	ice_update_eth_stats(vsi);
6701 
6702 	cur_ns->tx_errors = cur_es->tx_errors;
6703 	cur_ns->rx_dropped = cur_es->rx_discards;
6704 	cur_ns->tx_dropped = cur_es->tx_discards;
6705 	cur_ns->multicast = cur_es->rx_multicast;
6706 
6707 	/* update some more netdev stats if this is main VSI */
6708 	if (vsi->type == ICE_VSI_PF) {
6709 		cur_ns->rx_crc_errors = pf->stats.crc_errors;
6710 		cur_ns->rx_errors = pf->stats.crc_errors +
6711 				    pf->stats.illegal_bytes +
6712 				    pf->stats.rx_len_errors +
6713 				    pf->stats.rx_undersize +
6714 				    pf->hw_csum_rx_error +
6715 				    pf->stats.rx_jabber +
6716 				    pf->stats.rx_fragments +
6717 				    pf->stats.rx_oversize;
6718 		cur_ns->rx_length_errors = pf->stats.rx_len_errors;
6719 		/* record drops from the port level */
6720 		cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
6721 	}
6722 }
6723 
6724 /**
6725  * ice_update_pf_stats - Update PF port stats counters
6726  * @pf: PF whose stats needs to be updated
6727  */
ice_update_pf_stats(struct ice_pf * pf)6728 void ice_update_pf_stats(struct ice_pf *pf)
6729 {
6730 	struct ice_hw_port_stats *prev_ps, *cur_ps;
6731 	struct ice_hw *hw = &pf->hw;
6732 	u16 fd_ctr_base;
6733 	u8 port;
6734 
6735 	port = hw->port_info->lport;
6736 	prev_ps = &pf->stats_prev;
6737 	cur_ps = &pf->stats;
6738 
6739 	if (ice_is_reset_in_progress(pf->state))
6740 		pf->stat_prev_loaded = false;
6741 
6742 	ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
6743 			  &prev_ps->eth.rx_bytes,
6744 			  &cur_ps->eth.rx_bytes);
6745 
6746 	ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
6747 			  &prev_ps->eth.rx_unicast,
6748 			  &cur_ps->eth.rx_unicast);
6749 
6750 	ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
6751 			  &prev_ps->eth.rx_multicast,
6752 			  &cur_ps->eth.rx_multicast);
6753 
6754 	ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
6755 			  &prev_ps->eth.rx_broadcast,
6756 			  &cur_ps->eth.rx_broadcast);
6757 
6758 	ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
6759 			  &prev_ps->eth.rx_discards,
6760 			  &cur_ps->eth.rx_discards);
6761 
6762 	ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
6763 			  &prev_ps->eth.tx_bytes,
6764 			  &cur_ps->eth.tx_bytes);
6765 
6766 	ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
6767 			  &prev_ps->eth.tx_unicast,
6768 			  &cur_ps->eth.tx_unicast);
6769 
6770 	ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
6771 			  &prev_ps->eth.tx_multicast,
6772 			  &cur_ps->eth.tx_multicast);
6773 
6774 	ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
6775 			  &prev_ps->eth.tx_broadcast,
6776 			  &cur_ps->eth.tx_broadcast);
6777 
6778 	ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
6779 			  &prev_ps->tx_dropped_link_down,
6780 			  &cur_ps->tx_dropped_link_down);
6781 
6782 	ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
6783 			  &prev_ps->rx_size_64, &cur_ps->rx_size_64);
6784 
6785 	ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
6786 			  &prev_ps->rx_size_127, &cur_ps->rx_size_127);
6787 
6788 	ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
6789 			  &prev_ps->rx_size_255, &cur_ps->rx_size_255);
6790 
6791 	ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
6792 			  &prev_ps->rx_size_511, &cur_ps->rx_size_511);
6793 
6794 	ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
6795 			  &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
6796 
6797 	ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
6798 			  &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
6799 
6800 	ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
6801 			  &prev_ps->rx_size_big, &cur_ps->rx_size_big);
6802 
6803 	ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
6804 			  &prev_ps->tx_size_64, &cur_ps->tx_size_64);
6805 
6806 	ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
6807 			  &prev_ps->tx_size_127, &cur_ps->tx_size_127);
6808 
6809 	ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
6810 			  &prev_ps->tx_size_255, &cur_ps->tx_size_255);
6811 
6812 	ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
6813 			  &prev_ps->tx_size_511, &cur_ps->tx_size_511);
6814 
6815 	ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
6816 			  &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
6817 
6818 	ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
6819 			  &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
6820 
6821 	ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
6822 			  &prev_ps->tx_size_big, &cur_ps->tx_size_big);
6823 
6824 	fd_ctr_base = hw->fd_ctr_base;
6825 
6826 	ice_stat_update40(hw,
6827 			  GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
6828 			  pf->stat_prev_loaded, &prev_ps->fd_sb_match,
6829 			  &cur_ps->fd_sb_match);
6830 	ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
6831 			  &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
6832 
6833 	ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
6834 			  &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
6835 
6836 	ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
6837 			  &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
6838 
6839 	ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
6840 			  &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
6841 
6842 	ice_update_dcb_stats(pf);
6843 
6844 	ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
6845 			  &prev_ps->crc_errors, &cur_ps->crc_errors);
6846 
6847 	ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
6848 			  &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
6849 
6850 	ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
6851 			  &prev_ps->mac_local_faults,
6852 			  &cur_ps->mac_local_faults);
6853 
6854 	ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
6855 			  &prev_ps->mac_remote_faults,
6856 			  &cur_ps->mac_remote_faults);
6857 
6858 	ice_stat_update32(hw, GLPRT_RLEC(port), pf->stat_prev_loaded,
6859 			  &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
6860 
6861 	ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
6862 			  &prev_ps->rx_undersize, &cur_ps->rx_undersize);
6863 
6864 	ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
6865 			  &prev_ps->rx_fragments, &cur_ps->rx_fragments);
6866 
6867 	ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
6868 			  &prev_ps->rx_oversize, &cur_ps->rx_oversize);
6869 
6870 	ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
6871 			  &prev_ps->rx_jabber, &cur_ps->rx_jabber);
6872 
6873 	cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
6874 
6875 	pf->stat_prev_loaded = true;
6876 }
6877 
6878 /**
6879  * ice_get_stats64 - get statistics for network device structure
6880  * @netdev: network interface device structure
6881  * @stats: main device statistics structure
6882  */
6883 static
ice_get_stats64(struct net_device * netdev,struct rtnl_link_stats64 * stats)6884 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
6885 {
6886 	struct ice_netdev_priv *np = netdev_priv(netdev);
6887 	struct rtnl_link_stats64 *vsi_stats;
6888 	struct ice_vsi *vsi = np->vsi;
6889 
6890 	vsi_stats = &vsi->net_stats;
6891 
6892 	if (!vsi->num_txq || !vsi->num_rxq)
6893 		return;
6894 
6895 	/* netdev packet/byte stats come from ring counter. These are obtained
6896 	 * by summing up ring counters (done by ice_update_vsi_ring_stats).
6897 	 * But, only call the update routine and read the registers if VSI is
6898 	 * not down.
6899 	 */
6900 	if (!test_bit(ICE_VSI_DOWN, vsi->state))
6901 		ice_update_vsi_ring_stats(vsi);
6902 	stats->tx_packets = vsi_stats->tx_packets;
6903 	stats->tx_bytes = vsi_stats->tx_bytes;
6904 	stats->rx_packets = vsi_stats->rx_packets;
6905 	stats->rx_bytes = vsi_stats->rx_bytes;
6906 
6907 	/* The rest of the stats can be read from the hardware but instead we
6908 	 * just return values that the watchdog task has already obtained from
6909 	 * the hardware.
6910 	 */
6911 	stats->multicast = vsi_stats->multicast;
6912 	stats->tx_errors = vsi_stats->tx_errors;
6913 	stats->tx_dropped = vsi_stats->tx_dropped;
6914 	stats->rx_errors = vsi_stats->rx_errors;
6915 	stats->rx_dropped = vsi_stats->rx_dropped;
6916 	stats->rx_crc_errors = vsi_stats->rx_crc_errors;
6917 	stats->rx_length_errors = vsi_stats->rx_length_errors;
6918 }
6919 
6920 /**
6921  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
6922  * @vsi: VSI having NAPI disabled
6923  */
ice_napi_disable_all(struct ice_vsi * vsi)6924 static void ice_napi_disable_all(struct ice_vsi *vsi)
6925 {
6926 	int q_idx;
6927 
6928 	if (!vsi->netdev)
6929 		return;
6930 
6931 	ice_for_each_q_vector(vsi, q_idx) {
6932 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6933 
6934 		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6935 			napi_disable(&q_vector->napi);
6936 
6937 		cancel_work_sync(&q_vector->tx.dim.work);
6938 		cancel_work_sync(&q_vector->rx.dim.work);
6939 	}
6940 }
6941 
6942 /**
6943  * ice_down - Shutdown the connection
6944  * @vsi: The VSI being stopped
6945  *
6946  * Caller of this function is expected to set the vsi->state ICE_DOWN bit
6947  */
ice_down(struct ice_vsi * vsi)6948 int ice_down(struct ice_vsi *vsi)
6949 {
6950 	int i, tx_err, rx_err, vlan_err = 0;
6951 
6952 	WARN_ON(!test_bit(ICE_VSI_DOWN, vsi->state));
6953 
6954 	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
6955 		vlan_err = ice_vsi_del_vlan_zero(vsi);
6956 		ice_ptp_link_change(vsi->back, vsi->back->hw.pf_id, false);
6957 		netif_carrier_off(vsi->netdev);
6958 		netif_tx_disable(vsi->netdev);
6959 	} else if (vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
6960 		ice_eswitch_stop_all_tx_queues(vsi->back);
6961 	}
6962 
6963 	ice_vsi_dis_irq(vsi);
6964 
6965 	tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
6966 	if (tx_err)
6967 		netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
6968 			   vsi->vsi_num, tx_err);
6969 	if (!tx_err && ice_is_xdp_ena_vsi(vsi)) {
6970 		tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
6971 		if (tx_err)
6972 			netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
6973 				   vsi->vsi_num, tx_err);
6974 	}
6975 
6976 	rx_err = ice_vsi_stop_all_rx_rings(vsi);
6977 	if (rx_err)
6978 		netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
6979 			   vsi->vsi_num, rx_err);
6980 
6981 	ice_napi_disable_all(vsi);
6982 
6983 	ice_for_each_txq(vsi, i)
6984 		ice_clean_tx_ring(vsi->tx_rings[i]);
6985 
6986 	if (ice_is_xdp_ena_vsi(vsi))
6987 		ice_for_each_xdp_txq(vsi, i)
6988 			ice_clean_tx_ring(vsi->xdp_rings[i]);
6989 
6990 	ice_for_each_rxq(vsi, i)
6991 		ice_clean_rx_ring(vsi->rx_rings[i]);
6992 
6993 	if (tx_err || rx_err || vlan_err) {
6994 		netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
6995 			   vsi->vsi_num, vsi->vsw->sw_id);
6996 		return -EIO;
6997 	}
6998 
6999 	return 0;
7000 }
7001 
7002 /**
7003  * ice_down_up - shutdown the VSI connection and bring it up
7004  * @vsi: the VSI to be reconnected
7005  */
ice_down_up(struct ice_vsi * vsi)7006 int ice_down_up(struct ice_vsi *vsi)
7007 {
7008 	int ret;
7009 
7010 	/* if DOWN already set, nothing to do */
7011 	if (test_and_set_bit(ICE_VSI_DOWN, vsi->state))
7012 		return 0;
7013 
7014 	ret = ice_down(vsi);
7015 	if (ret)
7016 		return ret;
7017 
7018 	ret = ice_up(vsi);
7019 	if (ret) {
7020 		netdev_err(vsi->netdev, "reallocating resources failed during netdev features change, may need to reload driver\n");
7021 		return ret;
7022 	}
7023 
7024 	return 0;
7025 }
7026 
7027 /**
7028  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
7029  * @vsi: VSI having resources allocated
7030  *
7031  * Return 0 on success, negative on failure
7032  */
ice_vsi_setup_tx_rings(struct ice_vsi * vsi)7033 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
7034 {
7035 	int i, err = 0;
7036 
7037 	if (!vsi->num_txq) {
7038 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
7039 			vsi->vsi_num);
7040 		return -EINVAL;
7041 	}
7042 
7043 	ice_for_each_txq(vsi, i) {
7044 		struct ice_tx_ring *ring = vsi->tx_rings[i];
7045 
7046 		if (!ring)
7047 			return -EINVAL;
7048 
7049 		if (vsi->netdev)
7050 			ring->netdev = vsi->netdev;
7051 		err = ice_setup_tx_ring(ring);
7052 		if (err)
7053 			break;
7054 	}
7055 
7056 	return err;
7057 }
7058 
7059 /**
7060  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
7061  * @vsi: VSI having resources allocated
7062  *
7063  * Return 0 on success, negative on failure
7064  */
ice_vsi_setup_rx_rings(struct ice_vsi * vsi)7065 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
7066 {
7067 	int i, err = 0;
7068 
7069 	if (!vsi->num_rxq) {
7070 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
7071 			vsi->vsi_num);
7072 		return -EINVAL;
7073 	}
7074 
7075 	ice_for_each_rxq(vsi, i) {
7076 		struct ice_rx_ring *ring = vsi->rx_rings[i];
7077 
7078 		if (!ring)
7079 			return -EINVAL;
7080 
7081 		if (vsi->netdev)
7082 			ring->netdev = vsi->netdev;
7083 		err = ice_setup_rx_ring(ring);
7084 		if (err)
7085 			break;
7086 	}
7087 
7088 	return err;
7089 }
7090 
7091 /**
7092  * ice_vsi_open_ctrl - open control VSI for use
7093  * @vsi: the VSI to open
7094  *
7095  * Initialization of the Control VSI
7096  *
7097  * Returns 0 on success, negative value on error
7098  */
ice_vsi_open_ctrl(struct ice_vsi * vsi)7099 int ice_vsi_open_ctrl(struct ice_vsi *vsi)
7100 {
7101 	char int_name[ICE_INT_NAME_STR_LEN];
7102 	struct ice_pf *pf = vsi->back;
7103 	struct device *dev;
7104 	int err;
7105 
7106 	dev = ice_pf_to_dev(pf);
7107 	/* allocate descriptors */
7108 	err = ice_vsi_setup_tx_rings(vsi);
7109 	if (err)
7110 		goto err_setup_tx;
7111 
7112 	err = ice_vsi_setup_rx_rings(vsi);
7113 	if (err)
7114 		goto err_setup_rx;
7115 
7116 	err = ice_vsi_cfg_lan(vsi);
7117 	if (err)
7118 		goto err_setup_rx;
7119 
7120 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
7121 		 dev_driver_string(dev), dev_name(dev));
7122 	err = ice_vsi_req_irq_msix(vsi, int_name);
7123 	if (err)
7124 		goto err_setup_rx;
7125 
7126 	ice_vsi_cfg_msix(vsi);
7127 
7128 	err = ice_vsi_start_all_rx_rings(vsi);
7129 	if (err)
7130 		goto err_up_complete;
7131 
7132 	clear_bit(ICE_VSI_DOWN, vsi->state);
7133 	ice_vsi_ena_irq(vsi);
7134 
7135 	return 0;
7136 
7137 err_up_complete:
7138 	ice_down(vsi);
7139 err_setup_rx:
7140 	ice_vsi_free_rx_rings(vsi);
7141 err_setup_tx:
7142 	ice_vsi_free_tx_rings(vsi);
7143 
7144 	return err;
7145 }
7146 
7147 /**
7148  * ice_vsi_open - Called when a network interface is made active
7149  * @vsi: the VSI to open
7150  *
7151  * Initialization of the VSI
7152  *
7153  * Returns 0 on success, negative value on error
7154  */
ice_vsi_open(struct ice_vsi * vsi)7155 int ice_vsi_open(struct ice_vsi *vsi)
7156 {
7157 	char int_name[ICE_INT_NAME_STR_LEN];
7158 	struct ice_pf *pf = vsi->back;
7159 	int err;
7160 
7161 	/* allocate descriptors */
7162 	err = ice_vsi_setup_tx_rings(vsi);
7163 	if (err)
7164 		goto err_setup_tx;
7165 
7166 	err = ice_vsi_setup_rx_rings(vsi);
7167 	if (err)
7168 		goto err_setup_rx;
7169 
7170 	err = ice_vsi_cfg_lan(vsi);
7171 	if (err)
7172 		goto err_setup_rx;
7173 
7174 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7175 		 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
7176 	err = ice_vsi_req_irq_msix(vsi, int_name);
7177 	if (err)
7178 		goto err_setup_rx;
7179 
7180 	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
7181 
7182 	if (vsi->type == ICE_VSI_PF) {
7183 		/* Notify the stack of the actual queue counts. */
7184 		err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
7185 		if (err)
7186 			goto err_set_qs;
7187 
7188 		err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
7189 		if (err)
7190 			goto err_set_qs;
7191 	}
7192 
7193 	err = ice_up_complete(vsi);
7194 	if (err)
7195 		goto err_up_complete;
7196 
7197 	return 0;
7198 
7199 err_up_complete:
7200 	ice_down(vsi);
7201 err_set_qs:
7202 	ice_vsi_free_irq(vsi);
7203 err_setup_rx:
7204 	ice_vsi_free_rx_rings(vsi);
7205 err_setup_tx:
7206 	ice_vsi_free_tx_rings(vsi);
7207 
7208 	return err;
7209 }
7210 
7211 /**
7212  * ice_vsi_release_all - Delete all VSIs
7213  * @pf: PF from which all VSIs are being removed
7214  */
ice_vsi_release_all(struct ice_pf * pf)7215 static void ice_vsi_release_all(struct ice_pf *pf)
7216 {
7217 	int err, i;
7218 
7219 	if (!pf->vsi)
7220 		return;
7221 
7222 	ice_for_each_vsi(pf, i) {
7223 		if (!pf->vsi[i])
7224 			continue;
7225 
7226 		if (pf->vsi[i]->type == ICE_VSI_CHNL)
7227 			continue;
7228 
7229 		err = ice_vsi_release(pf->vsi[i]);
7230 		if (err)
7231 			dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
7232 				i, err, pf->vsi[i]->vsi_num);
7233 	}
7234 }
7235 
7236 /**
7237  * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
7238  * @pf: pointer to the PF instance
7239  * @type: VSI type to rebuild
7240  *
7241  * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
7242  */
ice_vsi_rebuild_by_type(struct ice_pf * pf,enum ice_vsi_type type)7243 static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
7244 {
7245 	struct device *dev = ice_pf_to_dev(pf);
7246 	int i, err;
7247 
7248 	ice_for_each_vsi(pf, i) {
7249 		struct ice_vsi *vsi = pf->vsi[i];
7250 
7251 		if (!vsi || vsi->type != type)
7252 			continue;
7253 
7254 		/* rebuild the VSI */
7255 		err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
7256 		if (err) {
7257 			dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
7258 				err, vsi->idx, ice_vsi_type_str(type));
7259 			return err;
7260 		}
7261 
7262 		/* replay filters for the VSI */
7263 		err = ice_replay_vsi(&pf->hw, vsi->idx);
7264 		if (err) {
7265 			dev_err(dev, "replay VSI failed, error %d, VSI index %d, type %s\n",
7266 				err, vsi->idx, ice_vsi_type_str(type));
7267 			return err;
7268 		}
7269 
7270 		/* Re-map HW VSI number, using VSI handle that has been
7271 		 * previously validated in ice_replay_vsi() call above
7272 		 */
7273 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
7274 
7275 		/* enable the VSI */
7276 		err = ice_ena_vsi(vsi, false);
7277 		if (err) {
7278 			dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
7279 				err, vsi->idx, ice_vsi_type_str(type));
7280 			return err;
7281 		}
7282 
7283 		dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
7284 			 ice_vsi_type_str(type));
7285 	}
7286 
7287 	return 0;
7288 }
7289 
7290 /**
7291  * ice_update_pf_netdev_link - Update PF netdev link status
7292  * @pf: pointer to the PF instance
7293  */
ice_update_pf_netdev_link(struct ice_pf * pf)7294 static void ice_update_pf_netdev_link(struct ice_pf *pf)
7295 {
7296 	bool link_up;
7297 	int i;
7298 
7299 	ice_for_each_vsi(pf, i) {
7300 		struct ice_vsi *vsi = pf->vsi[i];
7301 
7302 		if (!vsi || vsi->type != ICE_VSI_PF)
7303 			return;
7304 
7305 		ice_get_link_status(pf->vsi[i]->port_info, &link_up);
7306 		if (link_up) {
7307 			netif_carrier_on(pf->vsi[i]->netdev);
7308 			netif_tx_wake_all_queues(pf->vsi[i]->netdev);
7309 		} else {
7310 			netif_carrier_off(pf->vsi[i]->netdev);
7311 			netif_tx_stop_all_queues(pf->vsi[i]->netdev);
7312 		}
7313 	}
7314 }
7315 
7316 /**
7317  * ice_rebuild - rebuild after reset
7318  * @pf: PF to rebuild
7319  * @reset_type: type of reset
7320  *
7321  * Do not rebuild VF VSI in this flow because that is already handled via
7322  * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
7323  * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
7324  * to reset/rebuild all the VF VSI twice.
7325  */
ice_rebuild(struct ice_pf * pf,enum ice_reset_req reset_type)7326 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
7327 {
7328 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
7329 	struct device *dev = ice_pf_to_dev(pf);
7330 	struct ice_hw *hw = &pf->hw;
7331 	bool dvm;
7332 	int err;
7333 
7334 	if (test_bit(ICE_DOWN, pf->state))
7335 		goto clear_recovery;
7336 
7337 	dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
7338 
7339 #define ICE_EMP_RESET_SLEEP_MS 5000
7340 	if (reset_type == ICE_RESET_EMPR) {
7341 		/* If an EMP reset has occurred, any previously pending flash
7342 		 * update will have completed. We no longer know whether or
7343 		 * not the NVM update EMP reset is restricted.
7344 		 */
7345 		pf->fw_emp_reset_disabled = false;
7346 
7347 		msleep(ICE_EMP_RESET_SLEEP_MS);
7348 	}
7349 
7350 	err = ice_init_all_ctrlq(hw);
7351 	if (err) {
7352 		dev_err(dev, "control queues init failed %d\n", err);
7353 		goto err_init_ctrlq;
7354 	}
7355 
7356 	/* if DDP was previously loaded successfully */
7357 	if (!ice_is_safe_mode(pf)) {
7358 		/* reload the SW DB of filter tables */
7359 		if (reset_type == ICE_RESET_PFR)
7360 			ice_fill_blk_tbls(hw);
7361 		else
7362 			/* Reload DDP Package after CORER/GLOBR reset */
7363 			ice_load_pkg(NULL, pf);
7364 	}
7365 
7366 	err = ice_clear_pf_cfg(hw);
7367 	if (err) {
7368 		dev_err(dev, "clear PF configuration failed %d\n", err);
7369 		goto err_init_ctrlq;
7370 	}
7371 
7372 	ice_clear_pxe_mode(hw);
7373 
7374 	err = ice_init_nvm(hw);
7375 	if (err) {
7376 		dev_err(dev, "ice_init_nvm failed %d\n", err);
7377 		goto err_init_ctrlq;
7378 	}
7379 
7380 	err = ice_get_caps(hw);
7381 	if (err) {
7382 		dev_err(dev, "ice_get_caps failed %d\n", err);
7383 		goto err_init_ctrlq;
7384 	}
7385 
7386 	err = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
7387 	if (err) {
7388 		dev_err(dev, "set_mac_cfg failed %d\n", err);
7389 		goto err_init_ctrlq;
7390 	}
7391 
7392 	dvm = ice_is_dvm_ena(hw);
7393 
7394 	err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
7395 	if (err)
7396 		goto err_init_ctrlq;
7397 
7398 	err = ice_sched_init_port(hw->port_info);
7399 	if (err)
7400 		goto err_sched_init_port;
7401 
7402 	/* start misc vector */
7403 	err = ice_req_irq_msix_misc(pf);
7404 	if (err) {
7405 		dev_err(dev, "misc vector setup failed: %d\n", err);
7406 		goto err_sched_init_port;
7407 	}
7408 
7409 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7410 		wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
7411 		if (!rd32(hw, PFQF_FD_SIZE)) {
7412 			u16 unused, guar, b_effort;
7413 
7414 			guar = hw->func_caps.fd_fltr_guar;
7415 			b_effort = hw->func_caps.fd_fltr_best_effort;
7416 
7417 			/* force guaranteed filter pool for PF */
7418 			ice_alloc_fd_guar_item(hw, &unused, guar);
7419 			/* force shared filter pool for PF */
7420 			ice_alloc_fd_shrd_item(hw, &unused, b_effort);
7421 		}
7422 	}
7423 
7424 	if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
7425 		ice_dcb_rebuild(pf);
7426 
7427 	/* If the PF previously had enabled PTP, PTP init needs to happen before
7428 	 * the VSI rebuild. If not, this causes the PTP link status events to
7429 	 * fail.
7430 	 */
7431 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
7432 		ice_ptp_reset(pf);
7433 
7434 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
7435 		ice_gnss_init(pf);
7436 
7437 	/* rebuild PF VSI */
7438 	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
7439 	if (err) {
7440 		dev_err(dev, "PF VSI rebuild failed: %d\n", err);
7441 		goto err_vsi_rebuild;
7442 	}
7443 
7444 	/* configure PTP timestamping after VSI rebuild */
7445 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
7446 		ice_ptp_cfg_timestamp(pf, false);
7447 
7448 	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_SWITCHDEV_CTRL);
7449 	if (err) {
7450 		dev_err(dev, "Switchdev CTRL VSI rebuild failed: %d\n", err);
7451 		goto err_vsi_rebuild;
7452 	}
7453 
7454 	if (reset_type == ICE_RESET_PFR) {
7455 		err = ice_rebuild_channels(pf);
7456 		if (err) {
7457 			dev_err(dev, "failed to rebuild and replay ADQ VSIs, err %d\n",
7458 				err);
7459 			goto err_vsi_rebuild;
7460 		}
7461 	}
7462 
7463 	/* If Flow Director is active */
7464 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7465 		err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
7466 		if (err) {
7467 			dev_err(dev, "control VSI rebuild failed: %d\n", err);
7468 			goto err_vsi_rebuild;
7469 		}
7470 
7471 		/* replay HW Flow Director recipes */
7472 		if (hw->fdir_prof)
7473 			ice_fdir_replay_flows(hw);
7474 
7475 		/* replay Flow Director filters */
7476 		ice_fdir_replay_fltrs(pf);
7477 
7478 		ice_rebuild_arfs(pf);
7479 	}
7480 
7481 	if (vsi && vsi->netdev)
7482 		netif_device_attach(vsi->netdev);
7483 
7484 	ice_update_pf_netdev_link(pf);
7485 
7486 	/* tell the firmware we are up */
7487 	err = ice_send_version(pf);
7488 	if (err) {
7489 		dev_err(dev, "Rebuild failed due to error sending driver version: %d\n",
7490 			err);
7491 		goto err_vsi_rebuild;
7492 	}
7493 
7494 	ice_replay_post(hw);
7495 
7496 	/* if we get here, reset flow is successful */
7497 	clear_bit(ICE_RESET_FAILED, pf->state);
7498 
7499 	ice_plug_aux_dev(pf);
7500 	if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG))
7501 		ice_lag_rebuild(pf);
7502 	return;
7503 
7504 err_vsi_rebuild:
7505 err_sched_init_port:
7506 	ice_sched_cleanup_all(hw);
7507 err_init_ctrlq:
7508 	ice_shutdown_all_ctrlq(hw);
7509 	set_bit(ICE_RESET_FAILED, pf->state);
7510 clear_recovery:
7511 	/* set this bit in PF state to control service task scheduling */
7512 	set_bit(ICE_NEEDS_RESTART, pf->state);
7513 	dev_err(dev, "Rebuild failed, unload and reload driver\n");
7514 }
7515 
7516 /**
7517  * ice_change_mtu - NDO callback to change the MTU
7518  * @netdev: network interface device structure
7519  * @new_mtu: new value for maximum frame size
7520  *
7521  * Returns 0 on success, negative on failure
7522  */
ice_change_mtu(struct net_device * netdev,int new_mtu)7523 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
7524 {
7525 	struct ice_netdev_priv *np = netdev_priv(netdev);
7526 	struct ice_vsi *vsi = np->vsi;
7527 	struct ice_pf *pf = vsi->back;
7528 	struct bpf_prog *prog;
7529 	u8 count = 0;
7530 	int err = 0;
7531 
7532 	if (new_mtu == (int)netdev->mtu) {
7533 		netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
7534 		return 0;
7535 	}
7536 
7537 	prog = vsi->xdp_prog;
7538 	if (prog && !prog->aux->xdp_has_frags) {
7539 		int frame_size = ice_max_xdp_frame_size(vsi);
7540 
7541 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
7542 			netdev_err(netdev, "max MTU for XDP usage is %d\n",
7543 				   frame_size - ICE_ETH_PKT_HDR_PAD);
7544 			return -EINVAL;
7545 		}
7546 	} else if (test_bit(ICE_FLAG_LEGACY_RX, pf->flags)) {
7547 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > ICE_MAX_FRAME_LEGACY_RX) {
7548 			netdev_err(netdev, "Too big MTU for legacy-rx; Max is %d\n",
7549 				   ICE_MAX_FRAME_LEGACY_RX - ICE_ETH_PKT_HDR_PAD);
7550 			return -EINVAL;
7551 		}
7552 	}
7553 
7554 	/* if a reset is in progress, wait for some time for it to complete */
7555 	do {
7556 		if (ice_is_reset_in_progress(pf->state)) {
7557 			count++;
7558 			usleep_range(1000, 2000);
7559 		} else {
7560 			break;
7561 		}
7562 
7563 	} while (count < 100);
7564 
7565 	if (count == 100) {
7566 		netdev_err(netdev, "can't change MTU. Device is busy\n");
7567 		return -EBUSY;
7568 	}
7569 
7570 	netdev->mtu = (unsigned int)new_mtu;
7571 	err = ice_down_up(vsi);
7572 	if (err)
7573 		return err;
7574 
7575 	netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
7576 	set_bit(ICE_FLAG_MTU_CHANGED, pf->flags);
7577 
7578 	return err;
7579 }
7580 
7581 /**
7582  * ice_eth_ioctl - Access the hwtstamp interface
7583  * @netdev: network interface device structure
7584  * @ifr: interface request data
7585  * @cmd: ioctl command
7586  */
ice_eth_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)7587 static int ice_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
7588 {
7589 	struct ice_netdev_priv *np = netdev_priv(netdev);
7590 	struct ice_pf *pf = np->vsi->back;
7591 
7592 	switch (cmd) {
7593 	case SIOCGHWTSTAMP:
7594 		return ice_ptp_get_ts_config(pf, ifr);
7595 	case SIOCSHWTSTAMP:
7596 		return ice_ptp_set_ts_config(pf, ifr);
7597 	default:
7598 		return -EOPNOTSUPP;
7599 	}
7600 }
7601 
7602 /**
7603  * ice_aq_str - convert AQ err code to a string
7604  * @aq_err: the AQ error code to convert
7605  */
ice_aq_str(enum ice_aq_err aq_err)7606 const char *ice_aq_str(enum ice_aq_err aq_err)
7607 {
7608 	switch (aq_err) {
7609 	case ICE_AQ_RC_OK:
7610 		return "OK";
7611 	case ICE_AQ_RC_EPERM:
7612 		return "ICE_AQ_RC_EPERM";
7613 	case ICE_AQ_RC_ENOENT:
7614 		return "ICE_AQ_RC_ENOENT";
7615 	case ICE_AQ_RC_ENOMEM:
7616 		return "ICE_AQ_RC_ENOMEM";
7617 	case ICE_AQ_RC_EBUSY:
7618 		return "ICE_AQ_RC_EBUSY";
7619 	case ICE_AQ_RC_EEXIST:
7620 		return "ICE_AQ_RC_EEXIST";
7621 	case ICE_AQ_RC_EINVAL:
7622 		return "ICE_AQ_RC_EINVAL";
7623 	case ICE_AQ_RC_ENOSPC:
7624 		return "ICE_AQ_RC_ENOSPC";
7625 	case ICE_AQ_RC_ENOSYS:
7626 		return "ICE_AQ_RC_ENOSYS";
7627 	case ICE_AQ_RC_EMODE:
7628 		return "ICE_AQ_RC_EMODE";
7629 	case ICE_AQ_RC_ENOSEC:
7630 		return "ICE_AQ_RC_ENOSEC";
7631 	case ICE_AQ_RC_EBADSIG:
7632 		return "ICE_AQ_RC_EBADSIG";
7633 	case ICE_AQ_RC_ESVN:
7634 		return "ICE_AQ_RC_ESVN";
7635 	case ICE_AQ_RC_EBADMAN:
7636 		return "ICE_AQ_RC_EBADMAN";
7637 	case ICE_AQ_RC_EBADBUF:
7638 		return "ICE_AQ_RC_EBADBUF";
7639 	}
7640 
7641 	return "ICE_AQ_RC_UNKNOWN";
7642 }
7643 
7644 /**
7645  * ice_set_rss_lut - Set RSS LUT
7646  * @vsi: Pointer to VSI structure
7647  * @lut: Lookup table
7648  * @lut_size: Lookup table size
7649  *
7650  * Returns 0 on success, negative on failure
7651  */
ice_set_rss_lut(struct ice_vsi * vsi,u8 * lut,u16 lut_size)7652 int ice_set_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7653 {
7654 	struct ice_aq_get_set_rss_lut_params params = {};
7655 	struct ice_hw *hw = &vsi->back->hw;
7656 	int status;
7657 
7658 	if (!lut)
7659 		return -EINVAL;
7660 
7661 	params.vsi_handle = vsi->idx;
7662 	params.lut_size = lut_size;
7663 	params.lut_type = vsi->rss_lut_type;
7664 	params.lut = lut;
7665 
7666 	status = ice_aq_set_rss_lut(hw, &params);
7667 	if (status)
7668 		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS lut, err %d aq_err %s\n",
7669 			status, ice_aq_str(hw->adminq.sq_last_status));
7670 
7671 	return status;
7672 }
7673 
7674 /**
7675  * ice_set_rss_key - Set RSS key
7676  * @vsi: Pointer to the VSI structure
7677  * @seed: RSS hash seed
7678  *
7679  * Returns 0 on success, negative on failure
7680  */
ice_set_rss_key(struct ice_vsi * vsi,u8 * seed)7681 int ice_set_rss_key(struct ice_vsi *vsi, u8 *seed)
7682 {
7683 	struct ice_hw *hw = &vsi->back->hw;
7684 	int status;
7685 
7686 	if (!seed)
7687 		return -EINVAL;
7688 
7689 	status = ice_aq_set_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7690 	if (status)
7691 		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS key, err %d aq_err %s\n",
7692 			status, ice_aq_str(hw->adminq.sq_last_status));
7693 
7694 	return status;
7695 }
7696 
7697 /**
7698  * ice_get_rss_lut - Get RSS LUT
7699  * @vsi: Pointer to VSI structure
7700  * @lut: Buffer to store the lookup table entries
7701  * @lut_size: Size of buffer to store the lookup table entries
7702  *
7703  * Returns 0 on success, negative on failure
7704  */
ice_get_rss_lut(struct ice_vsi * vsi,u8 * lut,u16 lut_size)7705 int ice_get_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7706 {
7707 	struct ice_aq_get_set_rss_lut_params params = {};
7708 	struct ice_hw *hw = &vsi->back->hw;
7709 	int status;
7710 
7711 	if (!lut)
7712 		return -EINVAL;
7713 
7714 	params.vsi_handle = vsi->idx;
7715 	params.lut_size = lut_size;
7716 	params.lut_type = vsi->rss_lut_type;
7717 	params.lut = lut;
7718 
7719 	status = ice_aq_get_rss_lut(hw, &params);
7720 	if (status)
7721 		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS lut, err %d aq_err %s\n",
7722 			status, ice_aq_str(hw->adminq.sq_last_status));
7723 
7724 	return status;
7725 }
7726 
7727 /**
7728  * ice_get_rss_key - Get RSS key
7729  * @vsi: Pointer to VSI structure
7730  * @seed: Buffer to store the key in
7731  *
7732  * Returns 0 on success, negative on failure
7733  */
ice_get_rss_key(struct ice_vsi * vsi,u8 * seed)7734 int ice_get_rss_key(struct ice_vsi *vsi, u8 *seed)
7735 {
7736 	struct ice_hw *hw = &vsi->back->hw;
7737 	int status;
7738 
7739 	if (!seed)
7740 		return -EINVAL;
7741 
7742 	status = ice_aq_get_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7743 	if (status)
7744 		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS key, err %d aq_err %s\n",
7745 			status, ice_aq_str(hw->adminq.sq_last_status));
7746 
7747 	return status;
7748 }
7749 
7750 /**
7751  * ice_bridge_getlink - Get the hardware bridge mode
7752  * @skb: skb buff
7753  * @pid: process ID
7754  * @seq: RTNL message seq
7755  * @dev: the netdev being configured
7756  * @filter_mask: filter mask passed in
7757  * @nlflags: netlink flags passed in
7758  *
7759  * Return the bridge mode (VEB/VEPA)
7760  */
7761 static int
ice_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 filter_mask,int nlflags)7762 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
7763 		   struct net_device *dev, u32 filter_mask, int nlflags)
7764 {
7765 	struct ice_netdev_priv *np = netdev_priv(dev);
7766 	struct ice_vsi *vsi = np->vsi;
7767 	struct ice_pf *pf = vsi->back;
7768 	u16 bmode;
7769 
7770 	bmode = pf->first_sw->bridge_mode;
7771 
7772 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
7773 				       filter_mask, NULL);
7774 }
7775 
7776 /**
7777  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
7778  * @vsi: Pointer to VSI structure
7779  * @bmode: Hardware bridge mode (VEB/VEPA)
7780  *
7781  * Returns 0 on success, negative on failure
7782  */
ice_vsi_update_bridge_mode(struct ice_vsi * vsi,u16 bmode)7783 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
7784 {
7785 	struct ice_aqc_vsi_props *vsi_props;
7786 	struct ice_hw *hw = &vsi->back->hw;
7787 	struct ice_vsi_ctx *ctxt;
7788 	int ret;
7789 
7790 	vsi_props = &vsi->info;
7791 
7792 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
7793 	if (!ctxt)
7794 		return -ENOMEM;
7795 
7796 	ctxt->info = vsi->info;
7797 
7798 	if (bmode == BRIDGE_MODE_VEB)
7799 		/* change from VEPA to VEB mode */
7800 		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7801 	else
7802 		/* change from VEB to VEPA mode */
7803 		ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7804 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
7805 
7806 	ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
7807 	if (ret) {
7808 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %d aq_err %s\n",
7809 			bmode, ret, ice_aq_str(hw->adminq.sq_last_status));
7810 		goto out;
7811 	}
7812 	/* Update sw flags for book keeping */
7813 	vsi_props->sw_flags = ctxt->info.sw_flags;
7814 
7815 out:
7816 	kfree(ctxt);
7817 	return ret;
7818 }
7819 
7820 /**
7821  * ice_bridge_setlink - Set the hardware bridge mode
7822  * @dev: the netdev being configured
7823  * @nlh: RTNL message
7824  * @flags: bridge setlink flags
7825  * @extack: netlink extended ack
7826  *
7827  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
7828  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
7829  * not already set for all VSIs connected to this switch. And also update the
7830  * unicast switch filter rules for the corresponding switch of the netdev.
7831  */
7832 static int
ice_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 __always_unused flags,struct netlink_ext_ack __always_unused * extack)7833 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
7834 		   u16 __always_unused flags,
7835 		   struct netlink_ext_ack __always_unused *extack)
7836 {
7837 	struct ice_netdev_priv *np = netdev_priv(dev);
7838 	struct ice_pf *pf = np->vsi->back;
7839 	struct nlattr *attr, *br_spec;
7840 	struct ice_hw *hw = &pf->hw;
7841 	struct ice_sw *pf_sw;
7842 	int rem, v, err = 0;
7843 
7844 	pf_sw = pf->first_sw;
7845 	/* find the attribute in the netlink message */
7846 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
7847 	if (!br_spec)
7848 		return -EINVAL;
7849 
7850 	nla_for_each_nested(attr, br_spec, rem) {
7851 		__u16 mode;
7852 
7853 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
7854 			continue;
7855 		mode = nla_get_u16(attr);
7856 		if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
7857 			return -EINVAL;
7858 		/* Continue  if bridge mode is not being flipped */
7859 		if (mode == pf_sw->bridge_mode)
7860 			continue;
7861 		/* Iterates through the PF VSI list and update the loopback
7862 		 * mode of the VSI
7863 		 */
7864 		ice_for_each_vsi(pf, v) {
7865 			if (!pf->vsi[v])
7866 				continue;
7867 			err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
7868 			if (err)
7869 				return err;
7870 		}
7871 
7872 		hw->evb_veb = (mode == BRIDGE_MODE_VEB);
7873 		/* Update the unicast switch filter rules for the corresponding
7874 		 * switch of the netdev
7875 		 */
7876 		err = ice_update_sw_rule_bridge_mode(hw);
7877 		if (err) {
7878 			netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %s\n",
7879 				   mode, err,
7880 				   ice_aq_str(hw->adminq.sq_last_status));
7881 			/* revert hw->evb_veb */
7882 			hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
7883 			return err;
7884 		}
7885 
7886 		pf_sw->bridge_mode = mode;
7887 	}
7888 
7889 	return 0;
7890 }
7891 
7892 /**
7893  * ice_tx_timeout - Respond to a Tx Hang
7894  * @netdev: network interface device structure
7895  * @txqueue: Tx queue
7896  */
ice_tx_timeout(struct net_device * netdev,unsigned int txqueue)7897 static void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
7898 {
7899 	struct ice_netdev_priv *np = netdev_priv(netdev);
7900 	struct ice_tx_ring *tx_ring = NULL;
7901 	struct ice_vsi *vsi = np->vsi;
7902 	struct ice_pf *pf = vsi->back;
7903 	u32 i;
7904 
7905 	pf->tx_timeout_count++;
7906 
7907 	/* Check if PFC is enabled for the TC to which the queue belongs
7908 	 * to. If yes then Tx timeout is not caused by a hung queue, no
7909 	 * need to reset and rebuild
7910 	 */
7911 	if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
7912 		dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
7913 			 txqueue);
7914 		return;
7915 	}
7916 
7917 	/* now that we have an index, find the tx_ring struct */
7918 	ice_for_each_txq(vsi, i)
7919 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
7920 			if (txqueue == vsi->tx_rings[i]->q_index) {
7921 				tx_ring = vsi->tx_rings[i];
7922 				break;
7923 			}
7924 
7925 	/* Reset recovery level if enough time has elapsed after last timeout.
7926 	 * Also ensure no new reset action happens before next timeout period.
7927 	 */
7928 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
7929 		pf->tx_timeout_recovery_level = 1;
7930 	else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
7931 				       netdev->watchdog_timeo)))
7932 		return;
7933 
7934 	if (tx_ring) {
7935 		struct ice_hw *hw = &pf->hw;
7936 		u32 head, val = 0;
7937 
7938 		head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])) &
7939 			QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
7940 		/* Read interrupt register */
7941 		val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
7942 
7943 		netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
7944 			    vsi->vsi_num, txqueue, tx_ring->next_to_clean,
7945 			    head, tx_ring->next_to_use, val);
7946 	}
7947 
7948 	pf->tx_timeout_last_recovery = jiffies;
7949 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
7950 		    pf->tx_timeout_recovery_level, txqueue);
7951 
7952 	switch (pf->tx_timeout_recovery_level) {
7953 	case 1:
7954 		set_bit(ICE_PFR_REQ, pf->state);
7955 		break;
7956 	case 2:
7957 		set_bit(ICE_CORER_REQ, pf->state);
7958 		break;
7959 	case 3:
7960 		set_bit(ICE_GLOBR_REQ, pf->state);
7961 		break;
7962 	default:
7963 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
7964 		set_bit(ICE_DOWN, pf->state);
7965 		set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
7966 		set_bit(ICE_SERVICE_DIS, pf->state);
7967 		break;
7968 	}
7969 
7970 	ice_service_task_schedule(pf);
7971 	pf->tx_timeout_recovery_level++;
7972 }
7973 
7974 /**
7975  * ice_setup_tc_cls_flower - flower classifier offloads
7976  * @np: net device to configure
7977  * @filter_dev: device on which filter is added
7978  * @cls_flower: offload data
7979  */
7980 static int
ice_setup_tc_cls_flower(struct ice_netdev_priv * np,struct net_device * filter_dev,struct flow_cls_offload * cls_flower)7981 ice_setup_tc_cls_flower(struct ice_netdev_priv *np,
7982 			struct net_device *filter_dev,
7983 			struct flow_cls_offload *cls_flower)
7984 {
7985 	struct ice_vsi *vsi = np->vsi;
7986 
7987 	if (cls_flower->common.chain_index)
7988 		return -EOPNOTSUPP;
7989 
7990 	switch (cls_flower->command) {
7991 	case FLOW_CLS_REPLACE:
7992 		return ice_add_cls_flower(filter_dev, vsi, cls_flower);
7993 	case FLOW_CLS_DESTROY:
7994 		return ice_del_cls_flower(vsi, cls_flower);
7995 	default:
7996 		return -EINVAL;
7997 	}
7998 }
7999 
8000 /**
8001  * ice_setup_tc_block_cb - callback handler registered for TC block
8002  * @type: TC SETUP type
8003  * @type_data: TC flower offload data that contains user input
8004  * @cb_priv: netdev private data
8005  */
8006 static int
ice_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)8007 ice_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
8008 {
8009 	struct ice_netdev_priv *np = cb_priv;
8010 
8011 	switch (type) {
8012 	case TC_SETUP_CLSFLOWER:
8013 		return ice_setup_tc_cls_flower(np, np->vsi->netdev,
8014 					       type_data);
8015 	default:
8016 		return -EOPNOTSUPP;
8017 	}
8018 }
8019 
8020 /**
8021  * ice_validate_mqprio_qopt - Validate TCF input parameters
8022  * @vsi: Pointer to VSI
8023  * @mqprio_qopt: input parameters for mqprio queue configuration
8024  *
8025  * This function validates MQPRIO params, such as qcount (power of 2 wherever
8026  * needed), and make sure user doesn't specify qcount and BW rate limit
8027  * for TCs, which are more than "num_tc"
8028  */
8029 static int
ice_validate_mqprio_qopt(struct ice_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)8030 ice_validate_mqprio_qopt(struct ice_vsi *vsi,
8031 			 struct tc_mqprio_qopt_offload *mqprio_qopt)
8032 {
8033 	int non_power_of_2_qcount = 0;
8034 	struct ice_pf *pf = vsi->back;
8035 	int max_rss_q_cnt = 0;
8036 	u64 sum_min_rate = 0;
8037 	struct device *dev;
8038 	int i, speed;
8039 	u8 num_tc;
8040 
8041 	if (vsi->type != ICE_VSI_PF)
8042 		return -EINVAL;
8043 
8044 	if (mqprio_qopt->qopt.offset[0] != 0 ||
8045 	    mqprio_qopt->qopt.num_tc < 1 ||
8046 	    mqprio_qopt->qopt.num_tc > ICE_CHNL_MAX_TC)
8047 		return -EINVAL;
8048 
8049 	dev = ice_pf_to_dev(pf);
8050 	vsi->ch_rss_size = 0;
8051 	num_tc = mqprio_qopt->qopt.num_tc;
8052 	speed = ice_get_link_speed_kbps(vsi);
8053 
8054 	for (i = 0; num_tc; i++) {
8055 		int qcount = mqprio_qopt->qopt.count[i];
8056 		u64 max_rate, min_rate, rem;
8057 
8058 		if (!qcount)
8059 			return -EINVAL;
8060 
8061 		if (is_power_of_2(qcount)) {
8062 			if (non_power_of_2_qcount &&
8063 			    qcount > non_power_of_2_qcount) {
8064 				dev_err(dev, "qcount[%d] cannot be greater than non power of 2 qcount[%d]\n",
8065 					qcount, non_power_of_2_qcount);
8066 				return -EINVAL;
8067 			}
8068 			if (qcount > max_rss_q_cnt)
8069 				max_rss_q_cnt = qcount;
8070 		} else {
8071 			if (non_power_of_2_qcount &&
8072 			    qcount != non_power_of_2_qcount) {
8073 				dev_err(dev, "Only one non power of 2 qcount allowed[%d,%d]\n",
8074 					qcount, non_power_of_2_qcount);
8075 				return -EINVAL;
8076 			}
8077 			if (qcount < max_rss_q_cnt) {
8078 				dev_err(dev, "non power of 2 qcount[%d] cannot be less than other qcount[%d]\n",
8079 					qcount, max_rss_q_cnt);
8080 				return -EINVAL;
8081 			}
8082 			max_rss_q_cnt = qcount;
8083 			non_power_of_2_qcount = qcount;
8084 		}
8085 
8086 		/* TC command takes input in K/N/Gbps or K/M/Gbit etc but
8087 		 * converts the bandwidth rate limit into Bytes/s when
8088 		 * passing it down to the driver. So convert input bandwidth
8089 		 * from Bytes/s to Kbps
8090 		 */
8091 		max_rate = mqprio_qopt->max_rate[i];
8092 		max_rate = div_u64(max_rate, ICE_BW_KBPS_DIVISOR);
8093 
8094 		/* min_rate is minimum guaranteed rate and it can't be zero */
8095 		min_rate = mqprio_qopt->min_rate[i];
8096 		min_rate = div_u64(min_rate, ICE_BW_KBPS_DIVISOR);
8097 		sum_min_rate += min_rate;
8098 
8099 		if (min_rate && min_rate < ICE_MIN_BW_LIMIT) {
8100 			dev_err(dev, "TC%d: min_rate(%llu Kbps) < %u Kbps\n", i,
8101 				min_rate, ICE_MIN_BW_LIMIT);
8102 			return -EINVAL;
8103 		}
8104 
8105 		if (max_rate && max_rate > speed) {
8106 			dev_err(dev, "TC%d: max_rate(%llu Kbps) > link speed of %u Kbps\n",
8107 				i, max_rate, speed);
8108 			return -EINVAL;
8109 		}
8110 
8111 		iter_div_u64_rem(min_rate, ICE_MIN_BW_LIMIT, &rem);
8112 		if (rem) {
8113 			dev_err(dev, "TC%d: Min Rate not multiple of %u Kbps",
8114 				i, ICE_MIN_BW_LIMIT);
8115 			return -EINVAL;
8116 		}
8117 
8118 		iter_div_u64_rem(max_rate, ICE_MIN_BW_LIMIT, &rem);
8119 		if (rem) {
8120 			dev_err(dev, "TC%d: Max Rate not multiple of %u Kbps",
8121 				i, ICE_MIN_BW_LIMIT);
8122 			return -EINVAL;
8123 		}
8124 
8125 		/* min_rate can't be more than max_rate, except when max_rate
8126 		 * is zero (implies max_rate sought is max line rate). In such
8127 		 * a case min_rate can be more than max.
8128 		 */
8129 		if (max_rate && min_rate > max_rate) {
8130 			dev_err(dev, "min_rate %llu Kbps can't be more than max_rate %llu Kbps\n",
8131 				min_rate, max_rate);
8132 			return -EINVAL;
8133 		}
8134 
8135 		if (i >= mqprio_qopt->qopt.num_tc - 1)
8136 			break;
8137 		if (mqprio_qopt->qopt.offset[i + 1] !=
8138 		    (mqprio_qopt->qopt.offset[i] + qcount))
8139 			return -EINVAL;
8140 	}
8141 	if (vsi->num_rxq <
8142 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8143 		return -EINVAL;
8144 	if (vsi->num_txq <
8145 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8146 		return -EINVAL;
8147 
8148 	if (sum_min_rate && sum_min_rate > (u64)speed) {
8149 		dev_err(dev, "Invalid min Tx rate(%llu) Kbps > speed (%u) Kbps specified\n",
8150 			sum_min_rate, speed);
8151 		return -EINVAL;
8152 	}
8153 
8154 	/* make sure vsi->ch_rss_size is set correctly based on TC's qcount */
8155 	vsi->ch_rss_size = max_rss_q_cnt;
8156 
8157 	return 0;
8158 }
8159 
8160 /**
8161  * ice_add_vsi_to_fdir - add a VSI to the flow director group for PF
8162  * @pf: ptr to PF device
8163  * @vsi: ptr to VSI
8164  */
ice_add_vsi_to_fdir(struct ice_pf * pf,struct ice_vsi * vsi)8165 static int ice_add_vsi_to_fdir(struct ice_pf *pf, struct ice_vsi *vsi)
8166 {
8167 	struct device *dev = ice_pf_to_dev(pf);
8168 	bool added = false;
8169 	struct ice_hw *hw;
8170 	int flow;
8171 
8172 	if (!(vsi->num_gfltr || vsi->num_bfltr))
8173 		return -EINVAL;
8174 
8175 	hw = &pf->hw;
8176 	for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++) {
8177 		struct ice_fd_hw_prof *prof;
8178 		int tun, status;
8179 		u64 entry_h;
8180 
8181 		if (!(hw->fdir_prof && hw->fdir_prof[flow] &&
8182 		      hw->fdir_prof[flow]->cnt))
8183 			continue;
8184 
8185 		for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
8186 			enum ice_flow_priority prio;
8187 			u64 prof_id;
8188 
8189 			/* add this VSI to FDir profile for this flow */
8190 			prio = ICE_FLOW_PRIO_NORMAL;
8191 			prof = hw->fdir_prof[flow];
8192 			prof_id = flow + tun * ICE_FLTR_PTYPE_MAX;
8193 			status = ice_flow_add_entry(hw, ICE_BLK_FD, prof_id,
8194 						    prof->vsi_h[0], vsi->idx,
8195 						    prio, prof->fdir_seg[tun],
8196 						    &entry_h);
8197 			if (status) {
8198 				dev_err(dev, "channel VSI idx %d, not able to add to group %d\n",
8199 					vsi->idx, flow);
8200 				continue;
8201 			}
8202 
8203 			prof->entry_h[prof->cnt][tun] = entry_h;
8204 		}
8205 
8206 		/* store VSI for filter replay and delete */
8207 		prof->vsi_h[prof->cnt] = vsi->idx;
8208 		prof->cnt++;
8209 
8210 		added = true;
8211 		dev_dbg(dev, "VSI idx %d added to fdir group %d\n", vsi->idx,
8212 			flow);
8213 	}
8214 
8215 	if (!added)
8216 		dev_dbg(dev, "VSI idx %d not added to fdir groups\n", vsi->idx);
8217 
8218 	return 0;
8219 }
8220 
8221 /**
8222  * ice_add_channel - add a channel by adding VSI
8223  * @pf: ptr to PF device
8224  * @sw_id: underlying HW switching element ID
8225  * @ch: ptr to channel structure
8226  *
8227  * Add a channel (VSI) using add_vsi and queue_map
8228  */
ice_add_channel(struct ice_pf * pf,u16 sw_id,struct ice_channel * ch)8229 static int ice_add_channel(struct ice_pf *pf, u16 sw_id, struct ice_channel *ch)
8230 {
8231 	struct device *dev = ice_pf_to_dev(pf);
8232 	struct ice_vsi *vsi;
8233 
8234 	if (ch->type != ICE_VSI_CHNL) {
8235 		dev_err(dev, "add new VSI failed, ch->type %d\n", ch->type);
8236 		return -EINVAL;
8237 	}
8238 
8239 	vsi = ice_chnl_vsi_setup(pf, pf->hw.port_info, ch);
8240 	if (!vsi || vsi->type != ICE_VSI_CHNL) {
8241 		dev_err(dev, "create chnl VSI failure\n");
8242 		return -EINVAL;
8243 	}
8244 
8245 	ice_add_vsi_to_fdir(pf, vsi);
8246 
8247 	ch->sw_id = sw_id;
8248 	ch->vsi_num = vsi->vsi_num;
8249 	ch->info.mapping_flags = vsi->info.mapping_flags;
8250 	ch->ch_vsi = vsi;
8251 	/* set the back pointer of channel for newly created VSI */
8252 	vsi->ch = ch;
8253 
8254 	memcpy(&ch->info.q_mapping, &vsi->info.q_mapping,
8255 	       sizeof(vsi->info.q_mapping));
8256 	memcpy(&ch->info.tc_mapping, vsi->info.tc_mapping,
8257 	       sizeof(vsi->info.tc_mapping));
8258 
8259 	return 0;
8260 }
8261 
8262 /**
8263  * ice_chnl_cfg_res
8264  * @vsi: the VSI being setup
8265  * @ch: ptr to channel structure
8266  *
8267  * Configure channel specific resources such as rings, vector.
8268  */
ice_chnl_cfg_res(struct ice_vsi * vsi,struct ice_channel * ch)8269 static void ice_chnl_cfg_res(struct ice_vsi *vsi, struct ice_channel *ch)
8270 {
8271 	int i;
8272 
8273 	for (i = 0; i < ch->num_txq; i++) {
8274 		struct ice_q_vector *tx_q_vector, *rx_q_vector;
8275 		struct ice_ring_container *rc;
8276 		struct ice_tx_ring *tx_ring;
8277 		struct ice_rx_ring *rx_ring;
8278 
8279 		tx_ring = vsi->tx_rings[ch->base_q + i];
8280 		rx_ring = vsi->rx_rings[ch->base_q + i];
8281 		if (!tx_ring || !rx_ring)
8282 			continue;
8283 
8284 		/* setup ring being channel enabled */
8285 		tx_ring->ch = ch;
8286 		rx_ring->ch = ch;
8287 
8288 		/* following code block sets up vector specific attributes */
8289 		tx_q_vector = tx_ring->q_vector;
8290 		rx_q_vector = rx_ring->q_vector;
8291 		if (!tx_q_vector && !rx_q_vector)
8292 			continue;
8293 
8294 		if (tx_q_vector) {
8295 			tx_q_vector->ch = ch;
8296 			/* setup Tx and Rx ITR setting if DIM is off */
8297 			rc = &tx_q_vector->tx;
8298 			if (!ITR_IS_DYNAMIC(rc))
8299 				ice_write_itr(rc, rc->itr_setting);
8300 		}
8301 		if (rx_q_vector) {
8302 			rx_q_vector->ch = ch;
8303 			/* setup Tx and Rx ITR setting if DIM is off */
8304 			rc = &rx_q_vector->rx;
8305 			if (!ITR_IS_DYNAMIC(rc))
8306 				ice_write_itr(rc, rc->itr_setting);
8307 		}
8308 	}
8309 
8310 	/* it is safe to assume that, if channel has non-zero num_t[r]xq, then
8311 	 * GLINT_ITR register would have written to perform in-context
8312 	 * update, hence perform flush
8313 	 */
8314 	if (ch->num_txq || ch->num_rxq)
8315 		ice_flush(&vsi->back->hw);
8316 }
8317 
8318 /**
8319  * ice_cfg_chnl_all_res - configure channel resources
8320  * @vsi: pte to main_vsi
8321  * @ch: ptr to channel structure
8322  *
8323  * This function configures channel specific resources such as flow-director
8324  * counter index, and other resources such as queues, vectors, ITR settings
8325  */
8326 static void
ice_cfg_chnl_all_res(struct ice_vsi * vsi,struct ice_channel * ch)8327 ice_cfg_chnl_all_res(struct ice_vsi *vsi, struct ice_channel *ch)
8328 {
8329 	/* configure channel (aka ADQ) resources such as queues, vectors,
8330 	 * ITR settings for channel specific vectors and anything else
8331 	 */
8332 	ice_chnl_cfg_res(vsi, ch);
8333 }
8334 
8335 /**
8336  * ice_setup_hw_channel - setup new channel
8337  * @pf: ptr to PF device
8338  * @vsi: the VSI being setup
8339  * @ch: ptr to channel structure
8340  * @sw_id: underlying HW switching element ID
8341  * @type: type of channel to be created (VMDq2/VF)
8342  *
8343  * Setup new channel (VSI) based on specified type (VMDq2/VF)
8344  * and configures Tx rings accordingly
8345  */
8346 static int
ice_setup_hw_channel(struct ice_pf * pf,struct ice_vsi * vsi,struct ice_channel * ch,u16 sw_id,u8 type)8347 ice_setup_hw_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8348 		     struct ice_channel *ch, u16 sw_id, u8 type)
8349 {
8350 	struct device *dev = ice_pf_to_dev(pf);
8351 	int ret;
8352 
8353 	ch->base_q = vsi->next_base_q;
8354 	ch->type = type;
8355 
8356 	ret = ice_add_channel(pf, sw_id, ch);
8357 	if (ret) {
8358 		dev_err(dev, "failed to add_channel using sw_id %u\n", sw_id);
8359 		return ret;
8360 	}
8361 
8362 	/* configure/setup ADQ specific resources */
8363 	ice_cfg_chnl_all_res(vsi, ch);
8364 
8365 	/* make sure to update the next_base_q so that subsequent channel's
8366 	 * (aka ADQ) VSI queue map is correct
8367 	 */
8368 	vsi->next_base_q = vsi->next_base_q + ch->num_rxq;
8369 	dev_dbg(dev, "added channel: vsi_num %u, num_rxq %u\n", ch->vsi_num,
8370 		ch->num_rxq);
8371 
8372 	return 0;
8373 }
8374 
8375 /**
8376  * ice_setup_channel - setup new channel using uplink element
8377  * @pf: ptr to PF device
8378  * @vsi: the VSI being setup
8379  * @ch: ptr to channel structure
8380  *
8381  * Setup new channel (VSI) based on specified type (VMDq2/VF)
8382  * and uplink switching element
8383  */
8384 static bool
ice_setup_channel(struct ice_pf * pf,struct ice_vsi * vsi,struct ice_channel * ch)8385 ice_setup_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8386 		  struct ice_channel *ch)
8387 {
8388 	struct device *dev = ice_pf_to_dev(pf);
8389 	u16 sw_id;
8390 	int ret;
8391 
8392 	if (vsi->type != ICE_VSI_PF) {
8393 		dev_err(dev, "unsupported parent VSI type(%d)\n", vsi->type);
8394 		return false;
8395 	}
8396 
8397 	sw_id = pf->first_sw->sw_id;
8398 
8399 	/* create channel (VSI) */
8400 	ret = ice_setup_hw_channel(pf, vsi, ch, sw_id, ICE_VSI_CHNL);
8401 	if (ret) {
8402 		dev_err(dev, "failed to setup hw_channel\n");
8403 		return false;
8404 	}
8405 	dev_dbg(dev, "successfully created channel()\n");
8406 
8407 	return ch->ch_vsi ? true : false;
8408 }
8409 
8410 /**
8411  * ice_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
8412  * @vsi: VSI to be configured
8413  * @max_tx_rate: max Tx rate in Kbps to be configured as maximum BW limit
8414  * @min_tx_rate: min Tx rate in Kbps to be configured as minimum BW limit
8415  */
8416 static int
ice_set_bw_limit(struct ice_vsi * vsi,u64 max_tx_rate,u64 min_tx_rate)8417 ice_set_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate, u64 min_tx_rate)
8418 {
8419 	int err;
8420 
8421 	err = ice_set_min_bw_limit(vsi, min_tx_rate);
8422 	if (err)
8423 		return err;
8424 
8425 	return ice_set_max_bw_limit(vsi, max_tx_rate);
8426 }
8427 
8428 /**
8429  * ice_create_q_channel - function to create channel
8430  * @vsi: VSI to be configured
8431  * @ch: ptr to channel (it contains channel specific params)
8432  *
8433  * This function creates channel (VSI) using num_queues specified by user,
8434  * reconfigs RSS if needed.
8435  */
ice_create_q_channel(struct ice_vsi * vsi,struct ice_channel * ch)8436 static int ice_create_q_channel(struct ice_vsi *vsi, struct ice_channel *ch)
8437 {
8438 	struct ice_pf *pf = vsi->back;
8439 	struct device *dev;
8440 
8441 	if (!ch)
8442 		return -EINVAL;
8443 
8444 	dev = ice_pf_to_dev(pf);
8445 	if (!ch->num_txq || !ch->num_rxq) {
8446 		dev_err(dev, "Invalid num_queues requested: %d\n", ch->num_rxq);
8447 		return -EINVAL;
8448 	}
8449 
8450 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_txq) {
8451 		dev_err(dev, "cnt_q_avail (%u) less than num_queues %d\n",
8452 			vsi->cnt_q_avail, ch->num_txq);
8453 		return -EINVAL;
8454 	}
8455 
8456 	if (!ice_setup_channel(pf, vsi, ch)) {
8457 		dev_info(dev, "Failed to setup channel\n");
8458 		return -EINVAL;
8459 	}
8460 	/* configure BW rate limit */
8461 	if (ch->ch_vsi && (ch->max_tx_rate || ch->min_tx_rate)) {
8462 		int ret;
8463 
8464 		ret = ice_set_bw_limit(ch->ch_vsi, ch->max_tx_rate,
8465 				       ch->min_tx_rate);
8466 		if (ret)
8467 			dev_err(dev, "failed to set Tx rate of %llu Kbps for VSI(%u)\n",
8468 				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8469 		else
8470 			dev_dbg(dev, "set Tx rate of %llu Kbps for VSI(%u)\n",
8471 				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8472 	}
8473 
8474 	vsi->cnt_q_avail -= ch->num_txq;
8475 
8476 	return 0;
8477 }
8478 
8479 /**
8480  * ice_rem_all_chnl_fltrs - removes all channel filters
8481  * @pf: ptr to PF, TC-flower based filter are tracked at PF level
8482  *
8483  * Remove all advanced switch filters only if they are channel specific
8484  * tc-flower based filter
8485  */
ice_rem_all_chnl_fltrs(struct ice_pf * pf)8486 static void ice_rem_all_chnl_fltrs(struct ice_pf *pf)
8487 {
8488 	struct ice_tc_flower_fltr *fltr;
8489 	struct hlist_node *node;
8490 
8491 	/* to remove all channel filters, iterate an ordered list of filters */
8492 	hlist_for_each_entry_safe(fltr, node,
8493 				  &pf->tc_flower_fltr_list,
8494 				  tc_flower_node) {
8495 		struct ice_rule_query_data rule;
8496 		int status;
8497 
8498 		/* for now process only channel specific filters */
8499 		if (!ice_is_chnl_fltr(fltr))
8500 			continue;
8501 
8502 		rule.rid = fltr->rid;
8503 		rule.rule_id = fltr->rule_id;
8504 		rule.vsi_handle = fltr->dest_vsi_handle;
8505 		status = ice_rem_adv_rule_by_id(&pf->hw, &rule);
8506 		if (status) {
8507 			if (status == -ENOENT)
8508 				dev_dbg(ice_pf_to_dev(pf), "TC flower filter (rule_id %u) does not exist\n",
8509 					rule.rule_id);
8510 			else
8511 				dev_err(ice_pf_to_dev(pf), "failed to delete TC flower filter, status %d\n",
8512 					status);
8513 		} else if (fltr->dest_vsi) {
8514 			/* update advanced switch filter count */
8515 			if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
8516 				u32 flags = fltr->flags;
8517 
8518 				fltr->dest_vsi->num_chnl_fltr--;
8519 				if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
8520 					     ICE_TC_FLWR_FIELD_ENC_DST_MAC))
8521 					pf->num_dmac_chnl_fltrs--;
8522 			}
8523 		}
8524 
8525 		hlist_del(&fltr->tc_flower_node);
8526 		kfree(fltr);
8527 	}
8528 }
8529 
8530 /**
8531  * ice_remove_q_channels - Remove queue channels for the TCs
8532  * @vsi: VSI to be configured
8533  * @rem_fltr: delete advanced switch filter or not
8534  *
8535  * Remove queue channels for the TCs
8536  */
ice_remove_q_channels(struct ice_vsi * vsi,bool rem_fltr)8537 static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_fltr)
8538 {
8539 	struct ice_channel *ch, *ch_tmp;
8540 	struct ice_pf *pf = vsi->back;
8541 	int i;
8542 
8543 	/* remove all tc-flower based filter if they are channel filters only */
8544 	if (rem_fltr)
8545 		ice_rem_all_chnl_fltrs(pf);
8546 
8547 	/* remove ntuple filters since queue configuration is being changed */
8548 	if  (vsi->netdev->features & NETIF_F_NTUPLE) {
8549 		struct ice_hw *hw = &pf->hw;
8550 
8551 		mutex_lock(&hw->fdir_fltr_lock);
8552 		ice_fdir_del_all_fltrs(vsi);
8553 		mutex_unlock(&hw->fdir_fltr_lock);
8554 	}
8555 
8556 	/* perform cleanup for channels if they exist */
8557 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
8558 		struct ice_vsi *ch_vsi;
8559 
8560 		list_del(&ch->list);
8561 		ch_vsi = ch->ch_vsi;
8562 		if (!ch_vsi) {
8563 			kfree(ch);
8564 			continue;
8565 		}
8566 
8567 		/* Reset queue contexts */
8568 		for (i = 0; i < ch->num_rxq; i++) {
8569 			struct ice_tx_ring *tx_ring;
8570 			struct ice_rx_ring *rx_ring;
8571 
8572 			tx_ring = vsi->tx_rings[ch->base_q + i];
8573 			rx_ring = vsi->rx_rings[ch->base_q + i];
8574 			if (tx_ring) {
8575 				tx_ring->ch = NULL;
8576 				if (tx_ring->q_vector)
8577 					tx_ring->q_vector->ch = NULL;
8578 			}
8579 			if (rx_ring) {
8580 				rx_ring->ch = NULL;
8581 				if (rx_ring->q_vector)
8582 					rx_ring->q_vector->ch = NULL;
8583 			}
8584 		}
8585 
8586 		/* Release FD resources for the channel VSI */
8587 		ice_fdir_rem_adq_chnl(&pf->hw, ch->ch_vsi->idx);
8588 
8589 		/* clear the VSI from scheduler tree */
8590 		ice_rm_vsi_lan_cfg(ch->ch_vsi->port_info, ch->ch_vsi->idx);
8591 
8592 		/* Delete VSI from FW, PF and HW VSI arrays */
8593 		ice_vsi_delete(ch->ch_vsi);
8594 
8595 		/* free the channel */
8596 		kfree(ch);
8597 	}
8598 
8599 	/* clear the channel VSI map which is stored in main VSI */
8600 	ice_for_each_chnl_tc(i)
8601 		vsi->tc_map_vsi[i] = NULL;
8602 
8603 	/* reset main VSI's all TC information */
8604 	vsi->all_enatc = 0;
8605 	vsi->all_numtc = 0;
8606 }
8607 
8608 /**
8609  * ice_rebuild_channels - rebuild channel
8610  * @pf: ptr to PF
8611  *
8612  * Recreate channel VSIs and replay filters
8613  */
ice_rebuild_channels(struct ice_pf * pf)8614 static int ice_rebuild_channels(struct ice_pf *pf)
8615 {
8616 	struct device *dev = ice_pf_to_dev(pf);
8617 	struct ice_vsi *main_vsi;
8618 	bool rem_adv_fltr = true;
8619 	struct ice_channel *ch;
8620 	struct ice_vsi *vsi;
8621 	int tc_idx = 1;
8622 	int i, err;
8623 
8624 	main_vsi = ice_get_main_vsi(pf);
8625 	if (!main_vsi)
8626 		return 0;
8627 
8628 	if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) ||
8629 	    main_vsi->old_numtc == 1)
8630 		return 0; /* nothing to be done */
8631 
8632 	/* reconfigure main VSI based on old value of TC and cached values
8633 	 * for MQPRIO opts
8634 	 */
8635 	err = ice_vsi_cfg_tc(main_vsi, main_vsi->old_ena_tc);
8636 	if (err) {
8637 		dev_err(dev, "failed configuring TC(ena_tc:0x%02x) for HW VSI=%u\n",
8638 			main_vsi->old_ena_tc, main_vsi->vsi_num);
8639 		return err;
8640 	}
8641 
8642 	/* rebuild ADQ VSIs */
8643 	ice_for_each_vsi(pf, i) {
8644 		enum ice_vsi_type type;
8645 
8646 		vsi = pf->vsi[i];
8647 		if (!vsi || vsi->type != ICE_VSI_CHNL)
8648 			continue;
8649 
8650 		type = vsi->type;
8651 
8652 		/* rebuild ADQ VSI */
8653 		err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
8654 		if (err) {
8655 			dev_err(dev, "VSI (type:%s) at index %d rebuild failed, err %d\n",
8656 				ice_vsi_type_str(type), vsi->idx, err);
8657 			goto cleanup;
8658 		}
8659 
8660 		/* Re-map HW VSI number, using VSI handle that has been
8661 		 * previously validated in ice_replay_vsi() call above
8662 		 */
8663 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
8664 
8665 		/* replay filters for the VSI */
8666 		err = ice_replay_vsi(&pf->hw, vsi->idx);
8667 		if (err) {
8668 			dev_err(dev, "VSI (type:%s) replay failed, err %d, VSI index %d\n",
8669 				ice_vsi_type_str(type), err, vsi->idx);
8670 			rem_adv_fltr = false;
8671 			goto cleanup;
8672 		}
8673 		dev_info(dev, "VSI (type:%s) at index %d rebuilt successfully\n",
8674 			 ice_vsi_type_str(type), vsi->idx);
8675 
8676 		/* store ADQ VSI at correct TC index in main VSI's
8677 		 * map of TC to VSI
8678 		 */
8679 		main_vsi->tc_map_vsi[tc_idx++] = vsi;
8680 	}
8681 
8682 	/* ADQ VSI(s) has been rebuilt successfully, so setup
8683 	 * channel for main VSI's Tx and Rx rings
8684 	 */
8685 	list_for_each_entry(ch, &main_vsi->ch_list, list) {
8686 		struct ice_vsi *ch_vsi;
8687 
8688 		ch_vsi = ch->ch_vsi;
8689 		if (!ch_vsi)
8690 			continue;
8691 
8692 		/* reconfig channel resources */
8693 		ice_cfg_chnl_all_res(main_vsi, ch);
8694 
8695 		/* replay BW rate limit if it is non-zero */
8696 		if (!ch->max_tx_rate && !ch->min_tx_rate)
8697 			continue;
8698 
8699 		err = ice_set_bw_limit(ch_vsi, ch->max_tx_rate,
8700 				       ch->min_tx_rate);
8701 		if (err)
8702 			dev_err(dev, "failed (err:%d) to rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
8703 				err, ch->max_tx_rate, ch->min_tx_rate,
8704 				ch_vsi->vsi_num);
8705 		else
8706 			dev_dbg(dev, "successfully rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
8707 				ch->max_tx_rate, ch->min_tx_rate,
8708 				ch_vsi->vsi_num);
8709 	}
8710 
8711 	/* reconfig RSS for main VSI */
8712 	if (main_vsi->ch_rss_size)
8713 		ice_vsi_cfg_rss_lut_key(main_vsi);
8714 
8715 	return 0;
8716 
8717 cleanup:
8718 	ice_remove_q_channels(main_vsi, rem_adv_fltr);
8719 	return err;
8720 }
8721 
8722 /**
8723  * ice_create_q_channels - Add queue channel for the given TCs
8724  * @vsi: VSI to be configured
8725  *
8726  * Configures queue channel mapping to the given TCs
8727  */
ice_create_q_channels(struct ice_vsi * vsi)8728 static int ice_create_q_channels(struct ice_vsi *vsi)
8729 {
8730 	struct ice_pf *pf = vsi->back;
8731 	struct ice_channel *ch;
8732 	int ret = 0, i;
8733 
8734 	ice_for_each_chnl_tc(i) {
8735 		if (!(vsi->all_enatc & BIT(i)))
8736 			continue;
8737 
8738 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
8739 		if (!ch) {
8740 			ret = -ENOMEM;
8741 			goto err_free;
8742 		}
8743 		INIT_LIST_HEAD(&ch->list);
8744 		ch->num_rxq = vsi->mqprio_qopt.qopt.count[i];
8745 		ch->num_txq = vsi->mqprio_qopt.qopt.count[i];
8746 		ch->base_q = vsi->mqprio_qopt.qopt.offset[i];
8747 		ch->max_tx_rate = vsi->mqprio_qopt.max_rate[i];
8748 		ch->min_tx_rate = vsi->mqprio_qopt.min_rate[i];
8749 
8750 		/* convert to Kbits/s */
8751 		if (ch->max_tx_rate)
8752 			ch->max_tx_rate = div_u64(ch->max_tx_rate,
8753 						  ICE_BW_KBPS_DIVISOR);
8754 		if (ch->min_tx_rate)
8755 			ch->min_tx_rate = div_u64(ch->min_tx_rate,
8756 						  ICE_BW_KBPS_DIVISOR);
8757 
8758 		ret = ice_create_q_channel(vsi, ch);
8759 		if (ret) {
8760 			dev_err(ice_pf_to_dev(pf),
8761 				"failed creating channel TC:%d\n", i);
8762 			kfree(ch);
8763 			goto err_free;
8764 		}
8765 		list_add_tail(&ch->list, &vsi->ch_list);
8766 		vsi->tc_map_vsi[i] = ch->ch_vsi;
8767 		dev_dbg(ice_pf_to_dev(pf),
8768 			"successfully created channel: VSI %pK\n", ch->ch_vsi);
8769 	}
8770 	return 0;
8771 
8772 err_free:
8773 	ice_remove_q_channels(vsi, false);
8774 
8775 	return ret;
8776 }
8777 
8778 /**
8779  * ice_setup_tc_mqprio_qdisc - configure multiple traffic classes
8780  * @netdev: net device to configure
8781  * @type_data: TC offload data
8782  */
ice_setup_tc_mqprio_qdisc(struct net_device * netdev,void * type_data)8783 static int ice_setup_tc_mqprio_qdisc(struct net_device *netdev, void *type_data)
8784 {
8785 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8786 	struct ice_netdev_priv *np = netdev_priv(netdev);
8787 	struct ice_vsi *vsi = np->vsi;
8788 	struct ice_pf *pf = vsi->back;
8789 	u16 mode, ena_tc_qdisc = 0;
8790 	int cur_txq, cur_rxq;
8791 	u8 hw = 0, num_tcf;
8792 	struct device *dev;
8793 	int ret, i;
8794 
8795 	dev = ice_pf_to_dev(pf);
8796 	num_tcf = mqprio_qopt->qopt.num_tc;
8797 	hw = mqprio_qopt->qopt.hw;
8798 	mode = mqprio_qopt->mode;
8799 	if (!hw) {
8800 		clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8801 		vsi->ch_rss_size = 0;
8802 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8803 		goto config_tcf;
8804 	}
8805 
8806 	/* Generate queue region map for number of TCF requested */
8807 	for (i = 0; i < num_tcf; i++)
8808 		ena_tc_qdisc |= BIT(i);
8809 
8810 	switch (mode) {
8811 	case TC_MQPRIO_MODE_CHANNEL:
8812 
8813 		if (pf->hw.port_info->is_custom_tx_enabled) {
8814 			dev_err(dev, "Custom Tx scheduler feature enabled, can't configure ADQ\n");
8815 			return -EBUSY;
8816 		}
8817 		ice_tear_down_devlink_rate_tree(pf);
8818 
8819 		ret = ice_validate_mqprio_qopt(vsi, mqprio_qopt);
8820 		if (ret) {
8821 			netdev_err(netdev, "failed to validate_mqprio_qopt(), ret %d\n",
8822 				   ret);
8823 			return ret;
8824 		}
8825 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8826 		set_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8827 		/* don't assume state of hw_tc_offload during driver load
8828 		 * and set the flag for TC flower filter if hw_tc_offload
8829 		 * already ON
8830 		 */
8831 		if (vsi->netdev->features & NETIF_F_HW_TC)
8832 			set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
8833 		break;
8834 	default:
8835 		return -EINVAL;
8836 	}
8837 
8838 config_tcf:
8839 
8840 	/* Requesting same TCF configuration as already enabled */
8841 	if (ena_tc_qdisc == vsi->tc_cfg.ena_tc &&
8842 	    mode != TC_MQPRIO_MODE_CHANNEL)
8843 		return 0;
8844 
8845 	/* Pause VSI queues */
8846 	ice_dis_vsi(vsi, true);
8847 
8848 	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
8849 		ice_remove_q_channels(vsi, true);
8850 
8851 	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
8852 		vsi->req_txq = min_t(int, ice_get_avail_txq_count(pf),
8853 				     num_online_cpus());
8854 		vsi->req_rxq = min_t(int, ice_get_avail_rxq_count(pf),
8855 				     num_online_cpus());
8856 	} else {
8857 		/* logic to rebuild VSI, same like ethtool -L */
8858 		u16 offset = 0, qcount_tx = 0, qcount_rx = 0;
8859 
8860 		for (i = 0; i < num_tcf; i++) {
8861 			if (!(ena_tc_qdisc & BIT(i)))
8862 				continue;
8863 
8864 			offset = vsi->mqprio_qopt.qopt.offset[i];
8865 			qcount_rx = vsi->mqprio_qopt.qopt.count[i];
8866 			qcount_tx = vsi->mqprio_qopt.qopt.count[i];
8867 		}
8868 		vsi->req_txq = offset + qcount_tx;
8869 		vsi->req_rxq = offset + qcount_rx;
8870 
8871 		/* store away original rss_size info, so that it gets reused
8872 		 * form ice_vsi_rebuild during tc-qdisc delete stage - to
8873 		 * determine, what should be the rss_sizefor main VSI
8874 		 */
8875 		vsi->orig_rss_size = vsi->rss_size;
8876 	}
8877 
8878 	/* save current values of Tx and Rx queues before calling VSI rebuild
8879 	 * for fallback option
8880 	 */
8881 	cur_txq = vsi->num_txq;
8882 	cur_rxq = vsi->num_rxq;
8883 
8884 	/* proceed with rebuild main VSI using correct number of queues */
8885 	ret = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
8886 	if (ret) {
8887 		/* fallback to current number of queues */
8888 		dev_info(dev, "Rebuild failed with new queues, try with current number of queues\n");
8889 		vsi->req_txq = cur_txq;
8890 		vsi->req_rxq = cur_rxq;
8891 		clear_bit(ICE_RESET_FAILED, pf->state);
8892 		if (ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT)) {
8893 			dev_err(dev, "Rebuild of main VSI failed again\n");
8894 			return ret;
8895 		}
8896 	}
8897 
8898 	vsi->all_numtc = num_tcf;
8899 	vsi->all_enatc = ena_tc_qdisc;
8900 	ret = ice_vsi_cfg_tc(vsi, ena_tc_qdisc);
8901 	if (ret) {
8902 		netdev_err(netdev, "failed configuring TC for VSI id=%d\n",
8903 			   vsi->vsi_num);
8904 		goto exit;
8905 	}
8906 
8907 	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
8908 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8909 		u64 min_tx_rate = vsi->mqprio_qopt.min_rate[0];
8910 
8911 		/* set TC0 rate limit if specified */
8912 		if (max_tx_rate || min_tx_rate) {
8913 			/* convert to Kbits/s */
8914 			if (max_tx_rate)
8915 				max_tx_rate = div_u64(max_tx_rate, ICE_BW_KBPS_DIVISOR);
8916 			if (min_tx_rate)
8917 				min_tx_rate = div_u64(min_tx_rate, ICE_BW_KBPS_DIVISOR);
8918 
8919 			ret = ice_set_bw_limit(vsi, max_tx_rate, min_tx_rate);
8920 			if (!ret) {
8921 				dev_dbg(dev, "set Tx rate max %llu min %llu for VSI(%u)\n",
8922 					max_tx_rate, min_tx_rate, vsi->vsi_num);
8923 			} else {
8924 				dev_err(dev, "failed to set Tx rate max %llu min %llu for VSI(%u)\n",
8925 					max_tx_rate, min_tx_rate, vsi->vsi_num);
8926 				goto exit;
8927 			}
8928 		}
8929 		ret = ice_create_q_channels(vsi);
8930 		if (ret) {
8931 			netdev_err(netdev, "failed configuring queue channels\n");
8932 			goto exit;
8933 		} else {
8934 			netdev_dbg(netdev, "successfully configured channels\n");
8935 		}
8936 	}
8937 
8938 	if (vsi->ch_rss_size)
8939 		ice_vsi_cfg_rss_lut_key(vsi);
8940 
8941 exit:
8942 	/* if error, reset the all_numtc and all_enatc */
8943 	if (ret) {
8944 		vsi->all_numtc = 0;
8945 		vsi->all_enatc = 0;
8946 	}
8947 	/* resume VSI */
8948 	ice_ena_vsi(vsi, true);
8949 
8950 	return ret;
8951 }
8952 
8953 static LIST_HEAD(ice_block_cb_list);
8954 
8955 static int
ice_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)8956 ice_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8957 	     void *type_data)
8958 {
8959 	struct ice_netdev_priv *np = netdev_priv(netdev);
8960 	struct ice_pf *pf = np->vsi->back;
8961 	bool locked = false;
8962 	int err;
8963 
8964 	switch (type) {
8965 	case TC_SETUP_BLOCK:
8966 		return flow_block_cb_setup_simple(type_data,
8967 						  &ice_block_cb_list,
8968 						  ice_setup_tc_block_cb,
8969 						  np, np, true);
8970 	case TC_SETUP_QDISC_MQPRIO:
8971 		if (ice_is_eswitch_mode_switchdev(pf)) {
8972 			netdev_err(netdev, "TC MQPRIO offload not supported, switchdev is enabled\n");
8973 			return -EOPNOTSUPP;
8974 		}
8975 
8976 		if (pf->adev) {
8977 			mutex_lock(&pf->adev_mutex);
8978 			device_lock(&pf->adev->dev);
8979 			locked = true;
8980 			if (pf->adev->dev.driver) {
8981 				netdev_err(netdev, "Cannot change qdisc when RDMA is active\n");
8982 				err = -EBUSY;
8983 				goto adev_unlock;
8984 			}
8985 		}
8986 
8987 		/* setup traffic classifier for receive side */
8988 		mutex_lock(&pf->tc_mutex);
8989 		err = ice_setup_tc_mqprio_qdisc(netdev, type_data);
8990 		mutex_unlock(&pf->tc_mutex);
8991 
8992 adev_unlock:
8993 		if (locked) {
8994 			device_unlock(&pf->adev->dev);
8995 			mutex_unlock(&pf->adev_mutex);
8996 		}
8997 		return err;
8998 	default:
8999 		return -EOPNOTSUPP;
9000 	}
9001 	return -EOPNOTSUPP;
9002 }
9003 
9004 static struct ice_indr_block_priv *
ice_indr_block_priv_lookup(struct ice_netdev_priv * np,struct net_device * netdev)9005 ice_indr_block_priv_lookup(struct ice_netdev_priv *np,
9006 			   struct net_device *netdev)
9007 {
9008 	struct ice_indr_block_priv *cb_priv;
9009 
9010 	list_for_each_entry(cb_priv, &np->tc_indr_block_priv_list, list) {
9011 		if (!cb_priv->netdev)
9012 			return NULL;
9013 		if (cb_priv->netdev == netdev)
9014 			return cb_priv;
9015 	}
9016 	return NULL;
9017 }
9018 
9019 static int
ice_indr_setup_block_cb(enum tc_setup_type type,void * type_data,void * indr_priv)9020 ice_indr_setup_block_cb(enum tc_setup_type type, void *type_data,
9021 			void *indr_priv)
9022 {
9023 	struct ice_indr_block_priv *priv = indr_priv;
9024 	struct ice_netdev_priv *np = priv->np;
9025 
9026 	switch (type) {
9027 	case TC_SETUP_CLSFLOWER:
9028 		return ice_setup_tc_cls_flower(np, priv->netdev,
9029 					       (struct flow_cls_offload *)
9030 					       type_data);
9031 	default:
9032 		return -EOPNOTSUPP;
9033 	}
9034 }
9035 
9036 static int
ice_indr_setup_tc_block(struct net_device * netdev,struct Qdisc * sch,struct ice_netdev_priv * np,struct flow_block_offload * f,void * data,void (* cleanup)(struct flow_block_cb * block_cb))9037 ice_indr_setup_tc_block(struct net_device *netdev, struct Qdisc *sch,
9038 			struct ice_netdev_priv *np,
9039 			struct flow_block_offload *f, void *data,
9040 			void (*cleanup)(struct flow_block_cb *block_cb))
9041 {
9042 	struct ice_indr_block_priv *indr_priv;
9043 	struct flow_block_cb *block_cb;
9044 
9045 	if (!ice_is_tunnel_supported(netdev) &&
9046 	    !(is_vlan_dev(netdev) &&
9047 	      vlan_dev_real_dev(netdev) == np->vsi->netdev))
9048 		return -EOPNOTSUPP;
9049 
9050 	if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
9051 		return -EOPNOTSUPP;
9052 
9053 	switch (f->command) {
9054 	case FLOW_BLOCK_BIND:
9055 		indr_priv = ice_indr_block_priv_lookup(np, netdev);
9056 		if (indr_priv)
9057 			return -EEXIST;
9058 
9059 		indr_priv = kzalloc(sizeof(*indr_priv), GFP_KERNEL);
9060 		if (!indr_priv)
9061 			return -ENOMEM;
9062 
9063 		indr_priv->netdev = netdev;
9064 		indr_priv->np = np;
9065 		list_add(&indr_priv->list, &np->tc_indr_block_priv_list);
9066 
9067 		block_cb =
9068 			flow_indr_block_cb_alloc(ice_indr_setup_block_cb,
9069 						 indr_priv, indr_priv,
9070 						 ice_rep_indr_tc_block_unbind,
9071 						 f, netdev, sch, data, np,
9072 						 cleanup);
9073 
9074 		if (IS_ERR(block_cb)) {
9075 			list_del(&indr_priv->list);
9076 			kfree(indr_priv);
9077 			return PTR_ERR(block_cb);
9078 		}
9079 		flow_block_cb_add(block_cb, f);
9080 		list_add_tail(&block_cb->driver_list, &ice_block_cb_list);
9081 		break;
9082 	case FLOW_BLOCK_UNBIND:
9083 		indr_priv = ice_indr_block_priv_lookup(np, netdev);
9084 		if (!indr_priv)
9085 			return -ENOENT;
9086 
9087 		block_cb = flow_block_cb_lookup(f->block,
9088 						ice_indr_setup_block_cb,
9089 						indr_priv);
9090 		if (!block_cb)
9091 			return -ENOENT;
9092 
9093 		flow_indr_block_cb_remove(block_cb, f);
9094 
9095 		list_del(&block_cb->driver_list);
9096 		break;
9097 	default:
9098 		return -EOPNOTSUPP;
9099 	}
9100 	return 0;
9101 }
9102 
9103 static int
ice_indr_setup_tc_cb(struct net_device * netdev,struct Qdisc * sch,void * cb_priv,enum tc_setup_type type,void * type_data,void * data,void (* cleanup)(struct flow_block_cb * block_cb))9104 ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
9105 		     void *cb_priv, enum tc_setup_type type, void *type_data,
9106 		     void *data,
9107 		     void (*cleanup)(struct flow_block_cb *block_cb))
9108 {
9109 	switch (type) {
9110 	case TC_SETUP_BLOCK:
9111 		return ice_indr_setup_tc_block(netdev, sch, cb_priv, type_data,
9112 					       data, cleanup);
9113 
9114 	default:
9115 		return -EOPNOTSUPP;
9116 	}
9117 }
9118 
9119 /**
9120  * ice_open - Called when a network interface becomes active
9121  * @netdev: network interface device structure
9122  *
9123  * The open entry point is called when a network interface is made
9124  * active by the system (IFF_UP). At this point all resources needed
9125  * for transmit and receive operations are allocated, the interrupt
9126  * handler is registered with the OS, the netdev watchdog is enabled,
9127  * and the stack is notified that the interface is ready.
9128  *
9129  * Returns 0 on success, negative value on failure
9130  */
ice_open(struct net_device * netdev)9131 int ice_open(struct net_device *netdev)
9132 {
9133 	struct ice_netdev_priv *np = netdev_priv(netdev);
9134 	struct ice_pf *pf = np->vsi->back;
9135 
9136 	if (ice_is_reset_in_progress(pf->state)) {
9137 		netdev_err(netdev, "can't open net device while reset is in progress");
9138 		return -EBUSY;
9139 	}
9140 
9141 	return ice_open_internal(netdev);
9142 }
9143 
9144 /**
9145  * ice_open_internal - Called when a network interface becomes active
9146  * @netdev: network interface device structure
9147  *
9148  * Internal ice_open implementation. Should not be used directly except for ice_open and reset
9149  * handling routine
9150  *
9151  * Returns 0 on success, negative value on failure
9152  */
ice_open_internal(struct net_device * netdev)9153 int ice_open_internal(struct net_device *netdev)
9154 {
9155 	struct ice_netdev_priv *np = netdev_priv(netdev);
9156 	struct ice_vsi *vsi = np->vsi;
9157 	struct ice_pf *pf = vsi->back;
9158 	struct ice_port_info *pi;
9159 	int err;
9160 
9161 	if (test_bit(ICE_NEEDS_RESTART, pf->state)) {
9162 		netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
9163 		return -EIO;
9164 	}
9165 
9166 	netif_carrier_off(netdev);
9167 
9168 	pi = vsi->port_info;
9169 	err = ice_update_link_info(pi);
9170 	if (err) {
9171 		netdev_err(netdev, "Failed to get link info, error %d\n", err);
9172 		return err;
9173 	}
9174 
9175 	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
9176 
9177 	/* Set PHY if there is media, otherwise, turn off PHY */
9178 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
9179 		clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9180 		if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state)) {
9181 			err = ice_init_phy_user_cfg(pi);
9182 			if (err) {
9183 				netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
9184 					   err);
9185 				return err;
9186 			}
9187 		}
9188 
9189 		err = ice_configure_phy(vsi);
9190 		if (err) {
9191 			netdev_err(netdev, "Failed to set physical link up, error %d\n",
9192 				   err);
9193 			return err;
9194 		}
9195 	} else {
9196 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9197 		ice_set_link(vsi, false);
9198 	}
9199 
9200 	err = ice_vsi_open(vsi);
9201 	if (err)
9202 		netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
9203 			   vsi->vsi_num, vsi->vsw->sw_id);
9204 
9205 	/* Update existing tunnels information */
9206 	udp_tunnel_get_rx_info(netdev);
9207 
9208 	return err;
9209 }
9210 
9211 /**
9212  * ice_stop - Disables a network interface
9213  * @netdev: network interface device structure
9214  *
9215  * The stop entry point is called when an interface is de-activated by the OS,
9216  * and the netdevice enters the DOWN state. The hardware is still under the
9217  * driver's control, but the netdev interface is disabled.
9218  *
9219  * Returns success only - not allowed to fail
9220  */
ice_stop(struct net_device * netdev)9221 int ice_stop(struct net_device *netdev)
9222 {
9223 	struct ice_netdev_priv *np = netdev_priv(netdev);
9224 	struct ice_vsi *vsi = np->vsi;
9225 	struct ice_pf *pf = vsi->back;
9226 
9227 	if (ice_is_reset_in_progress(pf->state)) {
9228 		netdev_err(netdev, "can't stop net device while reset is in progress");
9229 		return -EBUSY;
9230 	}
9231 
9232 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
9233 		int link_err = ice_force_phys_link_state(vsi, false);
9234 
9235 		if (link_err) {
9236 			if (link_err == -ENOMEDIUM)
9237 				netdev_info(vsi->netdev, "Skipping link reconfig - no media attached, VSI %d\n",
9238 					    vsi->vsi_num);
9239 			else
9240 				netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
9241 					   vsi->vsi_num, link_err);
9242 
9243 			ice_vsi_close(vsi);
9244 			return -EIO;
9245 		}
9246 	}
9247 
9248 	ice_vsi_close(vsi);
9249 
9250 	return 0;
9251 }
9252 
9253 /**
9254  * ice_features_check - Validate encapsulated packet conforms to limits
9255  * @skb: skb buffer
9256  * @netdev: This port's netdev
9257  * @features: Offload features that the stack believes apply
9258  */
9259 static netdev_features_t
ice_features_check(struct sk_buff * skb,struct net_device __always_unused * netdev,netdev_features_t features)9260 ice_features_check(struct sk_buff *skb,
9261 		   struct net_device __always_unused *netdev,
9262 		   netdev_features_t features)
9263 {
9264 	bool gso = skb_is_gso(skb);
9265 	size_t len;
9266 
9267 	/* No point in doing any of this if neither checksum nor GSO are
9268 	 * being requested for this frame. We can rule out both by just
9269 	 * checking for CHECKSUM_PARTIAL
9270 	 */
9271 	if (skb->ip_summed != CHECKSUM_PARTIAL)
9272 		return features;
9273 
9274 	/* We cannot support GSO if the MSS is going to be less than
9275 	 * 64 bytes. If it is then we need to drop support for GSO.
9276 	 */
9277 	if (gso && (skb_shinfo(skb)->gso_size < ICE_TXD_CTX_MIN_MSS))
9278 		features &= ~NETIF_F_GSO_MASK;
9279 
9280 	len = skb_network_offset(skb);
9281 	if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
9282 		goto out_rm_features;
9283 
9284 	len = skb_network_header_len(skb);
9285 	if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9286 		goto out_rm_features;
9287 
9288 	if (skb->encapsulation) {
9289 		/* this must work for VXLAN frames AND IPIP/SIT frames, and in
9290 		 * the case of IPIP frames, the transport header pointer is
9291 		 * after the inner header! So check to make sure that this
9292 		 * is a GRE or UDP_TUNNEL frame before doing that math.
9293 		 */
9294 		if (gso && (skb_shinfo(skb)->gso_type &
9295 			    (SKB_GSO_GRE | SKB_GSO_UDP_TUNNEL))) {
9296 			len = skb_inner_network_header(skb) -
9297 			      skb_transport_header(skb);
9298 			if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
9299 				goto out_rm_features;
9300 		}
9301 
9302 		len = skb_inner_network_header_len(skb);
9303 		if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9304 			goto out_rm_features;
9305 	}
9306 
9307 	return features;
9308 out_rm_features:
9309 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
9310 }
9311 
9312 static const struct net_device_ops ice_netdev_safe_mode_ops = {
9313 	.ndo_open = ice_open,
9314 	.ndo_stop = ice_stop,
9315 	.ndo_start_xmit = ice_start_xmit,
9316 	.ndo_set_mac_address = ice_set_mac_address,
9317 	.ndo_validate_addr = eth_validate_addr,
9318 	.ndo_change_mtu = ice_change_mtu,
9319 	.ndo_get_stats64 = ice_get_stats64,
9320 	.ndo_tx_timeout = ice_tx_timeout,
9321 	.ndo_bpf = ice_xdp_safe_mode,
9322 };
9323 
9324 static const struct net_device_ops ice_netdev_ops = {
9325 	.ndo_open = ice_open,
9326 	.ndo_stop = ice_stop,
9327 	.ndo_start_xmit = ice_start_xmit,
9328 	.ndo_select_queue = ice_select_queue,
9329 	.ndo_features_check = ice_features_check,
9330 	.ndo_fix_features = ice_fix_features,
9331 	.ndo_set_rx_mode = ice_set_rx_mode,
9332 	.ndo_set_mac_address = ice_set_mac_address,
9333 	.ndo_validate_addr = eth_validate_addr,
9334 	.ndo_change_mtu = ice_change_mtu,
9335 	.ndo_get_stats64 = ice_get_stats64,
9336 	.ndo_set_tx_maxrate = ice_set_tx_maxrate,
9337 	.ndo_eth_ioctl = ice_eth_ioctl,
9338 	.ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
9339 	.ndo_set_vf_mac = ice_set_vf_mac,
9340 	.ndo_get_vf_config = ice_get_vf_cfg,
9341 	.ndo_set_vf_trust = ice_set_vf_trust,
9342 	.ndo_set_vf_vlan = ice_set_vf_port_vlan,
9343 	.ndo_set_vf_link_state = ice_set_vf_link_state,
9344 	.ndo_get_vf_stats = ice_get_vf_stats,
9345 	.ndo_set_vf_rate = ice_set_vf_bw,
9346 	.ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
9347 	.ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
9348 	.ndo_setup_tc = ice_setup_tc,
9349 	.ndo_set_features = ice_set_features,
9350 	.ndo_bridge_getlink = ice_bridge_getlink,
9351 	.ndo_bridge_setlink = ice_bridge_setlink,
9352 	.ndo_fdb_add = ice_fdb_add,
9353 	.ndo_fdb_del = ice_fdb_del,
9354 #ifdef CONFIG_RFS_ACCEL
9355 	.ndo_rx_flow_steer = ice_rx_flow_steer,
9356 #endif
9357 	.ndo_tx_timeout = ice_tx_timeout,
9358 	.ndo_bpf = ice_xdp,
9359 	.ndo_xdp_xmit = ice_xdp_xmit,
9360 	.ndo_xsk_wakeup = ice_xsk_wakeup,
9361 };
9362