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