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