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