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