• 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 "ice.h"
10 #include "ice_base.h"
11 #include "ice_lib.h"
12 #include "ice_fltr.h"
13 #include "ice_dcb_lib.h"
14 #include "ice_dcb_nl.h"
15 #include "ice_devlink.h"
16 
17 #define DRV_SUMMARY	"Intel(R) Ethernet Connection E800 Series Linux Driver"
18 static const char ice_driver_string[] = DRV_SUMMARY;
19 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
20 
21 /* DDP Package file located in firmware search paths (e.g. /lib/firmware/) */
22 #define ICE_DDP_PKG_PATH	"intel/ice/ddp/"
23 #define ICE_DDP_PKG_FILE	ICE_DDP_PKG_PATH "ice.pkg"
24 
25 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
26 MODULE_DESCRIPTION(DRV_SUMMARY);
27 MODULE_LICENSE("GPL v2");
28 MODULE_FIRMWARE(ICE_DDP_PKG_FILE);
29 
30 static int debug = -1;
31 module_param(debug, int, 0644);
32 #ifndef CONFIG_DYNAMIC_DEBUG
33 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
34 #else
35 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
36 #endif /* !CONFIG_DYNAMIC_DEBUG */
37 
38 static struct workqueue_struct *ice_wq;
39 static const struct net_device_ops ice_netdev_safe_mode_ops;
40 static const struct net_device_ops ice_netdev_ops;
41 static int ice_vsi_open(struct ice_vsi *vsi);
42 
43 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type);
44 
45 static void ice_vsi_release_all(struct ice_pf *pf);
46 
47 /**
48  * ice_get_tx_pending - returns number of Tx descriptors not processed
49  * @ring: the ring of descriptors
50  */
ice_get_tx_pending(struct ice_ring * ring)51 static u16 ice_get_tx_pending(struct ice_ring *ring)
52 {
53 	u16 head, tail;
54 
55 	head = ring->next_to_clean;
56 	tail = ring->next_to_use;
57 
58 	if (head != tail)
59 		return (head < tail) ?
60 			tail - head : (tail + ring->count - head);
61 	return 0;
62 }
63 
64 /**
65  * ice_check_for_hang_subtask - check for and recover hung queues
66  * @pf: pointer to PF struct
67  */
ice_check_for_hang_subtask(struct ice_pf * pf)68 static void ice_check_for_hang_subtask(struct ice_pf *pf)
69 {
70 	struct ice_vsi *vsi = NULL;
71 	struct ice_hw *hw;
72 	unsigned int i;
73 	int packets;
74 	u32 v;
75 
76 	ice_for_each_vsi(pf, v)
77 		if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
78 			vsi = pf->vsi[v];
79 			break;
80 		}
81 
82 	if (!vsi || test_bit(__ICE_DOWN, vsi->state))
83 		return;
84 
85 	if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
86 		return;
87 
88 	hw = &vsi->back->hw;
89 
90 	for (i = 0; i < vsi->num_txq; i++) {
91 		struct ice_ring *tx_ring = vsi->tx_rings[i];
92 
93 		if (tx_ring && tx_ring->desc) {
94 			/* If packet counter has not changed the queue is
95 			 * likely stalled, so force an interrupt for this
96 			 * queue.
97 			 *
98 			 * prev_pkt would be negative if there was no
99 			 * pending work.
100 			 */
101 			packets = tx_ring->stats.pkts & INT_MAX;
102 			if (tx_ring->tx_stats.prev_pkt == packets) {
103 				/* Trigger sw interrupt to revive the queue */
104 				ice_trigger_sw_intr(hw, tx_ring->q_vector);
105 				continue;
106 			}
107 
108 			/* Memory barrier between read of packet count and call
109 			 * to ice_get_tx_pending()
110 			 */
111 			smp_rmb();
112 			tx_ring->tx_stats.prev_pkt =
113 			    ice_get_tx_pending(tx_ring) ? packets : -1;
114 		}
115 	}
116 }
117 
118 /**
119  * ice_init_mac_fltr - Set initial MAC filters
120  * @pf: board private structure
121  *
122  * Set initial set of MAC filters for PF VSI; configure filters for permanent
123  * address and broadcast address. If an error is encountered, netdevice will be
124  * unregistered.
125  */
ice_init_mac_fltr(struct ice_pf * pf)126 static int ice_init_mac_fltr(struct ice_pf *pf)
127 {
128 	enum ice_status status;
129 	struct ice_vsi *vsi;
130 	u8 *perm_addr;
131 
132 	vsi = ice_get_main_vsi(pf);
133 	if (!vsi)
134 		return -EINVAL;
135 
136 	perm_addr = vsi->port_info->mac.perm_addr;
137 	status = ice_fltr_add_mac_and_broadcast(vsi, perm_addr, ICE_FWD_TO_VSI);
138 	if (!status)
139 		return 0;
140 
141 	/* We aren't useful with no MAC filters, so unregister if we
142 	 * had an error
143 	 */
144 	if (vsi->netdev->reg_state == NETREG_REGISTERED) {
145 		dev_err(ice_pf_to_dev(pf), "Could not add MAC filters error %s. Unregistering device\n",
146 			ice_stat_str(status));
147 		unregister_netdev(vsi->netdev);
148 		free_netdev(vsi->netdev);
149 		vsi->netdev = NULL;
150 	}
151 
152 	return -EIO;
153 }
154 
155 /**
156  * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
157  * @netdev: the net device on which the sync is happening
158  * @addr: MAC address to sync
159  *
160  * This is a callback function which is called by the in kernel device sync
161  * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
162  * populates the tmp_sync_list, which is later used by ice_add_mac to add the
163  * MAC filters from the hardware.
164  */
ice_add_mac_to_sync_list(struct net_device * netdev,const u8 * addr)165 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
166 {
167 	struct ice_netdev_priv *np = netdev_priv(netdev);
168 	struct ice_vsi *vsi = np->vsi;
169 
170 	if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr,
171 				     ICE_FWD_TO_VSI))
172 		return -EINVAL;
173 
174 	return 0;
175 }
176 
177 /**
178  * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
179  * @netdev: the net device on which the unsync is happening
180  * @addr: MAC address to unsync
181  *
182  * This is a callback function which is called by the in kernel device unsync
183  * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
184  * populates the tmp_unsync_list, which is later used by ice_remove_mac to
185  * delete the MAC filters from the hardware.
186  */
ice_add_mac_to_unsync_list(struct net_device * netdev,const u8 * addr)187 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
188 {
189 	struct ice_netdev_priv *np = netdev_priv(netdev);
190 	struct ice_vsi *vsi = np->vsi;
191 
192 	/* Under some circumstances, we might receive a request to delete our
193 	 * own device address from our uc list. Because we store the device
194 	 * address in the VSI's MAC filter list, we need to ignore such
195 	 * requests and not delete our device address from this list.
196 	 */
197 	if (ether_addr_equal(addr, netdev->dev_addr))
198 		return 0;
199 
200 	if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr,
201 				     ICE_FWD_TO_VSI))
202 		return -EINVAL;
203 
204 	return 0;
205 }
206 
207 /**
208  * ice_vsi_fltr_changed - check if filter state changed
209  * @vsi: VSI to be checked
210  *
211  * returns true if filter state has changed, false otherwise.
212  */
ice_vsi_fltr_changed(struct ice_vsi * vsi)213 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
214 {
215 	return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
216 	       test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
217 	       test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
218 }
219 
220 /**
221  * ice_cfg_promisc - Enable or disable promiscuous mode for a given PF
222  * @vsi: the VSI being configured
223  * @promisc_m: mask of promiscuous config bits
224  * @set_promisc: enable or disable promisc flag request
225  *
226  */
ice_cfg_promisc(struct ice_vsi * vsi,u8 promisc_m,bool set_promisc)227 static int ice_cfg_promisc(struct ice_vsi *vsi, u8 promisc_m, bool set_promisc)
228 {
229 	struct ice_hw *hw = &vsi->back->hw;
230 	enum ice_status status = 0;
231 
232 	if (vsi->type != ICE_VSI_PF)
233 		return 0;
234 
235 	if (vsi->vlan_ena) {
236 		status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m,
237 						  set_promisc);
238 	} else {
239 		if (set_promisc)
240 			status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m,
241 						     0);
242 		else
243 			status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m,
244 						       0);
245 	}
246 
247 	if (status)
248 		return -EIO;
249 
250 	return 0;
251 }
252 
253 /**
254  * ice_vsi_sync_fltr - Update the VSI filter list to the HW
255  * @vsi: ptr to the VSI
256  *
257  * Push any outstanding VSI filter changes through the AdminQ.
258  */
ice_vsi_sync_fltr(struct ice_vsi * vsi)259 static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
260 {
261 	struct device *dev = ice_pf_to_dev(vsi->back);
262 	struct net_device *netdev = vsi->netdev;
263 	bool promisc_forced_on = false;
264 	struct ice_pf *pf = vsi->back;
265 	struct ice_hw *hw = &pf->hw;
266 	enum ice_status status = 0;
267 	u32 changed_flags = 0;
268 	u8 promisc_m;
269 	int err = 0;
270 
271 	if (!vsi->netdev)
272 		return -EINVAL;
273 
274 	while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
275 		usleep_range(1000, 2000);
276 
277 	changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
278 	vsi->current_netdev_flags = vsi->netdev->flags;
279 
280 	INIT_LIST_HEAD(&vsi->tmp_sync_list);
281 	INIT_LIST_HEAD(&vsi->tmp_unsync_list);
282 
283 	if (ice_vsi_fltr_changed(vsi)) {
284 		clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
285 		clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
286 		clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
287 
288 		/* grab the netdev's addr_list_lock */
289 		netif_addr_lock_bh(netdev);
290 		__dev_uc_sync(netdev, ice_add_mac_to_sync_list,
291 			      ice_add_mac_to_unsync_list);
292 		__dev_mc_sync(netdev, ice_add_mac_to_sync_list,
293 			      ice_add_mac_to_unsync_list);
294 		/* our temp lists are populated. release lock */
295 		netif_addr_unlock_bh(netdev);
296 	}
297 
298 	/* Remove MAC addresses in the unsync list */
299 	status = ice_fltr_remove_mac_list(vsi, &vsi->tmp_unsync_list);
300 	ice_fltr_free_list(dev, &vsi->tmp_unsync_list);
301 	if (status) {
302 		netdev_err(netdev, "Failed to delete MAC filters\n");
303 		/* if we failed because of alloc failures, just bail */
304 		if (status == ICE_ERR_NO_MEMORY) {
305 			err = -ENOMEM;
306 			goto out;
307 		}
308 	}
309 
310 	/* Add MAC addresses in the sync list */
311 	status = ice_fltr_add_mac_list(vsi, &vsi->tmp_sync_list);
312 	ice_fltr_free_list(dev, &vsi->tmp_sync_list);
313 	/* If filter is added successfully or already exists, do not go into
314 	 * 'if' condition and report it as error. Instead continue processing
315 	 * rest of the function.
316 	 */
317 	if (status && status != ICE_ERR_ALREADY_EXISTS) {
318 		netdev_err(netdev, "Failed to add MAC filters\n");
319 		/* If there is no more space for new umac filters, VSI
320 		 * should go into promiscuous mode. There should be some
321 		 * space reserved for promiscuous filters.
322 		 */
323 		if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
324 		    !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
325 				      vsi->state)) {
326 			promisc_forced_on = true;
327 			netdev_warn(netdev, "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
328 				    vsi->vsi_num);
329 		} else {
330 			err = -EIO;
331 			goto out;
332 		}
333 	}
334 	/* check for changes in promiscuous modes */
335 	if (changed_flags & IFF_ALLMULTI) {
336 		if (vsi->current_netdev_flags & IFF_ALLMULTI) {
337 			if (vsi->vlan_ena)
338 				promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
339 			else
340 				promisc_m = ICE_MCAST_PROMISC_BITS;
341 
342 			err = ice_cfg_promisc(vsi, promisc_m, true);
343 			if (err) {
344 				netdev_err(netdev, "Error setting Multicast promiscuous mode on VSI %i\n",
345 					   vsi->vsi_num);
346 				vsi->current_netdev_flags &= ~IFF_ALLMULTI;
347 				goto out_promisc;
348 			}
349 		} else {
350 			/* !(vsi->current_netdev_flags & IFF_ALLMULTI) */
351 			if (vsi->vlan_ena)
352 				promisc_m = ICE_MCAST_VLAN_PROMISC_BITS;
353 			else
354 				promisc_m = ICE_MCAST_PROMISC_BITS;
355 
356 			err = ice_cfg_promisc(vsi, promisc_m, false);
357 			if (err) {
358 				netdev_err(netdev, "Error clearing Multicast promiscuous mode on VSI %i\n",
359 					   vsi->vsi_num);
360 				vsi->current_netdev_flags |= IFF_ALLMULTI;
361 				goto out_promisc;
362 			}
363 		}
364 	}
365 
366 	if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
367 	    test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
368 		clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
369 		if (vsi->current_netdev_flags & IFF_PROMISC) {
370 			/* Apply Rx filter rule to get traffic from wire */
371 			if (!ice_is_dflt_vsi_in_use(pf->first_sw)) {
372 				err = ice_set_dflt_vsi(pf->first_sw, vsi);
373 				if (err && err != -EEXIST) {
374 					netdev_err(netdev, "Error %d setting default VSI %i Rx rule\n",
375 						   err, vsi->vsi_num);
376 					vsi->current_netdev_flags &=
377 						~IFF_PROMISC;
378 					goto out_promisc;
379 				}
380 				ice_cfg_vlan_pruning(vsi, false, false);
381 			}
382 		} else {
383 			/* Clear Rx filter to remove traffic from wire */
384 			if (ice_is_vsi_dflt_vsi(pf->first_sw, vsi)) {
385 				err = ice_clear_dflt_vsi(pf->first_sw);
386 				if (err) {
387 					netdev_err(netdev, "Error %d clearing default VSI %i Rx rule\n",
388 						   err, vsi->vsi_num);
389 					vsi->current_netdev_flags |=
390 						IFF_PROMISC;
391 					goto out_promisc;
392 				}
393 				if (vsi->num_vlan > 1)
394 					ice_cfg_vlan_pruning(vsi, true, false);
395 			}
396 		}
397 	}
398 	goto exit;
399 
400 out_promisc:
401 	set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
402 	goto exit;
403 out:
404 	/* if something went wrong then set the changed flag so we try again */
405 	set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
406 	set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
407 exit:
408 	clear_bit(__ICE_CFG_BUSY, vsi->state);
409 	return err;
410 }
411 
412 /**
413  * ice_sync_fltr_subtask - Sync the VSI filter list with HW
414  * @pf: board private structure
415  */
ice_sync_fltr_subtask(struct ice_pf * pf)416 static void ice_sync_fltr_subtask(struct ice_pf *pf)
417 {
418 	int v;
419 
420 	if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
421 		return;
422 
423 	clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
424 
425 	ice_for_each_vsi(pf, v)
426 		if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
427 		    ice_vsi_sync_fltr(pf->vsi[v])) {
428 			/* come back and try again later */
429 			set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
430 			break;
431 		}
432 }
433 
434 /**
435  * ice_pf_dis_all_vsi - Pause all VSIs on a PF
436  * @pf: the PF
437  * @locked: is the rtnl_lock already held
438  */
ice_pf_dis_all_vsi(struct ice_pf * pf,bool locked)439 static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
440 {
441 	int v;
442 
443 	ice_for_each_vsi(pf, v)
444 		if (pf->vsi[v])
445 			ice_dis_vsi(pf->vsi[v], locked);
446 }
447 
448 /**
449  * ice_prepare_for_reset - prep for the core to reset
450  * @pf: board private structure
451  *
452  * Inform or close all dependent features in prep for reset.
453  */
454 static void
ice_prepare_for_reset(struct ice_pf * pf)455 ice_prepare_for_reset(struct ice_pf *pf)
456 {
457 	struct ice_hw *hw = &pf->hw;
458 	unsigned int i;
459 
460 	/* already prepared for reset */
461 	if (test_bit(__ICE_PREPARED_FOR_RESET, pf->state))
462 		return;
463 
464 	/* Notify VFs of impending reset */
465 	if (ice_check_sq_alive(hw, &hw->mailboxq))
466 		ice_vc_notify_reset(pf);
467 
468 	/* Disable VFs until reset is completed */
469 	ice_for_each_vf(pf, i)
470 		ice_set_vf_state_qs_dis(&pf->vf[i]);
471 
472 	/* clear SW filtering DB */
473 	ice_clear_hw_tbls(hw);
474 	/* disable the VSIs and their queues that are not already DOWN */
475 	ice_pf_dis_all_vsi(pf, false);
476 
477 	if (hw->port_info)
478 		ice_sched_clear_port(hw->port_info);
479 
480 	ice_shutdown_all_ctrlq(hw);
481 
482 	set_bit(__ICE_PREPARED_FOR_RESET, pf->state);
483 }
484 
485 /**
486  * ice_do_reset - Initiate one of many types of resets
487  * @pf: board private structure
488  * @reset_type: reset type requested
489  * before this function was called.
490  */
ice_do_reset(struct ice_pf * pf,enum ice_reset_req reset_type)491 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
492 {
493 	struct device *dev = ice_pf_to_dev(pf);
494 	struct ice_hw *hw = &pf->hw;
495 
496 	dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
497 
498 	ice_prepare_for_reset(pf);
499 
500 	/* trigger the reset */
501 	if (ice_reset(hw, reset_type)) {
502 		dev_err(dev, "reset %d failed\n", reset_type);
503 		set_bit(__ICE_RESET_FAILED, pf->state);
504 		clear_bit(__ICE_RESET_OICR_RECV, pf->state);
505 		clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
506 		clear_bit(__ICE_PFR_REQ, pf->state);
507 		clear_bit(__ICE_CORER_REQ, pf->state);
508 		clear_bit(__ICE_GLOBR_REQ, pf->state);
509 		return;
510 	}
511 
512 	/* PFR is a bit of a special case because it doesn't result in an OICR
513 	 * interrupt. So for PFR, rebuild after the reset and clear the reset-
514 	 * associated state bits.
515 	 */
516 	if (reset_type == ICE_RESET_PFR) {
517 		pf->pfr_count++;
518 		ice_rebuild(pf, reset_type);
519 		clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
520 		clear_bit(__ICE_PFR_REQ, pf->state);
521 		ice_reset_all_vfs(pf, true);
522 	}
523 }
524 
525 /**
526  * ice_reset_subtask - Set up for resetting the device and driver
527  * @pf: board private structure
528  */
ice_reset_subtask(struct ice_pf * pf)529 static void ice_reset_subtask(struct ice_pf *pf)
530 {
531 	enum ice_reset_req reset_type = ICE_RESET_INVAL;
532 
533 	/* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
534 	 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
535 	 * of reset is pending and sets bits in pf->state indicating the reset
536 	 * type and __ICE_RESET_OICR_RECV. So, if the latter bit is set
537 	 * prepare for pending reset if not already (for PF software-initiated
538 	 * global resets the software should already be prepared for it as
539 	 * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated
540 	 * by firmware or software on other PFs, that bit is not set so prepare
541 	 * for the reset now), poll for reset done, rebuild and return.
542 	 */
543 	if (test_bit(__ICE_RESET_OICR_RECV, pf->state)) {
544 		/* Perform the largest reset requested */
545 		if (test_and_clear_bit(__ICE_CORER_RECV, pf->state))
546 			reset_type = ICE_RESET_CORER;
547 		if (test_and_clear_bit(__ICE_GLOBR_RECV, pf->state))
548 			reset_type = ICE_RESET_GLOBR;
549 		if (test_and_clear_bit(__ICE_EMPR_RECV, pf->state))
550 			reset_type = ICE_RESET_EMPR;
551 		/* return if no valid reset type requested */
552 		if (reset_type == ICE_RESET_INVAL)
553 			return;
554 		ice_prepare_for_reset(pf);
555 
556 		/* make sure we are ready to rebuild */
557 		if (ice_check_reset(&pf->hw)) {
558 			set_bit(__ICE_RESET_FAILED, pf->state);
559 		} else {
560 			/* done with reset. start rebuild */
561 			pf->hw.reset_ongoing = false;
562 			ice_rebuild(pf, reset_type);
563 			/* clear bit to resume normal operations, but
564 			 * ICE_NEEDS_RESTART bit is set in case rebuild failed
565 			 */
566 			clear_bit(__ICE_RESET_OICR_RECV, pf->state);
567 			clear_bit(__ICE_PREPARED_FOR_RESET, pf->state);
568 			clear_bit(__ICE_PFR_REQ, pf->state);
569 			clear_bit(__ICE_CORER_REQ, pf->state);
570 			clear_bit(__ICE_GLOBR_REQ, pf->state);
571 			ice_reset_all_vfs(pf, true);
572 		}
573 
574 		return;
575 	}
576 
577 	/* No pending resets to finish processing. Check for new resets */
578 	if (test_bit(__ICE_PFR_REQ, pf->state))
579 		reset_type = ICE_RESET_PFR;
580 	if (test_bit(__ICE_CORER_REQ, pf->state))
581 		reset_type = ICE_RESET_CORER;
582 	if (test_bit(__ICE_GLOBR_REQ, pf->state))
583 		reset_type = ICE_RESET_GLOBR;
584 	/* If no valid reset type requested just return */
585 	if (reset_type == ICE_RESET_INVAL)
586 		return;
587 
588 	/* reset if not already down or busy */
589 	if (!test_bit(__ICE_DOWN, pf->state) &&
590 	    !test_bit(__ICE_CFG_BUSY, pf->state)) {
591 		ice_do_reset(pf, reset_type);
592 	}
593 }
594 
595 /**
596  * ice_print_topo_conflict - print topology conflict message
597  * @vsi: the VSI whose topology status is being checked
598  */
ice_print_topo_conflict(struct ice_vsi * vsi)599 static void ice_print_topo_conflict(struct ice_vsi *vsi)
600 {
601 	switch (vsi->port_info->phy.link_info.topo_media_conflict) {
602 	case ICE_AQ_LINK_TOPO_CONFLICT:
603 	case ICE_AQ_LINK_MEDIA_CONFLICT:
604 	case ICE_AQ_LINK_TOPO_UNREACH_PRT:
605 	case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT:
606 	case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA:
607 		netdev_info(vsi->netdev, "Possible mis-configuration of the Ethernet port detected, please use the Intel(R) Ethernet Port Configuration Tool application to address the issue.\n");
608 		break;
609 	case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA:
610 		netdev_info(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");
611 		break;
612 	default:
613 		break;
614 	}
615 }
616 
617 /**
618  * ice_print_link_msg - print link up or down message
619  * @vsi: the VSI whose link status is being queried
620  * @isup: boolean for if the link is now up or down
621  */
ice_print_link_msg(struct ice_vsi * vsi,bool isup)622 void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
623 {
624 	struct ice_aqc_get_phy_caps_data *caps;
625 	const char *an_advertised;
626 	enum ice_status status;
627 	const char *fec_req;
628 	const char *speed;
629 	const char *fec;
630 	const char *fc;
631 	const char *an;
632 
633 	if (!vsi)
634 		return;
635 
636 	if (vsi->current_isup == isup)
637 		return;
638 
639 	vsi->current_isup = isup;
640 
641 	if (!isup) {
642 		netdev_info(vsi->netdev, "NIC Link is Down\n");
643 		return;
644 	}
645 
646 	switch (vsi->port_info->phy.link_info.link_speed) {
647 	case ICE_AQ_LINK_SPEED_100GB:
648 		speed = "100 G";
649 		break;
650 	case ICE_AQ_LINK_SPEED_50GB:
651 		speed = "50 G";
652 		break;
653 	case ICE_AQ_LINK_SPEED_40GB:
654 		speed = "40 G";
655 		break;
656 	case ICE_AQ_LINK_SPEED_25GB:
657 		speed = "25 G";
658 		break;
659 	case ICE_AQ_LINK_SPEED_20GB:
660 		speed = "20 G";
661 		break;
662 	case ICE_AQ_LINK_SPEED_10GB:
663 		speed = "10 G";
664 		break;
665 	case ICE_AQ_LINK_SPEED_5GB:
666 		speed = "5 G";
667 		break;
668 	case ICE_AQ_LINK_SPEED_2500MB:
669 		speed = "2.5 G";
670 		break;
671 	case ICE_AQ_LINK_SPEED_1000MB:
672 		speed = "1 G";
673 		break;
674 	case ICE_AQ_LINK_SPEED_100MB:
675 		speed = "100 M";
676 		break;
677 	default:
678 		speed = "Unknown";
679 		break;
680 	}
681 
682 	switch (vsi->port_info->fc.current_mode) {
683 	case ICE_FC_FULL:
684 		fc = "Rx/Tx";
685 		break;
686 	case ICE_FC_TX_PAUSE:
687 		fc = "Tx";
688 		break;
689 	case ICE_FC_RX_PAUSE:
690 		fc = "Rx";
691 		break;
692 	case ICE_FC_NONE:
693 		fc = "None";
694 		break;
695 	default:
696 		fc = "Unknown";
697 		break;
698 	}
699 
700 	/* Get FEC mode based on negotiated link info */
701 	switch (vsi->port_info->phy.link_info.fec_info) {
702 	case ICE_AQ_LINK_25G_RS_528_FEC_EN:
703 	case ICE_AQ_LINK_25G_RS_544_FEC_EN:
704 		fec = "RS-FEC";
705 		break;
706 	case ICE_AQ_LINK_25G_KR_FEC_EN:
707 		fec = "FC-FEC/BASE-R";
708 		break;
709 	default:
710 		fec = "NONE";
711 		break;
712 	}
713 
714 	/* check if autoneg completed, might be false due to not supported */
715 	if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
716 		an = "True";
717 	else
718 		an = "False";
719 
720 	/* Get FEC mode requested based on PHY caps last SW configuration */
721 	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
722 	if (!caps) {
723 		fec_req = "Unknown";
724 		an_advertised = "Unknown";
725 		goto done;
726 	}
727 
728 	status = ice_aq_get_phy_caps(vsi->port_info, false,
729 				     ICE_AQC_REPORT_SW_CFG, caps, NULL);
730 	if (status)
731 		netdev_info(vsi->netdev, "Get phy capability failed.\n");
732 
733 	an_advertised = ice_is_phy_caps_an_enabled(caps) ? "On" : "Off";
734 
735 	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
736 	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
737 		fec_req = "RS-FEC";
738 	else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
739 		 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
740 		fec_req = "FC-FEC/BASE-R";
741 	else
742 		fec_req = "NONE";
743 
744 	kfree(caps);
745 
746 done:
747 	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",
748 		    speed, fec_req, fec, an_advertised, an, fc);
749 	ice_print_topo_conflict(vsi);
750 }
751 
752 /**
753  * ice_vsi_link_event - update the VSI's netdev
754  * @vsi: the VSI on which the link event occurred
755  * @link_up: whether or not the VSI needs to be set up or down
756  */
ice_vsi_link_event(struct ice_vsi * vsi,bool link_up)757 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
758 {
759 	if (!vsi)
760 		return;
761 
762 	if (test_bit(__ICE_DOWN, vsi->state) || !vsi->netdev)
763 		return;
764 
765 	if (vsi->type == ICE_VSI_PF) {
766 		if (link_up == netif_carrier_ok(vsi->netdev))
767 			return;
768 
769 		if (link_up) {
770 			netif_carrier_on(vsi->netdev);
771 			netif_tx_wake_all_queues(vsi->netdev);
772 		} else {
773 			netif_carrier_off(vsi->netdev);
774 			netif_tx_stop_all_queues(vsi->netdev);
775 		}
776 	}
777 }
778 
779 /**
780  * ice_set_dflt_mib - send a default config MIB to the FW
781  * @pf: private PF struct
782  *
783  * This function sends a default configuration MIB to the FW.
784  *
785  * If this function errors out at any point, the driver is still able to
786  * function.  The main impact is that LFC may not operate as expected.
787  * Therefore an error state in this function should be treated with a DBG
788  * message and continue on with driver rebuild/reenable.
789  */
ice_set_dflt_mib(struct ice_pf * pf)790 static void ice_set_dflt_mib(struct ice_pf *pf)
791 {
792 	struct device *dev = ice_pf_to_dev(pf);
793 	u8 mib_type, *buf, *lldpmib = NULL;
794 	u16 len, typelen, offset = 0;
795 	struct ice_lldp_org_tlv *tlv;
796 	struct ice_hw *hw;
797 	u32 ouisubtype;
798 
799 	if (!pf) {
800 		dev_dbg(dev, "%s NULL pf pointer\n", __func__);
801 		return;
802 	}
803 
804 	hw = &pf->hw;
805 	mib_type = SET_LOCAL_MIB_TYPE_LOCAL_MIB;
806 	lldpmib = kzalloc(ICE_LLDPDU_SIZE, GFP_KERNEL);
807 	if (!lldpmib) {
808 		dev_dbg(dev, "%s Failed to allocate MIB memory\n",
809 			__func__);
810 		return;
811 	}
812 
813 	/* Add ETS CFG TLV */
814 	tlv = (struct ice_lldp_org_tlv *)lldpmib;
815 	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
816 		   ICE_IEEE_ETS_TLV_LEN);
817 	tlv->typelen = htons(typelen);
818 	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
819 		      ICE_IEEE_SUBTYPE_ETS_CFG);
820 	tlv->ouisubtype = htonl(ouisubtype);
821 
822 	buf = tlv->tlvinfo;
823 	buf[0] = 0;
824 
825 	/* ETS CFG all UPs map to TC 0. Next 4 (1 - 4) Octets = 0.
826 	 * Octets 5 - 12 are BW values, set octet 5 to 100% BW.
827 	 * Octets 13 - 20 are TSA values - leave as zeros
828 	 */
829 	buf[5] = 0x64;
830 	len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
831 	offset += len + 2;
832 	tlv = (struct ice_lldp_org_tlv *)
833 		((char *)tlv + sizeof(tlv->typelen) + len);
834 
835 	/* Add ETS REC TLV */
836 	buf = tlv->tlvinfo;
837 	tlv->typelen = htons(typelen);
838 
839 	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
840 		      ICE_IEEE_SUBTYPE_ETS_REC);
841 	tlv->ouisubtype = htonl(ouisubtype);
842 
843 	/* First octet of buf is reserved
844 	 * Octets 1 - 4 map UP to TC - all UPs map to zero
845 	 * Octets 5 - 12 are BW values - set TC 0 to 100%.
846 	 * Octets 13 - 20 are TSA value - leave as zeros
847 	 */
848 	buf[5] = 0x64;
849 	offset += len + 2;
850 	tlv = (struct ice_lldp_org_tlv *)
851 		((char *)tlv + sizeof(tlv->typelen) + len);
852 
853 	/* Add PFC CFG TLV */
854 	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
855 		   ICE_IEEE_PFC_TLV_LEN);
856 	tlv->typelen = htons(typelen);
857 
858 	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
859 		      ICE_IEEE_SUBTYPE_PFC_CFG);
860 	tlv->ouisubtype = htonl(ouisubtype);
861 
862 	/* Octet 1 left as all zeros - PFC disabled */
863 	buf[0] = 0x08;
864 	len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
865 	offset += len + 2;
866 
867 	if (ice_aq_set_lldp_mib(hw, mib_type, (void *)lldpmib, offset, NULL))
868 		dev_dbg(dev, "%s Failed to set default LLDP MIB\n", __func__);
869 
870 	kfree(lldpmib);
871 }
872 
873 /**
874  * ice_link_event - process the link event
875  * @pf: PF that the link event is associated with
876  * @pi: port_info for the port that the link event is associated with
877  * @link_up: true if the physical link is up and false if it is down
878  * @link_speed: current link speed received from the link event
879  *
880  * Returns 0 on success and negative on failure
881  */
882 static int
ice_link_event(struct ice_pf * pf,struct ice_port_info * pi,bool link_up,u16 link_speed)883 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
884 	       u16 link_speed)
885 {
886 	struct device *dev = ice_pf_to_dev(pf);
887 	struct ice_phy_info *phy_info;
888 	struct ice_vsi *vsi;
889 	u16 old_link_speed;
890 	bool old_link;
891 	int result;
892 
893 	phy_info = &pi->phy;
894 	phy_info->link_info_old = phy_info->link_info;
895 
896 	old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
897 	old_link_speed = phy_info->link_info_old.link_speed;
898 
899 	/* update the link info structures and re-enable link events,
900 	 * don't bail on failure due to other book keeping needed
901 	 */
902 	result = ice_update_link_info(pi);
903 	if (result)
904 		dev_dbg(dev, "Failed to update link status and re-enable link events for port %d\n",
905 			pi->lport);
906 
907 	/* Check if the link state is up after updating link info, and treat
908 	 * this event as an UP event since the link is actually UP now.
909 	 */
910 	if (phy_info->link_info.link_info & ICE_AQ_LINK_UP)
911 		link_up = true;
912 
913 	vsi = ice_get_main_vsi(pf);
914 	if (!vsi || !vsi->port_info)
915 		return -EINVAL;
916 
917 	/* turn off PHY if media was removed */
918 	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
919 	    !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
920 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
921 
922 		result = ice_aq_set_link_restart_an(pi, false, NULL);
923 		if (result) {
924 			dev_dbg(dev, "Failed to set link down, VSI %d error %d\n",
925 				vsi->vsi_num, result);
926 			return result;
927 		}
928 	}
929 
930 	/* if the old link up/down and speed is the same as the new */
931 	if (link_up == old_link && link_speed == old_link_speed)
932 		return result;
933 
934 	if (ice_is_dcb_active(pf)) {
935 		if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
936 			ice_dcb_rebuild(pf);
937 	} else {
938 		if (link_up)
939 			ice_set_dflt_mib(pf);
940 	}
941 	ice_vsi_link_event(vsi, link_up);
942 	ice_print_link_msg(vsi, link_up);
943 
944 	ice_vc_notify_link_state(pf);
945 
946 	return result;
947 }
948 
949 /**
950  * ice_watchdog_subtask - periodic tasks not using event driven scheduling
951  * @pf: board private structure
952  */
ice_watchdog_subtask(struct ice_pf * pf)953 static void ice_watchdog_subtask(struct ice_pf *pf)
954 {
955 	int i;
956 
957 	/* if interface is down do nothing */
958 	if (test_bit(__ICE_DOWN, pf->state) ||
959 	    test_bit(__ICE_CFG_BUSY, pf->state))
960 		return;
961 
962 	/* make sure we don't do these things too often */
963 	if (time_before(jiffies,
964 			pf->serv_tmr_prev + pf->serv_tmr_period))
965 		return;
966 
967 	pf->serv_tmr_prev = jiffies;
968 
969 	/* Update the stats for active netdevs so the network stack
970 	 * can look at updated numbers whenever it cares to
971 	 */
972 	ice_update_pf_stats(pf);
973 	ice_for_each_vsi(pf, i)
974 		if (pf->vsi[i] && pf->vsi[i]->netdev)
975 			ice_update_vsi_stats(pf->vsi[i]);
976 }
977 
978 /**
979  * ice_init_link_events - enable/initialize link events
980  * @pi: pointer to the port_info instance
981  *
982  * Returns -EIO on failure, 0 on success
983  */
ice_init_link_events(struct ice_port_info * pi)984 static int ice_init_link_events(struct ice_port_info *pi)
985 {
986 	u16 mask;
987 
988 	mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
989 		       ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
990 
991 	if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
992 		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to set link event mask for port %d\n",
993 			pi->lport);
994 		return -EIO;
995 	}
996 
997 	if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
998 		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to enable link events for port %d\n",
999 			pi->lport);
1000 		return -EIO;
1001 	}
1002 
1003 	return 0;
1004 }
1005 
1006 /**
1007  * ice_handle_link_event - handle link event via ARQ
1008  * @pf: PF that the link event is associated with
1009  * @event: event structure containing link status info
1010  */
1011 static int
ice_handle_link_event(struct ice_pf * pf,struct ice_rq_event_info * event)1012 ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1013 {
1014 	struct ice_aqc_get_link_status_data *link_data;
1015 	struct ice_port_info *port_info;
1016 	int status;
1017 
1018 	link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
1019 	port_info = pf->hw.port_info;
1020 	if (!port_info)
1021 		return -EINVAL;
1022 
1023 	status = ice_link_event(pf, port_info,
1024 				!!(link_data->link_info & ICE_AQ_LINK_UP),
1025 				le16_to_cpu(link_data->link_speed));
1026 	if (status)
1027 		dev_dbg(ice_pf_to_dev(pf), "Could not process link event, error %d\n",
1028 			status);
1029 
1030 	return status;
1031 }
1032 
1033 enum ice_aq_task_state {
1034 	ICE_AQ_TASK_WAITING = 0,
1035 	ICE_AQ_TASK_COMPLETE,
1036 	ICE_AQ_TASK_CANCELED,
1037 };
1038 
1039 struct ice_aq_task {
1040 	struct hlist_node entry;
1041 
1042 	u16 opcode;
1043 	struct ice_rq_event_info *event;
1044 	enum ice_aq_task_state state;
1045 };
1046 
1047 /**
1048  * ice_wait_for_aq_event - Wait for an AdminQ event from firmware
1049  * @pf: pointer to the PF private structure
1050  * @opcode: the opcode to wait for
1051  * @timeout: how long to wait, in jiffies
1052  * @event: storage for the event info
1053  *
1054  * Waits for a specific AdminQ completion event on the ARQ for a given PF. The
1055  * current thread will be put to sleep until the specified event occurs or
1056  * until the given timeout is reached.
1057  *
1058  * To obtain only the descriptor contents, pass an event without an allocated
1059  * msg_buf. If the complete data buffer is desired, allocate the
1060  * event->msg_buf with enough space ahead of time.
1061  *
1062  * Returns: zero on success, or a negative error code on failure.
1063  */
ice_aq_wait_for_event(struct ice_pf * pf,u16 opcode,unsigned long timeout,struct ice_rq_event_info * event)1064 int ice_aq_wait_for_event(struct ice_pf *pf, u16 opcode, unsigned long timeout,
1065 			  struct ice_rq_event_info *event)
1066 {
1067 	struct device *dev = ice_pf_to_dev(pf);
1068 	struct ice_aq_task *task;
1069 	unsigned long start;
1070 	long ret;
1071 	int err;
1072 
1073 	task = kzalloc(sizeof(*task), GFP_KERNEL);
1074 	if (!task)
1075 		return -ENOMEM;
1076 
1077 	INIT_HLIST_NODE(&task->entry);
1078 	task->opcode = opcode;
1079 	task->event = event;
1080 	task->state = ICE_AQ_TASK_WAITING;
1081 
1082 	spin_lock_bh(&pf->aq_wait_lock);
1083 	hlist_add_head(&task->entry, &pf->aq_wait_list);
1084 	spin_unlock_bh(&pf->aq_wait_lock);
1085 
1086 	start = jiffies;
1087 
1088 	ret = wait_event_interruptible_timeout(pf->aq_wait_queue, task->state,
1089 					       timeout);
1090 	switch (task->state) {
1091 	case ICE_AQ_TASK_WAITING:
1092 		err = ret < 0 ? ret : -ETIMEDOUT;
1093 		break;
1094 	case ICE_AQ_TASK_CANCELED:
1095 		err = ret < 0 ? ret : -ECANCELED;
1096 		break;
1097 	case ICE_AQ_TASK_COMPLETE:
1098 		err = ret < 0 ? ret : 0;
1099 		break;
1100 	default:
1101 		WARN(1, "Unexpected AdminQ wait task state %u", task->state);
1102 		err = -EINVAL;
1103 		break;
1104 	}
1105 
1106 	dev_dbg(dev, "Waited %u msecs (max %u msecs) for firmware response to op 0x%04x\n",
1107 		jiffies_to_msecs(jiffies - start),
1108 		jiffies_to_msecs(timeout),
1109 		opcode);
1110 
1111 	spin_lock_bh(&pf->aq_wait_lock);
1112 	hlist_del(&task->entry);
1113 	spin_unlock_bh(&pf->aq_wait_lock);
1114 	kfree(task);
1115 
1116 	return err;
1117 }
1118 
1119 /**
1120  * ice_aq_check_events - Check if any thread is waiting for an AdminQ event
1121  * @pf: pointer to the PF private structure
1122  * @opcode: the opcode of the event
1123  * @event: the event to check
1124  *
1125  * Loops over the current list of pending threads waiting for an AdminQ event.
1126  * For each matching task, copy the contents of the event into the task
1127  * structure and wake up the thread.
1128  *
1129  * If multiple threads wait for the same opcode, they will all be woken up.
1130  *
1131  * Note that event->msg_buf will only be duplicated if the event has a buffer
1132  * with enough space already allocated. Otherwise, only the descriptor and
1133  * message length will be copied.
1134  *
1135  * Returns: true if an event was found, false otherwise
1136  */
ice_aq_check_events(struct ice_pf * pf,u16 opcode,struct ice_rq_event_info * event)1137 static void ice_aq_check_events(struct ice_pf *pf, u16 opcode,
1138 				struct ice_rq_event_info *event)
1139 {
1140 	struct ice_aq_task *task;
1141 	bool found = false;
1142 
1143 	spin_lock_bh(&pf->aq_wait_lock);
1144 	hlist_for_each_entry(task, &pf->aq_wait_list, entry) {
1145 		if (task->state || task->opcode != opcode)
1146 			continue;
1147 
1148 		memcpy(&task->event->desc, &event->desc, sizeof(event->desc));
1149 		task->event->msg_len = event->msg_len;
1150 
1151 		/* Only copy the data buffer if a destination was set */
1152 		if (task->event->msg_buf &&
1153 		    task->event->buf_len > event->buf_len) {
1154 			memcpy(task->event->msg_buf, event->msg_buf,
1155 			       event->buf_len);
1156 			task->event->buf_len = event->buf_len;
1157 		}
1158 
1159 		task->state = ICE_AQ_TASK_COMPLETE;
1160 		found = true;
1161 	}
1162 	spin_unlock_bh(&pf->aq_wait_lock);
1163 
1164 	if (found)
1165 		wake_up(&pf->aq_wait_queue);
1166 }
1167 
1168 /**
1169  * ice_aq_cancel_waiting_tasks - Immediately cancel all waiting tasks
1170  * @pf: the PF private structure
1171  *
1172  * Set all waiting tasks to ICE_AQ_TASK_CANCELED, and wake up their threads.
1173  * This will then cause ice_aq_wait_for_event to exit with -ECANCELED.
1174  */
ice_aq_cancel_waiting_tasks(struct ice_pf * pf)1175 static void ice_aq_cancel_waiting_tasks(struct ice_pf *pf)
1176 {
1177 	struct ice_aq_task *task;
1178 
1179 	spin_lock_bh(&pf->aq_wait_lock);
1180 	hlist_for_each_entry(task, &pf->aq_wait_list, entry)
1181 		task->state = ICE_AQ_TASK_CANCELED;
1182 	spin_unlock_bh(&pf->aq_wait_lock);
1183 
1184 	wake_up(&pf->aq_wait_queue);
1185 }
1186 
1187 /**
1188  * __ice_clean_ctrlq - helper function to clean controlq rings
1189  * @pf: ptr to struct ice_pf
1190  * @q_type: specific Control queue type
1191  */
__ice_clean_ctrlq(struct ice_pf * pf,enum ice_ctl_q q_type)1192 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
1193 {
1194 	struct device *dev = ice_pf_to_dev(pf);
1195 	struct ice_rq_event_info event;
1196 	struct ice_hw *hw = &pf->hw;
1197 	struct ice_ctl_q_info *cq;
1198 	u16 pending, i = 0;
1199 	const char *qtype;
1200 	u32 oldval, val;
1201 
1202 	/* Do not clean control queue if/when PF reset fails */
1203 	if (test_bit(__ICE_RESET_FAILED, pf->state))
1204 		return 0;
1205 
1206 	switch (q_type) {
1207 	case ICE_CTL_Q_ADMIN:
1208 		cq = &hw->adminq;
1209 		qtype = "Admin";
1210 		break;
1211 	case ICE_CTL_Q_MAILBOX:
1212 		cq = &hw->mailboxq;
1213 		qtype = "Mailbox";
1214 		break;
1215 	default:
1216 		dev_warn(dev, "Unknown control queue type 0x%x\n", q_type);
1217 		return 0;
1218 	}
1219 
1220 	/* check for error indications - PF_xx_AxQLEN register layout for
1221 	 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
1222 	 */
1223 	val = rd32(hw, cq->rq.len);
1224 	if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1225 		   PF_FW_ARQLEN_ARQCRIT_M)) {
1226 		oldval = val;
1227 		if (val & PF_FW_ARQLEN_ARQVFE_M)
1228 			dev_dbg(dev, "%s Receive Queue VF Error detected\n",
1229 				qtype);
1230 		if (val & PF_FW_ARQLEN_ARQOVFL_M) {
1231 			dev_dbg(dev, "%s Receive Queue Overflow Error detected\n",
1232 				qtype);
1233 		}
1234 		if (val & PF_FW_ARQLEN_ARQCRIT_M)
1235 			dev_dbg(dev, "%s Receive Queue Critical Error detected\n",
1236 				qtype);
1237 		val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1238 			 PF_FW_ARQLEN_ARQCRIT_M);
1239 		if (oldval != val)
1240 			wr32(hw, cq->rq.len, val);
1241 	}
1242 
1243 	val = rd32(hw, cq->sq.len);
1244 	if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1245 		   PF_FW_ATQLEN_ATQCRIT_M)) {
1246 		oldval = val;
1247 		if (val & PF_FW_ATQLEN_ATQVFE_M)
1248 			dev_dbg(dev, "%s Send Queue VF Error detected\n",
1249 				qtype);
1250 		if (val & PF_FW_ATQLEN_ATQOVFL_M) {
1251 			dev_dbg(dev, "%s Send Queue Overflow Error detected\n",
1252 				qtype);
1253 		}
1254 		if (val & PF_FW_ATQLEN_ATQCRIT_M)
1255 			dev_dbg(dev, "%s Send Queue Critical Error detected\n",
1256 				qtype);
1257 		val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1258 			 PF_FW_ATQLEN_ATQCRIT_M);
1259 		if (oldval != val)
1260 			wr32(hw, cq->sq.len, val);
1261 	}
1262 
1263 	event.buf_len = cq->rq_buf_size;
1264 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1265 	if (!event.msg_buf)
1266 		return 0;
1267 
1268 	do {
1269 		enum ice_status ret;
1270 		u16 opcode;
1271 
1272 		ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1273 		if (ret == ICE_ERR_AQ_NO_WORK)
1274 			break;
1275 		if (ret) {
1276 			dev_err(dev, "%s Receive Queue event error %s\n", qtype,
1277 				ice_stat_str(ret));
1278 			break;
1279 		}
1280 
1281 		opcode = le16_to_cpu(event.desc.opcode);
1282 
1283 		/* Notify any thread that might be waiting for this event */
1284 		ice_aq_check_events(pf, opcode, &event);
1285 
1286 		switch (opcode) {
1287 		case ice_aqc_opc_get_link_status:
1288 			if (ice_handle_link_event(pf, &event))
1289 				dev_err(dev, "Could not handle link event\n");
1290 			break;
1291 		case ice_aqc_opc_event_lan_overflow:
1292 			ice_vf_lan_overflow_event(pf, &event);
1293 			break;
1294 		case ice_mbx_opc_send_msg_to_pf:
1295 			ice_vc_process_vf_msg(pf, &event);
1296 			break;
1297 		case ice_aqc_opc_fw_logging:
1298 			ice_output_fw_log(hw, &event.desc, event.msg_buf);
1299 			break;
1300 		case ice_aqc_opc_lldp_set_mib_change:
1301 			ice_dcb_process_lldp_set_mib_change(pf, &event);
1302 			break;
1303 		default:
1304 			dev_dbg(dev, "%s Receive Queue unknown event 0x%04x ignored\n",
1305 				qtype, opcode);
1306 			break;
1307 		}
1308 	} while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1309 
1310 	kfree(event.msg_buf);
1311 
1312 	return pending && (i == ICE_DFLT_IRQ_WORK);
1313 }
1314 
1315 /**
1316  * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1317  * @hw: pointer to hardware info
1318  * @cq: control queue information
1319  *
1320  * returns true if there are pending messages in a queue, false if there aren't
1321  */
ice_ctrlq_pending(struct ice_hw * hw,struct ice_ctl_q_info * cq)1322 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1323 {
1324 	u16 ntu;
1325 
1326 	ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1327 	return cq->rq.next_to_clean != ntu;
1328 }
1329 
1330 /**
1331  * ice_clean_adminq_subtask - clean the AdminQ rings
1332  * @pf: board private structure
1333  */
ice_clean_adminq_subtask(struct ice_pf * pf)1334 static void ice_clean_adminq_subtask(struct ice_pf *pf)
1335 {
1336 	struct ice_hw *hw = &pf->hw;
1337 
1338 	if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
1339 		return;
1340 
1341 	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1342 		return;
1343 
1344 	clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
1345 
1346 	/* There might be a situation where new messages arrive to a control
1347 	 * queue between processing the last message and clearing the
1348 	 * EVENT_PENDING bit. So before exiting, check queue head again (using
1349 	 * ice_ctrlq_pending) and process new messages if any.
1350 	 */
1351 	if (ice_ctrlq_pending(hw, &hw->adminq))
1352 		__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1353 
1354 	ice_flush(hw);
1355 }
1356 
1357 /**
1358  * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1359  * @pf: board private structure
1360  */
ice_clean_mailboxq_subtask(struct ice_pf * pf)1361 static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1362 {
1363 	struct ice_hw *hw = &pf->hw;
1364 
1365 	if (!test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state))
1366 		return;
1367 
1368 	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1369 		return;
1370 
1371 	clear_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1372 
1373 	if (ice_ctrlq_pending(hw, &hw->mailboxq))
1374 		__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1375 
1376 	ice_flush(hw);
1377 }
1378 
1379 /**
1380  * ice_service_task_schedule - schedule the service task to wake up
1381  * @pf: board private structure
1382  *
1383  * If not already scheduled, this puts the task into the work queue.
1384  */
ice_service_task_schedule(struct ice_pf * pf)1385 void ice_service_task_schedule(struct ice_pf *pf)
1386 {
1387 	if (!test_bit(__ICE_SERVICE_DIS, pf->state) &&
1388 	    !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state) &&
1389 	    !test_bit(__ICE_NEEDS_RESTART, pf->state))
1390 		queue_work(ice_wq, &pf->serv_task);
1391 }
1392 
1393 /**
1394  * ice_service_task_complete - finish up the service task
1395  * @pf: board private structure
1396  */
ice_service_task_complete(struct ice_pf * pf)1397 static void ice_service_task_complete(struct ice_pf *pf)
1398 {
1399 	WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
1400 
1401 	/* force memory (pf->state) to sync before next service task */
1402 	smp_mb__before_atomic();
1403 	clear_bit(__ICE_SERVICE_SCHED, pf->state);
1404 }
1405 
1406 /**
1407  * ice_service_task_stop - stop service task and cancel works
1408  * @pf: board private structure
1409  *
1410  * Return 0 if the __ICE_SERVICE_DIS bit was not already set,
1411  * 1 otherwise.
1412  */
ice_service_task_stop(struct ice_pf * pf)1413 static int ice_service_task_stop(struct ice_pf *pf)
1414 {
1415 	int ret;
1416 
1417 	ret = test_and_set_bit(__ICE_SERVICE_DIS, pf->state);
1418 
1419 	if (pf->serv_tmr.function)
1420 		del_timer_sync(&pf->serv_tmr);
1421 	if (pf->serv_task.func)
1422 		cancel_work_sync(&pf->serv_task);
1423 
1424 	clear_bit(__ICE_SERVICE_SCHED, pf->state);
1425 	return ret;
1426 }
1427 
1428 /**
1429  * ice_service_task_restart - restart service task and schedule works
1430  * @pf: board private structure
1431  *
1432  * This function is needed for suspend and resume works (e.g WoL scenario)
1433  */
ice_service_task_restart(struct ice_pf * pf)1434 static void ice_service_task_restart(struct ice_pf *pf)
1435 {
1436 	clear_bit(__ICE_SERVICE_DIS, pf->state);
1437 	ice_service_task_schedule(pf);
1438 }
1439 
1440 /**
1441  * ice_service_timer - timer callback to schedule service task
1442  * @t: pointer to timer_list
1443  */
ice_service_timer(struct timer_list * t)1444 static void ice_service_timer(struct timer_list *t)
1445 {
1446 	struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1447 
1448 	mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1449 	ice_service_task_schedule(pf);
1450 }
1451 
1452 /**
1453  * ice_handle_mdd_event - handle malicious driver detect event
1454  * @pf: pointer to the PF structure
1455  *
1456  * Called from service task. OICR interrupt handler indicates MDD event.
1457  * VF MDD logging is guarded by net_ratelimit. Additional PF and VF log
1458  * messages are wrapped by netif_msg_[rx|tx]_err. Since VF Rx MDD events
1459  * disable the queue, the PF can be configured to reset the VF using ethtool
1460  * private flag mdd-auto-reset-vf.
1461  */
ice_handle_mdd_event(struct ice_pf * pf)1462 static void ice_handle_mdd_event(struct ice_pf *pf)
1463 {
1464 	struct device *dev = ice_pf_to_dev(pf);
1465 	struct ice_hw *hw = &pf->hw;
1466 	unsigned int i;
1467 	u32 reg;
1468 
1469 	if (!test_and_clear_bit(__ICE_MDD_EVENT_PENDING, pf->state)) {
1470 		/* Since the VF MDD event logging is rate limited, check if
1471 		 * there are pending MDD events.
1472 		 */
1473 		ice_print_vfs_mdd_events(pf);
1474 		return;
1475 	}
1476 
1477 	/* find what triggered an MDD event */
1478 	reg = rd32(hw, GL_MDET_TX_PQM);
1479 	if (reg & GL_MDET_TX_PQM_VALID_M) {
1480 		u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1481 				GL_MDET_TX_PQM_PF_NUM_S;
1482 		u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
1483 				GL_MDET_TX_PQM_VF_NUM_S;
1484 		u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1485 				GL_MDET_TX_PQM_MAL_TYPE_S;
1486 		u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
1487 				GL_MDET_TX_PQM_QNUM_S);
1488 
1489 		if (netif_msg_tx_err(pf))
1490 			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1491 				 event, queue, pf_num, vf_num);
1492 		wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1493 	}
1494 
1495 	reg = rd32(hw, GL_MDET_TX_TCLAN);
1496 	if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1497 		u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1498 				GL_MDET_TX_TCLAN_PF_NUM_S;
1499 		u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
1500 				GL_MDET_TX_TCLAN_VF_NUM_S;
1501 		u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1502 				GL_MDET_TX_TCLAN_MAL_TYPE_S;
1503 		u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1504 				GL_MDET_TX_TCLAN_QNUM_S);
1505 
1506 		if (netif_msg_tx_err(pf))
1507 			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1508 				 event, queue, pf_num, vf_num);
1509 		wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
1510 	}
1511 
1512 	reg = rd32(hw, GL_MDET_RX);
1513 	if (reg & GL_MDET_RX_VALID_M) {
1514 		u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
1515 				GL_MDET_RX_PF_NUM_S;
1516 		u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
1517 				GL_MDET_RX_VF_NUM_S;
1518 		u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
1519 				GL_MDET_RX_MAL_TYPE_S;
1520 		u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
1521 				GL_MDET_RX_QNUM_S);
1522 
1523 		if (netif_msg_rx_err(pf))
1524 			dev_info(dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1525 				 event, queue, pf_num, vf_num);
1526 		wr32(hw, GL_MDET_RX, 0xffffffff);
1527 	}
1528 
1529 	/* check to see if this PF caused an MDD event */
1530 	reg = rd32(hw, PF_MDET_TX_PQM);
1531 	if (reg & PF_MDET_TX_PQM_VALID_M) {
1532 		wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1533 		if (netif_msg_tx_err(pf))
1534 			dev_info(dev, "Malicious Driver Detection event TX_PQM detected on PF\n");
1535 	}
1536 
1537 	reg = rd32(hw, PF_MDET_TX_TCLAN);
1538 	if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1539 		wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
1540 		if (netif_msg_tx_err(pf))
1541 			dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on PF\n");
1542 	}
1543 
1544 	reg = rd32(hw, PF_MDET_RX);
1545 	if (reg & PF_MDET_RX_VALID_M) {
1546 		wr32(hw, PF_MDET_RX, 0xFFFF);
1547 		if (netif_msg_rx_err(pf))
1548 			dev_info(dev, "Malicious Driver Detection event RX detected on PF\n");
1549 	}
1550 
1551 	/* Check to see if one of the VFs caused an MDD event, and then
1552 	 * increment counters and set print pending
1553 	 */
1554 	ice_for_each_vf(pf, i) {
1555 		struct ice_vf *vf = &pf->vf[i];
1556 
1557 		reg = rd32(hw, VP_MDET_TX_PQM(i));
1558 		if (reg & VP_MDET_TX_PQM_VALID_M) {
1559 			wr32(hw, VP_MDET_TX_PQM(i), 0xFFFF);
1560 			vf->mdd_tx_events.count++;
1561 			set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1562 			if (netif_msg_tx_err(pf))
1563 				dev_info(dev, "Malicious Driver Detection event TX_PQM detected on VF %d\n",
1564 					 i);
1565 		}
1566 
1567 		reg = rd32(hw, VP_MDET_TX_TCLAN(i));
1568 		if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1569 			wr32(hw, VP_MDET_TX_TCLAN(i), 0xFFFF);
1570 			vf->mdd_tx_events.count++;
1571 			set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1572 			if (netif_msg_tx_err(pf))
1573 				dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on VF %d\n",
1574 					 i);
1575 		}
1576 
1577 		reg = rd32(hw, VP_MDET_TX_TDPU(i));
1578 		if (reg & VP_MDET_TX_TDPU_VALID_M) {
1579 			wr32(hw, VP_MDET_TX_TDPU(i), 0xFFFF);
1580 			vf->mdd_tx_events.count++;
1581 			set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1582 			if (netif_msg_tx_err(pf))
1583 				dev_info(dev, "Malicious Driver Detection event TX_TDPU detected on VF %d\n",
1584 					 i);
1585 		}
1586 
1587 		reg = rd32(hw, VP_MDET_RX(i));
1588 		if (reg & VP_MDET_RX_VALID_M) {
1589 			wr32(hw, VP_MDET_RX(i), 0xFFFF);
1590 			vf->mdd_rx_events.count++;
1591 			set_bit(__ICE_MDD_VF_PRINT_PENDING, pf->state);
1592 			if (netif_msg_rx_err(pf))
1593 				dev_info(dev, "Malicious Driver Detection event RX detected on VF %d\n",
1594 					 i);
1595 
1596 			/* Since the queue is disabled on VF Rx MDD events, the
1597 			 * PF can be configured to reset the VF through ethtool
1598 			 * private flag mdd-auto-reset-vf.
1599 			 */
1600 			if (test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
1601 				/* VF MDD event counters will be cleared by
1602 				 * reset, so print the event prior to reset.
1603 				 */
1604 				ice_print_vf_rx_mdd_event(vf);
1605 				ice_reset_vf(&pf->vf[i], false);
1606 			}
1607 		}
1608 	}
1609 
1610 	ice_print_vfs_mdd_events(pf);
1611 }
1612 
1613 /**
1614  * ice_force_phys_link_state - Force the physical link state
1615  * @vsi: VSI to force the physical link state to up/down
1616  * @link_up: true/false indicates to set the physical link to up/down
1617  *
1618  * Force the physical link state by getting the current PHY capabilities from
1619  * hardware and setting the PHY config based on the determined capabilities. If
1620  * link changes a link event will be triggered because both the Enable Automatic
1621  * Link Update and LESM Enable bits are set when setting the PHY capabilities.
1622  *
1623  * Returns 0 on success, negative on failure
1624  */
ice_force_phys_link_state(struct ice_vsi * vsi,bool link_up)1625 static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
1626 {
1627 	struct ice_aqc_get_phy_caps_data *pcaps;
1628 	struct ice_aqc_set_phy_cfg_data *cfg;
1629 	struct ice_port_info *pi;
1630 	struct device *dev;
1631 	int retcode;
1632 
1633 	if (!vsi || !vsi->port_info || !vsi->back)
1634 		return -EINVAL;
1635 	if (vsi->type != ICE_VSI_PF)
1636 		return 0;
1637 
1638 	dev = ice_pf_to_dev(vsi->back);
1639 
1640 	pi = vsi->port_info;
1641 
1642 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1643 	if (!pcaps)
1644 		return -ENOMEM;
1645 
1646 	retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
1647 				      NULL);
1648 	if (retcode) {
1649 		dev_err(dev, "Failed to get phy capabilities, VSI %d error %d\n",
1650 			vsi->vsi_num, retcode);
1651 		retcode = -EIO;
1652 		goto out;
1653 	}
1654 
1655 	/* No change in link */
1656 	if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
1657 	    link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
1658 		goto out;
1659 
1660 	/* Use the current user PHY configuration. The current user PHY
1661 	 * configuration is initialized during probe from PHY capabilities
1662 	 * software mode, and updated on set PHY configuration.
1663 	 */
1664 	cfg = kmemdup(&pi->phy.curr_user_phy_cfg, sizeof(*cfg), GFP_KERNEL);
1665 	if (!cfg) {
1666 		retcode = -ENOMEM;
1667 		goto out;
1668 	}
1669 
1670 	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1671 	if (link_up)
1672 		cfg->caps |= ICE_AQ_PHY_ENA_LINK;
1673 	else
1674 		cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
1675 
1676 	retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
1677 	if (retcode) {
1678 		dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
1679 			vsi->vsi_num, retcode);
1680 		retcode = -EIO;
1681 	}
1682 
1683 	kfree(cfg);
1684 out:
1685 	kfree(pcaps);
1686 	return retcode;
1687 }
1688 
1689 /**
1690  * ice_init_nvm_phy_type - Initialize the NVM PHY type
1691  * @pi: port info structure
1692  *
1693  * Initialize nvm_phy_type_[low|high] for link lenient mode support
1694  */
ice_init_nvm_phy_type(struct ice_port_info * pi)1695 static int ice_init_nvm_phy_type(struct ice_port_info *pi)
1696 {
1697 	struct ice_aqc_get_phy_caps_data *pcaps;
1698 	struct ice_pf *pf = pi->hw->back;
1699 	enum ice_status status;
1700 	int err = 0;
1701 
1702 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1703 	if (!pcaps)
1704 		return -ENOMEM;
1705 
1706 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_NVM_CAP, pcaps,
1707 				     NULL);
1708 
1709 	if (status) {
1710 		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
1711 		err = -EIO;
1712 		goto out;
1713 	}
1714 
1715 	pf->nvm_phy_type_hi = pcaps->phy_type_high;
1716 	pf->nvm_phy_type_lo = pcaps->phy_type_low;
1717 
1718 out:
1719 	kfree(pcaps);
1720 	return err;
1721 }
1722 
1723 /**
1724  * ice_init_link_dflt_override - Initialize link default override
1725  * @pi: port info structure
1726  *
1727  * Initialize link default override and PHY total port shutdown during probe
1728  */
ice_init_link_dflt_override(struct ice_port_info * pi)1729 static void ice_init_link_dflt_override(struct ice_port_info *pi)
1730 {
1731 	struct ice_link_default_override_tlv *ldo;
1732 	struct ice_pf *pf = pi->hw->back;
1733 
1734 	ldo = &pf->link_dflt_override;
1735 	if (ice_get_link_default_override(ldo, pi))
1736 		return;
1737 
1738 	if (!(ldo->options & ICE_LINK_OVERRIDE_PORT_DIS))
1739 		return;
1740 
1741 	/* Enable Total Port Shutdown (override/replace link-down-on-close
1742 	 * ethtool private flag) for ports with Port Disable bit set.
1743 	 */
1744 	set_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
1745 	set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
1746 }
1747 
1748 /**
1749  * ice_init_phy_cfg_dflt_override - Initialize PHY cfg default override settings
1750  * @pi: port info structure
1751  *
1752  * If default override is enabled, initialized the user PHY cfg speed and FEC
1753  * settings using the default override mask from the NVM.
1754  *
1755  * The PHY should only be configured with the default override settings the
1756  * first time media is available. The __ICE_LINK_DEFAULT_OVERRIDE_PENDING state
1757  * is used to indicate that the user PHY cfg default override is initialized
1758  * and the PHY has not been configured with the default override settings. The
1759  * state is set here, and cleared in ice_configure_phy the first time the PHY is
1760  * configured.
1761  */
ice_init_phy_cfg_dflt_override(struct ice_port_info * pi)1762 static void ice_init_phy_cfg_dflt_override(struct ice_port_info *pi)
1763 {
1764 	struct ice_link_default_override_tlv *ldo;
1765 	struct ice_aqc_set_phy_cfg_data *cfg;
1766 	struct ice_phy_info *phy = &pi->phy;
1767 	struct ice_pf *pf = pi->hw->back;
1768 
1769 	ldo = &pf->link_dflt_override;
1770 
1771 	/* If link default override is enabled, use to mask NVM PHY capabilities
1772 	 * for speed and FEC default configuration.
1773 	 */
1774 	cfg = &phy->curr_user_phy_cfg;
1775 
1776 	if (ldo->phy_type_low || ldo->phy_type_high) {
1777 		cfg->phy_type_low = pf->nvm_phy_type_lo &
1778 				    cpu_to_le64(ldo->phy_type_low);
1779 		cfg->phy_type_high = pf->nvm_phy_type_hi &
1780 				     cpu_to_le64(ldo->phy_type_high);
1781 	}
1782 	cfg->link_fec_opt = ldo->fec_options;
1783 	phy->curr_user_fec_req = ICE_FEC_AUTO;
1784 
1785 	set_bit(__ICE_LINK_DEFAULT_OVERRIDE_PENDING, pf->state);
1786 }
1787 
1788 /**
1789  * ice_init_phy_user_cfg - Initialize the PHY user configuration
1790  * @pi: port info structure
1791  *
1792  * Initialize the current user PHY configuration, speed, FEC, and FC requested
1793  * mode to default. The PHY defaults are from get PHY capabilities topology
1794  * with media so call when media is first available. An error is returned if
1795  * called when media is not available. The PHY initialization completed state is
1796  * set here.
1797  *
1798  * These configurations are used when setting PHY
1799  * configuration. The user PHY configuration is updated on set PHY
1800  * configuration. Returns 0 on success, negative on failure
1801  */
ice_init_phy_user_cfg(struct ice_port_info * pi)1802 static int ice_init_phy_user_cfg(struct ice_port_info *pi)
1803 {
1804 	struct ice_aqc_get_phy_caps_data *pcaps;
1805 	struct ice_phy_info *phy = &pi->phy;
1806 	struct ice_pf *pf = pi->hw->back;
1807 	enum ice_status status;
1808 	struct ice_vsi *vsi;
1809 	int err = 0;
1810 
1811 	if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
1812 		return -EIO;
1813 
1814 	vsi = ice_get_main_vsi(pf);
1815 	if (!vsi)
1816 		return -EINVAL;
1817 
1818 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1819 	if (!pcaps)
1820 		return -ENOMEM;
1821 
1822 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP, pcaps,
1823 				     NULL);
1824 	if (status) {
1825 		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
1826 		err = -EIO;
1827 		goto err_out;
1828 	}
1829 
1830 	ice_copy_phy_caps_to_cfg(pi, pcaps, &pi->phy.curr_user_phy_cfg);
1831 
1832 	/* check if lenient mode is supported and enabled */
1833 	if (ice_fw_supports_link_override(&vsi->back->hw) &&
1834 	    !(pcaps->module_compliance_enforcement &
1835 	      ICE_AQC_MOD_ENFORCE_STRICT_MODE)) {
1836 		set_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags);
1837 
1838 		/* if link default override is enabled, initialize user PHY
1839 		 * configuration with link default override values
1840 		 */
1841 		if (pf->link_dflt_override.options & ICE_LINK_OVERRIDE_EN) {
1842 			ice_init_phy_cfg_dflt_override(pi);
1843 			goto out;
1844 		}
1845 	}
1846 
1847 	/* if link default override is not enabled, initialize PHY using
1848 	 * topology with media
1849 	 */
1850 	phy->curr_user_fec_req = ice_caps_to_fec_mode(pcaps->caps,
1851 						      pcaps->link_fec_options);
1852 	phy->curr_user_fc_req = ice_caps_to_fc_mode(pcaps->caps);
1853 
1854 out:
1855 	phy->curr_user_speed_req = ICE_AQ_LINK_SPEED_M;
1856 	set_bit(__ICE_PHY_INIT_COMPLETE, pf->state);
1857 err_out:
1858 	kfree(pcaps);
1859 	return err;
1860 }
1861 
1862 /**
1863  * ice_configure_phy - configure PHY
1864  * @vsi: VSI of PHY
1865  *
1866  * Set the PHY configuration. If the current PHY configuration is the same as
1867  * the curr_user_phy_cfg, then do nothing to avoid link flap. Otherwise
1868  * configure the based get PHY capabilities for topology with media.
1869  */
ice_configure_phy(struct ice_vsi * vsi)1870 static int ice_configure_phy(struct ice_vsi *vsi)
1871 {
1872 	struct device *dev = ice_pf_to_dev(vsi->back);
1873 	struct ice_aqc_get_phy_caps_data *pcaps;
1874 	struct ice_aqc_set_phy_cfg_data *cfg;
1875 	struct ice_port_info *pi;
1876 	enum ice_status status;
1877 	int err = 0;
1878 
1879 	pi = vsi->port_info;
1880 	if (!pi)
1881 		return -EINVAL;
1882 
1883 	/* Ensure we have media as we cannot configure a medialess port */
1884 	if (!(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
1885 		return -EPERM;
1886 
1887 	ice_print_topo_conflict(vsi);
1888 
1889 	if (vsi->port_info->phy.link_info.topo_media_conflict ==
1890 	    ICE_AQ_LINK_TOPO_UNSUPP_MEDIA)
1891 		return -EPERM;
1892 
1893 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
1894 		return ice_force_phys_link_state(vsi, true);
1895 
1896 	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1897 	if (!pcaps)
1898 		return -ENOMEM;
1899 
1900 	/* Get current PHY config */
1901 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps,
1902 				     NULL);
1903 	if (status) {
1904 		dev_err(dev, "Failed to get PHY configuration, VSI %d error %s\n",
1905 			vsi->vsi_num, ice_stat_str(status));
1906 		err = -EIO;
1907 		goto done;
1908 	}
1909 
1910 	/* If PHY enable link is configured and configuration has not changed,
1911 	 * there's nothing to do
1912 	 */
1913 	if (pcaps->caps & ICE_AQC_PHY_EN_LINK &&
1914 	    ice_phy_caps_equals_cfg(pcaps, &pi->phy.curr_user_phy_cfg))
1915 		goto done;
1916 
1917 	/* Use PHY topology as baseline for configuration */
1918 	memset(pcaps, 0, sizeof(*pcaps));
1919 	status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP, pcaps,
1920 				     NULL);
1921 	if (status) {
1922 		dev_err(dev, "Failed to get PHY topology, VSI %d error %s\n",
1923 			vsi->vsi_num, ice_stat_str(status));
1924 		err = -EIO;
1925 		goto done;
1926 	}
1927 
1928 	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1929 	if (!cfg) {
1930 		err = -ENOMEM;
1931 		goto done;
1932 	}
1933 
1934 	ice_copy_phy_caps_to_cfg(pi, pcaps, cfg);
1935 
1936 	/* Speed - If default override pending, use curr_user_phy_cfg set in
1937 	 * ice_init_phy_user_cfg_ldo.
1938 	 */
1939 	if (test_and_clear_bit(__ICE_LINK_DEFAULT_OVERRIDE_PENDING,
1940 			       vsi->back->state)) {
1941 		cfg->phy_type_low = pi->phy.curr_user_phy_cfg.phy_type_low;
1942 		cfg->phy_type_high = pi->phy.curr_user_phy_cfg.phy_type_high;
1943 	} else {
1944 		u64 phy_low = 0, phy_high = 0;
1945 
1946 		ice_update_phy_type(&phy_low, &phy_high,
1947 				    pi->phy.curr_user_speed_req);
1948 		cfg->phy_type_low = pcaps->phy_type_low & cpu_to_le64(phy_low);
1949 		cfg->phy_type_high = pcaps->phy_type_high &
1950 				     cpu_to_le64(phy_high);
1951 	}
1952 
1953 	/* Can't provide what was requested; use PHY capabilities */
1954 	if (!cfg->phy_type_low && !cfg->phy_type_high) {
1955 		cfg->phy_type_low = pcaps->phy_type_low;
1956 		cfg->phy_type_high = pcaps->phy_type_high;
1957 	}
1958 
1959 	/* FEC */
1960 	ice_cfg_phy_fec(pi, cfg, pi->phy.curr_user_fec_req);
1961 
1962 	/* Can't provide what was requested; use PHY capabilities */
1963 	if (cfg->link_fec_opt !=
1964 	    (cfg->link_fec_opt & pcaps->link_fec_options)) {
1965 		cfg->caps |= pcaps->caps & ICE_AQC_PHY_EN_AUTO_FEC;
1966 		cfg->link_fec_opt = pcaps->link_fec_options;
1967 	}
1968 
1969 	/* Flow Control - always supported; no need to check against
1970 	 * capabilities
1971 	 */
1972 	ice_cfg_phy_fc(pi, cfg, pi->phy.curr_user_fc_req);
1973 
1974 	/* Enable link and link update */
1975 	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT | ICE_AQ_PHY_ENA_LINK;
1976 
1977 	status = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
1978 	if (status) {
1979 		dev_err(dev, "Failed to set phy config, VSI %d error %s\n",
1980 			vsi->vsi_num, ice_stat_str(status));
1981 		err = -EIO;
1982 	}
1983 
1984 	kfree(cfg);
1985 done:
1986 	kfree(pcaps);
1987 	return err;
1988 }
1989 
1990 /**
1991  * ice_check_media_subtask - Check for media
1992  * @pf: pointer to PF struct
1993  *
1994  * If media is available, then initialize PHY user configuration if it is not
1995  * been, and configure the PHY if the interface is up.
1996  */
ice_check_media_subtask(struct ice_pf * pf)1997 static void ice_check_media_subtask(struct ice_pf *pf)
1998 {
1999 	struct ice_port_info *pi;
2000 	struct ice_vsi *vsi;
2001 	int err;
2002 
2003 	/* No need to check for media if it's already present */
2004 	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags))
2005 		return;
2006 
2007 	vsi = ice_get_main_vsi(pf);
2008 	if (!vsi)
2009 		return;
2010 
2011 	/* Refresh link info and check if media is present */
2012 	pi = vsi->port_info;
2013 	err = ice_update_link_info(pi);
2014 	if (err)
2015 		return;
2016 
2017 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
2018 		if (!test_bit(__ICE_PHY_INIT_COMPLETE, pf->state))
2019 			ice_init_phy_user_cfg(pi);
2020 
2021 		/* PHY settings are reset on media insertion, reconfigure
2022 		 * PHY to preserve settings.
2023 		 */
2024 		if (test_bit(__ICE_DOWN, vsi->state) &&
2025 		    test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
2026 			return;
2027 
2028 		err = ice_configure_phy(vsi);
2029 		if (!err)
2030 			clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
2031 
2032 		/* A Link Status Event will be generated; the event handler
2033 		 * will complete bringing the interface up
2034 		 */
2035 	}
2036 }
2037 
2038 /**
2039  * ice_service_task - manage and run subtasks
2040  * @work: pointer to work_struct contained by the PF struct
2041  */
ice_service_task(struct work_struct * work)2042 static void ice_service_task(struct work_struct *work)
2043 {
2044 	struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
2045 	unsigned long start_time = jiffies;
2046 
2047 	/* subtasks */
2048 
2049 	/* process reset requests first */
2050 	ice_reset_subtask(pf);
2051 
2052 	/* bail if a reset/recovery cycle is pending or rebuild failed */
2053 	if (ice_is_reset_in_progress(pf->state) ||
2054 	    test_bit(__ICE_SUSPENDED, pf->state) ||
2055 	    test_bit(__ICE_NEEDS_RESTART, pf->state)) {
2056 		ice_service_task_complete(pf);
2057 		return;
2058 	}
2059 
2060 	ice_clean_adminq_subtask(pf);
2061 	ice_check_media_subtask(pf);
2062 	ice_check_for_hang_subtask(pf);
2063 	ice_sync_fltr_subtask(pf);
2064 	ice_handle_mdd_event(pf);
2065 	ice_watchdog_subtask(pf);
2066 
2067 	if (ice_is_safe_mode(pf)) {
2068 		ice_service_task_complete(pf);
2069 		return;
2070 	}
2071 
2072 	ice_process_vflr_event(pf);
2073 	ice_clean_mailboxq_subtask(pf);
2074 	ice_sync_arfs_fltrs(pf);
2075 	/* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
2076 	ice_service_task_complete(pf);
2077 
2078 	/* If the tasks have taken longer than one service timer period
2079 	 * or there is more work to be done, reset the service timer to
2080 	 * schedule the service task now.
2081 	 */
2082 	if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
2083 	    test_bit(__ICE_MDD_EVENT_PENDING, pf->state) ||
2084 	    test_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
2085 	    test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
2086 	    test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
2087 		mod_timer(&pf->serv_tmr, jiffies);
2088 }
2089 
2090 /**
2091  * ice_set_ctrlq_len - helper function to set controlq length
2092  * @hw: pointer to the HW instance
2093  */
ice_set_ctrlq_len(struct ice_hw * hw)2094 static void ice_set_ctrlq_len(struct ice_hw *hw)
2095 {
2096 	hw->adminq.num_rq_entries = ICE_AQ_LEN;
2097 	hw->adminq.num_sq_entries = ICE_AQ_LEN;
2098 	hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
2099 	hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
2100 	hw->mailboxq.num_rq_entries = PF_MBX_ARQLEN_ARQLEN_M;
2101 	hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN;
2102 	hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2103 	hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2104 }
2105 
2106 /**
2107  * ice_schedule_reset - schedule a reset
2108  * @pf: board private structure
2109  * @reset: reset being requested
2110  */
ice_schedule_reset(struct ice_pf * pf,enum ice_reset_req reset)2111 int ice_schedule_reset(struct ice_pf *pf, enum ice_reset_req reset)
2112 {
2113 	struct device *dev = ice_pf_to_dev(pf);
2114 
2115 	/* bail out if earlier reset has failed */
2116 	if (test_bit(__ICE_RESET_FAILED, pf->state)) {
2117 		dev_dbg(dev, "earlier reset has failed\n");
2118 		return -EIO;
2119 	}
2120 	/* bail if reset/recovery already in progress */
2121 	if (ice_is_reset_in_progress(pf->state)) {
2122 		dev_dbg(dev, "Reset already in progress\n");
2123 		return -EBUSY;
2124 	}
2125 
2126 	switch (reset) {
2127 	case ICE_RESET_PFR:
2128 		set_bit(__ICE_PFR_REQ, pf->state);
2129 		break;
2130 	case ICE_RESET_CORER:
2131 		set_bit(__ICE_CORER_REQ, pf->state);
2132 		break;
2133 	case ICE_RESET_GLOBR:
2134 		set_bit(__ICE_GLOBR_REQ, pf->state);
2135 		break;
2136 	default:
2137 		return -EINVAL;
2138 	}
2139 
2140 	ice_service_task_schedule(pf);
2141 	return 0;
2142 }
2143 
2144 /**
2145  * ice_irq_affinity_notify - Callback for affinity changes
2146  * @notify: context as to what irq was changed
2147  * @mask: the new affinity mask
2148  *
2149  * This is a callback function used by the irq_set_affinity_notifier function
2150  * so that we may register to receive changes to the irq affinity masks.
2151  */
2152 static void
ice_irq_affinity_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)2153 ice_irq_affinity_notify(struct irq_affinity_notify *notify,
2154 			const cpumask_t *mask)
2155 {
2156 	struct ice_q_vector *q_vector =
2157 		container_of(notify, struct ice_q_vector, affinity_notify);
2158 
2159 	cpumask_copy(&q_vector->affinity_mask, mask);
2160 }
2161 
2162 /**
2163  * ice_irq_affinity_release - Callback for affinity notifier release
2164  * @ref: internal core kernel usage
2165  *
2166  * This is a callback function used by the irq_set_affinity_notifier function
2167  * to inform the current notification subscriber that they will no longer
2168  * receive notifications.
2169  */
ice_irq_affinity_release(struct kref __always_unused * ref)2170 static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
2171 
2172 /**
2173  * ice_vsi_ena_irq - Enable IRQ for the given VSI
2174  * @vsi: the VSI being configured
2175  */
ice_vsi_ena_irq(struct ice_vsi * vsi)2176 static int ice_vsi_ena_irq(struct ice_vsi *vsi)
2177 {
2178 	struct ice_hw *hw = &vsi->back->hw;
2179 	int i;
2180 
2181 	ice_for_each_q_vector(vsi, i)
2182 		ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
2183 
2184 	ice_flush(hw);
2185 	return 0;
2186 }
2187 
2188 /**
2189  * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
2190  * @vsi: the VSI being configured
2191  * @basename: name for the vector
2192  */
ice_vsi_req_irq_msix(struct ice_vsi * vsi,char * basename)2193 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
2194 {
2195 	int q_vectors = vsi->num_q_vectors;
2196 	struct ice_pf *pf = vsi->back;
2197 	int base = vsi->base_vector;
2198 	struct device *dev;
2199 	int rx_int_idx = 0;
2200 	int tx_int_idx = 0;
2201 	int vector, err;
2202 	int irq_num;
2203 
2204 	dev = ice_pf_to_dev(pf);
2205 	for (vector = 0; vector < q_vectors; vector++) {
2206 		struct ice_q_vector *q_vector = vsi->q_vectors[vector];
2207 
2208 		irq_num = pf->msix_entries[base + vector].vector;
2209 
2210 		if (q_vector->tx.ring && q_vector->rx.ring) {
2211 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2212 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2213 			tx_int_idx++;
2214 		} else if (q_vector->rx.ring) {
2215 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2216 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
2217 		} else if (q_vector->tx.ring) {
2218 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2219 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
2220 		} else {
2221 			/* skip this unused q_vector */
2222 			continue;
2223 		}
2224 		err = devm_request_irq(dev, irq_num, vsi->irq_handler, 0,
2225 				       q_vector->name, q_vector);
2226 		if (err) {
2227 			netdev_err(vsi->netdev, "MSIX request_irq failed, error: %d\n",
2228 				   err);
2229 			goto free_q_irqs;
2230 		}
2231 
2232 		/* register for affinity change notifications */
2233 		if (!IS_ENABLED(CONFIG_RFS_ACCEL)) {
2234 			struct irq_affinity_notify *affinity_notify;
2235 
2236 			affinity_notify = &q_vector->affinity_notify;
2237 			affinity_notify->notify = ice_irq_affinity_notify;
2238 			affinity_notify->release = ice_irq_affinity_release;
2239 			irq_set_affinity_notifier(irq_num, affinity_notify);
2240 		}
2241 
2242 		/* assign the mask for this irq */
2243 		irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
2244 	}
2245 
2246 	vsi->irqs_ready = true;
2247 	return 0;
2248 
2249 free_q_irqs:
2250 	while (vector) {
2251 		vector--;
2252 		irq_num = pf->msix_entries[base + vector].vector;
2253 		if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2254 			irq_set_affinity_notifier(irq_num, NULL);
2255 		irq_set_affinity_hint(irq_num, NULL);
2256 		devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]);
2257 	}
2258 	return err;
2259 }
2260 
2261 /**
2262  * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
2263  * @vsi: VSI to setup Tx rings used by XDP
2264  *
2265  * Return 0 on success and negative value on error
2266  */
ice_xdp_alloc_setup_rings(struct ice_vsi * vsi)2267 static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
2268 {
2269 	struct device *dev = ice_pf_to_dev(vsi->back);
2270 	int i;
2271 
2272 	for (i = 0; i < vsi->num_xdp_txq; i++) {
2273 		u16 xdp_q_idx = vsi->alloc_txq + i;
2274 		struct ice_ring *xdp_ring;
2275 
2276 		xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
2277 
2278 		if (!xdp_ring)
2279 			goto free_xdp_rings;
2280 
2281 		xdp_ring->q_index = xdp_q_idx;
2282 		xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
2283 		xdp_ring->ring_active = false;
2284 		xdp_ring->vsi = vsi;
2285 		xdp_ring->netdev = NULL;
2286 		xdp_ring->dev = dev;
2287 		xdp_ring->count = vsi->num_tx_desc;
2288 		WRITE_ONCE(vsi->xdp_rings[i], xdp_ring);
2289 		if (ice_setup_tx_ring(xdp_ring))
2290 			goto free_xdp_rings;
2291 		ice_set_ring_xdp(xdp_ring);
2292 		xdp_ring->xsk_pool = ice_xsk_pool(xdp_ring);
2293 	}
2294 
2295 	return 0;
2296 
2297 free_xdp_rings:
2298 	for (; i >= 0; i--)
2299 		if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
2300 			ice_free_tx_ring(vsi->xdp_rings[i]);
2301 	return -ENOMEM;
2302 }
2303 
2304 /**
2305  * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
2306  * @vsi: VSI to set the bpf prog on
2307  * @prog: the bpf prog pointer
2308  */
ice_vsi_assign_bpf_prog(struct ice_vsi * vsi,struct bpf_prog * prog)2309 static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
2310 {
2311 	struct bpf_prog *old_prog;
2312 	int i;
2313 
2314 	old_prog = xchg(&vsi->xdp_prog, prog);
2315 	if (old_prog)
2316 		bpf_prog_put(old_prog);
2317 
2318 	ice_for_each_rxq(vsi, i)
2319 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
2320 }
2321 
2322 /**
2323  * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
2324  * @vsi: VSI to bring up Tx rings used by XDP
2325  * @prog: bpf program that will be assigned to VSI
2326  *
2327  * Return 0 on success and negative value on error
2328  */
ice_prepare_xdp_rings(struct ice_vsi * vsi,struct bpf_prog * prog)2329 int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog)
2330 {
2331 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2332 	int xdp_rings_rem = vsi->num_xdp_txq;
2333 	struct ice_pf *pf = vsi->back;
2334 	struct ice_qs_cfg xdp_qs_cfg = {
2335 		.qs_mutex = &pf->avail_q_mutex,
2336 		.pf_map = pf->avail_txqs,
2337 		.pf_map_size = pf->max_pf_txqs,
2338 		.q_count = vsi->num_xdp_txq,
2339 		.scatter_count = ICE_MAX_SCATTER_TXQS,
2340 		.vsi_map = vsi->txq_map,
2341 		.vsi_map_offset = vsi->alloc_txq,
2342 		.mapping_mode = ICE_VSI_MAP_CONTIG
2343 	};
2344 	enum ice_status status;
2345 	struct device *dev;
2346 	int i, v_idx;
2347 
2348 	dev = ice_pf_to_dev(pf);
2349 	vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq,
2350 				      sizeof(*vsi->xdp_rings), GFP_KERNEL);
2351 	if (!vsi->xdp_rings)
2352 		return -ENOMEM;
2353 
2354 	vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
2355 	if (__ice_vsi_get_qs(&xdp_qs_cfg))
2356 		goto err_map_xdp;
2357 
2358 	if (ice_xdp_alloc_setup_rings(vsi))
2359 		goto clear_xdp_rings;
2360 
2361 	/* follow the logic from ice_vsi_map_rings_to_vectors */
2362 	ice_for_each_q_vector(vsi, v_idx) {
2363 		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2364 		int xdp_rings_per_v, q_id, q_base;
2365 
2366 		xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
2367 					       vsi->num_q_vectors - v_idx);
2368 		q_base = vsi->num_xdp_txq - xdp_rings_rem;
2369 
2370 		for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
2371 			struct ice_ring *xdp_ring = vsi->xdp_rings[q_id];
2372 
2373 			xdp_ring->q_vector = q_vector;
2374 			xdp_ring->next = q_vector->tx.ring;
2375 			q_vector->tx.ring = xdp_ring;
2376 		}
2377 		xdp_rings_rem -= xdp_rings_per_v;
2378 	}
2379 
2380 	/* omit the scheduler update if in reset path; XDP queues will be
2381 	 * taken into account at the end of ice_vsi_rebuild, where
2382 	 * ice_cfg_vsi_lan is being called
2383 	 */
2384 	if (ice_is_reset_in_progress(pf->state))
2385 		return 0;
2386 
2387 	/* tell the Tx scheduler that right now we have
2388 	 * additional queues
2389 	 */
2390 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2391 		max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
2392 
2393 	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2394 				 max_txqs);
2395 	if (status) {
2396 		dev_err(dev, "Failed VSI LAN queue config for XDP, error: %s\n",
2397 			ice_stat_str(status));
2398 		goto clear_xdp_rings;
2399 	}
2400 
2401 	/* assign the prog only when it's not already present on VSI;
2402 	 * this flow is a subject of both ethtool -L and ndo_bpf flows;
2403 	 * VSI rebuild that happens under ethtool -L can expose us to
2404 	 * the bpf_prog refcount issues as we would be swapping same
2405 	 * bpf_prog pointers from vsi->xdp_prog and calling bpf_prog_put
2406 	 * on it as it would be treated as an 'old_prog'; for ndo_bpf
2407 	 * this is not harmful as dev_xdp_install bumps the refcount
2408 	 * before calling the op exposed by the driver;
2409 	 */
2410 	if (!ice_is_xdp_ena_vsi(vsi))
2411 		ice_vsi_assign_bpf_prog(vsi, prog);
2412 
2413 	return 0;
2414 clear_xdp_rings:
2415 	for (i = 0; i < vsi->num_xdp_txq; i++)
2416 		if (vsi->xdp_rings[i]) {
2417 			kfree_rcu(vsi->xdp_rings[i], rcu);
2418 			vsi->xdp_rings[i] = NULL;
2419 		}
2420 
2421 err_map_xdp:
2422 	mutex_lock(&pf->avail_q_mutex);
2423 	for (i = 0; i < vsi->num_xdp_txq; i++) {
2424 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2425 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2426 	}
2427 	mutex_unlock(&pf->avail_q_mutex);
2428 
2429 	devm_kfree(dev, vsi->xdp_rings);
2430 	return -ENOMEM;
2431 }
2432 
2433 /**
2434  * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
2435  * @vsi: VSI to remove XDP rings
2436  *
2437  * Detach XDP rings from irq vectors, clean up the PF bitmap and free
2438  * resources
2439  */
ice_destroy_xdp_rings(struct ice_vsi * vsi)2440 int ice_destroy_xdp_rings(struct ice_vsi *vsi)
2441 {
2442 	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2443 	struct ice_pf *pf = vsi->back;
2444 	int i, v_idx;
2445 
2446 	/* q_vectors are freed in reset path so there's no point in detaching
2447 	 * rings; in case of rebuild being triggered not from reset bits
2448 	 * in pf->state won't be set, so additionally check first q_vector
2449 	 * against NULL
2450 	 */
2451 	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2452 		goto free_qmap;
2453 
2454 	ice_for_each_q_vector(vsi, v_idx) {
2455 		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2456 		struct ice_ring *ring;
2457 
2458 		ice_for_each_ring(ring, q_vector->tx)
2459 			if (!ring->tx_buf || !ice_ring_is_xdp(ring))
2460 				break;
2461 
2462 		/* restore the value of last node prior to XDP setup */
2463 		q_vector->tx.ring = ring;
2464 	}
2465 
2466 free_qmap:
2467 	mutex_lock(&pf->avail_q_mutex);
2468 	for (i = 0; i < vsi->num_xdp_txq; i++) {
2469 		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2470 		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2471 	}
2472 	mutex_unlock(&pf->avail_q_mutex);
2473 
2474 	for (i = 0; i < vsi->num_xdp_txq; i++)
2475 		if (vsi->xdp_rings[i]) {
2476 			if (vsi->xdp_rings[i]->desc)
2477 				ice_free_tx_ring(vsi->xdp_rings[i]);
2478 			kfree_rcu(vsi->xdp_rings[i], rcu);
2479 			vsi->xdp_rings[i] = NULL;
2480 		}
2481 
2482 	devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
2483 	vsi->xdp_rings = NULL;
2484 
2485 	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2486 		return 0;
2487 
2488 	ice_vsi_assign_bpf_prog(vsi, NULL);
2489 
2490 	/* notify Tx scheduler that we destroyed XDP queues and bring
2491 	 * back the old number of child nodes
2492 	 */
2493 	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2494 		max_txqs[i] = vsi->num_txq;
2495 
2496 	/* change number of XDP Tx queues to 0 */
2497 	vsi->num_xdp_txq = 0;
2498 
2499 	return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2500 			       max_txqs);
2501 }
2502 
2503 /**
2504  * ice_xdp_setup_prog - Add or remove XDP eBPF program
2505  * @vsi: VSI to setup XDP for
2506  * @prog: XDP program
2507  * @extack: netlink extended ack
2508  */
2509 static int
ice_xdp_setup_prog(struct ice_vsi * vsi,struct bpf_prog * prog,struct netlink_ext_ack * extack)2510 ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
2511 		   struct netlink_ext_ack *extack)
2512 {
2513 	int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
2514 	bool if_running = netif_running(vsi->netdev);
2515 	int ret = 0, xdp_ring_err = 0;
2516 
2517 	if (frame_size > vsi->rx_buf_len) {
2518 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for loading XDP");
2519 		return -EOPNOTSUPP;
2520 	}
2521 
2522 	/* need to stop netdev while setting up the program for Rx rings */
2523 	if (if_running && !test_and_set_bit(__ICE_DOWN, vsi->state)) {
2524 		ret = ice_down(vsi);
2525 		if (ret) {
2526 			NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
2527 			return ret;
2528 		}
2529 	}
2530 
2531 	if (!ice_is_xdp_ena_vsi(vsi) && prog) {
2532 		vsi->num_xdp_txq = vsi->alloc_rxq;
2533 		xdp_ring_err = ice_prepare_xdp_rings(vsi, prog);
2534 		if (xdp_ring_err)
2535 			NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
2536 	} else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
2537 		xdp_ring_err = ice_destroy_xdp_rings(vsi);
2538 		if (xdp_ring_err)
2539 			NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
2540 	} else {
2541 		/* safe to call even when prog == vsi->xdp_prog as
2542 		 * dev_xdp_install in net/core/dev.c incremented prog's
2543 		 * refcount so corresponding bpf_prog_put won't cause
2544 		 * underflow
2545 		 */
2546 		ice_vsi_assign_bpf_prog(vsi, prog);
2547 	}
2548 
2549 	if (if_running)
2550 		ret = ice_up(vsi);
2551 
2552 	if (!ret && prog && vsi->xsk_pools) {
2553 		int i;
2554 
2555 		ice_for_each_rxq(vsi, i) {
2556 			struct ice_ring *rx_ring = vsi->rx_rings[i];
2557 
2558 			if (rx_ring->xsk_pool)
2559 				napi_schedule(&rx_ring->q_vector->napi);
2560 		}
2561 	}
2562 
2563 	return (ret || xdp_ring_err) ? -ENOMEM : 0;
2564 }
2565 
2566 /**
2567  * ice_xdp_safe_mode - XDP handler for safe mode
2568  * @dev: netdevice
2569  * @xdp: XDP command
2570  */
ice_xdp_safe_mode(struct net_device __always_unused * dev,struct netdev_bpf * xdp)2571 static int ice_xdp_safe_mode(struct net_device __always_unused *dev,
2572 			     struct netdev_bpf *xdp)
2573 {
2574 	NL_SET_ERR_MSG_MOD(xdp->extack,
2575 			   "Please provide working DDP firmware package in order to use XDP\n"
2576 			   "Refer to Documentation/networking/device_drivers/ethernet/intel/ice.rst");
2577 	return -EOPNOTSUPP;
2578 }
2579 
2580 /**
2581  * ice_xdp - implements XDP handler
2582  * @dev: netdevice
2583  * @xdp: XDP command
2584  */
ice_xdp(struct net_device * dev,struct netdev_bpf * xdp)2585 static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2586 {
2587 	struct ice_netdev_priv *np = netdev_priv(dev);
2588 	struct ice_vsi *vsi = np->vsi;
2589 
2590 	if (vsi->type != ICE_VSI_PF) {
2591 		NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF VSI");
2592 		return -EINVAL;
2593 	}
2594 
2595 	switch (xdp->command) {
2596 	case XDP_SETUP_PROG:
2597 		return ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
2598 	case XDP_SETUP_XSK_POOL:
2599 		return ice_xsk_pool_setup(vsi, xdp->xsk.pool,
2600 					  xdp->xsk.queue_id);
2601 	default:
2602 		return -EINVAL;
2603 	}
2604 }
2605 
2606 /**
2607  * ice_ena_misc_vector - enable the non-queue interrupts
2608  * @pf: board private structure
2609  */
ice_ena_misc_vector(struct ice_pf * pf)2610 static void ice_ena_misc_vector(struct ice_pf *pf)
2611 {
2612 	struct ice_hw *hw = &pf->hw;
2613 	u32 val;
2614 
2615 	/* Disable anti-spoof detection interrupt to prevent spurious event
2616 	 * interrupts during a function reset. Anti-spoof functionally is
2617 	 * still supported.
2618 	 */
2619 	val = rd32(hw, GL_MDCK_TX_TDPU);
2620 	val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
2621 	wr32(hw, GL_MDCK_TX_TDPU, val);
2622 
2623 	/* clear things first */
2624 	wr32(hw, PFINT_OICR_ENA, 0);	/* disable all */
2625 	rd32(hw, PFINT_OICR);		/* read to clear */
2626 
2627 	val = (PFINT_OICR_ECC_ERR_M |
2628 	       PFINT_OICR_MAL_DETECT_M |
2629 	       PFINT_OICR_GRST_M |
2630 	       PFINT_OICR_PCI_EXCEPTION_M |
2631 	       PFINT_OICR_VFLR_M |
2632 	       PFINT_OICR_HMC_ERR_M |
2633 	       PFINT_OICR_PE_CRITERR_M);
2634 
2635 	wr32(hw, PFINT_OICR_ENA, val);
2636 
2637 	/* SW_ITR_IDX = 0, but don't change INTENA */
2638 	wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
2639 	     GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
2640 }
2641 
2642 /**
2643  * ice_misc_intr - misc interrupt handler
2644  * @irq: interrupt number
2645  * @data: pointer to a q_vector
2646  */
ice_misc_intr(int __always_unused irq,void * data)2647 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
2648 {
2649 	struct ice_pf *pf = (struct ice_pf *)data;
2650 	struct ice_hw *hw = &pf->hw;
2651 	irqreturn_t ret = IRQ_NONE;
2652 	struct device *dev;
2653 	u32 oicr, ena_mask;
2654 
2655 	dev = ice_pf_to_dev(pf);
2656 	set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
2657 	set_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state);
2658 
2659 	oicr = rd32(hw, PFINT_OICR);
2660 	ena_mask = rd32(hw, PFINT_OICR_ENA);
2661 
2662 	if (oicr & PFINT_OICR_SWINT_M) {
2663 		ena_mask &= ~PFINT_OICR_SWINT_M;
2664 		pf->sw_int_count++;
2665 	}
2666 
2667 	if (oicr & PFINT_OICR_MAL_DETECT_M) {
2668 		ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
2669 		set_bit(__ICE_MDD_EVENT_PENDING, pf->state);
2670 	}
2671 	if (oicr & PFINT_OICR_VFLR_M) {
2672 		/* disable any further VFLR event notifications */
2673 		if (test_bit(__ICE_VF_RESETS_DISABLED, pf->state)) {
2674 			u32 reg = rd32(hw, PFINT_OICR_ENA);
2675 
2676 			reg &= ~PFINT_OICR_VFLR_M;
2677 			wr32(hw, PFINT_OICR_ENA, reg);
2678 		} else {
2679 			ena_mask &= ~PFINT_OICR_VFLR_M;
2680 			set_bit(__ICE_VFLR_EVENT_PENDING, pf->state);
2681 		}
2682 	}
2683 
2684 	if (oicr & PFINT_OICR_GRST_M) {
2685 		u32 reset;
2686 
2687 		/* we have a reset warning */
2688 		ena_mask &= ~PFINT_OICR_GRST_M;
2689 		reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
2690 			GLGEN_RSTAT_RESET_TYPE_S;
2691 
2692 		if (reset == ICE_RESET_CORER)
2693 			pf->corer_count++;
2694 		else if (reset == ICE_RESET_GLOBR)
2695 			pf->globr_count++;
2696 		else if (reset == ICE_RESET_EMPR)
2697 			pf->empr_count++;
2698 		else
2699 			dev_dbg(dev, "Invalid reset type %d\n", reset);
2700 
2701 		/* If a reset cycle isn't already in progress, we set a bit in
2702 		 * pf->state so that the service task can start a reset/rebuild.
2703 		 * We also make note of which reset happened so that peer
2704 		 * devices/drivers can be informed.
2705 		 */
2706 		if (!test_and_set_bit(__ICE_RESET_OICR_RECV, pf->state)) {
2707 			if (reset == ICE_RESET_CORER)
2708 				set_bit(__ICE_CORER_RECV, pf->state);
2709 			else if (reset == ICE_RESET_GLOBR)
2710 				set_bit(__ICE_GLOBR_RECV, pf->state);
2711 			else
2712 				set_bit(__ICE_EMPR_RECV, pf->state);
2713 
2714 			/* There are couple of different bits at play here.
2715 			 * hw->reset_ongoing indicates whether the hardware is
2716 			 * in reset. This is set to true when a reset interrupt
2717 			 * is received and set back to false after the driver
2718 			 * has determined that the hardware is out of reset.
2719 			 *
2720 			 * __ICE_RESET_OICR_RECV in pf->state indicates
2721 			 * that a post reset rebuild is required before the
2722 			 * driver is operational again. This is set above.
2723 			 *
2724 			 * As this is the start of the reset/rebuild cycle, set
2725 			 * both to indicate that.
2726 			 */
2727 			hw->reset_ongoing = true;
2728 		}
2729 	}
2730 
2731 	if (oicr & PFINT_OICR_HMC_ERR_M) {
2732 		ena_mask &= ~PFINT_OICR_HMC_ERR_M;
2733 		dev_dbg(dev, "HMC Error interrupt - info 0x%x, data 0x%x\n",
2734 			rd32(hw, PFHMC_ERRORINFO),
2735 			rd32(hw, PFHMC_ERRORDATA));
2736 	}
2737 
2738 	/* Report any remaining unexpected interrupts */
2739 	oicr &= ena_mask;
2740 	if (oicr) {
2741 		dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
2742 		/* If a critical error is pending there is no choice but to
2743 		 * reset the device.
2744 		 */
2745 		if (oicr & (PFINT_OICR_PE_CRITERR_M |
2746 			    PFINT_OICR_PCI_EXCEPTION_M |
2747 			    PFINT_OICR_ECC_ERR_M)) {
2748 			set_bit(__ICE_PFR_REQ, pf->state);
2749 			ice_service_task_schedule(pf);
2750 		}
2751 	}
2752 	ret = IRQ_HANDLED;
2753 
2754 	ice_service_task_schedule(pf);
2755 	ice_irq_dynamic_ena(hw, NULL, NULL);
2756 
2757 	return ret;
2758 }
2759 
2760 /**
2761  * ice_dis_ctrlq_interrupts - disable control queue interrupts
2762  * @hw: pointer to HW structure
2763  */
ice_dis_ctrlq_interrupts(struct ice_hw * hw)2764 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
2765 {
2766 	/* disable Admin queue Interrupt causes */
2767 	wr32(hw, PFINT_FW_CTL,
2768 	     rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
2769 
2770 	/* disable Mailbox queue Interrupt causes */
2771 	wr32(hw, PFINT_MBX_CTL,
2772 	     rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
2773 
2774 	/* disable Control queue Interrupt causes */
2775 	wr32(hw, PFINT_OICR_CTL,
2776 	     rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
2777 
2778 	ice_flush(hw);
2779 }
2780 
2781 /**
2782  * ice_free_irq_msix_misc - Unroll misc vector setup
2783  * @pf: board private structure
2784  */
ice_free_irq_msix_misc(struct ice_pf * pf)2785 static void ice_free_irq_msix_misc(struct ice_pf *pf)
2786 {
2787 	struct ice_hw *hw = &pf->hw;
2788 
2789 	ice_dis_ctrlq_interrupts(hw);
2790 
2791 	/* disable OICR interrupt */
2792 	wr32(hw, PFINT_OICR_ENA, 0);
2793 	ice_flush(hw);
2794 
2795 	if (pf->msix_entries) {
2796 		synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
2797 		devm_free_irq(ice_pf_to_dev(pf),
2798 			      pf->msix_entries[pf->oicr_idx].vector, pf);
2799 	}
2800 
2801 	pf->num_avail_sw_msix += 1;
2802 	ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
2803 }
2804 
2805 /**
2806  * ice_ena_ctrlq_interrupts - enable control queue interrupts
2807  * @hw: pointer to HW structure
2808  * @reg_idx: HW vector index to associate the control queue interrupts with
2809  */
ice_ena_ctrlq_interrupts(struct ice_hw * hw,u16 reg_idx)2810 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
2811 {
2812 	u32 val;
2813 
2814 	val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
2815 	       PFINT_OICR_CTL_CAUSE_ENA_M);
2816 	wr32(hw, PFINT_OICR_CTL, val);
2817 
2818 	/* enable Admin queue Interrupt causes */
2819 	val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
2820 	       PFINT_FW_CTL_CAUSE_ENA_M);
2821 	wr32(hw, PFINT_FW_CTL, val);
2822 
2823 	/* enable Mailbox queue Interrupt causes */
2824 	val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
2825 	       PFINT_MBX_CTL_CAUSE_ENA_M);
2826 	wr32(hw, PFINT_MBX_CTL, val);
2827 
2828 	ice_flush(hw);
2829 }
2830 
2831 /**
2832  * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
2833  * @pf: board private structure
2834  *
2835  * This sets up the handler for MSIX 0, which is used to manage the
2836  * non-queue interrupts, e.g. AdminQ and errors. This is not used
2837  * when in MSI or Legacy interrupt mode.
2838  */
ice_req_irq_msix_misc(struct ice_pf * pf)2839 static int ice_req_irq_msix_misc(struct ice_pf *pf)
2840 {
2841 	struct device *dev = ice_pf_to_dev(pf);
2842 	struct ice_hw *hw = &pf->hw;
2843 	int oicr_idx, err = 0;
2844 
2845 	if (!pf->int_name[0])
2846 		snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
2847 			 dev_driver_string(dev), dev_name(dev));
2848 
2849 	/* Do not request IRQ but do enable OICR interrupt since settings are
2850 	 * lost during reset. Note that this function is called only during
2851 	 * rebuild path and not while reset is in progress.
2852 	 */
2853 	if (ice_is_reset_in_progress(pf->state))
2854 		goto skip_req_irq;
2855 
2856 	/* reserve one vector in irq_tracker for misc interrupts */
2857 	oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2858 	if (oicr_idx < 0)
2859 		return oicr_idx;
2860 
2861 	pf->num_avail_sw_msix -= 1;
2862 	pf->oicr_idx = (u16)oicr_idx;
2863 
2864 	err = devm_request_irq(dev, pf->msix_entries[pf->oicr_idx].vector,
2865 			       ice_misc_intr, 0, pf->int_name, pf);
2866 	if (err) {
2867 		dev_err(dev, "devm_request_irq for %s failed: %d\n",
2868 			pf->int_name, err);
2869 		ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
2870 		pf->num_avail_sw_msix += 1;
2871 		return err;
2872 	}
2873 
2874 skip_req_irq:
2875 	ice_ena_misc_vector(pf);
2876 
2877 	ice_ena_ctrlq_interrupts(hw, pf->oicr_idx);
2878 	wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
2879 	     ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
2880 
2881 	ice_flush(hw);
2882 	ice_irq_dynamic_ena(hw, NULL, NULL);
2883 
2884 	return 0;
2885 }
2886 
2887 /**
2888  * ice_napi_add - register NAPI handler for the VSI
2889  * @vsi: VSI for which NAPI handler is to be registered
2890  *
2891  * This function is only called in the driver's load path. Registering the NAPI
2892  * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
2893  * reset/rebuild, etc.)
2894  */
ice_napi_add(struct ice_vsi * vsi)2895 static void ice_napi_add(struct ice_vsi *vsi)
2896 {
2897 	int v_idx;
2898 
2899 	if (!vsi->netdev)
2900 		return;
2901 
2902 	ice_for_each_q_vector(vsi, v_idx)
2903 		netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
2904 			       ice_napi_poll, NAPI_POLL_WEIGHT);
2905 }
2906 
2907 /**
2908  * ice_set_ops - set netdev and ethtools ops for the given netdev
2909  * @netdev: netdev instance
2910  */
ice_set_ops(struct net_device * netdev)2911 static void ice_set_ops(struct net_device *netdev)
2912 {
2913 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
2914 
2915 	if (ice_is_safe_mode(pf)) {
2916 		netdev->netdev_ops = &ice_netdev_safe_mode_ops;
2917 		ice_set_ethtool_safe_mode_ops(netdev);
2918 		return;
2919 	}
2920 
2921 	netdev->netdev_ops = &ice_netdev_ops;
2922 	netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
2923 	ice_set_ethtool_ops(netdev);
2924 }
2925 
2926 /**
2927  * ice_set_netdev_features - set features for the given netdev
2928  * @netdev: netdev instance
2929  */
ice_set_netdev_features(struct net_device * netdev)2930 static void ice_set_netdev_features(struct net_device *netdev)
2931 {
2932 	struct ice_pf *pf = ice_netdev_to_pf(netdev);
2933 	netdev_features_t csumo_features;
2934 	netdev_features_t vlano_features;
2935 	netdev_features_t dflt_features;
2936 	netdev_features_t tso_features;
2937 
2938 	if (ice_is_safe_mode(pf)) {
2939 		/* safe mode */
2940 		netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
2941 		netdev->hw_features = netdev->features;
2942 		return;
2943 	}
2944 
2945 	dflt_features = NETIF_F_SG	|
2946 			NETIF_F_HIGHDMA	|
2947 			NETIF_F_NTUPLE	|
2948 			NETIF_F_RXHASH;
2949 
2950 	csumo_features = NETIF_F_RXCSUM	  |
2951 			 NETIF_F_IP_CSUM  |
2952 			 NETIF_F_SCTP_CRC |
2953 			 NETIF_F_IPV6_CSUM;
2954 
2955 	vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
2956 			 NETIF_F_HW_VLAN_CTAG_TX     |
2957 			 NETIF_F_HW_VLAN_CTAG_RX;
2958 
2959 	tso_features = NETIF_F_TSO			|
2960 		       NETIF_F_TSO_ECN			|
2961 		       NETIF_F_TSO6			|
2962 		       NETIF_F_GSO_GRE			|
2963 		       NETIF_F_GSO_UDP_TUNNEL		|
2964 		       NETIF_F_GSO_GRE_CSUM		|
2965 		       NETIF_F_GSO_UDP_TUNNEL_CSUM	|
2966 		       NETIF_F_GSO_PARTIAL		|
2967 		       NETIF_F_GSO_IPXIP4		|
2968 		       NETIF_F_GSO_IPXIP6		|
2969 		       NETIF_F_GSO_UDP_L4;
2970 
2971 	netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
2972 					NETIF_F_GSO_GRE_CSUM;
2973 	/* set features that user can change */
2974 	netdev->hw_features = dflt_features | csumo_features |
2975 			      vlano_features | tso_features;
2976 
2977 	/* add support for HW_CSUM on packets with MPLS header */
2978 	netdev->mpls_features =  NETIF_F_HW_CSUM;
2979 
2980 	/* enable features */
2981 	netdev->features |= netdev->hw_features;
2982 	/* encap and VLAN devices inherit default, csumo and tso features */
2983 	netdev->hw_enc_features |= dflt_features | csumo_features |
2984 				   tso_features;
2985 	netdev->vlan_features |= dflt_features | csumo_features |
2986 				 tso_features;
2987 }
2988 
2989 /**
2990  * ice_cfg_netdev - Allocate, configure and register a netdev
2991  * @vsi: the VSI associated with the new netdev
2992  *
2993  * Returns 0 on success, negative value on failure
2994  */
ice_cfg_netdev(struct ice_vsi * vsi)2995 static int ice_cfg_netdev(struct ice_vsi *vsi)
2996 {
2997 	struct ice_pf *pf = vsi->back;
2998 	struct ice_netdev_priv *np;
2999 	struct net_device *netdev;
3000 	u8 mac_addr[ETH_ALEN];
3001 	int err;
3002 
3003 	err = ice_devlink_create_port(vsi);
3004 	if (err)
3005 		return err;
3006 
3007 	netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
3008 				    vsi->alloc_rxq);
3009 	if (!netdev) {
3010 		err = -ENOMEM;
3011 		goto err_destroy_devlink_port;
3012 	}
3013 
3014 	vsi->netdev = netdev;
3015 	np = netdev_priv(netdev);
3016 	np->vsi = vsi;
3017 
3018 	ice_set_netdev_features(netdev);
3019 
3020 	ice_set_ops(netdev);
3021 
3022 	if (vsi->type == ICE_VSI_PF) {
3023 		SET_NETDEV_DEV(netdev, ice_pf_to_dev(pf));
3024 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
3025 		ether_addr_copy(netdev->dev_addr, mac_addr);
3026 		ether_addr_copy(netdev->perm_addr, mac_addr);
3027 	}
3028 
3029 	netdev->priv_flags |= IFF_UNICAST_FLT;
3030 
3031 	/* Setup netdev TC information */
3032 	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
3033 
3034 	/* setup watchdog timeout value to be 5 second */
3035 	netdev->watchdog_timeo = 5 * HZ;
3036 
3037 	netdev->min_mtu = ETH_MIN_MTU;
3038 	netdev->max_mtu = ICE_MAX_MTU;
3039 
3040 	err = register_netdev(vsi->netdev);
3041 	if (err)
3042 		goto err_free_netdev;
3043 
3044 	devlink_port_type_eth_set(&vsi->devlink_port, vsi->netdev);
3045 
3046 	netif_carrier_off(vsi->netdev);
3047 
3048 	/* make sure transmit queues start off as stopped */
3049 	netif_tx_stop_all_queues(vsi->netdev);
3050 
3051 	return 0;
3052 
3053 err_free_netdev:
3054 	free_netdev(vsi->netdev);
3055 	vsi->netdev = NULL;
3056 err_destroy_devlink_port:
3057 	ice_devlink_destroy_port(vsi);
3058 	return err;
3059 }
3060 
3061 /**
3062  * ice_fill_rss_lut - Fill the RSS lookup table with default values
3063  * @lut: Lookup table
3064  * @rss_table_size: Lookup table size
3065  * @rss_size: Range of queue number for hashing
3066  */
ice_fill_rss_lut(u8 * lut,u16 rss_table_size,u16 rss_size)3067 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
3068 {
3069 	u16 i;
3070 
3071 	for (i = 0; i < rss_table_size; i++)
3072 		lut[i] = i % rss_size;
3073 }
3074 
3075 /**
3076  * ice_pf_vsi_setup - Set up a PF VSI
3077  * @pf: board private structure
3078  * @pi: pointer to the port_info instance
3079  *
3080  * Returns pointer to the successfully allocated VSI software struct
3081  * on success, otherwise returns NULL on failure.
3082  */
3083 static struct ice_vsi *
ice_pf_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi)3084 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3085 {
3086 	return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID);
3087 }
3088 
3089 /**
3090  * ice_ctrl_vsi_setup - Set up a control VSI
3091  * @pf: board private structure
3092  * @pi: pointer to the port_info instance
3093  *
3094  * Returns pointer to the successfully allocated VSI software struct
3095  * on success, otherwise returns NULL on failure.
3096  */
3097 static struct ice_vsi *
ice_ctrl_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi)3098 ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3099 {
3100 	return ice_vsi_setup(pf, pi, ICE_VSI_CTRL, ICE_INVAL_VFID);
3101 }
3102 
3103 /**
3104  * ice_lb_vsi_setup - Set up a loopback VSI
3105  * @pf: board private structure
3106  * @pi: pointer to the port_info instance
3107  *
3108  * Returns pointer to the successfully allocated VSI software struct
3109  * on success, otherwise returns NULL on failure.
3110  */
3111 struct ice_vsi *
ice_lb_vsi_setup(struct ice_pf * pf,struct ice_port_info * pi)3112 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3113 {
3114 	return ice_vsi_setup(pf, pi, ICE_VSI_LB, ICE_INVAL_VFID);
3115 }
3116 
3117 /**
3118  * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
3119  * @netdev: network interface to be adjusted
3120  * @proto: unused protocol
3121  * @vid: VLAN ID to be added
3122  *
3123  * net_device_ops implementation for adding VLAN IDs
3124  */
3125 static int
ice_vlan_rx_add_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3126 ice_vlan_rx_add_vid(struct net_device *netdev, __always_unused __be16 proto,
3127 		    u16 vid)
3128 {
3129 	struct ice_netdev_priv *np = netdev_priv(netdev);
3130 	struct ice_vsi *vsi = np->vsi;
3131 	int ret;
3132 
3133 	if (vid >= VLAN_N_VID) {
3134 		netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
3135 			   vid, VLAN_N_VID);
3136 		return -EINVAL;
3137 	}
3138 
3139 	if (vsi->info.pvid)
3140 		return -EINVAL;
3141 
3142 	/* VLAN 0 is added by default during load/reset */
3143 	if (!vid)
3144 		return 0;
3145 
3146 	/* Enable VLAN pruning when a VLAN other than 0 is added */
3147 	if (!ice_vsi_is_vlan_pruning_ena(vsi)) {
3148 		ret = ice_cfg_vlan_pruning(vsi, true, false);
3149 		if (ret)
3150 			return ret;
3151 	}
3152 
3153 	/* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
3154 	 * packets aren't pruned by the device's internal switch on Rx
3155 	 */
3156 	ret = ice_vsi_add_vlan(vsi, vid, ICE_FWD_TO_VSI);
3157 	if (!ret) {
3158 		vsi->vlan_ena = true;
3159 		set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
3160 	}
3161 
3162 	return ret;
3163 }
3164 
3165 /**
3166  * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
3167  * @netdev: network interface to be adjusted
3168  * @proto: unused protocol
3169  * @vid: VLAN ID to be removed
3170  *
3171  * net_device_ops implementation for removing VLAN IDs
3172  */
3173 static int
ice_vlan_rx_kill_vid(struct net_device * netdev,__always_unused __be16 proto,u16 vid)3174 ice_vlan_rx_kill_vid(struct net_device *netdev, __always_unused __be16 proto,
3175 		     u16 vid)
3176 {
3177 	struct ice_netdev_priv *np = netdev_priv(netdev);
3178 	struct ice_vsi *vsi = np->vsi;
3179 	int ret;
3180 
3181 	if (vsi->info.pvid)
3182 		return -EINVAL;
3183 
3184 	/* don't allow removal of VLAN 0 */
3185 	if (!vid)
3186 		return 0;
3187 
3188 	/* Make sure ice_vsi_kill_vlan is successful before updating VLAN
3189 	 * information
3190 	 */
3191 	ret = ice_vsi_kill_vlan(vsi, vid);
3192 	if (ret)
3193 		return ret;
3194 
3195 	/* Disable pruning when VLAN 0 is the only VLAN rule */
3196 	if (vsi->num_vlan == 1 && ice_vsi_is_vlan_pruning_ena(vsi))
3197 		ret = ice_cfg_vlan_pruning(vsi, false, false);
3198 
3199 	vsi->vlan_ena = false;
3200 	set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
3201 	return ret;
3202 }
3203 
3204 /**
3205  * ice_setup_pf_sw - Setup the HW switch on startup or after reset
3206  * @pf: board private structure
3207  *
3208  * Returns 0 on success, negative value on failure
3209  */
ice_setup_pf_sw(struct ice_pf * pf)3210 static int ice_setup_pf_sw(struct ice_pf *pf)
3211 {
3212 	struct ice_vsi *vsi;
3213 	int status = 0;
3214 
3215 	if (ice_is_reset_in_progress(pf->state))
3216 		return -EBUSY;
3217 
3218 	vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
3219 	if (!vsi)
3220 		return -ENOMEM;
3221 
3222 	status = ice_cfg_netdev(vsi);
3223 	if (status) {
3224 		status = -ENODEV;
3225 		goto unroll_vsi_setup;
3226 	}
3227 	/* netdev has to be configured before setting frame size */
3228 	ice_vsi_cfg_frame_size(vsi);
3229 
3230 	/* Setup DCB netlink interface */
3231 	ice_dcbnl_setup(vsi);
3232 
3233 	/* registering the NAPI handler requires both the queues and
3234 	 * netdev to be created, which are done in ice_pf_vsi_setup()
3235 	 * and ice_cfg_netdev() respectively
3236 	 */
3237 	ice_napi_add(vsi);
3238 
3239 	status = ice_set_cpu_rx_rmap(vsi);
3240 	if (status) {
3241 		dev_err(ice_pf_to_dev(pf), "Failed to set CPU Rx map VSI %d error %d\n",
3242 			vsi->vsi_num, status);
3243 		status = -EINVAL;
3244 		goto unroll_napi_add;
3245 	}
3246 	status = ice_init_mac_fltr(pf);
3247 	if (status)
3248 		goto free_cpu_rx_map;
3249 
3250 	return status;
3251 
3252 free_cpu_rx_map:
3253 	ice_free_cpu_rx_rmap(vsi);
3254 
3255 unroll_napi_add:
3256 	if (vsi) {
3257 		ice_napi_del(vsi);
3258 		if (vsi->netdev) {
3259 			if (vsi->netdev->reg_state == NETREG_REGISTERED)
3260 				unregister_netdev(vsi->netdev);
3261 			free_netdev(vsi->netdev);
3262 			vsi->netdev = NULL;
3263 		}
3264 	}
3265 
3266 unroll_vsi_setup:
3267 	ice_vsi_release(vsi);
3268 	return status;
3269 }
3270 
3271 /**
3272  * ice_get_avail_q_count - Get count of queues in use
3273  * @pf_qmap: bitmap to get queue use count from
3274  * @lock: pointer to a mutex that protects access to pf_qmap
3275  * @size: size of the bitmap
3276  */
3277 static u16
ice_get_avail_q_count(unsigned long * pf_qmap,struct mutex * lock,u16 size)3278 ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
3279 {
3280 	unsigned long bit;
3281 	u16 count = 0;
3282 
3283 	mutex_lock(lock);
3284 	for_each_clear_bit(bit, pf_qmap, size)
3285 		count++;
3286 	mutex_unlock(lock);
3287 
3288 	return count;
3289 }
3290 
3291 /**
3292  * ice_get_avail_txq_count - Get count of Tx queues in use
3293  * @pf: pointer to an ice_pf instance
3294  */
ice_get_avail_txq_count(struct ice_pf * pf)3295 u16 ice_get_avail_txq_count(struct ice_pf *pf)
3296 {
3297 	return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
3298 				     pf->max_pf_txqs);
3299 }
3300 
3301 /**
3302  * ice_get_avail_rxq_count - Get count of Rx queues in use
3303  * @pf: pointer to an ice_pf instance
3304  */
ice_get_avail_rxq_count(struct ice_pf * pf)3305 u16 ice_get_avail_rxq_count(struct ice_pf *pf)
3306 {
3307 	return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
3308 				     pf->max_pf_rxqs);
3309 }
3310 
3311 /**
3312  * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3313  * @pf: board private structure to initialize
3314  */
ice_deinit_pf(struct ice_pf * pf)3315 static void ice_deinit_pf(struct ice_pf *pf)
3316 {
3317 	ice_service_task_stop(pf);
3318 	mutex_destroy(&pf->sw_mutex);
3319 	mutex_destroy(&pf->tc_mutex);
3320 	mutex_destroy(&pf->avail_q_mutex);
3321 
3322 	if (pf->avail_txqs) {
3323 		bitmap_free(pf->avail_txqs);
3324 		pf->avail_txqs = NULL;
3325 	}
3326 
3327 	if (pf->avail_rxqs) {
3328 		bitmap_free(pf->avail_rxqs);
3329 		pf->avail_rxqs = NULL;
3330 	}
3331 }
3332 
3333 /**
3334  * ice_set_pf_caps - set PFs capability flags
3335  * @pf: pointer to the PF instance
3336  */
ice_set_pf_caps(struct ice_pf * pf)3337 static void ice_set_pf_caps(struct ice_pf *pf)
3338 {
3339 	struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
3340 
3341 	clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3342 	if (func_caps->common_cap.dcb)
3343 		set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3344 	clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3345 	if (func_caps->common_cap.sr_iov_1_1) {
3346 		set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3347 		pf->num_vfs_supported = min_t(int, func_caps->num_allocd_vfs,
3348 					      ICE_MAX_VF_COUNT);
3349 	}
3350 	clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
3351 	if (func_caps->common_cap.rss_table_size)
3352 		set_bit(ICE_FLAG_RSS_ENA, pf->flags);
3353 
3354 	clear_bit(ICE_FLAG_FD_ENA, pf->flags);
3355 	if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
3356 		u16 unused;
3357 
3358 		/* ctrl_vsi_idx will be set to a valid value when flow director
3359 		 * is setup by ice_init_fdir
3360 		 */
3361 		pf->ctrl_vsi_idx = ICE_NO_VSI;
3362 		set_bit(ICE_FLAG_FD_ENA, pf->flags);
3363 		/* force guaranteed filter pool for PF */
3364 		ice_alloc_fd_guar_item(&pf->hw, &unused,
3365 				       func_caps->fd_fltr_guar);
3366 		/* force shared filter pool for PF */
3367 		ice_alloc_fd_shrd_item(&pf->hw, &unused,
3368 				       func_caps->fd_fltr_best_effort);
3369 	}
3370 
3371 	pf->max_pf_txqs = func_caps->common_cap.num_txq;
3372 	pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
3373 }
3374 
3375 /**
3376  * ice_init_pf - Initialize general software structures (struct ice_pf)
3377  * @pf: board private structure to initialize
3378  */
ice_init_pf(struct ice_pf * pf)3379 static int ice_init_pf(struct ice_pf *pf)
3380 {
3381 	ice_set_pf_caps(pf);
3382 
3383 	mutex_init(&pf->sw_mutex);
3384 	mutex_init(&pf->tc_mutex);
3385 
3386 	INIT_HLIST_HEAD(&pf->aq_wait_list);
3387 	spin_lock_init(&pf->aq_wait_lock);
3388 	init_waitqueue_head(&pf->aq_wait_queue);
3389 
3390 	/* setup service timer and periodic service task */
3391 	timer_setup(&pf->serv_tmr, ice_service_timer, 0);
3392 	pf->serv_tmr_period = HZ;
3393 	INIT_WORK(&pf->serv_task, ice_service_task);
3394 	clear_bit(__ICE_SERVICE_SCHED, pf->state);
3395 
3396 	mutex_init(&pf->avail_q_mutex);
3397 	pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
3398 	if (!pf->avail_txqs)
3399 		return -ENOMEM;
3400 
3401 	pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
3402 	if (!pf->avail_rxqs) {
3403 		devm_kfree(ice_pf_to_dev(pf), pf->avail_txqs);
3404 		pf->avail_txqs = NULL;
3405 		return -ENOMEM;
3406 	}
3407 
3408 	return 0;
3409 }
3410 
3411 /**
3412  * ice_ena_msix_range - Request a range of MSIX vectors from the OS
3413  * @pf: board private structure
3414  *
3415  * compute the number of MSIX vectors required (v_budget) and request from
3416  * the OS. Return the number of vectors reserved or negative on failure
3417  */
ice_ena_msix_range(struct ice_pf * pf)3418 static int ice_ena_msix_range(struct ice_pf *pf)
3419 {
3420 	struct device *dev = ice_pf_to_dev(pf);
3421 	int v_left, v_actual, v_budget = 0;
3422 	int needed, err, i;
3423 
3424 	v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
3425 
3426 	/* reserve one vector for miscellaneous handler */
3427 	needed = 1;
3428 	if (v_left < needed)
3429 		goto no_hw_vecs_left_err;
3430 	v_budget += needed;
3431 	v_left -= needed;
3432 
3433 	/* reserve vectors for LAN traffic */
3434 	needed = min_t(int, num_online_cpus(), v_left);
3435 	if (v_left < needed)
3436 		goto no_hw_vecs_left_err;
3437 	pf->num_lan_msix = needed;
3438 	v_budget += needed;
3439 	v_left -= needed;
3440 
3441 	/* reserve one vector for flow director */
3442 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
3443 		needed = ICE_FDIR_MSIX;
3444 		if (v_left < needed)
3445 			goto no_hw_vecs_left_err;
3446 		v_budget += needed;
3447 		v_left -= needed;
3448 	}
3449 
3450 	pf->msix_entries = devm_kcalloc(dev, v_budget,
3451 					sizeof(*pf->msix_entries), GFP_KERNEL);
3452 
3453 	if (!pf->msix_entries) {
3454 		err = -ENOMEM;
3455 		goto exit_err;
3456 	}
3457 
3458 	for (i = 0; i < v_budget; i++)
3459 		pf->msix_entries[i].entry = i;
3460 
3461 	/* actually reserve the vectors */
3462 	v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
3463 					 ICE_MIN_MSIX, v_budget);
3464 
3465 	if (v_actual < 0) {
3466 		dev_err(dev, "unable to reserve MSI-X vectors\n");
3467 		err = v_actual;
3468 		goto msix_err;
3469 	}
3470 
3471 	if (v_actual < v_budget) {
3472 		dev_warn(dev, "not enough OS MSI-X vectors. requested = %d, obtained = %d\n",
3473 			 v_budget, v_actual);
3474 
3475 		if (v_actual < ICE_MIN_MSIX) {
3476 			/* error if we can't get minimum vectors */
3477 			pci_disable_msix(pf->pdev);
3478 			err = -ERANGE;
3479 			goto msix_err;
3480 		} else {
3481 			pf->num_lan_msix = ICE_MIN_LAN_TXRX_MSIX;
3482 		}
3483 	}
3484 
3485 	return v_actual;
3486 
3487 msix_err:
3488 	devm_kfree(dev, pf->msix_entries);
3489 	goto exit_err;
3490 
3491 no_hw_vecs_left_err:
3492 	dev_err(dev, "not enough device MSI-X vectors. requested = %d, available = %d\n",
3493 		needed, v_left);
3494 	err = -ERANGE;
3495 exit_err:
3496 	pf->num_lan_msix = 0;
3497 	return err;
3498 }
3499 
3500 /**
3501  * ice_dis_msix - Disable MSI-X interrupt setup in OS
3502  * @pf: board private structure
3503  */
ice_dis_msix(struct ice_pf * pf)3504 static void ice_dis_msix(struct ice_pf *pf)
3505 {
3506 	pci_disable_msix(pf->pdev);
3507 	devm_kfree(ice_pf_to_dev(pf), pf->msix_entries);
3508 	pf->msix_entries = NULL;
3509 }
3510 
3511 /**
3512  * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
3513  * @pf: board private structure
3514  */
ice_clear_interrupt_scheme(struct ice_pf * pf)3515 static void ice_clear_interrupt_scheme(struct ice_pf *pf)
3516 {
3517 	ice_dis_msix(pf);
3518 
3519 	if (pf->irq_tracker) {
3520 		devm_kfree(ice_pf_to_dev(pf), pf->irq_tracker);
3521 		pf->irq_tracker = NULL;
3522 	}
3523 }
3524 
3525 /**
3526  * ice_init_interrupt_scheme - Determine proper interrupt scheme
3527  * @pf: board private structure to initialize
3528  */
ice_init_interrupt_scheme(struct ice_pf * pf)3529 static int ice_init_interrupt_scheme(struct ice_pf *pf)
3530 {
3531 	int vectors;
3532 
3533 	vectors = ice_ena_msix_range(pf);
3534 
3535 	if (vectors < 0)
3536 		return vectors;
3537 
3538 	/* set up vector assignment tracking */
3539 	pf->irq_tracker =
3540 		devm_kzalloc(ice_pf_to_dev(pf), sizeof(*pf->irq_tracker) +
3541 			     (sizeof(u16) * vectors), GFP_KERNEL);
3542 	if (!pf->irq_tracker) {
3543 		ice_dis_msix(pf);
3544 		return -ENOMEM;
3545 	}
3546 
3547 	/* populate SW interrupts pool with number of OS granted IRQs. */
3548 	pf->num_avail_sw_msix = (u16)vectors;
3549 	pf->irq_tracker->num_entries = (u16)vectors;
3550 	pf->irq_tracker->end = pf->irq_tracker->num_entries;
3551 
3552 	return 0;
3553 }
3554 
3555 /**
3556  * ice_is_wol_supported - check if WoL is supported
3557  * @hw: pointer to hardware info
3558  *
3559  * Check if WoL is supported based on the HW configuration.
3560  * Returns true if NVM supports and enables WoL for this port, false otherwise
3561  */
ice_is_wol_supported(struct ice_hw * hw)3562 bool ice_is_wol_supported(struct ice_hw *hw)
3563 {
3564 	u16 wol_ctrl;
3565 
3566 	/* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
3567 	 * word) indicates WoL is not supported on the corresponding PF ID.
3568 	 */
3569 	if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
3570 		return false;
3571 
3572 	return !(BIT(hw->port_info->lport) & wol_ctrl);
3573 }
3574 
3575 /**
3576  * ice_vsi_recfg_qs - Change the number of queues on a VSI
3577  * @vsi: VSI being changed
3578  * @new_rx: new number of Rx queues
3579  * @new_tx: new number of Tx queues
3580  *
3581  * Only change the number of queues if new_tx, or new_rx is non-0.
3582  *
3583  * Returns 0 on success.
3584  */
ice_vsi_recfg_qs(struct ice_vsi * vsi,int new_rx,int new_tx)3585 int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx)
3586 {
3587 	struct ice_pf *pf = vsi->back;
3588 	int err = 0, timeout = 50;
3589 
3590 	if (!new_rx && !new_tx)
3591 		return -EINVAL;
3592 
3593 	while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) {
3594 		timeout--;
3595 		if (!timeout)
3596 			return -EBUSY;
3597 		usleep_range(1000, 2000);
3598 	}
3599 
3600 	if (new_tx)
3601 		vsi->req_txq = (u16)new_tx;
3602 	if (new_rx)
3603 		vsi->req_rxq = (u16)new_rx;
3604 
3605 	/* set for the next time the netdev is started */
3606 	if (!netif_running(vsi->netdev)) {
3607 		ice_vsi_rebuild(vsi, false);
3608 		dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
3609 		goto done;
3610 	}
3611 
3612 	ice_vsi_close(vsi);
3613 	ice_vsi_rebuild(vsi, false);
3614 	ice_pf_dcb_recfg(pf);
3615 	ice_vsi_open(vsi);
3616 done:
3617 	clear_bit(__ICE_CFG_BUSY, pf->state);
3618 	return err;
3619 }
3620 
3621 /**
3622  * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
3623  * @pf: PF to configure
3624  *
3625  * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
3626  * VSI can still Tx/Rx VLAN tagged packets.
3627  */
ice_set_safe_mode_vlan_cfg(struct ice_pf * pf)3628 static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
3629 {
3630 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
3631 	struct ice_vsi_ctx *ctxt;
3632 	enum ice_status status;
3633 	struct ice_hw *hw;
3634 
3635 	if (!vsi)
3636 		return;
3637 
3638 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
3639 	if (!ctxt)
3640 		return;
3641 
3642 	hw = &pf->hw;
3643 	ctxt->info = vsi->info;
3644 
3645 	ctxt->info.valid_sections =
3646 		cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
3647 			    ICE_AQ_VSI_PROP_SECURITY_VALID |
3648 			    ICE_AQ_VSI_PROP_SW_VALID);
3649 
3650 	/* disable VLAN anti-spoof */
3651 	ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
3652 				  ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
3653 
3654 	/* disable VLAN pruning and keep all other settings */
3655 	ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
3656 
3657 	/* allow all VLANs on Tx and don't strip on Rx */
3658 	ctxt->info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL |
3659 		ICE_AQ_VSI_VLAN_EMOD_NOTHING;
3660 
3661 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
3662 	if (status) {
3663 		dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %s aq_err %s\n",
3664 			ice_stat_str(status),
3665 			ice_aq_str(hw->adminq.sq_last_status));
3666 	} else {
3667 		vsi->info.sec_flags = ctxt->info.sec_flags;
3668 		vsi->info.sw_flags2 = ctxt->info.sw_flags2;
3669 		vsi->info.vlan_flags = ctxt->info.vlan_flags;
3670 	}
3671 
3672 	kfree(ctxt);
3673 }
3674 
3675 /**
3676  * ice_log_pkg_init - log result of DDP package load
3677  * @hw: pointer to hardware info
3678  * @status: status of package load
3679  */
3680 static void
ice_log_pkg_init(struct ice_hw * hw,enum ice_status * status)3681 ice_log_pkg_init(struct ice_hw *hw, enum ice_status *status)
3682 {
3683 	struct ice_pf *pf = (struct ice_pf *)hw->back;
3684 	struct device *dev = ice_pf_to_dev(pf);
3685 
3686 	switch (*status) {
3687 	case ICE_SUCCESS:
3688 		/* The package download AdminQ command returned success because
3689 		 * this download succeeded or ICE_ERR_AQ_NO_WORK since there is
3690 		 * already a package loaded on the device.
3691 		 */
3692 		if (hw->pkg_ver.major == hw->active_pkg_ver.major &&
3693 		    hw->pkg_ver.minor == hw->active_pkg_ver.minor &&
3694 		    hw->pkg_ver.update == hw->active_pkg_ver.update &&
3695 		    hw->pkg_ver.draft == hw->active_pkg_ver.draft &&
3696 		    !memcmp(hw->pkg_name, hw->active_pkg_name,
3697 			    sizeof(hw->pkg_name))) {
3698 			if (hw->pkg_dwnld_status == ICE_AQ_RC_EEXIST)
3699 				dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
3700 					 hw->active_pkg_name,
3701 					 hw->active_pkg_ver.major,
3702 					 hw->active_pkg_ver.minor,
3703 					 hw->active_pkg_ver.update,
3704 					 hw->active_pkg_ver.draft);
3705 			else
3706 				dev_info(dev, "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
3707 					 hw->active_pkg_name,
3708 					 hw->active_pkg_ver.major,
3709 					 hw->active_pkg_ver.minor,
3710 					 hw->active_pkg_ver.update,
3711 					 hw->active_pkg_ver.draft);
3712 		} else if (hw->active_pkg_ver.major != ICE_PKG_SUPP_VER_MAJ ||
3713 			   hw->active_pkg_ver.minor != ICE_PKG_SUPP_VER_MNR) {
3714 			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",
3715 				hw->active_pkg_name,
3716 				hw->active_pkg_ver.major,
3717 				hw->active_pkg_ver.minor,
3718 				ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
3719 			*status = ICE_ERR_NOT_SUPPORTED;
3720 		} else if (hw->active_pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
3721 			   hw->active_pkg_ver.minor == ICE_PKG_SUPP_VER_MNR) {
3722 			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",
3723 				 hw->active_pkg_name,
3724 				 hw->active_pkg_ver.major,
3725 				 hw->active_pkg_ver.minor,
3726 				 hw->active_pkg_ver.update,
3727 				 hw->active_pkg_ver.draft,
3728 				 hw->pkg_name,
3729 				 hw->pkg_ver.major,
3730 				 hw->pkg_ver.minor,
3731 				 hw->pkg_ver.update,
3732 				 hw->pkg_ver.draft);
3733 		} else {
3734 			dev_err(dev, "An unknown error occurred when loading the DDP package, please reboot the system.  If the problem persists, update the NVM.  Entering Safe Mode.\n");
3735 			*status = ICE_ERR_NOT_SUPPORTED;
3736 		}
3737 		break;
3738 	case ICE_ERR_FW_DDP_MISMATCH:
3739 		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");
3740 		break;
3741 	case ICE_ERR_BUF_TOO_SHORT:
3742 	case ICE_ERR_CFG:
3743 		dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
3744 		break;
3745 	case ICE_ERR_NOT_SUPPORTED:
3746 		/* Package File version not supported */
3747 		if (hw->pkg_ver.major > ICE_PKG_SUPP_VER_MAJ ||
3748 		    (hw->pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
3749 		     hw->pkg_ver.minor > ICE_PKG_SUPP_VER_MNR))
3750 			dev_err(dev, "The DDP package file version is higher than the driver supports.  Please use an updated driver.  Entering Safe Mode.\n");
3751 		else if (hw->pkg_ver.major < ICE_PKG_SUPP_VER_MAJ ||
3752 			 (hw->pkg_ver.major == ICE_PKG_SUPP_VER_MAJ &&
3753 			  hw->pkg_ver.minor < ICE_PKG_SUPP_VER_MNR))
3754 			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",
3755 				ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
3756 		break;
3757 	case ICE_ERR_AQ_ERROR:
3758 		switch (hw->pkg_dwnld_status) {
3759 		case ICE_AQ_RC_ENOSEC:
3760 		case ICE_AQ_RC_EBADSIG:
3761 			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");
3762 			return;
3763 		case ICE_AQ_RC_ESVN:
3764 			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");
3765 			return;
3766 		case ICE_AQ_RC_EBADMAN:
3767 		case ICE_AQ_RC_EBADBUF:
3768 			dev_err(dev, "An error occurred on the device while loading the DDP package.  The device will be reset.\n");
3769 			/* poll for reset to complete */
3770 			if (ice_check_reset(hw))
3771 				dev_err(dev, "Error resetting device. Please reload the driver\n");
3772 			return;
3773 		default:
3774 			break;
3775 		}
3776 		fallthrough;
3777 	default:
3778 		dev_err(dev, "An unknown error (%d) occurred when loading the DDP package.  Entering Safe Mode.\n",
3779 			*status);
3780 		break;
3781 	}
3782 }
3783 
3784 /**
3785  * ice_load_pkg - load/reload the DDP Package file
3786  * @firmware: firmware structure when firmware requested or NULL for reload
3787  * @pf: pointer to the PF instance
3788  *
3789  * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
3790  * initialize HW tables.
3791  */
3792 static void
ice_load_pkg(const struct firmware * firmware,struct ice_pf * pf)3793 ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
3794 {
3795 	enum ice_status status = ICE_ERR_PARAM;
3796 	struct device *dev = ice_pf_to_dev(pf);
3797 	struct ice_hw *hw = &pf->hw;
3798 
3799 	/* Load DDP Package */
3800 	if (firmware && !hw->pkg_copy) {
3801 		status = ice_copy_and_init_pkg(hw, firmware->data,
3802 					       firmware->size);
3803 		ice_log_pkg_init(hw, &status);
3804 	} else if (!firmware && hw->pkg_copy) {
3805 		/* Reload package during rebuild after CORER/GLOBR reset */
3806 		status = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
3807 		ice_log_pkg_init(hw, &status);
3808 	} else {
3809 		dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
3810 	}
3811 
3812 	if (status) {
3813 		/* Safe Mode */
3814 		clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
3815 		return;
3816 	}
3817 
3818 	/* Successful download package is the precondition for advanced
3819 	 * features, hence setting the ICE_FLAG_ADV_FEATURES flag
3820 	 */
3821 	set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
3822 }
3823 
3824 /**
3825  * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
3826  * @pf: pointer to the PF structure
3827  *
3828  * There is no error returned here because the driver should be able to handle
3829  * 128 Byte cache lines, so we only print a warning in case issues are seen,
3830  * specifically with Tx.
3831  */
ice_verify_cacheline_size(struct ice_pf * pf)3832 static void ice_verify_cacheline_size(struct ice_pf *pf)
3833 {
3834 	if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
3835 		dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
3836 			 ICE_CACHE_LINE_BYTES);
3837 }
3838 
3839 /**
3840  * ice_send_version - update firmware with driver version
3841  * @pf: PF struct
3842  *
3843  * Returns ICE_SUCCESS on success, else error code
3844  */
ice_send_version(struct ice_pf * pf)3845 static enum ice_status ice_send_version(struct ice_pf *pf)
3846 {
3847 	struct ice_driver_ver dv;
3848 
3849 	dv.major_ver = 0xff;
3850 	dv.minor_ver = 0xff;
3851 	dv.build_ver = 0xff;
3852 	dv.subbuild_ver = 0;
3853 	strscpy((char *)dv.driver_string, UTS_RELEASE,
3854 		sizeof(dv.driver_string));
3855 	return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
3856 }
3857 
3858 /**
3859  * ice_init_fdir - Initialize flow director VSI and configuration
3860  * @pf: pointer to the PF instance
3861  *
3862  * returns 0 on success, negative on error
3863  */
ice_init_fdir(struct ice_pf * pf)3864 static int ice_init_fdir(struct ice_pf *pf)
3865 {
3866 	struct device *dev = ice_pf_to_dev(pf);
3867 	struct ice_vsi *ctrl_vsi;
3868 	int err;
3869 
3870 	/* Side Band Flow Director needs to have a control VSI.
3871 	 * Allocate it and store it in the PF.
3872 	 */
3873 	ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
3874 	if (!ctrl_vsi) {
3875 		dev_dbg(dev, "could not create control VSI\n");
3876 		return -ENOMEM;
3877 	}
3878 
3879 	err = ice_vsi_open_ctrl(ctrl_vsi);
3880 	if (err) {
3881 		dev_dbg(dev, "could not open control VSI\n");
3882 		goto err_vsi_open;
3883 	}
3884 
3885 	mutex_init(&pf->hw.fdir_fltr_lock);
3886 
3887 	err = ice_fdir_create_dflt_rules(pf);
3888 	if (err)
3889 		goto err_fdir_rule;
3890 
3891 	return 0;
3892 
3893 err_fdir_rule:
3894 	ice_fdir_release_flows(&pf->hw);
3895 	ice_vsi_close(ctrl_vsi);
3896 err_vsi_open:
3897 	ice_vsi_release(ctrl_vsi);
3898 	if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
3899 		pf->vsi[pf->ctrl_vsi_idx] = NULL;
3900 		pf->ctrl_vsi_idx = ICE_NO_VSI;
3901 	}
3902 	return err;
3903 }
3904 
3905 /**
3906  * ice_get_opt_fw_name - return optional firmware file name or NULL
3907  * @pf: pointer to the PF instance
3908  */
ice_get_opt_fw_name(struct ice_pf * pf)3909 static char *ice_get_opt_fw_name(struct ice_pf *pf)
3910 {
3911 	/* Optional firmware name same as default with additional dash
3912 	 * followed by a EUI-64 identifier (PCIe Device Serial Number)
3913 	 */
3914 	struct pci_dev *pdev = pf->pdev;
3915 	char *opt_fw_filename;
3916 	u64 dsn;
3917 
3918 	/* Determine the name of the optional file using the DSN (two
3919 	 * dwords following the start of the DSN Capability).
3920 	 */
3921 	dsn = pci_get_dsn(pdev);
3922 	if (!dsn)
3923 		return NULL;
3924 
3925 	opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
3926 	if (!opt_fw_filename)
3927 		return NULL;
3928 
3929 	snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
3930 		 ICE_DDP_PKG_PATH, dsn);
3931 
3932 	return opt_fw_filename;
3933 }
3934 
3935 /**
3936  * ice_request_fw - Device initialization routine
3937  * @pf: pointer to the PF instance
3938  */
ice_request_fw(struct ice_pf * pf)3939 static void ice_request_fw(struct ice_pf *pf)
3940 {
3941 	char *opt_fw_filename = ice_get_opt_fw_name(pf);
3942 	const struct firmware *firmware = NULL;
3943 	struct device *dev = ice_pf_to_dev(pf);
3944 	int err = 0;
3945 
3946 	/* optional device-specific DDP (if present) overrides the default DDP
3947 	 * package file. kernel logs a debug message if the file doesn't exist,
3948 	 * and warning messages for other errors.
3949 	 */
3950 	if (opt_fw_filename) {
3951 		err = firmware_request_nowarn(&firmware, opt_fw_filename, dev);
3952 		if (err) {
3953 			kfree(opt_fw_filename);
3954 			goto dflt_pkg_load;
3955 		}
3956 
3957 		/* request for firmware was successful. Download to device */
3958 		ice_load_pkg(firmware, pf);
3959 		kfree(opt_fw_filename);
3960 		release_firmware(firmware);
3961 		return;
3962 	}
3963 
3964 dflt_pkg_load:
3965 	err = request_firmware(&firmware, ICE_DDP_PKG_FILE, dev);
3966 	if (err) {
3967 		dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
3968 		return;
3969 	}
3970 
3971 	/* request for firmware was successful. Download to device */
3972 	ice_load_pkg(firmware, pf);
3973 	release_firmware(firmware);
3974 }
3975 
3976 /**
3977  * ice_print_wake_reason - show the wake up cause in the log
3978  * @pf: pointer to the PF struct
3979  */
ice_print_wake_reason(struct ice_pf * pf)3980 static void ice_print_wake_reason(struct ice_pf *pf)
3981 {
3982 	u32 wus = pf->wakeup_reason;
3983 	const char *wake_str;
3984 
3985 	/* if no wake event, nothing to print */
3986 	if (!wus)
3987 		return;
3988 
3989 	if (wus & PFPM_WUS_LNKC_M)
3990 		wake_str = "Link\n";
3991 	else if (wus & PFPM_WUS_MAG_M)
3992 		wake_str = "Magic Packet\n";
3993 	else if (wus & PFPM_WUS_MNG_M)
3994 		wake_str = "Management\n";
3995 	else if (wus & PFPM_WUS_FW_RST_WK_M)
3996 		wake_str = "Firmware Reset\n";
3997 	else
3998 		wake_str = "Unknown\n";
3999 
4000 	dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
4001 }
4002 
4003 /**
4004  * ice_probe - Device initialization routine
4005  * @pdev: PCI device information struct
4006  * @ent: entry in ice_pci_tbl
4007  *
4008  * Returns 0 on success, negative on failure
4009  */
4010 static int
ice_probe(struct pci_dev * pdev,const struct pci_device_id __always_unused * ent)4011 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
4012 {
4013 	struct device *dev = &pdev->dev;
4014 	struct ice_pf *pf;
4015 	struct ice_hw *hw;
4016 	int i, err;
4017 
4018 	if (pdev->is_virtfn) {
4019 		dev_err(dev, "can't probe a virtual function\n");
4020 		return -EINVAL;
4021 	}
4022 
4023 	/* this driver uses devres, see
4024 	 * Documentation/driver-api/driver-model/devres.rst
4025 	 */
4026 	err = pcim_enable_device(pdev);
4027 	if (err)
4028 		return err;
4029 
4030 	err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
4031 	if (err) {
4032 		dev_err(dev, "BAR0 I/O map error %d\n", err);
4033 		return err;
4034 	}
4035 
4036 	pf = ice_allocate_pf(dev);
4037 	if (!pf)
4038 		return -ENOMEM;
4039 
4040 	/* set up for high or low DMA */
4041 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
4042 	if (err)
4043 		err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
4044 	if (err) {
4045 		dev_err(dev, "DMA configuration failed: 0x%x\n", err);
4046 		return err;
4047 	}
4048 
4049 	pci_enable_pcie_error_reporting(pdev);
4050 	pci_set_master(pdev);
4051 
4052 	pf->pdev = pdev;
4053 	pci_set_drvdata(pdev, pf);
4054 	set_bit(__ICE_DOWN, pf->state);
4055 	/* Disable service task until DOWN bit is cleared */
4056 	set_bit(__ICE_SERVICE_DIS, pf->state);
4057 
4058 	hw = &pf->hw;
4059 	hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
4060 	pci_save_state(pdev);
4061 
4062 	hw->back = pf;
4063 	hw->vendor_id = pdev->vendor;
4064 	hw->device_id = pdev->device;
4065 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
4066 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
4067 	hw->subsystem_device_id = pdev->subsystem_device;
4068 	hw->bus.device = PCI_SLOT(pdev->devfn);
4069 	hw->bus.func = PCI_FUNC(pdev->devfn);
4070 	ice_set_ctrlq_len(hw);
4071 
4072 	pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
4073 
4074 	err = ice_devlink_register(pf);
4075 	if (err) {
4076 		dev_err(dev, "ice_devlink_register failed: %d\n", err);
4077 		goto err_exit_unroll;
4078 	}
4079 
4080 #ifndef CONFIG_DYNAMIC_DEBUG
4081 	if (debug < -1)
4082 		hw->debug_mask = debug;
4083 #endif
4084 
4085 	err = ice_init_hw(hw);
4086 	if (err) {
4087 		dev_err(dev, "ice_init_hw failed: %d\n", err);
4088 		err = -EIO;
4089 		goto err_exit_unroll;
4090 	}
4091 
4092 	ice_request_fw(pf);
4093 
4094 	/* if ice_request_fw fails, ICE_FLAG_ADV_FEATURES bit won't be
4095 	 * set in pf->state, which will cause ice_is_safe_mode to return
4096 	 * true
4097 	 */
4098 	if (ice_is_safe_mode(pf)) {
4099 		dev_err(dev, "Package download failed. Advanced features disabled - Device now in Safe Mode\n");
4100 		/* we already got function/device capabilities but these don't
4101 		 * reflect what the driver needs to do in safe mode. Instead of
4102 		 * adding conditional logic everywhere to ignore these
4103 		 * device/function capabilities, override them.
4104 		 */
4105 		ice_set_safe_mode_caps(hw);
4106 	}
4107 
4108 	err = ice_init_pf(pf);
4109 	if (err) {
4110 		dev_err(dev, "ice_init_pf failed: %d\n", err);
4111 		goto err_init_pf_unroll;
4112 	}
4113 
4114 	ice_devlink_init_regions(pf);
4115 
4116 	pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4117 	pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4118 	pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
4119 	pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
4120 	i = 0;
4121 	if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4122 		pf->hw.udp_tunnel_nic.tables[i].n_entries =
4123 			pf->hw.tnl.valid_count[TNL_VXLAN];
4124 		pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
4125 			UDP_TUNNEL_TYPE_VXLAN;
4126 		i++;
4127 	}
4128 	if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4129 		pf->hw.udp_tunnel_nic.tables[i].n_entries =
4130 			pf->hw.tnl.valid_count[TNL_GENEVE];
4131 		pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
4132 			UDP_TUNNEL_TYPE_GENEVE;
4133 		i++;
4134 	}
4135 
4136 	pf->num_alloc_vsi = hw->func_caps.guar_num_vsi;
4137 	if (!pf->num_alloc_vsi) {
4138 		err = -EIO;
4139 		goto err_init_pf_unroll;
4140 	}
4141 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
4142 		dev_warn(&pf->pdev->dev,
4143 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
4144 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
4145 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
4146 	}
4147 
4148 	pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
4149 			       GFP_KERNEL);
4150 	if (!pf->vsi) {
4151 		err = -ENOMEM;
4152 		goto err_init_pf_unroll;
4153 	}
4154 
4155 	err = ice_init_interrupt_scheme(pf);
4156 	if (err) {
4157 		dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4158 		err = -EIO;
4159 		goto err_init_vsi_unroll;
4160 	}
4161 
4162 	/* In case of MSIX we are going to setup the misc vector right here
4163 	 * to handle admin queue events etc. In case of legacy and MSI
4164 	 * the misc functionality and queue processing is combined in
4165 	 * the same vector and that gets setup at open.
4166 	 */
4167 	err = ice_req_irq_msix_misc(pf);
4168 	if (err) {
4169 		dev_err(dev, "setup of misc vector failed: %d\n", err);
4170 		goto err_init_interrupt_unroll;
4171 	}
4172 
4173 	/* create switch struct for the switch element created by FW on boot */
4174 	pf->first_sw = devm_kzalloc(dev, sizeof(*pf->first_sw), GFP_KERNEL);
4175 	if (!pf->first_sw) {
4176 		err = -ENOMEM;
4177 		goto err_msix_misc_unroll;
4178 	}
4179 
4180 	if (hw->evb_veb)
4181 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4182 	else
4183 		pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
4184 
4185 	pf->first_sw->pf = pf;
4186 
4187 	/* record the sw_id available for later use */
4188 	pf->first_sw->sw_id = hw->port_info->sw_id;
4189 
4190 	err = ice_setup_pf_sw(pf);
4191 	if (err) {
4192 		dev_err(dev, "probe failed due to setup PF switch: %d\n", err);
4193 		goto err_alloc_sw_unroll;
4194 	}
4195 
4196 	clear_bit(__ICE_SERVICE_DIS, pf->state);
4197 
4198 	/* tell the firmware we are up */
4199 	err = ice_send_version(pf);
4200 	if (err) {
4201 		dev_err(dev, "probe failed sending driver version %s. error: %d\n",
4202 			UTS_RELEASE, err);
4203 		goto err_send_version_unroll;
4204 	}
4205 
4206 	/* since everything is good, start the service timer */
4207 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4208 
4209 	err = ice_init_link_events(pf->hw.port_info);
4210 	if (err) {
4211 		dev_err(dev, "ice_init_link_events failed: %d\n", err);
4212 		goto err_send_version_unroll;
4213 	}
4214 
4215 	/* not a fatal error if this fails */
4216 	err = ice_init_nvm_phy_type(pf->hw.port_info);
4217 	if (err)
4218 		dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4219 
4220 	/* not a fatal error if this fails */
4221 	err = ice_update_link_info(pf->hw.port_info);
4222 	if (err)
4223 		dev_err(dev, "ice_update_link_info failed: %d\n", err);
4224 
4225 	ice_init_link_dflt_override(pf->hw.port_info);
4226 
4227 	/* if media available, initialize PHY settings */
4228 	if (pf->hw.port_info->phy.link_info.link_info &
4229 	    ICE_AQ_MEDIA_AVAILABLE) {
4230 		/* not a fatal error if this fails */
4231 		err = ice_init_phy_user_cfg(pf->hw.port_info);
4232 		if (err)
4233 			dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4234 
4235 		if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4236 			struct ice_vsi *vsi = ice_get_main_vsi(pf);
4237 
4238 			if (vsi)
4239 				ice_configure_phy(vsi);
4240 		}
4241 	} else {
4242 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4243 	}
4244 
4245 	ice_verify_cacheline_size(pf);
4246 
4247 	/* Save wakeup reason register for later use */
4248 	pf->wakeup_reason = rd32(hw, PFPM_WUS);
4249 
4250 	/* check for a power management event */
4251 	ice_print_wake_reason(pf);
4252 
4253 	/* clear wake status, all bits */
4254 	wr32(hw, PFPM_WUS, U32_MAX);
4255 
4256 	/* Disable WoL at init, wait for user to enable */
4257 	device_set_wakeup_enable(dev, false);
4258 
4259 	if (ice_is_safe_mode(pf)) {
4260 		ice_set_safe_mode_vlan_cfg(pf);
4261 		goto probe_done;
4262 	}
4263 
4264 	/* initialize DDP driven features */
4265 
4266 	/* Note: Flow director init failure is non-fatal to load */
4267 	if (ice_init_fdir(pf))
4268 		dev_err(dev, "could not initialize flow director\n");
4269 
4270 	/* Note: DCB init failure is non-fatal to load */
4271 	if (ice_init_pf_dcb(pf, false)) {
4272 		clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4273 		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4274 	} else {
4275 		ice_cfg_lldp_mib_change(&pf->hw, true);
4276 	}
4277 
4278 	/* print PCI link speed and width */
4279 	pcie_print_link_status(pf->pdev);
4280 
4281 probe_done:
4282 	/* ready to go, so clear down state bit */
4283 	clear_bit(__ICE_DOWN, pf->state);
4284 	return 0;
4285 
4286 err_send_version_unroll:
4287 	ice_vsi_release_all(pf);
4288 err_alloc_sw_unroll:
4289 	set_bit(__ICE_SERVICE_DIS, pf->state);
4290 	set_bit(__ICE_DOWN, pf->state);
4291 	devm_kfree(dev, pf->first_sw);
4292 err_msix_misc_unroll:
4293 	ice_free_irq_msix_misc(pf);
4294 err_init_interrupt_unroll:
4295 	ice_clear_interrupt_scheme(pf);
4296 err_init_vsi_unroll:
4297 	devm_kfree(dev, pf->vsi);
4298 err_init_pf_unroll:
4299 	ice_deinit_pf(pf);
4300 	ice_devlink_destroy_regions(pf);
4301 	ice_deinit_hw(hw);
4302 err_exit_unroll:
4303 	ice_devlink_unregister(pf);
4304 	pci_disable_pcie_error_reporting(pdev);
4305 	pci_disable_device(pdev);
4306 	return err;
4307 }
4308 
4309 /**
4310  * ice_set_wake - enable or disable Wake on LAN
4311  * @pf: pointer to the PF struct
4312  *
4313  * Simple helper for WoL control
4314  */
ice_set_wake(struct ice_pf * pf)4315 static void ice_set_wake(struct ice_pf *pf)
4316 {
4317 	struct ice_hw *hw = &pf->hw;
4318 	bool wol = pf->wol_ena;
4319 
4320 	/* clear wake state, otherwise new wake events won't fire */
4321 	wr32(hw, PFPM_WUS, U32_MAX);
4322 
4323 	/* enable / disable APM wake up, no RMW needed */
4324 	wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
4325 
4326 	/* set magic packet filter enabled */
4327 	wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
4328 }
4329 
4330 /**
4331  * ice_setup_magic_mc_wake - setup device to wake on multicast magic packet
4332  * @pf: pointer to the PF struct
4333  *
4334  * Issue firmware command to enable multicast magic wake, making
4335  * sure that any locally administered address (LAA) is used for
4336  * wake, and that PF reset doesn't undo the LAA.
4337  */
ice_setup_mc_magic_wake(struct ice_pf * pf)4338 static void ice_setup_mc_magic_wake(struct ice_pf *pf)
4339 {
4340 	struct device *dev = ice_pf_to_dev(pf);
4341 	struct ice_hw *hw = &pf->hw;
4342 	enum ice_status status;
4343 	u8 mac_addr[ETH_ALEN];
4344 	struct ice_vsi *vsi;
4345 	u8 flags;
4346 
4347 	if (!pf->wol_ena)
4348 		return;
4349 
4350 	vsi = ice_get_main_vsi(pf);
4351 	if (!vsi)
4352 		return;
4353 
4354 	/* Get current MAC address in case it's an LAA */
4355 	if (vsi->netdev)
4356 		ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
4357 	else
4358 		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
4359 
4360 	flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
4361 		ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
4362 		ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
4363 
4364 	status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
4365 	if (status)
4366 		dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %s aq_err %s\n",
4367 			ice_stat_str(status),
4368 			ice_aq_str(hw->adminq.sq_last_status));
4369 }
4370 
4371 /**
4372  * ice_remove - Device removal routine
4373  * @pdev: PCI device information struct
4374  */
ice_remove(struct pci_dev * pdev)4375 static void ice_remove(struct pci_dev *pdev)
4376 {
4377 	struct ice_pf *pf = pci_get_drvdata(pdev);
4378 	int i;
4379 
4380 	for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
4381 		if (!ice_is_reset_in_progress(pf->state))
4382 			break;
4383 		msleep(100);
4384 	}
4385 
4386 	if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
4387 		set_bit(__ICE_VF_RESETS_DISABLED, pf->state);
4388 		ice_free_vfs(pf);
4389 	}
4390 
4391 	set_bit(__ICE_DOWN, pf->state);
4392 	ice_service_task_stop(pf);
4393 
4394 	ice_aq_cancel_waiting_tasks(pf);
4395 
4396 	mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
4397 	if (!ice_is_safe_mode(pf))
4398 		ice_remove_arfs(pf);
4399 	ice_setup_mc_magic_wake(pf);
4400 	ice_vsi_release_all(pf);
4401 	ice_set_wake(pf);
4402 	ice_free_irq_msix_misc(pf);
4403 	ice_for_each_vsi(pf, i) {
4404 		if (!pf->vsi[i])
4405 			continue;
4406 		ice_vsi_free_q_vectors(pf->vsi[i]);
4407 	}
4408 	ice_deinit_pf(pf);
4409 	ice_devlink_destroy_regions(pf);
4410 	ice_deinit_hw(&pf->hw);
4411 	ice_devlink_unregister(pf);
4412 
4413 	/* Issue a PFR as part of the prescribed driver unload flow.  Do not
4414 	 * do it via ice_schedule_reset() since there is no need to rebuild
4415 	 * and the service task is already stopped.
4416 	 */
4417 	ice_reset(&pf->hw, ICE_RESET_PFR);
4418 	pci_wait_for_pending_transaction(pdev);
4419 	ice_clear_interrupt_scheme(pf);
4420 	pci_disable_pcie_error_reporting(pdev);
4421 	pci_disable_device(pdev);
4422 }
4423 
4424 /**
4425  * ice_shutdown - PCI callback for shutting down device
4426  * @pdev: PCI device information struct
4427  */
ice_shutdown(struct pci_dev * pdev)4428 static void ice_shutdown(struct pci_dev *pdev)
4429 {
4430 	struct ice_pf *pf = pci_get_drvdata(pdev);
4431 
4432 	ice_remove(pdev);
4433 
4434 	if (system_state == SYSTEM_POWER_OFF) {
4435 		pci_wake_from_d3(pdev, pf->wol_ena);
4436 		pci_set_power_state(pdev, PCI_D3hot);
4437 	}
4438 }
4439 
4440 #ifdef CONFIG_PM
4441 /**
4442  * ice_prepare_for_shutdown - prep for PCI shutdown
4443  * @pf: board private structure
4444  *
4445  * Inform or close all dependent features in prep for PCI device shutdown
4446  */
ice_prepare_for_shutdown(struct ice_pf * pf)4447 static void ice_prepare_for_shutdown(struct ice_pf *pf)
4448 {
4449 	struct ice_hw *hw = &pf->hw;
4450 	u32 v;
4451 
4452 	/* Notify VFs of impending reset */
4453 	if (ice_check_sq_alive(hw, &hw->mailboxq))
4454 		ice_vc_notify_reset(pf);
4455 
4456 	dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
4457 
4458 	/* disable the VSIs and their queues that are not already DOWN */
4459 	ice_pf_dis_all_vsi(pf, false);
4460 
4461 	ice_for_each_vsi(pf, v)
4462 		if (pf->vsi[v])
4463 			pf->vsi[v]->vsi_num = 0;
4464 
4465 	ice_shutdown_all_ctrlq(hw);
4466 }
4467 
4468 /**
4469  * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
4470  * @pf: board private structure to reinitialize
4471  *
4472  * This routine reinitialize interrupt scheme that was cleared during
4473  * power management suspend callback.
4474  *
4475  * This should be called during resume routine to re-allocate the q_vectors
4476  * and reacquire interrupts.
4477  */
ice_reinit_interrupt_scheme(struct ice_pf * pf)4478 static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
4479 {
4480 	struct device *dev = ice_pf_to_dev(pf);
4481 	int ret, v;
4482 
4483 	/* Since we clear MSIX flag during suspend, we need to
4484 	 * set it back during resume...
4485 	 */
4486 
4487 	ret = ice_init_interrupt_scheme(pf);
4488 	if (ret) {
4489 		dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
4490 		return ret;
4491 	}
4492 
4493 	/* Remap vectors and rings, after successful re-init interrupts */
4494 	ice_for_each_vsi(pf, v) {
4495 		if (!pf->vsi[v])
4496 			continue;
4497 
4498 		ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
4499 		if (ret)
4500 			goto err_reinit;
4501 		ice_vsi_map_rings_to_vectors(pf->vsi[v]);
4502 	}
4503 
4504 	ret = ice_req_irq_msix_misc(pf);
4505 	if (ret) {
4506 		dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
4507 			ret);
4508 		goto err_reinit;
4509 	}
4510 
4511 	return 0;
4512 
4513 err_reinit:
4514 	while (v--)
4515 		if (pf->vsi[v])
4516 			ice_vsi_free_q_vectors(pf->vsi[v]);
4517 
4518 	return ret;
4519 }
4520 
4521 /**
4522  * ice_suspend
4523  * @dev: generic device information structure
4524  *
4525  * Power Management callback to quiesce the device and prepare
4526  * for D3 transition.
4527  */
ice_suspend(struct device * dev)4528 static int __maybe_unused ice_suspend(struct device *dev)
4529 {
4530 	struct pci_dev *pdev = to_pci_dev(dev);
4531 	struct ice_pf *pf;
4532 	int disabled, v;
4533 
4534 	pf = pci_get_drvdata(pdev);
4535 
4536 	if (!ice_pf_state_is_nominal(pf)) {
4537 		dev_err(dev, "Device is not ready, no need to suspend it\n");
4538 		return -EBUSY;
4539 	}
4540 
4541 	/* Stop watchdog tasks until resume completion.
4542 	 * Even though it is most likely that the service task is
4543 	 * disabled if the device is suspended or down, the service task's
4544 	 * state is controlled by a different state bit, and we should
4545 	 * store and honor whatever state that bit is in at this point.
4546 	 */
4547 	disabled = ice_service_task_stop(pf);
4548 
4549 	/* Already suspended?, then there is nothing to do */
4550 	if (test_and_set_bit(__ICE_SUSPENDED, pf->state)) {
4551 		if (!disabled)
4552 			ice_service_task_restart(pf);
4553 		return 0;
4554 	}
4555 
4556 	if (test_bit(__ICE_DOWN, pf->state) ||
4557 	    ice_is_reset_in_progress(pf->state)) {
4558 		dev_err(dev, "can't suspend device in reset or already down\n");
4559 		if (!disabled)
4560 			ice_service_task_restart(pf);
4561 		return 0;
4562 	}
4563 
4564 	ice_setup_mc_magic_wake(pf);
4565 
4566 	ice_prepare_for_shutdown(pf);
4567 
4568 	ice_set_wake(pf);
4569 
4570 	/* Free vectors, clear the interrupt scheme and release IRQs
4571 	 * for proper hibernation, especially with large number of CPUs.
4572 	 * Otherwise hibernation might fail when mapping all the vectors back
4573 	 * to CPU0.
4574 	 */
4575 	ice_free_irq_msix_misc(pf);
4576 	ice_for_each_vsi(pf, v) {
4577 		if (!pf->vsi[v])
4578 			continue;
4579 		ice_vsi_free_q_vectors(pf->vsi[v]);
4580 	}
4581 	ice_free_cpu_rx_rmap(ice_get_main_vsi(pf));
4582 	ice_clear_interrupt_scheme(pf);
4583 
4584 	pci_save_state(pdev);
4585 	pci_wake_from_d3(pdev, pf->wol_ena);
4586 	pci_set_power_state(pdev, PCI_D3hot);
4587 	return 0;
4588 }
4589 
4590 /**
4591  * ice_resume - PM callback for waking up from D3
4592  * @dev: generic device information structure
4593  */
ice_resume(struct device * dev)4594 static int __maybe_unused ice_resume(struct device *dev)
4595 {
4596 	struct pci_dev *pdev = to_pci_dev(dev);
4597 	enum ice_reset_req reset_type;
4598 	struct ice_pf *pf;
4599 	struct ice_hw *hw;
4600 	int ret;
4601 
4602 	pci_set_power_state(pdev, PCI_D0);
4603 	pci_restore_state(pdev);
4604 	pci_save_state(pdev);
4605 
4606 	if (!pci_device_is_present(pdev))
4607 		return -ENODEV;
4608 
4609 	ret = pci_enable_device_mem(pdev);
4610 	if (ret) {
4611 		dev_err(dev, "Cannot enable device after suspend\n");
4612 		return ret;
4613 	}
4614 
4615 	pf = pci_get_drvdata(pdev);
4616 	hw = &pf->hw;
4617 
4618 	pf->wakeup_reason = rd32(hw, PFPM_WUS);
4619 	ice_print_wake_reason(pf);
4620 
4621 	/* We cleared the interrupt scheme when we suspended, so we need to
4622 	 * restore it now to resume device functionality.
4623 	 */
4624 	ret = ice_reinit_interrupt_scheme(pf);
4625 	if (ret)
4626 		dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
4627 
4628 	clear_bit(__ICE_DOWN, pf->state);
4629 	/* Now perform PF reset and rebuild */
4630 	reset_type = ICE_RESET_PFR;
4631 	/* re-enable service task for reset, but allow reset to schedule it */
4632 	clear_bit(__ICE_SERVICE_DIS, pf->state);
4633 
4634 	if (ice_schedule_reset(pf, reset_type))
4635 		dev_err(dev, "Reset during resume failed.\n");
4636 
4637 	clear_bit(__ICE_SUSPENDED, pf->state);
4638 	ice_service_task_restart(pf);
4639 
4640 	/* Restart the service task */
4641 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4642 
4643 	return 0;
4644 }
4645 #endif /* CONFIG_PM */
4646 
4647 /**
4648  * ice_pci_err_detected - warning that PCI error has been detected
4649  * @pdev: PCI device information struct
4650  * @err: the type of PCI error
4651  *
4652  * Called to warn that something happened on the PCI bus and the error handling
4653  * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
4654  */
4655 static pci_ers_result_t
ice_pci_err_detected(struct pci_dev * pdev,pci_channel_state_t err)4656 ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
4657 {
4658 	struct ice_pf *pf = pci_get_drvdata(pdev);
4659 
4660 	if (!pf) {
4661 		dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
4662 			__func__, err);
4663 		return PCI_ERS_RESULT_DISCONNECT;
4664 	}
4665 
4666 	if (!test_bit(__ICE_SUSPENDED, pf->state)) {
4667 		ice_service_task_stop(pf);
4668 
4669 		if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
4670 			set_bit(__ICE_PFR_REQ, pf->state);
4671 			ice_prepare_for_reset(pf);
4672 		}
4673 	}
4674 
4675 	return PCI_ERS_RESULT_NEED_RESET;
4676 }
4677 
4678 /**
4679  * ice_pci_err_slot_reset - a PCI slot reset has just happened
4680  * @pdev: PCI device information struct
4681  *
4682  * Called to determine if the driver can recover from the PCI slot reset by
4683  * using a register read to determine if the device is recoverable.
4684  */
ice_pci_err_slot_reset(struct pci_dev * pdev)4685 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
4686 {
4687 	struct ice_pf *pf = pci_get_drvdata(pdev);
4688 	pci_ers_result_t result;
4689 	int err;
4690 	u32 reg;
4691 
4692 	err = pci_enable_device_mem(pdev);
4693 	if (err) {
4694 		dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
4695 			err);
4696 		result = PCI_ERS_RESULT_DISCONNECT;
4697 	} else {
4698 		pci_set_master(pdev);
4699 		pci_restore_state(pdev);
4700 		pci_save_state(pdev);
4701 		pci_wake_from_d3(pdev, false);
4702 
4703 		/* Check for life */
4704 		reg = rd32(&pf->hw, GLGEN_RTRIG);
4705 		if (!reg)
4706 			result = PCI_ERS_RESULT_RECOVERED;
4707 		else
4708 			result = PCI_ERS_RESULT_DISCONNECT;
4709 	}
4710 
4711 	err = pci_aer_clear_nonfatal_status(pdev);
4712 	if (err)
4713 		dev_dbg(&pdev->dev, "pci_aer_clear_nonfatal_status() failed, error %d\n",
4714 			err);
4715 		/* non-fatal, continue */
4716 
4717 	return result;
4718 }
4719 
4720 /**
4721  * ice_pci_err_resume - restart operations after PCI error recovery
4722  * @pdev: PCI device information struct
4723  *
4724  * Called to allow the driver to bring things back up after PCI error and/or
4725  * reset recovery have finished
4726  */
ice_pci_err_resume(struct pci_dev * pdev)4727 static void ice_pci_err_resume(struct pci_dev *pdev)
4728 {
4729 	struct ice_pf *pf = pci_get_drvdata(pdev);
4730 
4731 	if (!pf) {
4732 		dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
4733 			__func__);
4734 		return;
4735 	}
4736 
4737 	if (test_bit(__ICE_SUSPENDED, pf->state)) {
4738 		dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
4739 			__func__);
4740 		return;
4741 	}
4742 
4743 	ice_restore_all_vfs_msi_state(pdev);
4744 
4745 	ice_do_reset(pf, ICE_RESET_PFR);
4746 	ice_service_task_restart(pf);
4747 	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
4748 }
4749 
4750 /**
4751  * ice_pci_err_reset_prepare - prepare device driver for PCI reset
4752  * @pdev: PCI device information struct
4753  */
ice_pci_err_reset_prepare(struct pci_dev * pdev)4754 static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
4755 {
4756 	struct ice_pf *pf = pci_get_drvdata(pdev);
4757 
4758 	if (!test_bit(__ICE_SUSPENDED, pf->state)) {
4759 		ice_service_task_stop(pf);
4760 
4761 		if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) {
4762 			set_bit(__ICE_PFR_REQ, pf->state);
4763 			ice_prepare_for_reset(pf);
4764 		}
4765 	}
4766 }
4767 
4768 /**
4769  * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
4770  * @pdev: PCI device information struct
4771  */
ice_pci_err_reset_done(struct pci_dev * pdev)4772 static void ice_pci_err_reset_done(struct pci_dev *pdev)
4773 {
4774 	ice_pci_err_resume(pdev);
4775 }
4776 
4777 /* ice_pci_tbl - PCI Device ID Table
4778  *
4779  * Wildcard entries (PCI_ANY_ID) should come last
4780  * Last entry must be all 0s
4781  *
4782  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
4783  *   Class, Class Mask, private data (not used) }
4784  */
4785 static const struct pci_device_id ice_pci_tbl[] = {
4786 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
4787 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
4788 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
4789 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE), 0 },
4790 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP), 0 },
4791 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP), 0 },
4792 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE), 0 },
4793 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP), 0 },
4794 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP), 0 },
4795 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T), 0 },
4796 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII), 0 },
4797 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE), 0 },
4798 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP), 0 },
4799 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP), 0 },
4800 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T), 0 },
4801 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII), 0 },
4802 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE), 0 },
4803 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP), 0 },
4804 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T), 0 },
4805 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII), 0 },
4806 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE), 0 },
4807 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP), 0 },
4808 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T), 0 },
4809 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE), 0 },
4810 	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP), 0 },
4811 	/* required last entry */
4812 	{ 0, }
4813 };
4814 MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
4815 
4816 static __maybe_unused SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
4817 
4818 static const struct pci_error_handlers ice_pci_err_handler = {
4819 	.error_detected = ice_pci_err_detected,
4820 	.slot_reset = ice_pci_err_slot_reset,
4821 	.reset_prepare = ice_pci_err_reset_prepare,
4822 	.reset_done = ice_pci_err_reset_done,
4823 	.resume = ice_pci_err_resume
4824 };
4825 
4826 static struct pci_driver ice_driver = {
4827 	.name = KBUILD_MODNAME,
4828 	.id_table = ice_pci_tbl,
4829 	.probe = ice_probe,
4830 	.remove = ice_remove,
4831 #ifdef CONFIG_PM
4832 	.driver.pm = &ice_pm_ops,
4833 #endif /* CONFIG_PM */
4834 	.shutdown = ice_shutdown,
4835 	.sriov_configure = ice_sriov_configure,
4836 	.err_handler = &ice_pci_err_handler
4837 };
4838 
4839 /**
4840  * ice_module_init - Driver registration routine
4841  *
4842  * ice_module_init is the first routine called when the driver is
4843  * loaded. All it does is register with the PCI subsystem.
4844  */
ice_module_init(void)4845 static int __init ice_module_init(void)
4846 {
4847 	int status;
4848 
4849 	pr_info("%s\n", ice_driver_string);
4850 	pr_info("%s\n", ice_copyright);
4851 
4852 	ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME);
4853 	if (!ice_wq) {
4854 		pr_err("Failed to create workqueue\n");
4855 		return -ENOMEM;
4856 	}
4857 
4858 	status = pci_register_driver(&ice_driver);
4859 	if (status) {
4860 		pr_err("failed to register PCI driver, err %d\n", status);
4861 		destroy_workqueue(ice_wq);
4862 	}
4863 
4864 	return status;
4865 }
4866 module_init(ice_module_init);
4867 
4868 /**
4869  * ice_module_exit - Driver exit cleanup routine
4870  *
4871  * ice_module_exit is called just before the driver is removed
4872  * from memory.
4873  */
ice_module_exit(void)4874 static void __exit ice_module_exit(void)
4875 {
4876 	pci_unregister_driver(&ice_driver);
4877 	destroy_workqueue(ice_wq);
4878 	pr_info("module unloaded\n");
4879 }
4880 module_exit(ice_module_exit);
4881 
4882 /**
4883  * ice_set_mac_address - NDO callback to set MAC address
4884  * @netdev: network interface device structure
4885  * @pi: pointer to an address structure
4886  *
4887  * Returns 0 on success, negative on failure
4888  */
ice_set_mac_address(struct net_device * netdev,void * pi)4889 static int ice_set_mac_address(struct net_device *netdev, void *pi)
4890 {
4891 	struct ice_netdev_priv *np = netdev_priv(netdev);
4892 	struct ice_vsi *vsi = np->vsi;
4893 	struct ice_pf *pf = vsi->back;
4894 	struct ice_hw *hw = &pf->hw;
4895 	struct sockaddr *addr = pi;
4896 	enum ice_status status;
4897 	u8 old_mac[ETH_ALEN];
4898 	u8 flags = 0;
4899 	int err = 0;
4900 	u8 *mac;
4901 
4902 	mac = (u8 *)addr->sa_data;
4903 
4904 	if (!is_valid_ether_addr(mac))
4905 		return -EADDRNOTAVAIL;
4906 
4907 	if (ether_addr_equal(netdev->dev_addr, mac)) {
4908 		netdev_dbg(netdev, "already using mac %pM\n", mac);
4909 		return 0;
4910 	}
4911 
4912 	if (test_bit(__ICE_DOWN, pf->state) ||
4913 	    ice_is_reset_in_progress(pf->state)) {
4914 		netdev_err(netdev, "can't set mac %pM. device not ready\n",
4915 			   mac);
4916 		return -EBUSY;
4917 	}
4918 
4919 	netif_addr_lock_bh(netdev);
4920 	ether_addr_copy(old_mac, netdev->dev_addr);
4921 	/* change the netdev's MAC address */
4922 	memcpy(netdev->dev_addr, mac, netdev->addr_len);
4923 	netif_addr_unlock_bh(netdev);
4924 
4925 	/* Clean up old MAC filter. Not an error if old filter doesn't exist */
4926 	status = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
4927 	if (status && status != ICE_ERR_DOES_NOT_EXIST) {
4928 		err = -EADDRNOTAVAIL;
4929 		goto err_update_filters;
4930 	}
4931 
4932 	/* Add filter for new MAC. If filter exists, return success */
4933 	status = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
4934 	if (status == ICE_ERR_ALREADY_EXISTS)
4935 		/* Although this MAC filter is already present in hardware it's
4936 		 * possible in some cases (e.g. bonding) that dev_addr was
4937 		 * modified outside of the driver and needs to be restored back
4938 		 * to this value.
4939 		 */
4940 		netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
4941 	else if (status)
4942 		/* error if the new filter addition failed */
4943 		err = -EADDRNOTAVAIL;
4944 
4945 err_update_filters:
4946 	if (err) {
4947 		netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
4948 			   mac);
4949 		netif_addr_lock_bh(netdev);
4950 		ether_addr_copy(netdev->dev_addr, old_mac);
4951 		netif_addr_unlock_bh(netdev);
4952 		return err;
4953 	}
4954 
4955 	netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
4956 		   netdev->dev_addr);
4957 
4958 	/* write new MAC address to the firmware */
4959 	flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
4960 	status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
4961 	if (status) {
4962 		netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %s\n",
4963 			   mac, ice_stat_str(status));
4964 	}
4965 	return 0;
4966 }
4967 
4968 /**
4969  * ice_set_rx_mode - NDO callback to set the netdev filters
4970  * @netdev: network interface device structure
4971  */
ice_set_rx_mode(struct net_device * netdev)4972 static void ice_set_rx_mode(struct net_device *netdev)
4973 {
4974 	struct ice_netdev_priv *np = netdev_priv(netdev);
4975 	struct ice_vsi *vsi = np->vsi;
4976 
4977 	if (!vsi)
4978 		return;
4979 
4980 	/* Set the flags to synchronize filters
4981 	 * ndo_set_rx_mode may be triggered even without a change in netdev
4982 	 * flags
4983 	 */
4984 	set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
4985 	set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
4986 	set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
4987 
4988 	/* schedule our worker thread which will take care of
4989 	 * applying the new filter changes
4990 	 */
4991 	ice_service_task_schedule(vsi->back);
4992 }
4993 
4994 /**
4995  * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
4996  * @netdev: network interface device structure
4997  * @queue_index: Queue ID
4998  * @maxrate: maximum bandwidth in Mbps
4999  */
5000 static int
ice_set_tx_maxrate(struct net_device * netdev,int queue_index,u32 maxrate)5001 ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
5002 {
5003 	struct ice_netdev_priv *np = netdev_priv(netdev);
5004 	struct ice_vsi *vsi = np->vsi;
5005 	enum ice_status status;
5006 	u16 q_handle;
5007 	u8 tc;
5008 
5009 	/* Validate maxrate requested is within permitted range */
5010 	if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
5011 		netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
5012 			   maxrate, queue_index);
5013 		return -EINVAL;
5014 	}
5015 
5016 	q_handle = vsi->tx_rings[queue_index]->q_handle;
5017 	tc = ice_dcb_get_tc(vsi, queue_index);
5018 
5019 	/* Set BW back to default, when user set maxrate to 0 */
5020 	if (!maxrate)
5021 		status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
5022 					       q_handle, ICE_MAX_BW);
5023 	else
5024 		status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
5025 					  q_handle, ICE_MAX_BW, maxrate * 1000);
5026 	if (status) {
5027 		netdev_err(netdev, "Unable to set Tx max rate, error %s\n",
5028 			   ice_stat_str(status));
5029 		return -EIO;
5030 	}
5031 
5032 	return 0;
5033 }
5034 
5035 /**
5036  * ice_fdb_add - add an entry to the hardware database
5037  * @ndm: the input from the stack
5038  * @tb: pointer to array of nladdr (unused)
5039  * @dev: the net device pointer
5040  * @addr: the MAC address entry being added
5041  * @vid: VLAN ID
5042  * @flags: instructions from stack about fdb operation
5043  * @extack: netlink extended ack
5044  */
5045 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)5046 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
5047 	    struct net_device *dev, const unsigned char *addr, u16 vid,
5048 	    u16 flags, struct netlink_ext_ack __always_unused *extack)
5049 {
5050 	int err;
5051 
5052 	if (vid) {
5053 		netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
5054 		return -EINVAL;
5055 	}
5056 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
5057 		netdev_err(dev, "FDB only supports static addresses\n");
5058 		return -EINVAL;
5059 	}
5060 
5061 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
5062 		err = dev_uc_add_excl(dev, addr);
5063 	else if (is_multicast_ether_addr(addr))
5064 		err = dev_mc_add_excl(dev, addr);
5065 	else
5066 		err = -EINVAL;
5067 
5068 	/* Only return duplicate errors if NLM_F_EXCL is set */
5069 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
5070 		err = 0;
5071 
5072 	return err;
5073 }
5074 
5075 /**
5076  * ice_fdb_del - delete an entry from the hardware database
5077  * @ndm: the input from the stack
5078  * @tb: pointer to array of nladdr (unused)
5079  * @dev: the net device pointer
5080  * @addr: the MAC address entry being added
5081  * @vid: VLAN ID
5082  */
5083 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)5084 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
5085 	    struct net_device *dev, const unsigned char *addr,
5086 	    __always_unused u16 vid)
5087 {
5088 	int err;
5089 
5090 	if (ndm->ndm_state & NUD_PERMANENT) {
5091 		netdev_err(dev, "FDB only supports static addresses\n");
5092 		return -EINVAL;
5093 	}
5094 
5095 	if (is_unicast_ether_addr(addr))
5096 		err = dev_uc_del(dev, addr);
5097 	else if (is_multicast_ether_addr(addr))
5098 		err = dev_mc_del(dev, addr);
5099 	else
5100 		err = -EINVAL;
5101 
5102 	return err;
5103 }
5104 
5105 /**
5106  * ice_set_features - set the netdev feature flags
5107  * @netdev: ptr to the netdev being adjusted
5108  * @features: the feature set that the stack is suggesting
5109  */
5110 static int
ice_set_features(struct net_device * netdev,netdev_features_t features)5111 ice_set_features(struct net_device *netdev, netdev_features_t features)
5112 {
5113 	struct ice_netdev_priv *np = netdev_priv(netdev);
5114 	struct ice_vsi *vsi = np->vsi;
5115 	struct ice_pf *pf = vsi->back;
5116 	int ret = 0;
5117 
5118 	/* Don't set any netdev advanced features with device in Safe Mode */
5119 	if (ice_is_safe_mode(vsi->back)) {
5120 		dev_err(ice_pf_to_dev(vsi->back), "Device is in Safe Mode - not enabling advanced netdev features\n");
5121 		return ret;
5122 	}
5123 
5124 	/* Do not change setting during reset */
5125 	if (ice_is_reset_in_progress(pf->state)) {
5126 		dev_err(ice_pf_to_dev(vsi->back), "Device is resetting, changing advanced netdev features temporarily unavailable.\n");
5127 		return -EBUSY;
5128 	}
5129 
5130 	/* Multiple features can be changed in one call so keep features in
5131 	 * separate if/else statements to guarantee each feature is checked
5132 	 */
5133 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
5134 		ret = ice_vsi_manage_rss_lut(vsi, true);
5135 	else if (!(features & NETIF_F_RXHASH) &&
5136 		 netdev->features & NETIF_F_RXHASH)
5137 		ret = ice_vsi_manage_rss_lut(vsi, false);
5138 
5139 	if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
5140 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
5141 		ret = ice_vsi_manage_vlan_stripping(vsi, true);
5142 	else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
5143 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
5144 		ret = ice_vsi_manage_vlan_stripping(vsi, false);
5145 
5146 	if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
5147 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
5148 		ret = ice_vsi_manage_vlan_insertion(vsi);
5149 	else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
5150 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
5151 		ret = ice_vsi_manage_vlan_insertion(vsi);
5152 
5153 	if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
5154 	    !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
5155 		ret = ice_cfg_vlan_pruning(vsi, true, false);
5156 	else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
5157 		 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER))
5158 		ret = ice_cfg_vlan_pruning(vsi, false, false);
5159 
5160 	if ((features & NETIF_F_NTUPLE) &&
5161 	    !(netdev->features & NETIF_F_NTUPLE)) {
5162 		ice_vsi_manage_fdir(vsi, true);
5163 		ice_init_arfs(vsi);
5164 	} else if (!(features & NETIF_F_NTUPLE) &&
5165 		 (netdev->features & NETIF_F_NTUPLE)) {
5166 		ice_vsi_manage_fdir(vsi, false);
5167 		ice_clear_arfs(vsi);
5168 	}
5169 
5170 	return ret;
5171 }
5172 
5173 /**
5174  * ice_vsi_vlan_setup - Setup VLAN offload properties on a VSI
5175  * @vsi: VSI to setup VLAN properties for
5176  */
ice_vsi_vlan_setup(struct ice_vsi * vsi)5177 static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
5178 {
5179 	int ret = 0;
5180 
5181 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
5182 		ret = ice_vsi_manage_vlan_stripping(vsi, true);
5183 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
5184 		ret = ice_vsi_manage_vlan_insertion(vsi);
5185 
5186 	return ret;
5187 }
5188 
5189 /**
5190  * ice_vsi_cfg - Setup the VSI
5191  * @vsi: the VSI being configured
5192  *
5193  * Return 0 on success and negative value on error
5194  */
ice_vsi_cfg(struct ice_vsi * vsi)5195 int ice_vsi_cfg(struct ice_vsi *vsi)
5196 {
5197 	int err;
5198 
5199 	if (vsi->netdev) {
5200 		ice_set_rx_mode(vsi->netdev);
5201 
5202 		err = ice_vsi_vlan_setup(vsi);
5203 
5204 		if (err)
5205 			return err;
5206 	}
5207 	ice_vsi_cfg_dcb_rings(vsi);
5208 
5209 	err = ice_vsi_cfg_lan_txqs(vsi);
5210 	if (!err && ice_is_xdp_ena_vsi(vsi))
5211 		err = ice_vsi_cfg_xdp_txqs(vsi);
5212 	if (!err)
5213 		err = ice_vsi_cfg_rxqs(vsi);
5214 
5215 	return err;
5216 }
5217 
5218 /**
5219  * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5220  * @vsi: the VSI being configured
5221  */
ice_napi_enable_all(struct ice_vsi * vsi)5222 static void ice_napi_enable_all(struct ice_vsi *vsi)
5223 {
5224 	int q_idx;
5225 
5226 	if (!vsi->netdev)
5227 		return;
5228 
5229 	ice_for_each_q_vector(vsi, q_idx) {
5230 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
5231 
5232 		if (q_vector->rx.ring || q_vector->tx.ring)
5233 			napi_enable(&q_vector->napi);
5234 	}
5235 }
5236 
5237 /**
5238  * ice_up_complete - Finish the last steps of bringing up a connection
5239  * @vsi: The VSI being configured
5240  *
5241  * Return 0 on success and negative value on error
5242  */
ice_up_complete(struct ice_vsi * vsi)5243 static int ice_up_complete(struct ice_vsi *vsi)
5244 {
5245 	struct ice_pf *pf = vsi->back;
5246 	int err;
5247 
5248 	ice_vsi_cfg_msix(vsi);
5249 
5250 	/* Enable only Rx rings, Tx rings were enabled by the FW when the
5251 	 * Tx queue group list was configured and the context bits were
5252 	 * programmed using ice_vsi_cfg_txqs
5253 	 */
5254 	err = ice_vsi_start_all_rx_rings(vsi);
5255 	if (err)
5256 		return err;
5257 
5258 	clear_bit(__ICE_DOWN, vsi->state);
5259 	ice_napi_enable_all(vsi);
5260 	ice_vsi_ena_irq(vsi);
5261 
5262 	if (vsi->port_info &&
5263 	    (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
5264 	    vsi->netdev) {
5265 		ice_print_link_msg(vsi, true);
5266 		netif_tx_start_all_queues(vsi->netdev);
5267 		netif_carrier_on(vsi->netdev);
5268 	}
5269 
5270 	/* clear this now, and the first stats read will be used as baseline */
5271 	vsi->stat_offsets_loaded = false;
5272 
5273 	ice_service_task_schedule(pf);
5274 
5275 	return 0;
5276 }
5277 
5278 /**
5279  * ice_up - Bring the connection back up after being down
5280  * @vsi: VSI being configured
5281  */
ice_up(struct ice_vsi * vsi)5282 int ice_up(struct ice_vsi *vsi)
5283 {
5284 	int err;
5285 
5286 	err = ice_vsi_cfg(vsi);
5287 	if (!err)
5288 		err = ice_up_complete(vsi);
5289 
5290 	return err;
5291 }
5292 
5293 /**
5294  * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
5295  * @ring: Tx or Rx ring to read stats from
5296  * @pkts: packets stats counter
5297  * @bytes: bytes stats counter
5298  *
5299  * This function fetches stats from the ring considering the atomic operations
5300  * that needs to be performed to read u64 values in 32 bit machine.
5301  */
5302 static void
ice_fetch_u64_stats_per_ring(struct ice_ring * ring,u64 * pkts,u64 * bytes)5303 ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts, u64 *bytes)
5304 {
5305 	unsigned int start;
5306 	*pkts = 0;
5307 	*bytes = 0;
5308 
5309 	if (!ring)
5310 		return;
5311 	do {
5312 		start = u64_stats_fetch_begin_irq(&ring->syncp);
5313 		*pkts = ring->stats.pkts;
5314 		*bytes = ring->stats.bytes;
5315 	} while (u64_stats_fetch_retry_irq(&ring->syncp, start));
5316 }
5317 
5318 /**
5319  * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
5320  * @vsi: the VSI to be updated
5321  * @rings: rings to work on
5322  * @count: number of rings
5323  */
5324 static void
ice_update_vsi_tx_ring_stats(struct ice_vsi * vsi,struct ice_ring ** rings,u16 count)5325 ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi, struct ice_ring **rings,
5326 			     u16 count)
5327 {
5328 	struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
5329 	u16 i;
5330 
5331 	for (i = 0; i < count; i++) {
5332 		struct ice_ring *ring;
5333 		u64 pkts, bytes;
5334 
5335 		ring = READ_ONCE(rings[i]);
5336 		ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
5337 		vsi_stats->tx_packets += pkts;
5338 		vsi_stats->tx_bytes += bytes;
5339 		vsi->tx_restart += ring->tx_stats.restart_q;
5340 		vsi->tx_busy += ring->tx_stats.tx_busy;
5341 		vsi->tx_linearize += ring->tx_stats.tx_linearize;
5342 	}
5343 }
5344 
5345 /**
5346  * ice_update_vsi_ring_stats - Update VSI stats counters
5347  * @vsi: the VSI to be updated
5348  */
ice_update_vsi_ring_stats(struct ice_vsi * vsi)5349 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
5350 {
5351 	struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
5352 	struct ice_ring *ring;
5353 	u64 pkts, bytes;
5354 	int i;
5355 
5356 	/* reset netdev stats */
5357 	vsi_stats->tx_packets = 0;
5358 	vsi_stats->tx_bytes = 0;
5359 	vsi_stats->rx_packets = 0;
5360 	vsi_stats->rx_bytes = 0;
5361 
5362 	/* reset non-netdev (extended) stats */
5363 	vsi->tx_restart = 0;
5364 	vsi->tx_busy = 0;
5365 	vsi->tx_linearize = 0;
5366 	vsi->rx_buf_failed = 0;
5367 	vsi->rx_page_failed = 0;
5368 	vsi->rx_gro_dropped = 0;
5369 
5370 	rcu_read_lock();
5371 
5372 	/* update Tx rings counters */
5373 	ice_update_vsi_tx_ring_stats(vsi, vsi->tx_rings, vsi->num_txq);
5374 
5375 	/* update Rx rings counters */
5376 	ice_for_each_rxq(vsi, i) {
5377 		ring = READ_ONCE(vsi->rx_rings[i]);
5378 		ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
5379 		vsi_stats->rx_packets += pkts;
5380 		vsi_stats->rx_bytes += bytes;
5381 		vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
5382 		vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
5383 		vsi->rx_gro_dropped += ring->rx_stats.gro_dropped;
5384 	}
5385 
5386 	/* update XDP Tx rings counters */
5387 	if (ice_is_xdp_ena_vsi(vsi))
5388 		ice_update_vsi_tx_ring_stats(vsi, vsi->xdp_rings,
5389 					     vsi->num_xdp_txq);
5390 
5391 	rcu_read_unlock();
5392 }
5393 
5394 /**
5395  * ice_update_vsi_stats - Update VSI stats counters
5396  * @vsi: the VSI to be updated
5397  */
ice_update_vsi_stats(struct ice_vsi * vsi)5398 void ice_update_vsi_stats(struct ice_vsi *vsi)
5399 {
5400 	struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
5401 	struct ice_eth_stats *cur_es = &vsi->eth_stats;
5402 	struct ice_pf *pf = vsi->back;
5403 
5404 	if (test_bit(__ICE_DOWN, vsi->state) ||
5405 	    test_bit(__ICE_CFG_BUSY, pf->state))
5406 		return;
5407 
5408 	/* get stats as recorded by Tx/Rx rings */
5409 	ice_update_vsi_ring_stats(vsi);
5410 
5411 	/* get VSI stats as recorded by the hardware */
5412 	ice_update_eth_stats(vsi);
5413 
5414 	cur_ns->tx_errors = cur_es->tx_errors;
5415 	cur_ns->rx_dropped = cur_es->rx_discards + vsi->rx_gro_dropped;
5416 	cur_ns->tx_dropped = cur_es->tx_discards;
5417 	cur_ns->multicast = cur_es->rx_multicast;
5418 
5419 	/* update some more netdev stats if this is main VSI */
5420 	if (vsi->type == ICE_VSI_PF) {
5421 		cur_ns->rx_crc_errors = pf->stats.crc_errors;
5422 		cur_ns->rx_errors = pf->stats.crc_errors +
5423 				    pf->stats.illegal_bytes +
5424 				    pf->stats.rx_len_errors +
5425 				    pf->stats.rx_undersize +
5426 				    pf->hw_csum_rx_error +
5427 				    pf->stats.rx_jabber +
5428 				    pf->stats.rx_fragments +
5429 				    pf->stats.rx_oversize;
5430 		cur_ns->rx_length_errors = pf->stats.rx_len_errors;
5431 		/* record drops from the port level */
5432 		cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
5433 	}
5434 }
5435 
5436 /**
5437  * ice_update_pf_stats - Update PF port stats counters
5438  * @pf: PF whose stats needs to be updated
5439  */
ice_update_pf_stats(struct ice_pf * pf)5440 void ice_update_pf_stats(struct ice_pf *pf)
5441 {
5442 	struct ice_hw_port_stats *prev_ps, *cur_ps;
5443 	struct ice_hw *hw = &pf->hw;
5444 	u16 fd_ctr_base;
5445 	u8 port;
5446 
5447 	port = hw->port_info->lport;
5448 	prev_ps = &pf->stats_prev;
5449 	cur_ps = &pf->stats;
5450 
5451 	ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
5452 			  &prev_ps->eth.rx_bytes,
5453 			  &cur_ps->eth.rx_bytes);
5454 
5455 	ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
5456 			  &prev_ps->eth.rx_unicast,
5457 			  &cur_ps->eth.rx_unicast);
5458 
5459 	ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
5460 			  &prev_ps->eth.rx_multicast,
5461 			  &cur_ps->eth.rx_multicast);
5462 
5463 	ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
5464 			  &prev_ps->eth.rx_broadcast,
5465 			  &cur_ps->eth.rx_broadcast);
5466 
5467 	ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
5468 			  &prev_ps->eth.rx_discards,
5469 			  &cur_ps->eth.rx_discards);
5470 
5471 	ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
5472 			  &prev_ps->eth.tx_bytes,
5473 			  &cur_ps->eth.tx_bytes);
5474 
5475 	ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
5476 			  &prev_ps->eth.tx_unicast,
5477 			  &cur_ps->eth.tx_unicast);
5478 
5479 	ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
5480 			  &prev_ps->eth.tx_multicast,
5481 			  &cur_ps->eth.tx_multicast);
5482 
5483 	ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
5484 			  &prev_ps->eth.tx_broadcast,
5485 			  &cur_ps->eth.tx_broadcast);
5486 
5487 	ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
5488 			  &prev_ps->tx_dropped_link_down,
5489 			  &cur_ps->tx_dropped_link_down);
5490 
5491 	ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
5492 			  &prev_ps->rx_size_64, &cur_ps->rx_size_64);
5493 
5494 	ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
5495 			  &prev_ps->rx_size_127, &cur_ps->rx_size_127);
5496 
5497 	ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
5498 			  &prev_ps->rx_size_255, &cur_ps->rx_size_255);
5499 
5500 	ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
5501 			  &prev_ps->rx_size_511, &cur_ps->rx_size_511);
5502 
5503 	ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
5504 			  &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
5505 
5506 	ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
5507 			  &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
5508 
5509 	ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
5510 			  &prev_ps->rx_size_big, &cur_ps->rx_size_big);
5511 
5512 	ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
5513 			  &prev_ps->tx_size_64, &cur_ps->tx_size_64);
5514 
5515 	ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
5516 			  &prev_ps->tx_size_127, &cur_ps->tx_size_127);
5517 
5518 	ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
5519 			  &prev_ps->tx_size_255, &cur_ps->tx_size_255);
5520 
5521 	ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
5522 			  &prev_ps->tx_size_511, &cur_ps->tx_size_511);
5523 
5524 	ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
5525 			  &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
5526 
5527 	ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
5528 			  &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
5529 
5530 	ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
5531 			  &prev_ps->tx_size_big, &cur_ps->tx_size_big);
5532 
5533 	fd_ctr_base = hw->fd_ctr_base;
5534 
5535 	ice_stat_update40(hw,
5536 			  GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
5537 			  pf->stat_prev_loaded, &prev_ps->fd_sb_match,
5538 			  &cur_ps->fd_sb_match);
5539 	ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
5540 			  &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
5541 
5542 	ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
5543 			  &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
5544 
5545 	ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
5546 			  &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
5547 
5548 	ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
5549 			  &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
5550 
5551 	ice_update_dcb_stats(pf);
5552 
5553 	ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
5554 			  &prev_ps->crc_errors, &cur_ps->crc_errors);
5555 
5556 	ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
5557 			  &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
5558 
5559 	ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
5560 			  &prev_ps->mac_local_faults,
5561 			  &cur_ps->mac_local_faults);
5562 
5563 	ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
5564 			  &prev_ps->mac_remote_faults,
5565 			  &cur_ps->mac_remote_faults);
5566 
5567 	ice_stat_update32(hw, GLPRT_RLEC(port), pf->stat_prev_loaded,
5568 			  &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
5569 
5570 	ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
5571 			  &prev_ps->rx_undersize, &cur_ps->rx_undersize);
5572 
5573 	ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
5574 			  &prev_ps->rx_fragments, &cur_ps->rx_fragments);
5575 
5576 	ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
5577 			  &prev_ps->rx_oversize, &cur_ps->rx_oversize);
5578 
5579 	ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
5580 			  &prev_ps->rx_jabber, &cur_ps->rx_jabber);
5581 
5582 	cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
5583 
5584 	pf->stat_prev_loaded = true;
5585 }
5586 
5587 /**
5588  * ice_get_stats64 - get statistics for network device structure
5589  * @netdev: network interface device structure
5590  * @stats: main device statistics structure
5591  */
5592 static
ice_get_stats64(struct net_device * netdev,struct rtnl_link_stats64 * stats)5593 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
5594 {
5595 	struct ice_netdev_priv *np = netdev_priv(netdev);
5596 	struct rtnl_link_stats64 *vsi_stats;
5597 	struct ice_vsi *vsi = np->vsi;
5598 
5599 	vsi_stats = &vsi->net_stats;
5600 
5601 	if (!vsi->num_txq || !vsi->num_rxq)
5602 		return;
5603 
5604 	/* netdev packet/byte stats come from ring counter. These are obtained
5605 	 * by summing up ring counters (done by ice_update_vsi_ring_stats).
5606 	 * But, only call the update routine and read the registers if VSI is
5607 	 * not down.
5608 	 */
5609 	if (!test_bit(__ICE_DOWN, vsi->state))
5610 		ice_update_vsi_ring_stats(vsi);
5611 	stats->tx_packets = vsi_stats->tx_packets;
5612 	stats->tx_bytes = vsi_stats->tx_bytes;
5613 	stats->rx_packets = vsi_stats->rx_packets;
5614 	stats->rx_bytes = vsi_stats->rx_bytes;
5615 
5616 	/* The rest of the stats can be read from the hardware but instead we
5617 	 * just return values that the watchdog task has already obtained from
5618 	 * the hardware.
5619 	 */
5620 	stats->multicast = vsi_stats->multicast;
5621 	stats->tx_errors = vsi_stats->tx_errors;
5622 	stats->tx_dropped = vsi_stats->tx_dropped;
5623 	stats->rx_errors = vsi_stats->rx_errors;
5624 	stats->rx_dropped = vsi_stats->rx_dropped;
5625 	stats->rx_crc_errors = vsi_stats->rx_crc_errors;
5626 	stats->rx_length_errors = vsi_stats->rx_length_errors;
5627 }
5628 
5629 /**
5630  * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5631  * @vsi: VSI having NAPI disabled
5632  */
ice_napi_disable_all(struct ice_vsi * vsi)5633 static void ice_napi_disable_all(struct ice_vsi *vsi)
5634 {
5635 	int q_idx;
5636 
5637 	if (!vsi->netdev)
5638 		return;
5639 
5640 	ice_for_each_q_vector(vsi, q_idx) {
5641 		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
5642 
5643 		if (q_vector->rx.ring || q_vector->tx.ring)
5644 			napi_disable(&q_vector->napi);
5645 	}
5646 }
5647 
5648 /**
5649  * ice_down - Shutdown the connection
5650  * @vsi: The VSI being stopped
5651  */
ice_down(struct ice_vsi * vsi)5652 int ice_down(struct ice_vsi *vsi)
5653 {
5654 	int i, tx_err, rx_err, link_err = 0;
5655 
5656 	/* Caller of this function is expected to set the
5657 	 * vsi->state __ICE_DOWN bit
5658 	 */
5659 	if (vsi->netdev) {
5660 		netif_carrier_off(vsi->netdev);
5661 		netif_tx_disable(vsi->netdev);
5662 	}
5663 
5664 	ice_vsi_dis_irq(vsi);
5665 
5666 	tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
5667 	if (tx_err)
5668 		netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
5669 			   vsi->vsi_num, tx_err);
5670 	if (!tx_err && ice_is_xdp_ena_vsi(vsi)) {
5671 		tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
5672 		if (tx_err)
5673 			netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
5674 				   vsi->vsi_num, tx_err);
5675 	}
5676 
5677 	rx_err = ice_vsi_stop_all_rx_rings(vsi);
5678 	if (rx_err)
5679 		netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
5680 			   vsi->vsi_num, rx_err);
5681 
5682 	ice_napi_disable_all(vsi);
5683 
5684 	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
5685 		link_err = ice_force_phys_link_state(vsi, false);
5686 		if (link_err)
5687 			netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
5688 				   vsi->vsi_num, link_err);
5689 	}
5690 
5691 	ice_for_each_txq(vsi, i)
5692 		ice_clean_tx_ring(vsi->tx_rings[i]);
5693 
5694 	ice_for_each_rxq(vsi, i)
5695 		ice_clean_rx_ring(vsi->rx_rings[i]);
5696 
5697 	if (tx_err || rx_err || link_err) {
5698 		netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
5699 			   vsi->vsi_num, vsi->vsw->sw_id);
5700 		return -EIO;
5701 	}
5702 
5703 	return 0;
5704 }
5705 
5706 /**
5707  * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
5708  * @vsi: VSI having resources allocated
5709  *
5710  * Return 0 on success, negative on failure
5711  */
ice_vsi_setup_tx_rings(struct ice_vsi * vsi)5712 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
5713 {
5714 	int i, err = 0;
5715 
5716 	if (!vsi->num_txq) {
5717 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
5718 			vsi->vsi_num);
5719 		return -EINVAL;
5720 	}
5721 
5722 	ice_for_each_txq(vsi, i) {
5723 		struct ice_ring *ring = vsi->tx_rings[i];
5724 
5725 		if (!ring)
5726 			return -EINVAL;
5727 
5728 		ring->netdev = vsi->netdev;
5729 		err = ice_setup_tx_ring(ring);
5730 		if (err)
5731 			break;
5732 	}
5733 
5734 	return err;
5735 }
5736 
5737 /**
5738  * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
5739  * @vsi: VSI having resources allocated
5740  *
5741  * Return 0 on success, negative on failure
5742  */
ice_vsi_setup_rx_rings(struct ice_vsi * vsi)5743 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
5744 {
5745 	int i, err = 0;
5746 
5747 	if (!vsi->num_rxq) {
5748 		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
5749 			vsi->vsi_num);
5750 		return -EINVAL;
5751 	}
5752 
5753 	ice_for_each_rxq(vsi, i) {
5754 		struct ice_ring *ring = vsi->rx_rings[i];
5755 
5756 		if (!ring)
5757 			return -EINVAL;
5758 
5759 		ring->netdev = vsi->netdev;
5760 		err = ice_setup_rx_ring(ring);
5761 		if (err)
5762 			break;
5763 	}
5764 
5765 	return err;
5766 }
5767 
5768 /**
5769  * ice_vsi_open_ctrl - open control VSI for use
5770  * @vsi: the VSI to open
5771  *
5772  * Initialization of the Control VSI
5773  *
5774  * Returns 0 on success, negative value on error
5775  */
ice_vsi_open_ctrl(struct ice_vsi * vsi)5776 int ice_vsi_open_ctrl(struct ice_vsi *vsi)
5777 {
5778 	char int_name[ICE_INT_NAME_STR_LEN];
5779 	struct ice_pf *pf = vsi->back;
5780 	struct device *dev;
5781 	int err;
5782 
5783 	dev = ice_pf_to_dev(pf);
5784 	/* allocate descriptors */
5785 	err = ice_vsi_setup_tx_rings(vsi);
5786 	if (err)
5787 		goto err_setup_tx;
5788 
5789 	err = ice_vsi_setup_rx_rings(vsi);
5790 	if (err)
5791 		goto err_setup_rx;
5792 
5793 	err = ice_vsi_cfg(vsi);
5794 	if (err)
5795 		goto err_setup_rx;
5796 
5797 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
5798 		 dev_driver_string(dev), dev_name(dev));
5799 	err = ice_vsi_req_irq_msix(vsi, int_name);
5800 	if (err)
5801 		goto err_setup_rx;
5802 
5803 	ice_vsi_cfg_msix(vsi);
5804 
5805 	err = ice_vsi_start_all_rx_rings(vsi);
5806 	if (err)
5807 		goto err_up_complete;
5808 
5809 	clear_bit(__ICE_DOWN, vsi->state);
5810 	ice_vsi_ena_irq(vsi);
5811 
5812 	return 0;
5813 
5814 err_up_complete:
5815 	ice_down(vsi);
5816 err_setup_rx:
5817 	ice_vsi_free_rx_rings(vsi);
5818 err_setup_tx:
5819 	ice_vsi_free_tx_rings(vsi);
5820 
5821 	return err;
5822 }
5823 
5824 /**
5825  * ice_vsi_open - Called when a network interface is made active
5826  * @vsi: the VSI to open
5827  *
5828  * Initialization of the VSI
5829  *
5830  * Returns 0 on success, negative value on error
5831  */
ice_vsi_open(struct ice_vsi * vsi)5832 static int ice_vsi_open(struct ice_vsi *vsi)
5833 {
5834 	char int_name[ICE_INT_NAME_STR_LEN];
5835 	struct ice_pf *pf = vsi->back;
5836 	int err;
5837 
5838 	/* allocate descriptors */
5839 	err = ice_vsi_setup_tx_rings(vsi);
5840 	if (err)
5841 		goto err_setup_tx;
5842 
5843 	err = ice_vsi_setup_rx_rings(vsi);
5844 	if (err)
5845 		goto err_setup_rx;
5846 
5847 	err = ice_vsi_cfg(vsi);
5848 	if (err)
5849 		goto err_setup_rx;
5850 
5851 	snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
5852 		 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
5853 	err = ice_vsi_req_irq_msix(vsi, int_name);
5854 	if (err)
5855 		goto err_setup_rx;
5856 
5857 	/* Notify the stack of the actual queue counts. */
5858 	err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
5859 	if (err)
5860 		goto err_set_qs;
5861 
5862 	err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
5863 	if (err)
5864 		goto err_set_qs;
5865 
5866 	err = ice_up_complete(vsi);
5867 	if (err)
5868 		goto err_up_complete;
5869 
5870 	return 0;
5871 
5872 err_up_complete:
5873 	ice_down(vsi);
5874 err_set_qs:
5875 	ice_vsi_free_irq(vsi);
5876 err_setup_rx:
5877 	ice_vsi_free_rx_rings(vsi);
5878 err_setup_tx:
5879 	ice_vsi_free_tx_rings(vsi);
5880 
5881 	return err;
5882 }
5883 
5884 /**
5885  * ice_vsi_release_all - Delete all VSIs
5886  * @pf: PF from which all VSIs are being removed
5887  */
ice_vsi_release_all(struct ice_pf * pf)5888 static void ice_vsi_release_all(struct ice_pf *pf)
5889 {
5890 	int err, i;
5891 
5892 	if (!pf->vsi)
5893 		return;
5894 
5895 	ice_for_each_vsi(pf, i) {
5896 		if (!pf->vsi[i])
5897 			continue;
5898 
5899 		err = ice_vsi_release(pf->vsi[i]);
5900 		if (err)
5901 			dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
5902 				i, err, pf->vsi[i]->vsi_num);
5903 	}
5904 }
5905 
5906 /**
5907  * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
5908  * @pf: pointer to the PF instance
5909  * @type: VSI type to rebuild
5910  *
5911  * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
5912  */
ice_vsi_rebuild_by_type(struct ice_pf * pf,enum ice_vsi_type type)5913 static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
5914 {
5915 	struct device *dev = ice_pf_to_dev(pf);
5916 	enum ice_status status;
5917 	int i, err;
5918 
5919 	ice_for_each_vsi(pf, i) {
5920 		struct ice_vsi *vsi = pf->vsi[i];
5921 
5922 		if (!vsi || vsi->type != type)
5923 			continue;
5924 
5925 		/* rebuild the VSI */
5926 		err = ice_vsi_rebuild(vsi, true);
5927 		if (err) {
5928 			dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
5929 				err, vsi->idx, ice_vsi_type_str(type));
5930 			return err;
5931 		}
5932 
5933 		/* replay filters for the VSI */
5934 		status = ice_replay_vsi(&pf->hw, vsi->idx);
5935 		if (status) {
5936 			dev_err(dev, "replay VSI failed, status %s, VSI index %d, type %s\n",
5937 				ice_stat_str(status), vsi->idx,
5938 				ice_vsi_type_str(type));
5939 			return -EIO;
5940 		}
5941 
5942 		/* Re-map HW VSI number, using VSI handle that has been
5943 		 * previously validated in ice_replay_vsi() call above
5944 		 */
5945 		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
5946 
5947 		/* enable the VSI */
5948 		err = ice_ena_vsi(vsi, false);
5949 		if (err) {
5950 			dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
5951 				err, vsi->idx, ice_vsi_type_str(type));
5952 			return err;
5953 		}
5954 
5955 		dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
5956 			 ice_vsi_type_str(type));
5957 	}
5958 
5959 	return 0;
5960 }
5961 
5962 /**
5963  * ice_update_pf_netdev_link - Update PF netdev link status
5964  * @pf: pointer to the PF instance
5965  */
ice_update_pf_netdev_link(struct ice_pf * pf)5966 static void ice_update_pf_netdev_link(struct ice_pf *pf)
5967 {
5968 	bool link_up;
5969 	int i;
5970 
5971 	ice_for_each_vsi(pf, i) {
5972 		struct ice_vsi *vsi = pf->vsi[i];
5973 
5974 		if (!vsi || vsi->type != ICE_VSI_PF)
5975 			return;
5976 
5977 		ice_get_link_status(pf->vsi[i]->port_info, &link_up);
5978 		if (link_up) {
5979 			netif_carrier_on(pf->vsi[i]->netdev);
5980 			netif_tx_wake_all_queues(pf->vsi[i]->netdev);
5981 		} else {
5982 			netif_carrier_off(pf->vsi[i]->netdev);
5983 			netif_tx_stop_all_queues(pf->vsi[i]->netdev);
5984 		}
5985 	}
5986 }
5987 
5988 /**
5989  * ice_rebuild - rebuild after reset
5990  * @pf: PF to rebuild
5991  * @reset_type: type of reset
5992  *
5993  * Do not rebuild VF VSI in this flow because that is already handled via
5994  * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
5995  * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
5996  * to reset/rebuild all the VF VSI twice.
5997  */
ice_rebuild(struct ice_pf * pf,enum ice_reset_req reset_type)5998 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
5999 {
6000 	struct device *dev = ice_pf_to_dev(pf);
6001 	struct ice_hw *hw = &pf->hw;
6002 	enum ice_status ret;
6003 	int err;
6004 
6005 	if (test_bit(__ICE_DOWN, pf->state))
6006 		goto clear_recovery;
6007 
6008 	dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
6009 
6010 	ret = ice_init_all_ctrlq(hw);
6011 	if (ret) {
6012 		dev_err(dev, "control queues init failed %s\n",
6013 			ice_stat_str(ret));
6014 		goto err_init_ctrlq;
6015 	}
6016 
6017 	/* if DDP was previously loaded successfully */
6018 	if (!ice_is_safe_mode(pf)) {
6019 		/* reload the SW DB of filter tables */
6020 		if (reset_type == ICE_RESET_PFR)
6021 			ice_fill_blk_tbls(hw);
6022 		else
6023 			/* Reload DDP Package after CORER/GLOBR reset */
6024 			ice_load_pkg(NULL, pf);
6025 	}
6026 
6027 	ret = ice_clear_pf_cfg(hw);
6028 	if (ret) {
6029 		dev_err(dev, "clear PF configuration failed %s\n",
6030 			ice_stat_str(ret));
6031 		goto err_init_ctrlq;
6032 	}
6033 
6034 	if (pf->first_sw->dflt_vsi_ena)
6035 		dev_info(dev, "Clearing default VSI, re-enable after reset completes\n");
6036 	/* clear the default VSI configuration if it exists */
6037 	pf->first_sw->dflt_vsi = NULL;
6038 	pf->first_sw->dflt_vsi_ena = false;
6039 
6040 	ice_clear_pxe_mode(hw);
6041 
6042 	ret = ice_get_caps(hw);
6043 	if (ret) {
6044 		dev_err(dev, "ice_get_caps failed %s\n", ice_stat_str(ret));
6045 		goto err_init_ctrlq;
6046 	}
6047 
6048 	ret = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
6049 	if (ret) {
6050 		dev_err(dev, "set_mac_cfg failed %s\n", ice_stat_str(ret));
6051 		goto err_init_ctrlq;
6052 	}
6053 
6054 	err = ice_sched_init_port(hw->port_info);
6055 	if (err)
6056 		goto err_sched_init_port;
6057 
6058 	/* start misc vector */
6059 	err = ice_req_irq_msix_misc(pf);
6060 	if (err) {
6061 		dev_err(dev, "misc vector setup failed: %d\n", err);
6062 		goto err_sched_init_port;
6063 	}
6064 
6065 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
6066 		wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
6067 		if (!rd32(hw, PFQF_FD_SIZE)) {
6068 			u16 unused, guar, b_effort;
6069 
6070 			guar = hw->func_caps.fd_fltr_guar;
6071 			b_effort = hw->func_caps.fd_fltr_best_effort;
6072 
6073 			/* force guaranteed filter pool for PF */
6074 			ice_alloc_fd_guar_item(hw, &unused, guar);
6075 			/* force shared filter pool for PF */
6076 			ice_alloc_fd_shrd_item(hw, &unused, b_effort);
6077 		}
6078 	}
6079 
6080 	if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
6081 		ice_dcb_rebuild(pf);
6082 
6083 	/* rebuild PF VSI */
6084 	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
6085 	if (err) {
6086 		dev_err(dev, "PF VSI rebuild failed: %d\n", err);
6087 		goto err_vsi_rebuild;
6088 	}
6089 
6090 	/* If Flow Director is active */
6091 	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
6092 		err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
6093 		if (err) {
6094 			dev_err(dev, "control VSI rebuild failed: %d\n", err);
6095 			goto err_vsi_rebuild;
6096 		}
6097 
6098 		/* replay HW Flow Director recipes */
6099 		if (hw->fdir_prof)
6100 			ice_fdir_replay_flows(hw);
6101 
6102 		/* replay Flow Director filters */
6103 		ice_fdir_replay_fltrs(pf);
6104 
6105 		ice_rebuild_arfs(pf);
6106 	}
6107 
6108 	ice_update_pf_netdev_link(pf);
6109 
6110 	/* tell the firmware we are up */
6111 	ret = ice_send_version(pf);
6112 	if (ret) {
6113 		dev_err(dev, "Rebuild failed due to error sending driver version: %s\n",
6114 			ice_stat_str(ret));
6115 		goto err_vsi_rebuild;
6116 	}
6117 
6118 	ice_replay_post(hw);
6119 
6120 	/* if we get here, reset flow is successful */
6121 	clear_bit(__ICE_RESET_FAILED, pf->state);
6122 	return;
6123 
6124 err_vsi_rebuild:
6125 err_sched_init_port:
6126 	ice_sched_cleanup_all(hw);
6127 err_init_ctrlq:
6128 	ice_shutdown_all_ctrlq(hw);
6129 	set_bit(__ICE_RESET_FAILED, pf->state);
6130 clear_recovery:
6131 	/* set this bit in PF state to control service task scheduling */
6132 	set_bit(__ICE_NEEDS_RESTART, pf->state);
6133 	dev_err(dev, "Rebuild failed, unload and reload driver\n");
6134 }
6135 
6136 /**
6137  * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
6138  * @vsi: Pointer to VSI structure
6139  */
ice_max_xdp_frame_size(struct ice_vsi * vsi)6140 static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
6141 {
6142 	if (PAGE_SIZE >= 8192 || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
6143 		return ICE_RXBUF_2048 - XDP_PACKET_HEADROOM;
6144 	else
6145 		return ICE_RXBUF_3072;
6146 }
6147 
6148 /**
6149  * ice_change_mtu - NDO callback to change the MTU
6150  * @netdev: network interface device structure
6151  * @new_mtu: new value for maximum frame size
6152  *
6153  * Returns 0 on success, negative on failure
6154  */
ice_change_mtu(struct net_device * netdev,int new_mtu)6155 static int ice_change_mtu(struct net_device *netdev, int new_mtu)
6156 {
6157 	struct ice_netdev_priv *np = netdev_priv(netdev);
6158 	struct ice_vsi *vsi = np->vsi;
6159 	struct ice_pf *pf = vsi->back;
6160 	u8 count = 0;
6161 
6162 	if (new_mtu == (int)netdev->mtu) {
6163 		netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
6164 		return 0;
6165 	}
6166 
6167 	if (ice_is_xdp_ena_vsi(vsi)) {
6168 		int frame_size = ice_max_xdp_frame_size(vsi);
6169 
6170 		if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
6171 			netdev_err(netdev, "max MTU for XDP usage is %d\n",
6172 				   frame_size - ICE_ETH_PKT_HDR_PAD);
6173 			return -EINVAL;
6174 		}
6175 	}
6176 
6177 	if (new_mtu < (int)netdev->min_mtu) {
6178 		netdev_err(netdev, "new MTU invalid. min_mtu is %d\n",
6179 			   netdev->min_mtu);
6180 		return -EINVAL;
6181 	} else if (new_mtu > (int)netdev->max_mtu) {
6182 		netdev_err(netdev, "new MTU invalid. max_mtu is %d\n",
6183 			   netdev->min_mtu);
6184 		return -EINVAL;
6185 	}
6186 	/* if a reset is in progress, wait for some time for it to complete */
6187 	do {
6188 		if (ice_is_reset_in_progress(pf->state)) {
6189 			count++;
6190 			usleep_range(1000, 2000);
6191 		} else {
6192 			break;
6193 		}
6194 
6195 	} while (count < 100);
6196 
6197 	if (count == 100) {
6198 		netdev_err(netdev, "can't change MTU. Device is busy\n");
6199 		return -EBUSY;
6200 	}
6201 
6202 	netdev->mtu = (unsigned int)new_mtu;
6203 
6204 	/* if VSI is up, bring it down and then back up */
6205 	if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
6206 		int err;
6207 
6208 		err = ice_down(vsi);
6209 		if (err) {
6210 			netdev_err(netdev, "change MTU if_up err %d\n", err);
6211 			return err;
6212 		}
6213 
6214 		err = ice_up(vsi);
6215 		if (err) {
6216 			netdev_err(netdev, "change MTU if_up err %d\n", err);
6217 			return err;
6218 		}
6219 	}
6220 
6221 	netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
6222 	return 0;
6223 }
6224 
6225 /**
6226  * ice_aq_str - convert AQ err code to a string
6227  * @aq_err: the AQ error code to convert
6228  */
ice_aq_str(enum ice_aq_err aq_err)6229 const char *ice_aq_str(enum ice_aq_err aq_err)
6230 {
6231 	switch (aq_err) {
6232 	case ICE_AQ_RC_OK:
6233 		return "OK";
6234 	case ICE_AQ_RC_EPERM:
6235 		return "ICE_AQ_RC_EPERM";
6236 	case ICE_AQ_RC_ENOENT:
6237 		return "ICE_AQ_RC_ENOENT";
6238 	case ICE_AQ_RC_ENOMEM:
6239 		return "ICE_AQ_RC_ENOMEM";
6240 	case ICE_AQ_RC_EBUSY:
6241 		return "ICE_AQ_RC_EBUSY";
6242 	case ICE_AQ_RC_EEXIST:
6243 		return "ICE_AQ_RC_EEXIST";
6244 	case ICE_AQ_RC_EINVAL:
6245 		return "ICE_AQ_RC_EINVAL";
6246 	case ICE_AQ_RC_ENOSPC:
6247 		return "ICE_AQ_RC_ENOSPC";
6248 	case ICE_AQ_RC_ENOSYS:
6249 		return "ICE_AQ_RC_ENOSYS";
6250 	case ICE_AQ_RC_EMODE:
6251 		return "ICE_AQ_RC_EMODE";
6252 	case ICE_AQ_RC_ENOSEC:
6253 		return "ICE_AQ_RC_ENOSEC";
6254 	case ICE_AQ_RC_EBADSIG:
6255 		return "ICE_AQ_RC_EBADSIG";
6256 	case ICE_AQ_RC_ESVN:
6257 		return "ICE_AQ_RC_ESVN";
6258 	case ICE_AQ_RC_EBADMAN:
6259 		return "ICE_AQ_RC_EBADMAN";
6260 	case ICE_AQ_RC_EBADBUF:
6261 		return "ICE_AQ_RC_EBADBUF";
6262 	}
6263 
6264 	return "ICE_AQ_RC_UNKNOWN";
6265 }
6266 
6267 /**
6268  * ice_stat_str - convert status err code to a string
6269  * @stat_err: the status error code to convert
6270  */
ice_stat_str(enum ice_status stat_err)6271 const char *ice_stat_str(enum ice_status stat_err)
6272 {
6273 	switch (stat_err) {
6274 	case ICE_SUCCESS:
6275 		return "OK";
6276 	case ICE_ERR_PARAM:
6277 		return "ICE_ERR_PARAM";
6278 	case ICE_ERR_NOT_IMPL:
6279 		return "ICE_ERR_NOT_IMPL";
6280 	case ICE_ERR_NOT_READY:
6281 		return "ICE_ERR_NOT_READY";
6282 	case ICE_ERR_NOT_SUPPORTED:
6283 		return "ICE_ERR_NOT_SUPPORTED";
6284 	case ICE_ERR_BAD_PTR:
6285 		return "ICE_ERR_BAD_PTR";
6286 	case ICE_ERR_INVAL_SIZE:
6287 		return "ICE_ERR_INVAL_SIZE";
6288 	case ICE_ERR_DEVICE_NOT_SUPPORTED:
6289 		return "ICE_ERR_DEVICE_NOT_SUPPORTED";
6290 	case ICE_ERR_RESET_FAILED:
6291 		return "ICE_ERR_RESET_FAILED";
6292 	case ICE_ERR_FW_API_VER:
6293 		return "ICE_ERR_FW_API_VER";
6294 	case ICE_ERR_NO_MEMORY:
6295 		return "ICE_ERR_NO_MEMORY";
6296 	case ICE_ERR_CFG:
6297 		return "ICE_ERR_CFG";
6298 	case ICE_ERR_OUT_OF_RANGE:
6299 		return "ICE_ERR_OUT_OF_RANGE";
6300 	case ICE_ERR_ALREADY_EXISTS:
6301 		return "ICE_ERR_ALREADY_EXISTS";
6302 	case ICE_ERR_NVM_CHECKSUM:
6303 		return "ICE_ERR_NVM_CHECKSUM";
6304 	case ICE_ERR_BUF_TOO_SHORT:
6305 		return "ICE_ERR_BUF_TOO_SHORT";
6306 	case ICE_ERR_NVM_BLANK_MODE:
6307 		return "ICE_ERR_NVM_BLANK_MODE";
6308 	case ICE_ERR_IN_USE:
6309 		return "ICE_ERR_IN_USE";
6310 	case ICE_ERR_MAX_LIMIT:
6311 		return "ICE_ERR_MAX_LIMIT";
6312 	case ICE_ERR_RESET_ONGOING:
6313 		return "ICE_ERR_RESET_ONGOING";
6314 	case ICE_ERR_HW_TABLE:
6315 		return "ICE_ERR_HW_TABLE";
6316 	case ICE_ERR_DOES_NOT_EXIST:
6317 		return "ICE_ERR_DOES_NOT_EXIST";
6318 	case ICE_ERR_FW_DDP_MISMATCH:
6319 		return "ICE_ERR_FW_DDP_MISMATCH";
6320 	case ICE_ERR_AQ_ERROR:
6321 		return "ICE_ERR_AQ_ERROR";
6322 	case ICE_ERR_AQ_TIMEOUT:
6323 		return "ICE_ERR_AQ_TIMEOUT";
6324 	case ICE_ERR_AQ_FULL:
6325 		return "ICE_ERR_AQ_FULL";
6326 	case ICE_ERR_AQ_NO_WORK:
6327 		return "ICE_ERR_AQ_NO_WORK";
6328 	case ICE_ERR_AQ_EMPTY:
6329 		return "ICE_ERR_AQ_EMPTY";
6330 	case ICE_ERR_AQ_FW_CRITICAL:
6331 		return "ICE_ERR_AQ_FW_CRITICAL";
6332 	}
6333 
6334 	return "ICE_ERR_UNKNOWN";
6335 }
6336 
6337 /**
6338  * ice_set_rss - Set RSS keys and lut
6339  * @vsi: Pointer to VSI structure
6340  * @seed: RSS hash seed
6341  * @lut: Lookup table
6342  * @lut_size: Lookup table size
6343  *
6344  * Returns 0 on success, negative on failure
6345  */
ice_set_rss(struct ice_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)6346 int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
6347 {
6348 	struct ice_pf *pf = vsi->back;
6349 	struct ice_hw *hw = &pf->hw;
6350 	enum ice_status status;
6351 	struct device *dev;
6352 
6353 	dev = ice_pf_to_dev(pf);
6354 	if (seed) {
6355 		struct ice_aqc_get_set_rss_keys *buf =
6356 				  (struct ice_aqc_get_set_rss_keys *)seed;
6357 
6358 		status = ice_aq_set_rss_key(hw, vsi->idx, buf);
6359 
6360 		if (status) {
6361 			dev_err(dev, "Cannot set RSS key, err %s aq_err %s\n",
6362 				ice_stat_str(status),
6363 				ice_aq_str(hw->adminq.sq_last_status));
6364 			return -EIO;
6365 		}
6366 	}
6367 
6368 	if (lut) {
6369 		status = ice_aq_set_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
6370 					    lut, lut_size);
6371 		if (status) {
6372 			dev_err(dev, "Cannot set RSS lut, err %s aq_err %s\n",
6373 				ice_stat_str(status),
6374 				ice_aq_str(hw->adminq.sq_last_status));
6375 			return -EIO;
6376 		}
6377 	}
6378 
6379 	return 0;
6380 }
6381 
6382 /**
6383  * ice_get_rss - Get RSS keys and lut
6384  * @vsi: Pointer to VSI structure
6385  * @seed: Buffer to store the keys
6386  * @lut: Buffer to store the lookup table entries
6387  * @lut_size: Size of buffer to store the lookup table entries
6388  *
6389  * Returns 0 on success, negative on failure
6390  */
ice_get_rss(struct ice_vsi * vsi,u8 * seed,u8 * lut,u16 lut_size)6391 int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
6392 {
6393 	struct ice_pf *pf = vsi->back;
6394 	struct ice_hw *hw = &pf->hw;
6395 	enum ice_status status;
6396 	struct device *dev;
6397 
6398 	dev = ice_pf_to_dev(pf);
6399 	if (seed) {
6400 		struct ice_aqc_get_set_rss_keys *buf =
6401 				  (struct ice_aqc_get_set_rss_keys *)seed;
6402 
6403 		status = ice_aq_get_rss_key(hw, vsi->idx, buf);
6404 		if (status) {
6405 			dev_err(dev, "Cannot get RSS key, err %s aq_err %s\n",
6406 				ice_stat_str(status),
6407 				ice_aq_str(hw->adminq.sq_last_status));
6408 			return -EIO;
6409 		}
6410 	}
6411 
6412 	if (lut) {
6413 		status = ice_aq_get_rss_lut(hw, vsi->idx, vsi->rss_lut_type,
6414 					    lut, lut_size);
6415 		if (status) {
6416 			dev_err(dev, "Cannot get RSS lut, err %s aq_err %s\n",
6417 				ice_stat_str(status),
6418 				ice_aq_str(hw->adminq.sq_last_status));
6419 			return -EIO;
6420 		}
6421 	}
6422 
6423 	return 0;
6424 }
6425 
6426 /**
6427  * ice_bridge_getlink - Get the hardware bridge mode
6428  * @skb: skb buff
6429  * @pid: process ID
6430  * @seq: RTNL message seq
6431  * @dev: the netdev being configured
6432  * @filter_mask: filter mask passed in
6433  * @nlflags: netlink flags passed in
6434  *
6435  * Return the bridge mode (VEB/VEPA)
6436  */
6437 static int
ice_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u32 filter_mask,int nlflags)6438 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
6439 		   struct net_device *dev, u32 filter_mask, int nlflags)
6440 {
6441 	struct ice_netdev_priv *np = netdev_priv(dev);
6442 	struct ice_vsi *vsi = np->vsi;
6443 	struct ice_pf *pf = vsi->back;
6444 	u16 bmode;
6445 
6446 	bmode = pf->first_sw->bridge_mode;
6447 
6448 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
6449 				       filter_mask, NULL);
6450 }
6451 
6452 /**
6453  * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
6454  * @vsi: Pointer to VSI structure
6455  * @bmode: Hardware bridge mode (VEB/VEPA)
6456  *
6457  * Returns 0 on success, negative on failure
6458  */
ice_vsi_update_bridge_mode(struct ice_vsi * vsi,u16 bmode)6459 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
6460 {
6461 	struct ice_aqc_vsi_props *vsi_props;
6462 	struct ice_hw *hw = &vsi->back->hw;
6463 	struct ice_vsi_ctx *ctxt;
6464 	enum ice_status status;
6465 	int ret = 0;
6466 
6467 	vsi_props = &vsi->info;
6468 
6469 	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
6470 	if (!ctxt)
6471 		return -ENOMEM;
6472 
6473 	ctxt->info = vsi->info;
6474 
6475 	if (bmode == BRIDGE_MODE_VEB)
6476 		/* change from VEPA to VEB mode */
6477 		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
6478 	else
6479 		/* change from VEB to VEPA mode */
6480 		ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
6481 	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
6482 
6483 	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
6484 	if (status) {
6485 		dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %s aq_err %s\n",
6486 			bmode, ice_stat_str(status),
6487 			ice_aq_str(hw->adminq.sq_last_status));
6488 		ret = -EIO;
6489 		goto out;
6490 	}
6491 	/* Update sw flags for book keeping */
6492 	vsi_props->sw_flags = ctxt->info.sw_flags;
6493 
6494 out:
6495 	kfree(ctxt);
6496 	return ret;
6497 }
6498 
6499 /**
6500  * ice_bridge_setlink - Set the hardware bridge mode
6501  * @dev: the netdev being configured
6502  * @nlh: RTNL message
6503  * @flags: bridge setlink flags
6504  * @extack: netlink extended ack
6505  *
6506  * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
6507  * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
6508  * not already set for all VSIs connected to this switch. And also update the
6509  * unicast switch filter rules for the corresponding switch of the netdev.
6510  */
6511 static int
ice_bridge_setlink(struct net_device * dev,struct nlmsghdr * nlh,u16 __always_unused flags,struct netlink_ext_ack __always_unused * extack)6512 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
6513 		   u16 __always_unused flags,
6514 		   struct netlink_ext_ack __always_unused *extack)
6515 {
6516 	struct ice_netdev_priv *np = netdev_priv(dev);
6517 	struct ice_pf *pf = np->vsi->back;
6518 	struct nlattr *attr, *br_spec;
6519 	struct ice_hw *hw = &pf->hw;
6520 	enum ice_status status;
6521 	struct ice_sw *pf_sw;
6522 	int rem, v, err = 0;
6523 
6524 	pf_sw = pf->first_sw;
6525 	/* find the attribute in the netlink message */
6526 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
6527 
6528 	nla_for_each_nested(attr, br_spec, rem) {
6529 		__u16 mode;
6530 
6531 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
6532 			continue;
6533 		mode = nla_get_u16(attr);
6534 		if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
6535 			return -EINVAL;
6536 		/* Continue  if bridge mode is not being flipped */
6537 		if (mode == pf_sw->bridge_mode)
6538 			continue;
6539 		/* Iterates through the PF VSI list and update the loopback
6540 		 * mode of the VSI
6541 		 */
6542 		ice_for_each_vsi(pf, v) {
6543 			if (!pf->vsi[v])
6544 				continue;
6545 			err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
6546 			if (err)
6547 				return err;
6548 		}
6549 
6550 		hw->evb_veb = (mode == BRIDGE_MODE_VEB);
6551 		/* Update the unicast switch filter rules for the corresponding
6552 		 * switch of the netdev
6553 		 */
6554 		status = ice_update_sw_rule_bridge_mode(hw);
6555 		if (status) {
6556 			netdev_err(dev, "switch rule update failed, mode = %d err %s aq_err %s\n",
6557 				   mode, ice_stat_str(status),
6558 				   ice_aq_str(hw->adminq.sq_last_status));
6559 			/* revert hw->evb_veb */
6560 			hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
6561 			return -EIO;
6562 		}
6563 
6564 		pf_sw->bridge_mode = mode;
6565 	}
6566 
6567 	return 0;
6568 }
6569 
6570 /**
6571  * ice_tx_timeout - Respond to a Tx Hang
6572  * @netdev: network interface device structure
6573  * @txqueue: Tx queue
6574  */
ice_tx_timeout(struct net_device * netdev,unsigned int txqueue)6575 static void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
6576 {
6577 	struct ice_netdev_priv *np = netdev_priv(netdev);
6578 	struct ice_ring *tx_ring = NULL;
6579 	struct ice_vsi *vsi = np->vsi;
6580 	struct ice_pf *pf = vsi->back;
6581 	u32 i;
6582 
6583 	pf->tx_timeout_count++;
6584 
6585 	/* Check if PFC is enabled for the TC to which the queue belongs
6586 	 * to. If yes then Tx timeout is not caused by a hung queue, no
6587 	 * need to reset and rebuild
6588 	 */
6589 	if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
6590 		dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
6591 			 txqueue);
6592 		return;
6593 	}
6594 
6595 	/* now that we have an index, find the tx_ring struct */
6596 	for (i = 0; i < vsi->num_txq; i++)
6597 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
6598 			if (txqueue == vsi->tx_rings[i]->q_index) {
6599 				tx_ring = vsi->tx_rings[i];
6600 				break;
6601 			}
6602 
6603 	/* Reset recovery level if enough time has elapsed after last timeout.
6604 	 * Also ensure no new reset action happens before next timeout period.
6605 	 */
6606 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
6607 		pf->tx_timeout_recovery_level = 1;
6608 	else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
6609 				       netdev->watchdog_timeo)))
6610 		return;
6611 
6612 	if (tx_ring) {
6613 		struct ice_hw *hw = &pf->hw;
6614 		u32 head, val = 0;
6615 
6616 		head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])) &
6617 			QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
6618 		/* Read interrupt register */
6619 		val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
6620 
6621 		netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
6622 			    vsi->vsi_num, txqueue, tx_ring->next_to_clean,
6623 			    head, tx_ring->next_to_use, val);
6624 	}
6625 
6626 	pf->tx_timeout_last_recovery = jiffies;
6627 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
6628 		    pf->tx_timeout_recovery_level, txqueue);
6629 
6630 	switch (pf->tx_timeout_recovery_level) {
6631 	case 1:
6632 		set_bit(__ICE_PFR_REQ, pf->state);
6633 		break;
6634 	case 2:
6635 		set_bit(__ICE_CORER_REQ, pf->state);
6636 		break;
6637 	case 3:
6638 		set_bit(__ICE_GLOBR_REQ, pf->state);
6639 		break;
6640 	default:
6641 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
6642 		set_bit(__ICE_DOWN, pf->state);
6643 		set_bit(__ICE_NEEDS_RESTART, vsi->state);
6644 		set_bit(__ICE_SERVICE_DIS, pf->state);
6645 		break;
6646 	}
6647 
6648 	ice_service_task_schedule(pf);
6649 	pf->tx_timeout_recovery_level++;
6650 }
6651 
6652 /**
6653  * ice_open - Called when a network interface becomes active
6654  * @netdev: network interface device structure
6655  *
6656  * The open entry point is called when a network interface is made
6657  * active by the system (IFF_UP). At this point all resources needed
6658  * for transmit and receive operations are allocated, the interrupt
6659  * handler is registered with the OS, the netdev watchdog is enabled,
6660  * and the stack is notified that the interface is ready.
6661  *
6662  * Returns 0 on success, negative value on failure
6663  */
ice_open(struct net_device * netdev)6664 int ice_open(struct net_device *netdev)
6665 {
6666 	struct ice_netdev_priv *np = netdev_priv(netdev);
6667 	struct ice_pf *pf = np->vsi->back;
6668 
6669 	if (ice_is_reset_in_progress(pf->state)) {
6670 		netdev_err(netdev, "can't open net device while reset is in progress");
6671 		return -EBUSY;
6672 	}
6673 
6674 	return ice_open_internal(netdev);
6675 }
6676 
6677 /**
6678  * ice_open_internal - Called when a network interface becomes active
6679  * @netdev: network interface device structure
6680  *
6681  * Internal ice_open implementation. Should not be used directly except for ice_open and reset
6682  * handling routine
6683  *
6684  * Returns 0 on success, negative value on failure
6685  */
ice_open_internal(struct net_device * netdev)6686 int ice_open_internal(struct net_device *netdev)
6687 {
6688 	struct ice_netdev_priv *np = netdev_priv(netdev);
6689 	struct ice_vsi *vsi = np->vsi;
6690 	struct ice_pf *pf = vsi->back;
6691 	struct ice_port_info *pi;
6692 	int err;
6693 
6694 	if (test_bit(__ICE_NEEDS_RESTART, pf->state)) {
6695 		netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
6696 		return -EIO;
6697 	}
6698 
6699 	if (test_bit(__ICE_DOWN, pf->state)) {
6700 		netdev_err(netdev, "device is not ready yet\n");
6701 		return -EBUSY;
6702 	}
6703 
6704 	netif_carrier_off(netdev);
6705 
6706 	pi = vsi->port_info;
6707 	err = ice_update_link_info(pi);
6708 	if (err) {
6709 		netdev_err(netdev, "Failed to get link info, error %d\n",
6710 			   err);
6711 		return err;
6712 	}
6713 
6714 	/* Set PHY if there is media, otherwise, turn off PHY */
6715 	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
6716 		clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
6717 		if (!test_bit(__ICE_PHY_INIT_COMPLETE, pf->state)) {
6718 			err = ice_init_phy_user_cfg(pi);
6719 			if (err) {
6720 				netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
6721 					   err);
6722 				return err;
6723 			}
6724 		}
6725 
6726 		err = ice_configure_phy(vsi);
6727 		if (err) {
6728 			netdev_err(netdev, "Failed to set physical link up, error %d\n",
6729 				   err);
6730 			return err;
6731 		}
6732 	} else {
6733 		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
6734 		err = ice_aq_set_link_restart_an(pi, false, NULL);
6735 		if (err) {
6736 			netdev_err(netdev, "Failed to set PHY state, VSI %d error %d\n",
6737 				   vsi->vsi_num, err);
6738 			return err;
6739 		}
6740 	}
6741 
6742 	err = ice_vsi_open(vsi);
6743 	if (err)
6744 		netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
6745 			   vsi->vsi_num, vsi->vsw->sw_id);
6746 
6747 	/* Update existing tunnels information */
6748 	udp_tunnel_get_rx_info(netdev);
6749 
6750 	return err;
6751 }
6752 
6753 /**
6754  * ice_stop - Disables a network interface
6755  * @netdev: network interface device structure
6756  *
6757  * The stop entry point is called when an interface is de-activated by the OS,
6758  * and the netdevice enters the DOWN state. The hardware is still under the
6759  * driver's control, but the netdev interface is disabled.
6760  *
6761  * Returns success only - not allowed to fail
6762  */
ice_stop(struct net_device * netdev)6763 int ice_stop(struct net_device *netdev)
6764 {
6765 	struct ice_netdev_priv *np = netdev_priv(netdev);
6766 	struct ice_vsi *vsi = np->vsi;
6767 	struct ice_pf *pf = vsi->back;
6768 
6769 	if (ice_is_reset_in_progress(pf->state)) {
6770 		netdev_err(netdev, "can't stop net device while reset is in progress");
6771 		return -EBUSY;
6772 	}
6773 
6774 	ice_vsi_close(vsi);
6775 
6776 	return 0;
6777 }
6778 
6779 /**
6780  * ice_features_check - Validate encapsulated packet conforms to limits
6781  * @skb: skb buffer
6782  * @netdev: This port's netdev
6783  * @features: Offload features that the stack believes apply
6784  */
6785 static netdev_features_t
ice_features_check(struct sk_buff * skb,struct net_device __always_unused * netdev,netdev_features_t features)6786 ice_features_check(struct sk_buff *skb,
6787 		   struct net_device __always_unused *netdev,
6788 		   netdev_features_t features)
6789 {
6790 	size_t len;
6791 
6792 	/* No point in doing any of this if neither checksum nor GSO are
6793 	 * being requested for this frame. We can rule out both by just
6794 	 * checking for CHECKSUM_PARTIAL
6795 	 */
6796 	if (skb->ip_summed != CHECKSUM_PARTIAL)
6797 		return features;
6798 
6799 	/* We cannot support GSO if the MSS is going to be less than
6800 	 * 64 bytes. If it is then we need to drop support for GSO.
6801 	 */
6802 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
6803 		features &= ~NETIF_F_GSO_MASK;
6804 
6805 	len = skb_network_header(skb) - skb->data;
6806 	if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
6807 		goto out_rm_features;
6808 
6809 	len = skb_transport_header(skb) - skb_network_header(skb);
6810 	if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
6811 		goto out_rm_features;
6812 
6813 	if (skb->encapsulation) {
6814 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
6815 		if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
6816 			goto out_rm_features;
6817 
6818 		len = skb_inner_transport_header(skb) -
6819 		      skb_inner_network_header(skb);
6820 		if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
6821 			goto out_rm_features;
6822 	}
6823 
6824 	return features;
6825 out_rm_features:
6826 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
6827 }
6828 
6829 static const struct net_device_ops ice_netdev_safe_mode_ops = {
6830 	.ndo_open = ice_open,
6831 	.ndo_stop = ice_stop,
6832 	.ndo_start_xmit = ice_start_xmit,
6833 	.ndo_set_mac_address = ice_set_mac_address,
6834 	.ndo_validate_addr = eth_validate_addr,
6835 	.ndo_change_mtu = ice_change_mtu,
6836 	.ndo_get_stats64 = ice_get_stats64,
6837 	.ndo_tx_timeout = ice_tx_timeout,
6838 	.ndo_bpf = ice_xdp_safe_mode,
6839 };
6840 
6841 static const struct net_device_ops ice_netdev_ops = {
6842 	.ndo_open = ice_open,
6843 	.ndo_stop = ice_stop,
6844 	.ndo_start_xmit = ice_start_xmit,
6845 	.ndo_features_check = ice_features_check,
6846 	.ndo_set_rx_mode = ice_set_rx_mode,
6847 	.ndo_set_mac_address = ice_set_mac_address,
6848 	.ndo_validate_addr = eth_validate_addr,
6849 	.ndo_change_mtu = ice_change_mtu,
6850 	.ndo_get_stats64 = ice_get_stats64,
6851 	.ndo_set_tx_maxrate = ice_set_tx_maxrate,
6852 	.ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
6853 	.ndo_set_vf_mac = ice_set_vf_mac,
6854 	.ndo_get_vf_config = ice_get_vf_cfg,
6855 	.ndo_set_vf_trust = ice_set_vf_trust,
6856 	.ndo_set_vf_vlan = ice_set_vf_port_vlan,
6857 	.ndo_set_vf_link_state = ice_set_vf_link_state,
6858 	.ndo_get_vf_stats = ice_get_vf_stats,
6859 	.ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
6860 	.ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
6861 	.ndo_set_features = ice_set_features,
6862 	.ndo_bridge_getlink = ice_bridge_getlink,
6863 	.ndo_bridge_setlink = ice_bridge_setlink,
6864 	.ndo_fdb_add = ice_fdb_add,
6865 	.ndo_fdb_del = ice_fdb_del,
6866 #ifdef CONFIG_RFS_ACCEL
6867 	.ndo_rx_flow_steer = ice_rx_flow_steer,
6868 #endif
6869 	.ndo_tx_timeout = ice_tx_timeout,
6870 	.ndo_bpf = ice_xdp,
6871 	.ndo_xdp_xmit = ice_xdp_xmit,
6872 	.ndo_xsk_wakeup = ice_xsk_wakeup,
6873 	.ndo_udp_tunnel_add = udp_tunnel_nic_add_port,
6874 	.ndo_udp_tunnel_del = udp_tunnel_nic_del_port,
6875 };
6876