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