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(pf->first_sw)) {
416 				err = ice_set_dflt_vsi(pf->first_sw, 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(pf->first_sw, vsi)) {
430 				err = ice_clear_dflt_vsi(pf->first_sw);
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 	ice_unplug_aux_dev(pf);
2403 
2404 	switch (reset) {
2405 	case ICE_RESET_PFR:
2406 		set_bit(ICE_PFR_REQ, pf->state);
2407 		break;
2408 	case ICE_RESET_CORER:
2409 		set_bit(ICE_CORER_REQ, pf->state);
2410 		break;
2411 	case ICE_RESET_GLOBR:
2412 		set_bit(ICE_GLOBR_REQ, pf->state);
2413 		break;
2414 	default:
2415 		return -EINVAL;
2416 	}
2417 
2418 	ice_service_task_schedule(pf);
2419 	return 0;
2420 }
2421 
2422 /**
2423  * ice_irq_affinity_notify - Callback for affinity changes
2424  * @notify: context as to what irq was changed
2425  * @mask: the new affinity mask
2426  *
2427  * This is a callback function used by the irq_set_affinity_notifier function
2428  * so that we may register to receive changes to the irq affinity masks.
2429  */
2430 static void
ice_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)2431 ice_irq_affinity_notify(struct irq_affinity_notify *notify,
2432 			const cpumask_t *mask)
2433 {
2434 	struct ice_q_vector *q_vector =
2435 		container_of(notify, struct ice_q_vector, affinity_notify);
2436 
2437 	cpumask_copy(&q_vector->affinity_mask, mask);
2438 }
2439 
2440 /**
2441  * ice_irq_affinity_release - Callback for affinity notifier release
2442  * @ref: internal core kernel usage
2443  *
2444  * This is a callback function used by the irq_set_affinity_notifier function
2445  * to inform the current notification subscriber that they will no longer
2446  * receive notifications.
2447  */
ice_irq_affinity_release(struct kref __always_unused * ref)2448 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
2449 
2450 /**
2451  * ice_vsi_ena_irq - Enable IRQ for the given VSI
2452  * @vsi: the VSI being configured
2453  */
ice_vsi_ena_irq(struct ice_vsi * vsi)2454 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
2455 {
2456 	struct ice_hw *hw = &vsi->back->hw;
2457 	int i;
2458 
2459 	ice_for_each_q_vector(vsi, i)
2460 		ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
2461 
2462 	ice_flush(hw);
2463 	return 0;
2464 }
2465 
2466 /**
2467  * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
2468  * @vsi: the VSI being configured
2469  * @basename: name for the vector
2470  */
ice_vsi_req_irq_msix(struct ice_vsi * vsi,char * basename)2471 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
2472 {
2473 	int q_vectors = vsi->num_q_vectors;
2474 	struct ice_pf *pf = vsi->back;
2475 	int base = vsi->base_vector;
2476 	struct device *dev;
2477 	int rx_int_idx = 0;
2478 	int tx_int_idx = 0;
2479 	int vector, err;
2480 	int irq_num;
2481 
2482 	dev = ice_pf_to_dev(pf);
2483 	for (vector = 0; vector < q_vectors; vector++) {
2484 		struct ice_q_vector *q_vector = vsi->q_vectors[vector];
2485 
2486 		irq_num = pf->msix_entries[base + vector].vector;
2487 
2488 		if (q_vector->tx.tx_ring && q_vector->rx.rx_ring) {
2489 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2490 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2491 			tx_int_idx++;
2492 		} else if (q_vector->rx.rx_ring) {
2493 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2494 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
2495 		} else if (q_vector->tx.tx_ring) {
2496 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2497 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
2498 		} else {
2499 			/* skip this unused q_vector */
2500 			continue;
2501 		}
2502 		if (vsi->type == ICE_VSI_CTRL && vsi->vf)
2503 			err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2504 					       IRQF_SHARED, q_vector->name,
2505 					       q_vector);
2506 		else
2507 			err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2508 					       0, q_vector->name, q_vector);
2509 		if (err) {
2510 			netdev_err(vsi->netdev, "MSIX request_irq failed, error: %d\n",
2511 				   err);
2512 			goto free_q_irqs;
2513 		}
2514 
2515 		/* register for affinity change notifications */
2516 		if (!IS_ENABLED(CONFIG_RFS_ACCEL)) {
2517 			struct irq_affinity_notify *affinity_notify;
2518 
2519 			affinity_notify = &q_vector->affinity_notify;
2520 			affinity_notify->notify = ice_irq_affinity_notify;
2521 			affinity_notify->release = ice_irq_affinity_release;
2522 			irq_set_affinity_notifier(irq_num, affinity_notify);
2523 		}
2524 
2525 		/* assign the mask for this irq */
2526 		irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
2527 	}
2528 
2529 	err = ice_set_cpu_rx_rmap(vsi);
2530 	if (err) {
2531 		netdev_err(vsi->netdev, "Failed to setup CPU RMAP on VSI %u: %pe\n",
2532 			   vsi->vsi_num, ERR_PTR(err));
2533 		goto free_q_irqs;
2534 	}
2535 
2536 	vsi->irqs_ready = true;
2537 	return 0;
2538 
2539 free_q_irqs:
2540 	while (vector) {
2541 		vector--;
2542 		irq_num = pf->msix_entries[base + vector].vector;
2543 		if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2544 			irq_set_affinity_notifier(irq_num, NULL);
2545 		irq_set_affinity_hint(irq_num, NULL);
2546 		devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]);
2547 	}
2548 	return err;
2549 }
2550 
2551 /**
2552  * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
2553  * @vsi: VSI to setup Tx rings used by XDP
2554  *
2555  * Return 0 on success and negative value on error
2556  */
ice_xdp_alloc_setup_rings(struct ice_vsi * vsi)2557 static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
2558 {
2559 	struct device *dev = ice_pf_to_dev(vsi->back);
2560 	struct ice_tx_desc *tx_desc;
2561 	int i, j;
2562 
2563 	ice_for_each_xdp_txq(vsi, i) {
2564 		u16 xdp_q_idx = vsi->alloc_txq + i;
2565 		struct ice_tx_ring *xdp_ring;
2566 
2567 		xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
2568 
2569 		if (!xdp_ring)
2570 			goto free_xdp_rings;
2571 
2572 		xdp_ring->q_index = xdp_q_idx;
2573 		xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
2574 		xdp_ring->vsi = vsi;
2575 		xdp_ring->netdev = NULL;
2576 		xdp_ring->dev = dev;
2577 		xdp_ring->count = vsi->num_tx_desc;
2578 		xdp_ring->next_dd = ICE_RING_QUARTER(xdp_ring) - 1;
2579 		xdp_ring->next_rs = ICE_RING_QUARTER(xdp_ring) - 1;
2580 		WRITE_ONCE(vsi->xdp_rings[i], xdp_ring);
2581 		if (ice_setup_tx_ring(xdp_ring))
2582 			goto free_xdp_rings;
2583 		ice_set_ring_xdp(xdp_ring);
2584 		spin_lock_init(&xdp_ring->tx_lock);
2585 		for (j = 0; j < xdp_ring->count; j++) {
2586 			tx_desc = ICE_TX_DESC(xdp_ring, j);
2587 			tx_desc->cmd_type_offset_bsz = 0;
2588 		}
2589 	}
2590 
2591 	return 0;
2592 
2593 free_xdp_rings:
2594 	for (; i >= 0; i--)
2595 		if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
2596 			ice_free_tx_ring(vsi->xdp_rings[i]);
2597 	return -ENOMEM;
2598 }
2599 
2600 /**
2601  * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
2602  * @vsi: VSI to set the bpf prog on
2603  * @prog: the bpf prog pointer
2604  */
ice_vsi_assign_bpf_prog(struct ice_vsi * vsi,struct bpf_prog * prog)2605 static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
2606 {
2607 	struct bpf_prog *old_prog;
2608 	int i;
2609 
2610 	old_prog = xchg(&vsi->xdp_prog, prog);
2611 	if (old_prog)
2612 		bpf_prog_put(old_prog);
2613 
2614 	ice_for_each_rxq(vsi, i)
2615 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
2616 }
2617 
2618 /**
2619  * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
2620  * @vsi: VSI to bring up Tx rings used by XDP
2621  * @prog: bpf program that will be assigned to VSI
2622  *
2623  * Return 0 on success and negative value on error
2624  */
ice_prepare_xdp_rings(struct ice_vsi * vsi,struct bpf_prog * prog)2625 int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog)
2626 {
2627 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2628 	int xdp_rings_rem = vsi->num_xdp_txq;
2629 	struct ice_pf *pf = vsi->back;
2630 	struct ice_qs_cfg xdp_qs_cfg = {
2631 		.qs_mutex = &pf->avail_q_mutex,
2632 		.pf_map = pf->avail_txqs,
2633 		.pf_map_size = pf->max_pf_txqs,
2634 		.q_count = vsi->num_xdp_txq,
2635 		.scatter_count = ICE_MAX_SCATTER_TXQS,
2636 		.vsi_map = vsi->txq_map,
2637 		.vsi_map_offset = vsi->alloc_txq,
2638 		.mapping_mode = ICE_VSI_MAP_CONTIG
2639 	};
2640 	struct device *dev;
2641 	int i, v_idx;
2642 	int status;
2643 
2644 	dev = ice_pf_to_dev(pf);
2645 	vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq,
2646 				      sizeof(*vsi->xdp_rings), GFP_KERNEL);
2647 	if (!vsi->xdp_rings)
2648 		return -ENOMEM;
2649 
2650 	vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
2651 	if (__ice_vsi_get_qs(&xdp_qs_cfg))
2652 		goto err_map_xdp;
2653 
2654 	if (static_key_enabled(&ice_xdp_locking_key))
2655 		netdev_warn(vsi->netdev,
2656 			    "Could not allocate one XDP Tx ring per CPU, XDP_TX/XDP_REDIRECT actions will be slower\n");
2657 
2658 	if (ice_xdp_alloc_setup_rings(vsi))
2659 		goto clear_xdp_rings;
2660 
2661 	/* follow the logic from ice_vsi_map_rings_to_vectors */
2662 	ice_for_each_q_vector(vsi, v_idx) {
2663 		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2664 		int xdp_rings_per_v, q_id, q_base;
2665 
2666 		xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
2667 					       vsi->num_q_vectors - v_idx);
2668 		q_base = vsi->num_xdp_txq - xdp_rings_rem;
2669 
2670 		for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
2671 			struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_id];
2672 
2673 			xdp_ring->q_vector = q_vector;
2674 			xdp_ring->next = q_vector->tx.tx_ring;
2675 			q_vector->tx.tx_ring = xdp_ring;
2676 		}
2677 		xdp_rings_rem -= xdp_rings_per_v;
2678 	}
2679 
2680 	ice_for_each_rxq(vsi, i) {
2681 		if (static_key_enabled(&ice_xdp_locking_key)) {
2682 			vsi->rx_rings[i]->xdp_ring = vsi->xdp_rings[i % vsi->num_xdp_txq];
2683 		} else {
2684 			struct ice_q_vector *q_vector = vsi->rx_rings[i]->q_vector;
2685 			struct ice_tx_ring *ring;
2686 
2687 			ice_for_each_tx_ring(ring, q_vector->tx) {
2688 				if (ice_ring_is_xdp(ring)) {
2689 					vsi->rx_rings[i]->xdp_ring = ring;
2690 					break;
2691 				}
2692 			}
2693 		}
2694 		ice_tx_xsk_pool(vsi, i);
2695 	}
2696 
2697 	/* omit the scheduler update if in reset path; XDP queues will be
2698 	 * taken into account at the end of ice_vsi_rebuild, where
2699 	 * ice_cfg_vsi_lan is being called
2700 	 */
2701 	if (ice_is_reset_in_progress(pf->state))
2702 		return 0;
2703 
2704 	/* tell the Tx scheduler that right now we have
2705 	 * additional queues
2706 	 */
2707 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2708 		max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
2709 
2710 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2711 				 max_txqs);
2712 	if (status) {
2713 		dev_err(dev, "Failed VSI LAN queue config for XDP, error: %d\n",
2714 			status);
2715 		goto clear_xdp_rings;
2716 	}
2717 
2718 	/* assign the prog only when it's not already present on VSI;
2719 	 * this flow is a subject of both ethtool -L and ndo_bpf flows;
2720 	 * VSI rebuild that happens under ethtool -L can expose us to
2721 	 * the bpf_prog refcount issues as we would be swapping same
2722 	 * bpf_prog pointers from vsi->xdp_prog and calling bpf_prog_put
2723 	 * on it as it would be treated as an 'old_prog'; for ndo_bpf
2724 	 * this is not harmful as dev_xdp_install bumps the refcount
2725 	 * before calling the op exposed by the driver;
2726 	 */
2727 	if (!ice_is_xdp_ena_vsi(vsi))
2728 		ice_vsi_assign_bpf_prog(vsi, prog);
2729 
2730 	return 0;
2731 clear_xdp_rings:
2732 	ice_for_each_xdp_txq(vsi, i)
2733 		if (vsi->xdp_rings[i]) {
2734 			kfree_rcu(vsi->xdp_rings[i], rcu);
2735 			vsi->xdp_rings[i] = NULL;
2736 		}
2737 
2738 err_map_xdp:
2739 	mutex_lock(&pf->avail_q_mutex);
2740 	ice_for_each_xdp_txq(vsi, i) {
2741 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2742 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2743 	}
2744 	mutex_unlock(&pf->avail_q_mutex);
2745 
2746 	devm_kfree(dev, vsi->xdp_rings);
2747 	return -ENOMEM;
2748 }
2749 
2750 /**
2751  * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
2752  * @vsi: VSI to remove XDP rings
2753  *
2754  * Detach XDP rings from irq vectors, clean up the PF bitmap and free
2755  * resources
2756  */
ice_destroy_xdp_rings(struct ice_vsi * vsi)2757 int ice_destroy_xdp_rings(struct ice_vsi *vsi)
2758 {
2759 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2760 	struct ice_pf *pf = vsi->back;
2761 	int i, v_idx;
2762 
2763 	/* q_vectors are freed in reset path so there's no point in detaching
2764 	 * rings; in case of rebuild being triggered not from reset bits
2765 	 * in pf->state won't be set, so additionally check first q_vector
2766 	 * against NULL
2767 	 */
2768 	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2769 		goto free_qmap;
2770 
2771 	ice_for_each_q_vector(vsi, v_idx) {
2772 		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2773 		struct ice_tx_ring *ring;
2774 
2775 		ice_for_each_tx_ring(ring, q_vector->tx)
2776 			if (!ring->tx_buf || !ice_ring_is_xdp(ring))
2777 				break;
2778 
2779 		/* restore the value of last node prior to XDP setup */
2780 		q_vector->tx.tx_ring = ring;
2781 	}
2782 
2783 free_qmap:
2784 	mutex_lock(&pf->avail_q_mutex);
2785 	ice_for_each_xdp_txq(vsi, i) {
2786 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2787 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2788 	}
2789 	mutex_unlock(&pf->avail_q_mutex);
2790 
2791 	ice_for_each_xdp_txq(vsi, i)
2792 		if (vsi->xdp_rings[i]) {
2793 			if (vsi->xdp_rings[i]->desc) {
2794 				synchronize_rcu();
2795 				ice_free_tx_ring(vsi->xdp_rings[i]);
2796 			}
2797 			kfree_rcu(vsi->xdp_rings[i], rcu);
2798 			vsi->xdp_rings[i] = NULL;
2799 		}
2800 
2801 	devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
2802 	vsi->xdp_rings = NULL;
2803 
2804 	if (static_key_enabled(&ice_xdp_locking_key))
2805 		static_branch_dec(&ice_xdp_locking_key);
2806 
2807 	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2808 		return 0;
2809 
2810 	ice_vsi_assign_bpf_prog(vsi, NULL);
2811 
2812 	/* notify Tx scheduler that we destroyed XDP queues and bring
2813 	 * back the old number of child nodes
2814 	 */
2815 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2816 		max_txqs[i] = vsi->num_txq;
2817 
2818 	/* change number of XDP Tx queues to 0 */
2819 	vsi->num_xdp_txq = 0;
2820 
2821 	return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2822 			       max_txqs);
2823 }
2824 
2825 /**
2826  * ice_vsi_rx_napi_schedule - Schedule napi on RX queues from VSI
2827  * @vsi: VSI to schedule napi on
2828  */
ice_vsi_rx_napi_schedule(struct ice_vsi * vsi)2829 static void ice_vsi_rx_napi_schedule(struct ice_vsi *vsi)
2830 {
2831 	int i;
2832 
2833 	ice_for_each_rxq(vsi, i) {
2834 		struct ice_rx_ring *rx_ring = vsi->rx_rings[i];
2835 
2836 		if (rx_ring->xsk_pool)
2837 			napi_schedule(&rx_ring->q_vector->napi);
2838 	}
2839 }
2840 
2841 /**
2842  * ice_vsi_determine_xdp_res - figure out how many Tx qs can XDP have
2843  * @vsi: VSI to determine the count of XDP Tx qs
2844  *
2845  * returns 0 if Tx qs count is higher than at least half of CPU count,
2846  * -ENOMEM otherwise
2847  */
ice_vsi_determine_xdp_res(struct ice_vsi * vsi)2848 int ice_vsi_determine_xdp_res(struct ice_vsi *vsi)
2849 {
2850 	u16 avail = ice_get_avail_txq_count(vsi->back);
2851 	u16 cpus = num_possible_cpus();
2852 
2853 	if (avail < cpus / 2)
2854 		return -ENOMEM;
2855 
2856 	vsi->num_xdp_txq = min_t(u16, avail, cpus);
2857 
2858 	if (vsi->num_xdp_txq < cpus)
2859 		static_branch_inc(&ice_xdp_locking_key);
2860 
2861 	return 0;
2862 }
2863 
2864 /**
2865  * ice_xdp_setup_prog - Add or remove XDP eBPF program
2866  * @vsi: VSI to setup XDP for
2867  * @prog: XDP program
2868  * @extack: netlink extended ack
2869  */
2870 static int
ice_xdp_setup_prog(struct ice_vsi * vsi,struct bpf_prog * prog,struct netlink_ext_ack * extack)2871 ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
2872 		   struct netlink_ext_ack *extack)
2873 {
2874 	int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
2875 	bool if_running = netif_running(vsi->netdev);
2876 	int ret = 0, xdp_ring_err = 0;
2877 
2878 	if (frame_size > vsi->rx_buf_len) {
2879 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for loading XDP");
2880 		return -EOPNOTSUPP;
2881 	}
2882 
2883 	/* need to stop netdev while setting up the program for Rx rings */
2884 	if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
2885 		ret = ice_down(vsi);
2886 		if (ret) {
2887 			NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
2888 			return ret;
2889 		}
2890 	}
2891 
2892 	if (!ice_is_xdp_ena_vsi(vsi) && prog) {
2893 		xdp_ring_err = ice_vsi_determine_xdp_res(vsi);
2894 		if (xdp_ring_err) {
2895 			NL_SET_ERR_MSG_MOD(extack, "Not enough Tx resources for XDP");
2896 		} else {
2897 			xdp_ring_err = ice_prepare_xdp_rings(vsi, prog);
2898 			if (xdp_ring_err)
2899 				NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
2900 		}
2901 		/* reallocate Rx queues that are used for zero-copy */
2902 		xdp_ring_err = ice_realloc_zc_buf(vsi, true);
2903 		if (xdp_ring_err)
2904 			NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Rx resources failed");
2905 	} else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
2906 		xdp_ring_err = ice_destroy_xdp_rings(vsi);
2907 		if (xdp_ring_err)
2908 			NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
2909 		/* reallocate Rx queues that were used for zero-copy */
2910 		xdp_ring_err = ice_realloc_zc_buf(vsi, false);
2911 		if (xdp_ring_err)
2912 			NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Rx resources failed");
2913 	} else {
2914 		/* safe to call even when prog == vsi->xdp_prog as
2915 		 * dev_xdp_install in net/core/dev.c incremented prog's
2916 		 * refcount so corresponding bpf_prog_put won't cause
2917 		 * underflow
2918 		 */
2919 		ice_vsi_assign_bpf_prog(vsi, prog);
2920 	}
2921 
2922 	if (if_running)
2923 		ret = ice_up(vsi);
2924 
2925 	if (!ret && prog)
2926 		ice_vsi_rx_napi_schedule(vsi);
2927 
2928 	return (ret || xdp_ring_err) ? -ENOMEM : 0;
2929 }
2930 
2931 /**
2932  * ice_xdp_safe_mode - XDP handler for safe mode
2933  * @dev: netdevice
2934  * @xdp: XDP command
2935  */
ice_xdp_safe_mode(struct net_device __always_unused * dev,struct netdev_bpf * xdp)2936 static int ice_xdp_safe_mode(struct net_device __always_unused *dev,
2937 			     struct netdev_bpf *xdp)
2938 {
2939 	NL_SET_ERR_MSG_MOD(xdp->extack,
2940 			   "Please provide working DDP firmware package in order to use XDP\n"
2941 			   "Refer to Documentation/networking/device_drivers/ethernet/intel/ice.rst");
2942 	return -EOPNOTSUPP;
2943 }
2944 
2945 /**
2946  * ice_xdp - implements XDP handler
2947  * @dev: netdevice
2948  * @xdp: XDP command
2949  */
ice_xdp(struct net_device * dev,struct netdev_bpf * xdp)2950 static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2951 {
2952 	struct ice_netdev_priv *np = netdev_priv(dev);
2953 	struct ice_vsi *vsi = np->vsi;
2954 
2955 	if (vsi->type != ICE_VSI_PF) {
2956 		NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF VSI");
2957 		return -EINVAL;
2958 	}
2959 
2960 	switch (xdp->command) {
2961 	case XDP_SETUP_PROG:
2962 		return ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
2963 	case XDP_SETUP_XSK_POOL:
2964 		return ice_xsk_pool_setup(vsi, xdp->xsk.pool,
2965 					  xdp->xsk.queue_id);
2966 	default:
2967 		return -EINVAL;
2968 	}
2969 }
2970 
2971 /**
2972  * ice_ena_misc_vector - enable the non-queue interrupts
2973  * @pf: board private structure
2974  */
ice_ena_misc_vector(struct ice_pf * pf)2975 static void ice_ena_misc_vector(struct ice_pf *pf)
2976 {
2977 	struct ice_hw *hw = &pf->hw;
2978 	u32 val;
2979 
2980 	/* Disable anti-spoof detection interrupt to prevent spurious event
2981 	 * interrupts during a function reset. Anti-spoof functionally is
2982 	 * still supported.
2983 	 */
2984 	val = rd32(hw, GL_MDCK_TX_TDPU);
2985 	val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
2986 	wr32(hw, GL_MDCK_TX_TDPU, val);
2987 
2988 	/* clear things first */
2989 	wr32(hw, PFINT_OICR_ENA, 0);	/* disable all */
2990 	rd32(hw, PFINT_OICR);		/* read to clear */
2991 
2992 	val = (PFINT_OICR_ECC_ERR_M |
2993 	       PFINT_OICR_MAL_DETECT_M |
2994 	       PFINT_OICR_GRST_M |
2995 	       PFINT_OICR_PCI_EXCEPTION_M |
2996 	       PFINT_OICR_VFLR_M |
2997 	       PFINT_OICR_HMC_ERR_M |
2998 	       PFINT_OICR_PE_PUSH_M |
2999 	       PFINT_OICR_PE_CRITERR_M);
3000 
3001 	wr32(hw, PFINT_OICR_ENA, val);
3002 
3003 	/* SW_ITR_IDX = 0, but don't change INTENA */
3004 	wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
3005 	     GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
3006 }
3007 
3008 /**
3009  * ice_misc_intr - misc interrupt handler
3010  * @irq: interrupt number
3011  * @data: pointer to a q_vector
3012  */
ice_misc_intr(int __always_unused irq,void * data)3013 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
3014 {
3015 	struct ice_pf *pf = (struct ice_pf *)data;
3016 	struct ice_hw *hw = &pf->hw;
3017 	irqreturn_t ret = IRQ_NONE;
3018 	struct device *dev;
3019 	u32 oicr, ena_mask;
3020 
3021 	dev = ice_pf_to_dev(pf);
3022 	set_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
3023 	set_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
3024 	set_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
3025 
3026 	oicr = rd32(hw, PFINT_OICR);
3027 	ena_mask = rd32(hw, PFINT_OICR_ENA);
3028 
3029 	if (oicr & PFINT_OICR_SWINT_M) {
3030 		ena_mask &= ~PFINT_OICR_SWINT_M;
3031 		pf->sw_int_count++;
3032 	}
3033 
3034 	if (oicr & PFINT_OICR_MAL_DETECT_M) {
3035 		ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
3036 		set_bit(ICE_MDD_EVENT_PENDING, pf->state);
3037 	}
3038 	if (oicr & PFINT_OICR_VFLR_M) {
3039 		/* disable any further VFLR event notifications */
3040 		if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
3041 			u32 reg = rd32(hw, PFINT_OICR_ENA);
3042 
3043 			reg &= ~PFINT_OICR_VFLR_M;
3044 			wr32(hw, PFINT_OICR_ENA, reg);
3045 		} else {
3046 			ena_mask &= ~PFINT_OICR_VFLR_M;
3047 			set_bit(ICE_VFLR_EVENT_PENDING, pf->state);
3048 		}
3049 	}
3050 
3051 	if (oicr & PFINT_OICR_GRST_M) {
3052 		u32 reset;
3053 
3054 		/* we have a reset warning */
3055 		ena_mask &= ~PFINT_OICR_GRST_M;
3056 		reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
3057 			GLGEN_RSTAT_RESET_TYPE_S;
3058 
3059 		if (reset == ICE_RESET_CORER)
3060 			pf->corer_count++;
3061 		else if (reset == ICE_RESET_GLOBR)
3062 			pf->globr_count++;
3063 		else if (reset == ICE_RESET_EMPR)
3064 			pf->empr_count++;
3065 		else
3066 			dev_dbg(dev, "Invalid reset type %d\n", reset);
3067 
3068 		/* If a reset cycle isn't already in progress, we set a bit in
3069 		 * pf->state so that the service task can start a reset/rebuild.
3070 		 */
3071 		if (!test_and_set_bit(ICE_RESET_OICR_RECV, pf->state)) {
3072 			if (reset == ICE_RESET_CORER)
3073 				set_bit(ICE_CORER_RECV, pf->state);
3074 			else if (reset == ICE_RESET_GLOBR)
3075 				set_bit(ICE_GLOBR_RECV, pf->state);
3076 			else
3077 				set_bit(ICE_EMPR_RECV, pf->state);
3078 
3079 			/* There are couple of different bits at play here.
3080 			 * hw->reset_ongoing indicates whether the hardware is
3081 			 * in reset. This is set to true when a reset interrupt
3082 			 * is received and set back to false after the driver
3083 			 * has determined that the hardware is out of reset.
3084 			 *
3085 			 * ICE_RESET_OICR_RECV in pf->state indicates
3086 			 * that a post reset rebuild is required before the
3087 			 * driver is operational again. This is set above.
3088 			 *
3089 			 * As this is the start of the reset/rebuild cycle, set
3090 			 * both to indicate that.
3091 			 */
3092 			hw->reset_ongoing = true;
3093 		}
3094 	}
3095 
3096 	if (oicr & PFINT_OICR_TSYN_TX_M) {
3097 		ena_mask &= ~PFINT_OICR_TSYN_TX_M;
3098 		ice_ptp_process_ts(pf);
3099 	}
3100 
3101 	if (oicr & PFINT_OICR_TSYN_EVNT_M) {
3102 		u8 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3103 		u32 gltsyn_stat = rd32(hw, GLTSYN_STAT(tmr_idx));
3104 
3105 		/* Save EVENTs from GTSYN register */
3106 		pf->ptp.ext_ts_irq |= gltsyn_stat & (GLTSYN_STAT_EVENT0_M |
3107 						     GLTSYN_STAT_EVENT1_M |
3108 						     GLTSYN_STAT_EVENT2_M);
3109 		ena_mask &= ~PFINT_OICR_TSYN_EVNT_M;
3110 		kthread_queue_work(pf->ptp.kworker, &pf->ptp.extts_work);
3111 	}
3112 
3113 #define ICE_AUX_CRIT_ERR (PFINT_OICR_PE_CRITERR_M | PFINT_OICR_HMC_ERR_M | PFINT_OICR_PE_PUSH_M)
3114 	if (oicr & ICE_AUX_CRIT_ERR) {
3115 		pf->oicr_err_reg |= oicr;
3116 		set_bit(ICE_AUX_ERR_PENDING, pf->state);
3117 		ena_mask &= ~ICE_AUX_CRIT_ERR;
3118 	}
3119 
3120 	/* Report any remaining unexpected interrupts */
3121 	oicr &= ena_mask;
3122 	if (oicr) {
3123 		dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
3124 		/* If a critical error is pending there is no choice but to
3125 		 * reset the device.
3126 		 */
3127 		if (oicr & (PFINT_OICR_PCI_EXCEPTION_M |
3128 			    PFINT_OICR_ECC_ERR_M)) {
3129 			set_bit(ICE_PFR_REQ, pf->state);
3130 			ice_service_task_schedule(pf);
3131 		}
3132 	}
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_dis_ctrlq_interrupts - disable control queue interrupts
3143  * @hw: pointer to HW structure
3144  */
ice_dis_ctrlq_interrupts(struct ice_hw * hw)3145 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
3146 {
3147 	/* disable Admin queue Interrupt causes */
3148 	wr32(hw, PFINT_FW_CTL,
3149 	     rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
3150 
3151 	/* disable Mailbox queue Interrupt causes */
3152 	wr32(hw, PFINT_MBX_CTL,
3153 	     rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
3154 
3155 	wr32(hw, PFINT_SB_CTL,
3156 	     rd32(hw, PFINT_SB_CTL) & ~PFINT_SB_CTL_CAUSE_ENA_M);
3157 
3158 	/* disable Control queue Interrupt causes */
3159 	wr32(hw, PFINT_OICR_CTL,
3160 	     rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
3161 
3162 	ice_flush(hw);
3163 }
3164 
3165 /**
3166  * ice_free_irq_msix_misc - Unroll misc vector setup
3167  * @pf: board private structure
3168  */
ice_free_irq_msix_misc(struct ice_pf * pf)3169 static void ice_free_irq_msix_misc(struct ice_pf *pf)
3170 {
3171 	struct ice_hw *hw = &pf->hw;
3172 
3173 	ice_dis_ctrlq_interrupts(hw);
3174 
3175 	/* disable OICR interrupt */
3176 	wr32(hw, PFINT_OICR_ENA, 0);
3177 	ice_flush(hw);
3178 
3179 	if (pf->msix_entries) {
3180 		synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
3181 		devm_free_irq(ice_pf_to_dev(pf),
3182 			      pf->msix_entries[pf->oicr_idx].vector, pf);
3183 	}
3184 
3185 	pf->num_avail_sw_msix += 1;
3186 	ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
3187 }
3188 
3189 /**
3190  * ice_ena_ctrlq_interrupts - enable control queue interrupts
3191  * @hw: pointer to HW structure
3192  * @reg_idx: HW vector index to associate the control queue interrupts with
3193  */
ice_ena_ctrlq_interrupts(struct ice_hw * hw,u16 reg_idx)3194 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
3195 {
3196 	u32 val;
3197 
3198 	val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
3199 	       PFINT_OICR_CTL_CAUSE_ENA_M);
3200 	wr32(hw, PFINT_OICR_CTL, val);
3201 
3202 	/* enable Admin queue Interrupt causes */
3203 	val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
3204 	       PFINT_FW_CTL_CAUSE_ENA_M);
3205 	wr32(hw, PFINT_FW_CTL, val);
3206 
3207 	/* enable Mailbox queue Interrupt causes */
3208 	val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
3209 	       PFINT_MBX_CTL_CAUSE_ENA_M);
3210 	wr32(hw, PFINT_MBX_CTL, val);
3211 
3212 	/* This enables Sideband queue Interrupt causes */
3213 	val = ((reg_idx & PFINT_SB_CTL_MSIX_INDX_M) |
3214 	       PFINT_SB_CTL_CAUSE_ENA_M);
3215 	wr32(hw, PFINT_SB_CTL, val);
3216 
3217 	ice_flush(hw);
3218 }
3219 
3220 /**
3221  * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
3222  * @pf: board private structure
3223  *
3224  * This sets up the handler for MSIX 0, which is used to manage the
3225  * non-queue interrupts, e.g. AdminQ and errors. This is not used
3226  * when in MSI or Legacy interrupt mode.
3227  */
ice_req_irq_msix_misc(struct ice_pf * pf)3228 static int ice_req_irq_msix_misc(struct ice_pf *pf)
3229 {
3230 	struct device *dev = ice_pf_to_dev(pf);
3231 	struct ice_hw *hw = &pf->hw;
3232 	int oicr_idx, err = 0;
3233 
3234 	if (!pf->int_name[0])
3235 		snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
3236 			 dev_driver_string(dev), dev_name(dev));
3237 
3238 	/* Do not request IRQ but do enable OICR interrupt since settings are
3239 	 * lost during reset. Note that this function is called only during
3240 	 * rebuild path and not while reset is in progress.
3241 	 */
3242 	if (ice_is_reset_in_progress(pf->state))
3243 		goto skip_req_irq;
3244 
3245 	/* reserve one vector in irq_tracker for misc interrupts */
3246 	oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
3247 	if (oicr_idx < 0)
3248 		return oicr_idx;
3249 
3250 	pf->num_avail_sw_msix -= 1;
3251 	pf->oicr_idx = (u16)oicr_idx;
3252 
3253 	err = devm_request_irq(dev, pf->msix_entries[pf->oicr_idx].vector,
3254 			       ice_misc_intr, 0, pf->int_name, pf);
3255 	if (err) {
3256 		dev_err(dev, "devm_request_irq for %s failed: %d\n",
3257 			pf->int_name, err);
3258 		ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
3259 		pf->num_avail_sw_msix += 1;
3260 		return err;
3261 	}
3262 
3263 skip_req_irq:
3264 	ice_ena_misc_vector(pf);
3265 
3266 	ice_ena_ctrlq_interrupts(hw, pf->oicr_idx);
3267 	wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
3268 	     ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
3269 
3270 	ice_flush(hw);
3271 	ice_irq_dynamic_ena(hw, NULL, NULL);
3272 
3273 	return 0;
3274 }
3275 
3276 /**
3277  * ice_napi_add - register NAPI handler for the VSI
3278  * @vsi: VSI for which NAPI handler is to be registered
3279  *
3280  * This function is only called in the driver's load path. Registering the NAPI
3281  * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
3282  * reset/rebuild, etc.)
3283  */
ice_napi_add(struct ice_vsi * vsi)3284 static void ice_napi_add(struct ice_vsi *vsi)
3285 {
3286 	int v_idx;
3287 
3288 	if (!vsi->netdev)
3289 		return;
3290 
3291 	ice_for_each_q_vector(vsi, v_idx)
3292 		netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
3293 			       ice_napi_poll, NAPI_POLL_WEIGHT);
3294 }
3295 
3296 /**
3297  * ice_set_ops - set netdev and ethtools ops for the given netdev
3298  * @netdev: netdev instance
3299  */
ice_set_ops(struct net_device * netdev)3300 static void ice_set_ops(struct net_device *netdev)
3301 {
3302 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
3303 
3304 	if (ice_is_safe_mode(pf)) {
3305 		netdev->netdev_ops = &ice_netdev_safe_mode_ops;
3306 		ice_set_ethtool_safe_mode_ops(netdev);
3307 		return;
3308 	}
3309 
3310 	netdev->netdev_ops = &ice_netdev_ops;
3311 	netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
3312 	ice_set_ethtool_ops(netdev);
3313 }
3314 
3315 /**
3316  * ice_set_netdev_features - set features for the given netdev
3317  * @netdev: netdev instance
3318  */
ice_set_netdev_features(struct net_device * netdev)3319 static void ice_set_netdev_features(struct net_device *netdev)
3320 {
3321 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
3322 	bool is_dvm_ena = ice_is_dvm_ena(&pf->hw);
3323 	netdev_features_t csumo_features;
3324 	netdev_features_t vlano_features;
3325 	netdev_features_t dflt_features;
3326 	netdev_features_t tso_features;
3327 
3328 	if (ice_is_safe_mode(pf)) {
3329 		/* safe mode */
3330 		netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
3331 		netdev->hw_features = netdev->features;
3332 		return;
3333 	}
3334 
3335 	dflt_features = NETIF_F_SG	|
3336 			NETIF_F_HIGHDMA	|
3337 			NETIF_F_NTUPLE	|
3338 			NETIF_F_RXHASH;
3339 
3340 	csumo_features = NETIF_F_RXCSUM	  |
3341 			 NETIF_F_IP_CSUM  |
3342 			 NETIF_F_SCTP_CRC |
3343 			 NETIF_F_IPV6_CSUM;
3344 
3345 	vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
3346 			 NETIF_F_HW_VLAN_CTAG_TX     |
3347 			 NETIF_F_HW_VLAN_CTAG_RX;
3348 
3349 	/* Enable CTAG/STAG filtering by default in Double VLAN Mode (DVM) */
3350 	if (is_dvm_ena)
3351 		vlano_features |= NETIF_F_HW_VLAN_STAG_FILTER;
3352 
3353 	tso_features = NETIF_F_TSO			|
3354 		       NETIF_F_TSO_ECN			|
3355 		       NETIF_F_TSO6			|
3356 		       NETIF_F_GSO_GRE			|
3357 		       NETIF_F_GSO_UDP_TUNNEL		|
3358 		       NETIF_F_GSO_GRE_CSUM		|
3359 		       NETIF_F_GSO_UDP_TUNNEL_CSUM	|
3360 		       NETIF_F_GSO_PARTIAL		|
3361 		       NETIF_F_GSO_IPXIP4		|
3362 		       NETIF_F_GSO_IPXIP6		|
3363 		       NETIF_F_GSO_UDP_L4;
3364 
3365 	netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3366 					NETIF_F_GSO_GRE_CSUM;
3367 	/* set features that user can change */
3368 	netdev->hw_features = dflt_features | csumo_features |
3369 			      vlano_features | tso_features;
3370 
3371 	/* add support for HW_CSUM on packets with MPLS header */
3372 	netdev->mpls_features =  NETIF_F_HW_CSUM |
3373 				 NETIF_F_TSO     |
3374 				 NETIF_F_TSO6;
3375 
3376 	/* enable features */
3377 	netdev->features |= netdev->hw_features;
3378 
3379 	netdev->hw_features |= NETIF_F_HW_TC;
3380 
3381 	/* encap and VLAN devices inherit default, csumo and tso features */
3382 	netdev->hw_enc_features |= dflt_features | csumo_features |
3383 				   tso_features;
3384 	netdev->vlan_features |= dflt_features | csumo_features |
3385 				 tso_features;
3386 
3387 	/* advertise support but don't enable by default since only one type of
3388 	 * VLAN offload can be enabled at a time (i.e. CTAG or STAG). When one
3389 	 * type turns on the other has to be turned off. This is enforced by the
3390 	 * ice_fix_features() ndo callback.
3391 	 */
3392 	if (is_dvm_ena)
3393 		netdev->hw_features |= NETIF_F_HW_VLAN_STAG_RX |
3394 			NETIF_F_HW_VLAN_STAG_TX;
3395 }
3396 
3397 /**
3398  * ice_cfg_netdev - Allocate, configure and register a netdev
3399  * @vsi: the VSI associated with the new netdev
3400  *
3401  * Returns 0 on success, negative value on failure
3402  */
ice_cfg_netdev(struct ice_vsi * vsi)3403 static int ice_cfg_netdev(struct ice_vsi *vsi)
3404 {
3405 	struct ice_netdev_priv *np;
3406 	struct net_device *netdev;
3407 	u8 mac_addr[ETH_ALEN];
3408 
3409 	netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
3410 				    vsi->alloc_rxq);
3411 	if (!netdev)
3412 		return -ENOMEM;
3413 
3414 	set_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
3415 	vsi->netdev = netdev;
3416 	np = netdev_priv(netdev);
3417 	np->vsi = vsi;
3418 
3419 	ice_set_netdev_features(netdev);
3420 
3421 	ice_set_ops(netdev);
3422 
3423 	if (vsi->type == ICE_VSI_PF) {
3424 		SET_NETDEV_DEV(netdev, ice_pf_to_dev(vsi->back));
3425 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
3426 		eth_hw_addr_set(netdev, mac_addr);
3427 		ether_addr_copy(netdev->perm_addr, mac_addr);
3428 	}
3429 
3430 	netdev->priv_flags |= IFF_UNICAST_FLT;
3431 
3432 	/* Setup netdev TC information */
3433 	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
3434 
3435 	/* setup watchdog timeout value to be 5 second */
3436 	netdev->watchdog_timeo = 5 * HZ;
3437 
3438 	netdev->min_mtu = ETH_MIN_MTU;
3439 	netdev->max_mtu = ICE_MAX_MTU;
3440 
3441 	return 0;
3442 }
3443 
3444 /**
3445  * ice_fill_rss_lut - Fill the RSS lookup table with default values
3446  * @lut: Lookup table
3447  * @rss_table_size: Lookup table size
3448  * @rss_size: Range of queue number for hashing
3449  */
ice_fill_rss_lut(u8 * lut,u16 rss_table_size,u16 rss_size)3450 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
3451 {
3452 	u16 i;
3453 
3454 	for (i = 0; i < rss_table_size; i++)
3455 		lut[i] = i % rss_size;
3456 }
3457 
3458 /**
3459  * ice_pf_vsi_setup - Set up a PF VSI
3460  * @pf: board private structure
3461  * @pi: pointer to the port_info instance
3462  *
3463  * Returns pointer to the successfully allocated VSI software struct
3464  * on success, otherwise returns NULL on failure.
3465  */
3466 static struct ice_vsi *
ice_pf_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi)3467 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3468 {
3469 	return ice_vsi_setup(pf, pi, ICE_VSI_PF, NULL, NULL);
3470 }
3471 
3472 static struct ice_vsi *
ice_chnl_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi,struct ice_channel * ch)3473 ice_chnl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
3474 		   struct ice_channel *ch)
3475 {
3476 	return ice_vsi_setup(pf, pi, ICE_VSI_CHNL, NULL, ch);
3477 }
3478 
3479 /**
3480  * ice_ctrl_vsi_setup - Set up a control VSI
3481  * @pf: board private structure
3482  * @pi: pointer to the port_info instance
3483  *
3484  * Returns pointer to the successfully allocated VSI software struct
3485  * on success, otherwise returns NULL on failure.
3486  */
3487 static struct ice_vsi *
ice_ctrl_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi)3488 ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3489 {
3490 	return ice_vsi_setup(pf, pi, ICE_VSI_CTRL, NULL, NULL);
3491 }
3492 
3493 /**
3494  * ice_lb_vsi_setup - Set up a loopback VSI
3495  * @pf: board private structure
3496  * @pi: pointer to the port_info instance
3497  *
3498  * Returns pointer to the successfully allocated VSI software struct
3499  * on success, otherwise returns NULL on failure.
3500  */
3501 struct ice_vsi *
ice_lb_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi)3502 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3503 {
3504 	return ice_vsi_setup(pf, pi, ICE_VSI_LB, NULL, NULL);
3505 }
3506 
3507 /**
3508  * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
3509  * @netdev: network interface to be adjusted
3510  * @proto: VLAN TPID
3511  * @vid: VLAN ID to be added
3512  *
3513  * net_device_ops implementation for adding VLAN IDs
3514  */
3515 static int
ice_vlan_rx_add_vid(struct net_device * netdev,__be16 proto,u16 vid)3516 ice_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
3517 {
3518 	struct ice_netdev_priv *np = netdev_priv(netdev);
3519 	struct ice_vsi_vlan_ops *vlan_ops;
3520 	struct ice_vsi *vsi = np->vsi;
3521 	struct ice_vlan vlan;
3522 	int ret;
3523 
3524 	/* VLAN 0 is added by default during load/reset */
3525 	if (!vid)
3526 		return 0;
3527 
3528 	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3529 		usleep_range(1000, 2000);
3530 
3531 	/* Add multicast promisc rule for the VLAN ID to be added if
3532 	 * all-multicast is currently enabled.
3533 	 */
3534 	if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3535 		ret = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3536 					       ICE_MCAST_VLAN_PROMISC_BITS,
3537 					       vid);
3538 		if (ret)
3539 			goto finish;
3540 	}
3541 
3542 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3543 
3544 	/* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
3545 	 * packets aren't pruned by the device's internal switch on Rx
3546 	 */
3547 	vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3548 	ret = vlan_ops->add_vlan(vsi, &vlan);
3549 	if (ret)
3550 		goto finish;
3551 
3552 	/* If all-multicast is currently enabled and this VLAN ID is only one
3553 	 * besides VLAN-0 we have to update look-up type of multicast promisc
3554 	 * rule for VLAN-0 from ICE_SW_LKUP_PROMISC to ICE_SW_LKUP_PROMISC_VLAN.
3555 	 */
3556 	if ((vsi->current_netdev_flags & IFF_ALLMULTI) &&
3557 	    ice_vsi_num_non_zero_vlans(vsi) == 1) {
3558 		ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3559 					   ICE_MCAST_PROMISC_BITS, 0);
3560 		ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3561 					 ICE_MCAST_VLAN_PROMISC_BITS, 0);
3562 	}
3563 
3564 finish:
3565 	clear_bit(ICE_CFG_BUSY, vsi->state);
3566 
3567 	return ret;
3568 }
3569 
3570 /**
3571  * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
3572  * @netdev: network interface to be adjusted
3573  * @proto: VLAN TPID
3574  * @vid: VLAN ID to be removed
3575  *
3576  * net_device_ops implementation for removing VLAN IDs
3577  */
3578 static int
ice_vlan_rx_kill_vid(struct net_device * netdev,__be16 proto,u16 vid)3579 ice_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
3580 {
3581 	struct ice_netdev_priv *np = netdev_priv(netdev);
3582 	struct ice_vsi_vlan_ops *vlan_ops;
3583 	struct ice_vsi *vsi = np->vsi;
3584 	struct ice_vlan vlan;
3585 	int ret;
3586 
3587 	/* don't allow removal of VLAN 0 */
3588 	if (!vid)
3589 		return 0;
3590 
3591 	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3592 		usleep_range(1000, 2000);
3593 
3594 	ret = ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3595 				    ICE_MCAST_VLAN_PROMISC_BITS, vid);
3596 	if (ret) {
3597 		netdev_err(netdev, "Error clearing multicast promiscuous mode on VSI %i\n",
3598 			   vsi->vsi_num);
3599 		vsi->current_netdev_flags |= IFF_ALLMULTI;
3600 	}
3601 
3602 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3603 
3604 	/* Make sure VLAN delete is successful before updating VLAN
3605 	 * information
3606 	 */
3607 	vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3608 	ret = vlan_ops->del_vlan(vsi, &vlan);
3609 	if (ret)
3610 		goto finish;
3611 
3612 	/* Remove multicast promisc rule for the removed VLAN ID if
3613 	 * all-multicast is enabled.
3614 	 */
3615 	if (vsi->current_netdev_flags & IFF_ALLMULTI)
3616 		ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3617 					   ICE_MCAST_VLAN_PROMISC_BITS, vid);
3618 
3619 	if (!ice_vsi_has_non_zero_vlans(vsi)) {
3620 		/* Update look-up type of multicast promisc rule for VLAN 0
3621 		 * from ICE_SW_LKUP_PROMISC_VLAN to ICE_SW_LKUP_PROMISC when
3622 		 * all-multicast is enabled and VLAN 0 is the only VLAN rule.
3623 		 */
3624 		if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3625 			ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3626 						   ICE_MCAST_VLAN_PROMISC_BITS,
3627 						   0);
3628 			ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3629 						 ICE_MCAST_PROMISC_BITS, 0);
3630 		}
3631 	}
3632 
3633 finish:
3634 	clear_bit(ICE_CFG_BUSY, vsi->state);
3635 
3636 	return ret;
3637 }
3638 
3639 /**
3640  * ice_rep_indr_tc_block_unbind
3641  * @cb_priv: indirection block private data
3642  */
ice_rep_indr_tc_block_unbind(void * cb_priv)3643 static void ice_rep_indr_tc_block_unbind(void *cb_priv)
3644 {
3645 	struct ice_indr_block_priv *indr_priv = cb_priv;
3646 
3647 	list_del(&indr_priv->list);
3648 	kfree(indr_priv);
3649 }
3650 
3651 /**
3652  * ice_tc_indir_block_unregister - Unregister TC indirect block notifications
3653  * @vsi: VSI struct which has the netdev
3654  */
ice_tc_indir_block_unregister(struct ice_vsi * vsi)3655 static void ice_tc_indir_block_unregister(struct ice_vsi *vsi)
3656 {
3657 	struct ice_netdev_priv *np = netdev_priv(vsi->netdev);
3658 
3659 	flow_indr_dev_unregister(ice_indr_setup_tc_cb, np,
3660 				 ice_rep_indr_tc_block_unbind);
3661 }
3662 
3663 /**
3664  * ice_tc_indir_block_remove - clean indirect TC block notifications
3665  * @pf: PF structure
3666  */
ice_tc_indir_block_remove(struct ice_pf * pf)3667 static void ice_tc_indir_block_remove(struct ice_pf *pf)
3668 {
3669 	struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
3670 
3671 	if (!pf_vsi)
3672 		return;
3673 
3674 	ice_tc_indir_block_unregister(pf_vsi);
3675 }
3676 
3677 /**
3678  * ice_tc_indir_block_register - Register TC indirect block notifications
3679  * @vsi: VSI struct which has the netdev
3680  *
3681  * Returns 0 on success, negative value on failure
3682  */
ice_tc_indir_block_register(struct ice_vsi * vsi)3683 static int ice_tc_indir_block_register(struct ice_vsi *vsi)
3684 {
3685 	struct ice_netdev_priv *np;
3686 
3687 	if (!vsi || !vsi->netdev)
3688 		return -EINVAL;
3689 
3690 	np = netdev_priv(vsi->netdev);
3691 
3692 	INIT_LIST_HEAD(&np->tc_indr_block_priv_list);
3693 	return flow_indr_dev_register(ice_indr_setup_tc_cb, np);
3694 }
3695 
3696 /**
3697  * ice_setup_pf_sw - Setup the HW switch on startup or after reset
3698  * @pf: board private structure
3699  *
3700  * Returns 0 on success, negative value on failure
3701  */
ice_setup_pf_sw(struct ice_pf * pf)3702 static int ice_setup_pf_sw(struct ice_pf *pf)
3703 {
3704 	struct device *dev = ice_pf_to_dev(pf);
3705 	bool dvm = ice_is_dvm_ena(&pf->hw);
3706 	struct ice_vsi *vsi;
3707 	int status;
3708 
3709 	if (ice_is_reset_in_progress(pf->state))
3710 		return -EBUSY;
3711 
3712 	status = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
3713 	if (status)
3714 		return -EIO;
3715 
3716 	vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
3717 	if (!vsi)
3718 		return -ENOMEM;
3719 
3720 	/* init channel list */
3721 	INIT_LIST_HEAD(&vsi->ch_list);
3722 
3723 	status = ice_cfg_netdev(vsi);
3724 	if (status)
3725 		goto unroll_vsi_setup;
3726 	/* netdev has to be configured before setting frame size */
3727 	ice_vsi_cfg_frame_size(vsi);
3728 
3729 	/* init indirect block notifications */
3730 	status = ice_tc_indir_block_register(vsi);
3731 	if (status) {
3732 		dev_err(dev, "Failed to register netdev notifier\n");
3733 		goto unroll_cfg_netdev;
3734 	}
3735 
3736 	/* Setup DCB netlink interface */
3737 	ice_dcbnl_setup(vsi);
3738 
3739 	/* registering the NAPI handler requires both the queues and
3740 	 * netdev to be created, which are done in ice_pf_vsi_setup()
3741 	 * and ice_cfg_netdev() respectively
3742 	 */
3743 	ice_napi_add(vsi);
3744 
3745 	status = ice_init_mac_fltr(pf);
3746 	if (status)
3747 		goto unroll_napi_add;
3748 
3749 	return 0;
3750 
3751 unroll_napi_add:
3752 	ice_tc_indir_block_unregister(vsi);
3753 unroll_cfg_netdev:
3754 	if (vsi) {
3755 		ice_napi_del(vsi);
3756 		if (vsi->netdev) {
3757 			clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
3758 			free_netdev(vsi->netdev);
3759 			vsi->netdev = NULL;
3760 		}
3761 	}
3762 
3763 unroll_vsi_setup:
3764 	ice_vsi_release(vsi);
3765 	return status;
3766 }
3767 
3768 /**
3769  * ice_get_avail_q_count - Get count of queues in use
3770  * @pf_qmap: bitmap to get queue use count from
3771  * @lock: pointer to a mutex that protects access to pf_qmap
3772  * @size: size of the bitmap
3773  */
3774 static u16
ice_get_avail_q_count(unsigned long * pf_qmap,struct mutex * lock,u16 size)3775 ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
3776 {
3777 	unsigned long bit;
3778 	u16 count = 0;
3779 
3780 	mutex_lock(lock);
3781 	for_each_clear_bit(bit, pf_qmap, size)
3782 		count++;
3783 	mutex_unlock(lock);
3784 
3785 	return count;
3786 }
3787 
3788 /**
3789  * ice_get_avail_txq_count - Get count of Tx queues in use
3790  * @pf: pointer to an ice_pf instance
3791  */
ice_get_avail_txq_count(struct ice_pf * pf)3792 u16 ice_get_avail_txq_count(struct ice_pf *pf)
3793 {
3794 	return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
3795 				     pf->max_pf_txqs);
3796 }
3797 
3798 /**
3799  * ice_get_avail_rxq_count - Get count of Rx queues in use
3800  * @pf: pointer to an ice_pf instance
3801  */
ice_get_avail_rxq_count(struct ice_pf * pf)3802 u16 ice_get_avail_rxq_count(struct ice_pf *pf)
3803 {
3804 	return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
3805 				     pf->max_pf_rxqs);
3806 }
3807 
3808 /**
3809  * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3810  * @pf: board private structure to initialize
3811  */
ice_deinit_pf(struct ice_pf * pf)3812 static void ice_deinit_pf(struct ice_pf *pf)
3813 {
3814 	ice_service_task_stop(pf);
3815 	mutex_destroy(&pf->adev_mutex);
3816 	mutex_destroy(&pf->sw_mutex);
3817 	mutex_destroy(&pf->tc_mutex);
3818 	mutex_destroy(&pf->avail_q_mutex);
3819 	mutex_destroy(&pf->vfs.table_lock);
3820 
3821 	if (pf->avail_txqs) {
3822 		bitmap_free(pf->avail_txqs);
3823 		pf->avail_txqs = NULL;
3824 	}
3825 
3826 	if (pf->avail_rxqs) {
3827 		bitmap_free(pf->avail_rxqs);
3828 		pf->avail_rxqs = NULL;
3829 	}
3830 
3831 	if (pf->ptp.clock)
3832 		ptp_clock_unregister(pf->ptp.clock);
3833 }
3834 
3835 /**
3836  * ice_set_pf_caps - set PFs capability flags
3837  * @pf: pointer to the PF instance
3838  */
ice_set_pf_caps(struct ice_pf * pf)3839 static void ice_set_pf_caps(struct ice_pf *pf)
3840 {
3841 	struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
3842 
3843 	clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3844 	if (func_caps->common_cap.rdma)
3845 		set_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3846 	clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3847 	if (func_caps->common_cap.dcb)
3848 		set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3849 	clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3850 	if (func_caps->common_cap.sr_iov_1_1) {
3851 		set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3852 		pf->vfs.num_supported = min_t(int, func_caps->num_allocd_vfs,
3853 					      ICE_MAX_SRIOV_VFS);
3854 	}
3855 	clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
3856 	if (func_caps->common_cap.rss_table_size)
3857 		set_bit(ICE_FLAG_RSS_ENA, pf->flags);
3858 
3859 	clear_bit(ICE_FLAG_FD_ENA, pf->flags);
3860 	if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
3861 		u16 unused;
3862 
3863 		/* ctrl_vsi_idx will be set to a valid value when flow director
3864 		 * is setup by ice_init_fdir
3865 		 */
3866 		pf->ctrl_vsi_idx = ICE_NO_VSI;
3867 		set_bit(ICE_FLAG_FD_ENA, pf->flags);
3868 		/* force guaranteed filter pool for PF */
3869 		ice_alloc_fd_guar_item(&pf->hw, &unused,
3870 				       func_caps->fd_fltr_guar);
3871 		/* force shared filter pool for PF */
3872 		ice_alloc_fd_shrd_item(&pf->hw, &unused,
3873 				       func_caps->fd_fltr_best_effort);
3874 	}
3875 
3876 	clear_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
3877 	if (func_caps->common_cap.ieee_1588)
3878 		set_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
3879 
3880 	pf->max_pf_txqs = func_caps->common_cap.num_txq;
3881 	pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
3882 }
3883 
3884 /**
3885  * ice_init_pf - Initialize general software structures (struct ice_pf)
3886  * @pf: board private structure to initialize
3887  */
ice_init_pf(struct ice_pf * pf)3888 static int ice_init_pf(struct ice_pf *pf)
3889 {
3890 	ice_set_pf_caps(pf);
3891 
3892 	mutex_init(&pf->sw_mutex);
3893 	mutex_init(&pf->tc_mutex);
3894 	mutex_init(&pf->adev_mutex);
3895 
3896 	INIT_HLIST_HEAD(&pf->aq_wait_list);
3897 	spin_lock_init(&pf->aq_wait_lock);
3898 	init_waitqueue_head(&pf->aq_wait_queue);
3899 
3900 	init_waitqueue_head(&pf->reset_wait_queue);
3901 
3902 	/* setup service timer and periodic service task */
3903 	timer_setup(&pf->serv_tmr, ice_service_timer, 0);
3904 	pf->serv_tmr_period = HZ;
3905 	INIT_WORK(&pf->serv_task, ice_service_task);
3906 	clear_bit(ICE_SERVICE_SCHED, pf->state);
3907 
3908 	mutex_init(&pf->avail_q_mutex);
3909 	pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
3910 	if (!pf->avail_txqs)
3911 		return -ENOMEM;
3912 
3913 	pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
3914 	if (!pf->avail_rxqs) {
3915 		bitmap_free(pf->avail_txqs);
3916 		pf->avail_txqs = NULL;
3917 		return -ENOMEM;
3918 	}
3919 
3920 	mutex_init(&pf->vfs.table_lock);
3921 	hash_init(pf->vfs.table);
3922 
3923 	return 0;
3924 }
3925 
3926 /**
3927  * ice_ena_msix_range - Request a range of MSIX vectors from the OS
3928  * @pf: board private structure
3929  *
3930  * compute the number of MSIX vectors required (v_budget) and request from
3931  * the OS. Return the number of vectors reserved or negative on failure
3932  */
ice_ena_msix_range(struct ice_pf * pf)3933 static int ice_ena_msix_range(struct ice_pf *pf)
3934 {
3935 	int num_cpus, v_left, v_actual, v_other, v_budget = 0;
3936 	struct device *dev = ice_pf_to_dev(pf);
3937 	int needed, err, i;
3938 
3939 	v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
3940 	num_cpus = num_online_cpus();
3941 
3942 	/* reserve for LAN miscellaneous handler */
3943 	needed = ICE_MIN_LAN_OICR_MSIX;
3944 	if (v_left < needed)
3945 		goto no_hw_vecs_left_err;
3946 	v_budget += needed;
3947 	v_left -= needed;
3948 
3949 	/* reserve for flow director */
3950 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
3951 		needed = ICE_FDIR_MSIX;
3952 		if (v_left < needed)
3953 			goto no_hw_vecs_left_err;
3954 		v_budget += needed;
3955 		v_left -= needed;
3956 	}
3957 
3958 	/* reserve for switchdev */
3959 	needed = ICE_ESWITCH_MSIX;
3960 	if (v_left < needed)
3961 		goto no_hw_vecs_left_err;
3962 	v_budget += needed;
3963 	v_left -= needed;
3964 
3965 	/* total used for non-traffic vectors */
3966 	v_other = v_budget;
3967 
3968 	/* reserve vectors for LAN traffic */
3969 	needed = num_cpus;
3970 	if (v_left < needed)
3971 		goto no_hw_vecs_left_err;
3972 	pf->num_lan_msix = needed;
3973 	v_budget += needed;
3974 	v_left -= needed;
3975 
3976 	/* reserve vectors for RDMA auxiliary driver */
3977 	if (ice_is_rdma_ena(pf)) {
3978 		needed = num_cpus + ICE_RDMA_NUM_AEQ_MSIX;
3979 		if (v_left < needed)
3980 			goto no_hw_vecs_left_err;
3981 		pf->num_rdma_msix = needed;
3982 		v_budget += needed;
3983 		v_left -= needed;
3984 	}
3985 
3986 	pf->msix_entries = devm_kcalloc(dev, v_budget,
3987 					sizeof(*pf->msix_entries), GFP_KERNEL);
3988 	if (!pf->msix_entries) {
3989 		err = -ENOMEM;
3990 		goto exit_err;
3991 	}
3992 
3993 	for (i = 0; i < v_budget; i++)
3994 		pf->msix_entries[i].entry = i;
3995 
3996 	/* actually reserve the vectors */
3997 	v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
3998 					 ICE_MIN_MSIX, v_budget);
3999 	if (v_actual < 0) {
4000 		dev_err(dev, "unable to reserve MSI-X vectors\n");
4001 		err = v_actual;
4002 		goto msix_err;
4003 	}
4004 
4005 	if (v_actual < v_budget) {
4006 		dev_warn(dev, "not enough OS MSI-X vectors. requested = %d, obtained = %d\n",
4007 			 v_budget, v_actual);
4008 
4009 		if (v_actual < ICE_MIN_MSIX) {
4010 			/* error if we can't get minimum vectors */
4011 			pci_disable_msix(pf->pdev);
4012 			err = -ERANGE;
4013 			goto msix_err;
4014 		} else {
4015 			int v_remain = v_actual - v_other;
4016 			int v_rdma = 0, v_min_rdma = 0;
4017 
4018 			if (ice_is_rdma_ena(pf)) {
4019 				/* Need at least 1 interrupt in addition to
4020 				 * AEQ MSIX
4021 				 */
4022 				v_rdma = ICE_RDMA_NUM_AEQ_MSIX + 1;
4023 				v_min_rdma = ICE_MIN_RDMA_MSIX;
4024 			}
4025 
4026 			if (v_actual == ICE_MIN_MSIX ||
4027 			    v_remain < ICE_MIN_LAN_TXRX_MSIX + v_min_rdma) {
4028 				dev_warn(dev, "Not enough MSI-X vectors to support RDMA.\n");
4029 				clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
4030 
4031 				pf->num_rdma_msix = 0;
4032 				pf->num_lan_msix = ICE_MIN_LAN_TXRX_MSIX;
4033 			} else if ((v_remain < ICE_MIN_LAN_TXRX_MSIX + v_rdma) ||
4034 				   (v_remain - v_rdma < v_rdma)) {
4035 				/* Support minimum RDMA and give remaining
4036 				 * vectors to LAN MSIX
4037 				 */
4038 				pf->num_rdma_msix = v_min_rdma;
4039 				pf->num_lan_msix = v_remain - v_min_rdma;
4040 			} else {
4041 				/* Split remaining MSIX with RDMA after
4042 				 * accounting for AEQ MSIX
4043 				 */
4044 				pf->num_rdma_msix = (v_remain - ICE_RDMA_NUM_AEQ_MSIX) / 2 +
4045 						    ICE_RDMA_NUM_AEQ_MSIX;
4046 				pf->num_lan_msix = v_remain - pf->num_rdma_msix;
4047 			}
4048 
4049 			dev_notice(dev, "Enabled %d MSI-X vectors for LAN traffic.\n",
4050 				   pf->num_lan_msix);
4051 
4052 			if (ice_is_rdma_ena(pf))
4053 				dev_notice(dev, "Enabled %d MSI-X vectors for RDMA.\n",
4054 					   pf->num_rdma_msix);
4055 		}
4056 	}
4057 
4058 	return v_actual;
4059 
4060 msix_err:
4061 	devm_kfree(dev, pf->msix_entries);
4062 	goto exit_err;
4063 
4064 no_hw_vecs_left_err:
4065 	dev_err(dev, "not enough device MSI-X vectors. requested = %d, available = %d\n",
4066 		needed, v_left);
4067 	err = -ERANGE;
4068 exit_err:
4069 	pf->num_rdma_msix = 0;
4070 	pf->num_lan_msix = 0;
4071 	return err;
4072 }
4073 
4074 /**
4075  * ice_dis_msix - Disable MSI-X interrupt setup in OS
4076  * @pf: board private structure
4077  */
ice_dis_msix(struct ice_pf * pf)4078 static void ice_dis_msix(struct ice_pf *pf)
4079 {
4080 	pci_disable_msix(pf->pdev);
4081 	devm_kfree(ice_pf_to_dev(pf), pf->msix_entries);
4082 	pf->msix_entries = NULL;
4083 }
4084 
4085 /**
4086  * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
4087  * @pf: board private structure
4088  */
ice_clear_interrupt_scheme(struct ice_pf * pf)4089 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
4090 {
4091 	ice_dis_msix(pf);
4092 
4093 	if (pf->irq_tracker) {
4094 		devm_kfree(ice_pf_to_dev(pf), pf->irq_tracker);
4095 		pf->irq_tracker = NULL;
4096 	}
4097 }
4098 
4099 /**
4100  * ice_init_interrupt_scheme - Determine proper interrupt scheme
4101  * @pf: board private structure to initialize
4102  */
ice_init_interrupt_scheme(struct ice_pf * pf)4103 static int ice_init_interrupt_scheme(struct ice_pf *pf)
4104 {
4105 	int vectors;
4106 
4107 	vectors = ice_ena_msix_range(pf);
4108 
4109 	if (vectors < 0)
4110 		return vectors;
4111 
4112 	/* set up vector assignment tracking */
4113 	pf->irq_tracker = devm_kzalloc(ice_pf_to_dev(pf),
4114 				       struct_size(pf->irq_tracker, list, vectors),
4115 				       GFP_KERNEL);
4116 	if (!pf->irq_tracker) {
4117 		ice_dis_msix(pf);
4118 		return -ENOMEM;
4119 	}
4120 
4121 	/* populate SW interrupts pool with number of OS granted IRQs. */
4122 	pf->num_avail_sw_msix = (u16)vectors;
4123 	pf->irq_tracker->num_entries = (u16)vectors;
4124 	pf->irq_tracker->end = pf->irq_tracker->num_entries;
4125 
4126 	return 0;
4127 }
4128 
4129 /**
4130  * ice_is_wol_supported - check if WoL is supported
4131  * @hw: pointer to hardware info
4132  *
4133  * Check if WoL is supported based on the HW configuration.
4134  * Returns true if NVM supports and enables WoL for this port, false otherwise
4135  */
ice_is_wol_supported(struct ice_hw * hw)4136 bool ice_is_wol_supported(struct ice_hw *hw)
4137 {
4138 	u16 wol_ctrl;
4139 
4140 	/* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
4141 	 * word) indicates WoL is not supported on the corresponding PF ID.
4142 	 */
4143 	if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
4144 		return false;
4145 
4146 	return !(BIT(hw->port_info->lport) & wol_ctrl);
4147 }
4148 
4149 /**
4150  * ice_vsi_recfg_qs - Change the number of queues on a VSI
4151  * @vsi: VSI being changed
4152  * @new_rx: new number of Rx queues
4153  * @new_tx: new number of Tx queues
4154  *
4155  * Only change the number of queues if new_tx, or new_rx is non-0.
4156  *
4157  * Returns 0 on success.
4158  */
ice_vsi_recfg_qs(struct ice_vsi * vsi,int new_rx,int new_tx)4159 int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx)
4160 {
4161 	struct ice_pf *pf = vsi->back;
4162 	int err = 0, timeout = 50;
4163 
4164 	if (!new_rx && !new_tx)
4165 		return -EINVAL;
4166 
4167 	while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
4168 		timeout--;
4169 		if (!timeout)
4170 			return -EBUSY;
4171 		usleep_range(1000, 2000);
4172 	}
4173 
4174 	if (new_tx)
4175 		vsi->req_txq = (u16)new_tx;
4176 	if (new_rx)
4177 		vsi->req_rxq = (u16)new_rx;
4178 
4179 	/* set for the next time the netdev is started */
4180 	if (!netif_running(vsi->netdev)) {
4181 		ice_vsi_rebuild(vsi, false);
4182 		dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
4183 		goto done;
4184 	}
4185 
4186 	ice_vsi_close(vsi);
4187 	ice_vsi_rebuild(vsi, false);
4188 	ice_pf_dcb_recfg(pf);
4189 	ice_vsi_open(vsi);
4190 done:
4191 	clear_bit(ICE_CFG_BUSY, pf->state);
4192 	return err;
4193 }
4194 
4195 /**
4196  * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
4197  * @pf: PF to configure
4198  *
4199  * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
4200  * VSI can still Tx/Rx VLAN tagged packets.
4201  */
ice_set_safe_mode_vlan_cfg(struct ice_pf * pf)4202 static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
4203 {
4204 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
4205 	struct ice_vsi_ctx *ctxt;
4206 	struct ice_hw *hw;
4207 	int status;
4208 
4209 	if (!vsi)
4210 		return;
4211 
4212 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
4213 	if (!ctxt)
4214 		return;
4215 
4216 	hw = &pf->hw;
4217 	ctxt->info = vsi->info;
4218 
4219 	ctxt->info.valid_sections =
4220 		cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
4221 			    ICE_AQ_VSI_PROP_SECURITY_VALID |
4222 			    ICE_AQ_VSI_PROP_SW_VALID);
4223 
4224 	/* disable VLAN anti-spoof */
4225 	ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4226 				  ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4227 
4228 	/* disable VLAN pruning and keep all other settings */
4229 	ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
4230 
4231 	/* allow all VLANs on Tx and don't strip on Rx */
4232 	ctxt->info.inner_vlan_flags = ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL |
4233 		ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING;
4234 
4235 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4236 	if (status) {
4237 		dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %d aq_err %s\n",
4238 			status, ice_aq_str(hw->adminq.sq_last_status));
4239 	} else {
4240 		vsi->info.sec_flags = ctxt->info.sec_flags;
4241 		vsi->info.sw_flags2 = ctxt->info.sw_flags2;
4242 		vsi->info.inner_vlan_flags = ctxt->info.inner_vlan_flags;
4243 	}
4244 
4245 	kfree(ctxt);
4246 }
4247 
4248 /**
4249  * ice_log_pkg_init - log result of DDP package load
4250  * @hw: pointer to hardware info
4251  * @state: state of package load
4252  */
ice_log_pkg_init(struct ice_hw * hw,enum ice_ddp_state state)4253 static void ice_log_pkg_init(struct ice_hw *hw, enum ice_ddp_state state)
4254 {
4255 	struct ice_pf *pf = hw->back;
4256 	struct device *dev;
4257 
4258 	dev = ice_pf_to_dev(pf);
4259 
4260 	switch (state) {
4261 	case ICE_DDP_PKG_SUCCESS:
4262 		dev_info(dev, "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
4263 			 hw->active_pkg_name,
4264 			 hw->active_pkg_ver.major,
4265 			 hw->active_pkg_ver.minor,
4266 			 hw->active_pkg_ver.update,
4267 			 hw->active_pkg_ver.draft);
4268 		break;
4269 	case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
4270 		dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
4271 			 hw->active_pkg_name,
4272 			 hw->active_pkg_ver.major,
4273 			 hw->active_pkg_ver.minor,
4274 			 hw->active_pkg_ver.update,
4275 			 hw->active_pkg_ver.draft);
4276 		break;
4277 	case ICE_DDP_PKG_ALREADY_LOADED_NOT_SUPPORTED:
4278 		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",
4279 			hw->active_pkg_name,
4280 			hw->active_pkg_ver.major,
4281 			hw->active_pkg_ver.minor,
4282 			ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4283 		break;
4284 	case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
4285 		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",
4286 			 hw->active_pkg_name,
4287 			 hw->active_pkg_ver.major,
4288 			 hw->active_pkg_ver.minor,
4289 			 hw->active_pkg_ver.update,
4290 			 hw->active_pkg_ver.draft,
4291 			 hw->pkg_name,
4292 			 hw->pkg_ver.major,
4293 			 hw->pkg_ver.minor,
4294 			 hw->pkg_ver.update,
4295 			 hw->pkg_ver.draft);
4296 		break;
4297 	case ICE_DDP_PKG_FW_MISMATCH:
4298 		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");
4299 		break;
4300 	case ICE_DDP_PKG_INVALID_FILE:
4301 		dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
4302 		break;
4303 	case ICE_DDP_PKG_FILE_VERSION_TOO_HIGH:
4304 		dev_err(dev, "The DDP package file version is higher than the driver supports.  Please use an updated driver.  Entering Safe Mode.\n");
4305 		break;
4306 	case ICE_DDP_PKG_FILE_VERSION_TOO_LOW:
4307 		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",
4308 			ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4309 		break;
4310 	case ICE_DDP_PKG_FILE_SIGNATURE_INVALID:
4311 		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");
4312 		break;
4313 	case ICE_DDP_PKG_FILE_REVISION_TOO_LOW:
4314 		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");
4315 		break;
4316 	case ICE_DDP_PKG_LOAD_ERROR:
4317 		dev_err(dev, "An error occurred on the device while loading the DDP package.  The device will be reset.\n");
4318 		/* poll for reset to complete */
4319 		if (ice_check_reset(hw))
4320 			dev_err(dev, "Error resetting device. Please reload the driver\n");
4321 		break;
4322 	case ICE_DDP_PKG_ERR:
4323 	default:
4324 		dev_err(dev, "An unknown error occurred when loading the DDP package.  Entering Safe Mode.\n");
4325 		break;
4326 	}
4327 }
4328 
4329 /**
4330  * ice_load_pkg - load/reload the DDP Package file
4331  * @firmware: firmware structure when firmware requested or NULL for reload
4332  * @pf: pointer to the PF instance
4333  *
4334  * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
4335  * initialize HW tables.
4336  */
4337 static void
ice_load_pkg(const struct firmware * firmware,struct ice_pf * pf)4338 ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
4339 {
4340 	enum ice_ddp_state state = ICE_DDP_PKG_ERR;
4341 	struct device *dev = ice_pf_to_dev(pf);
4342 	struct ice_hw *hw = &pf->hw;
4343 
4344 	/* Load DDP Package */
4345 	if (firmware && !hw->pkg_copy) {
4346 		state = ice_copy_and_init_pkg(hw, firmware->data,
4347 					      firmware->size);
4348 		ice_log_pkg_init(hw, state);
4349 	} else if (!firmware && hw->pkg_copy) {
4350 		/* Reload package during rebuild after CORER/GLOBR reset */
4351 		state = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
4352 		ice_log_pkg_init(hw, state);
4353 	} else {
4354 		dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
4355 	}
4356 
4357 	if (!ice_is_init_pkg_successful(state)) {
4358 		/* Safe Mode */
4359 		clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4360 		return;
4361 	}
4362 
4363 	/* Successful download package is the precondition for advanced
4364 	 * features, hence setting the ICE_FLAG_ADV_FEATURES flag
4365 	 */
4366 	set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4367 }
4368 
4369 /**
4370  * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
4371  * @pf: pointer to the PF structure
4372  *
4373  * There is no error returned here because the driver should be able to handle
4374  * 128 Byte cache lines, so we only print a warning in case issues are seen,
4375  * specifically with Tx.
4376  */
ice_verify_cacheline_size(struct ice_pf * pf)4377 static void ice_verify_cacheline_size(struct ice_pf *pf)
4378 {
4379 	if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
4380 		dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
4381 			 ICE_CACHE_LINE_BYTES);
4382 }
4383 
4384 /**
4385  * ice_send_version - update firmware with driver version
4386  * @pf: PF struct
4387  *
4388  * Returns 0 on success, else error code
4389  */
ice_send_version(struct ice_pf * pf)4390 static int ice_send_version(struct ice_pf *pf)
4391 {
4392 	struct ice_driver_ver dv;
4393 
4394 	dv.major_ver = 0xff;
4395 	dv.minor_ver = 0xff;
4396 	dv.build_ver = 0xff;
4397 	dv.subbuild_ver = 0;
4398 	strscpy((char *)dv.driver_string, UTS_RELEASE,
4399 		sizeof(dv.driver_string));
4400 	return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
4401 }
4402 
4403 /**
4404  * ice_init_fdir - Initialize flow director VSI and configuration
4405  * @pf: pointer to the PF instance
4406  *
4407  * returns 0 on success, negative on error
4408  */
ice_init_fdir(struct ice_pf * pf)4409 static int ice_init_fdir(struct ice_pf *pf)
4410 {
4411 	struct device *dev = ice_pf_to_dev(pf);
4412 	struct ice_vsi *ctrl_vsi;
4413 	int err;
4414 
4415 	/* Side Band Flow Director needs to have a control VSI.
4416 	 * Allocate it and store it in the PF.
4417 	 */
4418 	ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
4419 	if (!ctrl_vsi) {
4420 		dev_dbg(dev, "could not create control VSI\n");
4421 		return -ENOMEM;
4422 	}
4423 
4424 	err = ice_vsi_open_ctrl(ctrl_vsi);
4425 	if (err) {
4426 		dev_dbg(dev, "could not open control VSI\n");
4427 		goto err_vsi_open;
4428 	}
4429 
4430 	mutex_init(&pf->hw.fdir_fltr_lock);
4431 
4432 	err = ice_fdir_create_dflt_rules(pf);
4433 	if (err)
4434 		goto err_fdir_rule;
4435 
4436 	return 0;
4437 
4438 err_fdir_rule:
4439 	ice_fdir_release_flows(&pf->hw);
4440 	ice_vsi_close(ctrl_vsi);
4441 err_vsi_open:
4442 	ice_vsi_release(ctrl_vsi);
4443 	if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4444 		pf->vsi[pf->ctrl_vsi_idx] = NULL;
4445 		pf->ctrl_vsi_idx = ICE_NO_VSI;
4446 	}
4447 	return err;
4448 }
4449 
4450 /**
4451  * ice_get_opt_fw_name - return optional firmware file name or NULL
4452  * @pf: pointer to the PF instance
4453  */
ice_get_opt_fw_name(struct ice_pf * pf)4454 static char *ice_get_opt_fw_name(struct ice_pf *pf)
4455 {
4456 	/* Optional firmware name same as default with additional dash
4457 	 * followed by a EUI-64 identifier (PCIe Device Serial Number)
4458 	 */
4459 	struct pci_dev *pdev = pf->pdev;
4460 	char *opt_fw_filename;
4461 	u64 dsn;
4462 
4463 	/* Determine the name of the optional file using the DSN (two
4464 	 * dwords following the start of the DSN Capability).
4465 	 */
4466 	dsn = pci_get_dsn(pdev);
4467 	if (!dsn)
4468 		return NULL;
4469 
4470 	opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
4471 	if (!opt_fw_filename)
4472 		return NULL;
4473 
4474 	snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
4475 		 ICE_DDP_PKG_PATH, dsn);
4476 
4477 	return opt_fw_filename;
4478 }
4479 
4480 /**
4481  * ice_request_fw - Device initialization routine
4482  * @pf: pointer to the PF instance
4483  */
ice_request_fw(struct ice_pf * pf)4484 static void ice_request_fw(struct ice_pf *pf)
4485 {
4486 	char *opt_fw_filename = ice_get_opt_fw_name(pf);
4487 	const struct firmware *firmware = NULL;
4488 	struct device *dev = ice_pf_to_dev(pf);
4489 	int err = 0;
4490 
4491 	/* optional device-specific DDP (if present) overrides the default DDP
4492 	 * package file. kernel logs a debug message if the file doesn't exist,
4493 	 * and warning messages for other errors.
4494 	 */
4495 	if (opt_fw_filename) {
4496 		err = firmware_request_nowarn(&firmware, opt_fw_filename, dev);
4497 		if (err) {
4498 			kfree(opt_fw_filename);
4499 			goto dflt_pkg_load;
4500 		}
4501 
4502 		/* request for firmware was successful. Download to device */
4503 		ice_load_pkg(firmware, pf);
4504 		kfree(opt_fw_filename);
4505 		release_firmware(firmware);
4506 		return;
4507 	}
4508 
4509 dflt_pkg_load:
4510 	err = request_firmware(&firmware, ICE_DDP_PKG_FILE, dev);
4511 	if (err) {
4512 		dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
4513 		return;
4514 	}
4515 
4516 	/* request for firmware was successful. Download to device */
4517 	ice_load_pkg(firmware, pf);
4518 	release_firmware(firmware);
4519 }
4520 
4521 /**
4522  * ice_print_wake_reason - show the wake up cause in the log
4523  * @pf: pointer to the PF struct
4524  */
ice_print_wake_reason(struct ice_pf * pf)4525 static void ice_print_wake_reason(struct ice_pf *pf)
4526 {
4527 	u32 wus = pf->wakeup_reason;
4528 	const char *wake_str;
4529 
4530 	/* if no wake event, nothing to print */
4531 	if (!wus)
4532 		return;
4533 
4534 	if (wus & PFPM_WUS_LNKC_M)
4535 		wake_str = "Link\n";
4536 	else if (wus & PFPM_WUS_MAG_M)
4537 		wake_str = "Magic Packet\n";
4538 	else if (wus & PFPM_WUS_MNG_M)
4539 		wake_str = "Management\n";
4540 	else if (wus & PFPM_WUS_FW_RST_WK_M)
4541 		wake_str = "Firmware Reset\n";
4542 	else
4543 		wake_str = "Unknown\n";
4544 
4545 	dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
4546 }
4547 
4548 /**
4549  * ice_register_netdev - register netdev and devlink port
4550  * @pf: pointer to the PF struct
4551  */
ice_register_netdev(struct ice_pf * pf)4552 static int ice_register_netdev(struct ice_pf *pf)
4553 {
4554 	struct ice_vsi *vsi;
4555 	int err = 0;
4556 
4557 	vsi = ice_get_main_vsi(pf);
4558 	if (!vsi || !vsi->netdev)
4559 		return -EIO;
4560 
4561 	err = register_netdev(vsi->netdev);
4562 	if (err)
4563 		goto err_register_netdev;
4564 
4565 	set_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4566 	netif_carrier_off(vsi->netdev);
4567 	netif_tx_stop_all_queues(vsi->netdev);
4568 	err = ice_devlink_create_pf_port(pf);
4569 	if (err)
4570 		goto err_devlink_create;
4571 
4572 	devlink_port_type_eth_set(&pf->devlink_port, vsi->netdev);
4573 
4574 	return 0;
4575 err_devlink_create:
4576 	unregister_netdev(vsi->netdev);
4577 	clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4578 err_register_netdev:
4579 	free_netdev(vsi->netdev);
4580 	vsi->netdev = NULL;
4581 	clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4582 	return err;
4583 }
4584 
4585 /**
4586  * ice_probe - Device initialization routine
4587  * @pdev: PCI device information struct
4588  * @ent: entry in ice_pci_tbl
4589  *
4590  * Returns 0 on success, negative on failure
4591  */
4592 static int
ice_probe(struct pci_dev * pdev,const struct pci_device_id __always_unused * ent)4593 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
4594 {
4595 	struct device *dev = &pdev->dev;
4596 	struct ice_pf *pf;
4597 	struct ice_hw *hw;
4598 	int i, err;
4599 
4600 	if (pdev->is_virtfn) {
4601 		dev_err(dev, "can't probe a virtual function\n");
4602 		return -EINVAL;
4603 	}
4604 
4605 	/* this driver uses devres, see
4606 	 * Documentation/driver-api/driver-model/devres.rst
4607 	 */
4608 	err = pcim_enable_device(pdev);
4609 	if (err)
4610 		return err;
4611 
4612 	err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), dev_driver_string(dev));
4613 	if (err) {
4614 		dev_err(dev, "BAR0 I/O map error %d\n", err);
4615 		return err;
4616 	}
4617 
4618 	pf = ice_allocate_pf(dev);
4619 	if (!pf)
4620 		return -ENOMEM;
4621 
4622 	/* initialize Auxiliary index to invalid value */
4623 	pf->aux_idx = -1;
4624 
4625 	/* set up for high or low DMA */
4626 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
4627 	if (err) {
4628 		dev_err(dev, "DMA configuration failed: 0x%x\n", err);
4629 		return err;
4630 	}
4631 
4632 	pci_enable_pcie_error_reporting(pdev);
4633 	pci_set_master(pdev);
4634 
4635 	pf->pdev = pdev;
4636 	pci_set_drvdata(pdev, pf);
4637 	set_bit(ICE_DOWN, pf->state);
4638 	/* Disable service task until DOWN bit is cleared */
4639 	set_bit(ICE_SERVICE_DIS, pf->state);
4640 
4641 	hw = &pf->hw;
4642 	hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
4643 	pci_save_state(pdev);
4644 
4645 	hw->back = pf;
4646 	hw->vendor_id = pdev->vendor;
4647 	hw->device_id = pdev->device;
4648 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
4649 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
4650 	hw->subsystem_device_id = pdev->subsystem_device;
4651 	hw->bus.device = PCI_SLOT(pdev->devfn);
4652 	hw->bus.func = PCI_FUNC(pdev->devfn);
4653 	ice_set_ctrlq_len(hw);
4654 
4655 	pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
4656 
4657 #ifndef CONFIG_DYNAMIC_DEBUG
4658 	if (debug < -1)
4659 		hw->debug_mask = debug;
4660 #endif
4661 
4662 	err = ice_init_hw(hw);
4663 	if (err) {
4664 		dev_err(dev, "ice_init_hw failed: %d\n", err);
4665 		err = -EIO;
4666 		goto err_exit_unroll;
4667 	}
4668 
4669 	ice_init_feature_support(pf);
4670 
4671 	ice_request_fw(pf);
4672 
4673 	/* if ice_request_fw fails, ICE_FLAG_ADV_FEATURES bit won't be
4674 	 * set in pf->state, which will cause ice_is_safe_mode to return
4675 	 * true
4676 	 */
4677 	if (ice_is_safe_mode(pf)) {
4678 		/* we already got function/device capabilities but these don't
4679 		 * reflect what the driver needs to do in safe mode. Instead of
4680 		 * adding conditional logic everywhere to ignore these
4681 		 * device/function capabilities, override them.
4682 		 */
4683 		ice_set_safe_mode_caps(hw);
4684 	}
4685 
4686 	hw->ucast_shared = true;
4687 
4688 	err = ice_init_pf(pf);
4689 	if (err) {
4690 		dev_err(dev, "ice_init_pf failed: %d\n", err);
4691 		goto err_init_pf_unroll;
4692 	}
4693 
4694 	ice_devlink_init_regions(pf);
4695 
4696 	pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4697 	pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4698 	pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
4699 	pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
4700 	i = 0;
4701 	if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4702 		pf->hw.udp_tunnel_nic.tables[i].n_entries =
4703 			pf->hw.tnl.valid_count[TNL_VXLAN];
4704 		pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
4705 			UDP_TUNNEL_TYPE_VXLAN;
4706 		i++;
4707 	}
4708 	if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4709 		pf->hw.udp_tunnel_nic.tables[i].n_entries =
4710 			pf->hw.tnl.valid_count[TNL_GENEVE];
4711 		pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
4712 			UDP_TUNNEL_TYPE_GENEVE;
4713 		i++;
4714 	}
4715 
4716 	pf->num_alloc_vsi = hw->func_caps.guar_num_vsi;
4717 	if (!pf->num_alloc_vsi) {
4718 		err = -EIO;
4719 		goto err_init_pf_unroll;
4720 	}
4721 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
4722 		dev_warn(&pf->pdev->dev,
4723 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
4724 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
4725 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
4726 	}
4727 
4728 	pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
4729 			       GFP_KERNEL);
4730 	if (!pf->vsi) {
4731 		err = -ENOMEM;
4732 		goto err_init_pf_unroll;
4733 	}
4734 
4735 	err = ice_init_interrupt_scheme(pf);
4736 	if (err) {
4737 		dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4738 		err = -EIO;
4739 		goto err_init_vsi_unroll;
4740 	}
4741 
4742 	/* In case of MSIX we are going to setup the misc vector right here
4743 	 * to handle admin queue events etc. In case of legacy and MSI
4744 	 * the misc functionality and queue processing is combined in
4745 	 * the same vector and that gets setup at open.
4746 	 */
4747 	err = ice_req_irq_msix_misc(pf);
4748 	if (err) {
4749 		dev_err(dev, "setup of misc vector failed: %d\n", err);
4750 		goto err_init_interrupt_unroll;
4751 	}
4752 
4753 	/* create switch struct for the switch element created by FW on boot */
4754 	pf->first_sw = devm_kzalloc(dev, sizeof(*pf->first_sw), GFP_KERNEL);
4755 	if (!pf->first_sw) {
4756 		err = -ENOMEM;
4757 		goto err_msix_misc_unroll;
4758 	}
4759 
4760 	if (hw->evb_veb)
4761 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4762 	else
4763 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
4764 
4765 	pf->first_sw->pf = pf;
4766 
4767 	/* record the sw_id available for later use */
4768 	pf->first_sw->sw_id = hw->port_info->sw_id;
4769 
4770 	err = ice_setup_pf_sw(pf);
4771 	if (err) {
4772 		dev_err(dev, "probe failed due to setup PF switch: %d\n", err);
4773 		goto err_alloc_sw_unroll;
4774 	}
4775 
4776 	clear_bit(ICE_SERVICE_DIS, pf->state);
4777 
4778 	/* tell the firmware we are up */
4779 	err = ice_send_version(pf);
4780 	if (err) {
4781 		dev_err(dev, "probe failed sending driver version %s. error: %d\n",
4782 			UTS_RELEASE, err);
4783 		goto err_send_version_unroll;
4784 	}
4785 
4786 	/* since everything is good, start the service timer */
4787 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4788 
4789 	err = ice_init_link_events(pf->hw.port_info);
4790 	if (err) {
4791 		dev_err(dev, "ice_init_link_events failed: %d\n", err);
4792 		goto err_send_version_unroll;
4793 	}
4794 
4795 	/* not a fatal error if this fails */
4796 	err = ice_init_nvm_phy_type(pf->hw.port_info);
4797 	if (err)
4798 		dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4799 
4800 	/* not a fatal error if this fails */
4801 	err = ice_update_link_info(pf->hw.port_info);
4802 	if (err)
4803 		dev_err(dev, "ice_update_link_info failed: %d\n", err);
4804 
4805 	ice_init_link_dflt_override(pf->hw.port_info);
4806 
4807 	ice_check_link_cfg_err(pf,
4808 			       pf->hw.port_info->phy.link_info.link_cfg_err);
4809 
4810 	/* if media available, initialize PHY settings */
4811 	if (pf->hw.port_info->phy.link_info.link_info &
4812 	    ICE_AQ_MEDIA_AVAILABLE) {
4813 		/* not a fatal error if this fails */
4814 		err = ice_init_phy_user_cfg(pf->hw.port_info);
4815 		if (err)
4816 			dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4817 
4818 		if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4819 			struct ice_vsi *vsi = ice_get_main_vsi(pf);
4820 
4821 			if (vsi)
4822 				ice_configure_phy(vsi);
4823 		}
4824 	} else {
4825 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4826 	}
4827 
4828 	ice_verify_cacheline_size(pf);
4829 
4830 	/* Save wakeup reason register for later use */
4831 	pf->wakeup_reason = rd32(hw, PFPM_WUS);
4832 
4833 	/* check for a power management event */
4834 	ice_print_wake_reason(pf);
4835 
4836 	/* clear wake status, all bits */
4837 	wr32(hw, PFPM_WUS, U32_MAX);
4838 
4839 	/* Disable WoL at init, wait for user to enable */
4840 	device_set_wakeup_enable(dev, false);
4841 
4842 	if (ice_is_safe_mode(pf)) {
4843 		ice_set_safe_mode_vlan_cfg(pf);
4844 		goto probe_done;
4845 	}
4846 
4847 	/* initialize DDP driven features */
4848 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4849 		ice_ptp_init(pf);
4850 
4851 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
4852 		ice_gnss_init(pf);
4853 
4854 	/* Note: Flow director init failure is non-fatal to load */
4855 	if (ice_init_fdir(pf))
4856 		dev_err(dev, "could not initialize flow director\n");
4857 
4858 	/* Note: DCB init failure is non-fatal to load */
4859 	if (ice_init_pf_dcb(pf, false)) {
4860 		clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4861 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4862 	} else {
4863 		ice_cfg_lldp_mib_change(&pf->hw, true);
4864 	}
4865 
4866 	if (ice_init_lag(pf))
4867 		dev_warn(dev, "Failed to init link aggregation support\n");
4868 
4869 	/* print PCI link speed and width */
4870 	pcie_print_link_status(pf->pdev);
4871 
4872 probe_done:
4873 	err = ice_register_netdev(pf);
4874 	if (err)
4875 		goto err_netdev_reg;
4876 
4877 	err = ice_devlink_register_params(pf);
4878 	if (err)
4879 		goto err_netdev_reg;
4880 
4881 	/* ready to go, so clear down state bit */
4882 	clear_bit(ICE_DOWN, pf->state);
4883 	if (ice_is_rdma_ena(pf)) {
4884 		pf->aux_idx = ida_alloc(&ice_aux_ida, GFP_KERNEL);
4885 		if (pf->aux_idx < 0) {
4886 			dev_err(dev, "Failed to allocate device ID for AUX driver\n");
4887 			err = -ENOMEM;
4888 			goto err_devlink_reg_param;
4889 		}
4890 
4891 		err = ice_init_rdma(pf);
4892 		if (err) {
4893 			dev_err(dev, "Failed to initialize RDMA: %d\n", err);
4894 			err = -EIO;
4895 			goto err_init_aux_unroll;
4896 		}
4897 	} else {
4898 		dev_warn(dev, "RDMA is not supported on this device\n");
4899 	}
4900 
4901 	ice_devlink_register(pf);
4902 	return 0;
4903 
4904 err_init_aux_unroll:
4905 	pf->adev = NULL;
4906 	ida_free(&ice_aux_ida, pf->aux_idx);
4907 err_devlink_reg_param:
4908 	ice_devlink_unregister_params(pf);
4909 err_netdev_reg:
4910 err_send_version_unroll:
4911 	ice_vsi_release_all(pf);
4912 err_alloc_sw_unroll:
4913 	set_bit(ICE_SERVICE_DIS, pf->state);
4914 	set_bit(ICE_DOWN, pf->state);
4915 	devm_kfree(dev, pf->first_sw);
4916 err_msix_misc_unroll:
4917 	ice_free_irq_msix_misc(pf);
4918 err_init_interrupt_unroll:
4919 	ice_clear_interrupt_scheme(pf);
4920 err_init_vsi_unroll:
4921 	devm_kfree(dev, pf->vsi);
4922 err_init_pf_unroll:
4923 	ice_deinit_pf(pf);
4924 	ice_devlink_destroy_regions(pf);
4925 	ice_deinit_hw(hw);
4926 err_exit_unroll:
4927 	pci_disable_pcie_error_reporting(pdev);
4928 	pci_disable_device(pdev);
4929 	return err;
4930 }
4931 
4932 /**
4933  * ice_set_wake - enable or disable Wake on LAN
4934  * @pf: pointer to the PF struct
4935  *
4936  * Simple helper for WoL control
4937  */
ice_set_wake(struct ice_pf * pf)4938 static void ice_set_wake(struct ice_pf *pf)
4939 {
4940 	struct ice_hw *hw = &pf->hw;
4941 	bool wol = pf->wol_ena;
4942 
4943 	/* clear wake state, otherwise new wake events won't fire */
4944 	wr32(hw, PFPM_WUS, U32_MAX);
4945 
4946 	/* enable / disable APM wake up, no RMW needed */
4947 	wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
4948 
4949 	/* set magic packet filter enabled */
4950 	wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
4951 }
4952 
4953 /**
4954  * ice_setup_mc_magic_wake - setup device to wake on multicast magic packet
4955  * @pf: pointer to the PF struct
4956  *
4957  * Issue firmware command to enable multicast magic wake, making
4958  * sure that any locally administered address (LAA) is used for
4959  * wake, and that PF reset doesn't undo the LAA.
4960  */
ice_setup_mc_magic_wake(struct ice_pf * pf)4961 static void ice_setup_mc_magic_wake(struct ice_pf *pf)
4962 {
4963 	struct device *dev = ice_pf_to_dev(pf);
4964 	struct ice_hw *hw = &pf->hw;
4965 	u8 mac_addr[ETH_ALEN];
4966 	struct ice_vsi *vsi;
4967 	int status;
4968 	u8 flags;
4969 
4970 	if (!pf->wol_ena)
4971 		return;
4972 
4973 	vsi = ice_get_main_vsi(pf);
4974 	if (!vsi)
4975 		return;
4976 
4977 	/* Get current MAC address in case it's an LAA */
4978 	if (vsi->netdev)
4979 		ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
4980 	else
4981 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
4982 
4983 	flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
4984 		ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
4985 		ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
4986 
4987 	status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
4988 	if (status)
4989 		dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %d aq_err %s\n",
4990 			status, ice_aq_str(hw->adminq.sq_last_status));
4991 }
4992 
4993 /**
4994  * ice_remove - Device removal routine
4995  * @pdev: PCI device information struct
4996  */
ice_remove(struct pci_dev * pdev)4997 static void ice_remove(struct pci_dev *pdev)
4998 {
4999 	struct ice_pf *pf = pci_get_drvdata(pdev);
5000 	int i;
5001 
5002 	ice_devlink_unregister(pf);
5003 	for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
5004 		if (!ice_is_reset_in_progress(pf->state))
5005 			break;
5006 		msleep(100);
5007 	}
5008 
5009 	ice_tc_indir_block_remove(pf);
5010 
5011 	if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
5012 		set_bit(ICE_VF_RESETS_DISABLED, pf->state);
5013 		ice_free_vfs(pf);
5014 	}
5015 
5016 	ice_service_task_stop(pf);
5017 
5018 	ice_aq_cancel_waiting_tasks(pf);
5019 	ice_unplug_aux_dev(pf);
5020 	if (pf->aux_idx >= 0)
5021 		ida_free(&ice_aux_ida, pf->aux_idx);
5022 	ice_devlink_unregister_params(pf);
5023 	set_bit(ICE_DOWN, pf->state);
5024 
5025 	ice_deinit_lag(pf);
5026 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
5027 		ice_ptp_release(pf);
5028 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
5029 		ice_gnss_exit(pf);
5030 	if (!ice_is_safe_mode(pf))
5031 		ice_remove_arfs(pf);
5032 	ice_setup_mc_magic_wake(pf);
5033 	ice_vsi_release_all(pf);
5034 	mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
5035 	ice_set_wake(pf);
5036 	ice_free_irq_msix_misc(pf);
5037 	ice_for_each_vsi(pf, i) {
5038 		if (!pf->vsi[i])
5039 			continue;
5040 		ice_vsi_free_q_vectors(pf->vsi[i]);
5041 	}
5042 	ice_deinit_pf(pf);
5043 	ice_devlink_destroy_regions(pf);
5044 	ice_deinit_hw(&pf->hw);
5045 
5046 	/* Issue a PFR as part of the prescribed driver unload flow.  Do not
5047 	 * do it via ice_schedule_reset() since there is no need to rebuild
5048 	 * and the service task is already stopped.
5049 	 */
5050 	ice_reset(&pf->hw, ICE_RESET_PFR);
5051 	pci_wait_for_pending_transaction(pdev);
5052 	ice_clear_interrupt_scheme(pf);
5053 	pci_disable_pcie_error_reporting(pdev);
5054 	pci_disable_device(pdev);
5055 }
5056 
5057 /**
5058  * ice_shutdown - PCI callback for shutting down device
5059  * @pdev: PCI device information struct
5060  */
ice_shutdown(struct pci_dev * pdev)5061 static void ice_shutdown(struct pci_dev *pdev)
5062 {
5063 	struct ice_pf *pf = pci_get_drvdata(pdev);
5064 
5065 	ice_remove(pdev);
5066 
5067 	if (system_state == SYSTEM_POWER_OFF) {
5068 		pci_wake_from_d3(pdev, pf->wol_ena);
5069 		pci_set_power_state(pdev, PCI_D3hot);
5070 	}
5071 }
5072 
5073 #ifdef CONFIG_PM
5074 /**
5075  * ice_prepare_for_shutdown - prep for PCI shutdown
5076  * @pf: board private structure
5077  *
5078  * Inform or close all dependent features in prep for PCI device shutdown
5079  */
ice_prepare_for_shutdown(struct ice_pf * pf)5080 static void ice_prepare_for_shutdown(struct ice_pf *pf)
5081 {
5082 	struct ice_hw *hw = &pf->hw;
5083 	u32 v;
5084 
5085 	/* Notify VFs of impending reset */
5086 	if (ice_check_sq_alive(hw, &hw->mailboxq))
5087 		ice_vc_notify_reset(pf);
5088 
5089 	dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
5090 
5091 	/* disable the VSIs and their queues that are not already DOWN */
5092 	ice_pf_dis_all_vsi(pf, false);
5093 
5094 	ice_for_each_vsi(pf, v)
5095 		if (pf->vsi[v])
5096 			pf->vsi[v]->vsi_num = 0;
5097 
5098 	ice_shutdown_all_ctrlq(hw);
5099 }
5100 
5101 /**
5102  * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
5103  * @pf: board private structure to reinitialize
5104  *
5105  * This routine reinitialize interrupt scheme that was cleared during
5106  * power management suspend callback.
5107  *
5108  * This should be called during resume routine to re-allocate the q_vectors
5109  * and reacquire interrupts.
5110  */
ice_reinit_interrupt_scheme(struct ice_pf * pf)5111 static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
5112 {
5113 	struct device *dev = ice_pf_to_dev(pf);
5114 	int ret, v;
5115 
5116 	/* Since we clear MSIX flag during suspend, we need to
5117 	 * set it back during resume...
5118 	 */
5119 
5120 	ret = ice_init_interrupt_scheme(pf);
5121 	if (ret) {
5122 		dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
5123 		return ret;
5124 	}
5125 
5126 	/* Remap vectors and rings, after successful re-init interrupts */
5127 	ice_for_each_vsi(pf, v) {
5128 		if (!pf->vsi[v])
5129 			continue;
5130 
5131 		ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
5132 		if (ret)
5133 			goto err_reinit;
5134 		ice_vsi_map_rings_to_vectors(pf->vsi[v]);
5135 	}
5136 
5137 	ret = ice_req_irq_msix_misc(pf);
5138 	if (ret) {
5139 		dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
5140 			ret);
5141 		goto err_reinit;
5142 	}
5143 
5144 	return 0;
5145 
5146 err_reinit:
5147 	while (v--)
5148 		if (pf->vsi[v])
5149 			ice_vsi_free_q_vectors(pf->vsi[v]);
5150 
5151 	return ret;
5152 }
5153 
5154 /**
5155  * ice_suspend
5156  * @dev: generic device information structure
5157  *
5158  * Power Management callback to quiesce the device and prepare
5159  * for D3 transition.
5160  */
ice_suspend(struct device * dev)5161 static int __maybe_unused ice_suspend(struct device *dev)
5162 {
5163 	struct pci_dev *pdev = to_pci_dev(dev);
5164 	struct ice_pf *pf;
5165 	int disabled, v;
5166 
5167 	pf = pci_get_drvdata(pdev);
5168 
5169 	if (!ice_pf_state_is_nominal(pf)) {
5170 		dev_err(dev, "Device is not ready, no need to suspend it\n");
5171 		return -EBUSY;
5172 	}
5173 
5174 	/* Stop watchdog tasks until resume completion.
5175 	 * Even though it is most likely that the service task is
5176 	 * disabled if the device is suspended or down, the service task's
5177 	 * state is controlled by a different state bit, and we should
5178 	 * store and honor whatever state that bit is in at this point.
5179 	 */
5180 	disabled = ice_service_task_stop(pf);
5181 
5182 	ice_unplug_aux_dev(pf);
5183 
5184 	/* Already suspended?, then there is nothing to do */
5185 	if (test_and_set_bit(ICE_SUSPENDED, pf->state)) {
5186 		if (!disabled)
5187 			ice_service_task_restart(pf);
5188 		return 0;
5189 	}
5190 
5191 	if (test_bit(ICE_DOWN, pf->state) ||
5192 	    ice_is_reset_in_progress(pf->state)) {
5193 		dev_err(dev, "can't suspend device in reset or already down\n");
5194 		if (!disabled)
5195 			ice_service_task_restart(pf);
5196 		return 0;
5197 	}
5198 
5199 	ice_setup_mc_magic_wake(pf);
5200 
5201 	ice_prepare_for_shutdown(pf);
5202 
5203 	ice_set_wake(pf);
5204 
5205 	/* Free vectors, clear the interrupt scheme and release IRQs
5206 	 * for proper hibernation, especially with large number of CPUs.
5207 	 * Otherwise hibernation might fail when mapping all the vectors back
5208 	 * to CPU0.
5209 	 */
5210 	ice_free_irq_msix_misc(pf);
5211 	ice_for_each_vsi(pf, v) {
5212 		if (!pf->vsi[v])
5213 			continue;
5214 		ice_vsi_free_q_vectors(pf->vsi[v]);
5215 	}
5216 	ice_clear_interrupt_scheme(pf);
5217 
5218 	pci_save_state(pdev);
5219 	pci_wake_from_d3(pdev, pf->wol_ena);
5220 	pci_set_power_state(pdev, PCI_D3hot);
5221 	return 0;
5222 }
5223 
5224 /**
5225  * ice_resume - PM callback for waking up from D3
5226  * @dev: generic device information structure
5227  */
ice_resume(struct device * dev)5228 static int __maybe_unused ice_resume(struct device *dev)
5229 {
5230 	struct pci_dev *pdev = to_pci_dev(dev);
5231 	enum ice_reset_req reset_type;
5232 	struct ice_pf *pf;
5233 	struct ice_hw *hw;
5234 	int ret;
5235 
5236 	pci_set_power_state(pdev, PCI_D0);
5237 	pci_restore_state(pdev);
5238 	pci_save_state(pdev);
5239 
5240 	if (!pci_device_is_present(pdev))
5241 		return -ENODEV;
5242 
5243 	ret = pci_enable_device_mem(pdev);
5244 	if (ret) {
5245 		dev_err(dev, "Cannot enable device after suspend\n");
5246 		return ret;
5247 	}
5248 
5249 	pf = pci_get_drvdata(pdev);
5250 	hw = &pf->hw;
5251 
5252 	pf->wakeup_reason = rd32(hw, PFPM_WUS);
5253 	ice_print_wake_reason(pf);
5254 
5255 	/* We cleared the interrupt scheme when we suspended, so we need to
5256 	 * restore it now to resume device functionality.
5257 	 */
5258 	ret = ice_reinit_interrupt_scheme(pf);
5259 	if (ret)
5260 		dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
5261 
5262 	clear_bit(ICE_DOWN, pf->state);
5263 	/* Now perform PF reset and rebuild */
5264 	reset_type = ICE_RESET_PFR;
5265 	/* re-enable service task for reset, but allow reset to schedule it */
5266 	clear_bit(ICE_SERVICE_DIS, pf->state);
5267 
5268 	if (ice_schedule_reset(pf, reset_type))
5269 		dev_err(dev, "Reset during resume failed.\n");
5270 
5271 	clear_bit(ICE_SUSPENDED, pf->state);
5272 	ice_service_task_restart(pf);
5273 
5274 	/* Restart the service task */
5275 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5276 
5277 	return 0;
5278 }
5279 #endif /* CONFIG_PM */
5280 
5281 /**
5282  * ice_pci_err_detected - warning that PCI error has been detected
5283  * @pdev: PCI device information struct
5284  * @err: the type of PCI error
5285  *
5286  * Called to warn that something happened on the PCI bus and the error handling
5287  * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
5288  */
5289 static pci_ers_result_t
ice_pci_err_detected(struct pci_dev * pdev,pci_channel_state_t err)5290 ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
5291 {
5292 	struct ice_pf *pf = pci_get_drvdata(pdev);
5293 
5294 	if (!pf) {
5295 		dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
5296 			__func__, err);
5297 		return PCI_ERS_RESULT_DISCONNECT;
5298 	}
5299 
5300 	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5301 		ice_service_task_stop(pf);
5302 
5303 		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5304 			set_bit(ICE_PFR_REQ, pf->state);
5305 			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5306 		}
5307 	}
5308 
5309 	return PCI_ERS_RESULT_NEED_RESET;
5310 }
5311 
5312 /**
5313  * ice_pci_err_slot_reset - a PCI slot reset has just happened
5314  * @pdev: PCI device information struct
5315  *
5316  * Called to determine if the driver can recover from the PCI slot reset by
5317  * using a register read to determine if the device is recoverable.
5318  */
ice_pci_err_slot_reset(struct pci_dev * pdev)5319 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
5320 {
5321 	struct ice_pf *pf = pci_get_drvdata(pdev);
5322 	pci_ers_result_t result;
5323 	int err;
5324 	u32 reg;
5325 
5326 	err = pci_enable_device_mem(pdev);
5327 	if (err) {
5328 		dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
5329 			err);
5330 		result = PCI_ERS_RESULT_DISCONNECT;
5331 	} else {
5332 		pci_set_master(pdev);
5333 		pci_restore_state(pdev);
5334 		pci_save_state(pdev);
5335 		pci_wake_from_d3(pdev, false);
5336 
5337 		/* Check for life */
5338 		reg = rd32(&pf->hw, GLGEN_RTRIG);
5339 		if (!reg)
5340 			result = PCI_ERS_RESULT_RECOVERED;
5341 		else
5342 			result = PCI_ERS_RESULT_DISCONNECT;
5343 	}
5344 
5345 	err = pci_aer_clear_nonfatal_status(pdev);
5346 	if (err)
5347 		dev_dbg(&pdev->dev, "pci_aer_clear_nonfatal_status() failed, error %d\n",
5348 			err);
5349 		/* non-fatal, continue */
5350 
5351 	return result;
5352 }
5353 
5354 /**
5355  * ice_pci_err_resume - restart operations after PCI error recovery
5356  * @pdev: PCI device information struct
5357  *
5358  * Called to allow the driver to bring things back up after PCI error and/or
5359  * reset recovery have finished
5360  */
ice_pci_err_resume(struct pci_dev * pdev)5361 static void ice_pci_err_resume(struct pci_dev *pdev)
5362 {
5363 	struct ice_pf *pf = pci_get_drvdata(pdev);
5364 
5365 	if (!pf) {
5366 		dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
5367 			__func__);
5368 		return;
5369 	}
5370 
5371 	if (test_bit(ICE_SUSPENDED, pf->state)) {
5372 		dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
5373 			__func__);
5374 		return;
5375 	}
5376 
5377 	ice_restore_all_vfs_msi_state(pdev);
5378 
5379 	ice_do_reset(pf, ICE_RESET_PFR);
5380 	ice_service_task_restart(pf);
5381 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5382 }
5383 
5384 /**
5385  * ice_pci_err_reset_prepare - prepare device driver for PCI reset
5386  * @pdev: PCI device information struct
5387  */
ice_pci_err_reset_prepare(struct pci_dev * pdev)5388 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
5389 {
5390 	struct ice_pf *pf = pci_get_drvdata(pdev);
5391 
5392 	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5393 		ice_service_task_stop(pf);
5394 
5395 		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5396 			set_bit(ICE_PFR_REQ, pf->state);
5397 			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5398 		}
5399 	}
5400 }
5401 
5402 /**
5403  * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
5404  * @pdev: PCI device information struct
5405  */
ice_pci_err_reset_done(struct pci_dev * pdev)5406 static void ice_pci_err_reset_done(struct pci_dev *pdev)
5407 {
5408 	ice_pci_err_resume(pdev);
5409 }
5410 
5411 /* ice_pci_tbl - PCI Device ID Table
5412  *
5413  * Wildcard entries (PCI_ANY_ID) should come last
5414  * Last entry must be all 0s
5415  *
5416  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
5417  *   Class, Class Mask, private data (not used) }
5418  */
5419 static const struct pci_device_id ice_pci_tbl[] = {
5420 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
5421 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
5422 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
5423 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE), 0 },
5424 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP), 0 },
5425 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP), 0 },
5426 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE), 0 },
5427 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP), 0 },
5428 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP), 0 },
5429 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T), 0 },
5430 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII), 0 },
5431 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE), 0 },
5432 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP), 0 },
5433 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP), 0 },
5434 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T), 0 },
5435 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII), 0 },
5436 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE), 0 },
5437 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP), 0 },
5438 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T), 0 },
5439 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII), 0 },
5440 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE), 0 },
5441 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP), 0 },
5442 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T), 0 },
5443 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE), 0 },
5444 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP), 0 },
5445 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822_SI_DFLT), 0 },
5446 	/* required last entry */
5447 	{ 0, }
5448 };
5449 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
5450 
5451 static __maybe_unused SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
5452 
5453 static const struct pci_error_handlers ice_pci_err_handler = {
5454 	.error_detected = ice_pci_err_detected,
5455 	.slot_reset = ice_pci_err_slot_reset,
5456 	.reset_prepare = ice_pci_err_reset_prepare,
5457 	.reset_done = ice_pci_err_reset_done,
5458 	.resume = ice_pci_err_resume
5459 };
5460 
5461 static struct pci_driver ice_driver = {
5462 	.name = KBUILD_MODNAME,
5463 	.id_table = ice_pci_tbl,
5464 	.probe = ice_probe,
5465 	.remove = ice_remove,
5466 #ifdef CONFIG_PM
5467 	.driver.pm = &ice_pm_ops,
5468 #endif /* CONFIG_PM */
5469 	.shutdown = ice_shutdown,
5470 	.sriov_configure = ice_sriov_configure,
5471 	.err_handler = &ice_pci_err_handler
5472 };
5473 
5474 /**
5475  * ice_module_init - Driver registration routine
5476  *
5477  * ice_module_init is the first routine called when the driver is
5478  * loaded. All it does is register with the PCI subsystem.
5479  */
ice_module_init(void)5480 static int __init ice_module_init(void)
5481 {
5482 	int status;
5483 
5484 	pr_info("%s\n", ice_driver_string);
5485 	pr_info("%s\n", ice_copyright);
5486 
5487 	ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
5488 	if (!ice_wq) {
5489 		pr_err("Failed to create workqueue\n");
5490 		return -ENOMEM;
5491 	}
5492 
5493 	status = pci_register_driver(&ice_driver);
5494 	if (status) {
5495 		pr_err("failed to register PCI driver, err %d\n", status);
5496 		destroy_workqueue(ice_wq);
5497 	}
5498 
5499 	return status;
5500 }
5501 module_init(ice_module_init);
5502 
5503 /**
5504  * ice_module_exit - Driver exit cleanup routine
5505  *
5506  * ice_module_exit is called just before the driver is removed
5507  * from memory.
5508  */
ice_module_exit(void)5509 static void __exit ice_module_exit(void)
5510 {
5511 	pci_unregister_driver(&ice_driver);
5512 	destroy_workqueue(ice_wq);
5513 	pr_info("module unloaded\n");
5514 }
5515 module_exit(ice_module_exit);
5516 
5517 /**
5518  * ice_set_mac_address - NDO callback to set MAC address
5519  * @netdev: network interface device structure
5520  * @pi: pointer to an address structure
5521  *
5522  * Returns 0 on success, negative on failure
5523  */
ice_set_mac_address(struct net_device * netdev,void * pi)5524 static int ice_set_mac_address(struct net_device *netdev, void *pi)
5525 {
5526 	struct ice_netdev_priv *np = netdev_priv(netdev);
5527 	struct ice_vsi *vsi = np->vsi;
5528 	struct ice_pf *pf = vsi->back;
5529 	struct ice_hw *hw = &pf->hw;
5530 	struct sockaddr *addr = pi;
5531 	u8 old_mac[ETH_ALEN];
5532 	u8 flags = 0;
5533 	u8 *mac;
5534 	int err;
5535 
5536 	mac = (u8 *)addr->sa_data;
5537 
5538 	if (!is_valid_ether_addr(mac))
5539 		return -EADDRNOTAVAIL;
5540 
5541 	if (ether_addr_equal(netdev->dev_addr, mac)) {
5542 		netdev_dbg(netdev, "already using mac %pM\n", mac);
5543 		return 0;
5544 	}
5545 
5546 	if (test_bit(ICE_DOWN, pf->state) ||
5547 	    ice_is_reset_in_progress(pf->state)) {
5548 		netdev_err(netdev, "can't set mac %pM. device not ready\n",
5549 			   mac);
5550 		return -EBUSY;
5551 	}
5552 
5553 	if (ice_chnl_dmac_fltr_cnt(pf)) {
5554 		netdev_err(netdev, "can't set mac %pM. Device has tc-flower filters, delete all of them and try again\n",
5555 			   mac);
5556 		return -EAGAIN;
5557 	}
5558 
5559 	netif_addr_lock_bh(netdev);
5560 	ether_addr_copy(old_mac, netdev->dev_addr);
5561 	/* change the netdev's MAC address */
5562 	eth_hw_addr_set(netdev, mac);
5563 	netif_addr_unlock_bh(netdev);
5564 
5565 	/* Clean up old MAC filter. Not an error if old filter doesn't exist */
5566 	err = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
5567 	if (err && err != -ENOENT) {
5568 		err = -EADDRNOTAVAIL;
5569 		goto err_update_filters;
5570 	}
5571 
5572 	/* Add filter for new MAC. If filter exists, return success */
5573 	err = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
5574 	if (err == -EEXIST) {
5575 		/* Although this MAC filter is already present in hardware it's
5576 		 * possible in some cases (e.g. bonding) that dev_addr was
5577 		 * modified outside of the driver and needs to be restored back
5578 		 * to this value.
5579 		 */
5580 		netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
5581 
5582 		return 0;
5583 	} else if (err) {
5584 		/* error if the new filter addition failed */
5585 		err = -EADDRNOTAVAIL;
5586 	}
5587 
5588 err_update_filters:
5589 	if (err) {
5590 		netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
5591 			   mac);
5592 		netif_addr_lock_bh(netdev);
5593 		eth_hw_addr_set(netdev, old_mac);
5594 		netif_addr_unlock_bh(netdev);
5595 		return err;
5596 	}
5597 
5598 	netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
5599 		   netdev->dev_addr);
5600 
5601 	/* write new MAC address to the firmware */
5602 	flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
5603 	err = ice_aq_manage_mac_write(hw, mac, flags, NULL);
5604 	if (err) {
5605 		netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n",
5606 			   mac, err);
5607 	}
5608 	return 0;
5609 }
5610 
5611 /**
5612  * ice_set_rx_mode - NDO callback to set the netdev filters
5613  * @netdev: network interface device structure
5614  */
ice_set_rx_mode(struct net_device * netdev)5615 static void ice_set_rx_mode(struct net_device *netdev)
5616 {
5617 	struct ice_netdev_priv *np = netdev_priv(netdev);
5618 	struct ice_vsi *vsi = np->vsi;
5619 
5620 	if (!vsi)
5621 		return;
5622 
5623 	/* Set the flags to synchronize filters
5624 	 * ndo_set_rx_mode may be triggered even without a change in netdev
5625 	 * flags
5626 	 */
5627 	set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
5628 	set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
5629 	set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
5630 
5631 	/* schedule our worker thread which will take care of
5632 	 * applying the new filter changes
5633 	 */
5634 	ice_service_task_schedule(vsi->back);
5635 }
5636 
5637 /**
5638  * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
5639  * @netdev: network interface device structure
5640  * @queue_index: Queue ID
5641  * @maxrate: maximum bandwidth in Mbps
5642  */
5643 static int
ice_set_tx_maxrate(struct net_device * netdev,int queue_index,u32 maxrate)5644 ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
5645 {
5646 	struct ice_netdev_priv *np = netdev_priv(netdev);
5647 	struct ice_vsi *vsi = np->vsi;
5648 	u16 q_handle;
5649 	int status;
5650 	u8 tc;
5651 
5652 	/* Validate maxrate requested is within permitted range */
5653 	if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
5654 		netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
5655 			   maxrate, queue_index);
5656 		return -EINVAL;
5657 	}
5658 
5659 	q_handle = vsi->tx_rings[queue_index]->q_handle;
5660 	tc = ice_dcb_get_tc(vsi, queue_index);
5661 
5662 	/* Set BW back to default, when user set maxrate to 0 */
5663 	if (!maxrate)
5664 		status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
5665 					       q_handle, ICE_MAX_BW);
5666 	else
5667 		status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
5668 					  q_handle, ICE_MAX_BW, maxrate * 1000);
5669 	if (status)
5670 		netdev_err(netdev, "Unable to set Tx max rate, error %d\n",
5671 			   status);
5672 
5673 	return status;
5674 }
5675 
5676 /**
5677  * ice_fdb_add - add an entry to the hardware database
5678  * @ndm: the input from the stack
5679  * @tb: pointer to array of nladdr (unused)
5680  * @dev: the net device pointer
5681  * @addr: the MAC address entry being added
5682  * @vid: VLAN ID
5683  * @flags: instructions from stack about fdb operation
5684  * @extack: netlink extended ack
5685  */
5686 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)5687 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
5688 	    struct net_device *dev, const unsigned char *addr, u16 vid,
5689 	    u16 flags, struct netlink_ext_ack __always_unused *extack)
5690 {
5691 	int err;
5692 
5693 	if (vid) {
5694 		netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
5695 		return -EINVAL;
5696 	}
5697 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
5698 		netdev_err(dev, "FDB only supports static addresses\n");
5699 		return -EINVAL;
5700 	}
5701 
5702 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
5703 		err = dev_uc_add_excl(dev, addr);
5704 	else if (is_multicast_ether_addr(addr))
5705 		err = dev_mc_add_excl(dev, addr);
5706 	else
5707 		err = -EINVAL;
5708 
5709 	/* Only return duplicate errors if NLM_F_EXCL is set */
5710 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
5711 		err = 0;
5712 
5713 	return err;
5714 }
5715 
5716 /**
5717  * ice_fdb_del - delete an entry from the hardware database
5718  * @ndm: the input from the stack
5719  * @tb: pointer to array of nladdr (unused)
5720  * @dev: the net device pointer
5721  * @addr: the MAC address entry being added
5722  * @vid: VLAN ID
5723  * @extack: netlink extended ack
5724  */
5725 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)5726 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
5727 	    struct net_device *dev, const unsigned char *addr,
5728 	    __always_unused u16 vid, struct netlink_ext_ack *extack)
5729 {
5730 	int err;
5731 
5732 	if (ndm->ndm_state & NUD_PERMANENT) {
5733 		netdev_err(dev, "FDB only supports static addresses\n");
5734 		return -EINVAL;
5735 	}
5736 
5737 	if (is_unicast_ether_addr(addr))
5738 		err = dev_uc_del(dev, addr);
5739 	else if (is_multicast_ether_addr(addr))
5740 		err = dev_mc_del(dev, addr);
5741 	else
5742 		err = -EINVAL;
5743 
5744 	return err;
5745 }
5746 
5747 #define NETIF_VLAN_OFFLOAD_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
5748 					 NETIF_F_HW_VLAN_CTAG_TX | \
5749 					 NETIF_F_HW_VLAN_STAG_RX | \
5750 					 NETIF_F_HW_VLAN_STAG_TX)
5751 
5752 #define NETIF_VLAN_FILTERING_FEATURES	(NETIF_F_HW_VLAN_CTAG_FILTER | \
5753 					 NETIF_F_HW_VLAN_STAG_FILTER)
5754 
5755 /**
5756  * ice_fix_features - fix the netdev features flags based on device limitations
5757  * @netdev: ptr to the netdev that flags are being fixed on
5758  * @features: features that need to be checked and possibly fixed
5759  *
5760  * Make sure any fixups are made to features in this callback. This enables the
5761  * driver to not have to check unsupported configurations throughout the driver
5762  * because that's the responsiblity of this callback.
5763  *
5764  * Single VLAN Mode (SVM) Supported Features:
5765  *	NETIF_F_HW_VLAN_CTAG_FILTER
5766  *	NETIF_F_HW_VLAN_CTAG_RX
5767  *	NETIF_F_HW_VLAN_CTAG_TX
5768  *
5769  * Double VLAN Mode (DVM) Supported Features:
5770  *	NETIF_F_HW_VLAN_CTAG_FILTER
5771  *	NETIF_F_HW_VLAN_CTAG_RX
5772  *	NETIF_F_HW_VLAN_CTAG_TX
5773  *
5774  *	NETIF_F_HW_VLAN_STAG_FILTER
5775  *	NETIF_HW_VLAN_STAG_RX
5776  *	NETIF_HW_VLAN_STAG_TX
5777  *
5778  * Features that need fixing:
5779  *	Cannot simultaneously enable CTAG and STAG stripping and/or insertion.
5780  *	These are mutually exlusive as the VSI context cannot support multiple
5781  *	VLAN ethertypes simultaneously for stripping and/or insertion. If this
5782  *	is not done, then default to clearing the requested STAG offload
5783  *	settings.
5784  *
5785  *	All supported filtering has to be enabled or disabled together. For
5786  *	example, in DVM, CTAG and STAG filtering have to be enabled and disabled
5787  *	together. If this is not done, then default to VLAN filtering disabled.
5788  *	These are mutually exclusive as there is currently no way to
5789  *	enable/disable VLAN filtering based on VLAN ethertype when using VLAN
5790  *	prune rules.
5791  */
5792 static netdev_features_t
ice_fix_features(struct net_device * netdev,netdev_features_t features)5793 ice_fix_features(struct net_device *netdev, netdev_features_t features)
5794 {
5795 	struct ice_netdev_priv *np = netdev_priv(netdev);
5796 	netdev_features_t req_vlan_fltr, cur_vlan_fltr;
5797 	bool cur_ctag, cur_stag, req_ctag, req_stag;
5798 
5799 	cur_vlan_fltr = netdev->features & NETIF_VLAN_FILTERING_FEATURES;
5800 	cur_ctag = cur_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
5801 	cur_stag = cur_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
5802 
5803 	req_vlan_fltr = features & NETIF_VLAN_FILTERING_FEATURES;
5804 	req_ctag = req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
5805 	req_stag = req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
5806 
5807 	if (req_vlan_fltr != cur_vlan_fltr) {
5808 		if (ice_is_dvm_ena(&np->vsi->back->hw)) {
5809 			if (req_ctag && req_stag) {
5810 				features |= NETIF_VLAN_FILTERING_FEATURES;
5811 			} else if (!req_ctag && !req_stag) {
5812 				features &= ~NETIF_VLAN_FILTERING_FEATURES;
5813 			} else if ((!cur_ctag && req_ctag && !cur_stag) ||
5814 				   (!cur_stag && req_stag && !cur_ctag)) {
5815 				features |= NETIF_VLAN_FILTERING_FEATURES;
5816 				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");
5817 			} else if ((cur_ctag && !req_ctag && cur_stag) ||
5818 				   (cur_stag && !req_stag && cur_ctag)) {
5819 				features &= ~NETIF_VLAN_FILTERING_FEATURES;
5820 				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");
5821 			}
5822 		} else {
5823 			if (req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER)
5824 				netdev_warn(netdev, "cannot support requested 802.1ad filtering setting in SVM mode\n");
5825 
5826 			if (req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER)
5827 				features |= NETIF_F_HW_VLAN_CTAG_FILTER;
5828 		}
5829 	}
5830 
5831 	if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
5832 	    (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))) {
5833 		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");
5834 		features &= ~(NETIF_F_HW_VLAN_STAG_RX |
5835 			      NETIF_F_HW_VLAN_STAG_TX);
5836 	}
5837 
5838 	return features;
5839 }
5840 
5841 /**
5842  * ice_set_vlan_offload_features - set VLAN offload features for the PF VSI
5843  * @vsi: PF's VSI
5844  * @features: features used to determine VLAN offload settings
5845  *
5846  * First, determine the vlan_ethertype based on the VLAN offload bits in
5847  * features. Then determine if stripping and insertion should be enabled or
5848  * disabled. Finally enable or disable VLAN stripping and insertion.
5849  */
5850 static int
ice_set_vlan_offload_features(struct ice_vsi * vsi,netdev_features_t features)5851 ice_set_vlan_offload_features(struct ice_vsi *vsi, netdev_features_t features)
5852 {
5853 	bool enable_stripping = true, enable_insertion = true;
5854 	struct ice_vsi_vlan_ops *vlan_ops;
5855 	int strip_err = 0, insert_err = 0;
5856 	u16 vlan_ethertype = 0;
5857 
5858 	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
5859 
5860 	if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
5861 		vlan_ethertype = ETH_P_8021AD;
5862 	else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
5863 		vlan_ethertype = ETH_P_8021Q;
5864 
5865 	if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
5866 		enable_stripping = false;
5867 	if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
5868 		enable_insertion = false;
5869 
5870 	if (enable_stripping)
5871 		strip_err = vlan_ops->ena_stripping(vsi, vlan_ethertype);
5872 	else
5873 		strip_err = vlan_ops->dis_stripping(vsi);
5874 
5875 	if (enable_insertion)
5876 		insert_err = vlan_ops->ena_insertion(vsi, vlan_ethertype);
5877 	else
5878 		insert_err = vlan_ops->dis_insertion(vsi);
5879 
5880 	if (strip_err || insert_err)
5881 		return -EIO;
5882 
5883 	return 0;
5884 }
5885 
5886 /**
5887  * ice_set_vlan_filtering_features - set VLAN filtering features for the PF VSI
5888  * @vsi: PF's VSI
5889  * @features: features used to determine VLAN filtering settings
5890  *
5891  * Enable or disable Rx VLAN filtering based on the VLAN filtering bits in the
5892  * features.
5893  */
5894 static int
ice_set_vlan_filtering_features(struct ice_vsi * vsi,netdev_features_t features)5895 ice_set_vlan_filtering_features(struct ice_vsi *vsi, netdev_features_t features)
5896 {
5897 	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
5898 	int err = 0;
5899 
5900 	/* support Single VLAN Mode (SVM) and Double VLAN Mode (DVM) by checking
5901 	 * if either bit is set
5902 	 */
5903 	if (features &
5904 	    (NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER))
5905 		err = vlan_ops->ena_rx_filtering(vsi);
5906 	else
5907 		err = vlan_ops->dis_rx_filtering(vsi);
5908 
5909 	return err;
5910 }
5911 
5912 /**
5913  * ice_set_vlan_features - set VLAN settings based on suggested feature set
5914  * @netdev: ptr to the netdev being adjusted
5915  * @features: the feature set that the stack is suggesting
5916  *
5917  * Only update VLAN settings if the requested_vlan_features are different than
5918  * the current_vlan_features.
5919  */
5920 static int
ice_set_vlan_features(struct net_device * netdev,netdev_features_t features)5921 ice_set_vlan_features(struct net_device *netdev, netdev_features_t features)
5922 {
5923 	netdev_features_t current_vlan_features, requested_vlan_features;
5924 	struct ice_netdev_priv *np = netdev_priv(netdev);
5925 	struct ice_vsi *vsi = np->vsi;
5926 	int err;
5927 
5928 	current_vlan_features = netdev->features & NETIF_VLAN_OFFLOAD_FEATURES;
5929 	requested_vlan_features = features & NETIF_VLAN_OFFLOAD_FEATURES;
5930 	if (current_vlan_features ^ requested_vlan_features) {
5931 		err = ice_set_vlan_offload_features(vsi, features);
5932 		if (err)
5933 			return err;
5934 	}
5935 
5936 	current_vlan_features = netdev->features &
5937 		NETIF_VLAN_FILTERING_FEATURES;
5938 	requested_vlan_features = features & NETIF_VLAN_FILTERING_FEATURES;
5939 	if (current_vlan_features ^ requested_vlan_features) {
5940 		err = ice_set_vlan_filtering_features(vsi, features);
5941 		if (err)
5942 			return err;
5943 	}
5944 
5945 	return 0;
5946 }
5947 
5948 /**
5949  * ice_set_features - set the netdev feature flags
5950  * @netdev: ptr to the netdev being adjusted
5951  * @features: the feature set that the stack is suggesting
5952  */
5953 static int
ice_set_features(struct net_device * netdev,netdev_features_t features)5954 ice_set_features(struct net_device *netdev, netdev_features_t features)
5955 {
5956 	struct ice_netdev_priv *np = netdev_priv(netdev);
5957 	struct ice_vsi *vsi = np->vsi;
5958 	struct ice_pf *pf = vsi->back;
5959 	int ret = 0;
5960 
5961 	/* Don't set any netdev advanced features with device in Safe Mode */
5962 	if (ice_is_safe_mode(vsi->back)) {
5963 		dev_err(ice_pf_to_dev(vsi->back), "Device is in Safe Mode - not enabling advanced netdev features\n");
5964 		return ret;
5965 	}
5966 
5967 	/* Do not change setting during reset */
5968 	if (ice_is_reset_in_progress(pf->state)) {
5969 		dev_err(ice_pf_to_dev(vsi->back), "Device is resetting, changing advanced netdev features temporarily unavailable.\n");
5970 		return -EBUSY;
5971 	}
5972 
5973 	/* Multiple features can be changed in one call so keep features in
5974 	 * separate if/else statements to guarantee each feature is checked
5975 	 */
5976 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
5977 		ice_vsi_manage_rss_lut(vsi, true);
5978 	else if (!(features & NETIF_F_RXHASH) &&
5979 		 netdev->features & NETIF_F_RXHASH)
5980 		ice_vsi_manage_rss_lut(vsi, false);
5981 
5982 	ret = ice_set_vlan_features(netdev, features);
5983 	if (ret)
5984 		return ret;
5985 
5986 	if ((features & NETIF_F_NTUPLE) &&
5987 	    !(netdev->features & NETIF_F_NTUPLE)) {
5988 		ice_vsi_manage_fdir(vsi, true);
5989 		ice_init_arfs(vsi);
5990 	} else if (!(features & NETIF_F_NTUPLE) &&
5991 		 (netdev->features & NETIF_F_NTUPLE)) {
5992 		ice_vsi_manage_fdir(vsi, false);
5993 		ice_clear_arfs(vsi);
5994 	}
5995 
5996 	/* don't turn off hw_tc_offload when ADQ is already enabled */
5997 	if (!(features & NETIF_F_HW_TC) && ice_is_adq_active(pf)) {
5998 		dev_err(ice_pf_to_dev(pf), "ADQ is active, can't turn hw_tc_offload off\n");
5999 		return -EACCES;
6000 	}
6001 
6002 	if ((features & NETIF_F_HW_TC) &&
6003 	    !(netdev->features & NETIF_F_HW_TC))
6004 		set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
6005 	else
6006 		clear_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
6007 
6008 	return 0;
6009 }
6010 
6011 /**
6012  * ice_vsi_vlan_setup - Setup VLAN offload properties on a PF VSI
6013  * @vsi: VSI to setup VLAN properties for
6014  */
ice_vsi_vlan_setup(struct ice_vsi * vsi)6015 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
6016 {
6017 	int err;
6018 
6019 	err = ice_set_vlan_offload_features(vsi, vsi->netdev->features);
6020 	if (err)
6021 		return err;
6022 
6023 	err = ice_set_vlan_filtering_features(vsi, vsi->netdev->features);
6024 	if (err)
6025 		return err;
6026 
6027 	return ice_vsi_add_vlan_zero(vsi);
6028 }
6029 
6030 /**
6031  * ice_vsi_cfg - Setup the VSI
6032  * @vsi: the VSI being configured
6033  *
6034  * Return 0 on success and negative value on error
6035  */
ice_vsi_cfg(struct ice_vsi * vsi)6036 int ice_vsi_cfg(struct ice_vsi *vsi)
6037 {
6038 	int err;
6039 
6040 	if (vsi->netdev) {
6041 		ice_set_rx_mode(vsi->netdev);
6042 
6043 		if (vsi->type != ICE_VSI_LB) {
6044 			err = ice_vsi_vlan_setup(vsi);
6045 
6046 			if (err)
6047 				return err;
6048 		}
6049 	}
6050 	ice_vsi_cfg_dcb_rings(vsi);
6051 
6052 	err = ice_vsi_cfg_lan_txqs(vsi);
6053 	if (!err && ice_is_xdp_ena_vsi(vsi))
6054 		err = ice_vsi_cfg_xdp_txqs(vsi);
6055 	if (!err)
6056 		err = ice_vsi_cfg_rxqs(vsi);
6057 
6058 	return err;
6059 }
6060 
6061 /* THEORY OF MODERATION:
6062  * The ice driver hardware works differently than the hardware that DIMLIB was
6063  * originally made for. ice hardware doesn't have packet count limits that
6064  * can trigger an interrupt, but it *does* have interrupt rate limit support,
6065  * which is hard-coded to a limit of 250,000 ints/second.
6066  * If not using dynamic moderation, the INTRL value can be modified
6067  * by ethtool rx-usecs-high.
6068  */
6069 struct ice_dim {
6070 	/* the throttle rate for interrupts, basically worst case delay before
6071 	 * an initial interrupt fires, value is stored in microseconds.
6072 	 */
6073 	u16 itr;
6074 };
6075 
6076 /* Make a different profile for Rx that doesn't allow quite so aggressive
6077  * moderation at the high end (it maxes out at 126us or about 8k interrupts a
6078  * second.
6079  */
6080 static const struct ice_dim rx_profile[] = {
6081 	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6082 	{8},    /* 125,000 ints/s */
6083 	{16},   /*  62,500 ints/s */
6084 	{62},   /*  16,129 ints/s */
6085 	{126}   /*   7,936 ints/s */
6086 };
6087 
6088 /* The transmit profile, which has the same sorts of values
6089  * as the previous struct
6090  */
6091 static const struct ice_dim tx_profile[] = {
6092 	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6093 	{8},    /* 125,000 ints/s */
6094 	{40},   /*  16,125 ints/s */
6095 	{128},  /*   7,812 ints/s */
6096 	{256}   /*   3,906 ints/s */
6097 };
6098 
ice_tx_dim_work(struct work_struct * work)6099 static void ice_tx_dim_work(struct work_struct *work)
6100 {
6101 	struct ice_ring_container *rc;
6102 	struct dim *dim;
6103 	u16 itr;
6104 
6105 	dim = container_of(work, struct dim, work);
6106 	rc = (struct ice_ring_container *)dim->priv;
6107 
6108 	WARN_ON(dim->profile_ix >= ARRAY_SIZE(tx_profile));
6109 
6110 	/* look up the values in our local table */
6111 	itr = tx_profile[dim->profile_ix].itr;
6112 
6113 	ice_trace(tx_dim_work, container_of(rc, struct ice_q_vector, tx), dim);
6114 	ice_write_itr(rc, itr);
6115 
6116 	dim->state = DIM_START_MEASURE;
6117 }
6118 
ice_rx_dim_work(struct work_struct * work)6119 static void ice_rx_dim_work(struct work_struct *work)
6120 {
6121 	struct ice_ring_container *rc;
6122 	struct dim *dim;
6123 	u16 itr;
6124 
6125 	dim = container_of(work, struct dim, work);
6126 	rc = (struct ice_ring_container *)dim->priv;
6127 
6128 	WARN_ON(dim->profile_ix >= ARRAY_SIZE(rx_profile));
6129 
6130 	/* look up the values in our local table */
6131 	itr = rx_profile[dim->profile_ix].itr;
6132 
6133 	ice_trace(rx_dim_work, container_of(rc, struct ice_q_vector, rx), dim);
6134 	ice_write_itr(rc, itr);
6135 
6136 	dim->state = DIM_START_MEASURE;
6137 }
6138 
6139 #define ICE_DIM_DEFAULT_PROFILE_IX 1
6140 
6141 /**
6142  * ice_init_moderation - set up interrupt moderation
6143  * @q_vector: the vector containing rings to be configured
6144  *
6145  * Set up interrupt moderation registers, with the intent to do the right thing
6146  * when called from reset or from probe, and whether or not dynamic moderation
6147  * is enabled or not. Take special care to write all the registers in both
6148  * dynamic moderation mode or not in order to make sure hardware is in a known
6149  * state.
6150  */
ice_init_moderation(struct ice_q_vector * q_vector)6151 static void ice_init_moderation(struct ice_q_vector *q_vector)
6152 {
6153 	struct ice_ring_container *rc;
6154 	bool tx_dynamic, rx_dynamic;
6155 
6156 	rc = &q_vector->tx;
6157 	INIT_WORK(&rc->dim.work, ice_tx_dim_work);
6158 	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6159 	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6160 	rc->dim.priv = rc;
6161 	tx_dynamic = ITR_IS_DYNAMIC(rc);
6162 
6163 	/* set the initial TX ITR to match the above */
6164 	ice_write_itr(rc, tx_dynamic ?
6165 		      tx_profile[rc->dim.profile_ix].itr : rc->itr_setting);
6166 
6167 	rc = &q_vector->rx;
6168 	INIT_WORK(&rc->dim.work, ice_rx_dim_work);
6169 	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6170 	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6171 	rc->dim.priv = rc;
6172 	rx_dynamic = ITR_IS_DYNAMIC(rc);
6173 
6174 	/* set the initial RX ITR to match the above */
6175 	ice_write_itr(rc, rx_dynamic ? rx_profile[rc->dim.profile_ix].itr :
6176 				       rc->itr_setting);
6177 
6178 	ice_set_q_vector_intrl(q_vector);
6179 }
6180 
6181 /**
6182  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
6183  * @vsi: the VSI being configured
6184  */
ice_napi_enable_all(struct ice_vsi * vsi)6185 static void ice_napi_enable_all(struct ice_vsi *vsi)
6186 {
6187 	int q_idx;
6188 
6189 	if (!vsi->netdev)
6190 		return;
6191 
6192 	ice_for_each_q_vector(vsi, q_idx) {
6193 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6194 
6195 		ice_init_moderation(q_vector);
6196 
6197 		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6198 			napi_enable(&q_vector->napi);
6199 	}
6200 }
6201 
6202 /**
6203  * ice_up_complete - Finish the last steps of bringing up a connection
6204  * @vsi: The VSI being configured
6205  *
6206  * Return 0 on success and negative value on error
6207  */
ice_up_complete(struct ice_vsi * vsi)6208 static int ice_up_complete(struct ice_vsi *vsi)
6209 {
6210 	struct ice_pf *pf = vsi->back;
6211 	int err;
6212 
6213 	ice_vsi_cfg_msix(vsi);
6214 
6215 	/* Enable only Rx rings, Tx rings were enabled by the FW when the
6216 	 * Tx queue group list was configured and the context bits were
6217 	 * programmed using ice_vsi_cfg_txqs
6218 	 */
6219 	err = ice_vsi_start_all_rx_rings(vsi);
6220 	if (err)
6221 		return err;
6222 
6223 	clear_bit(ICE_VSI_DOWN, vsi->state);
6224 	ice_napi_enable_all(vsi);
6225 	ice_vsi_ena_irq(vsi);
6226 
6227 	if (vsi->port_info &&
6228 	    (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
6229 	    vsi->netdev) {
6230 		ice_print_link_msg(vsi, true);
6231 		netif_tx_start_all_queues(vsi->netdev);
6232 		netif_carrier_on(vsi->netdev);
6233 		if (!ice_is_e810(&pf->hw))
6234 			ice_ptp_link_change(pf, pf->hw.pf_id, true);
6235 	}
6236 
6237 	/* Perform an initial read of the statistics registers now to
6238 	 * set the baseline so counters are ready when interface is up
6239 	 */
6240 	ice_update_eth_stats(vsi);
6241 	ice_service_task_schedule(pf);
6242 
6243 	return 0;
6244 }
6245 
6246 /**
6247  * ice_up - Bring the connection back up after being down
6248  * @vsi: VSI being configured
6249  */
ice_up(struct ice_vsi * vsi)6250 int ice_up(struct ice_vsi *vsi)
6251 {
6252 	int err;
6253 
6254 	err = ice_vsi_cfg(vsi);
6255 	if (!err)
6256 		err = ice_up_complete(vsi);
6257 
6258 	return err;
6259 }
6260 
6261 /**
6262  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
6263  * @syncp: pointer to u64_stats_sync
6264  * @stats: stats that pkts and bytes count will be taken from
6265  * @pkts: packets stats counter
6266  * @bytes: bytes stats counter
6267  *
6268  * This function fetches stats from the ring considering the atomic operations
6269  * that needs to be performed to read u64 values in 32 bit machine.
6270  */
6271 void
ice_fetch_u64_stats_per_ring(struct u64_stats_sync * syncp,struct ice_q_stats stats,u64 * pkts,u64 * bytes)6272 ice_fetch_u64_stats_per_ring(struct u64_stats_sync *syncp,
6273 			     struct ice_q_stats stats, u64 *pkts, u64 *bytes)
6274 {
6275 	unsigned int start;
6276 
6277 	do {
6278 		start = u64_stats_fetch_begin_irq(syncp);
6279 		*pkts = stats.pkts;
6280 		*bytes = stats.bytes;
6281 	} while (u64_stats_fetch_retry_irq(syncp, start));
6282 }
6283 
6284 /**
6285  * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
6286  * @vsi: the VSI to be updated
6287  * @vsi_stats: the stats struct to be updated
6288  * @rings: rings to work on
6289  * @count: number of rings
6290  */
6291 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)6292 ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi,
6293 			     struct rtnl_link_stats64 *vsi_stats,
6294 			     struct ice_tx_ring **rings, u16 count)
6295 {
6296 	u16 i;
6297 
6298 	for (i = 0; i < count; i++) {
6299 		struct ice_tx_ring *ring;
6300 		u64 pkts = 0, bytes = 0;
6301 
6302 		ring = READ_ONCE(rings[i]);
6303 		if (!ring)
6304 			continue;
6305 		ice_fetch_u64_stats_per_ring(&ring->syncp, ring->stats, &pkts, &bytes);
6306 		vsi_stats->tx_packets += pkts;
6307 		vsi_stats->tx_bytes += bytes;
6308 		vsi->tx_restart += ring->tx_stats.restart_q;
6309 		vsi->tx_busy += ring->tx_stats.tx_busy;
6310 		vsi->tx_linearize += ring->tx_stats.tx_linearize;
6311 	}
6312 }
6313 
6314 /**
6315  * ice_update_vsi_ring_stats - Update VSI stats counters
6316  * @vsi: the VSI to be updated
6317  */
ice_update_vsi_ring_stats(struct ice_vsi * vsi)6318 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
6319 {
6320 	struct rtnl_link_stats64 *vsi_stats;
6321 	u64 pkts, bytes;
6322 	int i;
6323 
6324 	vsi_stats = kzalloc(sizeof(*vsi_stats), GFP_ATOMIC);
6325 	if (!vsi_stats)
6326 		return;
6327 
6328 	/* reset non-netdev (extended) stats */
6329 	vsi->tx_restart = 0;
6330 	vsi->tx_busy = 0;
6331 	vsi->tx_linearize = 0;
6332 	vsi->rx_buf_failed = 0;
6333 	vsi->rx_page_failed = 0;
6334 
6335 	rcu_read_lock();
6336 
6337 	/* update Tx rings counters */
6338 	ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->tx_rings,
6339 				     vsi->num_txq);
6340 
6341 	/* update Rx rings counters */
6342 	ice_for_each_rxq(vsi, i) {
6343 		struct ice_rx_ring *ring = READ_ONCE(vsi->rx_rings[i]);
6344 
6345 		ice_fetch_u64_stats_per_ring(&ring->syncp, ring->stats, &pkts, &bytes);
6346 		vsi_stats->rx_packets += pkts;
6347 		vsi_stats->rx_bytes += bytes;
6348 		vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
6349 		vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
6350 	}
6351 
6352 	/* update XDP Tx rings counters */
6353 	if (ice_is_xdp_ena_vsi(vsi))
6354 		ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->xdp_rings,
6355 					     vsi->num_xdp_txq);
6356 
6357 	rcu_read_unlock();
6358 
6359 	vsi->net_stats.tx_packets = vsi_stats->tx_packets;
6360 	vsi->net_stats.tx_bytes = vsi_stats->tx_bytes;
6361 	vsi->net_stats.rx_packets = vsi_stats->rx_packets;
6362 	vsi->net_stats.rx_bytes = vsi_stats->rx_bytes;
6363 
6364 	kfree(vsi_stats);
6365 }
6366 
6367 /**
6368  * ice_update_vsi_stats - Update VSI stats counters
6369  * @vsi: the VSI to be updated
6370  */
ice_update_vsi_stats(struct ice_vsi * vsi)6371 void ice_update_vsi_stats(struct ice_vsi *vsi)
6372 {
6373 	struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
6374 	struct ice_eth_stats *cur_es = &vsi->eth_stats;
6375 	struct ice_pf *pf = vsi->back;
6376 
6377 	if (test_bit(ICE_VSI_DOWN, vsi->state) ||
6378 	    test_bit(ICE_CFG_BUSY, pf->state))
6379 		return;
6380 
6381 	/* get stats as recorded by Tx/Rx rings */
6382 	ice_update_vsi_ring_stats(vsi);
6383 
6384 	/* get VSI stats as recorded by the hardware */
6385 	ice_update_eth_stats(vsi);
6386 
6387 	cur_ns->tx_errors = cur_es->tx_errors;
6388 	cur_ns->rx_dropped = cur_es->rx_discards;
6389 	cur_ns->tx_dropped = cur_es->tx_discards;
6390 	cur_ns->multicast = cur_es->rx_multicast;
6391 
6392 	/* update some more netdev stats if this is main VSI */
6393 	if (vsi->type == ICE_VSI_PF) {
6394 		cur_ns->rx_crc_errors = pf->stats.crc_errors;
6395 		cur_ns->rx_errors = pf->stats.crc_errors +
6396 				    pf->stats.illegal_bytes +
6397 				    pf->stats.rx_len_errors +
6398 				    pf->stats.rx_undersize +
6399 				    pf->hw_csum_rx_error +
6400 				    pf->stats.rx_jabber +
6401 				    pf->stats.rx_fragments +
6402 				    pf->stats.rx_oversize;
6403 		cur_ns->rx_length_errors = pf->stats.rx_len_errors;
6404 		/* record drops from the port level */
6405 		cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
6406 	}
6407 }
6408 
6409 /**
6410  * ice_update_pf_stats - Update PF port stats counters
6411  * @pf: PF whose stats needs to be updated
6412  */
ice_update_pf_stats(struct ice_pf * pf)6413 void ice_update_pf_stats(struct ice_pf *pf)
6414 {
6415 	struct ice_hw_port_stats *prev_ps, *cur_ps;
6416 	struct ice_hw *hw = &pf->hw;
6417 	u16 fd_ctr_base;
6418 	u8 port;
6419 
6420 	port = hw->port_info->lport;
6421 	prev_ps = &pf->stats_prev;
6422 	cur_ps = &pf->stats;
6423 
6424 	ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
6425 			  &prev_ps->eth.rx_bytes,
6426 			  &cur_ps->eth.rx_bytes);
6427 
6428 	ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
6429 			  &prev_ps->eth.rx_unicast,
6430 			  &cur_ps->eth.rx_unicast);
6431 
6432 	ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
6433 			  &prev_ps->eth.rx_multicast,
6434 			  &cur_ps->eth.rx_multicast);
6435 
6436 	ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
6437 			  &prev_ps->eth.rx_broadcast,
6438 			  &cur_ps->eth.rx_broadcast);
6439 
6440 	ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
6441 			  &prev_ps->eth.rx_discards,
6442 			  &cur_ps->eth.rx_discards);
6443 
6444 	ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
6445 			  &prev_ps->eth.tx_bytes,
6446 			  &cur_ps->eth.tx_bytes);
6447 
6448 	ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
6449 			  &prev_ps->eth.tx_unicast,
6450 			  &cur_ps->eth.tx_unicast);
6451 
6452 	ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
6453 			  &prev_ps->eth.tx_multicast,
6454 			  &cur_ps->eth.tx_multicast);
6455 
6456 	ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
6457 			  &prev_ps->eth.tx_broadcast,
6458 			  &cur_ps->eth.tx_broadcast);
6459 
6460 	ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
6461 			  &prev_ps->tx_dropped_link_down,
6462 			  &cur_ps->tx_dropped_link_down);
6463 
6464 	ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
6465 			  &prev_ps->rx_size_64, &cur_ps->rx_size_64);
6466 
6467 	ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
6468 			  &prev_ps->rx_size_127, &cur_ps->rx_size_127);
6469 
6470 	ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
6471 			  &prev_ps->rx_size_255, &cur_ps->rx_size_255);
6472 
6473 	ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
6474 			  &prev_ps->rx_size_511, &cur_ps->rx_size_511);
6475 
6476 	ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
6477 			  &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
6478 
6479 	ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
6480 			  &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
6481 
6482 	ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
6483 			  &prev_ps->rx_size_big, &cur_ps->rx_size_big);
6484 
6485 	ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
6486 			  &prev_ps->tx_size_64, &cur_ps->tx_size_64);
6487 
6488 	ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
6489 			  &prev_ps->tx_size_127, &cur_ps->tx_size_127);
6490 
6491 	ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
6492 			  &prev_ps->tx_size_255, &cur_ps->tx_size_255);
6493 
6494 	ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
6495 			  &prev_ps->tx_size_511, &cur_ps->tx_size_511);
6496 
6497 	ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
6498 			  &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
6499 
6500 	ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
6501 			  &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
6502 
6503 	ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
6504 			  &prev_ps->tx_size_big, &cur_ps->tx_size_big);
6505 
6506 	fd_ctr_base = hw->fd_ctr_base;
6507 
6508 	ice_stat_update40(hw,
6509 			  GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
6510 			  pf->stat_prev_loaded, &prev_ps->fd_sb_match,
6511 			  &cur_ps->fd_sb_match);
6512 	ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
6513 			  &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
6514 
6515 	ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
6516 			  &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
6517 
6518 	ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
6519 			  &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
6520 
6521 	ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
6522 			  &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
6523 
6524 	ice_update_dcb_stats(pf);
6525 
6526 	ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
6527 			  &prev_ps->crc_errors, &cur_ps->crc_errors);
6528 
6529 	ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
6530 			  &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
6531 
6532 	ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
6533 			  &prev_ps->mac_local_faults,
6534 			  &cur_ps->mac_local_faults);
6535 
6536 	ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
6537 			  &prev_ps->mac_remote_faults,
6538 			  &cur_ps->mac_remote_faults);
6539 
6540 	ice_stat_update32(hw, GLPRT_RLEC(port), pf->stat_prev_loaded,
6541 			  &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
6542 
6543 	ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
6544 			  &prev_ps->rx_undersize, &cur_ps->rx_undersize);
6545 
6546 	ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
6547 			  &prev_ps->rx_fragments, &cur_ps->rx_fragments);
6548 
6549 	ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
6550 			  &prev_ps->rx_oversize, &cur_ps->rx_oversize);
6551 
6552 	ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
6553 			  &prev_ps->rx_jabber, &cur_ps->rx_jabber);
6554 
6555 	cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
6556 
6557 	pf->stat_prev_loaded = true;
6558 }
6559 
6560 /**
6561  * ice_get_stats64 - get statistics for network device structure
6562  * @netdev: network interface device structure
6563  * @stats: main device statistics structure
6564  */
6565 static
ice_get_stats64(struct net_device * netdev,struct rtnl_link_stats64 * stats)6566 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
6567 {
6568 	struct ice_netdev_priv *np = netdev_priv(netdev);
6569 	struct rtnl_link_stats64 *vsi_stats;
6570 	struct ice_vsi *vsi = np->vsi;
6571 
6572 	vsi_stats = &vsi->net_stats;
6573 
6574 	if (!vsi->num_txq || !vsi->num_rxq)
6575 		return;
6576 
6577 	/* netdev packet/byte stats come from ring counter. These are obtained
6578 	 * by summing up ring counters (done by ice_update_vsi_ring_stats).
6579 	 * But, only call the update routine and read the registers if VSI is
6580 	 * not down.
6581 	 */
6582 	if (!test_bit(ICE_VSI_DOWN, vsi->state))
6583 		ice_update_vsi_ring_stats(vsi);
6584 	stats->tx_packets = vsi_stats->tx_packets;
6585 	stats->tx_bytes = vsi_stats->tx_bytes;
6586 	stats->rx_packets = vsi_stats->rx_packets;
6587 	stats->rx_bytes = vsi_stats->rx_bytes;
6588 
6589 	/* The rest of the stats can be read from the hardware but instead we
6590 	 * just return values that the watchdog task has already obtained from
6591 	 * the hardware.
6592 	 */
6593 	stats->multicast = vsi_stats->multicast;
6594 	stats->tx_errors = vsi_stats->tx_errors;
6595 	stats->tx_dropped = vsi_stats->tx_dropped;
6596 	stats->rx_errors = vsi_stats->rx_errors;
6597 	stats->rx_dropped = vsi_stats->rx_dropped;
6598 	stats->rx_crc_errors = vsi_stats->rx_crc_errors;
6599 	stats->rx_length_errors = vsi_stats->rx_length_errors;
6600 }
6601 
6602 /**
6603  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
6604  * @vsi: VSI having NAPI disabled
6605  */
ice_napi_disable_all(struct ice_vsi * vsi)6606 static void ice_napi_disable_all(struct ice_vsi *vsi)
6607 {
6608 	int q_idx;
6609 
6610 	if (!vsi->netdev)
6611 		return;
6612 
6613 	ice_for_each_q_vector(vsi, q_idx) {
6614 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6615 
6616 		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6617 			napi_disable(&q_vector->napi);
6618 
6619 		cancel_work_sync(&q_vector->tx.dim.work);
6620 		cancel_work_sync(&q_vector->rx.dim.work);
6621 	}
6622 }
6623 
6624 /**
6625  * ice_down - Shutdown the connection
6626  * @vsi: The VSI being stopped
6627  *
6628  * Caller of this function is expected to set the vsi->state ICE_DOWN bit
6629  */
ice_down(struct ice_vsi * vsi)6630 int ice_down(struct ice_vsi *vsi)
6631 {
6632 	int i, tx_err, rx_err, link_err = 0, vlan_err = 0;
6633 
6634 	WARN_ON(!test_bit(ICE_VSI_DOWN, vsi->state));
6635 
6636 	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
6637 		vlan_err = ice_vsi_del_vlan_zero(vsi);
6638 		if (!ice_is_e810(&vsi->back->hw))
6639 			ice_ptp_link_change(vsi->back, vsi->back->hw.pf_id, false);
6640 		netif_carrier_off(vsi->netdev);
6641 		netif_tx_disable(vsi->netdev);
6642 	} else if (vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
6643 		ice_eswitch_stop_all_tx_queues(vsi->back);
6644 	}
6645 
6646 	ice_vsi_dis_irq(vsi);
6647 
6648 	tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
6649 	if (tx_err)
6650 		netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
6651 			   vsi->vsi_num, tx_err);
6652 	if (!tx_err && ice_is_xdp_ena_vsi(vsi)) {
6653 		tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
6654 		if (tx_err)
6655 			netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
6656 				   vsi->vsi_num, tx_err);
6657 	}
6658 
6659 	rx_err = ice_vsi_stop_all_rx_rings(vsi);
6660 	if (rx_err)
6661 		netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
6662 			   vsi->vsi_num, rx_err);
6663 
6664 	ice_napi_disable_all(vsi);
6665 
6666 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
6667 		link_err = ice_force_phys_link_state(vsi, false);
6668 		if (link_err)
6669 			netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
6670 				   vsi->vsi_num, link_err);
6671 	}
6672 
6673 	ice_for_each_txq(vsi, i)
6674 		ice_clean_tx_ring(vsi->tx_rings[i]);
6675 
6676 	ice_for_each_rxq(vsi, i)
6677 		ice_clean_rx_ring(vsi->rx_rings[i]);
6678 
6679 	if (tx_err || rx_err || link_err || vlan_err) {
6680 		netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
6681 			   vsi->vsi_num, vsi->vsw->sw_id);
6682 		return -EIO;
6683 	}
6684 
6685 	return 0;
6686 }
6687 
6688 /**
6689  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
6690  * @vsi: VSI having resources allocated
6691  *
6692  * Return 0 on success, negative on failure
6693  */
ice_vsi_setup_tx_rings(struct ice_vsi * vsi)6694 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
6695 {
6696 	int i, err = 0;
6697 
6698 	if (!vsi->num_txq) {
6699 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
6700 			vsi->vsi_num);
6701 		return -EINVAL;
6702 	}
6703 
6704 	ice_for_each_txq(vsi, i) {
6705 		struct ice_tx_ring *ring = vsi->tx_rings[i];
6706 
6707 		if (!ring)
6708 			return -EINVAL;
6709 
6710 		if (vsi->netdev)
6711 			ring->netdev = vsi->netdev;
6712 		err = ice_setup_tx_ring(ring);
6713 		if (err)
6714 			break;
6715 	}
6716 
6717 	return err;
6718 }
6719 
6720 /**
6721  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
6722  * @vsi: VSI having resources allocated
6723  *
6724  * Return 0 on success, negative on failure
6725  */
ice_vsi_setup_rx_rings(struct ice_vsi * vsi)6726 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
6727 {
6728 	int i, err = 0;
6729 
6730 	if (!vsi->num_rxq) {
6731 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
6732 			vsi->vsi_num);
6733 		return -EINVAL;
6734 	}
6735 
6736 	ice_for_each_rxq(vsi, i) {
6737 		struct ice_rx_ring *ring = vsi->rx_rings[i];
6738 
6739 		if (!ring)
6740 			return -EINVAL;
6741 
6742 		if (vsi->netdev)
6743 			ring->netdev = vsi->netdev;
6744 		err = ice_setup_rx_ring(ring);
6745 		if (err)
6746 			break;
6747 	}
6748 
6749 	return err;
6750 }
6751 
6752 /**
6753  * ice_vsi_open_ctrl - open control VSI for use
6754  * @vsi: the VSI to open
6755  *
6756  * Initialization of the Control VSI
6757  *
6758  * Returns 0 on success, negative value on error
6759  */
ice_vsi_open_ctrl(struct ice_vsi * vsi)6760 int ice_vsi_open_ctrl(struct ice_vsi *vsi)
6761 {
6762 	char int_name[ICE_INT_NAME_STR_LEN];
6763 	struct ice_pf *pf = vsi->back;
6764 	struct device *dev;
6765 	int err;
6766 
6767 	dev = ice_pf_to_dev(pf);
6768 	/* allocate descriptors */
6769 	err = ice_vsi_setup_tx_rings(vsi);
6770 	if (err)
6771 		goto err_setup_tx;
6772 
6773 	err = ice_vsi_setup_rx_rings(vsi);
6774 	if (err)
6775 		goto err_setup_rx;
6776 
6777 	err = ice_vsi_cfg(vsi);
6778 	if (err)
6779 		goto err_setup_rx;
6780 
6781 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
6782 		 dev_driver_string(dev), dev_name(dev));
6783 	err = ice_vsi_req_irq_msix(vsi, int_name);
6784 	if (err)
6785 		goto err_setup_rx;
6786 
6787 	ice_vsi_cfg_msix(vsi);
6788 
6789 	err = ice_vsi_start_all_rx_rings(vsi);
6790 	if (err)
6791 		goto err_up_complete;
6792 
6793 	clear_bit(ICE_VSI_DOWN, vsi->state);
6794 	ice_vsi_ena_irq(vsi);
6795 
6796 	return 0;
6797 
6798 err_up_complete:
6799 	ice_down(vsi);
6800 err_setup_rx:
6801 	ice_vsi_free_rx_rings(vsi);
6802 err_setup_tx:
6803 	ice_vsi_free_tx_rings(vsi);
6804 
6805 	return err;
6806 }
6807 
6808 /**
6809  * ice_vsi_open - Called when a network interface is made active
6810  * @vsi: the VSI to open
6811  *
6812  * Initialization of the VSI
6813  *
6814  * Returns 0 on success, negative value on error
6815  */
ice_vsi_open(struct ice_vsi * vsi)6816 int ice_vsi_open(struct ice_vsi *vsi)
6817 {
6818 	char int_name[ICE_INT_NAME_STR_LEN];
6819 	struct ice_pf *pf = vsi->back;
6820 	int err;
6821 
6822 	/* allocate descriptors */
6823 	err = ice_vsi_setup_tx_rings(vsi);
6824 	if (err)
6825 		goto err_setup_tx;
6826 
6827 	err = ice_vsi_setup_rx_rings(vsi);
6828 	if (err)
6829 		goto err_setup_rx;
6830 
6831 	err = ice_vsi_cfg(vsi);
6832 	if (err)
6833 		goto err_setup_rx;
6834 
6835 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
6836 		 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
6837 	err = ice_vsi_req_irq_msix(vsi, int_name);
6838 	if (err)
6839 		goto err_setup_rx;
6840 
6841 	if (vsi->type == ICE_VSI_PF) {
6842 		/* Notify the stack of the actual queue counts. */
6843 		err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
6844 		if (err)
6845 			goto err_set_qs;
6846 
6847 		err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
6848 		if (err)
6849 			goto err_set_qs;
6850 	}
6851 
6852 	err = ice_up_complete(vsi);
6853 	if (err)
6854 		goto err_up_complete;
6855 
6856 	return 0;
6857 
6858 err_up_complete:
6859 	ice_down(vsi);
6860 err_set_qs:
6861 	ice_vsi_free_irq(vsi);
6862 err_setup_rx:
6863 	ice_vsi_free_rx_rings(vsi);
6864 err_setup_tx:
6865 	ice_vsi_free_tx_rings(vsi);
6866 
6867 	return err;
6868 }
6869 
6870 /**
6871  * ice_vsi_release_all - Delete all VSIs
6872  * @pf: PF from which all VSIs are being removed
6873  */
ice_vsi_release_all(struct ice_pf * pf)6874 static void ice_vsi_release_all(struct ice_pf *pf)
6875 {
6876 	int err, i;
6877 
6878 	if (!pf->vsi)
6879 		return;
6880 
6881 	ice_for_each_vsi(pf, i) {
6882 		if (!pf->vsi[i])
6883 			continue;
6884 
6885 		if (pf->vsi[i]->type == ICE_VSI_CHNL)
6886 			continue;
6887 
6888 		err = ice_vsi_release(pf->vsi[i]);
6889 		if (err)
6890 			dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
6891 				i, err, pf->vsi[i]->vsi_num);
6892 	}
6893 }
6894 
6895 /**
6896  * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
6897  * @pf: pointer to the PF instance
6898  * @type: VSI type to rebuild
6899  *
6900  * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
6901  */
ice_vsi_rebuild_by_type(struct ice_pf * pf,enum ice_vsi_type type)6902 static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
6903 {
6904 	struct device *dev = ice_pf_to_dev(pf);
6905 	int i, err;
6906 
6907 	ice_for_each_vsi(pf, i) {
6908 		struct ice_vsi *vsi = pf->vsi[i];
6909 
6910 		if (!vsi || vsi->type != type)
6911 			continue;
6912 
6913 		/* rebuild the VSI */
6914 		err = ice_vsi_rebuild(vsi, true);
6915 		if (err) {
6916 			dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
6917 				err, vsi->idx, ice_vsi_type_str(type));
6918 			return err;
6919 		}
6920 
6921 		/* replay filters for the VSI */
6922 		err = ice_replay_vsi(&pf->hw, vsi->idx);
6923 		if (err) {
6924 			dev_err(dev, "replay VSI failed, error %d, VSI index %d, type %s\n",
6925 				err, vsi->idx, ice_vsi_type_str(type));
6926 			return err;
6927 		}
6928 
6929 		/* Re-map HW VSI number, using VSI handle that has been
6930 		 * previously validated in ice_replay_vsi() call above
6931 		 */
6932 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
6933 
6934 		/* enable the VSI */
6935 		err = ice_ena_vsi(vsi, false);
6936 		if (err) {
6937 			dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
6938 				err, vsi->idx, ice_vsi_type_str(type));
6939 			return err;
6940 		}
6941 
6942 		dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
6943 			 ice_vsi_type_str(type));
6944 	}
6945 
6946 	return 0;
6947 }
6948 
6949 /**
6950  * ice_update_pf_netdev_link - Update PF netdev link status
6951  * @pf: pointer to the PF instance
6952  */
ice_update_pf_netdev_link(struct ice_pf * pf)6953 static void ice_update_pf_netdev_link(struct ice_pf *pf)
6954 {
6955 	bool link_up;
6956 	int i;
6957 
6958 	ice_for_each_vsi(pf, i) {
6959 		struct ice_vsi *vsi = pf->vsi[i];
6960 
6961 		if (!vsi || vsi->type != ICE_VSI_PF)
6962 			return;
6963 
6964 		ice_get_link_status(pf->vsi[i]->port_info, &link_up);
6965 		if (link_up) {
6966 			netif_carrier_on(pf->vsi[i]->netdev);
6967 			netif_tx_wake_all_queues(pf->vsi[i]->netdev);
6968 		} else {
6969 			netif_carrier_off(pf->vsi[i]->netdev);
6970 			netif_tx_stop_all_queues(pf->vsi[i]->netdev);
6971 		}
6972 	}
6973 }
6974 
6975 /**
6976  * ice_rebuild - rebuild after reset
6977  * @pf: PF to rebuild
6978  * @reset_type: type of reset
6979  *
6980  * Do not rebuild VF VSI in this flow because that is already handled via
6981  * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
6982  * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
6983  * to reset/rebuild all the VF VSI twice.
6984  */
ice_rebuild(struct ice_pf * pf,enum ice_reset_req reset_type)6985 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
6986 {
6987 	struct device *dev = ice_pf_to_dev(pf);
6988 	struct ice_hw *hw = &pf->hw;
6989 	bool dvm;
6990 	int err;
6991 
6992 	if (test_bit(ICE_DOWN, pf->state))
6993 		goto clear_recovery;
6994 
6995 	dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
6996 
6997 #define ICE_EMP_RESET_SLEEP_MS 5000
6998 	if (reset_type == ICE_RESET_EMPR) {
6999 		/* If an EMP reset has occurred, any previously pending flash
7000 		 * update will have completed. We no longer know whether or
7001 		 * not the NVM update EMP reset is restricted.
7002 		 */
7003 		pf->fw_emp_reset_disabled = false;
7004 
7005 		msleep(ICE_EMP_RESET_SLEEP_MS);
7006 	}
7007 
7008 	err = ice_init_all_ctrlq(hw);
7009 	if (err) {
7010 		dev_err(dev, "control queues init failed %d\n", err);
7011 		goto err_init_ctrlq;
7012 	}
7013 
7014 	/* if DDP was previously loaded successfully */
7015 	if (!ice_is_safe_mode(pf)) {
7016 		/* reload the SW DB of filter tables */
7017 		if (reset_type == ICE_RESET_PFR)
7018 			ice_fill_blk_tbls(hw);
7019 		else
7020 			/* Reload DDP Package after CORER/GLOBR reset */
7021 			ice_load_pkg(NULL, pf);
7022 	}
7023 
7024 	err = ice_clear_pf_cfg(hw);
7025 	if (err) {
7026 		dev_err(dev, "clear PF configuration failed %d\n", err);
7027 		goto err_init_ctrlq;
7028 	}
7029 
7030 	if (pf->first_sw->dflt_vsi_ena)
7031 		dev_info(dev, "Clearing default VSI, re-enable after reset completes\n");
7032 	/* clear the default VSI configuration if it exists */
7033 	pf->first_sw->dflt_vsi = NULL;
7034 	pf->first_sw->dflt_vsi_ena = false;
7035 
7036 	ice_clear_pxe_mode(hw);
7037 
7038 	err = ice_init_nvm(hw);
7039 	if (err) {
7040 		dev_err(dev, "ice_init_nvm failed %d\n", err);
7041 		goto err_init_ctrlq;
7042 	}
7043 
7044 	err = ice_get_caps(hw);
7045 	if (err) {
7046 		dev_err(dev, "ice_get_caps failed %d\n", err);
7047 		goto err_init_ctrlq;
7048 	}
7049 
7050 	err = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
7051 	if (err) {
7052 		dev_err(dev, "set_mac_cfg failed %d\n", err);
7053 		goto err_init_ctrlq;
7054 	}
7055 
7056 	dvm = ice_is_dvm_ena(hw);
7057 
7058 	err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
7059 	if (err)
7060 		goto err_init_ctrlq;
7061 
7062 	err = ice_sched_init_port(hw->port_info);
7063 	if (err)
7064 		goto err_sched_init_port;
7065 
7066 	/* start misc vector */
7067 	err = ice_req_irq_msix_misc(pf);
7068 	if (err) {
7069 		dev_err(dev, "misc vector setup failed: %d\n", err);
7070 		goto err_sched_init_port;
7071 	}
7072 
7073 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7074 		wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
7075 		if (!rd32(hw, PFQF_FD_SIZE)) {
7076 			u16 unused, guar, b_effort;
7077 
7078 			guar = hw->func_caps.fd_fltr_guar;
7079 			b_effort = hw->func_caps.fd_fltr_best_effort;
7080 
7081 			/* force guaranteed filter pool for PF */
7082 			ice_alloc_fd_guar_item(hw, &unused, guar);
7083 			/* force shared filter pool for PF */
7084 			ice_alloc_fd_shrd_item(hw, &unused, b_effort);
7085 		}
7086 	}
7087 
7088 	if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
7089 		ice_dcb_rebuild(pf);
7090 
7091 	/* If the PF previously had enabled PTP, PTP init needs to happen before
7092 	 * the VSI rebuild. If not, this causes the PTP link status events to
7093 	 * fail.
7094 	 */
7095 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
7096 		ice_ptp_reset(pf);
7097 
7098 	if (ice_is_feature_supported(pf, ICE_F_GNSS))
7099 		ice_gnss_init(pf);
7100 
7101 	/* rebuild PF VSI */
7102 	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
7103 	if (err) {
7104 		dev_err(dev, "PF VSI rebuild failed: %d\n", err);
7105 		goto err_vsi_rebuild;
7106 	}
7107 
7108 	/* configure PTP timestamping after VSI rebuild */
7109 	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
7110 		ice_ptp_cfg_timestamp(pf, false);
7111 
7112 	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_SWITCHDEV_CTRL);
7113 	if (err) {
7114 		dev_err(dev, "Switchdev CTRL VSI rebuild failed: %d\n", err);
7115 		goto err_vsi_rebuild;
7116 	}
7117 
7118 	if (reset_type == ICE_RESET_PFR) {
7119 		err = ice_rebuild_channels(pf);
7120 		if (err) {
7121 			dev_err(dev, "failed to rebuild and replay ADQ VSIs, err %d\n",
7122 				err);
7123 			goto err_vsi_rebuild;
7124 		}
7125 	}
7126 
7127 	/* If Flow Director is active */
7128 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7129 		err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
7130 		if (err) {
7131 			dev_err(dev, "control VSI rebuild failed: %d\n", err);
7132 			goto err_vsi_rebuild;
7133 		}
7134 
7135 		/* replay HW Flow Director recipes */
7136 		if (hw->fdir_prof)
7137 			ice_fdir_replay_flows(hw);
7138 
7139 		/* replay Flow Director filters */
7140 		ice_fdir_replay_fltrs(pf);
7141 
7142 		ice_rebuild_arfs(pf);
7143 	}
7144 
7145 	ice_update_pf_netdev_link(pf);
7146 
7147 	/* tell the firmware we are up */
7148 	err = ice_send_version(pf);
7149 	if (err) {
7150 		dev_err(dev, "Rebuild failed due to error sending driver version: %d\n",
7151 			err);
7152 		goto err_vsi_rebuild;
7153 	}
7154 
7155 	ice_replay_post(hw);
7156 
7157 	/* if we get here, reset flow is successful */
7158 	clear_bit(ICE_RESET_FAILED, pf->state);
7159 
7160 	ice_plug_aux_dev(pf);
7161 	return;
7162 
7163 err_vsi_rebuild:
7164 err_sched_init_port:
7165 	ice_sched_cleanup_all(hw);
7166 err_init_ctrlq:
7167 	ice_shutdown_all_ctrlq(hw);
7168 	set_bit(ICE_RESET_FAILED, pf->state);
7169 clear_recovery:
7170 	/* set this bit in PF state to control service task scheduling */
7171 	set_bit(ICE_NEEDS_RESTART, pf->state);
7172 	dev_err(dev, "Rebuild failed, unload and reload driver\n");
7173 }
7174 
7175 /**
7176  * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
7177  * @vsi: Pointer to VSI structure
7178  */
ice_max_xdp_frame_size(struct ice_vsi * vsi)7179 static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
7180 {
7181 	if (PAGE_SIZE >= 8192 || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
7182 		return ICE_RXBUF_2048 - XDP_PACKET_HEADROOM;
7183 	else
7184 		return ICE_RXBUF_3072;
7185 }
7186 
7187 /**
7188  * ice_change_mtu - NDO callback to change the MTU
7189  * @netdev: network interface device structure
7190  * @new_mtu: new value for maximum frame size
7191  *
7192  * Returns 0 on success, negative on failure
7193  */
ice_change_mtu(struct net_device * netdev,int new_mtu)7194 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
7195 {
7196 	struct ice_netdev_priv *np = netdev_priv(netdev);
7197 	struct ice_vsi *vsi = np->vsi;
7198 	struct ice_pf *pf = vsi->back;
7199 	u8 count = 0;
7200 	int err = 0;
7201 
7202 	if (new_mtu == (int)netdev->mtu) {
7203 		netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
7204 		return 0;
7205 	}
7206 
7207 	if (ice_is_xdp_ena_vsi(vsi)) {
7208 		int frame_size = ice_max_xdp_frame_size(vsi);
7209 
7210 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
7211 			netdev_err(netdev, "max MTU for XDP usage is %d\n",
7212 				   frame_size - ICE_ETH_PKT_HDR_PAD);
7213 			return -EINVAL;
7214 		}
7215 	}
7216 
7217 	/* if a reset is in progress, wait for some time for it to complete */
7218 	do {
7219 		if (ice_is_reset_in_progress(pf->state)) {
7220 			count++;
7221 			usleep_range(1000, 2000);
7222 		} else {
7223 			break;
7224 		}
7225 
7226 	} while (count < 100);
7227 
7228 	if (count == 100) {
7229 		netdev_err(netdev, "can't change MTU. Device is busy\n");
7230 		return -EBUSY;
7231 	}
7232 
7233 	netdev->mtu = (unsigned int)new_mtu;
7234 
7235 	/* if VSI is up, bring it down and then back up */
7236 	if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
7237 		err = ice_down(vsi);
7238 		if (err) {
7239 			netdev_err(netdev, "change MTU if_down err %d\n", err);
7240 			return err;
7241 		}
7242 
7243 		err = ice_up(vsi);
7244 		if (err) {
7245 			netdev_err(netdev, "change MTU if_up err %d\n", err);
7246 			return err;
7247 		}
7248 	}
7249 
7250 	netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
7251 	set_bit(ICE_FLAG_MTU_CHANGED, pf->flags);
7252 
7253 	return err;
7254 }
7255 
7256 /**
7257  * ice_eth_ioctl - Access the hwtstamp interface
7258  * @netdev: network interface device structure
7259  * @ifr: interface request data
7260  * @cmd: ioctl command
7261  */
ice_eth_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)7262 static int ice_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
7263 {
7264 	struct ice_netdev_priv *np = netdev_priv(netdev);
7265 	struct ice_pf *pf = np->vsi->back;
7266 
7267 	switch (cmd) {
7268 	case SIOCGHWTSTAMP:
7269 		return ice_ptp_get_ts_config(pf, ifr);
7270 	case SIOCSHWTSTAMP:
7271 		return ice_ptp_set_ts_config(pf, ifr);
7272 	default:
7273 		return -EOPNOTSUPP;
7274 	}
7275 }
7276 
7277 /**
7278  * ice_aq_str - convert AQ err code to a string
7279  * @aq_err: the AQ error code to convert
7280  */
ice_aq_str(enum ice_aq_err aq_err)7281 const char *ice_aq_str(enum ice_aq_err aq_err)
7282 {
7283 	switch (aq_err) {
7284 	case ICE_AQ_RC_OK:
7285 		return "OK";
7286 	case ICE_AQ_RC_EPERM:
7287 		return "ICE_AQ_RC_EPERM";
7288 	case ICE_AQ_RC_ENOENT:
7289 		return "ICE_AQ_RC_ENOENT";
7290 	case ICE_AQ_RC_ENOMEM:
7291 		return "ICE_AQ_RC_ENOMEM";
7292 	case ICE_AQ_RC_EBUSY:
7293 		return "ICE_AQ_RC_EBUSY";
7294 	case ICE_AQ_RC_EEXIST:
7295 		return "ICE_AQ_RC_EEXIST";
7296 	case ICE_AQ_RC_EINVAL:
7297 		return "ICE_AQ_RC_EINVAL";
7298 	case ICE_AQ_RC_ENOSPC:
7299 		return "ICE_AQ_RC_ENOSPC";
7300 	case ICE_AQ_RC_ENOSYS:
7301 		return "ICE_AQ_RC_ENOSYS";
7302 	case ICE_AQ_RC_EMODE:
7303 		return "ICE_AQ_RC_EMODE";
7304 	case ICE_AQ_RC_ENOSEC:
7305 		return "ICE_AQ_RC_ENOSEC";
7306 	case ICE_AQ_RC_EBADSIG:
7307 		return "ICE_AQ_RC_EBADSIG";
7308 	case ICE_AQ_RC_ESVN:
7309 		return "ICE_AQ_RC_ESVN";
7310 	case ICE_AQ_RC_EBADMAN:
7311 		return "ICE_AQ_RC_EBADMAN";
7312 	case ICE_AQ_RC_EBADBUF:
7313 		return "ICE_AQ_RC_EBADBUF";
7314 	}
7315 
7316 	return "ICE_AQ_RC_UNKNOWN";
7317 }
7318 
7319 /**
7320  * ice_set_rss_lut - Set RSS LUT
7321  * @vsi: Pointer to VSI structure
7322  * @lut: Lookup table
7323  * @lut_size: Lookup table size
7324  *
7325  * Returns 0 on success, negative on failure
7326  */
ice_set_rss_lut(struct ice_vsi * vsi,u8 * lut,u16 lut_size)7327 int ice_set_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7328 {
7329 	struct ice_aq_get_set_rss_lut_params params = {};
7330 	struct ice_hw *hw = &vsi->back->hw;
7331 	int status;
7332 
7333 	if (!lut)
7334 		return -EINVAL;
7335 
7336 	params.vsi_handle = vsi->idx;
7337 	params.lut_size = lut_size;
7338 	params.lut_type = vsi->rss_lut_type;
7339 	params.lut = lut;
7340 
7341 	status = ice_aq_set_rss_lut(hw, &params);
7342 	if (status)
7343 		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS lut, err %d aq_err %s\n",
7344 			status, ice_aq_str(hw->adminq.sq_last_status));
7345 
7346 	return status;
7347 }
7348 
7349 /**
7350  * ice_set_rss_key - Set RSS key
7351  * @vsi: Pointer to the VSI structure
7352  * @seed: RSS hash seed
7353  *
7354  * Returns 0 on success, negative on failure
7355  */
ice_set_rss_key(struct ice_vsi * vsi,u8 * seed)7356 int ice_set_rss_key(struct ice_vsi *vsi, u8 *seed)
7357 {
7358 	struct ice_hw *hw = &vsi->back->hw;
7359 	int status;
7360 
7361 	if (!seed)
7362 		return -EINVAL;
7363 
7364 	status = ice_aq_set_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7365 	if (status)
7366 		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS key, err %d aq_err %s\n",
7367 			status, ice_aq_str(hw->adminq.sq_last_status));
7368 
7369 	return status;
7370 }
7371 
7372 /**
7373  * ice_get_rss_lut - Get RSS LUT
7374  * @vsi: Pointer to VSI structure
7375  * @lut: Buffer to store the lookup table entries
7376  * @lut_size: Size of buffer to store the lookup table entries
7377  *
7378  * Returns 0 on success, negative on failure
7379  */
ice_get_rss_lut(struct ice_vsi * vsi,u8 * lut,u16 lut_size)7380 int ice_get_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7381 {
7382 	struct ice_aq_get_set_rss_lut_params params = {};
7383 	struct ice_hw *hw = &vsi->back->hw;
7384 	int status;
7385 
7386 	if (!lut)
7387 		return -EINVAL;
7388 
7389 	params.vsi_handle = vsi->idx;
7390 	params.lut_size = lut_size;
7391 	params.lut_type = vsi->rss_lut_type;
7392 	params.lut = lut;
7393 
7394 	status = ice_aq_get_rss_lut(hw, &params);
7395 	if (status)
7396 		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS lut, err %d aq_err %s\n",
7397 			status, ice_aq_str(hw->adminq.sq_last_status));
7398 
7399 	return status;
7400 }
7401 
7402 /**
7403  * ice_get_rss_key - Get RSS key
7404  * @vsi: Pointer to VSI structure
7405  * @seed: Buffer to store the key in
7406  *
7407  * Returns 0 on success, negative on failure
7408  */
ice_get_rss_key(struct ice_vsi * vsi,u8 * seed)7409 int ice_get_rss_key(struct ice_vsi *vsi, u8 *seed)
7410 {
7411 	struct ice_hw *hw = &vsi->back->hw;
7412 	int status;
7413 
7414 	if (!seed)
7415 		return -EINVAL;
7416 
7417 	status = ice_aq_get_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7418 	if (status)
7419 		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS key, err %d aq_err %s\n",
7420 			status, ice_aq_str(hw->adminq.sq_last_status));
7421 
7422 	return status;
7423 }
7424 
7425 /**
7426  * ice_bridge_getlink - Get the hardware bridge mode
7427  * @skb: skb buff
7428  * @pid: process ID
7429  * @seq: RTNL message seq
7430  * @dev: the netdev being configured
7431  * @filter_mask: filter mask passed in
7432  * @nlflags: netlink flags passed in
7433  *
7434  * Return the bridge mode (VEB/VEPA)
7435  */
7436 static int
ice_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 filter_mask,int nlflags)7437 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
7438 		   struct net_device *dev, u32 filter_mask, int nlflags)
7439 {
7440 	struct ice_netdev_priv *np = netdev_priv(dev);
7441 	struct ice_vsi *vsi = np->vsi;
7442 	struct ice_pf *pf = vsi->back;
7443 	u16 bmode;
7444 
7445 	bmode = pf->first_sw->bridge_mode;
7446 
7447 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
7448 				       filter_mask, NULL);
7449 }
7450 
7451 /**
7452  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
7453  * @vsi: Pointer to VSI structure
7454  * @bmode: Hardware bridge mode (VEB/VEPA)
7455  *
7456  * Returns 0 on success, negative on failure
7457  */
ice_vsi_update_bridge_mode(struct ice_vsi * vsi,u16 bmode)7458 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
7459 {
7460 	struct ice_aqc_vsi_props *vsi_props;
7461 	struct ice_hw *hw = &vsi->back->hw;
7462 	struct ice_vsi_ctx *ctxt;
7463 	int ret;
7464 
7465 	vsi_props = &vsi->info;
7466 
7467 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
7468 	if (!ctxt)
7469 		return -ENOMEM;
7470 
7471 	ctxt->info = vsi->info;
7472 
7473 	if (bmode == BRIDGE_MODE_VEB)
7474 		/* change from VEPA to VEB mode */
7475 		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7476 	else
7477 		/* change from VEB to VEPA mode */
7478 		ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7479 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
7480 
7481 	ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
7482 	if (ret) {
7483 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %d aq_err %s\n",
7484 			bmode, ret, ice_aq_str(hw->adminq.sq_last_status));
7485 		goto out;
7486 	}
7487 	/* Update sw flags for book keeping */
7488 	vsi_props->sw_flags = ctxt->info.sw_flags;
7489 
7490 out:
7491 	kfree(ctxt);
7492 	return ret;
7493 }
7494 
7495 /**
7496  * ice_bridge_setlink - Set the hardware bridge mode
7497  * @dev: the netdev being configured
7498  * @nlh: RTNL message
7499  * @flags: bridge setlink flags
7500  * @extack: netlink extended ack
7501  *
7502  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
7503  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
7504  * not already set for all VSIs connected to this switch. And also update the
7505  * unicast switch filter rules for the corresponding switch of the netdev.
7506  */
7507 static int
ice_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 __always_unused flags,struct netlink_ext_ack __always_unused * extack)7508 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
7509 		   u16 __always_unused flags,
7510 		   struct netlink_ext_ack __always_unused *extack)
7511 {
7512 	struct ice_netdev_priv *np = netdev_priv(dev);
7513 	struct ice_pf *pf = np->vsi->back;
7514 	struct nlattr *attr, *br_spec;
7515 	struct ice_hw *hw = &pf->hw;
7516 	struct ice_sw *pf_sw;
7517 	int rem, v, err = 0;
7518 
7519 	pf_sw = pf->first_sw;
7520 	/* find the attribute in the netlink message */
7521 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
7522 
7523 	nla_for_each_nested(attr, br_spec, rem) {
7524 		__u16 mode;
7525 
7526 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
7527 			continue;
7528 		mode = nla_get_u16(attr);
7529 		if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
7530 			return -EINVAL;
7531 		/* Continue  if bridge mode is not being flipped */
7532 		if (mode == pf_sw->bridge_mode)
7533 			continue;
7534 		/* Iterates through the PF VSI list and update the loopback
7535 		 * mode of the VSI
7536 		 */
7537 		ice_for_each_vsi(pf, v) {
7538 			if (!pf->vsi[v])
7539 				continue;
7540 			err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
7541 			if (err)
7542 				return err;
7543 		}
7544 
7545 		hw->evb_veb = (mode == BRIDGE_MODE_VEB);
7546 		/* Update the unicast switch filter rules for the corresponding
7547 		 * switch of the netdev
7548 		 */
7549 		err = ice_update_sw_rule_bridge_mode(hw);
7550 		if (err) {
7551 			netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %s\n",
7552 				   mode, err,
7553 				   ice_aq_str(hw->adminq.sq_last_status));
7554 			/* revert hw->evb_veb */
7555 			hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
7556 			return err;
7557 		}
7558 
7559 		pf_sw->bridge_mode = mode;
7560 	}
7561 
7562 	return 0;
7563 }
7564 
7565 /**
7566  * ice_tx_timeout - Respond to a Tx Hang
7567  * @netdev: network interface device structure
7568  * @txqueue: Tx queue
7569  */
ice_tx_timeout(struct net_device * netdev,unsigned int txqueue)7570 static void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
7571 {
7572 	struct ice_netdev_priv *np = netdev_priv(netdev);
7573 	struct ice_tx_ring *tx_ring = NULL;
7574 	struct ice_vsi *vsi = np->vsi;
7575 	struct ice_pf *pf = vsi->back;
7576 	u32 i;
7577 
7578 	pf->tx_timeout_count++;
7579 
7580 	/* Check if PFC is enabled for the TC to which the queue belongs
7581 	 * to. If yes then Tx timeout is not caused by a hung queue, no
7582 	 * need to reset and rebuild
7583 	 */
7584 	if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
7585 		dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
7586 			 txqueue);
7587 		return;
7588 	}
7589 
7590 	/* now that we have an index, find the tx_ring struct */
7591 	ice_for_each_txq(vsi, i)
7592 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
7593 			if (txqueue == vsi->tx_rings[i]->q_index) {
7594 				tx_ring = vsi->tx_rings[i];
7595 				break;
7596 			}
7597 
7598 	/* Reset recovery level if enough time has elapsed after last timeout.
7599 	 * Also ensure no new reset action happens before next timeout period.
7600 	 */
7601 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
7602 		pf->tx_timeout_recovery_level = 1;
7603 	else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
7604 				       netdev->watchdog_timeo)))
7605 		return;
7606 
7607 	if (tx_ring) {
7608 		struct ice_hw *hw = &pf->hw;
7609 		u32 head, val = 0;
7610 
7611 		head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])) &
7612 			QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
7613 		/* Read interrupt register */
7614 		val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
7615 
7616 		netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
7617 			    vsi->vsi_num, txqueue, tx_ring->next_to_clean,
7618 			    head, tx_ring->next_to_use, val);
7619 	}
7620 
7621 	pf->tx_timeout_last_recovery = jiffies;
7622 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
7623 		    pf->tx_timeout_recovery_level, txqueue);
7624 
7625 	switch (pf->tx_timeout_recovery_level) {
7626 	case 1:
7627 		set_bit(ICE_PFR_REQ, pf->state);
7628 		break;
7629 	case 2:
7630 		set_bit(ICE_CORER_REQ, pf->state);
7631 		break;
7632 	case 3:
7633 		set_bit(ICE_GLOBR_REQ, pf->state);
7634 		break;
7635 	default:
7636 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
7637 		set_bit(ICE_DOWN, pf->state);
7638 		set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
7639 		set_bit(ICE_SERVICE_DIS, pf->state);
7640 		break;
7641 	}
7642 
7643 	ice_service_task_schedule(pf);
7644 	pf->tx_timeout_recovery_level++;
7645 }
7646 
7647 /**
7648  * ice_setup_tc_cls_flower - flower classifier offloads
7649  * @np: net device to configure
7650  * @filter_dev: device on which filter is added
7651  * @cls_flower: offload data
7652  */
7653 static int
ice_setup_tc_cls_flower(struct ice_netdev_priv * np,struct net_device * filter_dev,struct flow_cls_offload * cls_flower)7654 ice_setup_tc_cls_flower(struct ice_netdev_priv *np,
7655 			struct net_device *filter_dev,
7656 			struct flow_cls_offload *cls_flower)
7657 {
7658 	struct ice_vsi *vsi = np->vsi;
7659 
7660 	if (cls_flower->common.chain_index)
7661 		return -EOPNOTSUPP;
7662 
7663 	switch (cls_flower->command) {
7664 	case FLOW_CLS_REPLACE:
7665 		return ice_add_cls_flower(filter_dev, vsi, cls_flower);
7666 	case FLOW_CLS_DESTROY:
7667 		return ice_del_cls_flower(vsi, cls_flower);
7668 	default:
7669 		return -EINVAL;
7670 	}
7671 }
7672 
7673 /**
7674  * ice_setup_tc_block_cb - callback handler registered for TC block
7675  * @type: TC SETUP type
7676  * @type_data: TC flower offload data that contains user input
7677  * @cb_priv: netdev private data
7678  */
7679 static int
ice_setup_tc_block_cb(enum tc_setup_type type,void * type_data,void * cb_priv)7680 ice_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
7681 {
7682 	struct ice_netdev_priv *np = cb_priv;
7683 
7684 	switch (type) {
7685 	case TC_SETUP_CLSFLOWER:
7686 		return ice_setup_tc_cls_flower(np, np->vsi->netdev,
7687 					       type_data);
7688 	default:
7689 		return -EOPNOTSUPP;
7690 	}
7691 }
7692 
7693 /**
7694  * ice_validate_mqprio_qopt - Validate TCF input parameters
7695  * @vsi: Pointer to VSI
7696  * @mqprio_qopt: input parameters for mqprio queue configuration
7697  *
7698  * This function validates MQPRIO params, such as qcount (power of 2 wherever
7699  * needed), and make sure user doesn't specify qcount and BW rate limit
7700  * for TCs, which are more than "num_tc"
7701  */
7702 static int
ice_validate_mqprio_qopt(struct ice_vsi * vsi,struct tc_mqprio_qopt_offload * mqprio_qopt)7703 ice_validate_mqprio_qopt(struct ice_vsi *vsi,
7704 			 struct tc_mqprio_qopt_offload *mqprio_qopt)
7705 {
7706 	u64 sum_max_rate = 0, sum_min_rate = 0;
7707 	int non_power_of_2_qcount = 0;
7708 	struct ice_pf *pf = vsi->back;
7709 	int max_rss_q_cnt = 0;
7710 	struct device *dev;
7711 	int i, speed;
7712 	u8 num_tc;
7713 
7714 	if (vsi->type != ICE_VSI_PF)
7715 		return -EINVAL;
7716 
7717 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7718 	    mqprio_qopt->qopt.num_tc < 1 ||
7719 	    mqprio_qopt->qopt.num_tc > ICE_CHNL_MAX_TC)
7720 		return -EINVAL;
7721 
7722 	dev = ice_pf_to_dev(pf);
7723 	vsi->ch_rss_size = 0;
7724 	num_tc = mqprio_qopt->qopt.num_tc;
7725 
7726 	for (i = 0; num_tc; i++) {
7727 		int qcount = mqprio_qopt->qopt.count[i];
7728 		u64 max_rate, min_rate, rem;
7729 
7730 		if (!qcount)
7731 			return -EINVAL;
7732 
7733 		if (is_power_of_2(qcount)) {
7734 			if (non_power_of_2_qcount &&
7735 			    qcount > non_power_of_2_qcount) {
7736 				dev_err(dev, "qcount[%d] cannot be greater than non power of 2 qcount[%d]\n",
7737 					qcount, non_power_of_2_qcount);
7738 				return -EINVAL;
7739 			}
7740 			if (qcount > max_rss_q_cnt)
7741 				max_rss_q_cnt = qcount;
7742 		} else {
7743 			if (non_power_of_2_qcount &&
7744 			    qcount != non_power_of_2_qcount) {
7745 				dev_err(dev, "Only one non power of 2 qcount allowed[%d,%d]\n",
7746 					qcount, non_power_of_2_qcount);
7747 				return -EINVAL;
7748 			}
7749 			if (qcount < max_rss_q_cnt) {
7750 				dev_err(dev, "non power of 2 qcount[%d] cannot be less than other qcount[%d]\n",
7751 					qcount, max_rss_q_cnt);
7752 				return -EINVAL;
7753 			}
7754 			max_rss_q_cnt = qcount;
7755 			non_power_of_2_qcount = qcount;
7756 		}
7757 
7758 		/* TC command takes input in K/N/Gbps or K/M/Gbit etc but
7759 		 * converts the bandwidth rate limit into Bytes/s when
7760 		 * passing it down to the driver. So convert input bandwidth
7761 		 * from Bytes/s to Kbps
7762 		 */
7763 		max_rate = mqprio_qopt->max_rate[i];
7764 		max_rate = div_u64(max_rate, ICE_BW_KBPS_DIVISOR);
7765 		sum_max_rate += max_rate;
7766 
7767 		/* min_rate is minimum guaranteed rate and it can't be zero */
7768 		min_rate = mqprio_qopt->min_rate[i];
7769 		min_rate = div_u64(min_rate, ICE_BW_KBPS_DIVISOR);
7770 		sum_min_rate += min_rate;
7771 
7772 		if (min_rate && min_rate < ICE_MIN_BW_LIMIT) {
7773 			dev_err(dev, "TC%d: min_rate(%llu Kbps) < %u Kbps\n", i,
7774 				min_rate, ICE_MIN_BW_LIMIT);
7775 			return -EINVAL;
7776 		}
7777 
7778 		iter_div_u64_rem(min_rate, ICE_MIN_BW_LIMIT, &rem);
7779 		if (rem) {
7780 			dev_err(dev, "TC%d: Min Rate not multiple of %u Kbps",
7781 				i, ICE_MIN_BW_LIMIT);
7782 			return -EINVAL;
7783 		}
7784 
7785 		iter_div_u64_rem(max_rate, ICE_MIN_BW_LIMIT, &rem);
7786 		if (rem) {
7787 			dev_err(dev, "TC%d: Max Rate not multiple of %u Kbps",
7788 				i, ICE_MIN_BW_LIMIT);
7789 			return -EINVAL;
7790 		}
7791 
7792 		/* min_rate can't be more than max_rate, except when max_rate
7793 		 * is zero (implies max_rate sought is max line rate). In such
7794 		 * a case min_rate can be more than max.
7795 		 */
7796 		if (max_rate && min_rate > max_rate) {
7797 			dev_err(dev, "min_rate %llu Kbps can't be more than max_rate %llu Kbps\n",
7798 				min_rate, max_rate);
7799 			return -EINVAL;
7800 		}
7801 
7802 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7803 			break;
7804 		if (mqprio_qopt->qopt.offset[i + 1] !=
7805 		    (mqprio_qopt->qopt.offset[i] + qcount))
7806 			return -EINVAL;
7807 	}
7808 	if (vsi->num_rxq <
7809 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7810 		return -EINVAL;
7811 	if (vsi->num_txq <
7812 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7813 		return -EINVAL;
7814 
7815 	speed = ice_get_link_speed_kbps(vsi);
7816 	if (sum_max_rate && sum_max_rate > (u64)speed) {
7817 		dev_err(dev, "Invalid max Tx rate(%llu) Kbps > speed(%u) Kbps specified\n",
7818 			sum_max_rate, speed);
7819 		return -EINVAL;
7820 	}
7821 	if (sum_min_rate && sum_min_rate > (u64)speed) {
7822 		dev_err(dev, "Invalid min Tx rate(%llu) Kbps > speed (%u) Kbps specified\n",
7823 			sum_min_rate, speed);
7824 		return -EINVAL;
7825 	}
7826 
7827 	/* make sure vsi->ch_rss_size is set correctly based on TC's qcount */
7828 	vsi->ch_rss_size = max_rss_q_cnt;
7829 
7830 	return 0;
7831 }
7832 
7833 /**
7834  * ice_add_vsi_to_fdir - add a VSI to the flow director group for PF
7835  * @pf: ptr to PF device
7836  * @vsi: ptr to VSI
7837  */
ice_add_vsi_to_fdir(struct ice_pf * pf,struct ice_vsi * vsi)7838 static int ice_add_vsi_to_fdir(struct ice_pf *pf, struct ice_vsi *vsi)
7839 {
7840 	struct device *dev = ice_pf_to_dev(pf);
7841 	bool added = false;
7842 	struct ice_hw *hw;
7843 	int flow;
7844 
7845 	if (!(vsi->num_gfltr || vsi->num_bfltr))
7846 		return -EINVAL;
7847 
7848 	hw = &pf->hw;
7849 	for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++) {
7850 		struct ice_fd_hw_prof *prof;
7851 		int tun, status;
7852 		u64 entry_h;
7853 
7854 		if (!(hw->fdir_prof && hw->fdir_prof[flow] &&
7855 		      hw->fdir_prof[flow]->cnt))
7856 			continue;
7857 
7858 		for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
7859 			enum ice_flow_priority prio;
7860 			u64 prof_id;
7861 
7862 			/* add this VSI to FDir profile for this flow */
7863 			prio = ICE_FLOW_PRIO_NORMAL;
7864 			prof = hw->fdir_prof[flow];
7865 			prof_id = flow + tun * ICE_FLTR_PTYPE_MAX;
7866 			status = ice_flow_add_entry(hw, ICE_BLK_FD, prof_id,
7867 						    prof->vsi_h[0], vsi->idx,
7868 						    prio, prof->fdir_seg[tun],
7869 						    &entry_h);
7870 			if (status) {
7871 				dev_err(dev, "channel VSI idx %d, not able to add to group %d\n",
7872 					vsi->idx, flow);
7873 				continue;
7874 			}
7875 
7876 			prof->entry_h[prof->cnt][tun] = entry_h;
7877 		}
7878 
7879 		/* store VSI for filter replay and delete */
7880 		prof->vsi_h[prof->cnt] = vsi->idx;
7881 		prof->cnt++;
7882 
7883 		added = true;
7884 		dev_dbg(dev, "VSI idx %d added to fdir group %d\n", vsi->idx,
7885 			flow);
7886 	}
7887 
7888 	if (!added)
7889 		dev_dbg(dev, "VSI idx %d not added to fdir groups\n", vsi->idx);
7890 
7891 	return 0;
7892 }
7893 
7894 /**
7895  * ice_add_channel - add a channel by adding VSI
7896  * @pf: ptr to PF device
7897  * @sw_id: underlying HW switching element ID
7898  * @ch: ptr to channel structure
7899  *
7900  * Add a channel (VSI) using add_vsi and queue_map
7901  */
ice_add_channel(struct ice_pf * pf,u16 sw_id,struct ice_channel * ch)7902 static int ice_add_channel(struct ice_pf *pf, u16 sw_id, struct ice_channel *ch)
7903 {
7904 	struct device *dev = ice_pf_to_dev(pf);
7905 	struct ice_vsi *vsi;
7906 
7907 	if (ch->type != ICE_VSI_CHNL) {
7908 		dev_err(dev, "add new VSI failed, ch->type %d\n", ch->type);
7909 		return -EINVAL;
7910 	}
7911 
7912 	vsi = ice_chnl_vsi_setup(pf, pf->hw.port_info, ch);
7913 	if (!vsi || vsi->type != ICE_VSI_CHNL) {
7914 		dev_err(dev, "create chnl VSI failure\n");
7915 		return -EINVAL;
7916 	}
7917 
7918 	ice_add_vsi_to_fdir(pf, vsi);
7919 
7920 	ch->sw_id = sw_id;
7921 	ch->vsi_num = vsi->vsi_num;
7922 	ch->info.mapping_flags = vsi->info.mapping_flags;
7923 	ch->ch_vsi = vsi;
7924 	/* set the back pointer of channel for newly created VSI */
7925 	vsi->ch = ch;
7926 
7927 	memcpy(&ch->info.q_mapping, &vsi->info.q_mapping,
7928 	       sizeof(vsi->info.q_mapping));
7929 	memcpy(&ch->info.tc_mapping, vsi->info.tc_mapping,
7930 	       sizeof(vsi->info.tc_mapping));
7931 
7932 	return 0;
7933 }
7934 
7935 /**
7936  * ice_chnl_cfg_res
7937  * @vsi: the VSI being setup
7938  * @ch: ptr to channel structure
7939  *
7940  * Configure channel specific resources such as rings, vector.
7941  */
ice_chnl_cfg_res(struct ice_vsi * vsi,struct ice_channel * ch)7942 static void ice_chnl_cfg_res(struct ice_vsi *vsi, struct ice_channel *ch)
7943 {
7944 	int i;
7945 
7946 	for (i = 0; i < ch->num_txq; i++) {
7947 		struct ice_q_vector *tx_q_vector, *rx_q_vector;
7948 		struct ice_ring_container *rc;
7949 		struct ice_tx_ring *tx_ring;
7950 		struct ice_rx_ring *rx_ring;
7951 
7952 		tx_ring = vsi->tx_rings[ch->base_q + i];
7953 		rx_ring = vsi->rx_rings[ch->base_q + i];
7954 		if (!tx_ring || !rx_ring)
7955 			continue;
7956 
7957 		/* setup ring being channel enabled */
7958 		tx_ring->ch = ch;
7959 		rx_ring->ch = ch;
7960 
7961 		/* following code block sets up vector specific attributes */
7962 		tx_q_vector = tx_ring->q_vector;
7963 		rx_q_vector = rx_ring->q_vector;
7964 		if (!tx_q_vector && !rx_q_vector)
7965 			continue;
7966 
7967 		if (tx_q_vector) {
7968 			tx_q_vector->ch = ch;
7969 			/* setup Tx and Rx ITR setting if DIM is off */
7970 			rc = &tx_q_vector->tx;
7971 			if (!ITR_IS_DYNAMIC(rc))
7972 				ice_write_itr(rc, rc->itr_setting);
7973 		}
7974 		if (rx_q_vector) {
7975 			rx_q_vector->ch = ch;
7976 			/* setup Tx and Rx ITR setting if DIM is off */
7977 			rc = &rx_q_vector->rx;
7978 			if (!ITR_IS_DYNAMIC(rc))
7979 				ice_write_itr(rc, rc->itr_setting);
7980 		}
7981 	}
7982 
7983 	/* it is safe to assume that, if channel has non-zero num_t[r]xq, then
7984 	 * GLINT_ITR register would have written to perform in-context
7985 	 * update, hence perform flush
7986 	 */
7987 	if (ch->num_txq || ch->num_rxq)
7988 		ice_flush(&vsi->back->hw);
7989 }
7990 
7991 /**
7992  * ice_cfg_chnl_all_res - configure channel resources
7993  * @vsi: pte to main_vsi
7994  * @ch: ptr to channel structure
7995  *
7996  * This function configures channel specific resources such as flow-director
7997  * counter index, and other resources such as queues, vectors, ITR settings
7998  */
7999 static void
ice_cfg_chnl_all_res(struct ice_vsi * vsi,struct ice_channel * ch)8000 ice_cfg_chnl_all_res(struct ice_vsi *vsi, struct ice_channel *ch)
8001 {
8002 	/* configure channel (aka ADQ) resources such as queues, vectors,
8003 	 * ITR settings for channel specific vectors and anything else
8004 	 */
8005 	ice_chnl_cfg_res(vsi, ch);
8006 }
8007 
8008 /**
8009  * ice_setup_hw_channel - setup new channel
8010  * @pf: ptr to PF device
8011  * @vsi: the VSI being setup
8012  * @ch: ptr to channel structure
8013  * @sw_id: underlying HW switching element ID
8014  * @type: type of channel to be created (VMDq2/VF)
8015  *
8016  * Setup new channel (VSI) based on specified type (VMDq2/VF)
8017  * and configures Tx rings accordingly
8018  */
8019 static int
ice_setup_hw_channel(struct ice_pf * pf,struct ice_vsi * vsi,struct ice_channel * ch,u16 sw_id,u8 type)8020 ice_setup_hw_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8021 		     struct ice_channel *ch, u16 sw_id, u8 type)
8022 {
8023 	struct device *dev = ice_pf_to_dev(pf);
8024 	int ret;
8025 
8026 	ch->base_q = vsi->next_base_q;
8027 	ch->type = type;
8028 
8029 	ret = ice_add_channel(pf, sw_id, ch);
8030 	if (ret) {
8031 		dev_err(dev, "failed to add_channel using sw_id %u\n", sw_id);
8032 		return ret;
8033 	}
8034 
8035 	/* configure/setup ADQ specific resources */
8036 	ice_cfg_chnl_all_res(vsi, ch);
8037 
8038 	/* make sure to update the next_base_q so that subsequent channel's
8039 	 * (aka ADQ) VSI queue map is correct
8040 	 */
8041 	vsi->next_base_q = vsi->next_base_q + ch->num_rxq;
8042 	dev_dbg(dev, "added channel: vsi_num %u, num_rxq %u\n", ch->vsi_num,
8043 		ch->num_rxq);
8044 
8045 	return 0;
8046 }
8047 
8048 /**
8049  * ice_setup_channel - setup new channel using uplink element
8050  * @pf: ptr to PF device
8051  * @vsi: the VSI being setup
8052  * @ch: ptr to channel structure
8053  *
8054  * Setup new channel (VSI) based on specified type (VMDq2/VF)
8055  * and uplink switching element
8056  */
8057 static bool
ice_setup_channel(struct ice_pf * pf,struct ice_vsi * vsi,struct ice_channel * ch)8058 ice_setup_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8059 		  struct ice_channel *ch)
8060 {
8061 	struct device *dev = ice_pf_to_dev(pf);
8062 	u16 sw_id;
8063 	int ret;
8064 
8065 	if (vsi->type != ICE_VSI_PF) {
8066 		dev_err(dev, "unsupported parent VSI type(%d)\n", vsi->type);
8067 		return false;
8068 	}
8069 
8070 	sw_id = pf->first_sw->sw_id;
8071 
8072 	/* create channel (VSI) */
8073 	ret = ice_setup_hw_channel(pf, vsi, ch, sw_id, ICE_VSI_CHNL);
8074 	if (ret) {
8075 		dev_err(dev, "failed to setup hw_channel\n");
8076 		return false;
8077 	}
8078 	dev_dbg(dev, "successfully created channel()\n");
8079 
8080 	return ch->ch_vsi ? true : false;
8081 }
8082 
8083 /**
8084  * ice_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
8085  * @vsi: VSI to be configured
8086  * @max_tx_rate: max Tx rate in Kbps to be configured as maximum BW limit
8087  * @min_tx_rate: min Tx rate in Kbps to be configured as minimum BW limit
8088  */
8089 static int
ice_set_bw_limit(struct ice_vsi * vsi,u64 max_tx_rate,u64 min_tx_rate)8090 ice_set_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate, u64 min_tx_rate)
8091 {
8092 	int err;
8093 
8094 	err = ice_set_min_bw_limit(vsi, min_tx_rate);
8095 	if (err)
8096 		return err;
8097 
8098 	return ice_set_max_bw_limit(vsi, max_tx_rate);
8099 }
8100 
8101 /**
8102  * ice_create_q_channel - function to create channel
8103  * @vsi: VSI to be configured
8104  * @ch: ptr to channel (it contains channel specific params)
8105  *
8106  * This function creates channel (VSI) using num_queues specified by user,
8107  * reconfigs RSS if needed.
8108  */
ice_create_q_channel(struct ice_vsi * vsi,struct ice_channel * ch)8109 static int ice_create_q_channel(struct ice_vsi *vsi, struct ice_channel *ch)
8110 {
8111 	struct ice_pf *pf = vsi->back;
8112 	struct device *dev;
8113 
8114 	if (!ch)
8115 		return -EINVAL;
8116 
8117 	dev = ice_pf_to_dev(pf);
8118 	if (!ch->num_txq || !ch->num_rxq) {
8119 		dev_err(dev, "Invalid num_queues requested: %d\n", ch->num_rxq);
8120 		return -EINVAL;
8121 	}
8122 
8123 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_txq) {
8124 		dev_err(dev, "cnt_q_avail (%u) less than num_queues %d\n",
8125 			vsi->cnt_q_avail, ch->num_txq);
8126 		return -EINVAL;
8127 	}
8128 
8129 	if (!ice_setup_channel(pf, vsi, ch)) {
8130 		dev_info(dev, "Failed to setup channel\n");
8131 		return -EINVAL;
8132 	}
8133 	/* configure BW rate limit */
8134 	if (ch->ch_vsi && (ch->max_tx_rate || ch->min_tx_rate)) {
8135 		int ret;
8136 
8137 		ret = ice_set_bw_limit(ch->ch_vsi, ch->max_tx_rate,
8138 				       ch->min_tx_rate);
8139 		if (ret)
8140 			dev_err(dev, "failed to set Tx rate of %llu Kbps for VSI(%u)\n",
8141 				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8142 		else
8143 			dev_dbg(dev, "set Tx rate of %llu Kbps for VSI(%u)\n",
8144 				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8145 	}
8146 
8147 	vsi->cnt_q_avail -= ch->num_txq;
8148 
8149 	return 0;
8150 }
8151 
8152 /**
8153  * ice_rem_all_chnl_fltrs - removes all channel filters
8154  * @pf: ptr to PF, TC-flower based filter are tracked at PF level
8155  *
8156  * Remove all advanced switch filters only if they are channel specific
8157  * tc-flower based filter
8158  */
ice_rem_all_chnl_fltrs(struct ice_pf * pf)8159 static void ice_rem_all_chnl_fltrs(struct ice_pf *pf)
8160 {
8161 	struct ice_tc_flower_fltr *fltr;
8162 	struct hlist_node *node;
8163 
8164 	/* to remove all channel filters, iterate an ordered list of filters */
8165 	hlist_for_each_entry_safe(fltr, node,
8166 				  &pf->tc_flower_fltr_list,
8167 				  tc_flower_node) {
8168 		struct ice_rule_query_data rule;
8169 		int status;
8170 
8171 		/* for now process only channel specific filters */
8172 		if (!ice_is_chnl_fltr(fltr))
8173 			continue;
8174 
8175 		rule.rid = fltr->rid;
8176 		rule.rule_id = fltr->rule_id;
8177 		rule.vsi_handle = fltr->dest_id;
8178 		status = ice_rem_adv_rule_by_id(&pf->hw, &rule);
8179 		if (status) {
8180 			if (status == -ENOENT)
8181 				dev_dbg(ice_pf_to_dev(pf), "TC flower filter (rule_id %u) does not exist\n",
8182 					rule.rule_id);
8183 			else
8184 				dev_err(ice_pf_to_dev(pf), "failed to delete TC flower filter, status %d\n",
8185 					status);
8186 		} else if (fltr->dest_vsi) {
8187 			/* update advanced switch filter count */
8188 			if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
8189 				u32 flags = fltr->flags;
8190 
8191 				fltr->dest_vsi->num_chnl_fltr--;
8192 				if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
8193 					     ICE_TC_FLWR_FIELD_ENC_DST_MAC))
8194 					pf->num_dmac_chnl_fltrs--;
8195 			}
8196 		}
8197 
8198 		hlist_del(&fltr->tc_flower_node);
8199 		kfree(fltr);
8200 	}
8201 }
8202 
8203 /**
8204  * ice_remove_q_channels - Remove queue channels for the TCs
8205  * @vsi: VSI to be configured
8206  * @rem_fltr: delete advanced switch filter or not
8207  *
8208  * Remove queue channels for the TCs
8209  */
ice_remove_q_channels(struct ice_vsi * vsi,bool rem_fltr)8210 static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_fltr)
8211 {
8212 	struct ice_channel *ch, *ch_tmp;
8213 	struct ice_pf *pf = vsi->back;
8214 	int i;
8215 
8216 	/* remove all tc-flower based filter if they are channel filters only */
8217 	if (rem_fltr)
8218 		ice_rem_all_chnl_fltrs(pf);
8219 
8220 	/* remove ntuple filters since queue configuration is being changed */
8221 	if  (vsi->netdev->features & NETIF_F_NTUPLE) {
8222 		struct ice_hw *hw = &pf->hw;
8223 
8224 		mutex_lock(&hw->fdir_fltr_lock);
8225 		ice_fdir_del_all_fltrs(vsi);
8226 		mutex_unlock(&hw->fdir_fltr_lock);
8227 	}
8228 
8229 	/* perform cleanup for channels if they exist */
8230 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
8231 		struct ice_vsi *ch_vsi;
8232 
8233 		list_del(&ch->list);
8234 		ch_vsi = ch->ch_vsi;
8235 		if (!ch_vsi) {
8236 			kfree(ch);
8237 			continue;
8238 		}
8239 
8240 		/* Reset queue contexts */
8241 		for (i = 0; i < ch->num_rxq; i++) {
8242 			struct ice_tx_ring *tx_ring;
8243 			struct ice_rx_ring *rx_ring;
8244 
8245 			tx_ring = vsi->tx_rings[ch->base_q + i];
8246 			rx_ring = vsi->rx_rings[ch->base_q + i];
8247 			if (tx_ring) {
8248 				tx_ring->ch = NULL;
8249 				if (tx_ring->q_vector)
8250 					tx_ring->q_vector->ch = NULL;
8251 			}
8252 			if (rx_ring) {
8253 				rx_ring->ch = NULL;
8254 				if (rx_ring->q_vector)
8255 					rx_ring->q_vector->ch = NULL;
8256 			}
8257 		}
8258 
8259 		/* Release FD resources for the channel VSI */
8260 		ice_fdir_rem_adq_chnl(&pf->hw, ch->ch_vsi->idx);
8261 
8262 		/* clear the VSI from scheduler tree */
8263 		ice_rm_vsi_lan_cfg(ch->ch_vsi->port_info, ch->ch_vsi->idx);
8264 
8265 		/* Delete VSI from FW */
8266 		ice_vsi_delete(ch->ch_vsi);
8267 
8268 		/* Delete VSI from PF and HW VSI arrays */
8269 		ice_vsi_clear(ch->ch_vsi);
8270 
8271 		/* free the channel */
8272 		kfree(ch);
8273 	}
8274 
8275 	/* clear the channel VSI map which is stored in main VSI */
8276 	ice_for_each_chnl_tc(i)
8277 		vsi->tc_map_vsi[i] = NULL;
8278 
8279 	/* reset main VSI's all TC information */
8280 	vsi->all_enatc = 0;
8281 	vsi->all_numtc = 0;
8282 }
8283 
8284 /**
8285  * ice_rebuild_channels - rebuild channel
8286  * @pf: ptr to PF
8287  *
8288  * Recreate channel VSIs and replay filters
8289  */
ice_rebuild_channels(struct ice_pf * pf)8290 static int ice_rebuild_channels(struct ice_pf *pf)
8291 {
8292 	struct device *dev = ice_pf_to_dev(pf);
8293 	struct ice_vsi *main_vsi;
8294 	bool rem_adv_fltr = true;
8295 	struct ice_channel *ch;
8296 	struct ice_vsi *vsi;
8297 	int tc_idx = 1;
8298 	int i, err;
8299 
8300 	main_vsi = ice_get_main_vsi(pf);
8301 	if (!main_vsi)
8302 		return 0;
8303 
8304 	if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) ||
8305 	    main_vsi->old_numtc == 1)
8306 		return 0; /* nothing to be done */
8307 
8308 	/* reconfigure main VSI based on old value of TC and cached values
8309 	 * for MQPRIO opts
8310 	 */
8311 	err = ice_vsi_cfg_tc(main_vsi, main_vsi->old_ena_tc);
8312 	if (err) {
8313 		dev_err(dev, "failed configuring TC(ena_tc:0x%02x) for HW VSI=%u\n",
8314 			main_vsi->old_ena_tc, main_vsi->vsi_num);
8315 		return err;
8316 	}
8317 
8318 	/* rebuild ADQ VSIs */
8319 	ice_for_each_vsi(pf, i) {
8320 		enum ice_vsi_type type;
8321 
8322 		vsi = pf->vsi[i];
8323 		if (!vsi || vsi->type != ICE_VSI_CHNL)
8324 			continue;
8325 
8326 		type = vsi->type;
8327 
8328 		/* rebuild ADQ VSI */
8329 		err = ice_vsi_rebuild(vsi, true);
8330 		if (err) {
8331 			dev_err(dev, "VSI (type:%s) at index %d rebuild failed, err %d\n",
8332 				ice_vsi_type_str(type), vsi->idx, err);
8333 			goto cleanup;
8334 		}
8335 
8336 		/* Re-map HW VSI number, using VSI handle that has been
8337 		 * previously validated in ice_replay_vsi() call above
8338 		 */
8339 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
8340 
8341 		/* replay filters for the VSI */
8342 		err = ice_replay_vsi(&pf->hw, vsi->idx);
8343 		if (err) {
8344 			dev_err(dev, "VSI (type:%s) replay failed, err %d, VSI index %d\n",
8345 				ice_vsi_type_str(type), err, vsi->idx);
8346 			rem_adv_fltr = false;
8347 			goto cleanup;
8348 		}
8349 		dev_info(dev, "VSI (type:%s) at index %d rebuilt successfully\n",
8350 			 ice_vsi_type_str(type), vsi->idx);
8351 
8352 		/* store ADQ VSI at correct TC index in main VSI's
8353 		 * map of TC to VSI
8354 		 */
8355 		main_vsi->tc_map_vsi[tc_idx++] = vsi;
8356 	}
8357 
8358 	/* ADQ VSI(s) has been rebuilt successfully, so setup
8359 	 * channel for main VSI's Tx and Rx rings
8360 	 */
8361 	list_for_each_entry(ch, &main_vsi->ch_list, list) {
8362 		struct ice_vsi *ch_vsi;
8363 
8364 		ch_vsi = ch->ch_vsi;
8365 		if (!ch_vsi)
8366 			continue;
8367 
8368 		/* reconfig channel resources */
8369 		ice_cfg_chnl_all_res(main_vsi, ch);
8370 
8371 		/* replay BW rate limit if it is non-zero */
8372 		if (!ch->max_tx_rate && !ch->min_tx_rate)
8373 			continue;
8374 
8375 		err = ice_set_bw_limit(ch_vsi, ch->max_tx_rate,
8376 				       ch->min_tx_rate);
8377 		if (err)
8378 			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",
8379 				err, ch->max_tx_rate, ch->min_tx_rate,
8380 				ch_vsi->vsi_num);
8381 		else
8382 			dev_dbg(dev, "successfully rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
8383 				ch->max_tx_rate, ch->min_tx_rate,
8384 				ch_vsi->vsi_num);
8385 	}
8386 
8387 	/* reconfig RSS for main VSI */
8388 	if (main_vsi->ch_rss_size)
8389 		ice_vsi_cfg_rss_lut_key(main_vsi);
8390 
8391 	return 0;
8392 
8393 cleanup:
8394 	ice_remove_q_channels(main_vsi, rem_adv_fltr);
8395 	return err;
8396 }
8397 
8398 /**
8399  * ice_create_q_channels - Add queue channel for the given TCs
8400  * @vsi: VSI to be configured
8401  *
8402  * Configures queue channel mapping to the given TCs
8403  */
ice_create_q_channels(struct ice_vsi * vsi)8404 static int ice_create_q_channels(struct ice_vsi *vsi)
8405 {
8406 	struct ice_pf *pf = vsi->back;
8407 	struct ice_channel *ch;
8408 	int ret = 0, i;
8409 
8410 	ice_for_each_chnl_tc(i) {
8411 		if (!(vsi->all_enatc & BIT(i)))
8412 			continue;
8413 
8414 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
8415 		if (!ch) {
8416 			ret = -ENOMEM;
8417 			goto err_free;
8418 		}
8419 		INIT_LIST_HEAD(&ch->list);
8420 		ch->num_rxq = vsi->mqprio_qopt.qopt.count[i];
8421 		ch->num_txq = vsi->mqprio_qopt.qopt.count[i];
8422 		ch->base_q = vsi->mqprio_qopt.qopt.offset[i];
8423 		ch->max_tx_rate = vsi->mqprio_qopt.max_rate[i];
8424 		ch->min_tx_rate = vsi->mqprio_qopt.min_rate[i];
8425 
8426 		/* convert to Kbits/s */
8427 		if (ch->max_tx_rate)
8428 			ch->max_tx_rate = div_u64(ch->max_tx_rate,
8429 						  ICE_BW_KBPS_DIVISOR);
8430 		if (ch->min_tx_rate)
8431 			ch->min_tx_rate = div_u64(ch->min_tx_rate,
8432 						  ICE_BW_KBPS_DIVISOR);
8433 
8434 		ret = ice_create_q_channel(vsi, ch);
8435 		if (ret) {
8436 			dev_err(ice_pf_to_dev(pf),
8437 				"failed creating channel TC:%d\n", i);
8438 			kfree(ch);
8439 			goto err_free;
8440 		}
8441 		list_add_tail(&ch->list, &vsi->ch_list);
8442 		vsi->tc_map_vsi[i] = ch->ch_vsi;
8443 		dev_dbg(ice_pf_to_dev(pf),
8444 			"successfully created channel: VSI %pK\n", ch->ch_vsi);
8445 	}
8446 	return 0;
8447 
8448 err_free:
8449 	ice_remove_q_channels(vsi, false);
8450 
8451 	return ret;
8452 }
8453 
8454 /**
8455  * ice_setup_tc_mqprio_qdisc - configure multiple traffic classes
8456  * @netdev: net device to configure
8457  * @type_data: TC offload data
8458  */
ice_setup_tc_mqprio_qdisc(struct net_device * netdev,void * type_data)8459 static int ice_setup_tc_mqprio_qdisc(struct net_device *netdev, void *type_data)
8460 {
8461 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8462 	struct ice_netdev_priv *np = netdev_priv(netdev);
8463 	struct ice_vsi *vsi = np->vsi;
8464 	struct ice_pf *pf = vsi->back;
8465 	u16 mode, ena_tc_qdisc = 0;
8466 	int cur_txq, cur_rxq;
8467 	u8 hw = 0, num_tcf;
8468 	struct device *dev;
8469 	int ret, i;
8470 
8471 	dev = ice_pf_to_dev(pf);
8472 	num_tcf = mqprio_qopt->qopt.num_tc;
8473 	hw = mqprio_qopt->qopt.hw;
8474 	mode = mqprio_qopt->mode;
8475 	if (!hw) {
8476 		clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8477 		vsi->ch_rss_size = 0;
8478 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8479 		goto config_tcf;
8480 	}
8481 
8482 	/* Generate queue region map for number of TCF requested */
8483 	for (i = 0; i < num_tcf; i++)
8484 		ena_tc_qdisc |= BIT(i);
8485 
8486 	switch (mode) {
8487 	case TC_MQPRIO_MODE_CHANNEL:
8488 
8489 		ret = ice_validate_mqprio_qopt(vsi, mqprio_qopt);
8490 		if (ret) {
8491 			netdev_err(netdev, "failed to validate_mqprio_qopt(), ret %d\n",
8492 				   ret);
8493 			return ret;
8494 		}
8495 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8496 		set_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8497 		/* don't assume state of hw_tc_offload during driver load
8498 		 * and set the flag for TC flower filter if hw_tc_offload
8499 		 * already ON
8500 		 */
8501 		if (vsi->netdev->features & NETIF_F_HW_TC)
8502 			set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
8503 		break;
8504 	default:
8505 		return -EINVAL;
8506 	}
8507 
8508 config_tcf:
8509 
8510 	/* Requesting same TCF configuration as already enabled */
8511 	if (ena_tc_qdisc == vsi->tc_cfg.ena_tc &&
8512 	    mode != TC_MQPRIO_MODE_CHANNEL)
8513 		return 0;
8514 
8515 	/* Pause VSI queues */
8516 	ice_dis_vsi(vsi, true);
8517 
8518 	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
8519 		ice_remove_q_channels(vsi, true);
8520 
8521 	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
8522 		vsi->req_txq = min_t(int, ice_get_avail_txq_count(pf),
8523 				     num_online_cpus());
8524 		vsi->req_rxq = min_t(int, ice_get_avail_rxq_count(pf),
8525 				     num_online_cpus());
8526 	} else {
8527 		/* logic to rebuild VSI, same like ethtool -L */
8528 		u16 offset = 0, qcount_tx = 0, qcount_rx = 0;
8529 
8530 		for (i = 0; i < num_tcf; i++) {
8531 			if (!(ena_tc_qdisc & BIT(i)))
8532 				continue;
8533 
8534 			offset = vsi->mqprio_qopt.qopt.offset[i];
8535 			qcount_rx = vsi->mqprio_qopt.qopt.count[i];
8536 			qcount_tx = vsi->mqprio_qopt.qopt.count[i];
8537 		}
8538 		vsi->req_txq = offset + qcount_tx;
8539 		vsi->req_rxq = offset + qcount_rx;
8540 
8541 		/* store away original rss_size info, so that it gets reused
8542 		 * form ice_vsi_rebuild during tc-qdisc delete stage - to
8543 		 * determine, what should be the rss_sizefor main VSI
8544 		 */
8545 		vsi->orig_rss_size = vsi->rss_size;
8546 	}
8547 
8548 	/* save current values of Tx and Rx queues before calling VSI rebuild
8549 	 * for fallback option
8550 	 */
8551 	cur_txq = vsi->num_txq;
8552 	cur_rxq = vsi->num_rxq;
8553 
8554 	/* proceed with rebuild main VSI using correct number of queues */
8555 	ret = ice_vsi_rebuild(vsi, false);
8556 	if (ret) {
8557 		/* fallback to current number of queues */
8558 		dev_info(dev, "Rebuild failed with new queues, try with current number of queues\n");
8559 		vsi->req_txq = cur_txq;
8560 		vsi->req_rxq = cur_rxq;
8561 		clear_bit(ICE_RESET_FAILED, pf->state);
8562 		if (ice_vsi_rebuild(vsi, false)) {
8563 			dev_err(dev, "Rebuild of main VSI failed again\n");
8564 			return ret;
8565 		}
8566 	}
8567 
8568 	vsi->all_numtc = num_tcf;
8569 	vsi->all_enatc = ena_tc_qdisc;
8570 	ret = ice_vsi_cfg_tc(vsi, ena_tc_qdisc);
8571 	if (ret) {
8572 		netdev_err(netdev, "failed configuring TC for VSI id=%d\n",
8573 			   vsi->vsi_num);
8574 		goto exit;
8575 	}
8576 
8577 	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
8578 		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8579 		u64 min_tx_rate = vsi->mqprio_qopt.min_rate[0];
8580 
8581 		/* set TC0 rate limit if specified */
8582 		if (max_tx_rate || min_tx_rate) {
8583 			/* convert to Kbits/s */
8584 			if (max_tx_rate)
8585 				max_tx_rate = div_u64(max_tx_rate, ICE_BW_KBPS_DIVISOR);
8586 			if (min_tx_rate)
8587 				min_tx_rate = div_u64(min_tx_rate, ICE_BW_KBPS_DIVISOR);
8588 
8589 			ret = ice_set_bw_limit(vsi, max_tx_rate, min_tx_rate);
8590 			if (!ret) {
8591 				dev_dbg(dev, "set Tx rate max %llu min %llu for VSI(%u)\n",
8592 					max_tx_rate, min_tx_rate, vsi->vsi_num);
8593 			} else {
8594 				dev_err(dev, "failed to set Tx rate max %llu min %llu for VSI(%u)\n",
8595 					max_tx_rate, min_tx_rate, vsi->vsi_num);
8596 				goto exit;
8597 			}
8598 		}
8599 		ret = ice_create_q_channels(vsi);
8600 		if (ret) {
8601 			netdev_err(netdev, "failed configuring queue channels\n");
8602 			goto exit;
8603 		} else {
8604 			netdev_dbg(netdev, "successfully configured channels\n");
8605 		}
8606 	}
8607 
8608 	if (vsi->ch_rss_size)
8609 		ice_vsi_cfg_rss_lut_key(vsi);
8610 
8611 exit:
8612 	/* if error, reset the all_numtc and all_enatc */
8613 	if (ret) {
8614 		vsi->all_numtc = 0;
8615 		vsi->all_enatc = 0;
8616 	}
8617 	/* resume VSI */
8618 	ice_ena_vsi(vsi, true);
8619 
8620 	return ret;
8621 }
8622 
8623 static LIST_HEAD(ice_block_cb_list);
8624 
8625 static int
ice_setup_tc(struct net_device * netdev,enum tc_setup_type type,void * type_data)8626 ice_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8627 	     void *type_data)
8628 {
8629 	struct ice_netdev_priv *np = netdev_priv(netdev);
8630 	struct ice_pf *pf = np->vsi->back;
8631 	int err;
8632 
8633 	switch (type) {
8634 	case TC_SETUP_BLOCK:
8635 		return flow_block_cb_setup_simple(type_data,
8636 						  &ice_block_cb_list,
8637 						  ice_setup_tc_block_cb,
8638 						  np, np, true);
8639 	case TC_SETUP_QDISC_MQPRIO:
8640 		/* setup traffic classifier for receive side */
8641 		mutex_lock(&pf->tc_mutex);
8642 		err = ice_setup_tc_mqprio_qdisc(netdev, type_data);
8643 		mutex_unlock(&pf->tc_mutex);
8644 		return err;
8645 	default:
8646 		return -EOPNOTSUPP;
8647 	}
8648 	return -EOPNOTSUPP;
8649 }
8650 
8651 static struct ice_indr_block_priv *
ice_indr_block_priv_lookup(struct ice_netdev_priv * np,struct net_device * netdev)8652 ice_indr_block_priv_lookup(struct ice_netdev_priv *np,
8653 			   struct net_device *netdev)
8654 {
8655 	struct ice_indr_block_priv *cb_priv;
8656 
8657 	list_for_each_entry(cb_priv, &np->tc_indr_block_priv_list, list) {
8658 		if (!cb_priv->netdev)
8659 			return NULL;
8660 		if (cb_priv->netdev == netdev)
8661 			return cb_priv;
8662 	}
8663 	return NULL;
8664 }
8665 
8666 static int
ice_indr_setup_block_cb(enum tc_setup_type type,void * type_data,void * indr_priv)8667 ice_indr_setup_block_cb(enum tc_setup_type type, void *type_data,
8668 			void *indr_priv)
8669 {
8670 	struct ice_indr_block_priv *priv = indr_priv;
8671 	struct ice_netdev_priv *np = priv->np;
8672 
8673 	switch (type) {
8674 	case TC_SETUP_CLSFLOWER:
8675 		return ice_setup_tc_cls_flower(np, priv->netdev,
8676 					       (struct flow_cls_offload *)
8677 					       type_data);
8678 	default:
8679 		return -EOPNOTSUPP;
8680 	}
8681 }
8682 
8683 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))8684 ice_indr_setup_tc_block(struct net_device *netdev, struct Qdisc *sch,
8685 			struct ice_netdev_priv *np,
8686 			struct flow_block_offload *f, void *data,
8687 			void (*cleanup)(struct flow_block_cb *block_cb))
8688 {
8689 	struct ice_indr_block_priv *indr_priv;
8690 	struct flow_block_cb *block_cb;
8691 
8692 	if (!ice_is_tunnel_supported(netdev) &&
8693 	    !(is_vlan_dev(netdev) &&
8694 	      vlan_dev_real_dev(netdev) == np->vsi->netdev))
8695 		return -EOPNOTSUPP;
8696 
8697 	if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
8698 		return -EOPNOTSUPP;
8699 
8700 	switch (f->command) {
8701 	case FLOW_BLOCK_BIND:
8702 		indr_priv = ice_indr_block_priv_lookup(np, netdev);
8703 		if (indr_priv)
8704 			return -EEXIST;
8705 
8706 		indr_priv = kzalloc(sizeof(*indr_priv), GFP_KERNEL);
8707 		if (!indr_priv)
8708 			return -ENOMEM;
8709 
8710 		indr_priv->netdev = netdev;
8711 		indr_priv->np = np;
8712 		list_add(&indr_priv->list, &np->tc_indr_block_priv_list);
8713 
8714 		block_cb =
8715 			flow_indr_block_cb_alloc(ice_indr_setup_block_cb,
8716 						 indr_priv, indr_priv,
8717 						 ice_rep_indr_tc_block_unbind,
8718 						 f, netdev, sch, data, np,
8719 						 cleanup);
8720 
8721 		if (IS_ERR(block_cb)) {
8722 			list_del(&indr_priv->list);
8723 			kfree(indr_priv);
8724 			return PTR_ERR(block_cb);
8725 		}
8726 		flow_block_cb_add(block_cb, f);
8727 		list_add_tail(&block_cb->driver_list, &ice_block_cb_list);
8728 		break;
8729 	case FLOW_BLOCK_UNBIND:
8730 		indr_priv = ice_indr_block_priv_lookup(np, netdev);
8731 		if (!indr_priv)
8732 			return -ENOENT;
8733 
8734 		block_cb = flow_block_cb_lookup(f->block,
8735 						ice_indr_setup_block_cb,
8736 						indr_priv);
8737 		if (!block_cb)
8738 			return -ENOENT;
8739 
8740 		flow_indr_block_cb_remove(block_cb, f);
8741 
8742 		list_del(&block_cb->driver_list);
8743 		break;
8744 	default:
8745 		return -EOPNOTSUPP;
8746 	}
8747 	return 0;
8748 }
8749 
8750 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))8751 ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
8752 		     void *cb_priv, enum tc_setup_type type, void *type_data,
8753 		     void *data,
8754 		     void (*cleanup)(struct flow_block_cb *block_cb))
8755 {
8756 	switch (type) {
8757 	case TC_SETUP_BLOCK:
8758 		return ice_indr_setup_tc_block(netdev, sch, cb_priv, type_data,
8759 					       data, cleanup);
8760 
8761 	default:
8762 		return -EOPNOTSUPP;
8763 	}
8764 }
8765 
8766 /**
8767  * ice_open - Called when a network interface becomes active
8768  * @netdev: network interface device structure
8769  *
8770  * The open entry point is called when a network interface is made
8771  * active by the system (IFF_UP). At this point all resources needed
8772  * for transmit and receive operations are allocated, the interrupt
8773  * handler is registered with the OS, the netdev watchdog is enabled,
8774  * and the stack is notified that the interface is ready.
8775  *
8776  * Returns 0 on success, negative value on failure
8777  */
ice_open(struct net_device * netdev)8778 int ice_open(struct net_device *netdev)
8779 {
8780 	struct ice_netdev_priv *np = netdev_priv(netdev);
8781 	struct ice_pf *pf = np->vsi->back;
8782 
8783 	if (ice_is_reset_in_progress(pf->state)) {
8784 		netdev_err(netdev, "can't open net device while reset is in progress");
8785 		return -EBUSY;
8786 	}
8787 
8788 	return ice_open_internal(netdev);
8789 }
8790 
8791 /**
8792  * ice_open_internal - Called when a network interface becomes active
8793  * @netdev: network interface device structure
8794  *
8795  * Internal ice_open implementation. Should not be used directly except for ice_open and reset
8796  * handling routine
8797  *
8798  * Returns 0 on success, negative value on failure
8799  */
ice_open_internal(struct net_device * netdev)8800 int ice_open_internal(struct net_device *netdev)
8801 {
8802 	struct ice_netdev_priv *np = netdev_priv(netdev);
8803 	struct ice_vsi *vsi = np->vsi;
8804 	struct ice_pf *pf = vsi->back;
8805 	struct ice_port_info *pi;
8806 	int err;
8807 
8808 	if (test_bit(ICE_NEEDS_RESTART, pf->state)) {
8809 		netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
8810 		return -EIO;
8811 	}
8812 
8813 	netif_carrier_off(netdev);
8814 
8815 	pi = vsi->port_info;
8816 	err = ice_update_link_info(pi);
8817 	if (err) {
8818 		netdev_err(netdev, "Failed to get link info, error %d\n", err);
8819 		return err;
8820 	}
8821 
8822 	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
8823 
8824 	/* Set PHY if there is media, otherwise, turn off PHY */
8825 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
8826 		clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
8827 		if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state)) {
8828 			err = ice_init_phy_user_cfg(pi);
8829 			if (err) {
8830 				netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
8831 					   err);
8832 				return err;
8833 			}
8834 		}
8835 
8836 		err = ice_configure_phy(vsi);
8837 		if (err) {
8838 			netdev_err(netdev, "Failed to set physical link up, error %d\n",
8839 				   err);
8840 			return err;
8841 		}
8842 	} else {
8843 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
8844 		ice_set_link(vsi, false);
8845 	}
8846 
8847 	err = ice_vsi_open(vsi);
8848 	if (err)
8849 		netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
8850 			   vsi->vsi_num, vsi->vsw->sw_id);
8851 
8852 	/* Update existing tunnels information */
8853 	udp_tunnel_get_rx_info(netdev);
8854 
8855 	return err;
8856 }
8857 
8858 /**
8859  * ice_stop - Disables a network interface
8860  * @netdev: network interface device structure
8861  *
8862  * The stop entry point is called when an interface is de-activated by the OS,
8863  * and the netdevice enters the DOWN state. The hardware is still under the
8864  * driver's control, but the netdev interface is disabled.
8865  *
8866  * Returns success only - not allowed to fail
8867  */
ice_stop(struct net_device * netdev)8868 int ice_stop(struct net_device *netdev)
8869 {
8870 	struct ice_netdev_priv *np = netdev_priv(netdev);
8871 	struct ice_vsi *vsi = np->vsi;
8872 	struct ice_pf *pf = vsi->back;
8873 
8874 	if (ice_is_reset_in_progress(pf->state)) {
8875 		netdev_err(netdev, "can't stop net device while reset is in progress");
8876 		return -EBUSY;
8877 	}
8878 
8879 	ice_vsi_close(vsi);
8880 
8881 	return 0;
8882 }
8883 
8884 /**
8885  * ice_features_check - Validate encapsulated packet conforms to limits
8886  * @skb: skb buffer
8887  * @netdev: This port's netdev
8888  * @features: Offload features that the stack believes apply
8889  */
8890 static netdev_features_t
ice_features_check(struct sk_buff * skb,struct net_device __always_unused * netdev,netdev_features_t features)8891 ice_features_check(struct sk_buff *skb,
8892 		   struct net_device __always_unused *netdev,
8893 		   netdev_features_t features)
8894 {
8895 	bool gso = skb_is_gso(skb);
8896 	size_t len;
8897 
8898 	/* No point in doing any of this if neither checksum nor GSO are
8899 	 * being requested for this frame. We can rule out both by just
8900 	 * checking for CHECKSUM_PARTIAL
8901 	 */
8902 	if (skb->ip_summed != CHECKSUM_PARTIAL)
8903 		return features;
8904 
8905 	/* We cannot support GSO if the MSS is going to be less than
8906 	 * 64 bytes. If it is then we need to drop support for GSO.
8907 	 */
8908 	if (gso && (skb_shinfo(skb)->gso_size < ICE_TXD_CTX_MIN_MSS))
8909 		features &= ~NETIF_F_GSO_MASK;
8910 
8911 	len = skb_network_offset(skb);
8912 	if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
8913 		goto out_rm_features;
8914 
8915 	len = skb_network_header_len(skb);
8916 	if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
8917 		goto out_rm_features;
8918 
8919 	if (skb->encapsulation) {
8920 		/* this must work for VXLAN frames AND IPIP/SIT frames, and in
8921 		 * the case of IPIP frames, the transport header pointer is
8922 		 * after the inner header! So check to make sure that this
8923 		 * is a GRE or UDP_TUNNEL frame before doing that math.
8924 		 */
8925 		if (gso && (skb_shinfo(skb)->gso_type &
8926 			    (SKB_GSO_GRE | SKB_GSO_UDP_TUNNEL))) {
8927 			len = skb_inner_network_header(skb) -
8928 			      skb_transport_header(skb);
8929 			if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
8930 				goto out_rm_features;
8931 		}
8932 
8933 		len = skb_inner_network_header_len(skb);
8934 		if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
8935 			goto out_rm_features;
8936 	}
8937 
8938 	return features;
8939 out_rm_features:
8940 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
8941 }
8942 
8943 static const struct net_device_ops ice_netdev_safe_mode_ops = {
8944 	.ndo_open = ice_open,
8945 	.ndo_stop = ice_stop,
8946 	.ndo_start_xmit = ice_start_xmit,
8947 	.ndo_set_mac_address = ice_set_mac_address,
8948 	.ndo_validate_addr = eth_validate_addr,
8949 	.ndo_change_mtu = ice_change_mtu,
8950 	.ndo_get_stats64 = ice_get_stats64,
8951 	.ndo_tx_timeout = ice_tx_timeout,
8952 	.ndo_bpf = ice_xdp_safe_mode,
8953 };
8954 
8955 static const struct net_device_ops ice_netdev_ops = {
8956 	.ndo_open = ice_open,
8957 	.ndo_stop = ice_stop,
8958 	.ndo_start_xmit = ice_start_xmit,
8959 	.ndo_select_queue = ice_select_queue,
8960 	.ndo_features_check = ice_features_check,
8961 	.ndo_fix_features = ice_fix_features,
8962 	.ndo_set_rx_mode = ice_set_rx_mode,
8963 	.ndo_set_mac_address = ice_set_mac_address,
8964 	.ndo_validate_addr = eth_validate_addr,
8965 	.ndo_change_mtu = ice_change_mtu,
8966 	.ndo_get_stats64 = ice_get_stats64,
8967 	.ndo_set_tx_maxrate = ice_set_tx_maxrate,
8968 	.ndo_eth_ioctl = ice_eth_ioctl,
8969 	.ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
8970 	.ndo_set_vf_mac = ice_set_vf_mac,
8971 	.ndo_get_vf_config = ice_get_vf_cfg,
8972 	.ndo_set_vf_trust = ice_set_vf_trust,
8973 	.ndo_set_vf_vlan = ice_set_vf_port_vlan,
8974 	.ndo_set_vf_link_state = ice_set_vf_link_state,
8975 	.ndo_get_vf_stats = ice_get_vf_stats,
8976 	.ndo_set_vf_rate = ice_set_vf_bw,
8977 	.ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
8978 	.ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
8979 	.ndo_setup_tc = ice_setup_tc,
8980 	.ndo_set_features = ice_set_features,
8981 	.ndo_bridge_getlink = ice_bridge_getlink,
8982 	.ndo_bridge_setlink = ice_bridge_setlink,
8983 	.ndo_fdb_add = ice_fdb_add,
8984 	.ndo_fdb_del = ice_fdb_del,
8985 #ifdef CONFIG_RFS_ACCEL
8986 	.ndo_rx_flow_steer = ice_rx_flow_steer,
8987 #endif
8988 	.ndo_tx_timeout = ice_tx_timeout,
8989 	.ndo_bpf = ice_xdp,
8990 	.ndo_xdp_xmit = ice_xdp_xmit,
8991 	.ndo_xsk_wakeup = ice_xsk_wakeup,
8992 	.ndo_get_devlink_port = ice_get_devlink_port,
8993 };
8994