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