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