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