xref: /linux/drivers/net/ethernet/intel/ice/ice_main.c (revision d69eb204c255c35abd9e8cb621484e8074c75eaa)
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 	if (tx->init) {
3180 		ice_ptp_complete_tx_single_tstamp(tx);
3181 
3182 		idx = find_next_bit_wrap(tx->in_use, tx->len,
3183 					 tx->last_ll_ts_idx_read + 1);
3184 		if (idx != tx->len)
3185 			ice_ptp_req_tx_single_tstamp(tx, idx);
3186 	}
3187 	spin_unlock_irqrestore(&tx->lock, flags);
3188 
3189 	val = GLINT_DYN_CTL_INTENA_M | GLINT_DYN_CTL_CLEARPBA_M |
3190 	      (ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
3191 	pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
3192 	wr32(hw, GLINT_DYN_CTL(pf->ll_ts_irq.index + pf_intr_start_offset),
3193 	     val);
3194 
3195 	return IRQ_HANDLED;
3196 }
3197 
3198 /**
3199  * ice_misc_intr - misc interrupt handler
3200  * @irq: interrupt number
3201  * @data: pointer to a q_vector
3202  */
ice_misc_intr(int __always_unused irq,void * data)3203 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
3204 {
3205 	struct ice_pf *pf = (struct ice_pf *)data;
3206 	irqreturn_t ret = IRQ_HANDLED;
3207 	struct ice_hw *hw = &pf->hw;
3208 	struct device *dev;
3209 	u32 oicr, ena_mask;
3210 
3211 	dev = ice_pf_to_dev(pf);
3212 	set_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
3213 	set_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
3214 	set_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
3215 
3216 	oicr = rd32(hw, PFINT_OICR);
3217 	ena_mask = rd32(hw, PFINT_OICR_ENA);
3218 
3219 	if (oicr & PFINT_OICR_SWINT_M) {
3220 		ena_mask &= ~PFINT_OICR_SWINT_M;
3221 		pf->sw_int_count++;
3222 	}
3223 
3224 	if (oicr & PFINT_OICR_MAL_DETECT_M) {
3225 		ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
3226 		set_bit(ICE_MDD_EVENT_PENDING, pf->state);
3227 	}
3228 	if (oicr & PFINT_OICR_VFLR_M) {
3229 		/* disable any further VFLR event notifications */
3230 		if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
3231 			u32 reg = rd32(hw, PFINT_OICR_ENA);
3232 
3233 			reg &= ~PFINT_OICR_VFLR_M;
3234 			wr32(hw, PFINT_OICR_ENA, reg);
3235 		} else {
3236 			ena_mask &= ~PFINT_OICR_VFLR_M;
3237 			set_bit(ICE_VFLR_EVENT_PENDING, pf->state);
3238 		}
3239 	}
3240 
3241 	if (oicr & PFINT_OICR_GRST_M) {
3242 		u32 reset;
3243 
3244 		/* we have a reset warning */
3245 		ena_mask &= ~PFINT_OICR_GRST_M;
3246 		reset = FIELD_GET(GLGEN_RSTAT_RESET_TYPE_M,
3247 				  rd32(hw, GLGEN_RSTAT));
3248 
3249 		if (reset == ICE_RESET_CORER)
3250 			pf->corer_count++;
3251 		else if (reset == ICE_RESET_GLOBR)
3252 			pf->globr_count++;
3253 		else if (reset == ICE_RESET_EMPR)
3254 			pf->empr_count++;
3255 		else
3256 			dev_dbg(dev, "Invalid reset type %d\n", reset);
3257 
3258 		/* If a reset cycle isn't already in progress, we set a bit in
3259 		 * pf->state so that the service task can start a reset/rebuild.
3260 		 */
3261 		if (!test_and_set_bit(ICE_RESET_OICR_RECV, pf->state)) {
3262 			if (reset == ICE_RESET_CORER)
3263 				set_bit(ICE_CORER_RECV, pf->state);
3264 			else if (reset == ICE_RESET_GLOBR)
3265 				set_bit(ICE_GLOBR_RECV, pf->state);
3266 			else
3267 				set_bit(ICE_EMPR_RECV, pf->state);
3268 
3269 			/* There are couple of different bits at play here.
3270 			 * hw->reset_ongoing indicates whether the hardware is
3271 			 * in reset. This is set to true when a reset interrupt
3272 			 * is received and set back to false after the driver
3273 			 * has determined that the hardware is out of reset.
3274 			 *
3275 			 * ICE_RESET_OICR_RECV in pf->state indicates
3276 			 * that a post reset rebuild is required before the
3277 			 * driver is operational again. This is set above.
3278 			 *
3279 			 * As this is the start of the reset/rebuild cycle, set
3280 			 * both to indicate that.
3281 			 */
3282 			hw->reset_ongoing = true;
3283 		}
3284 	}
3285 
3286 	if (oicr & PFINT_OICR_TSYN_TX_M) {
3287 		ena_mask &= ~PFINT_OICR_TSYN_TX_M;
3288 
3289 		ret = ice_ptp_ts_irq(pf);
3290 	}
3291 
3292 	if (oicr & PFINT_OICR_TSYN_EVNT_M) {
3293 		u8 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3294 		u32 gltsyn_stat = rd32(hw, GLTSYN_STAT(tmr_idx));
3295 
3296 		ena_mask &= ~PFINT_OICR_TSYN_EVNT_M;
3297 
3298 		if (ice_pf_src_tmr_owned(pf)) {
3299 			/* Save EVENTs from GLTSYN register */
3300 			pf->ptp.ext_ts_irq |= gltsyn_stat &
3301 					      (GLTSYN_STAT_EVENT0_M |
3302 					       GLTSYN_STAT_EVENT1_M |
3303 					       GLTSYN_STAT_EVENT2_M);
3304 
3305 			ice_ptp_extts_event(pf);
3306 		}
3307 	}
3308 
3309 #define ICE_AUX_CRIT_ERR (PFINT_OICR_PE_CRITERR_M | PFINT_OICR_HMC_ERR_M | PFINT_OICR_PE_PUSH_M)
3310 	if (oicr & ICE_AUX_CRIT_ERR) {
3311 		pf->oicr_err_reg |= oicr;
3312 		set_bit(ICE_AUX_ERR_PENDING, pf->state);
3313 		ena_mask &= ~ICE_AUX_CRIT_ERR;
3314 	}
3315 
3316 	/* Report any remaining unexpected interrupts */
3317 	oicr &= ena_mask;
3318 	if (oicr) {
3319 		dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
3320 		/* If a critical error is pending there is no choice but to
3321 		 * reset the device.
3322 		 */
3323 		if (oicr & (PFINT_OICR_PCI_EXCEPTION_M |
3324 			    PFINT_OICR_ECC_ERR_M)) {
3325 			set_bit(ICE_PFR_REQ, pf->state);
3326 		}
3327 	}
3328 	ice_service_task_schedule(pf);
3329 	if (ret == IRQ_HANDLED)
3330 		ice_irq_dynamic_ena(hw, NULL, NULL);
3331 
3332 	return ret;
3333 }
3334 
3335 /**
3336  * ice_misc_intr_thread_fn - misc interrupt thread function
3337  * @irq: interrupt number
3338  * @data: pointer to a q_vector
3339  */
ice_misc_intr_thread_fn(int __always_unused irq,void * data)3340 static irqreturn_t ice_misc_intr_thread_fn(int __always_unused irq, void *data)
3341 {
3342 	struct ice_pf *pf = data;
3343 	struct ice_hw *hw;
3344 
3345 	hw = &pf->hw;
3346 
3347 	if (ice_is_reset_in_progress(pf->state))
3348 		goto skip_irq;
3349 
3350 	if (test_and_clear_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread)) {
3351 		/* Process outstanding Tx timestamps. If there is more work,
3352 		 * re-arm the interrupt to trigger again.
3353 		 */
3354 		if (ice_ptp_process_ts(pf) == ICE_TX_TSTAMP_WORK_PENDING) {
3355 			wr32(hw, PFINT_OICR, PFINT_OICR_TSYN_TX_M);
3356 			ice_flush(hw);
3357 		}
3358 	}
3359 
3360 skip_irq:
3361 	ice_irq_dynamic_ena(hw, NULL, NULL);
3362 
3363 	return IRQ_HANDLED;
3364 }
3365 
3366 /**
3367  * ice_dis_ctrlq_interrupts - disable control queue interrupts
3368  * @hw: pointer to HW structure
3369  */
ice_dis_ctrlq_interrupts(struct ice_hw * hw)3370 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
3371 {
3372 	/* disable Admin queue Interrupt causes */
3373 	wr32(hw, PFINT_FW_CTL,
3374 	     rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
3375 
3376 	/* disable Mailbox queue Interrupt causes */
3377 	wr32(hw, PFINT_MBX_CTL,
3378 	     rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
3379 
3380 	wr32(hw, PFINT_SB_CTL,
3381 	     rd32(hw, PFINT_SB_CTL) & ~PFINT_SB_CTL_CAUSE_ENA_M);
3382 
3383 	/* disable Control queue Interrupt causes */
3384 	wr32(hw, PFINT_OICR_CTL,
3385 	     rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
3386 
3387 	ice_flush(hw);
3388 }
3389 
3390 /**
3391  * ice_free_irq_msix_ll_ts- Unroll ll_ts vector setup
3392  * @pf: board private structure
3393  */
ice_free_irq_msix_ll_ts(struct ice_pf * pf)3394 static void ice_free_irq_msix_ll_ts(struct ice_pf *pf)
3395 {
3396 	int irq_num = pf->ll_ts_irq.virq;
3397 
3398 	synchronize_irq(irq_num);
3399 	devm_free_irq(ice_pf_to_dev(pf), irq_num, pf);
3400 
3401 	ice_free_irq(pf, pf->ll_ts_irq);
3402 }
3403 
3404 /**
3405  * ice_free_irq_msix_misc - Unroll misc vector setup
3406  * @pf: board private structure
3407  */
ice_free_irq_msix_misc(struct ice_pf * pf)3408 static void ice_free_irq_msix_misc(struct ice_pf *pf)
3409 {
3410 	int misc_irq_num = pf->oicr_irq.virq;
3411 	struct ice_hw *hw = &pf->hw;
3412 
3413 	ice_dis_ctrlq_interrupts(hw);
3414 
3415 	/* disable OICR interrupt */
3416 	wr32(hw, PFINT_OICR_ENA, 0);
3417 	ice_flush(hw);
3418 
3419 	synchronize_irq(misc_irq_num);
3420 	devm_free_irq(ice_pf_to_dev(pf), misc_irq_num, pf);
3421 
3422 	ice_free_irq(pf, pf->oicr_irq);
3423 	if (pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3424 		ice_free_irq_msix_ll_ts(pf);
3425 }
3426 
3427 /**
3428  * ice_ena_ctrlq_interrupts - enable control queue interrupts
3429  * @hw: pointer to HW structure
3430  * @reg_idx: HW vector index to associate the control queue interrupts with
3431  */
ice_ena_ctrlq_interrupts(struct ice_hw * hw,u16 reg_idx)3432 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
3433 {
3434 	u32 val;
3435 
3436 	val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
3437 	       PFINT_OICR_CTL_CAUSE_ENA_M);
3438 	wr32(hw, PFINT_OICR_CTL, val);
3439 
3440 	/* enable Admin queue Interrupt causes */
3441 	val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
3442 	       PFINT_FW_CTL_CAUSE_ENA_M);
3443 	wr32(hw, PFINT_FW_CTL, val);
3444 
3445 	/* enable Mailbox queue Interrupt causes */
3446 	val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
3447 	       PFINT_MBX_CTL_CAUSE_ENA_M);
3448 	wr32(hw, PFINT_MBX_CTL, val);
3449 
3450 	if (!hw->dev_caps.ts_dev_info.ts_ll_int_read) {
3451 		/* enable Sideband queue Interrupt causes */
3452 		val = ((reg_idx & PFINT_SB_CTL_MSIX_INDX_M) |
3453 		       PFINT_SB_CTL_CAUSE_ENA_M);
3454 		wr32(hw, PFINT_SB_CTL, val);
3455 	}
3456 
3457 	ice_flush(hw);
3458 }
3459 
3460 /**
3461  * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
3462  * @pf: board private structure
3463  *
3464  * This sets up the handler for MSIX 0, which is used to manage the
3465  * non-queue interrupts, e.g. AdminQ and errors. This is not used
3466  * when in MSI or Legacy interrupt mode.
3467  */
ice_req_irq_msix_misc(struct ice_pf * pf)3468 static int ice_req_irq_msix_misc(struct ice_pf *pf)
3469 {
3470 	struct device *dev = ice_pf_to_dev(pf);
3471 	struct ice_hw *hw = &pf->hw;
3472 	u32 pf_intr_start_offset;
3473 	struct msi_map irq;
3474 	int err = 0;
3475 
3476 	if (!pf->int_name[0])
3477 		snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
3478 			 dev_driver_string(dev), dev_name(dev));
3479 
3480 	if (!pf->int_name_ll_ts[0])
3481 		snprintf(pf->int_name_ll_ts, sizeof(pf->int_name_ll_ts) - 1,
3482 			 "%s-%s:ll_ts", dev_driver_string(dev), dev_name(dev));
3483 	/* Do not request IRQ but do enable OICR interrupt since settings are
3484 	 * lost during reset. Note that this function is called only during
3485 	 * rebuild path and not while reset is in progress.
3486 	 */
3487 	if (ice_is_reset_in_progress(pf->state))
3488 		goto skip_req_irq;
3489 
3490 	/* reserve one vector in irq_tracker for misc interrupts */
3491 	irq = ice_alloc_irq(pf, false);
3492 	if (irq.index < 0)
3493 		return irq.index;
3494 
3495 	pf->oicr_irq = irq;
3496 	err = devm_request_threaded_irq(dev, pf->oicr_irq.virq, ice_misc_intr,
3497 					ice_misc_intr_thread_fn, 0,
3498 					pf->int_name, pf);
3499 	if (err) {
3500 		dev_err(dev, "devm_request_threaded_irq for %s failed: %d\n",
3501 			pf->int_name, err);
3502 		ice_free_irq(pf, pf->oicr_irq);
3503 		return err;
3504 	}
3505 
3506 	/* reserve one vector in irq_tracker for ll_ts interrupt */
3507 	if (!pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3508 		goto skip_req_irq;
3509 
3510 	irq = ice_alloc_irq(pf, false);
3511 	if (irq.index < 0)
3512 		return irq.index;
3513 
3514 	pf->ll_ts_irq = irq;
3515 	err = devm_request_irq(dev, pf->ll_ts_irq.virq, ice_ll_ts_intr, 0,
3516 			       pf->int_name_ll_ts, pf);
3517 	if (err) {
3518 		dev_err(dev, "devm_request_irq for %s failed: %d\n",
3519 			pf->int_name_ll_ts, err);
3520 		ice_free_irq(pf, pf->ll_ts_irq);
3521 		return err;
3522 	}
3523 
3524 skip_req_irq:
3525 	ice_ena_misc_vector(pf);
3526 
3527 	ice_ena_ctrlq_interrupts(hw, pf->oicr_irq.index);
3528 	/* This enables LL TS interrupt */
3529 	pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
3530 	if (pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3531 		wr32(hw, PFINT_SB_CTL,
3532 		     ((pf->ll_ts_irq.index + pf_intr_start_offset) &
3533 		      PFINT_SB_CTL_MSIX_INDX_M) | PFINT_SB_CTL_CAUSE_ENA_M);
3534 	wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_irq.index),
3535 	     ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
3536 
3537 	ice_flush(hw);
3538 	ice_irq_dynamic_ena(hw, NULL, NULL);
3539 
3540 	return 0;
3541 }
3542 
3543 /**
3544  * ice_set_ops - set netdev and ethtools ops for the given netdev
3545  * @vsi: the VSI associated with the new netdev
3546  */
ice_set_ops(struct ice_vsi * vsi)3547 static void ice_set_ops(struct ice_vsi *vsi)
3548 {
3549 	struct net_device *netdev = vsi->netdev;
3550 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
3551 
3552 	if (ice_is_safe_mode(pf)) {
3553 		netdev->netdev_ops = &ice_netdev_safe_mode_ops;
3554 		ice_set_ethtool_safe_mode_ops(netdev);
3555 		return;
3556 	}
3557 
3558 	netdev->netdev_ops = &ice_netdev_ops;
3559 	netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
3560 	netdev->xdp_metadata_ops = &ice_xdp_md_ops;
3561 	ice_set_ethtool_ops(netdev);
3562 
3563 	if (vsi->type != ICE_VSI_PF)
3564 		return;
3565 
3566 	netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
3567 			       NETDEV_XDP_ACT_XSK_ZEROCOPY |
3568 			       NETDEV_XDP_ACT_RX_SG;
3569 	netdev->xdp_zc_max_segs = ICE_MAX_BUF_TXD;
3570 }
3571 
3572 /**
3573  * ice_set_netdev_features - set features for the given netdev
3574  * @netdev: netdev instance
3575  */
ice_set_netdev_features(struct net_device * netdev)3576 void ice_set_netdev_features(struct net_device *netdev)
3577 {
3578 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
3579 	bool is_dvm_ena = ice_is_dvm_ena(&pf->hw);
3580 	netdev_features_t csumo_features;
3581 	netdev_features_t vlano_features;
3582 	netdev_features_t dflt_features;
3583 	netdev_features_t tso_features;
3584 
3585 	if (ice_is_safe_mode(pf)) {
3586 		/* safe mode */
3587 		netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
3588 		netdev->hw_features = netdev->features;
3589 		return;
3590 	}
3591 
3592 	dflt_features = NETIF_F_SG	|
3593 			NETIF_F_HIGHDMA	|
3594 			NETIF_F_NTUPLE	|
3595 			NETIF_F_RXHASH;
3596 
3597 	csumo_features = NETIF_F_RXCSUM	  |
3598 			 NETIF_F_IP_CSUM  |
3599 			 NETIF_F_SCTP_CRC |
3600 			 NETIF_F_IPV6_CSUM;
3601 
3602 	vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
3603 			 NETIF_F_HW_VLAN_CTAG_TX     |
3604 			 NETIF_F_HW_VLAN_CTAG_RX;
3605 
3606 	/* Enable CTAG/STAG filtering by default in Double VLAN Mode (DVM) */
3607 	if (is_dvm_ena)
3608 		vlano_features |= NETIF_F_HW_VLAN_STAG_FILTER;
3609 
3610 	tso_features = NETIF_F_TSO			|
3611 		       NETIF_F_TSO_ECN			|
3612 		       NETIF_F_TSO6			|
3613 		       NETIF_F_GSO_GRE			|
3614 		       NETIF_F_GSO_UDP_TUNNEL		|
3615 		       NETIF_F_GSO_GRE_CSUM		|
3616 		       NETIF_F_GSO_UDP_TUNNEL_CSUM	|
3617 		       NETIF_F_GSO_PARTIAL		|
3618 		       NETIF_F_GSO_IPXIP4		|
3619 		       NETIF_F_GSO_IPXIP6		|
3620 		       NETIF_F_GSO_UDP_L4;
3621 
3622 	netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3623 					NETIF_F_GSO_GRE_CSUM;
3624 	/* set features that user can change */
3625 	netdev->hw_features = dflt_features | csumo_features |
3626 			      vlano_features | tso_features;
3627 
3628 	/* add support for HW_CSUM on packets with MPLS header */
3629 	netdev->mpls_features =  NETIF_F_HW_CSUM |
3630 				 NETIF_F_TSO     |
3631 				 NETIF_F_TSO6;
3632 
3633 	/* enable features */
3634 	netdev->features |= netdev->hw_features;
3635 
3636 	netdev->hw_features |= NETIF_F_HW_TC;
3637 	netdev->hw_features |= NETIF_F_LOOPBACK;
3638 
3639 	/* encap and VLAN devices inherit default, csumo and tso features */
3640 	netdev->hw_enc_features |= dflt_features | csumo_features |
3641 				   tso_features;
3642 	netdev->vlan_features |= dflt_features | csumo_features |
3643 				 tso_features;
3644 
3645 	/* advertise support but don't enable by default since only one type of
3646 	 * VLAN offload can be enabled at a time (i.e. CTAG or STAG). When one
3647 	 * type turns on the other has to be turned off. This is enforced by the
3648 	 * ice_fix_features() ndo callback.
3649 	 */
3650 	if (is_dvm_ena)
3651 		netdev->hw_features |= NETIF_F_HW_VLAN_STAG_RX |
3652 			NETIF_F_HW_VLAN_STAG_TX;
3653 
3654 	/* Leave CRC / FCS stripping enabled by default, but allow the value to
3655 	 * be changed at runtime
3656 	 */
3657 	netdev->hw_features |= NETIF_F_RXFCS;
3658 
3659 	/* Allow core to manage IRQs affinity */
3660 	netif_set_affinity_auto(netdev);
3661 
3662 	/* Mutual exclusivity for TSO and GCS is enforced by the set features
3663 	 * ndo callback.
3664 	 */
3665 	if (ice_is_feature_supported(pf, ICE_F_GCS))
3666 		netdev->hw_features |= NETIF_F_HW_CSUM;
3667 
3668 	netif_set_tso_max_size(netdev, ICE_MAX_TSO_SIZE);
3669 }
3670 
3671 /**
3672  * ice_fill_rss_lut - Fill the RSS lookup table with default values
3673  * @lut: Lookup table
3674  * @rss_table_size: Lookup table size
3675  * @rss_size: Range of queue number for hashing
3676  */
ice_fill_rss_lut(u8 * lut,u16 rss_table_size,u16 rss_size)3677 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
3678 {
3679 	u16 i;
3680 
3681 	for (i = 0; i < rss_table_size; i++)
3682 		lut[i] = i % rss_size;
3683 }
3684 
3685 /**
3686  * ice_pf_vsi_setup - Set up a PF VSI
3687  * @pf: board private structure
3688  * @pi: pointer to the port_info instance
3689  *
3690  * Returns pointer to the successfully allocated VSI software struct
3691  * on success, otherwise returns NULL on failure.
3692  */
3693 static struct ice_vsi *
ice_pf_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi)3694 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3695 {
3696 	struct ice_vsi_cfg_params params = {};
3697 
3698 	params.type = ICE_VSI_PF;
3699 	params.port_info = pi;
3700 	params.flags = ICE_VSI_FLAG_INIT;
3701 
3702 	return ice_vsi_setup(pf, &params);
3703 }
3704 
3705 static struct ice_vsi *
ice_chnl_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi,struct ice_channel * ch)3706 ice_chnl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
3707 		   struct ice_channel *ch)
3708 {
3709 	struct ice_vsi_cfg_params params = {};
3710 
3711 	params.type = ICE_VSI_CHNL;
3712 	params.port_info = pi;
3713 	params.ch = ch;
3714 	params.flags = ICE_VSI_FLAG_INIT;
3715 
3716 	return ice_vsi_setup(pf, &params);
3717 }
3718 
3719 /**
3720  * ice_ctrl_vsi_setup - Set up a control VSI
3721  * @pf: board private structure
3722  * @pi: pointer to the port_info instance
3723  *
3724  * Returns pointer to the successfully allocated VSI software struct
3725  * on success, otherwise returns NULL on failure.
3726  */
3727 static struct ice_vsi *
ice_ctrl_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi)3728 ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3729 {
3730 	struct ice_vsi_cfg_params params = {};
3731 
3732 	params.type = ICE_VSI_CTRL;
3733 	params.port_info = pi;
3734 	params.flags = ICE_VSI_FLAG_INIT;
3735 
3736 	return ice_vsi_setup(pf, &params);
3737 }
3738 
3739 /**
3740  * ice_lb_vsi_setup - Set up a loopback VSI
3741  * @pf: board private structure
3742  * @pi: pointer to the port_info instance
3743  *
3744  * Returns pointer to the successfully allocated VSI software struct
3745  * on success, otherwise returns NULL on failure.
3746  */
3747 struct ice_vsi *
ice_lb_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi)3748 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3749 {
3750 	struct ice_vsi_cfg_params params = {};
3751 
3752 	params.type = ICE_VSI_LB;
3753 	params.port_info = pi;
3754 	params.flags = ICE_VSI_FLAG_INIT;
3755 
3756 	return ice_vsi_setup(pf, &params);
3757 }
3758 
3759 /**
3760  * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
3761  * @netdev: network interface to be adjusted
3762  * @proto: VLAN TPID
3763  * @vid: VLAN ID to be added
3764  *
3765  * net_device_ops implementation for adding VLAN IDs
3766  */
ice_vlan_rx_add_vid(struct net_device * netdev,__be16 proto,u16 vid)3767 int ice_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
3768 {
3769 	struct ice_netdev_priv *np = netdev_priv(netdev);
3770 	struct ice_vsi_vlan_ops *vlan_ops;
3771 	struct ice_vsi *vsi = np->vsi;
3772 	struct ice_vlan vlan;
3773 	int ret;
3774 
3775 	/* VLAN 0 is added by default during load/reset */
3776 	if (!vid)
3777 		return 0;
3778 
3779 	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3780 		usleep_range(1000, 2000);
3781 
3782 	/* Add multicast promisc rule for the VLAN ID to be added if
3783 	 * all-multicast is currently enabled.
3784 	 */
3785 	if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3786 		ret = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3787 					       ICE_MCAST_VLAN_PROMISC_BITS,
3788 					       vid);
3789 		if (ret)
3790 			goto finish;
3791 	}
3792 
3793 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3794 
3795 	/* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
3796 	 * packets aren't pruned by the device's internal switch on Rx
3797 	 */
3798 	vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3799 	ret = vlan_ops->add_vlan(vsi, &vlan);
3800 	if (ret)
3801 		goto finish;
3802 
3803 	/* If all-multicast is currently enabled and this VLAN ID is only one
3804 	 * besides VLAN-0 we have to update look-up type of multicast promisc
3805 	 * rule for VLAN-0 from ICE_SW_LKUP_PROMISC to ICE_SW_LKUP_PROMISC_VLAN.
3806 	 */
3807 	if ((vsi->current_netdev_flags & IFF_ALLMULTI) &&
3808 	    ice_vsi_num_non_zero_vlans(vsi) == 1) {
3809 		ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3810 					   ICE_MCAST_PROMISC_BITS, 0);
3811 		ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3812 					 ICE_MCAST_VLAN_PROMISC_BITS, 0);
3813 	}
3814 
3815 finish:
3816 	clear_bit(ICE_CFG_BUSY, vsi->state);
3817 
3818 	return ret;
3819 }
3820 
3821 /**
3822  * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
3823  * @netdev: network interface to be adjusted
3824  * @proto: VLAN TPID
3825  * @vid: VLAN ID to be removed
3826  *
3827  * net_device_ops implementation for removing VLAN IDs
3828  */
ice_vlan_rx_kill_vid(struct net_device * netdev,__be16 proto,u16 vid)3829 int ice_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
3830 {
3831 	struct ice_netdev_priv *np = netdev_priv(netdev);
3832 	struct ice_vsi_vlan_ops *vlan_ops;
3833 	struct ice_vsi *vsi = np->vsi;
3834 	struct ice_vlan vlan;
3835 	int ret;
3836 
3837 	/* don't allow removal of VLAN 0 */
3838 	if (!vid)
3839 		return 0;
3840 
3841 	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3842 		usleep_range(1000, 2000);
3843 
3844 	ret = ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3845 				    ICE_MCAST_VLAN_PROMISC_BITS, vid);
3846 	if (ret) {
3847 		netdev_err(netdev, "Error clearing multicast promiscuous mode on VSI %i\n",
3848 			   vsi->vsi_num);
3849 		vsi->current_netdev_flags |= IFF_ALLMULTI;
3850 	}
3851 
3852 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3853 
3854 	/* Make sure VLAN delete is successful before updating VLAN
3855 	 * information
3856 	 */
3857 	vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3858 	ret = vlan_ops->del_vlan(vsi, &vlan);
3859 	if (ret)
3860 		goto finish;
3861 
3862 	/* Remove multicast promisc rule for the removed VLAN ID if
3863 	 * all-multicast is enabled.
3864 	 */
3865 	if (vsi->current_netdev_flags & IFF_ALLMULTI)
3866 		ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3867 					   ICE_MCAST_VLAN_PROMISC_BITS, vid);
3868 
3869 	if (!ice_vsi_has_non_zero_vlans(vsi)) {
3870 		/* Update look-up type of multicast promisc rule for VLAN 0
3871 		 * from ICE_SW_LKUP_PROMISC_VLAN to ICE_SW_LKUP_PROMISC when
3872 		 * all-multicast is enabled and VLAN 0 is the only VLAN rule.
3873 		 */
3874 		if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3875 			ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3876 						   ICE_MCAST_VLAN_PROMISC_BITS,
3877 						   0);
3878 			ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3879 						 ICE_MCAST_PROMISC_BITS, 0);
3880 		}
3881 	}
3882 
3883 finish:
3884 	clear_bit(ICE_CFG_BUSY, vsi->state);
3885 
3886 	return ret;
3887 }
3888 
3889 /**
3890  * ice_rep_indr_tc_block_unbind
3891  * @cb_priv: indirection block private data
3892  */
ice_rep_indr_tc_block_unbind(void * cb_priv)3893 static void ice_rep_indr_tc_block_unbind(void *cb_priv)
3894 {
3895 	struct ice_indr_block_priv *indr_priv = cb_priv;
3896 
3897 	list_del(&indr_priv->list);
3898 	kfree(indr_priv);
3899 }
3900 
3901 /**
3902  * ice_tc_indir_block_unregister - Unregister TC indirect block notifications
3903  * @vsi: VSI struct which has the netdev
3904  */
ice_tc_indir_block_unregister(struct ice_vsi * vsi)3905 static void ice_tc_indir_block_unregister(struct ice_vsi *vsi)
3906 {
3907 	struct ice_netdev_priv *np = netdev_priv(vsi->netdev);
3908 
3909 	flow_indr_dev_unregister(ice_indr_setup_tc_cb, np,
3910 				 ice_rep_indr_tc_block_unbind);
3911 }
3912 
3913 /**
3914  * ice_tc_indir_block_register - Register TC indirect block notifications
3915  * @vsi: VSI struct which has the netdev
3916  *
3917  * Returns 0 on success, negative value on failure
3918  */
ice_tc_indir_block_register(struct ice_vsi * vsi)3919 static int ice_tc_indir_block_register(struct ice_vsi *vsi)
3920 {
3921 	struct ice_netdev_priv *np;
3922 
3923 	if (!vsi || !vsi->netdev)
3924 		return -EINVAL;
3925 
3926 	np = netdev_priv(vsi->netdev);
3927 
3928 	INIT_LIST_HEAD(&np->tc_indr_block_priv_list);
3929 	return flow_indr_dev_register(ice_indr_setup_tc_cb, np);
3930 }
3931 
3932 /**
3933  * ice_get_avail_q_count - Get count of queues in use
3934  * @pf_qmap: bitmap to get queue use count from
3935  * @lock: pointer to a mutex that protects access to pf_qmap
3936  * @size: size of the bitmap
3937  */
3938 static u16
ice_get_avail_q_count(unsigned long * pf_qmap,struct mutex * lock,u16 size)3939 ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
3940 {
3941 	unsigned long bit;
3942 	u16 count = 0;
3943 
3944 	mutex_lock(lock);
3945 	for_each_clear_bit(bit, pf_qmap, size)
3946 		count++;
3947 	mutex_unlock(lock);
3948 
3949 	return count;
3950 }
3951 
3952 /**
3953  * ice_get_avail_txq_count - Get count of Tx queues in use
3954  * @pf: pointer to an ice_pf instance
3955  */
ice_get_avail_txq_count(struct ice_pf * pf)3956 u16 ice_get_avail_txq_count(struct ice_pf *pf)
3957 {
3958 	return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
3959 				     pf->max_pf_txqs);
3960 }
3961 
3962 /**
3963  * ice_get_avail_rxq_count - Get count of Rx queues in use
3964  * @pf: pointer to an ice_pf instance
3965  */
ice_get_avail_rxq_count(struct ice_pf * pf)3966 u16 ice_get_avail_rxq_count(struct ice_pf *pf)
3967 {
3968 	return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
3969 				     pf->max_pf_rxqs);
3970 }
3971 
3972 /**
3973  * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3974  * @pf: board private structure to initialize
3975  */
ice_deinit_pf(struct ice_pf * pf)3976 static void ice_deinit_pf(struct ice_pf *pf)
3977 {
3978 	ice_service_task_stop(pf);
3979 	mutex_destroy(&pf->lag_mutex);
3980 	mutex_destroy(&pf->adev_mutex);
3981 	mutex_destroy(&pf->sw_mutex);
3982 	mutex_destroy(&pf->tc_mutex);
3983 	mutex_destroy(&pf->avail_q_mutex);
3984 	mutex_destroy(&pf->vfs.table_lock);
3985 
3986 	if (pf->avail_txqs) {
3987 		bitmap_free(pf->avail_txqs);
3988 		pf->avail_txqs = NULL;
3989 	}
3990 
3991 	if (pf->avail_rxqs) {
3992 		bitmap_free(pf->avail_rxqs);
3993 		pf->avail_rxqs = NULL;
3994 	}
3995 
3996 	if (pf->ptp.clock)
3997 		ptp_clock_unregister(pf->ptp.clock);
3998 
3999 	xa_destroy(&pf->dyn_ports);
4000 	xa_destroy(&pf->sf_nums);
4001 }
4002 
4003 /**
4004  * ice_set_pf_caps - set PFs capability flags
4005  * @pf: pointer to the PF instance
4006  */
ice_set_pf_caps(struct ice_pf * pf)4007 static void ice_set_pf_caps(struct ice_pf *pf)
4008 {
4009 	struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
4010 
4011 	clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
4012 	if (func_caps->common_cap.rdma)
4013 		set_bit(ICE_FLAG_RDMA_ENA, pf->flags);
4014 	clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4015 	if (func_caps->common_cap.dcb)
4016 		set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4017 	clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
4018 	if (func_caps->common_cap.sr_iov_1_1) {
4019 		set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
4020 		pf->vfs.num_supported = min_t(int, func_caps->num_allocd_vfs,
4021 					      ICE_MAX_SRIOV_VFS);
4022 	}
4023 	clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
4024 	if (func_caps->common_cap.rss_table_size)
4025 		set_bit(ICE_FLAG_RSS_ENA, pf->flags);
4026 
4027 	clear_bit(ICE_FLAG_FD_ENA, pf->flags);
4028 	if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
4029 		u16 unused;
4030 
4031 		/* ctrl_vsi_idx will be set to a valid value when flow director
4032 		 * is setup by ice_init_fdir
4033 		 */
4034 		pf->ctrl_vsi_idx = ICE_NO_VSI;
4035 		set_bit(ICE_FLAG_FD_ENA, pf->flags);
4036 		/* force guaranteed filter pool for PF */
4037 		ice_alloc_fd_guar_item(&pf->hw, &unused,
4038 				       func_caps->fd_fltr_guar);
4039 		/* force shared filter pool for PF */
4040 		ice_alloc_fd_shrd_item(&pf->hw, &unused,
4041 				       func_caps->fd_fltr_best_effort);
4042 	}
4043 
4044 	clear_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
4045 	if (func_caps->common_cap.ieee_1588)
4046 		set_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
4047 
4048 	pf->max_pf_txqs = func_caps->common_cap.num_txq;
4049 	pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
4050 }
4051 
4052 /**
4053  * ice_init_pf - Initialize general software structures (struct ice_pf)
4054  * @pf: board private structure to initialize
4055  */
ice_init_pf(struct ice_pf * pf)4056 static int ice_init_pf(struct ice_pf *pf)
4057 {
4058 	ice_set_pf_caps(pf);
4059 
4060 	mutex_init(&pf->sw_mutex);
4061 	mutex_init(&pf->tc_mutex);
4062 	mutex_init(&pf->adev_mutex);
4063 	mutex_init(&pf->lag_mutex);
4064 
4065 	INIT_HLIST_HEAD(&pf->aq_wait_list);
4066 	spin_lock_init(&pf->aq_wait_lock);
4067 	init_waitqueue_head(&pf->aq_wait_queue);
4068 
4069 	init_waitqueue_head(&pf->reset_wait_queue);
4070 
4071 	/* setup service timer and periodic service task */
4072 	timer_setup(&pf->serv_tmr, ice_service_timer, 0);
4073 	pf->serv_tmr_period = HZ;
4074 	INIT_WORK(&pf->serv_task, ice_service_task);
4075 	clear_bit(ICE_SERVICE_SCHED, pf->state);
4076 
4077 	mutex_init(&pf->avail_q_mutex);
4078 	pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
4079 	if (!pf->avail_txqs)
4080 		return -ENOMEM;
4081 
4082 	pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
4083 	if (!pf->avail_rxqs) {
4084 		bitmap_free(pf->avail_txqs);
4085 		pf->avail_txqs = NULL;
4086 		return -ENOMEM;
4087 	}
4088 
4089 	mutex_init(&pf->vfs.table_lock);
4090 	hash_init(pf->vfs.table);
4091 	if (ice_is_feature_supported(pf, ICE_F_MBX_LIMIT))
4092 		wr32(&pf->hw, E830_MBX_PF_IN_FLIGHT_VF_MSGS_THRESH,
4093 		     ICE_MBX_OVERFLOW_WATERMARK);
4094 	else
4095 		ice_mbx_init_snapshot(&pf->hw);
4096 
4097 	xa_init(&pf->dyn_ports);
4098 	xa_init(&pf->sf_nums);
4099 
4100 	return 0;
4101 }
4102 
4103 /**
4104  * ice_is_wol_supported - check if WoL is supported
4105  * @hw: pointer to hardware info
4106  *
4107  * Check if WoL is supported based on the HW configuration.
4108  * Returns true if NVM supports and enables WoL for this port, false otherwise
4109  */
ice_is_wol_supported(struct ice_hw * hw)4110 bool ice_is_wol_supported(struct ice_hw *hw)
4111 {
4112 	u16 wol_ctrl;
4113 
4114 	/* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
4115 	 * word) indicates WoL is not supported on the corresponding PF ID.
4116 	 */
4117 	if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
4118 		return false;
4119 
4120 	return !(BIT(hw->port_info->lport) & wol_ctrl);
4121 }
4122 
4123 /**
4124  * ice_vsi_recfg_qs - Change the number of queues on a VSI
4125  * @vsi: VSI being changed
4126  * @new_rx: new number of Rx queues
4127  * @new_tx: new number of Tx queues
4128  * @locked: is adev device_lock held
4129  *
4130  * Only change the number of queues if new_tx, or new_rx is non-0.
4131  *
4132  * Returns 0 on success.
4133  */
ice_vsi_recfg_qs(struct ice_vsi * vsi,int new_rx,int new_tx,bool locked)4134 int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx, bool locked)
4135 {
4136 	struct ice_pf *pf = vsi->back;
4137 	int i, err = 0, timeout = 50;
4138 
4139 	if (!new_rx && !new_tx)
4140 		return -EINVAL;
4141 
4142 	while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
4143 		timeout--;
4144 		if (!timeout)
4145 			return -EBUSY;
4146 		usleep_range(1000, 2000);
4147 	}
4148 
4149 	if (new_tx)
4150 		vsi->req_txq = (u16)new_tx;
4151 	if (new_rx)
4152 		vsi->req_rxq = (u16)new_rx;
4153 
4154 	/* set for the next time the netdev is started */
4155 	if (!netif_running(vsi->netdev)) {
4156 		err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
4157 		if (err)
4158 			goto rebuild_err;
4159 		dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
4160 		goto done;
4161 	}
4162 
4163 	ice_vsi_close(vsi);
4164 	err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
4165 	if (err)
4166 		goto rebuild_err;
4167 
4168 	ice_for_each_traffic_class(i) {
4169 		if (vsi->tc_cfg.ena_tc & BIT(i))
4170 			netdev_set_tc_queue(vsi->netdev,
4171 					    vsi->tc_cfg.tc_info[i].netdev_tc,
4172 					    vsi->tc_cfg.tc_info[i].qcount_tx,
4173 					    vsi->tc_cfg.tc_info[i].qoffset);
4174 	}
4175 	ice_pf_dcb_recfg(pf, locked);
4176 	ice_vsi_open(vsi);
4177 	goto done;
4178 
4179 rebuild_err:
4180 	dev_err(ice_pf_to_dev(pf), "Error during VSI rebuild: %d. Unload and reload the driver.\n",
4181 		err);
4182 done:
4183 	clear_bit(ICE_CFG_BUSY, pf->state);
4184 	return err;
4185 }
4186 
4187 /**
4188  * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
4189  * @pf: PF to configure
4190  *
4191  * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
4192  * VSI can still Tx/Rx VLAN tagged packets.
4193  */
ice_set_safe_mode_vlan_cfg(struct ice_pf * pf)4194 static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
4195 {
4196 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
4197 	struct ice_vsi_ctx *ctxt;
4198 	struct ice_hw *hw;
4199 	int status;
4200 
4201 	if (!vsi)
4202 		return;
4203 
4204 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
4205 	if (!ctxt)
4206 		return;
4207 
4208 	hw = &pf->hw;
4209 	ctxt->info = vsi->info;
4210 
4211 	ctxt->info.valid_sections =
4212 		cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
4213 			    ICE_AQ_VSI_PROP_SECURITY_VALID |
4214 			    ICE_AQ_VSI_PROP_SW_VALID);
4215 
4216 	/* disable VLAN anti-spoof */
4217 	ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4218 				  ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4219 
4220 	/* disable VLAN pruning and keep all other settings */
4221 	ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
4222 
4223 	/* allow all VLANs on Tx and don't strip on Rx */
4224 	ctxt->info.inner_vlan_flags = ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL |
4225 		ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING;
4226 
4227 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4228 	if (status) {
4229 		dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %d aq_err %s\n",
4230 			status, libie_aq_str(hw->adminq.sq_last_status));
4231 	} else {
4232 		vsi->info.sec_flags = ctxt->info.sec_flags;
4233 		vsi->info.sw_flags2 = ctxt->info.sw_flags2;
4234 		vsi->info.inner_vlan_flags = ctxt->info.inner_vlan_flags;
4235 	}
4236 
4237 	kfree(ctxt);
4238 }
4239 
4240 /**
4241  * ice_log_pkg_init - log result of DDP package load
4242  * @hw: pointer to hardware info
4243  * @state: state of package load
4244  */
ice_log_pkg_init(struct ice_hw * hw,enum ice_ddp_state state)4245 static void ice_log_pkg_init(struct ice_hw *hw, enum ice_ddp_state state)
4246 {
4247 	struct ice_pf *pf = hw->back;
4248 	struct device *dev;
4249 
4250 	dev = ice_pf_to_dev(pf);
4251 
4252 	switch (state) {
4253 	case ICE_DDP_PKG_SUCCESS:
4254 		dev_info(dev, "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
4255 			 hw->active_pkg_name,
4256 			 hw->active_pkg_ver.major,
4257 			 hw->active_pkg_ver.minor,
4258 			 hw->active_pkg_ver.update,
4259 			 hw->active_pkg_ver.draft);
4260 		break;
4261 	case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
4262 		dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
4263 			 hw->active_pkg_name,
4264 			 hw->active_pkg_ver.major,
4265 			 hw->active_pkg_ver.minor,
4266 			 hw->active_pkg_ver.update,
4267 			 hw->active_pkg_ver.draft);
4268 		break;
4269 	case ICE_DDP_PKG_ALREADY_LOADED_NOT_SUPPORTED:
4270 		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",
4271 			hw->active_pkg_name,
4272 			hw->active_pkg_ver.major,
4273 			hw->active_pkg_ver.minor,
4274 			ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4275 		break;
4276 	case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
4277 		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",
4278 			 hw->active_pkg_name,
4279 			 hw->active_pkg_ver.major,
4280 			 hw->active_pkg_ver.minor,
4281 			 hw->active_pkg_ver.update,
4282 			 hw->active_pkg_ver.draft,
4283 			 hw->pkg_name,
4284 			 hw->pkg_ver.major,
4285 			 hw->pkg_ver.minor,
4286 			 hw->pkg_ver.update,
4287 			 hw->pkg_ver.draft);
4288 		break;
4289 	case ICE_DDP_PKG_FW_MISMATCH:
4290 		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");
4291 		break;
4292 	case ICE_DDP_PKG_INVALID_FILE:
4293 		dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
4294 		break;
4295 	case ICE_DDP_PKG_FILE_VERSION_TOO_HIGH:
4296 		dev_err(dev, "The DDP package file version is higher than the driver supports.  Please use an updated driver.  Entering Safe Mode.\n");
4297 		break;
4298 	case ICE_DDP_PKG_FILE_VERSION_TOO_LOW:
4299 		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",
4300 			ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4301 		break;
4302 	case ICE_DDP_PKG_FILE_SIGNATURE_INVALID:
4303 		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");
4304 		break;
4305 	case ICE_DDP_PKG_FILE_REVISION_TOO_LOW:
4306 		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");
4307 		break;
4308 	case ICE_DDP_PKG_LOAD_ERROR:
4309 		dev_err(dev, "An error occurred on the device while loading the DDP package.  The device will be reset.\n");
4310 		/* poll for reset to complete */
4311 		if (ice_check_reset(hw))
4312 			dev_err(dev, "Error resetting device. Please reload the driver\n");
4313 		break;
4314 	case ICE_DDP_PKG_ERR:
4315 	default:
4316 		dev_err(dev, "An unknown error occurred when loading the DDP package.  Entering Safe Mode.\n");
4317 		break;
4318 	}
4319 }
4320 
4321 /**
4322  * ice_load_pkg - load/reload the DDP Package file
4323  * @firmware: firmware structure when firmware requested or NULL for reload
4324  * @pf: pointer to the PF instance
4325  *
4326  * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
4327  * initialize HW tables.
4328  */
4329 static void
ice_load_pkg(const struct firmware * firmware,struct ice_pf * pf)4330 ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
4331 {
4332 	enum ice_ddp_state state = ICE_DDP_PKG_ERR;
4333 	struct device *dev = ice_pf_to_dev(pf);
4334 	struct ice_hw *hw = &pf->hw;
4335 
4336 	/* Load DDP Package */
4337 	if (firmware && !hw->pkg_copy) {
4338 		state = ice_copy_and_init_pkg(hw, firmware->data,
4339 					      firmware->size);
4340 		ice_log_pkg_init(hw, state);
4341 	} else if (!firmware && hw->pkg_copy) {
4342 		/* Reload package during rebuild after CORER/GLOBR reset */
4343 		state = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
4344 		ice_log_pkg_init(hw, state);
4345 	} else {
4346 		dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
4347 	}
4348 
4349 	if (!ice_is_init_pkg_successful(state)) {
4350 		/* Safe Mode */
4351 		clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4352 		return;
4353 	}
4354 
4355 	/* Successful download package is the precondition for advanced
4356 	 * features, hence setting the ICE_FLAG_ADV_FEATURES flag
4357 	 */
4358 	set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4359 }
4360 
4361 /**
4362  * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
4363  * @pf: pointer to the PF structure
4364  *
4365  * There is no error returned here because the driver should be able to handle
4366  * 128 Byte cache lines, so we only print a warning in case issues are seen,
4367  * specifically with Tx.
4368  */
ice_verify_cacheline_size(struct ice_pf * pf)4369 static void ice_verify_cacheline_size(struct ice_pf *pf)
4370 {
4371 	if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
4372 		dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
4373 			 ICE_CACHE_LINE_BYTES);
4374 }
4375 
4376 /**
4377  * ice_send_version - update firmware with driver version
4378  * @pf: PF struct
4379  *
4380  * Returns 0 on success, else error code
4381  */
ice_send_version(struct ice_pf * pf)4382 static int ice_send_version(struct ice_pf *pf)
4383 {
4384 	struct ice_driver_ver dv;
4385 
4386 	dv.major_ver = 0xff;
4387 	dv.minor_ver = 0xff;
4388 	dv.build_ver = 0xff;
4389 	dv.subbuild_ver = 0;
4390 	strscpy((char *)dv.driver_string, UTS_RELEASE,
4391 		sizeof(dv.driver_string));
4392 	return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
4393 }
4394 
4395 /**
4396  * ice_init_fdir - Initialize flow director VSI and configuration
4397  * @pf: pointer to the PF instance
4398  *
4399  * returns 0 on success, negative on error
4400  */
ice_init_fdir(struct ice_pf * pf)4401 static int ice_init_fdir(struct ice_pf *pf)
4402 {
4403 	struct device *dev = ice_pf_to_dev(pf);
4404 	struct ice_vsi *ctrl_vsi;
4405 	int err;
4406 
4407 	/* Side Band Flow Director needs to have a control VSI.
4408 	 * Allocate it and store it in the PF.
4409 	 */
4410 	ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
4411 	if (!ctrl_vsi) {
4412 		dev_dbg(dev, "could not create control VSI\n");
4413 		return -ENOMEM;
4414 	}
4415 
4416 	err = ice_vsi_open_ctrl(ctrl_vsi);
4417 	if (err) {
4418 		dev_dbg(dev, "could not open control VSI\n");
4419 		goto err_vsi_open;
4420 	}
4421 
4422 	mutex_init(&pf->hw.fdir_fltr_lock);
4423 
4424 	err = ice_fdir_create_dflt_rules(pf);
4425 	if (err)
4426 		goto err_fdir_rule;
4427 
4428 	return 0;
4429 
4430 err_fdir_rule:
4431 	ice_fdir_release_flows(&pf->hw);
4432 	ice_vsi_close(ctrl_vsi);
4433 err_vsi_open:
4434 	ice_vsi_release(ctrl_vsi);
4435 	if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4436 		pf->vsi[pf->ctrl_vsi_idx] = NULL;
4437 		pf->ctrl_vsi_idx = ICE_NO_VSI;
4438 	}
4439 	return err;
4440 }
4441 
ice_deinit_fdir(struct ice_pf * pf)4442 static void ice_deinit_fdir(struct ice_pf *pf)
4443 {
4444 	struct ice_vsi *vsi = ice_get_ctrl_vsi(pf);
4445 
4446 	if (!vsi)
4447 		return;
4448 
4449 	ice_vsi_manage_fdir(vsi, false);
4450 	ice_vsi_release(vsi);
4451 	if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4452 		pf->vsi[pf->ctrl_vsi_idx] = NULL;
4453 		pf->ctrl_vsi_idx = ICE_NO_VSI;
4454 	}
4455 
4456 	mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
4457 }
4458 
4459 /**
4460  * ice_get_opt_fw_name - return optional firmware file name or NULL
4461  * @pf: pointer to the PF instance
4462  */
ice_get_opt_fw_name(struct ice_pf * pf)4463 static char *ice_get_opt_fw_name(struct ice_pf *pf)
4464 {
4465 	/* Optional firmware name same as default with additional dash
4466 	 * followed by a EUI-64 identifier (PCIe Device Serial Number)
4467 	 */
4468 	struct pci_dev *pdev = pf->pdev;
4469 	char *opt_fw_filename;
4470 	u64 dsn;
4471 
4472 	/* Determine the name of the optional file using the DSN (two
4473 	 * dwords following the start of the DSN Capability).
4474 	 */
4475 	dsn = pci_get_dsn(pdev);
4476 	if (!dsn)
4477 		return NULL;
4478 
4479 	opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
4480 	if (!opt_fw_filename)
4481 		return NULL;
4482 
4483 	snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
4484 		 ICE_DDP_PKG_PATH, dsn);
4485 
4486 	return opt_fw_filename;
4487 }
4488 
4489 /**
4490  * ice_request_fw - Device initialization routine
4491  * @pf: pointer to the PF instance
4492  * @firmware: double pointer to firmware struct
4493  *
4494  * Return: zero when successful, negative values otherwise.
4495  */
ice_request_fw(struct ice_pf * pf,const struct firmware ** firmware)4496 static int ice_request_fw(struct ice_pf *pf, const struct firmware **firmware)
4497 {
4498 	char *opt_fw_filename = ice_get_opt_fw_name(pf);
4499 	struct device *dev = ice_pf_to_dev(pf);
4500 	int err = 0;
4501 
4502 	/* optional device-specific DDP (if present) overrides the default DDP
4503 	 * package file. kernel logs a debug message if the file doesn't exist,
4504 	 * and warning messages for other errors.
4505 	 */
4506 	if (opt_fw_filename) {
4507 		err = firmware_request_nowarn(firmware, opt_fw_filename, dev);
4508 		kfree(opt_fw_filename);
4509 		if (!err)
4510 			return err;
4511 	}
4512 	err = request_firmware(firmware, ICE_DDP_PKG_FILE, dev);
4513 	if (err)
4514 		dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
4515 
4516 	return err;
4517 }
4518 
4519 /**
4520  * ice_init_tx_topology - performs Tx topology initialization
4521  * @hw: pointer to the hardware structure
4522  * @firmware: pointer to firmware structure
4523  *
4524  * Return: zero when init was successful, negative values otherwise.
4525  */
4526 static int
ice_init_tx_topology(struct ice_hw * hw,const struct firmware * firmware)4527 ice_init_tx_topology(struct ice_hw *hw, const struct firmware *firmware)
4528 {
4529 	u8 num_tx_sched_layers = hw->num_tx_sched_layers;
4530 	struct ice_pf *pf = hw->back;
4531 	struct device *dev;
4532 	int err;
4533 
4534 	dev = ice_pf_to_dev(pf);
4535 	err = ice_cfg_tx_topo(hw, firmware->data, firmware->size);
4536 	if (!err) {
4537 		if (hw->num_tx_sched_layers > num_tx_sched_layers)
4538 			dev_info(dev, "Tx scheduling layers switching feature disabled\n");
4539 		else
4540 			dev_info(dev, "Tx scheduling layers switching feature enabled\n");
4541 		return 0;
4542 	} else if (err == -ENODEV) {
4543 		/* If we failed to re-initialize the device, we can no longer
4544 		 * continue loading.
4545 		 */
4546 		dev_warn(dev, "Failed to initialize hardware after applying Tx scheduling configuration.\n");
4547 		return err;
4548 	} else if (err == -EIO) {
4549 		dev_info(dev, "DDP package does not support Tx scheduling layers switching feature - please update to the latest DDP package and try again\n");
4550 		return 0;
4551 	} else if (err == -EEXIST) {
4552 		return 0;
4553 	}
4554 
4555 	/* Do not treat this as a fatal error. */
4556 	dev_info(dev, "Failed to apply Tx scheduling configuration, err %pe\n",
4557 		 ERR_PTR(err));
4558 	return 0;
4559 }
4560 
4561 /**
4562  * ice_init_supported_rxdids - Initialize supported Rx descriptor IDs
4563  * @hw: pointer to the hardware structure
4564  * @pf: pointer to pf structure
4565  *
4566  * The pf->supported_rxdids bitmap is used to indicate to VFs which descriptor
4567  * formats the PF hardware supports. The exact list of supported RXDIDs
4568  * depends on the loaded DDP package. The IDs can be determined by reading the
4569  * GLFLXP_RXDID_FLAGS register after the DDP package is loaded.
4570  *
4571  * Note that the legacy 32-byte RXDID 0 is always supported but is not listed
4572  * in the DDP package. The 16-byte legacy descriptor is never supported by
4573  * VFs.
4574  */
ice_init_supported_rxdids(struct ice_hw * hw,struct ice_pf * pf)4575 static void ice_init_supported_rxdids(struct ice_hw *hw, struct ice_pf *pf)
4576 {
4577 	pf->supported_rxdids = BIT(ICE_RXDID_LEGACY_1);
4578 
4579 	for (int i = ICE_RXDID_FLEX_NIC; i < ICE_FLEX_DESC_RXDID_MAX_NUM; i++) {
4580 		u32 regval;
4581 
4582 		regval = rd32(hw, GLFLXP_RXDID_FLAGS(i, 0));
4583 		if ((regval >> GLFLXP_RXDID_FLAGS_FLEXIFLAG_4N_S)
4584 			& GLFLXP_RXDID_FLAGS_FLEXIFLAG_4N_M)
4585 			pf->supported_rxdids |= BIT(i);
4586 	}
4587 }
4588 
4589 /**
4590  * ice_init_ddp_config - DDP related configuration
4591  * @hw: pointer to the hardware structure
4592  * @pf: pointer to pf structure
4593  *
4594  * This function loads DDP file from the disk, then initializes Tx
4595  * topology. At the end DDP package is loaded on the card.
4596  *
4597  * Return: zero when init was successful, negative values otherwise.
4598  */
ice_init_ddp_config(struct ice_hw * hw,struct ice_pf * pf)4599 static int ice_init_ddp_config(struct ice_hw *hw, struct ice_pf *pf)
4600 {
4601 	struct device *dev = ice_pf_to_dev(pf);
4602 	const struct firmware *firmware = NULL;
4603 	int err;
4604 
4605 	err = ice_request_fw(pf, &firmware);
4606 	if (err) {
4607 		dev_err(dev, "Fail during requesting FW: %d\n", err);
4608 		return err;
4609 	}
4610 
4611 	err = ice_init_tx_topology(hw, firmware);
4612 	if (err) {
4613 		dev_err(dev, "Fail during initialization of Tx topology: %d\n",
4614 			err);
4615 		release_firmware(firmware);
4616 		return err;
4617 	}
4618 
4619 	/* Download firmware to device */
4620 	ice_load_pkg(firmware, pf);
4621 	release_firmware(firmware);
4622 
4623 	/* Initialize the supported Rx descriptor IDs after loading DDP */
4624 	ice_init_supported_rxdids(hw, pf);
4625 
4626 	return 0;
4627 }
4628 
4629 /**
4630  * ice_print_wake_reason - show the wake up cause in the log
4631  * @pf: pointer to the PF struct
4632  */
ice_print_wake_reason(struct ice_pf * pf)4633 static void ice_print_wake_reason(struct ice_pf *pf)
4634 {
4635 	u32 wus = pf->wakeup_reason;
4636 	const char *wake_str;
4637 
4638 	/* if no wake event, nothing to print */
4639 	if (!wus)
4640 		return;
4641 
4642 	if (wus & PFPM_WUS_LNKC_M)
4643 		wake_str = "Link\n";
4644 	else if (wus & PFPM_WUS_MAG_M)
4645 		wake_str = "Magic Packet\n";
4646 	else if (wus & PFPM_WUS_MNG_M)
4647 		wake_str = "Management\n";
4648 	else if (wus & PFPM_WUS_FW_RST_WK_M)
4649 		wake_str = "Firmware Reset\n";
4650 	else
4651 		wake_str = "Unknown\n";
4652 
4653 	dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
4654 }
4655 
4656 /**
4657  * ice_pf_fwlog_update_module - update 1 module
4658  * @pf: pointer to the PF struct
4659  * @log_level: log_level to use for the @module
4660  * @module: module to update
4661  */
ice_pf_fwlog_update_module(struct ice_pf * pf,int log_level,int module)4662 void ice_pf_fwlog_update_module(struct ice_pf *pf, int log_level, int module)
4663 {
4664 	struct ice_hw *hw = &pf->hw;
4665 
4666 	hw->fwlog_cfg.module_entries[module].log_level = log_level;
4667 }
4668 
4669 /**
4670  * ice_register_netdev - register netdev
4671  * @vsi: pointer to the VSI struct
4672  */
ice_register_netdev(struct ice_vsi * vsi)4673 static int ice_register_netdev(struct ice_vsi *vsi)
4674 {
4675 	int err;
4676 
4677 	if (!vsi || !vsi->netdev)
4678 		return -EIO;
4679 
4680 	err = register_netdev(vsi->netdev);
4681 	if (err)
4682 		return err;
4683 
4684 	set_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4685 	netif_carrier_off(vsi->netdev);
4686 	netif_tx_stop_all_queues(vsi->netdev);
4687 
4688 	return 0;
4689 }
4690 
ice_unregister_netdev(struct ice_vsi * vsi)4691 static void ice_unregister_netdev(struct ice_vsi *vsi)
4692 {
4693 	if (!vsi || !vsi->netdev)
4694 		return;
4695 
4696 	unregister_netdev(vsi->netdev);
4697 	clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4698 }
4699 
4700 /**
4701  * ice_cfg_netdev - Allocate, configure and register a netdev
4702  * @vsi: the VSI associated with the new netdev
4703  *
4704  * Returns 0 on success, negative value on failure
4705  */
ice_cfg_netdev(struct ice_vsi * vsi)4706 static int ice_cfg_netdev(struct ice_vsi *vsi)
4707 {
4708 	struct ice_netdev_priv *np;
4709 	struct net_device *netdev;
4710 	u8 mac_addr[ETH_ALEN];
4711 
4712 	netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
4713 				    vsi->alloc_rxq);
4714 	if (!netdev)
4715 		return -ENOMEM;
4716 
4717 	set_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4718 	vsi->netdev = netdev;
4719 	np = netdev_priv(netdev);
4720 	np->vsi = vsi;
4721 
4722 	ice_set_netdev_features(netdev);
4723 	ice_set_ops(vsi);
4724 
4725 	if (vsi->type == ICE_VSI_PF) {
4726 		SET_NETDEV_DEV(netdev, ice_pf_to_dev(vsi->back));
4727 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
4728 		eth_hw_addr_set(netdev, mac_addr);
4729 	}
4730 
4731 	netdev->priv_flags |= IFF_UNICAST_FLT;
4732 
4733 	/* Setup netdev TC information */
4734 	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
4735 
4736 	netdev->max_mtu = ICE_MAX_MTU;
4737 
4738 	return 0;
4739 }
4740 
ice_decfg_netdev(struct ice_vsi * vsi)4741 static void ice_decfg_netdev(struct ice_vsi *vsi)
4742 {
4743 	clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4744 	free_netdev(vsi->netdev);
4745 	vsi->netdev = NULL;
4746 }
4747 
ice_init_dev(struct ice_pf * pf)4748 int ice_init_dev(struct ice_pf *pf)
4749 {
4750 	struct device *dev = ice_pf_to_dev(pf);
4751 	struct ice_hw *hw = &pf->hw;
4752 	int err;
4753 
4754 	ice_init_feature_support(pf);
4755 
4756 	err = ice_init_ddp_config(hw, pf);
4757 
4758 	/* if ice_init_ddp_config fails, ICE_FLAG_ADV_FEATURES bit won't be
4759 	 * set in pf->state, which will cause ice_is_safe_mode to return
4760 	 * true
4761 	 */
4762 	if (err || ice_is_safe_mode(pf)) {
4763 		/* we already got function/device capabilities but these don't
4764 		 * reflect what the driver needs to do in safe mode. Instead of
4765 		 * adding conditional logic everywhere to ignore these
4766 		 * device/function capabilities, override them.
4767 		 */
4768 		ice_set_safe_mode_caps(hw);
4769 	}
4770 
4771 	err = ice_init_pf(pf);
4772 	if (err) {
4773 		dev_err(dev, "ice_init_pf failed: %d\n", err);
4774 		return err;
4775 	}
4776 
4777 	pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4778 	pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4779 	pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
4780 	if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4781 		pf->hw.udp_tunnel_nic.tables[0].n_entries =
4782 			pf->hw.tnl.valid_count[TNL_VXLAN];
4783 		pf->hw.udp_tunnel_nic.tables[0].tunnel_types =
4784 			UDP_TUNNEL_TYPE_VXLAN;
4785 	}
4786 	if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4787 		pf->hw.udp_tunnel_nic.tables[1].n_entries =
4788 			pf->hw.tnl.valid_count[TNL_GENEVE];
4789 		pf->hw.udp_tunnel_nic.tables[1].tunnel_types =
4790 			UDP_TUNNEL_TYPE_GENEVE;
4791 	}
4792 
4793 	err = ice_init_interrupt_scheme(pf);
4794 	if (err) {
4795 		dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4796 		err = -EIO;
4797 		goto unroll_pf_init;
4798 	}
4799 
4800 	/* In case of MSIX we are going to setup the misc vector right here
4801 	 * to handle admin queue events etc. In case of legacy and MSI
4802 	 * the misc functionality and queue processing is combined in
4803 	 * the same vector and that gets setup at open.
4804 	 */
4805 	err = ice_req_irq_msix_misc(pf);
4806 	if (err) {
4807 		dev_err(dev, "setup of misc vector failed: %d\n", err);
4808 		goto unroll_irq_scheme_init;
4809 	}
4810 
4811 	return 0;
4812 
4813 unroll_irq_scheme_init:
4814 	ice_clear_interrupt_scheme(pf);
4815 unroll_pf_init:
4816 	ice_deinit_pf(pf);
4817 	return err;
4818 }
4819 
ice_deinit_dev(struct ice_pf * pf)4820 void ice_deinit_dev(struct ice_pf *pf)
4821 {
4822 	ice_free_irq_msix_misc(pf);
4823 	ice_deinit_pf(pf);
4824 	ice_deinit_hw(&pf->hw);
4825 
4826 	/* Service task is already stopped, so call reset directly. */
4827 	ice_reset(&pf->hw, ICE_RESET_PFR);
4828 	pci_wait_for_pending_transaction(pf->pdev);
4829 	ice_clear_interrupt_scheme(pf);
4830 }
4831 
ice_init_features(struct ice_pf * pf)4832 static void ice_init_features(struct ice_pf *pf)
4833 {
4834 	struct device *dev = ice_pf_to_dev(pf);
4835 
4836 	if (ice_is_safe_mode(pf))
4837 		return;
4838 
4839 	/* initialize DDP driven features */
4840 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4841 		ice_ptp_init(pf);
4842 
4843 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
4844 		ice_gnss_init(pf);
4845 
4846 	if (ice_is_feature_supported(pf, ICE_F_CGU) ||
4847 	    ice_is_feature_supported(pf, ICE_F_PHY_RCLK))
4848 		ice_dpll_init(pf);
4849 
4850 	/* Note: Flow director init failure is non-fatal to load */
4851 	if (ice_init_fdir(pf))
4852 		dev_err(dev, "could not initialize flow director\n");
4853 
4854 	/* Note: DCB init failure is non-fatal to load */
4855 	if (ice_init_pf_dcb(pf, false)) {
4856 		clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4857 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4858 	} else {
4859 		ice_cfg_lldp_mib_change(&pf->hw, true);
4860 	}
4861 
4862 	if (ice_init_lag(pf))
4863 		dev_warn(dev, "Failed to init link aggregation support\n");
4864 
4865 	ice_hwmon_init(pf);
4866 }
4867 
ice_deinit_features(struct ice_pf * pf)4868 static void ice_deinit_features(struct ice_pf *pf)
4869 {
4870 	if (ice_is_safe_mode(pf))
4871 		return;
4872 
4873 	ice_deinit_lag(pf);
4874 	if (test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags))
4875 		ice_cfg_lldp_mib_change(&pf->hw, false);
4876 	ice_deinit_fdir(pf);
4877 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
4878 		ice_gnss_exit(pf);
4879 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4880 		ice_ptp_release(pf);
4881 	if (test_bit(ICE_FLAG_DPLL, pf->flags))
4882 		ice_dpll_deinit(pf);
4883 	if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV)
4884 		xa_destroy(&pf->eswitch.reprs);
4885 }
4886 
ice_init_wakeup(struct ice_pf * pf)4887 static void ice_init_wakeup(struct ice_pf *pf)
4888 {
4889 	/* Save wakeup reason register for later use */
4890 	pf->wakeup_reason = rd32(&pf->hw, PFPM_WUS);
4891 
4892 	/* check for a power management event */
4893 	ice_print_wake_reason(pf);
4894 
4895 	/* clear wake status, all bits */
4896 	wr32(&pf->hw, PFPM_WUS, U32_MAX);
4897 
4898 	/* Disable WoL at init, wait for user to enable */
4899 	device_set_wakeup_enable(ice_pf_to_dev(pf), false);
4900 }
4901 
ice_init_link(struct ice_pf * pf)4902 static int ice_init_link(struct ice_pf *pf)
4903 {
4904 	struct device *dev = ice_pf_to_dev(pf);
4905 	int err;
4906 
4907 	err = ice_init_link_events(pf->hw.port_info);
4908 	if (err) {
4909 		dev_err(dev, "ice_init_link_events failed: %d\n", err);
4910 		return err;
4911 	}
4912 
4913 	/* not a fatal error if this fails */
4914 	err = ice_init_nvm_phy_type(pf->hw.port_info);
4915 	if (err)
4916 		dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4917 
4918 	/* not a fatal error if this fails */
4919 	err = ice_update_link_info(pf->hw.port_info);
4920 	if (err)
4921 		dev_err(dev, "ice_update_link_info failed: %d\n", err);
4922 
4923 	ice_init_link_dflt_override(pf->hw.port_info);
4924 
4925 	ice_check_link_cfg_err(pf,
4926 			       pf->hw.port_info->phy.link_info.link_cfg_err);
4927 
4928 	/* if media available, initialize PHY settings */
4929 	if (pf->hw.port_info->phy.link_info.link_info &
4930 	    ICE_AQ_MEDIA_AVAILABLE) {
4931 		/* not a fatal error if this fails */
4932 		err = ice_init_phy_user_cfg(pf->hw.port_info);
4933 		if (err)
4934 			dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4935 
4936 		if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4937 			struct ice_vsi *vsi = ice_get_main_vsi(pf);
4938 
4939 			if (vsi)
4940 				ice_configure_phy(vsi);
4941 		}
4942 	} else {
4943 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4944 	}
4945 
4946 	return err;
4947 }
4948 
ice_init_pf_sw(struct ice_pf * pf)4949 static int ice_init_pf_sw(struct ice_pf *pf)
4950 {
4951 	bool dvm = ice_is_dvm_ena(&pf->hw);
4952 	struct ice_vsi *vsi;
4953 	int err;
4954 
4955 	/* create switch struct for the switch element created by FW on boot */
4956 	pf->first_sw = kzalloc(sizeof(*pf->first_sw), GFP_KERNEL);
4957 	if (!pf->first_sw)
4958 		return -ENOMEM;
4959 
4960 	if (pf->hw.evb_veb)
4961 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4962 	else
4963 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
4964 
4965 	pf->first_sw->pf = pf;
4966 
4967 	/* record the sw_id available for later use */
4968 	pf->first_sw->sw_id = pf->hw.port_info->sw_id;
4969 
4970 	err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
4971 	if (err)
4972 		goto err_aq_set_port_params;
4973 
4974 	vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
4975 	if (!vsi) {
4976 		err = -ENOMEM;
4977 		goto err_pf_vsi_setup;
4978 	}
4979 
4980 	return 0;
4981 
4982 err_pf_vsi_setup:
4983 err_aq_set_port_params:
4984 	kfree(pf->first_sw);
4985 	return err;
4986 }
4987 
ice_deinit_pf_sw(struct ice_pf * pf)4988 static void ice_deinit_pf_sw(struct ice_pf *pf)
4989 {
4990 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
4991 
4992 	if (!vsi)
4993 		return;
4994 
4995 	ice_vsi_release(vsi);
4996 	kfree(pf->first_sw);
4997 }
4998 
ice_alloc_vsis(struct ice_pf * pf)4999 static int ice_alloc_vsis(struct ice_pf *pf)
5000 {
5001 	struct device *dev = ice_pf_to_dev(pf);
5002 
5003 	pf->num_alloc_vsi = pf->hw.func_caps.guar_num_vsi;
5004 	if (!pf->num_alloc_vsi)
5005 		return -EIO;
5006 
5007 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
5008 		dev_warn(dev,
5009 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
5010 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
5011 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
5012 	}
5013 
5014 	pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
5015 			       GFP_KERNEL);
5016 	if (!pf->vsi)
5017 		return -ENOMEM;
5018 
5019 	pf->vsi_stats = devm_kcalloc(dev, pf->num_alloc_vsi,
5020 				     sizeof(*pf->vsi_stats), GFP_KERNEL);
5021 	if (!pf->vsi_stats) {
5022 		devm_kfree(dev, pf->vsi);
5023 		return -ENOMEM;
5024 	}
5025 
5026 	return 0;
5027 }
5028 
ice_dealloc_vsis(struct ice_pf * pf)5029 static void ice_dealloc_vsis(struct ice_pf *pf)
5030 {
5031 	devm_kfree(ice_pf_to_dev(pf), pf->vsi_stats);
5032 	pf->vsi_stats = NULL;
5033 
5034 	pf->num_alloc_vsi = 0;
5035 	devm_kfree(ice_pf_to_dev(pf), pf->vsi);
5036 	pf->vsi = NULL;
5037 }
5038 
ice_init_devlink(struct ice_pf * pf)5039 static int ice_init_devlink(struct ice_pf *pf)
5040 {
5041 	int err;
5042 
5043 	err = ice_devlink_register_params(pf);
5044 	if (err)
5045 		return err;
5046 
5047 	ice_devlink_init_regions(pf);
5048 	ice_devlink_register(pf);
5049 	ice_health_init(pf);
5050 
5051 	return 0;
5052 }
5053 
ice_deinit_devlink(struct ice_pf * pf)5054 static void ice_deinit_devlink(struct ice_pf *pf)
5055 {
5056 	ice_health_deinit(pf);
5057 	ice_devlink_unregister(pf);
5058 	ice_devlink_destroy_regions(pf);
5059 	ice_devlink_unregister_params(pf);
5060 }
5061 
ice_init(struct ice_pf * pf)5062 static int ice_init(struct ice_pf *pf)
5063 {
5064 	int err;
5065 
5066 	err = ice_init_dev(pf);
5067 	if (err)
5068 		return err;
5069 
5070 	if (pf->hw.mac_type == ICE_MAC_E830) {
5071 		err = pci_enable_ptm(pf->pdev, NULL);
5072 		if (err)
5073 			dev_dbg(ice_pf_to_dev(pf), "PCIe PTM not supported by PCIe bus/controller\n");
5074 	}
5075 
5076 	err = ice_alloc_vsis(pf);
5077 	if (err)
5078 		goto err_alloc_vsis;
5079 
5080 	err = ice_init_pf_sw(pf);
5081 	if (err)
5082 		goto err_init_pf_sw;
5083 
5084 	ice_init_wakeup(pf);
5085 
5086 	err = ice_init_link(pf);
5087 	if (err)
5088 		goto err_init_link;
5089 
5090 	err = ice_send_version(pf);
5091 	if (err)
5092 		goto err_init_link;
5093 
5094 	ice_verify_cacheline_size(pf);
5095 
5096 	if (ice_is_safe_mode(pf))
5097 		ice_set_safe_mode_vlan_cfg(pf);
5098 	else
5099 		/* print PCI link speed and width */
5100 		pcie_print_link_status(pf->pdev);
5101 
5102 	/* ready to go, so clear down state bit */
5103 	clear_bit(ICE_DOWN, pf->state);
5104 	clear_bit(ICE_SERVICE_DIS, pf->state);
5105 
5106 	/* since everything is good, start the service timer */
5107 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5108 
5109 	return 0;
5110 
5111 err_init_link:
5112 	ice_deinit_pf_sw(pf);
5113 err_init_pf_sw:
5114 	ice_dealloc_vsis(pf);
5115 err_alloc_vsis:
5116 	ice_deinit_dev(pf);
5117 	return err;
5118 }
5119 
ice_deinit(struct ice_pf * pf)5120 static void ice_deinit(struct ice_pf *pf)
5121 {
5122 	set_bit(ICE_SERVICE_DIS, pf->state);
5123 	set_bit(ICE_DOWN, pf->state);
5124 
5125 	ice_deinit_pf_sw(pf);
5126 	ice_dealloc_vsis(pf);
5127 	ice_deinit_dev(pf);
5128 }
5129 
5130 /**
5131  * ice_load - load pf by init hw and starting VSI
5132  * @pf: pointer to the pf instance
5133  *
5134  * This function has to be called under devl_lock.
5135  */
ice_load(struct ice_pf * pf)5136 int ice_load(struct ice_pf *pf)
5137 {
5138 	struct ice_vsi *vsi;
5139 	int err;
5140 
5141 	devl_assert_locked(priv_to_devlink(pf));
5142 
5143 	vsi = ice_get_main_vsi(pf);
5144 
5145 	/* init channel list */
5146 	INIT_LIST_HEAD(&vsi->ch_list);
5147 
5148 	err = ice_cfg_netdev(vsi);
5149 	if (err)
5150 		return err;
5151 
5152 	/* Setup DCB netlink interface */
5153 	ice_dcbnl_setup(vsi);
5154 
5155 	err = ice_init_mac_fltr(pf);
5156 	if (err)
5157 		goto err_init_mac_fltr;
5158 
5159 	err = ice_devlink_create_pf_port(pf);
5160 	if (err)
5161 		goto err_devlink_create_pf_port;
5162 
5163 	SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
5164 
5165 	err = ice_register_netdev(vsi);
5166 	if (err)
5167 		goto err_register_netdev;
5168 
5169 	err = ice_tc_indir_block_register(vsi);
5170 	if (err)
5171 		goto err_tc_indir_block_register;
5172 
5173 	ice_napi_add(vsi);
5174 
5175 	ice_init_features(pf);
5176 
5177 	err = ice_init_rdma(pf);
5178 	if (err)
5179 		goto err_init_rdma;
5180 
5181 	ice_service_task_restart(pf);
5182 
5183 	clear_bit(ICE_DOWN, pf->state);
5184 
5185 	return 0;
5186 
5187 err_init_rdma:
5188 	ice_deinit_features(pf);
5189 	ice_tc_indir_block_unregister(vsi);
5190 err_tc_indir_block_register:
5191 	ice_unregister_netdev(vsi);
5192 err_register_netdev:
5193 	ice_devlink_destroy_pf_port(pf);
5194 err_devlink_create_pf_port:
5195 err_init_mac_fltr:
5196 	ice_decfg_netdev(vsi);
5197 	return err;
5198 }
5199 
5200 /**
5201  * ice_unload - unload pf by stopping VSI and deinit hw
5202  * @pf: pointer to the pf instance
5203  *
5204  * This function has to be called under devl_lock.
5205  */
ice_unload(struct ice_pf * pf)5206 void ice_unload(struct ice_pf *pf)
5207 {
5208 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
5209 
5210 	devl_assert_locked(priv_to_devlink(pf));
5211 
5212 	ice_deinit_rdma(pf);
5213 	ice_deinit_features(pf);
5214 	ice_tc_indir_block_unregister(vsi);
5215 	ice_unregister_netdev(vsi);
5216 	ice_devlink_destroy_pf_port(pf);
5217 	ice_decfg_netdev(vsi);
5218 }
5219 
ice_probe_recovery_mode(struct ice_pf * pf)5220 static int ice_probe_recovery_mode(struct ice_pf *pf)
5221 {
5222 	struct device *dev = ice_pf_to_dev(pf);
5223 	int err;
5224 
5225 	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");
5226 
5227 	INIT_HLIST_HEAD(&pf->aq_wait_list);
5228 	spin_lock_init(&pf->aq_wait_lock);
5229 	init_waitqueue_head(&pf->aq_wait_queue);
5230 
5231 	timer_setup(&pf->serv_tmr, ice_service_timer, 0);
5232 	pf->serv_tmr_period = HZ;
5233 	INIT_WORK(&pf->serv_task, ice_service_task_recovery_mode);
5234 	clear_bit(ICE_SERVICE_SCHED, pf->state);
5235 	err = ice_create_all_ctrlq(&pf->hw);
5236 	if (err)
5237 		return err;
5238 
5239 	scoped_guard(devl, priv_to_devlink(pf)) {
5240 		err = ice_init_devlink(pf);
5241 		if (err)
5242 			return err;
5243 	}
5244 
5245 	ice_service_task_restart(pf);
5246 
5247 	return 0;
5248 }
5249 
5250 /**
5251  * ice_probe - Device initialization routine
5252  * @pdev: PCI device information struct
5253  * @ent: entry in ice_pci_tbl
5254  *
5255  * Returns 0 on success, negative on failure
5256  */
5257 static int
ice_probe(struct pci_dev * pdev,const struct pci_device_id __always_unused * ent)5258 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
5259 {
5260 	struct device *dev = &pdev->dev;
5261 	struct ice_adapter *adapter;
5262 	struct ice_pf *pf;
5263 	struct ice_hw *hw;
5264 	int err;
5265 
5266 	if (pdev->is_virtfn) {
5267 		dev_err(dev, "can't probe a virtual function\n");
5268 		return -EINVAL;
5269 	}
5270 
5271 	/* when under a kdump kernel initiate a reset before enabling the
5272 	 * device in order to clear out any pending DMA transactions. These
5273 	 * transactions can cause some systems to machine check when doing
5274 	 * the pcim_enable_device() below.
5275 	 */
5276 	if (is_kdump_kernel()) {
5277 		pci_save_state(pdev);
5278 		pci_clear_master(pdev);
5279 		err = pcie_flr(pdev);
5280 		if (err)
5281 			return err;
5282 		pci_restore_state(pdev);
5283 	}
5284 
5285 	/* this driver uses devres, see
5286 	 * Documentation/driver-api/driver-model/devres.rst
5287 	 */
5288 	err = pcim_enable_device(pdev);
5289 	if (err)
5290 		return err;
5291 
5292 	err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), dev_driver_string(dev));
5293 	if (err) {
5294 		dev_err(dev, "BAR0 I/O map error %d\n", err);
5295 		return err;
5296 	}
5297 
5298 	pf = ice_allocate_pf(dev);
5299 	if (!pf)
5300 		return -ENOMEM;
5301 
5302 	/* initialize Auxiliary index to invalid value */
5303 	pf->aux_idx = -1;
5304 
5305 	/* set up for high or low DMA */
5306 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
5307 	if (err) {
5308 		dev_err(dev, "DMA configuration failed: 0x%x\n", err);
5309 		return err;
5310 	}
5311 
5312 	pci_set_master(pdev);
5313 	pf->pdev = pdev;
5314 	pci_set_drvdata(pdev, pf);
5315 	set_bit(ICE_DOWN, pf->state);
5316 	/* Disable service task until DOWN bit is cleared */
5317 	set_bit(ICE_SERVICE_DIS, pf->state);
5318 
5319 	hw = &pf->hw;
5320 	hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
5321 	pci_save_state(pdev);
5322 
5323 	hw->back = pf;
5324 	hw->port_info = NULL;
5325 	hw->vendor_id = pdev->vendor;
5326 	hw->device_id = pdev->device;
5327 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
5328 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
5329 	hw->subsystem_device_id = pdev->subsystem_device;
5330 	hw->bus.device = PCI_SLOT(pdev->devfn);
5331 	hw->bus.func = PCI_FUNC(pdev->devfn);
5332 	ice_set_ctrlq_len(hw);
5333 
5334 	pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
5335 
5336 #ifndef CONFIG_DYNAMIC_DEBUG
5337 	if (debug < -1)
5338 		hw->debug_mask = debug;
5339 #endif
5340 
5341 	if (ice_is_recovery_mode(hw))
5342 		return ice_probe_recovery_mode(pf);
5343 
5344 	err = ice_init_hw(hw);
5345 	if (err) {
5346 		dev_err(dev, "ice_init_hw failed: %d\n", err);
5347 		return err;
5348 	}
5349 
5350 	adapter = ice_adapter_get(pdev);
5351 	if (IS_ERR(adapter)) {
5352 		err = PTR_ERR(adapter);
5353 		goto unroll_hw_init;
5354 	}
5355 	pf->adapter = adapter;
5356 
5357 	err = ice_init(pf);
5358 	if (err)
5359 		goto unroll_adapter;
5360 
5361 	devl_lock(priv_to_devlink(pf));
5362 	err = ice_load(pf);
5363 	if (err)
5364 		goto unroll_init;
5365 
5366 	err = ice_init_devlink(pf);
5367 	if (err)
5368 		goto unroll_load;
5369 	devl_unlock(priv_to_devlink(pf));
5370 
5371 	return 0;
5372 
5373 unroll_load:
5374 	ice_unload(pf);
5375 unroll_init:
5376 	devl_unlock(priv_to_devlink(pf));
5377 	ice_deinit(pf);
5378 unroll_adapter:
5379 	ice_adapter_put(pdev);
5380 unroll_hw_init:
5381 	ice_deinit_hw(hw);
5382 	return err;
5383 }
5384 
5385 /**
5386  * ice_set_wake - enable or disable Wake on LAN
5387  * @pf: pointer to the PF struct
5388  *
5389  * Simple helper for WoL control
5390  */
ice_set_wake(struct ice_pf * pf)5391 static void ice_set_wake(struct ice_pf *pf)
5392 {
5393 	struct ice_hw *hw = &pf->hw;
5394 	bool wol = pf->wol_ena;
5395 
5396 	/* clear wake state, otherwise new wake events won't fire */
5397 	wr32(hw, PFPM_WUS, U32_MAX);
5398 
5399 	/* enable / disable APM wake up, no RMW needed */
5400 	wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
5401 
5402 	/* set magic packet filter enabled */
5403 	wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
5404 }
5405 
5406 /**
5407  * ice_setup_mc_magic_wake - setup device to wake on multicast magic packet
5408  * @pf: pointer to the PF struct
5409  *
5410  * Issue firmware command to enable multicast magic wake, making
5411  * sure that any locally administered address (LAA) is used for
5412  * wake, and that PF reset doesn't undo the LAA.
5413  */
ice_setup_mc_magic_wake(struct ice_pf * pf)5414 static void ice_setup_mc_magic_wake(struct ice_pf *pf)
5415 {
5416 	struct device *dev = ice_pf_to_dev(pf);
5417 	struct ice_hw *hw = &pf->hw;
5418 	u8 mac_addr[ETH_ALEN];
5419 	struct ice_vsi *vsi;
5420 	int status;
5421 	u8 flags;
5422 
5423 	if (!pf->wol_ena)
5424 		return;
5425 
5426 	vsi = ice_get_main_vsi(pf);
5427 	if (!vsi)
5428 		return;
5429 
5430 	/* Get current MAC address in case it's an LAA */
5431 	if (vsi->netdev)
5432 		ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
5433 	else
5434 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
5435 
5436 	flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
5437 		ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
5438 		ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
5439 
5440 	status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
5441 	if (status)
5442 		dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %d aq_err %s\n",
5443 			status, libie_aq_str(hw->adminq.sq_last_status));
5444 }
5445 
5446 /**
5447  * ice_remove - Device removal routine
5448  * @pdev: PCI device information struct
5449  */
ice_remove(struct pci_dev * pdev)5450 static void ice_remove(struct pci_dev *pdev)
5451 {
5452 	struct ice_pf *pf = pci_get_drvdata(pdev);
5453 	int i;
5454 
5455 	for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
5456 		if (!ice_is_reset_in_progress(pf->state))
5457 			break;
5458 		msleep(100);
5459 	}
5460 
5461 	if (ice_is_recovery_mode(&pf->hw)) {
5462 		ice_service_task_stop(pf);
5463 		scoped_guard(devl, priv_to_devlink(pf)) {
5464 			ice_deinit_devlink(pf);
5465 		}
5466 		return;
5467 	}
5468 
5469 	if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
5470 		set_bit(ICE_VF_RESETS_DISABLED, pf->state);
5471 		ice_free_vfs(pf);
5472 	}
5473 
5474 	ice_hwmon_exit(pf);
5475 
5476 	ice_service_task_stop(pf);
5477 	ice_aq_cancel_waiting_tasks(pf);
5478 	set_bit(ICE_DOWN, pf->state);
5479 
5480 	if (!ice_is_safe_mode(pf))
5481 		ice_remove_arfs(pf);
5482 
5483 	devl_lock(priv_to_devlink(pf));
5484 	ice_dealloc_all_dynamic_ports(pf);
5485 	ice_deinit_devlink(pf);
5486 
5487 	ice_unload(pf);
5488 	devl_unlock(priv_to_devlink(pf));
5489 
5490 	ice_deinit(pf);
5491 	ice_vsi_release_all(pf);
5492 
5493 	ice_setup_mc_magic_wake(pf);
5494 	ice_set_wake(pf);
5495 
5496 	ice_adapter_put(pdev);
5497 }
5498 
5499 /**
5500  * ice_shutdown - PCI callback for shutting down device
5501  * @pdev: PCI device information struct
5502  */
ice_shutdown(struct pci_dev * pdev)5503 static void ice_shutdown(struct pci_dev *pdev)
5504 {
5505 	struct ice_pf *pf = pci_get_drvdata(pdev);
5506 
5507 	ice_remove(pdev);
5508 
5509 	if (system_state == SYSTEM_POWER_OFF) {
5510 		pci_wake_from_d3(pdev, pf->wol_ena);
5511 		pci_set_power_state(pdev, PCI_D3hot);
5512 	}
5513 }
5514 
5515 /**
5516  * ice_prepare_for_shutdown - prep for PCI shutdown
5517  * @pf: board private structure
5518  *
5519  * Inform or close all dependent features in prep for PCI device shutdown
5520  */
ice_prepare_for_shutdown(struct ice_pf * pf)5521 static void ice_prepare_for_shutdown(struct ice_pf *pf)
5522 {
5523 	struct ice_hw *hw = &pf->hw;
5524 	u32 v;
5525 
5526 	/* Notify VFs of impending reset */
5527 	if (ice_check_sq_alive(hw, &hw->mailboxq))
5528 		ice_vc_notify_reset(pf);
5529 
5530 	dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
5531 
5532 	/* disable the VSIs and their queues that are not already DOWN */
5533 	ice_pf_dis_all_vsi(pf, false);
5534 
5535 	ice_for_each_vsi(pf, v)
5536 		if (pf->vsi[v])
5537 			pf->vsi[v]->vsi_num = 0;
5538 
5539 	ice_shutdown_all_ctrlq(hw, true);
5540 }
5541 
5542 /**
5543  * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
5544  * @pf: board private structure to reinitialize
5545  *
5546  * This routine reinitialize interrupt scheme that was cleared during
5547  * power management suspend callback.
5548  *
5549  * This should be called during resume routine to re-allocate the q_vectors
5550  * and reacquire interrupts.
5551  */
ice_reinit_interrupt_scheme(struct ice_pf * pf)5552 static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
5553 {
5554 	struct device *dev = ice_pf_to_dev(pf);
5555 	int ret, v;
5556 
5557 	/* Since we clear MSIX flag during suspend, we need to
5558 	 * set it back during resume...
5559 	 */
5560 
5561 	ret = ice_init_interrupt_scheme(pf);
5562 	if (ret) {
5563 		dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
5564 		return ret;
5565 	}
5566 
5567 	/* Remap vectors and rings, after successful re-init interrupts */
5568 	ice_for_each_vsi(pf, v) {
5569 		if (!pf->vsi[v])
5570 			continue;
5571 
5572 		ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
5573 		if (ret)
5574 			goto err_reinit;
5575 		ice_vsi_map_rings_to_vectors(pf->vsi[v]);
5576 		rtnl_lock();
5577 		ice_vsi_set_napi_queues(pf->vsi[v]);
5578 		rtnl_unlock();
5579 	}
5580 
5581 	ret = ice_req_irq_msix_misc(pf);
5582 	if (ret) {
5583 		dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
5584 			ret);
5585 		goto err_reinit;
5586 	}
5587 
5588 	return 0;
5589 
5590 err_reinit:
5591 	while (v--)
5592 		if (pf->vsi[v]) {
5593 			rtnl_lock();
5594 			ice_vsi_clear_napi_queues(pf->vsi[v]);
5595 			rtnl_unlock();
5596 			ice_vsi_free_q_vectors(pf->vsi[v]);
5597 		}
5598 
5599 	return ret;
5600 }
5601 
5602 /**
5603  * ice_suspend
5604  * @dev: generic device information structure
5605  *
5606  * Power Management callback to quiesce the device and prepare
5607  * for D3 transition.
5608  */
ice_suspend(struct device * dev)5609 static int ice_suspend(struct device *dev)
5610 {
5611 	struct pci_dev *pdev = to_pci_dev(dev);
5612 	struct ice_pf *pf;
5613 	int disabled, v;
5614 
5615 	pf = pci_get_drvdata(pdev);
5616 
5617 	if (!ice_pf_state_is_nominal(pf)) {
5618 		dev_err(dev, "Device is not ready, no need to suspend it\n");
5619 		return -EBUSY;
5620 	}
5621 
5622 	/* Stop watchdog tasks until resume completion.
5623 	 * Even though it is most likely that the service task is
5624 	 * disabled if the device is suspended or down, the service task's
5625 	 * state is controlled by a different state bit, and we should
5626 	 * store and honor whatever state that bit is in at this point.
5627 	 */
5628 	disabled = ice_service_task_stop(pf);
5629 
5630 	ice_deinit_rdma(pf);
5631 
5632 	/* Already suspended?, then there is nothing to do */
5633 	if (test_and_set_bit(ICE_SUSPENDED, pf->state)) {
5634 		if (!disabled)
5635 			ice_service_task_restart(pf);
5636 		return 0;
5637 	}
5638 
5639 	if (test_bit(ICE_DOWN, pf->state) ||
5640 	    ice_is_reset_in_progress(pf->state)) {
5641 		dev_err(dev, "can't suspend device in reset or already down\n");
5642 		if (!disabled)
5643 			ice_service_task_restart(pf);
5644 		return 0;
5645 	}
5646 
5647 	ice_setup_mc_magic_wake(pf);
5648 
5649 	ice_prepare_for_shutdown(pf);
5650 
5651 	ice_set_wake(pf);
5652 
5653 	/* Free vectors, clear the interrupt scheme and release IRQs
5654 	 * for proper hibernation, especially with large number of CPUs.
5655 	 * Otherwise hibernation might fail when mapping all the vectors back
5656 	 * to CPU0.
5657 	 */
5658 	ice_free_irq_msix_misc(pf);
5659 	ice_for_each_vsi(pf, v) {
5660 		if (!pf->vsi[v])
5661 			continue;
5662 		rtnl_lock();
5663 		ice_vsi_clear_napi_queues(pf->vsi[v]);
5664 		rtnl_unlock();
5665 		ice_vsi_free_q_vectors(pf->vsi[v]);
5666 	}
5667 	ice_clear_interrupt_scheme(pf);
5668 
5669 	pci_save_state(pdev);
5670 	pci_wake_from_d3(pdev, pf->wol_ena);
5671 	pci_set_power_state(pdev, PCI_D3hot);
5672 	return 0;
5673 }
5674 
5675 /**
5676  * ice_resume - PM callback for waking up from D3
5677  * @dev: generic device information structure
5678  */
ice_resume(struct device * dev)5679 static int ice_resume(struct device *dev)
5680 {
5681 	struct pci_dev *pdev = to_pci_dev(dev);
5682 	enum ice_reset_req reset_type;
5683 	struct ice_pf *pf;
5684 	struct ice_hw *hw;
5685 	int ret;
5686 
5687 	pci_set_power_state(pdev, PCI_D0);
5688 	pci_restore_state(pdev);
5689 	pci_save_state(pdev);
5690 
5691 	if (!pci_device_is_present(pdev))
5692 		return -ENODEV;
5693 
5694 	ret = pci_enable_device_mem(pdev);
5695 	if (ret) {
5696 		dev_err(dev, "Cannot enable device after suspend\n");
5697 		return ret;
5698 	}
5699 
5700 	pf = pci_get_drvdata(pdev);
5701 	hw = &pf->hw;
5702 
5703 	pf->wakeup_reason = rd32(hw, PFPM_WUS);
5704 	ice_print_wake_reason(pf);
5705 
5706 	/* We cleared the interrupt scheme when we suspended, so we need to
5707 	 * restore it now to resume device functionality.
5708 	 */
5709 	ret = ice_reinit_interrupt_scheme(pf);
5710 	if (ret)
5711 		dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
5712 
5713 	ret = ice_init_rdma(pf);
5714 	if (ret)
5715 		dev_err(dev, "Reinitialize RDMA during resume failed: %d\n",
5716 			ret);
5717 
5718 	clear_bit(ICE_DOWN, pf->state);
5719 	/* Now perform PF reset and rebuild */
5720 	reset_type = ICE_RESET_PFR;
5721 	/* re-enable service task for reset, but allow reset to schedule it */
5722 	clear_bit(ICE_SERVICE_DIS, pf->state);
5723 
5724 	if (ice_schedule_reset(pf, reset_type))
5725 		dev_err(dev, "Reset during resume failed.\n");
5726 
5727 	clear_bit(ICE_SUSPENDED, pf->state);
5728 	ice_service_task_restart(pf);
5729 
5730 	/* Restart the service task */
5731 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5732 
5733 	return 0;
5734 }
5735 
5736 /**
5737  * ice_pci_err_detected - warning that PCI error has been detected
5738  * @pdev: PCI device information struct
5739  * @err: the type of PCI error
5740  *
5741  * Called to warn that something happened on the PCI bus and the error handling
5742  * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
5743  */
5744 static pci_ers_result_t
ice_pci_err_detected(struct pci_dev * pdev,pci_channel_state_t err)5745 ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
5746 {
5747 	struct ice_pf *pf = pci_get_drvdata(pdev);
5748 
5749 	if (!pf) {
5750 		dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
5751 			__func__, err);
5752 		return PCI_ERS_RESULT_DISCONNECT;
5753 	}
5754 
5755 	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5756 		ice_service_task_stop(pf);
5757 
5758 		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5759 			set_bit(ICE_PFR_REQ, pf->state);
5760 			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5761 		}
5762 	}
5763 
5764 	return PCI_ERS_RESULT_NEED_RESET;
5765 }
5766 
5767 /**
5768  * ice_pci_err_slot_reset - a PCI slot reset has just happened
5769  * @pdev: PCI device information struct
5770  *
5771  * Called to determine if the driver can recover from the PCI slot reset by
5772  * using a register read to determine if the device is recoverable.
5773  */
ice_pci_err_slot_reset(struct pci_dev * pdev)5774 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
5775 {
5776 	struct ice_pf *pf = pci_get_drvdata(pdev);
5777 	pci_ers_result_t result;
5778 	int err;
5779 	u32 reg;
5780 
5781 	err = pci_enable_device_mem(pdev);
5782 	if (err) {
5783 		dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
5784 			err);
5785 		result = PCI_ERS_RESULT_DISCONNECT;
5786 	} else {
5787 		pci_set_master(pdev);
5788 		pci_restore_state(pdev);
5789 		pci_save_state(pdev);
5790 		pci_wake_from_d3(pdev, false);
5791 
5792 		/* Check for life */
5793 		reg = rd32(&pf->hw, GLGEN_RTRIG);
5794 		if (!reg)
5795 			result = PCI_ERS_RESULT_RECOVERED;
5796 		else
5797 			result = PCI_ERS_RESULT_DISCONNECT;
5798 	}
5799 
5800 	return result;
5801 }
5802 
5803 /**
5804  * ice_pci_err_resume - restart operations after PCI error recovery
5805  * @pdev: PCI device information struct
5806  *
5807  * Called to allow the driver to bring things back up after PCI error and/or
5808  * reset recovery have finished
5809  */
ice_pci_err_resume(struct pci_dev * pdev)5810 static void ice_pci_err_resume(struct pci_dev *pdev)
5811 {
5812 	struct ice_pf *pf = pci_get_drvdata(pdev);
5813 
5814 	if (!pf) {
5815 		dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
5816 			__func__);
5817 		return;
5818 	}
5819 
5820 	if (test_bit(ICE_SUSPENDED, pf->state)) {
5821 		dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
5822 			__func__);
5823 		return;
5824 	}
5825 
5826 	ice_restore_all_vfs_msi_state(pf);
5827 
5828 	ice_do_reset(pf, ICE_RESET_PFR);
5829 	ice_service_task_restart(pf);
5830 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5831 }
5832 
5833 /**
5834  * ice_pci_err_reset_prepare - prepare device driver for PCI reset
5835  * @pdev: PCI device information struct
5836  */
ice_pci_err_reset_prepare(struct pci_dev * pdev)5837 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
5838 {
5839 	struct ice_pf *pf = pci_get_drvdata(pdev);
5840 
5841 	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5842 		ice_service_task_stop(pf);
5843 
5844 		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5845 			set_bit(ICE_PFR_REQ, pf->state);
5846 			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5847 		}
5848 	}
5849 }
5850 
5851 /**
5852  * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
5853  * @pdev: PCI device information struct
5854  */
ice_pci_err_reset_done(struct pci_dev * pdev)5855 static void ice_pci_err_reset_done(struct pci_dev *pdev)
5856 {
5857 	ice_pci_err_resume(pdev);
5858 }
5859 
5860 /* ice_pci_tbl - PCI Device ID Table
5861  *
5862  * Wildcard entries (PCI_ANY_ID) should come last
5863  * Last entry must be all 0s
5864  *
5865  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
5866  *   Class, Class Mask, private data (not used) }
5867  */
5868 static const struct pci_device_id ice_pci_tbl[] = {
5869 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE) },
5870 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP) },
5871 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP) },
5872 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE) },
5873 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP) },
5874 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP) },
5875 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE) },
5876 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP) },
5877 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP) },
5878 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T) },
5879 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII) },
5880 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE) },
5881 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP) },
5882 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP) },
5883 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T) },
5884 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII) },
5885 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE) },
5886 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP) },
5887 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T) },
5888 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII) },
5889 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE) },
5890 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP) },
5891 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T) },
5892 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE) },
5893 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP) },
5894 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822_SI_DFLT) },
5895 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_BACKPLANE), },
5896 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_QSFP), },
5897 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_SFP), },
5898 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E825C_SGMII), },
5899 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_BACKPLANE) },
5900 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_QSFP56) },
5901 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_SFP) },
5902 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830CC_SFP_DD) },
5903 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_BACKPLANE), },
5904 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_BACKPLANE), },
5905 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_QSFP), },
5906 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_QSFP), },
5907 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830C_SFP), },
5908 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_XXV_SFP), },
5909 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E835CC_BACKPLANE), },
5910 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E835CC_QSFP56), },
5911 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E835CC_SFP), },
5912 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E835C_BACKPLANE), },
5913 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E835C_QSFP), },
5914 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E835C_SFP), },
5915 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E835_L_BACKPLANE), },
5916 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E835_L_QSFP), },
5917 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E835_L_SFP), },
5918 	/* required last entry */
5919 	{}
5920 };
5921 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
5922 
5923 static DEFINE_SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
5924 
5925 static const struct pci_error_handlers ice_pci_err_handler = {
5926 	.error_detected = ice_pci_err_detected,
5927 	.slot_reset = ice_pci_err_slot_reset,
5928 	.reset_prepare = ice_pci_err_reset_prepare,
5929 	.reset_done = ice_pci_err_reset_done,
5930 	.resume = ice_pci_err_resume
5931 };
5932 
5933 static struct pci_driver ice_driver = {
5934 	.name = KBUILD_MODNAME,
5935 	.id_table = ice_pci_tbl,
5936 	.probe = ice_probe,
5937 	.remove = ice_remove,
5938 	.driver.pm = pm_sleep_ptr(&ice_pm_ops),
5939 	.shutdown = ice_shutdown,
5940 	.sriov_configure = ice_sriov_configure,
5941 	.sriov_get_vf_total_msix = ice_sriov_get_vf_total_msix,
5942 	.sriov_set_msix_vec_count = ice_sriov_set_msix_vec_count,
5943 	.err_handler = &ice_pci_err_handler
5944 };
5945 
5946 /**
5947  * ice_module_init - Driver registration routine
5948  *
5949  * ice_module_init is the first routine called when the driver is
5950  * loaded. All it does is register with the PCI subsystem.
5951  */
ice_module_init(void)5952 static int __init ice_module_init(void)
5953 {
5954 	int status = -ENOMEM;
5955 
5956 	pr_info("%s\n", ice_driver_string);
5957 	pr_info("%s\n", ice_copyright);
5958 
5959 	ice_adv_lnk_speed_maps_init();
5960 
5961 	ice_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, KBUILD_MODNAME);
5962 	if (!ice_wq) {
5963 		pr_err("Failed to create workqueue\n");
5964 		return status;
5965 	}
5966 
5967 	ice_lag_wq = alloc_ordered_workqueue("ice_lag_wq", 0);
5968 	if (!ice_lag_wq) {
5969 		pr_err("Failed to create LAG workqueue\n");
5970 		goto err_dest_wq;
5971 	}
5972 
5973 	ice_debugfs_init();
5974 
5975 	status = pci_register_driver(&ice_driver);
5976 	if (status) {
5977 		pr_err("failed to register PCI driver, err %d\n", status);
5978 		goto err_dest_lag_wq;
5979 	}
5980 
5981 	status = ice_sf_driver_register();
5982 	if (status) {
5983 		pr_err("Failed to register SF driver, err %d\n", status);
5984 		goto err_sf_driver;
5985 	}
5986 
5987 	return 0;
5988 
5989 err_sf_driver:
5990 	pci_unregister_driver(&ice_driver);
5991 err_dest_lag_wq:
5992 	destroy_workqueue(ice_lag_wq);
5993 	ice_debugfs_exit();
5994 err_dest_wq:
5995 	destroy_workqueue(ice_wq);
5996 	return status;
5997 }
5998 module_init(ice_module_init);
5999 
6000 /**
6001  * ice_module_exit - Driver exit cleanup routine
6002  *
6003  * ice_module_exit is called just before the driver is removed
6004  * from memory.
6005  */
ice_module_exit(void)6006 static void __exit ice_module_exit(void)
6007 {
6008 	ice_sf_driver_unregister();
6009 	pci_unregister_driver(&ice_driver);
6010 	ice_debugfs_exit();
6011 	destroy_workqueue(ice_wq);
6012 	destroy_workqueue(ice_lag_wq);
6013 	pr_info("module unloaded\n");
6014 }
6015 module_exit(ice_module_exit);
6016 
6017 /**
6018  * ice_set_mac_address - NDO callback to set MAC address
6019  * @netdev: network interface device structure
6020  * @pi: pointer to an address structure
6021  *
6022  * Returns 0 on success, negative on failure
6023  */
ice_set_mac_address(struct net_device * netdev,void * pi)6024 static int ice_set_mac_address(struct net_device *netdev, void *pi)
6025 {
6026 	struct ice_netdev_priv *np = netdev_priv(netdev);
6027 	struct ice_vsi *vsi = np->vsi;
6028 	struct ice_pf *pf = vsi->back;
6029 	struct ice_hw *hw = &pf->hw;
6030 	struct sockaddr *addr = pi;
6031 	u8 old_mac[ETH_ALEN];
6032 	u8 flags = 0;
6033 	u8 *mac;
6034 	int err;
6035 
6036 	mac = (u8 *)addr->sa_data;
6037 
6038 	if (!is_valid_ether_addr(mac))
6039 		return -EADDRNOTAVAIL;
6040 
6041 	if (test_bit(ICE_DOWN, pf->state) ||
6042 	    ice_is_reset_in_progress(pf->state)) {
6043 		netdev_err(netdev, "can't set mac %pM. device not ready\n",
6044 			   mac);
6045 		return -EBUSY;
6046 	}
6047 
6048 	if (ice_chnl_dmac_fltr_cnt(pf)) {
6049 		netdev_err(netdev, "can't set mac %pM. Device has tc-flower filters, delete all of them and try again\n",
6050 			   mac);
6051 		return -EAGAIN;
6052 	}
6053 
6054 	netif_addr_lock_bh(netdev);
6055 	ether_addr_copy(old_mac, netdev->dev_addr);
6056 	/* change the netdev's MAC address */
6057 	eth_hw_addr_set(netdev, mac);
6058 	netif_addr_unlock_bh(netdev);
6059 
6060 	/* Clean up old MAC filter. Not an error if old filter doesn't exist */
6061 	err = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
6062 	if (err && err != -ENOENT) {
6063 		err = -EADDRNOTAVAIL;
6064 		goto err_update_filters;
6065 	}
6066 
6067 	/* Add filter for new MAC. If filter exists, return success */
6068 	err = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
6069 	if (err == -EEXIST) {
6070 		/* Although this MAC filter is already present in hardware it's
6071 		 * possible in some cases (e.g. bonding) that dev_addr was
6072 		 * modified outside of the driver and needs to be restored back
6073 		 * to this value.
6074 		 */
6075 		netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
6076 
6077 		return 0;
6078 	} else if (err) {
6079 		/* error if the new filter addition failed */
6080 		err = -EADDRNOTAVAIL;
6081 	}
6082 
6083 err_update_filters:
6084 	if (err) {
6085 		netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
6086 			   mac);
6087 		netif_addr_lock_bh(netdev);
6088 		eth_hw_addr_set(netdev, old_mac);
6089 		netif_addr_unlock_bh(netdev);
6090 		return err;
6091 	}
6092 
6093 	netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
6094 		   netdev->dev_addr);
6095 
6096 	/* write new MAC address to the firmware */
6097 	flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
6098 	err = ice_aq_manage_mac_write(hw, mac, flags, NULL);
6099 	if (err) {
6100 		netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n",
6101 			   mac, err);
6102 	}
6103 	return 0;
6104 }
6105 
6106 /**
6107  * ice_set_rx_mode - NDO callback to set the netdev filters
6108  * @netdev: network interface device structure
6109  */
ice_set_rx_mode(struct net_device * netdev)6110 static void ice_set_rx_mode(struct net_device *netdev)
6111 {
6112 	struct ice_netdev_priv *np = netdev_priv(netdev);
6113 	struct ice_vsi *vsi = np->vsi;
6114 
6115 	if (!vsi || ice_is_switchdev_running(vsi->back))
6116 		return;
6117 
6118 	/* Set the flags to synchronize filters
6119 	 * ndo_set_rx_mode may be triggered even without a change in netdev
6120 	 * flags
6121 	 */
6122 	set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
6123 	set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
6124 	set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
6125 
6126 	/* schedule our worker thread which will take care of
6127 	 * applying the new filter changes
6128 	 */
6129 	ice_service_task_schedule(vsi->back);
6130 }
6131 
6132 /**
6133  * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
6134  * @netdev: network interface device structure
6135  * @queue_index: Queue ID
6136  * @maxrate: maximum bandwidth in Mbps
6137  */
6138 static int
ice_set_tx_maxrate(struct net_device * netdev,int queue_index,u32 maxrate)6139 ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
6140 {
6141 	struct ice_netdev_priv *np = netdev_priv(netdev);
6142 	struct ice_vsi *vsi = np->vsi;
6143 	u16 q_handle;
6144 	int status;
6145 	u8 tc;
6146 
6147 	/* Validate maxrate requested is within permitted range */
6148 	if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
6149 		netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
6150 			   maxrate, queue_index);
6151 		return -EINVAL;
6152 	}
6153 
6154 	q_handle = vsi->tx_rings[queue_index]->q_handle;
6155 	tc = ice_dcb_get_tc(vsi, queue_index);
6156 
6157 	vsi = ice_locate_vsi_using_queue(vsi, queue_index);
6158 	if (!vsi) {
6159 		netdev_err(netdev, "Invalid VSI for given queue %d\n",
6160 			   queue_index);
6161 		return -EINVAL;
6162 	}
6163 
6164 	/* Set BW back to default, when user set maxrate to 0 */
6165 	if (!maxrate)
6166 		status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
6167 					       q_handle, ICE_MAX_BW);
6168 	else
6169 		status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
6170 					  q_handle, ICE_MAX_BW, maxrate * 1000);
6171 	if (status)
6172 		netdev_err(netdev, "Unable to set Tx max rate, error %d\n",
6173 			   status);
6174 
6175 	return status;
6176 }
6177 
6178 /**
6179  * ice_fdb_add - add an entry to the hardware database
6180  * @ndm: the input from the stack
6181  * @tb: pointer to array of nladdr (unused)
6182  * @dev: the net device pointer
6183  * @addr: the MAC address entry being added
6184  * @vid: VLAN ID
6185  * @flags: instructions from stack about fdb operation
6186  * @notified: whether notification was emitted
6187  * @extack: netlink extended ack
6188  */
6189 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)6190 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
6191 	    struct net_device *dev, const unsigned char *addr, u16 vid,
6192 	    u16 flags, bool *notified,
6193 	    struct netlink_ext_ack __always_unused *extack)
6194 {
6195 	int err;
6196 
6197 	if (vid) {
6198 		netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
6199 		return -EINVAL;
6200 	}
6201 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6202 		netdev_err(dev, "FDB only supports static addresses\n");
6203 		return -EINVAL;
6204 	}
6205 
6206 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
6207 		err = dev_uc_add_excl(dev, addr);
6208 	else if (is_multicast_ether_addr(addr))
6209 		err = dev_mc_add_excl(dev, addr);
6210 	else
6211 		err = -EINVAL;
6212 
6213 	/* Only return duplicate errors if NLM_F_EXCL is set */
6214 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
6215 		err = 0;
6216 
6217 	return err;
6218 }
6219 
6220 /**
6221  * ice_fdb_del - delete an entry from the hardware database
6222  * @ndm: the input from the stack
6223  * @tb: pointer to array of nladdr (unused)
6224  * @dev: the net device pointer
6225  * @addr: the MAC address entry being added
6226  * @vid: VLAN ID
6227  * @notified: whether notification was emitted
6228  * @extack: netlink extended ack
6229  */
6230 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)6231 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
6232 	    struct net_device *dev, const unsigned char *addr,
6233 	    __always_unused u16 vid, bool *notified,
6234 	    struct netlink_ext_ack *extack)
6235 {
6236 	int err;
6237 
6238 	if (ndm->ndm_state & NUD_PERMANENT) {
6239 		netdev_err(dev, "FDB only supports static addresses\n");
6240 		return -EINVAL;
6241 	}
6242 
6243 	if (is_unicast_ether_addr(addr))
6244 		err = dev_uc_del(dev, addr);
6245 	else if (is_multicast_ether_addr(addr))
6246 		err = dev_mc_del(dev, addr);
6247 	else
6248 		err = -EINVAL;
6249 
6250 	return err;
6251 }
6252 
6253 #define NETIF_VLAN_OFFLOAD_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
6254 					 NETIF_F_HW_VLAN_CTAG_TX | \
6255 					 NETIF_F_HW_VLAN_STAG_RX | \
6256 					 NETIF_F_HW_VLAN_STAG_TX)
6257 
6258 #define NETIF_VLAN_STRIPPING_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
6259 					 NETIF_F_HW_VLAN_STAG_RX)
6260 
6261 #define NETIF_VLAN_FILTERING_FEATURES	(NETIF_F_HW_VLAN_CTAG_FILTER | \
6262 					 NETIF_F_HW_VLAN_STAG_FILTER)
6263 
6264 /**
6265  * ice_fix_features - fix the netdev features flags based on device limitations
6266  * @netdev: ptr to the netdev that flags are being fixed on
6267  * @features: features that need to be checked and possibly fixed
6268  *
6269  * Make sure any fixups are made to features in this callback. This enables the
6270  * driver to not have to check unsupported configurations throughout the driver
6271  * because that's the responsiblity of this callback.
6272  *
6273  * Single VLAN Mode (SVM) Supported Features:
6274  *	NETIF_F_HW_VLAN_CTAG_FILTER
6275  *	NETIF_F_HW_VLAN_CTAG_RX
6276  *	NETIF_F_HW_VLAN_CTAG_TX
6277  *
6278  * Double VLAN Mode (DVM) Supported Features:
6279  *	NETIF_F_HW_VLAN_CTAG_FILTER
6280  *	NETIF_F_HW_VLAN_CTAG_RX
6281  *	NETIF_F_HW_VLAN_CTAG_TX
6282  *
6283  *	NETIF_F_HW_VLAN_STAG_FILTER
6284  *	NETIF_HW_VLAN_STAG_RX
6285  *	NETIF_HW_VLAN_STAG_TX
6286  *
6287  * Features that need fixing:
6288  *	Cannot simultaneously enable CTAG and STAG stripping and/or insertion.
6289  *	These are mutually exlusive as the VSI context cannot support multiple
6290  *	VLAN ethertypes simultaneously for stripping and/or insertion. If this
6291  *	is not done, then default to clearing the requested STAG offload
6292  *	settings.
6293  *
6294  *	All supported filtering has to be enabled or disabled together. For
6295  *	example, in DVM, CTAG and STAG filtering have to be enabled and disabled
6296  *	together. If this is not done, then default to VLAN filtering disabled.
6297  *	These are mutually exclusive as there is currently no way to
6298  *	enable/disable VLAN filtering based on VLAN ethertype when using VLAN
6299  *	prune rules.
6300  */
6301 static netdev_features_t
ice_fix_features(struct net_device * netdev,netdev_features_t features)6302 ice_fix_features(struct net_device *netdev, netdev_features_t features)
6303 {
6304 	struct ice_netdev_priv *np = netdev_priv(netdev);
6305 	netdev_features_t req_vlan_fltr, cur_vlan_fltr;
6306 	bool cur_ctag, cur_stag, req_ctag, req_stag;
6307 
6308 	cur_vlan_fltr = netdev->features & NETIF_VLAN_FILTERING_FEATURES;
6309 	cur_ctag = cur_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6310 	cur_stag = cur_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
6311 
6312 	req_vlan_fltr = features & NETIF_VLAN_FILTERING_FEATURES;
6313 	req_ctag = req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6314 	req_stag = req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
6315 
6316 	if (req_vlan_fltr != cur_vlan_fltr) {
6317 		if (ice_is_dvm_ena(&np->vsi->back->hw)) {
6318 			if (req_ctag && req_stag) {
6319 				features |= NETIF_VLAN_FILTERING_FEATURES;
6320 			} else if (!req_ctag && !req_stag) {
6321 				features &= ~NETIF_VLAN_FILTERING_FEATURES;
6322 			} else if ((!cur_ctag && req_ctag && !cur_stag) ||
6323 				   (!cur_stag && req_stag && !cur_ctag)) {
6324 				features |= NETIF_VLAN_FILTERING_FEATURES;
6325 				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");
6326 			} else if ((cur_ctag && !req_ctag && cur_stag) ||
6327 				   (cur_stag && !req_stag && cur_ctag)) {
6328 				features &= ~NETIF_VLAN_FILTERING_FEATURES;
6329 				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");
6330 			}
6331 		} else {
6332 			if (req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER)
6333 				netdev_warn(netdev, "cannot support requested 802.1ad filtering setting in SVM mode\n");
6334 
6335 			if (req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER)
6336 				features |= NETIF_F_HW_VLAN_CTAG_FILTER;
6337 		}
6338 	}
6339 
6340 	if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
6341 	    (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))) {
6342 		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");
6343 		features &= ~(NETIF_F_HW_VLAN_STAG_RX |
6344 			      NETIF_F_HW_VLAN_STAG_TX);
6345 	}
6346 
6347 	if (!(netdev->features & NETIF_F_RXFCS) &&
6348 	    (features & NETIF_F_RXFCS) &&
6349 	    (features & NETIF_VLAN_STRIPPING_FEATURES) &&
6350 	    !ice_vsi_has_non_zero_vlans(np->vsi)) {
6351 		netdev_warn(netdev, "Disabling VLAN stripping as FCS/CRC stripping is also disabled and there is no VLAN configured\n");
6352 		features &= ~NETIF_VLAN_STRIPPING_FEATURES;
6353 	}
6354 
6355 	return features;
6356 }
6357 
6358 /**
6359  * ice_set_rx_rings_vlan_proto - update rings with new stripped VLAN proto
6360  * @vsi: PF's VSI
6361  * @vlan_ethertype: VLAN ethertype (802.1Q or 802.1ad) in network byte order
6362  *
6363  * Store current stripped VLAN proto in ring packet context,
6364  * so it can be accessed more efficiently by packet processing code.
6365  */
6366 static void
ice_set_rx_rings_vlan_proto(struct ice_vsi * vsi,__be16 vlan_ethertype)6367 ice_set_rx_rings_vlan_proto(struct ice_vsi *vsi, __be16 vlan_ethertype)
6368 {
6369 	u16 i;
6370 
6371 	ice_for_each_alloc_rxq(vsi, i)
6372 		vsi->rx_rings[i]->pkt_ctx.vlan_proto = vlan_ethertype;
6373 }
6374 
6375 /**
6376  * ice_set_vlan_offload_features - set VLAN offload features for the PF VSI
6377  * @vsi: PF's VSI
6378  * @features: features used to determine VLAN offload settings
6379  *
6380  * First, determine the vlan_ethertype based on the VLAN offload bits in
6381  * features. Then determine if stripping and insertion should be enabled or
6382  * disabled. Finally enable or disable VLAN stripping and insertion.
6383  */
6384 static int
ice_set_vlan_offload_features(struct ice_vsi * vsi,netdev_features_t features)6385 ice_set_vlan_offload_features(struct ice_vsi *vsi, netdev_features_t features)
6386 {
6387 	bool enable_stripping = true, enable_insertion = true;
6388 	struct ice_vsi_vlan_ops *vlan_ops;
6389 	int strip_err = 0, insert_err = 0;
6390 	u16 vlan_ethertype = 0;
6391 
6392 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6393 
6394 	if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
6395 		vlan_ethertype = ETH_P_8021AD;
6396 	else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
6397 		vlan_ethertype = ETH_P_8021Q;
6398 
6399 	if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
6400 		enable_stripping = false;
6401 	if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
6402 		enable_insertion = false;
6403 
6404 	if (enable_stripping)
6405 		strip_err = vlan_ops->ena_stripping(vsi, vlan_ethertype);
6406 	else
6407 		strip_err = vlan_ops->dis_stripping(vsi);
6408 
6409 	if (enable_insertion)
6410 		insert_err = vlan_ops->ena_insertion(vsi, vlan_ethertype);
6411 	else
6412 		insert_err = vlan_ops->dis_insertion(vsi);
6413 
6414 	if (strip_err || insert_err)
6415 		return -EIO;
6416 
6417 	ice_set_rx_rings_vlan_proto(vsi, enable_stripping ?
6418 				    htons(vlan_ethertype) : 0);
6419 
6420 	return 0;
6421 }
6422 
6423 /**
6424  * ice_set_vlan_filtering_features - set VLAN filtering features for the PF VSI
6425  * @vsi: PF's VSI
6426  * @features: features used to determine VLAN filtering settings
6427  *
6428  * Enable or disable Rx VLAN filtering based on the VLAN filtering bits in the
6429  * features.
6430  */
6431 static int
ice_set_vlan_filtering_features(struct ice_vsi * vsi,netdev_features_t features)6432 ice_set_vlan_filtering_features(struct ice_vsi *vsi, netdev_features_t features)
6433 {
6434 	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6435 	int err = 0;
6436 
6437 	/* support Single VLAN Mode (SVM) and Double VLAN Mode (DVM) by checking
6438 	 * if either bit is set. In switchdev mode Rx filtering should never be
6439 	 * enabled.
6440 	 */
6441 	if ((features &
6442 	     (NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)) &&
6443 	     !ice_is_eswitch_mode_switchdev(vsi->back))
6444 		err = vlan_ops->ena_rx_filtering(vsi);
6445 	else
6446 		err = vlan_ops->dis_rx_filtering(vsi);
6447 
6448 	return err;
6449 }
6450 
6451 /**
6452  * ice_set_vlan_features - set VLAN settings based on suggested feature set
6453  * @netdev: ptr to the netdev being adjusted
6454  * @features: the feature set that the stack is suggesting
6455  *
6456  * Only update VLAN settings if the requested_vlan_features are different than
6457  * the current_vlan_features.
6458  */
6459 static int
ice_set_vlan_features(struct net_device * netdev,netdev_features_t features)6460 ice_set_vlan_features(struct net_device *netdev, netdev_features_t features)
6461 {
6462 	netdev_features_t current_vlan_features, requested_vlan_features;
6463 	struct ice_netdev_priv *np = netdev_priv(netdev);
6464 	struct ice_vsi *vsi = np->vsi;
6465 	int err;
6466 
6467 	current_vlan_features = netdev->features & NETIF_VLAN_OFFLOAD_FEATURES;
6468 	requested_vlan_features = features & NETIF_VLAN_OFFLOAD_FEATURES;
6469 	if (current_vlan_features ^ requested_vlan_features) {
6470 		if ((features & NETIF_F_RXFCS) &&
6471 		    (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6472 			dev_err(ice_pf_to_dev(vsi->back),
6473 				"To enable VLAN stripping, you must first enable FCS/CRC stripping\n");
6474 			return -EIO;
6475 		}
6476 
6477 		err = ice_set_vlan_offload_features(vsi, features);
6478 		if (err)
6479 			return err;
6480 	}
6481 
6482 	current_vlan_features = netdev->features &
6483 		NETIF_VLAN_FILTERING_FEATURES;
6484 	requested_vlan_features = features & NETIF_VLAN_FILTERING_FEATURES;
6485 	if (current_vlan_features ^ requested_vlan_features) {
6486 		err = ice_set_vlan_filtering_features(vsi, features);
6487 		if (err)
6488 			return err;
6489 	}
6490 
6491 	return 0;
6492 }
6493 
6494 /**
6495  * ice_set_loopback - turn on/off loopback mode on underlying PF
6496  * @vsi: ptr to VSI
6497  * @ena: flag to indicate the on/off setting
6498  */
ice_set_loopback(struct ice_vsi * vsi,bool ena)6499 static int ice_set_loopback(struct ice_vsi *vsi, bool ena)
6500 {
6501 	bool if_running = netif_running(vsi->netdev);
6502 	int ret;
6503 
6504 	if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
6505 		ret = ice_down(vsi);
6506 		if (ret) {
6507 			netdev_err(vsi->netdev, "Preparing device to toggle loopback failed\n");
6508 			return ret;
6509 		}
6510 	}
6511 	ret = ice_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
6512 	if (ret)
6513 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
6514 	if (if_running)
6515 		ret = ice_up(vsi);
6516 
6517 	return ret;
6518 }
6519 
6520 /**
6521  * ice_set_features - set the netdev feature flags
6522  * @netdev: ptr to the netdev being adjusted
6523  * @features: the feature set that the stack is suggesting
6524  */
6525 static int
ice_set_features(struct net_device * netdev,netdev_features_t features)6526 ice_set_features(struct net_device *netdev, netdev_features_t features)
6527 {
6528 	netdev_features_t changed = netdev->features ^ features;
6529 	struct ice_netdev_priv *np = netdev_priv(netdev);
6530 	struct ice_vsi *vsi = np->vsi;
6531 	struct ice_pf *pf = vsi->back;
6532 	int ret = 0;
6533 
6534 	/* Don't set any netdev advanced features with device in Safe Mode */
6535 	if (ice_is_safe_mode(pf)) {
6536 		dev_err(ice_pf_to_dev(pf),
6537 			"Device is in Safe Mode - not enabling advanced netdev features\n");
6538 		return ret;
6539 	}
6540 
6541 	/* Do not change setting during reset */
6542 	if (ice_is_reset_in_progress(pf->state)) {
6543 		dev_err(ice_pf_to_dev(pf),
6544 			"Device is resetting, changing advanced netdev features temporarily unavailable.\n");
6545 		return -EBUSY;
6546 	}
6547 
6548 	/* Multiple features can be changed in one call so keep features in
6549 	 * separate if/else statements to guarantee each feature is checked
6550 	 */
6551 	if (changed & NETIF_F_RXHASH)
6552 		ice_vsi_manage_rss_lut(vsi, !!(features & NETIF_F_RXHASH));
6553 
6554 	ret = ice_set_vlan_features(netdev, features);
6555 	if (ret)
6556 		return ret;
6557 
6558 	/* Turn on receive of FCS aka CRC, and after setting this
6559 	 * flag the packet data will have the 4 byte CRC appended
6560 	 */
6561 	if (changed & NETIF_F_RXFCS) {
6562 		if ((features & NETIF_F_RXFCS) &&
6563 		    (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6564 			dev_err(ice_pf_to_dev(vsi->back),
6565 				"To disable FCS/CRC stripping, you must first disable VLAN stripping\n");
6566 			return -EIO;
6567 		}
6568 
6569 		ice_vsi_cfg_crc_strip(vsi, !!(features & NETIF_F_RXFCS));
6570 		ret = ice_down_up(vsi);
6571 		if (ret)
6572 			return ret;
6573 	}
6574 
6575 	if (changed & NETIF_F_NTUPLE) {
6576 		bool ena = !!(features & NETIF_F_NTUPLE);
6577 
6578 		ice_vsi_manage_fdir(vsi, ena);
6579 		ena ? ice_init_arfs(vsi) : ice_clear_arfs(vsi);
6580 	}
6581 
6582 	/* don't turn off hw_tc_offload when ADQ is already enabled */
6583 	if (!(features & NETIF_F_HW_TC) && ice_is_adq_active(pf)) {
6584 		dev_err(ice_pf_to_dev(pf), "ADQ is active, can't turn hw_tc_offload off\n");
6585 		return -EACCES;
6586 	}
6587 
6588 	if (changed & NETIF_F_HW_TC) {
6589 		bool ena = !!(features & NETIF_F_HW_TC);
6590 
6591 		assign_bit(ICE_FLAG_CLS_FLOWER, pf->flags, ena);
6592 	}
6593 
6594 	if (changed & NETIF_F_LOOPBACK)
6595 		ret = ice_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
6596 
6597 	/* Due to E830 hardware limitations, TSO (NETIF_F_ALL_TSO) with GCS
6598 	 * (NETIF_F_HW_CSUM) is not supported.
6599 	 */
6600 	if (ice_is_feature_supported(pf, ICE_F_GCS) &&
6601 	    ((features & NETIF_F_HW_CSUM) && (features & NETIF_F_ALL_TSO))) {
6602 		if (netdev->features & NETIF_F_HW_CSUM)
6603 			dev_err(ice_pf_to_dev(pf), "To enable TSO, you must first disable HW checksum.\n");
6604 		else
6605 			dev_err(ice_pf_to_dev(pf), "To enable HW checksum, you must first disable TSO.\n");
6606 		return -EIO;
6607 	}
6608 
6609 	return ret;
6610 }
6611 
6612 /**
6613  * ice_vsi_vlan_setup - Setup VLAN offload properties on a PF VSI
6614  * @vsi: VSI to setup VLAN properties for
6615  */
ice_vsi_vlan_setup(struct ice_vsi * vsi)6616 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
6617 {
6618 	int err;
6619 
6620 	err = ice_set_vlan_offload_features(vsi, vsi->netdev->features);
6621 	if (err)
6622 		return err;
6623 
6624 	err = ice_set_vlan_filtering_features(vsi, vsi->netdev->features);
6625 	if (err)
6626 		return err;
6627 
6628 	return ice_vsi_add_vlan_zero(vsi);
6629 }
6630 
6631 /**
6632  * ice_vsi_cfg_lan - Setup the VSI lan related config
6633  * @vsi: the VSI being configured
6634  *
6635  * Return 0 on success and negative value on error
6636  */
ice_vsi_cfg_lan(struct ice_vsi * vsi)6637 int ice_vsi_cfg_lan(struct ice_vsi *vsi)
6638 {
6639 	int err;
6640 
6641 	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
6642 		ice_set_rx_mode(vsi->netdev);
6643 
6644 		err = ice_vsi_vlan_setup(vsi);
6645 		if (err)
6646 			return err;
6647 	}
6648 	ice_vsi_cfg_dcb_rings(vsi);
6649 
6650 	err = ice_vsi_cfg_lan_txqs(vsi);
6651 	if (!err && ice_is_xdp_ena_vsi(vsi))
6652 		err = ice_vsi_cfg_xdp_txqs(vsi);
6653 	if (!err)
6654 		err = ice_vsi_cfg_rxqs(vsi);
6655 
6656 	return err;
6657 }
6658 
6659 /* THEORY OF MODERATION:
6660  * The ice driver hardware works differently than the hardware that DIMLIB was
6661  * originally made for. ice hardware doesn't have packet count limits that
6662  * can trigger an interrupt, but it *does* have interrupt rate limit support,
6663  * which is hard-coded to a limit of 250,000 ints/second.
6664  * If not using dynamic moderation, the INTRL value can be modified
6665  * by ethtool rx-usecs-high.
6666  */
6667 struct ice_dim {
6668 	/* the throttle rate for interrupts, basically worst case delay before
6669 	 * an initial interrupt fires, value is stored in microseconds.
6670 	 */
6671 	u16 itr;
6672 };
6673 
6674 /* Make a different profile for Rx that doesn't allow quite so aggressive
6675  * moderation at the high end (it maxes out at 126us or about 8k interrupts a
6676  * second.
6677  */
6678 static const struct ice_dim rx_profile[] = {
6679 	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6680 	{8},    /* 125,000 ints/s */
6681 	{16},   /*  62,500 ints/s */
6682 	{62},   /*  16,129 ints/s */
6683 	{126}   /*   7,936 ints/s */
6684 };
6685 
6686 /* The transmit profile, which has the same sorts of values
6687  * as the previous struct
6688  */
6689 static const struct ice_dim tx_profile[] = {
6690 	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6691 	{8},    /* 125,000 ints/s */
6692 	{40},   /*  16,125 ints/s */
6693 	{128},  /*   7,812 ints/s */
6694 	{256}   /*   3,906 ints/s */
6695 };
6696 
ice_tx_dim_work(struct work_struct * work)6697 static void ice_tx_dim_work(struct work_struct *work)
6698 {
6699 	struct ice_ring_container *rc;
6700 	struct dim *dim;
6701 	u16 itr;
6702 
6703 	dim = container_of(work, struct dim, work);
6704 	rc = dim->priv;
6705 
6706 	WARN_ON(dim->profile_ix >= ARRAY_SIZE(tx_profile));
6707 
6708 	/* look up the values in our local table */
6709 	itr = tx_profile[dim->profile_ix].itr;
6710 
6711 	ice_trace(tx_dim_work, container_of(rc, struct ice_q_vector, tx), dim);
6712 	ice_write_itr(rc, itr);
6713 
6714 	dim->state = DIM_START_MEASURE;
6715 }
6716 
ice_rx_dim_work(struct work_struct * work)6717 static void ice_rx_dim_work(struct work_struct *work)
6718 {
6719 	struct ice_ring_container *rc;
6720 	struct dim *dim;
6721 	u16 itr;
6722 
6723 	dim = container_of(work, struct dim, work);
6724 	rc = dim->priv;
6725 
6726 	WARN_ON(dim->profile_ix >= ARRAY_SIZE(rx_profile));
6727 
6728 	/* look up the values in our local table */
6729 	itr = rx_profile[dim->profile_ix].itr;
6730 
6731 	ice_trace(rx_dim_work, container_of(rc, struct ice_q_vector, rx), dim);
6732 	ice_write_itr(rc, itr);
6733 
6734 	dim->state = DIM_START_MEASURE;
6735 }
6736 
6737 #define ICE_DIM_DEFAULT_PROFILE_IX 1
6738 
6739 /**
6740  * ice_init_moderation - set up interrupt moderation
6741  * @q_vector: the vector containing rings to be configured
6742  *
6743  * Set up interrupt moderation registers, with the intent to do the right thing
6744  * when called from reset or from probe, and whether or not dynamic moderation
6745  * is enabled or not. Take special care to write all the registers in both
6746  * dynamic moderation mode or not in order to make sure hardware is in a known
6747  * state.
6748  */
ice_init_moderation(struct ice_q_vector * q_vector)6749 static void ice_init_moderation(struct ice_q_vector *q_vector)
6750 {
6751 	struct ice_ring_container *rc;
6752 	bool tx_dynamic, rx_dynamic;
6753 
6754 	rc = &q_vector->tx;
6755 	INIT_WORK(&rc->dim.work, ice_tx_dim_work);
6756 	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6757 	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6758 	rc->dim.priv = rc;
6759 	tx_dynamic = ITR_IS_DYNAMIC(rc);
6760 
6761 	/* set the initial TX ITR to match the above */
6762 	ice_write_itr(rc, tx_dynamic ?
6763 		      tx_profile[rc->dim.profile_ix].itr : rc->itr_setting);
6764 
6765 	rc = &q_vector->rx;
6766 	INIT_WORK(&rc->dim.work, ice_rx_dim_work);
6767 	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6768 	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6769 	rc->dim.priv = rc;
6770 	rx_dynamic = ITR_IS_DYNAMIC(rc);
6771 
6772 	/* set the initial RX ITR to match the above */
6773 	ice_write_itr(rc, rx_dynamic ? rx_profile[rc->dim.profile_ix].itr :
6774 				       rc->itr_setting);
6775 
6776 	ice_set_q_vector_intrl(q_vector);
6777 }
6778 
6779 /**
6780  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
6781  * @vsi: the VSI being configured
6782  */
ice_napi_enable_all(struct ice_vsi * vsi)6783 static void ice_napi_enable_all(struct ice_vsi *vsi)
6784 {
6785 	int q_idx;
6786 
6787 	if (!vsi->netdev)
6788 		return;
6789 
6790 	ice_for_each_q_vector(vsi, q_idx) {
6791 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6792 
6793 		ice_init_moderation(q_vector);
6794 
6795 		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6796 			napi_enable(&q_vector->napi);
6797 	}
6798 }
6799 
6800 /**
6801  * ice_up_complete - Finish the last steps of bringing up a connection
6802  * @vsi: The VSI being configured
6803  *
6804  * Return 0 on success and negative value on error
6805  */
ice_up_complete(struct ice_vsi * vsi)6806 static int ice_up_complete(struct ice_vsi *vsi)
6807 {
6808 	struct ice_pf *pf = vsi->back;
6809 	int err;
6810 
6811 	ice_vsi_cfg_msix(vsi);
6812 
6813 	/* Enable only Rx rings, Tx rings were enabled by the FW when the
6814 	 * Tx queue group list was configured and the context bits were
6815 	 * programmed using ice_vsi_cfg_txqs
6816 	 */
6817 	err = ice_vsi_start_all_rx_rings(vsi);
6818 	if (err)
6819 		return err;
6820 
6821 	clear_bit(ICE_VSI_DOWN, vsi->state);
6822 	ice_napi_enable_all(vsi);
6823 	ice_vsi_ena_irq(vsi);
6824 
6825 	if (vsi->port_info &&
6826 	    (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
6827 	    ((vsi->netdev && (vsi->type == ICE_VSI_PF ||
6828 			      vsi->type == ICE_VSI_SF)))) {
6829 		ice_print_link_msg(vsi, true);
6830 		netif_tx_start_all_queues(vsi->netdev);
6831 		netif_carrier_on(vsi->netdev);
6832 		ice_ptp_link_change(pf, true);
6833 	}
6834 
6835 	/* Perform an initial read of the statistics registers now to
6836 	 * set the baseline so counters are ready when interface is up
6837 	 */
6838 	ice_update_eth_stats(vsi);
6839 
6840 	if (vsi->type == ICE_VSI_PF)
6841 		ice_service_task_schedule(pf);
6842 
6843 	return 0;
6844 }
6845 
6846 /**
6847  * ice_up - Bring the connection back up after being down
6848  * @vsi: VSI being configured
6849  */
ice_up(struct ice_vsi * vsi)6850 int ice_up(struct ice_vsi *vsi)
6851 {
6852 	int err;
6853 
6854 	err = ice_vsi_cfg_lan(vsi);
6855 	if (!err)
6856 		err = ice_up_complete(vsi);
6857 
6858 	return err;
6859 }
6860 
6861 /**
6862  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
6863  * @syncp: pointer to u64_stats_sync
6864  * @stats: stats that pkts and bytes count will be taken from
6865  * @pkts: packets stats counter
6866  * @bytes: bytes stats counter
6867  *
6868  * This function fetches stats from the ring considering the atomic operations
6869  * that needs to be performed to read u64 values in 32 bit machine.
6870  */
6871 void
ice_fetch_u64_stats_per_ring(struct u64_stats_sync * syncp,struct ice_q_stats stats,u64 * pkts,u64 * bytes)6872 ice_fetch_u64_stats_per_ring(struct u64_stats_sync *syncp,
6873 			     struct ice_q_stats stats, u64 *pkts, u64 *bytes)
6874 {
6875 	unsigned int start;
6876 
6877 	do {
6878 		start = u64_stats_fetch_begin(syncp);
6879 		*pkts = stats.pkts;
6880 		*bytes = stats.bytes;
6881 	} while (u64_stats_fetch_retry(syncp, start));
6882 }
6883 
6884 /**
6885  * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
6886  * @vsi: the VSI to be updated
6887  * @vsi_stats: the stats struct to be updated
6888  * @rings: rings to work on
6889  * @count: number of rings
6890  */
6891 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)6892 ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi,
6893 			     struct rtnl_link_stats64 *vsi_stats,
6894 			     struct ice_tx_ring **rings, u16 count)
6895 {
6896 	u16 i;
6897 
6898 	for (i = 0; i < count; i++) {
6899 		struct ice_tx_ring *ring;
6900 		u64 pkts = 0, bytes = 0;
6901 
6902 		ring = READ_ONCE(rings[i]);
6903 		if (!ring || !ring->ring_stats)
6904 			continue;
6905 		ice_fetch_u64_stats_per_ring(&ring->ring_stats->syncp,
6906 					     ring->ring_stats->stats, &pkts,
6907 					     &bytes);
6908 		vsi_stats->tx_packets += pkts;
6909 		vsi_stats->tx_bytes += bytes;
6910 		vsi->tx_restart += ring->ring_stats->tx_stats.restart_q;
6911 		vsi->tx_busy += ring->ring_stats->tx_stats.tx_busy;
6912 		vsi->tx_linearize += ring->ring_stats->tx_stats.tx_linearize;
6913 	}
6914 }
6915 
6916 /**
6917  * ice_update_vsi_ring_stats - Update VSI stats counters
6918  * @vsi: the VSI to be updated
6919  */
ice_update_vsi_ring_stats(struct ice_vsi * vsi)6920 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
6921 {
6922 	struct rtnl_link_stats64 *net_stats, *stats_prev;
6923 	struct rtnl_link_stats64 *vsi_stats;
6924 	struct ice_pf *pf = vsi->back;
6925 	u64 pkts, bytes;
6926 	int i;
6927 
6928 	vsi_stats = kzalloc(sizeof(*vsi_stats), GFP_ATOMIC);
6929 	if (!vsi_stats)
6930 		return;
6931 
6932 	/* reset non-netdev (extended) stats */
6933 	vsi->tx_restart = 0;
6934 	vsi->tx_busy = 0;
6935 	vsi->tx_linearize = 0;
6936 	vsi->rx_buf_failed = 0;
6937 	vsi->rx_page_failed = 0;
6938 
6939 	rcu_read_lock();
6940 
6941 	/* update Tx rings counters */
6942 	ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->tx_rings,
6943 				     vsi->num_txq);
6944 
6945 	/* update Rx rings counters */
6946 	ice_for_each_rxq(vsi, i) {
6947 		struct ice_rx_ring *ring = READ_ONCE(vsi->rx_rings[i]);
6948 		struct ice_ring_stats *ring_stats;
6949 
6950 		ring_stats = ring->ring_stats;
6951 		ice_fetch_u64_stats_per_ring(&ring_stats->syncp,
6952 					     ring_stats->stats, &pkts,
6953 					     &bytes);
6954 		vsi_stats->rx_packets += pkts;
6955 		vsi_stats->rx_bytes += bytes;
6956 		vsi->rx_buf_failed += ring_stats->rx_stats.alloc_buf_failed;
6957 		vsi->rx_page_failed += ring_stats->rx_stats.alloc_page_failed;
6958 	}
6959 
6960 	/* update XDP Tx rings counters */
6961 	if (ice_is_xdp_ena_vsi(vsi))
6962 		ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->xdp_rings,
6963 					     vsi->num_xdp_txq);
6964 
6965 	rcu_read_unlock();
6966 
6967 	net_stats = &vsi->net_stats;
6968 	stats_prev = &vsi->net_stats_prev;
6969 
6970 	/* Update netdev counters, but keep in mind that values could start at
6971 	 * random value after PF reset. And as we increase the reported stat by
6972 	 * diff of Prev-Cur, we need to be sure that Prev is valid. If it's not,
6973 	 * let's skip this round.
6974 	 */
6975 	if (likely(pf->stat_prev_loaded)) {
6976 		net_stats->tx_packets += vsi_stats->tx_packets - stats_prev->tx_packets;
6977 		net_stats->tx_bytes += vsi_stats->tx_bytes - stats_prev->tx_bytes;
6978 		net_stats->rx_packets += vsi_stats->rx_packets - stats_prev->rx_packets;
6979 		net_stats->rx_bytes += vsi_stats->rx_bytes - stats_prev->rx_bytes;
6980 	}
6981 
6982 	stats_prev->tx_packets = vsi_stats->tx_packets;
6983 	stats_prev->tx_bytes = vsi_stats->tx_bytes;
6984 	stats_prev->rx_packets = vsi_stats->rx_packets;
6985 	stats_prev->rx_bytes = vsi_stats->rx_bytes;
6986 
6987 	kfree(vsi_stats);
6988 }
6989 
6990 /**
6991  * ice_update_vsi_stats - Update VSI stats counters
6992  * @vsi: the VSI to be updated
6993  */
ice_update_vsi_stats(struct ice_vsi * vsi)6994 void ice_update_vsi_stats(struct ice_vsi *vsi)
6995 {
6996 	struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
6997 	struct ice_eth_stats *cur_es = &vsi->eth_stats;
6998 	struct ice_pf *pf = vsi->back;
6999 
7000 	if (test_bit(ICE_VSI_DOWN, vsi->state) ||
7001 	    test_bit(ICE_CFG_BUSY, pf->state))
7002 		return;
7003 
7004 	/* get stats as recorded by Tx/Rx rings */
7005 	ice_update_vsi_ring_stats(vsi);
7006 
7007 	/* get VSI stats as recorded by the hardware */
7008 	ice_update_eth_stats(vsi);
7009 
7010 	cur_ns->tx_errors = cur_es->tx_errors;
7011 	cur_ns->rx_dropped = cur_es->rx_discards;
7012 	cur_ns->tx_dropped = cur_es->tx_discards;
7013 	cur_ns->multicast = cur_es->rx_multicast;
7014 
7015 	/* update some more netdev stats if this is main VSI */
7016 	if (vsi->type == ICE_VSI_PF) {
7017 		cur_ns->rx_crc_errors = pf->stats.crc_errors;
7018 		cur_ns->rx_errors = pf->stats.crc_errors +
7019 				    pf->stats.illegal_bytes +
7020 				    pf->stats.rx_undersize +
7021 				    pf->hw_csum_rx_error +
7022 				    pf->stats.rx_jabber +
7023 				    pf->stats.rx_fragments +
7024 				    pf->stats.rx_oversize;
7025 		/* record drops from the port level */
7026 		cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
7027 	}
7028 }
7029 
7030 /**
7031  * ice_update_pf_stats - Update PF port stats counters
7032  * @pf: PF whose stats needs to be updated
7033  */
ice_update_pf_stats(struct ice_pf * pf)7034 void ice_update_pf_stats(struct ice_pf *pf)
7035 {
7036 	struct ice_hw_port_stats *prev_ps, *cur_ps;
7037 	struct ice_hw *hw = &pf->hw;
7038 	u16 fd_ctr_base;
7039 	u8 port;
7040 
7041 	port = hw->port_info->lport;
7042 	prev_ps = &pf->stats_prev;
7043 	cur_ps = &pf->stats;
7044 
7045 	if (ice_is_reset_in_progress(pf->state))
7046 		pf->stat_prev_loaded = false;
7047 
7048 	ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
7049 			  &prev_ps->eth.rx_bytes,
7050 			  &cur_ps->eth.rx_bytes);
7051 
7052 	ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
7053 			  &prev_ps->eth.rx_unicast,
7054 			  &cur_ps->eth.rx_unicast);
7055 
7056 	ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
7057 			  &prev_ps->eth.rx_multicast,
7058 			  &cur_ps->eth.rx_multicast);
7059 
7060 	ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
7061 			  &prev_ps->eth.rx_broadcast,
7062 			  &cur_ps->eth.rx_broadcast);
7063 
7064 	ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
7065 			  &prev_ps->eth.rx_discards,
7066 			  &cur_ps->eth.rx_discards);
7067 
7068 	ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
7069 			  &prev_ps->eth.tx_bytes,
7070 			  &cur_ps->eth.tx_bytes);
7071 
7072 	ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
7073 			  &prev_ps->eth.tx_unicast,
7074 			  &cur_ps->eth.tx_unicast);
7075 
7076 	ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
7077 			  &prev_ps->eth.tx_multicast,
7078 			  &cur_ps->eth.tx_multicast);
7079 
7080 	ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
7081 			  &prev_ps->eth.tx_broadcast,
7082 			  &cur_ps->eth.tx_broadcast);
7083 
7084 	ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
7085 			  &prev_ps->tx_dropped_link_down,
7086 			  &cur_ps->tx_dropped_link_down);
7087 
7088 	ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
7089 			  &prev_ps->rx_size_64, &cur_ps->rx_size_64);
7090 
7091 	ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
7092 			  &prev_ps->rx_size_127, &cur_ps->rx_size_127);
7093 
7094 	ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
7095 			  &prev_ps->rx_size_255, &cur_ps->rx_size_255);
7096 
7097 	ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
7098 			  &prev_ps->rx_size_511, &cur_ps->rx_size_511);
7099 
7100 	ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
7101 			  &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
7102 
7103 	ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
7104 			  &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
7105 
7106 	ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
7107 			  &prev_ps->rx_size_big, &cur_ps->rx_size_big);
7108 
7109 	ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
7110 			  &prev_ps->tx_size_64, &cur_ps->tx_size_64);
7111 
7112 	ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
7113 			  &prev_ps->tx_size_127, &cur_ps->tx_size_127);
7114 
7115 	ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
7116 			  &prev_ps->tx_size_255, &cur_ps->tx_size_255);
7117 
7118 	ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
7119 			  &prev_ps->tx_size_511, &cur_ps->tx_size_511);
7120 
7121 	ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
7122 			  &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
7123 
7124 	ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
7125 			  &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
7126 
7127 	ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
7128 			  &prev_ps->tx_size_big, &cur_ps->tx_size_big);
7129 
7130 	fd_ctr_base = hw->fd_ctr_base;
7131 
7132 	ice_stat_update40(hw,
7133 			  GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
7134 			  pf->stat_prev_loaded, &prev_ps->fd_sb_match,
7135 			  &cur_ps->fd_sb_match);
7136 	ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
7137 			  &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
7138 
7139 	ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
7140 			  &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
7141 
7142 	ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
7143 			  &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
7144 
7145 	ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
7146 			  &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
7147 
7148 	ice_update_dcb_stats(pf);
7149 
7150 	ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
7151 			  &prev_ps->crc_errors, &cur_ps->crc_errors);
7152 
7153 	ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
7154 			  &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
7155 
7156 	ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
7157 			  &prev_ps->mac_local_faults,
7158 			  &cur_ps->mac_local_faults);
7159 
7160 	ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
7161 			  &prev_ps->mac_remote_faults,
7162 			  &cur_ps->mac_remote_faults);
7163 
7164 	ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
7165 			  &prev_ps->rx_undersize, &cur_ps->rx_undersize);
7166 
7167 	ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
7168 			  &prev_ps->rx_fragments, &cur_ps->rx_fragments);
7169 
7170 	ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
7171 			  &prev_ps->rx_oversize, &cur_ps->rx_oversize);
7172 
7173 	ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
7174 			  &prev_ps->rx_jabber, &cur_ps->rx_jabber);
7175 
7176 	cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
7177 
7178 	pf->stat_prev_loaded = true;
7179 }
7180 
7181 /**
7182  * ice_get_stats64 - get statistics for network device structure
7183  * @netdev: network interface device structure
7184  * @stats: main device statistics structure
7185  */
ice_get_stats64(struct net_device * netdev,struct rtnl_link_stats64 * stats)7186 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
7187 {
7188 	struct ice_netdev_priv *np = netdev_priv(netdev);
7189 	struct rtnl_link_stats64 *vsi_stats;
7190 	struct ice_vsi *vsi = np->vsi;
7191 
7192 	vsi_stats = &vsi->net_stats;
7193 
7194 	if (!vsi->num_txq || !vsi->num_rxq)
7195 		return;
7196 
7197 	/* netdev packet/byte stats come from ring counter. These are obtained
7198 	 * by summing up ring counters (done by ice_update_vsi_ring_stats).
7199 	 * But, only call the update routine and read the registers if VSI is
7200 	 * not down.
7201 	 */
7202 	if (!test_bit(ICE_VSI_DOWN, vsi->state))
7203 		ice_update_vsi_ring_stats(vsi);
7204 	stats->tx_packets = vsi_stats->tx_packets;
7205 	stats->tx_bytes = vsi_stats->tx_bytes;
7206 	stats->rx_packets = vsi_stats->rx_packets;
7207 	stats->rx_bytes = vsi_stats->rx_bytes;
7208 
7209 	/* The rest of the stats can be read from the hardware but instead we
7210 	 * just return values that the watchdog task has already obtained from
7211 	 * the hardware.
7212 	 */
7213 	stats->multicast = vsi_stats->multicast;
7214 	stats->tx_errors = vsi_stats->tx_errors;
7215 	stats->tx_dropped = vsi_stats->tx_dropped;
7216 	stats->rx_errors = vsi_stats->rx_errors;
7217 	stats->rx_dropped = vsi_stats->rx_dropped;
7218 	stats->rx_crc_errors = vsi_stats->rx_crc_errors;
7219 	stats->rx_length_errors = vsi_stats->rx_length_errors;
7220 }
7221 
7222 /**
7223  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
7224  * @vsi: VSI having NAPI disabled
7225  */
ice_napi_disable_all(struct ice_vsi * vsi)7226 static void ice_napi_disable_all(struct ice_vsi *vsi)
7227 {
7228 	int q_idx;
7229 
7230 	if (!vsi->netdev)
7231 		return;
7232 
7233 	ice_for_each_q_vector(vsi, q_idx) {
7234 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
7235 
7236 		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
7237 			napi_disable(&q_vector->napi);
7238 
7239 		cancel_work_sync(&q_vector->tx.dim.work);
7240 		cancel_work_sync(&q_vector->rx.dim.work);
7241 	}
7242 }
7243 
7244 /**
7245  * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
7246  * @vsi: the VSI being un-configured
7247  */
ice_vsi_dis_irq(struct ice_vsi * vsi)7248 static void ice_vsi_dis_irq(struct ice_vsi *vsi)
7249 {
7250 	struct ice_pf *pf = vsi->back;
7251 	struct ice_hw *hw = &pf->hw;
7252 	u32 val;
7253 	int i;
7254 
7255 	/* disable interrupt causation from each Rx queue; Tx queues are
7256 	 * handled in ice_vsi_stop_tx_ring()
7257 	 */
7258 	if (vsi->rx_rings) {
7259 		ice_for_each_rxq(vsi, i) {
7260 			if (vsi->rx_rings[i]) {
7261 				u16 reg;
7262 
7263 				reg = vsi->rx_rings[i]->reg_idx;
7264 				val = rd32(hw, QINT_RQCTL(reg));
7265 				val &= ~QINT_RQCTL_CAUSE_ENA_M;
7266 				wr32(hw, QINT_RQCTL(reg), val);
7267 			}
7268 		}
7269 	}
7270 
7271 	/* disable each interrupt */
7272 	ice_for_each_q_vector(vsi, i) {
7273 		if (!vsi->q_vectors[i])
7274 			continue;
7275 		wr32(hw, GLINT_DYN_CTL(vsi->q_vectors[i]->reg_idx), 0);
7276 	}
7277 
7278 	ice_flush(hw);
7279 
7280 	/* don't call synchronize_irq() for VF's from the host */
7281 	if (vsi->type == ICE_VSI_VF)
7282 		return;
7283 
7284 	ice_for_each_q_vector(vsi, i)
7285 		synchronize_irq(vsi->q_vectors[i]->irq.virq);
7286 }
7287 
7288 /**
7289  * ice_down - Shutdown the connection
7290  * @vsi: The VSI being stopped
7291  *
7292  * Caller of this function is expected to set the vsi->state ICE_DOWN bit
7293  */
ice_down(struct ice_vsi * vsi)7294 int ice_down(struct ice_vsi *vsi)
7295 {
7296 	int i, tx_err, rx_err, vlan_err = 0;
7297 
7298 	WARN_ON(!test_bit(ICE_VSI_DOWN, vsi->state));
7299 
7300 	if (vsi->netdev) {
7301 		vlan_err = ice_vsi_del_vlan_zero(vsi);
7302 		ice_ptp_link_change(vsi->back, false);
7303 		netif_carrier_off(vsi->netdev);
7304 		netif_tx_disable(vsi->netdev);
7305 	}
7306 
7307 	ice_vsi_dis_irq(vsi);
7308 
7309 	tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
7310 	if (tx_err)
7311 		netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
7312 			   vsi->vsi_num, tx_err);
7313 	if (!tx_err && vsi->xdp_rings) {
7314 		tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
7315 		if (tx_err)
7316 			netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
7317 				   vsi->vsi_num, tx_err);
7318 	}
7319 
7320 	rx_err = ice_vsi_stop_all_rx_rings(vsi);
7321 	if (rx_err)
7322 		netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
7323 			   vsi->vsi_num, rx_err);
7324 
7325 	ice_napi_disable_all(vsi);
7326 
7327 	ice_for_each_txq(vsi, i)
7328 		ice_clean_tx_ring(vsi->tx_rings[i]);
7329 
7330 	if (vsi->xdp_rings)
7331 		ice_for_each_xdp_txq(vsi, i)
7332 			ice_clean_tx_ring(vsi->xdp_rings[i]);
7333 
7334 	ice_for_each_rxq(vsi, i)
7335 		ice_clean_rx_ring(vsi->rx_rings[i]);
7336 
7337 	if (tx_err || rx_err || vlan_err) {
7338 		netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
7339 			   vsi->vsi_num, vsi->vsw->sw_id);
7340 		return -EIO;
7341 	}
7342 
7343 	return 0;
7344 }
7345 
7346 /**
7347  * ice_down_up - shutdown the VSI connection and bring it up
7348  * @vsi: the VSI to be reconnected
7349  */
ice_down_up(struct ice_vsi * vsi)7350 int ice_down_up(struct ice_vsi *vsi)
7351 {
7352 	int ret;
7353 
7354 	/* if DOWN already set, nothing to do */
7355 	if (test_and_set_bit(ICE_VSI_DOWN, vsi->state))
7356 		return 0;
7357 
7358 	ret = ice_down(vsi);
7359 	if (ret)
7360 		return ret;
7361 
7362 	ret = ice_up(vsi);
7363 	if (ret) {
7364 		netdev_err(vsi->netdev, "reallocating resources failed during netdev features change, may need to reload driver\n");
7365 		return ret;
7366 	}
7367 
7368 	return 0;
7369 }
7370 
7371 /**
7372  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
7373  * @vsi: VSI having resources allocated
7374  *
7375  * Return 0 on success, negative on failure
7376  */
ice_vsi_setup_tx_rings(struct ice_vsi * vsi)7377 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
7378 {
7379 	int i, err = 0;
7380 
7381 	if (!vsi->num_txq) {
7382 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
7383 			vsi->vsi_num);
7384 		return -EINVAL;
7385 	}
7386 
7387 	ice_for_each_txq(vsi, i) {
7388 		struct ice_tx_ring *ring = vsi->tx_rings[i];
7389 
7390 		if (!ring)
7391 			return -EINVAL;
7392 
7393 		if (vsi->netdev)
7394 			ring->netdev = vsi->netdev;
7395 		err = ice_setup_tx_ring(ring);
7396 		if (err)
7397 			break;
7398 	}
7399 
7400 	return err;
7401 }
7402 
7403 /**
7404  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
7405  * @vsi: VSI having resources allocated
7406  *
7407  * Return 0 on success, negative on failure
7408  */
ice_vsi_setup_rx_rings(struct ice_vsi * vsi)7409 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
7410 {
7411 	int i, err = 0;
7412 
7413 	if (!vsi->num_rxq) {
7414 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
7415 			vsi->vsi_num);
7416 		return -EINVAL;
7417 	}
7418 
7419 	ice_for_each_rxq(vsi, i) {
7420 		struct ice_rx_ring *ring = vsi->rx_rings[i];
7421 
7422 		if (!ring)
7423 			return -EINVAL;
7424 
7425 		if (vsi->netdev)
7426 			ring->netdev = vsi->netdev;
7427 		err = ice_setup_rx_ring(ring);
7428 		if (err)
7429 			break;
7430 	}
7431 
7432 	return err;
7433 }
7434 
7435 /**
7436  * ice_vsi_open_ctrl - open control VSI for use
7437  * @vsi: the VSI to open
7438  *
7439  * Initialization of the Control VSI
7440  *
7441  * Returns 0 on success, negative value on error
7442  */
ice_vsi_open_ctrl(struct ice_vsi * vsi)7443 int ice_vsi_open_ctrl(struct ice_vsi *vsi)
7444 {
7445 	char int_name[ICE_INT_NAME_STR_LEN];
7446 	struct ice_pf *pf = vsi->back;
7447 	struct device *dev;
7448 	int err;
7449 
7450 	dev = ice_pf_to_dev(pf);
7451 	/* allocate descriptors */
7452 	err = ice_vsi_setup_tx_rings(vsi);
7453 	if (err)
7454 		goto err_setup_tx;
7455 
7456 	err = ice_vsi_setup_rx_rings(vsi);
7457 	if (err)
7458 		goto err_setup_rx;
7459 
7460 	err = ice_vsi_cfg_lan(vsi);
7461 	if (err)
7462 		goto err_setup_rx;
7463 
7464 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
7465 		 dev_driver_string(dev), dev_name(dev));
7466 	err = ice_vsi_req_irq_msix(vsi, int_name);
7467 	if (err)
7468 		goto err_setup_rx;
7469 
7470 	ice_vsi_cfg_msix(vsi);
7471 
7472 	err = ice_vsi_start_all_rx_rings(vsi);
7473 	if (err)
7474 		goto err_up_complete;
7475 
7476 	clear_bit(ICE_VSI_DOWN, vsi->state);
7477 	ice_vsi_ena_irq(vsi);
7478 
7479 	return 0;
7480 
7481 err_up_complete:
7482 	ice_down(vsi);
7483 err_setup_rx:
7484 	ice_vsi_free_rx_rings(vsi);
7485 err_setup_tx:
7486 	ice_vsi_free_tx_rings(vsi);
7487 
7488 	return err;
7489 }
7490 
7491 /**
7492  * ice_vsi_open - Called when a network interface is made active
7493  * @vsi: the VSI to open
7494  *
7495  * Initialization of the VSI
7496  *
7497  * Returns 0 on success, negative value on error
7498  */
ice_vsi_open(struct ice_vsi * vsi)7499 int ice_vsi_open(struct ice_vsi *vsi)
7500 {
7501 	char int_name[ICE_INT_NAME_STR_LEN];
7502 	struct ice_pf *pf = vsi->back;
7503 	int err;
7504 
7505 	/* allocate descriptors */
7506 	err = ice_vsi_setup_tx_rings(vsi);
7507 	if (err)
7508 		goto err_setup_tx;
7509 
7510 	err = ice_vsi_setup_rx_rings(vsi);
7511 	if (err)
7512 		goto err_setup_rx;
7513 
7514 	err = ice_vsi_cfg_lan(vsi);
7515 	if (err)
7516 		goto err_setup_rx;
7517 
7518 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7519 		 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
7520 	err = ice_vsi_req_irq_msix(vsi, int_name);
7521 	if (err)
7522 		goto err_setup_rx;
7523 
7524 	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
7525 
7526 	if (vsi->type == ICE_VSI_PF || vsi->type == ICE_VSI_SF) {
7527 		/* Notify the stack of the actual queue counts. */
7528 		err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
7529 		if (err)
7530 			goto err_set_qs;
7531 
7532 		err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
7533 		if (err)
7534 			goto err_set_qs;
7535 
7536 		ice_vsi_set_napi_queues(vsi);
7537 	}
7538 
7539 	err = ice_up_complete(vsi);
7540 	if (err)
7541 		goto err_up_complete;
7542 
7543 	return 0;
7544 
7545 err_up_complete:
7546 	ice_down(vsi);
7547 err_set_qs:
7548 	ice_vsi_free_irq(vsi);
7549 err_setup_rx:
7550 	ice_vsi_free_rx_rings(vsi);
7551 err_setup_tx:
7552 	ice_vsi_free_tx_rings(vsi);
7553 
7554 	return err;
7555 }
7556 
7557 /**
7558  * ice_vsi_release_all - Delete all VSIs
7559  * @pf: PF from which all VSIs are being removed
7560  */
ice_vsi_release_all(struct ice_pf * pf)7561 static void ice_vsi_release_all(struct ice_pf *pf)
7562 {
7563 	int err, i;
7564 
7565 	if (!pf->vsi)
7566 		return;
7567 
7568 	ice_for_each_vsi(pf, i) {
7569 		if (!pf->vsi[i])
7570 			continue;
7571 
7572 		if (pf->vsi[i]->type == ICE_VSI_CHNL)
7573 			continue;
7574 
7575 		err = ice_vsi_release(pf->vsi[i]);
7576 		if (err)
7577 			dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
7578 				i, err, pf->vsi[i]->vsi_num);
7579 	}
7580 }
7581 
7582 /**
7583  * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
7584  * @pf: pointer to the PF instance
7585  * @type: VSI type to rebuild
7586  *
7587  * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
7588  */
ice_vsi_rebuild_by_type(struct ice_pf * pf,enum ice_vsi_type type)7589 static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
7590 {
7591 	struct device *dev = ice_pf_to_dev(pf);
7592 	int i, err;
7593 
7594 	ice_for_each_vsi(pf, i) {
7595 		struct ice_vsi *vsi = pf->vsi[i];
7596 
7597 		if (!vsi || vsi->type != type)
7598 			continue;
7599 
7600 		/* rebuild the VSI */
7601 		err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
7602 		if (err) {
7603 			dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
7604 				err, vsi->idx, ice_vsi_type_str(type));
7605 			return err;
7606 		}
7607 
7608 		/* replay filters for the VSI */
7609 		err = ice_replay_vsi(&pf->hw, vsi->idx);
7610 		if (err) {
7611 			dev_err(dev, "replay VSI failed, error %d, VSI index %d, type %s\n",
7612 				err, vsi->idx, ice_vsi_type_str(type));
7613 			return err;
7614 		}
7615 
7616 		/* Re-map HW VSI number, using VSI handle that has been
7617 		 * previously validated in ice_replay_vsi() call above
7618 		 */
7619 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
7620 
7621 		/* enable the VSI */
7622 		err = ice_ena_vsi(vsi, false);
7623 		if (err) {
7624 			dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
7625 				err, vsi->idx, ice_vsi_type_str(type));
7626 			return err;
7627 		}
7628 
7629 		dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
7630 			 ice_vsi_type_str(type));
7631 	}
7632 
7633 	return 0;
7634 }
7635 
7636 /**
7637  * ice_update_pf_netdev_link - Update PF netdev link status
7638  * @pf: pointer to the PF instance
7639  */
ice_update_pf_netdev_link(struct ice_pf * pf)7640 static void ice_update_pf_netdev_link(struct ice_pf *pf)
7641 {
7642 	bool link_up;
7643 	int i;
7644 
7645 	ice_for_each_vsi(pf, i) {
7646 		struct ice_vsi *vsi = pf->vsi[i];
7647 
7648 		if (!vsi || vsi->type != ICE_VSI_PF)
7649 			return;
7650 
7651 		ice_get_link_status(pf->vsi[i]->port_info, &link_up);
7652 		if (link_up) {
7653 			netif_carrier_on(pf->vsi[i]->netdev);
7654 			netif_tx_wake_all_queues(pf->vsi[i]->netdev);
7655 		} else {
7656 			netif_carrier_off(pf->vsi[i]->netdev);
7657 			netif_tx_stop_all_queues(pf->vsi[i]->netdev);
7658 		}
7659 	}
7660 }
7661 
7662 /**
7663  * ice_rebuild - rebuild after reset
7664  * @pf: PF to rebuild
7665  * @reset_type: type of reset
7666  *
7667  * Do not rebuild VF VSI in this flow because that is already handled via
7668  * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
7669  * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
7670  * to reset/rebuild all the VF VSI twice.
7671  */
ice_rebuild(struct ice_pf * pf,enum ice_reset_req reset_type)7672 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
7673 {
7674 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
7675 	struct device *dev = ice_pf_to_dev(pf);
7676 	struct ice_hw *hw = &pf->hw;
7677 	bool dvm;
7678 	int err;
7679 
7680 	if (test_bit(ICE_DOWN, pf->state))
7681 		goto clear_recovery;
7682 
7683 	dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
7684 
7685 #define ICE_EMP_RESET_SLEEP_MS 5000
7686 	if (reset_type == ICE_RESET_EMPR) {
7687 		/* If an EMP reset has occurred, any previously pending flash
7688 		 * update will have completed. We no longer know whether or
7689 		 * not the NVM update EMP reset is restricted.
7690 		 */
7691 		pf->fw_emp_reset_disabled = false;
7692 
7693 		msleep(ICE_EMP_RESET_SLEEP_MS);
7694 	}
7695 
7696 	err = ice_init_all_ctrlq(hw);
7697 	if (err) {
7698 		dev_err(dev, "control queues init failed %d\n", err);
7699 		goto err_init_ctrlq;
7700 	}
7701 
7702 	/* if DDP was previously loaded successfully */
7703 	if (!ice_is_safe_mode(pf)) {
7704 		/* reload the SW DB of filter tables */
7705 		if (reset_type == ICE_RESET_PFR)
7706 			ice_fill_blk_tbls(hw);
7707 		else
7708 			/* Reload DDP Package after CORER/GLOBR reset */
7709 			ice_load_pkg(NULL, pf);
7710 	}
7711 
7712 	err = ice_clear_pf_cfg(hw);
7713 	if (err) {
7714 		dev_err(dev, "clear PF configuration failed %d\n", err);
7715 		goto err_init_ctrlq;
7716 	}
7717 
7718 	ice_clear_pxe_mode(hw);
7719 
7720 	err = ice_init_nvm(hw);
7721 	if (err) {
7722 		dev_err(dev, "ice_init_nvm failed %d\n", err);
7723 		goto err_init_ctrlq;
7724 	}
7725 
7726 	err = ice_get_caps(hw);
7727 	if (err) {
7728 		dev_err(dev, "ice_get_caps failed %d\n", err);
7729 		goto err_init_ctrlq;
7730 	}
7731 
7732 	err = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
7733 	if (err) {
7734 		dev_err(dev, "set_mac_cfg failed %d\n", err);
7735 		goto err_init_ctrlq;
7736 	}
7737 
7738 	dvm = ice_is_dvm_ena(hw);
7739 
7740 	err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
7741 	if (err)
7742 		goto err_init_ctrlq;
7743 
7744 	err = ice_sched_init_port(hw->port_info);
7745 	if (err)
7746 		goto err_sched_init_port;
7747 
7748 	/* start misc vector */
7749 	err = ice_req_irq_msix_misc(pf);
7750 	if (err) {
7751 		dev_err(dev, "misc vector setup failed: %d\n", err);
7752 		goto err_sched_init_port;
7753 	}
7754 
7755 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7756 		wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
7757 		if (!rd32(hw, PFQF_FD_SIZE)) {
7758 			u16 unused, guar, b_effort;
7759 
7760 			guar = hw->func_caps.fd_fltr_guar;
7761 			b_effort = hw->func_caps.fd_fltr_best_effort;
7762 
7763 			/* force guaranteed filter pool for PF */
7764 			ice_alloc_fd_guar_item(hw, &unused, guar);
7765 			/* force shared filter pool for PF */
7766 			ice_alloc_fd_shrd_item(hw, &unused, b_effort);
7767 		}
7768 	}
7769 
7770 	if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
7771 		ice_dcb_rebuild(pf);
7772 
7773 	/* If the PF previously had enabled PTP, PTP init needs to happen before
7774 	 * the VSI rebuild. If not, this causes the PTP link status events to
7775 	 * fail.
7776 	 */
7777 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
7778 		ice_ptp_rebuild(pf, reset_type);
7779 
7780 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
7781 		ice_gnss_init(pf);
7782 
7783 	/* rebuild PF VSI */
7784 	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
7785 	if (err) {
7786 		dev_err(dev, "PF VSI rebuild failed: %d\n", err);
7787 		goto err_vsi_rebuild;
7788 	}
7789 
7790 	if (reset_type == ICE_RESET_PFR) {
7791 		err = ice_rebuild_channels(pf);
7792 		if (err) {
7793 			dev_err(dev, "failed to rebuild and replay ADQ VSIs, err %d\n",
7794 				err);
7795 			goto err_vsi_rebuild;
7796 		}
7797 	}
7798 
7799 	/* If Flow Director is active */
7800 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7801 		err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
7802 		if (err) {
7803 			dev_err(dev, "control VSI rebuild failed: %d\n", err);
7804 			goto err_vsi_rebuild;
7805 		}
7806 
7807 		/* replay HW Flow Director recipes */
7808 		if (hw->fdir_prof)
7809 			ice_fdir_replay_flows(hw);
7810 
7811 		/* replay Flow Director filters */
7812 		ice_fdir_replay_fltrs(pf);
7813 
7814 		ice_rebuild_arfs(pf);
7815 	}
7816 
7817 	if (vsi && vsi->netdev)
7818 		netif_device_attach(vsi->netdev);
7819 
7820 	ice_update_pf_netdev_link(pf);
7821 
7822 	/* tell the firmware we are up */
7823 	err = ice_send_version(pf);
7824 	if (err) {
7825 		dev_err(dev, "Rebuild failed due to error sending driver version: %d\n",
7826 			err);
7827 		goto err_vsi_rebuild;
7828 	}
7829 
7830 	ice_replay_post(hw);
7831 
7832 	/* if we get here, reset flow is successful */
7833 	clear_bit(ICE_RESET_FAILED, pf->state);
7834 
7835 	ice_health_clear(pf);
7836 
7837 	ice_plug_aux_dev(pf);
7838 	if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG))
7839 		ice_lag_rebuild(pf);
7840 
7841 	/* Restore timestamp mode settings after VSI rebuild */
7842 	ice_ptp_restore_timestamp_mode(pf);
7843 	return;
7844 
7845 err_vsi_rebuild:
7846 err_sched_init_port:
7847 	ice_sched_cleanup_all(hw);
7848 err_init_ctrlq:
7849 	ice_shutdown_all_ctrlq(hw, false);
7850 	set_bit(ICE_RESET_FAILED, pf->state);
7851 clear_recovery:
7852 	/* set this bit in PF state to control service task scheduling */
7853 	set_bit(ICE_NEEDS_RESTART, pf->state);
7854 	dev_err(dev, "Rebuild failed, unload and reload driver\n");
7855 }
7856 
7857 /**
7858  * ice_change_mtu - NDO callback to change the MTU
7859  * @netdev: network interface device structure
7860  * @new_mtu: new value for maximum frame size
7861  *
7862  * Returns 0 on success, negative on failure
7863  */
ice_change_mtu(struct net_device * netdev,int new_mtu)7864 int ice_change_mtu(struct net_device *netdev, int new_mtu)
7865 {
7866 	struct ice_netdev_priv *np = netdev_priv(netdev);
7867 	struct ice_vsi *vsi = np->vsi;
7868 	struct ice_pf *pf = vsi->back;
7869 	struct bpf_prog *prog;
7870 	u8 count = 0;
7871 	int err = 0;
7872 
7873 	if (new_mtu == (int)netdev->mtu) {
7874 		netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
7875 		return 0;
7876 	}
7877 
7878 	prog = vsi->xdp_prog;
7879 	if (prog && !prog->aux->xdp_has_frags) {
7880 		int frame_size = ice_max_xdp_frame_size(vsi);
7881 
7882 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
7883 			netdev_err(netdev, "max MTU for XDP usage is %d\n",
7884 				   frame_size - ICE_ETH_PKT_HDR_PAD);
7885 			return -EINVAL;
7886 		}
7887 	} else if (test_bit(ICE_FLAG_LEGACY_RX, pf->flags)) {
7888 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > ICE_MAX_FRAME_LEGACY_RX) {
7889 			netdev_err(netdev, "Too big MTU for legacy-rx; Max is %d\n",
7890 				   ICE_MAX_FRAME_LEGACY_RX - ICE_ETH_PKT_HDR_PAD);
7891 			return -EINVAL;
7892 		}
7893 	}
7894 
7895 	/* if a reset is in progress, wait for some time for it to complete */
7896 	do {
7897 		if (ice_is_reset_in_progress(pf->state)) {
7898 			count++;
7899 			usleep_range(1000, 2000);
7900 		} else {
7901 			break;
7902 		}
7903 
7904 	} while (count < 100);
7905 
7906 	if (count == 100) {
7907 		netdev_err(netdev, "can't change MTU. Device is busy\n");
7908 		return -EBUSY;
7909 	}
7910 
7911 	WRITE_ONCE(netdev->mtu, (unsigned int)new_mtu);
7912 	err = ice_down_up(vsi);
7913 	if (err)
7914 		return err;
7915 
7916 	netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
7917 	set_bit(ICE_FLAG_MTU_CHANGED, pf->flags);
7918 
7919 	return err;
7920 }
7921 
7922 /**
7923  * ice_set_rss_lut - Set RSS LUT
7924  * @vsi: Pointer to VSI structure
7925  * @lut: Lookup table
7926  * @lut_size: Lookup table size
7927  *
7928  * Returns 0 on success, negative on failure
7929  */
ice_set_rss_lut(struct ice_vsi * vsi,u8 * lut,u16 lut_size)7930 int ice_set_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7931 {
7932 	struct ice_aq_get_set_rss_lut_params params = {};
7933 	struct ice_hw *hw = &vsi->back->hw;
7934 	int status;
7935 
7936 	if (!lut)
7937 		return -EINVAL;
7938 
7939 	params.vsi_handle = vsi->idx;
7940 	params.lut_size = lut_size;
7941 	params.lut_type = vsi->rss_lut_type;
7942 	params.lut = lut;
7943 
7944 	status = ice_aq_set_rss_lut(hw, &params);
7945 	if (status)
7946 		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS lut, err %d aq_err %s\n",
7947 			status, libie_aq_str(hw->adminq.sq_last_status));
7948 
7949 	return status;
7950 }
7951 
7952 /**
7953  * ice_set_rss_key - Set RSS key
7954  * @vsi: Pointer to the VSI structure
7955  * @seed: RSS hash seed
7956  *
7957  * Returns 0 on success, negative on failure
7958  */
ice_set_rss_key(struct ice_vsi * vsi,u8 * seed)7959 int ice_set_rss_key(struct ice_vsi *vsi, u8 *seed)
7960 {
7961 	struct ice_hw *hw = &vsi->back->hw;
7962 	int status;
7963 
7964 	if (!seed)
7965 		return -EINVAL;
7966 
7967 	status = ice_aq_set_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7968 	if (status)
7969 		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS key, err %d aq_err %s\n",
7970 			status, libie_aq_str(hw->adminq.sq_last_status));
7971 
7972 	return status;
7973 }
7974 
7975 /**
7976  * ice_get_rss_lut - Get RSS LUT
7977  * @vsi: Pointer to VSI structure
7978  * @lut: Buffer to store the lookup table entries
7979  * @lut_size: Size of buffer to store the lookup table entries
7980  *
7981  * Returns 0 on success, negative on failure
7982  */
ice_get_rss_lut(struct ice_vsi * vsi,u8 * lut,u16 lut_size)7983 int ice_get_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7984 {
7985 	struct ice_aq_get_set_rss_lut_params params = {};
7986 	struct ice_hw *hw = &vsi->back->hw;
7987 	int status;
7988 
7989 	if (!lut)
7990 		return -EINVAL;
7991 
7992 	params.vsi_handle = vsi->idx;
7993 	params.lut_size = lut_size;
7994 	params.lut_type = vsi->rss_lut_type;
7995 	params.lut = lut;
7996 
7997 	status = ice_aq_get_rss_lut(hw, &params);
7998 	if (status)
7999 		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS lut, err %d aq_err %s\n",
8000 			status, libie_aq_str(hw->adminq.sq_last_status));
8001 
8002 	return status;
8003 }
8004 
8005 /**
8006  * ice_get_rss_key - Get RSS key
8007  * @vsi: Pointer to VSI structure
8008  * @seed: Buffer to store the key in
8009  *
8010  * Returns 0 on success, negative on failure
8011  */
ice_get_rss_key(struct ice_vsi * vsi,u8 * seed)8012 int ice_get_rss_key(struct ice_vsi *vsi, u8 *seed)
8013 {
8014 	struct ice_hw *hw = &vsi->back->hw;
8015 	int status;
8016 
8017 	if (!seed)
8018 		return -EINVAL;
8019 
8020 	status = ice_aq_get_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
8021 	if (status)
8022 		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS key, err %d aq_err %s\n",
8023 			status, libie_aq_str(hw->adminq.sq_last_status));
8024 
8025 	return status;
8026 }
8027 
8028 /**
8029  * ice_set_rss_hfunc - Set RSS HASH function
8030  * @vsi: Pointer to VSI structure
8031  * @hfunc: hash function (ICE_AQ_VSI_Q_OPT_RSS_*)
8032  *
8033  * Returns 0 on success, negative on failure
8034  */
ice_set_rss_hfunc(struct ice_vsi * vsi,u8 hfunc)8035 int ice_set_rss_hfunc(struct ice_vsi *vsi, u8 hfunc)
8036 {
8037 	struct ice_hw *hw = &vsi->back->hw;
8038 	struct ice_vsi_ctx *ctx;
8039 	bool symm;
8040 	int err;
8041 
8042 	if (hfunc == vsi->rss_hfunc)
8043 		return 0;
8044 
8045 	if (hfunc != ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ &&
8046 	    hfunc != ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ)
8047 		return -EOPNOTSUPP;
8048 
8049 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8050 	if (!ctx)
8051 		return -ENOMEM;
8052 
8053 	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
8054 	ctx->info.q_opt_rss = vsi->info.q_opt_rss;
8055 	ctx->info.q_opt_rss &= ~ICE_AQ_VSI_Q_OPT_RSS_HASH_M;
8056 	ctx->info.q_opt_rss |=
8057 		FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_HASH_M, hfunc);
8058 	ctx->info.q_opt_tc = vsi->info.q_opt_tc;
8059 	ctx->info.q_opt_flags = vsi->info.q_opt_rss;
8060 
8061 	err = ice_update_vsi(hw, vsi->idx, ctx, NULL);
8062 	if (err) {
8063 		dev_err(ice_pf_to_dev(vsi->back), "Failed to configure RSS hash for VSI %d, error %d\n",
8064 			vsi->vsi_num, err);
8065 	} else {
8066 		vsi->info.q_opt_rss = ctx->info.q_opt_rss;
8067 		vsi->rss_hfunc = hfunc;
8068 		netdev_info(vsi->netdev, "Hash function set to: %sToeplitz\n",
8069 			    hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ ?
8070 			    "Symmetric " : "");
8071 	}
8072 	kfree(ctx);
8073 	if (err)
8074 		return err;
8075 
8076 	/* Fix the symmetry setting for all existing RSS configurations */
8077 	symm = !!(hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ);
8078 	return ice_set_rss_cfg_symm(hw, vsi, symm);
8079 }
8080 
8081 /**
8082  * ice_bridge_getlink - Get the hardware bridge mode
8083  * @skb: skb buff
8084  * @pid: process ID
8085  * @seq: RTNL message seq
8086  * @dev: the netdev being configured
8087  * @filter_mask: filter mask passed in
8088  * @nlflags: netlink flags passed in
8089  *
8090  * Return the bridge mode (VEB/VEPA)
8091  */
8092 static int
ice_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 filter_mask,int nlflags)8093 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
8094 		   struct net_device *dev, u32 filter_mask, int nlflags)
8095 {
8096 	struct ice_netdev_priv *np = netdev_priv(dev);
8097 	struct ice_vsi *vsi = np->vsi;
8098 	struct ice_pf *pf = vsi->back;
8099 	u16 bmode;
8100 
8101 	bmode = pf->first_sw->bridge_mode;
8102 
8103 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
8104 				       filter_mask, NULL);
8105 }
8106 
8107 /**
8108  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
8109  * @vsi: Pointer to VSI structure
8110  * @bmode: Hardware bridge mode (VEB/VEPA)
8111  *
8112  * Returns 0 on success, negative on failure
8113  */
ice_vsi_update_bridge_mode(struct ice_vsi * vsi,u16 bmode)8114 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
8115 {
8116 	struct ice_aqc_vsi_props *vsi_props;
8117 	struct ice_hw *hw = &vsi->back->hw;
8118 	struct ice_vsi_ctx *ctxt;
8119 	int ret;
8120 
8121 	vsi_props = &vsi->info;
8122 
8123 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
8124 	if (!ctxt)
8125 		return -ENOMEM;
8126 
8127 	ctxt->info = vsi->info;
8128 
8129 	if (bmode == BRIDGE_MODE_VEB)
8130 		/* change from VEPA to VEB mode */
8131 		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
8132 	else
8133 		/* change from VEB to VEPA mode */
8134 		ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
8135 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
8136 
8137 	ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
8138 	if (ret) {
8139 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %d aq_err %s\n",
8140 			bmode, ret, libie_aq_str(hw->adminq.sq_last_status));
8141 		goto out;
8142 	}
8143 	/* Update sw flags for book keeping */
8144 	vsi_props->sw_flags = ctxt->info.sw_flags;
8145 
8146 out:
8147 	kfree(ctxt);
8148 	return ret;
8149 }
8150 
8151 /**
8152  * ice_bridge_setlink - Set the hardware bridge mode
8153  * @dev: the netdev being configured
8154  * @nlh: RTNL message
8155  * @flags: bridge setlink flags
8156  * @extack: netlink extended ack
8157  *
8158  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
8159  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
8160  * not already set for all VSIs connected to this switch. And also update the
8161  * unicast switch filter rules for the corresponding switch of the netdev.
8162  */
8163 static int
ice_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 __always_unused flags,struct netlink_ext_ack __always_unused * extack)8164 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
8165 		   u16 __always_unused flags,
8166 		   struct netlink_ext_ack __always_unused *extack)
8167 {
8168 	struct ice_netdev_priv *np = netdev_priv(dev);
8169 	struct ice_pf *pf = np->vsi->back;
8170 	struct nlattr *attr, *br_spec;
8171 	struct ice_hw *hw = &pf->hw;
8172 	struct ice_sw *pf_sw;
8173 	int rem, v, err = 0;
8174 
8175 	pf_sw = pf->first_sw;
8176 	/* find the attribute in the netlink message */
8177 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8178 	if (!br_spec)
8179 		return -EINVAL;
8180 
8181 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
8182 		__u16 mode = nla_get_u16(attr);
8183 
8184 		if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
8185 			return -EINVAL;
8186 		/* Continue  if bridge mode is not being flipped */
8187 		if (mode == pf_sw->bridge_mode)
8188 			continue;
8189 		/* Iterates through the PF VSI list and update the loopback
8190 		 * mode of the VSI
8191 		 */
8192 		ice_for_each_vsi(pf, v) {
8193 			if (!pf->vsi[v])
8194 				continue;
8195 			err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
8196 			if (err)
8197 				return err;
8198 		}
8199 
8200 		hw->evb_veb = (mode == BRIDGE_MODE_VEB);
8201 		/* Update the unicast switch filter rules for the corresponding
8202 		 * switch of the netdev
8203 		 */
8204 		err = ice_update_sw_rule_bridge_mode(hw);
8205 		if (err) {
8206 			netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %s\n",
8207 				   mode, err,
8208 				   libie_aq_str(hw->adminq.sq_last_status));
8209 			/* revert hw->evb_veb */
8210 			hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
8211 			return err;
8212 		}
8213 
8214 		pf_sw->bridge_mode = mode;
8215 	}
8216 
8217 	return 0;
8218 }
8219 
8220 /**
8221  * ice_tx_timeout - Respond to a Tx Hang
8222  * @netdev: network interface device structure
8223  * @txqueue: Tx queue
8224  */
ice_tx_timeout(struct net_device * netdev,unsigned int txqueue)8225 void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
8226 {
8227 	struct ice_netdev_priv *np = netdev_priv(netdev);
8228 	struct ice_tx_ring *tx_ring = NULL;
8229 	struct ice_vsi *vsi = np->vsi;
8230 	struct ice_pf *pf = vsi->back;
8231 	u32 i;
8232 
8233 	pf->tx_timeout_count++;
8234 
8235 	/* Check if PFC is enabled for the TC to which the queue belongs
8236 	 * to. If yes then Tx timeout is not caused by a hung queue, no
8237 	 * need to reset and rebuild
8238 	 */
8239 	if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
8240 		dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
8241 			 txqueue);
8242 		return;
8243 	}
8244 
8245 	/* now that we have an index, find the tx_ring struct */
8246 	ice_for_each_txq(vsi, i)
8247 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
8248 			if (txqueue == vsi->tx_rings[i]->q_index) {
8249 				tx_ring = vsi->tx_rings[i];
8250 				break;
8251 			}
8252 
8253 	/* Reset recovery level if enough time has elapsed after last timeout.
8254 	 * Also ensure no new reset action happens before next timeout period.
8255 	 */
8256 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
8257 		pf->tx_timeout_recovery_level = 1;
8258 	else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
8259 				       netdev->watchdog_timeo)))
8260 		return;
8261 
8262 	if (tx_ring) {
8263 		struct ice_hw *hw = &pf->hw;
8264 		u32 head, intr = 0;
8265 
8266 		head = FIELD_GET(QTX_COMM_HEAD_HEAD_M,
8267 				 rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])));
8268 		/* Read interrupt register */
8269 		intr = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
8270 
8271 		netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
8272 			    vsi->vsi_num, txqueue, tx_ring->next_to_clean,
8273 			    head, tx_ring->next_to_use, intr);
8274 
8275 		ice_prep_tx_hang_report(pf, tx_ring, vsi->vsi_num, head, intr);
8276 	}
8277 
8278 	pf->tx_timeout_last_recovery = jiffies;
8279 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
8280 		    pf->tx_timeout_recovery_level, txqueue);
8281 
8282 	switch (pf->tx_timeout_recovery_level) {
8283 	case 1:
8284 		set_bit(ICE_PFR_REQ, pf->state);
8285 		break;
8286 	case 2:
8287 		set_bit(ICE_CORER_REQ, pf->state);
8288 		break;
8289 	case 3:
8290 		set_bit(ICE_GLOBR_REQ, pf->state);
8291 		break;
8292 	default:
8293 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
8294 		set_bit(ICE_DOWN, pf->state);
8295 		set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
8296 		set_bit(ICE_SERVICE_DIS, pf->state);
8297 		break;
8298 	}
8299 
8300 	ice_service_task_schedule(pf);
8301 	pf->tx_timeout_recovery_level++;
8302 }
8303 
8304 /**
8305  * ice_setup_tc_cls_flower - flower classifier offloads
8306  * @np: net device to configure
8307  * @filter_dev: device on which filter is added
8308  * @cls_flower: offload data
8309  * @ingress: if the rule is added to an ingress block
8310  *
8311  * Return: 0 if the flower was successfully added or deleted,
8312  *	   negative error code otherwise.
8313  */
8314 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)8315 ice_setup_tc_cls_flower(struct ice_netdev_priv *np,
8316 			struct net_device *filter_dev,
8317 			struct flow_cls_offload *cls_flower,
8318 			bool ingress)
8319 {
8320 	struct ice_vsi *vsi = np->vsi;
8321 
8322 	if (cls_flower->common.chain_index)
8323 		return -EOPNOTSUPP;
8324 
8325 	switch (cls_flower->command) {
8326 	case FLOW_CLS_REPLACE:
8327 		return ice_add_cls_flower(filter_dev, vsi, cls_flower, ingress);
8328 	case FLOW_CLS_DESTROY:
8329 		return ice_del_cls_flower(vsi, cls_flower);
8330 	default:
8331 		return -EINVAL;
8332 	}
8333 }
8334 
8335 /**
8336  * ice_setup_tc_block_cb_ingress - callback handler for ingress TC block
8337  * @type: TC SETUP type
8338  * @type_data: TC flower offload data that contains user input
8339  * @cb_priv: netdev private data
8340  *
8341  * Return: 0 if the setup was successful, negative error code otherwise.
8342  */
8343 static int
ice_setup_tc_block_cb_ingress(enum tc_setup_type type,void * type_data,void * cb_priv)8344 ice_setup_tc_block_cb_ingress(enum tc_setup_type type, void *type_data,
8345 			      void *cb_priv)
8346 {
8347 	struct ice_netdev_priv *np = cb_priv;
8348 
8349 	switch (type) {
8350 	case TC_SETUP_CLSFLOWER:
8351 		return ice_setup_tc_cls_flower(np, np->vsi->netdev,
8352 					       type_data, true);
8353 	default:
8354 		return -EOPNOTSUPP;
8355 	}
8356 }
8357 
8358 /**
8359  * ice_setup_tc_block_cb_egress - callback handler for egress TC block
8360  * @type: TC SETUP type
8361  * @type_data: TC flower offload data that contains user input
8362  * @cb_priv: netdev private data
8363  *
8364  * Return: 0 if the setup was successful, negative error code otherwise.
8365  */
8366 static int
ice_setup_tc_block_cb_egress(enum tc_setup_type type,void * type_data,void * cb_priv)8367 ice_setup_tc_block_cb_egress(enum tc_setup_type type, void *type_data,
8368 			     void *cb_priv)
8369 {
8370 	struct ice_netdev_priv *np = cb_priv;
8371 
8372 	switch (type) {
8373 	case TC_SETUP_CLSFLOWER:
8374 		return ice_setup_tc_cls_flower(np, np->vsi->netdev,
8375 					       type_data, false);
8376 	default:
8377 		return -EOPNOTSUPP;
8378 	}
8379 }
8380 
8381 /**
8382  * ice_validate_mqprio_qopt - Validate TCF input parameters
8383  * @vsi: Pointer to VSI
8384  * @mqprio_qopt: input parameters for mqprio queue configuration
8385  *
8386  * This function validates MQPRIO params, such as qcount (power of 2 wherever
8387  * needed), and make sure user doesn't specify qcount and BW rate limit
8388  * for TCs, which are more than "num_tc"
8389  */
8390 static int
ice_validate_mqprio_qopt(struct ice_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)8391 ice_validate_mqprio_qopt(struct ice_vsi *vsi,
8392 			 struct tc_mqprio_qopt_offload *mqprio_qopt)
8393 {
8394 	int non_power_of_2_qcount = 0;
8395 	struct ice_pf *pf = vsi->back;
8396 	int max_rss_q_cnt = 0;
8397 	u64 sum_min_rate = 0;
8398 	struct device *dev;
8399 	int i, speed;
8400 	u8 num_tc;
8401 
8402 	if (vsi->type != ICE_VSI_PF)
8403 		return -EINVAL;
8404 
8405 	if (mqprio_qopt->qopt.offset[0] != 0 ||
8406 	    mqprio_qopt->qopt.num_tc < 1 ||
8407 	    mqprio_qopt->qopt.num_tc > ICE_CHNL_MAX_TC)
8408 		return -EINVAL;
8409 
8410 	dev = ice_pf_to_dev(pf);
8411 	vsi->ch_rss_size = 0;
8412 	num_tc = mqprio_qopt->qopt.num_tc;
8413 	speed = ice_get_link_speed_kbps(vsi);
8414 
8415 	for (i = 0; num_tc; i++) {
8416 		int qcount = mqprio_qopt->qopt.count[i];
8417 		u64 max_rate, min_rate, rem;
8418 
8419 		if (!qcount)
8420 			return -EINVAL;
8421 
8422 		if (is_power_of_2(qcount)) {
8423 			if (non_power_of_2_qcount &&
8424 			    qcount > non_power_of_2_qcount) {
8425 				dev_err(dev, "qcount[%d] cannot be greater than non power of 2 qcount[%d]\n",
8426 					qcount, non_power_of_2_qcount);
8427 				return -EINVAL;
8428 			}
8429 			if (qcount > max_rss_q_cnt)
8430 				max_rss_q_cnt = qcount;
8431 		} else {
8432 			if (non_power_of_2_qcount &&
8433 			    qcount != non_power_of_2_qcount) {
8434 				dev_err(dev, "Only one non power of 2 qcount allowed[%d,%d]\n",
8435 					qcount, non_power_of_2_qcount);
8436 				return -EINVAL;
8437 			}
8438 			if (qcount < max_rss_q_cnt) {
8439 				dev_err(dev, "non power of 2 qcount[%d] cannot be less than other qcount[%d]\n",
8440 					qcount, max_rss_q_cnt);
8441 				return -EINVAL;
8442 			}
8443 			max_rss_q_cnt = qcount;
8444 			non_power_of_2_qcount = qcount;
8445 		}
8446 
8447 		/* TC command takes input in K/N/Gbps or K/M/Gbit etc but
8448 		 * converts the bandwidth rate limit into Bytes/s when
8449 		 * passing it down to the driver. So convert input bandwidth
8450 		 * from Bytes/s to Kbps
8451 		 */
8452 		max_rate = mqprio_qopt->max_rate[i];
8453 		max_rate = div_u64(max_rate, ICE_BW_KBPS_DIVISOR);
8454 
8455 		/* min_rate is minimum guaranteed rate and it can't be zero */
8456 		min_rate = mqprio_qopt->min_rate[i];
8457 		min_rate = div_u64(min_rate, ICE_BW_KBPS_DIVISOR);
8458 		sum_min_rate += min_rate;
8459 
8460 		if (min_rate && min_rate < ICE_MIN_BW_LIMIT) {
8461 			dev_err(dev, "TC%d: min_rate(%llu Kbps) < %u Kbps\n", i,
8462 				min_rate, ICE_MIN_BW_LIMIT);
8463 			return -EINVAL;
8464 		}
8465 
8466 		if (max_rate && max_rate > speed) {
8467 			dev_err(dev, "TC%d: max_rate(%llu Kbps) > link speed of %u Kbps\n",
8468 				i, max_rate, speed);
8469 			return -EINVAL;
8470 		}
8471 
8472 		iter_div_u64_rem(min_rate, ICE_MIN_BW_LIMIT, &rem);
8473 		if (rem) {
8474 			dev_err(dev, "TC%d: Min Rate not multiple of %u Kbps",
8475 				i, ICE_MIN_BW_LIMIT);
8476 			return -EINVAL;
8477 		}
8478 
8479 		iter_div_u64_rem(max_rate, ICE_MIN_BW_LIMIT, &rem);
8480 		if (rem) {
8481 			dev_err(dev, "TC%d: Max Rate not multiple of %u Kbps",
8482 				i, ICE_MIN_BW_LIMIT);
8483 			return -EINVAL;
8484 		}
8485 
8486 		/* min_rate can't be more than max_rate, except when max_rate
8487 		 * is zero (implies max_rate sought is max line rate). In such
8488 		 * a case min_rate can be more than max.
8489 		 */
8490 		if (max_rate && min_rate > max_rate) {
8491 			dev_err(dev, "min_rate %llu Kbps can't be more than max_rate %llu Kbps\n",
8492 				min_rate, max_rate);
8493 			return -EINVAL;
8494 		}
8495 
8496 		if (i >= mqprio_qopt->qopt.num_tc - 1)
8497 			break;
8498 		if (mqprio_qopt->qopt.offset[i + 1] !=
8499 		    (mqprio_qopt->qopt.offset[i] + qcount))
8500 			return -EINVAL;
8501 	}
8502 	if (vsi->num_rxq <
8503 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8504 		return -EINVAL;
8505 	if (vsi->num_txq <
8506 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8507 		return -EINVAL;
8508 
8509 	if (sum_min_rate && sum_min_rate > (u64)speed) {
8510 		dev_err(dev, "Invalid min Tx rate(%llu) Kbps > speed (%u) Kbps specified\n",
8511 			sum_min_rate, speed);
8512 		return -EINVAL;
8513 	}
8514 
8515 	/* make sure vsi->ch_rss_size is set correctly based on TC's qcount */
8516 	vsi->ch_rss_size = max_rss_q_cnt;
8517 
8518 	return 0;
8519 }
8520 
8521 /**
8522  * ice_add_vsi_to_fdir - add a VSI to the flow director group for PF
8523  * @pf: ptr to PF device
8524  * @vsi: ptr to VSI
8525  */
ice_add_vsi_to_fdir(struct ice_pf * pf,struct ice_vsi * vsi)8526 static int ice_add_vsi_to_fdir(struct ice_pf *pf, struct ice_vsi *vsi)
8527 {
8528 	struct device *dev = ice_pf_to_dev(pf);
8529 	bool added = false;
8530 	struct ice_hw *hw;
8531 	int flow;
8532 
8533 	if (!(vsi->num_gfltr || vsi->num_bfltr))
8534 		return -EINVAL;
8535 
8536 	hw = &pf->hw;
8537 	for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++) {
8538 		struct ice_fd_hw_prof *prof;
8539 		int tun, status;
8540 		u64 entry_h;
8541 
8542 		if (!(hw->fdir_prof && hw->fdir_prof[flow] &&
8543 		      hw->fdir_prof[flow]->cnt))
8544 			continue;
8545 
8546 		for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
8547 			enum ice_flow_priority prio;
8548 
8549 			/* add this VSI to FDir profile for this flow */
8550 			prio = ICE_FLOW_PRIO_NORMAL;
8551 			prof = hw->fdir_prof[flow];
8552 			status = ice_flow_add_entry(hw, ICE_BLK_FD,
8553 						    prof->prof_id[tun],
8554 						    prof->vsi_h[0], vsi->idx,
8555 						    prio, prof->fdir_seg[tun],
8556 						    &entry_h);
8557 			if (status) {
8558 				dev_err(dev, "channel VSI idx %d, not able to add to group %d\n",
8559 					vsi->idx, flow);
8560 				continue;
8561 			}
8562 
8563 			prof->entry_h[prof->cnt][tun] = entry_h;
8564 		}
8565 
8566 		/* store VSI for filter replay and delete */
8567 		prof->vsi_h[prof->cnt] = vsi->idx;
8568 		prof->cnt++;
8569 
8570 		added = true;
8571 		dev_dbg(dev, "VSI idx %d added to fdir group %d\n", vsi->idx,
8572 			flow);
8573 	}
8574 
8575 	if (!added)
8576 		dev_dbg(dev, "VSI idx %d not added to fdir groups\n", vsi->idx);
8577 
8578 	return 0;
8579 }
8580 
8581 /**
8582  * ice_add_channel - add a channel by adding VSI
8583  * @pf: ptr to PF device
8584  * @sw_id: underlying HW switching element ID
8585  * @ch: ptr to channel structure
8586  *
8587  * Add a channel (VSI) using add_vsi and queue_map
8588  */
ice_add_channel(struct ice_pf * pf,u16 sw_id,struct ice_channel * ch)8589 static int ice_add_channel(struct ice_pf *pf, u16 sw_id, struct ice_channel *ch)
8590 {
8591 	struct device *dev = ice_pf_to_dev(pf);
8592 	struct ice_vsi *vsi;
8593 
8594 	if (ch->type != ICE_VSI_CHNL) {
8595 		dev_err(dev, "add new VSI failed, ch->type %d\n", ch->type);
8596 		return -EINVAL;
8597 	}
8598 
8599 	vsi = ice_chnl_vsi_setup(pf, pf->hw.port_info, ch);
8600 	if (!vsi || vsi->type != ICE_VSI_CHNL) {
8601 		dev_err(dev, "create chnl VSI failure\n");
8602 		return -EINVAL;
8603 	}
8604 
8605 	ice_add_vsi_to_fdir(pf, vsi);
8606 
8607 	ch->sw_id = sw_id;
8608 	ch->vsi_num = vsi->vsi_num;
8609 	ch->info.mapping_flags = vsi->info.mapping_flags;
8610 	ch->ch_vsi = vsi;
8611 	/* set the back pointer of channel for newly created VSI */
8612 	vsi->ch = ch;
8613 
8614 	memcpy(&ch->info.q_mapping, &vsi->info.q_mapping,
8615 	       sizeof(vsi->info.q_mapping));
8616 	memcpy(&ch->info.tc_mapping, vsi->info.tc_mapping,
8617 	       sizeof(vsi->info.tc_mapping));
8618 
8619 	return 0;
8620 }
8621 
8622 /**
8623  * ice_chnl_cfg_res
8624  * @vsi: the VSI being setup
8625  * @ch: ptr to channel structure
8626  *
8627  * Configure channel specific resources such as rings, vector.
8628  */
ice_chnl_cfg_res(struct ice_vsi * vsi,struct ice_channel * ch)8629 static void ice_chnl_cfg_res(struct ice_vsi *vsi, struct ice_channel *ch)
8630 {
8631 	int i;
8632 
8633 	for (i = 0; i < ch->num_txq; i++) {
8634 		struct ice_q_vector *tx_q_vector, *rx_q_vector;
8635 		struct ice_ring_container *rc;
8636 		struct ice_tx_ring *tx_ring;
8637 		struct ice_rx_ring *rx_ring;
8638 
8639 		tx_ring = vsi->tx_rings[ch->base_q + i];
8640 		rx_ring = vsi->rx_rings[ch->base_q + i];
8641 		if (!tx_ring || !rx_ring)
8642 			continue;
8643 
8644 		/* setup ring being channel enabled */
8645 		tx_ring->ch = ch;
8646 		rx_ring->ch = ch;
8647 
8648 		/* following code block sets up vector specific attributes */
8649 		tx_q_vector = tx_ring->q_vector;
8650 		rx_q_vector = rx_ring->q_vector;
8651 		if (!tx_q_vector && !rx_q_vector)
8652 			continue;
8653 
8654 		if (tx_q_vector) {
8655 			tx_q_vector->ch = ch;
8656 			/* setup Tx and Rx ITR setting if DIM is off */
8657 			rc = &tx_q_vector->tx;
8658 			if (!ITR_IS_DYNAMIC(rc))
8659 				ice_write_itr(rc, rc->itr_setting);
8660 		}
8661 		if (rx_q_vector) {
8662 			rx_q_vector->ch = ch;
8663 			/* setup Tx and Rx ITR setting if DIM is off */
8664 			rc = &rx_q_vector->rx;
8665 			if (!ITR_IS_DYNAMIC(rc))
8666 				ice_write_itr(rc, rc->itr_setting);
8667 		}
8668 	}
8669 
8670 	/* it is safe to assume that, if channel has non-zero num_t[r]xq, then
8671 	 * GLINT_ITR register would have written to perform in-context
8672 	 * update, hence perform flush
8673 	 */
8674 	if (ch->num_txq || ch->num_rxq)
8675 		ice_flush(&vsi->back->hw);
8676 }
8677 
8678 /**
8679  * ice_cfg_chnl_all_res - configure channel resources
8680  * @vsi: pte to main_vsi
8681  * @ch: ptr to channel structure
8682  *
8683  * This function configures channel specific resources such as flow-director
8684  * counter index, and other resources such as queues, vectors, ITR settings
8685  */
8686 static void
ice_cfg_chnl_all_res(struct ice_vsi * vsi,struct ice_channel * ch)8687 ice_cfg_chnl_all_res(struct ice_vsi *vsi, struct ice_channel *ch)
8688 {
8689 	/* configure channel (aka ADQ) resources such as queues, vectors,
8690 	 * ITR settings for channel specific vectors and anything else
8691 	 */
8692 	ice_chnl_cfg_res(vsi, ch);
8693 }
8694 
8695 /**
8696  * ice_setup_hw_channel - setup new channel
8697  * @pf: ptr to PF device
8698  * @vsi: the VSI being setup
8699  * @ch: ptr to channel structure
8700  * @sw_id: underlying HW switching element ID
8701  * @type: type of channel to be created (VMDq2/VF)
8702  *
8703  * Setup new channel (VSI) based on specified type (VMDq2/VF)
8704  * and configures Tx rings accordingly
8705  */
8706 static int
ice_setup_hw_channel(struct ice_pf * pf,struct ice_vsi * vsi,struct ice_channel * ch,u16 sw_id,u8 type)8707 ice_setup_hw_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8708 		     struct ice_channel *ch, u16 sw_id, u8 type)
8709 {
8710 	struct device *dev = ice_pf_to_dev(pf);
8711 	int ret;
8712 
8713 	ch->base_q = vsi->next_base_q;
8714 	ch->type = type;
8715 
8716 	ret = ice_add_channel(pf, sw_id, ch);
8717 	if (ret) {
8718 		dev_err(dev, "failed to add_channel using sw_id %u\n", sw_id);
8719 		return ret;
8720 	}
8721 
8722 	/* configure/setup ADQ specific resources */
8723 	ice_cfg_chnl_all_res(vsi, ch);
8724 
8725 	/* make sure to update the next_base_q so that subsequent channel's
8726 	 * (aka ADQ) VSI queue map is correct
8727 	 */
8728 	vsi->next_base_q = vsi->next_base_q + ch->num_rxq;
8729 	dev_dbg(dev, "added channel: vsi_num %u, num_rxq %u\n", ch->vsi_num,
8730 		ch->num_rxq);
8731 
8732 	return 0;
8733 }
8734 
8735 /**
8736  * ice_setup_channel - setup new channel using uplink element
8737  * @pf: ptr to PF device
8738  * @vsi: the VSI being setup
8739  * @ch: ptr to channel structure
8740  *
8741  * Setup new channel (VSI) based on specified type (VMDq2/VF)
8742  * and uplink switching element
8743  */
8744 static bool
ice_setup_channel(struct ice_pf * pf,struct ice_vsi * vsi,struct ice_channel * ch)8745 ice_setup_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8746 		  struct ice_channel *ch)
8747 {
8748 	struct device *dev = ice_pf_to_dev(pf);
8749 	u16 sw_id;
8750 	int ret;
8751 
8752 	if (vsi->type != ICE_VSI_PF) {
8753 		dev_err(dev, "unsupported parent VSI type(%d)\n", vsi->type);
8754 		return false;
8755 	}
8756 
8757 	sw_id = pf->first_sw->sw_id;
8758 
8759 	/* create channel (VSI) */
8760 	ret = ice_setup_hw_channel(pf, vsi, ch, sw_id, ICE_VSI_CHNL);
8761 	if (ret) {
8762 		dev_err(dev, "failed to setup hw_channel\n");
8763 		return false;
8764 	}
8765 	dev_dbg(dev, "successfully created channel()\n");
8766 
8767 	return ch->ch_vsi ? true : false;
8768 }
8769 
8770 /**
8771  * ice_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
8772  * @vsi: VSI to be configured
8773  * @max_tx_rate: max Tx rate in Kbps to be configured as maximum BW limit
8774  * @min_tx_rate: min Tx rate in Kbps to be configured as minimum BW limit
8775  */
8776 static int
ice_set_bw_limit(struct ice_vsi * vsi,u64 max_tx_rate,u64 min_tx_rate)8777 ice_set_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate, u64 min_tx_rate)
8778 {
8779 	int err;
8780 
8781 	err = ice_set_min_bw_limit(vsi, min_tx_rate);
8782 	if (err)
8783 		return err;
8784 
8785 	return ice_set_max_bw_limit(vsi, max_tx_rate);
8786 }
8787 
8788 /**
8789  * ice_create_q_channel - function to create channel
8790  * @vsi: VSI to be configured
8791  * @ch: ptr to channel (it contains channel specific params)
8792  *
8793  * This function creates channel (VSI) using num_queues specified by user,
8794  * reconfigs RSS if needed.
8795  */
ice_create_q_channel(struct ice_vsi * vsi,struct ice_channel * ch)8796 static int ice_create_q_channel(struct ice_vsi *vsi, struct ice_channel *ch)
8797 {
8798 	struct ice_pf *pf = vsi->back;
8799 	struct device *dev;
8800 
8801 	if (!ch)
8802 		return -EINVAL;
8803 
8804 	dev = ice_pf_to_dev(pf);
8805 	if (!ch->num_txq || !ch->num_rxq) {
8806 		dev_err(dev, "Invalid num_queues requested: %d\n", ch->num_rxq);
8807 		return -EINVAL;
8808 	}
8809 
8810 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_txq) {
8811 		dev_err(dev, "cnt_q_avail (%u) less than num_queues %d\n",
8812 			vsi->cnt_q_avail, ch->num_txq);
8813 		return -EINVAL;
8814 	}
8815 
8816 	if (!ice_setup_channel(pf, vsi, ch)) {
8817 		dev_info(dev, "Failed to setup channel\n");
8818 		return -EINVAL;
8819 	}
8820 	/* configure BW rate limit */
8821 	if (ch->ch_vsi && (ch->max_tx_rate || ch->min_tx_rate)) {
8822 		int ret;
8823 
8824 		ret = ice_set_bw_limit(ch->ch_vsi, ch->max_tx_rate,
8825 				       ch->min_tx_rate);
8826 		if (ret)
8827 			dev_err(dev, "failed to set Tx rate of %llu Kbps for VSI(%u)\n",
8828 				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8829 		else
8830 			dev_dbg(dev, "set Tx rate of %llu Kbps for VSI(%u)\n",
8831 				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8832 	}
8833 
8834 	vsi->cnt_q_avail -= ch->num_txq;
8835 
8836 	return 0;
8837 }
8838 
8839 /**
8840  * ice_rem_all_chnl_fltrs - removes all channel filters
8841  * @pf: ptr to PF, TC-flower based filter are tracked at PF level
8842  *
8843  * Remove all advanced switch filters only if they are channel specific
8844  * tc-flower based filter
8845  */
ice_rem_all_chnl_fltrs(struct ice_pf * pf)8846 static void ice_rem_all_chnl_fltrs(struct ice_pf *pf)
8847 {
8848 	struct ice_tc_flower_fltr *fltr;
8849 	struct hlist_node *node;
8850 
8851 	/* to remove all channel filters, iterate an ordered list of filters */
8852 	hlist_for_each_entry_safe(fltr, node,
8853 				  &pf->tc_flower_fltr_list,
8854 				  tc_flower_node) {
8855 		struct ice_rule_query_data rule;
8856 		int status;
8857 
8858 		/* for now process only channel specific filters */
8859 		if (!ice_is_chnl_fltr(fltr))
8860 			continue;
8861 
8862 		rule.rid = fltr->rid;
8863 		rule.rule_id = fltr->rule_id;
8864 		rule.vsi_handle = fltr->dest_vsi_handle;
8865 		status = ice_rem_adv_rule_by_id(&pf->hw, &rule);
8866 		if (status) {
8867 			if (status == -ENOENT)
8868 				dev_dbg(ice_pf_to_dev(pf), "TC flower filter (rule_id %u) does not exist\n",
8869 					rule.rule_id);
8870 			else
8871 				dev_err(ice_pf_to_dev(pf), "failed to delete TC flower filter, status %d\n",
8872 					status);
8873 		} else if (fltr->dest_vsi) {
8874 			/* update advanced switch filter count */
8875 			if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
8876 				u32 flags = fltr->flags;
8877 
8878 				fltr->dest_vsi->num_chnl_fltr--;
8879 				if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
8880 					     ICE_TC_FLWR_FIELD_ENC_DST_MAC))
8881 					pf->num_dmac_chnl_fltrs--;
8882 			}
8883 		}
8884 
8885 		hlist_del(&fltr->tc_flower_node);
8886 		kfree(fltr);
8887 	}
8888 }
8889 
8890 /**
8891  * ice_remove_q_channels - Remove queue channels for the TCs
8892  * @vsi: VSI to be configured
8893  * @rem_fltr: delete advanced switch filter or not
8894  *
8895  * Remove queue channels for the TCs
8896  */
ice_remove_q_channels(struct ice_vsi * vsi,bool rem_fltr)8897 static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_fltr)
8898 {
8899 	struct ice_channel *ch, *ch_tmp;
8900 	struct ice_pf *pf = vsi->back;
8901 	int i;
8902 
8903 	/* remove all tc-flower based filter if they are channel filters only */
8904 	if (rem_fltr)
8905 		ice_rem_all_chnl_fltrs(pf);
8906 
8907 	/* remove ntuple filters since queue configuration is being changed */
8908 	if  (vsi->netdev->features & NETIF_F_NTUPLE) {
8909 		struct ice_hw *hw = &pf->hw;
8910 
8911 		mutex_lock(&hw->fdir_fltr_lock);
8912 		ice_fdir_del_all_fltrs(vsi);
8913 		mutex_unlock(&hw->fdir_fltr_lock);
8914 	}
8915 
8916 	/* perform cleanup for channels if they exist */
8917 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
8918 		struct ice_vsi *ch_vsi;
8919 
8920 		list_del(&ch->list);
8921 		ch_vsi = ch->ch_vsi;
8922 		if (!ch_vsi) {
8923 			kfree(ch);
8924 			continue;
8925 		}
8926 
8927 		/* Reset queue contexts */
8928 		for (i = 0; i < ch->num_rxq; i++) {
8929 			struct ice_tx_ring *tx_ring;
8930 			struct ice_rx_ring *rx_ring;
8931 
8932 			tx_ring = vsi->tx_rings[ch->base_q + i];
8933 			rx_ring = vsi->rx_rings[ch->base_q + i];
8934 			if (tx_ring) {
8935 				tx_ring->ch = NULL;
8936 				if (tx_ring->q_vector)
8937 					tx_ring->q_vector->ch = NULL;
8938 			}
8939 			if (rx_ring) {
8940 				rx_ring->ch = NULL;
8941 				if (rx_ring->q_vector)
8942 					rx_ring->q_vector->ch = NULL;
8943 			}
8944 		}
8945 
8946 		/* Release FD resources for the channel VSI */
8947 		ice_fdir_rem_adq_chnl(&pf->hw, ch->ch_vsi->idx);
8948 
8949 		/* clear the VSI from scheduler tree */
8950 		ice_rm_vsi_lan_cfg(ch->ch_vsi->port_info, ch->ch_vsi->idx);
8951 
8952 		/* Delete VSI from FW, PF and HW VSI arrays */
8953 		ice_vsi_delete(ch->ch_vsi);
8954 
8955 		/* free the channel */
8956 		kfree(ch);
8957 	}
8958 
8959 	/* clear the channel VSI map which is stored in main VSI */
8960 	ice_for_each_chnl_tc(i)
8961 		vsi->tc_map_vsi[i] = NULL;
8962 
8963 	/* reset main VSI's all TC information */
8964 	vsi->all_enatc = 0;
8965 	vsi->all_numtc = 0;
8966 }
8967 
8968 /**
8969  * ice_rebuild_channels - rebuild channel
8970  * @pf: ptr to PF
8971  *
8972  * Recreate channel VSIs and replay filters
8973  */
ice_rebuild_channels(struct ice_pf * pf)8974 static int ice_rebuild_channels(struct ice_pf *pf)
8975 {
8976 	struct device *dev = ice_pf_to_dev(pf);
8977 	struct ice_vsi *main_vsi;
8978 	bool rem_adv_fltr = true;
8979 	struct ice_channel *ch;
8980 	struct ice_vsi *vsi;
8981 	int tc_idx = 1;
8982 	int i, err;
8983 
8984 	main_vsi = ice_get_main_vsi(pf);
8985 	if (!main_vsi)
8986 		return 0;
8987 
8988 	if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) ||
8989 	    main_vsi->old_numtc == 1)
8990 		return 0; /* nothing to be done */
8991 
8992 	/* reconfigure main VSI based on old value of TC and cached values
8993 	 * for MQPRIO opts
8994 	 */
8995 	err = ice_vsi_cfg_tc(main_vsi, main_vsi->old_ena_tc);
8996 	if (err) {
8997 		dev_err(dev, "failed configuring TC(ena_tc:0x%02x) for HW VSI=%u\n",
8998 			main_vsi->old_ena_tc, main_vsi->vsi_num);
8999 		return err;
9000 	}
9001 
9002 	/* rebuild ADQ VSIs */
9003 	ice_for_each_vsi(pf, i) {
9004 		enum ice_vsi_type type;
9005 
9006 		vsi = pf->vsi[i];
9007 		if (!vsi || vsi->type != ICE_VSI_CHNL)
9008 			continue;
9009 
9010 		type = vsi->type;
9011 
9012 		/* rebuild ADQ VSI */
9013 		err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
9014 		if (err) {
9015 			dev_err(dev, "VSI (type:%s) at index %d rebuild failed, err %d\n",
9016 				ice_vsi_type_str(type), vsi->idx, err);
9017 			goto cleanup;
9018 		}
9019 
9020 		/* Re-map HW VSI number, using VSI handle that has been
9021 		 * previously validated in ice_replay_vsi() call above
9022 		 */
9023 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
9024 
9025 		/* replay filters for the VSI */
9026 		err = ice_replay_vsi(&pf->hw, vsi->idx);
9027 		if (err) {
9028 			dev_err(dev, "VSI (type:%s) replay failed, err %d, VSI index %d\n",
9029 				ice_vsi_type_str(type), err, vsi->idx);
9030 			rem_adv_fltr = false;
9031 			goto cleanup;
9032 		}
9033 		dev_info(dev, "VSI (type:%s) at index %d rebuilt successfully\n",
9034 			 ice_vsi_type_str(type), vsi->idx);
9035 
9036 		/* store ADQ VSI at correct TC index in main VSI's
9037 		 * map of TC to VSI
9038 		 */
9039 		main_vsi->tc_map_vsi[tc_idx++] = vsi;
9040 	}
9041 
9042 	/* ADQ VSI(s) has been rebuilt successfully, so setup
9043 	 * channel for main VSI's Tx and Rx rings
9044 	 */
9045 	list_for_each_entry(ch, &main_vsi->ch_list, list) {
9046 		struct ice_vsi *ch_vsi;
9047 
9048 		ch_vsi = ch->ch_vsi;
9049 		if (!ch_vsi)
9050 			continue;
9051 
9052 		/* reconfig channel resources */
9053 		ice_cfg_chnl_all_res(main_vsi, ch);
9054 
9055 		/* replay BW rate limit if it is non-zero */
9056 		if (!ch->max_tx_rate && !ch->min_tx_rate)
9057 			continue;
9058 
9059 		err = ice_set_bw_limit(ch_vsi, ch->max_tx_rate,
9060 				       ch->min_tx_rate);
9061 		if (err)
9062 			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",
9063 				err, ch->max_tx_rate, ch->min_tx_rate,
9064 				ch_vsi->vsi_num);
9065 		else
9066 			dev_dbg(dev, "successfully rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
9067 				ch->max_tx_rate, ch->min_tx_rate,
9068 				ch_vsi->vsi_num);
9069 	}
9070 
9071 	/* reconfig RSS for main VSI */
9072 	if (main_vsi->ch_rss_size)
9073 		ice_vsi_cfg_rss_lut_key(main_vsi);
9074 
9075 	return 0;
9076 
9077 cleanup:
9078 	ice_remove_q_channels(main_vsi, rem_adv_fltr);
9079 	return err;
9080 }
9081 
9082 /**
9083  * ice_create_q_channels - Add queue channel for the given TCs
9084  * @vsi: VSI to be configured
9085  *
9086  * Configures queue channel mapping to the given TCs
9087  */
ice_create_q_channels(struct ice_vsi * vsi)9088 static int ice_create_q_channels(struct ice_vsi *vsi)
9089 {
9090 	struct ice_pf *pf = vsi->back;
9091 	struct ice_channel *ch;
9092 	int ret = 0, i;
9093 
9094 	ice_for_each_chnl_tc(i) {
9095 		if (!(vsi->all_enatc & BIT(i)))
9096 			continue;
9097 
9098 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
9099 		if (!ch) {
9100 			ret = -ENOMEM;
9101 			goto err_free;
9102 		}
9103 		INIT_LIST_HEAD(&ch->list);
9104 		ch->num_rxq = vsi->mqprio_qopt.qopt.count[i];
9105 		ch->num_txq = vsi->mqprio_qopt.qopt.count[i];
9106 		ch->base_q = vsi->mqprio_qopt.qopt.offset[i];
9107 		ch->max_tx_rate = vsi->mqprio_qopt.max_rate[i];
9108 		ch->min_tx_rate = vsi->mqprio_qopt.min_rate[i];
9109 
9110 		/* convert to Kbits/s */
9111 		if (ch->max_tx_rate)
9112 			ch->max_tx_rate = div_u64(ch->max_tx_rate,
9113 						  ICE_BW_KBPS_DIVISOR);
9114 		if (ch->min_tx_rate)
9115 			ch->min_tx_rate = div_u64(ch->min_tx_rate,
9116 						  ICE_BW_KBPS_DIVISOR);
9117 
9118 		ret = ice_create_q_channel(vsi, ch);
9119 		if (ret) {
9120 			dev_err(ice_pf_to_dev(pf),
9121 				"failed creating channel TC:%d\n", i);
9122 			kfree(ch);
9123 			goto err_free;
9124 		}
9125 		list_add_tail(&ch->list, &vsi->ch_list);
9126 		vsi->tc_map_vsi[i] = ch->ch_vsi;
9127 		dev_dbg(ice_pf_to_dev(pf),
9128 			"successfully created channel: VSI %pK\n", ch->ch_vsi);
9129 	}
9130 	return 0;
9131 
9132 err_free:
9133 	ice_remove_q_channels(vsi, false);
9134 
9135 	return ret;
9136 }
9137 
9138 /**
9139  * ice_setup_tc_mqprio_qdisc - configure multiple traffic classes
9140  * @netdev: net device to configure
9141  * @type_data: TC offload data
9142  */
ice_setup_tc_mqprio_qdisc(struct net_device * netdev,void * type_data)9143 static int ice_setup_tc_mqprio_qdisc(struct net_device *netdev, void *type_data)
9144 {
9145 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
9146 	struct ice_netdev_priv *np = netdev_priv(netdev);
9147 	struct ice_vsi *vsi = np->vsi;
9148 	struct ice_pf *pf = vsi->back;
9149 	u16 mode, ena_tc_qdisc = 0;
9150 	int cur_txq, cur_rxq;
9151 	u8 hw = 0, num_tcf;
9152 	struct device *dev;
9153 	int ret, i;
9154 
9155 	dev = ice_pf_to_dev(pf);
9156 	num_tcf = mqprio_qopt->qopt.num_tc;
9157 	hw = mqprio_qopt->qopt.hw;
9158 	mode = mqprio_qopt->mode;
9159 	if (!hw) {
9160 		clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
9161 		vsi->ch_rss_size = 0;
9162 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
9163 		goto config_tcf;
9164 	}
9165 
9166 	/* Generate queue region map for number of TCF requested */
9167 	for (i = 0; i < num_tcf; i++)
9168 		ena_tc_qdisc |= BIT(i);
9169 
9170 	switch (mode) {
9171 	case TC_MQPRIO_MODE_CHANNEL:
9172 
9173 		if (pf->hw.port_info->is_custom_tx_enabled) {
9174 			dev_err(dev, "Custom Tx scheduler feature enabled, can't configure ADQ\n");
9175 			return -EBUSY;
9176 		}
9177 		ice_tear_down_devlink_rate_tree(pf);
9178 
9179 		ret = ice_validate_mqprio_qopt(vsi, mqprio_qopt);
9180 		if (ret) {
9181 			netdev_err(netdev, "failed to validate_mqprio_qopt(), ret %d\n",
9182 				   ret);
9183 			return ret;
9184 		}
9185 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
9186 		set_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
9187 		/* don't assume state of hw_tc_offload during driver load
9188 		 * and set the flag for TC flower filter if hw_tc_offload
9189 		 * already ON
9190 		 */
9191 		if (vsi->netdev->features & NETIF_F_HW_TC)
9192 			set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
9193 		break;
9194 	default:
9195 		return -EINVAL;
9196 	}
9197 
9198 config_tcf:
9199 
9200 	/* Requesting same TCF configuration as already enabled */
9201 	if (ena_tc_qdisc == vsi->tc_cfg.ena_tc &&
9202 	    mode != TC_MQPRIO_MODE_CHANNEL)
9203 		return 0;
9204 
9205 	/* Pause VSI queues */
9206 	ice_dis_vsi(vsi, true);
9207 
9208 	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
9209 		ice_remove_q_channels(vsi, true);
9210 
9211 	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
9212 		vsi->req_txq = min_t(int, ice_get_avail_txq_count(pf),
9213 				     num_online_cpus());
9214 		vsi->req_rxq = min_t(int, ice_get_avail_rxq_count(pf),
9215 				     num_online_cpus());
9216 	} else {
9217 		/* logic to rebuild VSI, same like ethtool -L */
9218 		u16 offset = 0, qcount_tx = 0, qcount_rx = 0;
9219 
9220 		for (i = 0; i < num_tcf; i++) {
9221 			if (!(ena_tc_qdisc & BIT(i)))
9222 				continue;
9223 
9224 			offset = vsi->mqprio_qopt.qopt.offset[i];
9225 			qcount_rx = vsi->mqprio_qopt.qopt.count[i];
9226 			qcount_tx = vsi->mqprio_qopt.qopt.count[i];
9227 		}
9228 		vsi->req_txq = offset + qcount_tx;
9229 		vsi->req_rxq = offset + qcount_rx;
9230 
9231 		/* store away original rss_size info, so that it gets reused
9232 		 * form ice_vsi_rebuild during tc-qdisc delete stage - to
9233 		 * determine, what should be the rss_sizefor main VSI
9234 		 */
9235 		vsi->orig_rss_size = vsi->rss_size;
9236 	}
9237 
9238 	/* save current values of Tx and Rx queues before calling VSI rebuild
9239 	 * for fallback option
9240 	 */
9241 	cur_txq = vsi->num_txq;
9242 	cur_rxq = vsi->num_rxq;
9243 
9244 	/* proceed with rebuild main VSI using correct number of queues */
9245 	ret = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
9246 	if (ret) {
9247 		/* fallback to current number of queues */
9248 		dev_info(dev, "Rebuild failed with new queues, try with current number of queues\n");
9249 		vsi->req_txq = cur_txq;
9250 		vsi->req_rxq = cur_rxq;
9251 		clear_bit(ICE_RESET_FAILED, pf->state);
9252 		if (ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT)) {
9253 			dev_err(dev, "Rebuild of main VSI failed again\n");
9254 			return ret;
9255 		}
9256 	}
9257 
9258 	vsi->all_numtc = num_tcf;
9259 	vsi->all_enatc = ena_tc_qdisc;
9260 	ret = ice_vsi_cfg_tc(vsi, ena_tc_qdisc);
9261 	if (ret) {
9262 		netdev_err(netdev, "failed configuring TC for VSI id=%d\n",
9263 			   vsi->vsi_num);
9264 		goto exit;
9265 	}
9266 
9267 	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
9268 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
9269 		u64 min_tx_rate = vsi->mqprio_qopt.min_rate[0];
9270 
9271 		/* set TC0 rate limit if specified */
9272 		if (max_tx_rate || min_tx_rate) {
9273 			/* convert to Kbits/s */
9274 			if (max_tx_rate)
9275 				max_tx_rate = div_u64(max_tx_rate, ICE_BW_KBPS_DIVISOR);
9276 			if (min_tx_rate)
9277 				min_tx_rate = div_u64(min_tx_rate, ICE_BW_KBPS_DIVISOR);
9278 
9279 			ret = ice_set_bw_limit(vsi, max_tx_rate, min_tx_rate);
9280 			if (!ret) {
9281 				dev_dbg(dev, "set Tx rate max %llu min %llu for VSI(%u)\n",
9282 					max_tx_rate, min_tx_rate, vsi->vsi_num);
9283 			} else {
9284 				dev_err(dev, "failed to set Tx rate max %llu min %llu for VSI(%u)\n",
9285 					max_tx_rate, min_tx_rate, vsi->vsi_num);
9286 				goto exit;
9287 			}
9288 		}
9289 		ret = ice_create_q_channels(vsi);
9290 		if (ret) {
9291 			netdev_err(netdev, "failed configuring queue channels\n");
9292 			goto exit;
9293 		} else {
9294 			netdev_dbg(netdev, "successfully configured channels\n");
9295 		}
9296 	}
9297 
9298 	if (vsi->ch_rss_size)
9299 		ice_vsi_cfg_rss_lut_key(vsi);
9300 
9301 exit:
9302 	/* if error, reset the all_numtc and all_enatc */
9303 	if (ret) {
9304 		vsi->all_numtc = 0;
9305 		vsi->all_enatc = 0;
9306 	}
9307 	/* resume VSI */
9308 	ice_ena_vsi(vsi, true);
9309 
9310 	return ret;
9311 }
9312 
9313 static LIST_HEAD(ice_block_cb_list);
9314 
9315 static int
ice_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)9316 ice_setup_tc(struct net_device *netdev, enum tc_setup_type type,
9317 	     void *type_data)
9318 {
9319 	struct ice_netdev_priv *np = netdev_priv(netdev);
9320 	enum flow_block_binder_type binder_type;
9321 	struct iidc_rdma_core_dev_info *cdev;
9322 	struct ice_pf *pf = np->vsi->back;
9323 	flow_setup_cb_t *flower_handler;
9324 	bool locked = false;
9325 	int err;
9326 
9327 	switch (type) {
9328 	case TC_SETUP_BLOCK:
9329 		binder_type =
9330 			((struct flow_block_offload *)type_data)->binder_type;
9331 
9332 		switch (binder_type) {
9333 		case FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS:
9334 			flower_handler = ice_setup_tc_block_cb_ingress;
9335 			break;
9336 		case FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS:
9337 			flower_handler = ice_setup_tc_block_cb_egress;
9338 			break;
9339 		default:
9340 			return -EOPNOTSUPP;
9341 		}
9342 
9343 		return flow_block_cb_setup_simple(type_data,
9344 						  &ice_block_cb_list,
9345 						  flower_handler,
9346 						  np, np, false);
9347 	case TC_SETUP_QDISC_MQPRIO:
9348 		if (ice_is_eswitch_mode_switchdev(pf)) {
9349 			netdev_err(netdev, "TC MQPRIO offload not supported, switchdev is enabled\n");
9350 			return -EOPNOTSUPP;
9351 		}
9352 
9353 		cdev = pf->cdev_info;
9354 		if (cdev && cdev->adev) {
9355 			mutex_lock(&pf->adev_mutex);
9356 			device_lock(&cdev->adev->dev);
9357 			locked = true;
9358 			if (cdev->adev->dev.driver) {
9359 				netdev_err(netdev, "Cannot change qdisc when RDMA is active\n");
9360 				err = -EBUSY;
9361 				goto adev_unlock;
9362 			}
9363 		}
9364 
9365 		/* setup traffic classifier for receive side */
9366 		mutex_lock(&pf->tc_mutex);
9367 		err = ice_setup_tc_mqprio_qdisc(netdev, type_data);
9368 		mutex_unlock(&pf->tc_mutex);
9369 
9370 adev_unlock:
9371 		if (locked) {
9372 			device_unlock(&cdev->adev->dev);
9373 			mutex_unlock(&pf->adev_mutex);
9374 		}
9375 		return err;
9376 	default:
9377 		return -EOPNOTSUPP;
9378 	}
9379 	return -EOPNOTSUPP;
9380 }
9381 
9382 static struct ice_indr_block_priv *
ice_indr_block_priv_lookup(struct ice_netdev_priv * np,struct net_device * netdev)9383 ice_indr_block_priv_lookup(struct ice_netdev_priv *np,
9384 			   struct net_device *netdev)
9385 {
9386 	struct ice_indr_block_priv *cb_priv;
9387 
9388 	list_for_each_entry(cb_priv, &np->tc_indr_block_priv_list, list) {
9389 		if (!cb_priv->netdev)
9390 			return NULL;
9391 		if (cb_priv->netdev == netdev)
9392 			return cb_priv;
9393 	}
9394 	return NULL;
9395 }
9396 
9397 static int
ice_indr_setup_block_cb(enum tc_setup_type type,void * type_data,void * indr_priv)9398 ice_indr_setup_block_cb(enum tc_setup_type type, void *type_data,
9399 			void *indr_priv)
9400 {
9401 	struct ice_indr_block_priv *priv = indr_priv;
9402 	struct ice_netdev_priv *np = priv->np;
9403 
9404 	switch (type) {
9405 	case TC_SETUP_CLSFLOWER:
9406 		return ice_setup_tc_cls_flower(np, priv->netdev,
9407 					       (struct flow_cls_offload *)
9408 					       type_data, false);
9409 	default:
9410 		return -EOPNOTSUPP;
9411 	}
9412 }
9413 
9414 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))9415 ice_indr_setup_tc_block(struct net_device *netdev, struct Qdisc *sch,
9416 			struct ice_netdev_priv *np,
9417 			struct flow_block_offload *f, void *data,
9418 			void (*cleanup)(struct flow_block_cb *block_cb))
9419 {
9420 	struct ice_indr_block_priv *indr_priv;
9421 	struct flow_block_cb *block_cb;
9422 
9423 	if (!ice_is_tunnel_supported(netdev) &&
9424 	    !(is_vlan_dev(netdev) &&
9425 	      vlan_dev_real_dev(netdev) == np->vsi->netdev))
9426 		return -EOPNOTSUPP;
9427 
9428 	if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
9429 		return -EOPNOTSUPP;
9430 
9431 	switch (f->command) {
9432 	case FLOW_BLOCK_BIND:
9433 		indr_priv = ice_indr_block_priv_lookup(np, netdev);
9434 		if (indr_priv)
9435 			return -EEXIST;
9436 
9437 		indr_priv = kzalloc(sizeof(*indr_priv), GFP_KERNEL);
9438 		if (!indr_priv)
9439 			return -ENOMEM;
9440 
9441 		indr_priv->netdev = netdev;
9442 		indr_priv->np = np;
9443 		list_add(&indr_priv->list, &np->tc_indr_block_priv_list);
9444 
9445 		block_cb =
9446 			flow_indr_block_cb_alloc(ice_indr_setup_block_cb,
9447 						 indr_priv, indr_priv,
9448 						 ice_rep_indr_tc_block_unbind,
9449 						 f, netdev, sch, data, np,
9450 						 cleanup);
9451 
9452 		if (IS_ERR(block_cb)) {
9453 			list_del(&indr_priv->list);
9454 			kfree(indr_priv);
9455 			return PTR_ERR(block_cb);
9456 		}
9457 		flow_block_cb_add(block_cb, f);
9458 		list_add_tail(&block_cb->driver_list, &ice_block_cb_list);
9459 		break;
9460 	case FLOW_BLOCK_UNBIND:
9461 		indr_priv = ice_indr_block_priv_lookup(np, netdev);
9462 		if (!indr_priv)
9463 			return -ENOENT;
9464 
9465 		block_cb = flow_block_cb_lookup(f->block,
9466 						ice_indr_setup_block_cb,
9467 						indr_priv);
9468 		if (!block_cb)
9469 			return -ENOENT;
9470 
9471 		flow_indr_block_cb_remove(block_cb, f);
9472 
9473 		list_del(&block_cb->driver_list);
9474 		break;
9475 	default:
9476 		return -EOPNOTSUPP;
9477 	}
9478 	return 0;
9479 }
9480 
9481 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))9482 ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
9483 		     void *cb_priv, enum tc_setup_type type, void *type_data,
9484 		     void *data,
9485 		     void (*cleanup)(struct flow_block_cb *block_cb))
9486 {
9487 	switch (type) {
9488 	case TC_SETUP_BLOCK:
9489 		return ice_indr_setup_tc_block(netdev, sch, cb_priv, type_data,
9490 					       data, cleanup);
9491 
9492 	default:
9493 		return -EOPNOTSUPP;
9494 	}
9495 }
9496 
9497 /**
9498  * ice_open - Called when a network interface becomes active
9499  * @netdev: network interface device structure
9500  *
9501  * The open entry point is called when a network interface is made
9502  * active by the system (IFF_UP). At this point all resources needed
9503  * for transmit and receive operations are allocated, the interrupt
9504  * handler is registered with the OS, the netdev watchdog is enabled,
9505  * and the stack is notified that the interface is ready.
9506  *
9507  * Returns 0 on success, negative value on failure
9508  */
ice_open(struct net_device * netdev)9509 int ice_open(struct net_device *netdev)
9510 {
9511 	struct ice_netdev_priv *np = netdev_priv(netdev);
9512 	struct ice_pf *pf = np->vsi->back;
9513 
9514 	if (ice_is_reset_in_progress(pf->state)) {
9515 		netdev_err(netdev, "can't open net device while reset is in progress");
9516 		return -EBUSY;
9517 	}
9518 
9519 	return ice_open_internal(netdev);
9520 }
9521 
9522 /**
9523  * ice_open_internal - Called when a network interface becomes active
9524  * @netdev: network interface device structure
9525  *
9526  * Internal ice_open implementation. Should not be used directly except for ice_open and reset
9527  * handling routine
9528  *
9529  * Returns 0 on success, negative value on failure
9530  */
ice_open_internal(struct net_device * netdev)9531 int ice_open_internal(struct net_device *netdev)
9532 {
9533 	struct ice_netdev_priv *np = netdev_priv(netdev);
9534 	struct ice_vsi *vsi = np->vsi;
9535 	struct ice_pf *pf = vsi->back;
9536 	struct ice_port_info *pi;
9537 	int err;
9538 
9539 	if (test_bit(ICE_NEEDS_RESTART, pf->state)) {
9540 		netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
9541 		return -EIO;
9542 	}
9543 
9544 	netif_carrier_off(netdev);
9545 
9546 	pi = vsi->port_info;
9547 	err = ice_update_link_info(pi);
9548 	if (err) {
9549 		netdev_err(netdev, "Failed to get link info, error %d\n", err);
9550 		return err;
9551 	}
9552 
9553 	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
9554 
9555 	/* Set PHY if there is media, otherwise, turn off PHY */
9556 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
9557 		clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9558 		if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state)) {
9559 			err = ice_init_phy_user_cfg(pi);
9560 			if (err) {
9561 				netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
9562 					   err);
9563 				return err;
9564 			}
9565 		}
9566 
9567 		err = ice_configure_phy(vsi);
9568 		if (err) {
9569 			netdev_err(netdev, "Failed to set physical link up, error %d\n",
9570 				   err);
9571 			return err;
9572 		}
9573 	} else {
9574 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9575 		ice_set_link(vsi, false);
9576 	}
9577 
9578 	err = ice_vsi_open(vsi);
9579 	if (err)
9580 		netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
9581 			   vsi->vsi_num, vsi->vsw->sw_id);
9582 
9583 	/* Update existing tunnels information */
9584 	udp_tunnel_get_rx_info(netdev);
9585 
9586 	return err;
9587 }
9588 
9589 /**
9590  * ice_stop - Disables a network interface
9591  * @netdev: network interface device structure
9592  *
9593  * The stop entry point is called when an interface is de-activated by the OS,
9594  * and the netdevice enters the DOWN state. The hardware is still under the
9595  * driver's control, but the netdev interface is disabled.
9596  *
9597  * Returns success only - not allowed to fail
9598  */
ice_stop(struct net_device * netdev)9599 int ice_stop(struct net_device *netdev)
9600 {
9601 	struct ice_netdev_priv *np = netdev_priv(netdev);
9602 	struct ice_vsi *vsi = np->vsi;
9603 	struct ice_pf *pf = vsi->back;
9604 
9605 	if (ice_is_reset_in_progress(pf->state)) {
9606 		netdev_err(netdev, "can't stop net device while reset is in progress");
9607 		return -EBUSY;
9608 	}
9609 
9610 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
9611 		int link_err = ice_force_phys_link_state(vsi, false);
9612 
9613 		if (link_err) {
9614 			if (link_err == -ENOMEDIUM)
9615 				netdev_info(vsi->netdev, "Skipping link reconfig - no media attached, VSI %d\n",
9616 					    vsi->vsi_num);
9617 			else
9618 				netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
9619 					   vsi->vsi_num, link_err);
9620 
9621 			ice_vsi_close(vsi);
9622 			return -EIO;
9623 		}
9624 	}
9625 
9626 	ice_vsi_close(vsi);
9627 
9628 	return 0;
9629 }
9630 
9631 /**
9632  * ice_features_check - Validate encapsulated packet conforms to limits
9633  * @skb: skb buffer
9634  * @netdev: This port's netdev
9635  * @features: Offload features that the stack believes apply
9636  */
9637 static netdev_features_t
ice_features_check(struct sk_buff * skb,struct net_device __always_unused * netdev,netdev_features_t features)9638 ice_features_check(struct sk_buff *skb,
9639 		   struct net_device __always_unused *netdev,
9640 		   netdev_features_t features)
9641 {
9642 	bool gso = skb_is_gso(skb);
9643 	size_t len;
9644 
9645 	/* No point in doing any of this if neither checksum nor GSO are
9646 	 * being requested for this frame. We can rule out both by just
9647 	 * checking for CHECKSUM_PARTIAL
9648 	 */
9649 	if (skb->ip_summed != CHECKSUM_PARTIAL)
9650 		return features;
9651 
9652 	/* We cannot support GSO if the MSS is going to be less than
9653 	 * 64 bytes. If it is then we need to drop support for GSO.
9654 	 */
9655 	if (gso && (skb_shinfo(skb)->gso_size < ICE_TXD_CTX_MIN_MSS))
9656 		features &= ~NETIF_F_GSO_MASK;
9657 
9658 	len = skb_network_offset(skb);
9659 	if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
9660 		goto out_rm_features;
9661 
9662 	len = skb_network_header_len(skb);
9663 	if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9664 		goto out_rm_features;
9665 
9666 	if (skb->encapsulation) {
9667 		/* this must work for VXLAN frames AND IPIP/SIT frames, and in
9668 		 * the case of IPIP frames, the transport header pointer is
9669 		 * after the inner header! So check to make sure that this
9670 		 * is a GRE or UDP_TUNNEL frame before doing that math.
9671 		 */
9672 		if (gso && (skb_shinfo(skb)->gso_type &
9673 			    (SKB_GSO_GRE | SKB_GSO_UDP_TUNNEL))) {
9674 			len = skb_inner_network_header(skb) -
9675 			      skb_transport_header(skb);
9676 			if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
9677 				goto out_rm_features;
9678 		}
9679 
9680 		len = skb_inner_network_header_len(skb);
9681 		if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9682 			goto out_rm_features;
9683 	}
9684 
9685 	return features;
9686 out_rm_features:
9687 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
9688 }
9689 
9690 static const struct net_device_ops ice_netdev_safe_mode_ops = {
9691 	.ndo_open = ice_open,
9692 	.ndo_stop = ice_stop,
9693 	.ndo_start_xmit = ice_start_xmit,
9694 	.ndo_set_mac_address = ice_set_mac_address,
9695 	.ndo_validate_addr = eth_validate_addr,
9696 	.ndo_change_mtu = ice_change_mtu,
9697 	.ndo_get_stats64 = ice_get_stats64,
9698 	.ndo_tx_timeout = ice_tx_timeout,
9699 	.ndo_bpf = ice_xdp_safe_mode,
9700 };
9701 
9702 static const struct net_device_ops ice_netdev_ops = {
9703 	.ndo_open = ice_open,
9704 	.ndo_stop = ice_stop,
9705 	.ndo_start_xmit = ice_start_xmit,
9706 	.ndo_select_queue = ice_select_queue,
9707 	.ndo_features_check = ice_features_check,
9708 	.ndo_fix_features = ice_fix_features,
9709 	.ndo_set_rx_mode = ice_set_rx_mode,
9710 	.ndo_set_mac_address = ice_set_mac_address,
9711 	.ndo_validate_addr = eth_validate_addr,
9712 	.ndo_change_mtu = ice_change_mtu,
9713 	.ndo_get_stats64 = ice_get_stats64,
9714 	.ndo_set_tx_maxrate = ice_set_tx_maxrate,
9715 	.ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
9716 	.ndo_set_vf_mac = ice_set_vf_mac,
9717 	.ndo_get_vf_config = ice_get_vf_cfg,
9718 	.ndo_set_vf_trust = ice_set_vf_trust,
9719 	.ndo_set_vf_vlan = ice_set_vf_port_vlan,
9720 	.ndo_set_vf_link_state = ice_set_vf_link_state,
9721 	.ndo_get_vf_stats = ice_get_vf_stats,
9722 	.ndo_set_vf_rate = ice_set_vf_bw,
9723 	.ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
9724 	.ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
9725 	.ndo_setup_tc = ice_setup_tc,
9726 	.ndo_set_features = ice_set_features,
9727 	.ndo_bridge_getlink = ice_bridge_getlink,
9728 	.ndo_bridge_setlink = ice_bridge_setlink,
9729 	.ndo_fdb_add = ice_fdb_add,
9730 	.ndo_fdb_del = ice_fdb_del,
9731 #ifdef CONFIG_RFS_ACCEL
9732 	.ndo_rx_flow_steer = ice_rx_flow_steer,
9733 #endif
9734 	.ndo_tx_timeout = ice_tx_timeout,
9735 	.ndo_bpf = ice_xdp,
9736 	.ndo_xdp_xmit = ice_xdp_xmit,
9737 	.ndo_xsk_wakeup = ice_xsk_wakeup,
9738 	.ndo_hwtstamp_get = ice_ptp_hwtstamp_get,
9739 	.ndo_hwtstamp_set = ice_ptp_hwtstamp_set,
9740 };
9741