Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (c) 2018-2023, Intel Corporation. */
   3
   4/* Intel(R) Ethernet Connection E800 Series Linux Driver */
   5
   6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
   7
   8#include <generated/utsrelease.h>
   9#include <linux/crash_dump.h>
  10#include "ice.h"
  11#include "ice_base.h"
  12#include "ice_lib.h"
  13#include "ice_fltr.h"
  14#include "ice_dcb_lib.h"
  15#include "ice_dcb_nl.h"
  16#include "ice_devlink.h"
  17#include "ice_hwmon.h"
  18/* Including ice_trace.h with CREATE_TRACE_POINTS defined will generate the
  19 * ice tracepoint functions. This must be done exactly once across the
  20 * ice driver.
  21 */
  22#define CREATE_TRACE_POINTS
  23#include "ice_trace.h"
  24#include "ice_eswitch.h"
  25#include "ice_tc_lib.h"
  26#include "ice_vsi_vlan_ops.h"
  27#include <net/xdp_sock_drv.h>
  28
  29#define DRV_SUMMARY	"Intel(R) Ethernet Connection E800 Series Linux Driver"
  30static const char ice_driver_string[] = DRV_SUMMARY;
  31static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
  32
  33/* DDP Package file located in firmware search paths (e.g. /lib/firmware/) */
  34#define ICE_DDP_PKG_PATH	"intel/ice/ddp/"
  35#define ICE_DDP_PKG_FILE	ICE_DDP_PKG_PATH "ice.pkg"
  36
  37MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  38MODULE_DESCRIPTION(DRV_SUMMARY);
  39MODULE_LICENSE("GPL v2");
  40MODULE_FIRMWARE(ICE_DDP_PKG_FILE);
  41
  42static int debug = -1;
  43module_param(debug, int, 0644);
  44#ifndef CONFIG_DYNAMIC_DEBUG
  45MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
  46#else
  47MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
  48#endif /* !CONFIG_DYNAMIC_DEBUG */
  49
 
  50DEFINE_STATIC_KEY_FALSE(ice_xdp_locking_key);
  51EXPORT_SYMBOL(ice_xdp_locking_key);
  52
  53/**
  54 * ice_hw_to_dev - Get device pointer from the hardware structure
  55 * @hw: pointer to the device HW structure
  56 *
  57 * Used to access the device pointer from compilation units which can't easily
  58 * include the definition of struct ice_pf without leading to circular header
  59 * dependencies.
  60 */
  61struct device *ice_hw_to_dev(struct ice_hw *hw)
  62{
  63	struct ice_pf *pf = container_of(hw, struct ice_pf, hw);
  64
  65	return &pf->pdev->dev;
  66}
  67
  68static struct workqueue_struct *ice_wq;
  69struct workqueue_struct *ice_lag_wq;
  70static const struct net_device_ops ice_netdev_safe_mode_ops;
  71static const struct net_device_ops ice_netdev_ops;
  72
  73static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type);
  74
  75static void ice_vsi_release_all(struct ice_pf *pf);
  76
  77static int ice_rebuild_channels(struct ice_pf *pf);
  78static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_adv_fltr);
  79
  80static int
  81ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
  82		     void *cb_priv, enum tc_setup_type type, void *type_data,
  83		     void *data,
  84		     void (*cleanup)(struct flow_block_cb *block_cb));
  85
  86bool netif_is_ice(const struct net_device *dev)
  87{
  88	return dev && (dev->netdev_ops == &ice_netdev_ops);
  89}
  90
  91/**
  92 * ice_get_tx_pending - returns number of Tx descriptors not processed
  93 * @ring: the ring of descriptors
  94 */
  95static u16 ice_get_tx_pending(struct ice_tx_ring *ring)
  96{
  97	u16 head, tail;
  98
  99	head = ring->next_to_clean;
 100	tail = ring->next_to_use;
 101
 102	if (head != tail)
 103		return (head < tail) ?
 104			tail - head : (tail + ring->count - head);
 105	return 0;
 106}
 107
 108/**
 109 * ice_check_for_hang_subtask - check for and recover hung queues
 110 * @pf: pointer to PF struct
 111 */
 112static void ice_check_for_hang_subtask(struct ice_pf *pf)
 113{
 114	struct ice_vsi *vsi = NULL;
 115	struct ice_hw *hw;
 116	unsigned int i;
 117	int packets;
 118	u32 v;
 119
 120	ice_for_each_vsi(pf, v)
 121		if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
 122			vsi = pf->vsi[v];
 123			break;
 124		}
 125
 126	if (!vsi || test_bit(ICE_VSI_DOWN, vsi->state))
 127		return;
 128
 129	if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
 130		return;
 131
 132	hw = &vsi->back->hw;
 133
 134	ice_for_each_txq(vsi, i) {
 135		struct ice_tx_ring *tx_ring = vsi->tx_rings[i];
 136		struct ice_ring_stats *ring_stats;
 137
 138		if (!tx_ring)
 139			continue;
 140		if (ice_ring_ch_enabled(tx_ring))
 141			continue;
 142
 143		ring_stats = tx_ring->ring_stats;
 144		if (!ring_stats)
 145			continue;
 146
 147		if (tx_ring->desc) {
 148			/* If packet counter has not changed the queue is
 149			 * likely stalled, so force an interrupt for this
 150			 * queue.
 151			 *
 152			 * prev_pkt would be negative if there was no
 153			 * pending work.
 154			 */
 155			packets = ring_stats->stats.pkts & INT_MAX;
 156			if (ring_stats->tx_stats.prev_pkt == packets) {
 157				/* Trigger sw interrupt to revive the queue */
 158				ice_trigger_sw_intr(hw, tx_ring->q_vector);
 159				continue;
 160			}
 161
 162			/* Memory barrier between read of packet count and call
 163			 * to ice_get_tx_pending()
 164			 */
 165			smp_rmb();
 166			ring_stats->tx_stats.prev_pkt =
 167			    ice_get_tx_pending(tx_ring) ? packets : -1;
 168		}
 169	}
 170}
 171
 172/**
 173 * ice_init_mac_fltr - Set initial MAC filters
 174 * @pf: board private structure
 175 *
 176 * Set initial set of MAC filters for PF VSI; configure filters for permanent
 177 * address and broadcast address. If an error is encountered, netdevice will be
 178 * unregistered.
 179 */
 180static int ice_init_mac_fltr(struct ice_pf *pf)
 181{
 182	struct ice_vsi *vsi;
 183	u8 *perm_addr;
 184
 185	vsi = ice_get_main_vsi(pf);
 186	if (!vsi)
 187		return -EINVAL;
 188
 189	perm_addr = vsi->port_info->mac.perm_addr;
 190	return ice_fltr_add_mac_and_broadcast(vsi, perm_addr, ICE_FWD_TO_VSI);
 191}
 192
 193/**
 194 * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
 195 * @netdev: the net device on which the sync is happening
 196 * @addr: MAC address to sync
 197 *
 198 * This is a callback function which is called by the in kernel device sync
 199 * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
 200 * populates the tmp_sync_list, which is later used by ice_add_mac to add the
 201 * MAC filters from the hardware.
 202 */
 203static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
 204{
 205	struct ice_netdev_priv *np = netdev_priv(netdev);
 206	struct ice_vsi *vsi = np->vsi;
 207
 208	if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr,
 209				     ICE_FWD_TO_VSI))
 210		return -EINVAL;
 211
 212	return 0;
 213}
 214
 215/**
 216 * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
 217 * @netdev: the net device on which the unsync is happening
 218 * @addr: MAC address to unsync
 219 *
 220 * This is a callback function which is called by the in kernel device unsync
 221 * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
 222 * populates the tmp_unsync_list, which is later used by ice_remove_mac to
 223 * delete the MAC filters from the hardware.
 224 */
 225static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
 226{
 227	struct ice_netdev_priv *np = netdev_priv(netdev);
 228	struct ice_vsi *vsi = np->vsi;
 229
 230	/* Under some circumstances, we might receive a request to delete our
 231	 * own device address from our uc list. Because we store the device
 232	 * address in the VSI's MAC filter list, we need to ignore such
 233	 * requests and not delete our device address from this list.
 234	 */
 235	if (ether_addr_equal(addr, netdev->dev_addr))
 236		return 0;
 237
 238	if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr,
 239				     ICE_FWD_TO_VSI))
 240		return -EINVAL;
 241
 242	return 0;
 243}
 244
 245/**
 246 * ice_vsi_fltr_changed - check if filter state changed
 247 * @vsi: VSI to be checked
 248 *
 249 * returns true if filter state has changed, false otherwise.
 250 */
 251static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
 252{
 253	return test_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state) ||
 254	       test_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
 255}
 256
 257/**
 258 * ice_set_promisc - Enable promiscuous mode for a given PF
 259 * @vsi: the VSI being configured
 260 * @promisc_m: mask of promiscuous config bits
 261 *
 262 */
 263static int ice_set_promisc(struct ice_vsi *vsi, u8 promisc_m)
 264{
 265	int status;
 266
 267	if (vsi->type != ICE_VSI_PF)
 268		return 0;
 269
 270	if (ice_vsi_has_non_zero_vlans(vsi)) {
 271		promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
 272		status = ice_fltr_set_vlan_vsi_promisc(&vsi->back->hw, vsi,
 273						       promisc_m);
 274	} else {
 275		status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
 276						  promisc_m, 0);
 277	}
 278	if (status && status != -EEXIST)
 279		return status;
 280
 281	netdev_dbg(vsi->netdev, "set promisc filter bits for VSI %i: 0x%x\n",
 282		   vsi->vsi_num, promisc_m);
 283	return 0;
 284}
 285
 286/**
 287 * ice_clear_promisc - Disable promiscuous mode for a given PF
 288 * @vsi: the VSI being configured
 289 * @promisc_m: mask of promiscuous config bits
 290 *
 291 */
 292static int ice_clear_promisc(struct ice_vsi *vsi, u8 promisc_m)
 293{
 294	int status;
 295
 296	if (vsi->type != ICE_VSI_PF)
 297		return 0;
 298
 299	if (ice_vsi_has_non_zero_vlans(vsi)) {
 300		promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
 301		status = ice_fltr_clear_vlan_vsi_promisc(&vsi->back->hw, vsi,
 302							 promisc_m);
 303	} else {
 304		status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
 305						    promisc_m, 0);
 306	}
 307
 308	netdev_dbg(vsi->netdev, "clear promisc filter bits for VSI %i: 0x%x\n",
 309		   vsi->vsi_num, promisc_m);
 310	return status;
 311}
 312
 313/**
 314 * ice_vsi_sync_fltr - Update the VSI filter list to the HW
 315 * @vsi: ptr to the VSI
 316 *
 317 * Push any outstanding VSI filter changes through the AdminQ.
 318 */
 319static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
 320{
 321	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
 322	struct device *dev = ice_pf_to_dev(vsi->back);
 323	struct net_device *netdev = vsi->netdev;
 324	bool promisc_forced_on = false;
 325	struct ice_pf *pf = vsi->back;
 326	struct ice_hw *hw = &pf->hw;
 327	u32 changed_flags = 0;
 328	int err;
 329
 330	if (!vsi->netdev)
 331		return -EINVAL;
 332
 333	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
 334		usleep_range(1000, 2000);
 335
 336	changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
 337	vsi->current_netdev_flags = vsi->netdev->flags;
 338
 339	INIT_LIST_HEAD(&vsi->tmp_sync_list);
 340	INIT_LIST_HEAD(&vsi->tmp_unsync_list);
 341
 342	if (ice_vsi_fltr_changed(vsi)) {
 343		clear_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
 344		clear_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
 345
 346		/* grab the netdev's addr_list_lock */
 347		netif_addr_lock_bh(netdev);
 348		__dev_uc_sync(netdev, ice_add_mac_to_sync_list,
 349			      ice_add_mac_to_unsync_list);
 350		__dev_mc_sync(netdev, ice_add_mac_to_sync_list,
 351			      ice_add_mac_to_unsync_list);
 352		/* our temp lists are populated. release lock */
 353		netif_addr_unlock_bh(netdev);
 354	}
 355
 356	/* Remove MAC addresses in the unsync list */
 357	err = ice_fltr_remove_mac_list(vsi, &vsi->tmp_unsync_list);
 358	ice_fltr_free_list(dev, &vsi->tmp_unsync_list);
 359	if (err) {
 360		netdev_err(netdev, "Failed to delete MAC filters\n");
 361		/* if we failed because of alloc failures, just bail */
 362		if (err == -ENOMEM)
 363			goto out;
 364	}
 365
 366	/* Add MAC addresses in the sync list */
 367	err = ice_fltr_add_mac_list(vsi, &vsi->tmp_sync_list);
 368	ice_fltr_free_list(dev, &vsi->tmp_sync_list);
 369	/* If filter is added successfully or already exists, do not go into
 370	 * 'if' condition and report it as error. Instead continue processing
 371	 * rest of the function.
 372	 */
 373	if (err && err != -EEXIST) {
 374		netdev_err(netdev, "Failed to add MAC filters\n");
 375		/* If there is no more space for new umac filters, VSI
 376		 * should go into promiscuous mode. There should be some
 377		 * space reserved for promiscuous filters.
 378		 */
 379		if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
 380		    !test_and_set_bit(ICE_FLTR_OVERFLOW_PROMISC,
 381				      vsi->state)) {
 382			promisc_forced_on = true;
 383			netdev_warn(netdev, "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
 384				    vsi->vsi_num);
 385		} else {
 386			goto out;
 387		}
 388	}
 389	err = 0;
 390	/* check for changes in promiscuous modes */
 391	if (changed_flags & IFF_ALLMULTI) {
 392		if (vsi->current_netdev_flags & IFF_ALLMULTI) {
 393			err = ice_set_promisc(vsi, ICE_MCAST_PROMISC_BITS);
 394			if (err) {
 395				vsi->current_netdev_flags &= ~IFF_ALLMULTI;
 396				goto out_promisc;
 397			}
 398		} else {
 399			/* !(vsi->current_netdev_flags & IFF_ALLMULTI) */
 400			err = ice_clear_promisc(vsi, ICE_MCAST_PROMISC_BITS);
 401			if (err) {
 402				vsi->current_netdev_flags |= IFF_ALLMULTI;
 403				goto out_promisc;
 404			}
 405		}
 406	}
 407
 408	if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
 409	    test_bit(ICE_VSI_PROMISC_CHANGED, vsi->state)) {
 410		clear_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
 411		if (vsi->current_netdev_flags & IFF_PROMISC) {
 412			/* Apply Rx filter rule to get traffic from wire */
 413			if (!ice_is_dflt_vsi_in_use(vsi->port_info)) {
 414				err = ice_set_dflt_vsi(vsi);
 415				if (err && err != -EEXIST) {
 416					netdev_err(netdev, "Error %d setting default VSI %i Rx rule\n",
 417						   err, vsi->vsi_num);
 418					vsi->current_netdev_flags &=
 419						~IFF_PROMISC;
 420					goto out_promisc;
 421				}
 422				err = 0;
 423				vlan_ops->dis_rx_filtering(vsi);
 424
 425				/* promiscuous mode implies allmulticast so
 426				 * that VSIs that are in promiscuous mode are
 427				 * subscribed to multicast packets coming to
 428				 * the port
 429				 */
 430				err = ice_set_promisc(vsi,
 431						      ICE_MCAST_PROMISC_BITS);
 432				if (err)
 433					goto out_promisc;
 434			}
 435		} else {
 436			/* Clear Rx filter to remove traffic from wire */
 437			if (ice_is_vsi_dflt_vsi(vsi)) {
 438				err = ice_clear_dflt_vsi(vsi);
 439				if (err) {
 440					netdev_err(netdev, "Error %d clearing default VSI %i Rx rule\n",
 441						   err, vsi->vsi_num);
 442					vsi->current_netdev_flags |=
 443						IFF_PROMISC;
 444					goto out_promisc;
 445				}
 446				if (vsi->netdev->features &
 447				    NETIF_F_HW_VLAN_CTAG_FILTER)
 448					vlan_ops->ena_rx_filtering(vsi);
 449			}
 450
 451			/* disable allmulti here, but only if allmulti is not
 452			 * still enabled for the netdev
 453			 */
 454			if (!(vsi->current_netdev_flags & IFF_ALLMULTI)) {
 455				err = ice_clear_promisc(vsi,
 456							ICE_MCAST_PROMISC_BITS);
 457				if (err) {
 458					netdev_err(netdev, "Error %d clearing multicast promiscuous on VSI %i\n",
 459						   err, vsi->vsi_num);
 460				}
 461			}
 462		}
 463	}
 464	goto exit;
 465
 466out_promisc:
 467	set_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
 468	goto exit;
 469out:
 470	/* if something went wrong then set the changed flag so we try again */
 471	set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
 472	set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
 473exit:
 474	clear_bit(ICE_CFG_BUSY, vsi->state);
 475	return err;
 476}
 477
 478/**
 479 * ice_sync_fltr_subtask - Sync the VSI filter list with HW
 480 * @pf: board private structure
 481 */
 482static void ice_sync_fltr_subtask(struct ice_pf *pf)
 483{
 484	int v;
 485
 486	if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
 487		return;
 488
 489	clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
 490
 491	ice_for_each_vsi(pf, v)
 492		if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
 493		    ice_vsi_sync_fltr(pf->vsi[v])) {
 494			/* come back and try again later */
 495			set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
 496			break;
 497		}
 498}
 499
 500/**
 501 * ice_pf_dis_all_vsi - Pause all VSIs on a PF
 502 * @pf: the PF
 503 * @locked: is the rtnl_lock already held
 504 */
 505static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
 506{
 507	int node;
 508	int v;
 509
 510	ice_for_each_vsi(pf, v)
 511		if (pf->vsi[v])
 512			ice_dis_vsi(pf->vsi[v], locked);
 513
 514	for (node = 0; node < ICE_MAX_PF_AGG_NODES; node++)
 515		pf->pf_agg_node[node].num_vsis = 0;
 516
 517	for (node = 0; node < ICE_MAX_VF_AGG_NODES; node++)
 518		pf->vf_agg_node[node].num_vsis = 0;
 519}
 520
 521/**
 522 * ice_clear_sw_switch_recipes - clear switch recipes
 523 * @pf: board private structure
 524 *
 525 * Mark switch recipes as not created in sw structures. There are cases where
 526 * rules (especially advanced rules) need to be restored, either re-read from
 527 * hardware or added again. For example after the reset. 'recp_created' flag
 528 * prevents from doing that and need to be cleared upfront.
 529 */
 530static void ice_clear_sw_switch_recipes(struct ice_pf *pf)
 531{
 532	struct ice_sw_recipe *recp;
 533	u8 i;
 534
 535	recp = pf->hw.switch_info->recp_list;
 536	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++)
 537		recp[i].recp_created = false;
 538}
 539
 540/**
 541 * ice_prepare_for_reset - prep for reset
 542 * @pf: board private structure
 543 * @reset_type: reset type requested
 544 *
 545 * Inform or close all dependent features in prep for reset.
 546 */
 547static void
 548ice_prepare_for_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
 549{
 550	struct ice_hw *hw = &pf->hw;
 551	struct ice_vsi *vsi;
 552	struct ice_vf *vf;
 553	unsigned int bkt;
 554
 555	dev_dbg(ice_pf_to_dev(pf), "reset_type=%d\n", reset_type);
 556
 557	/* already prepared for reset */
 558	if (test_bit(ICE_PREPARED_FOR_RESET, pf->state))
 559		return;
 560
 561	ice_unplug_aux_dev(pf);
 562
 563	/* Notify VFs of impending reset */
 564	if (ice_check_sq_alive(hw, &hw->mailboxq))
 565		ice_vc_notify_reset(pf);
 566
 567	/* Disable VFs until reset is completed */
 568	mutex_lock(&pf->vfs.table_lock);
 569	ice_for_each_vf(pf, bkt, vf)
 570		ice_set_vf_state_dis(vf);
 571	mutex_unlock(&pf->vfs.table_lock);
 572
 573	if (ice_is_eswitch_mode_switchdev(pf)) {
 574		if (reset_type != ICE_RESET_PFR)
 575			ice_clear_sw_switch_recipes(pf);
 576	}
 577
 578	/* release ADQ specific HW and SW resources */
 579	vsi = ice_get_main_vsi(pf);
 580	if (!vsi)
 581		goto skip;
 582
 583	/* to be on safe side, reset orig_rss_size so that normal flow
 584	 * of deciding rss_size can take precedence
 585	 */
 586	vsi->orig_rss_size = 0;
 587
 588	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
 589		if (reset_type == ICE_RESET_PFR) {
 590			vsi->old_ena_tc = vsi->all_enatc;
 591			vsi->old_numtc = vsi->all_numtc;
 592		} else {
 593			ice_remove_q_channels(vsi, true);
 594
 595			/* for other reset type, do not support channel rebuild
 596			 * hence reset needed info
 597			 */
 598			vsi->old_ena_tc = 0;
 599			vsi->all_enatc = 0;
 600			vsi->old_numtc = 0;
 601			vsi->all_numtc = 0;
 602			vsi->req_txq = 0;
 603			vsi->req_rxq = 0;
 604			clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
 605			memset(&vsi->mqprio_qopt, 0, sizeof(vsi->mqprio_qopt));
 606		}
 607	}
 608skip:
 609
 610	/* clear SW filtering DB */
 611	ice_clear_hw_tbls(hw);
 612	/* disable the VSIs and their queues that are not already DOWN */
 613	ice_pf_dis_all_vsi(pf, false);
 614
 615	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
 616		ice_ptp_prepare_for_reset(pf);
 617
 618	if (ice_is_feature_supported(pf, ICE_F_GNSS))
 619		ice_gnss_exit(pf);
 620
 621	if (hw->port_info)
 622		ice_sched_clear_port(hw->port_info);
 623
 624	ice_shutdown_all_ctrlq(hw);
 625
 626	set_bit(ICE_PREPARED_FOR_RESET, pf->state);
 627}
 628
 629/**
 630 * ice_do_reset - Initiate one of many types of resets
 631 * @pf: board private structure
 632 * @reset_type: reset type requested before this function was called.
 633 */
 634static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
 635{
 636	struct device *dev = ice_pf_to_dev(pf);
 637	struct ice_hw *hw = &pf->hw;
 638
 639	dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
 640
 641	if (pf->lag && pf->lag->bonded && reset_type == ICE_RESET_PFR) {
 642		dev_dbg(dev, "PFR on a bonded interface, promoting to CORER\n");
 643		reset_type = ICE_RESET_CORER;
 644	}
 645
 646	ice_prepare_for_reset(pf, reset_type);
 647
 648	/* trigger the reset */
 649	if (ice_reset(hw, reset_type)) {
 650		dev_err(dev, "reset %d failed\n", reset_type);
 651		set_bit(ICE_RESET_FAILED, pf->state);
 652		clear_bit(ICE_RESET_OICR_RECV, pf->state);
 653		clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
 654		clear_bit(ICE_PFR_REQ, pf->state);
 655		clear_bit(ICE_CORER_REQ, pf->state);
 656		clear_bit(ICE_GLOBR_REQ, pf->state);
 657		wake_up(&pf->reset_wait_queue);
 658		return;
 659	}
 660
 661	/* PFR is a bit of a special case because it doesn't result in an OICR
 662	 * interrupt. So for PFR, rebuild after the reset and clear the reset-
 663	 * associated state bits.
 664	 */
 665	if (reset_type == ICE_RESET_PFR) {
 666		pf->pfr_count++;
 667		ice_rebuild(pf, reset_type);
 668		clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
 669		clear_bit(ICE_PFR_REQ, pf->state);
 670		wake_up(&pf->reset_wait_queue);
 671		ice_reset_all_vfs(pf);
 672	}
 673}
 674
 675/**
 676 * ice_reset_subtask - Set up for resetting the device and driver
 677 * @pf: board private structure
 678 */
 679static void ice_reset_subtask(struct ice_pf *pf)
 680{
 681	enum ice_reset_req reset_type = ICE_RESET_INVAL;
 682
 683	/* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
 684	 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
 685	 * of reset is pending and sets bits in pf->state indicating the reset
 686	 * type and ICE_RESET_OICR_RECV. So, if the latter bit is set
 687	 * prepare for pending reset if not already (for PF software-initiated
 688	 * global resets the software should already be prepared for it as
 689	 * indicated by ICE_PREPARED_FOR_RESET; for global resets initiated
 690	 * by firmware or software on other PFs, that bit is not set so prepare
 691	 * for the reset now), poll for reset done, rebuild and return.
 692	 */
 693	if (test_bit(ICE_RESET_OICR_RECV, pf->state)) {
 694		/* Perform the largest reset requested */
 695		if (test_and_clear_bit(ICE_CORER_RECV, pf->state))
 696			reset_type = ICE_RESET_CORER;
 697		if (test_and_clear_bit(ICE_GLOBR_RECV, pf->state))
 698			reset_type = ICE_RESET_GLOBR;
 699		if (test_and_clear_bit(ICE_EMPR_RECV, pf->state))
 700			reset_type = ICE_RESET_EMPR;
 701		/* return if no valid reset type requested */
 702		if (reset_type == ICE_RESET_INVAL)
 703			return;
 704		ice_prepare_for_reset(pf, reset_type);
 705
 706		/* make sure we are ready to rebuild */
 707		if (ice_check_reset(&pf->hw)) {
 708			set_bit(ICE_RESET_FAILED, pf->state);
 709		} else {
 710			/* done with reset. start rebuild */
 711			pf->hw.reset_ongoing = false;
 712			ice_rebuild(pf, reset_type);
 713			/* clear bit to resume normal operations, but
 714			 * ICE_NEEDS_RESTART bit is set in case rebuild failed
 715			 */
 716			clear_bit(ICE_RESET_OICR_RECV, pf->state);
 717			clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
 718			clear_bit(ICE_PFR_REQ, pf->state);
 719			clear_bit(ICE_CORER_REQ, pf->state);
 720			clear_bit(ICE_GLOBR_REQ, pf->state);
 721			wake_up(&pf->reset_wait_queue);
 722			ice_reset_all_vfs(pf);
 723		}
 724
 725		return;
 726	}
 727
 728	/* No pending resets to finish processing. Check for new resets */
 729	if (test_bit(ICE_PFR_REQ, pf->state)) {
 730		reset_type = ICE_RESET_PFR;
 731		if (pf->lag && pf->lag->bonded) {
 732			dev_dbg(ice_pf_to_dev(pf), "PFR on a bonded interface, promoting to CORER\n");
 733			reset_type = ICE_RESET_CORER;
 734		}
 735	}
 736	if (test_bit(ICE_CORER_REQ, pf->state))
 737		reset_type = ICE_RESET_CORER;
 738	if (test_bit(ICE_GLOBR_REQ, pf->state))
 739		reset_type = ICE_RESET_GLOBR;
 740	/* If no valid reset type requested just return */
 741	if (reset_type == ICE_RESET_INVAL)
 742		return;
 743
 744	/* reset if not already down or busy */
 745	if (!test_bit(ICE_DOWN, pf->state) &&
 746	    !test_bit(ICE_CFG_BUSY, pf->state)) {
 747		ice_do_reset(pf, reset_type);
 748	}
 749}
 750
 751/**
 752 * ice_print_topo_conflict - print topology conflict message
 753 * @vsi: the VSI whose topology status is being checked
 754 */
 755static void ice_print_topo_conflict(struct ice_vsi *vsi)
 756{
 757	switch (vsi->port_info->phy.link_info.topo_media_conflict) {
 758	case ICE_AQ_LINK_TOPO_CONFLICT:
 759	case ICE_AQ_LINK_MEDIA_CONFLICT:
 760	case ICE_AQ_LINK_TOPO_UNREACH_PRT:
 761	case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT:
 762	case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA:
 763		netdev_info(vsi->netdev, "Potential misconfiguration of the Ethernet port detected. If it was not intended, please use the Intel (R) Ethernet Port Configuration Tool to address the issue.\n");
 764		break;
 765	case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA:
 766		if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, vsi->back->flags))
 767			netdev_warn(vsi->netdev, "An unsupported module type was detected. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules\n");
 768		else
 769			netdev_err(vsi->netdev, "Rx/Tx is disabled on this device because an unsupported module type was detected. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
 770		break;
 771	default:
 772		break;
 773	}
 774}
 775
 776/**
 777 * ice_print_link_msg - print link up or down message
 778 * @vsi: the VSI whose link status is being queried
 779 * @isup: boolean for if the link is now up or down
 780 */
 781void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
 782{
 783	struct ice_aqc_get_phy_caps_data *caps;
 784	const char *an_advertised;
 785	const char *fec_req;
 786	const char *speed;
 787	const char *fec;
 788	const char *fc;
 789	const char *an;
 790	int status;
 791
 792	if (!vsi)
 793		return;
 794
 795	if (vsi->current_isup == isup)
 796		return;
 797
 798	vsi->current_isup = isup;
 799
 800	if (!isup) {
 801		netdev_info(vsi->netdev, "NIC Link is Down\n");
 802		return;
 803	}
 804
 805	switch (vsi->port_info->phy.link_info.link_speed) {
 806	case ICE_AQ_LINK_SPEED_100GB:
 807		speed = "100 G";
 808		break;
 809	case ICE_AQ_LINK_SPEED_50GB:
 810		speed = "50 G";
 811		break;
 812	case ICE_AQ_LINK_SPEED_40GB:
 813		speed = "40 G";
 814		break;
 815	case ICE_AQ_LINK_SPEED_25GB:
 816		speed = "25 G";
 817		break;
 818	case ICE_AQ_LINK_SPEED_20GB:
 819		speed = "20 G";
 820		break;
 821	case ICE_AQ_LINK_SPEED_10GB:
 822		speed = "10 G";
 823		break;
 824	case ICE_AQ_LINK_SPEED_5GB:
 825		speed = "5 G";
 826		break;
 827	case ICE_AQ_LINK_SPEED_2500MB:
 828		speed = "2.5 G";
 829		break;
 830	case ICE_AQ_LINK_SPEED_1000MB:
 831		speed = "1 G";
 832		break;
 833	case ICE_AQ_LINK_SPEED_100MB:
 834		speed = "100 M";
 835		break;
 836	default:
 837		speed = "Unknown ";
 838		break;
 839	}
 840
 841	switch (vsi->port_info->fc.current_mode) {
 842	case ICE_FC_FULL:
 843		fc = "Rx/Tx";
 844		break;
 845	case ICE_FC_TX_PAUSE:
 846		fc = "Tx";
 847		break;
 848	case ICE_FC_RX_PAUSE:
 849		fc = "Rx";
 850		break;
 851	case ICE_FC_NONE:
 852		fc = "None";
 853		break;
 854	default:
 855		fc = "Unknown";
 856		break;
 857	}
 858
 859	/* Get FEC mode based on negotiated link info */
 860	switch (vsi->port_info->phy.link_info.fec_info) {
 861	case ICE_AQ_LINK_25G_RS_528_FEC_EN:
 862	case ICE_AQ_LINK_25G_RS_544_FEC_EN:
 863		fec = "RS-FEC";
 864		break;
 865	case ICE_AQ_LINK_25G_KR_FEC_EN:
 866		fec = "FC-FEC/BASE-R";
 867		break;
 868	default:
 869		fec = "NONE";
 870		break;
 871	}
 872
 873	/* check if autoneg completed, might be false due to not supported */
 874	if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
 875		an = "True";
 876	else
 877		an = "False";
 878
 879	/* Get FEC mode requested based on PHY caps last SW configuration */
 880	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
 881	if (!caps) {
 882		fec_req = "Unknown";
 883		an_advertised = "Unknown";
 884		goto done;
 885	}
 886
 887	status = ice_aq_get_phy_caps(vsi->port_info, false,
 888				     ICE_AQC_REPORT_ACTIVE_CFG, caps, NULL);
 889	if (status)
 890		netdev_info(vsi->netdev, "Get phy capability failed.\n");
 891
 892	an_advertised = ice_is_phy_caps_an_enabled(caps) ? "On" : "Off";
 893
 894	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
 895	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
 896		fec_req = "RS-FEC";
 897	else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
 898		 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
 899		fec_req = "FC-FEC/BASE-R";
 900	else
 901		fec_req = "NONE";
 902
 903	kfree(caps);
 904
 905done:
 906	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",
 907		    speed, fec_req, fec, an_advertised, an, fc);
 908	ice_print_topo_conflict(vsi);
 909}
 910
 911/**
 912 * ice_vsi_link_event - update the VSI's netdev
 913 * @vsi: the VSI on which the link event occurred
 914 * @link_up: whether or not the VSI needs to be set up or down
 915 */
 916static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
 917{
 918	if (!vsi)
 919		return;
 920
 921	if (test_bit(ICE_VSI_DOWN, vsi->state) || !vsi->netdev)
 922		return;
 923
 924	if (vsi->type == ICE_VSI_PF) {
 925		if (link_up == netif_carrier_ok(vsi->netdev))
 926			return;
 927
 928		if (link_up) {
 929			netif_carrier_on(vsi->netdev);
 930			netif_tx_wake_all_queues(vsi->netdev);
 931		} else {
 932			netif_carrier_off(vsi->netdev);
 933			netif_tx_stop_all_queues(vsi->netdev);
 934		}
 935	}
 936}
 937
 938/**
 939 * ice_set_dflt_mib - send a default config MIB to the FW
 940 * @pf: private PF struct
 941 *
 942 * This function sends a default configuration MIB to the FW.
 943 *
 944 * If this function errors out at any point, the driver is still able to
 945 * function.  The main impact is that LFC may not operate as expected.
 946 * Therefore an error state in this function should be treated with a DBG
 947 * message and continue on with driver rebuild/reenable.
 948 */
 949static void ice_set_dflt_mib(struct ice_pf *pf)
 950{
 951	struct device *dev = ice_pf_to_dev(pf);
 952	u8 mib_type, *buf, *lldpmib = NULL;
 953	u16 len, typelen, offset = 0;
 954	struct ice_lldp_org_tlv *tlv;
 955	struct ice_hw *hw = &pf->hw;
 956	u32 ouisubtype;
 957
 958	mib_type = SET_LOCAL_MIB_TYPE_LOCAL_MIB;
 959	lldpmib = kzalloc(ICE_LLDPDU_SIZE, GFP_KERNEL);
 960	if (!lldpmib) {
 961		dev_dbg(dev, "%s Failed to allocate MIB memory\n",
 962			__func__);
 963		return;
 964	}
 965
 966	/* Add ETS CFG TLV */
 967	tlv = (struct ice_lldp_org_tlv *)lldpmib;
 968	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
 969		   ICE_IEEE_ETS_TLV_LEN);
 970	tlv->typelen = htons(typelen);
 971	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
 972		      ICE_IEEE_SUBTYPE_ETS_CFG);
 973	tlv->ouisubtype = htonl(ouisubtype);
 974
 975	buf = tlv->tlvinfo;
 976	buf[0] = 0;
 977
 978	/* ETS CFG all UPs map to TC 0. Next 4 (1 - 4) Octets = 0.
 979	 * Octets 5 - 12 are BW values, set octet 5 to 100% BW.
 980	 * Octets 13 - 20 are TSA values - leave as zeros
 981	 */
 982	buf[5] = 0x64;
 983	len = FIELD_GET(ICE_LLDP_TLV_LEN_M, typelen);
 984	offset += len + 2;
 985	tlv = (struct ice_lldp_org_tlv *)
 986		((char *)tlv + sizeof(tlv->typelen) + len);
 987
 988	/* Add ETS REC TLV */
 989	buf = tlv->tlvinfo;
 990	tlv->typelen = htons(typelen);
 991
 992	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
 993		      ICE_IEEE_SUBTYPE_ETS_REC);
 994	tlv->ouisubtype = htonl(ouisubtype);
 995
 996	/* First octet of buf is reserved
 997	 * Octets 1 - 4 map UP to TC - all UPs map to zero
 998	 * Octets 5 - 12 are BW values - set TC 0 to 100%.
 999	 * Octets 13 - 20 are TSA value - leave as zeros
1000	 */
1001	buf[5] = 0x64;
1002	offset += len + 2;
1003	tlv = (struct ice_lldp_org_tlv *)
1004		((char *)tlv + sizeof(tlv->typelen) + len);
1005
1006	/* Add PFC CFG TLV */
1007	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
1008		   ICE_IEEE_PFC_TLV_LEN);
1009	tlv->typelen = htons(typelen);
1010
1011	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
1012		      ICE_IEEE_SUBTYPE_PFC_CFG);
1013	tlv->ouisubtype = htonl(ouisubtype);
1014
1015	/* Octet 1 left as all zeros - PFC disabled */
1016	buf[0] = 0x08;
1017	len = FIELD_GET(ICE_LLDP_TLV_LEN_M, typelen);
1018	offset += len + 2;
1019
1020	if (ice_aq_set_lldp_mib(hw, mib_type, (void *)lldpmib, offset, NULL))
1021		dev_dbg(dev, "%s Failed to set default LLDP MIB\n", __func__);
1022
1023	kfree(lldpmib);
1024}
1025
1026/**
1027 * ice_check_phy_fw_load - check if PHY FW load failed
1028 * @pf: pointer to PF struct
1029 * @link_cfg_err: bitmap from the link info structure
1030 *
1031 * check if external PHY FW load failed and print an error message if it did
1032 */
1033static void ice_check_phy_fw_load(struct ice_pf *pf, u8 link_cfg_err)
1034{
1035	if (!(link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE)) {
1036		clear_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
1037		return;
1038	}
1039
1040	if (test_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags))
1041		return;
1042
1043	if (link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE) {
1044		dev_err(ice_pf_to_dev(pf), "Device failed to load the FW for the external PHY. Please download and install the latest NVM for your device and try again\n");
1045		set_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
1046	}
1047}
1048
1049/**
1050 * ice_check_module_power
1051 * @pf: pointer to PF struct
1052 * @link_cfg_err: bitmap from the link info structure
1053 *
1054 * check module power level returned by a previous call to aq_get_link_info
1055 * and print error messages if module power level is not supported
1056 */
1057static void ice_check_module_power(struct ice_pf *pf, u8 link_cfg_err)
1058{
1059	/* if module power level is supported, clear the flag */
1060	if (!(link_cfg_err & (ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT |
1061			      ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED))) {
1062		clear_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1063		return;
1064	}
1065
1066	/* if ICE_FLAG_MOD_POWER_UNSUPPORTED was previously set and the
1067	 * above block didn't clear this bit, there's nothing to do
1068	 */
1069	if (test_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags))
1070		return;
1071
1072	if (link_cfg_err & ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT) {
1073		dev_err(ice_pf_to_dev(pf), "The installed module is incompatible with the device's NVM image. Cannot start link\n");
1074		set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1075	} else if (link_cfg_err & ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED) {
1076		dev_err(ice_pf_to_dev(pf), "The module's power requirements exceed the device's power supply. Cannot start link\n");
1077		set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1078	}
1079}
1080
1081/**
1082 * ice_check_link_cfg_err - check if link configuration failed
1083 * @pf: pointer to the PF struct
1084 * @link_cfg_err: bitmap from the link info structure
1085 *
1086 * print if any link configuration failure happens due to the value in the
1087 * link_cfg_err parameter in the link info structure
1088 */
1089static void ice_check_link_cfg_err(struct ice_pf *pf, u8 link_cfg_err)
1090{
1091	ice_check_module_power(pf, link_cfg_err);
1092	ice_check_phy_fw_load(pf, link_cfg_err);
1093}
1094
1095/**
1096 * ice_link_event - process the link event
1097 * @pf: PF that the link event is associated with
1098 * @pi: port_info for the port that the link event is associated with
1099 * @link_up: true if the physical link is up and false if it is down
1100 * @link_speed: current link speed received from the link event
1101 *
1102 * Returns 0 on success and negative on failure
1103 */
1104static int
1105ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
1106	       u16 link_speed)
1107{
1108	struct device *dev = ice_pf_to_dev(pf);
1109	struct ice_phy_info *phy_info;
1110	struct ice_vsi *vsi;
1111	u16 old_link_speed;
1112	bool old_link;
1113	int status;
1114
1115	phy_info = &pi->phy;
1116	phy_info->link_info_old = phy_info->link_info;
1117
1118	old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
1119	old_link_speed = phy_info->link_info_old.link_speed;
1120
1121	/* update the link info structures and re-enable link events,
1122	 * don't bail on failure due to other book keeping needed
1123	 */
1124	status = ice_update_link_info(pi);
1125	if (status)
1126		dev_dbg(dev, "Failed to update link status on port %d, err %d aq_err %s\n",
1127			pi->lport, status,
1128			ice_aq_str(pi->hw->adminq.sq_last_status));
1129
1130	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
1131
1132	/* Check if the link state is up after updating link info, and treat
1133	 * this event as an UP event since the link is actually UP now.
1134	 */
1135	if (phy_info->link_info.link_info & ICE_AQ_LINK_UP)
1136		link_up = true;
1137
1138	vsi = ice_get_main_vsi(pf);
1139	if (!vsi || !vsi->port_info)
1140		return -EINVAL;
1141
1142	/* turn off PHY if media was removed */
1143	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
1144	    !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
1145		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
1146		ice_set_link(vsi, false);
1147	}
1148
1149	/* if the old link up/down and speed is the same as the new */
1150	if (link_up == old_link && link_speed == old_link_speed)
1151		return 0;
1152
1153	ice_ptp_link_change(pf, pf->hw.pf_id, link_up);
1154
1155	if (ice_is_dcb_active(pf)) {
1156		if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
1157			ice_dcb_rebuild(pf);
1158	} else {
1159		if (link_up)
1160			ice_set_dflt_mib(pf);
1161	}
1162	ice_vsi_link_event(vsi, link_up);
1163	ice_print_link_msg(vsi, link_up);
1164
1165	ice_vc_notify_link_state(pf);
1166
1167	return 0;
1168}
1169
1170/**
1171 * ice_watchdog_subtask - periodic tasks not using event driven scheduling
1172 * @pf: board private structure
1173 */
1174static void ice_watchdog_subtask(struct ice_pf *pf)
1175{
1176	int i;
1177
1178	/* if interface is down do nothing */
1179	if (test_bit(ICE_DOWN, pf->state) ||
1180	    test_bit(ICE_CFG_BUSY, pf->state))
1181		return;
1182
1183	/* make sure we don't do these things too often */
1184	if (time_before(jiffies,
1185			pf->serv_tmr_prev + pf->serv_tmr_period))
1186		return;
1187
1188	pf->serv_tmr_prev = jiffies;
1189
1190	/* Update the stats for active netdevs so the network stack
1191	 * can look at updated numbers whenever it cares to
1192	 */
1193	ice_update_pf_stats(pf);
1194	ice_for_each_vsi(pf, i)
1195		if (pf->vsi[i] && pf->vsi[i]->netdev)
1196			ice_update_vsi_stats(pf->vsi[i]);
1197}
1198
1199/**
1200 * ice_init_link_events - enable/initialize link events
1201 * @pi: pointer to the port_info instance
1202 *
1203 * Returns -EIO on failure, 0 on success
1204 */
1205static int ice_init_link_events(struct ice_port_info *pi)
1206{
1207	u16 mask;
1208
1209	mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
1210		       ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL |
1211		       ICE_AQ_LINK_EVENT_PHY_FW_LOAD_FAIL));
1212
1213	if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
1214		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to set link event mask for port %d\n",
1215			pi->lport);
1216		return -EIO;
1217	}
1218
1219	if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
1220		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to enable link events for port %d\n",
1221			pi->lport);
1222		return -EIO;
1223	}
1224
1225	return 0;
1226}
1227
1228/**
1229 * ice_handle_link_event - handle link event via ARQ
1230 * @pf: PF that the link event is associated with
1231 * @event: event structure containing link status info
1232 */
1233static int
1234ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1235{
1236	struct ice_aqc_get_link_status_data *link_data;
1237	struct ice_port_info *port_info;
1238	int status;
1239
1240	link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
1241	port_info = pf->hw.port_info;
1242	if (!port_info)
1243		return -EINVAL;
1244
1245	status = ice_link_event(pf, port_info,
1246				!!(link_data->link_info & ICE_AQ_LINK_UP),
1247				le16_to_cpu(link_data->link_speed));
1248	if (status)
1249		dev_dbg(ice_pf_to_dev(pf), "Could not process link event, error %d\n",
1250			status);
1251
1252	return status;
1253}
1254
1255/**
1256 * ice_get_fwlog_data - copy the FW log data from ARQ event
1257 * @pf: PF that the FW log event is associated with
1258 * @event: event structure containing FW log data
1259 */
1260static void
1261ice_get_fwlog_data(struct ice_pf *pf, struct ice_rq_event_info *event)
1262{
1263	struct ice_fwlog_data *fwlog;
1264	struct ice_hw *hw = &pf->hw;
1265
1266	fwlog = &hw->fwlog_ring.rings[hw->fwlog_ring.tail];
1267
1268	memset(fwlog->data, 0, PAGE_SIZE);
1269	fwlog->data_size = le16_to_cpu(event->desc.datalen);
1270
1271	memcpy(fwlog->data, event->msg_buf, fwlog->data_size);
1272	ice_fwlog_ring_increment(&hw->fwlog_ring.tail, hw->fwlog_ring.size);
1273
1274	if (ice_fwlog_ring_full(&hw->fwlog_ring)) {
1275		/* the rings are full so bump the head to create room */
1276		ice_fwlog_ring_increment(&hw->fwlog_ring.head,
1277					 hw->fwlog_ring.size);
1278	}
1279}
1280
1281/**
1282 * ice_aq_prep_for_event - Prepare to wait for an AdminQ event from firmware
1283 * @pf: pointer to the PF private structure
1284 * @task: intermediate helper storage and identifier for waiting
1285 * @opcode: the opcode to wait for
 
 
1286 *
1287 * Prepares to wait for a specific AdminQ completion event on the ARQ for
1288 * a given PF. Actual wait would be done by a call to ice_aq_wait_for_event().
1289 *
1290 * Calls are separated to allow caller registering for event before sending
1291 * the command, which mitigates a race between registering and FW responding.
1292 *
1293 * To obtain only the descriptor contents, pass an task->event with null
1294 * msg_buf. If the complete data buffer is desired, allocate the
1295 * task->event.msg_buf with enough space ahead of time.
 
 
1296 */
1297void ice_aq_prep_for_event(struct ice_pf *pf, struct ice_aq_task *task,
1298			   u16 opcode)
1299{
 
 
 
 
 
 
 
 
 
 
1300	INIT_HLIST_NODE(&task->entry);
1301	task->opcode = opcode;
 
1302	task->state = ICE_AQ_TASK_WAITING;
1303
1304	spin_lock_bh(&pf->aq_wait_lock);
1305	hlist_add_head(&task->entry, &pf->aq_wait_list);
1306	spin_unlock_bh(&pf->aq_wait_lock);
1307}
1308
1309/**
1310 * ice_aq_wait_for_event - Wait for an AdminQ event from firmware
1311 * @pf: pointer to the PF private structure
1312 * @task: ptr prepared by ice_aq_prep_for_event()
1313 * @timeout: how long to wait, in jiffies
1314 *
1315 * Waits for a specific AdminQ completion event on the ARQ for a given PF. The
1316 * current thread will be put to sleep until the specified event occurs or
1317 * until the given timeout is reached.
1318 *
1319 * Returns: zero on success, or a negative error code on failure.
1320 */
1321int ice_aq_wait_for_event(struct ice_pf *pf, struct ice_aq_task *task,
1322			  unsigned long timeout)
1323{
1324	enum ice_aq_task_state *state = &task->state;
1325	struct device *dev = ice_pf_to_dev(pf);
1326	unsigned long start = jiffies;
1327	long ret;
1328	int err;
1329
1330	ret = wait_event_interruptible_timeout(pf->aq_wait_queue,
1331					       *state != ICE_AQ_TASK_WAITING,
1332					       timeout);
1333	switch (*state) {
1334	case ICE_AQ_TASK_NOT_PREPARED:
1335		WARN(1, "call to %s without ice_aq_prep_for_event()", __func__);
1336		err = -EINVAL;
1337		break;
1338	case ICE_AQ_TASK_WAITING:
1339		err = ret < 0 ? ret : -ETIMEDOUT;
1340		break;
1341	case ICE_AQ_TASK_CANCELED:
1342		err = ret < 0 ? ret : -ECANCELED;
1343		break;
1344	case ICE_AQ_TASK_COMPLETE:
1345		err = ret < 0 ? ret : 0;
1346		break;
1347	default:
1348		WARN(1, "Unexpected AdminQ wait task state %u", *state);
1349		err = -EINVAL;
1350		break;
1351	}
1352
1353	dev_dbg(dev, "Waited %u msecs (max %u msecs) for firmware response to op 0x%04x\n",
1354		jiffies_to_msecs(jiffies - start),
1355		jiffies_to_msecs(timeout),
1356		task->opcode);
1357
1358	spin_lock_bh(&pf->aq_wait_lock);
1359	hlist_del(&task->entry);
1360	spin_unlock_bh(&pf->aq_wait_lock);
 
1361
1362	return err;
1363}
1364
1365/**
1366 * ice_aq_check_events - Check if any thread is waiting for an AdminQ event
1367 * @pf: pointer to the PF private structure
1368 * @opcode: the opcode of the event
1369 * @event: the event to check
1370 *
1371 * Loops over the current list of pending threads waiting for an AdminQ event.
1372 * For each matching task, copy the contents of the event into the task
1373 * structure and wake up the thread.
1374 *
1375 * If multiple threads wait for the same opcode, they will all be woken up.
1376 *
1377 * Note that event->msg_buf will only be duplicated if the event has a buffer
1378 * with enough space already allocated. Otherwise, only the descriptor and
1379 * message length will be copied.
1380 *
1381 * Returns: true if an event was found, false otherwise
1382 */
1383static void ice_aq_check_events(struct ice_pf *pf, u16 opcode,
1384				struct ice_rq_event_info *event)
1385{
1386	struct ice_rq_event_info *task_ev;
1387	struct ice_aq_task *task;
1388	bool found = false;
1389
1390	spin_lock_bh(&pf->aq_wait_lock);
1391	hlist_for_each_entry(task, &pf->aq_wait_list, entry) {
1392		if (task->state != ICE_AQ_TASK_WAITING)
1393			continue;
1394		if (task->opcode != opcode)
1395			continue;
1396
1397		task_ev = &task->event;
1398		memcpy(&task_ev->desc, &event->desc, sizeof(event->desc));
1399		task_ev->msg_len = event->msg_len;
1400
1401		/* Only copy the data buffer if a destination was set */
1402		if (task_ev->msg_buf && task_ev->buf_len >= event->buf_len) {
1403			memcpy(task_ev->msg_buf, event->msg_buf,
 
1404			       event->buf_len);
1405			task_ev->buf_len = event->buf_len;
1406		}
1407
1408		task->state = ICE_AQ_TASK_COMPLETE;
1409		found = true;
1410	}
1411	spin_unlock_bh(&pf->aq_wait_lock);
1412
1413	if (found)
1414		wake_up(&pf->aq_wait_queue);
1415}
1416
1417/**
1418 * ice_aq_cancel_waiting_tasks - Immediately cancel all waiting tasks
1419 * @pf: the PF private structure
1420 *
1421 * Set all waiting tasks to ICE_AQ_TASK_CANCELED, and wake up their threads.
1422 * This will then cause ice_aq_wait_for_event to exit with -ECANCELED.
1423 */
1424static void ice_aq_cancel_waiting_tasks(struct ice_pf *pf)
1425{
1426	struct ice_aq_task *task;
1427
1428	spin_lock_bh(&pf->aq_wait_lock);
1429	hlist_for_each_entry(task, &pf->aq_wait_list, entry)
1430		task->state = ICE_AQ_TASK_CANCELED;
1431	spin_unlock_bh(&pf->aq_wait_lock);
1432
1433	wake_up(&pf->aq_wait_queue);
1434}
1435
1436#define ICE_MBX_OVERFLOW_WATERMARK 64
1437
1438/**
1439 * __ice_clean_ctrlq - helper function to clean controlq rings
1440 * @pf: ptr to struct ice_pf
1441 * @q_type: specific Control queue type
1442 */
1443static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
1444{
1445	struct device *dev = ice_pf_to_dev(pf);
1446	struct ice_rq_event_info event;
1447	struct ice_hw *hw = &pf->hw;
1448	struct ice_ctl_q_info *cq;
1449	u16 pending, i = 0;
1450	const char *qtype;
1451	u32 oldval, val;
1452
1453	/* Do not clean control queue if/when PF reset fails */
1454	if (test_bit(ICE_RESET_FAILED, pf->state))
1455		return 0;
1456
1457	switch (q_type) {
1458	case ICE_CTL_Q_ADMIN:
1459		cq = &hw->adminq;
1460		qtype = "Admin";
1461		break;
1462	case ICE_CTL_Q_SB:
1463		cq = &hw->sbq;
1464		qtype = "Sideband";
1465		break;
1466	case ICE_CTL_Q_MAILBOX:
1467		cq = &hw->mailboxq;
1468		qtype = "Mailbox";
1469		/* we are going to try to detect a malicious VF, so set the
1470		 * state to begin detection
1471		 */
1472		hw->mbx_snapshot.mbx_buf.state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT;
1473		break;
1474	default:
1475		dev_warn(dev, "Unknown control queue type 0x%x\n", q_type);
1476		return 0;
1477	}
1478
1479	/* check for error indications - PF_xx_AxQLEN register layout for
1480	 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
1481	 */
1482	val = rd32(hw, cq->rq.len);
1483	if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1484		   PF_FW_ARQLEN_ARQCRIT_M)) {
1485		oldval = val;
1486		if (val & PF_FW_ARQLEN_ARQVFE_M)
1487			dev_dbg(dev, "%s Receive Queue VF Error detected\n",
1488				qtype);
1489		if (val & PF_FW_ARQLEN_ARQOVFL_M) {
1490			dev_dbg(dev, "%s Receive Queue Overflow Error detected\n",
1491				qtype);
1492		}
1493		if (val & PF_FW_ARQLEN_ARQCRIT_M)
1494			dev_dbg(dev, "%s Receive Queue Critical Error detected\n",
1495				qtype);
1496		val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1497			 PF_FW_ARQLEN_ARQCRIT_M);
1498		if (oldval != val)
1499			wr32(hw, cq->rq.len, val);
1500	}
1501
1502	val = rd32(hw, cq->sq.len);
1503	if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1504		   PF_FW_ATQLEN_ATQCRIT_M)) {
1505		oldval = val;
1506		if (val & PF_FW_ATQLEN_ATQVFE_M)
1507			dev_dbg(dev, "%s Send Queue VF Error detected\n",
1508				qtype);
1509		if (val & PF_FW_ATQLEN_ATQOVFL_M) {
1510			dev_dbg(dev, "%s Send Queue Overflow Error detected\n",
1511				qtype);
1512		}
1513		if (val & PF_FW_ATQLEN_ATQCRIT_M)
1514			dev_dbg(dev, "%s Send Queue Critical Error detected\n",
1515				qtype);
1516		val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1517			 PF_FW_ATQLEN_ATQCRIT_M);
1518		if (oldval != val)
1519			wr32(hw, cq->sq.len, val);
1520	}
1521
1522	event.buf_len = cq->rq_buf_size;
1523	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1524	if (!event.msg_buf)
1525		return 0;
1526
1527	do {
1528		struct ice_mbx_data data = {};
1529		u16 opcode;
1530		int ret;
1531
1532		ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1533		if (ret == -EALREADY)
1534			break;
1535		if (ret) {
1536			dev_err(dev, "%s Receive Queue event error %d\n", qtype,
1537				ret);
1538			break;
1539		}
1540
1541		opcode = le16_to_cpu(event.desc.opcode);
1542
1543		/* Notify any thread that might be waiting for this event */
1544		ice_aq_check_events(pf, opcode, &event);
1545
1546		switch (opcode) {
1547		case ice_aqc_opc_get_link_status:
1548			if (ice_handle_link_event(pf, &event))
1549				dev_err(dev, "Could not handle link event\n");
1550			break;
1551		case ice_aqc_opc_event_lan_overflow:
1552			ice_vf_lan_overflow_event(pf, &event);
1553			break;
1554		case ice_mbx_opc_send_msg_to_pf:
1555			data.num_msg_proc = i;
1556			data.num_pending_arq = pending;
1557			data.max_num_msgs_mbx = hw->mailboxq.num_rq_entries;
1558			data.async_watermark_val = ICE_MBX_OVERFLOW_WATERMARK;
1559
1560			ice_vc_process_vf_msg(pf, &event, &data);
1561			break;
1562		case ice_aqc_opc_fw_logs_event:
1563			ice_get_fwlog_data(pf, &event);
1564			break;
1565		case ice_aqc_opc_lldp_set_mib_change:
1566			ice_dcb_process_lldp_set_mib_change(pf, &event);
1567			break;
1568		default:
1569			dev_dbg(dev, "%s Receive Queue unknown event 0x%04x ignored\n",
1570				qtype, opcode);
1571			break;
1572		}
1573	} while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1574
1575	kfree(event.msg_buf);
1576
1577	return pending && (i == ICE_DFLT_IRQ_WORK);
1578}
1579
1580/**
1581 * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1582 * @hw: pointer to hardware info
1583 * @cq: control queue information
1584 *
1585 * returns true if there are pending messages in a queue, false if there aren't
1586 */
1587static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1588{
1589	u16 ntu;
1590
1591	ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1592	return cq->rq.next_to_clean != ntu;
1593}
1594
1595/**
1596 * ice_clean_adminq_subtask - clean the AdminQ rings
1597 * @pf: board private structure
1598 */
1599static void ice_clean_adminq_subtask(struct ice_pf *pf)
1600{
1601	struct ice_hw *hw = &pf->hw;
1602
1603	if (!test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
1604		return;
1605
1606	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1607		return;
1608
1609	clear_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
1610
1611	/* There might be a situation where new messages arrive to a control
1612	 * queue between processing the last message and clearing the
1613	 * EVENT_PENDING bit. So before exiting, check queue head again (using
1614	 * ice_ctrlq_pending) and process new messages if any.
1615	 */
1616	if (ice_ctrlq_pending(hw, &hw->adminq))
1617		__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1618
1619	ice_flush(hw);
1620}
1621
1622/**
1623 * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1624 * @pf: board private structure
1625 */
1626static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1627{
1628	struct ice_hw *hw = &pf->hw;
1629
1630	if (!test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state))
1631		return;
1632
1633	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1634		return;
1635
1636	clear_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1637
1638	if (ice_ctrlq_pending(hw, &hw->mailboxq))
1639		__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1640
1641	ice_flush(hw);
1642}
1643
1644/**
1645 * ice_clean_sbq_subtask - clean the Sideband Queue rings
1646 * @pf: board private structure
1647 */
1648static void ice_clean_sbq_subtask(struct ice_pf *pf)
1649{
1650	struct ice_hw *hw = &pf->hw;
1651
1652	/* Nothing to do here if sideband queue is not supported */
1653	if (!ice_is_sbq_supported(hw)) {
1654		clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1655		return;
1656	}
1657
1658	if (!test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state))
1659		return;
1660
1661	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_SB))
1662		return;
1663
1664	clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1665
1666	if (ice_ctrlq_pending(hw, &hw->sbq))
1667		__ice_clean_ctrlq(pf, ICE_CTL_Q_SB);
1668
1669	ice_flush(hw);
1670}
1671
1672/**
1673 * ice_service_task_schedule - schedule the service task to wake up
1674 * @pf: board private structure
1675 *
1676 * If not already scheduled, this puts the task into the work queue.
1677 */
1678void ice_service_task_schedule(struct ice_pf *pf)
1679{
1680	if (!test_bit(ICE_SERVICE_DIS, pf->state) &&
1681	    !test_and_set_bit(ICE_SERVICE_SCHED, pf->state) &&
1682	    !test_bit(ICE_NEEDS_RESTART, pf->state))
1683		queue_work(ice_wq, &pf->serv_task);
1684}
1685
1686/**
1687 * ice_service_task_complete - finish up the service task
1688 * @pf: board private structure
1689 */
1690static void ice_service_task_complete(struct ice_pf *pf)
1691{
1692	WARN_ON(!test_bit(ICE_SERVICE_SCHED, pf->state));
1693
1694	/* force memory (pf->state) to sync before next service task */
1695	smp_mb__before_atomic();
1696	clear_bit(ICE_SERVICE_SCHED, pf->state);
1697}
1698
1699/**
1700 * ice_service_task_stop - stop service task and cancel works
1701 * @pf: board private structure
1702 *
1703 * Return 0 if the ICE_SERVICE_DIS bit was not already set,
1704 * 1 otherwise.
1705 */
1706static int ice_service_task_stop(struct ice_pf *pf)
1707{
1708	int ret;
1709
1710	ret = test_and_set_bit(ICE_SERVICE_DIS, pf->state);
1711
1712	if (pf->serv_tmr.function)
1713		del_timer_sync(&pf->serv_tmr);
1714	if (pf->serv_task.func)
1715		cancel_work_sync(&pf->serv_task);
1716
1717	clear_bit(ICE_SERVICE_SCHED, pf->state);
1718	return ret;
1719}
1720
1721/**
1722 * ice_service_task_restart - restart service task and schedule works
1723 * @pf: board private structure
1724 *
1725 * This function is needed for suspend and resume works (e.g WoL scenario)
1726 */
1727static void ice_service_task_restart(struct ice_pf *pf)
1728{
1729	clear_bit(ICE_SERVICE_DIS, pf->state);
1730	ice_service_task_schedule(pf);
1731}
1732
1733/**
1734 * ice_service_timer - timer callback to schedule service task
1735 * @t: pointer to timer_list
1736 */
1737static void ice_service_timer(struct timer_list *t)
1738{
1739	struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1740
1741	mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1742	ice_service_task_schedule(pf);
1743}
1744
1745/**
1746 * ice_handle_mdd_event - handle malicious driver detect event
1747 * @pf: pointer to the PF structure
1748 *
1749 * Called from service task. OICR interrupt handler indicates MDD event.
1750 * VF MDD logging is guarded by net_ratelimit. Additional PF and VF log
1751 * messages are wrapped by netif_msg_[rx|tx]_err. Since VF Rx MDD events
1752 * disable the queue, the PF can be configured to reset the VF using ethtool
1753 * private flag mdd-auto-reset-vf.
1754 */
1755static void ice_handle_mdd_event(struct ice_pf *pf)
1756{
1757	struct device *dev = ice_pf_to_dev(pf);
1758	struct ice_hw *hw = &pf->hw;
1759	struct ice_vf *vf;
1760	unsigned int bkt;
1761	u32 reg;
1762
1763	if (!test_and_clear_bit(ICE_MDD_EVENT_PENDING, pf->state)) {
1764		/* Since the VF MDD event logging is rate limited, check if
1765		 * there are pending MDD events.
1766		 */
1767		ice_print_vfs_mdd_events(pf);
1768		return;
1769	}
1770
1771	/* find what triggered an MDD event */
1772	reg = rd32(hw, GL_MDET_TX_PQM);
1773	if (reg & GL_MDET_TX_PQM_VALID_M) {
1774		u8 pf_num = FIELD_GET(GL_MDET_TX_PQM_PF_NUM_M, reg);
1775		u16 vf_num = FIELD_GET(GL_MDET_TX_PQM_VF_NUM_M, reg);
1776		u8 event = FIELD_GET(GL_MDET_TX_PQM_MAL_TYPE_M, reg);
1777		u16 queue = FIELD_GET(GL_MDET_TX_PQM_QNUM_M, reg);
 
 
 
 
1778
1779		if (netif_msg_tx_err(pf))
1780			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1781				 event, queue, pf_num, vf_num);
1782		wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1783	}
1784
1785	reg = rd32(hw, GL_MDET_TX_TCLAN_BY_MAC(hw));
1786	if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1787		u8 pf_num = FIELD_GET(GL_MDET_TX_TCLAN_PF_NUM_M, reg);
1788		u16 vf_num = FIELD_GET(GL_MDET_TX_TCLAN_VF_NUM_M, reg);
1789		u8 event = FIELD_GET(GL_MDET_TX_TCLAN_MAL_TYPE_M, reg);
1790		u16 queue = FIELD_GET(GL_MDET_TX_TCLAN_QNUM_M, reg);
 
 
 
 
1791
1792		if (netif_msg_tx_err(pf))
1793			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1794				 event, queue, pf_num, vf_num);
1795		wr32(hw, GL_MDET_TX_TCLAN_BY_MAC(hw), U32_MAX);
1796	}
1797
1798	reg = rd32(hw, GL_MDET_RX);
1799	if (reg & GL_MDET_RX_VALID_M) {
1800		u8 pf_num = FIELD_GET(GL_MDET_RX_PF_NUM_M, reg);
1801		u16 vf_num = FIELD_GET(GL_MDET_RX_VF_NUM_M, reg);
1802		u8 event = FIELD_GET(GL_MDET_RX_MAL_TYPE_M, reg);
1803		u16 queue = FIELD_GET(GL_MDET_RX_QNUM_M, reg);
 
 
 
 
1804
1805		if (netif_msg_rx_err(pf))
1806			dev_info(dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1807				 event, queue, pf_num, vf_num);
1808		wr32(hw, GL_MDET_RX, 0xffffffff);
1809	}
1810
1811	/* check to see if this PF caused an MDD event */
1812	reg = rd32(hw, PF_MDET_TX_PQM);
1813	if (reg & PF_MDET_TX_PQM_VALID_M) {
1814		wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1815		if (netif_msg_tx_err(pf))
1816			dev_info(dev, "Malicious Driver Detection event TX_PQM detected on PF\n");
1817	}
1818
1819	reg = rd32(hw, PF_MDET_TX_TCLAN_BY_MAC(hw));
1820	if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1821		wr32(hw, PF_MDET_TX_TCLAN_BY_MAC(hw), 0xffff);
1822		if (netif_msg_tx_err(pf))
1823			dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on PF\n");
1824	}
1825
1826	reg = rd32(hw, PF_MDET_RX);
1827	if (reg & PF_MDET_RX_VALID_M) {
1828		wr32(hw, PF_MDET_RX, 0xFFFF);
1829		if (netif_msg_rx_err(pf))
1830			dev_info(dev, "Malicious Driver Detection event RX detected on PF\n");
1831	}
1832
1833	/* Check to see if one of the VFs caused an MDD event, and then
1834	 * increment counters and set print pending
1835	 */
1836	mutex_lock(&pf->vfs.table_lock);
1837	ice_for_each_vf(pf, bkt, vf) {
1838		reg = rd32(hw, VP_MDET_TX_PQM(vf->vf_id));
1839		if (reg & VP_MDET_TX_PQM_VALID_M) {
1840			wr32(hw, VP_MDET_TX_PQM(vf->vf_id), 0xFFFF);
1841			vf->mdd_tx_events.count++;
1842			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1843			if (netif_msg_tx_err(pf))
1844				dev_info(dev, "Malicious Driver Detection event TX_PQM detected on VF %d\n",
1845					 vf->vf_id);
1846		}
1847
1848		reg = rd32(hw, VP_MDET_TX_TCLAN(vf->vf_id));
1849		if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1850			wr32(hw, VP_MDET_TX_TCLAN(vf->vf_id), 0xFFFF);
1851			vf->mdd_tx_events.count++;
1852			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1853			if (netif_msg_tx_err(pf))
1854				dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on VF %d\n",
1855					 vf->vf_id);
1856		}
1857
1858		reg = rd32(hw, VP_MDET_TX_TDPU(vf->vf_id));
1859		if (reg & VP_MDET_TX_TDPU_VALID_M) {
1860			wr32(hw, VP_MDET_TX_TDPU(vf->vf_id), 0xFFFF);
1861			vf->mdd_tx_events.count++;
1862			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1863			if (netif_msg_tx_err(pf))
1864				dev_info(dev, "Malicious Driver Detection event TX_TDPU detected on VF %d\n",
1865					 vf->vf_id);
1866		}
1867
1868		reg = rd32(hw, VP_MDET_RX(vf->vf_id));
1869		if (reg & VP_MDET_RX_VALID_M) {
1870			wr32(hw, VP_MDET_RX(vf->vf_id), 0xFFFF);
1871			vf->mdd_rx_events.count++;
1872			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1873			if (netif_msg_rx_err(pf))
1874				dev_info(dev, "Malicious Driver Detection event RX detected on VF %d\n",
1875					 vf->vf_id);
1876
1877			/* Since the queue is disabled on VF Rx MDD events, the
1878			 * PF can be configured to reset the VF through ethtool
1879			 * private flag mdd-auto-reset-vf.
1880			 */
1881			if (test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
1882				/* VF MDD event counters will be cleared by
1883				 * reset, so print the event prior to reset.
1884				 */
1885				ice_print_vf_rx_mdd_event(vf);
1886				ice_reset_vf(vf, ICE_VF_RESET_LOCK);
1887			}
1888		}
1889	}
1890	mutex_unlock(&pf->vfs.table_lock);
1891
1892	ice_print_vfs_mdd_events(pf);
1893}
1894
1895/**
1896 * ice_force_phys_link_state - Force the physical link state
1897 * @vsi: VSI to force the physical link state to up/down
1898 * @link_up: true/false indicates to set the physical link to up/down
1899 *
1900 * Force the physical link state by getting the current PHY capabilities from
1901 * hardware and setting the PHY config based on the determined capabilities. If
1902 * link changes a link event will be triggered because both the Enable Automatic
1903 * Link Update and LESM Enable bits are set when setting the PHY capabilities.
1904 *
1905 * Returns 0 on success, negative on failure
1906 */
1907static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
1908{
1909	struct ice_aqc_get_phy_caps_data *pcaps;
1910	struct ice_aqc_set_phy_cfg_data *cfg;
1911	struct ice_port_info *pi;
1912	struct device *dev;
1913	int retcode;
1914
1915	if (!vsi || !vsi->port_info || !vsi->back)
1916		return -EINVAL;
1917	if (vsi->type != ICE_VSI_PF)
1918		return 0;
1919
1920	dev = ice_pf_to_dev(vsi->back);
1921
1922	pi = vsi->port_info;
1923
1924	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1925	if (!pcaps)
1926		return -ENOMEM;
1927
1928	retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
1929				      NULL);
1930	if (retcode) {
1931		dev_err(dev, "Failed to get phy capabilities, VSI %d error %d\n",
1932			vsi->vsi_num, retcode);
1933		retcode = -EIO;
1934		goto out;
1935	}
1936
1937	/* No change in link */
1938	if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
1939	    link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
1940		goto out;
1941
1942	/* Use the current user PHY configuration. The current user PHY
1943	 * configuration is initialized during probe from PHY capabilities
1944	 * software mode, and updated on set PHY configuration.
1945	 */
1946	cfg = kmemdup(&pi->phy.curr_user_phy_cfg, sizeof(*cfg), GFP_KERNEL);
1947	if (!cfg) {
1948		retcode = -ENOMEM;
1949		goto out;
1950	}
1951
1952	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1953	if (link_up)
1954		cfg->caps |= ICE_AQ_PHY_ENA_LINK;
1955	else
1956		cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
1957
1958	retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
1959	if (retcode) {
1960		dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
1961			vsi->vsi_num, retcode);
1962		retcode = -EIO;
1963	}
1964
1965	kfree(cfg);
1966out:
1967	kfree(pcaps);
1968	return retcode;
1969}
1970
1971/**
1972 * ice_init_nvm_phy_type - Initialize the NVM PHY type
1973 * @pi: port info structure
1974 *
1975 * Initialize nvm_phy_type_[low|high] for link lenient mode support
1976 */
1977static int ice_init_nvm_phy_type(struct ice_port_info *pi)
1978{
1979	struct ice_aqc_get_phy_caps_data *pcaps;
1980	struct ice_pf *pf = pi->hw->back;
1981	int err;
1982
1983	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1984	if (!pcaps)
1985		return -ENOMEM;
1986
1987	err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA,
1988				  pcaps, NULL);
1989
1990	if (err) {
1991		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
1992		goto out;
1993	}
1994
1995	pf->nvm_phy_type_hi = pcaps->phy_type_high;
1996	pf->nvm_phy_type_lo = pcaps->phy_type_low;
1997
1998out:
1999	kfree(pcaps);
2000	return err;
2001}
2002
2003/**
2004 * ice_init_link_dflt_override - Initialize link default override
2005 * @pi: port info structure
2006 *
2007 * Initialize link default override and PHY total port shutdown during probe
2008 */
2009static void ice_init_link_dflt_override(struct ice_port_info *pi)
2010{
2011	struct ice_link_default_override_tlv *ldo;
2012	struct ice_pf *pf = pi->hw->back;
2013
2014	ldo = &pf->link_dflt_override;
2015	if (ice_get_link_default_override(ldo, pi))
2016		return;
2017
2018	if (!(ldo->options & ICE_LINK_OVERRIDE_PORT_DIS))
2019		return;
2020
2021	/* Enable Total Port Shutdown (override/replace link-down-on-close
2022	 * ethtool private flag) for ports with Port Disable bit set.
2023	 */
2024	set_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
2025	set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
2026}
2027
2028/**
2029 * ice_init_phy_cfg_dflt_override - Initialize PHY cfg default override settings
2030 * @pi: port info structure
2031 *
2032 * If default override is enabled, initialize the user PHY cfg speed and FEC
2033 * settings using the default override mask from the NVM.
2034 *
2035 * The PHY should only be configured with the default override settings the
2036 * first time media is available. The ICE_LINK_DEFAULT_OVERRIDE_PENDING state
2037 * is used to indicate that the user PHY cfg default override is initialized
2038 * and the PHY has not been configured with the default override settings. The
2039 * state is set here, and cleared in ice_configure_phy the first time the PHY is
2040 * configured.
2041 *
2042 * This function should be called only if the FW doesn't support default
2043 * configuration mode, as reported by ice_fw_supports_report_dflt_cfg.
2044 */
2045static void ice_init_phy_cfg_dflt_override(struct ice_port_info *pi)
2046{
2047	struct ice_link_default_override_tlv *ldo;
2048	struct ice_aqc_set_phy_cfg_data *cfg;
2049	struct ice_phy_info *phy = &pi->phy;
2050	struct ice_pf *pf = pi->hw->back;
2051
2052	ldo = &pf->link_dflt_override;
2053
2054	/* If link default override is enabled, use to mask NVM PHY capabilities
2055	 * for speed and FEC default configuration.
2056	 */
2057	cfg = &phy->curr_user_phy_cfg;
2058
2059	if (ldo->phy_type_low || ldo->phy_type_high) {
2060		cfg->phy_type_low = pf->nvm_phy_type_lo &
2061				    cpu_to_le64(ldo->phy_type_low);
2062		cfg->phy_type_high = pf->nvm_phy_type_hi &
2063				     cpu_to_le64(ldo->phy_type_high);
2064	}
2065	cfg->link_fec_opt = ldo->fec_options;
2066	phy->curr_user_fec_req = ICE_FEC_AUTO;
2067
2068	set_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING, pf->state);
2069}
2070
2071/**
2072 * ice_init_phy_user_cfg - Initialize the PHY user configuration
2073 * @pi: port info structure
2074 *
2075 * Initialize the current user PHY configuration, speed, FEC, and FC requested
2076 * mode to default. The PHY defaults are from get PHY capabilities topology
2077 * with media so call when media is first available. An error is returned if
2078 * called when media is not available. The PHY initialization completed state is
2079 * set here.
2080 *
2081 * These configurations are used when setting PHY
2082 * configuration. The user PHY configuration is updated on set PHY
2083 * configuration. Returns 0 on success, negative on failure
2084 */
2085static int ice_init_phy_user_cfg(struct ice_port_info *pi)
2086{
2087	struct ice_aqc_get_phy_caps_data *pcaps;
2088	struct ice_phy_info *phy = &pi->phy;
2089	struct ice_pf *pf = pi->hw->back;
2090	int err;
2091
2092	if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
2093		return -EIO;
2094
2095	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2096	if (!pcaps)
2097		return -ENOMEM;
2098
2099	if (ice_fw_supports_report_dflt_cfg(pi->hw))
2100		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2101					  pcaps, NULL);
2102	else
2103		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2104					  pcaps, NULL);
2105	if (err) {
2106		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
2107		goto err_out;
2108	}
2109
2110	ice_copy_phy_caps_to_cfg(pi, pcaps, &pi->phy.curr_user_phy_cfg);
2111
2112	/* check if lenient mode is supported and enabled */
2113	if (ice_fw_supports_link_override(pi->hw) &&
2114	    !(pcaps->module_compliance_enforcement &
2115	      ICE_AQC_MOD_ENFORCE_STRICT_MODE)) {
2116		set_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags);
2117
2118		/* if the FW supports default PHY configuration mode, then the driver
2119		 * does not have to apply link override settings. If not,
2120		 * initialize user PHY configuration with link override values
2121		 */
2122		if (!ice_fw_supports_report_dflt_cfg(pi->hw) &&
2123		    (pf->link_dflt_override.options & ICE_LINK_OVERRIDE_EN)) {
2124			ice_init_phy_cfg_dflt_override(pi);
2125			goto out;
2126		}
2127	}
2128
2129	/* if link default override is not enabled, set user flow control and
2130	 * FEC settings based on what get_phy_caps returned
2131	 */
2132	phy->curr_user_fec_req = ice_caps_to_fec_mode(pcaps->caps,
2133						      pcaps->link_fec_options);
2134	phy->curr_user_fc_req = ice_caps_to_fc_mode(pcaps->caps);
2135
2136out:
2137	phy->curr_user_speed_req = ICE_AQ_LINK_SPEED_M;
2138	set_bit(ICE_PHY_INIT_COMPLETE, pf->state);
2139err_out:
2140	kfree(pcaps);
2141	return err;
2142}
2143
2144/**
2145 * ice_configure_phy - configure PHY
2146 * @vsi: VSI of PHY
2147 *
2148 * Set the PHY configuration. If the current PHY configuration is the same as
2149 * the curr_user_phy_cfg, then do nothing to avoid link flap. Otherwise
2150 * configure the based get PHY capabilities for topology with media.
2151 */
2152static int ice_configure_phy(struct ice_vsi *vsi)
2153{
2154	struct device *dev = ice_pf_to_dev(vsi->back);
2155	struct ice_port_info *pi = vsi->port_info;
2156	struct ice_aqc_get_phy_caps_data *pcaps;
2157	struct ice_aqc_set_phy_cfg_data *cfg;
2158	struct ice_phy_info *phy = &pi->phy;
2159	struct ice_pf *pf = vsi->back;
2160	int err;
2161
2162	/* Ensure we have media as we cannot configure a medialess port */
2163	if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
2164		return -ENOMEDIUM;
2165
2166	ice_print_topo_conflict(vsi);
2167
2168	if (!test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags) &&
2169	    phy->link_info.topo_media_conflict == ICE_AQ_LINK_TOPO_UNSUPP_MEDIA)
2170		return -EPERM;
2171
2172	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))
2173		return ice_force_phys_link_state(vsi, true);
2174
2175	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2176	if (!pcaps)
2177		return -ENOMEM;
2178
2179	/* Get current PHY config */
2180	err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
2181				  NULL);
2182	if (err) {
2183		dev_err(dev, "Failed to get PHY configuration, VSI %d error %d\n",
2184			vsi->vsi_num, err);
2185		goto done;
2186	}
2187
2188	/* If PHY enable link is configured and configuration has not changed,
2189	 * there's nothing to do
2190	 */
2191	if (pcaps->caps & ICE_AQC_PHY_EN_LINK &&
2192	    ice_phy_caps_equals_cfg(pcaps, &phy->curr_user_phy_cfg))
2193		goto done;
2194
2195	/* Use PHY topology as baseline for configuration */
2196	memset(pcaps, 0, sizeof(*pcaps));
2197	if (ice_fw_supports_report_dflt_cfg(pi->hw))
2198		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2199					  pcaps, NULL);
2200	else
2201		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2202					  pcaps, NULL);
2203	if (err) {
2204		dev_err(dev, "Failed to get PHY caps, VSI %d error %d\n",
2205			vsi->vsi_num, err);
2206		goto done;
2207	}
2208
2209	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
2210	if (!cfg) {
2211		err = -ENOMEM;
2212		goto done;
2213	}
2214
2215	ice_copy_phy_caps_to_cfg(pi, pcaps, cfg);
2216
2217	/* Speed - If default override pending, use curr_user_phy_cfg set in
2218	 * ice_init_phy_user_cfg_ldo.
2219	 */
2220	if (test_and_clear_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING,
2221			       vsi->back->state)) {
2222		cfg->phy_type_low = phy->curr_user_phy_cfg.phy_type_low;
2223		cfg->phy_type_high = phy->curr_user_phy_cfg.phy_type_high;
2224	} else {
2225		u64 phy_low = 0, phy_high = 0;
2226
2227		ice_update_phy_type(&phy_low, &phy_high,
2228				    pi->phy.curr_user_speed_req);
2229		cfg->phy_type_low = pcaps->phy_type_low & cpu_to_le64(phy_low);
2230		cfg->phy_type_high = pcaps->phy_type_high &
2231				     cpu_to_le64(phy_high);
2232	}
2233
2234	/* Can't provide what was requested; use PHY capabilities */
2235	if (!cfg->phy_type_low && !cfg->phy_type_high) {
2236		cfg->phy_type_low = pcaps->phy_type_low;
2237		cfg->phy_type_high = pcaps->phy_type_high;
2238	}
2239
2240	/* FEC */
2241	ice_cfg_phy_fec(pi, cfg, phy->curr_user_fec_req);
2242
2243	/* Can't provide what was requested; use PHY capabilities */
2244	if (cfg->link_fec_opt !=
2245	    (cfg->link_fec_opt & pcaps->link_fec_options)) {
2246		cfg->caps |= pcaps->caps & ICE_AQC_PHY_EN_AUTO_FEC;
2247		cfg->link_fec_opt = pcaps->link_fec_options;
2248	}
2249
2250	/* Flow Control - always supported; no need to check against
2251	 * capabilities
2252	 */
2253	ice_cfg_phy_fc(pi, cfg, phy->curr_user_fc_req);
2254
2255	/* Enable link and link update */
2256	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT | ICE_AQ_PHY_ENA_LINK;
2257
2258	err = ice_aq_set_phy_cfg(&pf->hw, pi, cfg, NULL);
2259	if (err)
2260		dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
2261			vsi->vsi_num, err);
2262
2263	kfree(cfg);
2264done:
2265	kfree(pcaps);
2266	return err;
2267}
2268
2269/**
2270 * ice_check_media_subtask - Check for media
2271 * @pf: pointer to PF struct
2272 *
2273 * If media is available, then initialize PHY user configuration if it is not
2274 * been, and configure the PHY if the interface is up.
2275 */
2276static void ice_check_media_subtask(struct ice_pf *pf)
2277{
2278	struct ice_port_info *pi;
2279	struct ice_vsi *vsi;
2280	int err;
2281
2282	/* No need to check for media if it's already present */
2283	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags))
2284		return;
2285
2286	vsi = ice_get_main_vsi(pf);
2287	if (!vsi)
2288		return;
2289
2290	/* Refresh link info and check if media is present */
2291	pi = vsi->port_info;
2292	err = ice_update_link_info(pi);
2293	if (err)
2294		return;
2295
2296	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
2297
2298	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
2299		if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state))
2300			ice_init_phy_user_cfg(pi);
2301
2302		/* PHY settings are reset on media insertion, reconfigure
2303		 * PHY to preserve settings.
2304		 */
2305		if (test_bit(ICE_VSI_DOWN, vsi->state) &&
2306		    test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
2307			return;
2308
2309		err = ice_configure_phy(vsi);
2310		if (!err)
2311			clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
2312
2313		/* A Link Status Event will be generated; the event handler
2314		 * will complete bringing the interface up
2315		 */
2316	}
2317}
2318
2319/**
2320 * ice_service_task - manage and run subtasks
2321 * @work: pointer to work_struct contained by the PF struct
2322 */
2323static void ice_service_task(struct work_struct *work)
2324{
2325	struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
2326	unsigned long start_time = jiffies;
2327
2328	/* subtasks */
2329
2330	/* process reset requests first */
2331	ice_reset_subtask(pf);
2332
2333	/* bail if a reset/recovery cycle is pending or rebuild failed */
2334	if (ice_is_reset_in_progress(pf->state) ||
2335	    test_bit(ICE_SUSPENDED, pf->state) ||
2336	    test_bit(ICE_NEEDS_RESTART, pf->state)) {
2337		ice_service_task_complete(pf);
2338		return;
2339	}
2340
2341	if (test_and_clear_bit(ICE_AUX_ERR_PENDING, pf->state)) {
2342		struct iidc_event *event;
2343
2344		event = kzalloc(sizeof(*event), GFP_KERNEL);
2345		if (event) {
2346			set_bit(IIDC_EVENT_CRIT_ERR, event->type);
2347			/* report the entire OICR value to AUX driver */
2348			swap(event->reg, pf->oicr_err_reg);
2349			ice_send_event_to_aux(pf, event);
2350			kfree(event);
2351		}
2352	}
2353
2354	/* unplug aux dev per request, if an unplug request came in
2355	 * while processing a plug request, this will handle it
2356	 */
2357	if (test_and_clear_bit(ICE_FLAG_UNPLUG_AUX_DEV, pf->flags))
2358		ice_unplug_aux_dev(pf);
2359
2360	/* Plug aux device per request */
2361	if (test_and_clear_bit(ICE_FLAG_PLUG_AUX_DEV, pf->flags))
2362		ice_plug_aux_dev(pf);
2363
 
 
 
 
 
 
 
 
 
2364	if (test_and_clear_bit(ICE_FLAG_MTU_CHANGED, pf->flags)) {
2365		struct iidc_event *event;
2366
2367		event = kzalloc(sizeof(*event), GFP_KERNEL);
2368		if (event) {
2369			set_bit(IIDC_EVENT_AFTER_MTU_CHANGE, event->type);
2370			ice_send_event_to_aux(pf, event);
2371			kfree(event);
2372		}
2373	}
2374
2375	ice_clean_adminq_subtask(pf);
2376	ice_check_media_subtask(pf);
2377	ice_check_for_hang_subtask(pf);
2378	ice_sync_fltr_subtask(pf);
2379	ice_handle_mdd_event(pf);
2380	ice_watchdog_subtask(pf);
2381
2382	if (ice_is_safe_mode(pf)) {
2383		ice_service_task_complete(pf);
2384		return;
2385	}
2386
2387	ice_process_vflr_event(pf);
2388	ice_clean_mailboxq_subtask(pf);
2389	ice_clean_sbq_subtask(pf);
2390	ice_sync_arfs_fltrs(pf);
2391	ice_flush_fdir_ctx(pf);
2392
2393	/* Clear ICE_SERVICE_SCHED flag to allow scheduling next event */
2394	ice_service_task_complete(pf);
2395
2396	/* If the tasks have taken longer than one service timer period
2397	 * or there is more work to be done, reset the service timer to
2398	 * schedule the service task now.
2399	 */
2400	if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
2401	    test_bit(ICE_MDD_EVENT_PENDING, pf->state) ||
2402	    test_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
2403	    test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
2404	    test_bit(ICE_FD_VF_FLUSH_CTX, pf->state) ||
2405	    test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state) ||
2406	    test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
2407		mod_timer(&pf->serv_tmr, jiffies);
2408}
2409
2410/**
2411 * ice_set_ctrlq_len - helper function to set controlq length
2412 * @hw: pointer to the HW instance
2413 */
2414static void ice_set_ctrlq_len(struct ice_hw *hw)
2415{
2416	hw->adminq.num_rq_entries = ICE_AQ_LEN;
2417	hw->adminq.num_sq_entries = ICE_AQ_LEN;
2418	hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
2419	hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
2420	hw->mailboxq.num_rq_entries = PF_MBX_ARQLEN_ARQLEN_M;
2421	hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN;
2422	hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2423	hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2424	hw->sbq.num_rq_entries = ICE_SBQ_LEN;
2425	hw->sbq.num_sq_entries = ICE_SBQ_LEN;
2426	hw->sbq.rq_buf_size = ICE_SBQ_MAX_BUF_LEN;
2427	hw->sbq.sq_buf_size = ICE_SBQ_MAX_BUF_LEN;
2428}
2429
2430/**
2431 * ice_schedule_reset - schedule a reset
2432 * @pf: board private structure
2433 * @reset: reset being requested
2434 */
2435int ice_schedule_reset(struct ice_pf *pf, enum ice_reset_req reset)
2436{
2437	struct device *dev = ice_pf_to_dev(pf);
2438
2439	/* bail out if earlier reset has failed */
2440	if (test_bit(ICE_RESET_FAILED, pf->state)) {
2441		dev_dbg(dev, "earlier reset has failed\n");
2442		return -EIO;
2443	}
2444	/* bail if reset/recovery already in progress */
2445	if (ice_is_reset_in_progress(pf->state)) {
2446		dev_dbg(dev, "Reset already in progress\n");
2447		return -EBUSY;
2448	}
2449
2450	switch (reset) {
2451	case ICE_RESET_PFR:
2452		set_bit(ICE_PFR_REQ, pf->state);
2453		break;
2454	case ICE_RESET_CORER:
2455		set_bit(ICE_CORER_REQ, pf->state);
2456		break;
2457	case ICE_RESET_GLOBR:
2458		set_bit(ICE_GLOBR_REQ, pf->state);
2459		break;
2460	default:
2461		return -EINVAL;
2462	}
2463
2464	ice_service_task_schedule(pf);
2465	return 0;
2466}
2467
2468/**
2469 * ice_irq_affinity_notify - Callback for affinity changes
2470 * @notify: context as to what irq was changed
2471 * @mask: the new affinity mask
2472 *
2473 * This is a callback function used by the irq_set_affinity_notifier function
2474 * so that we may register to receive changes to the irq affinity masks.
2475 */
2476static void
2477ice_irq_affinity_notify(struct irq_affinity_notify *notify,
2478			const cpumask_t *mask)
2479{
2480	struct ice_q_vector *q_vector =
2481		container_of(notify, struct ice_q_vector, affinity_notify);
2482
2483	cpumask_copy(&q_vector->affinity_mask, mask);
2484}
2485
2486/**
2487 * ice_irq_affinity_release - Callback for affinity notifier release
2488 * @ref: internal core kernel usage
2489 *
2490 * This is a callback function used by the irq_set_affinity_notifier function
2491 * to inform the current notification subscriber that they will no longer
2492 * receive notifications.
2493 */
2494static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
2495
2496/**
2497 * ice_vsi_ena_irq - Enable IRQ for the given VSI
2498 * @vsi: the VSI being configured
2499 */
2500static int ice_vsi_ena_irq(struct ice_vsi *vsi)
2501{
2502	struct ice_hw *hw = &vsi->back->hw;
2503	int i;
2504
2505	ice_for_each_q_vector(vsi, i)
2506		ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
2507
2508	ice_flush(hw);
2509	return 0;
2510}
2511
2512/**
2513 * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
2514 * @vsi: the VSI being configured
2515 * @basename: name for the vector
2516 */
2517static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
2518{
2519	int q_vectors = vsi->num_q_vectors;
2520	struct ice_pf *pf = vsi->back;
 
2521	struct device *dev;
2522	int rx_int_idx = 0;
2523	int tx_int_idx = 0;
2524	int vector, err;
2525	int irq_num;
2526
2527	dev = ice_pf_to_dev(pf);
2528	for (vector = 0; vector < q_vectors; vector++) {
2529		struct ice_q_vector *q_vector = vsi->q_vectors[vector];
2530
2531		irq_num = q_vector->irq.virq;
2532
2533		if (q_vector->tx.tx_ring && q_vector->rx.rx_ring) {
2534			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2535				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2536			tx_int_idx++;
2537		} else if (q_vector->rx.rx_ring) {
2538			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2539				 "%s-%s-%d", basename, "rx", rx_int_idx++);
2540		} else if (q_vector->tx.tx_ring) {
2541			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2542				 "%s-%s-%d", basename, "tx", tx_int_idx++);
2543		} else {
2544			/* skip this unused q_vector */
2545			continue;
2546		}
2547		if (vsi->type == ICE_VSI_CTRL && vsi->vf)
2548			err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2549					       IRQF_SHARED, q_vector->name,
2550					       q_vector);
2551		else
2552			err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2553					       0, q_vector->name, q_vector);
2554		if (err) {
2555			netdev_err(vsi->netdev, "MSIX request_irq failed, error: %d\n",
2556				   err);
2557			goto free_q_irqs;
2558		}
2559
2560		/* register for affinity change notifications */
2561		if (!IS_ENABLED(CONFIG_RFS_ACCEL)) {
2562			struct irq_affinity_notify *affinity_notify;
2563
2564			affinity_notify = &q_vector->affinity_notify;
2565			affinity_notify->notify = ice_irq_affinity_notify;
2566			affinity_notify->release = ice_irq_affinity_release;
2567			irq_set_affinity_notifier(irq_num, affinity_notify);
2568		}
2569
2570		/* assign the mask for this irq */
2571		irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
2572	}
2573
2574	err = ice_set_cpu_rx_rmap(vsi);
2575	if (err) {
2576		netdev_err(vsi->netdev, "Failed to setup CPU RMAP on VSI %u: %pe\n",
2577			   vsi->vsi_num, ERR_PTR(err));
2578		goto free_q_irqs;
2579	}
2580
2581	vsi->irqs_ready = true;
2582	return 0;
2583
2584free_q_irqs:
2585	while (vector--) {
2586		irq_num = vsi->q_vectors[vector]->irq.virq;
 
2587		if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2588			irq_set_affinity_notifier(irq_num, NULL);
2589		irq_set_affinity_hint(irq_num, NULL);
2590		devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]);
2591	}
2592	return err;
2593}
2594
2595/**
2596 * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
2597 * @vsi: VSI to setup Tx rings used by XDP
2598 *
2599 * Return 0 on success and negative value on error
2600 */
2601static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
2602{
2603	struct device *dev = ice_pf_to_dev(vsi->back);
2604	struct ice_tx_desc *tx_desc;
2605	int i, j;
2606
2607	ice_for_each_xdp_txq(vsi, i) {
2608		u16 xdp_q_idx = vsi->alloc_txq + i;
2609		struct ice_ring_stats *ring_stats;
2610		struct ice_tx_ring *xdp_ring;
2611
2612		xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
2613		if (!xdp_ring)
2614			goto free_xdp_rings;
2615
2616		ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
2617		if (!ring_stats) {
2618			ice_free_tx_ring(xdp_ring);
2619			goto free_xdp_rings;
2620		}
2621
2622		xdp_ring->ring_stats = ring_stats;
2623		xdp_ring->q_index = xdp_q_idx;
2624		xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
2625		xdp_ring->vsi = vsi;
2626		xdp_ring->netdev = NULL;
2627		xdp_ring->dev = dev;
2628		xdp_ring->count = vsi->num_tx_desc;
 
 
2629		WRITE_ONCE(vsi->xdp_rings[i], xdp_ring);
2630		if (ice_setup_tx_ring(xdp_ring))
2631			goto free_xdp_rings;
2632		ice_set_ring_xdp(xdp_ring);
2633		spin_lock_init(&xdp_ring->tx_lock);
2634		for (j = 0; j < xdp_ring->count; j++) {
2635			tx_desc = ICE_TX_DESC(xdp_ring, j);
2636			tx_desc->cmd_type_offset_bsz = 0;
2637		}
2638	}
2639
2640	return 0;
2641
2642free_xdp_rings:
2643	for (; i >= 0; i--) {
2644		if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc) {
2645			kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
2646			vsi->xdp_rings[i]->ring_stats = NULL;
2647			ice_free_tx_ring(vsi->xdp_rings[i]);
2648		}
2649	}
2650	return -ENOMEM;
2651}
2652
2653/**
2654 * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
2655 * @vsi: VSI to set the bpf prog on
2656 * @prog: the bpf prog pointer
2657 */
2658static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
2659{
2660	struct bpf_prog *old_prog;
2661	int i;
2662
2663	old_prog = xchg(&vsi->xdp_prog, prog);
2664	ice_for_each_rxq(vsi, i)
2665		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
2666
2667	if (old_prog)
2668		bpf_prog_put(old_prog);
 
 
 
2669}
2670
2671/**
2672 * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
2673 * @vsi: VSI to bring up Tx rings used by XDP
2674 * @prog: bpf program that will be assigned to VSI
2675 *
2676 * Return 0 on success and negative value on error
2677 */
2678int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog)
2679{
2680	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2681	int xdp_rings_rem = vsi->num_xdp_txq;
2682	struct ice_pf *pf = vsi->back;
2683	struct ice_qs_cfg xdp_qs_cfg = {
2684		.qs_mutex = &pf->avail_q_mutex,
2685		.pf_map = pf->avail_txqs,
2686		.pf_map_size = pf->max_pf_txqs,
2687		.q_count = vsi->num_xdp_txq,
2688		.scatter_count = ICE_MAX_SCATTER_TXQS,
2689		.vsi_map = vsi->txq_map,
2690		.vsi_map_offset = vsi->alloc_txq,
2691		.mapping_mode = ICE_VSI_MAP_CONTIG
2692	};
2693	struct device *dev;
2694	int i, v_idx;
2695	int status;
2696
2697	dev = ice_pf_to_dev(pf);
2698	vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq,
2699				      sizeof(*vsi->xdp_rings), GFP_KERNEL);
2700	if (!vsi->xdp_rings)
2701		return -ENOMEM;
2702
2703	vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
2704	if (__ice_vsi_get_qs(&xdp_qs_cfg))
2705		goto err_map_xdp;
2706
2707	if (static_key_enabled(&ice_xdp_locking_key))
2708		netdev_warn(vsi->netdev,
2709			    "Could not allocate one XDP Tx ring per CPU, XDP_TX/XDP_REDIRECT actions will be slower\n");
2710
2711	if (ice_xdp_alloc_setup_rings(vsi))
2712		goto clear_xdp_rings;
2713
2714	/* follow the logic from ice_vsi_map_rings_to_vectors */
2715	ice_for_each_q_vector(vsi, v_idx) {
2716		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2717		int xdp_rings_per_v, q_id, q_base;
2718
2719		xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
2720					       vsi->num_q_vectors - v_idx);
2721		q_base = vsi->num_xdp_txq - xdp_rings_rem;
2722
2723		for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
2724			struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_id];
2725
2726			xdp_ring->q_vector = q_vector;
2727			xdp_ring->next = q_vector->tx.tx_ring;
2728			q_vector->tx.tx_ring = xdp_ring;
2729		}
2730		xdp_rings_rem -= xdp_rings_per_v;
2731	}
2732
2733	ice_for_each_rxq(vsi, i) {
2734		if (static_key_enabled(&ice_xdp_locking_key)) {
2735			vsi->rx_rings[i]->xdp_ring = vsi->xdp_rings[i % vsi->num_xdp_txq];
2736		} else {
2737			struct ice_q_vector *q_vector = vsi->rx_rings[i]->q_vector;
2738			struct ice_tx_ring *ring;
2739
2740			ice_for_each_tx_ring(ring, q_vector->tx) {
2741				if (ice_ring_is_xdp(ring)) {
2742					vsi->rx_rings[i]->xdp_ring = ring;
2743					break;
2744				}
2745			}
2746		}
2747		ice_tx_xsk_pool(vsi, i);
2748	}
2749
2750	/* omit the scheduler update if in reset path; XDP queues will be
2751	 * taken into account at the end of ice_vsi_rebuild, where
2752	 * ice_cfg_vsi_lan is being called
2753	 */
2754	if (ice_is_reset_in_progress(pf->state))
2755		return 0;
2756
2757	/* tell the Tx scheduler that right now we have
2758	 * additional queues
2759	 */
2760	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2761		max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
2762
2763	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2764				 max_txqs);
2765	if (status) {
2766		dev_err(dev, "Failed VSI LAN queue config for XDP, error: %d\n",
2767			status);
2768		goto clear_xdp_rings;
2769	}
2770
2771	/* assign the prog only when it's not already present on VSI;
2772	 * this flow is a subject of both ethtool -L and ndo_bpf flows;
2773	 * VSI rebuild that happens under ethtool -L can expose us to
2774	 * the bpf_prog refcount issues as we would be swapping same
2775	 * bpf_prog pointers from vsi->xdp_prog and calling bpf_prog_put
2776	 * on it as it would be treated as an 'old_prog'; for ndo_bpf
2777	 * this is not harmful as dev_xdp_install bumps the refcount
2778	 * before calling the op exposed by the driver;
2779	 */
2780	if (!ice_is_xdp_ena_vsi(vsi))
2781		ice_vsi_assign_bpf_prog(vsi, prog);
2782
2783	return 0;
2784clear_xdp_rings:
2785	ice_for_each_xdp_txq(vsi, i)
2786		if (vsi->xdp_rings[i]) {
2787			kfree_rcu(vsi->xdp_rings[i], rcu);
2788			vsi->xdp_rings[i] = NULL;
2789		}
2790
2791err_map_xdp:
2792	mutex_lock(&pf->avail_q_mutex);
2793	ice_for_each_xdp_txq(vsi, i) {
2794		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2795		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2796	}
2797	mutex_unlock(&pf->avail_q_mutex);
2798
2799	devm_kfree(dev, vsi->xdp_rings);
2800	return -ENOMEM;
2801}
2802
2803/**
2804 * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
2805 * @vsi: VSI to remove XDP rings
2806 *
2807 * Detach XDP rings from irq vectors, clean up the PF bitmap and free
2808 * resources
2809 */
2810int ice_destroy_xdp_rings(struct ice_vsi *vsi)
2811{
2812	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2813	struct ice_pf *pf = vsi->back;
2814	int i, v_idx;
2815
2816	/* q_vectors are freed in reset path so there's no point in detaching
2817	 * rings; in case of rebuild being triggered not from reset bits
2818	 * in pf->state won't be set, so additionally check first q_vector
2819	 * against NULL
2820	 */
2821	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2822		goto free_qmap;
2823
2824	ice_for_each_q_vector(vsi, v_idx) {
2825		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2826		struct ice_tx_ring *ring;
2827
2828		ice_for_each_tx_ring(ring, q_vector->tx)
2829			if (!ring->tx_buf || !ice_ring_is_xdp(ring))
2830				break;
2831
2832		/* restore the value of last node prior to XDP setup */
2833		q_vector->tx.tx_ring = ring;
2834	}
2835
2836free_qmap:
2837	mutex_lock(&pf->avail_q_mutex);
2838	ice_for_each_xdp_txq(vsi, i) {
2839		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2840		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2841	}
2842	mutex_unlock(&pf->avail_q_mutex);
2843
2844	ice_for_each_xdp_txq(vsi, i)
2845		if (vsi->xdp_rings[i]) {
2846			if (vsi->xdp_rings[i]->desc) {
2847				synchronize_rcu();
2848				ice_free_tx_ring(vsi->xdp_rings[i]);
2849			}
2850			kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
2851			vsi->xdp_rings[i]->ring_stats = NULL;
2852			kfree_rcu(vsi->xdp_rings[i], rcu);
2853			vsi->xdp_rings[i] = NULL;
2854		}
2855
2856	devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
2857	vsi->xdp_rings = NULL;
2858
2859	if (static_key_enabled(&ice_xdp_locking_key))
2860		static_branch_dec(&ice_xdp_locking_key);
2861
2862	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2863		return 0;
2864
2865	ice_vsi_assign_bpf_prog(vsi, NULL);
2866
2867	/* notify Tx scheduler that we destroyed XDP queues and bring
2868	 * back the old number of child nodes
2869	 */
2870	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2871		max_txqs[i] = vsi->num_txq;
2872
2873	/* change number of XDP Tx queues to 0 */
2874	vsi->num_xdp_txq = 0;
2875
2876	return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2877			       max_txqs);
2878}
2879
2880/**
2881 * ice_vsi_rx_napi_schedule - Schedule napi on RX queues from VSI
2882 * @vsi: VSI to schedule napi on
2883 */
2884static void ice_vsi_rx_napi_schedule(struct ice_vsi *vsi)
2885{
2886	int i;
2887
2888	ice_for_each_rxq(vsi, i) {
2889		struct ice_rx_ring *rx_ring = vsi->rx_rings[i];
2890
2891		if (rx_ring->xsk_pool)
2892			napi_schedule(&rx_ring->q_vector->napi);
2893	}
2894}
2895
2896/**
2897 * ice_vsi_determine_xdp_res - figure out how many Tx qs can XDP have
2898 * @vsi: VSI to determine the count of XDP Tx qs
2899 *
2900 * returns 0 if Tx qs count is higher than at least half of CPU count,
2901 * -ENOMEM otherwise
2902 */
2903int ice_vsi_determine_xdp_res(struct ice_vsi *vsi)
2904{
2905	u16 avail = ice_get_avail_txq_count(vsi->back);
2906	u16 cpus = num_possible_cpus();
2907
2908	if (avail < cpus / 2)
2909		return -ENOMEM;
2910
2911	vsi->num_xdp_txq = min_t(u16, avail, cpus);
2912
2913	if (vsi->num_xdp_txq < cpus)
2914		static_branch_inc(&ice_xdp_locking_key);
2915
2916	return 0;
2917}
2918
2919/**
2920 * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2921 * @vsi: Pointer to VSI structure
2922 */
2923static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
2924{
2925	if (test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
2926		return ICE_RXBUF_1664;
2927	else
2928		return ICE_RXBUF_3072;
2929}
2930
2931/**
2932 * ice_xdp_setup_prog - Add or remove XDP eBPF program
2933 * @vsi: VSI to setup XDP for
2934 * @prog: XDP program
2935 * @extack: netlink extended ack
2936 */
2937static int
2938ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
2939		   struct netlink_ext_ack *extack)
2940{
2941	unsigned int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
2942	bool if_running = netif_running(vsi->netdev);
2943	int ret = 0, xdp_ring_err = 0;
2944
2945	if (prog && !prog->aux->xdp_has_frags) {
2946		if (frame_size > ice_max_xdp_frame_size(vsi)) {
2947			NL_SET_ERR_MSG_MOD(extack,
2948					   "MTU is too large for linear frames and XDP prog does not support frags");
2949			return -EOPNOTSUPP;
2950		}
2951	}
2952
2953	/* hot swap progs and avoid toggling link */
2954	if (ice_is_xdp_ena_vsi(vsi) == !!prog) {
2955		ice_vsi_assign_bpf_prog(vsi, prog);
2956		return 0;
2957	}
2958
2959	/* need to stop netdev while setting up the program for Rx rings */
2960	if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
2961		ret = ice_down(vsi);
2962		if (ret) {
2963			NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
2964			return ret;
2965		}
2966	}
2967
2968	if (!ice_is_xdp_ena_vsi(vsi) && prog) {
2969		xdp_ring_err = ice_vsi_determine_xdp_res(vsi);
2970		if (xdp_ring_err) {
2971			NL_SET_ERR_MSG_MOD(extack, "Not enough Tx resources for XDP");
2972		} else {
2973			xdp_ring_err = ice_prepare_xdp_rings(vsi, prog);
2974			if (xdp_ring_err)
2975				NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
2976		}
2977		xdp_features_set_redirect_target(vsi->netdev, true);
2978		/* reallocate Rx queues that are used for zero-copy */
2979		xdp_ring_err = ice_realloc_zc_buf(vsi, true);
2980		if (xdp_ring_err)
2981			NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Rx resources failed");
2982	} else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
2983		xdp_features_clear_redirect_target(vsi->netdev);
2984		xdp_ring_err = ice_destroy_xdp_rings(vsi);
2985		if (xdp_ring_err)
2986			NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
2987		/* reallocate Rx queues that were used for zero-copy */
2988		xdp_ring_err = ice_realloc_zc_buf(vsi, false);
2989		if (xdp_ring_err)
2990			NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Rx resources failed");
 
 
 
 
 
 
 
2991	}
2992
2993	if (if_running)
2994		ret = ice_up(vsi);
2995
2996	if (!ret && prog)
2997		ice_vsi_rx_napi_schedule(vsi);
2998
2999	return (ret || xdp_ring_err) ? -ENOMEM : 0;
3000}
3001
3002/**
3003 * ice_xdp_safe_mode - XDP handler for safe mode
3004 * @dev: netdevice
3005 * @xdp: XDP command
3006 */
3007static int ice_xdp_safe_mode(struct net_device __always_unused *dev,
3008			     struct netdev_bpf *xdp)
3009{
3010	NL_SET_ERR_MSG_MOD(xdp->extack,
3011			   "Please provide working DDP firmware package in order to use XDP\n"
3012			   "Refer to Documentation/networking/device_drivers/ethernet/intel/ice.rst");
3013	return -EOPNOTSUPP;
3014}
3015
3016/**
3017 * ice_xdp - implements XDP handler
3018 * @dev: netdevice
3019 * @xdp: XDP command
3020 */
3021static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3022{
3023	struct ice_netdev_priv *np = netdev_priv(dev);
3024	struct ice_vsi *vsi = np->vsi;
3025
3026	if (vsi->type != ICE_VSI_PF) {
3027		NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF VSI");
3028		return -EINVAL;
3029	}
3030
3031	switch (xdp->command) {
3032	case XDP_SETUP_PROG:
3033		return ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
3034	case XDP_SETUP_XSK_POOL:
3035		return ice_xsk_pool_setup(vsi, xdp->xsk.pool,
3036					  xdp->xsk.queue_id);
3037	default:
3038		return -EINVAL;
3039	}
3040}
3041
3042/**
3043 * ice_ena_misc_vector - enable the non-queue interrupts
3044 * @pf: board private structure
3045 */
3046static void ice_ena_misc_vector(struct ice_pf *pf)
3047{
3048	struct ice_hw *hw = &pf->hw;
3049	u32 pf_intr_start_offset;
3050	u32 val;
3051
3052	/* Disable anti-spoof detection interrupt to prevent spurious event
3053	 * interrupts during a function reset. Anti-spoof functionally is
3054	 * still supported.
3055	 */
3056	val = rd32(hw, GL_MDCK_TX_TDPU);
3057	val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
3058	wr32(hw, GL_MDCK_TX_TDPU, val);
3059
3060	/* clear things first */
3061	wr32(hw, PFINT_OICR_ENA, 0);	/* disable all */
3062	rd32(hw, PFINT_OICR);		/* read to clear */
3063
3064	val = (PFINT_OICR_ECC_ERR_M |
3065	       PFINT_OICR_MAL_DETECT_M |
3066	       PFINT_OICR_GRST_M |
3067	       PFINT_OICR_PCI_EXCEPTION_M |
3068	       PFINT_OICR_VFLR_M |
3069	       PFINT_OICR_HMC_ERR_M |
3070	       PFINT_OICR_PE_PUSH_M |
3071	       PFINT_OICR_PE_CRITERR_M);
3072
3073	wr32(hw, PFINT_OICR_ENA, val);
3074
3075	/* SW_ITR_IDX = 0, but don't change INTENA */
3076	wr32(hw, GLINT_DYN_CTL(pf->oicr_irq.index),
3077	     GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
3078
3079	if (!pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3080		return;
3081	pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
3082	wr32(hw, GLINT_DYN_CTL(pf->ll_ts_irq.index + pf_intr_start_offset),
3083	     GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
3084}
3085
3086/**
3087 * ice_ll_ts_intr - ll_ts interrupt handler
3088 * @irq: interrupt number
3089 * @data: pointer to a q_vector
3090 */
3091static irqreturn_t ice_ll_ts_intr(int __always_unused irq, void *data)
3092{
3093	struct ice_pf *pf = data;
3094	u32 pf_intr_start_offset;
3095	struct ice_ptp_tx *tx;
3096	unsigned long flags;
3097	struct ice_hw *hw;
3098	u32 val;
3099	u8 idx;
3100
3101	hw = &pf->hw;
3102	tx = &pf->ptp.port.tx;
3103	spin_lock_irqsave(&tx->lock, flags);
3104	ice_ptp_complete_tx_single_tstamp(tx);
3105
3106	idx = find_next_bit_wrap(tx->in_use, tx->len,
3107				 tx->last_ll_ts_idx_read + 1);
3108	if (idx != tx->len)
3109		ice_ptp_req_tx_single_tstamp(tx, idx);
3110	spin_unlock_irqrestore(&tx->lock, flags);
3111
3112	val = GLINT_DYN_CTL_INTENA_M | GLINT_DYN_CTL_CLEARPBA_M |
3113	      (ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
3114	pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
3115	wr32(hw, GLINT_DYN_CTL(pf->ll_ts_irq.index + pf_intr_start_offset),
3116	     val);
3117
3118	return IRQ_HANDLED;
3119}
3120
3121/**
3122 * ice_misc_intr - misc interrupt handler
3123 * @irq: interrupt number
3124 * @data: pointer to a q_vector
3125 */
3126static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
3127{
3128	struct ice_pf *pf = (struct ice_pf *)data;
3129	irqreturn_t ret = IRQ_HANDLED;
3130	struct ice_hw *hw = &pf->hw;
 
3131	struct device *dev;
3132	u32 oicr, ena_mask;
3133
3134	dev = ice_pf_to_dev(pf);
3135	set_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
3136	set_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
3137	set_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
3138
3139	oicr = rd32(hw, PFINT_OICR);
3140	ena_mask = rd32(hw, PFINT_OICR_ENA);
3141
3142	if (oicr & PFINT_OICR_SWINT_M) {
3143		ena_mask &= ~PFINT_OICR_SWINT_M;
3144		pf->sw_int_count++;
3145	}
3146
3147	if (oicr & PFINT_OICR_MAL_DETECT_M) {
3148		ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
3149		set_bit(ICE_MDD_EVENT_PENDING, pf->state);
3150	}
3151	if (oicr & PFINT_OICR_VFLR_M) {
3152		/* disable any further VFLR event notifications */
3153		if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
3154			u32 reg = rd32(hw, PFINT_OICR_ENA);
3155
3156			reg &= ~PFINT_OICR_VFLR_M;
3157			wr32(hw, PFINT_OICR_ENA, reg);
3158		} else {
3159			ena_mask &= ~PFINT_OICR_VFLR_M;
3160			set_bit(ICE_VFLR_EVENT_PENDING, pf->state);
3161		}
3162	}
3163
3164	if (oicr & PFINT_OICR_GRST_M) {
3165		u32 reset;
3166
3167		/* we have a reset warning */
3168		ena_mask &= ~PFINT_OICR_GRST_M;
3169		reset = FIELD_GET(GLGEN_RSTAT_RESET_TYPE_M,
3170				  rd32(hw, GLGEN_RSTAT));
3171
3172		if (reset == ICE_RESET_CORER)
3173			pf->corer_count++;
3174		else if (reset == ICE_RESET_GLOBR)
3175			pf->globr_count++;
3176		else if (reset == ICE_RESET_EMPR)
3177			pf->empr_count++;
3178		else
3179			dev_dbg(dev, "Invalid reset type %d\n", reset);
3180
3181		/* If a reset cycle isn't already in progress, we set a bit in
3182		 * pf->state so that the service task can start a reset/rebuild.
3183		 */
3184		if (!test_and_set_bit(ICE_RESET_OICR_RECV, pf->state)) {
3185			if (reset == ICE_RESET_CORER)
3186				set_bit(ICE_CORER_RECV, pf->state);
3187			else if (reset == ICE_RESET_GLOBR)
3188				set_bit(ICE_GLOBR_RECV, pf->state);
3189			else
3190				set_bit(ICE_EMPR_RECV, pf->state);
3191
3192			/* There are couple of different bits at play here.
3193			 * hw->reset_ongoing indicates whether the hardware is
3194			 * in reset. This is set to true when a reset interrupt
3195			 * is received and set back to false after the driver
3196			 * has determined that the hardware is out of reset.
3197			 *
3198			 * ICE_RESET_OICR_RECV in pf->state indicates
3199			 * that a post reset rebuild is required before the
3200			 * driver is operational again. This is set above.
3201			 *
3202			 * As this is the start of the reset/rebuild cycle, set
3203			 * both to indicate that.
3204			 */
3205			hw->reset_ongoing = true;
3206		}
3207	}
3208
3209	if (oicr & PFINT_OICR_TSYN_TX_M) {
3210		ena_mask &= ~PFINT_OICR_TSYN_TX_M;
3211		if (ice_pf_state_is_nominal(pf) &&
3212		    pf->hw.dev_caps.ts_dev_info.ts_ll_int_read) {
3213			struct ice_ptp_tx *tx = &pf->ptp.port.tx;
3214			unsigned long flags;
3215			u8 idx;
3216
3217			spin_lock_irqsave(&tx->lock, flags);
3218			idx = find_next_bit_wrap(tx->in_use, tx->len,
3219						 tx->last_ll_ts_idx_read + 1);
3220			if (idx != tx->len)
3221				ice_ptp_req_tx_single_tstamp(tx, idx);
3222			spin_unlock_irqrestore(&tx->lock, flags);
3223		} else if (ice_ptp_pf_handles_tx_interrupt(pf)) {
3224			set_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread);
3225			ret = IRQ_WAKE_THREAD;
3226		}
3227	}
3228
3229	if (oicr & PFINT_OICR_TSYN_EVNT_M) {
3230		u8 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3231		u32 gltsyn_stat = rd32(hw, GLTSYN_STAT(tmr_idx));
3232
 
 
 
 
3233		ena_mask &= ~PFINT_OICR_TSYN_EVNT_M;
3234
3235		if (ice_pf_src_tmr_owned(pf)) {
3236			/* Save EVENTs from GLTSYN register */
3237			pf->ptp.ext_ts_irq |= gltsyn_stat &
3238					      (GLTSYN_STAT_EVENT0_M |
3239					       GLTSYN_STAT_EVENT1_M |
3240					       GLTSYN_STAT_EVENT2_M);
3241
3242			ice_ptp_extts_event(pf);
3243		}
3244	}
3245
3246#define ICE_AUX_CRIT_ERR (PFINT_OICR_PE_CRITERR_M | PFINT_OICR_HMC_ERR_M | PFINT_OICR_PE_PUSH_M)
3247	if (oicr & ICE_AUX_CRIT_ERR) {
3248		pf->oicr_err_reg |= oicr;
3249		set_bit(ICE_AUX_ERR_PENDING, pf->state);
3250		ena_mask &= ~ICE_AUX_CRIT_ERR;
3251	}
3252
3253	/* Report any remaining unexpected interrupts */
3254	oicr &= ena_mask;
3255	if (oicr) {
3256		dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
3257		/* If a critical error is pending there is no choice but to
3258		 * reset the device.
3259		 */
3260		if (oicr & (PFINT_OICR_PCI_EXCEPTION_M |
3261			    PFINT_OICR_ECC_ERR_M)) {
3262			set_bit(ICE_PFR_REQ, pf->state);
 
3263		}
3264	}
 
 
 
3265	ice_service_task_schedule(pf);
3266	if (ret == IRQ_HANDLED)
3267		ice_irq_dynamic_ena(hw, NULL, NULL);
3268
3269	return ret;
3270}
3271
3272/**
3273 * ice_misc_intr_thread_fn - misc interrupt thread function
3274 * @irq: interrupt number
3275 * @data: pointer to a q_vector
3276 */
3277static irqreturn_t ice_misc_intr_thread_fn(int __always_unused irq, void *data)
3278{
3279	struct ice_pf *pf = data;
3280	struct ice_hw *hw;
3281
3282	hw = &pf->hw;
3283
3284	if (ice_is_reset_in_progress(pf->state))
3285		goto skip_irq;
3286
3287	if (test_and_clear_bit(ICE_MISC_THREAD_TX_TSTAMP, pf->misc_thread)) {
3288		/* Process outstanding Tx timestamps. If there is more work,
3289		 * re-arm the interrupt to trigger again.
3290		 */
3291		if (ice_ptp_process_ts(pf) == ICE_TX_TSTAMP_WORK_PENDING) {
3292			wr32(hw, PFINT_OICR, PFINT_OICR_TSYN_TX_M);
3293			ice_flush(hw);
3294		}
3295	}
3296
3297skip_irq:
3298	ice_irq_dynamic_ena(hw, NULL, NULL);
3299
3300	return IRQ_HANDLED;
3301}
3302
3303/**
3304 * ice_dis_ctrlq_interrupts - disable control queue interrupts
3305 * @hw: pointer to HW structure
3306 */
3307static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
3308{
3309	/* disable Admin queue Interrupt causes */
3310	wr32(hw, PFINT_FW_CTL,
3311	     rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
3312
3313	/* disable Mailbox queue Interrupt causes */
3314	wr32(hw, PFINT_MBX_CTL,
3315	     rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
3316
3317	wr32(hw, PFINT_SB_CTL,
3318	     rd32(hw, PFINT_SB_CTL) & ~PFINT_SB_CTL_CAUSE_ENA_M);
3319
3320	/* disable Control queue Interrupt causes */
3321	wr32(hw, PFINT_OICR_CTL,
3322	     rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
3323
3324	ice_flush(hw);
3325}
3326
3327/**
3328 * ice_free_irq_msix_ll_ts- Unroll ll_ts vector setup
3329 * @pf: board private structure
3330 */
3331static void ice_free_irq_msix_ll_ts(struct ice_pf *pf)
3332{
3333	int irq_num = pf->ll_ts_irq.virq;
3334
3335	synchronize_irq(irq_num);
3336	devm_free_irq(ice_pf_to_dev(pf), irq_num, pf);
3337
3338	ice_free_irq(pf, pf->ll_ts_irq);
3339}
3340
3341/**
3342 * ice_free_irq_msix_misc - Unroll misc vector setup
3343 * @pf: board private structure
3344 */
3345static void ice_free_irq_msix_misc(struct ice_pf *pf)
3346{
3347	int misc_irq_num = pf->oicr_irq.virq;
3348	struct ice_hw *hw = &pf->hw;
3349
3350	ice_dis_ctrlq_interrupts(hw);
3351
3352	/* disable OICR interrupt */
3353	wr32(hw, PFINT_OICR_ENA, 0);
3354	ice_flush(hw);
3355
3356	synchronize_irq(misc_irq_num);
3357	devm_free_irq(ice_pf_to_dev(pf), misc_irq_num, pf);
 
 
 
3358
3359	ice_free_irq(pf, pf->oicr_irq);
3360	if (pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3361		ice_free_irq_msix_ll_ts(pf);
3362}
3363
3364/**
3365 * ice_ena_ctrlq_interrupts - enable control queue interrupts
3366 * @hw: pointer to HW structure
3367 * @reg_idx: HW vector index to associate the control queue interrupts with
3368 */
3369static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
3370{
3371	u32 val;
3372
3373	val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
3374	       PFINT_OICR_CTL_CAUSE_ENA_M);
3375	wr32(hw, PFINT_OICR_CTL, val);
3376
3377	/* enable Admin queue Interrupt causes */
3378	val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
3379	       PFINT_FW_CTL_CAUSE_ENA_M);
3380	wr32(hw, PFINT_FW_CTL, val);
3381
3382	/* enable Mailbox queue Interrupt causes */
3383	val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
3384	       PFINT_MBX_CTL_CAUSE_ENA_M);
3385	wr32(hw, PFINT_MBX_CTL, val);
3386
3387	if (!hw->dev_caps.ts_dev_info.ts_ll_int_read) {
3388		/* enable Sideband queue Interrupt causes */
3389		val = ((reg_idx & PFINT_SB_CTL_MSIX_INDX_M) |
3390		       PFINT_SB_CTL_CAUSE_ENA_M);
3391		wr32(hw, PFINT_SB_CTL, val);
3392	}
3393
3394	ice_flush(hw);
3395}
3396
3397/**
3398 * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
3399 * @pf: board private structure
3400 *
3401 * This sets up the handler for MSIX 0, which is used to manage the
3402 * non-queue interrupts, e.g. AdminQ and errors. This is not used
3403 * when in MSI or Legacy interrupt mode.
3404 */
3405static int ice_req_irq_msix_misc(struct ice_pf *pf)
3406{
3407	struct device *dev = ice_pf_to_dev(pf);
3408	struct ice_hw *hw = &pf->hw;
3409	u32 pf_intr_start_offset;
3410	struct msi_map irq;
3411	int err = 0;
3412
3413	if (!pf->int_name[0])
3414		snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
3415			 dev_driver_string(dev), dev_name(dev));
3416
3417	if (!pf->int_name_ll_ts[0])
3418		snprintf(pf->int_name_ll_ts, sizeof(pf->int_name_ll_ts) - 1,
3419			 "%s-%s:ll_ts", dev_driver_string(dev), dev_name(dev));
3420	/* Do not request IRQ but do enable OICR interrupt since settings are
3421	 * lost during reset. Note that this function is called only during
3422	 * rebuild path and not while reset is in progress.
3423	 */
3424	if (ice_is_reset_in_progress(pf->state))
3425		goto skip_req_irq;
3426
3427	/* reserve one vector in irq_tracker for misc interrupts */
3428	irq = ice_alloc_irq(pf, false);
3429	if (irq.index < 0)
3430		return irq.index;
3431
3432	pf->oicr_irq = irq;
3433	err = devm_request_threaded_irq(dev, pf->oicr_irq.virq, ice_misc_intr,
3434					ice_misc_intr_thread_fn, 0,
3435					pf->int_name, pf);
 
 
 
3436	if (err) {
3437		dev_err(dev, "devm_request_threaded_irq for %s failed: %d\n",
3438			pf->int_name, err);
3439		ice_free_irq(pf, pf->oicr_irq);
3440		return err;
3441	}
3442
3443	/* reserve one vector in irq_tracker for ll_ts interrupt */
3444	if (!pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3445		goto skip_req_irq;
3446
3447	irq = ice_alloc_irq(pf, false);
3448	if (irq.index < 0)
3449		return irq.index;
3450
3451	pf->ll_ts_irq = irq;
3452	err = devm_request_irq(dev, pf->ll_ts_irq.virq, ice_ll_ts_intr, 0,
3453			       pf->int_name_ll_ts, pf);
3454	if (err) {
3455		dev_err(dev, "devm_request_irq for %s failed: %d\n",
3456			pf->int_name_ll_ts, err);
3457		ice_free_irq(pf, pf->ll_ts_irq);
3458		return err;
3459	}
3460
3461skip_req_irq:
3462	ice_ena_misc_vector(pf);
3463
3464	ice_ena_ctrlq_interrupts(hw, pf->oicr_irq.index);
3465	/* This enables LL TS interrupt */
3466	pf_intr_start_offset = rd32(hw, PFINT_ALLOC) & PFINT_ALLOC_FIRST;
3467	if (pf->hw.dev_caps.ts_dev_info.ts_ll_int_read)
3468		wr32(hw, PFINT_SB_CTL,
3469		     ((pf->ll_ts_irq.index + pf_intr_start_offset) &
3470		      PFINT_SB_CTL_MSIX_INDX_M) | PFINT_SB_CTL_CAUSE_ENA_M);
3471	wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_irq.index),
3472	     ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
3473
3474	ice_flush(hw);
3475	ice_irq_dynamic_ena(hw, NULL, NULL);
3476
3477	return 0;
3478}
3479
3480/**
3481 * ice_napi_add - register NAPI handler for the VSI
3482 * @vsi: VSI for which NAPI handler is to be registered
3483 *
3484 * This function is only called in the driver's load path. Registering the NAPI
3485 * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
3486 * reset/rebuild, etc.)
3487 */
3488static void ice_napi_add(struct ice_vsi *vsi)
3489{
3490	int v_idx;
3491
3492	if (!vsi->netdev)
3493		return;
3494
3495	ice_for_each_q_vector(vsi, v_idx) {
3496		netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
3497			       ice_napi_poll);
3498		__ice_q_vector_set_napi_queues(vsi->q_vectors[v_idx], false);
3499	}
3500}
3501
3502/**
3503 * ice_set_ops - set netdev and ethtools ops for the given netdev
3504 * @vsi: the VSI associated with the new netdev
3505 */
3506static void ice_set_ops(struct ice_vsi *vsi)
3507{
3508	struct net_device *netdev = vsi->netdev;
3509	struct ice_pf *pf = ice_netdev_to_pf(netdev);
3510
3511	if (ice_is_safe_mode(pf)) {
3512		netdev->netdev_ops = &ice_netdev_safe_mode_ops;
3513		ice_set_ethtool_safe_mode_ops(netdev);
3514		return;
3515	}
3516
3517	netdev->netdev_ops = &ice_netdev_ops;
3518	netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
3519	netdev->xdp_metadata_ops = &ice_xdp_md_ops;
3520	ice_set_ethtool_ops(netdev);
3521
3522	if (vsi->type != ICE_VSI_PF)
3523		return;
3524
3525	netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
3526			       NETDEV_XDP_ACT_XSK_ZEROCOPY |
3527			       NETDEV_XDP_ACT_RX_SG;
3528	netdev->xdp_zc_max_segs = ICE_MAX_BUF_TXD;
3529}
3530
3531/**
3532 * ice_set_netdev_features - set features for the given netdev
3533 * @netdev: netdev instance
3534 */
3535static void ice_set_netdev_features(struct net_device *netdev)
3536{
3537	struct ice_pf *pf = ice_netdev_to_pf(netdev);
3538	bool is_dvm_ena = ice_is_dvm_ena(&pf->hw);
3539	netdev_features_t csumo_features;
3540	netdev_features_t vlano_features;
3541	netdev_features_t dflt_features;
3542	netdev_features_t tso_features;
3543
3544	if (ice_is_safe_mode(pf)) {
3545		/* safe mode */
3546		netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
3547		netdev->hw_features = netdev->features;
3548		return;
3549	}
3550
3551	dflt_features = NETIF_F_SG	|
3552			NETIF_F_HIGHDMA	|
3553			NETIF_F_NTUPLE	|
3554			NETIF_F_RXHASH;
3555
3556	csumo_features = NETIF_F_RXCSUM	  |
3557			 NETIF_F_IP_CSUM  |
3558			 NETIF_F_SCTP_CRC |
3559			 NETIF_F_IPV6_CSUM;
3560
3561	vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
3562			 NETIF_F_HW_VLAN_CTAG_TX     |
3563			 NETIF_F_HW_VLAN_CTAG_RX;
3564
3565	/* Enable CTAG/STAG filtering by default in Double VLAN Mode (DVM) */
3566	if (is_dvm_ena)
3567		vlano_features |= NETIF_F_HW_VLAN_STAG_FILTER;
3568
3569	tso_features = NETIF_F_TSO			|
3570		       NETIF_F_TSO_ECN			|
3571		       NETIF_F_TSO6			|
3572		       NETIF_F_GSO_GRE			|
3573		       NETIF_F_GSO_UDP_TUNNEL		|
3574		       NETIF_F_GSO_GRE_CSUM		|
3575		       NETIF_F_GSO_UDP_TUNNEL_CSUM	|
3576		       NETIF_F_GSO_PARTIAL		|
3577		       NETIF_F_GSO_IPXIP4		|
3578		       NETIF_F_GSO_IPXIP6		|
3579		       NETIF_F_GSO_UDP_L4;
3580
3581	netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3582					NETIF_F_GSO_GRE_CSUM;
3583	/* set features that user can change */
3584	netdev->hw_features = dflt_features | csumo_features |
3585			      vlano_features | tso_features;
3586
3587	/* add support for HW_CSUM on packets with MPLS header */
3588	netdev->mpls_features =  NETIF_F_HW_CSUM |
3589				 NETIF_F_TSO     |
3590				 NETIF_F_TSO6;
3591
3592	/* enable features */
3593	netdev->features |= netdev->hw_features;
3594
3595	netdev->hw_features |= NETIF_F_HW_TC;
3596	netdev->hw_features |= NETIF_F_LOOPBACK;
3597
3598	/* encap and VLAN devices inherit default, csumo and tso features */
3599	netdev->hw_enc_features |= dflt_features | csumo_features |
3600				   tso_features;
3601	netdev->vlan_features |= dflt_features | csumo_features |
3602				 tso_features;
3603
3604	/* advertise support but don't enable by default since only one type of
3605	 * VLAN offload can be enabled at a time (i.e. CTAG or STAG). When one
3606	 * type turns on the other has to be turned off. This is enforced by the
3607	 * ice_fix_features() ndo callback.
3608	 */
3609	if (is_dvm_ena)
3610		netdev->hw_features |= NETIF_F_HW_VLAN_STAG_RX |
3611			NETIF_F_HW_VLAN_STAG_TX;
3612
3613	/* Leave CRC / FCS stripping enabled by default, but allow the value to
3614	 * be changed at runtime
3615	 */
3616	netdev->hw_features |= NETIF_F_RXFCS;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3617
3618	netif_set_tso_max_size(netdev, ICE_MAX_TSO_SIZE);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3619}
3620
3621/**
3622 * ice_fill_rss_lut - Fill the RSS lookup table with default values
3623 * @lut: Lookup table
3624 * @rss_table_size: Lookup table size
3625 * @rss_size: Range of queue number for hashing
3626 */
3627void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
3628{
3629	u16 i;
3630
3631	for (i = 0; i < rss_table_size; i++)
3632		lut[i] = i % rss_size;
3633}
3634
3635/**
3636 * ice_pf_vsi_setup - Set up a PF VSI
3637 * @pf: board private structure
3638 * @pi: pointer to the port_info instance
3639 *
3640 * Returns pointer to the successfully allocated VSI software struct
3641 * on success, otherwise returns NULL on failure.
3642 */
3643static struct ice_vsi *
3644ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3645{
3646	struct ice_vsi_cfg_params params = {};
3647
3648	params.type = ICE_VSI_PF;
3649	params.pi = pi;
3650	params.flags = ICE_VSI_FLAG_INIT;
3651
3652	return ice_vsi_setup(pf, &params);
3653}
3654
3655static struct ice_vsi *
3656ice_chnl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
3657		   struct ice_channel *ch)
3658{
3659	struct ice_vsi_cfg_params params = {};
3660
3661	params.type = ICE_VSI_CHNL;
3662	params.pi = pi;
3663	params.ch = ch;
3664	params.flags = ICE_VSI_FLAG_INIT;
3665
3666	return ice_vsi_setup(pf, &params);
3667}
3668
3669/**
3670 * ice_ctrl_vsi_setup - Set up a control VSI
3671 * @pf: board private structure
3672 * @pi: pointer to the port_info instance
3673 *
3674 * Returns pointer to the successfully allocated VSI software struct
3675 * on success, otherwise returns NULL on failure.
3676 */
3677static struct ice_vsi *
3678ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3679{
3680	struct ice_vsi_cfg_params params = {};
3681
3682	params.type = ICE_VSI_CTRL;
3683	params.pi = pi;
3684	params.flags = ICE_VSI_FLAG_INIT;
3685
3686	return ice_vsi_setup(pf, &params);
3687}
3688
3689/**
3690 * ice_lb_vsi_setup - Set up a loopback VSI
3691 * @pf: board private structure
3692 * @pi: pointer to the port_info instance
3693 *
3694 * Returns pointer to the successfully allocated VSI software struct
3695 * on success, otherwise returns NULL on failure.
3696 */
3697struct ice_vsi *
3698ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3699{
3700	struct ice_vsi_cfg_params params = {};
3701
3702	params.type = ICE_VSI_LB;
3703	params.pi = pi;
3704	params.flags = ICE_VSI_FLAG_INIT;
3705
3706	return ice_vsi_setup(pf, &params);
3707}
3708
3709/**
3710 * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
3711 * @netdev: network interface to be adjusted
3712 * @proto: VLAN TPID
3713 * @vid: VLAN ID to be added
3714 *
3715 * net_device_ops implementation for adding VLAN IDs
3716 */
3717static int
3718ice_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
3719{
3720	struct ice_netdev_priv *np = netdev_priv(netdev);
3721	struct ice_vsi_vlan_ops *vlan_ops;
3722	struct ice_vsi *vsi = np->vsi;
3723	struct ice_vlan vlan;
3724	int ret;
3725
3726	/* VLAN 0 is added by default during load/reset */
3727	if (!vid)
3728		return 0;
3729
3730	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3731		usleep_range(1000, 2000);
3732
3733	/* Add multicast promisc rule for the VLAN ID to be added if
3734	 * all-multicast is currently enabled.
3735	 */
3736	if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3737		ret = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3738					       ICE_MCAST_VLAN_PROMISC_BITS,
3739					       vid);
3740		if (ret)
3741			goto finish;
3742	}
3743
3744	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3745
3746	/* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
3747	 * packets aren't pruned by the device's internal switch on Rx
3748	 */
3749	vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3750	ret = vlan_ops->add_vlan(vsi, &vlan);
3751	if (ret)
3752		goto finish;
3753
3754	/* If all-multicast is currently enabled and this VLAN ID is only one
3755	 * besides VLAN-0 we have to update look-up type of multicast promisc
3756	 * rule for VLAN-0 from ICE_SW_LKUP_PROMISC to ICE_SW_LKUP_PROMISC_VLAN.
3757	 */
3758	if ((vsi->current_netdev_flags & IFF_ALLMULTI) &&
3759	    ice_vsi_num_non_zero_vlans(vsi) == 1) {
3760		ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3761					   ICE_MCAST_PROMISC_BITS, 0);
3762		ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3763					 ICE_MCAST_VLAN_PROMISC_BITS, 0);
3764	}
3765
3766finish:
3767	clear_bit(ICE_CFG_BUSY, vsi->state);
3768
3769	return ret;
3770}
3771
3772/**
3773 * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
3774 * @netdev: network interface to be adjusted
3775 * @proto: VLAN TPID
3776 * @vid: VLAN ID to be removed
3777 *
3778 * net_device_ops implementation for removing VLAN IDs
3779 */
3780static int
3781ice_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
3782{
3783	struct ice_netdev_priv *np = netdev_priv(netdev);
3784	struct ice_vsi_vlan_ops *vlan_ops;
3785	struct ice_vsi *vsi = np->vsi;
3786	struct ice_vlan vlan;
3787	int ret;
3788
3789	/* don't allow removal of VLAN 0 */
3790	if (!vid)
3791		return 0;
3792
3793	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3794		usleep_range(1000, 2000);
3795
3796	ret = ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3797				    ICE_MCAST_VLAN_PROMISC_BITS, vid);
3798	if (ret) {
3799		netdev_err(netdev, "Error clearing multicast promiscuous mode on VSI %i\n",
3800			   vsi->vsi_num);
3801		vsi->current_netdev_flags |= IFF_ALLMULTI;
3802	}
3803
3804	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3805
3806	/* Make sure VLAN delete is successful before updating VLAN
3807	 * information
3808	 */
3809	vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3810	ret = vlan_ops->del_vlan(vsi, &vlan);
3811	if (ret)
3812		goto finish;
3813
3814	/* Remove multicast promisc rule for the removed VLAN ID if
3815	 * all-multicast is enabled.
3816	 */
3817	if (vsi->current_netdev_flags & IFF_ALLMULTI)
3818		ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3819					   ICE_MCAST_VLAN_PROMISC_BITS, vid);
3820
3821	if (!ice_vsi_has_non_zero_vlans(vsi)) {
3822		/* Update look-up type of multicast promisc rule for VLAN 0
3823		 * from ICE_SW_LKUP_PROMISC_VLAN to ICE_SW_LKUP_PROMISC when
3824		 * all-multicast is enabled and VLAN 0 is the only VLAN rule.
3825		 */
3826		if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3827			ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3828						   ICE_MCAST_VLAN_PROMISC_BITS,
3829						   0);
3830			ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3831						 ICE_MCAST_PROMISC_BITS, 0);
3832		}
3833	}
3834
3835finish:
3836	clear_bit(ICE_CFG_BUSY, vsi->state);
3837
3838	return ret;
3839}
3840
3841/**
3842 * ice_rep_indr_tc_block_unbind
3843 * @cb_priv: indirection block private data
3844 */
3845static void ice_rep_indr_tc_block_unbind(void *cb_priv)
3846{
3847	struct ice_indr_block_priv *indr_priv = cb_priv;
3848
3849	list_del(&indr_priv->list);
3850	kfree(indr_priv);
3851}
3852
3853/**
3854 * ice_tc_indir_block_unregister - Unregister TC indirect block notifications
3855 * @vsi: VSI struct which has the netdev
3856 */
3857static void ice_tc_indir_block_unregister(struct ice_vsi *vsi)
3858{
3859	struct ice_netdev_priv *np = netdev_priv(vsi->netdev);
3860
3861	flow_indr_dev_unregister(ice_indr_setup_tc_cb, np,
3862				 ice_rep_indr_tc_block_unbind);
3863}
3864
3865/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3866 * ice_tc_indir_block_register - Register TC indirect block notifications
3867 * @vsi: VSI struct which has the netdev
3868 *
3869 * Returns 0 on success, negative value on failure
3870 */
3871static int ice_tc_indir_block_register(struct ice_vsi *vsi)
3872{
3873	struct ice_netdev_priv *np;
3874
3875	if (!vsi || !vsi->netdev)
3876		return -EINVAL;
3877
3878	np = netdev_priv(vsi->netdev);
3879
3880	INIT_LIST_HEAD(&np->tc_indr_block_priv_list);
3881	return flow_indr_dev_register(ice_indr_setup_tc_cb, np);
3882}
3883
3884/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3885 * ice_get_avail_q_count - Get count of queues in use
3886 * @pf_qmap: bitmap to get queue use count from
3887 * @lock: pointer to a mutex that protects access to pf_qmap
3888 * @size: size of the bitmap
3889 */
3890static u16
3891ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
3892{
3893	unsigned long bit;
3894	u16 count = 0;
3895
3896	mutex_lock(lock);
3897	for_each_clear_bit(bit, pf_qmap, size)
3898		count++;
3899	mutex_unlock(lock);
3900
3901	return count;
3902}
3903
3904/**
3905 * ice_get_avail_txq_count - Get count of Tx queues in use
3906 * @pf: pointer to an ice_pf instance
3907 */
3908u16 ice_get_avail_txq_count(struct ice_pf *pf)
3909{
3910	return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
3911				     pf->max_pf_txqs);
3912}
3913
3914/**
3915 * ice_get_avail_rxq_count - Get count of Rx queues in use
3916 * @pf: pointer to an ice_pf instance
3917 */
3918u16 ice_get_avail_rxq_count(struct ice_pf *pf)
3919{
3920	return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
3921				     pf->max_pf_rxqs);
3922}
3923
3924/**
3925 * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3926 * @pf: board private structure to initialize
3927 */
3928static void ice_deinit_pf(struct ice_pf *pf)
3929{
3930	ice_service_task_stop(pf);
3931	mutex_destroy(&pf->lag_mutex);
3932	mutex_destroy(&pf->adev_mutex);
3933	mutex_destroy(&pf->sw_mutex);
3934	mutex_destroy(&pf->tc_mutex);
3935	mutex_destroy(&pf->avail_q_mutex);
3936	mutex_destroy(&pf->vfs.table_lock);
3937
3938	if (pf->avail_txqs) {
3939		bitmap_free(pf->avail_txqs);
3940		pf->avail_txqs = NULL;
3941	}
3942
3943	if (pf->avail_rxqs) {
3944		bitmap_free(pf->avail_rxqs);
3945		pf->avail_rxqs = NULL;
3946	}
3947
3948	if (pf->ptp.clock)
3949		ptp_clock_unregister(pf->ptp.clock);
3950}
3951
3952/**
3953 * ice_set_pf_caps - set PFs capability flags
3954 * @pf: pointer to the PF instance
3955 */
3956static void ice_set_pf_caps(struct ice_pf *pf)
3957{
3958	struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
3959
3960	clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3961	if (func_caps->common_cap.rdma)
3962		set_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3963	clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3964	if (func_caps->common_cap.dcb)
3965		set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3966	clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3967	if (func_caps->common_cap.sr_iov_1_1) {
3968		set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3969		pf->vfs.num_supported = min_t(int, func_caps->num_allocd_vfs,
3970					      ICE_MAX_SRIOV_VFS);
3971	}
3972	clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
3973	if (func_caps->common_cap.rss_table_size)
3974		set_bit(ICE_FLAG_RSS_ENA, pf->flags);
3975
3976	clear_bit(ICE_FLAG_FD_ENA, pf->flags);
3977	if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
3978		u16 unused;
3979
3980		/* ctrl_vsi_idx will be set to a valid value when flow director
3981		 * is setup by ice_init_fdir
3982		 */
3983		pf->ctrl_vsi_idx = ICE_NO_VSI;
3984		set_bit(ICE_FLAG_FD_ENA, pf->flags);
3985		/* force guaranteed filter pool for PF */
3986		ice_alloc_fd_guar_item(&pf->hw, &unused,
3987				       func_caps->fd_fltr_guar);
3988		/* force shared filter pool for PF */
3989		ice_alloc_fd_shrd_item(&pf->hw, &unused,
3990				       func_caps->fd_fltr_best_effort);
3991	}
3992
3993	clear_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
3994	if (func_caps->common_cap.ieee_1588 &&
3995	    !(pf->hw.mac_type == ICE_MAC_E830))
3996		set_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
3997
3998	pf->max_pf_txqs = func_caps->common_cap.num_txq;
3999	pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
4000}
4001
4002/**
4003 * ice_init_pf - Initialize general software structures (struct ice_pf)
4004 * @pf: board private structure to initialize
4005 */
4006static int ice_init_pf(struct ice_pf *pf)
4007{
4008	ice_set_pf_caps(pf);
4009
4010	mutex_init(&pf->sw_mutex);
4011	mutex_init(&pf->tc_mutex);
4012	mutex_init(&pf->adev_mutex);
4013	mutex_init(&pf->lag_mutex);
4014
4015	INIT_HLIST_HEAD(&pf->aq_wait_list);
4016	spin_lock_init(&pf->aq_wait_lock);
4017	init_waitqueue_head(&pf->aq_wait_queue);
4018
4019	init_waitqueue_head(&pf->reset_wait_queue);
4020
4021	/* setup service timer and periodic service task */
4022	timer_setup(&pf->serv_tmr, ice_service_timer, 0);
4023	pf->serv_tmr_period = HZ;
4024	INIT_WORK(&pf->serv_task, ice_service_task);
4025	clear_bit(ICE_SERVICE_SCHED, pf->state);
4026
4027	mutex_init(&pf->avail_q_mutex);
4028	pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
4029	if (!pf->avail_txqs)
4030		return -ENOMEM;
4031
4032	pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
4033	if (!pf->avail_rxqs) {
4034		bitmap_free(pf->avail_txqs);
4035		pf->avail_txqs = NULL;
4036		return -ENOMEM;
4037	}
4038
4039	mutex_init(&pf->vfs.table_lock);
4040	hash_init(pf->vfs.table);
4041	ice_mbx_init_snapshot(&pf->hw);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4042
4043	return 0;
4044}
4045
4046/**
4047 * ice_is_wol_supported - check if WoL is supported
4048 * @hw: pointer to hardware info
4049 *
4050 * Check if WoL is supported based on the HW configuration.
4051 * Returns true if NVM supports and enables WoL for this port, false otherwise
4052 */
4053bool ice_is_wol_supported(struct ice_hw *hw)
4054{
4055	u16 wol_ctrl;
4056
4057	/* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
4058	 * word) indicates WoL is not supported on the corresponding PF ID.
4059	 */
4060	if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
4061		return false;
4062
4063	return !(BIT(hw->port_info->lport) & wol_ctrl);
4064}
4065
4066/**
4067 * ice_vsi_recfg_qs - Change the number of queues on a VSI
4068 * @vsi: VSI being changed
4069 * @new_rx: new number of Rx queues
4070 * @new_tx: new number of Tx queues
4071 * @locked: is adev device_lock held
4072 *
4073 * Only change the number of queues if new_tx, or new_rx is non-0.
4074 *
4075 * Returns 0 on success.
4076 */
4077int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx, bool locked)
4078{
4079	struct ice_pf *pf = vsi->back;
4080	int err = 0, timeout = 50;
4081
4082	if (!new_rx && !new_tx)
4083		return -EINVAL;
4084
4085	while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
4086		timeout--;
4087		if (!timeout)
4088			return -EBUSY;
4089		usleep_range(1000, 2000);
4090	}
4091
4092	if (new_tx)
4093		vsi->req_txq = (u16)new_tx;
4094	if (new_rx)
4095		vsi->req_rxq = (u16)new_rx;
4096
4097	/* set for the next time the netdev is started */
4098	if (!netif_running(vsi->netdev)) {
4099		ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
4100		dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
4101		goto done;
4102	}
4103
4104	ice_vsi_close(vsi);
4105	ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
4106	ice_pf_dcb_recfg(pf, locked);
4107	ice_vsi_open(vsi);
4108done:
4109	clear_bit(ICE_CFG_BUSY, pf->state);
4110	return err;
4111}
4112
4113/**
4114 * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
4115 * @pf: PF to configure
4116 *
4117 * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
4118 * VSI can still Tx/Rx VLAN tagged packets.
4119 */
4120static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
4121{
4122	struct ice_vsi *vsi = ice_get_main_vsi(pf);
4123	struct ice_vsi_ctx *ctxt;
4124	struct ice_hw *hw;
4125	int status;
4126
4127	if (!vsi)
4128		return;
4129
4130	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
4131	if (!ctxt)
4132		return;
4133
4134	hw = &pf->hw;
4135	ctxt->info = vsi->info;
4136
4137	ctxt->info.valid_sections =
4138		cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
4139			    ICE_AQ_VSI_PROP_SECURITY_VALID |
4140			    ICE_AQ_VSI_PROP_SW_VALID);
4141
4142	/* disable VLAN anti-spoof */
4143	ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4144				  ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4145
4146	/* disable VLAN pruning and keep all other settings */
4147	ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
4148
4149	/* allow all VLANs on Tx and don't strip on Rx */
4150	ctxt->info.inner_vlan_flags = ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL |
4151		ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING;
4152
4153	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4154	if (status) {
4155		dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %d aq_err %s\n",
4156			status, ice_aq_str(hw->adminq.sq_last_status));
4157	} else {
4158		vsi->info.sec_flags = ctxt->info.sec_flags;
4159		vsi->info.sw_flags2 = ctxt->info.sw_flags2;
4160		vsi->info.inner_vlan_flags = ctxt->info.inner_vlan_flags;
4161	}
4162
4163	kfree(ctxt);
4164}
4165
4166/**
4167 * ice_log_pkg_init - log result of DDP package load
4168 * @hw: pointer to hardware info
4169 * @state: state of package load
4170 */
4171static void ice_log_pkg_init(struct ice_hw *hw, enum ice_ddp_state state)
4172{
4173	struct ice_pf *pf = hw->back;
4174	struct device *dev;
4175
4176	dev = ice_pf_to_dev(pf);
4177
4178	switch (state) {
4179	case ICE_DDP_PKG_SUCCESS:
4180		dev_info(dev, "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
4181			 hw->active_pkg_name,
4182			 hw->active_pkg_ver.major,
4183			 hw->active_pkg_ver.minor,
4184			 hw->active_pkg_ver.update,
4185			 hw->active_pkg_ver.draft);
4186		break;
4187	case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
4188		dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
4189			 hw->active_pkg_name,
4190			 hw->active_pkg_ver.major,
4191			 hw->active_pkg_ver.minor,
4192			 hw->active_pkg_ver.update,
4193			 hw->active_pkg_ver.draft);
4194		break;
4195	case ICE_DDP_PKG_ALREADY_LOADED_NOT_SUPPORTED:
4196		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",
4197			hw->active_pkg_name,
4198			hw->active_pkg_ver.major,
4199			hw->active_pkg_ver.minor,
4200			ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4201		break;
4202	case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
4203		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",
4204			 hw->active_pkg_name,
4205			 hw->active_pkg_ver.major,
4206			 hw->active_pkg_ver.minor,
4207			 hw->active_pkg_ver.update,
4208			 hw->active_pkg_ver.draft,
4209			 hw->pkg_name,
4210			 hw->pkg_ver.major,
4211			 hw->pkg_ver.minor,
4212			 hw->pkg_ver.update,
4213			 hw->pkg_ver.draft);
4214		break;
4215	case ICE_DDP_PKG_FW_MISMATCH:
4216		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");
4217		break;
4218	case ICE_DDP_PKG_INVALID_FILE:
4219		dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
4220		break;
4221	case ICE_DDP_PKG_FILE_VERSION_TOO_HIGH:
4222		dev_err(dev, "The DDP package file version is higher than the driver supports.  Please use an updated driver.  Entering Safe Mode.\n");
4223		break;
4224	case ICE_DDP_PKG_FILE_VERSION_TOO_LOW:
4225		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",
4226			ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4227		break;
4228	case ICE_DDP_PKG_FILE_SIGNATURE_INVALID:
4229		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");
4230		break;
4231	case ICE_DDP_PKG_FILE_REVISION_TOO_LOW:
4232		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");
4233		break;
4234	case ICE_DDP_PKG_LOAD_ERROR:
4235		dev_err(dev, "An error occurred on the device while loading the DDP package.  The device will be reset.\n");
4236		/* poll for reset to complete */
4237		if (ice_check_reset(hw))
4238			dev_err(dev, "Error resetting device. Please reload the driver\n");
4239		break;
4240	case ICE_DDP_PKG_ERR:
4241	default:
4242		dev_err(dev, "An unknown error occurred when loading the DDP package.  Entering Safe Mode.\n");
4243		break;
4244	}
4245}
4246
4247/**
4248 * ice_load_pkg - load/reload the DDP Package file
4249 * @firmware: firmware structure when firmware requested or NULL for reload
4250 * @pf: pointer to the PF instance
4251 *
4252 * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
4253 * initialize HW tables.
4254 */
4255static void
4256ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
4257{
4258	enum ice_ddp_state state = ICE_DDP_PKG_ERR;
4259	struct device *dev = ice_pf_to_dev(pf);
4260	struct ice_hw *hw = &pf->hw;
4261
4262	/* Load DDP Package */
4263	if (firmware && !hw->pkg_copy) {
4264		state = ice_copy_and_init_pkg(hw, firmware->data,
4265					      firmware->size);
4266		ice_log_pkg_init(hw, state);
4267	} else if (!firmware && hw->pkg_copy) {
4268		/* Reload package during rebuild after CORER/GLOBR reset */
4269		state = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
4270		ice_log_pkg_init(hw, state);
4271	} else {
4272		dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
4273	}
4274
4275	if (!ice_is_init_pkg_successful(state)) {
4276		/* Safe Mode */
4277		clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4278		return;
4279	}
4280
4281	/* Successful download package is the precondition for advanced
4282	 * features, hence setting the ICE_FLAG_ADV_FEATURES flag
4283	 */
4284	set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4285}
4286
4287/**
4288 * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
4289 * @pf: pointer to the PF structure
4290 *
4291 * There is no error returned here because the driver should be able to handle
4292 * 128 Byte cache lines, so we only print a warning in case issues are seen,
4293 * specifically with Tx.
4294 */
4295static void ice_verify_cacheline_size(struct ice_pf *pf)
4296{
4297	if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
4298		dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
4299			 ICE_CACHE_LINE_BYTES);
4300}
4301
4302/**
4303 * ice_send_version - update firmware with driver version
4304 * @pf: PF struct
4305 *
4306 * Returns 0 on success, else error code
4307 */
4308static int ice_send_version(struct ice_pf *pf)
4309{
4310	struct ice_driver_ver dv;
4311
4312	dv.major_ver = 0xff;
4313	dv.minor_ver = 0xff;
4314	dv.build_ver = 0xff;
4315	dv.subbuild_ver = 0;
4316	strscpy((char *)dv.driver_string, UTS_RELEASE,
4317		sizeof(dv.driver_string));
4318	return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
4319}
4320
4321/**
4322 * ice_init_fdir - Initialize flow director VSI and configuration
4323 * @pf: pointer to the PF instance
4324 *
4325 * returns 0 on success, negative on error
4326 */
4327static int ice_init_fdir(struct ice_pf *pf)
4328{
4329	struct device *dev = ice_pf_to_dev(pf);
4330	struct ice_vsi *ctrl_vsi;
4331	int err;
4332
4333	/* Side Band Flow Director needs to have a control VSI.
4334	 * Allocate it and store it in the PF.
4335	 */
4336	ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
4337	if (!ctrl_vsi) {
4338		dev_dbg(dev, "could not create control VSI\n");
4339		return -ENOMEM;
4340	}
4341
4342	err = ice_vsi_open_ctrl(ctrl_vsi);
4343	if (err) {
4344		dev_dbg(dev, "could not open control VSI\n");
4345		goto err_vsi_open;
4346	}
4347
4348	mutex_init(&pf->hw.fdir_fltr_lock);
4349
4350	err = ice_fdir_create_dflt_rules(pf);
4351	if (err)
4352		goto err_fdir_rule;
4353
4354	return 0;
4355
4356err_fdir_rule:
4357	ice_fdir_release_flows(&pf->hw);
4358	ice_vsi_close(ctrl_vsi);
4359err_vsi_open:
4360	ice_vsi_release(ctrl_vsi);
4361	if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4362		pf->vsi[pf->ctrl_vsi_idx] = NULL;
4363		pf->ctrl_vsi_idx = ICE_NO_VSI;
4364	}
4365	return err;
4366}
4367
4368static void ice_deinit_fdir(struct ice_pf *pf)
4369{
4370	struct ice_vsi *vsi = ice_get_ctrl_vsi(pf);
4371
4372	if (!vsi)
4373		return;
4374
4375	ice_vsi_manage_fdir(vsi, false);
4376	ice_vsi_release(vsi);
4377	if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4378		pf->vsi[pf->ctrl_vsi_idx] = NULL;
4379		pf->ctrl_vsi_idx = ICE_NO_VSI;
4380	}
4381
4382	mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
4383}
4384
4385/**
4386 * ice_get_opt_fw_name - return optional firmware file name or NULL
4387 * @pf: pointer to the PF instance
4388 */
4389static char *ice_get_opt_fw_name(struct ice_pf *pf)
4390{
4391	/* Optional firmware name same as default with additional dash
4392	 * followed by a EUI-64 identifier (PCIe Device Serial Number)
4393	 */
4394	struct pci_dev *pdev = pf->pdev;
4395	char *opt_fw_filename;
4396	u64 dsn;
4397
4398	/* Determine the name of the optional file using the DSN (two
4399	 * dwords following the start of the DSN Capability).
4400	 */
4401	dsn = pci_get_dsn(pdev);
4402	if (!dsn)
4403		return NULL;
4404
4405	opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
4406	if (!opt_fw_filename)
4407		return NULL;
4408
4409	snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
4410		 ICE_DDP_PKG_PATH, dsn);
4411
4412	return opt_fw_filename;
4413}
4414
4415/**
4416 * ice_request_fw - Device initialization routine
4417 * @pf: pointer to the PF instance
4418 */
4419static void ice_request_fw(struct ice_pf *pf)
4420{
4421	char *opt_fw_filename = ice_get_opt_fw_name(pf);
4422	const struct firmware *firmware = NULL;
4423	struct device *dev = ice_pf_to_dev(pf);
4424	int err = 0;
4425
4426	/* optional device-specific DDP (if present) overrides the default DDP
4427	 * package file. kernel logs a debug message if the file doesn't exist,
4428	 * and warning messages for other errors.
4429	 */
4430	if (opt_fw_filename) {
4431		err = firmware_request_nowarn(&firmware, opt_fw_filename, dev);
4432		if (err) {
4433			kfree(opt_fw_filename);
4434			goto dflt_pkg_load;
4435		}
4436
4437		/* request for firmware was successful. Download to device */
4438		ice_load_pkg(firmware, pf);
4439		kfree(opt_fw_filename);
4440		release_firmware(firmware);
4441		return;
4442	}
4443
4444dflt_pkg_load:
4445	err = request_firmware(&firmware, ICE_DDP_PKG_FILE, dev);
4446	if (err) {
4447		dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
4448		return;
4449	}
4450
4451	/* request for firmware was successful. Download to device */
4452	ice_load_pkg(firmware, pf);
4453	release_firmware(firmware);
4454}
4455
4456/**
4457 * ice_print_wake_reason - show the wake up cause in the log
4458 * @pf: pointer to the PF struct
4459 */
4460static void ice_print_wake_reason(struct ice_pf *pf)
4461{
4462	u32 wus = pf->wakeup_reason;
4463	const char *wake_str;
4464
4465	/* if no wake event, nothing to print */
4466	if (!wus)
4467		return;
4468
4469	if (wus & PFPM_WUS_LNKC_M)
4470		wake_str = "Link\n";
4471	else if (wus & PFPM_WUS_MAG_M)
4472		wake_str = "Magic Packet\n";
4473	else if (wus & PFPM_WUS_MNG_M)
4474		wake_str = "Management\n";
4475	else if (wus & PFPM_WUS_FW_RST_WK_M)
4476		wake_str = "Firmware Reset\n";
4477	else
4478		wake_str = "Unknown\n";
4479
4480	dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
4481}
4482
4483/**
4484 * ice_pf_fwlog_update_module - update 1 module
4485 * @pf: pointer to the PF struct
4486 * @log_level: log_level to use for the @module
4487 * @module: module to update
4488 */
4489void ice_pf_fwlog_update_module(struct ice_pf *pf, int log_level, int module)
4490{
4491	struct ice_hw *hw = &pf->hw;
4492
4493	hw->fwlog_cfg.module_entries[module].log_level = log_level;
4494}
4495
4496/**
4497 * ice_register_netdev - register netdev
4498 * @vsi: pointer to the VSI struct
4499 */
4500static int ice_register_netdev(struct ice_vsi *vsi)
4501{
4502	int err;
 
4503
 
4504	if (!vsi || !vsi->netdev)
4505		return -EIO;
4506
4507	err = register_netdev(vsi->netdev);
4508	if (err)
4509		return err;
4510
4511	set_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4512	netif_carrier_off(vsi->netdev);
4513	netif_tx_stop_all_queues(vsi->netdev);
4514
4515	return 0;
4516}
4517
4518static void ice_unregister_netdev(struct ice_vsi *vsi)
4519{
4520	if (!vsi || !vsi->netdev)
4521		return;
4522
4523	unregister_netdev(vsi->netdev);
4524	clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4525}
4526
4527/**
4528 * ice_cfg_netdev - Allocate, configure and register a netdev
4529 * @vsi: the VSI associated with the new netdev
 
4530 *
4531 * Returns 0 on success, negative value on failure
4532 */
4533static int ice_cfg_netdev(struct ice_vsi *vsi)
 
4534{
4535	struct ice_netdev_priv *np;
4536	struct net_device *netdev;
4537	u8 mac_addr[ETH_ALEN];
4538
4539	netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
4540				    vsi->alloc_rxq);
4541	if (!netdev)
4542		return -ENOMEM;
4543
4544	set_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4545	vsi->netdev = netdev;
4546	np = netdev_priv(netdev);
4547	np->vsi = vsi;
4548
4549	ice_set_netdev_features(netdev);
4550	ice_set_ops(vsi);
4551
4552	if (vsi->type == ICE_VSI_PF) {
4553		SET_NETDEV_DEV(netdev, ice_pf_to_dev(vsi->back));
4554		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
4555		eth_hw_addr_set(netdev, mac_addr);
4556	}
4557
4558	netdev->priv_flags |= IFF_UNICAST_FLT;
4559
4560	/* Setup netdev TC information */
4561	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
4562
4563	netdev->max_mtu = ICE_MAX_MTU;
4564
4565	return 0;
4566}
4567
4568static void ice_decfg_netdev(struct ice_vsi *vsi)
4569{
4570	clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4571	free_netdev(vsi->netdev);
4572	vsi->netdev = NULL;
4573}
4574
4575static int ice_start_eth(struct ice_vsi *vsi)
4576{
4577	int err;
4578
4579	err = ice_init_mac_fltr(vsi->back);
4580	if (err)
4581		return err;
4582
4583	err = ice_vsi_open(vsi);
4584	if (err)
4585		ice_fltr_remove_all(vsi);
4586
4587	return err;
4588}
4589
4590static void ice_stop_eth(struct ice_vsi *vsi)
4591{
4592	ice_fltr_remove_all(vsi);
4593	ice_vsi_close(vsi);
4594}
4595
4596static int ice_init_eth(struct ice_pf *pf)
4597{
4598	struct ice_vsi *vsi = ice_get_main_vsi(pf);
4599	int err;
4600
4601	if (!vsi)
4602		return -EINVAL;
4603
4604	/* init channel list */
4605	INIT_LIST_HEAD(&vsi->ch_list);
4606
4607	err = ice_cfg_netdev(vsi);
4608	if (err)
4609		return err;
4610	/* Setup DCB netlink interface */
4611	ice_dcbnl_setup(vsi);
4612
4613	err = ice_init_mac_fltr(pf);
4614	if (err)
4615		goto err_init_mac_fltr;
4616
4617	err = ice_devlink_create_pf_port(pf);
4618	if (err)
4619		goto err_devlink_create_pf_port;
4620
4621	SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
4622
4623	err = ice_register_netdev(vsi);
4624	if (err)
4625		goto err_register_netdev;
4626
4627	err = ice_tc_indir_block_register(vsi);
4628	if (err)
4629		goto err_tc_indir_block_register;
4630
4631	ice_napi_add(vsi);
4632
4633	return 0;
4634
4635err_tc_indir_block_register:
4636	ice_unregister_netdev(vsi);
4637err_register_netdev:
4638	ice_devlink_destroy_pf_port(pf);
4639err_devlink_create_pf_port:
4640err_init_mac_fltr:
4641	ice_decfg_netdev(vsi);
4642	return err;
4643}
4644
4645static void ice_deinit_eth(struct ice_pf *pf)
4646{
4647	struct ice_vsi *vsi = ice_get_main_vsi(pf);
4648
4649	if (!vsi)
4650		return;
 
 
 
 
4651
4652	ice_vsi_close(vsi);
4653	ice_unregister_netdev(vsi);
4654	ice_devlink_destroy_pf_port(pf);
4655	ice_tc_indir_block_unregister(vsi);
4656	ice_decfg_netdev(vsi);
4657}
4658
4659/**
4660 * ice_wait_for_fw - wait for full FW readiness
4661 * @hw: pointer to the hardware structure
4662 * @timeout: milliseconds that can elapse before timing out
4663 */
4664static int ice_wait_for_fw(struct ice_hw *hw, u32 timeout)
4665{
4666	int fw_loading;
4667	u32 elapsed = 0;
4668
4669	while (elapsed <= timeout) {
4670		fw_loading = rd32(hw, GL_MNG_FWSM) & GL_MNG_FWSM_FW_LOADING_M;
 
4671
4672		/* firmware was not yet loaded, we have to wait more */
4673		if (fw_loading) {
4674			elapsed += 100;
4675			msleep(100);
4676			continue;
4677		}
4678		return 0;
4679	}
 
4680
4681	return -ETIMEDOUT;
4682}
4683
4684static int ice_init_dev(struct ice_pf *pf)
4685{
4686	struct device *dev = ice_pf_to_dev(pf);
4687	struct ice_hw *hw = &pf->hw;
4688	int err;
4689
4690	err = ice_init_hw(hw);
4691	if (err) {
4692		dev_err(dev, "ice_init_hw failed: %d\n", err);
4693		return err;
4694	}
4695
4696	/* Some cards require longer initialization times
4697	 * due to necessity of loading FW from an external source.
4698	 * This can take even half a minute.
4699	 */
4700	if (ice_is_pf_c827(hw)) {
4701		err = ice_wait_for_fw(hw, 30000);
4702		if (err) {
4703			dev_err(dev, "ice_wait_for_fw timed out");
4704			return err;
4705		}
4706	}
4707
4708	ice_init_feature_support(pf);
4709
4710	ice_request_fw(pf);
4711
4712	/* if ice_request_fw fails, ICE_FLAG_ADV_FEATURES bit won't be
4713	 * set in pf->state, which will cause ice_is_safe_mode to return
4714	 * true
4715	 */
4716	if (ice_is_safe_mode(pf)) {
4717		/* we already got function/device capabilities but these don't
4718		 * reflect what the driver needs to do in safe mode. Instead of
4719		 * adding conditional logic everywhere to ignore these
4720		 * device/function capabilities, override them.
4721		 */
4722		ice_set_safe_mode_caps(hw);
4723	}
4724
4725	err = ice_init_pf(pf);
4726	if (err) {
4727		dev_err(dev, "ice_init_pf failed: %d\n", err);
4728		goto err_init_pf;
4729	}
4730
 
 
4731	pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4732	pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4733	pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
4734	pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
 
4735	if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4736		pf->hw.udp_tunnel_nic.tables[0].n_entries =
4737			pf->hw.tnl.valid_count[TNL_VXLAN];
4738		pf->hw.udp_tunnel_nic.tables[0].tunnel_types =
4739			UDP_TUNNEL_TYPE_VXLAN;
 
4740	}
4741	if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4742		pf->hw.udp_tunnel_nic.tables[1].n_entries =
4743			pf->hw.tnl.valid_count[TNL_GENEVE];
4744		pf->hw.udp_tunnel_nic.tables[1].tunnel_types =
4745			UDP_TUNNEL_TYPE_GENEVE;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4746	}
4747
4748	err = ice_init_interrupt_scheme(pf);
4749	if (err) {
4750		dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4751		err = -EIO;
4752		goto err_init_interrupt_scheme;
4753	}
4754
4755	/* In case of MSIX we are going to setup the misc vector right here
4756	 * to handle admin queue events etc. In case of legacy and MSI
4757	 * the misc functionality and queue processing is combined in
4758	 * the same vector and that gets setup at open.
4759	 */
4760	err = ice_req_irq_msix_misc(pf);
4761	if (err) {
4762		dev_err(dev, "setup of misc vector failed: %d\n", err);
4763		goto err_req_irq_msix_misc;
4764	}
4765
4766	return 0;
4767
4768err_req_irq_msix_misc:
4769	ice_clear_interrupt_scheme(pf);
4770err_init_interrupt_scheme:
4771	ice_deinit_pf(pf);
4772err_init_pf:
4773	ice_deinit_hw(hw);
4774	return err;
4775}
4776
4777static void ice_deinit_dev(struct ice_pf *pf)
4778{
4779	ice_free_irq_msix_misc(pf);
4780	ice_deinit_pf(pf);
4781	ice_deinit_hw(&pf->hw);
4782
4783	/* Service task is already stopped, so call reset directly. */
4784	ice_reset(&pf->hw, ICE_RESET_PFR);
4785	pci_wait_for_pending_transaction(pf->pdev);
4786	ice_clear_interrupt_scheme(pf);
4787}
4788
4789static void ice_init_features(struct ice_pf *pf)
4790{
4791	struct device *dev = ice_pf_to_dev(pf);
4792
4793	if (ice_is_safe_mode(pf))
4794		return;
4795
4796	/* initialize DDP driven features */
4797	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4798		ice_ptp_init(pf);
4799
4800	if (ice_is_feature_supported(pf, ICE_F_GNSS))
4801		ice_gnss_init(pf);
4802
4803	if (ice_is_feature_supported(pf, ICE_F_CGU) ||
4804	    ice_is_feature_supported(pf, ICE_F_PHY_RCLK))
4805		ice_dpll_init(pf);
4806
4807	/* Note: Flow director init failure is non-fatal to load */
4808	if (ice_init_fdir(pf))
4809		dev_err(dev, "could not initialize flow director\n");
4810
4811	/* Note: DCB init failure is non-fatal to load */
4812	if (ice_init_pf_dcb(pf, false)) {
4813		clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4814		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4815	} else {
4816		ice_cfg_lldp_mib_change(&pf->hw, true);
4817	}
4818
4819	if (ice_init_lag(pf))
4820		dev_warn(dev, "Failed to init link aggregation support\n");
4821
4822	ice_hwmon_init(pf);
4823}
4824
4825static void ice_deinit_features(struct ice_pf *pf)
4826{
4827	if (ice_is_safe_mode(pf))
4828		return;
4829
4830	ice_deinit_lag(pf);
4831	if (test_bit(ICE_FLAG_DCB_CAPABLE, pf->flags))
4832		ice_cfg_lldp_mib_change(&pf->hw, false);
4833	ice_deinit_fdir(pf);
4834	if (ice_is_feature_supported(pf, ICE_F_GNSS))
4835		ice_gnss_exit(pf);
4836	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4837		ice_ptp_release(pf);
4838	if (test_bit(ICE_FLAG_DPLL, pf->flags))
4839		ice_dpll_deinit(pf);
4840	if (pf->eswitch_mode == DEVLINK_ESWITCH_MODE_SWITCHDEV)
4841		xa_destroy(&pf->eswitch.reprs);
4842}
4843
4844static void ice_init_wakeup(struct ice_pf *pf)
4845{
4846	/* Save wakeup reason register for later use */
4847	pf->wakeup_reason = rd32(&pf->hw, PFPM_WUS);
4848
4849	/* check for a power management event */
4850	ice_print_wake_reason(pf);
 
 
 
4851
4852	/* clear wake status, all bits */
4853	wr32(&pf->hw, PFPM_WUS, U32_MAX);
4854
4855	/* Disable WoL at init, wait for user to enable */
4856	device_set_wakeup_enable(ice_pf_to_dev(pf), false);
4857}
 
 
 
 
4858
4859static int ice_init_link(struct ice_pf *pf)
4860{
4861	struct device *dev = ice_pf_to_dev(pf);
4862	int err;
4863
4864	err = ice_init_link_events(pf->hw.port_info);
4865	if (err) {
4866		dev_err(dev, "ice_init_link_events failed: %d\n", err);
4867		return err;
4868	}
4869
4870	/* not a fatal error if this fails */
4871	err = ice_init_nvm_phy_type(pf->hw.port_info);
4872	if (err)
4873		dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4874
4875	/* not a fatal error if this fails */
4876	err = ice_update_link_info(pf->hw.port_info);
4877	if (err)
4878		dev_err(dev, "ice_update_link_info failed: %d\n", err);
4879
4880	ice_init_link_dflt_override(pf->hw.port_info);
4881
4882	ice_check_link_cfg_err(pf,
4883			       pf->hw.port_info->phy.link_info.link_cfg_err);
4884
4885	/* if media available, initialize PHY settings */
4886	if (pf->hw.port_info->phy.link_info.link_info &
4887	    ICE_AQ_MEDIA_AVAILABLE) {
4888		/* not a fatal error if this fails */
4889		err = ice_init_phy_user_cfg(pf->hw.port_info);
4890		if (err)
4891			dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4892
4893		if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4894			struct ice_vsi *vsi = ice_get_main_vsi(pf);
4895
4896			if (vsi)
4897				ice_configure_phy(vsi);
4898		}
4899	} else {
4900		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4901	}
4902
4903	return err;
4904}
4905
4906static int ice_init_pf_sw(struct ice_pf *pf)
4907{
4908	bool dvm = ice_is_dvm_ena(&pf->hw);
4909	struct ice_vsi *vsi;
4910	int err;
4911
4912	/* create switch struct for the switch element created by FW on boot */
4913	pf->first_sw = kzalloc(sizeof(*pf->first_sw), GFP_KERNEL);
4914	if (!pf->first_sw)
4915		return -ENOMEM;
4916
4917	if (pf->hw.evb_veb)
4918		pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4919	else
4920		pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
4921
4922	pf->first_sw->pf = pf;
 
4923
4924	/* record the sw_id available for later use */
4925	pf->first_sw->sw_id = pf->hw.port_info->sw_id;
4926
4927	err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
4928	if (err)
4929		goto err_aq_set_port_params;
4930
4931	vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
4932	if (!vsi) {
4933		err = -ENOMEM;
4934		goto err_pf_vsi_setup;
4935	}
4936
4937	return 0;
4938
4939err_pf_vsi_setup:
4940err_aq_set_port_params:
4941	kfree(pf->first_sw);
4942	return err;
4943}
4944
4945static void ice_deinit_pf_sw(struct ice_pf *pf)
4946{
4947	struct ice_vsi *vsi = ice_get_main_vsi(pf);
4948
4949	if (!vsi)
4950		return;
4951
4952	ice_vsi_release(vsi);
4953	kfree(pf->first_sw);
4954}
4955
4956static int ice_alloc_vsis(struct ice_pf *pf)
4957{
4958	struct device *dev = ice_pf_to_dev(pf);
4959
4960	pf->num_alloc_vsi = pf->hw.func_caps.guar_num_vsi;
4961	if (!pf->num_alloc_vsi)
4962		return -EIO;
4963
4964	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
4965		dev_warn(dev,
4966			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
4967			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
4968		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
4969	}
4970
4971	pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
4972			       GFP_KERNEL);
4973	if (!pf->vsi)
4974		return -ENOMEM;
4975
4976	pf->vsi_stats = devm_kcalloc(dev, pf->num_alloc_vsi,
4977				     sizeof(*pf->vsi_stats), GFP_KERNEL);
4978	if (!pf->vsi_stats) {
4979		devm_kfree(dev, pf->vsi);
4980		return -ENOMEM;
 
4981	}
4982
4983	return 0;
4984}
4985
4986static void ice_dealloc_vsis(struct ice_pf *pf)
4987{
4988	devm_kfree(ice_pf_to_dev(pf), pf->vsi_stats);
4989	pf->vsi_stats = NULL;
4990
4991	pf->num_alloc_vsi = 0;
4992	devm_kfree(ice_pf_to_dev(pf), pf->vsi);
4993	pf->vsi = NULL;
4994}
4995
4996static int ice_init_devlink(struct ice_pf *pf)
4997{
4998	int err;
4999
5000	err = ice_devlink_register_params(pf);
5001	if (err)
5002		return err;
5003
5004	ice_devlink_init_regions(pf);
5005	ice_devlink_register(pf);
5006
5007	return 0;
5008}
5009
5010static void ice_deinit_devlink(struct ice_pf *pf)
5011{
5012	ice_devlink_unregister(pf);
5013	ice_devlink_destroy_regions(pf);
5014	ice_devlink_unregister_params(pf);
5015}
5016
5017static int ice_init(struct ice_pf *pf)
5018{
5019	int err;
5020
5021	err = ice_init_dev(pf);
5022	if (err)
5023		return err;
5024
5025	err = ice_alloc_vsis(pf);
 
5026	if (err)
5027		goto err_alloc_vsis;
5028
5029	err = ice_init_pf_sw(pf);
5030	if (err)
5031		goto err_init_pf_sw;
 
 
5032
5033	ice_init_wakeup(pf);
5034
5035	err = ice_init_link(pf);
5036	if (err)
5037		goto err_init_link;
5038
5039	err = ice_send_version(pf);
5040	if (err)
5041		goto err_init_link;
5042
5043	ice_verify_cacheline_size(pf);
5044
5045	if (ice_is_safe_mode(pf))
5046		ice_set_safe_mode_vlan_cfg(pf);
5047	else
5048		/* print PCI link speed and width */
5049		pcie_print_link_status(pf->pdev);
5050
5051	/* ready to go, so clear down state bit */
5052	clear_bit(ICE_DOWN, pf->state);
5053	clear_bit(ICE_SERVICE_DIS, pf->state);
 
 
 
 
 
 
5054
5055	/* since everything is good, start the service timer */
5056	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
 
 
 
 
 
 
 
5057
 
5058	return 0;
5059
5060err_init_link:
5061	ice_deinit_pf_sw(pf);
5062err_init_pf_sw:
5063	ice_dealloc_vsis(pf);
5064err_alloc_vsis:
5065	ice_deinit_dev(pf);
5066	return err;
5067}
5068
5069static void ice_deinit(struct ice_pf *pf)
5070{
5071	set_bit(ICE_SERVICE_DIS, pf->state);
5072	set_bit(ICE_DOWN, pf->state);
5073
5074	ice_deinit_pf_sw(pf);
5075	ice_dealloc_vsis(pf);
5076	ice_deinit_dev(pf);
5077}
5078
5079/**
5080 * ice_load - load pf by init hw and starting VSI
5081 * @pf: pointer to the pf instance
5082 */
5083int ice_load(struct ice_pf *pf)
5084{
5085	struct ice_vsi_cfg_params params = {};
5086	struct ice_vsi *vsi;
5087	int err;
5088
5089	err = ice_init_dev(pf);
5090	if (err)
5091		return err;
5092
5093	vsi = ice_get_main_vsi(pf);
5094
5095	params = ice_vsi_to_params(vsi);
5096	params.flags = ICE_VSI_FLAG_INIT;
5097
5098	rtnl_lock();
5099	err = ice_vsi_cfg(vsi, &params);
5100	if (err)
5101		goto err_vsi_cfg;
5102
5103	err = ice_start_eth(ice_get_main_vsi(pf));
5104	if (err)
5105		goto err_start_eth;
5106	rtnl_unlock();
5107
5108	err = ice_init_rdma(pf);
5109	if (err)
5110		goto err_init_rdma;
5111
5112	ice_init_features(pf);
5113	ice_service_task_restart(pf);
5114
5115	clear_bit(ICE_DOWN, pf->state);
5116
5117	return 0;
5118
5119err_init_rdma:
5120	ice_vsi_close(ice_get_main_vsi(pf));
5121	rtnl_lock();
5122err_start_eth:
5123	ice_vsi_decfg(ice_get_main_vsi(pf));
5124err_vsi_cfg:
5125	rtnl_unlock();
5126	ice_deinit_dev(pf);
5127	return err;
5128}
5129
5130/**
5131 * ice_unload - unload pf by stopping VSI and deinit hw
5132 * @pf: pointer to the pf instance
5133 */
5134void ice_unload(struct ice_pf *pf)
5135{
5136	ice_deinit_features(pf);
5137	ice_deinit_rdma(pf);
5138	rtnl_lock();
5139	ice_stop_eth(ice_get_main_vsi(pf));
5140	ice_vsi_decfg(ice_get_main_vsi(pf));
5141	rtnl_unlock();
5142	ice_deinit_dev(pf);
5143}
5144
5145/**
5146 * ice_probe - Device initialization routine
5147 * @pdev: PCI device information struct
5148 * @ent: entry in ice_pci_tbl
5149 *
5150 * Returns 0 on success, negative on failure
5151 */
5152static int
5153ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
5154{
5155	struct device *dev = &pdev->dev;
5156	struct ice_pf *pf;
5157	struct ice_hw *hw;
5158	int err;
5159
5160	if (pdev->is_virtfn) {
5161		dev_err(dev, "can't probe a virtual function\n");
5162		return -EINVAL;
5163	}
5164
5165	/* when under a kdump kernel initiate a reset before enabling the
5166	 * device in order to clear out any pending DMA transactions. These
5167	 * transactions can cause some systems to machine check when doing
5168	 * the pcim_enable_device() below.
5169	 */
5170	if (is_kdump_kernel()) {
5171		pci_save_state(pdev);
5172		pci_clear_master(pdev);
5173		err = pcie_flr(pdev);
5174		if (err)
5175			return err;
5176		pci_restore_state(pdev);
5177	}
5178
5179	/* this driver uses devres, see
5180	 * Documentation/driver-api/driver-model/devres.rst
5181	 */
5182	err = pcim_enable_device(pdev);
5183	if (err)
5184		return err;
5185
5186	err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), dev_driver_string(dev));
5187	if (err) {
5188		dev_err(dev, "BAR0 I/O map error %d\n", err);
5189		return err;
5190	}
5191
5192	pf = ice_allocate_pf(dev);
5193	if (!pf)
5194		return -ENOMEM;
5195
5196	/* initialize Auxiliary index to invalid value */
5197	pf->aux_idx = -1;
5198
5199	/* set up for high or low DMA */
5200	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
5201	if (err) {
5202		dev_err(dev, "DMA configuration failed: 0x%x\n", err);
5203		return err;
5204	}
5205
5206	pci_set_master(pdev);
5207
5208	pf->pdev = pdev;
5209	pci_set_drvdata(pdev, pf);
5210	set_bit(ICE_DOWN, pf->state);
5211	/* Disable service task until DOWN bit is cleared */
5212	set_bit(ICE_SERVICE_DIS, pf->state);
5213
5214	hw = &pf->hw;
5215	hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
5216	pci_save_state(pdev);
5217
5218	hw->back = pf;
5219	hw->port_info = NULL;
5220	hw->vendor_id = pdev->vendor;
5221	hw->device_id = pdev->device;
5222	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
5223	hw->subsystem_vendor_id = pdev->subsystem_vendor;
5224	hw->subsystem_device_id = pdev->subsystem_device;
5225	hw->bus.device = PCI_SLOT(pdev->devfn);
5226	hw->bus.func = PCI_FUNC(pdev->devfn);
5227	ice_set_ctrlq_len(hw);
5228
5229	pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
5230
5231#ifndef CONFIG_DYNAMIC_DEBUG
5232	if (debug < -1)
5233		hw->debug_mask = debug;
5234#endif
5235
5236	err = ice_init(pf);
5237	if (err)
5238		goto err_init;
5239
5240	err = ice_init_eth(pf);
5241	if (err)
5242		goto err_init_eth;
5243
5244	err = ice_init_rdma(pf);
5245	if (err)
5246		goto err_init_rdma;
5247
5248	err = ice_init_devlink(pf);
5249	if (err)
5250		goto err_init_devlink;
5251
5252	ice_init_features(pf);
5253
5254	return 0;
5255
5256err_init_devlink:
5257	ice_deinit_rdma(pf);
5258err_init_rdma:
5259	ice_deinit_eth(pf);
5260err_init_eth:
5261	ice_deinit(pf);
5262err_init:
5263	pci_disable_device(pdev);
5264	return err;
5265}
5266
5267/**
5268 * ice_set_wake - enable or disable Wake on LAN
5269 * @pf: pointer to the PF struct
5270 *
5271 * Simple helper for WoL control
5272 */
5273static void ice_set_wake(struct ice_pf *pf)
5274{
5275	struct ice_hw *hw = &pf->hw;
5276	bool wol = pf->wol_ena;
5277
5278	/* clear wake state, otherwise new wake events won't fire */
5279	wr32(hw, PFPM_WUS, U32_MAX);
5280
5281	/* enable / disable APM wake up, no RMW needed */
5282	wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
5283
5284	/* set magic packet filter enabled */
5285	wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
5286}
5287
5288/**
5289 * ice_setup_mc_magic_wake - setup device to wake on multicast magic packet
5290 * @pf: pointer to the PF struct
5291 *
5292 * Issue firmware command to enable multicast magic wake, making
5293 * sure that any locally administered address (LAA) is used for
5294 * wake, and that PF reset doesn't undo the LAA.
5295 */
5296static void ice_setup_mc_magic_wake(struct ice_pf *pf)
5297{
5298	struct device *dev = ice_pf_to_dev(pf);
5299	struct ice_hw *hw = &pf->hw;
5300	u8 mac_addr[ETH_ALEN];
5301	struct ice_vsi *vsi;
5302	int status;
5303	u8 flags;
5304
5305	if (!pf->wol_ena)
5306		return;
5307
5308	vsi = ice_get_main_vsi(pf);
5309	if (!vsi)
5310		return;
5311
5312	/* Get current MAC address in case it's an LAA */
5313	if (vsi->netdev)
5314		ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
5315	else
5316		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
5317
5318	flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
5319		ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
5320		ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
5321
5322	status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
5323	if (status)
5324		dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %d aq_err %s\n",
5325			status, ice_aq_str(hw->adminq.sq_last_status));
5326}
5327
5328/**
5329 * ice_remove - Device removal routine
5330 * @pdev: PCI device information struct
5331 */
5332static void ice_remove(struct pci_dev *pdev)
5333{
5334	struct ice_pf *pf = pci_get_drvdata(pdev);
5335	int i;
5336
 
5337	for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
5338		if (!ice_is_reset_in_progress(pf->state))
5339			break;
5340		msleep(100);
5341	}
5342
5343	ice_debugfs_exit();
5344
5345	if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
5346		set_bit(ICE_VF_RESETS_DISABLED, pf->state);
5347		ice_free_vfs(pf);
5348	}
5349
5350	ice_hwmon_exit(pf);
5351
5352	ice_service_task_stop(pf);
 
5353	ice_aq_cancel_waiting_tasks(pf);
 
 
 
 
5354	set_bit(ICE_DOWN, pf->state);
5355
 
 
 
 
 
5356	if (!ice_is_safe_mode(pf))
5357		ice_remove_arfs(pf);
5358	ice_deinit_features(pf);
5359	ice_deinit_devlink(pf);
5360	ice_deinit_rdma(pf);
5361	ice_deinit_eth(pf);
5362	ice_deinit(pf);
5363
5364	ice_vsi_release_all(pf);
5365
5366	ice_setup_mc_magic_wake(pf);
 
 
 
5367	ice_set_wake(pf);
 
 
 
 
 
 
 
 
 
 
 
5368
 
 
 
 
 
 
 
 
5369	pci_disable_device(pdev);
5370}
5371
5372/**
5373 * ice_shutdown - PCI callback for shutting down device
5374 * @pdev: PCI device information struct
5375 */
5376static void ice_shutdown(struct pci_dev *pdev)
5377{
5378	struct ice_pf *pf = pci_get_drvdata(pdev);
5379
5380	ice_remove(pdev);
5381
5382	if (system_state == SYSTEM_POWER_OFF) {
5383		pci_wake_from_d3(pdev, pf->wol_ena);
5384		pci_set_power_state(pdev, PCI_D3hot);
5385	}
5386}
5387
5388#ifdef CONFIG_PM
5389/**
5390 * ice_prepare_for_shutdown - prep for PCI shutdown
5391 * @pf: board private structure
5392 *
5393 * Inform or close all dependent features in prep for PCI device shutdown
5394 */
5395static void ice_prepare_for_shutdown(struct ice_pf *pf)
5396{
5397	struct ice_hw *hw = &pf->hw;
5398	u32 v;
5399
5400	/* Notify VFs of impending reset */
5401	if (ice_check_sq_alive(hw, &hw->mailboxq))
5402		ice_vc_notify_reset(pf);
5403
5404	dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
5405
5406	/* disable the VSIs and their queues that are not already DOWN */
5407	ice_pf_dis_all_vsi(pf, false);
5408
5409	ice_for_each_vsi(pf, v)
5410		if (pf->vsi[v])
5411			pf->vsi[v]->vsi_num = 0;
5412
5413	ice_shutdown_all_ctrlq(hw);
5414}
5415
5416/**
5417 * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
5418 * @pf: board private structure to reinitialize
5419 *
5420 * This routine reinitialize interrupt scheme that was cleared during
5421 * power management suspend callback.
5422 *
5423 * This should be called during resume routine to re-allocate the q_vectors
5424 * and reacquire interrupts.
5425 */
5426static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
5427{
5428	struct device *dev = ice_pf_to_dev(pf);
5429	int ret, v;
5430
5431	/* Since we clear MSIX flag during suspend, we need to
5432	 * set it back during resume...
5433	 */
5434
5435	ret = ice_init_interrupt_scheme(pf);
5436	if (ret) {
5437		dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
5438		return ret;
5439	}
5440
5441	/* Remap vectors and rings, after successful re-init interrupts */
5442	ice_for_each_vsi(pf, v) {
5443		if (!pf->vsi[v])
5444			continue;
5445
5446		ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
5447		if (ret)
5448			goto err_reinit;
5449		ice_vsi_map_rings_to_vectors(pf->vsi[v]);
5450		ice_vsi_set_napi_queues(pf->vsi[v]);
5451	}
5452
5453	ret = ice_req_irq_msix_misc(pf);
5454	if (ret) {
5455		dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
5456			ret);
5457		goto err_reinit;
5458	}
5459
5460	return 0;
5461
5462err_reinit:
5463	while (v--)
5464		if (pf->vsi[v])
5465			ice_vsi_free_q_vectors(pf->vsi[v]);
5466
5467	return ret;
5468}
5469
5470/**
5471 * ice_suspend
5472 * @dev: generic device information structure
5473 *
5474 * Power Management callback to quiesce the device and prepare
5475 * for D3 transition.
5476 */
5477static int __maybe_unused ice_suspend(struct device *dev)
5478{
5479	struct pci_dev *pdev = to_pci_dev(dev);
5480	struct ice_pf *pf;
5481	int disabled, v;
5482
5483	pf = pci_get_drvdata(pdev);
5484
5485	if (!ice_pf_state_is_nominal(pf)) {
5486		dev_err(dev, "Device is not ready, no need to suspend it\n");
5487		return -EBUSY;
5488	}
5489
5490	/* Stop watchdog tasks until resume completion.
5491	 * Even though it is most likely that the service task is
5492	 * disabled if the device is suspended or down, the service task's
5493	 * state is controlled by a different state bit, and we should
5494	 * store and honor whatever state that bit is in at this point.
5495	 */
5496	disabled = ice_service_task_stop(pf);
5497
5498	ice_unplug_aux_dev(pf);
5499
5500	/* Already suspended?, then there is nothing to do */
5501	if (test_and_set_bit(ICE_SUSPENDED, pf->state)) {
5502		if (!disabled)
5503			ice_service_task_restart(pf);
5504		return 0;
5505	}
5506
5507	if (test_bit(ICE_DOWN, pf->state) ||
5508	    ice_is_reset_in_progress(pf->state)) {
5509		dev_err(dev, "can't suspend device in reset or already down\n");
5510		if (!disabled)
5511			ice_service_task_restart(pf);
5512		return 0;
5513	}
5514
5515	ice_setup_mc_magic_wake(pf);
5516
5517	ice_prepare_for_shutdown(pf);
5518
5519	ice_set_wake(pf);
5520
5521	/* Free vectors, clear the interrupt scheme and release IRQs
5522	 * for proper hibernation, especially with large number of CPUs.
5523	 * Otherwise hibernation might fail when mapping all the vectors back
5524	 * to CPU0.
5525	 */
5526	ice_free_irq_msix_misc(pf);
5527	ice_for_each_vsi(pf, v) {
5528		if (!pf->vsi[v])
5529			continue;
5530		ice_vsi_free_q_vectors(pf->vsi[v]);
5531	}
5532	ice_clear_interrupt_scheme(pf);
5533
5534	pci_save_state(pdev);
5535	pci_wake_from_d3(pdev, pf->wol_ena);
5536	pci_set_power_state(pdev, PCI_D3hot);
5537	return 0;
5538}
5539
5540/**
5541 * ice_resume - PM callback for waking up from D3
5542 * @dev: generic device information structure
5543 */
5544static int __maybe_unused ice_resume(struct device *dev)
5545{
5546	struct pci_dev *pdev = to_pci_dev(dev);
5547	enum ice_reset_req reset_type;
5548	struct ice_pf *pf;
5549	struct ice_hw *hw;
5550	int ret;
5551
5552	pci_set_power_state(pdev, PCI_D0);
5553	pci_restore_state(pdev);
5554	pci_save_state(pdev);
5555
5556	if (!pci_device_is_present(pdev))
5557		return -ENODEV;
5558
5559	ret = pci_enable_device_mem(pdev);
5560	if (ret) {
5561		dev_err(dev, "Cannot enable device after suspend\n");
5562		return ret;
5563	}
5564
5565	pf = pci_get_drvdata(pdev);
5566	hw = &pf->hw;
5567
5568	pf->wakeup_reason = rd32(hw, PFPM_WUS);
5569	ice_print_wake_reason(pf);
5570
5571	/* We cleared the interrupt scheme when we suspended, so we need to
5572	 * restore it now to resume device functionality.
5573	 */
5574	ret = ice_reinit_interrupt_scheme(pf);
5575	if (ret)
5576		dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
5577
5578	clear_bit(ICE_DOWN, pf->state);
5579	/* Now perform PF reset and rebuild */
5580	reset_type = ICE_RESET_PFR;
5581	/* re-enable service task for reset, but allow reset to schedule it */
5582	clear_bit(ICE_SERVICE_DIS, pf->state);
5583
5584	if (ice_schedule_reset(pf, reset_type))
5585		dev_err(dev, "Reset during resume failed.\n");
5586
5587	clear_bit(ICE_SUSPENDED, pf->state);
5588	ice_service_task_restart(pf);
5589
5590	/* Restart the service task */
5591	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5592
5593	return 0;
5594}
5595#endif /* CONFIG_PM */
5596
5597/**
5598 * ice_pci_err_detected - warning that PCI error has been detected
5599 * @pdev: PCI device information struct
5600 * @err: the type of PCI error
5601 *
5602 * Called to warn that something happened on the PCI bus and the error handling
5603 * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
5604 */
5605static pci_ers_result_t
5606ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
5607{
5608	struct ice_pf *pf = pci_get_drvdata(pdev);
5609
5610	if (!pf) {
5611		dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
5612			__func__, err);
5613		return PCI_ERS_RESULT_DISCONNECT;
5614	}
5615
5616	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5617		ice_service_task_stop(pf);
5618
5619		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5620			set_bit(ICE_PFR_REQ, pf->state);
5621			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5622		}
5623	}
5624
5625	return PCI_ERS_RESULT_NEED_RESET;
5626}
5627
5628/**
5629 * ice_pci_err_slot_reset - a PCI slot reset has just happened
5630 * @pdev: PCI device information struct
5631 *
5632 * Called to determine if the driver can recover from the PCI slot reset by
5633 * using a register read to determine if the device is recoverable.
5634 */
5635static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
5636{
5637	struct ice_pf *pf = pci_get_drvdata(pdev);
5638	pci_ers_result_t result;
5639	int err;
5640	u32 reg;
5641
5642	err = pci_enable_device_mem(pdev);
5643	if (err) {
5644		dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
5645			err);
5646		result = PCI_ERS_RESULT_DISCONNECT;
5647	} else {
5648		pci_set_master(pdev);
5649		pci_restore_state(pdev);
5650		pci_save_state(pdev);
5651		pci_wake_from_d3(pdev, false);
5652
5653		/* Check for life */
5654		reg = rd32(&pf->hw, GLGEN_RTRIG);
5655		if (!reg)
5656			result = PCI_ERS_RESULT_RECOVERED;
5657		else
5658			result = PCI_ERS_RESULT_DISCONNECT;
5659	}
5660
5661	return result;
5662}
5663
5664/**
5665 * ice_pci_err_resume - restart operations after PCI error recovery
5666 * @pdev: PCI device information struct
5667 *
5668 * Called to allow the driver to bring things back up after PCI error and/or
5669 * reset recovery have finished
5670 */
5671static void ice_pci_err_resume(struct pci_dev *pdev)
5672{
5673	struct ice_pf *pf = pci_get_drvdata(pdev);
5674
5675	if (!pf) {
5676		dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
5677			__func__);
5678		return;
5679	}
5680
5681	if (test_bit(ICE_SUSPENDED, pf->state)) {
5682		dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
5683			__func__);
5684		return;
5685	}
5686
5687	ice_restore_all_vfs_msi_state(pf);
5688
5689	ice_do_reset(pf, ICE_RESET_PFR);
5690	ice_service_task_restart(pf);
5691	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5692}
5693
5694/**
5695 * ice_pci_err_reset_prepare - prepare device driver for PCI reset
5696 * @pdev: PCI device information struct
5697 */
5698static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
5699{
5700	struct ice_pf *pf = pci_get_drvdata(pdev);
5701
5702	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5703		ice_service_task_stop(pf);
5704
5705		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5706			set_bit(ICE_PFR_REQ, pf->state);
5707			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5708		}
5709	}
5710}
5711
5712/**
5713 * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
5714 * @pdev: PCI device information struct
5715 */
5716static void ice_pci_err_reset_done(struct pci_dev *pdev)
5717{
5718	ice_pci_err_resume(pdev);
5719}
5720
5721/* ice_pci_tbl - PCI Device ID Table
5722 *
5723 * Wildcard entries (PCI_ANY_ID) should come last
5724 * Last entry must be all 0s
5725 *
5726 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
5727 *   Class, Class Mask, private data (not used) }
5728 */
5729static const struct pci_device_id ice_pci_tbl[] = {
5730	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE) },
5731	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP) },
5732	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP) },
5733	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE) },
5734	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP) },
5735	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP) },
5736	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE) },
5737	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP) },
5738	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP) },
5739	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T) },
5740	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII) },
5741	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE) },
5742	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP) },
5743	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP) },
5744	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T) },
5745	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII) },
5746	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE) },
5747	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP) },
5748	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T) },
5749	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII) },
5750	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE) },
5751	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP) },
5752	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T) },
5753	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE) },
5754	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP) },
5755	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822_SI_DFLT) },
5756	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_BACKPLANE) },
5757	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_QSFP56) },
5758	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_SFP) },
5759	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E830_SFP_DD) },
5760	/* required last entry */
5761	{}
5762};
5763MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
5764
5765static __maybe_unused SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
5766
5767static const struct pci_error_handlers ice_pci_err_handler = {
5768	.error_detected = ice_pci_err_detected,
5769	.slot_reset = ice_pci_err_slot_reset,
5770	.reset_prepare = ice_pci_err_reset_prepare,
5771	.reset_done = ice_pci_err_reset_done,
5772	.resume = ice_pci_err_resume
5773};
5774
5775static struct pci_driver ice_driver = {
5776	.name = KBUILD_MODNAME,
5777	.id_table = ice_pci_tbl,
5778	.probe = ice_probe,
5779	.remove = ice_remove,
5780#ifdef CONFIG_PM
5781	.driver.pm = &ice_pm_ops,
5782#endif /* CONFIG_PM */
5783	.shutdown = ice_shutdown,
5784	.sriov_configure = ice_sriov_configure,
5785	.sriov_get_vf_total_msix = ice_sriov_get_vf_total_msix,
5786	.sriov_set_msix_vec_count = ice_sriov_set_msix_vec_count,
5787	.err_handler = &ice_pci_err_handler
5788};
5789
5790/**
5791 * ice_module_init - Driver registration routine
5792 *
5793 * ice_module_init is the first routine called when the driver is
5794 * loaded. All it does is register with the PCI subsystem.
5795 */
5796static int __init ice_module_init(void)
5797{
5798	int status = -ENOMEM;
5799
5800	pr_info("%s\n", ice_driver_string);
5801	pr_info("%s\n", ice_copyright);
5802
5803	ice_adv_lnk_speed_maps_init();
5804
5805	ice_wq = alloc_workqueue("%s", 0, 0, KBUILD_MODNAME);
5806	if (!ice_wq) {
5807		pr_err("Failed to create workqueue\n");
5808		return status;
5809	}
5810
5811	ice_lag_wq = alloc_ordered_workqueue("ice_lag_wq", 0);
5812	if (!ice_lag_wq) {
5813		pr_err("Failed to create LAG workqueue\n");
5814		goto err_dest_wq;
5815	}
5816
5817	ice_debugfs_init();
5818
5819	status = pci_register_driver(&ice_driver);
5820	if (status) {
5821		pr_err("failed to register PCI driver, err %d\n", status);
5822		goto err_dest_lag_wq;
5823	}
5824
5825	return 0;
5826
5827err_dest_lag_wq:
5828	destroy_workqueue(ice_lag_wq);
5829	ice_debugfs_exit();
5830err_dest_wq:
5831	destroy_workqueue(ice_wq);
5832	return status;
5833}
5834module_init(ice_module_init);
5835
5836/**
5837 * ice_module_exit - Driver exit cleanup routine
5838 *
5839 * ice_module_exit is called just before the driver is removed
5840 * from memory.
5841 */
5842static void __exit ice_module_exit(void)
5843{
5844	pci_unregister_driver(&ice_driver);
5845	destroy_workqueue(ice_wq);
5846	destroy_workqueue(ice_lag_wq);
5847	pr_info("module unloaded\n");
5848}
5849module_exit(ice_module_exit);
5850
5851/**
5852 * ice_set_mac_address - NDO callback to set MAC address
5853 * @netdev: network interface device structure
5854 * @pi: pointer to an address structure
5855 *
5856 * Returns 0 on success, negative on failure
5857 */
5858static int ice_set_mac_address(struct net_device *netdev, void *pi)
5859{
5860	struct ice_netdev_priv *np = netdev_priv(netdev);
5861	struct ice_vsi *vsi = np->vsi;
5862	struct ice_pf *pf = vsi->back;
5863	struct ice_hw *hw = &pf->hw;
5864	struct sockaddr *addr = pi;
5865	u8 old_mac[ETH_ALEN];
5866	u8 flags = 0;
5867	u8 *mac;
5868	int err;
5869
5870	mac = (u8 *)addr->sa_data;
5871
5872	if (!is_valid_ether_addr(mac))
5873		return -EADDRNOTAVAIL;
5874
 
 
 
 
 
5875	if (test_bit(ICE_DOWN, pf->state) ||
5876	    ice_is_reset_in_progress(pf->state)) {
5877		netdev_err(netdev, "can't set mac %pM. device not ready\n",
5878			   mac);
5879		return -EBUSY;
5880	}
5881
5882	if (ice_chnl_dmac_fltr_cnt(pf)) {
5883		netdev_err(netdev, "can't set mac %pM. Device has tc-flower filters, delete all of them and try again\n",
5884			   mac);
5885		return -EAGAIN;
5886	}
5887
5888	netif_addr_lock_bh(netdev);
5889	ether_addr_copy(old_mac, netdev->dev_addr);
5890	/* change the netdev's MAC address */
5891	eth_hw_addr_set(netdev, mac);
5892	netif_addr_unlock_bh(netdev);
5893
5894	/* Clean up old MAC filter. Not an error if old filter doesn't exist */
5895	err = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
5896	if (err && err != -ENOENT) {
5897		err = -EADDRNOTAVAIL;
5898		goto err_update_filters;
5899	}
5900
5901	/* Add filter for new MAC. If filter exists, return success */
5902	err = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
5903	if (err == -EEXIST) {
5904		/* Although this MAC filter is already present in hardware it's
5905		 * possible in some cases (e.g. bonding) that dev_addr was
5906		 * modified outside of the driver and needs to be restored back
5907		 * to this value.
5908		 */
5909		netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
5910
5911		return 0;
5912	} else if (err) {
5913		/* error if the new filter addition failed */
5914		err = -EADDRNOTAVAIL;
5915	}
5916
5917err_update_filters:
5918	if (err) {
5919		netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
5920			   mac);
5921		netif_addr_lock_bh(netdev);
5922		eth_hw_addr_set(netdev, old_mac);
5923		netif_addr_unlock_bh(netdev);
5924		return err;
5925	}
5926
5927	netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
5928		   netdev->dev_addr);
5929
5930	/* write new MAC address to the firmware */
5931	flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
5932	err = ice_aq_manage_mac_write(hw, mac, flags, NULL);
5933	if (err) {
5934		netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n",
5935			   mac, err);
5936	}
5937	return 0;
5938}
5939
5940/**
5941 * ice_set_rx_mode - NDO callback to set the netdev filters
5942 * @netdev: network interface device structure
5943 */
5944static void ice_set_rx_mode(struct net_device *netdev)
5945{
5946	struct ice_netdev_priv *np = netdev_priv(netdev);
5947	struct ice_vsi *vsi = np->vsi;
5948
5949	if (!vsi || ice_is_switchdev_running(vsi->back))
5950		return;
5951
5952	/* Set the flags to synchronize filters
5953	 * ndo_set_rx_mode may be triggered even without a change in netdev
5954	 * flags
5955	 */
5956	set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
5957	set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
5958	set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
5959
5960	/* schedule our worker thread which will take care of
5961	 * applying the new filter changes
5962	 */
5963	ice_service_task_schedule(vsi->back);
5964}
5965
5966/**
5967 * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
5968 * @netdev: network interface device structure
5969 * @queue_index: Queue ID
5970 * @maxrate: maximum bandwidth in Mbps
5971 */
5972static int
5973ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
5974{
5975	struct ice_netdev_priv *np = netdev_priv(netdev);
5976	struct ice_vsi *vsi = np->vsi;
5977	u16 q_handle;
5978	int status;
5979	u8 tc;
5980
5981	/* Validate maxrate requested is within permitted range */
5982	if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
5983		netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
5984			   maxrate, queue_index);
5985		return -EINVAL;
5986	}
5987
5988	q_handle = vsi->tx_rings[queue_index]->q_handle;
5989	tc = ice_dcb_get_tc(vsi, queue_index);
5990
5991	vsi = ice_locate_vsi_using_queue(vsi, queue_index);
5992	if (!vsi) {
5993		netdev_err(netdev, "Invalid VSI for given queue %d\n",
5994			   queue_index);
5995		return -EINVAL;
5996	}
5997
5998	/* Set BW back to default, when user set maxrate to 0 */
5999	if (!maxrate)
6000		status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
6001					       q_handle, ICE_MAX_BW);
6002	else
6003		status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
6004					  q_handle, ICE_MAX_BW, maxrate * 1000);
6005	if (status)
6006		netdev_err(netdev, "Unable to set Tx max rate, error %d\n",
6007			   status);
6008
6009	return status;
6010}
6011
6012/**
6013 * ice_fdb_add - add an entry to the hardware database
6014 * @ndm: the input from the stack
6015 * @tb: pointer to array of nladdr (unused)
6016 * @dev: the net device pointer
6017 * @addr: the MAC address entry being added
6018 * @vid: VLAN ID
6019 * @flags: instructions from stack about fdb operation
6020 * @extack: netlink extended ack
6021 */
6022static int
6023ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
6024	    struct net_device *dev, const unsigned char *addr, u16 vid,
6025	    u16 flags, struct netlink_ext_ack __always_unused *extack)
6026{
6027	int err;
6028
6029	if (vid) {
6030		netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
6031		return -EINVAL;
6032	}
6033	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
6034		netdev_err(dev, "FDB only supports static addresses\n");
6035		return -EINVAL;
6036	}
6037
6038	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
6039		err = dev_uc_add_excl(dev, addr);
6040	else if (is_multicast_ether_addr(addr))
6041		err = dev_mc_add_excl(dev, addr);
6042	else
6043		err = -EINVAL;
6044
6045	/* Only return duplicate errors if NLM_F_EXCL is set */
6046	if (err == -EEXIST && !(flags & NLM_F_EXCL))
6047		err = 0;
6048
6049	return err;
6050}
6051
6052/**
6053 * ice_fdb_del - delete an entry from the hardware database
6054 * @ndm: the input from the stack
6055 * @tb: pointer to array of nladdr (unused)
6056 * @dev: the net device pointer
6057 * @addr: the MAC address entry being added
6058 * @vid: VLAN ID
6059 * @extack: netlink extended ack
6060 */
6061static int
6062ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
6063	    struct net_device *dev, const unsigned char *addr,
6064	    __always_unused u16 vid, struct netlink_ext_ack *extack)
6065{
6066	int err;
6067
6068	if (ndm->ndm_state & NUD_PERMANENT) {
6069		netdev_err(dev, "FDB only supports static addresses\n");
6070		return -EINVAL;
6071	}
6072
6073	if (is_unicast_ether_addr(addr))
6074		err = dev_uc_del(dev, addr);
6075	else if (is_multicast_ether_addr(addr))
6076		err = dev_mc_del(dev, addr);
6077	else
6078		err = -EINVAL;
6079
6080	return err;
6081}
6082
6083#define NETIF_VLAN_OFFLOAD_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
6084					 NETIF_F_HW_VLAN_CTAG_TX | \
6085					 NETIF_F_HW_VLAN_STAG_RX | \
6086					 NETIF_F_HW_VLAN_STAG_TX)
6087
6088#define NETIF_VLAN_STRIPPING_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
6089					 NETIF_F_HW_VLAN_STAG_RX)
6090
6091#define NETIF_VLAN_FILTERING_FEATURES	(NETIF_F_HW_VLAN_CTAG_FILTER | \
6092					 NETIF_F_HW_VLAN_STAG_FILTER)
6093
6094/**
6095 * ice_fix_features - fix the netdev features flags based on device limitations
6096 * @netdev: ptr to the netdev that flags are being fixed on
6097 * @features: features that need to be checked and possibly fixed
6098 *
6099 * Make sure any fixups are made to features in this callback. This enables the
6100 * driver to not have to check unsupported configurations throughout the driver
6101 * because that's the responsiblity of this callback.
6102 *
6103 * Single VLAN Mode (SVM) Supported Features:
6104 *	NETIF_F_HW_VLAN_CTAG_FILTER
6105 *	NETIF_F_HW_VLAN_CTAG_RX
6106 *	NETIF_F_HW_VLAN_CTAG_TX
6107 *
6108 * Double VLAN Mode (DVM) Supported Features:
6109 *	NETIF_F_HW_VLAN_CTAG_FILTER
6110 *	NETIF_F_HW_VLAN_CTAG_RX
6111 *	NETIF_F_HW_VLAN_CTAG_TX
6112 *
6113 *	NETIF_F_HW_VLAN_STAG_FILTER
6114 *	NETIF_HW_VLAN_STAG_RX
6115 *	NETIF_HW_VLAN_STAG_TX
6116 *
6117 * Features that need fixing:
6118 *	Cannot simultaneously enable CTAG and STAG stripping and/or insertion.
6119 *	These are mutually exlusive as the VSI context cannot support multiple
6120 *	VLAN ethertypes simultaneously for stripping and/or insertion. If this
6121 *	is not done, then default to clearing the requested STAG offload
6122 *	settings.
6123 *
6124 *	All supported filtering has to be enabled or disabled together. For
6125 *	example, in DVM, CTAG and STAG filtering have to be enabled and disabled
6126 *	together. If this is not done, then default to VLAN filtering disabled.
6127 *	These are mutually exclusive as there is currently no way to
6128 *	enable/disable VLAN filtering based on VLAN ethertype when using VLAN
6129 *	prune rules.
6130 */
6131static netdev_features_t
6132ice_fix_features(struct net_device *netdev, netdev_features_t features)
6133{
6134	struct ice_netdev_priv *np = netdev_priv(netdev);
6135	netdev_features_t req_vlan_fltr, cur_vlan_fltr;
6136	bool cur_ctag, cur_stag, req_ctag, req_stag;
6137
6138	cur_vlan_fltr = netdev->features & NETIF_VLAN_FILTERING_FEATURES;
6139	cur_ctag = cur_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6140	cur_stag = cur_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
6141
6142	req_vlan_fltr = features & NETIF_VLAN_FILTERING_FEATURES;
6143	req_ctag = req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
6144	req_stag = req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
6145
6146	if (req_vlan_fltr != cur_vlan_fltr) {
6147		if (ice_is_dvm_ena(&np->vsi->back->hw)) {
6148			if (req_ctag && req_stag) {
6149				features |= NETIF_VLAN_FILTERING_FEATURES;
6150			} else if (!req_ctag && !req_stag) {
6151				features &= ~NETIF_VLAN_FILTERING_FEATURES;
6152			} else if ((!cur_ctag && req_ctag && !cur_stag) ||
6153				   (!cur_stag && req_stag && !cur_ctag)) {
6154				features |= NETIF_VLAN_FILTERING_FEATURES;
6155				netdev_warn(netdev,  "802.1Q and 802.1ad VLAN filtering must be either both on or both off. VLAN filtering has been enabled for both types.\n");
6156			} else if ((cur_ctag && !req_ctag && cur_stag) ||
6157				   (cur_stag && !req_stag && cur_ctag)) {
6158				features &= ~NETIF_VLAN_FILTERING_FEATURES;
6159				netdev_warn(netdev,  "802.1Q and 802.1ad VLAN filtering must be either both on or both off. VLAN filtering has been disabled for both types.\n");
6160			}
6161		} else {
6162			if (req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER)
6163				netdev_warn(netdev, "cannot support requested 802.1ad filtering setting in SVM mode\n");
6164
6165			if (req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER)
6166				features |= NETIF_F_HW_VLAN_CTAG_FILTER;
6167		}
6168	}
6169
6170	if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
6171	    (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))) {
6172		netdev_warn(netdev, "cannot support CTAG and STAG VLAN stripping and/or insertion simultaneously since CTAG and STAG offloads are mutually exclusive, clearing STAG offload settings\n");
6173		features &= ~(NETIF_F_HW_VLAN_STAG_RX |
6174			      NETIF_F_HW_VLAN_STAG_TX);
6175	}
6176
6177	if (!(netdev->features & NETIF_F_RXFCS) &&
6178	    (features & NETIF_F_RXFCS) &&
6179	    (features & NETIF_VLAN_STRIPPING_FEATURES) &&
6180	    !ice_vsi_has_non_zero_vlans(np->vsi)) {
6181		netdev_warn(netdev, "Disabling VLAN stripping as FCS/CRC stripping is also disabled and there is no VLAN configured\n");
6182		features &= ~NETIF_VLAN_STRIPPING_FEATURES;
6183	}
6184
6185	return features;
6186}
6187
6188/**
6189 * ice_set_rx_rings_vlan_proto - update rings with new stripped VLAN proto
6190 * @vsi: PF's VSI
6191 * @vlan_ethertype: VLAN ethertype (802.1Q or 802.1ad) in network byte order
6192 *
6193 * Store current stripped VLAN proto in ring packet context,
6194 * so it can be accessed more efficiently by packet processing code.
6195 */
6196static void
6197ice_set_rx_rings_vlan_proto(struct ice_vsi *vsi, __be16 vlan_ethertype)
6198{
6199	u16 i;
6200
6201	ice_for_each_alloc_rxq(vsi, i)
6202		vsi->rx_rings[i]->pkt_ctx.vlan_proto = vlan_ethertype;
6203}
6204
6205/**
6206 * ice_set_vlan_offload_features - set VLAN offload features for the PF VSI
6207 * @vsi: PF's VSI
6208 * @features: features used to determine VLAN offload settings
6209 *
6210 * First, determine the vlan_ethertype based on the VLAN offload bits in
6211 * features. Then determine if stripping and insertion should be enabled or
6212 * disabled. Finally enable or disable VLAN stripping and insertion.
6213 */
6214static int
6215ice_set_vlan_offload_features(struct ice_vsi *vsi, netdev_features_t features)
6216{
6217	bool enable_stripping = true, enable_insertion = true;
6218	struct ice_vsi_vlan_ops *vlan_ops;
6219	int strip_err = 0, insert_err = 0;
6220	u16 vlan_ethertype = 0;
6221
6222	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6223
6224	if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
6225		vlan_ethertype = ETH_P_8021AD;
6226	else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
6227		vlan_ethertype = ETH_P_8021Q;
6228
6229	if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
6230		enable_stripping = false;
6231	if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
6232		enable_insertion = false;
6233
6234	if (enable_stripping)
6235		strip_err = vlan_ops->ena_stripping(vsi, vlan_ethertype);
6236	else
6237		strip_err = vlan_ops->dis_stripping(vsi);
6238
6239	if (enable_insertion)
6240		insert_err = vlan_ops->ena_insertion(vsi, vlan_ethertype);
6241	else
6242		insert_err = vlan_ops->dis_insertion(vsi);
6243
6244	if (strip_err || insert_err)
6245		return -EIO;
6246
6247	ice_set_rx_rings_vlan_proto(vsi, enable_stripping ?
6248				    htons(vlan_ethertype) : 0);
6249
6250	return 0;
6251}
6252
6253/**
6254 * ice_set_vlan_filtering_features - set VLAN filtering features for the PF VSI
6255 * @vsi: PF's VSI
6256 * @features: features used to determine VLAN filtering settings
6257 *
6258 * Enable or disable Rx VLAN filtering based on the VLAN filtering bits in the
6259 * features.
6260 */
6261static int
6262ice_set_vlan_filtering_features(struct ice_vsi *vsi, netdev_features_t features)
6263{
6264	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
6265	int err = 0;
6266
6267	/* support Single VLAN Mode (SVM) and Double VLAN Mode (DVM) by checking
6268	 * if either bit is set
6269	 */
6270	if (features &
6271	    (NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER))
6272		err = vlan_ops->ena_rx_filtering(vsi);
6273	else
6274		err = vlan_ops->dis_rx_filtering(vsi);
6275
6276	return err;
6277}
6278
6279/**
6280 * ice_set_vlan_features - set VLAN settings based on suggested feature set
6281 * @netdev: ptr to the netdev being adjusted
6282 * @features: the feature set that the stack is suggesting
6283 *
6284 * Only update VLAN settings if the requested_vlan_features are different than
6285 * the current_vlan_features.
6286 */
6287static int
6288ice_set_vlan_features(struct net_device *netdev, netdev_features_t features)
6289{
6290	netdev_features_t current_vlan_features, requested_vlan_features;
6291	struct ice_netdev_priv *np = netdev_priv(netdev);
6292	struct ice_vsi *vsi = np->vsi;
6293	int err;
6294
6295	current_vlan_features = netdev->features & NETIF_VLAN_OFFLOAD_FEATURES;
6296	requested_vlan_features = features & NETIF_VLAN_OFFLOAD_FEATURES;
6297	if (current_vlan_features ^ requested_vlan_features) {
6298		if ((features & NETIF_F_RXFCS) &&
6299		    (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6300			dev_err(ice_pf_to_dev(vsi->back),
6301				"To enable VLAN stripping, you must first enable FCS/CRC stripping\n");
6302			return -EIO;
6303		}
6304
6305		err = ice_set_vlan_offload_features(vsi, features);
6306		if (err)
6307			return err;
6308	}
6309
6310	current_vlan_features = netdev->features &
6311		NETIF_VLAN_FILTERING_FEATURES;
6312	requested_vlan_features = features & NETIF_VLAN_FILTERING_FEATURES;
6313	if (current_vlan_features ^ requested_vlan_features) {
6314		err = ice_set_vlan_filtering_features(vsi, features);
6315		if (err)
6316			return err;
6317	}
6318
6319	return 0;
6320}
6321
6322/**
6323 * ice_set_loopback - turn on/off loopback mode on underlying PF
6324 * @vsi: ptr to VSI
6325 * @ena: flag to indicate the on/off setting
6326 */
6327static int ice_set_loopback(struct ice_vsi *vsi, bool ena)
6328{
6329	bool if_running = netif_running(vsi->netdev);
6330	int ret;
6331
6332	if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
6333		ret = ice_down(vsi);
6334		if (ret) {
6335			netdev_err(vsi->netdev, "Preparing device to toggle loopback failed\n");
6336			return ret;
6337		}
6338	}
6339	ret = ice_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
6340	if (ret)
6341		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
6342	if (if_running)
6343		ret = ice_up(vsi);
6344
6345	return ret;
6346}
6347
6348/**
6349 * ice_set_features - set the netdev feature flags
6350 * @netdev: ptr to the netdev being adjusted
6351 * @features: the feature set that the stack is suggesting
6352 */
6353static int
6354ice_set_features(struct net_device *netdev, netdev_features_t features)
6355{
6356	netdev_features_t changed = netdev->features ^ features;
6357	struct ice_netdev_priv *np = netdev_priv(netdev);
6358	struct ice_vsi *vsi = np->vsi;
6359	struct ice_pf *pf = vsi->back;
6360	int ret = 0;
6361
6362	/* Don't set any netdev advanced features with device in Safe Mode */
6363	if (ice_is_safe_mode(pf)) {
6364		dev_err(ice_pf_to_dev(pf),
6365			"Device is in Safe Mode - not enabling advanced netdev features\n");
6366		return ret;
6367	}
6368
6369	/* Do not change setting during reset */
6370	if (ice_is_reset_in_progress(pf->state)) {
6371		dev_err(ice_pf_to_dev(pf),
6372			"Device is resetting, changing advanced netdev features temporarily unavailable.\n");
6373		return -EBUSY;
6374	}
6375
6376	/* Multiple features can be changed in one call so keep features in
6377	 * separate if/else statements to guarantee each feature is checked
6378	 */
6379	if (changed & NETIF_F_RXHASH)
6380		ice_vsi_manage_rss_lut(vsi, !!(features & NETIF_F_RXHASH));
6381
6382	ret = ice_set_vlan_features(netdev, features);
6383	if (ret)
6384		return ret;
6385
6386	/* Turn on receive of FCS aka CRC, and after setting this
6387	 * flag the packet data will have the 4 byte CRC appended
6388	 */
6389	if (changed & NETIF_F_RXFCS) {
6390		if ((features & NETIF_F_RXFCS) &&
6391		    (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6392			dev_err(ice_pf_to_dev(vsi->back),
6393				"To disable FCS/CRC stripping, you must first disable VLAN stripping\n");
6394			return -EIO;
6395		}
6396
6397		ice_vsi_cfg_crc_strip(vsi, !!(features & NETIF_F_RXFCS));
6398		ret = ice_down_up(vsi);
6399		if (ret)
6400			return ret;
6401	}
6402
6403	if (changed & NETIF_F_NTUPLE) {
6404		bool ena = !!(features & NETIF_F_NTUPLE);
6405
6406		ice_vsi_manage_fdir(vsi, ena);
6407		ena ? ice_init_arfs(vsi) : ice_clear_arfs(vsi);
6408	}
6409
6410	/* don't turn off hw_tc_offload when ADQ is already enabled */
6411	if (!(features & NETIF_F_HW_TC) && ice_is_adq_active(pf)) {
6412		dev_err(ice_pf_to_dev(pf), "ADQ is active, can't turn hw_tc_offload off\n");
6413		return -EACCES;
6414	}
6415
6416	if (changed & NETIF_F_HW_TC) {
6417		bool ena = !!(features & NETIF_F_HW_TC);
6418
6419		ena ? set_bit(ICE_FLAG_CLS_FLOWER, pf->flags) :
6420		      clear_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
6421	}
6422
6423	if (changed & NETIF_F_LOOPBACK)
6424		ret = ice_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
6425
6426	return ret;
6427}
6428
6429/**
6430 * ice_vsi_vlan_setup - Setup VLAN offload properties on a PF VSI
6431 * @vsi: VSI to setup VLAN properties for
6432 */
6433static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
6434{
6435	int err;
6436
6437	err = ice_set_vlan_offload_features(vsi, vsi->netdev->features);
6438	if (err)
6439		return err;
6440
6441	err = ice_set_vlan_filtering_features(vsi, vsi->netdev->features);
6442	if (err)
6443		return err;
6444
6445	return ice_vsi_add_vlan_zero(vsi);
6446}
6447
6448/**
6449 * ice_vsi_cfg_lan - Setup the VSI lan related config
6450 * @vsi: the VSI being configured
6451 *
6452 * Return 0 on success and negative value on error
6453 */
6454int ice_vsi_cfg_lan(struct ice_vsi *vsi)
6455{
6456	int err;
6457
6458	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
6459		ice_set_rx_mode(vsi->netdev);
6460
6461		err = ice_vsi_vlan_setup(vsi);
6462		if (err)
6463			return err;
 
 
 
6464	}
6465	ice_vsi_cfg_dcb_rings(vsi);
6466
6467	err = ice_vsi_cfg_lan_txqs(vsi);
6468	if (!err && ice_is_xdp_ena_vsi(vsi))
6469		err = ice_vsi_cfg_xdp_txqs(vsi);
6470	if (!err)
6471		err = ice_vsi_cfg_rxqs(vsi);
6472
6473	return err;
6474}
6475
6476/* THEORY OF MODERATION:
6477 * The ice driver hardware works differently than the hardware that DIMLIB was
6478 * originally made for. ice hardware doesn't have packet count limits that
6479 * can trigger an interrupt, but it *does* have interrupt rate limit support,
6480 * which is hard-coded to a limit of 250,000 ints/second.
6481 * If not using dynamic moderation, the INTRL value can be modified
6482 * by ethtool rx-usecs-high.
6483 */
6484struct ice_dim {
6485	/* the throttle rate for interrupts, basically worst case delay before
6486	 * an initial interrupt fires, value is stored in microseconds.
6487	 */
6488	u16 itr;
6489};
6490
6491/* Make a different profile for Rx that doesn't allow quite so aggressive
6492 * moderation at the high end (it maxes out at 126us or about 8k interrupts a
6493 * second.
6494 */
6495static const struct ice_dim rx_profile[] = {
6496	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6497	{8},    /* 125,000 ints/s */
6498	{16},   /*  62,500 ints/s */
6499	{62},   /*  16,129 ints/s */
6500	{126}   /*   7,936 ints/s */
6501};
6502
6503/* The transmit profile, which has the same sorts of values
6504 * as the previous struct
6505 */
6506static const struct ice_dim tx_profile[] = {
6507	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6508	{8},    /* 125,000 ints/s */
6509	{40},   /*  16,125 ints/s */
6510	{128},  /*   7,812 ints/s */
6511	{256}   /*   3,906 ints/s */
6512};
6513
6514static void ice_tx_dim_work(struct work_struct *work)
6515{
6516	struct ice_ring_container *rc;
6517	struct dim *dim;
6518	u16 itr;
6519
6520	dim = container_of(work, struct dim, work);
6521	rc = dim->priv;
6522
6523	WARN_ON(dim->profile_ix >= ARRAY_SIZE(tx_profile));
6524
6525	/* look up the values in our local table */
6526	itr = tx_profile[dim->profile_ix].itr;
6527
6528	ice_trace(tx_dim_work, container_of(rc, struct ice_q_vector, tx), dim);
6529	ice_write_itr(rc, itr);
6530
6531	dim->state = DIM_START_MEASURE;
6532}
6533
6534static void ice_rx_dim_work(struct work_struct *work)
6535{
6536	struct ice_ring_container *rc;
6537	struct dim *dim;
6538	u16 itr;
6539
6540	dim = container_of(work, struct dim, work);
6541	rc = dim->priv;
6542
6543	WARN_ON(dim->profile_ix >= ARRAY_SIZE(rx_profile));
6544
6545	/* look up the values in our local table */
6546	itr = rx_profile[dim->profile_ix].itr;
6547
6548	ice_trace(rx_dim_work, container_of(rc, struct ice_q_vector, rx), dim);
6549	ice_write_itr(rc, itr);
6550
6551	dim->state = DIM_START_MEASURE;
6552}
6553
6554#define ICE_DIM_DEFAULT_PROFILE_IX 1
6555
6556/**
6557 * ice_init_moderation - set up interrupt moderation
6558 * @q_vector: the vector containing rings to be configured
6559 *
6560 * Set up interrupt moderation registers, with the intent to do the right thing
6561 * when called from reset or from probe, and whether or not dynamic moderation
6562 * is enabled or not. Take special care to write all the registers in both
6563 * dynamic moderation mode or not in order to make sure hardware is in a known
6564 * state.
6565 */
6566static void ice_init_moderation(struct ice_q_vector *q_vector)
6567{
6568	struct ice_ring_container *rc;
6569	bool tx_dynamic, rx_dynamic;
6570
6571	rc = &q_vector->tx;
6572	INIT_WORK(&rc->dim.work, ice_tx_dim_work);
6573	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6574	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6575	rc->dim.priv = rc;
6576	tx_dynamic = ITR_IS_DYNAMIC(rc);
6577
6578	/* set the initial TX ITR to match the above */
6579	ice_write_itr(rc, tx_dynamic ?
6580		      tx_profile[rc->dim.profile_ix].itr : rc->itr_setting);
6581
6582	rc = &q_vector->rx;
6583	INIT_WORK(&rc->dim.work, ice_rx_dim_work);
6584	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6585	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6586	rc->dim.priv = rc;
6587	rx_dynamic = ITR_IS_DYNAMIC(rc);
6588
6589	/* set the initial RX ITR to match the above */
6590	ice_write_itr(rc, rx_dynamic ? rx_profile[rc->dim.profile_ix].itr :
6591				       rc->itr_setting);
6592
6593	ice_set_q_vector_intrl(q_vector);
6594}
6595
6596/**
6597 * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
6598 * @vsi: the VSI being configured
6599 */
6600static void ice_napi_enable_all(struct ice_vsi *vsi)
6601{
6602	int q_idx;
6603
6604	if (!vsi->netdev)
6605		return;
6606
6607	ice_for_each_q_vector(vsi, q_idx) {
6608		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6609
6610		ice_init_moderation(q_vector);
6611
6612		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6613			napi_enable(&q_vector->napi);
6614	}
6615}
6616
6617/**
6618 * ice_up_complete - Finish the last steps of bringing up a connection
6619 * @vsi: The VSI being configured
6620 *
6621 * Return 0 on success and negative value on error
6622 */
6623static int ice_up_complete(struct ice_vsi *vsi)
6624{
6625	struct ice_pf *pf = vsi->back;
6626	int err;
6627
6628	ice_vsi_cfg_msix(vsi);
6629
6630	/* Enable only Rx rings, Tx rings were enabled by the FW when the
6631	 * Tx queue group list was configured and the context bits were
6632	 * programmed using ice_vsi_cfg_txqs
6633	 */
6634	err = ice_vsi_start_all_rx_rings(vsi);
6635	if (err)
6636		return err;
6637
6638	clear_bit(ICE_VSI_DOWN, vsi->state);
6639	ice_napi_enable_all(vsi);
6640	ice_vsi_ena_irq(vsi);
6641
6642	if (vsi->port_info &&
6643	    (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
6644	    vsi->netdev && vsi->type == ICE_VSI_PF) {
6645		ice_print_link_msg(vsi, true);
6646		netif_tx_start_all_queues(vsi->netdev);
6647		netif_carrier_on(vsi->netdev);
6648		ice_ptp_link_change(pf, pf->hw.pf_id, true);
6649	}
6650
6651	/* Perform an initial read of the statistics registers now to
6652	 * set the baseline so counters are ready when interface is up
6653	 */
6654	ice_update_eth_stats(vsi);
6655
6656	if (vsi->type == ICE_VSI_PF)
6657		ice_service_task_schedule(pf);
6658
6659	return 0;
6660}
6661
6662/**
6663 * ice_up - Bring the connection back up after being down
6664 * @vsi: VSI being configured
6665 */
6666int ice_up(struct ice_vsi *vsi)
6667{
6668	int err;
6669
6670	err = ice_vsi_cfg_lan(vsi);
6671	if (!err)
6672		err = ice_up_complete(vsi);
6673
6674	return err;
6675}
6676
6677/**
6678 * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
6679 * @syncp: pointer to u64_stats_sync
6680 * @stats: stats that pkts and bytes count will be taken from
6681 * @pkts: packets stats counter
6682 * @bytes: bytes stats counter
6683 *
6684 * This function fetches stats from the ring considering the atomic operations
6685 * that needs to be performed to read u64 values in 32 bit machine.
6686 */
6687void
6688ice_fetch_u64_stats_per_ring(struct u64_stats_sync *syncp,
6689			     struct ice_q_stats stats, u64 *pkts, u64 *bytes)
6690{
6691	unsigned int start;
6692
6693	do {
6694		start = u64_stats_fetch_begin(syncp);
6695		*pkts = stats.pkts;
6696		*bytes = stats.bytes;
6697	} while (u64_stats_fetch_retry(syncp, start));
6698}
6699
6700/**
6701 * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
6702 * @vsi: the VSI to be updated
6703 * @vsi_stats: the stats struct to be updated
6704 * @rings: rings to work on
6705 * @count: number of rings
6706 */
6707static void
6708ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi,
6709			     struct rtnl_link_stats64 *vsi_stats,
6710			     struct ice_tx_ring **rings, u16 count)
6711{
6712	u16 i;
6713
6714	for (i = 0; i < count; i++) {
6715		struct ice_tx_ring *ring;
6716		u64 pkts = 0, bytes = 0;
6717
6718		ring = READ_ONCE(rings[i]);
6719		if (!ring || !ring->ring_stats)
6720			continue;
6721		ice_fetch_u64_stats_per_ring(&ring->ring_stats->syncp,
6722					     ring->ring_stats->stats, &pkts,
6723					     &bytes);
6724		vsi_stats->tx_packets += pkts;
6725		vsi_stats->tx_bytes += bytes;
6726		vsi->tx_restart += ring->ring_stats->tx_stats.restart_q;
6727		vsi->tx_busy += ring->ring_stats->tx_stats.tx_busy;
6728		vsi->tx_linearize += ring->ring_stats->tx_stats.tx_linearize;
6729	}
6730}
6731
6732/**
6733 * ice_update_vsi_ring_stats - Update VSI stats counters
6734 * @vsi: the VSI to be updated
6735 */
6736static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
6737{
6738	struct rtnl_link_stats64 *net_stats, *stats_prev;
6739	struct rtnl_link_stats64 *vsi_stats;
6740	u64 pkts, bytes;
6741	int i;
6742
6743	vsi_stats = kzalloc(sizeof(*vsi_stats), GFP_ATOMIC);
6744	if (!vsi_stats)
6745		return;
6746
6747	/* reset non-netdev (extended) stats */
6748	vsi->tx_restart = 0;
6749	vsi->tx_busy = 0;
6750	vsi->tx_linearize = 0;
6751	vsi->rx_buf_failed = 0;
6752	vsi->rx_page_failed = 0;
6753
6754	rcu_read_lock();
6755
6756	/* update Tx rings counters */
6757	ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->tx_rings,
6758				     vsi->num_txq);
6759
6760	/* update Rx rings counters */
6761	ice_for_each_rxq(vsi, i) {
6762		struct ice_rx_ring *ring = READ_ONCE(vsi->rx_rings[i]);
6763		struct ice_ring_stats *ring_stats;
6764
6765		ring_stats = ring->ring_stats;
6766		ice_fetch_u64_stats_per_ring(&ring_stats->syncp,
6767					     ring_stats->stats, &pkts,
6768					     &bytes);
6769		vsi_stats->rx_packets += pkts;
6770		vsi_stats->rx_bytes += bytes;
6771		vsi->rx_buf_failed += ring_stats->rx_stats.alloc_buf_failed;
6772		vsi->rx_page_failed += ring_stats->rx_stats.alloc_page_failed;
6773	}
6774
6775	/* update XDP Tx rings counters */
6776	if (ice_is_xdp_ena_vsi(vsi))
6777		ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->xdp_rings,
6778					     vsi->num_xdp_txq);
6779
6780	rcu_read_unlock();
6781
6782	net_stats = &vsi->net_stats;
6783	stats_prev = &vsi->net_stats_prev;
6784
6785	/* clear prev counters after reset */
6786	if (vsi_stats->tx_packets < stats_prev->tx_packets ||
6787	    vsi_stats->rx_packets < stats_prev->rx_packets) {
6788		stats_prev->tx_packets = 0;
6789		stats_prev->tx_bytes = 0;
6790		stats_prev->rx_packets = 0;
6791		stats_prev->rx_bytes = 0;
6792	}
6793
6794	/* update netdev counters */
6795	net_stats->tx_packets += vsi_stats->tx_packets - stats_prev->tx_packets;
6796	net_stats->tx_bytes += vsi_stats->tx_bytes - stats_prev->tx_bytes;
6797	net_stats->rx_packets += vsi_stats->rx_packets - stats_prev->rx_packets;
6798	net_stats->rx_bytes += vsi_stats->rx_bytes - stats_prev->rx_bytes;
6799
6800	stats_prev->tx_packets = vsi_stats->tx_packets;
6801	stats_prev->tx_bytes = vsi_stats->tx_bytes;
6802	stats_prev->rx_packets = vsi_stats->rx_packets;
6803	stats_prev->rx_bytes = vsi_stats->rx_bytes;
6804
6805	kfree(vsi_stats);
6806}
6807
6808/**
6809 * ice_update_vsi_stats - Update VSI stats counters
6810 * @vsi: the VSI to be updated
6811 */
6812void ice_update_vsi_stats(struct ice_vsi *vsi)
6813{
6814	struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
6815	struct ice_eth_stats *cur_es = &vsi->eth_stats;
6816	struct ice_pf *pf = vsi->back;
6817
6818	if (test_bit(ICE_VSI_DOWN, vsi->state) ||
6819	    test_bit(ICE_CFG_BUSY, pf->state))
6820		return;
6821
6822	/* get stats as recorded by Tx/Rx rings */
6823	ice_update_vsi_ring_stats(vsi);
6824
6825	/* get VSI stats as recorded by the hardware */
6826	ice_update_eth_stats(vsi);
6827
6828	cur_ns->tx_errors = cur_es->tx_errors;
6829	cur_ns->rx_dropped = cur_es->rx_discards;
6830	cur_ns->tx_dropped = cur_es->tx_discards;
6831	cur_ns->multicast = cur_es->rx_multicast;
6832
6833	/* update some more netdev stats if this is main VSI */
6834	if (vsi->type == ICE_VSI_PF) {
6835		cur_ns->rx_crc_errors = pf->stats.crc_errors;
6836		cur_ns->rx_errors = pf->stats.crc_errors +
6837				    pf->stats.illegal_bytes +
 
6838				    pf->stats.rx_undersize +
6839				    pf->hw_csum_rx_error +
6840				    pf->stats.rx_jabber +
6841				    pf->stats.rx_fragments +
6842				    pf->stats.rx_oversize;
 
6843		/* record drops from the port level */
6844		cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
6845	}
6846}
6847
6848/**
6849 * ice_update_pf_stats - Update PF port stats counters
6850 * @pf: PF whose stats needs to be updated
6851 */
6852void ice_update_pf_stats(struct ice_pf *pf)
6853{
6854	struct ice_hw_port_stats *prev_ps, *cur_ps;
6855	struct ice_hw *hw = &pf->hw;
6856	u16 fd_ctr_base;
6857	u8 port;
6858
6859	port = hw->port_info->lport;
6860	prev_ps = &pf->stats_prev;
6861	cur_ps = &pf->stats;
6862
6863	if (ice_is_reset_in_progress(pf->state))
6864		pf->stat_prev_loaded = false;
6865
6866	ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
6867			  &prev_ps->eth.rx_bytes,
6868			  &cur_ps->eth.rx_bytes);
6869
6870	ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
6871			  &prev_ps->eth.rx_unicast,
6872			  &cur_ps->eth.rx_unicast);
6873
6874	ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
6875			  &prev_ps->eth.rx_multicast,
6876			  &cur_ps->eth.rx_multicast);
6877
6878	ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
6879			  &prev_ps->eth.rx_broadcast,
6880			  &cur_ps->eth.rx_broadcast);
6881
6882	ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
6883			  &prev_ps->eth.rx_discards,
6884			  &cur_ps->eth.rx_discards);
6885
6886	ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
6887			  &prev_ps->eth.tx_bytes,
6888			  &cur_ps->eth.tx_bytes);
6889
6890	ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
6891			  &prev_ps->eth.tx_unicast,
6892			  &cur_ps->eth.tx_unicast);
6893
6894	ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
6895			  &prev_ps->eth.tx_multicast,
6896			  &cur_ps->eth.tx_multicast);
6897
6898	ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
6899			  &prev_ps->eth.tx_broadcast,
6900			  &cur_ps->eth.tx_broadcast);
6901
6902	ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
6903			  &prev_ps->tx_dropped_link_down,
6904			  &cur_ps->tx_dropped_link_down);
6905
6906	ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
6907			  &prev_ps->rx_size_64, &cur_ps->rx_size_64);
6908
6909	ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
6910			  &prev_ps->rx_size_127, &cur_ps->rx_size_127);
6911
6912	ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
6913			  &prev_ps->rx_size_255, &cur_ps->rx_size_255);
6914
6915	ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
6916			  &prev_ps->rx_size_511, &cur_ps->rx_size_511);
6917
6918	ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
6919			  &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
6920
6921	ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
6922			  &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
6923
6924	ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
6925			  &prev_ps->rx_size_big, &cur_ps->rx_size_big);
6926
6927	ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
6928			  &prev_ps->tx_size_64, &cur_ps->tx_size_64);
6929
6930	ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
6931			  &prev_ps->tx_size_127, &cur_ps->tx_size_127);
6932
6933	ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
6934			  &prev_ps->tx_size_255, &cur_ps->tx_size_255);
6935
6936	ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
6937			  &prev_ps->tx_size_511, &cur_ps->tx_size_511);
6938
6939	ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
6940			  &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
6941
6942	ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
6943			  &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
6944
6945	ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
6946			  &prev_ps->tx_size_big, &cur_ps->tx_size_big);
6947
6948	fd_ctr_base = hw->fd_ctr_base;
6949
6950	ice_stat_update40(hw,
6951			  GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
6952			  pf->stat_prev_loaded, &prev_ps->fd_sb_match,
6953			  &cur_ps->fd_sb_match);
6954	ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
6955			  &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
6956
6957	ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
6958			  &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
6959
6960	ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
6961			  &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
6962
6963	ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
6964			  &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
6965
6966	ice_update_dcb_stats(pf);
6967
6968	ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
6969			  &prev_ps->crc_errors, &cur_ps->crc_errors);
6970
6971	ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
6972			  &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
6973
6974	ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
6975			  &prev_ps->mac_local_faults,
6976			  &cur_ps->mac_local_faults);
6977
6978	ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
6979			  &prev_ps->mac_remote_faults,
6980			  &cur_ps->mac_remote_faults);
6981
 
 
 
6982	ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
6983			  &prev_ps->rx_undersize, &cur_ps->rx_undersize);
6984
6985	ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
6986			  &prev_ps->rx_fragments, &cur_ps->rx_fragments);
6987
6988	ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
6989			  &prev_ps->rx_oversize, &cur_ps->rx_oversize);
6990
6991	ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
6992			  &prev_ps->rx_jabber, &cur_ps->rx_jabber);
6993
6994	cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
6995
6996	pf->stat_prev_loaded = true;
6997}
6998
6999/**
7000 * ice_get_stats64 - get statistics for network device structure
7001 * @netdev: network interface device structure
7002 * @stats: main device statistics structure
7003 */
7004static
7005void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
7006{
7007	struct ice_netdev_priv *np = netdev_priv(netdev);
7008	struct rtnl_link_stats64 *vsi_stats;
7009	struct ice_vsi *vsi = np->vsi;
7010
7011	vsi_stats = &vsi->net_stats;
7012
7013	if (!vsi->num_txq || !vsi->num_rxq)
7014		return;
7015
7016	/* netdev packet/byte stats come from ring counter. These are obtained
7017	 * by summing up ring counters (done by ice_update_vsi_ring_stats).
7018	 * But, only call the update routine and read the registers if VSI is
7019	 * not down.
7020	 */
7021	if (!test_bit(ICE_VSI_DOWN, vsi->state))
7022		ice_update_vsi_ring_stats(vsi);
7023	stats->tx_packets = vsi_stats->tx_packets;
7024	stats->tx_bytes = vsi_stats->tx_bytes;
7025	stats->rx_packets = vsi_stats->rx_packets;
7026	stats->rx_bytes = vsi_stats->rx_bytes;
7027
7028	/* The rest of the stats can be read from the hardware but instead we
7029	 * just return values that the watchdog task has already obtained from
7030	 * the hardware.
7031	 */
7032	stats->multicast = vsi_stats->multicast;
7033	stats->tx_errors = vsi_stats->tx_errors;
7034	stats->tx_dropped = vsi_stats->tx_dropped;
7035	stats->rx_errors = vsi_stats->rx_errors;
7036	stats->rx_dropped = vsi_stats->rx_dropped;
7037	stats->rx_crc_errors = vsi_stats->rx_crc_errors;
7038	stats->rx_length_errors = vsi_stats->rx_length_errors;
7039}
7040
7041/**
7042 * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
7043 * @vsi: VSI having NAPI disabled
7044 */
7045static void ice_napi_disable_all(struct ice_vsi *vsi)
7046{
7047	int q_idx;
7048
7049	if (!vsi->netdev)
7050		return;
7051
7052	ice_for_each_q_vector(vsi, q_idx) {
7053		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
7054
7055		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
7056			napi_disable(&q_vector->napi);
7057
7058		cancel_work_sync(&q_vector->tx.dim.work);
7059		cancel_work_sync(&q_vector->rx.dim.work);
7060	}
7061}
7062
7063/**
7064 * ice_down - Shutdown the connection
7065 * @vsi: The VSI being stopped
7066 *
7067 * Caller of this function is expected to set the vsi->state ICE_DOWN bit
7068 */
7069int ice_down(struct ice_vsi *vsi)
7070{
7071	int i, tx_err, rx_err, vlan_err = 0;
7072
7073	WARN_ON(!test_bit(ICE_VSI_DOWN, vsi->state));
7074
7075	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
7076		vlan_err = ice_vsi_del_vlan_zero(vsi);
7077		ice_ptp_link_change(vsi->back, vsi->back->hw.pf_id, false);
7078		netif_carrier_off(vsi->netdev);
7079		netif_tx_disable(vsi->netdev);
7080	} else if (vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
7081		ice_eswitch_stop_all_tx_queues(vsi->back);
7082	}
7083
7084	ice_vsi_dis_irq(vsi);
7085
7086	tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
7087	if (tx_err)
7088		netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
7089			   vsi->vsi_num, tx_err);
7090	if (!tx_err && ice_is_xdp_ena_vsi(vsi)) {
7091		tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
7092		if (tx_err)
7093			netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
7094				   vsi->vsi_num, tx_err);
7095	}
7096
7097	rx_err = ice_vsi_stop_all_rx_rings(vsi);
7098	if (rx_err)
7099		netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
7100			   vsi->vsi_num, rx_err);
7101
7102	ice_napi_disable_all(vsi);
7103
7104	ice_for_each_txq(vsi, i)
7105		ice_clean_tx_ring(vsi->tx_rings[i]);
7106
7107	if (ice_is_xdp_ena_vsi(vsi))
7108		ice_for_each_xdp_txq(vsi, i)
7109			ice_clean_tx_ring(vsi->xdp_rings[i]);
7110
7111	ice_for_each_rxq(vsi, i)
7112		ice_clean_rx_ring(vsi->rx_rings[i]);
7113
7114	if (tx_err || rx_err || vlan_err) {
7115		netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
7116			   vsi->vsi_num, vsi->vsw->sw_id);
7117		return -EIO;
7118	}
7119
7120	return 0;
7121}
7122
7123/**
7124 * ice_down_up - shutdown the VSI connection and bring it up
7125 * @vsi: the VSI to be reconnected
7126 */
7127int ice_down_up(struct ice_vsi *vsi)
7128{
7129	int ret;
7130
7131	/* if DOWN already set, nothing to do */
7132	if (test_and_set_bit(ICE_VSI_DOWN, vsi->state))
7133		return 0;
7134
7135	ret = ice_down(vsi);
7136	if (ret)
7137		return ret;
7138
7139	ret = ice_up(vsi);
7140	if (ret) {
7141		netdev_err(vsi->netdev, "reallocating resources failed during netdev features change, may need to reload driver\n");
7142		return ret;
7143	}
7144
7145	return 0;
7146}
7147
7148/**
7149 * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
7150 * @vsi: VSI having resources allocated
7151 *
7152 * Return 0 on success, negative on failure
7153 */
7154int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
7155{
7156	int i, err = 0;
7157
7158	if (!vsi->num_txq) {
7159		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
7160			vsi->vsi_num);
7161		return -EINVAL;
7162	}
7163
7164	ice_for_each_txq(vsi, i) {
7165		struct ice_tx_ring *ring = vsi->tx_rings[i];
7166
7167		if (!ring)
7168			return -EINVAL;
7169
7170		if (vsi->netdev)
7171			ring->netdev = vsi->netdev;
7172		err = ice_setup_tx_ring(ring);
7173		if (err)
7174			break;
7175	}
7176
7177	return err;
7178}
7179
7180/**
7181 * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
7182 * @vsi: VSI having resources allocated
7183 *
7184 * Return 0 on success, negative on failure
7185 */
7186int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
7187{
7188	int i, err = 0;
7189
7190	if (!vsi->num_rxq) {
7191		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
7192			vsi->vsi_num);
7193		return -EINVAL;
7194	}
7195
7196	ice_for_each_rxq(vsi, i) {
7197		struct ice_rx_ring *ring = vsi->rx_rings[i];
7198
7199		if (!ring)
7200			return -EINVAL;
7201
7202		if (vsi->netdev)
7203			ring->netdev = vsi->netdev;
7204		err = ice_setup_rx_ring(ring);
7205		if (err)
7206			break;
7207	}
7208
7209	return err;
7210}
7211
7212/**
7213 * ice_vsi_open_ctrl - open control VSI for use
7214 * @vsi: the VSI to open
7215 *
7216 * Initialization of the Control VSI
7217 *
7218 * Returns 0 on success, negative value on error
7219 */
7220int ice_vsi_open_ctrl(struct ice_vsi *vsi)
7221{
7222	char int_name[ICE_INT_NAME_STR_LEN];
7223	struct ice_pf *pf = vsi->back;
7224	struct device *dev;
7225	int err;
7226
7227	dev = ice_pf_to_dev(pf);
7228	/* allocate descriptors */
7229	err = ice_vsi_setup_tx_rings(vsi);
7230	if (err)
7231		goto err_setup_tx;
7232
7233	err = ice_vsi_setup_rx_rings(vsi);
7234	if (err)
7235		goto err_setup_rx;
7236
7237	err = ice_vsi_cfg_lan(vsi);
7238	if (err)
7239		goto err_setup_rx;
7240
7241	snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
7242		 dev_driver_string(dev), dev_name(dev));
7243	err = ice_vsi_req_irq_msix(vsi, int_name);
7244	if (err)
7245		goto err_setup_rx;
7246
7247	ice_vsi_cfg_msix(vsi);
7248
7249	err = ice_vsi_start_all_rx_rings(vsi);
7250	if (err)
7251		goto err_up_complete;
7252
7253	clear_bit(ICE_VSI_DOWN, vsi->state);
7254	ice_vsi_ena_irq(vsi);
7255
7256	return 0;
7257
7258err_up_complete:
7259	ice_down(vsi);
7260err_setup_rx:
7261	ice_vsi_free_rx_rings(vsi);
7262err_setup_tx:
7263	ice_vsi_free_tx_rings(vsi);
7264
7265	return err;
7266}
7267
7268/**
7269 * ice_vsi_open - Called when a network interface is made active
7270 * @vsi: the VSI to open
7271 *
7272 * Initialization of the VSI
7273 *
7274 * Returns 0 on success, negative value on error
7275 */
7276int ice_vsi_open(struct ice_vsi *vsi)
7277{
7278	char int_name[ICE_INT_NAME_STR_LEN];
7279	struct ice_pf *pf = vsi->back;
7280	int err;
7281
7282	/* allocate descriptors */
7283	err = ice_vsi_setup_tx_rings(vsi);
7284	if (err)
7285		goto err_setup_tx;
7286
7287	err = ice_vsi_setup_rx_rings(vsi);
7288	if (err)
7289		goto err_setup_rx;
7290
7291	err = ice_vsi_cfg_lan(vsi);
7292	if (err)
7293		goto err_setup_rx;
7294
7295	snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7296		 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
7297	err = ice_vsi_req_irq_msix(vsi, int_name);
7298	if (err)
7299		goto err_setup_rx;
7300
7301	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
7302
7303	if (vsi->type == ICE_VSI_PF) {
7304		/* Notify the stack of the actual queue counts. */
7305		err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
7306		if (err)
7307			goto err_set_qs;
7308
7309		err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
7310		if (err)
7311			goto err_set_qs;
7312	}
7313
7314	err = ice_up_complete(vsi);
7315	if (err)
7316		goto err_up_complete;
7317
7318	return 0;
7319
7320err_up_complete:
7321	ice_down(vsi);
7322err_set_qs:
7323	ice_vsi_free_irq(vsi);
7324err_setup_rx:
7325	ice_vsi_free_rx_rings(vsi);
7326err_setup_tx:
7327	ice_vsi_free_tx_rings(vsi);
7328
7329	return err;
7330}
7331
7332/**
7333 * ice_vsi_release_all - Delete all VSIs
7334 * @pf: PF from which all VSIs are being removed
7335 */
7336static void ice_vsi_release_all(struct ice_pf *pf)
7337{
7338	int err, i;
7339
7340	if (!pf->vsi)
7341		return;
7342
7343	ice_for_each_vsi(pf, i) {
7344		if (!pf->vsi[i])
7345			continue;
7346
7347		if (pf->vsi[i]->type == ICE_VSI_CHNL)
7348			continue;
7349
7350		err = ice_vsi_release(pf->vsi[i]);
7351		if (err)
7352			dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
7353				i, err, pf->vsi[i]->vsi_num);
7354	}
7355}
7356
7357/**
7358 * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
7359 * @pf: pointer to the PF instance
7360 * @type: VSI type to rebuild
7361 *
7362 * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
7363 */
7364static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
7365{
7366	struct device *dev = ice_pf_to_dev(pf);
7367	int i, err;
7368
7369	ice_for_each_vsi(pf, i) {
7370		struct ice_vsi *vsi = pf->vsi[i];
7371
7372		if (!vsi || vsi->type != type)
7373			continue;
7374
7375		/* rebuild the VSI */
7376		err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
7377		if (err) {
7378			dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
7379				err, vsi->idx, ice_vsi_type_str(type));
7380			return err;
7381		}
7382
7383		/* replay filters for the VSI */
7384		err = ice_replay_vsi(&pf->hw, vsi->idx);
7385		if (err) {
7386			dev_err(dev, "replay VSI failed, error %d, VSI index %d, type %s\n",
7387				err, vsi->idx, ice_vsi_type_str(type));
7388			return err;
7389		}
7390
7391		/* Re-map HW VSI number, using VSI handle that has been
7392		 * previously validated in ice_replay_vsi() call above
7393		 */
7394		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
7395
7396		/* enable the VSI */
7397		err = ice_ena_vsi(vsi, false);
7398		if (err) {
7399			dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
7400				err, vsi->idx, ice_vsi_type_str(type));
7401			return err;
7402		}
7403
7404		dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
7405			 ice_vsi_type_str(type));
7406	}
7407
7408	return 0;
7409}
7410
7411/**
7412 * ice_update_pf_netdev_link - Update PF netdev link status
7413 * @pf: pointer to the PF instance
7414 */
7415static void ice_update_pf_netdev_link(struct ice_pf *pf)
7416{
7417	bool link_up;
7418	int i;
7419
7420	ice_for_each_vsi(pf, i) {
7421		struct ice_vsi *vsi = pf->vsi[i];
7422
7423		if (!vsi || vsi->type != ICE_VSI_PF)
7424			return;
7425
7426		ice_get_link_status(pf->vsi[i]->port_info, &link_up);
7427		if (link_up) {
7428			netif_carrier_on(pf->vsi[i]->netdev);
7429			netif_tx_wake_all_queues(pf->vsi[i]->netdev);
7430		} else {
7431			netif_carrier_off(pf->vsi[i]->netdev);
7432			netif_tx_stop_all_queues(pf->vsi[i]->netdev);
7433		}
7434	}
7435}
7436
7437/**
7438 * ice_rebuild - rebuild after reset
7439 * @pf: PF to rebuild
7440 * @reset_type: type of reset
7441 *
7442 * Do not rebuild VF VSI in this flow because that is already handled via
7443 * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
7444 * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
7445 * to reset/rebuild all the VF VSI twice.
7446 */
7447static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
7448{
7449	struct device *dev = ice_pf_to_dev(pf);
7450	struct ice_hw *hw = &pf->hw;
7451	bool dvm;
7452	int err;
7453
7454	if (test_bit(ICE_DOWN, pf->state))
7455		goto clear_recovery;
7456
7457	dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
7458
7459#define ICE_EMP_RESET_SLEEP_MS 5000
7460	if (reset_type == ICE_RESET_EMPR) {
7461		/* If an EMP reset has occurred, any previously pending flash
7462		 * update will have completed. We no longer know whether or
7463		 * not the NVM update EMP reset is restricted.
7464		 */
7465		pf->fw_emp_reset_disabled = false;
7466
7467		msleep(ICE_EMP_RESET_SLEEP_MS);
7468	}
7469
7470	err = ice_init_all_ctrlq(hw);
7471	if (err) {
7472		dev_err(dev, "control queues init failed %d\n", err);
7473		goto err_init_ctrlq;
7474	}
7475
7476	/* if DDP was previously loaded successfully */
7477	if (!ice_is_safe_mode(pf)) {
7478		/* reload the SW DB of filter tables */
7479		if (reset_type == ICE_RESET_PFR)
7480			ice_fill_blk_tbls(hw);
7481		else
7482			/* Reload DDP Package after CORER/GLOBR reset */
7483			ice_load_pkg(NULL, pf);
7484	}
7485
7486	err = ice_clear_pf_cfg(hw);
7487	if (err) {
7488		dev_err(dev, "clear PF configuration failed %d\n", err);
7489		goto err_init_ctrlq;
7490	}
7491
7492	ice_clear_pxe_mode(hw);
7493
7494	err = ice_init_nvm(hw);
7495	if (err) {
7496		dev_err(dev, "ice_init_nvm failed %d\n", err);
7497		goto err_init_ctrlq;
7498	}
7499
7500	err = ice_get_caps(hw);
7501	if (err) {
7502		dev_err(dev, "ice_get_caps failed %d\n", err);
7503		goto err_init_ctrlq;
7504	}
7505
7506	err = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
7507	if (err) {
7508		dev_err(dev, "set_mac_cfg failed %d\n", err);
7509		goto err_init_ctrlq;
7510	}
7511
7512	dvm = ice_is_dvm_ena(hw);
7513
7514	err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
7515	if (err)
7516		goto err_init_ctrlq;
7517
7518	err = ice_sched_init_port(hw->port_info);
7519	if (err)
7520		goto err_sched_init_port;
7521
7522	/* start misc vector */
7523	err = ice_req_irq_msix_misc(pf);
7524	if (err) {
7525		dev_err(dev, "misc vector setup failed: %d\n", err);
7526		goto err_sched_init_port;
7527	}
7528
7529	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7530		wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
7531		if (!rd32(hw, PFQF_FD_SIZE)) {
7532			u16 unused, guar, b_effort;
7533
7534			guar = hw->func_caps.fd_fltr_guar;
7535			b_effort = hw->func_caps.fd_fltr_best_effort;
7536
7537			/* force guaranteed filter pool for PF */
7538			ice_alloc_fd_guar_item(hw, &unused, guar);
7539			/* force shared filter pool for PF */
7540			ice_alloc_fd_shrd_item(hw, &unused, b_effort);
7541		}
7542	}
7543
7544	if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
7545		ice_dcb_rebuild(pf);
7546
7547	/* If the PF previously had enabled PTP, PTP init needs to happen before
7548	 * the VSI rebuild. If not, this causes the PTP link status events to
7549	 * fail.
7550	 */
7551	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
7552		ice_ptp_reset(pf);
7553
7554	if (ice_is_feature_supported(pf, ICE_F_GNSS))
7555		ice_gnss_init(pf);
7556
7557	/* rebuild PF VSI */
7558	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
7559	if (err) {
7560		dev_err(dev, "PF VSI rebuild failed: %d\n", err);
7561		goto err_vsi_rebuild;
7562	}
7563
7564	err = ice_eswitch_rebuild(pf);
 
 
 
 
7565	if (err) {
7566		dev_err(dev, "Switchdev rebuild failed: %d\n", err);
7567		goto err_vsi_rebuild;
7568	}
7569
7570	if (reset_type == ICE_RESET_PFR) {
7571		err = ice_rebuild_channels(pf);
7572		if (err) {
7573			dev_err(dev, "failed to rebuild and replay ADQ VSIs, err %d\n",
7574				err);
7575			goto err_vsi_rebuild;
7576		}
7577	}
7578
7579	/* If Flow Director is active */
7580	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7581		err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
7582		if (err) {
7583			dev_err(dev, "control VSI rebuild failed: %d\n", err);
7584			goto err_vsi_rebuild;
7585		}
7586
7587		/* replay HW Flow Director recipes */
7588		if (hw->fdir_prof)
7589			ice_fdir_replay_flows(hw);
7590
7591		/* replay Flow Director filters */
7592		ice_fdir_replay_fltrs(pf);
7593
7594		ice_rebuild_arfs(pf);
7595	}
7596
7597	ice_update_pf_netdev_link(pf);
7598
7599	/* tell the firmware we are up */
7600	err = ice_send_version(pf);
7601	if (err) {
7602		dev_err(dev, "Rebuild failed due to error sending driver version: %d\n",
7603			err);
7604		goto err_vsi_rebuild;
7605	}
7606
7607	ice_replay_post(hw);
7608
7609	/* if we get here, reset flow is successful */
7610	clear_bit(ICE_RESET_FAILED, pf->state);
7611
7612	ice_plug_aux_dev(pf);
7613	if (ice_is_feature_supported(pf, ICE_F_SRIOV_LAG))
7614		ice_lag_rebuild(pf);
7615
7616	/* Restore timestamp mode settings after VSI rebuild */
7617	ice_ptp_restore_timestamp_mode(pf);
7618	return;
7619
7620err_vsi_rebuild:
7621err_sched_init_port:
7622	ice_sched_cleanup_all(hw);
7623err_init_ctrlq:
7624	ice_shutdown_all_ctrlq(hw);
7625	set_bit(ICE_RESET_FAILED, pf->state);
7626clear_recovery:
7627	/* set this bit in PF state to control service task scheduling */
7628	set_bit(ICE_NEEDS_RESTART, pf->state);
7629	dev_err(dev, "Rebuild failed, unload and reload driver\n");
7630}
7631
7632/**
 
 
 
 
 
 
 
 
 
 
 
 
7633 * ice_change_mtu - NDO callback to change the MTU
7634 * @netdev: network interface device structure
7635 * @new_mtu: new value for maximum frame size
7636 *
7637 * Returns 0 on success, negative on failure
7638 */
7639static int ice_change_mtu(struct net_device *netdev, int new_mtu)
7640{
7641	struct ice_netdev_priv *np = netdev_priv(netdev);
7642	struct ice_vsi *vsi = np->vsi;
7643	struct ice_pf *pf = vsi->back;
7644	struct bpf_prog *prog;
7645	u8 count = 0;
7646	int err = 0;
7647
7648	if (new_mtu == (int)netdev->mtu) {
7649		netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
7650		return 0;
7651	}
7652
7653	prog = vsi->xdp_prog;
7654	if (prog && !prog->aux->xdp_has_frags) {
7655		int frame_size = ice_max_xdp_frame_size(vsi);
7656
7657		if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
7658			netdev_err(netdev, "max MTU for XDP usage is %d\n",
7659				   frame_size - ICE_ETH_PKT_HDR_PAD);
7660			return -EINVAL;
7661		}
7662	} else if (test_bit(ICE_FLAG_LEGACY_RX, pf->flags)) {
7663		if (new_mtu + ICE_ETH_PKT_HDR_PAD > ICE_MAX_FRAME_LEGACY_RX) {
7664			netdev_err(netdev, "Too big MTU for legacy-rx; Max is %d\n",
7665				   ICE_MAX_FRAME_LEGACY_RX - ICE_ETH_PKT_HDR_PAD);
7666			return -EINVAL;
7667		}
7668	}
7669
7670	/* if a reset is in progress, wait for some time for it to complete */
7671	do {
7672		if (ice_is_reset_in_progress(pf->state)) {
7673			count++;
7674			usleep_range(1000, 2000);
7675		} else {
7676			break;
7677		}
7678
7679	} while (count < 100);
7680
7681	if (count == 100) {
7682		netdev_err(netdev, "can't change MTU. Device is busy\n");
7683		return -EBUSY;
7684	}
7685
7686	netdev->mtu = (unsigned int)new_mtu;
7687	err = ice_down_up(vsi);
7688	if (err)
7689		return err;
 
 
 
 
 
 
 
 
 
 
 
 
7690
7691	netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
7692	set_bit(ICE_FLAG_MTU_CHANGED, pf->flags);
7693
7694	return err;
7695}
7696
7697/**
7698 * ice_eth_ioctl - Access the hwtstamp interface
7699 * @netdev: network interface device structure
7700 * @ifr: interface request data
7701 * @cmd: ioctl command
7702 */
7703static int ice_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
7704{
7705	struct ice_netdev_priv *np = netdev_priv(netdev);
7706	struct ice_pf *pf = np->vsi->back;
7707
7708	switch (cmd) {
7709	case SIOCGHWTSTAMP:
7710		return ice_ptp_get_ts_config(pf, ifr);
7711	case SIOCSHWTSTAMP:
7712		return ice_ptp_set_ts_config(pf, ifr);
7713	default:
7714		return -EOPNOTSUPP;
7715	}
7716}
7717
7718/**
7719 * ice_aq_str - convert AQ err code to a string
7720 * @aq_err: the AQ error code to convert
7721 */
7722const char *ice_aq_str(enum ice_aq_err aq_err)
7723{
7724	switch (aq_err) {
7725	case ICE_AQ_RC_OK:
7726		return "OK";
7727	case ICE_AQ_RC_EPERM:
7728		return "ICE_AQ_RC_EPERM";
7729	case ICE_AQ_RC_ENOENT:
7730		return "ICE_AQ_RC_ENOENT";
7731	case ICE_AQ_RC_ENOMEM:
7732		return "ICE_AQ_RC_ENOMEM";
7733	case ICE_AQ_RC_EBUSY:
7734		return "ICE_AQ_RC_EBUSY";
7735	case ICE_AQ_RC_EEXIST:
7736		return "ICE_AQ_RC_EEXIST";
7737	case ICE_AQ_RC_EINVAL:
7738		return "ICE_AQ_RC_EINVAL";
7739	case ICE_AQ_RC_ENOSPC:
7740		return "ICE_AQ_RC_ENOSPC";
7741	case ICE_AQ_RC_ENOSYS:
7742		return "ICE_AQ_RC_ENOSYS";
7743	case ICE_AQ_RC_EMODE:
7744		return "ICE_AQ_RC_EMODE";
7745	case ICE_AQ_RC_ENOSEC:
7746		return "ICE_AQ_RC_ENOSEC";
7747	case ICE_AQ_RC_EBADSIG:
7748		return "ICE_AQ_RC_EBADSIG";
7749	case ICE_AQ_RC_ESVN:
7750		return "ICE_AQ_RC_ESVN";
7751	case ICE_AQ_RC_EBADMAN:
7752		return "ICE_AQ_RC_EBADMAN";
7753	case ICE_AQ_RC_EBADBUF:
7754		return "ICE_AQ_RC_EBADBUF";
7755	}
7756
7757	return "ICE_AQ_RC_UNKNOWN";
7758}
7759
7760/**
7761 * ice_set_rss_lut - Set RSS LUT
7762 * @vsi: Pointer to VSI structure
7763 * @lut: Lookup table
7764 * @lut_size: Lookup table size
7765 *
7766 * Returns 0 on success, negative on failure
7767 */
7768int ice_set_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7769{
7770	struct ice_aq_get_set_rss_lut_params params = {};
7771	struct ice_hw *hw = &vsi->back->hw;
7772	int status;
7773
7774	if (!lut)
7775		return -EINVAL;
7776
7777	params.vsi_handle = vsi->idx;
7778	params.lut_size = lut_size;
7779	params.lut_type = vsi->rss_lut_type;
7780	params.lut = lut;
7781
7782	status = ice_aq_set_rss_lut(hw, &params);
7783	if (status)
7784		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS lut, err %d aq_err %s\n",
7785			status, ice_aq_str(hw->adminq.sq_last_status));
7786
7787	return status;
7788}
7789
7790/**
7791 * ice_set_rss_key - Set RSS key
7792 * @vsi: Pointer to the VSI structure
7793 * @seed: RSS hash seed
7794 *
7795 * Returns 0 on success, negative on failure
7796 */
7797int ice_set_rss_key(struct ice_vsi *vsi, u8 *seed)
7798{
7799	struct ice_hw *hw = &vsi->back->hw;
7800	int status;
7801
7802	if (!seed)
7803		return -EINVAL;
7804
7805	status = ice_aq_set_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7806	if (status)
7807		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS key, err %d aq_err %s\n",
7808			status, ice_aq_str(hw->adminq.sq_last_status));
7809
7810	return status;
7811}
7812
7813/**
7814 * ice_get_rss_lut - Get RSS LUT
7815 * @vsi: Pointer to VSI structure
7816 * @lut: Buffer to store the lookup table entries
7817 * @lut_size: Size of buffer to store the lookup table entries
7818 *
7819 * Returns 0 on success, negative on failure
7820 */
7821int ice_get_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7822{
7823	struct ice_aq_get_set_rss_lut_params params = {};
7824	struct ice_hw *hw = &vsi->back->hw;
7825	int status;
7826
7827	if (!lut)
7828		return -EINVAL;
7829
7830	params.vsi_handle = vsi->idx;
7831	params.lut_size = lut_size;
7832	params.lut_type = vsi->rss_lut_type;
7833	params.lut = lut;
7834
7835	status = ice_aq_get_rss_lut(hw, &params);
7836	if (status)
7837		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS lut, err %d aq_err %s\n",
7838			status, ice_aq_str(hw->adminq.sq_last_status));
7839
7840	return status;
7841}
7842
7843/**
7844 * ice_get_rss_key - Get RSS key
7845 * @vsi: Pointer to VSI structure
7846 * @seed: Buffer to store the key in
7847 *
7848 * Returns 0 on success, negative on failure
7849 */
7850int ice_get_rss_key(struct ice_vsi *vsi, u8 *seed)
7851{
7852	struct ice_hw *hw = &vsi->back->hw;
7853	int status;
7854
7855	if (!seed)
7856		return -EINVAL;
7857
7858	status = ice_aq_get_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7859	if (status)
7860		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS key, err %d aq_err %s\n",
7861			status, ice_aq_str(hw->adminq.sq_last_status));
7862
7863	return status;
7864}
7865
7866/**
7867 * ice_set_rss_hfunc - Set RSS HASH function
7868 * @vsi: Pointer to VSI structure
7869 * @hfunc: hash function (ICE_AQ_VSI_Q_OPT_RSS_*)
7870 *
7871 * Returns 0 on success, negative on failure
7872 */
7873int ice_set_rss_hfunc(struct ice_vsi *vsi, u8 hfunc)
7874{
7875	struct ice_hw *hw = &vsi->back->hw;
7876	struct ice_vsi_ctx *ctx;
7877	bool symm;
7878	int err;
7879
7880	if (hfunc == vsi->rss_hfunc)
7881		return 0;
7882
7883	if (hfunc != ICE_AQ_VSI_Q_OPT_RSS_HASH_TPLZ &&
7884	    hfunc != ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ)
7885		return -EOPNOTSUPP;
7886
7887	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
7888	if (!ctx)
7889		return -ENOMEM;
7890
7891	ctx->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_Q_OPT_VALID);
7892	ctx->info.q_opt_rss = vsi->info.q_opt_rss;
7893	ctx->info.q_opt_rss &= ~ICE_AQ_VSI_Q_OPT_RSS_HASH_M;
7894	ctx->info.q_opt_rss |=
7895		FIELD_PREP(ICE_AQ_VSI_Q_OPT_RSS_HASH_M, hfunc);
7896	ctx->info.q_opt_tc = vsi->info.q_opt_tc;
7897	ctx->info.q_opt_flags = vsi->info.q_opt_rss;
7898
7899	err = ice_update_vsi(hw, vsi->idx, ctx, NULL);
7900	if (err) {
7901		dev_err(ice_pf_to_dev(vsi->back), "Failed to configure RSS hash for VSI %d, error %d\n",
7902			vsi->vsi_num, err);
7903	} else {
7904		vsi->info.q_opt_rss = ctx->info.q_opt_rss;
7905		vsi->rss_hfunc = hfunc;
7906		netdev_info(vsi->netdev, "Hash function set to: %sToeplitz\n",
7907			    hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ ?
7908			    "Symmetric " : "");
7909	}
7910	kfree(ctx);
7911	if (err)
7912		return err;
7913
7914	/* Fix the symmetry setting for all existing RSS configurations */
7915	symm = !!(hfunc == ICE_AQ_VSI_Q_OPT_RSS_HASH_SYM_TPLZ);
7916	return ice_set_rss_cfg_symm(hw, vsi, symm);
7917}
7918
7919/**
7920 * ice_bridge_getlink - Get the hardware bridge mode
7921 * @skb: skb buff
7922 * @pid: process ID
7923 * @seq: RTNL message seq
7924 * @dev: the netdev being configured
7925 * @filter_mask: filter mask passed in
7926 * @nlflags: netlink flags passed in
7927 *
7928 * Return the bridge mode (VEB/VEPA)
7929 */
7930static int
7931ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
7932		   struct net_device *dev, u32 filter_mask, int nlflags)
7933{
7934	struct ice_netdev_priv *np = netdev_priv(dev);
7935	struct ice_vsi *vsi = np->vsi;
7936	struct ice_pf *pf = vsi->back;
7937	u16 bmode;
7938
7939	bmode = pf->first_sw->bridge_mode;
7940
7941	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
7942				       filter_mask, NULL);
7943}
7944
7945/**
7946 * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
7947 * @vsi: Pointer to VSI structure
7948 * @bmode: Hardware bridge mode (VEB/VEPA)
7949 *
7950 * Returns 0 on success, negative on failure
7951 */
7952static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
7953{
7954	struct ice_aqc_vsi_props *vsi_props;
7955	struct ice_hw *hw = &vsi->back->hw;
7956	struct ice_vsi_ctx *ctxt;
7957	int ret;
7958
7959	vsi_props = &vsi->info;
7960
7961	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
7962	if (!ctxt)
7963		return -ENOMEM;
7964
7965	ctxt->info = vsi->info;
7966
7967	if (bmode == BRIDGE_MODE_VEB)
7968		/* change from VEPA to VEB mode */
7969		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7970	else
7971		/* change from VEB to VEPA mode */
7972		ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7973	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
7974
7975	ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
7976	if (ret) {
7977		dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %d aq_err %s\n",
7978			bmode, ret, ice_aq_str(hw->adminq.sq_last_status));
7979		goto out;
7980	}
7981	/* Update sw flags for book keeping */
7982	vsi_props->sw_flags = ctxt->info.sw_flags;
7983
7984out:
7985	kfree(ctxt);
7986	return ret;
7987}
7988
7989/**
7990 * ice_bridge_setlink - Set the hardware bridge mode
7991 * @dev: the netdev being configured
7992 * @nlh: RTNL message
7993 * @flags: bridge setlink flags
7994 * @extack: netlink extended ack
7995 *
7996 * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
7997 * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
7998 * not already set for all VSIs connected to this switch. And also update the
7999 * unicast switch filter rules for the corresponding switch of the netdev.
8000 */
8001static int
8002ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
8003		   u16 __always_unused flags,
8004		   struct netlink_ext_ack __always_unused *extack)
8005{
8006	struct ice_netdev_priv *np = netdev_priv(dev);
8007	struct ice_pf *pf = np->vsi->back;
8008	struct nlattr *attr, *br_spec;
8009	struct ice_hw *hw = &pf->hw;
8010	struct ice_sw *pf_sw;
8011	int rem, v, err = 0;
8012
8013	pf_sw = pf->first_sw;
8014	/* find the attribute in the netlink message */
8015	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
8016	if (!br_spec)
8017		return -EINVAL;
8018
8019	nla_for_each_nested(attr, br_spec, rem) {
8020		__u16 mode;
8021
8022		if (nla_type(attr) != IFLA_BRIDGE_MODE)
8023			continue;
8024		mode = nla_get_u16(attr);
8025		if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
8026			return -EINVAL;
8027		/* Continue  if bridge mode is not being flipped */
8028		if (mode == pf_sw->bridge_mode)
8029			continue;
8030		/* Iterates through the PF VSI list and update the loopback
8031		 * mode of the VSI
8032		 */
8033		ice_for_each_vsi(pf, v) {
8034			if (!pf->vsi[v])
8035				continue;
8036			err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
8037			if (err)
8038				return err;
8039		}
8040
8041		hw->evb_veb = (mode == BRIDGE_MODE_VEB);
8042		/* Update the unicast switch filter rules for the corresponding
8043		 * switch of the netdev
8044		 */
8045		err = ice_update_sw_rule_bridge_mode(hw);
8046		if (err) {
8047			netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %s\n",
8048				   mode, err,
8049				   ice_aq_str(hw->adminq.sq_last_status));
8050			/* revert hw->evb_veb */
8051			hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
8052			return err;
8053		}
8054
8055		pf_sw->bridge_mode = mode;
8056	}
8057
8058	return 0;
8059}
8060
8061/**
8062 * ice_tx_timeout - Respond to a Tx Hang
8063 * @netdev: network interface device structure
8064 * @txqueue: Tx queue
8065 */
8066static void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
8067{
8068	struct ice_netdev_priv *np = netdev_priv(netdev);
8069	struct ice_tx_ring *tx_ring = NULL;
8070	struct ice_vsi *vsi = np->vsi;
8071	struct ice_pf *pf = vsi->back;
8072	u32 i;
8073
8074	pf->tx_timeout_count++;
8075
8076	/* Check if PFC is enabled for the TC to which the queue belongs
8077	 * to. If yes then Tx timeout is not caused by a hung queue, no
8078	 * need to reset and rebuild
8079	 */
8080	if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
8081		dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
8082			 txqueue);
8083		return;
8084	}
8085
8086	/* now that we have an index, find the tx_ring struct */
8087	ice_for_each_txq(vsi, i)
8088		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
8089			if (txqueue == vsi->tx_rings[i]->q_index) {
8090				tx_ring = vsi->tx_rings[i];
8091				break;
8092			}
8093
8094	/* Reset recovery level if enough time has elapsed after last timeout.
8095	 * Also ensure no new reset action happens before next timeout period.
8096	 */
8097	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
8098		pf->tx_timeout_recovery_level = 1;
8099	else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
8100				       netdev->watchdog_timeo)))
8101		return;
8102
8103	if (tx_ring) {
8104		struct ice_hw *hw = &pf->hw;
8105		u32 head, val = 0;
8106
8107		head = FIELD_GET(QTX_COMM_HEAD_HEAD_M,
8108				 rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])));
8109		/* Read interrupt register */
8110		val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
8111
8112		netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
8113			    vsi->vsi_num, txqueue, tx_ring->next_to_clean,
8114			    head, tx_ring->next_to_use, val);
8115	}
8116
8117	pf->tx_timeout_last_recovery = jiffies;
8118	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
8119		    pf->tx_timeout_recovery_level, txqueue);
8120
8121	switch (pf->tx_timeout_recovery_level) {
8122	case 1:
8123		set_bit(ICE_PFR_REQ, pf->state);
8124		break;
8125	case 2:
8126		set_bit(ICE_CORER_REQ, pf->state);
8127		break;
8128	case 3:
8129		set_bit(ICE_GLOBR_REQ, pf->state);
8130		break;
8131	default:
8132		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
8133		set_bit(ICE_DOWN, pf->state);
8134		set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
8135		set_bit(ICE_SERVICE_DIS, pf->state);
8136		break;
8137	}
8138
8139	ice_service_task_schedule(pf);
8140	pf->tx_timeout_recovery_level++;
8141}
8142
8143/**
8144 * ice_setup_tc_cls_flower - flower classifier offloads
8145 * @np: net device to configure
8146 * @filter_dev: device on which filter is added
8147 * @cls_flower: offload data
8148 */
8149static int
8150ice_setup_tc_cls_flower(struct ice_netdev_priv *np,
8151			struct net_device *filter_dev,
8152			struct flow_cls_offload *cls_flower)
8153{
8154	struct ice_vsi *vsi = np->vsi;
8155
8156	if (cls_flower->common.chain_index)
8157		return -EOPNOTSUPP;
8158
8159	switch (cls_flower->command) {
8160	case FLOW_CLS_REPLACE:
8161		return ice_add_cls_flower(filter_dev, vsi, cls_flower);
8162	case FLOW_CLS_DESTROY:
8163		return ice_del_cls_flower(vsi, cls_flower);
8164	default:
8165		return -EINVAL;
8166	}
8167}
8168
8169/**
8170 * ice_setup_tc_block_cb - callback handler registered for TC block
8171 * @type: TC SETUP type
8172 * @type_data: TC flower offload data that contains user input
8173 * @cb_priv: netdev private data
8174 */
8175static int
8176ice_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
8177{
8178	struct ice_netdev_priv *np = cb_priv;
8179
8180	switch (type) {
8181	case TC_SETUP_CLSFLOWER:
8182		return ice_setup_tc_cls_flower(np, np->vsi->netdev,
8183					       type_data);
8184	default:
8185		return -EOPNOTSUPP;
8186	}
8187}
8188
8189/**
8190 * ice_validate_mqprio_qopt - Validate TCF input parameters
8191 * @vsi: Pointer to VSI
8192 * @mqprio_qopt: input parameters for mqprio queue configuration
8193 *
8194 * This function validates MQPRIO params, such as qcount (power of 2 wherever
8195 * needed), and make sure user doesn't specify qcount and BW rate limit
8196 * for TCs, which are more than "num_tc"
8197 */
8198static int
8199ice_validate_mqprio_qopt(struct ice_vsi *vsi,
8200			 struct tc_mqprio_qopt_offload *mqprio_qopt)
8201{
 
8202	int non_power_of_2_qcount = 0;
8203	struct ice_pf *pf = vsi->back;
8204	int max_rss_q_cnt = 0;
8205	u64 sum_min_rate = 0;
8206	struct device *dev;
8207	int i, speed;
8208	u8 num_tc;
8209
8210	if (vsi->type != ICE_VSI_PF)
8211		return -EINVAL;
8212
8213	if (mqprio_qopt->qopt.offset[0] != 0 ||
8214	    mqprio_qopt->qopt.num_tc < 1 ||
8215	    mqprio_qopt->qopt.num_tc > ICE_CHNL_MAX_TC)
8216		return -EINVAL;
8217
8218	dev = ice_pf_to_dev(pf);
8219	vsi->ch_rss_size = 0;
8220	num_tc = mqprio_qopt->qopt.num_tc;
8221	speed = ice_get_link_speed_kbps(vsi);
8222
8223	for (i = 0; num_tc; i++) {
8224		int qcount = mqprio_qopt->qopt.count[i];
8225		u64 max_rate, min_rate, rem;
8226
8227		if (!qcount)
8228			return -EINVAL;
8229
8230		if (is_power_of_2(qcount)) {
8231			if (non_power_of_2_qcount &&
8232			    qcount > non_power_of_2_qcount) {
8233				dev_err(dev, "qcount[%d] cannot be greater than non power of 2 qcount[%d]\n",
8234					qcount, non_power_of_2_qcount);
8235				return -EINVAL;
8236			}
8237			if (qcount > max_rss_q_cnt)
8238				max_rss_q_cnt = qcount;
8239		} else {
8240			if (non_power_of_2_qcount &&
8241			    qcount != non_power_of_2_qcount) {
8242				dev_err(dev, "Only one non power of 2 qcount allowed[%d,%d]\n",
8243					qcount, non_power_of_2_qcount);
8244				return -EINVAL;
8245			}
8246			if (qcount < max_rss_q_cnt) {
8247				dev_err(dev, "non power of 2 qcount[%d] cannot be less than other qcount[%d]\n",
8248					qcount, max_rss_q_cnt);
8249				return -EINVAL;
8250			}
8251			max_rss_q_cnt = qcount;
8252			non_power_of_2_qcount = qcount;
8253		}
8254
8255		/* TC command takes input in K/N/Gbps or K/M/Gbit etc but
8256		 * converts the bandwidth rate limit into Bytes/s when
8257		 * passing it down to the driver. So convert input bandwidth
8258		 * from Bytes/s to Kbps
8259		 */
8260		max_rate = mqprio_qopt->max_rate[i];
8261		max_rate = div_u64(max_rate, ICE_BW_KBPS_DIVISOR);
 
8262
8263		/* min_rate is minimum guaranteed rate and it can't be zero */
8264		min_rate = mqprio_qopt->min_rate[i];
8265		min_rate = div_u64(min_rate, ICE_BW_KBPS_DIVISOR);
8266		sum_min_rate += min_rate;
8267
8268		if (min_rate && min_rate < ICE_MIN_BW_LIMIT) {
8269			dev_err(dev, "TC%d: min_rate(%llu Kbps) < %u Kbps\n", i,
8270				min_rate, ICE_MIN_BW_LIMIT);
8271			return -EINVAL;
8272		}
8273
8274		if (max_rate && max_rate > speed) {
8275			dev_err(dev, "TC%d: max_rate(%llu Kbps) > link speed of %u Kbps\n",
8276				i, max_rate, speed);
8277			return -EINVAL;
8278		}
8279
8280		iter_div_u64_rem(min_rate, ICE_MIN_BW_LIMIT, &rem);
8281		if (rem) {
8282			dev_err(dev, "TC%d: Min Rate not multiple of %u Kbps",
8283				i, ICE_MIN_BW_LIMIT);
8284			return -EINVAL;
8285		}
8286
8287		iter_div_u64_rem(max_rate, ICE_MIN_BW_LIMIT, &rem);
8288		if (rem) {
8289			dev_err(dev, "TC%d: Max Rate not multiple of %u Kbps",
8290				i, ICE_MIN_BW_LIMIT);
8291			return -EINVAL;
8292		}
8293
8294		/* min_rate can't be more than max_rate, except when max_rate
8295		 * is zero (implies max_rate sought is max line rate). In such
8296		 * a case min_rate can be more than max.
8297		 */
8298		if (max_rate && min_rate > max_rate) {
8299			dev_err(dev, "min_rate %llu Kbps can't be more than max_rate %llu Kbps\n",
8300				min_rate, max_rate);
8301			return -EINVAL;
8302		}
8303
8304		if (i >= mqprio_qopt->qopt.num_tc - 1)
8305			break;
8306		if (mqprio_qopt->qopt.offset[i + 1] !=
8307		    (mqprio_qopt->qopt.offset[i] + qcount))
8308			return -EINVAL;
8309	}
8310	if (vsi->num_rxq <
8311	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8312		return -EINVAL;
8313	if (vsi->num_txq <
8314	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
8315		return -EINVAL;
8316
 
 
 
 
 
 
8317	if (sum_min_rate && sum_min_rate > (u64)speed) {
8318		dev_err(dev, "Invalid min Tx rate(%llu) Kbps > speed (%u) Kbps specified\n",
8319			sum_min_rate, speed);
8320		return -EINVAL;
8321	}
8322
8323	/* make sure vsi->ch_rss_size is set correctly based on TC's qcount */
8324	vsi->ch_rss_size = max_rss_q_cnt;
8325
8326	return 0;
8327}
8328
8329/**
8330 * ice_add_vsi_to_fdir - add a VSI to the flow director group for PF
8331 * @pf: ptr to PF device
8332 * @vsi: ptr to VSI
8333 */
8334static int ice_add_vsi_to_fdir(struct ice_pf *pf, struct ice_vsi *vsi)
8335{
8336	struct device *dev = ice_pf_to_dev(pf);
8337	bool added = false;
8338	struct ice_hw *hw;
8339	int flow;
8340
8341	if (!(vsi->num_gfltr || vsi->num_bfltr))
8342		return -EINVAL;
8343
8344	hw = &pf->hw;
8345	for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++) {
8346		struct ice_fd_hw_prof *prof;
8347		int tun, status;
8348		u64 entry_h;
8349
8350		if (!(hw->fdir_prof && hw->fdir_prof[flow] &&
8351		      hw->fdir_prof[flow]->cnt))
8352			continue;
8353
8354		for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
8355			enum ice_flow_priority prio;
 
8356
8357			/* add this VSI to FDir profile for this flow */
8358			prio = ICE_FLOW_PRIO_NORMAL;
8359			prof = hw->fdir_prof[flow];
8360			status = ice_flow_add_entry(hw, ICE_BLK_FD,
8361						    prof->prof_id[tun],
8362						    prof->vsi_h[0], vsi->idx,
8363						    prio, prof->fdir_seg[tun],
8364						    &entry_h);
8365			if (status) {
8366				dev_err(dev, "channel VSI idx %d, not able to add to group %d\n",
8367					vsi->idx, flow);
8368				continue;
8369			}
8370
8371			prof->entry_h[prof->cnt][tun] = entry_h;
8372		}
8373
8374		/* store VSI for filter replay and delete */
8375		prof->vsi_h[prof->cnt] = vsi->idx;
8376		prof->cnt++;
8377
8378		added = true;
8379		dev_dbg(dev, "VSI idx %d added to fdir group %d\n", vsi->idx,
8380			flow);
8381	}
8382
8383	if (!added)
8384		dev_dbg(dev, "VSI idx %d not added to fdir groups\n", vsi->idx);
8385
8386	return 0;
8387}
8388
8389/**
8390 * ice_add_channel - add a channel by adding VSI
8391 * @pf: ptr to PF device
8392 * @sw_id: underlying HW switching element ID
8393 * @ch: ptr to channel structure
8394 *
8395 * Add a channel (VSI) using add_vsi and queue_map
8396 */
8397static int ice_add_channel(struct ice_pf *pf, u16 sw_id, struct ice_channel *ch)
8398{
8399	struct device *dev = ice_pf_to_dev(pf);
8400	struct ice_vsi *vsi;
8401
8402	if (ch->type != ICE_VSI_CHNL) {
8403		dev_err(dev, "add new VSI failed, ch->type %d\n", ch->type);
8404		return -EINVAL;
8405	}
8406
8407	vsi = ice_chnl_vsi_setup(pf, pf->hw.port_info, ch);
8408	if (!vsi || vsi->type != ICE_VSI_CHNL) {
8409		dev_err(dev, "create chnl VSI failure\n");
8410		return -EINVAL;
8411	}
8412
8413	ice_add_vsi_to_fdir(pf, vsi);
8414
8415	ch->sw_id = sw_id;
8416	ch->vsi_num = vsi->vsi_num;
8417	ch->info.mapping_flags = vsi->info.mapping_flags;
8418	ch->ch_vsi = vsi;
8419	/* set the back pointer of channel for newly created VSI */
8420	vsi->ch = ch;
8421
8422	memcpy(&ch->info.q_mapping, &vsi->info.q_mapping,
8423	       sizeof(vsi->info.q_mapping));
8424	memcpy(&ch->info.tc_mapping, vsi->info.tc_mapping,
8425	       sizeof(vsi->info.tc_mapping));
8426
8427	return 0;
8428}
8429
8430/**
8431 * ice_chnl_cfg_res
8432 * @vsi: the VSI being setup
8433 * @ch: ptr to channel structure
8434 *
8435 * Configure channel specific resources such as rings, vector.
8436 */
8437static void ice_chnl_cfg_res(struct ice_vsi *vsi, struct ice_channel *ch)
8438{
8439	int i;
8440
8441	for (i = 0; i < ch->num_txq; i++) {
8442		struct ice_q_vector *tx_q_vector, *rx_q_vector;
8443		struct ice_ring_container *rc;
8444		struct ice_tx_ring *tx_ring;
8445		struct ice_rx_ring *rx_ring;
8446
8447		tx_ring = vsi->tx_rings[ch->base_q + i];
8448		rx_ring = vsi->rx_rings[ch->base_q + i];
8449		if (!tx_ring || !rx_ring)
8450			continue;
8451
8452		/* setup ring being channel enabled */
8453		tx_ring->ch = ch;
8454		rx_ring->ch = ch;
8455
8456		/* following code block sets up vector specific attributes */
8457		tx_q_vector = tx_ring->q_vector;
8458		rx_q_vector = rx_ring->q_vector;
8459		if (!tx_q_vector && !rx_q_vector)
8460			continue;
8461
8462		if (tx_q_vector) {
8463			tx_q_vector->ch = ch;
8464			/* setup Tx and Rx ITR setting if DIM is off */
8465			rc = &tx_q_vector->tx;
8466			if (!ITR_IS_DYNAMIC(rc))
8467				ice_write_itr(rc, rc->itr_setting);
8468		}
8469		if (rx_q_vector) {
8470			rx_q_vector->ch = ch;
8471			/* setup Tx and Rx ITR setting if DIM is off */
8472			rc = &rx_q_vector->rx;
8473			if (!ITR_IS_DYNAMIC(rc))
8474				ice_write_itr(rc, rc->itr_setting);
8475		}
8476	}
8477
8478	/* it is safe to assume that, if channel has non-zero num_t[r]xq, then
8479	 * GLINT_ITR register would have written to perform in-context
8480	 * update, hence perform flush
8481	 */
8482	if (ch->num_txq || ch->num_rxq)
8483		ice_flush(&vsi->back->hw);
8484}
8485
8486/**
8487 * ice_cfg_chnl_all_res - configure channel resources
8488 * @vsi: pte to main_vsi
8489 * @ch: ptr to channel structure
8490 *
8491 * This function configures channel specific resources such as flow-director
8492 * counter index, and other resources such as queues, vectors, ITR settings
8493 */
8494static void
8495ice_cfg_chnl_all_res(struct ice_vsi *vsi, struct ice_channel *ch)
8496{
8497	/* configure channel (aka ADQ) resources such as queues, vectors,
8498	 * ITR settings for channel specific vectors and anything else
8499	 */
8500	ice_chnl_cfg_res(vsi, ch);
8501}
8502
8503/**
8504 * ice_setup_hw_channel - setup new channel
8505 * @pf: ptr to PF device
8506 * @vsi: the VSI being setup
8507 * @ch: ptr to channel structure
8508 * @sw_id: underlying HW switching element ID
8509 * @type: type of channel to be created (VMDq2/VF)
8510 *
8511 * Setup new channel (VSI) based on specified type (VMDq2/VF)
8512 * and configures Tx rings accordingly
8513 */
8514static int
8515ice_setup_hw_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8516		     struct ice_channel *ch, u16 sw_id, u8 type)
8517{
8518	struct device *dev = ice_pf_to_dev(pf);
8519	int ret;
8520
8521	ch->base_q = vsi->next_base_q;
8522	ch->type = type;
8523
8524	ret = ice_add_channel(pf, sw_id, ch);
8525	if (ret) {
8526		dev_err(dev, "failed to add_channel using sw_id %u\n", sw_id);
8527		return ret;
8528	}
8529
8530	/* configure/setup ADQ specific resources */
8531	ice_cfg_chnl_all_res(vsi, ch);
8532
8533	/* make sure to update the next_base_q so that subsequent channel's
8534	 * (aka ADQ) VSI queue map is correct
8535	 */
8536	vsi->next_base_q = vsi->next_base_q + ch->num_rxq;
8537	dev_dbg(dev, "added channel: vsi_num %u, num_rxq %u\n", ch->vsi_num,
8538		ch->num_rxq);
8539
8540	return 0;
8541}
8542
8543/**
8544 * ice_setup_channel - setup new channel using uplink element
8545 * @pf: ptr to PF device
8546 * @vsi: the VSI being setup
8547 * @ch: ptr to channel structure
8548 *
8549 * Setup new channel (VSI) based on specified type (VMDq2/VF)
8550 * and uplink switching element
8551 */
8552static bool
8553ice_setup_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8554		  struct ice_channel *ch)
8555{
8556	struct device *dev = ice_pf_to_dev(pf);
8557	u16 sw_id;
8558	int ret;
8559
8560	if (vsi->type != ICE_VSI_PF) {
8561		dev_err(dev, "unsupported parent VSI type(%d)\n", vsi->type);
8562		return false;
8563	}
8564
8565	sw_id = pf->first_sw->sw_id;
8566
8567	/* create channel (VSI) */
8568	ret = ice_setup_hw_channel(pf, vsi, ch, sw_id, ICE_VSI_CHNL);
8569	if (ret) {
8570		dev_err(dev, "failed to setup hw_channel\n");
8571		return false;
8572	}
8573	dev_dbg(dev, "successfully created channel()\n");
8574
8575	return ch->ch_vsi ? true : false;
8576}
8577
8578/**
8579 * ice_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
8580 * @vsi: VSI to be configured
8581 * @max_tx_rate: max Tx rate in Kbps to be configured as maximum BW limit
8582 * @min_tx_rate: min Tx rate in Kbps to be configured as minimum BW limit
8583 */
8584static int
8585ice_set_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate, u64 min_tx_rate)
8586{
8587	int err;
8588
8589	err = ice_set_min_bw_limit(vsi, min_tx_rate);
8590	if (err)
8591		return err;
8592
8593	return ice_set_max_bw_limit(vsi, max_tx_rate);
8594}
8595
8596/**
8597 * ice_create_q_channel - function to create channel
8598 * @vsi: VSI to be configured
8599 * @ch: ptr to channel (it contains channel specific params)
8600 *
8601 * This function creates channel (VSI) using num_queues specified by user,
8602 * reconfigs RSS if needed.
8603 */
8604static int ice_create_q_channel(struct ice_vsi *vsi, struct ice_channel *ch)
8605{
8606	struct ice_pf *pf = vsi->back;
8607	struct device *dev;
8608
8609	if (!ch)
8610		return -EINVAL;
8611
8612	dev = ice_pf_to_dev(pf);
8613	if (!ch->num_txq || !ch->num_rxq) {
8614		dev_err(dev, "Invalid num_queues requested: %d\n", ch->num_rxq);
8615		return -EINVAL;
8616	}
8617
8618	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_txq) {
8619		dev_err(dev, "cnt_q_avail (%u) less than num_queues %d\n",
8620			vsi->cnt_q_avail, ch->num_txq);
8621		return -EINVAL;
8622	}
8623
8624	if (!ice_setup_channel(pf, vsi, ch)) {
8625		dev_info(dev, "Failed to setup channel\n");
8626		return -EINVAL;
8627	}
8628	/* configure BW rate limit */
8629	if (ch->ch_vsi && (ch->max_tx_rate || ch->min_tx_rate)) {
8630		int ret;
8631
8632		ret = ice_set_bw_limit(ch->ch_vsi, ch->max_tx_rate,
8633				       ch->min_tx_rate);
8634		if (ret)
8635			dev_err(dev, "failed to set Tx rate of %llu Kbps for VSI(%u)\n",
8636				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8637		else
8638			dev_dbg(dev, "set Tx rate of %llu Kbps for VSI(%u)\n",
8639				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8640	}
8641
8642	vsi->cnt_q_avail -= ch->num_txq;
8643
8644	return 0;
8645}
8646
8647/**
8648 * ice_rem_all_chnl_fltrs - removes all channel filters
8649 * @pf: ptr to PF, TC-flower based filter are tracked at PF level
8650 *
8651 * Remove all advanced switch filters only if they are channel specific
8652 * tc-flower based filter
8653 */
8654static void ice_rem_all_chnl_fltrs(struct ice_pf *pf)
8655{
8656	struct ice_tc_flower_fltr *fltr;
8657	struct hlist_node *node;
8658
8659	/* to remove all channel filters, iterate an ordered list of filters */
8660	hlist_for_each_entry_safe(fltr, node,
8661				  &pf->tc_flower_fltr_list,
8662				  tc_flower_node) {
8663		struct ice_rule_query_data rule;
8664		int status;
8665
8666		/* for now process only channel specific filters */
8667		if (!ice_is_chnl_fltr(fltr))
8668			continue;
8669
8670		rule.rid = fltr->rid;
8671		rule.rule_id = fltr->rule_id;
8672		rule.vsi_handle = fltr->dest_vsi_handle;
8673		status = ice_rem_adv_rule_by_id(&pf->hw, &rule);
8674		if (status) {
8675			if (status == -ENOENT)
8676				dev_dbg(ice_pf_to_dev(pf), "TC flower filter (rule_id %u) does not exist\n",
8677					rule.rule_id);
8678			else
8679				dev_err(ice_pf_to_dev(pf), "failed to delete TC flower filter, status %d\n",
8680					status);
8681		} else if (fltr->dest_vsi) {
8682			/* update advanced switch filter count */
8683			if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
8684				u32 flags = fltr->flags;
8685
8686				fltr->dest_vsi->num_chnl_fltr--;
8687				if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
8688					     ICE_TC_FLWR_FIELD_ENC_DST_MAC))
8689					pf->num_dmac_chnl_fltrs--;
8690			}
8691		}
8692
8693		hlist_del(&fltr->tc_flower_node);
8694		kfree(fltr);
8695	}
8696}
8697
8698/**
8699 * ice_remove_q_channels - Remove queue channels for the TCs
8700 * @vsi: VSI to be configured
8701 * @rem_fltr: delete advanced switch filter or not
8702 *
8703 * Remove queue channels for the TCs
8704 */
8705static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_fltr)
8706{
8707	struct ice_channel *ch, *ch_tmp;
8708	struct ice_pf *pf = vsi->back;
8709	int i;
8710
8711	/* remove all tc-flower based filter if they are channel filters only */
8712	if (rem_fltr)
8713		ice_rem_all_chnl_fltrs(pf);
8714
8715	/* remove ntuple filters since queue configuration is being changed */
8716	if  (vsi->netdev->features & NETIF_F_NTUPLE) {
8717		struct ice_hw *hw = &pf->hw;
8718
8719		mutex_lock(&hw->fdir_fltr_lock);
8720		ice_fdir_del_all_fltrs(vsi);
8721		mutex_unlock(&hw->fdir_fltr_lock);
8722	}
8723
8724	/* perform cleanup for channels if they exist */
8725	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
8726		struct ice_vsi *ch_vsi;
8727
8728		list_del(&ch->list);
8729		ch_vsi = ch->ch_vsi;
8730		if (!ch_vsi) {
8731			kfree(ch);
8732			continue;
8733		}
8734
8735		/* Reset queue contexts */
8736		for (i = 0; i < ch->num_rxq; i++) {
8737			struct ice_tx_ring *tx_ring;
8738			struct ice_rx_ring *rx_ring;
8739
8740			tx_ring = vsi->tx_rings[ch->base_q + i];
8741			rx_ring = vsi->rx_rings[ch->base_q + i];
8742			if (tx_ring) {
8743				tx_ring->ch = NULL;
8744				if (tx_ring->q_vector)
8745					tx_ring->q_vector->ch = NULL;
8746			}
8747			if (rx_ring) {
8748				rx_ring->ch = NULL;
8749				if (rx_ring->q_vector)
8750					rx_ring->q_vector->ch = NULL;
8751			}
8752		}
8753
8754		/* Release FD resources for the channel VSI */
8755		ice_fdir_rem_adq_chnl(&pf->hw, ch->ch_vsi->idx);
8756
8757		/* clear the VSI from scheduler tree */
8758		ice_rm_vsi_lan_cfg(ch->ch_vsi->port_info, ch->ch_vsi->idx);
8759
8760		/* Delete VSI from FW, PF and HW VSI arrays */
8761		ice_vsi_delete(ch->ch_vsi);
8762
 
 
 
8763		/* free the channel */
8764		kfree(ch);
8765	}
8766
8767	/* clear the channel VSI map which is stored in main VSI */
8768	ice_for_each_chnl_tc(i)
8769		vsi->tc_map_vsi[i] = NULL;
8770
8771	/* reset main VSI's all TC information */
8772	vsi->all_enatc = 0;
8773	vsi->all_numtc = 0;
8774}
8775
8776/**
8777 * ice_rebuild_channels - rebuild channel
8778 * @pf: ptr to PF
8779 *
8780 * Recreate channel VSIs and replay filters
8781 */
8782static int ice_rebuild_channels(struct ice_pf *pf)
8783{
8784	struct device *dev = ice_pf_to_dev(pf);
8785	struct ice_vsi *main_vsi;
8786	bool rem_adv_fltr = true;
8787	struct ice_channel *ch;
8788	struct ice_vsi *vsi;
8789	int tc_idx = 1;
8790	int i, err;
8791
8792	main_vsi = ice_get_main_vsi(pf);
8793	if (!main_vsi)
8794		return 0;
8795
8796	if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) ||
8797	    main_vsi->old_numtc == 1)
8798		return 0; /* nothing to be done */
8799
8800	/* reconfigure main VSI based on old value of TC and cached values
8801	 * for MQPRIO opts
8802	 */
8803	err = ice_vsi_cfg_tc(main_vsi, main_vsi->old_ena_tc);
8804	if (err) {
8805		dev_err(dev, "failed configuring TC(ena_tc:0x%02x) for HW VSI=%u\n",
8806			main_vsi->old_ena_tc, main_vsi->vsi_num);
8807		return err;
8808	}
8809
8810	/* rebuild ADQ VSIs */
8811	ice_for_each_vsi(pf, i) {
8812		enum ice_vsi_type type;
8813
8814		vsi = pf->vsi[i];
8815		if (!vsi || vsi->type != ICE_VSI_CHNL)
8816			continue;
8817
8818		type = vsi->type;
8819
8820		/* rebuild ADQ VSI */
8821		err = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_INIT);
8822		if (err) {
8823			dev_err(dev, "VSI (type:%s) at index %d rebuild failed, err %d\n",
8824				ice_vsi_type_str(type), vsi->idx, err);
8825			goto cleanup;
8826		}
8827
8828		/* Re-map HW VSI number, using VSI handle that has been
8829		 * previously validated in ice_replay_vsi() call above
8830		 */
8831		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
8832
8833		/* replay filters for the VSI */
8834		err = ice_replay_vsi(&pf->hw, vsi->idx);
8835		if (err) {
8836			dev_err(dev, "VSI (type:%s) replay failed, err %d, VSI index %d\n",
8837				ice_vsi_type_str(type), err, vsi->idx);
8838			rem_adv_fltr = false;
8839			goto cleanup;
8840		}
8841		dev_info(dev, "VSI (type:%s) at index %d rebuilt successfully\n",
8842			 ice_vsi_type_str(type), vsi->idx);
8843
8844		/* store ADQ VSI at correct TC index in main VSI's
8845		 * map of TC to VSI
8846		 */
8847		main_vsi->tc_map_vsi[tc_idx++] = vsi;
8848	}
8849
8850	/* ADQ VSI(s) has been rebuilt successfully, so setup
8851	 * channel for main VSI's Tx and Rx rings
8852	 */
8853	list_for_each_entry(ch, &main_vsi->ch_list, list) {
8854		struct ice_vsi *ch_vsi;
8855
8856		ch_vsi = ch->ch_vsi;
8857		if (!ch_vsi)
8858			continue;
8859
8860		/* reconfig channel resources */
8861		ice_cfg_chnl_all_res(main_vsi, ch);
8862
8863		/* replay BW rate limit if it is non-zero */
8864		if (!ch->max_tx_rate && !ch->min_tx_rate)
8865			continue;
8866
8867		err = ice_set_bw_limit(ch_vsi, ch->max_tx_rate,
8868				       ch->min_tx_rate);
8869		if (err)
8870			dev_err(dev, "failed (err:%d) to rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
8871				err, ch->max_tx_rate, ch->min_tx_rate,
8872				ch_vsi->vsi_num);
8873		else
8874			dev_dbg(dev, "successfully rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
8875				ch->max_tx_rate, ch->min_tx_rate,
8876				ch_vsi->vsi_num);
8877	}
8878
8879	/* reconfig RSS for main VSI */
8880	if (main_vsi->ch_rss_size)
8881		ice_vsi_cfg_rss_lut_key(main_vsi);
8882
8883	return 0;
8884
8885cleanup:
8886	ice_remove_q_channels(main_vsi, rem_adv_fltr);
8887	return err;
8888}
8889
8890/**
8891 * ice_create_q_channels - Add queue channel for the given TCs
8892 * @vsi: VSI to be configured
8893 *
8894 * Configures queue channel mapping to the given TCs
8895 */
8896static int ice_create_q_channels(struct ice_vsi *vsi)
8897{
8898	struct ice_pf *pf = vsi->back;
8899	struct ice_channel *ch;
8900	int ret = 0, i;
8901
8902	ice_for_each_chnl_tc(i) {
8903		if (!(vsi->all_enatc & BIT(i)))
8904			continue;
8905
8906		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
8907		if (!ch) {
8908			ret = -ENOMEM;
8909			goto err_free;
8910		}
8911		INIT_LIST_HEAD(&ch->list);
8912		ch->num_rxq = vsi->mqprio_qopt.qopt.count[i];
8913		ch->num_txq = vsi->mqprio_qopt.qopt.count[i];
8914		ch->base_q = vsi->mqprio_qopt.qopt.offset[i];
8915		ch->max_tx_rate = vsi->mqprio_qopt.max_rate[i];
8916		ch->min_tx_rate = vsi->mqprio_qopt.min_rate[i];
8917
8918		/* convert to Kbits/s */
8919		if (ch->max_tx_rate)
8920			ch->max_tx_rate = div_u64(ch->max_tx_rate,
8921						  ICE_BW_KBPS_DIVISOR);
8922		if (ch->min_tx_rate)
8923			ch->min_tx_rate = div_u64(ch->min_tx_rate,
8924						  ICE_BW_KBPS_DIVISOR);
8925
8926		ret = ice_create_q_channel(vsi, ch);
8927		if (ret) {
8928			dev_err(ice_pf_to_dev(pf),
8929				"failed creating channel TC:%d\n", i);
8930			kfree(ch);
8931			goto err_free;
8932		}
8933		list_add_tail(&ch->list, &vsi->ch_list);
8934		vsi->tc_map_vsi[i] = ch->ch_vsi;
8935		dev_dbg(ice_pf_to_dev(pf),
8936			"successfully created channel: VSI %pK\n", ch->ch_vsi);
8937	}
8938	return 0;
8939
8940err_free:
8941	ice_remove_q_channels(vsi, false);
8942
8943	return ret;
8944}
8945
8946/**
8947 * ice_setup_tc_mqprio_qdisc - configure multiple traffic classes
8948 * @netdev: net device to configure
8949 * @type_data: TC offload data
8950 */
8951static int ice_setup_tc_mqprio_qdisc(struct net_device *netdev, void *type_data)
8952{
8953	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8954	struct ice_netdev_priv *np = netdev_priv(netdev);
8955	struct ice_vsi *vsi = np->vsi;
8956	struct ice_pf *pf = vsi->back;
8957	u16 mode, ena_tc_qdisc = 0;
8958	int cur_txq, cur_rxq;
8959	u8 hw = 0, num_tcf;
8960	struct device *dev;
8961	int ret, i;
8962
8963	dev = ice_pf_to_dev(pf);
8964	num_tcf = mqprio_qopt->qopt.num_tc;
8965	hw = mqprio_qopt->qopt.hw;
8966	mode = mqprio_qopt->mode;
8967	if (!hw) {
8968		clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8969		vsi->ch_rss_size = 0;
8970		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8971		goto config_tcf;
8972	}
8973
8974	/* Generate queue region map for number of TCF requested */
8975	for (i = 0; i < num_tcf; i++)
8976		ena_tc_qdisc |= BIT(i);
8977
8978	switch (mode) {
8979	case TC_MQPRIO_MODE_CHANNEL:
8980
8981		if (pf->hw.port_info->is_custom_tx_enabled) {
8982			dev_err(dev, "Custom Tx scheduler feature enabled, can't configure ADQ\n");
8983			return -EBUSY;
8984		}
8985		ice_tear_down_devlink_rate_tree(pf);
8986
8987		ret = ice_validate_mqprio_qopt(vsi, mqprio_qopt);
8988		if (ret) {
8989			netdev_err(netdev, "failed to validate_mqprio_qopt(), ret %d\n",
8990				   ret);
8991			return ret;
8992		}
8993		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8994		set_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8995		/* don't assume state of hw_tc_offload during driver load
8996		 * and set the flag for TC flower filter if hw_tc_offload
8997		 * already ON
8998		 */
8999		if (vsi->netdev->features & NETIF_F_HW_TC)
9000			set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
9001		break;
9002	default:
9003		return -EINVAL;
9004	}
9005
9006config_tcf:
9007
9008	/* Requesting same TCF configuration as already enabled */
9009	if (ena_tc_qdisc == vsi->tc_cfg.ena_tc &&
9010	    mode != TC_MQPRIO_MODE_CHANNEL)
9011		return 0;
9012
9013	/* Pause VSI queues */
9014	ice_dis_vsi(vsi, true);
9015
9016	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
9017		ice_remove_q_channels(vsi, true);
9018
9019	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
9020		vsi->req_txq = min_t(int, ice_get_avail_txq_count(pf),
9021				     num_online_cpus());
9022		vsi->req_rxq = min_t(int, ice_get_avail_rxq_count(pf),
9023				     num_online_cpus());
9024	} else {
9025		/* logic to rebuild VSI, same like ethtool -L */
9026		u16 offset = 0, qcount_tx = 0, qcount_rx = 0;
9027
9028		for (i = 0; i < num_tcf; i++) {
9029			if (!(ena_tc_qdisc & BIT(i)))
9030				continue;
9031
9032			offset = vsi->mqprio_qopt.qopt.offset[i];
9033			qcount_rx = vsi->mqprio_qopt.qopt.count[i];
9034			qcount_tx = vsi->mqprio_qopt.qopt.count[i];
9035		}
9036		vsi->req_txq = offset + qcount_tx;
9037		vsi->req_rxq = offset + qcount_rx;
9038
9039		/* store away original rss_size info, so that it gets reused
9040		 * form ice_vsi_rebuild during tc-qdisc delete stage - to
9041		 * determine, what should be the rss_sizefor main VSI
9042		 */
9043		vsi->orig_rss_size = vsi->rss_size;
9044	}
9045
9046	/* save current values of Tx and Rx queues before calling VSI rebuild
9047	 * for fallback option
9048	 */
9049	cur_txq = vsi->num_txq;
9050	cur_rxq = vsi->num_rxq;
9051
9052	/* proceed with rebuild main VSI using correct number of queues */
9053	ret = ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT);
9054	if (ret) {
9055		/* fallback to current number of queues */
9056		dev_info(dev, "Rebuild failed with new queues, try with current number of queues\n");
9057		vsi->req_txq = cur_txq;
9058		vsi->req_rxq = cur_rxq;
9059		clear_bit(ICE_RESET_FAILED, pf->state);
9060		if (ice_vsi_rebuild(vsi, ICE_VSI_FLAG_NO_INIT)) {
9061			dev_err(dev, "Rebuild of main VSI failed again\n");
9062			return ret;
9063		}
9064	}
9065
9066	vsi->all_numtc = num_tcf;
9067	vsi->all_enatc = ena_tc_qdisc;
9068	ret = ice_vsi_cfg_tc(vsi, ena_tc_qdisc);
9069	if (ret) {
9070		netdev_err(netdev, "failed configuring TC for VSI id=%d\n",
9071			   vsi->vsi_num);
9072		goto exit;
9073	}
9074
9075	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
9076		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
9077		u64 min_tx_rate = vsi->mqprio_qopt.min_rate[0];
9078
9079		/* set TC0 rate limit if specified */
9080		if (max_tx_rate || min_tx_rate) {
9081			/* convert to Kbits/s */
9082			if (max_tx_rate)
9083				max_tx_rate = div_u64(max_tx_rate, ICE_BW_KBPS_DIVISOR);
9084			if (min_tx_rate)
9085				min_tx_rate = div_u64(min_tx_rate, ICE_BW_KBPS_DIVISOR);
9086
9087			ret = ice_set_bw_limit(vsi, max_tx_rate, min_tx_rate);
9088			if (!ret) {
9089				dev_dbg(dev, "set Tx rate max %llu min %llu for VSI(%u)\n",
9090					max_tx_rate, min_tx_rate, vsi->vsi_num);
9091			} else {
9092				dev_err(dev, "failed to set Tx rate max %llu min %llu for VSI(%u)\n",
9093					max_tx_rate, min_tx_rate, vsi->vsi_num);
9094				goto exit;
9095			}
9096		}
9097		ret = ice_create_q_channels(vsi);
9098		if (ret) {
9099			netdev_err(netdev, "failed configuring queue channels\n");
9100			goto exit;
9101		} else {
9102			netdev_dbg(netdev, "successfully configured channels\n");
9103		}
9104	}
9105
9106	if (vsi->ch_rss_size)
9107		ice_vsi_cfg_rss_lut_key(vsi);
9108
9109exit:
9110	/* if error, reset the all_numtc and all_enatc */
9111	if (ret) {
9112		vsi->all_numtc = 0;
9113		vsi->all_enatc = 0;
9114	}
9115	/* resume VSI */
9116	ice_ena_vsi(vsi, true);
9117
9118	return ret;
9119}
9120
9121static LIST_HEAD(ice_block_cb_list);
9122
9123static int
9124ice_setup_tc(struct net_device *netdev, enum tc_setup_type type,
9125	     void *type_data)
9126{
9127	struct ice_netdev_priv *np = netdev_priv(netdev);
9128	struct ice_pf *pf = np->vsi->back;
9129	bool locked = false;
9130	int err;
9131
9132	switch (type) {
9133	case TC_SETUP_BLOCK:
9134		return flow_block_cb_setup_simple(type_data,
9135						  &ice_block_cb_list,
9136						  ice_setup_tc_block_cb,
9137						  np, np, true);
9138	case TC_SETUP_QDISC_MQPRIO:
9139		if (ice_is_eswitch_mode_switchdev(pf)) {
9140			netdev_err(netdev, "TC MQPRIO offload not supported, switchdev is enabled\n");
9141			return -EOPNOTSUPP;
9142		}
9143
9144		if (pf->adev) {
9145			mutex_lock(&pf->adev_mutex);
9146			device_lock(&pf->adev->dev);
9147			locked = true;
9148			if (pf->adev->dev.driver) {
9149				netdev_err(netdev, "Cannot change qdisc when RDMA is active\n");
9150				err = -EBUSY;
9151				goto adev_unlock;
9152			}
9153		}
9154
9155		/* setup traffic classifier for receive side */
9156		mutex_lock(&pf->tc_mutex);
9157		err = ice_setup_tc_mqprio_qdisc(netdev, type_data);
9158		mutex_unlock(&pf->tc_mutex);
9159
9160adev_unlock:
9161		if (locked) {
9162			device_unlock(&pf->adev->dev);
9163			mutex_unlock(&pf->adev_mutex);
9164		}
9165		return err;
9166	default:
9167		return -EOPNOTSUPP;
9168	}
9169	return -EOPNOTSUPP;
9170}
9171
9172static struct ice_indr_block_priv *
9173ice_indr_block_priv_lookup(struct ice_netdev_priv *np,
9174			   struct net_device *netdev)
9175{
9176	struct ice_indr_block_priv *cb_priv;
9177
9178	list_for_each_entry(cb_priv, &np->tc_indr_block_priv_list, list) {
9179		if (!cb_priv->netdev)
9180			return NULL;
9181		if (cb_priv->netdev == netdev)
9182			return cb_priv;
9183	}
9184	return NULL;
9185}
9186
9187static int
9188ice_indr_setup_block_cb(enum tc_setup_type type, void *type_data,
9189			void *indr_priv)
9190{
9191	struct ice_indr_block_priv *priv = indr_priv;
9192	struct ice_netdev_priv *np = priv->np;
9193
9194	switch (type) {
9195	case TC_SETUP_CLSFLOWER:
9196		return ice_setup_tc_cls_flower(np, priv->netdev,
9197					       (struct flow_cls_offload *)
9198					       type_data);
9199	default:
9200		return -EOPNOTSUPP;
9201	}
9202}
9203
9204static int
9205ice_indr_setup_tc_block(struct net_device *netdev, struct Qdisc *sch,
9206			struct ice_netdev_priv *np,
9207			struct flow_block_offload *f, void *data,
9208			void (*cleanup)(struct flow_block_cb *block_cb))
9209{
9210	struct ice_indr_block_priv *indr_priv;
9211	struct flow_block_cb *block_cb;
9212
9213	if (!ice_is_tunnel_supported(netdev) &&
9214	    !(is_vlan_dev(netdev) &&
9215	      vlan_dev_real_dev(netdev) == np->vsi->netdev))
9216		return -EOPNOTSUPP;
9217
9218	if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
9219		return -EOPNOTSUPP;
9220
9221	switch (f->command) {
9222	case FLOW_BLOCK_BIND:
9223		indr_priv = ice_indr_block_priv_lookup(np, netdev);
9224		if (indr_priv)
9225			return -EEXIST;
9226
9227		indr_priv = kzalloc(sizeof(*indr_priv), GFP_KERNEL);
9228		if (!indr_priv)
9229			return -ENOMEM;
9230
9231		indr_priv->netdev = netdev;
9232		indr_priv->np = np;
9233		list_add(&indr_priv->list, &np->tc_indr_block_priv_list);
9234
9235		block_cb =
9236			flow_indr_block_cb_alloc(ice_indr_setup_block_cb,
9237						 indr_priv, indr_priv,
9238						 ice_rep_indr_tc_block_unbind,
9239						 f, netdev, sch, data, np,
9240						 cleanup);
9241
9242		if (IS_ERR(block_cb)) {
9243			list_del(&indr_priv->list);
9244			kfree(indr_priv);
9245			return PTR_ERR(block_cb);
9246		}
9247		flow_block_cb_add(block_cb, f);
9248		list_add_tail(&block_cb->driver_list, &ice_block_cb_list);
9249		break;
9250	case FLOW_BLOCK_UNBIND:
9251		indr_priv = ice_indr_block_priv_lookup(np, netdev);
9252		if (!indr_priv)
9253			return -ENOENT;
9254
9255		block_cb = flow_block_cb_lookup(f->block,
9256						ice_indr_setup_block_cb,
9257						indr_priv);
9258		if (!block_cb)
9259			return -ENOENT;
9260
9261		flow_indr_block_cb_remove(block_cb, f);
9262
9263		list_del(&block_cb->driver_list);
9264		break;
9265	default:
9266		return -EOPNOTSUPP;
9267	}
9268	return 0;
9269}
9270
9271static int
9272ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
9273		     void *cb_priv, enum tc_setup_type type, void *type_data,
9274		     void *data,
9275		     void (*cleanup)(struct flow_block_cb *block_cb))
9276{
9277	switch (type) {
9278	case TC_SETUP_BLOCK:
9279		return ice_indr_setup_tc_block(netdev, sch, cb_priv, type_data,
9280					       data, cleanup);
9281
9282	default:
9283		return -EOPNOTSUPP;
9284	}
9285}
9286
9287/**
9288 * ice_open - Called when a network interface becomes active
9289 * @netdev: network interface device structure
9290 *
9291 * The open entry point is called when a network interface is made
9292 * active by the system (IFF_UP). At this point all resources needed
9293 * for transmit and receive operations are allocated, the interrupt
9294 * handler is registered with the OS, the netdev watchdog is enabled,
9295 * and the stack is notified that the interface is ready.
9296 *
9297 * Returns 0 on success, negative value on failure
9298 */
9299int ice_open(struct net_device *netdev)
9300{
9301	struct ice_netdev_priv *np = netdev_priv(netdev);
9302	struct ice_pf *pf = np->vsi->back;
9303
9304	if (ice_is_reset_in_progress(pf->state)) {
9305		netdev_err(netdev, "can't open net device while reset is in progress");
9306		return -EBUSY;
9307	}
9308
9309	return ice_open_internal(netdev);
9310}
9311
9312/**
9313 * ice_open_internal - Called when a network interface becomes active
9314 * @netdev: network interface device structure
9315 *
9316 * Internal ice_open implementation. Should not be used directly except for ice_open and reset
9317 * handling routine
9318 *
9319 * Returns 0 on success, negative value on failure
9320 */
9321int ice_open_internal(struct net_device *netdev)
9322{
9323	struct ice_netdev_priv *np = netdev_priv(netdev);
9324	struct ice_vsi *vsi = np->vsi;
9325	struct ice_pf *pf = vsi->back;
9326	struct ice_port_info *pi;
9327	int err;
9328
9329	if (test_bit(ICE_NEEDS_RESTART, pf->state)) {
9330		netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
9331		return -EIO;
9332	}
9333
9334	netif_carrier_off(netdev);
9335
9336	pi = vsi->port_info;
9337	err = ice_update_link_info(pi);
9338	if (err) {
9339		netdev_err(netdev, "Failed to get link info, error %d\n", err);
9340		return err;
9341	}
9342
9343	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
9344
9345	/* Set PHY if there is media, otherwise, turn off PHY */
9346	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
9347		clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9348		if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state)) {
9349			err = ice_init_phy_user_cfg(pi);
9350			if (err) {
9351				netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
9352					   err);
9353				return err;
9354			}
9355		}
9356
9357		err = ice_configure_phy(vsi);
9358		if (err) {
9359			netdev_err(netdev, "Failed to set physical link up, error %d\n",
9360				   err);
9361			return err;
9362		}
9363	} else {
9364		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9365		ice_set_link(vsi, false);
9366	}
9367
9368	err = ice_vsi_open(vsi);
9369	if (err)
9370		netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
9371			   vsi->vsi_num, vsi->vsw->sw_id);
9372
9373	/* Update existing tunnels information */
9374	udp_tunnel_get_rx_info(netdev);
9375
9376	return err;
9377}
9378
9379/**
9380 * ice_stop - Disables a network interface
9381 * @netdev: network interface device structure
9382 *
9383 * The stop entry point is called when an interface is de-activated by the OS,
9384 * and the netdevice enters the DOWN state. The hardware is still under the
9385 * driver's control, but the netdev interface is disabled.
9386 *
9387 * Returns success only - not allowed to fail
9388 */
9389int ice_stop(struct net_device *netdev)
9390{
9391	struct ice_netdev_priv *np = netdev_priv(netdev);
9392	struct ice_vsi *vsi = np->vsi;
9393	struct ice_pf *pf = vsi->back;
9394
9395	if (ice_is_reset_in_progress(pf->state)) {
9396		netdev_err(netdev, "can't stop net device while reset is in progress");
9397		return -EBUSY;
9398	}
9399
9400	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
9401		int link_err = ice_force_phys_link_state(vsi, false);
9402
9403		if (link_err) {
9404			if (link_err == -ENOMEDIUM)
9405				netdev_info(vsi->netdev, "Skipping link reconfig - no media attached, VSI %d\n",
9406					    vsi->vsi_num);
9407			else
9408				netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
9409					   vsi->vsi_num, link_err);
9410
9411			ice_vsi_close(vsi);
9412			return -EIO;
9413		}
9414	}
9415
9416	ice_vsi_close(vsi);
9417
9418	return 0;
9419}
9420
9421/**
9422 * ice_features_check - Validate encapsulated packet conforms to limits
9423 * @skb: skb buffer
9424 * @netdev: This port's netdev
9425 * @features: Offload features that the stack believes apply
9426 */
9427static netdev_features_t
9428ice_features_check(struct sk_buff *skb,
9429		   struct net_device __always_unused *netdev,
9430		   netdev_features_t features)
9431{
9432	bool gso = skb_is_gso(skb);
9433	size_t len;
9434
9435	/* No point in doing any of this if neither checksum nor GSO are
9436	 * being requested for this frame. We can rule out both by just
9437	 * checking for CHECKSUM_PARTIAL
9438	 */
9439	if (skb->ip_summed != CHECKSUM_PARTIAL)
9440		return features;
9441
9442	/* We cannot support GSO if the MSS is going to be less than
9443	 * 64 bytes. If it is then we need to drop support for GSO.
9444	 */
9445	if (gso && (skb_shinfo(skb)->gso_size < ICE_TXD_CTX_MIN_MSS))
9446		features &= ~NETIF_F_GSO_MASK;
9447
9448	len = skb_network_offset(skb);
9449	if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
9450		goto out_rm_features;
9451
9452	len = skb_network_header_len(skb);
9453	if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9454		goto out_rm_features;
9455
9456	if (skb->encapsulation) {
9457		/* this must work for VXLAN frames AND IPIP/SIT frames, and in
9458		 * the case of IPIP frames, the transport header pointer is
9459		 * after the inner header! So check to make sure that this
9460		 * is a GRE or UDP_TUNNEL frame before doing that math.
9461		 */
9462		if (gso && (skb_shinfo(skb)->gso_type &
9463			    (SKB_GSO_GRE | SKB_GSO_UDP_TUNNEL))) {
9464			len = skb_inner_network_header(skb) -
9465			      skb_transport_header(skb);
9466			if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
9467				goto out_rm_features;
9468		}
9469
9470		len = skb_inner_network_header_len(skb);
9471		if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9472			goto out_rm_features;
9473	}
9474
9475	return features;
9476out_rm_features:
9477	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
9478}
9479
9480static const struct net_device_ops ice_netdev_safe_mode_ops = {
9481	.ndo_open = ice_open,
9482	.ndo_stop = ice_stop,
9483	.ndo_start_xmit = ice_start_xmit,
9484	.ndo_set_mac_address = ice_set_mac_address,
9485	.ndo_validate_addr = eth_validate_addr,
9486	.ndo_change_mtu = ice_change_mtu,
9487	.ndo_get_stats64 = ice_get_stats64,
9488	.ndo_tx_timeout = ice_tx_timeout,
9489	.ndo_bpf = ice_xdp_safe_mode,
9490};
9491
9492static const struct net_device_ops ice_netdev_ops = {
9493	.ndo_open = ice_open,
9494	.ndo_stop = ice_stop,
9495	.ndo_start_xmit = ice_start_xmit,
9496	.ndo_select_queue = ice_select_queue,
9497	.ndo_features_check = ice_features_check,
9498	.ndo_fix_features = ice_fix_features,
9499	.ndo_set_rx_mode = ice_set_rx_mode,
9500	.ndo_set_mac_address = ice_set_mac_address,
9501	.ndo_validate_addr = eth_validate_addr,
9502	.ndo_change_mtu = ice_change_mtu,
9503	.ndo_get_stats64 = ice_get_stats64,
9504	.ndo_set_tx_maxrate = ice_set_tx_maxrate,
9505	.ndo_eth_ioctl = ice_eth_ioctl,
9506	.ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
9507	.ndo_set_vf_mac = ice_set_vf_mac,
9508	.ndo_get_vf_config = ice_get_vf_cfg,
9509	.ndo_set_vf_trust = ice_set_vf_trust,
9510	.ndo_set_vf_vlan = ice_set_vf_port_vlan,
9511	.ndo_set_vf_link_state = ice_set_vf_link_state,
9512	.ndo_get_vf_stats = ice_get_vf_stats,
9513	.ndo_set_vf_rate = ice_set_vf_bw,
9514	.ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
9515	.ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
9516	.ndo_setup_tc = ice_setup_tc,
9517	.ndo_set_features = ice_set_features,
9518	.ndo_bridge_getlink = ice_bridge_getlink,
9519	.ndo_bridge_setlink = ice_bridge_setlink,
9520	.ndo_fdb_add = ice_fdb_add,
9521	.ndo_fdb_del = ice_fdb_del,
9522#ifdef CONFIG_RFS_ACCEL
9523	.ndo_rx_flow_steer = ice_rx_flow_steer,
9524#endif
9525	.ndo_tx_timeout = ice_tx_timeout,
9526	.ndo_bpf = ice_xdp,
9527	.ndo_xdp_xmit = ice_xdp_xmit,
9528	.ndo_xsk_wakeup = ice_xsk_wakeup,
9529};
v6.2
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (c) 2018, Intel Corporation. */
   3
   4/* Intel(R) Ethernet Connection E800 Series Linux Driver */
   5
   6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
   7
   8#include <generated/utsrelease.h>
 
   9#include "ice.h"
  10#include "ice_base.h"
  11#include "ice_lib.h"
  12#include "ice_fltr.h"
  13#include "ice_dcb_lib.h"
  14#include "ice_dcb_nl.h"
  15#include "ice_devlink.h"
 
  16/* Including ice_trace.h with CREATE_TRACE_POINTS defined will generate the
  17 * ice tracepoint functions. This must be done exactly once across the
  18 * ice driver.
  19 */
  20#define CREATE_TRACE_POINTS
  21#include "ice_trace.h"
  22#include "ice_eswitch.h"
  23#include "ice_tc_lib.h"
  24#include "ice_vsi_vlan_ops.h"
 
  25
  26#define DRV_SUMMARY	"Intel(R) Ethernet Connection E800 Series Linux Driver"
  27static const char ice_driver_string[] = DRV_SUMMARY;
  28static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
  29
  30/* DDP Package file located in firmware search paths (e.g. /lib/firmware/) */
  31#define ICE_DDP_PKG_PATH	"intel/ice/ddp/"
  32#define ICE_DDP_PKG_FILE	ICE_DDP_PKG_PATH "ice.pkg"
  33
  34MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  35MODULE_DESCRIPTION(DRV_SUMMARY);
  36MODULE_LICENSE("GPL v2");
  37MODULE_FIRMWARE(ICE_DDP_PKG_FILE);
  38
  39static int debug = -1;
  40module_param(debug, int, 0644);
  41#ifndef CONFIG_DYNAMIC_DEBUG
  42MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
  43#else
  44MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
  45#endif /* !CONFIG_DYNAMIC_DEBUG */
  46
  47static DEFINE_IDA(ice_aux_ida);
  48DEFINE_STATIC_KEY_FALSE(ice_xdp_locking_key);
  49EXPORT_SYMBOL(ice_xdp_locking_key);
  50
  51/**
  52 * ice_hw_to_dev - Get device pointer from the hardware structure
  53 * @hw: pointer to the device HW structure
  54 *
  55 * Used to access the device pointer from compilation units which can't easily
  56 * include the definition of struct ice_pf without leading to circular header
  57 * dependencies.
  58 */
  59struct device *ice_hw_to_dev(struct ice_hw *hw)
  60{
  61	struct ice_pf *pf = container_of(hw, struct ice_pf, hw);
  62
  63	return &pf->pdev->dev;
  64}
  65
  66static struct workqueue_struct *ice_wq;
 
  67static const struct net_device_ops ice_netdev_safe_mode_ops;
  68static const struct net_device_ops ice_netdev_ops;
  69
  70static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type);
  71
  72static void ice_vsi_release_all(struct ice_pf *pf);
  73
  74static int ice_rebuild_channels(struct ice_pf *pf);
  75static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_adv_fltr);
  76
  77static int
  78ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
  79		     void *cb_priv, enum tc_setup_type type, void *type_data,
  80		     void *data,
  81		     void (*cleanup)(struct flow_block_cb *block_cb));
  82
  83bool netif_is_ice(struct net_device *dev)
  84{
  85	return dev && (dev->netdev_ops == &ice_netdev_ops);
  86}
  87
  88/**
  89 * ice_get_tx_pending - returns number of Tx descriptors not processed
  90 * @ring: the ring of descriptors
  91 */
  92static u16 ice_get_tx_pending(struct ice_tx_ring *ring)
  93{
  94	u16 head, tail;
  95
  96	head = ring->next_to_clean;
  97	tail = ring->next_to_use;
  98
  99	if (head != tail)
 100		return (head < tail) ?
 101			tail - head : (tail + ring->count - head);
 102	return 0;
 103}
 104
 105/**
 106 * ice_check_for_hang_subtask - check for and recover hung queues
 107 * @pf: pointer to PF struct
 108 */
 109static void ice_check_for_hang_subtask(struct ice_pf *pf)
 110{
 111	struct ice_vsi *vsi = NULL;
 112	struct ice_hw *hw;
 113	unsigned int i;
 114	int packets;
 115	u32 v;
 116
 117	ice_for_each_vsi(pf, v)
 118		if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) {
 119			vsi = pf->vsi[v];
 120			break;
 121		}
 122
 123	if (!vsi || test_bit(ICE_VSI_DOWN, vsi->state))
 124		return;
 125
 126	if (!(vsi->netdev && netif_carrier_ok(vsi->netdev)))
 127		return;
 128
 129	hw = &vsi->back->hw;
 130
 131	ice_for_each_txq(vsi, i) {
 132		struct ice_tx_ring *tx_ring = vsi->tx_rings[i];
 133		struct ice_ring_stats *ring_stats;
 134
 135		if (!tx_ring)
 136			continue;
 137		if (ice_ring_ch_enabled(tx_ring))
 138			continue;
 139
 140		ring_stats = tx_ring->ring_stats;
 141		if (!ring_stats)
 142			continue;
 143
 144		if (tx_ring->desc) {
 145			/* If packet counter has not changed the queue is
 146			 * likely stalled, so force an interrupt for this
 147			 * queue.
 148			 *
 149			 * prev_pkt would be negative if there was no
 150			 * pending work.
 151			 */
 152			packets = ring_stats->stats.pkts & INT_MAX;
 153			if (ring_stats->tx_stats.prev_pkt == packets) {
 154				/* Trigger sw interrupt to revive the queue */
 155				ice_trigger_sw_intr(hw, tx_ring->q_vector);
 156				continue;
 157			}
 158
 159			/* Memory barrier between read of packet count and call
 160			 * to ice_get_tx_pending()
 161			 */
 162			smp_rmb();
 163			ring_stats->tx_stats.prev_pkt =
 164			    ice_get_tx_pending(tx_ring) ? packets : -1;
 165		}
 166	}
 167}
 168
 169/**
 170 * ice_init_mac_fltr - Set initial MAC filters
 171 * @pf: board private structure
 172 *
 173 * Set initial set of MAC filters for PF VSI; configure filters for permanent
 174 * address and broadcast address. If an error is encountered, netdevice will be
 175 * unregistered.
 176 */
 177static int ice_init_mac_fltr(struct ice_pf *pf)
 178{
 179	struct ice_vsi *vsi;
 180	u8 *perm_addr;
 181
 182	vsi = ice_get_main_vsi(pf);
 183	if (!vsi)
 184		return -EINVAL;
 185
 186	perm_addr = vsi->port_info->mac.perm_addr;
 187	return ice_fltr_add_mac_and_broadcast(vsi, perm_addr, ICE_FWD_TO_VSI);
 188}
 189
 190/**
 191 * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced
 192 * @netdev: the net device on which the sync is happening
 193 * @addr: MAC address to sync
 194 *
 195 * This is a callback function which is called by the in kernel device sync
 196 * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
 197 * populates the tmp_sync_list, which is later used by ice_add_mac to add the
 198 * MAC filters from the hardware.
 199 */
 200static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
 201{
 202	struct ice_netdev_priv *np = netdev_priv(netdev);
 203	struct ice_vsi *vsi = np->vsi;
 204
 205	if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr,
 206				     ICE_FWD_TO_VSI))
 207		return -EINVAL;
 208
 209	return 0;
 210}
 211
 212/**
 213 * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced
 214 * @netdev: the net device on which the unsync is happening
 215 * @addr: MAC address to unsync
 216 *
 217 * This is a callback function which is called by the in kernel device unsync
 218 * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
 219 * populates the tmp_unsync_list, which is later used by ice_remove_mac to
 220 * delete the MAC filters from the hardware.
 221 */
 222static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
 223{
 224	struct ice_netdev_priv *np = netdev_priv(netdev);
 225	struct ice_vsi *vsi = np->vsi;
 226
 227	/* Under some circumstances, we might receive a request to delete our
 228	 * own device address from our uc list. Because we store the device
 229	 * address in the VSI's MAC filter list, we need to ignore such
 230	 * requests and not delete our device address from this list.
 231	 */
 232	if (ether_addr_equal(addr, netdev->dev_addr))
 233		return 0;
 234
 235	if (ice_fltr_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr,
 236				     ICE_FWD_TO_VSI))
 237		return -EINVAL;
 238
 239	return 0;
 240}
 241
 242/**
 243 * ice_vsi_fltr_changed - check if filter state changed
 244 * @vsi: VSI to be checked
 245 *
 246 * returns true if filter state has changed, false otherwise.
 247 */
 248static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
 249{
 250	return test_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state) ||
 251	       test_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
 252}
 253
 254/**
 255 * ice_set_promisc - Enable promiscuous mode for a given PF
 256 * @vsi: the VSI being configured
 257 * @promisc_m: mask of promiscuous config bits
 258 *
 259 */
 260static int ice_set_promisc(struct ice_vsi *vsi, u8 promisc_m)
 261{
 262	int status;
 263
 264	if (vsi->type != ICE_VSI_PF)
 265		return 0;
 266
 267	if (ice_vsi_has_non_zero_vlans(vsi)) {
 268		promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
 269		status = ice_fltr_set_vlan_vsi_promisc(&vsi->back->hw, vsi,
 270						       promisc_m);
 271	} else {
 272		status = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
 273						  promisc_m, 0);
 274	}
 275	if (status && status != -EEXIST)
 276		return status;
 277
 278	netdev_dbg(vsi->netdev, "set promisc filter bits for VSI %i: 0x%x\n",
 279		   vsi->vsi_num, promisc_m);
 280	return 0;
 281}
 282
 283/**
 284 * ice_clear_promisc - Disable promiscuous mode for a given PF
 285 * @vsi: the VSI being configured
 286 * @promisc_m: mask of promiscuous config bits
 287 *
 288 */
 289static int ice_clear_promisc(struct ice_vsi *vsi, u8 promisc_m)
 290{
 291	int status;
 292
 293	if (vsi->type != ICE_VSI_PF)
 294		return 0;
 295
 296	if (ice_vsi_has_non_zero_vlans(vsi)) {
 297		promisc_m |= (ICE_PROMISC_VLAN_RX | ICE_PROMISC_VLAN_TX);
 298		status = ice_fltr_clear_vlan_vsi_promisc(&vsi->back->hw, vsi,
 299							 promisc_m);
 300	} else {
 301		status = ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
 302						    promisc_m, 0);
 303	}
 304
 305	netdev_dbg(vsi->netdev, "clear promisc filter bits for VSI %i: 0x%x\n",
 306		   vsi->vsi_num, promisc_m);
 307	return status;
 308}
 309
 310/**
 311 * ice_vsi_sync_fltr - Update the VSI filter list to the HW
 312 * @vsi: ptr to the VSI
 313 *
 314 * Push any outstanding VSI filter changes through the AdminQ.
 315 */
 316static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
 317{
 318	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
 319	struct device *dev = ice_pf_to_dev(vsi->back);
 320	struct net_device *netdev = vsi->netdev;
 321	bool promisc_forced_on = false;
 322	struct ice_pf *pf = vsi->back;
 323	struct ice_hw *hw = &pf->hw;
 324	u32 changed_flags = 0;
 325	int err;
 326
 327	if (!vsi->netdev)
 328		return -EINVAL;
 329
 330	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
 331		usleep_range(1000, 2000);
 332
 333	changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
 334	vsi->current_netdev_flags = vsi->netdev->flags;
 335
 336	INIT_LIST_HEAD(&vsi->tmp_sync_list);
 337	INIT_LIST_HEAD(&vsi->tmp_unsync_list);
 338
 339	if (ice_vsi_fltr_changed(vsi)) {
 340		clear_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
 341		clear_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
 342
 343		/* grab the netdev's addr_list_lock */
 344		netif_addr_lock_bh(netdev);
 345		__dev_uc_sync(netdev, ice_add_mac_to_sync_list,
 346			      ice_add_mac_to_unsync_list);
 347		__dev_mc_sync(netdev, ice_add_mac_to_sync_list,
 348			      ice_add_mac_to_unsync_list);
 349		/* our temp lists are populated. release lock */
 350		netif_addr_unlock_bh(netdev);
 351	}
 352
 353	/* Remove MAC addresses in the unsync list */
 354	err = ice_fltr_remove_mac_list(vsi, &vsi->tmp_unsync_list);
 355	ice_fltr_free_list(dev, &vsi->tmp_unsync_list);
 356	if (err) {
 357		netdev_err(netdev, "Failed to delete MAC filters\n");
 358		/* if we failed because of alloc failures, just bail */
 359		if (err == -ENOMEM)
 360			goto out;
 361	}
 362
 363	/* Add MAC addresses in the sync list */
 364	err = ice_fltr_add_mac_list(vsi, &vsi->tmp_sync_list);
 365	ice_fltr_free_list(dev, &vsi->tmp_sync_list);
 366	/* If filter is added successfully or already exists, do not go into
 367	 * 'if' condition and report it as error. Instead continue processing
 368	 * rest of the function.
 369	 */
 370	if (err && err != -EEXIST) {
 371		netdev_err(netdev, "Failed to add MAC filters\n");
 372		/* If there is no more space for new umac filters, VSI
 373		 * should go into promiscuous mode. There should be some
 374		 * space reserved for promiscuous filters.
 375		 */
 376		if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
 377		    !test_and_set_bit(ICE_FLTR_OVERFLOW_PROMISC,
 378				      vsi->state)) {
 379			promisc_forced_on = true;
 380			netdev_warn(netdev, "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
 381				    vsi->vsi_num);
 382		} else {
 383			goto out;
 384		}
 385	}
 386	err = 0;
 387	/* check for changes in promiscuous modes */
 388	if (changed_flags & IFF_ALLMULTI) {
 389		if (vsi->current_netdev_flags & IFF_ALLMULTI) {
 390			err = ice_set_promisc(vsi, ICE_MCAST_PROMISC_BITS);
 391			if (err) {
 392				vsi->current_netdev_flags &= ~IFF_ALLMULTI;
 393				goto out_promisc;
 394			}
 395		} else {
 396			/* !(vsi->current_netdev_flags & IFF_ALLMULTI) */
 397			err = ice_clear_promisc(vsi, ICE_MCAST_PROMISC_BITS);
 398			if (err) {
 399				vsi->current_netdev_flags |= IFF_ALLMULTI;
 400				goto out_promisc;
 401			}
 402		}
 403	}
 404
 405	if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
 406	    test_bit(ICE_VSI_PROMISC_CHANGED, vsi->state)) {
 407		clear_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
 408		if (vsi->current_netdev_flags & IFF_PROMISC) {
 409			/* Apply Rx filter rule to get traffic from wire */
 410			if (!ice_is_dflt_vsi_in_use(vsi->port_info)) {
 411				err = ice_set_dflt_vsi(vsi);
 412				if (err && err != -EEXIST) {
 413					netdev_err(netdev, "Error %d setting default VSI %i Rx rule\n",
 414						   err, vsi->vsi_num);
 415					vsi->current_netdev_flags &=
 416						~IFF_PROMISC;
 417					goto out_promisc;
 418				}
 419				err = 0;
 420				vlan_ops->dis_rx_filtering(vsi);
 421
 422				/* promiscuous mode implies allmulticast so
 423				 * that VSIs that are in promiscuous mode are
 424				 * subscribed to multicast packets coming to
 425				 * the port
 426				 */
 427				err = ice_set_promisc(vsi,
 428						      ICE_MCAST_PROMISC_BITS);
 429				if (err)
 430					goto out_promisc;
 431			}
 432		} else {
 433			/* Clear Rx filter to remove traffic from wire */
 434			if (ice_is_vsi_dflt_vsi(vsi)) {
 435				err = ice_clear_dflt_vsi(vsi);
 436				if (err) {
 437					netdev_err(netdev, "Error %d clearing default VSI %i Rx rule\n",
 438						   err, vsi->vsi_num);
 439					vsi->current_netdev_flags |=
 440						IFF_PROMISC;
 441					goto out_promisc;
 442				}
 443				if (vsi->netdev->features &
 444				    NETIF_F_HW_VLAN_CTAG_FILTER)
 445					vlan_ops->ena_rx_filtering(vsi);
 446			}
 447
 448			/* disable allmulti here, but only if allmulti is not
 449			 * still enabled for the netdev
 450			 */
 451			if (!(vsi->current_netdev_flags & IFF_ALLMULTI)) {
 452				err = ice_clear_promisc(vsi,
 453							ICE_MCAST_PROMISC_BITS);
 454				if (err) {
 455					netdev_err(netdev, "Error %d clearing multicast promiscuous on VSI %i\n",
 456						   err, vsi->vsi_num);
 457				}
 458			}
 459		}
 460	}
 461	goto exit;
 462
 463out_promisc:
 464	set_bit(ICE_VSI_PROMISC_CHANGED, vsi->state);
 465	goto exit;
 466out:
 467	/* if something went wrong then set the changed flag so we try again */
 468	set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
 469	set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
 470exit:
 471	clear_bit(ICE_CFG_BUSY, vsi->state);
 472	return err;
 473}
 474
 475/**
 476 * ice_sync_fltr_subtask - Sync the VSI filter list with HW
 477 * @pf: board private structure
 478 */
 479static void ice_sync_fltr_subtask(struct ice_pf *pf)
 480{
 481	int v;
 482
 483	if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
 484		return;
 485
 486	clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
 487
 488	ice_for_each_vsi(pf, v)
 489		if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
 490		    ice_vsi_sync_fltr(pf->vsi[v])) {
 491			/* come back and try again later */
 492			set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
 493			break;
 494		}
 495}
 496
 497/**
 498 * ice_pf_dis_all_vsi - Pause all VSIs on a PF
 499 * @pf: the PF
 500 * @locked: is the rtnl_lock already held
 501 */
 502static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked)
 503{
 504	int node;
 505	int v;
 506
 507	ice_for_each_vsi(pf, v)
 508		if (pf->vsi[v])
 509			ice_dis_vsi(pf->vsi[v], locked);
 510
 511	for (node = 0; node < ICE_MAX_PF_AGG_NODES; node++)
 512		pf->pf_agg_node[node].num_vsis = 0;
 513
 514	for (node = 0; node < ICE_MAX_VF_AGG_NODES; node++)
 515		pf->vf_agg_node[node].num_vsis = 0;
 516}
 517
 518/**
 519 * ice_clear_sw_switch_recipes - clear switch recipes
 520 * @pf: board private structure
 521 *
 522 * Mark switch recipes as not created in sw structures. There are cases where
 523 * rules (especially advanced rules) need to be restored, either re-read from
 524 * hardware or added again. For example after the reset. 'recp_created' flag
 525 * prevents from doing that and need to be cleared upfront.
 526 */
 527static void ice_clear_sw_switch_recipes(struct ice_pf *pf)
 528{
 529	struct ice_sw_recipe *recp;
 530	u8 i;
 531
 532	recp = pf->hw.switch_info->recp_list;
 533	for (i = 0; i < ICE_MAX_NUM_RECIPES; i++)
 534		recp[i].recp_created = false;
 535}
 536
 537/**
 538 * ice_prepare_for_reset - prep for reset
 539 * @pf: board private structure
 540 * @reset_type: reset type requested
 541 *
 542 * Inform or close all dependent features in prep for reset.
 543 */
 544static void
 545ice_prepare_for_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
 546{
 547	struct ice_hw *hw = &pf->hw;
 548	struct ice_vsi *vsi;
 549	struct ice_vf *vf;
 550	unsigned int bkt;
 551
 552	dev_dbg(ice_pf_to_dev(pf), "reset_type=%d\n", reset_type);
 553
 554	/* already prepared for reset */
 555	if (test_bit(ICE_PREPARED_FOR_RESET, pf->state))
 556		return;
 557
 558	ice_unplug_aux_dev(pf);
 559
 560	/* Notify VFs of impending reset */
 561	if (ice_check_sq_alive(hw, &hw->mailboxq))
 562		ice_vc_notify_reset(pf);
 563
 564	/* Disable VFs until reset is completed */
 565	mutex_lock(&pf->vfs.table_lock);
 566	ice_for_each_vf(pf, bkt, vf)
 567		ice_set_vf_state_qs_dis(vf);
 568	mutex_unlock(&pf->vfs.table_lock);
 569
 570	if (ice_is_eswitch_mode_switchdev(pf)) {
 571		if (reset_type != ICE_RESET_PFR)
 572			ice_clear_sw_switch_recipes(pf);
 573	}
 574
 575	/* release ADQ specific HW and SW resources */
 576	vsi = ice_get_main_vsi(pf);
 577	if (!vsi)
 578		goto skip;
 579
 580	/* to be on safe side, reset orig_rss_size so that normal flow
 581	 * of deciding rss_size can take precedence
 582	 */
 583	vsi->orig_rss_size = 0;
 584
 585	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
 586		if (reset_type == ICE_RESET_PFR) {
 587			vsi->old_ena_tc = vsi->all_enatc;
 588			vsi->old_numtc = vsi->all_numtc;
 589		} else {
 590			ice_remove_q_channels(vsi, true);
 591
 592			/* for other reset type, do not support channel rebuild
 593			 * hence reset needed info
 594			 */
 595			vsi->old_ena_tc = 0;
 596			vsi->all_enatc = 0;
 597			vsi->old_numtc = 0;
 598			vsi->all_numtc = 0;
 599			vsi->req_txq = 0;
 600			vsi->req_rxq = 0;
 601			clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
 602			memset(&vsi->mqprio_qopt, 0, sizeof(vsi->mqprio_qopt));
 603		}
 604	}
 605skip:
 606
 607	/* clear SW filtering DB */
 608	ice_clear_hw_tbls(hw);
 609	/* disable the VSIs and their queues that are not already DOWN */
 610	ice_pf_dis_all_vsi(pf, false);
 611
 612	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
 613		ice_ptp_prepare_for_reset(pf);
 614
 615	if (ice_is_feature_supported(pf, ICE_F_GNSS))
 616		ice_gnss_exit(pf);
 617
 618	if (hw->port_info)
 619		ice_sched_clear_port(hw->port_info);
 620
 621	ice_shutdown_all_ctrlq(hw);
 622
 623	set_bit(ICE_PREPARED_FOR_RESET, pf->state);
 624}
 625
 626/**
 627 * ice_do_reset - Initiate one of many types of resets
 628 * @pf: board private structure
 629 * @reset_type: reset type requested before this function was called.
 630 */
 631static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
 632{
 633	struct device *dev = ice_pf_to_dev(pf);
 634	struct ice_hw *hw = &pf->hw;
 635
 636	dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
 637
 
 
 
 
 
 638	ice_prepare_for_reset(pf, reset_type);
 639
 640	/* trigger the reset */
 641	if (ice_reset(hw, reset_type)) {
 642		dev_err(dev, "reset %d failed\n", reset_type);
 643		set_bit(ICE_RESET_FAILED, pf->state);
 644		clear_bit(ICE_RESET_OICR_RECV, pf->state);
 645		clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
 646		clear_bit(ICE_PFR_REQ, pf->state);
 647		clear_bit(ICE_CORER_REQ, pf->state);
 648		clear_bit(ICE_GLOBR_REQ, pf->state);
 649		wake_up(&pf->reset_wait_queue);
 650		return;
 651	}
 652
 653	/* PFR is a bit of a special case because it doesn't result in an OICR
 654	 * interrupt. So for PFR, rebuild after the reset and clear the reset-
 655	 * associated state bits.
 656	 */
 657	if (reset_type == ICE_RESET_PFR) {
 658		pf->pfr_count++;
 659		ice_rebuild(pf, reset_type);
 660		clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
 661		clear_bit(ICE_PFR_REQ, pf->state);
 662		wake_up(&pf->reset_wait_queue);
 663		ice_reset_all_vfs(pf);
 664	}
 665}
 666
 667/**
 668 * ice_reset_subtask - Set up for resetting the device and driver
 669 * @pf: board private structure
 670 */
 671static void ice_reset_subtask(struct ice_pf *pf)
 672{
 673	enum ice_reset_req reset_type = ICE_RESET_INVAL;
 674
 675	/* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
 676	 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type
 677	 * of reset is pending and sets bits in pf->state indicating the reset
 678	 * type and ICE_RESET_OICR_RECV. So, if the latter bit is set
 679	 * prepare for pending reset if not already (for PF software-initiated
 680	 * global resets the software should already be prepared for it as
 681	 * indicated by ICE_PREPARED_FOR_RESET; for global resets initiated
 682	 * by firmware or software on other PFs, that bit is not set so prepare
 683	 * for the reset now), poll for reset done, rebuild and return.
 684	 */
 685	if (test_bit(ICE_RESET_OICR_RECV, pf->state)) {
 686		/* Perform the largest reset requested */
 687		if (test_and_clear_bit(ICE_CORER_RECV, pf->state))
 688			reset_type = ICE_RESET_CORER;
 689		if (test_and_clear_bit(ICE_GLOBR_RECV, pf->state))
 690			reset_type = ICE_RESET_GLOBR;
 691		if (test_and_clear_bit(ICE_EMPR_RECV, pf->state))
 692			reset_type = ICE_RESET_EMPR;
 693		/* return if no valid reset type requested */
 694		if (reset_type == ICE_RESET_INVAL)
 695			return;
 696		ice_prepare_for_reset(pf, reset_type);
 697
 698		/* make sure we are ready to rebuild */
 699		if (ice_check_reset(&pf->hw)) {
 700			set_bit(ICE_RESET_FAILED, pf->state);
 701		} else {
 702			/* done with reset. start rebuild */
 703			pf->hw.reset_ongoing = false;
 704			ice_rebuild(pf, reset_type);
 705			/* clear bit to resume normal operations, but
 706			 * ICE_NEEDS_RESTART bit is set in case rebuild failed
 707			 */
 708			clear_bit(ICE_RESET_OICR_RECV, pf->state);
 709			clear_bit(ICE_PREPARED_FOR_RESET, pf->state);
 710			clear_bit(ICE_PFR_REQ, pf->state);
 711			clear_bit(ICE_CORER_REQ, pf->state);
 712			clear_bit(ICE_GLOBR_REQ, pf->state);
 713			wake_up(&pf->reset_wait_queue);
 714			ice_reset_all_vfs(pf);
 715		}
 716
 717		return;
 718	}
 719
 720	/* No pending resets to finish processing. Check for new resets */
 721	if (test_bit(ICE_PFR_REQ, pf->state))
 722		reset_type = ICE_RESET_PFR;
 
 
 
 
 
 723	if (test_bit(ICE_CORER_REQ, pf->state))
 724		reset_type = ICE_RESET_CORER;
 725	if (test_bit(ICE_GLOBR_REQ, pf->state))
 726		reset_type = ICE_RESET_GLOBR;
 727	/* If no valid reset type requested just return */
 728	if (reset_type == ICE_RESET_INVAL)
 729		return;
 730
 731	/* reset if not already down or busy */
 732	if (!test_bit(ICE_DOWN, pf->state) &&
 733	    !test_bit(ICE_CFG_BUSY, pf->state)) {
 734		ice_do_reset(pf, reset_type);
 735	}
 736}
 737
 738/**
 739 * ice_print_topo_conflict - print topology conflict message
 740 * @vsi: the VSI whose topology status is being checked
 741 */
 742static void ice_print_topo_conflict(struct ice_vsi *vsi)
 743{
 744	switch (vsi->port_info->phy.link_info.topo_media_conflict) {
 745	case ICE_AQ_LINK_TOPO_CONFLICT:
 746	case ICE_AQ_LINK_MEDIA_CONFLICT:
 747	case ICE_AQ_LINK_TOPO_UNREACH_PRT:
 748	case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT:
 749	case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA:
 750		netdev_info(vsi->netdev, "Potential misconfiguration of the Ethernet port detected. If it was not intended, please use the Intel (R) Ethernet Port Configuration Tool to address the issue.\n");
 751		break;
 752	case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA:
 753		if (test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, vsi->back->flags))
 754			netdev_warn(vsi->netdev, "An unsupported module type was detected. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules\n");
 755		else
 756			netdev_err(vsi->netdev, "Rx/Tx is disabled on this device because an unsupported module type was detected. Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
 757		break;
 758	default:
 759		break;
 760	}
 761}
 762
 763/**
 764 * ice_print_link_msg - print link up or down message
 765 * @vsi: the VSI whose link status is being queried
 766 * @isup: boolean for if the link is now up or down
 767 */
 768void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
 769{
 770	struct ice_aqc_get_phy_caps_data *caps;
 771	const char *an_advertised;
 772	const char *fec_req;
 773	const char *speed;
 774	const char *fec;
 775	const char *fc;
 776	const char *an;
 777	int status;
 778
 779	if (!vsi)
 780		return;
 781
 782	if (vsi->current_isup == isup)
 783		return;
 784
 785	vsi->current_isup = isup;
 786
 787	if (!isup) {
 788		netdev_info(vsi->netdev, "NIC Link is Down\n");
 789		return;
 790	}
 791
 792	switch (vsi->port_info->phy.link_info.link_speed) {
 793	case ICE_AQ_LINK_SPEED_100GB:
 794		speed = "100 G";
 795		break;
 796	case ICE_AQ_LINK_SPEED_50GB:
 797		speed = "50 G";
 798		break;
 799	case ICE_AQ_LINK_SPEED_40GB:
 800		speed = "40 G";
 801		break;
 802	case ICE_AQ_LINK_SPEED_25GB:
 803		speed = "25 G";
 804		break;
 805	case ICE_AQ_LINK_SPEED_20GB:
 806		speed = "20 G";
 807		break;
 808	case ICE_AQ_LINK_SPEED_10GB:
 809		speed = "10 G";
 810		break;
 811	case ICE_AQ_LINK_SPEED_5GB:
 812		speed = "5 G";
 813		break;
 814	case ICE_AQ_LINK_SPEED_2500MB:
 815		speed = "2.5 G";
 816		break;
 817	case ICE_AQ_LINK_SPEED_1000MB:
 818		speed = "1 G";
 819		break;
 820	case ICE_AQ_LINK_SPEED_100MB:
 821		speed = "100 M";
 822		break;
 823	default:
 824		speed = "Unknown ";
 825		break;
 826	}
 827
 828	switch (vsi->port_info->fc.current_mode) {
 829	case ICE_FC_FULL:
 830		fc = "Rx/Tx";
 831		break;
 832	case ICE_FC_TX_PAUSE:
 833		fc = "Tx";
 834		break;
 835	case ICE_FC_RX_PAUSE:
 836		fc = "Rx";
 837		break;
 838	case ICE_FC_NONE:
 839		fc = "None";
 840		break;
 841	default:
 842		fc = "Unknown";
 843		break;
 844	}
 845
 846	/* Get FEC mode based on negotiated link info */
 847	switch (vsi->port_info->phy.link_info.fec_info) {
 848	case ICE_AQ_LINK_25G_RS_528_FEC_EN:
 849	case ICE_AQ_LINK_25G_RS_544_FEC_EN:
 850		fec = "RS-FEC";
 851		break;
 852	case ICE_AQ_LINK_25G_KR_FEC_EN:
 853		fec = "FC-FEC/BASE-R";
 854		break;
 855	default:
 856		fec = "NONE";
 857		break;
 858	}
 859
 860	/* check if autoneg completed, might be false due to not supported */
 861	if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)
 862		an = "True";
 863	else
 864		an = "False";
 865
 866	/* Get FEC mode requested based on PHY caps last SW configuration */
 867	caps = kzalloc(sizeof(*caps), GFP_KERNEL);
 868	if (!caps) {
 869		fec_req = "Unknown";
 870		an_advertised = "Unknown";
 871		goto done;
 872	}
 873
 874	status = ice_aq_get_phy_caps(vsi->port_info, false,
 875				     ICE_AQC_REPORT_ACTIVE_CFG, caps, NULL);
 876	if (status)
 877		netdev_info(vsi->netdev, "Get phy capability failed.\n");
 878
 879	an_advertised = ice_is_phy_caps_an_enabled(caps) ? "On" : "Off";
 880
 881	if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ ||
 882	    caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ)
 883		fec_req = "RS-FEC";
 884	else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ ||
 885		 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ)
 886		fec_req = "FC-FEC/BASE-R";
 887	else
 888		fec_req = "NONE";
 889
 890	kfree(caps);
 891
 892done:
 893	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",
 894		    speed, fec_req, fec, an_advertised, an, fc);
 895	ice_print_topo_conflict(vsi);
 896}
 897
 898/**
 899 * ice_vsi_link_event - update the VSI's netdev
 900 * @vsi: the VSI on which the link event occurred
 901 * @link_up: whether or not the VSI needs to be set up or down
 902 */
 903static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
 904{
 905	if (!vsi)
 906		return;
 907
 908	if (test_bit(ICE_VSI_DOWN, vsi->state) || !vsi->netdev)
 909		return;
 910
 911	if (vsi->type == ICE_VSI_PF) {
 912		if (link_up == netif_carrier_ok(vsi->netdev))
 913			return;
 914
 915		if (link_up) {
 916			netif_carrier_on(vsi->netdev);
 917			netif_tx_wake_all_queues(vsi->netdev);
 918		} else {
 919			netif_carrier_off(vsi->netdev);
 920			netif_tx_stop_all_queues(vsi->netdev);
 921		}
 922	}
 923}
 924
 925/**
 926 * ice_set_dflt_mib - send a default config MIB to the FW
 927 * @pf: private PF struct
 928 *
 929 * This function sends a default configuration MIB to the FW.
 930 *
 931 * If this function errors out at any point, the driver is still able to
 932 * function.  The main impact is that LFC may not operate as expected.
 933 * Therefore an error state in this function should be treated with a DBG
 934 * message and continue on with driver rebuild/reenable.
 935 */
 936static void ice_set_dflt_mib(struct ice_pf *pf)
 937{
 938	struct device *dev = ice_pf_to_dev(pf);
 939	u8 mib_type, *buf, *lldpmib = NULL;
 940	u16 len, typelen, offset = 0;
 941	struct ice_lldp_org_tlv *tlv;
 942	struct ice_hw *hw = &pf->hw;
 943	u32 ouisubtype;
 944
 945	mib_type = SET_LOCAL_MIB_TYPE_LOCAL_MIB;
 946	lldpmib = kzalloc(ICE_LLDPDU_SIZE, GFP_KERNEL);
 947	if (!lldpmib) {
 948		dev_dbg(dev, "%s Failed to allocate MIB memory\n",
 949			__func__);
 950		return;
 951	}
 952
 953	/* Add ETS CFG TLV */
 954	tlv = (struct ice_lldp_org_tlv *)lldpmib;
 955	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
 956		   ICE_IEEE_ETS_TLV_LEN);
 957	tlv->typelen = htons(typelen);
 958	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
 959		      ICE_IEEE_SUBTYPE_ETS_CFG);
 960	tlv->ouisubtype = htonl(ouisubtype);
 961
 962	buf = tlv->tlvinfo;
 963	buf[0] = 0;
 964
 965	/* ETS CFG all UPs map to TC 0. Next 4 (1 - 4) Octets = 0.
 966	 * Octets 5 - 12 are BW values, set octet 5 to 100% BW.
 967	 * Octets 13 - 20 are TSA values - leave as zeros
 968	 */
 969	buf[5] = 0x64;
 970	len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
 971	offset += len + 2;
 972	tlv = (struct ice_lldp_org_tlv *)
 973		((char *)tlv + sizeof(tlv->typelen) + len);
 974
 975	/* Add ETS REC TLV */
 976	buf = tlv->tlvinfo;
 977	tlv->typelen = htons(typelen);
 978
 979	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
 980		      ICE_IEEE_SUBTYPE_ETS_REC);
 981	tlv->ouisubtype = htonl(ouisubtype);
 982
 983	/* First octet of buf is reserved
 984	 * Octets 1 - 4 map UP to TC - all UPs map to zero
 985	 * Octets 5 - 12 are BW values - set TC 0 to 100%.
 986	 * Octets 13 - 20 are TSA value - leave as zeros
 987	 */
 988	buf[5] = 0x64;
 989	offset += len + 2;
 990	tlv = (struct ice_lldp_org_tlv *)
 991		((char *)tlv + sizeof(tlv->typelen) + len);
 992
 993	/* Add PFC CFG TLV */
 994	typelen = ((ICE_TLV_TYPE_ORG << ICE_LLDP_TLV_TYPE_S) |
 995		   ICE_IEEE_PFC_TLV_LEN);
 996	tlv->typelen = htons(typelen);
 997
 998	ouisubtype = ((ICE_IEEE_8021QAZ_OUI << ICE_LLDP_TLV_OUI_S) |
 999		      ICE_IEEE_SUBTYPE_PFC_CFG);
1000	tlv->ouisubtype = htonl(ouisubtype);
1001
1002	/* Octet 1 left as all zeros - PFC disabled */
1003	buf[0] = 0x08;
1004	len = (typelen & ICE_LLDP_TLV_LEN_M) >> ICE_LLDP_TLV_LEN_S;
1005	offset += len + 2;
1006
1007	if (ice_aq_set_lldp_mib(hw, mib_type, (void *)lldpmib, offset, NULL))
1008		dev_dbg(dev, "%s Failed to set default LLDP MIB\n", __func__);
1009
1010	kfree(lldpmib);
1011}
1012
1013/**
1014 * ice_check_phy_fw_load - check if PHY FW load failed
1015 * @pf: pointer to PF struct
1016 * @link_cfg_err: bitmap from the link info structure
1017 *
1018 * check if external PHY FW load failed and print an error message if it did
1019 */
1020static void ice_check_phy_fw_load(struct ice_pf *pf, u8 link_cfg_err)
1021{
1022	if (!(link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE)) {
1023		clear_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
1024		return;
1025	}
1026
1027	if (test_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags))
1028		return;
1029
1030	if (link_cfg_err & ICE_AQ_LINK_EXTERNAL_PHY_LOAD_FAILURE) {
1031		dev_err(ice_pf_to_dev(pf), "Device failed to load the FW for the external PHY. Please download and install the latest NVM for your device and try again\n");
1032		set_bit(ICE_FLAG_PHY_FW_LOAD_FAILED, pf->flags);
1033	}
1034}
1035
1036/**
1037 * ice_check_module_power
1038 * @pf: pointer to PF struct
1039 * @link_cfg_err: bitmap from the link info structure
1040 *
1041 * check module power level returned by a previous call to aq_get_link_info
1042 * and print error messages if module power level is not supported
1043 */
1044static void ice_check_module_power(struct ice_pf *pf, u8 link_cfg_err)
1045{
1046	/* if module power level is supported, clear the flag */
1047	if (!(link_cfg_err & (ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT |
1048			      ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED))) {
1049		clear_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1050		return;
1051	}
1052
1053	/* if ICE_FLAG_MOD_POWER_UNSUPPORTED was previously set and the
1054	 * above block didn't clear this bit, there's nothing to do
1055	 */
1056	if (test_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags))
1057		return;
1058
1059	if (link_cfg_err & ICE_AQ_LINK_INVAL_MAX_POWER_LIMIT) {
1060		dev_err(ice_pf_to_dev(pf), "The installed module is incompatible with the device's NVM image. Cannot start link\n");
1061		set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1062	} else if (link_cfg_err & ICE_AQ_LINK_MODULE_POWER_UNSUPPORTED) {
1063		dev_err(ice_pf_to_dev(pf), "The module's power requirements exceed the device's power supply. Cannot start link\n");
1064		set_bit(ICE_FLAG_MOD_POWER_UNSUPPORTED, pf->flags);
1065	}
1066}
1067
1068/**
1069 * ice_check_link_cfg_err - check if link configuration failed
1070 * @pf: pointer to the PF struct
1071 * @link_cfg_err: bitmap from the link info structure
1072 *
1073 * print if any link configuration failure happens due to the value in the
1074 * link_cfg_err parameter in the link info structure
1075 */
1076static void ice_check_link_cfg_err(struct ice_pf *pf, u8 link_cfg_err)
1077{
1078	ice_check_module_power(pf, link_cfg_err);
1079	ice_check_phy_fw_load(pf, link_cfg_err);
1080}
1081
1082/**
1083 * ice_link_event - process the link event
1084 * @pf: PF that the link event is associated with
1085 * @pi: port_info for the port that the link event is associated with
1086 * @link_up: true if the physical link is up and false if it is down
1087 * @link_speed: current link speed received from the link event
1088 *
1089 * Returns 0 on success and negative on failure
1090 */
1091static int
1092ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up,
1093	       u16 link_speed)
1094{
1095	struct device *dev = ice_pf_to_dev(pf);
1096	struct ice_phy_info *phy_info;
1097	struct ice_vsi *vsi;
1098	u16 old_link_speed;
1099	bool old_link;
1100	int status;
1101
1102	phy_info = &pi->phy;
1103	phy_info->link_info_old = phy_info->link_info;
1104
1105	old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
1106	old_link_speed = phy_info->link_info_old.link_speed;
1107
1108	/* update the link info structures and re-enable link events,
1109	 * don't bail on failure due to other book keeping needed
1110	 */
1111	status = ice_update_link_info(pi);
1112	if (status)
1113		dev_dbg(dev, "Failed to update link status on port %d, err %d aq_err %s\n",
1114			pi->lport, status,
1115			ice_aq_str(pi->hw->adminq.sq_last_status));
1116
1117	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
1118
1119	/* Check if the link state is up after updating link info, and treat
1120	 * this event as an UP event since the link is actually UP now.
1121	 */
1122	if (phy_info->link_info.link_info & ICE_AQ_LINK_UP)
1123		link_up = true;
1124
1125	vsi = ice_get_main_vsi(pf);
1126	if (!vsi || !vsi->port_info)
1127		return -EINVAL;
1128
1129	/* turn off PHY if media was removed */
1130	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) &&
1131	    !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) {
1132		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
1133		ice_set_link(vsi, false);
1134	}
1135
1136	/* if the old link up/down and speed is the same as the new */
1137	if (link_up == old_link && link_speed == old_link_speed)
1138		return 0;
1139
1140	ice_ptp_link_change(pf, pf->hw.pf_id, link_up);
1141
1142	if (ice_is_dcb_active(pf)) {
1143		if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
1144			ice_dcb_rebuild(pf);
1145	} else {
1146		if (link_up)
1147			ice_set_dflt_mib(pf);
1148	}
1149	ice_vsi_link_event(vsi, link_up);
1150	ice_print_link_msg(vsi, link_up);
1151
1152	ice_vc_notify_link_state(pf);
1153
1154	return 0;
1155}
1156
1157/**
1158 * ice_watchdog_subtask - periodic tasks not using event driven scheduling
1159 * @pf: board private structure
1160 */
1161static void ice_watchdog_subtask(struct ice_pf *pf)
1162{
1163	int i;
1164
1165	/* if interface is down do nothing */
1166	if (test_bit(ICE_DOWN, pf->state) ||
1167	    test_bit(ICE_CFG_BUSY, pf->state))
1168		return;
1169
1170	/* make sure we don't do these things too often */
1171	if (time_before(jiffies,
1172			pf->serv_tmr_prev + pf->serv_tmr_period))
1173		return;
1174
1175	pf->serv_tmr_prev = jiffies;
1176
1177	/* Update the stats for active netdevs so the network stack
1178	 * can look at updated numbers whenever it cares to
1179	 */
1180	ice_update_pf_stats(pf);
1181	ice_for_each_vsi(pf, i)
1182		if (pf->vsi[i] && pf->vsi[i]->netdev)
1183			ice_update_vsi_stats(pf->vsi[i]);
1184}
1185
1186/**
1187 * ice_init_link_events - enable/initialize link events
1188 * @pi: pointer to the port_info instance
1189 *
1190 * Returns -EIO on failure, 0 on success
1191 */
1192static int ice_init_link_events(struct ice_port_info *pi)
1193{
1194	u16 mask;
1195
1196	mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
1197		       ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL |
1198		       ICE_AQ_LINK_EVENT_PHY_FW_LOAD_FAIL));
1199
1200	if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
1201		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to set link event mask for port %d\n",
1202			pi->lport);
1203		return -EIO;
1204	}
1205
1206	if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
1207		dev_dbg(ice_hw_to_dev(pi->hw), "Failed to enable link events for port %d\n",
1208			pi->lport);
1209		return -EIO;
1210	}
1211
1212	return 0;
1213}
1214
1215/**
1216 * ice_handle_link_event - handle link event via ARQ
1217 * @pf: PF that the link event is associated with
1218 * @event: event structure containing link status info
1219 */
1220static int
1221ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event)
1222{
1223	struct ice_aqc_get_link_status_data *link_data;
1224	struct ice_port_info *port_info;
1225	int status;
1226
1227	link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf;
1228	port_info = pf->hw.port_info;
1229	if (!port_info)
1230		return -EINVAL;
1231
1232	status = ice_link_event(pf, port_info,
1233				!!(link_data->link_info & ICE_AQ_LINK_UP),
1234				le16_to_cpu(link_data->link_speed));
1235	if (status)
1236		dev_dbg(ice_pf_to_dev(pf), "Could not process link event, error %d\n",
1237			status);
1238
1239	return status;
1240}
1241
1242enum ice_aq_task_state {
1243	ICE_AQ_TASK_WAITING = 0,
1244	ICE_AQ_TASK_COMPLETE,
1245	ICE_AQ_TASK_CANCELED,
1246};
 
 
 
 
 
 
 
 
 
 
1247
1248struct ice_aq_task {
1249	struct hlist_node entry;
1250
1251	u16 opcode;
1252	struct ice_rq_event_info *event;
1253	enum ice_aq_task_state state;
1254};
 
 
1255
1256/**
1257 * ice_aq_wait_for_event - Wait for an AdminQ event from firmware
1258 * @pf: pointer to the PF private structure
 
1259 * @opcode: the opcode to wait for
1260 * @timeout: how long to wait, in jiffies
1261 * @event: storage for the event info
1262 *
1263 * Waits for a specific AdminQ completion event on the ARQ for a given PF. The
1264 * current thread will be put to sleep until the specified event occurs or
1265 * until the given timeout is reached.
 
 
1266 *
1267 * To obtain only the descriptor contents, pass an event without an allocated
1268 * msg_buf. If the complete data buffer is desired, allocate the
1269 * event->msg_buf with enough space ahead of time.
1270 *
1271 * Returns: zero on success, or a negative error code on failure.
1272 */
1273int ice_aq_wait_for_event(struct ice_pf *pf, u16 opcode, unsigned long timeout,
1274			  struct ice_rq_event_info *event)
1275{
1276	struct device *dev = ice_pf_to_dev(pf);
1277	struct ice_aq_task *task;
1278	unsigned long start;
1279	long ret;
1280	int err;
1281
1282	task = kzalloc(sizeof(*task), GFP_KERNEL);
1283	if (!task)
1284		return -ENOMEM;
1285
1286	INIT_HLIST_NODE(&task->entry);
1287	task->opcode = opcode;
1288	task->event = event;
1289	task->state = ICE_AQ_TASK_WAITING;
1290
1291	spin_lock_bh(&pf->aq_wait_lock);
1292	hlist_add_head(&task->entry, &pf->aq_wait_list);
1293	spin_unlock_bh(&pf->aq_wait_lock);
 
1294
1295	start = jiffies;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1296
1297	ret = wait_event_interruptible_timeout(pf->aq_wait_queue, task->state,
 
1298					       timeout);
1299	switch (task->state) {
 
 
 
 
1300	case ICE_AQ_TASK_WAITING:
1301		err = ret < 0 ? ret : -ETIMEDOUT;
1302		break;
1303	case ICE_AQ_TASK_CANCELED:
1304		err = ret < 0 ? ret : -ECANCELED;
1305		break;
1306	case ICE_AQ_TASK_COMPLETE:
1307		err = ret < 0 ? ret : 0;
1308		break;
1309	default:
1310		WARN(1, "Unexpected AdminQ wait task state %u", task->state);
1311		err = -EINVAL;
1312		break;
1313	}
1314
1315	dev_dbg(dev, "Waited %u msecs (max %u msecs) for firmware response to op 0x%04x\n",
1316		jiffies_to_msecs(jiffies - start),
1317		jiffies_to_msecs(timeout),
1318		opcode);
1319
1320	spin_lock_bh(&pf->aq_wait_lock);
1321	hlist_del(&task->entry);
1322	spin_unlock_bh(&pf->aq_wait_lock);
1323	kfree(task);
1324
1325	return err;
1326}
1327
1328/**
1329 * ice_aq_check_events - Check if any thread is waiting for an AdminQ event
1330 * @pf: pointer to the PF private structure
1331 * @opcode: the opcode of the event
1332 * @event: the event to check
1333 *
1334 * Loops over the current list of pending threads waiting for an AdminQ event.
1335 * For each matching task, copy the contents of the event into the task
1336 * structure and wake up the thread.
1337 *
1338 * If multiple threads wait for the same opcode, they will all be woken up.
1339 *
1340 * Note that event->msg_buf will only be duplicated if the event has a buffer
1341 * with enough space already allocated. Otherwise, only the descriptor and
1342 * message length will be copied.
1343 *
1344 * Returns: true if an event was found, false otherwise
1345 */
1346static void ice_aq_check_events(struct ice_pf *pf, u16 opcode,
1347				struct ice_rq_event_info *event)
1348{
 
1349	struct ice_aq_task *task;
1350	bool found = false;
1351
1352	spin_lock_bh(&pf->aq_wait_lock);
1353	hlist_for_each_entry(task, &pf->aq_wait_list, entry) {
1354		if (task->state || task->opcode != opcode)
 
 
1355			continue;
1356
1357		memcpy(&task->event->desc, &event->desc, sizeof(event->desc));
1358		task->event->msg_len = event->msg_len;
 
1359
1360		/* Only copy the data buffer if a destination was set */
1361		if (task->event->msg_buf &&
1362		    task->event->buf_len > event->buf_len) {
1363			memcpy(task->event->msg_buf, event->msg_buf,
1364			       event->buf_len);
1365			task->event->buf_len = event->buf_len;
1366		}
1367
1368		task->state = ICE_AQ_TASK_COMPLETE;
1369		found = true;
1370	}
1371	spin_unlock_bh(&pf->aq_wait_lock);
1372
1373	if (found)
1374		wake_up(&pf->aq_wait_queue);
1375}
1376
1377/**
1378 * ice_aq_cancel_waiting_tasks - Immediately cancel all waiting tasks
1379 * @pf: the PF private structure
1380 *
1381 * Set all waiting tasks to ICE_AQ_TASK_CANCELED, and wake up their threads.
1382 * This will then cause ice_aq_wait_for_event to exit with -ECANCELED.
1383 */
1384static void ice_aq_cancel_waiting_tasks(struct ice_pf *pf)
1385{
1386	struct ice_aq_task *task;
1387
1388	spin_lock_bh(&pf->aq_wait_lock);
1389	hlist_for_each_entry(task, &pf->aq_wait_list, entry)
1390		task->state = ICE_AQ_TASK_CANCELED;
1391	spin_unlock_bh(&pf->aq_wait_lock);
1392
1393	wake_up(&pf->aq_wait_queue);
1394}
1395
 
 
1396/**
1397 * __ice_clean_ctrlq - helper function to clean controlq rings
1398 * @pf: ptr to struct ice_pf
1399 * @q_type: specific Control queue type
1400 */
1401static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
1402{
1403	struct device *dev = ice_pf_to_dev(pf);
1404	struct ice_rq_event_info event;
1405	struct ice_hw *hw = &pf->hw;
1406	struct ice_ctl_q_info *cq;
1407	u16 pending, i = 0;
1408	const char *qtype;
1409	u32 oldval, val;
1410
1411	/* Do not clean control queue if/when PF reset fails */
1412	if (test_bit(ICE_RESET_FAILED, pf->state))
1413		return 0;
1414
1415	switch (q_type) {
1416	case ICE_CTL_Q_ADMIN:
1417		cq = &hw->adminq;
1418		qtype = "Admin";
1419		break;
1420	case ICE_CTL_Q_SB:
1421		cq = &hw->sbq;
1422		qtype = "Sideband";
1423		break;
1424	case ICE_CTL_Q_MAILBOX:
1425		cq = &hw->mailboxq;
1426		qtype = "Mailbox";
1427		/* we are going to try to detect a malicious VF, so set the
1428		 * state to begin detection
1429		 */
1430		hw->mbx_snapshot.mbx_buf.state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT;
1431		break;
1432	default:
1433		dev_warn(dev, "Unknown control queue type 0x%x\n", q_type);
1434		return 0;
1435	}
1436
1437	/* check for error indications - PF_xx_AxQLEN register layout for
1438	 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
1439	 */
1440	val = rd32(hw, cq->rq.len);
1441	if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1442		   PF_FW_ARQLEN_ARQCRIT_M)) {
1443		oldval = val;
1444		if (val & PF_FW_ARQLEN_ARQVFE_M)
1445			dev_dbg(dev, "%s Receive Queue VF Error detected\n",
1446				qtype);
1447		if (val & PF_FW_ARQLEN_ARQOVFL_M) {
1448			dev_dbg(dev, "%s Receive Queue Overflow Error detected\n",
1449				qtype);
1450		}
1451		if (val & PF_FW_ARQLEN_ARQCRIT_M)
1452			dev_dbg(dev, "%s Receive Queue Critical Error detected\n",
1453				qtype);
1454		val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
1455			 PF_FW_ARQLEN_ARQCRIT_M);
1456		if (oldval != val)
1457			wr32(hw, cq->rq.len, val);
1458	}
1459
1460	val = rd32(hw, cq->sq.len);
1461	if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1462		   PF_FW_ATQLEN_ATQCRIT_M)) {
1463		oldval = val;
1464		if (val & PF_FW_ATQLEN_ATQVFE_M)
1465			dev_dbg(dev, "%s Send Queue VF Error detected\n",
1466				qtype);
1467		if (val & PF_FW_ATQLEN_ATQOVFL_M) {
1468			dev_dbg(dev, "%s Send Queue Overflow Error detected\n",
1469				qtype);
1470		}
1471		if (val & PF_FW_ATQLEN_ATQCRIT_M)
1472			dev_dbg(dev, "%s Send Queue Critical Error detected\n",
1473				qtype);
1474		val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
1475			 PF_FW_ATQLEN_ATQCRIT_M);
1476		if (oldval != val)
1477			wr32(hw, cq->sq.len, val);
1478	}
1479
1480	event.buf_len = cq->rq_buf_size;
1481	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
1482	if (!event.msg_buf)
1483		return 0;
1484
1485	do {
 
1486		u16 opcode;
1487		int ret;
1488
1489		ret = ice_clean_rq_elem(hw, cq, &event, &pending);
1490		if (ret == -EALREADY)
1491			break;
1492		if (ret) {
1493			dev_err(dev, "%s Receive Queue event error %d\n", qtype,
1494				ret);
1495			break;
1496		}
1497
1498		opcode = le16_to_cpu(event.desc.opcode);
1499
1500		/* Notify any thread that might be waiting for this event */
1501		ice_aq_check_events(pf, opcode, &event);
1502
1503		switch (opcode) {
1504		case ice_aqc_opc_get_link_status:
1505			if (ice_handle_link_event(pf, &event))
1506				dev_err(dev, "Could not handle link event\n");
1507			break;
1508		case ice_aqc_opc_event_lan_overflow:
1509			ice_vf_lan_overflow_event(pf, &event);
1510			break;
1511		case ice_mbx_opc_send_msg_to_pf:
1512			if (!ice_is_malicious_vf(pf, &event, i, pending))
1513				ice_vc_process_vf_msg(pf, &event);
 
 
 
 
1514			break;
1515		case ice_aqc_opc_fw_logging:
1516			ice_output_fw_log(hw, &event.desc, event.msg_buf);
1517			break;
1518		case ice_aqc_opc_lldp_set_mib_change:
1519			ice_dcb_process_lldp_set_mib_change(pf, &event);
1520			break;
1521		default:
1522			dev_dbg(dev, "%s Receive Queue unknown event 0x%04x ignored\n",
1523				qtype, opcode);
1524			break;
1525		}
1526	} while (pending && (i++ < ICE_DFLT_IRQ_WORK));
1527
1528	kfree(event.msg_buf);
1529
1530	return pending && (i == ICE_DFLT_IRQ_WORK);
1531}
1532
1533/**
1534 * ice_ctrlq_pending - check if there is a difference between ntc and ntu
1535 * @hw: pointer to hardware info
1536 * @cq: control queue information
1537 *
1538 * returns true if there are pending messages in a queue, false if there aren't
1539 */
1540static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
1541{
1542	u16 ntu;
1543
1544	ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
1545	return cq->rq.next_to_clean != ntu;
1546}
1547
1548/**
1549 * ice_clean_adminq_subtask - clean the AdminQ rings
1550 * @pf: board private structure
1551 */
1552static void ice_clean_adminq_subtask(struct ice_pf *pf)
1553{
1554	struct ice_hw *hw = &pf->hw;
1555
1556	if (!test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
1557		return;
1558
1559	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
1560		return;
1561
1562	clear_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
1563
1564	/* There might be a situation where new messages arrive to a control
1565	 * queue between processing the last message and clearing the
1566	 * EVENT_PENDING bit. So before exiting, check queue head again (using
1567	 * ice_ctrlq_pending) and process new messages if any.
1568	 */
1569	if (ice_ctrlq_pending(hw, &hw->adminq))
1570		__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
1571
1572	ice_flush(hw);
1573}
1574
1575/**
1576 * ice_clean_mailboxq_subtask - clean the MailboxQ rings
1577 * @pf: board private structure
1578 */
1579static void ice_clean_mailboxq_subtask(struct ice_pf *pf)
1580{
1581	struct ice_hw *hw = &pf->hw;
1582
1583	if (!test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state))
1584		return;
1585
1586	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX))
1587		return;
1588
1589	clear_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
1590
1591	if (ice_ctrlq_pending(hw, &hw->mailboxq))
1592		__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX);
1593
1594	ice_flush(hw);
1595}
1596
1597/**
1598 * ice_clean_sbq_subtask - clean the Sideband Queue rings
1599 * @pf: board private structure
1600 */
1601static void ice_clean_sbq_subtask(struct ice_pf *pf)
1602{
1603	struct ice_hw *hw = &pf->hw;
1604
1605	/* Nothing to do here if sideband queue is not supported */
1606	if (!ice_is_sbq_supported(hw)) {
1607		clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1608		return;
1609	}
1610
1611	if (!test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state))
1612		return;
1613
1614	if (__ice_clean_ctrlq(pf, ICE_CTL_Q_SB))
1615		return;
1616
1617	clear_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
1618
1619	if (ice_ctrlq_pending(hw, &hw->sbq))
1620		__ice_clean_ctrlq(pf, ICE_CTL_Q_SB);
1621
1622	ice_flush(hw);
1623}
1624
1625/**
1626 * ice_service_task_schedule - schedule the service task to wake up
1627 * @pf: board private structure
1628 *
1629 * If not already scheduled, this puts the task into the work queue.
1630 */
1631void ice_service_task_schedule(struct ice_pf *pf)
1632{
1633	if (!test_bit(ICE_SERVICE_DIS, pf->state) &&
1634	    !test_and_set_bit(ICE_SERVICE_SCHED, pf->state) &&
1635	    !test_bit(ICE_NEEDS_RESTART, pf->state))
1636		queue_work(ice_wq, &pf->serv_task);
1637}
1638
1639/**
1640 * ice_service_task_complete - finish up the service task
1641 * @pf: board private structure
1642 */
1643static void ice_service_task_complete(struct ice_pf *pf)
1644{
1645	WARN_ON(!test_bit(ICE_SERVICE_SCHED, pf->state));
1646
1647	/* force memory (pf->state) to sync before next service task */
1648	smp_mb__before_atomic();
1649	clear_bit(ICE_SERVICE_SCHED, pf->state);
1650}
1651
1652/**
1653 * ice_service_task_stop - stop service task and cancel works
1654 * @pf: board private structure
1655 *
1656 * Return 0 if the ICE_SERVICE_DIS bit was not already set,
1657 * 1 otherwise.
1658 */
1659static int ice_service_task_stop(struct ice_pf *pf)
1660{
1661	int ret;
1662
1663	ret = test_and_set_bit(ICE_SERVICE_DIS, pf->state);
1664
1665	if (pf->serv_tmr.function)
1666		del_timer_sync(&pf->serv_tmr);
1667	if (pf->serv_task.func)
1668		cancel_work_sync(&pf->serv_task);
1669
1670	clear_bit(ICE_SERVICE_SCHED, pf->state);
1671	return ret;
1672}
1673
1674/**
1675 * ice_service_task_restart - restart service task and schedule works
1676 * @pf: board private structure
1677 *
1678 * This function is needed for suspend and resume works (e.g WoL scenario)
1679 */
1680static void ice_service_task_restart(struct ice_pf *pf)
1681{
1682	clear_bit(ICE_SERVICE_DIS, pf->state);
1683	ice_service_task_schedule(pf);
1684}
1685
1686/**
1687 * ice_service_timer - timer callback to schedule service task
1688 * @t: pointer to timer_list
1689 */
1690static void ice_service_timer(struct timer_list *t)
1691{
1692	struct ice_pf *pf = from_timer(pf, t, serv_tmr);
1693
1694	mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
1695	ice_service_task_schedule(pf);
1696}
1697
1698/**
1699 * ice_handle_mdd_event - handle malicious driver detect event
1700 * @pf: pointer to the PF structure
1701 *
1702 * Called from service task. OICR interrupt handler indicates MDD event.
1703 * VF MDD logging is guarded by net_ratelimit. Additional PF and VF log
1704 * messages are wrapped by netif_msg_[rx|tx]_err. Since VF Rx MDD events
1705 * disable the queue, the PF can be configured to reset the VF using ethtool
1706 * private flag mdd-auto-reset-vf.
1707 */
1708static void ice_handle_mdd_event(struct ice_pf *pf)
1709{
1710	struct device *dev = ice_pf_to_dev(pf);
1711	struct ice_hw *hw = &pf->hw;
1712	struct ice_vf *vf;
1713	unsigned int bkt;
1714	u32 reg;
1715
1716	if (!test_and_clear_bit(ICE_MDD_EVENT_PENDING, pf->state)) {
1717		/* Since the VF MDD event logging is rate limited, check if
1718		 * there are pending MDD events.
1719		 */
1720		ice_print_vfs_mdd_events(pf);
1721		return;
1722	}
1723
1724	/* find what triggered an MDD event */
1725	reg = rd32(hw, GL_MDET_TX_PQM);
1726	if (reg & GL_MDET_TX_PQM_VALID_M) {
1727		u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >>
1728				GL_MDET_TX_PQM_PF_NUM_S;
1729		u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >>
1730				GL_MDET_TX_PQM_VF_NUM_S;
1731		u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >>
1732				GL_MDET_TX_PQM_MAL_TYPE_S;
1733		u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >>
1734				GL_MDET_TX_PQM_QNUM_S);
1735
1736		if (netif_msg_tx_err(pf))
1737			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1738				 event, queue, pf_num, vf_num);
1739		wr32(hw, GL_MDET_TX_PQM, 0xffffffff);
1740	}
1741
1742	reg = rd32(hw, GL_MDET_TX_TCLAN);
1743	if (reg & GL_MDET_TX_TCLAN_VALID_M) {
1744		u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >>
1745				GL_MDET_TX_TCLAN_PF_NUM_S;
1746		u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >>
1747				GL_MDET_TX_TCLAN_VF_NUM_S;
1748		u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >>
1749				GL_MDET_TX_TCLAN_MAL_TYPE_S;
1750		u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >>
1751				GL_MDET_TX_TCLAN_QNUM_S);
1752
1753		if (netif_msg_tx_err(pf))
1754			dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n",
1755				 event, queue, pf_num, vf_num);
1756		wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff);
1757	}
1758
1759	reg = rd32(hw, GL_MDET_RX);
1760	if (reg & GL_MDET_RX_VALID_M) {
1761		u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >>
1762				GL_MDET_RX_PF_NUM_S;
1763		u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >>
1764				GL_MDET_RX_VF_NUM_S;
1765		u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >>
1766				GL_MDET_RX_MAL_TYPE_S;
1767		u16 queue = ((reg & GL_MDET_RX_QNUM_M) >>
1768				GL_MDET_RX_QNUM_S);
1769
1770		if (netif_msg_rx_err(pf))
1771			dev_info(dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n",
1772				 event, queue, pf_num, vf_num);
1773		wr32(hw, GL_MDET_RX, 0xffffffff);
1774	}
1775
1776	/* check to see if this PF caused an MDD event */
1777	reg = rd32(hw, PF_MDET_TX_PQM);
1778	if (reg & PF_MDET_TX_PQM_VALID_M) {
1779		wr32(hw, PF_MDET_TX_PQM, 0xFFFF);
1780		if (netif_msg_tx_err(pf))
1781			dev_info(dev, "Malicious Driver Detection event TX_PQM detected on PF\n");
1782	}
1783
1784	reg = rd32(hw, PF_MDET_TX_TCLAN);
1785	if (reg & PF_MDET_TX_TCLAN_VALID_M) {
1786		wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF);
1787		if (netif_msg_tx_err(pf))
1788			dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on PF\n");
1789	}
1790
1791	reg = rd32(hw, PF_MDET_RX);
1792	if (reg & PF_MDET_RX_VALID_M) {
1793		wr32(hw, PF_MDET_RX, 0xFFFF);
1794		if (netif_msg_rx_err(pf))
1795			dev_info(dev, "Malicious Driver Detection event RX detected on PF\n");
1796	}
1797
1798	/* Check to see if one of the VFs caused an MDD event, and then
1799	 * increment counters and set print pending
1800	 */
1801	mutex_lock(&pf->vfs.table_lock);
1802	ice_for_each_vf(pf, bkt, vf) {
1803		reg = rd32(hw, VP_MDET_TX_PQM(vf->vf_id));
1804		if (reg & VP_MDET_TX_PQM_VALID_M) {
1805			wr32(hw, VP_MDET_TX_PQM(vf->vf_id), 0xFFFF);
1806			vf->mdd_tx_events.count++;
1807			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1808			if (netif_msg_tx_err(pf))
1809				dev_info(dev, "Malicious Driver Detection event TX_PQM detected on VF %d\n",
1810					 vf->vf_id);
1811		}
1812
1813		reg = rd32(hw, VP_MDET_TX_TCLAN(vf->vf_id));
1814		if (reg & VP_MDET_TX_TCLAN_VALID_M) {
1815			wr32(hw, VP_MDET_TX_TCLAN(vf->vf_id), 0xFFFF);
1816			vf->mdd_tx_events.count++;
1817			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1818			if (netif_msg_tx_err(pf))
1819				dev_info(dev, "Malicious Driver Detection event TX_TCLAN detected on VF %d\n",
1820					 vf->vf_id);
1821		}
1822
1823		reg = rd32(hw, VP_MDET_TX_TDPU(vf->vf_id));
1824		if (reg & VP_MDET_TX_TDPU_VALID_M) {
1825			wr32(hw, VP_MDET_TX_TDPU(vf->vf_id), 0xFFFF);
1826			vf->mdd_tx_events.count++;
1827			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1828			if (netif_msg_tx_err(pf))
1829				dev_info(dev, "Malicious Driver Detection event TX_TDPU detected on VF %d\n",
1830					 vf->vf_id);
1831		}
1832
1833		reg = rd32(hw, VP_MDET_RX(vf->vf_id));
1834		if (reg & VP_MDET_RX_VALID_M) {
1835			wr32(hw, VP_MDET_RX(vf->vf_id), 0xFFFF);
1836			vf->mdd_rx_events.count++;
1837			set_bit(ICE_MDD_VF_PRINT_PENDING, pf->state);
1838			if (netif_msg_rx_err(pf))
1839				dev_info(dev, "Malicious Driver Detection event RX detected on VF %d\n",
1840					 vf->vf_id);
1841
1842			/* Since the queue is disabled on VF Rx MDD events, the
1843			 * PF can be configured to reset the VF through ethtool
1844			 * private flag mdd-auto-reset-vf.
1845			 */
1846			if (test_bit(ICE_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
1847				/* VF MDD event counters will be cleared by
1848				 * reset, so print the event prior to reset.
1849				 */
1850				ice_print_vf_rx_mdd_event(vf);
1851				ice_reset_vf(vf, ICE_VF_RESET_LOCK);
1852			}
1853		}
1854	}
1855	mutex_unlock(&pf->vfs.table_lock);
1856
1857	ice_print_vfs_mdd_events(pf);
1858}
1859
1860/**
1861 * ice_force_phys_link_state - Force the physical link state
1862 * @vsi: VSI to force the physical link state to up/down
1863 * @link_up: true/false indicates to set the physical link to up/down
1864 *
1865 * Force the physical link state by getting the current PHY capabilities from
1866 * hardware and setting the PHY config based on the determined capabilities. If
1867 * link changes a link event will be triggered because both the Enable Automatic
1868 * Link Update and LESM Enable bits are set when setting the PHY capabilities.
1869 *
1870 * Returns 0 on success, negative on failure
1871 */
1872static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up)
1873{
1874	struct ice_aqc_get_phy_caps_data *pcaps;
1875	struct ice_aqc_set_phy_cfg_data *cfg;
1876	struct ice_port_info *pi;
1877	struct device *dev;
1878	int retcode;
1879
1880	if (!vsi || !vsi->port_info || !vsi->back)
1881		return -EINVAL;
1882	if (vsi->type != ICE_VSI_PF)
1883		return 0;
1884
1885	dev = ice_pf_to_dev(vsi->back);
1886
1887	pi = vsi->port_info;
1888
1889	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1890	if (!pcaps)
1891		return -ENOMEM;
1892
1893	retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
1894				      NULL);
1895	if (retcode) {
1896		dev_err(dev, "Failed to get phy capabilities, VSI %d error %d\n",
1897			vsi->vsi_num, retcode);
1898		retcode = -EIO;
1899		goto out;
1900	}
1901
1902	/* No change in link */
1903	if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) &&
1904	    link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP))
1905		goto out;
1906
1907	/* Use the current user PHY configuration. The current user PHY
1908	 * configuration is initialized during probe from PHY capabilities
1909	 * software mode, and updated on set PHY configuration.
1910	 */
1911	cfg = kmemdup(&pi->phy.curr_user_phy_cfg, sizeof(*cfg), GFP_KERNEL);
1912	if (!cfg) {
1913		retcode = -ENOMEM;
1914		goto out;
1915	}
1916
1917	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT;
1918	if (link_up)
1919		cfg->caps |= ICE_AQ_PHY_ENA_LINK;
1920	else
1921		cfg->caps &= ~ICE_AQ_PHY_ENA_LINK;
1922
1923	retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi, cfg, NULL);
1924	if (retcode) {
1925		dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
1926			vsi->vsi_num, retcode);
1927		retcode = -EIO;
1928	}
1929
1930	kfree(cfg);
1931out:
1932	kfree(pcaps);
1933	return retcode;
1934}
1935
1936/**
1937 * ice_init_nvm_phy_type - Initialize the NVM PHY type
1938 * @pi: port info structure
1939 *
1940 * Initialize nvm_phy_type_[low|high] for link lenient mode support
1941 */
1942static int ice_init_nvm_phy_type(struct ice_port_info *pi)
1943{
1944	struct ice_aqc_get_phy_caps_data *pcaps;
1945	struct ice_pf *pf = pi->hw->back;
1946	int err;
1947
1948	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
1949	if (!pcaps)
1950		return -ENOMEM;
1951
1952	err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_NO_MEDIA,
1953				  pcaps, NULL);
1954
1955	if (err) {
1956		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
1957		goto out;
1958	}
1959
1960	pf->nvm_phy_type_hi = pcaps->phy_type_high;
1961	pf->nvm_phy_type_lo = pcaps->phy_type_low;
1962
1963out:
1964	kfree(pcaps);
1965	return err;
1966}
1967
1968/**
1969 * ice_init_link_dflt_override - Initialize link default override
1970 * @pi: port info structure
1971 *
1972 * Initialize link default override and PHY total port shutdown during probe
1973 */
1974static void ice_init_link_dflt_override(struct ice_port_info *pi)
1975{
1976	struct ice_link_default_override_tlv *ldo;
1977	struct ice_pf *pf = pi->hw->back;
1978
1979	ldo = &pf->link_dflt_override;
1980	if (ice_get_link_default_override(ldo, pi))
1981		return;
1982
1983	if (!(ldo->options & ICE_LINK_OVERRIDE_PORT_DIS))
1984		return;
1985
1986	/* Enable Total Port Shutdown (override/replace link-down-on-close
1987	 * ethtool private flag) for ports with Port Disable bit set.
1988	 */
1989	set_bit(ICE_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
1990	set_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
1991}
1992
1993/**
1994 * ice_init_phy_cfg_dflt_override - Initialize PHY cfg default override settings
1995 * @pi: port info structure
1996 *
1997 * If default override is enabled, initialize the user PHY cfg speed and FEC
1998 * settings using the default override mask from the NVM.
1999 *
2000 * The PHY should only be configured with the default override settings the
2001 * first time media is available. The ICE_LINK_DEFAULT_OVERRIDE_PENDING state
2002 * is used to indicate that the user PHY cfg default override is initialized
2003 * and the PHY has not been configured with the default override settings. The
2004 * state is set here, and cleared in ice_configure_phy the first time the PHY is
2005 * configured.
2006 *
2007 * This function should be called only if the FW doesn't support default
2008 * configuration mode, as reported by ice_fw_supports_report_dflt_cfg.
2009 */
2010static void ice_init_phy_cfg_dflt_override(struct ice_port_info *pi)
2011{
2012	struct ice_link_default_override_tlv *ldo;
2013	struct ice_aqc_set_phy_cfg_data *cfg;
2014	struct ice_phy_info *phy = &pi->phy;
2015	struct ice_pf *pf = pi->hw->back;
2016
2017	ldo = &pf->link_dflt_override;
2018
2019	/* If link default override is enabled, use to mask NVM PHY capabilities
2020	 * for speed and FEC default configuration.
2021	 */
2022	cfg = &phy->curr_user_phy_cfg;
2023
2024	if (ldo->phy_type_low || ldo->phy_type_high) {
2025		cfg->phy_type_low = pf->nvm_phy_type_lo &
2026				    cpu_to_le64(ldo->phy_type_low);
2027		cfg->phy_type_high = pf->nvm_phy_type_hi &
2028				     cpu_to_le64(ldo->phy_type_high);
2029	}
2030	cfg->link_fec_opt = ldo->fec_options;
2031	phy->curr_user_fec_req = ICE_FEC_AUTO;
2032
2033	set_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING, pf->state);
2034}
2035
2036/**
2037 * ice_init_phy_user_cfg - Initialize the PHY user configuration
2038 * @pi: port info structure
2039 *
2040 * Initialize the current user PHY configuration, speed, FEC, and FC requested
2041 * mode to default. The PHY defaults are from get PHY capabilities topology
2042 * with media so call when media is first available. An error is returned if
2043 * called when media is not available. The PHY initialization completed state is
2044 * set here.
2045 *
2046 * These configurations are used when setting PHY
2047 * configuration. The user PHY configuration is updated on set PHY
2048 * configuration. Returns 0 on success, negative on failure
2049 */
2050static int ice_init_phy_user_cfg(struct ice_port_info *pi)
2051{
2052	struct ice_aqc_get_phy_caps_data *pcaps;
2053	struct ice_phy_info *phy = &pi->phy;
2054	struct ice_pf *pf = pi->hw->back;
2055	int err;
2056
2057	if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
2058		return -EIO;
2059
2060	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2061	if (!pcaps)
2062		return -ENOMEM;
2063
2064	if (ice_fw_supports_report_dflt_cfg(pi->hw))
2065		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2066					  pcaps, NULL);
2067	else
2068		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2069					  pcaps, NULL);
2070	if (err) {
2071		dev_err(ice_pf_to_dev(pf), "Get PHY capability failed.\n");
2072		goto err_out;
2073	}
2074
2075	ice_copy_phy_caps_to_cfg(pi, pcaps, &pi->phy.curr_user_phy_cfg);
2076
2077	/* check if lenient mode is supported and enabled */
2078	if (ice_fw_supports_link_override(pi->hw) &&
2079	    !(pcaps->module_compliance_enforcement &
2080	      ICE_AQC_MOD_ENFORCE_STRICT_MODE)) {
2081		set_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags);
2082
2083		/* if the FW supports default PHY configuration mode, then the driver
2084		 * does not have to apply link override settings. If not,
2085		 * initialize user PHY configuration with link override values
2086		 */
2087		if (!ice_fw_supports_report_dflt_cfg(pi->hw) &&
2088		    (pf->link_dflt_override.options & ICE_LINK_OVERRIDE_EN)) {
2089			ice_init_phy_cfg_dflt_override(pi);
2090			goto out;
2091		}
2092	}
2093
2094	/* if link default override is not enabled, set user flow control and
2095	 * FEC settings based on what get_phy_caps returned
2096	 */
2097	phy->curr_user_fec_req = ice_caps_to_fec_mode(pcaps->caps,
2098						      pcaps->link_fec_options);
2099	phy->curr_user_fc_req = ice_caps_to_fc_mode(pcaps->caps);
2100
2101out:
2102	phy->curr_user_speed_req = ICE_AQ_LINK_SPEED_M;
2103	set_bit(ICE_PHY_INIT_COMPLETE, pf->state);
2104err_out:
2105	kfree(pcaps);
2106	return err;
2107}
2108
2109/**
2110 * ice_configure_phy - configure PHY
2111 * @vsi: VSI of PHY
2112 *
2113 * Set the PHY configuration. If the current PHY configuration is the same as
2114 * the curr_user_phy_cfg, then do nothing to avoid link flap. Otherwise
2115 * configure the based get PHY capabilities for topology with media.
2116 */
2117static int ice_configure_phy(struct ice_vsi *vsi)
2118{
2119	struct device *dev = ice_pf_to_dev(vsi->back);
2120	struct ice_port_info *pi = vsi->port_info;
2121	struct ice_aqc_get_phy_caps_data *pcaps;
2122	struct ice_aqc_set_phy_cfg_data *cfg;
2123	struct ice_phy_info *phy = &pi->phy;
2124	struct ice_pf *pf = vsi->back;
2125	int err;
2126
2127	/* Ensure we have media as we cannot configure a medialess port */
2128	if (!(phy->link_info.link_info & ICE_AQ_MEDIA_AVAILABLE))
2129		return -EPERM;
2130
2131	ice_print_topo_conflict(vsi);
2132
2133	if (!test_bit(ICE_FLAG_LINK_LENIENT_MODE_ENA, pf->flags) &&
2134	    phy->link_info.topo_media_conflict == ICE_AQ_LINK_TOPO_UNSUPP_MEDIA)
2135		return -EPERM;
2136
2137	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))
2138		return ice_force_phys_link_state(vsi, true);
2139
2140	pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL);
2141	if (!pcaps)
2142		return -ENOMEM;
2143
2144	/* Get current PHY config */
2145	err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_ACTIVE_CFG, pcaps,
2146				  NULL);
2147	if (err) {
2148		dev_err(dev, "Failed to get PHY configuration, VSI %d error %d\n",
2149			vsi->vsi_num, err);
2150		goto done;
2151	}
2152
2153	/* If PHY enable link is configured and configuration has not changed,
2154	 * there's nothing to do
2155	 */
2156	if (pcaps->caps & ICE_AQC_PHY_EN_LINK &&
2157	    ice_phy_caps_equals_cfg(pcaps, &phy->curr_user_phy_cfg))
2158		goto done;
2159
2160	/* Use PHY topology as baseline for configuration */
2161	memset(pcaps, 0, sizeof(*pcaps));
2162	if (ice_fw_supports_report_dflt_cfg(pi->hw))
2163		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_DFLT_CFG,
2164					  pcaps, NULL);
2165	else
2166		err = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP_MEDIA,
2167					  pcaps, NULL);
2168	if (err) {
2169		dev_err(dev, "Failed to get PHY caps, VSI %d error %d\n",
2170			vsi->vsi_num, err);
2171		goto done;
2172	}
2173
2174	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
2175	if (!cfg) {
2176		err = -ENOMEM;
2177		goto done;
2178	}
2179
2180	ice_copy_phy_caps_to_cfg(pi, pcaps, cfg);
2181
2182	/* Speed - If default override pending, use curr_user_phy_cfg set in
2183	 * ice_init_phy_user_cfg_ldo.
2184	 */
2185	if (test_and_clear_bit(ICE_LINK_DEFAULT_OVERRIDE_PENDING,
2186			       vsi->back->state)) {
2187		cfg->phy_type_low = phy->curr_user_phy_cfg.phy_type_low;
2188		cfg->phy_type_high = phy->curr_user_phy_cfg.phy_type_high;
2189	} else {
2190		u64 phy_low = 0, phy_high = 0;
2191
2192		ice_update_phy_type(&phy_low, &phy_high,
2193				    pi->phy.curr_user_speed_req);
2194		cfg->phy_type_low = pcaps->phy_type_low & cpu_to_le64(phy_low);
2195		cfg->phy_type_high = pcaps->phy_type_high &
2196				     cpu_to_le64(phy_high);
2197	}
2198
2199	/* Can't provide what was requested; use PHY capabilities */
2200	if (!cfg->phy_type_low && !cfg->phy_type_high) {
2201		cfg->phy_type_low = pcaps->phy_type_low;
2202		cfg->phy_type_high = pcaps->phy_type_high;
2203	}
2204
2205	/* FEC */
2206	ice_cfg_phy_fec(pi, cfg, phy->curr_user_fec_req);
2207
2208	/* Can't provide what was requested; use PHY capabilities */
2209	if (cfg->link_fec_opt !=
2210	    (cfg->link_fec_opt & pcaps->link_fec_options)) {
2211		cfg->caps |= pcaps->caps & ICE_AQC_PHY_EN_AUTO_FEC;
2212		cfg->link_fec_opt = pcaps->link_fec_options;
2213	}
2214
2215	/* Flow Control - always supported; no need to check against
2216	 * capabilities
2217	 */
2218	ice_cfg_phy_fc(pi, cfg, phy->curr_user_fc_req);
2219
2220	/* Enable link and link update */
2221	cfg->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT | ICE_AQ_PHY_ENA_LINK;
2222
2223	err = ice_aq_set_phy_cfg(&pf->hw, pi, cfg, NULL);
2224	if (err)
2225		dev_err(dev, "Failed to set phy config, VSI %d error %d\n",
2226			vsi->vsi_num, err);
2227
2228	kfree(cfg);
2229done:
2230	kfree(pcaps);
2231	return err;
2232}
2233
2234/**
2235 * ice_check_media_subtask - Check for media
2236 * @pf: pointer to PF struct
2237 *
2238 * If media is available, then initialize PHY user configuration if it is not
2239 * been, and configure the PHY if the interface is up.
2240 */
2241static void ice_check_media_subtask(struct ice_pf *pf)
2242{
2243	struct ice_port_info *pi;
2244	struct ice_vsi *vsi;
2245	int err;
2246
2247	/* No need to check for media if it's already present */
2248	if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags))
2249		return;
2250
2251	vsi = ice_get_main_vsi(pf);
2252	if (!vsi)
2253		return;
2254
2255	/* Refresh link info and check if media is present */
2256	pi = vsi->port_info;
2257	err = ice_update_link_info(pi);
2258	if (err)
2259		return;
2260
2261	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
2262
2263	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
2264		if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state))
2265			ice_init_phy_user_cfg(pi);
2266
2267		/* PHY settings are reset on media insertion, reconfigure
2268		 * PHY to preserve settings.
2269		 */
2270		if (test_bit(ICE_VSI_DOWN, vsi->state) &&
2271		    test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags))
2272			return;
2273
2274		err = ice_configure_phy(vsi);
2275		if (!err)
2276			clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
2277
2278		/* A Link Status Event will be generated; the event handler
2279		 * will complete bringing the interface up
2280		 */
2281	}
2282}
2283
2284/**
2285 * ice_service_task - manage and run subtasks
2286 * @work: pointer to work_struct contained by the PF struct
2287 */
2288static void ice_service_task(struct work_struct *work)
2289{
2290	struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
2291	unsigned long start_time = jiffies;
2292
2293	/* subtasks */
2294
2295	/* process reset requests first */
2296	ice_reset_subtask(pf);
2297
2298	/* bail if a reset/recovery cycle is pending or rebuild failed */
2299	if (ice_is_reset_in_progress(pf->state) ||
2300	    test_bit(ICE_SUSPENDED, pf->state) ||
2301	    test_bit(ICE_NEEDS_RESTART, pf->state)) {
2302		ice_service_task_complete(pf);
2303		return;
2304	}
2305
2306	if (test_and_clear_bit(ICE_AUX_ERR_PENDING, pf->state)) {
2307		struct iidc_event *event;
2308
2309		event = kzalloc(sizeof(*event), GFP_KERNEL);
2310		if (event) {
2311			set_bit(IIDC_EVENT_CRIT_ERR, event->type);
2312			/* report the entire OICR value to AUX driver */
2313			swap(event->reg, pf->oicr_err_reg);
2314			ice_send_event_to_aux(pf, event);
2315			kfree(event);
2316		}
2317	}
2318
2319	if (test_bit(ICE_FLAG_PLUG_AUX_DEV, pf->flags)) {
2320		/* Plug aux device per request */
 
 
 
 
 
 
2321		ice_plug_aux_dev(pf);
2322
2323		/* Mark plugging as done but check whether unplug was
2324		 * requested during ice_plug_aux_dev() call
2325		 * (e.g. from ice_clear_rdma_cap()) and if so then
2326		 * plug aux device.
2327		 */
2328		if (!test_and_clear_bit(ICE_FLAG_PLUG_AUX_DEV, pf->flags))
2329			ice_unplug_aux_dev(pf);
2330	}
2331
2332	if (test_and_clear_bit(ICE_FLAG_MTU_CHANGED, pf->flags)) {
2333		struct iidc_event *event;
2334
2335		event = kzalloc(sizeof(*event), GFP_KERNEL);
2336		if (event) {
2337			set_bit(IIDC_EVENT_AFTER_MTU_CHANGE, event->type);
2338			ice_send_event_to_aux(pf, event);
2339			kfree(event);
2340		}
2341	}
2342
2343	ice_clean_adminq_subtask(pf);
2344	ice_check_media_subtask(pf);
2345	ice_check_for_hang_subtask(pf);
2346	ice_sync_fltr_subtask(pf);
2347	ice_handle_mdd_event(pf);
2348	ice_watchdog_subtask(pf);
2349
2350	if (ice_is_safe_mode(pf)) {
2351		ice_service_task_complete(pf);
2352		return;
2353	}
2354
2355	ice_process_vflr_event(pf);
2356	ice_clean_mailboxq_subtask(pf);
2357	ice_clean_sbq_subtask(pf);
2358	ice_sync_arfs_fltrs(pf);
2359	ice_flush_fdir_ctx(pf);
2360
2361	/* Clear ICE_SERVICE_SCHED flag to allow scheduling next event */
2362	ice_service_task_complete(pf);
2363
2364	/* If the tasks have taken longer than one service timer period
2365	 * or there is more work to be done, reset the service timer to
2366	 * schedule the service task now.
2367	 */
2368	if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
2369	    test_bit(ICE_MDD_EVENT_PENDING, pf->state) ||
2370	    test_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
2371	    test_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state) ||
2372	    test_bit(ICE_FD_VF_FLUSH_CTX, pf->state) ||
2373	    test_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state) ||
2374	    test_bit(ICE_ADMINQ_EVENT_PENDING, pf->state))
2375		mod_timer(&pf->serv_tmr, jiffies);
2376}
2377
2378/**
2379 * ice_set_ctrlq_len - helper function to set controlq length
2380 * @hw: pointer to the HW instance
2381 */
2382static void ice_set_ctrlq_len(struct ice_hw *hw)
2383{
2384	hw->adminq.num_rq_entries = ICE_AQ_LEN;
2385	hw->adminq.num_sq_entries = ICE_AQ_LEN;
2386	hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
2387	hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
2388	hw->mailboxq.num_rq_entries = PF_MBX_ARQLEN_ARQLEN_M;
2389	hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN;
2390	hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2391	hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN;
2392	hw->sbq.num_rq_entries = ICE_SBQ_LEN;
2393	hw->sbq.num_sq_entries = ICE_SBQ_LEN;
2394	hw->sbq.rq_buf_size = ICE_SBQ_MAX_BUF_LEN;
2395	hw->sbq.sq_buf_size = ICE_SBQ_MAX_BUF_LEN;
2396}
2397
2398/**
2399 * ice_schedule_reset - schedule a reset
2400 * @pf: board private structure
2401 * @reset: reset being requested
2402 */
2403int ice_schedule_reset(struct ice_pf *pf, enum ice_reset_req reset)
2404{
2405	struct device *dev = ice_pf_to_dev(pf);
2406
2407	/* bail out if earlier reset has failed */
2408	if (test_bit(ICE_RESET_FAILED, pf->state)) {
2409		dev_dbg(dev, "earlier reset has failed\n");
2410		return -EIO;
2411	}
2412	/* bail if reset/recovery already in progress */
2413	if (ice_is_reset_in_progress(pf->state)) {
2414		dev_dbg(dev, "Reset already in progress\n");
2415		return -EBUSY;
2416	}
2417
2418	switch (reset) {
2419	case ICE_RESET_PFR:
2420		set_bit(ICE_PFR_REQ, pf->state);
2421		break;
2422	case ICE_RESET_CORER:
2423		set_bit(ICE_CORER_REQ, pf->state);
2424		break;
2425	case ICE_RESET_GLOBR:
2426		set_bit(ICE_GLOBR_REQ, pf->state);
2427		break;
2428	default:
2429		return -EINVAL;
2430	}
2431
2432	ice_service_task_schedule(pf);
2433	return 0;
2434}
2435
2436/**
2437 * ice_irq_affinity_notify - Callback for affinity changes
2438 * @notify: context as to what irq was changed
2439 * @mask: the new affinity mask
2440 *
2441 * This is a callback function used by the irq_set_affinity_notifier function
2442 * so that we may register to receive changes to the irq affinity masks.
2443 */
2444static void
2445ice_irq_affinity_notify(struct irq_affinity_notify *notify,
2446			const cpumask_t *mask)
2447{
2448	struct ice_q_vector *q_vector =
2449		container_of(notify, struct ice_q_vector, affinity_notify);
2450
2451	cpumask_copy(&q_vector->affinity_mask, mask);
2452}
2453
2454/**
2455 * ice_irq_affinity_release - Callback for affinity notifier release
2456 * @ref: internal core kernel usage
2457 *
2458 * This is a callback function used by the irq_set_affinity_notifier function
2459 * to inform the current notification subscriber that they will no longer
2460 * receive notifications.
2461 */
2462static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
2463
2464/**
2465 * ice_vsi_ena_irq - Enable IRQ for the given VSI
2466 * @vsi: the VSI being configured
2467 */
2468static int ice_vsi_ena_irq(struct ice_vsi *vsi)
2469{
2470	struct ice_hw *hw = &vsi->back->hw;
2471	int i;
2472
2473	ice_for_each_q_vector(vsi, i)
2474		ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
2475
2476	ice_flush(hw);
2477	return 0;
2478}
2479
2480/**
2481 * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
2482 * @vsi: the VSI being configured
2483 * @basename: name for the vector
2484 */
2485static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
2486{
2487	int q_vectors = vsi->num_q_vectors;
2488	struct ice_pf *pf = vsi->back;
2489	int base = vsi->base_vector;
2490	struct device *dev;
2491	int rx_int_idx = 0;
2492	int tx_int_idx = 0;
2493	int vector, err;
2494	int irq_num;
2495
2496	dev = ice_pf_to_dev(pf);
2497	for (vector = 0; vector < q_vectors; vector++) {
2498		struct ice_q_vector *q_vector = vsi->q_vectors[vector];
2499
2500		irq_num = pf->msix_entries[base + vector].vector;
2501
2502		if (q_vector->tx.tx_ring && q_vector->rx.rx_ring) {
2503			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2504				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2505			tx_int_idx++;
2506		} else if (q_vector->rx.rx_ring) {
2507			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2508				 "%s-%s-%d", basename, "rx", rx_int_idx++);
2509		} else if (q_vector->tx.tx_ring) {
2510			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2511				 "%s-%s-%d", basename, "tx", tx_int_idx++);
2512		} else {
2513			/* skip this unused q_vector */
2514			continue;
2515		}
2516		if (vsi->type == ICE_VSI_CTRL && vsi->vf)
2517			err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2518					       IRQF_SHARED, q_vector->name,
2519					       q_vector);
2520		else
2521			err = devm_request_irq(dev, irq_num, vsi->irq_handler,
2522					       0, q_vector->name, q_vector);
2523		if (err) {
2524			netdev_err(vsi->netdev, "MSIX request_irq failed, error: %d\n",
2525				   err);
2526			goto free_q_irqs;
2527		}
2528
2529		/* register for affinity change notifications */
2530		if (!IS_ENABLED(CONFIG_RFS_ACCEL)) {
2531			struct irq_affinity_notify *affinity_notify;
2532
2533			affinity_notify = &q_vector->affinity_notify;
2534			affinity_notify->notify = ice_irq_affinity_notify;
2535			affinity_notify->release = ice_irq_affinity_release;
2536			irq_set_affinity_notifier(irq_num, affinity_notify);
2537		}
2538
2539		/* assign the mask for this irq */
2540		irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
2541	}
2542
2543	err = ice_set_cpu_rx_rmap(vsi);
2544	if (err) {
2545		netdev_err(vsi->netdev, "Failed to setup CPU RMAP on VSI %u: %pe\n",
2546			   vsi->vsi_num, ERR_PTR(err));
2547		goto free_q_irqs;
2548	}
2549
2550	vsi->irqs_ready = true;
2551	return 0;
2552
2553free_q_irqs:
2554	while (vector) {
2555		vector--;
2556		irq_num = pf->msix_entries[base + vector].vector;
2557		if (!IS_ENABLED(CONFIG_RFS_ACCEL))
2558			irq_set_affinity_notifier(irq_num, NULL);
2559		irq_set_affinity_hint(irq_num, NULL);
2560		devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]);
2561	}
2562	return err;
2563}
2564
2565/**
2566 * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP
2567 * @vsi: VSI to setup Tx rings used by XDP
2568 *
2569 * Return 0 on success and negative value on error
2570 */
2571static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi)
2572{
2573	struct device *dev = ice_pf_to_dev(vsi->back);
2574	struct ice_tx_desc *tx_desc;
2575	int i, j;
2576
2577	ice_for_each_xdp_txq(vsi, i) {
2578		u16 xdp_q_idx = vsi->alloc_txq + i;
2579		struct ice_ring_stats *ring_stats;
2580		struct ice_tx_ring *xdp_ring;
2581
2582		xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL);
2583		if (!xdp_ring)
2584			goto free_xdp_rings;
2585
2586		ring_stats = kzalloc(sizeof(*ring_stats), GFP_KERNEL);
2587		if (!ring_stats) {
2588			ice_free_tx_ring(xdp_ring);
2589			goto free_xdp_rings;
2590		}
2591
2592		xdp_ring->ring_stats = ring_stats;
2593		xdp_ring->q_index = xdp_q_idx;
2594		xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx];
2595		xdp_ring->vsi = vsi;
2596		xdp_ring->netdev = NULL;
2597		xdp_ring->dev = dev;
2598		xdp_ring->count = vsi->num_tx_desc;
2599		xdp_ring->next_dd = ICE_RING_QUARTER(xdp_ring) - 1;
2600		xdp_ring->next_rs = ICE_RING_QUARTER(xdp_ring) - 1;
2601		WRITE_ONCE(vsi->xdp_rings[i], xdp_ring);
2602		if (ice_setup_tx_ring(xdp_ring))
2603			goto free_xdp_rings;
2604		ice_set_ring_xdp(xdp_ring);
2605		spin_lock_init(&xdp_ring->tx_lock);
2606		for (j = 0; j < xdp_ring->count; j++) {
2607			tx_desc = ICE_TX_DESC(xdp_ring, j);
2608			tx_desc->cmd_type_offset_bsz = 0;
2609		}
2610	}
2611
2612	return 0;
2613
2614free_xdp_rings:
2615	for (; i >= 0; i--) {
2616		if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc) {
2617			kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
2618			vsi->xdp_rings[i]->ring_stats = NULL;
2619			ice_free_tx_ring(vsi->xdp_rings[i]);
2620		}
2621	}
2622	return -ENOMEM;
2623}
2624
2625/**
2626 * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI
2627 * @vsi: VSI to set the bpf prog on
2628 * @prog: the bpf prog pointer
2629 */
2630static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog)
2631{
2632	struct bpf_prog *old_prog;
2633	int i;
2634
2635	old_prog = xchg(&vsi->xdp_prog, prog);
 
 
 
2636	if (old_prog)
2637		bpf_prog_put(old_prog);
2638
2639	ice_for_each_rxq(vsi, i)
2640		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
2641}
2642
2643/**
2644 * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP
2645 * @vsi: VSI to bring up Tx rings used by XDP
2646 * @prog: bpf program that will be assigned to VSI
2647 *
2648 * Return 0 on success and negative value on error
2649 */
2650int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog)
2651{
2652	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2653	int xdp_rings_rem = vsi->num_xdp_txq;
2654	struct ice_pf *pf = vsi->back;
2655	struct ice_qs_cfg xdp_qs_cfg = {
2656		.qs_mutex = &pf->avail_q_mutex,
2657		.pf_map = pf->avail_txqs,
2658		.pf_map_size = pf->max_pf_txqs,
2659		.q_count = vsi->num_xdp_txq,
2660		.scatter_count = ICE_MAX_SCATTER_TXQS,
2661		.vsi_map = vsi->txq_map,
2662		.vsi_map_offset = vsi->alloc_txq,
2663		.mapping_mode = ICE_VSI_MAP_CONTIG
2664	};
2665	struct device *dev;
2666	int i, v_idx;
2667	int status;
2668
2669	dev = ice_pf_to_dev(pf);
2670	vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq,
2671				      sizeof(*vsi->xdp_rings), GFP_KERNEL);
2672	if (!vsi->xdp_rings)
2673		return -ENOMEM;
2674
2675	vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode;
2676	if (__ice_vsi_get_qs(&xdp_qs_cfg))
2677		goto err_map_xdp;
2678
2679	if (static_key_enabled(&ice_xdp_locking_key))
2680		netdev_warn(vsi->netdev,
2681			    "Could not allocate one XDP Tx ring per CPU, XDP_TX/XDP_REDIRECT actions will be slower\n");
2682
2683	if (ice_xdp_alloc_setup_rings(vsi))
2684		goto clear_xdp_rings;
2685
2686	/* follow the logic from ice_vsi_map_rings_to_vectors */
2687	ice_for_each_q_vector(vsi, v_idx) {
2688		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2689		int xdp_rings_per_v, q_id, q_base;
2690
2691		xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem,
2692					       vsi->num_q_vectors - v_idx);
2693		q_base = vsi->num_xdp_txq - xdp_rings_rem;
2694
2695		for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) {
2696			struct ice_tx_ring *xdp_ring = vsi->xdp_rings[q_id];
2697
2698			xdp_ring->q_vector = q_vector;
2699			xdp_ring->next = q_vector->tx.tx_ring;
2700			q_vector->tx.tx_ring = xdp_ring;
2701		}
2702		xdp_rings_rem -= xdp_rings_per_v;
2703	}
2704
2705	ice_for_each_rxq(vsi, i) {
2706		if (static_key_enabled(&ice_xdp_locking_key)) {
2707			vsi->rx_rings[i]->xdp_ring = vsi->xdp_rings[i % vsi->num_xdp_txq];
2708		} else {
2709			struct ice_q_vector *q_vector = vsi->rx_rings[i]->q_vector;
2710			struct ice_tx_ring *ring;
2711
2712			ice_for_each_tx_ring(ring, q_vector->tx) {
2713				if (ice_ring_is_xdp(ring)) {
2714					vsi->rx_rings[i]->xdp_ring = ring;
2715					break;
2716				}
2717			}
2718		}
2719		ice_tx_xsk_pool(vsi, i);
2720	}
2721
2722	/* omit the scheduler update if in reset path; XDP queues will be
2723	 * taken into account at the end of ice_vsi_rebuild, where
2724	 * ice_cfg_vsi_lan is being called
2725	 */
2726	if (ice_is_reset_in_progress(pf->state))
2727		return 0;
2728
2729	/* tell the Tx scheduler that right now we have
2730	 * additional queues
2731	 */
2732	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2733		max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq;
2734
2735	status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2736				 max_txqs);
2737	if (status) {
2738		dev_err(dev, "Failed VSI LAN queue config for XDP, error: %d\n",
2739			status);
2740		goto clear_xdp_rings;
2741	}
2742
2743	/* assign the prog only when it's not already present on VSI;
2744	 * this flow is a subject of both ethtool -L and ndo_bpf flows;
2745	 * VSI rebuild that happens under ethtool -L can expose us to
2746	 * the bpf_prog refcount issues as we would be swapping same
2747	 * bpf_prog pointers from vsi->xdp_prog and calling bpf_prog_put
2748	 * on it as it would be treated as an 'old_prog'; for ndo_bpf
2749	 * this is not harmful as dev_xdp_install bumps the refcount
2750	 * before calling the op exposed by the driver;
2751	 */
2752	if (!ice_is_xdp_ena_vsi(vsi))
2753		ice_vsi_assign_bpf_prog(vsi, prog);
2754
2755	return 0;
2756clear_xdp_rings:
2757	ice_for_each_xdp_txq(vsi, i)
2758		if (vsi->xdp_rings[i]) {
2759			kfree_rcu(vsi->xdp_rings[i], rcu);
2760			vsi->xdp_rings[i] = NULL;
2761		}
2762
2763err_map_xdp:
2764	mutex_lock(&pf->avail_q_mutex);
2765	ice_for_each_xdp_txq(vsi, i) {
2766		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2767		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2768	}
2769	mutex_unlock(&pf->avail_q_mutex);
2770
2771	devm_kfree(dev, vsi->xdp_rings);
2772	return -ENOMEM;
2773}
2774
2775/**
2776 * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings
2777 * @vsi: VSI to remove XDP rings
2778 *
2779 * Detach XDP rings from irq vectors, clean up the PF bitmap and free
2780 * resources
2781 */
2782int ice_destroy_xdp_rings(struct ice_vsi *vsi)
2783{
2784	u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
2785	struct ice_pf *pf = vsi->back;
2786	int i, v_idx;
2787
2788	/* q_vectors are freed in reset path so there's no point in detaching
2789	 * rings; in case of rebuild being triggered not from reset bits
2790	 * in pf->state won't be set, so additionally check first q_vector
2791	 * against NULL
2792	 */
2793	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2794		goto free_qmap;
2795
2796	ice_for_each_q_vector(vsi, v_idx) {
2797		struct ice_q_vector *q_vector = vsi->q_vectors[v_idx];
2798		struct ice_tx_ring *ring;
2799
2800		ice_for_each_tx_ring(ring, q_vector->tx)
2801			if (!ring->tx_buf || !ice_ring_is_xdp(ring))
2802				break;
2803
2804		/* restore the value of last node prior to XDP setup */
2805		q_vector->tx.tx_ring = ring;
2806	}
2807
2808free_qmap:
2809	mutex_lock(&pf->avail_q_mutex);
2810	ice_for_each_xdp_txq(vsi, i) {
2811		clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs);
2812		vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX;
2813	}
2814	mutex_unlock(&pf->avail_q_mutex);
2815
2816	ice_for_each_xdp_txq(vsi, i)
2817		if (vsi->xdp_rings[i]) {
2818			if (vsi->xdp_rings[i]->desc) {
2819				synchronize_rcu();
2820				ice_free_tx_ring(vsi->xdp_rings[i]);
2821			}
2822			kfree_rcu(vsi->xdp_rings[i]->ring_stats, rcu);
2823			vsi->xdp_rings[i]->ring_stats = NULL;
2824			kfree_rcu(vsi->xdp_rings[i], rcu);
2825			vsi->xdp_rings[i] = NULL;
2826		}
2827
2828	devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings);
2829	vsi->xdp_rings = NULL;
2830
2831	if (static_key_enabled(&ice_xdp_locking_key))
2832		static_branch_dec(&ice_xdp_locking_key);
2833
2834	if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0])
2835		return 0;
2836
2837	ice_vsi_assign_bpf_prog(vsi, NULL);
2838
2839	/* notify Tx scheduler that we destroyed XDP queues and bring
2840	 * back the old number of child nodes
2841	 */
2842	for (i = 0; i < vsi->tc_cfg.numtc; i++)
2843		max_txqs[i] = vsi->num_txq;
2844
2845	/* change number of XDP Tx queues to 0 */
2846	vsi->num_xdp_txq = 0;
2847
2848	return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
2849			       max_txqs);
2850}
2851
2852/**
2853 * ice_vsi_rx_napi_schedule - Schedule napi on RX queues from VSI
2854 * @vsi: VSI to schedule napi on
2855 */
2856static void ice_vsi_rx_napi_schedule(struct ice_vsi *vsi)
2857{
2858	int i;
2859
2860	ice_for_each_rxq(vsi, i) {
2861		struct ice_rx_ring *rx_ring = vsi->rx_rings[i];
2862
2863		if (rx_ring->xsk_pool)
2864			napi_schedule(&rx_ring->q_vector->napi);
2865	}
2866}
2867
2868/**
2869 * ice_vsi_determine_xdp_res - figure out how many Tx qs can XDP have
2870 * @vsi: VSI to determine the count of XDP Tx qs
2871 *
2872 * returns 0 if Tx qs count is higher than at least half of CPU count,
2873 * -ENOMEM otherwise
2874 */
2875int ice_vsi_determine_xdp_res(struct ice_vsi *vsi)
2876{
2877	u16 avail = ice_get_avail_txq_count(vsi->back);
2878	u16 cpus = num_possible_cpus();
2879
2880	if (avail < cpus / 2)
2881		return -ENOMEM;
2882
2883	vsi->num_xdp_txq = min_t(u16, avail, cpus);
2884
2885	if (vsi->num_xdp_txq < cpus)
2886		static_branch_inc(&ice_xdp_locking_key);
2887
2888	return 0;
2889}
2890
2891/**
 
 
 
 
 
 
 
 
 
 
 
 
2892 * ice_xdp_setup_prog - Add or remove XDP eBPF program
2893 * @vsi: VSI to setup XDP for
2894 * @prog: XDP program
2895 * @extack: netlink extended ack
2896 */
2897static int
2898ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog,
2899		   struct netlink_ext_ack *extack)
2900{
2901	int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD;
2902	bool if_running = netif_running(vsi->netdev);
2903	int ret = 0, xdp_ring_err = 0;
2904
2905	if (frame_size > vsi->rx_buf_len) {
2906		NL_SET_ERR_MSG_MOD(extack, "MTU too large for loading XDP");
2907		return -EOPNOTSUPP;
 
 
 
 
 
 
 
 
 
2908	}
2909
2910	/* need to stop netdev while setting up the program for Rx rings */
2911	if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
2912		ret = ice_down(vsi);
2913		if (ret) {
2914			NL_SET_ERR_MSG_MOD(extack, "Preparing device for XDP attach failed");
2915			return ret;
2916		}
2917	}
2918
2919	if (!ice_is_xdp_ena_vsi(vsi) && prog) {
2920		xdp_ring_err = ice_vsi_determine_xdp_res(vsi);
2921		if (xdp_ring_err) {
2922			NL_SET_ERR_MSG_MOD(extack, "Not enough Tx resources for XDP");
2923		} else {
2924			xdp_ring_err = ice_prepare_xdp_rings(vsi, prog);
2925			if (xdp_ring_err)
2926				NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Tx resources failed");
2927		}
 
2928		/* reallocate Rx queues that are used for zero-copy */
2929		xdp_ring_err = ice_realloc_zc_buf(vsi, true);
2930		if (xdp_ring_err)
2931			NL_SET_ERR_MSG_MOD(extack, "Setting up XDP Rx resources failed");
2932	} else if (ice_is_xdp_ena_vsi(vsi) && !prog) {
 
2933		xdp_ring_err = ice_destroy_xdp_rings(vsi);
2934		if (xdp_ring_err)
2935			NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Tx resources failed");
2936		/* reallocate Rx queues that were used for zero-copy */
2937		xdp_ring_err = ice_realloc_zc_buf(vsi, false);
2938		if (xdp_ring_err)
2939			NL_SET_ERR_MSG_MOD(extack, "Freeing XDP Rx resources failed");
2940	} else {
2941		/* safe to call even when prog == vsi->xdp_prog as
2942		 * dev_xdp_install in net/core/dev.c incremented prog's
2943		 * refcount so corresponding bpf_prog_put won't cause
2944		 * underflow
2945		 */
2946		ice_vsi_assign_bpf_prog(vsi, prog);
2947	}
2948
2949	if (if_running)
2950		ret = ice_up(vsi);
2951
2952	if (!ret && prog)
2953		ice_vsi_rx_napi_schedule(vsi);
2954
2955	return (ret || xdp_ring_err) ? -ENOMEM : 0;
2956}
2957
2958/**
2959 * ice_xdp_safe_mode - XDP handler for safe mode
2960 * @dev: netdevice
2961 * @xdp: XDP command
2962 */
2963static int ice_xdp_safe_mode(struct net_device __always_unused *dev,
2964			     struct netdev_bpf *xdp)
2965{
2966	NL_SET_ERR_MSG_MOD(xdp->extack,
2967			   "Please provide working DDP firmware package in order to use XDP\n"
2968			   "Refer to Documentation/networking/device_drivers/ethernet/intel/ice.rst");
2969	return -EOPNOTSUPP;
2970}
2971
2972/**
2973 * ice_xdp - implements XDP handler
2974 * @dev: netdevice
2975 * @xdp: XDP command
2976 */
2977static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2978{
2979	struct ice_netdev_priv *np = netdev_priv(dev);
2980	struct ice_vsi *vsi = np->vsi;
2981
2982	if (vsi->type != ICE_VSI_PF) {
2983		NL_SET_ERR_MSG_MOD(xdp->extack, "XDP can be loaded only on PF VSI");
2984		return -EINVAL;
2985	}
2986
2987	switch (xdp->command) {
2988	case XDP_SETUP_PROG:
2989		return ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack);
2990	case XDP_SETUP_XSK_POOL:
2991		return ice_xsk_pool_setup(vsi, xdp->xsk.pool,
2992					  xdp->xsk.queue_id);
2993	default:
2994		return -EINVAL;
2995	}
2996}
2997
2998/**
2999 * ice_ena_misc_vector - enable the non-queue interrupts
3000 * @pf: board private structure
3001 */
3002static void ice_ena_misc_vector(struct ice_pf *pf)
3003{
3004	struct ice_hw *hw = &pf->hw;
 
3005	u32 val;
3006
3007	/* Disable anti-spoof detection interrupt to prevent spurious event
3008	 * interrupts during a function reset. Anti-spoof functionally is
3009	 * still supported.
3010	 */
3011	val = rd32(hw, GL_MDCK_TX_TDPU);
3012	val |= GL_MDCK_TX_TDPU_RCU_ANTISPOOF_ITR_DIS_M;
3013	wr32(hw, GL_MDCK_TX_TDPU, val);
3014
3015	/* clear things first */
3016	wr32(hw, PFINT_OICR_ENA, 0);	/* disable all */
3017	rd32(hw, PFINT_OICR);		/* read to clear */
3018
3019	val = (PFINT_OICR_ECC_ERR_M |
3020	       PFINT_OICR_MAL_DETECT_M |
3021	       PFINT_OICR_GRST_M |
3022	       PFINT_OICR_PCI_EXCEPTION_M |
3023	       PFINT_OICR_VFLR_M |
3024	       PFINT_OICR_HMC_ERR_M |
3025	       PFINT_OICR_PE_PUSH_M |
3026	       PFINT_OICR_PE_CRITERR_M);
3027
3028	wr32(hw, PFINT_OICR_ENA, val);
3029
3030	/* SW_ITR_IDX = 0, but don't change INTENA */
3031	wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
 
 
 
 
 
 
3032	     GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
3033}
3034
3035/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3036 * ice_misc_intr - misc interrupt handler
3037 * @irq: interrupt number
3038 * @data: pointer to a q_vector
3039 */
3040static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
3041{
3042	struct ice_pf *pf = (struct ice_pf *)data;
 
3043	struct ice_hw *hw = &pf->hw;
3044	irqreturn_t ret = IRQ_NONE;
3045	struct device *dev;
3046	u32 oicr, ena_mask;
3047
3048	dev = ice_pf_to_dev(pf);
3049	set_bit(ICE_ADMINQ_EVENT_PENDING, pf->state);
3050	set_bit(ICE_MAILBOXQ_EVENT_PENDING, pf->state);
3051	set_bit(ICE_SIDEBANDQ_EVENT_PENDING, pf->state);
3052
3053	oicr = rd32(hw, PFINT_OICR);
3054	ena_mask = rd32(hw, PFINT_OICR_ENA);
3055
3056	if (oicr & PFINT_OICR_SWINT_M) {
3057		ena_mask &= ~PFINT_OICR_SWINT_M;
3058		pf->sw_int_count++;
3059	}
3060
3061	if (oicr & PFINT_OICR_MAL_DETECT_M) {
3062		ena_mask &= ~PFINT_OICR_MAL_DETECT_M;
3063		set_bit(ICE_MDD_EVENT_PENDING, pf->state);
3064	}
3065	if (oicr & PFINT_OICR_VFLR_M) {
3066		/* disable any further VFLR event notifications */
3067		if (test_bit(ICE_VF_RESETS_DISABLED, pf->state)) {
3068			u32 reg = rd32(hw, PFINT_OICR_ENA);
3069
3070			reg &= ~PFINT_OICR_VFLR_M;
3071			wr32(hw, PFINT_OICR_ENA, reg);
3072		} else {
3073			ena_mask &= ~PFINT_OICR_VFLR_M;
3074			set_bit(ICE_VFLR_EVENT_PENDING, pf->state);
3075		}
3076	}
3077
3078	if (oicr & PFINT_OICR_GRST_M) {
3079		u32 reset;
3080
3081		/* we have a reset warning */
3082		ena_mask &= ~PFINT_OICR_GRST_M;
3083		reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
3084			GLGEN_RSTAT_RESET_TYPE_S;
3085
3086		if (reset == ICE_RESET_CORER)
3087			pf->corer_count++;
3088		else if (reset == ICE_RESET_GLOBR)
3089			pf->globr_count++;
3090		else if (reset == ICE_RESET_EMPR)
3091			pf->empr_count++;
3092		else
3093			dev_dbg(dev, "Invalid reset type %d\n", reset);
3094
3095		/* If a reset cycle isn't already in progress, we set a bit in
3096		 * pf->state so that the service task can start a reset/rebuild.
3097		 */
3098		if (!test_and_set_bit(ICE_RESET_OICR_RECV, pf->state)) {
3099			if (reset == ICE_RESET_CORER)
3100				set_bit(ICE_CORER_RECV, pf->state);
3101			else if (reset == ICE_RESET_GLOBR)
3102				set_bit(ICE_GLOBR_RECV, pf->state);
3103			else
3104				set_bit(ICE_EMPR_RECV, pf->state);
3105
3106			/* There are couple of different bits at play here.
3107			 * hw->reset_ongoing indicates whether the hardware is
3108			 * in reset. This is set to true when a reset interrupt
3109			 * is received and set back to false after the driver
3110			 * has determined that the hardware is out of reset.
3111			 *
3112			 * ICE_RESET_OICR_RECV in pf->state indicates
3113			 * that a post reset rebuild is required before the
3114			 * driver is operational again. This is set above.
3115			 *
3116			 * As this is the start of the reset/rebuild cycle, set
3117			 * both to indicate that.
3118			 */
3119			hw->reset_ongoing = true;
3120		}
3121	}
3122
3123	if (oicr & PFINT_OICR_TSYN_TX_M) {
3124		ena_mask &= ~PFINT_OICR_TSYN_TX_M;
3125		if (!hw->reset_ongoing)
 
 
 
 
 
 
 
 
 
 
 
 
 
3126			ret = IRQ_WAKE_THREAD;
 
3127	}
3128
3129	if (oicr & PFINT_OICR_TSYN_EVNT_M) {
3130		u8 tmr_idx = hw->func_caps.ts_func_info.tmr_index_owned;
3131		u32 gltsyn_stat = rd32(hw, GLTSYN_STAT(tmr_idx));
3132
3133		/* Save EVENTs from GTSYN register */
3134		pf->ptp.ext_ts_irq |= gltsyn_stat & (GLTSYN_STAT_EVENT0_M |
3135						     GLTSYN_STAT_EVENT1_M |
3136						     GLTSYN_STAT_EVENT2_M);
3137		ena_mask &= ~PFINT_OICR_TSYN_EVNT_M;
3138		kthread_queue_work(pf->ptp.kworker, &pf->ptp.extts_work);
 
 
 
 
 
 
 
 
 
3139	}
3140
3141#define ICE_AUX_CRIT_ERR (PFINT_OICR_PE_CRITERR_M | PFINT_OICR_HMC_ERR_M | PFINT_OICR_PE_PUSH_M)
3142	if (oicr & ICE_AUX_CRIT_ERR) {
3143		pf->oicr_err_reg |= oicr;
3144		set_bit(ICE_AUX_ERR_PENDING, pf->state);
3145		ena_mask &= ~ICE_AUX_CRIT_ERR;
3146	}
3147
3148	/* Report any remaining unexpected interrupts */
3149	oicr &= ena_mask;
3150	if (oicr) {
3151		dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr);
3152		/* If a critical error is pending there is no choice but to
3153		 * reset the device.
3154		 */
3155		if (oicr & (PFINT_OICR_PCI_EXCEPTION_M |
3156			    PFINT_OICR_ECC_ERR_M)) {
3157			set_bit(ICE_PFR_REQ, pf->state);
3158			ice_service_task_schedule(pf);
3159		}
3160	}
3161	if (!ret)
3162		ret = IRQ_HANDLED;
3163
3164	ice_service_task_schedule(pf);
3165	ice_irq_dynamic_ena(hw, NULL, NULL);
 
3166
3167	return ret;
3168}
3169
3170/**
3171 * ice_misc_intr_thread_fn - misc interrupt thread function
3172 * @irq: interrupt number
3173 * @data: pointer to a q_vector
3174 */
3175static irqreturn_t ice_misc_intr_thread_fn(int __always_unused irq, void *data)
3176{
3177	struct ice_pf *pf = data;
 
 
 
3178
3179	if (ice_is_reset_in_progress(pf->state))
3180		return IRQ_HANDLED;
 
 
 
 
 
 
 
 
 
 
3181
3182	while (!ice_ptp_process_ts(pf))
3183		usleep_range(50, 100);
3184
3185	return IRQ_HANDLED;
3186}
3187
3188/**
3189 * ice_dis_ctrlq_interrupts - disable control queue interrupts
3190 * @hw: pointer to HW structure
3191 */
3192static void ice_dis_ctrlq_interrupts(struct ice_hw *hw)
3193{
3194	/* disable Admin queue Interrupt causes */
3195	wr32(hw, PFINT_FW_CTL,
3196	     rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M);
3197
3198	/* disable Mailbox queue Interrupt causes */
3199	wr32(hw, PFINT_MBX_CTL,
3200	     rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M);
3201
3202	wr32(hw, PFINT_SB_CTL,
3203	     rd32(hw, PFINT_SB_CTL) & ~PFINT_SB_CTL_CAUSE_ENA_M);
3204
3205	/* disable Control queue Interrupt causes */
3206	wr32(hw, PFINT_OICR_CTL,
3207	     rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M);
3208
3209	ice_flush(hw);
3210}
3211
3212/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3213 * ice_free_irq_msix_misc - Unroll misc vector setup
3214 * @pf: board private structure
3215 */
3216static void ice_free_irq_msix_misc(struct ice_pf *pf)
3217{
 
3218	struct ice_hw *hw = &pf->hw;
3219
3220	ice_dis_ctrlq_interrupts(hw);
3221
3222	/* disable OICR interrupt */
3223	wr32(hw, PFINT_OICR_ENA, 0);
3224	ice_flush(hw);
3225
3226	if (pf->msix_entries) {
3227		synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
3228		devm_free_irq(ice_pf_to_dev(pf),
3229			      pf->msix_entries[pf->oicr_idx].vector, pf);
3230	}
3231
3232	pf->num_avail_sw_msix += 1;
3233	ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
 
3234}
3235
3236/**
3237 * ice_ena_ctrlq_interrupts - enable control queue interrupts
3238 * @hw: pointer to HW structure
3239 * @reg_idx: HW vector index to associate the control queue interrupts with
3240 */
3241static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx)
3242{
3243	u32 val;
3244
3245	val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
3246	       PFINT_OICR_CTL_CAUSE_ENA_M);
3247	wr32(hw, PFINT_OICR_CTL, val);
3248
3249	/* enable Admin queue Interrupt causes */
3250	val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) |
3251	       PFINT_FW_CTL_CAUSE_ENA_M);
3252	wr32(hw, PFINT_FW_CTL, val);
3253
3254	/* enable Mailbox queue Interrupt causes */
3255	val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) |
3256	       PFINT_MBX_CTL_CAUSE_ENA_M);
3257	wr32(hw, PFINT_MBX_CTL, val);
3258
3259	/* This enables Sideband queue Interrupt causes */
3260	val = ((reg_idx & PFINT_SB_CTL_MSIX_INDX_M) |
3261	       PFINT_SB_CTL_CAUSE_ENA_M);
3262	wr32(hw, PFINT_SB_CTL, val);
 
 
3263
3264	ice_flush(hw);
3265}
3266
3267/**
3268 * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
3269 * @pf: board private structure
3270 *
3271 * This sets up the handler for MSIX 0, which is used to manage the
3272 * non-queue interrupts, e.g. AdminQ and errors. This is not used
3273 * when in MSI or Legacy interrupt mode.
3274 */
3275static int ice_req_irq_msix_misc(struct ice_pf *pf)
3276{
3277	struct device *dev = ice_pf_to_dev(pf);
3278	struct ice_hw *hw = &pf->hw;
3279	int oicr_idx, err = 0;
 
 
3280
3281	if (!pf->int_name[0])
3282		snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
3283			 dev_driver_string(dev), dev_name(dev));
3284
 
 
 
3285	/* Do not request IRQ but do enable OICR interrupt since settings are
3286	 * lost during reset. Note that this function is called only during
3287	 * rebuild path and not while reset is in progress.
3288	 */
3289	if (ice_is_reset_in_progress(pf->state))
3290		goto skip_req_irq;
3291
3292	/* reserve one vector in irq_tracker for misc interrupts */
3293	oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
3294	if (oicr_idx < 0)
3295		return oicr_idx;
3296
3297	pf->num_avail_sw_msix -= 1;
3298	pf->oicr_idx = (u16)oicr_idx;
3299
3300	err = devm_request_threaded_irq(dev,
3301					pf->msix_entries[pf->oicr_idx].vector,
3302					ice_misc_intr, ice_misc_intr_thread_fn,
3303					0, pf->int_name, pf);
3304	if (err) {
3305		dev_err(dev, "devm_request_threaded_irq for %s failed: %d\n",
3306			pf->int_name, err);
3307		ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
3308		pf->num_avail_sw_msix += 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3309		return err;
3310	}
3311
3312skip_req_irq:
3313	ice_ena_misc_vector(pf);
3314
3315	ice_ena_ctrlq_interrupts(hw, pf->oicr_idx);
3316	wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
 
 
 
 
 
 
3317	     ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S);
3318
3319	ice_flush(hw);
3320	ice_irq_dynamic_ena(hw, NULL, NULL);
3321
3322	return 0;
3323}
3324
3325/**
3326 * ice_napi_add - register NAPI handler for the VSI
3327 * @vsi: VSI for which NAPI handler is to be registered
3328 *
3329 * This function is only called in the driver's load path. Registering the NAPI
3330 * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume,
3331 * reset/rebuild, etc.)
3332 */
3333static void ice_napi_add(struct ice_vsi *vsi)
3334{
3335	int v_idx;
3336
3337	if (!vsi->netdev)
3338		return;
3339
3340	ice_for_each_q_vector(vsi, v_idx)
3341		netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi,
3342			       ice_napi_poll);
 
 
3343}
3344
3345/**
3346 * ice_set_ops - set netdev and ethtools ops for the given netdev
3347 * @netdev: netdev instance
3348 */
3349static void ice_set_ops(struct net_device *netdev)
3350{
 
3351	struct ice_pf *pf = ice_netdev_to_pf(netdev);
3352
3353	if (ice_is_safe_mode(pf)) {
3354		netdev->netdev_ops = &ice_netdev_safe_mode_ops;
3355		ice_set_ethtool_safe_mode_ops(netdev);
3356		return;
3357	}
3358
3359	netdev->netdev_ops = &ice_netdev_ops;
3360	netdev->udp_tunnel_nic_info = &pf->hw.udp_tunnel_nic;
 
3361	ice_set_ethtool_ops(netdev);
 
 
 
 
 
 
 
 
3362}
3363
3364/**
3365 * ice_set_netdev_features - set features for the given netdev
3366 * @netdev: netdev instance
3367 */
3368static void ice_set_netdev_features(struct net_device *netdev)
3369{
3370	struct ice_pf *pf = ice_netdev_to_pf(netdev);
3371	bool is_dvm_ena = ice_is_dvm_ena(&pf->hw);
3372	netdev_features_t csumo_features;
3373	netdev_features_t vlano_features;
3374	netdev_features_t dflt_features;
3375	netdev_features_t tso_features;
3376
3377	if (ice_is_safe_mode(pf)) {
3378		/* safe mode */
3379		netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA;
3380		netdev->hw_features = netdev->features;
3381		return;
3382	}
3383
3384	dflt_features = NETIF_F_SG	|
3385			NETIF_F_HIGHDMA	|
3386			NETIF_F_NTUPLE	|
3387			NETIF_F_RXHASH;
3388
3389	csumo_features = NETIF_F_RXCSUM	  |
3390			 NETIF_F_IP_CSUM  |
3391			 NETIF_F_SCTP_CRC |
3392			 NETIF_F_IPV6_CSUM;
3393
3394	vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
3395			 NETIF_F_HW_VLAN_CTAG_TX     |
3396			 NETIF_F_HW_VLAN_CTAG_RX;
3397
3398	/* Enable CTAG/STAG filtering by default in Double VLAN Mode (DVM) */
3399	if (is_dvm_ena)
3400		vlano_features |= NETIF_F_HW_VLAN_STAG_FILTER;
3401
3402	tso_features = NETIF_F_TSO			|
3403		       NETIF_F_TSO_ECN			|
3404		       NETIF_F_TSO6			|
3405		       NETIF_F_GSO_GRE			|
3406		       NETIF_F_GSO_UDP_TUNNEL		|
3407		       NETIF_F_GSO_GRE_CSUM		|
3408		       NETIF_F_GSO_UDP_TUNNEL_CSUM	|
3409		       NETIF_F_GSO_PARTIAL		|
3410		       NETIF_F_GSO_IPXIP4		|
3411		       NETIF_F_GSO_IPXIP6		|
3412		       NETIF_F_GSO_UDP_L4;
3413
3414	netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3415					NETIF_F_GSO_GRE_CSUM;
3416	/* set features that user can change */
3417	netdev->hw_features = dflt_features | csumo_features |
3418			      vlano_features | tso_features;
3419
3420	/* add support for HW_CSUM on packets with MPLS header */
3421	netdev->mpls_features =  NETIF_F_HW_CSUM |
3422				 NETIF_F_TSO     |
3423				 NETIF_F_TSO6;
3424
3425	/* enable features */
3426	netdev->features |= netdev->hw_features;
3427
3428	netdev->hw_features |= NETIF_F_HW_TC;
3429	netdev->hw_features |= NETIF_F_LOOPBACK;
3430
3431	/* encap and VLAN devices inherit default, csumo and tso features */
3432	netdev->hw_enc_features |= dflt_features | csumo_features |
3433				   tso_features;
3434	netdev->vlan_features |= dflt_features | csumo_features |
3435				 tso_features;
3436
3437	/* advertise support but don't enable by default since only one type of
3438	 * VLAN offload can be enabled at a time (i.e. CTAG or STAG). When one
3439	 * type turns on the other has to be turned off. This is enforced by the
3440	 * ice_fix_features() ndo callback.
3441	 */
3442	if (is_dvm_ena)
3443		netdev->hw_features |= NETIF_F_HW_VLAN_STAG_RX |
3444			NETIF_F_HW_VLAN_STAG_TX;
3445
3446	/* Leave CRC / FCS stripping enabled by default, but allow the value to
3447	 * be changed at runtime
3448	 */
3449	netdev->hw_features |= NETIF_F_RXFCS;
3450}
3451
3452/**
3453 * ice_cfg_netdev - Allocate, configure and register a netdev
3454 * @vsi: the VSI associated with the new netdev
3455 *
3456 * Returns 0 on success, negative value on failure
3457 */
3458static int ice_cfg_netdev(struct ice_vsi *vsi)
3459{
3460	struct ice_netdev_priv *np;
3461	struct net_device *netdev;
3462	u8 mac_addr[ETH_ALEN];
3463
3464	netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
3465				    vsi->alloc_rxq);
3466	if (!netdev)
3467		return -ENOMEM;
3468
3469	set_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
3470	vsi->netdev = netdev;
3471	np = netdev_priv(netdev);
3472	np->vsi = vsi;
3473
3474	ice_set_netdev_features(netdev);
3475
3476	ice_set_ops(netdev);
3477
3478	if (vsi->type == ICE_VSI_PF) {
3479		SET_NETDEV_DEV(netdev, ice_pf_to_dev(vsi->back));
3480		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
3481		eth_hw_addr_set(netdev, mac_addr);
3482		ether_addr_copy(netdev->perm_addr, mac_addr);
3483	}
3484
3485	netdev->priv_flags |= IFF_UNICAST_FLT;
3486
3487	/* Setup netdev TC information */
3488	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
3489
3490	/* setup watchdog timeout value to be 5 second */
3491	netdev->watchdog_timeo = 5 * HZ;
3492
3493	netdev->min_mtu = ETH_MIN_MTU;
3494	netdev->max_mtu = ICE_MAX_MTU;
3495
3496	return 0;
3497}
3498
3499/**
3500 * ice_fill_rss_lut - Fill the RSS lookup table with default values
3501 * @lut: Lookup table
3502 * @rss_table_size: Lookup table size
3503 * @rss_size: Range of queue number for hashing
3504 */
3505void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
3506{
3507	u16 i;
3508
3509	for (i = 0; i < rss_table_size; i++)
3510		lut[i] = i % rss_size;
3511}
3512
3513/**
3514 * ice_pf_vsi_setup - Set up a PF VSI
3515 * @pf: board private structure
3516 * @pi: pointer to the port_info instance
3517 *
3518 * Returns pointer to the successfully allocated VSI software struct
3519 * on success, otherwise returns NULL on failure.
3520 */
3521static struct ice_vsi *
3522ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3523{
3524	return ice_vsi_setup(pf, pi, ICE_VSI_PF, NULL, NULL);
 
 
 
 
 
 
3525}
3526
3527static struct ice_vsi *
3528ice_chnl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
3529		   struct ice_channel *ch)
3530{
3531	return ice_vsi_setup(pf, pi, ICE_VSI_CHNL, NULL, ch);
 
 
 
 
 
 
 
3532}
3533
3534/**
3535 * ice_ctrl_vsi_setup - Set up a control VSI
3536 * @pf: board private structure
3537 * @pi: pointer to the port_info instance
3538 *
3539 * Returns pointer to the successfully allocated VSI software struct
3540 * on success, otherwise returns NULL on failure.
3541 */
3542static struct ice_vsi *
3543ice_ctrl_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3544{
3545	return ice_vsi_setup(pf, pi, ICE_VSI_CTRL, NULL, NULL);
 
 
 
 
 
 
3546}
3547
3548/**
3549 * ice_lb_vsi_setup - Set up a loopback VSI
3550 * @pf: board private structure
3551 * @pi: pointer to the port_info instance
3552 *
3553 * Returns pointer to the successfully allocated VSI software struct
3554 * on success, otherwise returns NULL on failure.
3555 */
3556struct ice_vsi *
3557ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi)
3558{
3559	return ice_vsi_setup(pf, pi, ICE_VSI_LB, NULL, NULL);
 
 
 
 
 
 
3560}
3561
3562/**
3563 * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload
3564 * @netdev: network interface to be adjusted
3565 * @proto: VLAN TPID
3566 * @vid: VLAN ID to be added
3567 *
3568 * net_device_ops implementation for adding VLAN IDs
3569 */
3570static int
3571ice_vlan_rx_add_vid(struct net_device *netdev, __be16 proto, u16 vid)
3572{
3573	struct ice_netdev_priv *np = netdev_priv(netdev);
3574	struct ice_vsi_vlan_ops *vlan_ops;
3575	struct ice_vsi *vsi = np->vsi;
3576	struct ice_vlan vlan;
3577	int ret;
3578
3579	/* VLAN 0 is added by default during load/reset */
3580	if (!vid)
3581		return 0;
3582
3583	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3584		usleep_range(1000, 2000);
3585
3586	/* Add multicast promisc rule for the VLAN ID to be added if
3587	 * all-multicast is currently enabled.
3588	 */
3589	if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3590		ret = ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3591					       ICE_MCAST_VLAN_PROMISC_BITS,
3592					       vid);
3593		if (ret)
3594			goto finish;
3595	}
3596
3597	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3598
3599	/* Add a switch rule for this VLAN ID so its corresponding VLAN tagged
3600	 * packets aren't pruned by the device's internal switch on Rx
3601	 */
3602	vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3603	ret = vlan_ops->add_vlan(vsi, &vlan);
3604	if (ret)
3605		goto finish;
3606
3607	/* If all-multicast is currently enabled and this VLAN ID is only one
3608	 * besides VLAN-0 we have to update look-up type of multicast promisc
3609	 * rule for VLAN-0 from ICE_SW_LKUP_PROMISC to ICE_SW_LKUP_PROMISC_VLAN.
3610	 */
3611	if ((vsi->current_netdev_flags & IFF_ALLMULTI) &&
3612	    ice_vsi_num_non_zero_vlans(vsi) == 1) {
3613		ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3614					   ICE_MCAST_PROMISC_BITS, 0);
3615		ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3616					 ICE_MCAST_VLAN_PROMISC_BITS, 0);
3617	}
3618
3619finish:
3620	clear_bit(ICE_CFG_BUSY, vsi->state);
3621
3622	return ret;
3623}
3624
3625/**
3626 * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload
3627 * @netdev: network interface to be adjusted
3628 * @proto: VLAN TPID
3629 * @vid: VLAN ID to be removed
3630 *
3631 * net_device_ops implementation for removing VLAN IDs
3632 */
3633static int
3634ice_vlan_rx_kill_vid(struct net_device *netdev, __be16 proto, u16 vid)
3635{
3636	struct ice_netdev_priv *np = netdev_priv(netdev);
3637	struct ice_vsi_vlan_ops *vlan_ops;
3638	struct ice_vsi *vsi = np->vsi;
3639	struct ice_vlan vlan;
3640	int ret;
3641
3642	/* don't allow removal of VLAN 0 */
3643	if (!vid)
3644		return 0;
3645
3646	while (test_and_set_bit(ICE_CFG_BUSY, vsi->state))
3647		usleep_range(1000, 2000);
3648
3649	ret = ice_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3650				    ICE_MCAST_VLAN_PROMISC_BITS, vid);
3651	if (ret) {
3652		netdev_err(netdev, "Error clearing multicast promiscuous mode on VSI %i\n",
3653			   vsi->vsi_num);
3654		vsi->current_netdev_flags |= IFF_ALLMULTI;
3655	}
3656
3657	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
3658
3659	/* Make sure VLAN delete is successful before updating VLAN
3660	 * information
3661	 */
3662	vlan = ICE_VLAN(be16_to_cpu(proto), vid, 0);
3663	ret = vlan_ops->del_vlan(vsi, &vlan);
3664	if (ret)
3665		goto finish;
3666
3667	/* Remove multicast promisc rule for the removed VLAN ID if
3668	 * all-multicast is enabled.
3669	 */
3670	if (vsi->current_netdev_flags & IFF_ALLMULTI)
3671		ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3672					   ICE_MCAST_VLAN_PROMISC_BITS, vid);
3673
3674	if (!ice_vsi_has_non_zero_vlans(vsi)) {
3675		/* Update look-up type of multicast promisc rule for VLAN 0
3676		 * from ICE_SW_LKUP_PROMISC_VLAN to ICE_SW_LKUP_PROMISC when
3677		 * all-multicast is enabled and VLAN 0 is the only VLAN rule.
3678		 */
3679		if (vsi->current_netdev_flags & IFF_ALLMULTI) {
3680			ice_fltr_clear_vsi_promisc(&vsi->back->hw, vsi->idx,
3681						   ICE_MCAST_VLAN_PROMISC_BITS,
3682						   0);
3683			ice_fltr_set_vsi_promisc(&vsi->back->hw, vsi->idx,
3684						 ICE_MCAST_PROMISC_BITS, 0);
3685		}
3686	}
3687
3688finish:
3689	clear_bit(ICE_CFG_BUSY, vsi->state);
3690
3691	return ret;
3692}
3693
3694/**
3695 * ice_rep_indr_tc_block_unbind
3696 * @cb_priv: indirection block private data
3697 */
3698static void ice_rep_indr_tc_block_unbind(void *cb_priv)
3699{
3700	struct ice_indr_block_priv *indr_priv = cb_priv;
3701
3702	list_del(&indr_priv->list);
3703	kfree(indr_priv);
3704}
3705
3706/**
3707 * ice_tc_indir_block_unregister - Unregister TC indirect block notifications
3708 * @vsi: VSI struct which has the netdev
3709 */
3710static void ice_tc_indir_block_unregister(struct ice_vsi *vsi)
3711{
3712	struct ice_netdev_priv *np = netdev_priv(vsi->netdev);
3713
3714	flow_indr_dev_unregister(ice_indr_setup_tc_cb, np,
3715				 ice_rep_indr_tc_block_unbind);
3716}
3717
3718/**
3719 * ice_tc_indir_block_remove - clean indirect TC block notifications
3720 * @pf: PF structure
3721 */
3722static void ice_tc_indir_block_remove(struct ice_pf *pf)
3723{
3724	struct ice_vsi *pf_vsi = ice_get_main_vsi(pf);
3725
3726	if (!pf_vsi)
3727		return;
3728
3729	ice_tc_indir_block_unregister(pf_vsi);
3730}
3731
3732/**
3733 * ice_tc_indir_block_register - Register TC indirect block notifications
3734 * @vsi: VSI struct which has the netdev
3735 *
3736 * Returns 0 on success, negative value on failure
3737 */
3738static int ice_tc_indir_block_register(struct ice_vsi *vsi)
3739{
3740	struct ice_netdev_priv *np;
3741
3742	if (!vsi || !vsi->netdev)
3743		return -EINVAL;
3744
3745	np = netdev_priv(vsi->netdev);
3746
3747	INIT_LIST_HEAD(&np->tc_indr_block_priv_list);
3748	return flow_indr_dev_register(ice_indr_setup_tc_cb, np);
3749}
3750
3751/**
3752 * ice_setup_pf_sw - Setup the HW switch on startup or after reset
3753 * @pf: board private structure
3754 *
3755 * Returns 0 on success, negative value on failure
3756 */
3757static int ice_setup_pf_sw(struct ice_pf *pf)
3758{
3759	struct device *dev = ice_pf_to_dev(pf);
3760	bool dvm = ice_is_dvm_ena(&pf->hw);
3761	struct ice_vsi *vsi;
3762	int status;
3763
3764	if (ice_is_reset_in_progress(pf->state))
3765		return -EBUSY;
3766
3767	status = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
3768	if (status)
3769		return -EIO;
3770
3771	vsi = ice_pf_vsi_setup(pf, pf->hw.port_info);
3772	if (!vsi)
3773		return -ENOMEM;
3774
3775	/* init channel list */
3776	INIT_LIST_HEAD(&vsi->ch_list);
3777
3778	status = ice_cfg_netdev(vsi);
3779	if (status)
3780		goto unroll_vsi_setup;
3781	/* netdev has to be configured before setting frame size */
3782	ice_vsi_cfg_frame_size(vsi);
3783
3784	/* init indirect block notifications */
3785	status = ice_tc_indir_block_register(vsi);
3786	if (status) {
3787		dev_err(dev, "Failed to register netdev notifier\n");
3788		goto unroll_cfg_netdev;
3789	}
3790
3791	/* Setup DCB netlink interface */
3792	ice_dcbnl_setup(vsi);
3793
3794	/* registering the NAPI handler requires both the queues and
3795	 * netdev to be created, which are done in ice_pf_vsi_setup()
3796	 * and ice_cfg_netdev() respectively
3797	 */
3798	ice_napi_add(vsi);
3799
3800	status = ice_init_mac_fltr(pf);
3801	if (status)
3802		goto unroll_napi_add;
3803
3804	return 0;
3805
3806unroll_napi_add:
3807	ice_tc_indir_block_unregister(vsi);
3808unroll_cfg_netdev:
3809	if (vsi) {
3810		ice_napi_del(vsi);
3811		if (vsi->netdev) {
3812			clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
3813			free_netdev(vsi->netdev);
3814			vsi->netdev = NULL;
3815		}
3816	}
3817
3818unroll_vsi_setup:
3819	ice_vsi_release(vsi);
3820	return status;
3821}
3822
3823/**
3824 * ice_get_avail_q_count - Get count of queues in use
3825 * @pf_qmap: bitmap to get queue use count from
3826 * @lock: pointer to a mutex that protects access to pf_qmap
3827 * @size: size of the bitmap
3828 */
3829static u16
3830ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size)
3831{
3832	unsigned long bit;
3833	u16 count = 0;
3834
3835	mutex_lock(lock);
3836	for_each_clear_bit(bit, pf_qmap, size)
3837		count++;
3838	mutex_unlock(lock);
3839
3840	return count;
3841}
3842
3843/**
3844 * ice_get_avail_txq_count - Get count of Tx queues in use
3845 * @pf: pointer to an ice_pf instance
3846 */
3847u16 ice_get_avail_txq_count(struct ice_pf *pf)
3848{
3849	return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex,
3850				     pf->max_pf_txqs);
3851}
3852
3853/**
3854 * ice_get_avail_rxq_count - Get count of Rx queues in use
3855 * @pf: pointer to an ice_pf instance
3856 */
3857u16 ice_get_avail_rxq_count(struct ice_pf *pf)
3858{
3859	return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex,
3860				     pf->max_pf_rxqs);
3861}
3862
3863/**
3864 * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
3865 * @pf: board private structure to initialize
3866 */
3867static void ice_deinit_pf(struct ice_pf *pf)
3868{
3869	ice_service_task_stop(pf);
 
3870	mutex_destroy(&pf->adev_mutex);
3871	mutex_destroy(&pf->sw_mutex);
3872	mutex_destroy(&pf->tc_mutex);
3873	mutex_destroy(&pf->avail_q_mutex);
3874	mutex_destroy(&pf->vfs.table_lock);
3875
3876	if (pf->avail_txqs) {
3877		bitmap_free(pf->avail_txqs);
3878		pf->avail_txqs = NULL;
3879	}
3880
3881	if (pf->avail_rxqs) {
3882		bitmap_free(pf->avail_rxqs);
3883		pf->avail_rxqs = NULL;
3884	}
3885
3886	if (pf->ptp.clock)
3887		ptp_clock_unregister(pf->ptp.clock);
3888}
3889
3890/**
3891 * ice_set_pf_caps - set PFs capability flags
3892 * @pf: pointer to the PF instance
3893 */
3894static void ice_set_pf_caps(struct ice_pf *pf)
3895{
3896	struct ice_hw_func_caps *func_caps = &pf->hw.func_caps;
3897
3898	clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3899	if (func_caps->common_cap.rdma)
3900		set_bit(ICE_FLAG_RDMA_ENA, pf->flags);
3901	clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3902	if (func_caps->common_cap.dcb)
3903		set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
3904	clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3905	if (func_caps->common_cap.sr_iov_1_1) {
3906		set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags);
3907		pf->vfs.num_supported = min_t(int, func_caps->num_allocd_vfs,
3908					      ICE_MAX_SRIOV_VFS);
3909	}
3910	clear_bit(ICE_FLAG_RSS_ENA, pf->flags);
3911	if (func_caps->common_cap.rss_table_size)
3912		set_bit(ICE_FLAG_RSS_ENA, pf->flags);
3913
3914	clear_bit(ICE_FLAG_FD_ENA, pf->flags);
3915	if (func_caps->fd_fltr_guar > 0 || func_caps->fd_fltr_best_effort > 0) {
3916		u16 unused;
3917
3918		/* ctrl_vsi_idx will be set to a valid value when flow director
3919		 * is setup by ice_init_fdir
3920		 */
3921		pf->ctrl_vsi_idx = ICE_NO_VSI;
3922		set_bit(ICE_FLAG_FD_ENA, pf->flags);
3923		/* force guaranteed filter pool for PF */
3924		ice_alloc_fd_guar_item(&pf->hw, &unused,
3925				       func_caps->fd_fltr_guar);
3926		/* force shared filter pool for PF */
3927		ice_alloc_fd_shrd_item(&pf->hw, &unused,
3928				       func_caps->fd_fltr_best_effort);
3929	}
3930
3931	clear_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
3932	if (func_caps->common_cap.ieee_1588)
 
3933		set_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags);
3934
3935	pf->max_pf_txqs = func_caps->common_cap.num_txq;
3936	pf->max_pf_rxqs = func_caps->common_cap.num_rxq;
3937}
3938
3939/**
3940 * ice_init_pf - Initialize general software structures (struct ice_pf)
3941 * @pf: board private structure to initialize
3942 */
3943static int ice_init_pf(struct ice_pf *pf)
3944{
3945	ice_set_pf_caps(pf);
3946
3947	mutex_init(&pf->sw_mutex);
3948	mutex_init(&pf->tc_mutex);
3949	mutex_init(&pf->adev_mutex);
 
3950
3951	INIT_HLIST_HEAD(&pf->aq_wait_list);
3952	spin_lock_init(&pf->aq_wait_lock);
3953	init_waitqueue_head(&pf->aq_wait_queue);
3954
3955	init_waitqueue_head(&pf->reset_wait_queue);
3956
3957	/* setup service timer and periodic service task */
3958	timer_setup(&pf->serv_tmr, ice_service_timer, 0);
3959	pf->serv_tmr_period = HZ;
3960	INIT_WORK(&pf->serv_task, ice_service_task);
3961	clear_bit(ICE_SERVICE_SCHED, pf->state);
3962
3963	mutex_init(&pf->avail_q_mutex);
3964	pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL);
3965	if (!pf->avail_txqs)
3966		return -ENOMEM;
3967
3968	pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL);
3969	if (!pf->avail_rxqs) {
3970		bitmap_free(pf->avail_txqs);
3971		pf->avail_txqs = NULL;
3972		return -ENOMEM;
3973	}
3974
3975	mutex_init(&pf->vfs.table_lock);
3976	hash_init(pf->vfs.table);
3977
3978	return 0;
3979}
3980
3981/**
3982 * ice_reduce_msix_usage - Reduce usage of MSI-X vectors
3983 * @pf: board private structure
3984 * @v_remain: number of remaining MSI-X vectors to be distributed
3985 *
3986 * Reduce the usage of MSI-X vectors when entire request cannot be fulfilled.
3987 * pf->num_lan_msix and pf->num_rdma_msix values are set based on number of
3988 * remaining vectors.
3989 */
3990static void ice_reduce_msix_usage(struct ice_pf *pf, int v_remain)
3991{
3992	int v_rdma;
3993
3994	if (!ice_is_rdma_ena(pf)) {
3995		pf->num_lan_msix = v_remain;
3996		return;
3997	}
3998
3999	/* RDMA needs at least 1 interrupt in addition to AEQ MSIX */
4000	v_rdma = ICE_RDMA_NUM_AEQ_MSIX + 1;
4001
4002	if (v_remain < ICE_MIN_LAN_TXRX_MSIX + ICE_MIN_RDMA_MSIX) {
4003		dev_warn(ice_pf_to_dev(pf), "Not enough MSI-X vectors to support RDMA.\n");
4004		clear_bit(ICE_FLAG_RDMA_ENA, pf->flags);
4005
4006		pf->num_rdma_msix = 0;
4007		pf->num_lan_msix = ICE_MIN_LAN_TXRX_MSIX;
4008	} else if ((v_remain < ICE_MIN_LAN_TXRX_MSIX + v_rdma) ||
4009		   (v_remain - v_rdma < v_rdma)) {
4010		/* Support minimum RDMA and give remaining vectors to LAN MSIX */
4011		pf->num_rdma_msix = ICE_MIN_RDMA_MSIX;
4012		pf->num_lan_msix = v_remain - ICE_MIN_RDMA_MSIX;
4013	} else {
4014		/* Split remaining MSIX with RDMA after accounting for AEQ MSIX
4015		 */
4016		pf->num_rdma_msix = (v_remain - ICE_RDMA_NUM_AEQ_MSIX) / 2 +
4017				    ICE_RDMA_NUM_AEQ_MSIX;
4018		pf->num_lan_msix = v_remain - pf->num_rdma_msix;
4019	}
4020}
4021
4022/**
4023 * ice_ena_msix_range - Request a range of MSIX vectors from the OS
4024 * @pf: board private structure
4025 *
4026 * Compute the number of MSIX vectors wanted and request from the OS. Adjust
4027 * device usage if there are not enough vectors. Return the number of vectors
4028 * reserved or negative on failure.
4029 */
4030static int ice_ena_msix_range(struct ice_pf *pf)
4031{
4032	int num_cpus, hw_num_msix, v_other, v_wanted, v_actual;
4033	struct device *dev = ice_pf_to_dev(pf);
4034	int err, i;
4035
4036	hw_num_msix = pf->hw.func_caps.common_cap.num_msix_vectors;
4037	num_cpus = num_online_cpus();
4038
4039	/* LAN miscellaneous handler */
4040	v_other = ICE_MIN_LAN_OICR_MSIX;
4041
4042	/* Flow Director */
4043	if (test_bit(ICE_FLAG_FD_ENA, pf->flags))
4044		v_other += ICE_FDIR_MSIX;
4045
4046	/* switchdev */
4047	v_other += ICE_ESWITCH_MSIX;
4048
4049	v_wanted = v_other;
4050
4051	/* LAN traffic */
4052	pf->num_lan_msix = num_cpus;
4053	v_wanted += pf->num_lan_msix;
4054
4055	/* RDMA auxiliary driver */
4056	if (ice_is_rdma_ena(pf)) {
4057		pf->num_rdma_msix = num_cpus + ICE_RDMA_NUM_AEQ_MSIX;
4058		v_wanted += pf->num_rdma_msix;
4059	}
4060
4061	if (v_wanted > hw_num_msix) {
4062		int v_remain;
4063
4064		dev_warn(dev, "not enough device MSI-X vectors. wanted = %d, available = %d\n",
4065			 v_wanted, hw_num_msix);
4066
4067		if (hw_num_msix < ICE_MIN_MSIX) {
4068			err = -ERANGE;
4069			goto exit_err;
4070		}
4071
4072		v_remain = hw_num_msix - v_other;
4073		if (v_remain < ICE_MIN_LAN_TXRX_MSIX) {
4074			v_other = ICE_MIN_MSIX - ICE_MIN_LAN_TXRX_MSIX;
4075			v_remain = ICE_MIN_LAN_TXRX_MSIX;
4076		}
4077
4078		ice_reduce_msix_usage(pf, v_remain);
4079		v_wanted = pf->num_lan_msix + pf->num_rdma_msix + v_other;
4080
4081		dev_notice(dev, "Reducing request to %d MSI-X vectors for LAN traffic.\n",
4082			   pf->num_lan_msix);
4083		if (ice_is_rdma_ena(pf))
4084			dev_notice(dev, "Reducing request to %d MSI-X vectors for RDMA.\n",
4085				   pf->num_rdma_msix);
4086	}
4087
4088	pf->msix_entries = devm_kcalloc(dev, v_wanted,
4089					sizeof(*pf->msix_entries), GFP_KERNEL);
4090	if (!pf->msix_entries) {
4091		err = -ENOMEM;
4092		goto exit_err;
4093	}
4094
4095	for (i = 0; i < v_wanted; i++)
4096		pf->msix_entries[i].entry = i;
4097
4098	/* actually reserve the vectors */
4099	v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
4100					 ICE_MIN_MSIX, v_wanted);
4101	if (v_actual < 0) {
4102		dev_err(dev, "unable to reserve MSI-X vectors\n");
4103		err = v_actual;
4104		goto msix_err;
4105	}
4106
4107	if (v_actual < v_wanted) {
4108		dev_warn(dev, "not enough OS MSI-X vectors. requested = %d, obtained = %d\n",
4109			 v_wanted, v_actual);
4110
4111		if (v_actual < ICE_MIN_MSIX) {
4112			/* error if we can't get minimum vectors */
4113			pci_disable_msix(pf->pdev);
4114			err = -ERANGE;
4115			goto msix_err;
4116		} else {
4117			int v_remain = v_actual - v_other;
4118
4119			if (v_remain < ICE_MIN_LAN_TXRX_MSIX)
4120				v_remain = ICE_MIN_LAN_TXRX_MSIX;
4121
4122			ice_reduce_msix_usage(pf, v_remain);
4123
4124			dev_notice(dev, "Enabled %d MSI-X vectors for LAN traffic.\n",
4125				   pf->num_lan_msix);
4126
4127			if (ice_is_rdma_ena(pf))
4128				dev_notice(dev, "Enabled %d MSI-X vectors for RDMA.\n",
4129					   pf->num_rdma_msix);
4130		}
4131	}
4132
4133	return v_actual;
4134
4135msix_err:
4136	devm_kfree(dev, pf->msix_entries);
4137
4138exit_err:
4139	pf->num_rdma_msix = 0;
4140	pf->num_lan_msix = 0;
4141	return err;
4142}
4143
4144/**
4145 * ice_dis_msix - Disable MSI-X interrupt setup in OS
4146 * @pf: board private structure
4147 */
4148static void ice_dis_msix(struct ice_pf *pf)
4149{
4150	pci_disable_msix(pf->pdev);
4151	devm_kfree(ice_pf_to_dev(pf), pf->msix_entries);
4152	pf->msix_entries = NULL;
4153}
4154
4155/**
4156 * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
4157 * @pf: board private structure
4158 */
4159static void ice_clear_interrupt_scheme(struct ice_pf *pf)
4160{
4161	ice_dis_msix(pf);
4162
4163	if (pf->irq_tracker) {
4164		devm_kfree(ice_pf_to_dev(pf), pf->irq_tracker);
4165		pf->irq_tracker = NULL;
4166	}
4167}
4168
4169/**
4170 * ice_init_interrupt_scheme - Determine proper interrupt scheme
4171 * @pf: board private structure to initialize
4172 */
4173static int ice_init_interrupt_scheme(struct ice_pf *pf)
4174{
4175	int vectors;
4176
4177	vectors = ice_ena_msix_range(pf);
4178
4179	if (vectors < 0)
4180		return vectors;
4181
4182	/* set up vector assignment tracking */
4183	pf->irq_tracker = devm_kzalloc(ice_pf_to_dev(pf),
4184				       struct_size(pf->irq_tracker, list, vectors),
4185				       GFP_KERNEL);
4186	if (!pf->irq_tracker) {
4187		ice_dis_msix(pf);
4188		return -ENOMEM;
4189	}
4190
4191	/* populate SW interrupts pool with number of OS granted IRQs. */
4192	pf->num_avail_sw_msix = (u16)vectors;
4193	pf->irq_tracker->num_entries = (u16)vectors;
4194	pf->irq_tracker->end = pf->irq_tracker->num_entries;
4195
4196	return 0;
4197}
4198
4199/**
4200 * ice_is_wol_supported - check if WoL is supported
4201 * @hw: pointer to hardware info
4202 *
4203 * Check if WoL is supported based on the HW configuration.
4204 * Returns true if NVM supports and enables WoL for this port, false otherwise
4205 */
4206bool ice_is_wol_supported(struct ice_hw *hw)
4207{
4208	u16 wol_ctrl;
4209
4210	/* A bit set to 1 in the NVM Software Reserved Word 2 (WoL control
4211	 * word) indicates WoL is not supported on the corresponding PF ID.
4212	 */
4213	if (ice_read_sr_word(hw, ICE_SR_NVM_WOL_CFG, &wol_ctrl))
4214		return false;
4215
4216	return !(BIT(hw->port_info->lport) & wol_ctrl);
4217}
4218
4219/**
4220 * ice_vsi_recfg_qs - Change the number of queues on a VSI
4221 * @vsi: VSI being changed
4222 * @new_rx: new number of Rx queues
4223 * @new_tx: new number of Tx queues
4224 * @locked: is adev device_lock held
4225 *
4226 * Only change the number of queues if new_tx, or new_rx is non-0.
4227 *
4228 * Returns 0 on success.
4229 */
4230int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx, bool locked)
4231{
4232	struct ice_pf *pf = vsi->back;
4233	int err = 0, timeout = 50;
4234
4235	if (!new_rx && !new_tx)
4236		return -EINVAL;
4237
4238	while (test_and_set_bit(ICE_CFG_BUSY, pf->state)) {
4239		timeout--;
4240		if (!timeout)
4241			return -EBUSY;
4242		usleep_range(1000, 2000);
4243	}
4244
4245	if (new_tx)
4246		vsi->req_txq = (u16)new_tx;
4247	if (new_rx)
4248		vsi->req_rxq = (u16)new_rx;
4249
4250	/* set for the next time the netdev is started */
4251	if (!netif_running(vsi->netdev)) {
4252		ice_vsi_rebuild(vsi, false);
4253		dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n");
4254		goto done;
4255	}
4256
4257	ice_vsi_close(vsi);
4258	ice_vsi_rebuild(vsi, false);
4259	ice_pf_dcb_recfg(pf, locked);
4260	ice_vsi_open(vsi);
4261done:
4262	clear_bit(ICE_CFG_BUSY, pf->state);
4263	return err;
4264}
4265
4266/**
4267 * ice_set_safe_mode_vlan_cfg - configure PF VSI to allow all VLANs in safe mode
4268 * @pf: PF to configure
4269 *
4270 * No VLAN offloads/filtering are advertised in safe mode so make sure the PF
4271 * VSI can still Tx/Rx VLAN tagged packets.
4272 */
4273static void ice_set_safe_mode_vlan_cfg(struct ice_pf *pf)
4274{
4275	struct ice_vsi *vsi = ice_get_main_vsi(pf);
4276	struct ice_vsi_ctx *ctxt;
4277	struct ice_hw *hw;
4278	int status;
4279
4280	if (!vsi)
4281		return;
4282
4283	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
4284	if (!ctxt)
4285		return;
4286
4287	hw = &pf->hw;
4288	ctxt->info = vsi->info;
4289
4290	ctxt->info.valid_sections =
4291		cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID |
4292			    ICE_AQ_VSI_PROP_SECURITY_VALID |
4293			    ICE_AQ_VSI_PROP_SW_VALID);
4294
4295	/* disable VLAN anti-spoof */
4296	ctxt->info.sec_flags &= ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
4297				  ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
4298
4299	/* disable VLAN pruning and keep all other settings */
4300	ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
4301
4302	/* allow all VLANs on Tx and don't strip on Rx */
4303	ctxt->info.inner_vlan_flags = ICE_AQ_VSI_INNER_VLAN_TX_MODE_ALL |
4304		ICE_AQ_VSI_INNER_VLAN_EMODE_NOTHING;
4305
4306	status = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
4307	if (status) {
4308		dev_err(ice_pf_to_dev(vsi->back), "Failed to update VSI for safe mode VLANs, err %d aq_err %s\n",
4309			status, ice_aq_str(hw->adminq.sq_last_status));
4310	} else {
4311		vsi->info.sec_flags = ctxt->info.sec_flags;
4312		vsi->info.sw_flags2 = ctxt->info.sw_flags2;
4313		vsi->info.inner_vlan_flags = ctxt->info.inner_vlan_flags;
4314	}
4315
4316	kfree(ctxt);
4317}
4318
4319/**
4320 * ice_log_pkg_init - log result of DDP package load
4321 * @hw: pointer to hardware info
4322 * @state: state of package load
4323 */
4324static void ice_log_pkg_init(struct ice_hw *hw, enum ice_ddp_state state)
4325{
4326	struct ice_pf *pf = hw->back;
4327	struct device *dev;
4328
4329	dev = ice_pf_to_dev(pf);
4330
4331	switch (state) {
4332	case ICE_DDP_PKG_SUCCESS:
4333		dev_info(dev, "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n",
4334			 hw->active_pkg_name,
4335			 hw->active_pkg_ver.major,
4336			 hw->active_pkg_ver.minor,
4337			 hw->active_pkg_ver.update,
4338			 hw->active_pkg_ver.draft);
4339		break;
4340	case ICE_DDP_PKG_SAME_VERSION_ALREADY_LOADED:
4341		dev_info(dev, "DDP package already present on device: %s version %d.%d.%d.%d\n",
4342			 hw->active_pkg_name,
4343			 hw->active_pkg_ver.major,
4344			 hw->active_pkg_ver.minor,
4345			 hw->active_pkg_ver.update,
4346			 hw->active_pkg_ver.draft);
4347		break;
4348	case ICE_DDP_PKG_ALREADY_LOADED_NOT_SUPPORTED:
4349		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",
4350			hw->active_pkg_name,
4351			hw->active_pkg_ver.major,
4352			hw->active_pkg_ver.minor,
4353			ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4354		break;
4355	case ICE_DDP_PKG_COMPATIBLE_ALREADY_LOADED:
4356		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",
4357			 hw->active_pkg_name,
4358			 hw->active_pkg_ver.major,
4359			 hw->active_pkg_ver.minor,
4360			 hw->active_pkg_ver.update,
4361			 hw->active_pkg_ver.draft,
4362			 hw->pkg_name,
4363			 hw->pkg_ver.major,
4364			 hw->pkg_ver.minor,
4365			 hw->pkg_ver.update,
4366			 hw->pkg_ver.draft);
4367		break;
4368	case ICE_DDP_PKG_FW_MISMATCH:
4369		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");
4370		break;
4371	case ICE_DDP_PKG_INVALID_FILE:
4372		dev_err(dev, "The DDP package file is invalid. Entering Safe Mode.\n");
4373		break;
4374	case ICE_DDP_PKG_FILE_VERSION_TOO_HIGH:
4375		dev_err(dev, "The DDP package file version is higher than the driver supports.  Please use an updated driver.  Entering Safe Mode.\n");
4376		break;
4377	case ICE_DDP_PKG_FILE_VERSION_TOO_LOW:
4378		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",
4379			ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR);
4380		break;
4381	case ICE_DDP_PKG_FILE_SIGNATURE_INVALID:
4382		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");
4383		break;
4384	case ICE_DDP_PKG_FILE_REVISION_TOO_LOW:
4385		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");
4386		break;
4387	case ICE_DDP_PKG_LOAD_ERROR:
4388		dev_err(dev, "An error occurred on the device while loading the DDP package.  The device will be reset.\n");
4389		/* poll for reset to complete */
4390		if (ice_check_reset(hw))
4391			dev_err(dev, "Error resetting device. Please reload the driver\n");
4392		break;
4393	case ICE_DDP_PKG_ERR:
4394	default:
4395		dev_err(dev, "An unknown error occurred when loading the DDP package.  Entering Safe Mode.\n");
4396		break;
4397	}
4398}
4399
4400/**
4401 * ice_load_pkg - load/reload the DDP Package file
4402 * @firmware: firmware structure when firmware requested or NULL for reload
4403 * @pf: pointer to the PF instance
4404 *
4405 * Called on probe and post CORER/GLOBR rebuild to load DDP Package and
4406 * initialize HW tables.
4407 */
4408static void
4409ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf)
4410{
4411	enum ice_ddp_state state = ICE_DDP_PKG_ERR;
4412	struct device *dev = ice_pf_to_dev(pf);
4413	struct ice_hw *hw = &pf->hw;
4414
4415	/* Load DDP Package */
4416	if (firmware && !hw->pkg_copy) {
4417		state = ice_copy_and_init_pkg(hw, firmware->data,
4418					      firmware->size);
4419		ice_log_pkg_init(hw, state);
4420	} else if (!firmware && hw->pkg_copy) {
4421		/* Reload package during rebuild after CORER/GLOBR reset */
4422		state = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size);
4423		ice_log_pkg_init(hw, state);
4424	} else {
4425		dev_err(dev, "The DDP package file failed to load. Entering Safe Mode.\n");
4426	}
4427
4428	if (!ice_is_init_pkg_successful(state)) {
4429		/* Safe Mode */
4430		clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4431		return;
4432	}
4433
4434	/* Successful download package is the precondition for advanced
4435	 * features, hence setting the ICE_FLAG_ADV_FEATURES flag
4436	 */
4437	set_bit(ICE_FLAG_ADV_FEATURES, pf->flags);
4438}
4439
4440/**
4441 * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines
4442 * @pf: pointer to the PF structure
4443 *
4444 * There is no error returned here because the driver should be able to handle
4445 * 128 Byte cache lines, so we only print a warning in case issues are seen,
4446 * specifically with Tx.
4447 */
4448static void ice_verify_cacheline_size(struct ice_pf *pf)
4449{
4450	if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M)
4451		dev_warn(ice_pf_to_dev(pf), "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n",
4452			 ICE_CACHE_LINE_BYTES);
4453}
4454
4455/**
4456 * ice_send_version - update firmware with driver version
4457 * @pf: PF struct
4458 *
4459 * Returns 0 on success, else error code
4460 */
4461static int ice_send_version(struct ice_pf *pf)
4462{
4463	struct ice_driver_ver dv;
4464
4465	dv.major_ver = 0xff;
4466	dv.minor_ver = 0xff;
4467	dv.build_ver = 0xff;
4468	dv.subbuild_ver = 0;
4469	strscpy((char *)dv.driver_string, UTS_RELEASE,
4470		sizeof(dv.driver_string));
4471	return ice_aq_send_driver_ver(&pf->hw, &dv, NULL);
4472}
4473
4474/**
4475 * ice_init_fdir - Initialize flow director VSI and configuration
4476 * @pf: pointer to the PF instance
4477 *
4478 * returns 0 on success, negative on error
4479 */
4480static int ice_init_fdir(struct ice_pf *pf)
4481{
4482	struct device *dev = ice_pf_to_dev(pf);
4483	struct ice_vsi *ctrl_vsi;
4484	int err;
4485
4486	/* Side Band Flow Director needs to have a control VSI.
4487	 * Allocate it and store it in the PF.
4488	 */
4489	ctrl_vsi = ice_ctrl_vsi_setup(pf, pf->hw.port_info);
4490	if (!ctrl_vsi) {
4491		dev_dbg(dev, "could not create control VSI\n");
4492		return -ENOMEM;
4493	}
4494
4495	err = ice_vsi_open_ctrl(ctrl_vsi);
4496	if (err) {
4497		dev_dbg(dev, "could not open control VSI\n");
4498		goto err_vsi_open;
4499	}
4500
4501	mutex_init(&pf->hw.fdir_fltr_lock);
4502
4503	err = ice_fdir_create_dflt_rules(pf);
4504	if (err)
4505		goto err_fdir_rule;
4506
4507	return 0;
4508
4509err_fdir_rule:
4510	ice_fdir_release_flows(&pf->hw);
4511	ice_vsi_close(ctrl_vsi);
4512err_vsi_open:
4513	ice_vsi_release(ctrl_vsi);
4514	if (pf->ctrl_vsi_idx != ICE_NO_VSI) {
4515		pf->vsi[pf->ctrl_vsi_idx] = NULL;
4516		pf->ctrl_vsi_idx = ICE_NO_VSI;
4517	}
4518	return err;
4519}
4520
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4521/**
4522 * ice_get_opt_fw_name - return optional firmware file name or NULL
4523 * @pf: pointer to the PF instance
4524 */
4525static char *ice_get_opt_fw_name(struct ice_pf *pf)
4526{
4527	/* Optional firmware name same as default with additional dash
4528	 * followed by a EUI-64 identifier (PCIe Device Serial Number)
4529	 */
4530	struct pci_dev *pdev = pf->pdev;
4531	char *opt_fw_filename;
4532	u64 dsn;
4533
4534	/* Determine the name of the optional file using the DSN (two
4535	 * dwords following the start of the DSN Capability).
4536	 */
4537	dsn = pci_get_dsn(pdev);
4538	if (!dsn)
4539		return NULL;
4540
4541	opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL);
4542	if (!opt_fw_filename)
4543		return NULL;
4544
4545	snprintf(opt_fw_filename, NAME_MAX, "%sice-%016llx.pkg",
4546		 ICE_DDP_PKG_PATH, dsn);
4547
4548	return opt_fw_filename;
4549}
4550
4551/**
4552 * ice_request_fw - Device initialization routine
4553 * @pf: pointer to the PF instance
4554 */
4555static void ice_request_fw(struct ice_pf *pf)
4556{
4557	char *opt_fw_filename = ice_get_opt_fw_name(pf);
4558	const struct firmware *firmware = NULL;
4559	struct device *dev = ice_pf_to_dev(pf);
4560	int err = 0;
4561
4562	/* optional device-specific DDP (if present) overrides the default DDP
4563	 * package file. kernel logs a debug message if the file doesn't exist,
4564	 * and warning messages for other errors.
4565	 */
4566	if (opt_fw_filename) {
4567		err = firmware_request_nowarn(&firmware, opt_fw_filename, dev);
4568		if (err) {
4569			kfree(opt_fw_filename);
4570			goto dflt_pkg_load;
4571		}
4572
4573		/* request for firmware was successful. Download to device */
4574		ice_load_pkg(firmware, pf);
4575		kfree(opt_fw_filename);
4576		release_firmware(firmware);
4577		return;
4578	}
4579
4580dflt_pkg_load:
4581	err = request_firmware(&firmware, ICE_DDP_PKG_FILE, dev);
4582	if (err) {
4583		dev_err(dev, "The DDP package file was not found or could not be read. Entering Safe Mode\n");
4584		return;
4585	}
4586
4587	/* request for firmware was successful. Download to device */
4588	ice_load_pkg(firmware, pf);
4589	release_firmware(firmware);
4590}
4591
4592/**
4593 * ice_print_wake_reason - show the wake up cause in the log
4594 * @pf: pointer to the PF struct
4595 */
4596static void ice_print_wake_reason(struct ice_pf *pf)
4597{
4598	u32 wus = pf->wakeup_reason;
4599	const char *wake_str;
4600
4601	/* if no wake event, nothing to print */
4602	if (!wus)
4603		return;
4604
4605	if (wus & PFPM_WUS_LNKC_M)
4606		wake_str = "Link\n";
4607	else if (wus & PFPM_WUS_MAG_M)
4608		wake_str = "Magic Packet\n";
4609	else if (wus & PFPM_WUS_MNG_M)
4610		wake_str = "Management\n";
4611	else if (wus & PFPM_WUS_FW_RST_WK_M)
4612		wake_str = "Firmware Reset\n";
4613	else
4614		wake_str = "Unknown\n";
4615
4616	dev_info(ice_pf_to_dev(pf), "Wake reason: %s", wake_str);
4617}
4618
4619/**
 
 
 
 
 
 
 
 
 
 
 
 
 
4620 * ice_register_netdev - register netdev
4621 * @pf: pointer to the PF struct
4622 */
4623static int ice_register_netdev(struct ice_pf *pf)
4624{
4625	struct ice_vsi *vsi;
4626	int err = 0;
4627
4628	vsi = ice_get_main_vsi(pf);
4629	if (!vsi || !vsi->netdev)
4630		return -EIO;
4631
4632	err = register_netdev(vsi->netdev);
4633	if (err)
4634		goto err_register_netdev;
4635
4636	set_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
4637	netif_carrier_off(vsi->netdev);
4638	netif_tx_stop_all_queues(vsi->netdev);
4639
4640	return 0;
4641err_register_netdev:
4642	free_netdev(vsi->netdev);
4643	vsi->netdev = NULL;
4644	clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
4645	return err;
 
 
 
 
4646}
4647
4648/**
4649 * ice_probe - Device initialization routine
4650 * @pdev: PCI device information struct
4651 * @ent: entry in ice_pci_tbl
4652 *
4653 * Returns 0 on success, negative on failure
4654 */
4655static int
4656ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent)
4657{
4658	struct device *dev = &pdev->dev;
4659	struct ice_vsi *vsi;
4660	struct ice_pf *pf;
4661	struct ice_hw *hw;
4662	int i, err;
 
 
 
 
 
 
 
 
 
 
 
4663
4664	if (pdev->is_virtfn) {
4665		dev_err(dev, "can't probe a virtual function\n");
4666		return -EINVAL;
 
4667	}
4668
4669	/* this driver uses devres, see
4670	 * Documentation/driver-api/driver-model/devres.rst
4671	 */
4672	err = pcim_enable_device(pdev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4673	if (err)
4674		return err;
4675
4676	err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), dev_driver_string(dev));
4677	if (err) {
4678		dev_err(dev, "BAR0 I/O map error %d\n", err);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4679		return err;
4680	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4681
4682	pf = ice_allocate_pf(dev);
4683	if (!pf)
4684		return -ENOMEM;
 
 
 
 
 
 
4685
4686	/* initialize Auxiliary index to invalid value */
4687	pf->aux_idx = -1;
 
4688
4689	/* set up for high or low DMA */
4690	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
4691	if (err) {
4692		dev_err(dev, "DMA configuration failed: 0x%x\n", err);
4693		return err;
4694	}
4695
4696	pci_enable_pcie_error_reporting(pdev);
4697	pci_set_master(pdev);
 
 
 
 
4698
4699	pf->pdev = pdev;
4700	pci_set_drvdata(pdev, pf);
4701	set_bit(ICE_DOWN, pf->state);
4702	/* Disable service task until DOWN bit is cleared */
4703	set_bit(ICE_SERVICE_DIS, pf->state);
 
 
 
 
4704
4705	hw = &pf->hw;
4706	hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
4707	pci_save_state(pdev);
4708
4709	hw->back = pf;
4710	hw->vendor_id = pdev->vendor;
4711	hw->device_id = pdev->device;
4712	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
4713	hw->subsystem_vendor_id = pdev->subsystem_vendor;
4714	hw->subsystem_device_id = pdev->subsystem_device;
4715	hw->bus.device = PCI_SLOT(pdev->devfn);
4716	hw->bus.func = PCI_FUNC(pdev->devfn);
4717	ice_set_ctrlq_len(hw);
4718
4719	pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
 
4720
4721#ifndef CONFIG_DYNAMIC_DEBUG
4722	if (debug < -1)
4723		hw->debug_mask = debug;
4724#endif
 
4725
4726	err = ice_init_hw(hw);
4727	if (err) {
4728		dev_err(dev, "ice_init_hw failed: %d\n", err);
4729		err = -EIO;
4730		goto err_exit_unroll;
 
 
 
 
 
 
 
 
 
 
 
4731	}
4732
4733	ice_init_feature_support(pf);
4734
4735	ice_request_fw(pf);
4736
4737	/* if ice_request_fw fails, ICE_FLAG_ADV_FEATURES bit won't be
4738	 * set in pf->state, which will cause ice_is_safe_mode to return
4739	 * true
4740	 */
4741	if (ice_is_safe_mode(pf)) {
4742		/* we already got function/device capabilities but these don't
4743		 * reflect what the driver needs to do in safe mode. Instead of
4744		 * adding conditional logic everywhere to ignore these
4745		 * device/function capabilities, override them.
4746		 */
4747		ice_set_safe_mode_caps(hw);
4748	}
4749
4750	err = ice_init_pf(pf);
4751	if (err) {
4752		dev_err(dev, "ice_init_pf failed: %d\n", err);
4753		goto err_init_pf_unroll;
4754	}
4755
4756	ice_devlink_init_regions(pf);
4757
4758	pf->hw.udp_tunnel_nic.set_port = ice_udp_tunnel_set_port;
4759	pf->hw.udp_tunnel_nic.unset_port = ice_udp_tunnel_unset_port;
4760	pf->hw.udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
4761	pf->hw.udp_tunnel_nic.shared = &pf->hw.udp_tunnel_shared;
4762	i = 0;
4763	if (pf->hw.tnl.valid_count[TNL_VXLAN]) {
4764		pf->hw.udp_tunnel_nic.tables[i].n_entries =
4765			pf->hw.tnl.valid_count[TNL_VXLAN];
4766		pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
4767			UDP_TUNNEL_TYPE_VXLAN;
4768		i++;
4769	}
4770	if (pf->hw.tnl.valid_count[TNL_GENEVE]) {
4771		pf->hw.udp_tunnel_nic.tables[i].n_entries =
4772			pf->hw.tnl.valid_count[TNL_GENEVE];
4773		pf->hw.udp_tunnel_nic.tables[i].tunnel_types =
4774			UDP_TUNNEL_TYPE_GENEVE;
4775		i++;
4776	}
4777
4778	pf->num_alloc_vsi = hw->func_caps.guar_num_vsi;
4779	if (!pf->num_alloc_vsi) {
4780		err = -EIO;
4781		goto err_init_pf_unroll;
4782	}
4783	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
4784		dev_warn(&pf->pdev->dev,
4785			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
4786			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
4787		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
4788	}
4789
4790	pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi),
4791			       GFP_KERNEL);
4792	if (!pf->vsi) {
4793		err = -ENOMEM;
4794		goto err_init_pf_unroll;
4795	}
4796
4797	pf->vsi_stats = devm_kcalloc(dev, pf->num_alloc_vsi,
4798				     sizeof(*pf->vsi_stats), GFP_KERNEL);
4799	if (!pf->vsi_stats) {
4800		err = -ENOMEM;
4801		goto err_init_vsi_unroll;
4802	}
4803
4804	err = ice_init_interrupt_scheme(pf);
4805	if (err) {
4806		dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err);
4807		err = -EIO;
4808		goto err_init_vsi_stats_unroll;
4809	}
4810
4811	/* In case of MSIX we are going to setup the misc vector right here
4812	 * to handle admin queue events etc. In case of legacy and MSI
4813	 * the misc functionality and queue processing is combined in
4814	 * the same vector and that gets setup at open.
4815	 */
4816	err = ice_req_irq_msix_misc(pf);
4817	if (err) {
4818		dev_err(dev, "setup of misc vector failed: %d\n", err);
4819		goto err_init_interrupt_unroll;
4820	}
4821
4822	/* create switch struct for the switch element created by FW on boot */
4823	pf->first_sw = devm_kzalloc(dev, sizeof(*pf->first_sw), GFP_KERNEL);
4824	if (!pf->first_sw) {
4825		err = -ENOMEM;
4826		goto err_msix_misc_unroll;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4827	}
4828
4829	if (hw->evb_veb)
4830		pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
4831	else
4832		pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA;
 
 
 
 
 
 
4833
4834	pf->first_sw->pf = pf;
 
 
 
 
 
 
 
 
 
 
 
 
4835
4836	/* record the sw_id available for later use */
4837	pf->first_sw->sw_id = hw->port_info->sw_id;
 
 
4838
4839	err = ice_setup_pf_sw(pf);
4840	if (err) {
4841		dev_err(dev, "probe failed due to setup PF switch: %d\n", err);
4842		goto err_alloc_sw_unroll;
4843	}
4844
4845	clear_bit(ICE_SERVICE_DIS, pf->state);
 
4846
4847	/* tell the firmware we are up */
4848	err = ice_send_version(pf);
4849	if (err) {
4850		dev_err(dev, "probe failed sending driver version %s. error: %d\n",
4851			UTS_RELEASE, err);
4852		goto err_send_version_unroll;
4853	}
4854
4855	/* since everything is good, start the service timer */
4856	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
 
 
4857
4858	err = ice_init_link_events(pf->hw.port_info);
4859	if (err) {
4860		dev_err(dev, "ice_init_link_events failed: %d\n", err);
4861		goto err_send_version_unroll;
4862	}
4863
4864	/* not a fatal error if this fails */
4865	err = ice_init_nvm_phy_type(pf->hw.port_info);
4866	if (err)
4867		dev_err(dev, "ice_init_nvm_phy_type failed: %d\n", err);
4868
4869	/* not a fatal error if this fails */
4870	err = ice_update_link_info(pf->hw.port_info);
4871	if (err)
4872		dev_err(dev, "ice_update_link_info failed: %d\n", err);
4873
4874	ice_init_link_dflt_override(pf->hw.port_info);
4875
4876	ice_check_link_cfg_err(pf,
4877			       pf->hw.port_info->phy.link_info.link_cfg_err);
4878
4879	/* if media available, initialize PHY settings */
4880	if (pf->hw.port_info->phy.link_info.link_info &
4881	    ICE_AQ_MEDIA_AVAILABLE) {
4882		/* not a fatal error if this fails */
4883		err = ice_init_phy_user_cfg(pf->hw.port_info);
4884		if (err)
4885			dev_err(dev, "ice_init_phy_user_cfg failed: %d\n", err);
4886
4887		if (!test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags)) {
4888			struct ice_vsi *vsi = ice_get_main_vsi(pf);
4889
4890			if (vsi)
4891				ice_configure_phy(vsi);
4892		}
4893	} else {
4894		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
4895	}
4896
4897	ice_verify_cacheline_size(pf);
 
 
 
 
 
 
 
 
 
 
 
 
4898
4899	/* Save wakeup reason register for later use */
4900	pf->wakeup_reason = rd32(hw, PFPM_WUS);
 
 
4901
4902	/* check for a power management event */
4903	ice_print_wake_reason(pf);
4904
4905	/* clear wake status, all bits */
4906	wr32(hw, PFPM_WUS, U32_MAX);
4907
4908	/* Disable WoL at init, wait for user to enable */
4909	device_set_wakeup_enable(dev, false);
 
4910
4911	if (ice_is_safe_mode(pf)) {
4912		ice_set_safe_mode_vlan_cfg(pf);
4913		goto probe_done;
 
4914	}
4915
4916	/* initialize DDP driven features */
4917	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
4918		ice_ptp_init(pf);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4919
4920	if (ice_is_feature_supported(pf, ICE_F_GNSS))
4921		ice_gnss_init(pf);
 
 
 
 
4922
4923	/* Note: Flow director init failure is non-fatal to load */
4924	if (ice_init_fdir(pf))
4925		dev_err(dev, "could not initialize flow director\n");
 
4926
4927	/* Note: DCB init failure is non-fatal to load */
4928	if (ice_init_pf_dcb(pf, false)) {
4929		clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags);
4930		clear_bit(ICE_FLAG_DCB_ENA, pf->flags);
4931	} else {
4932		ice_cfg_lldp_mib_change(&pf->hw, true);
4933	}
4934
4935	if (ice_init_lag(pf))
4936		dev_warn(dev, "Failed to init link aggregation support\n");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4937
4938	/* print PCI link speed and width */
4939	pcie_print_link_status(pf->pdev);
 
4940
4941probe_done:
4942	err = ice_devlink_create_pf_port(pf);
4943	if (err)
4944		goto err_create_pf_port;
4945
4946	vsi = ice_get_main_vsi(pf);
4947	if (!vsi || !vsi->netdev) {
4948		err = -EINVAL;
4949		goto err_netdev_reg;
4950	}
4951
4952	SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
4953
4954	err = ice_register_netdev(pf);
4955	if (err)
4956		goto err_netdev_reg;
4957
4958	err = ice_devlink_register_params(pf);
4959	if (err)
4960		goto err_netdev_reg;
 
 
 
 
 
 
 
 
4961
4962	/* ready to go, so clear down state bit */
4963	clear_bit(ICE_DOWN, pf->state);
4964	if (ice_is_rdma_ena(pf)) {
4965		pf->aux_idx = ida_alloc(&ice_aux_ida, GFP_KERNEL);
4966		if (pf->aux_idx < 0) {
4967			dev_err(dev, "Failed to allocate device ID for AUX driver\n");
4968			err = -ENOMEM;
4969			goto err_devlink_reg_param;
4970		}
4971
4972		err = ice_init_rdma(pf);
4973		if (err) {
4974			dev_err(dev, "Failed to initialize RDMA: %d\n", err);
4975			err = -EIO;
4976			goto err_init_aux_unroll;
4977		}
4978	} else {
4979		dev_warn(dev, "RDMA is not supported on this device\n");
4980	}
4981
4982	ice_devlink_register(pf);
4983	return 0;
4984
4985err_init_aux_unroll:
4986	pf->adev = NULL;
4987	ida_free(&ice_aux_ida, pf->aux_idx);
4988err_devlink_reg_param:
4989	ice_devlink_unregister_params(pf);
4990err_netdev_reg:
4991	ice_devlink_destroy_pf_port(pf);
4992err_create_pf_port:
4993err_send_version_unroll:
4994	ice_vsi_release_all(pf);
4995err_alloc_sw_unroll:
4996	set_bit(ICE_SERVICE_DIS, pf->state);
4997	set_bit(ICE_DOWN, pf->state);
4998	devm_kfree(dev, pf->first_sw);
4999err_msix_misc_unroll:
5000	ice_free_irq_msix_misc(pf);
5001err_init_interrupt_unroll:
5002	ice_clear_interrupt_scheme(pf);
5003err_init_vsi_stats_unroll:
5004	devm_kfree(dev, pf->vsi_stats);
5005	pf->vsi_stats = NULL;
5006err_init_vsi_unroll:
5007	devm_kfree(dev, pf->vsi);
5008err_init_pf_unroll:
5009	ice_deinit_pf(pf);
5010	ice_devlink_destroy_regions(pf);
5011	ice_deinit_hw(hw);
5012err_exit_unroll:
5013	pci_disable_pcie_error_reporting(pdev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5014	pci_disable_device(pdev);
5015	return err;
5016}
5017
5018/**
5019 * ice_set_wake - enable or disable Wake on LAN
5020 * @pf: pointer to the PF struct
5021 *
5022 * Simple helper for WoL control
5023 */
5024static void ice_set_wake(struct ice_pf *pf)
5025{
5026	struct ice_hw *hw = &pf->hw;
5027	bool wol = pf->wol_ena;
5028
5029	/* clear wake state, otherwise new wake events won't fire */
5030	wr32(hw, PFPM_WUS, U32_MAX);
5031
5032	/* enable / disable APM wake up, no RMW needed */
5033	wr32(hw, PFPM_APM, wol ? PFPM_APM_APME_M : 0);
5034
5035	/* set magic packet filter enabled */
5036	wr32(hw, PFPM_WUFC, wol ? PFPM_WUFC_MAG_M : 0);
5037}
5038
5039/**
5040 * ice_setup_mc_magic_wake - setup device to wake on multicast magic packet
5041 * @pf: pointer to the PF struct
5042 *
5043 * Issue firmware command to enable multicast magic wake, making
5044 * sure that any locally administered address (LAA) is used for
5045 * wake, and that PF reset doesn't undo the LAA.
5046 */
5047static void ice_setup_mc_magic_wake(struct ice_pf *pf)
5048{
5049	struct device *dev = ice_pf_to_dev(pf);
5050	struct ice_hw *hw = &pf->hw;
5051	u8 mac_addr[ETH_ALEN];
5052	struct ice_vsi *vsi;
5053	int status;
5054	u8 flags;
5055
5056	if (!pf->wol_ena)
5057		return;
5058
5059	vsi = ice_get_main_vsi(pf);
5060	if (!vsi)
5061		return;
5062
5063	/* Get current MAC address in case it's an LAA */
5064	if (vsi->netdev)
5065		ether_addr_copy(mac_addr, vsi->netdev->dev_addr);
5066	else
5067		ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
5068
5069	flags = ICE_AQC_MAN_MAC_WR_MC_MAG_EN |
5070		ICE_AQC_MAN_MAC_UPDATE_LAA_WOL |
5071		ICE_AQC_MAN_MAC_WR_WOL_LAA_PFR_KEEP;
5072
5073	status = ice_aq_manage_mac_write(hw, mac_addr, flags, NULL);
5074	if (status)
5075		dev_err(dev, "Failed to enable Multicast Magic Packet wake, err %d aq_err %s\n",
5076			status, ice_aq_str(hw->adminq.sq_last_status));
5077}
5078
5079/**
5080 * ice_remove - Device removal routine
5081 * @pdev: PCI device information struct
5082 */
5083static void ice_remove(struct pci_dev *pdev)
5084{
5085	struct ice_pf *pf = pci_get_drvdata(pdev);
5086	int i;
5087
5088	ice_devlink_unregister(pf);
5089	for (i = 0; i < ICE_MAX_RESET_WAIT; i++) {
5090		if (!ice_is_reset_in_progress(pf->state))
5091			break;
5092		msleep(100);
5093	}
5094
5095	ice_tc_indir_block_remove(pf);
5096
5097	if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) {
5098		set_bit(ICE_VF_RESETS_DISABLED, pf->state);
5099		ice_free_vfs(pf);
5100	}
5101
 
 
5102	ice_service_task_stop(pf);
5103
5104	ice_aq_cancel_waiting_tasks(pf);
5105	ice_unplug_aux_dev(pf);
5106	if (pf->aux_idx >= 0)
5107		ida_free(&ice_aux_ida, pf->aux_idx);
5108	ice_devlink_unregister_params(pf);
5109	set_bit(ICE_DOWN, pf->state);
5110
5111	ice_deinit_lag(pf);
5112	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
5113		ice_ptp_release(pf);
5114	if (ice_is_feature_supported(pf, ICE_F_GNSS))
5115		ice_gnss_exit(pf);
5116	if (!ice_is_safe_mode(pf))
5117		ice_remove_arfs(pf);
 
 
 
 
 
 
 
 
5118	ice_setup_mc_magic_wake(pf);
5119	ice_vsi_release_all(pf);
5120	mutex_destroy(&(&pf->hw)->fdir_fltr_lock);
5121	ice_devlink_destroy_pf_port(pf);
5122	ice_set_wake(pf);
5123	ice_free_irq_msix_misc(pf);
5124	ice_for_each_vsi(pf, i) {
5125		if (!pf->vsi[i])
5126			continue;
5127		ice_vsi_free_q_vectors(pf->vsi[i]);
5128	}
5129	devm_kfree(&pdev->dev, pf->vsi_stats);
5130	pf->vsi_stats = NULL;
5131	ice_deinit_pf(pf);
5132	ice_devlink_destroy_regions(pf);
5133	ice_deinit_hw(&pf->hw);
5134
5135	/* Issue a PFR as part of the prescribed driver unload flow.  Do not
5136	 * do it via ice_schedule_reset() since there is no need to rebuild
5137	 * and the service task is already stopped.
5138	 */
5139	ice_reset(&pf->hw, ICE_RESET_PFR);
5140	pci_wait_for_pending_transaction(pdev);
5141	ice_clear_interrupt_scheme(pf);
5142	pci_disable_pcie_error_reporting(pdev);
5143	pci_disable_device(pdev);
5144}
5145
5146/**
5147 * ice_shutdown - PCI callback for shutting down device
5148 * @pdev: PCI device information struct
5149 */
5150static void ice_shutdown(struct pci_dev *pdev)
5151{
5152	struct ice_pf *pf = pci_get_drvdata(pdev);
5153
5154	ice_remove(pdev);
5155
5156	if (system_state == SYSTEM_POWER_OFF) {
5157		pci_wake_from_d3(pdev, pf->wol_ena);
5158		pci_set_power_state(pdev, PCI_D3hot);
5159	}
5160}
5161
5162#ifdef CONFIG_PM
5163/**
5164 * ice_prepare_for_shutdown - prep for PCI shutdown
5165 * @pf: board private structure
5166 *
5167 * Inform or close all dependent features in prep for PCI device shutdown
5168 */
5169static void ice_prepare_for_shutdown(struct ice_pf *pf)
5170{
5171	struct ice_hw *hw = &pf->hw;
5172	u32 v;
5173
5174	/* Notify VFs of impending reset */
5175	if (ice_check_sq_alive(hw, &hw->mailboxq))
5176		ice_vc_notify_reset(pf);
5177
5178	dev_dbg(ice_pf_to_dev(pf), "Tearing down internal switch for shutdown\n");
5179
5180	/* disable the VSIs and their queues that are not already DOWN */
5181	ice_pf_dis_all_vsi(pf, false);
5182
5183	ice_for_each_vsi(pf, v)
5184		if (pf->vsi[v])
5185			pf->vsi[v]->vsi_num = 0;
5186
5187	ice_shutdown_all_ctrlq(hw);
5188}
5189
5190/**
5191 * ice_reinit_interrupt_scheme - Reinitialize interrupt scheme
5192 * @pf: board private structure to reinitialize
5193 *
5194 * This routine reinitialize interrupt scheme that was cleared during
5195 * power management suspend callback.
5196 *
5197 * This should be called during resume routine to re-allocate the q_vectors
5198 * and reacquire interrupts.
5199 */
5200static int ice_reinit_interrupt_scheme(struct ice_pf *pf)
5201{
5202	struct device *dev = ice_pf_to_dev(pf);
5203	int ret, v;
5204
5205	/* Since we clear MSIX flag during suspend, we need to
5206	 * set it back during resume...
5207	 */
5208
5209	ret = ice_init_interrupt_scheme(pf);
5210	if (ret) {
5211		dev_err(dev, "Failed to re-initialize interrupt %d\n", ret);
5212		return ret;
5213	}
5214
5215	/* Remap vectors and rings, after successful re-init interrupts */
5216	ice_for_each_vsi(pf, v) {
5217		if (!pf->vsi[v])
5218			continue;
5219
5220		ret = ice_vsi_alloc_q_vectors(pf->vsi[v]);
5221		if (ret)
5222			goto err_reinit;
5223		ice_vsi_map_rings_to_vectors(pf->vsi[v]);
 
5224	}
5225
5226	ret = ice_req_irq_msix_misc(pf);
5227	if (ret) {
5228		dev_err(dev, "Setting up misc vector failed after device suspend %d\n",
5229			ret);
5230		goto err_reinit;
5231	}
5232
5233	return 0;
5234
5235err_reinit:
5236	while (v--)
5237		if (pf->vsi[v])
5238			ice_vsi_free_q_vectors(pf->vsi[v]);
5239
5240	return ret;
5241}
5242
5243/**
5244 * ice_suspend
5245 * @dev: generic device information structure
5246 *
5247 * Power Management callback to quiesce the device and prepare
5248 * for D3 transition.
5249 */
5250static int __maybe_unused ice_suspend(struct device *dev)
5251{
5252	struct pci_dev *pdev = to_pci_dev(dev);
5253	struct ice_pf *pf;
5254	int disabled, v;
5255
5256	pf = pci_get_drvdata(pdev);
5257
5258	if (!ice_pf_state_is_nominal(pf)) {
5259		dev_err(dev, "Device is not ready, no need to suspend it\n");
5260		return -EBUSY;
5261	}
5262
5263	/* Stop watchdog tasks until resume completion.
5264	 * Even though it is most likely that the service task is
5265	 * disabled if the device is suspended or down, the service task's
5266	 * state is controlled by a different state bit, and we should
5267	 * store and honor whatever state that bit is in at this point.
5268	 */
5269	disabled = ice_service_task_stop(pf);
5270
5271	ice_unplug_aux_dev(pf);
5272
5273	/* Already suspended?, then there is nothing to do */
5274	if (test_and_set_bit(ICE_SUSPENDED, pf->state)) {
5275		if (!disabled)
5276			ice_service_task_restart(pf);
5277		return 0;
5278	}
5279
5280	if (test_bit(ICE_DOWN, pf->state) ||
5281	    ice_is_reset_in_progress(pf->state)) {
5282		dev_err(dev, "can't suspend device in reset or already down\n");
5283		if (!disabled)
5284			ice_service_task_restart(pf);
5285		return 0;
5286	}
5287
5288	ice_setup_mc_magic_wake(pf);
5289
5290	ice_prepare_for_shutdown(pf);
5291
5292	ice_set_wake(pf);
5293
5294	/* Free vectors, clear the interrupt scheme and release IRQs
5295	 * for proper hibernation, especially with large number of CPUs.
5296	 * Otherwise hibernation might fail when mapping all the vectors back
5297	 * to CPU0.
5298	 */
5299	ice_free_irq_msix_misc(pf);
5300	ice_for_each_vsi(pf, v) {
5301		if (!pf->vsi[v])
5302			continue;
5303		ice_vsi_free_q_vectors(pf->vsi[v]);
5304	}
5305	ice_clear_interrupt_scheme(pf);
5306
5307	pci_save_state(pdev);
5308	pci_wake_from_d3(pdev, pf->wol_ena);
5309	pci_set_power_state(pdev, PCI_D3hot);
5310	return 0;
5311}
5312
5313/**
5314 * ice_resume - PM callback for waking up from D3
5315 * @dev: generic device information structure
5316 */
5317static int __maybe_unused ice_resume(struct device *dev)
5318{
5319	struct pci_dev *pdev = to_pci_dev(dev);
5320	enum ice_reset_req reset_type;
5321	struct ice_pf *pf;
5322	struct ice_hw *hw;
5323	int ret;
5324
5325	pci_set_power_state(pdev, PCI_D0);
5326	pci_restore_state(pdev);
5327	pci_save_state(pdev);
5328
5329	if (!pci_device_is_present(pdev))
5330		return -ENODEV;
5331
5332	ret = pci_enable_device_mem(pdev);
5333	if (ret) {
5334		dev_err(dev, "Cannot enable device after suspend\n");
5335		return ret;
5336	}
5337
5338	pf = pci_get_drvdata(pdev);
5339	hw = &pf->hw;
5340
5341	pf->wakeup_reason = rd32(hw, PFPM_WUS);
5342	ice_print_wake_reason(pf);
5343
5344	/* We cleared the interrupt scheme when we suspended, so we need to
5345	 * restore it now to resume device functionality.
5346	 */
5347	ret = ice_reinit_interrupt_scheme(pf);
5348	if (ret)
5349		dev_err(dev, "Cannot restore interrupt scheme: %d\n", ret);
5350
5351	clear_bit(ICE_DOWN, pf->state);
5352	/* Now perform PF reset and rebuild */
5353	reset_type = ICE_RESET_PFR;
5354	/* re-enable service task for reset, but allow reset to schedule it */
5355	clear_bit(ICE_SERVICE_DIS, pf->state);
5356
5357	if (ice_schedule_reset(pf, reset_type))
5358		dev_err(dev, "Reset during resume failed.\n");
5359
5360	clear_bit(ICE_SUSPENDED, pf->state);
5361	ice_service_task_restart(pf);
5362
5363	/* Restart the service task */
5364	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5365
5366	return 0;
5367}
5368#endif /* CONFIG_PM */
5369
5370/**
5371 * ice_pci_err_detected - warning that PCI error has been detected
5372 * @pdev: PCI device information struct
5373 * @err: the type of PCI error
5374 *
5375 * Called to warn that something happened on the PCI bus and the error handling
5376 * is in progress.  Allows the driver to gracefully prepare/handle PCI errors.
5377 */
5378static pci_ers_result_t
5379ice_pci_err_detected(struct pci_dev *pdev, pci_channel_state_t err)
5380{
5381	struct ice_pf *pf = pci_get_drvdata(pdev);
5382
5383	if (!pf) {
5384		dev_err(&pdev->dev, "%s: unrecoverable device error %d\n",
5385			__func__, err);
5386		return PCI_ERS_RESULT_DISCONNECT;
5387	}
5388
5389	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5390		ice_service_task_stop(pf);
5391
5392		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5393			set_bit(ICE_PFR_REQ, pf->state);
5394			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5395		}
5396	}
5397
5398	return PCI_ERS_RESULT_NEED_RESET;
5399}
5400
5401/**
5402 * ice_pci_err_slot_reset - a PCI slot reset has just happened
5403 * @pdev: PCI device information struct
5404 *
5405 * Called to determine if the driver can recover from the PCI slot reset by
5406 * using a register read to determine if the device is recoverable.
5407 */
5408static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev)
5409{
5410	struct ice_pf *pf = pci_get_drvdata(pdev);
5411	pci_ers_result_t result;
5412	int err;
5413	u32 reg;
5414
5415	err = pci_enable_device_mem(pdev);
5416	if (err) {
5417		dev_err(&pdev->dev, "Cannot re-enable PCI device after reset, error %d\n",
5418			err);
5419		result = PCI_ERS_RESULT_DISCONNECT;
5420	} else {
5421		pci_set_master(pdev);
5422		pci_restore_state(pdev);
5423		pci_save_state(pdev);
5424		pci_wake_from_d3(pdev, false);
5425
5426		/* Check for life */
5427		reg = rd32(&pf->hw, GLGEN_RTRIG);
5428		if (!reg)
5429			result = PCI_ERS_RESULT_RECOVERED;
5430		else
5431			result = PCI_ERS_RESULT_DISCONNECT;
5432	}
5433
5434	return result;
5435}
5436
5437/**
5438 * ice_pci_err_resume - restart operations after PCI error recovery
5439 * @pdev: PCI device information struct
5440 *
5441 * Called to allow the driver to bring things back up after PCI error and/or
5442 * reset recovery have finished
5443 */
5444static void ice_pci_err_resume(struct pci_dev *pdev)
5445{
5446	struct ice_pf *pf = pci_get_drvdata(pdev);
5447
5448	if (!pf) {
5449		dev_err(&pdev->dev, "%s failed, device is unrecoverable\n",
5450			__func__);
5451		return;
5452	}
5453
5454	if (test_bit(ICE_SUSPENDED, pf->state)) {
5455		dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n",
5456			__func__);
5457		return;
5458	}
5459
5460	ice_restore_all_vfs_msi_state(pdev);
5461
5462	ice_do_reset(pf, ICE_RESET_PFR);
5463	ice_service_task_restart(pf);
5464	mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
5465}
5466
5467/**
5468 * ice_pci_err_reset_prepare - prepare device driver for PCI reset
5469 * @pdev: PCI device information struct
5470 */
5471static void ice_pci_err_reset_prepare(struct pci_dev *pdev)
5472{
5473	struct ice_pf *pf = pci_get_drvdata(pdev);
5474
5475	if (!test_bit(ICE_SUSPENDED, pf->state)) {
5476		ice_service_task_stop(pf);
5477
5478		if (!test_bit(ICE_PREPARED_FOR_RESET, pf->state)) {
5479			set_bit(ICE_PFR_REQ, pf->state);
5480			ice_prepare_for_reset(pf, ICE_RESET_PFR);
5481		}
5482	}
5483}
5484
5485/**
5486 * ice_pci_err_reset_done - PCI reset done, device driver reset can begin
5487 * @pdev: PCI device information struct
5488 */
5489static void ice_pci_err_reset_done(struct pci_dev *pdev)
5490{
5491	ice_pci_err_resume(pdev);
5492}
5493
5494/* ice_pci_tbl - PCI Device ID Table
5495 *
5496 * Wildcard entries (PCI_ANY_ID) should come last
5497 * Last entry must be all 0s
5498 *
5499 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
5500 *   Class, Class Mask, private data (not used) }
5501 */
5502static const struct pci_device_id ice_pci_tbl[] = {
5503	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 },
5504	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 },
5505	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 },
5506	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_BACKPLANE), 0 },
5507	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_QSFP), 0 },
5508	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E810_XXV_SFP), 0 },
5509	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_BACKPLANE), 0 },
5510	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_QSFP), 0 },
5511	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SFP), 0 },
5512	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_10G_BASE_T), 0 },
5513	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823C_SGMII), 0 },
5514	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE), 0 },
5515	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP), 0 },
5516	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP), 0 },
5517	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T), 0 },
5518	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII), 0 },
5519	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_BACKPLANE), 0 },
5520	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP), 0 },
5521	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T), 0 },
5522	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII), 0 },
5523	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_BACKPLANE), 0 },
5524	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_SFP), 0 },
5525	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_10G_BASE_T), 0 },
5526	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_1GBE), 0 },
5527	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E823L_QSFP), 0 },
5528	{ PCI_VDEVICE(INTEL, ICE_DEV_ID_E822_SI_DFLT), 0 },
 
 
 
 
5529	/* required last entry */
5530	{ 0, }
5531};
5532MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
5533
5534static __maybe_unused SIMPLE_DEV_PM_OPS(ice_pm_ops, ice_suspend, ice_resume);
5535
5536static const struct pci_error_handlers ice_pci_err_handler = {
5537	.error_detected = ice_pci_err_detected,
5538	.slot_reset = ice_pci_err_slot_reset,
5539	.reset_prepare = ice_pci_err_reset_prepare,
5540	.reset_done = ice_pci_err_reset_done,
5541	.resume = ice_pci_err_resume
5542};
5543
5544static struct pci_driver ice_driver = {
5545	.name = KBUILD_MODNAME,
5546	.id_table = ice_pci_tbl,
5547	.probe = ice_probe,
5548	.remove = ice_remove,
5549#ifdef CONFIG_PM
5550	.driver.pm = &ice_pm_ops,
5551#endif /* CONFIG_PM */
5552	.shutdown = ice_shutdown,
5553	.sriov_configure = ice_sriov_configure,
 
 
5554	.err_handler = &ice_pci_err_handler
5555};
5556
5557/**
5558 * ice_module_init - Driver registration routine
5559 *
5560 * ice_module_init is the first routine called when the driver is
5561 * loaded. All it does is register with the PCI subsystem.
5562 */
5563static int __init ice_module_init(void)
5564{
5565	int status;
5566
5567	pr_info("%s\n", ice_driver_string);
5568	pr_info("%s\n", ice_copyright);
5569
 
 
5570	ice_wq = alloc_workqueue("%s", 0, 0, KBUILD_MODNAME);
5571	if (!ice_wq) {
5572		pr_err("Failed to create workqueue\n");
5573		return -ENOMEM;
 
 
 
 
 
 
5574	}
5575
 
 
5576	status = pci_register_driver(&ice_driver);
5577	if (status) {
5578		pr_err("failed to register PCI driver, err %d\n", status);
5579		destroy_workqueue(ice_wq);
5580	}
5581
 
 
 
 
 
 
 
5582	return status;
5583}
5584module_init(ice_module_init);
5585
5586/**
5587 * ice_module_exit - Driver exit cleanup routine
5588 *
5589 * ice_module_exit is called just before the driver is removed
5590 * from memory.
5591 */
5592static void __exit ice_module_exit(void)
5593{
5594	pci_unregister_driver(&ice_driver);
5595	destroy_workqueue(ice_wq);
 
5596	pr_info("module unloaded\n");
5597}
5598module_exit(ice_module_exit);
5599
5600/**
5601 * ice_set_mac_address - NDO callback to set MAC address
5602 * @netdev: network interface device structure
5603 * @pi: pointer to an address structure
5604 *
5605 * Returns 0 on success, negative on failure
5606 */
5607static int ice_set_mac_address(struct net_device *netdev, void *pi)
5608{
5609	struct ice_netdev_priv *np = netdev_priv(netdev);
5610	struct ice_vsi *vsi = np->vsi;
5611	struct ice_pf *pf = vsi->back;
5612	struct ice_hw *hw = &pf->hw;
5613	struct sockaddr *addr = pi;
5614	u8 old_mac[ETH_ALEN];
5615	u8 flags = 0;
5616	u8 *mac;
5617	int err;
5618
5619	mac = (u8 *)addr->sa_data;
5620
5621	if (!is_valid_ether_addr(mac))
5622		return -EADDRNOTAVAIL;
5623
5624	if (ether_addr_equal(netdev->dev_addr, mac)) {
5625		netdev_dbg(netdev, "already using mac %pM\n", mac);
5626		return 0;
5627	}
5628
5629	if (test_bit(ICE_DOWN, pf->state) ||
5630	    ice_is_reset_in_progress(pf->state)) {
5631		netdev_err(netdev, "can't set mac %pM. device not ready\n",
5632			   mac);
5633		return -EBUSY;
5634	}
5635
5636	if (ice_chnl_dmac_fltr_cnt(pf)) {
5637		netdev_err(netdev, "can't set mac %pM. Device has tc-flower filters, delete all of them and try again\n",
5638			   mac);
5639		return -EAGAIN;
5640	}
5641
5642	netif_addr_lock_bh(netdev);
5643	ether_addr_copy(old_mac, netdev->dev_addr);
5644	/* change the netdev's MAC address */
5645	eth_hw_addr_set(netdev, mac);
5646	netif_addr_unlock_bh(netdev);
5647
5648	/* Clean up old MAC filter. Not an error if old filter doesn't exist */
5649	err = ice_fltr_remove_mac(vsi, old_mac, ICE_FWD_TO_VSI);
5650	if (err && err != -ENOENT) {
5651		err = -EADDRNOTAVAIL;
5652		goto err_update_filters;
5653	}
5654
5655	/* Add filter for new MAC. If filter exists, return success */
5656	err = ice_fltr_add_mac(vsi, mac, ICE_FWD_TO_VSI);
5657	if (err == -EEXIST) {
5658		/* Although this MAC filter is already present in hardware it's
5659		 * possible in some cases (e.g. bonding) that dev_addr was
5660		 * modified outside of the driver and needs to be restored back
5661		 * to this value.
5662		 */
5663		netdev_dbg(netdev, "filter for MAC %pM already exists\n", mac);
5664
5665		return 0;
5666	} else if (err) {
5667		/* error if the new filter addition failed */
5668		err = -EADDRNOTAVAIL;
5669	}
5670
5671err_update_filters:
5672	if (err) {
5673		netdev_err(netdev, "can't set MAC %pM. filter update failed\n",
5674			   mac);
5675		netif_addr_lock_bh(netdev);
5676		eth_hw_addr_set(netdev, old_mac);
5677		netif_addr_unlock_bh(netdev);
5678		return err;
5679	}
5680
5681	netdev_dbg(vsi->netdev, "updated MAC address to %pM\n",
5682		   netdev->dev_addr);
5683
5684	/* write new MAC address to the firmware */
5685	flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
5686	err = ice_aq_manage_mac_write(hw, mac, flags, NULL);
5687	if (err) {
5688		netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n",
5689			   mac, err);
5690	}
5691	return 0;
5692}
5693
5694/**
5695 * ice_set_rx_mode - NDO callback to set the netdev filters
5696 * @netdev: network interface device structure
5697 */
5698static void ice_set_rx_mode(struct net_device *netdev)
5699{
5700	struct ice_netdev_priv *np = netdev_priv(netdev);
5701	struct ice_vsi *vsi = np->vsi;
5702
5703	if (!vsi)
5704		return;
5705
5706	/* Set the flags to synchronize filters
5707	 * ndo_set_rx_mode may be triggered even without a change in netdev
5708	 * flags
5709	 */
5710	set_bit(ICE_VSI_UMAC_FLTR_CHANGED, vsi->state);
5711	set_bit(ICE_VSI_MMAC_FLTR_CHANGED, vsi->state);
5712	set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
5713
5714	/* schedule our worker thread which will take care of
5715	 * applying the new filter changes
5716	 */
5717	ice_service_task_schedule(vsi->back);
5718}
5719
5720/**
5721 * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate
5722 * @netdev: network interface device structure
5723 * @queue_index: Queue ID
5724 * @maxrate: maximum bandwidth in Mbps
5725 */
5726static int
5727ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate)
5728{
5729	struct ice_netdev_priv *np = netdev_priv(netdev);
5730	struct ice_vsi *vsi = np->vsi;
5731	u16 q_handle;
5732	int status;
5733	u8 tc;
5734
5735	/* Validate maxrate requested is within permitted range */
5736	if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) {
5737		netdev_err(netdev, "Invalid max rate %d specified for the queue %d\n",
5738			   maxrate, queue_index);
5739		return -EINVAL;
5740	}
5741
5742	q_handle = vsi->tx_rings[queue_index]->q_handle;
5743	tc = ice_dcb_get_tc(vsi, queue_index);
5744
 
 
 
 
 
 
 
5745	/* Set BW back to default, when user set maxrate to 0 */
5746	if (!maxrate)
5747		status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc,
5748					       q_handle, ICE_MAX_BW);
5749	else
5750		status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc,
5751					  q_handle, ICE_MAX_BW, maxrate * 1000);
5752	if (status)
5753		netdev_err(netdev, "Unable to set Tx max rate, error %d\n",
5754			   status);
5755
5756	return status;
5757}
5758
5759/**
5760 * ice_fdb_add - add an entry to the hardware database
5761 * @ndm: the input from the stack
5762 * @tb: pointer to array of nladdr (unused)
5763 * @dev: the net device pointer
5764 * @addr: the MAC address entry being added
5765 * @vid: VLAN ID
5766 * @flags: instructions from stack about fdb operation
5767 * @extack: netlink extended ack
5768 */
5769static int
5770ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
5771	    struct net_device *dev, const unsigned char *addr, u16 vid,
5772	    u16 flags, struct netlink_ext_ack __always_unused *extack)
5773{
5774	int err;
5775
5776	if (vid) {
5777		netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
5778		return -EINVAL;
5779	}
5780	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
5781		netdev_err(dev, "FDB only supports static addresses\n");
5782		return -EINVAL;
5783	}
5784
5785	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
5786		err = dev_uc_add_excl(dev, addr);
5787	else if (is_multicast_ether_addr(addr))
5788		err = dev_mc_add_excl(dev, addr);
5789	else
5790		err = -EINVAL;
5791
5792	/* Only return duplicate errors if NLM_F_EXCL is set */
5793	if (err == -EEXIST && !(flags & NLM_F_EXCL))
5794		err = 0;
5795
5796	return err;
5797}
5798
5799/**
5800 * ice_fdb_del - delete an entry from the hardware database
5801 * @ndm: the input from the stack
5802 * @tb: pointer to array of nladdr (unused)
5803 * @dev: the net device pointer
5804 * @addr: the MAC address entry being added
5805 * @vid: VLAN ID
5806 * @extack: netlink extended ack
5807 */
5808static int
5809ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
5810	    struct net_device *dev, const unsigned char *addr,
5811	    __always_unused u16 vid, struct netlink_ext_ack *extack)
5812{
5813	int err;
5814
5815	if (ndm->ndm_state & NUD_PERMANENT) {
5816		netdev_err(dev, "FDB only supports static addresses\n");
5817		return -EINVAL;
5818	}
5819
5820	if (is_unicast_ether_addr(addr))
5821		err = dev_uc_del(dev, addr);
5822	else if (is_multicast_ether_addr(addr))
5823		err = dev_mc_del(dev, addr);
5824	else
5825		err = -EINVAL;
5826
5827	return err;
5828}
5829
5830#define NETIF_VLAN_OFFLOAD_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
5831					 NETIF_F_HW_VLAN_CTAG_TX | \
5832					 NETIF_F_HW_VLAN_STAG_RX | \
5833					 NETIF_F_HW_VLAN_STAG_TX)
5834
5835#define NETIF_VLAN_STRIPPING_FEATURES	(NETIF_F_HW_VLAN_CTAG_RX | \
5836					 NETIF_F_HW_VLAN_STAG_RX)
5837
5838#define NETIF_VLAN_FILTERING_FEATURES	(NETIF_F_HW_VLAN_CTAG_FILTER | \
5839					 NETIF_F_HW_VLAN_STAG_FILTER)
5840
5841/**
5842 * ice_fix_features - fix the netdev features flags based on device limitations
5843 * @netdev: ptr to the netdev that flags are being fixed on
5844 * @features: features that need to be checked and possibly fixed
5845 *
5846 * Make sure any fixups are made to features in this callback. This enables the
5847 * driver to not have to check unsupported configurations throughout the driver
5848 * because that's the responsiblity of this callback.
5849 *
5850 * Single VLAN Mode (SVM) Supported Features:
5851 *	NETIF_F_HW_VLAN_CTAG_FILTER
5852 *	NETIF_F_HW_VLAN_CTAG_RX
5853 *	NETIF_F_HW_VLAN_CTAG_TX
5854 *
5855 * Double VLAN Mode (DVM) Supported Features:
5856 *	NETIF_F_HW_VLAN_CTAG_FILTER
5857 *	NETIF_F_HW_VLAN_CTAG_RX
5858 *	NETIF_F_HW_VLAN_CTAG_TX
5859 *
5860 *	NETIF_F_HW_VLAN_STAG_FILTER
5861 *	NETIF_HW_VLAN_STAG_RX
5862 *	NETIF_HW_VLAN_STAG_TX
5863 *
5864 * Features that need fixing:
5865 *	Cannot simultaneously enable CTAG and STAG stripping and/or insertion.
5866 *	These are mutually exlusive as the VSI context cannot support multiple
5867 *	VLAN ethertypes simultaneously for stripping and/or insertion. If this
5868 *	is not done, then default to clearing the requested STAG offload
5869 *	settings.
5870 *
5871 *	All supported filtering has to be enabled or disabled together. For
5872 *	example, in DVM, CTAG and STAG filtering have to be enabled and disabled
5873 *	together. If this is not done, then default to VLAN filtering disabled.
5874 *	These are mutually exclusive as there is currently no way to
5875 *	enable/disable VLAN filtering based on VLAN ethertype when using VLAN
5876 *	prune rules.
5877 */
5878static netdev_features_t
5879ice_fix_features(struct net_device *netdev, netdev_features_t features)
5880{
5881	struct ice_netdev_priv *np = netdev_priv(netdev);
5882	netdev_features_t req_vlan_fltr, cur_vlan_fltr;
5883	bool cur_ctag, cur_stag, req_ctag, req_stag;
5884
5885	cur_vlan_fltr = netdev->features & NETIF_VLAN_FILTERING_FEATURES;
5886	cur_ctag = cur_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
5887	cur_stag = cur_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
5888
5889	req_vlan_fltr = features & NETIF_VLAN_FILTERING_FEATURES;
5890	req_ctag = req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER;
5891	req_stag = req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER;
5892
5893	if (req_vlan_fltr != cur_vlan_fltr) {
5894		if (ice_is_dvm_ena(&np->vsi->back->hw)) {
5895			if (req_ctag && req_stag) {
5896				features |= NETIF_VLAN_FILTERING_FEATURES;
5897			} else if (!req_ctag && !req_stag) {
5898				features &= ~NETIF_VLAN_FILTERING_FEATURES;
5899			} else if ((!cur_ctag && req_ctag && !cur_stag) ||
5900				   (!cur_stag && req_stag && !cur_ctag)) {
5901				features |= NETIF_VLAN_FILTERING_FEATURES;
5902				netdev_warn(netdev,  "802.1Q and 802.1ad VLAN filtering must be either both on or both off. VLAN filtering has been enabled for both types.\n");
5903			} else if ((cur_ctag && !req_ctag && cur_stag) ||
5904				   (cur_stag && !req_stag && cur_ctag)) {
5905				features &= ~NETIF_VLAN_FILTERING_FEATURES;
5906				netdev_warn(netdev,  "802.1Q and 802.1ad VLAN filtering must be either both on or both off. VLAN filtering has been disabled for both types.\n");
5907			}
5908		} else {
5909			if (req_vlan_fltr & NETIF_F_HW_VLAN_STAG_FILTER)
5910				netdev_warn(netdev, "cannot support requested 802.1ad filtering setting in SVM mode\n");
5911
5912			if (req_vlan_fltr & NETIF_F_HW_VLAN_CTAG_FILTER)
5913				features |= NETIF_F_HW_VLAN_CTAG_FILTER;
5914		}
5915	}
5916
5917	if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX)) &&
5918	    (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))) {
5919		netdev_warn(netdev, "cannot support CTAG and STAG VLAN stripping and/or insertion simultaneously since CTAG and STAG offloads are mutually exclusive, clearing STAG offload settings\n");
5920		features &= ~(NETIF_F_HW_VLAN_STAG_RX |
5921			      NETIF_F_HW_VLAN_STAG_TX);
5922	}
5923
5924	if (!(netdev->features & NETIF_F_RXFCS) &&
5925	    (features & NETIF_F_RXFCS) &&
5926	    (features & NETIF_VLAN_STRIPPING_FEATURES) &&
5927	    !ice_vsi_has_non_zero_vlans(np->vsi)) {
5928		netdev_warn(netdev, "Disabling VLAN stripping as FCS/CRC stripping is also disabled and there is no VLAN configured\n");
5929		features &= ~NETIF_VLAN_STRIPPING_FEATURES;
5930	}
5931
5932	return features;
5933}
5934
5935/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5936 * ice_set_vlan_offload_features - set VLAN offload features for the PF VSI
5937 * @vsi: PF's VSI
5938 * @features: features used to determine VLAN offload settings
5939 *
5940 * First, determine the vlan_ethertype based on the VLAN offload bits in
5941 * features. Then determine if stripping and insertion should be enabled or
5942 * disabled. Finally enable or disable VLAN stripping and insertion.
5943 */
5944static int
5945ice_set_vlan_offload_features(struct ice_vsi *vsi, netdev_features_t features)
5946{
5947	bool enable_stripping = true, enable_insertion = true;
5948	struct ice_vsi_vlan_ops *vlan_ops;
5949	int strip_err = 0, insert_err = 0;
5950	u16 vlan_ethertype = 0;
5951
5952	vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
5953
5954	if (features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX))
5955		vlan_ethertype = ETH_P_8021AD;
5956	else if (features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX))
5957		vlan_ethertype = ETH_P_8021Q;
5958
5959	if (!(features & (NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_CTAG_RX)))
5960		enable_stripping = false;
5961	if (!(features & (NETIF_F_HW_VLAN_STAG_TX | NETIF_F_HW_VLAN_CTAG_TX)))
5962		enable_insertion = false;
5963
5964	if (enable_stripping)
5965		strip_err = vlan_ops->ena_stripping(vsi, vlan_ethertype);
5966	else
5967		strip_err = vlan_ops->dis_stripping(vsi);
5968
5969	if (enable_insertion)
5970		insert_err = vlan_ops->ena_insertion(vsi, vlan_ethertype);
5971	else
5972		insert_err = vlan_ops->dis_insertion(vsi);
5973
5974	if (strip_err || insert_err)
5975		return -EIO;
5976
 
 
 
5977	return 0;
5978}
5979
5980/**
5981 * ice_set_vlan_filtering_features - set VLAN filtering features for the PF VSI
5982 * @vsi: PF's VSI
5983 * @features: features used to determine VLAN filtering settings
5984 *
5985 * Enable or disable Rx VLAN filtering based on the VLAN filtering bits in the
5986 * features.
5987 */
5988static int
5989ice_set_vlan_filtering_features(struct ice_vsi *vsi, netdev_features_t features)
5990{
5991	struct ice_vsi_vlan_ops *vlan_ops = ice_get_compat_vsi_vlan_ops(vsi);
5992	int err = 0;
5993
5994	/* support Single VLAN Mode (SVM) and Double VLAN Mode (DVM) by checking
5995	 * if either bit is set
5996	 */
5997	if (features &
5998	    (NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER))
5999		err = vlan_ops->ena_rx_filtering(vsi);
6000	else
6001		err = vlan_ops->dis_rx_filtering(vsi);
6002
6003	return err;
6004}
6005
6006/**
6007 * ice_set_vlan_features - set VLAN settings based on suggested feature set
6008 * @netdev: ptr to the netdev being adjusted
6009 * @features: the feature set that the stack is suggesting
6010 *
6011 * Only update VLAN settings if the requested_vlan_features are different than
6012 * the current_vlan_features.
6013 */
6014static int
6015ice_set_vlan_features(struct net_device *netdev, netdev_features_t features)
6016{
6017	netdev_features_t current_vlan_features, requested_vlan_features;
6018	struct ice_netdev_priv *np = netdev_priv(netdev);
6019	struct ice_vsi *vsi = np->vsi;
6020	int err;
6021
6022	current_vlan_features = netdev->features & NETIF_VLAN_OFFLOAD_FEATURES;
6023	requested_vlan_features = features & NETIF_VLAN_OFFLOAD_FEATURES;
6024	if (current_vlan_features ^ requested_vlan_features) {
6025		if ((features & NETIF_F_RXFCS) &&
6026		    (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6027			dev_err(ice_pf_to_dev(vsi->back),
6028				"To enable VLAN stripping, you must first enable FCS/CRC stripping\n");
6029			return -EIO;
6030		}
6031
6032		err = ice_set_vlan_offload_features(vsi, features);
6033		if (err)
6034			return err;
6035	}
6036
6037	current_vlan_features = netdev->features &
6038		NETIF_VLAN_FILTERING_FEATURES;
6039	requested_vlan_features = features & NETIF_VLAN_FILTERING_FEATURES;
6040	if (current_vlan_features ^ requested_vlan_features) {
6041		err = ice_set_vlan_filtering_features(vsi, features);
6042		if (err)
6043			return err;
6044	}
6045
6046	return 0;
6047}
6048
6049/**
6050 * ice_set_loopback - turn on/off loopback mode on underlying PF
6051 * @vsi: ptr to VSI
6052 * @ena: flag to indicate the on/off setting
6053 */
6054static int ice_set_loopback(struct ice_vsi *vsi, bool ena)
6055{
6056	bool if_running = netif_running(vsi->netdev);
6057	int ret;
6058
6059	if (if_running && !test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
6060		ret = ice_down(vsi);
6061		if (ret) {
6062			netdev_err(vsi->netdev, "Preparing device to toggle loopback failed\n");
6063			return ret;
6064		}
6065	}
6066	ret = ice_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
6067	if (ret)
6068		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
6069	if (if_running)
6070		ret = ice_up(vsi);
6071
6072	return ret;
6073}
6074
6075/**
6076 * ice_set_features - set the netdev feature flags
6077 * @netdev: ptr to the netdev being adjusted
6078 * @features: the feature set that the stack is suggesting
6079 */
6080static int
6081ice_set_features(struct net_device *netdev, netdev_features_t features)
6082{
6083	netdev_features_t changed = netdev->features ^ features;
6084	struct ice_netdev_priv *np = netdev_priv(netdev);
6085	struct ice_vsi *vsi = np->vsi;
6086	struct ice_pf *pf = vsi->back;
6087	int ret = 0;
6088
6089	/* Don't set any netdev advanced features with device in Safe Mode */
6090	if (ice_is_safe_mode(pf)) {
6091		dev_err(ice_pf_to_dev(pf),
6092			"Device is in Safe Mode - not enabling advanced netdev features\n");
6093		return ret;
6094	}
6095
6096	/* Do not change setting during reset */
6097	if (ice_is_reset_in_progress(pf->state)) {
6098		dev_err(ice_pf_to_dev(pf),
6099			"Device is resetting, changing advanced netdev features temporarily unavailable.\n");
6100		return -EBUSY;
6101	}
6102
6103	/* Multiple features can be changed in one call so keep features in
6104	 * separate if/else statements to guarantee each feature is checked
6105	 */
6106	if (changed & NETIF_F_RXHASH)
6107		ice_vsi_manage_rss_lut(vsi, !!(features & NETIF_F_RXHASH));
6108
6109	ret = ice_set_vlan_features(netdev, features);
6110	if (ret)
6111		return ret;
6112
6113	/* Turn on receive of FCS aka CRC, and after setting this
6114	 * flag the packet data will have the 4 byte CRC appended
6115	 */
6116	if (changed & NETIF_F_RXFCS) {
6117		if ((features & NETIF_F_RXFCS) &&
6118		    (features & NETIF_VLAN_STRIPPING_FEATURES)) {
6119			dev_err(ice_pf_to_dev(vsi->back),
6120				"To disable FCS/CRC stripping, you must first disable VLAN stripping\n");
6121			return -EIO;
6122		}
6123
6124		ice_vsi_cfg_crc_strip(vsi, !!(features & NETIF_F_RXFCS));
6125		ret = ice_down_up(vsi);
6126		if (ret)
6127			return ret;
6128	}
6129
6130	if (changed & NETIF_F_NTUPLE) {
6131		bool ena = !!(features & NETIF_F_NTUPLE);
6132
6133		ice_vsi_manage_fdir(vsi, ena);
6134		ena ? ice_init_arfs(vsi) : ice_clear_arfs(vsi);
6135	}
6136
6137	/* don't turn off hw_tc_offload when ADQ is already enabled */
6138	if (!(features & NETIF_F_HW_TC) && ice_is_adq_active(pf)) {
6139		dev_err(ice_pf_to_dev(pf), "ADQ is active, can't turn hw_tc_offload off\n");
6140		return -EACCES;
6141	}
6142
6143	if (changed & NETIF_F_HW_TC) {
6144		bool ena = !!(features & NETIF_F_HW_TC);
6145
6146		ena ? set_bit(ICE_FLAG_CLS_FLOWER, pf->flags) :
6147		      clear_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
6148	}
6149
6150	if (changed & NETIF_F_LOOPBACK)
6151		ret = ice_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
6152
6153	return ret;
6154}
6155
6156/**
6157 * ice_vsi_vlan_setup - Setup VLAN offload properties on a PF VSI
6158 * @vsi: VSI to setup VLAN properties for
6159 */
6160static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
6161{
6162	int err;
6163
6164	err = ice_set_vlan_offload_features(vsi, vsi->netdev->features);
6165	if (err)
6166		return err;
6167
6168	err = ice_set_vlan_filtering_features(vsi, vsi->netdev->features);
6169	if (err)
6170		return err;
6171
6172	return ice_vsi_add_vlan_zero(vsi);
6173}
6174
6175/**
6176 * ice_vsi_cfg - Setup the VSI
6177 * @vsi: the VSI being configured
6178 *
6179 * Return 0 on success and negative value on error
6180 */
6181int ice_vsi_cfg(struct ice_vsi *vsi)
6182{
6183	int err;
6184
6185	if (vsi->netdev) {
6186		ice_set_rx_mode(vsi->netdev);
6187
6188		if (vsi->type != ICE_VSI_LB) {
6189			err = ice_vsi_vlan_setup(vsi);
6190
6191			if (err)
6192				return err;
6193		}
6194	}
6195	ice_vsi_cfg_dcb_rings(vsi);
6196
6197	err = ice_vsi_cfg_lan_txqs(vsi);
6198	if (!err && ice_is_xdp_ena_vsi(vsi))
6199		err = ice_vsi_cfg_xdp_txqs(vsi);
6200	if (!err)
6201		err = ice_vsi_cfg_rxqs(vsi);
6202
6203	return err;
6204}
6205
6206/* THEORY OF MODERATION:
6207 * The ice driver hardware works differently than the hardware that DIMLIB was
6208 * originally made for. ice hardware doesn't have packet count limits that
6209 * can trigger an interrupt, but it *does* have interrupt rate limit support,
6210 * which is hard-coded to a limit of 250,000 ints/second.
6211 * If not using dynamic moderation, the INTRL value can be modified
6212 * by ethtool rx-usecs-high.
6213 */
6214struct ice_dim {
6215	/* the throttle rate for interrupts, basically worst case delay before
6216	 * an initial interrupt fires, value is stored in microseconds.
6217	 */
6218	u16 itr;
6219};
6220
6221/* Make a different profile for Rx that doesn't allow quite so aggressive
6222 * moderation at the high end (it maxes out at 126us or about 8k interrupts a
6223 * second.
6224 */
6225static const struct ice_dim rx_profile[] = {
6226	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6227	{8},    /* 125,000 ints/s */
6228	{16},   /*  62,500 ints/s */
6229	{62},   /*  16,129 ints/s */
6230	{126}   /*   7,936 ints/s */
6231};
6232
6233/* The transmit profile, which has the same sorts of values
6234 * as the previous struct
6235 */
6236static const struct ice_dim tx_profile[] = {
6237	{2},    /* 500,000 ints/s, capped at 250K by INTRL */
6238	{8},    /* 125,000 ints/s */
6239	{40},   /*  16,125 ints/s */
6240	{128},  /*   7,812 ints/s */
6241	{256}   /*   3,906 ints/s */
6242};
6243
6244static void ice_tx_dim_work(struct work_struct *work)
6245{
6246	struct ice_ring_container *rc;
6247	struct dim *dim;
6248	u16 itr;
6249
6250	dim = container_of(work, struct dim, work);
6251	rc = (struct ice_ring_container *)dim->priv;
6252
6253	WARN_ON(dim->profile_ix >= ARRAY_SIZE(tx_profile));
6254
6255	/* look up the values in our local table */
6256	itr = tx_profile[dim->profile_ix].itr;
6257
6258	ice_trace(tx_dim_work, container_of(rc, struct ice_q_vector, tx), dim);
6259	ice_write_itr(rc, itr);
6260
6261	dim->state = DIM_START_MEASURE;
6262}
6263
6264static void ice_rx_dim_work(struct work_struct *work)
6265{
6266	struct ice_ring_container *rc;
6267	struct dim *dim;
6268	u16 itr;
6269
6270	dim = container_of(work, struct dim, work);
6271	rc = (struct ice_ring_container *)dim->priv;
6272
6273	WARN_ON(dim->profile_ix >= ARRAY_SIZE(rx_profile));
6274
6275	/* look up the values in our local table */
6276	itr = rx_profile[dim->profile_ix].itr;
6277
6278	ice_trace(rx_dim_work, container_of(rc, struct ice_q_vector, rx), dim);
6279	ice_write_itr(rc, itr);
6280
6281	dim->state = DIM_START_MEASURE;
6282}
6283
6284#define ICE_DIM_DEFAULT_PROFILE_IX 1
6285
6286/**
6287 * ice_init_moderation - set up interrupt moderation
6288 * @q_vector: the vector containing rings to be configured
6289 *
6290 * Set up interrupt moderation registers, with the intent to do the right thing
6291 * when called from reset or from probe, and whether or not dynamic moderation
6292 * is enabled or not. Take special care to write all the registers in both
6293 * dynamic moderation mode or not in order to make sure hardware is in a known
6294 * state.
6295 */
6296static void ice_init_moderation(struct ice_q_vector *q_vector)
6297{
6298	struct ice_ring_container *rc;
6299	bool tx_dynamic, rx_dynamic;
6300
6301	rc = &q_vector->tx;
6302	INIT_WORK(&rc->dim.work, ice_tx_dim_work);
6303	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6304	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6305	rc->dim.priv = rc;
6306	tx_dynamic = ITR_IS_DYNAMIC(rc);
6307
6308	/* set the initial TX ITR to match the above */
6309	ice_write_itr(rc, tx_dynamic ?
6310		      tx_profile[rc->dim.profile_ix].itr : rc->itr_setting);
6311
6312	rc = &q_vector->rx;
6313	INIT_WORK(&rc->dim.work, ice_rx_dim_work);
6314	rc->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
6315	rc->dim.profile_ix = ICE_DIM_DEFAULT_PROFILE_IX;
6316	rc->dim.priv = rc;
6317	rx_dynamic = ITR_IS_DYNAMIC(rc);
6318
6319	/* set the initial RX ITR to match the above */
6320	ice_write_itr(rc, rx_dynamic ? rx_profile[rc->dim.profile_ix].itr :
6321				       rc->itr_setting);
6322
6323	ice_set_q_vector_intrl(q_vector);
6324}
6325
6326/**
6327 * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
6328 * @vsi: the VSI being configured
6329 */
6330static void ice_napi_enable_all(struct ice_vsi *vsi)
6331{
6332	int q_idx;
6333
6334	if (!vsi->netdev)
6335		return;
6336
6337	ice_for_each_q_vector(vsi, q_idx) {
6338		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6339
6340		ice_init_moderation(q_vector);
6341
6342		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6343			napi_enable(&q_vector->napi);
6344	}
6345}
6346
6347/**
6348 * ice_up_complete - Finish the last steps of bringing up a connection
6349 * @vsi: The VSI being configured
6350 *
6351 * Return 0 on success and negative value on error
6352 */
6353static int ice_up_complete(struct ice_vsi *vsi)
6354{
6355	struct ice_pf *pf = vsi->back;
6356	int err;
6357
6358	ice_vsi_cfg_msix(vsi);
6359
6360	/* Enable only Rx rings, Tx rings were enabled by the FW when the
6361	 * Tx queue group list was configured and the context bits were
6362	 * programmed using ice_vsi_cfg_txqs
6363	 */
6364	err = ice_vsi_start_all_rx_rings(vsi);
6365	if (err)
6366		return err;
6367
6368	clear_bit(ICE_VSI_DOWN, vsi->state);
6369	ice_napi_enable_all(vsi);
6370	ice_vsi_ena_irq(vsi);
6371
6372	if (vsi->port_info &&
6373	    (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
6374	    vsi->netdev) {
6375		ice_print_link_msg(vsi, true);
6376		netif_tx_start_all_queues(vsi->netdev);
6377		netif_carrier_on(vsi->netdev);
6378		ice_ptp_link_change(pf, pf->hw.pf_id, true);
6379	}
6380
6381	/* Perform an initial read of the statistics registers now to
6382	 * set the baseline so counters are ready when interface is up
6383	 */
6384	ice_update_eth_stats(vsi);
6385	ice_service_task_schedule(pf);
 
 
6386
6387	return 0;
6388}
6389
6390/**
6391 * ice_up - Bring the connection back up after being down
6392 * @vsi: VSI being configured
6393 */
6394int ice_up(struct ice_vsi *vsi)
6395{
6396	int err;
6397
6398	err = ice_vsi_cfg(vsi);
6399	if (!err)
6400		err = ice_up_complete(vsi);
6401
6402	return err;
6403}
6404
6405/**
6406 * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
6407 * @syncp: pointer to u64_stats_sync
6408 * @stats: stats that pkts and bytes count will be taken from
6409 * @pkts: packets stats counter
6410 * @bytes: bytes stats counter
6411 *
6412 * This function fetches stats from the ring considering the atomic operations
6413 * that needs to be performed to read u64 values in 32 bit machine.
6414 */
6415void
6416ice_fetch_u64_stats_per_ring(struct u64_stats_sync *syncp,
6417			     struct ice_q_stats stats, u64 *pkts, u64 *bytes)
6418{
6419	unsigned int start;
6420
6421	do {
6422		start = u64_stats_fetch_begin(syncp);
6423		*pkts = stats.pkts;
6424		*bytes = stats.bytes;
6425	} while (u64_stats_fetch_retry(syncp, start));
6426}
6427
6428/**
6429 * ice_update_vsi_tx_ring_stats - Update VSI Tx ring stats counters
6430 * @vsi: the VSI to be updated
6431 * @vsi_stats: the stats struct to be updated
6432 * @rings: rings to work on
6433 * @count: number of rings
6434 */
6435static void
6436ice_update_vsi_tx_ring_stats(struct ice_vsi *vsi,
6437			     struct rtnl_link_stats64 *vsi_stats,
6438			     struct ice_tx_ring **rings, u16 count)
6439{
6440	u16 i;
6441
6442	for (i = 0; i < count; i++) {
6443		struct ice_tx_ring *ring;
6444		u64 pkts = 0, bytes = 0;
6445
6446		ring = READ_ONCE(rings[i]);
6447		if (!ring || !ring->ring_stats)
6448			continue;
6449		ice_fetch_u64_stats_per_ring(&ring->ring_stats->syncp,
6450					     ring->ring_stats->stats, &pkts,
6451					     &bytes);
6452		vsi_stats->tx_packets += pkts;
6453		vsi_stats->tx_bytes += bytes;
6454		vsi->tx_restart += ring->ring_stats->tx_stats.restart_q;
6455		vsi->tx_busy += ring->ring_stats->tx_stats.tx_busy;
6456		vsi->tx_linearize += ring->ring_stats->tx_stats.tx_linearize;
6457	}
6458}
6459
6460/**
6461 * ice_update_vsi_ring_stats - Update VSI stats counters
6462 * @vsi: the VSI to be updated
6463 */
6464static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
6465{
6466	struct rtnl_link_stats64 *net_stats, *stats_prev;
6467	struct rtnl_link_stats64 *vsi_stats;
6468	u64 pkts, bytes;
6469	int i;
6470
6471	vsi_stats = kzalloc(sizeof(*vsi_stats), GFP_ATOMIC);
6472	if (!vsi_stats)
6473		return;
6474
6475	/* reset non-netdev (extended) stats */
6476	vsi->tx_restart = 0;
6477	vsi->tx_busy = 0;
6478	vsi->tx_linearize = 0;
6479	vsi->rx_buf_failed = 0;
6480	vsi->rx_page_failed = 0;
6481
6482	rcu_read_lock();
6483
6484	/* update Tx rings counters */
6485	ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->tx_rings,
6486				     vsi->num_txq);
6487
6488	/* update Rx rings counters */
6489	ice_for_each_rxq(vsi, i) {
6490		struct ice_rx_ring *ring = READ_ONCE(vsi->rx_rings[i]);
6491		struct ice_ring_stats *ring_stats;
6492
6493		ring_stats = ring->ring_stats;
6494		ice_fetch_u64_stats_per_ring(&ring_stats->syncp,
6495					     ring_stats->stats, &pkts,
6496					     &bytes);
6497		vsi_stats->rx_packets += pkts;
6498		vsi_stats->rx_bytes += bytes;
6499		vsi->rx_buf_failed += ring_stats->rx_stats.alloc_buf_failed;
6500		vsi->rx_page_failed += ring_stats->rx_stats.alloc_page_failed;
6501	}
6502
6503	/* update XDP Tx rings counters */
6504	if (ice_is_xdp_ena_vsi(vsi))
6505		ice_update_vsi_tx_ring_stats(vsi, vsi_stats, vsi->xdp_rings,
6506					     vsi->num_xdp_txq);
6507
6508	rcu_read_unlock();
6509
6510	net_stats = &vsi->net_stats;
6511	stats_prev = &vsi->net_stats_prev;
6512
6513	/* clear prev counters after reset */
6514	if (vsi_stats->tx_packets < stats_prev->tx_packets ||
6515	    vsi_stats->rx_packets < stats_prev->rx_packets) {
6516		stats_prev->tx_packets = 0;
6517		stats_prev->tx_bytes = 0;
6518		stats_prev->rx_packets = 0;
6519		stats_prev->rx_bytes = 0;
6520	}
6521
6522	/* update netdev counters */
6523	net_stats->tx_packets += vsi_stats->tx_packets - stats_prev->tx_packets;
6524	net_stats->tx_bytes += vsi_stats->tx_bytes - stats_prev->tx_bytes;
6525	net_stats->rx_packets += vsi_stats->rx_packets - stats_prev->rx_packets;
6526	net_stats->rx_bytes += vsi_stats->rx_bytes - stats_prev->rx_bytes;
6527
6528	stats_prev->tx_packets = vsi_stats->tx_packets;
6529	stats_prev->tx_bytes = vsi_stats->tx_bytes;
6530	stats_prev->rx_packets = vsi_stats->rx_packets;
6531	stats_prev->rx_bytes = vsi_stats->rx_bytes;
6532
6533	kfree(vsi_stats);
6534}
6535
6536/**
6537 * ice_update_vsi_stats - Update VSI stats counters
6538 * @vsi: the VSI to be updated
6539 */
6540void ice_update_vsi_stats(struct ice_vsi *vsi)
6541{
6542	struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
6543	struct ice_eth_stats *cur_es = &vsi->eth_stats;
6544	struct ice_pf *pf = vsi->back;
6545
6546	if (test_bit(ICE_VSI_DOWN, vsi->state) ||
6547	    test_bit(ICE_CFG_BUSY, pf->state))
6548		return;
6549
6550	/* get stats as recorded by Tx/Rx rings */
6551	ice_update_vsi_ring_stats(vsi);
6552
6553	/* get VSI stats as recorded by the hardware */
6554	ice_update_eth_stats(vsi);
6555
6556	cur_ns->tx_errors = cur_es->tx_errors;
6557	cur_ns->rx_dropped = cur_es->rx_discards;
6558	cur_ns->tx_dropped = cur_es->tx_discards;
6559	cur_ns->multicast = cur_es->rx_multicast;
6560
6561	/* update some more netdev stats if this is main VSI */
6562	if (vsi->type == ICE_VSI_PF) {
6563		cur_ns->rx_crc_errors = pf->stats.crc_errors;
6564		cur_ns->rx_errors = pf->stats.crc_errors +
6565				    pf->stats.illegal_bytes +
6566				    pf->stats.rx_len_errors +
6567				    pf->stats.rx_undersize +
6568				    pf->hw_csum_rx_error +
6569				    pf->stats.rx_jabber +
6570				    pf->stats.rx_fragments +
6571				    pf->stats.rx_oversize;
6572		cur_ns->rx_length_errors = pf->stats.rx_len_errors;
6573		/* record drops from the port level */
6574		cur_ns->rx_missed_errors = pf->stats.eth.rx_discards;
6575	}
6576}
6577
6578/**
6579 * ice_update_pf_stats - Update PF port stats counters
6580 * @pf: PF whose stats needs to be updated
6581 */
6582void ice_update_pf_stats(struct ice_pf *pf)
6583{
6584	struct ice_hw_port_stats *prev_ps, *cur_ps;
6585	struct ice_hw *hw = &pf->hw;
6586	u16 fd_ctr_base;
6587	u8 port;
6588
6589	port = hw->port_info->lport;
6590	prev_ps = &pf->stats_prev;
6591	cur_ps = &pf->stats;
6592
6593	if (ice_is_reset_in_progress(pf->state))
6594		pf->stat_prev_loaded = false;
6595
6596	ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded,
6597			  &prev_ps->eth.rx_bytes,
6598			  &cur_ps->eth.rx_bytes);
6599
6600	ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded,
6601			  &prev_ps->eth.rx_unicast,
6602			  &cur_ps->eth.rx_unicast);
6603
6604	ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded,
6605			  &prev_ps->eth.rx_multicast,
6606			  &cur_ps->eth.rx_multicast);
6607
6608	ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded,
6609			  &prev_ps->eth.rx_broadcast,
6610			  &cur_ps->eth.rx_broadcast);
6611
6612	ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded,
6613			  &prev_ps->eth.rx_discards,
6614			  &cur_ps->eth.rx_discards);
6615
6616	ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded,
6617			  &prev_ps->eth.tx_bytes,
6618			  &cur_ps->eth.tx_bytes);
6619
6620	ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded,
6621			  &prev_ps->eth.tx_unicast,
6622			  &cur_ps->eth.tx_unicast);
6623
6624	ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded,
6625			  &prev_ps->eth.tx_multicast,
6626			  &cur_ps->eth.tx_multicast);
6627
6628	ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded,
6629			  &prev_ps->eth.tx_broadcast,
6630			  &cur_ps->eth.tx_broadcast);
6631
6632	ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded,
6633			  &prev_ps->tx_dropped_link_down,
6634			  &cur_ps->tx_dropped_link_down);
6635
6636	ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded,
6637			  &prev_ps->rx_size_64, &cur_ps->rx_size_64);
6638
6639	ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded,
6640			  &prev_ps->rx_size_127, &cur_ps->rx_size_127);
6641
6642	ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded,
6643			  &prev_ps->rx_size_255, &cur_ps->rx_size_255);
6644
6645	ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded,
6646			  &prev_ps->rx_size_511, &cur_ps->rx_size_511);
6647
6648	ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded,
6649			  &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
6650
6651	ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded,
6652			  &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
6653
6654	ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded,
6655			  &prev_ps->rx_size_big, &cur_ps->rx_size_big);
6656
6657	ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded,
6658			  &prev_ps->tx_size_64, &cur_ps->tx_size_64);
6659
6660	ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded,
6661			  &prev_ps->tx_size_127, &cur_ps->tx_size_127);
6662
6663	ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded,
6664			  &prev_ps->tx_size_255, &cur_ps->tx_size_255);
6665
6666	ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded,
6667			  &prev_ps->tx_size_511, &cur_ps->tx_size_511);
6668
6669	ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded,
6670			  &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
6671
6672	ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded,
6673			  &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
6674
6675	ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded,
6676			  &prev_ps->tx_size_big, &cur_ps->tx_size_big);
6677
6678	fd_ctr_base = hw->fd_ctr_base;
6679
6680	ice_stat_update40(hw,
6681			  GLSTAT_FD_CNT0L(ICE_FD_SB_STAT_IDX(fd_ctr_base)),
6682			  pf->stat_prev_loaded, &prev_ps->fd_sb_match,
6683			  &cur_ps->fd_sb_match);
6684	ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded,
6685			  &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
6686
6687	ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded,
6688			  &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
6689
6690	ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded,
6691			  &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
6692
6693	ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded,
6694			  &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
6695
6696	ice_update_dcb_stats(pf);
6697
6698	ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded,
6699			  &prev_ps->crc_errors, &cur_ps->crc_errors);
6700
6701	ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded,
6702			  &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
6703
6704	ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded,
6705			  &prev_ps->mac_local_faults,
6706			  &cur_ps->mac_local_faults);
6707
6708	ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded,
6709			  &prev_ps->mac_remote_faults,
6710			  &cur_ps->mac_remote_faults);
6711
6712	ice_stat_update32(hw, GLPRT_RLEC(port), pf->stat_prev_loaded,
6713			  &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
6714
6715	ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded,
6716			  &prev_ps->rx_undersize, &cur_ps->rx_undersize);
6717
6718	ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded,
6719			  &prev_ps->rx_fragments, &cur_ps->rx_fragments);
6720
6721	ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded,
6722			  &prev_ps->rx_oversize, &cur_ps->rx_oversize);
6723
6724	ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded,
6725			  &prev_ps->rx_jabber, &cur_ps->rx_jabber);
6726
6727	cur_ps->fd_sb_status = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0;
6728
6729	pf->stat_prev_loaded = true;
6730}
6731
6732/**
6733 * ice_get_stats64 - get statistics for network device structure
6734 * @netdev: network interface device structure
6735 * @stats: main device statistics structure
6736 */
6737static
6738void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
6739{
6740	struct ice_netdev_priv *np = netdev_priv(netdev);
6741	struct rtnl_link_stats64 *vsi_stats;
6742	struct ice_vsi *vsi = np->vsi;
6743
6744	vsi_stats = &vsi->net_stats;
6745
6746	if (!vsi->num_txq || !vsi->num_rxq)
6747		return;
6748
6749	/* netdev packet/byte stats come from ring counter. These are obtained
6750	 * by summing up ring counters (done by ice_update_vsi_ring_stats).
6751	 * But, only call the update routine and read the registers if VSI is
6752	 * not down.
6753	 */
6754	if (!test_bit(ICE_VSI_DOWN, vsi->state))
6755		ice_update_vsi_ring_stats(vsi);
6756	stats->tx_packets = vsi_stats->tx_packets;
6757	stats->tx_bytes = vsi_stats->tx_bytes;
6758	stats->rx_packets = vsi_stats->rx_packets;
6759	stats->rx_bytes = vsi_stats->rx_bytes;
6760
6761	/* The rest of the stats can be read from the hardware but instead we
6762	 * just return values that the watchdog task has already obtained from
6763	 * the hardware.
6764	 */
6765	stats->multicast = vsi_stats->multicast;
6766	stats->tx_errors = vsi_stats->tx_errors;
6767	stats->tx_dropped = vsi_stats->tx_dropped;
6768	stats->rx_errors = vsi_stats->rx_errors;
6769	stats->rx_dropped = vsi_stats->rx_dropped;
6770	stats->rx_crc_errors = vsi_stats->rx_crc_errors;
6771	stats->rx_length_errors = vsi_stats->rx_length_errors;
6772}
6773
6774/**
6775 * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
6776 * @vsi: VSI having NAPI disabled
6777 */
6778static void ice_napi_disable_all(struct ice_vsi *vsi)
6779{
6780	int q_idx;
6781
6782	if (!vsi->netdev)
6783		return;
6784
6785	ice_for_each_q_vector(vsi, q_idx) {
6786		struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
6787
6788		if (q_vector->rx.rx_ring || q_vector->tx.tx_ring)
6789			napi_disable(&q_vector->napi);
6790
6791		cancel_work_sync(&q_vector->tx.dim.work);
6792		cancel_work_sync(&q_vector->rx.dim.work);
6793	}
6794}
6795
6796/**
6797 * ice_down - Shutdown the connection
6798 * @vsi: The VSI being stopped
6799 *
6800 * Caller of this function is expected to set the vsi->state ICE_DOWN bit
6801 */
6802int ice_down(struct ice_vsi *vsi)
6803{
6804	int i, tx_err, rx_err, vlan_err = 0;
6805
6806	WARN_ON(!test_bit(ICE_VSI_DOWN, vsi->state));
6807
6808	if (vsi->netdev && vsi->type == ICE_VSI_PF) {
6809		vlan_err = ice_vsi_del_vlan_zero(vsi);
6810		ice_ptp_link_change(vsi->back, vsi->back->hw.pf_id, false);
6811		netif_carrier_off(vsi->netdev);
6812		netif_tx_disable(vsi->netdev);
6813	} else if (vsi->type == ICE_VSI_SWITCHDEV_CTRL) {
6814		ice_eswitch_stop_all_tx_queues(vsi->back);
6815	}
6816
6817	ice_vsi_dis_irq(vsi);
6818
6819	tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0);
6820	if (tx_err)
6821		netdev_err(vsi->netdev, "Failed stop Tx rings, VSI %d error %d\n",
6822			   vsi->vsi_num, tx_err);
6823	if (!tx_err && ice_is_xdp_ena_vsi(vsi)) {
6824		tx_err = ice_vsi_stop_xdp_tx_rings(vsi);
6825		if (tx_err)
6826			netdev_err(vsi->netdev, "Failed stop XDP rings, VSI %d error %d\n",
6827				   vsi->vsi_num, tx_err);
6828	}
6829
6830	rx_err = ice_vsi_stop_all_rx_rings(vsi);
6831	if (rx_err)
6832		netdev_err(vsi->netdev, "Failed stop Rx rings, VSI %d error %d\n",
6833			   vsi->vsi_num, rx_err);
6834
6835	ice_napi_disable_all(vsi);
6836
6837	ice_for_each_txq(vsi, i)
6838		ice_clean_tx_ring(vsi->tx_rings[i]);
6839
 
 
 
 
6840	ice_for_each_rxq(vsi, i)
6841		ice_clean_rx_ring(vsi->rx_rings[i]);
6842
6843	if (tx_err || rx_err || vlan_err) {
6844		netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
6845			   vsi->vsi_num, vsi->vsw->sw_id);
6846		return -EIO;
6847	}
6848
6849	return 0;
6850}
6851
6852/**
6853 * ice_down_up - shutdown the VSI connection and bring it up
6854 * @vsi: the VSI to be reconnected
6855 */
6856int ice_down_up(struct ice_vsi *vsi)
6857{
6858	int ret;
6859
6860	/* if DOWN already set, nothing to do */
6861	if (test_and_set_bit(ICE_VSI_DOWN, vsi->state))
6862		return 0;
6863
6864	ret = ice_down(vsi);
6865	if (ret)
6866		return ret;
6867
6868	ret = ice_up(vsi);
6869	if (ret) {
6870		netdev_err(vsi->netdev, "reallocating resources failed during netdev features change, may need to reload driver\n");
6871		return ret;
6872	}
6873
6874	return 0;
6875}
6876
6877/**
6878 * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
6879 * @vsi: VSI having resources allocated
6880 *
6881 * Return 0 on success, negative on failure
6882 */
6883int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
6884{
6885	int i, err = 0;
6886
6887	if (!vsi->num_txq) {
6888		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Tx queues\n",
6889			vsi->vsi_num);
6890		return -EINVAL;
6891	}
6892
6893	ice_for_each_txq(vsi, i) {
6894		struct ice_tx_ring *ring = vsi->tx_rings[i];
6895
6896		if (!ring)
6897			return -EINVAL;
6898
6899		if (vsi->netdev)
6900			ring->netdev = vsi->netdev;
6901		err = ice_setup_tx_ring(ring);
6902		if (err)
6903			break;
6904	}
6905
6906	return err;
6907}
6908
6909/**
6910 * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
6911 * @vsi: VSI having resources allocated
6912 *
6913 * Return 0 on success, negative on failure
6914 */
6915int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
6916{
6917	int i, err = 0;
6918
6919	if (!vsi->num_rxq) {
6920		dev_err(ice_pf_to_dev(vsi->back), "VSI %d has 0 Rx queues\n",
6921			vsi->vsi_num);
6922		return -EINVAL;
6923	}
6924
6925	ice_for_each_rxq(vsi, i) {
6926		struct ice_rx_ring *ring = vsi->rx_rings[i];
6927
6928		if (!ring)
6929			return -EINVAL;
6930
6931		if (vsi->netdev)
6932			ring->netdev = vsi->netdev;
6933		err = ice_setup_rx_ring(ring);
6934		if (err)
6935			break;
6936	}
6937
6938	return err;
6939}
6940
6941/**
6942 * ice_vsi_open_ctrl - open control VSI for use
6943 * @vsi: the VSI to open
6944 *
6945 * Initialization of the Control VSI
6946 *
6947 * Returns 0 on success, negative value on error
6948 */
6949int ice_vsi_open_ctrl(struct ice_vsi *vsi)
6950{
6951	char int_name[ICE_INT_NAME_STR_LEN];
6952	struct ice_pf *pf = vsi->back;
6953	struct device *dev;
6954	int err;
6955
6956	dev = ice_pf_to_dev(pf);
6957	/* allocate descriptors */
6958	err = ice_vsi_setup_tx_rings(vsi);
6959	if (err)
6960		goto err_setup_tx;
6961
6962	err = ice_vsi_setup_rx_rings(vsi);
6963	if (err)
6964		goto err_setup_rx;
6965
6966	err = ice_vsi_cfg(vsi);
6967	if (err)
6968		goto err_setup_rx;
6969
6970	snprintf(int_name, sizeof(int_name) - 1, "%s-%s:ctrl",
6971		 dev_driver_string(dev), dev_name(dev));
6972	err = ice_vsi_req_irq_msix(vsi, int_name);
6973	if (err)
6974		goto err_setup_rx;
6975
6976	ice_vsi_cfg_msix(vsi);
6977
6978	err = ice_vsi_start_all_rx_rings(vsi);
6979	if (err)
6980		goto err_up_complete;
6981
6982	clear_bit(ICE_VSI_DOWN, vsi->state);
6983	ice_vsi_ena_irq(vsi);
6984
6985	return 0;
6986
6987err_up_complete:
6988	ice_down(vsi);
6989err_setup_rx:
6990	ice_vsi_free_rx_rings(vsi);
6991err_setup_tx:
6992	ice_vsi_free_tx_rings(vsi);
6993
6994	return err;
6995}
6996
6997/**
6998 * ice_vsi_open - Called when a network interface is made active
6999 * @vsi: the VSI to open
7000 *
7001 * Initialization of the VSI
7002 *
7003 * Returns 0 on success, negative value on error
7004 */
7005int ice_vsi_open(struct ice_vsi *vsi)
7006{
7007	char int_name[ICE_INT_NAME_STR_LEN];
7008	struct ice_pf *pf = vsi->back;
7009	int err;
7010
7011	/* allocate descriptors */
7012	err = ice_vsi_setup_tx_rings(vsi);
7013	if (err)
7014		goto err_setup_tx;
7015
7016	err = ice_vsi_setup_rx_rings(vsi);
7017	if (err)
7018		goto err_setup_rx;
7019
7020	err = ice_vsi_cfg(vsi);
7021	if (err)
7022		goto err_setup_rx;
7023
7024	snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
7025		 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name);
7026	err = ice_vsi_req_irq_msix(vsi, int_name);
7027	if (err)
7028		goto err_setup_rx;
7029
7030	ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc);
7031
7032	if (vsi->type == ICE_VSI_PF) {
7033		/* Notify the stack of the actual queue counts. */
7034		err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
7035		if (err)
7036			goto err_set_qs;
7037
7038		err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
7039		if (err)
7040			goto err_set_qs;
7041	}
7042
7043	err = ice_up_complete(vsi);
7044	if (err)
7045		goto err_up_complete;
7046
7047	return 0;
7048
7049err_up_complete:
7050	ice_down(vsi);
7051err_set_qs:
7052	ice_vsi_free_irq(vsi);
7053err_setup_rx:
7054	ice_vsi_free_rx_rings(vsi);
7055err_setup_tx:
7056	ice_vsi_free_tx_rings(vsi);
7057
7058	return err;
7059}
7060
7061/**
7062 * ice_vsi_release_all - Delete all VSIs
7063 * @pf: PF from which all VSIs are being removed
7064 */
7065static void ice_vsi_release_all(struct ice_pf *pf)
7066{
7067	int err, i;
7068
7069	if (!pf->vsi)
7070		return;
7071
7072	ice_for_each_vsi(pf, i) {
7073		if (!pf->vsi[i])
7074			continue;
7075
7076		if (pf->vsi[i]->type == ICE_VSI_CHNL)
7077			continue;
7078
7079		err = ice_vsi_release(pf->vsi[i]);
7080		if (err)
7081			dev_dbg(ice_pf_to_dev(pf), "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n",
7082				i, err, pf->vsi[i]->vsi_num);
7083	}
7084}
7085
7086/**
7087 * ice_vsi_rebuild_by_type - Rebuild VSI of a given type
7088 * @pf: pointer to the PF instance
7089 * @type: VSI type to rebuild
7090 *
7091 * Iterates through the pf->vsi array and rebuilds VSIs of the requested type
7092 */
7093static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type)
7094{
7095	struct device *dev = ice_pf_to_dev(pf);
7096	int i, err;
7097
7098	ice_for_each_vsi(pf, i) {
7099		struct ice_vsi *vsi = pf->vsi[i];
7100
7101		if (!vsi || vsi->type != type)
7102			continue;
7103
7104		/* rebuild the VSI */
7105		err = ice_vsi_rebuild(vsi, true);
7106		if (err) {
7107			dev_err(dev, "rebuild VSI failed, err %d, VSI index %d, type %s\n",
7108				err, vsi->idx, ice_vsi_type_str(type));
7109			return err;
7110		}
7111
7112		/* replay filters for the VSI */
7113		err = ice_replay_vsi(&pf->hw, vsi->idx);
7114		if (err) {
7115			dev_err(dev, "replay VSI failed, error %d, VSI index %d, type %s\n",
7116				err, vsi->idx, ice_vsi_type_str(type));
7117			return err;
7118		}
7119
7120		/* Re-map HW VSI number, using VSI handle that has been
7121		 * previously validated in ice_replay_vsi() call above
7122		 */
7123		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
7124
7125		/* enable the VSI */
7126		err = ice_ena_vsi(vsi, false);
7127		if (err) {
7128			dev_err(dev, "enable VSI failed, err %d, VSI index %d, type %s\n",
7129				err, vsi->idx, ice_vsi_type_str(type));
7130			return err;
7131		}
7132
7133		dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx,
7134			 ice_vsi_type_str(type));
7135	}
7136
7137	return 0;
7138}
7139
7140/**
7141 * ice_update_pf_netdev_link - Update PF netdev link status
7142 * @pf: pointer to the PF instance
7143 */
7144static void ice_update_pf_netdev_link(struct ice_pf *pf)
7145{
7146	bool link_up;
7147	int i;
7148
7149	ice_for_each_vsi(pf, i) {
7150		struct ice_vsi *vsi = pf->vsi[i];
7151
7152		if (!vsi || vsi->type != ICE_VSI_PF)
7153			return;
7154
7155		ice_get_link_status(pf->vsi[i]->port_info, &link_up);
7156		if (link_up) {
7157			netif_carrier_on(pf->vsi[i]->netdev);
7158			netif_tx_wake_all_queues(pf->vsi[i]->netdev);
7159		} else {
7160			netif_carrier_off(pf->vsi[i]->netdev);
7161			netif_tx_stop_all_queues(pf->vsi[i]->netdev);
7162		}
7163	}
7164}
7165
7166/**
7167 * ice_rebuild - rebuild after reset
7168 * @pf: PF to rebuild
7169 * @reset_type: type of reset
7170 *
7171 * Do not rebuild VF VSI in this flow because that is already handled via
7172 * ice_reset_all_vfs(). This is because requirements for resetting a VF after a
7173 * PFR/CORER/GLOBER/etc. are different than the normal flow. Also, we don't want
7174 * to reset/rebuild all the VF VSI twice.
7175 */
7176static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type)
7177{
7178	struct device *dev = ice_pf_to_dev(pf);
7179	struct ice_hw *hw = &pf->hw;
7180	bool dvm;
7181	int err;
7182
7183	if (test_bit(ICE_DOWN, pf->state))
7184		goto clear_recovery;
7185
7186	dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type);
7187
7188#define ICE_EMP_RESET_SLEEP_MS 5000
7189	if (reset_type == ICE_RESET_EMPR) {
7190		/* If an EMP reset has occurred, any previously pending flash
7191		 * update will have completed. We no longer know whether or
7192		 * not the NVM update EMP reset is restricted.
7193		 */
7194		pf->fw_emp_reset_disabled = false;
7195
7196		msleep(ICE_EMP_RESET_SLEEP_MS);
7197	}
7198
7199	err = ice_init_all_ctrlq(hw);
7200	if (err) {
7201		dev_err(dev, "control queues init failed %d\n", err);
7202		goto err_init_ctrlq;
7203	}
7204
7205	/* if DDP was previously loaded successfully */
7206	if (!ice_is_safe_mode(pf)) {
7207		/* reload the SW DB of filter tables */
7208		if (reset_type == ICE_RESET_PFR)
7209			ice_fill_blk_tbls(hw);
7210		else
7211			/* Reload DDP Package after CORER/GLOBR reset */
7212			ice_load_pkg(NULL, pf);
7213	}
7214
7215	err = ice_clear_pf_cfg(hw);
7216	if (err) {
7217		dev_err(dev, "clear PF configuration failed %d\n", err);
7218		goto err_init_ctrlq;
7219	}
7220
7221	ice_clear_pxe_mode(hw);
7222
7223	err = ice_init_nvm(hw);
7224	if (err) {
7225		dev_err(dev, "ice_init_nvm failed %d\n", err);
7226		goto err_init_ctrlq;
7227	}
7228
7229	err = ice_get_caps(hw);
7230	if (err) {
7231		dev_err(dev, "ice_get_caps failed %d\n", err);
7232		goto err_init_ctrlq;
7233	}
7234
7235	err = ice_aq_set_mac_cfg(hw, ICE_AQ_SET_MAC_FRAME_SIZE_MAX, NULL);
7236	if (err) {
7237		dev_err(dev, "set_mac_cfg failed %d\n", err);
7238		goto err_init_ctrlq;
7239	}
7240
7241	dvm = ice_is_dvm_ena(hw);
7242
7243	err = ice_aq_set_port_params(pf->hw.port_info, dvm, NULL);
7244	if (err)
7245		goto err_init_ctrlq;
7246
7247	err = ice_sched_init_port(hw->port_info);
7248	if (err)
7249		goto err_sched_init_port;
7250
7251	/* start misc vector */
7252	err = ice_req_irq_msix_misc(pf);
7253	if (err) {
7254		dev_err(dev, "misc vector setup failed: %d\n", err);
7255		goto err_sched_init_port;
7256	}
7257
7258	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7259		wr32(hw, PFQF_FD_ENA, PFQF_FD_ENA_FD_ENA_M);
7260		if (!rd32(hw, PFQF_FD_SIZE)) {
7261			u16 unused, guar, b_effort;
7262
7263			guar = hw->func_caps.fd_fltr_guar;
7264			b_effort = hw->func_caps.fd_fltr_best_effort;
7265
7266			/* force guaranteed filter pool for PF */
7267			ice_alloc_fd_guar_item(hw, &unused, guar);
7268			/* force shared filter pool for PF */
7269			ice_alloc_fd_shrd_item(hw, &unused, b_effort);
7270		}
7271	}
7272
7273	if (test_bit(ICE_FLAG_DCB_ENA, pf->flags))
7274		ice_dcb_rebuild(pf);
7275
7276	/* If the PF previously had enabled PTP, PTP init needs to happen before
7277	 * the VSI rebuild. If not, this causes the PTP link status events to
7278	 * fail.
7279	 */
7280	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
7281		ice_ptp_reset(pf);
7282
7283	if (ice_is_feature_supported(pf, ICE_F_GNSS))
7284		ice_gnss_init(pf);
7285
7286	/* rebuild PF VSI */
7287	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF);
7288	if (err) {
7289		dev_err(dev, "PF VSI rebuild failed: %d\n", err);
7290		goto err_vsi_rebuild;
7291	}
7292
7293	/* configure PTP timestamping after VSI rebuild */
7294	if (test_bit(ICE_FLAG_PTP_SUPPORTED, pf->flags))
7295		ice_ptp_cfg_timestamp(pf, false);
7296
7297	err = ice_vsi_rebuild_by_type(pf, ICE_VSI_SWITCHDEV_CTRL);
7298	if (err) {
7299		dev_err(dev, "Switchdev CTRL VSI rebuild failed: %d\n", err);
7300		goto err_vsi_rebuild;
7301	}
7302
7303	if (reset_type == ICE_RESET_PFR) {
7304		err = ice_rebuild_channels(pf);
7305		if (err) {
7306			dev_err(dev, "failed to rebuild and replay ADQ VSIs, err %d\n",
7307				err);
7308			goto err_vsi_rebuild;
7309		}
7310	}
7311
7312	/* If Flow Director is active */
7313	if (test_bit(ICE_FLAG_FD_ENA, pf->flags)) {
7314		err = ice_vsi_rebuild_by_type(pf, ICE_VSI_CTRL);
7315		if (err) {
7316			dev_err(dev, "control VSI rebuild failed: %d\n", err);
7317			goto err_vsi_rebuild;
7318		}
7319
7320		/* replay HW Flow Director recipes */
7321		if (hw->fdir_prof)
7322			ice_fdir_replay_flows(hw);
7323
7324		/* replay Flow Director filters */
7325		ice_fdir_replay_fltrs(pf);
7326
7327		ice_rebuild_arfs(pf);
7328	}
7329
7330	ice_update_pf_netdev_link(pf);
7331
7332	/* tell the firmware we are up */
7333	err = ice_send_version(pf);
7334	if (err) {
7335		dev_err(dev, "Rebuild failed due to error sending driver version: %d\n",
7336			err);
7337		goto err_vsi_rebuild;
7338	}
7339
7340	ice_replay_post(hw);
7341
7342	/* if we get here, reset flow is successful */
7343	clear_bit(ICE_RESET_FAILED, pf->state);
7344
7345	ice_plug_aux_dev(pf);
 
 
 
 
 
7346	return;
7347
7348err_vsi_rebuild:
7349err_sched_init_port:
7350	ice_sched_cleanup_all(hw);
7351err_init_ctrlq:
7352	ice_shutdown_all_ctrlq(hw);
7353	set_bit(ICE_RESET_FAILED, pf->state);
7354clear_recovery:
7355	/* set this bit in PF state to control service task scheduling */
7356	set_bit(ICE_NEEDS_RESTART, pf->state);
7357	dev_err(dev, "Rebuild failed, unload and reload driver\n");
7358}
7359
7360/**
7361 * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP
7362 * @vsi: Pointer to VSI structure
7363 */
7364static int ice_max_xdp_frame_size(struct ice_vsi *vsi)
7365{
7366	if (PAGE_SIZE >= 8192 || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags))
7367		return ICE_RXBUF_2048 - XDP_PACKET_HEADROOM;
7368	else
7369		return ICE_RXBUF_3072;
7370}
7371
7372/**
7373 * ice_change_mtu - NDO callback to change the MTU
7374 * @netdev: network interface device structure
7375 * @new_mtu: new value for maximum frame size
7376 *
7377 * Returns 0 on success, negative on failure
7378 */
7379static int ice_change_mtu(struct net_device *netdev, int new_mtu)
7380{
7381	struct ice_netdev_priv *np = netdev_priv(netdev);
7382	struct ice_vsi *vsi = np->vsi;
7383	struct ice_pf *pf = vsi->back;
 
7384	u8 count = 0;
7385	int err = 0;
7386
7387	if (new_mtu == (int)netdev->mtu) {
7388		netdev_warn(netdev, "MTU is already %u\n", netdev->mtu);
7389		return 0;
7390	}
7391
7392	if (ice_is_xdp_ena_vsi(vsi)) {
 
7393		int frame_size = ice_max_xdp_frame_size(vsi);
7394
7395		if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) {
7396			netdev_err(netdev, "max MTU for XDP usage is %d\n",
7397				   frame_size - ICE_ETH_PKT_HDR_PAD);
7398			return -EINVAL;
7399		}
 
 
 
 
 
 
7400	}
7401
7402	/* if a reset is in progress, wait for some time for it to complete */
7403	do {
7404		if (ice_is_reset_in_progress(pf->state)) {
7405			count++;
7406			usleep_range(1000, 2000);
7407		} else {
7408			break;
7409		}
7410
7411	} while (count < 100);
7412
7413	if (count == 100) {
7414		netdev_err(netdev, "can't change MTU. Device is busy\n");
7415		return -EBUSY;
7416	}
7417
7418	netdev->mtu = (unsigned int)new_mtu;
7419
7420	/* if VSI is up, bring it down and then back up */
7421	if (!test_and_set_bit(ICE_VSI_DOWN, vsi->state)) {
7422		err = ice_down(vsi);
7423		if (err) {
7424			netdev_err(netdev, "change MTU if_down err %d\n", err);
7425			return err;
7426		}
7427
7428		err = ice_up(vsi);
7429		if (err) {
7430			netdev_err(netdev, "change MTU if_up err %d\n", err);
7431			return err;
7432		}
7433	}
7434
7435	netdev_dbg(netdev, "changed MTU to %d\n", new_mtu);
7436	set_bit(ICE_FLAG_MTU_CHANGED, pf->flags);
7437
7438	return err;
7439}
7440
7441/**
7442 * ice_eth_ioctl - Access the hwtstamp interface
7443 * @netdev: network interface device structure
7444 * @ifr: interface request data
7445 * @cmd: ioctl command
7446 */
7447static int ice_eth_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
7448{
7449	struct ice_netdev_priv *np = netdev_priv(netdev);
7450	struct ice_pf *pf = np->vsi->back;
7451
7452	switch (cmd) {
7453	case SIOCGHWTSTAMP:
7454		return ice_ptp_get_ts_config(pf, ifr);
7455	case SIOCSHWTSTAMP:
7456		return ice_ptp_set_ts_config(pf, ifr);
7457	default:
7458		return -EOPNOTSUPP;
7459	}
7460}
7461
7462/**
7463 * ice_aq_str - convert AQ err code to a string
7464 * @aq_err: the AQ error code to convert
7465 */
7466const char *ice_aq_str(enum ice_aq_err aq_err)
7467{
7468	switch (aq_err) {
7469	case ICE_AQ_RC_OK:
7470		return "OK";
7471	case ICE_AQ_RC_EPERM:
7472		return "ICE_AQ_RC_EPERM";
7473	case ICE_AQ_RC_ENOENT:
7474		return "ICE_AQ_RC_ENOENT";
7475	case ICE_AQ_RC_ENOMEM:
7476		return "ICE_AQ_RC_ENOMEM";
7477	case ICE_AQ_RC_EBUSY:
7478		return "ICE_AQ_RC_EBUSY";
7479	case ICE_AQ_RC_EEXIST:
7480		return "ICE_AQ_RC_EEXIST";
7481	case ICE_AQ_RC_EINVAL:
7482		return "ICE_AQ_RC_EINVAL";
7483	case ICE_AQ_RC_ENOSPC:
7484		return "ICE_AQ_RC_ENOSPC";
7485	case ICE_AQ_RC_ENOSYS:
7486		return "ICE_AQ_RC_ENOSYS";
7487	case ICE_AQ_RC_EMODE:
7488		return "ICE_AQ_RC_EMODE";
7489	case ICE_AQ_RC_ENOSEC:
7490		return "ICE_AQ_RC_ENOSEC";
7491	case ICE_AQ_RC_EBADSIG:
7492		return "ICE_AQ_RC_EBADSIG";
7493	case ICE_AQ_RC_ESVN:
7494		return "ICE_AQ_RC_ESVN";
7495	case ICE_AQ_RC_EBADMAN:
7496		return "ICE_AQ_RC_EBADMAN";
7497	case ICE_AQ_RC_EBADBUF:
7498		return "ICE_AQ_RC_EBADBUF";
7499	}
7500
7501	return "ICE_AQ_RC_UNKNOWN";
7502}
7503
7504/**
7505 * ice_set_rss_lut - Set RSS LUT
7506 * @vsi: Pointer to VSI structure
7507 * @lut: Lookup table
7508 * @lut_size: Lookup table size
7509 *
7510 * Returns 0 on success, negative on failure
7511 */
7512int ice_set_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7513{
7514	struct ice_aq_get_set_rss_lut_params params = {};
7515	struct ice_hw *hw = &vsi->back->hw;
7516	int status;
7517
7518	if (!lut)
7519		return -EINVAL;
7520
7521	params.vsi_handle = vsi->idx;
7522	params.lut_size = lut_size;
7523	params.lut_type = vsi->rss_lut_type;
7524	params.lut = lut;
7525
7526	status = ice_aq_set_rss_lut(hw, &params);
7527	if (status)
7528		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS lut, err %d aq_err %s\n",
7529			status, ice_aq_str(hw->adminq.sq_last_status));
7530
7531	return status;
7532}
7533
7534/**
7535 * ice_set_rss_key - Set RSS key
7536 * @vsi: Pointer to the VSI structure
7537 * @seed: RSS hash seed
7538 *
7539 * Returns 0 on success, negative on failure
7540 */
7541int ice_set_rss_key(struct ice_vsi *vsi, u8 *seed)
7542{
7543	struct ice_hw *hw = &vsi->back->hw;
7544	int status;
7545
7546	if (!seed)
7547		return -EINVAL;
7548
7549	status = ice_aq_set_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7550	if (status)
7551		dev_err(ice_pf_to_dev(vsi->back), "Cannot set RSS key, err %d aq_err %s\n",
7552			status, ice_aq_str(hw->adminq.sq_last_status));
7553
7554	return status;
7555}
7556
7557/**
7558 * ice_get_rss_lut - Get RSS LUT
7559 * @vsi: Pointer to VSI structure
7560 * @lut: Buffer to store the lookup table entries
7561 * @lut_size: Size of buffer to store the lookup table entries
7562 *
7563 * Returns 0 on success, negative on failure
7564 */
7565int ice_get_rss_lut(struct ice_vsi *vsi, u8 *lut, u16 lut_size)
7566{
7567	struct ice_aq_get_set_rss_lut_params params = {};
7568	struct ice_hw *hw = &vsi->back->hw;
7569	int status;
7570
7571	if (!lut)
7572		return -EINVAL;
7573
7574	params.vsi_handle = vsi->idx;
7575	params.lut_size = lut_size;
7576	params.lut_type = vsi->rss_lut_type;
7577	params.lut = lut;
7578
7579	status = ice_aq_get_rss_lut(hw, &params);
7580	if (status)
7581		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS lut, err %d aq_err %s\n",
7582			status, ice_aq_str(hw->adminq.sq_last_status));
7583
7584	return status;
7585}
7586
7587/**
7588 * ice_get_rss_key - Get RSS key
7589 * @vsi: Pointer to VSI structure
7590 * @seed: Buffer to store the key in
7591 *
7592 * Returns 0 on success, negative on failure
7593 */
7594int ice_get_rss_key(struct ice_vsi *vsi, u8 *seed)
7595{
7596	struct ice_hw *hw = &vsi->back->hw;
7597	int status;
7598
7599	if (!seed)
7600		return -EINVAL;
7601
7602	status = ice_aq_get_rss_key(hw, vsi->idx, (struct ice_aqc_get_set_rss_keys *)seed);
7603	if (status)
7604		dev_err(ice_pf_to_dev(vsi->back), "Cannot get RSS key, err %d aq_err %s\n",
7605			status, ice_aq_str(hw->adminq.sq_last_status));
7606
7607	return status;
7608}
7609
7610/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7611 * ice_bridge_getlink - Get the hardware bridge mode
7612 * @skb: skb buff
7613 * @pid: process ID
7614 * @seq: RTNL message seq
7615 * @dev: the netdev being configured
7616 * @filter_mask: filter mask passed in
7617 * @nlflags: netlink flags passed in
7618 *
7619 * Return the bridge mode (VEB/VEPA)
7620 */
7621static int
7622ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
7623		   struct net_device *dev, u32 filter_mask, int nlflags)
7624{
7625	struct ice_netdev_priv *np = netdev_priv(dev);
7626	struct ice_vsi *vsi = np->vsi;
7627	struct ice_pf *pf = vsi->back;
7628	u16 bmode;
7629
7630	bmode = pf->first_sw->bridge_mode;
7631
7632	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags,
7633				       filter_mask, NULL);
7634}
7635
7636/**
7637 * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA)
7638 * @vsi: Pointer to VSI structure
7639 * @bmode: Hardware bridge mode (VEB/VEPA)
7640 *
7641 * Returns 0 on success, negative on failure
7642 */
7643static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode)
7644{
7645	struct ice_aqc_vsi_props *vsi_props;
7646	struct ice_hw *hw = &vsi->back->hw;
7647	struct ice_vsi_ctx *ctxt;
7648	int ret;
7649
7650	vsi_props = &vsi->info;
7651
7652	ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
7653	if (!ctxt)
7654		return -ENOMEM;
7655
7656	ctxt->info = vsi->info;
7657
7658	if (bmode == BRIDGE_MODE_VEB)
7659		/* change from VEPA to VEB mode */
7660		ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7661	else
7662		/* change from VEB to VEPA mode */
7663		ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
7664	ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID);
7665
7666	ret = ice_update_vsi(hw, vsi->idx, ctxt, NULL);
7667	if (ret) {
7668		dev_err(ice_pf_to_dev(vsi->back), "update VSI for bridge mode failed, bmode = %d err %d aq_err %s\n",
7669			bmode, ret, ice_aq_str(hw->adminq.sq_last_status));
7670		goto out;
7671	}
7672	/* Update sw flags for book keeping */
7673	vsi_props->sw_flags = ctxt->info.sw_flags;
7674
7675out:
7676	kfree(ctxt);
7677	return ret;
7678}
7679
7680/**
7681 * ice_bridge_setlink - Set the hardware bridge mode
7682 * @dev: the netdev being configured
7683 * @nlh: RTNL message
7684 * @flags: bridge setlink flags
7685 * @extack: netlink extended ack
7686 *
7687 * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is
7688 * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if
7689 * not already set for all VSIs connected to this switch. And also update the
7690 * unicast switch filter rules for the corresponding switch of the netdev.
7691 */
7692static int
7693ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
7694		   u16 __always_unused flags,
7695		   struct netlink_ext_ack __always_unused *extack)
7696{
7697	struct ice_netdev_priv *np = netdev_priv(dev);
7698	struct ice_pf *pf = np->vsi->back;
7699	struct nlattr *attr, *br_spec;
7700	struct ice_hw *hw = &pf->hw;
7701	struct ice_sw *pf_sw;
7702	int rem, v, err = 0;
7703
7704	pf_sw = pf->first_sw;
7705	/* find the attribute in the netlink message */
7706	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
 
 
7707
7708	nla_for_each_nested(attr, br_spec, rem) {
7709		__u16 mode;
7710
7711		if (nla_type(attr) != IFLA_BRIDGE_MODE)
7712			continue;
7713		mode = nla_get_u16(attr);
7714		if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB)
7715			return -EINVAL;
7716		/* Continue  if bridge mode is not being flipped */
7717		if (mode == pf_sw->bridge_mode)
7718			continue;
7719		/* Iterates through the PF VSI list and update the loopback
7720		 * mode of the VSI
7721		 */
7722		ice_for_each_vsi(pf, v) {
7723			if (!pf->vsi[v])
7724				continue;
7725			err = ice_vsi_update_bridge_mode(pf->vsi[v], mode);
7726			if (err)
7727				return err;
7728		}
7729
7730		hw->evb_veb = (mode == BRIDGE_MODE_VEB);
7731		/* Update the unicast switch filter rules for the corresponding
7732		 * switch of the netdev
7733		 */
7734		err = ice_update_sw_rule_bridge_mode(hw);
7735		if (err) {
7736			netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %s\n",
7737				   mode, err,
7738				   ice_aq_str(hw->adminq.sq_last_status));
7739			/* revert hw->evb_veb */
7740			hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB);
7741			return err;
7742		}
7743
7744		pf_sw->bridge_mode = mode;
7745	}
7746
7747	return 0;
7748}
7749
7750/**
7751 * ice_tx_timeout - Respond to a Tx Hang
7752 * @netdev: network interface device structure
7753 * @txqueue: Tx queue
7754 */
7755static void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue)
7756{
7757	struct ice_netdev_priv *np = netdev_priv(netdev);
7758	struct ice_tx_ring *tx_ring = NULL;
7759	struct ice_vsi *vsi = np->vsi;
7760	struct ice_pf *pf = vsi->back;
7761	u32 i;
7762
7763	pf->tx_timeout_count++;
7764
7765	/* Check if PFC is enabled for the TC to which the queue belongs
7766	 * to. If yes then Tx timeout is not caused by a hung queue, no
7767	 * need to reset and rebuild
7768	 */
7769	if (ice_is_pfc_causing_hung_q(pf, txqueue)) {
7770		dev_info(ice_pf_to_dev(pf), "Fake Tx hang detected on queue %u, timeout caused by PFC storm\n",
7771			 txqueue);
7772		return;
7773	}
7774
7775	/* now that we have an index, find the tx_ring struct */
7776	ice_for_each_txq(vsi, i)
7777		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
7778			if (txqueue == vsi->tx_rings[i]->q_index) {
7779				tx_ring = vsi->tx_rings[i];
7780				break;
7781			}
7782
7783	/* Reset recovery level if enough time has elapsed after last timeout.
7784	 * Also ensure no new reset action happens before next timeout period.
7785	 */
7786	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20)))
7787		pf->tx_timeout_recovery_level = 1;
7788	else if (time_before(jiffies, (pf->tx_timeout_last_recovery +
7789				       netdev->watchdog_timeo)))
7790		return;
7791
7792	if (tx_ring) {
7793		struct ice_hw *hw = &pf->hw;
7794		u32 head, val = 0;
7795
7796		head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])) &
7797			QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S;
7798		/* Read interrupt register */
7799		val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx));
7800
7801		netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %u, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n",
7802			    vsi->vsi_num, txqueue, tx_ring->next_to_clean,
7803			    head, tx_ring->next_to_use, val);
7804	}
7805
7806	pf->tx_timeout_last_recovery = jiffies;
7807	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %u\n",
7808		    pf->tx_timeout_recovery_level, txqueue);
7809
7810	switch (pf->tx_timeout_recovery_level) {
7811	case 1:
7812		set_bit(ICE_PFR_REQ, pf->state);
7813		break;
7814	case 2:
7815		set_bit(ICE_CORER_REQ, pf->state);
7816		break;
7817	case 3:
7818		set_bit(ICE_GLOBR_REQ, pf->state);
7819		break;
7820	default:
7821		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n");
7822		set_bit(ICE_DOWN, pf->state);
7823		set_bit(ICE_VSI_NEEDS_RESTART, vsi->state);
7824		set_bit(ICE_SERVICE_DIS, pf->state);
7825		break;
7826	}
7827
7828	ice_service_task_schedule(pf);
7829	pf->tx_timeout_recovery_level++;
7830}
7831
7832/**
7833 * ice_setup_tc_cls_flower - flower classifier offloads
7834 * @np: net device to configure
7835 * @filter_dev: device on which filter is added
7836 * @cls_flower: offload data
7837 */
7838static int
7839ice_setup_tc_cls_flower(struct ice_netdev_priv *np,
7840			struct net_device *filter_dev,
7841			struct flow_cls_offload *cls_flower)
7842{
7843	struct ice_vsi *vsi = np->vsi;
7844
7845	if (cls_flower->common.chain_index)
7846		return -EOPNOTSUPP;
7847
7848	switch (cls_flower->command) {
7849	case FLOW_CLS_REPLACE:
7850		return ice_add_cls_flower(filter_dev, vsi, cls_flower);
7851	case FLOW_CLS_DESTROY:
7852		return ice_del_cls_flower(vsi, cls_flower);
7853	default:
7854		return -EINVAL;
7855	}
7856}
7857
7858/**
7859 * ice_setup_tc_block_cb - callback handler registered for TC block
7860 * @type: TC SETUP type
7861 * @type_data: TC flower offload data that contains user input
7862 * @cb_priv: netdev private data
7863 */
7864static int
7865ice_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
7866{
7867	struct ice_netdev_priv *np = cb_priv;
7868
7869	switch (type) {
7870	case TC_SETUP_CLSFLOWER:
7871		return ice_setup_tc_cls_flower(np, np->vsi->netdev,
7872					       type_data);
7873	default:
7874		return -EOPNOTSUPP;
7875	}
7876}
7877
7878/**
7879 * ice_validate_mqprio_qopt - Validate TCF input parameters
7880 * @vsi: Pointer to VSI
7881 * @mqprio_qopt: input parameters for mqprio queue configuration
7882 *
7883 * This function validates MQPRIO params, such as qcount (power of 2 wherever
7884 * needed), and make sure user doesn't specify qcount and BW rate limit
7885 * for TCs, which are more than "num_tc"
7886 */
7887static int
7888ice_validate_mqprio_qopt(struct ice_vsi *vsi,
7889			 struct tc_mqprio_qopt_offload *mqprio_qopt)
7890{
7891	u64 sum_max_rate = 0, sum_min_rate = 0;
7892	int non_power_of_2_qcount = 0;
7893	struct ice_pf *pf = vsi->back;
7894	int max_rss_q_cnt = 0;
 
7895	struct device *dev;
7896	int i, speed;
7897	u8 num_tc;
7898
7899	if (vsi->type != ICE_VSI_PF)
7900		return -EINVAL;
7901
7902	if (mqprio_qopt->qopt.offset[0] != 0 ||
7903	    mqprio_qopt->qopt.num_tc < 1 ||
7904	    mqprio_qopt->qopt.num_tc > ICE_CHNL_MAX_TC)
7905		return -EINVAL;
7906
7907	dev = ice_pf_to_dev(pf);
7908	vsi->ch_rss_size = 0;
7909	num_tc = mqprio_qopt->qopt.num_tc;
 
7910
7911	for (i = 0; num_tc; i++) {
7912		int qcount = mqprio_qopt->qopt.count[i];
7913		u64 max_rate, min_rate, rem;
7914
7915		if (!qcount)
7916			return -EINVAL;
7917
7918		if (is_power_of_2(qcount)) {
7919			if (non_power_of_2_qcount &&
7920			    qcount > non_power_of_2_qcount) {
7921				dev_err(dev, "qcount[%d] cannot be greater than non power of 2 qcount[%d]\n",
7922					qcount, non_power_of_2_qcount);
7923				return -EINVAL;
7924			}
7925			if (qcount > max_rss_q_cnt)
7926				max_rss_q_cnt = qcount;
7927		} else {
7928			if (non_power_of_2_qcount &&
7929			    qcount != non_power_of_2_qcount) {
7930				dev_err(dev, "Only one non power of 2 qcount allowed[%d,%d]\n",
7931					qcount, non_power_of_2_qcount);
7932				return -EINVAL;
7933			}
7934			if (qcount < max_rss_q_cnt) {
7935				dev_err(dev, "non power of 2 qcount[%d] cannot be less than other qcount[%d]\n",
7936					qcount, max_rss_q_cnt);
7937				return -EINVAL;
7938			}
7939			max_rss_q_cnt = qcount;
7940			non_power_of_2_qcount = qcount;
7941		}
7942
7943		/* TC command takes input in K/N/Gbps or K/M/Gbit etc but
7944		 * converts the bandwidth rate limit into Bytes/s when
7945		 * passing it down to the driver. So convert input bandwidth
7946		 * from Bytes/s to Kbps
7947		 */
7948		max_rate = mqprio_qopt->max_rate[i];
7949		max_rate = div_u64(max_rate, ICE_BW_KBPS_DIVISOR);
7950		sum_max_rate += max_rate;
7951
7952		/* min_rate is minimum guaranteed rate and it can't be zero */
7953		min_rate = mqprio_qopt->min_rate[i];
7954		min_rate = div_u64(min_rate, ICE_BW_KBPS_DIVISOR);
7955		sum_min_rate += min_rate;
7956
7957		if (min_rate && min_rate < ICE_MIN_BW_LIMIT) {
7958			dev_err(dev, "TC%d: min_rate(%llu Kbps) < %u Kbps\n", i,
7959				min_rate, ICE_MIN_BW_LIMIT);
7960			return -EINVAL;
7961		}
7962
 
 
 
 
 
 
7963		iter_div_u64_rem(min_rate, ICE_MIN_BW_LIMIT, &rem);
7964		if (rem) {
7965			dev_err(dev, "TC%d: Min Rate not multiple of %u Kbps",
7966				i, ICE_MIN_BW_LIMIT);
7967			return -EINVAL;
7968		}
7969
7970		iter_div_u64_rem(max_rate, ICE_MIN_BW_LIMIT, &rem);
7971		if (rem) {
7972			dev_err(dev, "TC%d: Max Rate not multiple of %u Kbps",
7973				i, ICE_MIN_BW_LIMIT);
7974			return -EINVAL;
7975		}
7976
7977		/* min_rate can't be more than max_rate, except when max_rate
7978		 * is zero (implies max_rate sought is max line rate). In such
7979		 * a case min_rate can be more than max.
7980		 */
7981		if (max_rate && min_rate > max_rate) {
7982			dev_err(dev, "min_rate %llu Kbps can't be more than max_rate %llu Kbps\n",
7983				min_rate, max_rate);
7984			return -EINVAL;
7985		}
7986
7987		if (i >= mqprio_qopt->qopt.num_tc - 1)
7988			break;
7989		if (mqprio_qopt->qopt.offset[i + 1] !=
7990		    (mqprio_qopt->qopt.offset[i] + qcount))
7991			return -EINVAL;
7992	}
7993	if (vsi->num_rxq <
7994	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7995		return -EINVAL;
7996	if (vsi->num_txq <
7997	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7998		return -EINVAL;
7999
8000	speed = ice_get_link_speed_kbps(vsi);
8001	if (sum_max_rate && sum_max_rate > (u64)speed) {
8002		dev_err(dev, "Invalid max Tx rate(%llu) Kbps > speed(%u) Kbps specified\n",
8003			sum_max_rate, speed);
8004		return -EINVAL;
8005	}
8006	if (sum_min_rate && sum_min_rate > (u64)speed) {
8007		dev_err(dev, "Invalid min Tx rate(%llu) Kbps > speed (%u) Kbps specified\n",
8008			sum_min_rate, speed);
8009		return -EINVAL;
8010	}
8011
8012	/* make sure vsi->ch_rss_size is set correctly based on TC's qcount */
8013	vsi->ch_rss_size = max_rss_q_cnt;
8014
8015	return 0;
8016}
8017
8018/**
8019 * ice_add_vsi_to_fdir - add a VSI to the flow director group for PF
8020 * @pf: ptr to PF device
8021 * @vsi: ptr to VSI
8022 */
8023static int ice_add_vsi_to_fdir(struct ice_pf *pf, struct ice_vsi *vsi)
8024{
8025	struct device *dev = ice_pf_to_dev(pf);
8026	bool added = false;
8027	struct ice_hw *hw;
8028	int flow;
8029
8030	if (!(vsi->num_gfltr || vsi->num_bfltr))
8031		return -EINVAL;
8032
8033	hw = &pf->hw;
8034	for (flow = 0; flow < ICE_FLTR_PTYPE_MAX; flow++) {
8035		struct ice_fd_hw_prof *prof;
8036		int tun, status;
8037		u64 entry_h;
8038
8039		if (!(hw->fdir_prof && hw->fdir_prof[flow] &&
8040		      hw->fdir_prof[flow]->cnt))
8041			continue;
8042
8043		for (tun = 0; tun < ICE_FD_HW_SEG_MAX; tun++) {
8044			enum ice_flow_priority prio;
8045			u64 prof_id;
8046
8047			/* add this VSI to FDir profile for this flow */
8048			prio = ICE_FLOW_PRIO_NORMAL;
8049			prof = hw->fdir_prof[flow];
8050			prof_id = flow + tun * ICE_FLTR_PTYPE_MAX;
8051			status = ice_flow_add_entry(hw, ICE_BLK_FD, prof_id,
8052						    prof->vsi_h[0], vsi->idx,
8053						    prio, prof->fdir_seg[tun],
8054						    &entry_h);
8055			if (status) {
8056				dev_err(dev, "channel VSI idx %d, not able to add to group %d\n",
8057					vsi->idx, flow);
8058				continue;
8059			}
8060
8061			prof->entry_h[prof->cnt][tun] = entry_h;
8062		}
8063
8064		/* store VSI for filter replay and delete */
8065		prof->vsi_h[prof->cnt] = vsi->idx;
8066		prof->cnt++;
8067
8068		added = true;
8069		dev_dbg(dev, "VSI idx %d added to fdir group %d\n", vsi->idx,
8070			flow);
8071	}
8072
8073	if (!added)
8074		dev_dbg(dev, "VSI idx %d not added to fdir groups\n", vsi->idx);
8075
8076	return 0;
8077}
8078
8079/**
8080 * ice_add_channel - add a channel by adding VSI
8081 * @pf: ptr to PF device
8082 * @sw_id: underlying HW switching element ID
8083 * @ch: ptr to channel structure
8084 *
8085 * Add a channel (VSI) using add_vsi and queue_map
8086 */
8087static int ice_add_channel(struct ice_pf *pf, u16 sw_id, struct ice_channel *ch)
8088{
8089	struct device *dev = ice_pf_to_dev(pf);
8090	struct ice_vsi *vsi;
8091
8092	if (ch->type != ICE_VSI_CHNL) {
8093		dev_err(dev, "add new VSI failed, ch->type %d\n", ch->type);
8094		return -EINVAL;
8095	}
8096
8097	vsi = ice_chnl_vsi_setup(pf, pf->hw.port_info, ch);
8098	if (!vsi || vsi->type != ICE_VSI_CHNL) {
8099		dev_err(dev, "create chnl VSI failure\n");
8100		return -EINVAL;
8101	}
8102
8103	ice_add_vsi_to_fdir(pf, vsi);
8104
8105	ch->sw_id = sw_id;
8106	ch->vsi_num = vsi->vsi_num;
8107	ch->info.mapping_flags = vsi->info.mapping_flags;
8108	ch->ch_vsi = vsi;
8109	/* set the back pointer of channel for newly created VSI */
8110	vsi->ch = ch;
8111
8112	memcpy(&ch->info.q_mapping, &vsi->info.q_mapping,
8113	       sizeof(vsi->info.q_mapping));
8114	memcpy(&ch->info.tc_mapping, vsi->info.tc_mapping,
8115	       sizeof(vsi->info.tc_mapping));
8116
8117	return 0;
8118}
8119
8120/**
8121 * ice_chnl_cfg_res
8122 * @vsi: the VSI being setup
8123 * @ch: ptr to channel structure
8124 *
8125 * Configure channel specific resources such as rings, vector.
8126 */
8127static void ice_chnl_cfg_res(struct ice_vsi *vsi, struct ice_channel *ch)
8128{
8129	int i;
8130
8131	for (i = 0; i < ch->num_txq; i++) {
8132		struct ice_q_vector *tx_q_vector, *rx_q_vector;
8133		struct ice_ring_container *rc;
8134		struct ice_tx_ring *tx_ring;
8135		struct ice_rx_ring *rx_ring;
8136
8137		tx_ring = vsi->tx_rings[ch->base_q + i];
8138		rx_ring = vsi->rx_rings[ch->base_q + i];
8139		if (!tx_ring || !rx_ring)
8140			continue;
8141
8142		/* setup ring being channel enabled */
8143		tx_ring->ch = ch;
8144		rx_ring->ch = ch;
8145
8146		/* following code block sets up vector specific attributes */
8147		tx_q_vector = tx_ring->q_vector;
8148		rx_q_vector = rx_ring->q_vector;
8149		if (!tx_q_vector && !rx_q_vector)
8150			continue;
8151
8152		if (tx_q_vector) {
8153			tx_q_vector->ch = ch;
8154			/* setup Tx and Rx ITR setting if DIM is off */
8155			rc = &tx_q_vector->tx;
8156			if (!ITR_IS_DYNAMIC(rc))
8157				ice_write_itr(rc, rc->itr_setting);
8158		}
8159		if (rx_q_vector) {
8160			rx_q_vector->ch = ch;
8161			/* setup Tx and Rx ITR setting if DIM is off */
8162			rc = &rx_q_vector->rx;
8163			if (!ITR_IS_DYNAMIC(rc))
8164				ice_write_itr(rc, rc->itr_setting);
8165		}
8166	}
8167
8168	/* it is safe to assume that, if channel has non-zero num_t[r]xq, then
8169	 * GLINT_ITR register would have written to perform in-context
8170	 * update, hence perform flush
8171	 */
8172	if (ch->num_txq || ch->num_rxq)
8173		ice_flush(&vsi->back->hw);
8174}
8175
8176/**
8177 * ice_cfg_chnl_all_res - configure channel resources
8178 * @vsi: pte to main_vsi
8179 * @ch: ptr to channel structure
8180 *
8181 * This function configures channel specific resources such as flow-director
8182 * counter index, and other resources such as queues, vectors, ITR settings
8183 */
8184static void
8185ice_cfg_chnl_all_res(struct ice_vsi *vsi, struct ice_channel *ch)
8186{
8187	/* configure channel (aka ADQ) resources such as queues, vectors,
8188	 * ITR settings for channel specific vectors and anything else
8189	 */
8190	ice_chnl_cfg_res(vsi, ch);
8191}
8192
8193/**
8194 * ice_setup_hw_channel - setup new channel
8195 * @pf: ptr to PF device
8196 * @vsi: the VSI being setup
8197 * @ch: ptr to channel structure
8198 * @sw_id: underlying HW switching element ID
8199 * @type: type of channel to be created (VMDq2/VF)
8200 *
8201 * Setup new channel (VSI) based on specified type (VMDq2/VF)
8202 * and configures Tx rings accordingly
8203 */
8204static int
8205ice_setup_hw_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8206		     struct ice_channel *ch, u16 sw_id, u8 type)
8207{
8208	struct device *dev = ice_pf_to_dev(pf);
8209	int ret;
8210
8211	ch->base_q = vsi->next_base_q;
8212	ch->type = type;
8213
8214	ret = ice_add_channel(pf, sw_id, ch);
8215	if (ret) {
8216		dev_err(dev, "failed to add_channel using sw_id %u\n", sw_id);
8217		return ret;
8218	}
8219
8220	/* configure/setup ADQ specific resources */
8221	ice_cfg_chnl_all_res(vsi, ch);
8222
8223	/* make sure to update the next_base_q so that subsequent channel's
8224	 * (aka ADQ) VSI queue map is correct
8225	 */
8226	vsi->next_base_q = vsi->next_base_q + ch->num_rxq;
8227	dev_dbg(dev, "added channel: vsi_num %u, num_rxq %u\n", ch->vsi_num,
8228		ch->num_rxq);
8229
8230	return 0;
8231}
8232
8233/**
8234 * ice_setup_channel - setup new channel using uplink element
8235 * @pf: ptr to PF device
8236 * @vsi: the VSI being setup
8237 * @ch: ptr to channel structure
8238 *
8239 * Setup new channel (VSI) based on specified type (VMDq2/VF)
8240 * and uplink switching element
8241 */
8242static bool
8243ice_setup_channel(struct ice_pf *pf, struct ice_vsi *vsi,
8244		  struct ice_channel *ch)
8245{
8246	struct device *dev = ice_pf_to_dev(pf);
8247	u16 sw_id;
8248	int ret;
8249
8250	if (vsi->type != ICE_VSI_PF) {
8251		dev_err(dev, "unsupported parent VSI type(%d)\n", vsi->type);
8252		return false;
8253	}
8254
8255	sw_id = pf->first_sw->sw_id;
8256
8257	/* create channel (VSI) */
8258	ret = ice_setup_hw_channel(pf, vsi, ch, sw_id, ICE_VSI_CHNL);
8259	if (ret) {
8260		dev_err(dev, "failed to setup hw_channel\n");
8261		return false;
8262	}
8263	dev_dbg(dev, "successfully created channel()\n");
8264
8265	return ch->ch_vsi ? true : false;
8266}
8267
8268/**
8269 * ice_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
8270 * @vsi: VSI to be configured
8271 * @max_tx_rate: max Tx rate in Kbps to be configured as maximum BW limit
8272 * @min_tx_rate: min Tx rate in Kbps to be configured as minimum BW limit
8273 */
8274static int
8275ice_set_bw_limit(struct ice_vsi *vsi, u64 max_tx_rate, u64 min_tx_rate)
8276{
8277	int err;
8278
8279	err = ice_set_min_bw_limit(vsi, min_tx_rate);
8280	if (err)
8281		return err;
8282
8283	return ice_set_max_bw_limit(vsi, max_tx_rate);
8284}
8285
8286/**
8287 * ice_create_q_channel - function to create channel
8288 * @vsi: VSI to be configured
8289 * @ch: ptr to channel (it contains channel specific params)
8290 *
8291 * This function creates channel (VSI) using num_queues specified by user,
8292 * reconfigs RSS if needed.
8293 */
8294static int ice_create_q_channel(struct ice_vsi *vsi, struct ice_channel *ch)
8295{
8296	struct ice_pf *pf = vsi->back;
8297	struct device *dev;
8298
8299	if (!ch)
8300		return -EINVAL;
8301
8302	dev = ice_pf_to_dev(pf);
8303	if (!ch->num_txq || !ch->num_rxq) {
8304		dev_err(dev, "Invalid num_queues requested: %d\n", ch->num_rxq);
8305		return -EINVAL;
8306	}
8307
8308	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_txq) {
8309		dev_err(dev, "cnt_q_avail (%u) less than num_queues %d\n",
8310			vsi->cnt_q_avail, ch->num_txq);
8311		return -EINVAL;
8312	}
8313
8314	if (!ice_setup_channel(pf, vsi, ch)) {
8315		dev_info(dev, "Failed to setup channel\n");
8316		return -EINVAL;
8317	}
8318	/* configure BW rate limit */
8319	if (ch->ch_vsi && (ch->max_tx_rate || ch->min_tx_rate)) {
8320		int ret;
8321
8322		ret = ice_set_bw_limit(ch->ch_vsi, ch->max_tx_rate,
8323				       ch->min_tx_rate);
8324		if (ret)
8325			dev_err(dev, "failed to set Tx rate of %llu Kbps for VSI(%u)\n",
8326				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8327		else
8328			dev_dbg(dev, "set Tx rate of %llu Kbps for VSI(%u)\n",
8329				ch->max_tx_rate, ch->ch_vsi->vsi_num);
8330	}
8331
8332	vsi->cnt_q_avail -= ch->num_txq;
8333
8334	return 0;
8335}
8336
8337/**
8338 * ice_rem_all_chnl_fltrs - removes all channel filters
8339 * @pf: ptr to PF, TC-flower based filter are tracked at PF level
8340 *
8341 * Remove all advanced switch filters only if they are channel specific
8342 * tc-flower based filter
8343 */
8344static void ice_rem_all_chnl_fltrs(struct ice_pf *pf)
8345{
8346	struct ice_tc_flower_fltr *fltr;
8347	struct hlist_node *node;
8348
8349	/* to remove all channel filters, iterate an ordered list of filters */
8350	hlist_for_each_entry_safe(fltr, node,
8351				  &pf->tc_flower_fltr_list,
8352				  tc_flower_node) {
8353		struct ice_rule_query_data rule;
8354		int status;
8355
8356		/* for now process only channel specific filters */
8357		if (!ice_is_chnl_fltr(fltr))
8358			continue;
8359
8360		rule.rid = fltr->rid;
8361		rule.rule_id = fltr->rule_id;
8362		rule.vsi_handle = fltr->dest_vsi_handle;
8363		status = ice_rem_adv_rule_by_id(&pf->hw, &rule);
8364		if (status) {
8365			if (status == -ENOENT)
8366				dev_dbg(ice_pf_to_dev(pf), "TC flower filter (rule_id %u) does not exist\n",
8367					rule.rule_id);
8368			else
8369				dev_err(ice_pf_to_dev(pf), "failed to delete TC flower filter, status %d\n",
8370					status);
8371		} else if (fltr->dest_vsi) {
8372			/* update advanced switch filter count */
8373			if (fltr->dest_vsi->type == ICE_VSI_CHNL) {
8374				u32 flags = fltr->flags;
8375
8376				fltr->dest_vsi->num_chnl_fltr--;
8377				if (flags & (ICE_TC_FLWR_FIELD_DST_MAC |
8378					     ICE_TC_FLWR_FIELD_ENC_DST_MAC))
8379					pf->num_dmac_chnl_fltrs--;
8380			}
8381		}
8382
8383		hlist_del(&fltr->tc_flower_node);
8384		kfree(fltr);
8385	}
8386}
8387
8388/**
8389 * ice_remove_q_channels - Remove queue channels for the TCs
8390 * @vsi: VSI to be configured
8391 * @rem_fltr: delete advanced switch filter or not
8392 *
8393 * Remove queue channels for the TCs
8394 */
8395static void ice_remove_q_channels(struct ice_vsi *vsi, bool rem_fltr)
8396{
8397	struct ice_channel *ch, *ch_tmp;
8398	struct ice_pf *pf = vsi->back;
8399	int i;
8400
8401	/* remove all tc-flower based filter if they are channel filters only */
8402	if (rem_fltr)
8403		ice_rem_all_chnl_fltrs(pf);
8404
8405	/* remove ntuple filters since queue configuration is being changed */
8406	if  (vsi->netdev->features & NETIF_F_NTUPLE) {
8407		struct ice_hw *hw = &pf->hw;
8408
8409		mutex_lock(&hw->fdir_fltr_lock);
8410		ice_fdir_del_all_fltrs(vsi);
8411		mutex_unlock(&hw->fdir_fltr_lock);
8412	}
8413
8414	/* perform cleanup for channels if they exist */
8415	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
8416		struct ice_vsi *ch_vsi;
8417
8418		list_del(&ch->list);
8419		ch_vsi = ch->ch_vsi;
8420		if (!ch_vsi) {
8421			kfree(ch);
8422			continue;
8423		}
8424
8425		/* Reset queue contexts */
8426		for (i = 0; i < ch->num_rxq; i++) {
8427			struct ice_tx_ring *tx_ring;
8428			struct ice_rx_ring *rx_ring;
8429
8430			tx_ring = vsi->tx_rings[ch->base_q + i];
8431			rx_ring = vsi->rx_rings[ch->base_q + i];
8432			if (tx_ring) {
8433				tx_ring->ch = NULL;
8434				if (tx_ring->q_vector)
8435					tx_ring->q_vector->ch = NULL;
8436			}
8437			if (rx_ring) {
8438				rx_ring->ch = NULL;
8439				if (rx_ring->q_vector)
8440					rx_ring->q_vector->ch = NULL;
8441			}
8442		}
8443
8444		/* Release FD resources for the channel VSI */
8445		ice_fdir_rem_adq_chnl(&pf->hw, ch->ch_vsi->idx);
8446
8447		/* clear the VSI from scheduler tree */
8448		ice_rm_vsi_lan_cfg(ch->ch_vsi->port_info, ch->ch_vsi->idx);
8449
8450		/* Delete VSI from FW */
8451		ice_vsi_delete(ch->ch_vsi);
8452
8453		/* Delete VSI from PF and HW VSI arrays */
8454		ice_vsi_clear(ch->ch_vsi);
8455
8456		/* free the channel */
8457		kfree(ch);
8458	}
8459
8460	/* clear the channel VSI map which is stored in main VSI */
8461	ice_for_each_chnl_tc(i)
8462		vsi->tc_map_vsi[i] = NULL;
8463
8464	/* reset main VSI's all TC information */
8465	vsi->all_enatc = 0;
8466	vsi->all_numtc = 0;
8467}
8468
8469/**
8470 * ice_rebuild_channels - rebuild channel
8471 * @pf: ptr to PF
8472 *
8473 * Recreate channel VSIs and replay filters
8474 */
8475static int ice_rebuild_channels(struct ice_pf *pf)
8476{
8477	struct device *dev = ice_pf_to_dev(pf);
8478	struct ice_vsi *main_vsi;
8479	bool rem_adv_fltr = true;
8480	struct ice_channel *ch;
8481	struct ice_vsi *vsi;
8482	int tc_idx = 1;
8483	int i, err;
8484
8485	main_vsi = ice_get_main_vsi(pf);
8486	if (!main_vsi)
8487		return 0;
8488
8489	if (!test_bit(ICE_FLAG_TC_MQPRIO, pf->flags) ||
8490	    main_vsi->old_numtc == 1)
8491		return 0; /* nothing to be done */
8492
8493	/* reconfigure main VSI based on old value of TC and cached values
8494	 * for MQPRIO opts
8495	 */
8496	err = ice_vsi_cfg_tc(main_vsi, main_vsi->old_ena_tc);
8497	if (err) {
8498		dev_err(dev, "failed configuring TC(ena_tc:0x%02x) for HW VSI=%u\n",
8499			main_vsi->old_ena_tc, main_vsi->vsi_num);
8500		return err;
8501	}
8502
8503	/* rebuild ADQ VSIs */
8504	ice_for_each_vsi(pf, i) {
8505		enum ice_vsi_type type;
8506
8507		vsi = pf->vsi[i];
8508		if (!vsi || vsi->type != ICE_VSI_CHNL)
8509			continue;
8510
8511		type = vsi->type;
8512
8513		/* rebuild ADQ VSI */
8514		err = ice_vsi_rebuild(vsi, true);
8515		if (err) {
8516			dev_err(dev, "VSI (type:%s) at index %d rebuild failed, err %d\n",
8517				ice_vsi_type_str(type), vsi->idx, err);
8518			goto cleanup;
8519		}
8520
8521		/* Re-map HW VSI number, using VSI handle that has been
8522		 * previously validated in ice_replay_vsi() call above
8523		 */
8524		vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx);
8525
8526		/* replay filters for the VSI */
8527		err = ice_replay_vsi(&pf->hw, vsi->idx);
8528		if (err) {
8529			dev_err(dev, "VSI (type:%s) replay failed, err %d, VSI index %d\n",
8530				ice_vsi_type_str(type), err, vsi->idx);
8531			rem_adv_fltr = false;
8532			goto cleanup;
8533		}
8534		dev_info(dev, "VSI (type:%s) at index %d rebuilt successfully\n",
8535			 ice_vsi_type_str(type), vsi->idx);
8536
8537		/* store ADQ VSI at correct TC index in main VSI's
8538		 * map of TC to VSI
8539		 */
8540		main_vsi->tc_map_vsi[tc_idx++] = vsi;
8541	}
8542
8543	/* ADQ VSI(s) has been rebuilt successfully, so setup
8544	 * channel for main VSI's Tx and Rx rings
8545	 */
8546	list_for_each_entry(ch, &main_vsi->ch_list, list) {
8547		struct ice_vsi *ch_vsi;
8548
8549		ch_vsi = ch->ch_vsi;
8550		if (!ch_vsi)
8551			continue;
8552
8553		/* reconfig channel resources */
8554		ice_cfg_chnl_all_res(main_vsi, ch);
8555
8556		/* replay BW rate limit if it is non-zero */
8557		if (!ch->max_tx_rate && !ch->min_tx_rate)
8558			continue;
8559
8560		err = ice_set_bw_limit(ch_vsi, ch->max_tx_rate,
8561				       ch->min_tx_rate);
8562		if (err)
8563			dev_err(dev, "failed (err:%d) to rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
8564				err, ch->max_tx_rate, ch->min_tx_rate,
8565				ch_vsi->vsi_num);
8566		else
8567			dev_dbg(dev, "successfully rebuild BW rate limit, max_tx_rate: %llu Kbps, min_tx_rate: %llu Kbps for VSI(%u)\n",
8568				ch->max_tx_rate, ch->min_tx_rate,
8569				ch_vsi->vsi_num);
8570	}
8571
8572	/* reconfig RSS for main VSI */
8573	if (main_vsi->ch_rss_size)
8574		ice_vsi_cfg_rss_lut_key(main_vsi);
8575
8576	return 0;
8577
8578cleanup:
8579	ice_remove_q_channels(main_vsi, rem_adv_fltr);
8580	return err;
8581}
8582
8583/**
8584 * ice_create_q_channels - Add queue channel for the given TCs
8585 * @vsi: VSI to be configured
8586 *
8587 * Configures queue channel mapping to the given TCs
8588 */
8589static int ice_create_q_channels(struct ice_vsi *vsi)
8590{
8591	struct ice_pf *pf = vsi->back;
8592	struct ice_channel *ch;
8593	int ret = 0, i;
8594
8595	ice_for_each_chnl_tc(i) {
8596		if (!(vsi->all_enatc & BIT(i)))
8597			continue;
8598
8599		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
8600		if (!ch) {
8601			ret = -ENOMEM;
8602			goto err_free;
8603		}
8604		INIT_LIST_HEAD(&ch->list);
8605		ch->num_rxq = vsi->mqprio_qopt.qopt.count[i];
8606		ch->num_txq = vsi->mqprio_qopt.qopt.count[i];
8607		ch->base_q = vsi->mqprio_qopt.qopt.offset[i];
8608		ch->max_tx_rate = vsi->mqprio_qopt.max_rate[i];
8609		ch->min_tx_rate = vsi->mqprio_qopt.min_rate[i];
8610
8611		/* convert to Kbits/s */
8612		if (ch->max_tx_rate)
8613			ch->max_tx_rate = div_u64(ch->max_tx_rate,
8614						  ICE_BW_KBPS_DIVISOR);
8615		if (ch->min_tx_rate)
8616			ch->min_tx_rate = div_u64(ch->min_tx_rate,
8617						  ICE_BW_KBPS_DIVISOR);
8618
8619		ret = ice_create_q_channel(vsi, ch);
8620		if (ret) {
8621			dev_err(ice_pf_to_dev(pf),
8622				"failed creating channel TC:%d\n", i);
8623			kfree(ch);
8624			goto err_free;
8625		}
8626		list_add_tail(&ch->list, &vsi->ch_list);
8627		vsi->tc_map_vsi[i] = ch->ch_vsi;
8628		dev_dbg(ice_pf_to_dev(pf),
8629			"successfully created channel: VSI %pK\n", ch->ch_vsi);
8630	}
8631	return 0;
8632
8633err_free:
8634	ice_remove_q_channels(vsi, false);
8635
8636	return ret;
8637}
8638
8639/**
8640 * ice_setup_tc_mqprio_qdisc - configure multiple traffic classes
8641 * @netdev: net device to configure
8642 * @type_data: TC offload data
8643 */
8644static int ice_setup_tc_mqprio_qdisc(struct net_device *netdev, void *type_data)
8645{
8646	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8647	struct ice_netdev_priv *np = netdev_priv(netdev);
8648	struct ice_vsi *vsi = np->vsi;
8649	struct ice_pf *pf = vsi->back;
8650	u16 mode, ena_tc_qdisc = 0;
8651	int cur_txq, cur_rxq;
8652	u8 hw = 0, num_tcf;
8653	struct device *dev;
8654	int ret, i;
8655
8656	dev = ice_pf_to_dev(pf);
8657	num_tcf = mqprio_qopt->qopt.num_tc;
8658	hw = mqprio_qopt->qopt.hw;
8659	mode = mqprio_qopt->mode;
8660	if (!hw) {
8661		clear_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8662		vsi->ch_rss_size = 0;
8663		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8664		goto config_tcf;
8665	}
8666
8667	/* Generate queue region map for number of TCF requested */
8668	for (i = 0; i < num_tcf; i++)
8669		ena_tc_qdisc |= BIT(i);
8670
8671	switch (mode) {
8672	case TC_MQPRIO_MODE_CHANNEL:
8673
8674		if (pf->hw.port_info->is_custom_tx_enabled) {
8675			dev_err(dev, "Custom Tx scheduler feature enabled, can't configure ADQ\n");
8676			return -EBUSY;
8677		}
8678		ice_tear_down_devlink_rate_tree(pf);
8679
8680		ret = ice_validate_mqprio_qopt(vsi, mqprio_qopt);
8681		if (ret) {
8682			netdev_err(netdev, "failed to validate_mqprio_qopt(), ret %d\n",
8683				   ret);
8684			return ret;
8685		}
8686		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8687		set_bit(ICE_FLAG_TC_MQPRIO, pf->flags);
8688		/* don't assume state of hw_tc_offload during driver load
8689		 * and set the flag for TC flower filter if hw_tc_offload
8690		 * already ON
8691		 */
8692		if (vsi->netdev->features & NETIF_F_HW_TC)
8693			set_bit(ICE_FLAG_CLS_FLOWER, pf->flags);
8694		break;
8695	default:
8696		return -EINVAL;
8697	}
8698
8699config_tcf:
8700
8701	/* Requesting same TCF configuration as already enabled */
8702	if (ena_tc_qdisc == vsi->tc_cfg.ena_tc &&
8703	    mode != TC_MQPRIO_MODE_CHANNEL)
8704		return 0;
8705
8706	/* Pause VSI queues */
8707	ice_dis_vsi(vsi, true);
8708
8709	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags))
8710		ice_remove_q_channels(vsi, true);
8711
8712	if (!hw && !test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
8713		vsi->req_txq = min_t(int, ice_get_avail_txq_count(pf),
8714				     num_online_cpus());
8715		vsi->req_rxq = min_t(int, ice_get_avail_rxq_count(pf),
8716				     num_online_cpus());
8717	} else {
8718		/* logic to rebuild VSI, same like ethtool -L */
8719		u16 offset = 0, qcount_tx = 0, qcount_rx = 0;
8720
8721		for (i = 0; i < num_tcf; i++) {
8722			if (!(ena_tc_qdisc & BIT(i)))
8723				continue;
8724
8725			offset = vsi->mqprio_qopt.qopt.offset[i];
8726			qcount_rx = vsi->mqprio_qopt.qopt.count[i];
8727			qcount_tx = vsi->mqprio_qopt.qopt.count[i];
8728		}
8729		vsi->req_txq = offset + qcount_tx;
8730		vsi->req_rxq = offset + qcount_rx;
8731
8732		/* store away original rss_size info, so that it gets reused
8733		 * form ice_vsi_rebuild during tc-qdisc delete stage - to
8734		 * determine, what should be the rss_sizefor main VSI
8735		 */
8736		vsi->orig_rss_size = vsi->rss_size;
8737	}
8738
8739	/* save current values of Tx and Rx queues before calling VSI rebuild
8740	 * for fallback option
8741	 */
8742	cur_txq = vsi->num_txq;
8743	cur_rxq = vsi->num_rxq;
8744
8745	/* proceed with rebuild main VSI using correct number of queues */
8746	ret = ice_vsi_rebuild(vsi, false);
8747	if (ret) {
8748		/* fallback to current number of queues */
8749		dev_info(dev, "Rebuild failed with new queues, try with current number of queues\n");
8750		vsi->req_txq = cur_txq;
8751		vsi->req_rxq = cur_rxq;
8752		clear_bit(ICE_RESET_FAILED, pf->state);
8753		if (ice_vsi_rebuild(vsi, false)) {
8754			dev_err(dev, "Rebuild of main VSI failed again\n");
8755			return ret;
8756		}
8757	}
8758
8759	vsi->all_numtc = num_tcf;
8760	vsi->all_enatc = ena_tc_qdisc;
8761	ret = ice_vsi_cfg_tc(vsi, ena_tc_qdisc);
8762	if (ret) {
8763		netdev_err(netdev, "failed configuring TC for VSI id=%d\n",
8764			   vsi->vsi_num);
8765		goto exit;
8766	}
8767
8768	if (test_bit(ICE_FLAG_TC_MQPRIO, pf->flags)) {
8769		u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
8770		u64 min_tx_rate = vsi->mqprio_qopt.min_rate[0];
8771
8772		/* set TC0 rate limit if specified */
8773		if (max_tx_rate || min_tx_rate) {
8774			/* convert to Kbits/s */
8775			if (max_tx_rate)
8776				max_tx_rate = div_u64(max_tx_rate, ICE_BW_KBPS_DIVISOR);
8777			if (min_tx_rate)
8778				min_tx_rate = div_u64(min_tx_rate, ICE_BW_KBPS_DIVISOR);
8779
8780			ret = ice_set_bw_limit(vsi, max_tx_rate, min_tx_rate);
8781			if (!ret) {
8782				dev_dbg(dev, "set Tx rate max %llu min %llu for VSI(%u)\n",
8783					max_tx_rate, min_tx_rate, vsi->vsi_num);
8784			} else {
8785				dev_err(dev, "failed to set Tx rate max %llu min %llu for VSI(%u)\n",
8786					max_tx_rate, min_tx_rate, vsi->vsi_num);
8787				goto exit;
8788			}
8789		}
8790		ret = ice_create_q_channels(vsi);
8791		if (ret) {
8792			netdev_err(netdev, "failed configuring queue channels\n");
8793			goto exit;
8794		} else {
8795			netdev_dbg(netdev, "successfully configured channels\n");
8796		}
8797	}
8798
8799	if (vsi->ch_rss_size)
8800		ice_vsi_cfg_rss_lut_key(vsi);
8801
8802exit:
8803	/* if error, reset the all_numtc and all_enatc */
8804	if (ret) {
8805		vsi->all_numtc = 0;
8806		vsi->all_enatc = 0;
8807	}
8808	/* resume VSI */
8809	ice_ena_vsi(vsi, true);
8810
8811	return ret;
8812}
8813
8814static LIST_HEAD(ice_block_cb_list);
8815
8816static int
8817ice_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8818	     void *type_data)
8819{
8820	struct ice_netdev_priv *np = netdev_priv(netdev);
8821	struct ice_pf *pf = np->vsi->back;
 
8822	int err;
8823
8824	switch (type) {
8825	case TC_SETUP_BLOCK:
8826		return flow_block_cb_setup_simple(type_data,
8827						  &ice_block_cb_list,
8828						  ice_setup_tc_block_cb,
8829						  np, np, true);
8830	case TC_SETUP_QDISC_MQPRIO:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8831		/* setup traffic classifier for receive side */
8832		mutex_lock(&pf->tc_mutex);
8833		err = ice_setup_tc_mqprio_qdisc(netdev, type_data);
8834		mutex_unlock(&pf->tc_mutex);
 
 
 
 
 
 
8835		return err;
8836	default:
8837		return -EOPNOTSUPP;
8838	}
8839	return -EOPNOTSUPP;
8840}
8841
8842static struct ice_indr_block_priv *
8843ice_indr_block_priv_lookup(struct ice_netdev_priv *np,
8844			   struct net_device *netdev)
8845{
8846	struct ice_indr_block_priv *cb_priv;
8847
8848	list_for_each_entry(cb_priv, &np->tc_indr_block_priv_list, list) {
8849		if (!cb_priv->netdev)
8850			return NULL;
8851		if (cb_priv->netdev == netdev)
8852			return cb_priv;
8853	}
8854	return NULL;
8855}
8856
8857static int
8858ice_indr_setup_block_cb(enum tc_setup_type type, void *type_data,
8859			void *indr_priv)
8860{
8861	struct ice_indr_block_priv *priv = indr_priv;
8862	struct ice_netdev_priv *np = priv->np;
8863
8864	switch (type) {
8865	case TC_SETUP_CLSFLOWER:
8866		return ice_setup_tc_cls_flower(np, priv->netdev,
8867					       (struct flow_cls_offload *)
8868					       type_data);
8869	default:
8870		return -EOPNOTSUPP;
8871	}
8872}
8873
8874static int
8875ice_indr_setup_tc_block(struct net_device *netdev, struct Qdisc *sch,
8876			struct ice_netdev_priv *np,
8877			struct flow_block_offload *f, void *data,
8878			void (*cleanup)(struct flow_block_cb *block_cb))
8879{
8880	struct ice_indr_block_priv *indr_priv;
8881	struct flow_block_cb *block_cb;
8882
8883	if (!ice_is_tunnel_supported(netdev) &&
8884	    !(is_vlan_dev(netdev) &&
8885	      vlan_dev_real_dev(netdev) == np->vsi->netdev))
8886		return -EOPNOTSUPP;
8887
8888	if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
8889		return -EOPNOTSUPP;
8890
8891	switch (f->command) {
8892	case FLOW_BLOCK_BIND:
8893		indr_priv = ice_indr_block_priv_lookup(np, netdev);
8894		if (indr_priv)
8895			return -EEXIST;
8896
8897		indr_priv = kzalloc(sizeof(*indr_priv), GFP_KERNEL);
8898		if (!indr_priv)
8899			return -ENOMEM;
8900
8901		indr_priv->netdev = netdev;
8902		indr_priv->np = np;
8903		list_add(&indr_priv->list, &np->tc_indr_block_priv_list);
8904
8905		block_cb =
8906			flow_indr_block_cb_alloc(ice_indr_setup_block_cb,
8907						 indr_priv, indr_priv,
8908						 ice_rep_indr_tc_block_unbind,
8909						 f, netdev, sch, data, np,
8910						 cleanup);
8911
8912		if (IS_ERR(block_cb)) {
8913			list_del(&indr_priv->list);
8914			kfree(indr_priv);
8915			return PTR_ERR(block_cb);
8916		}
8917		flow_block_cb_add(block_cb, f);
8918		list_add_tail(&block_cb->driver_list, &ice_block_cb_list);
8919		break;
8920	case FLOW_BLOCK_UNBIND:
8921		indr_priv = ice_indr_block_priv_lookup(np, netdev);
8922		if (!indr_priv)
8923			return -ENOENT;
8924
8925		block_cb = flow_block_cb_lookup(f->block,
8926						ice_indr_setup_block_cb,
8927						indr_priv);
8928		if (!block_cb)
8929			return -ENOENT;
8930
8931		flow_indr_block_cb_remove(block_cb, f);
8932
8933		list_del(&block_cb->driver_list);
8934		break;
8935	default:
8936		return -EOPNOTSUPP;
8937	}
8938	return 0;
8939}
8940
8941static int
8942ice_indr_setup_tc_cb(struct net_device *netdev, struct Qdisc *sch,
8943		     void *cb_priv, enum tc_setup_type type, void *type_data,
8944		     void *data,
8945		     void (*cleanup)(struct flow_block_cb *block_cb))
8946{
8947	switch (type) {
8948	case TC_SETUP_BLOCK:
8949		return ice_indr_setup_tc_block(netdev, sch, cb_priv, type_data,
8950					       data, cleanup);
8951
8952	default:
8953		return -EOPNOTSUPP;
8954	}
8955}
8956
8957/**
8958 * ice_open - Called when a network interface becomes active
8959 * @netdev: network interface device structure
8960 *
8961 * The open entry point is called when a network interface is made
8962 * active by the system (IFF_UP). At this point all resources needed
8963 * for transmit and receive operations are allocated, the interrupt
8964 * handler is registered with the OS, the netdev watchdog is enabled,
8965 * and the stack is notified that the interface is ready.
8966 *
8967 * Returns 0 on success, negative value on failure
8968 */
8969int ice_open(struct net_device *netdev)
8970{
8971	struct ice_netdev_priv *np = netdev_priv(netdev);
8972	struct ice_pf *pf = np->vsi->back;
8973
8974	if (ice_is_reset_in_progress(pf->state)) {
8975		netdev_err(netdev, "can't open net device while reset is in progress");
8976		return -EBUSY;
8977	}
8978
8979	return ice_open_internal(netdev);
8980}
8981
8982/**
8983 * ice_open_internal - Called when a network interface becomes active
8984 * @netdev: network interface device structure
8985 *
8986 * Internal ice_open implementation. Should not be used directly except for ice_open and reset
8987 * handling routine
8988 *
8989 * Returns 0 on success, negative value on failure
8990 */
8991int ice_open_internal(struct net_device *netdev)
8992{
8993	struct ice_netdev_priv *np = netdev_priv(netdev);
8994	struct ice_vsi *vsi = np->vsi;
8995	struct ice_pf *pf = vsi->back;
8996	struct ice_port_info *pi;
8997	int err;
8998
8999	if (test_bit(ICE_NEEDS_RESTART, pf->state)) {
9000		netdev_err(netdev, "driver needs to be unloaded and reloaded\n");
9001		return -EIO;
9002	}
9003
9004	netif_carrier_off(netdev);
9005
9006	pi = vsi->port_info;
9007	err = ice_update_link_info(pi);
9008	if (err) {
9009		netdev_err(netdev, "Failed to get link info, error %d\n", err);
9010		return err;
9011	}
9012
9013	ice_check_link_cfg_err(pf, pi->phy.link_info.link_cfg_err);
9014
9015	/* Set PHY if there is media, otherwise, turn off PHY */
9016	if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) {
9017		clear_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9018		if (!test_bit(ICE_PHY_INIT_COMPLETE, pf->state)) {
9019			err = ice_init_phy_user_cfg(pi);
9020			if (err) {
9021				netdev_err(netdev, "Failed to initialize PHY settings, error %d\n",
9022					   err);
9023				return err;
9024			}
9025		}
9026
9027		err = ice_configure_phy(vsi);
9028		if (err) {
9029			netdev_err(netdev, "Failed to set physical link up, error %d\n",
9030				   err);
9031			return err;
9032		}
9033	} else {
9034		set_bit(ICE_FLAG_NO_MEDIA, pf->flags);
9035		ice_set_link(vsi, false);
9036	}
9037
9038	err = ice_vsi_open(vsi);
9039	if (err)
9040		netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
9041			   vsi->vsi_num, vsi->vsw->sw_id);
9042
9043	/* Update existing tunnels information */
9044	udp_tunnel_get_rx_info(netdev);
9045
9046	return err;
9047}
9048
9049/**
9050 * ice_stop - Disables a network interface
9051 * @netdev: network interface device structure
9052 *
9053 * The stop entry point is called when an interface is de-activated by the OS,
9054 * and the netdevice enters the DOWN state. The hardware is still under the
9055 * driver's control, but the netdev interface is disabled.
9056 *
9057 * Returns success only - not allowed to fail
9058 */
9059int ice_stop(struct net_device *netdev)
9060{
9061	struct ice_netdev_priv *np = netdev_priv(netdev);
9062	struct ice_vsi *vsi = np->vsi;
9063	struct ice_pf *pf = vsi->back;
9064
9065	if (ice_is_reset_in_progress(pf->state)) {
9066		netdev_err(netdev, "can't stop net device while reset is in progress");
9067		return -EBUSY;
9068	}
9069
9070	if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) {
9071		int link_err = ice_force_phys_link_state(vsi, false);
9072
9073		if (link_err) {
9074			netdev_err(vsi->netdev, "Failed to set physical link down, VSI %d error %d\n",
9075				   vsi->vsi_num, link_err);
 
 
 
 
 
 
9076			return -EIO;
9077		}
9078	}
9079
9080	ice_vsi_close(vsi);
9081
9082	return 0;
9083}
9084
9085/**
9086 * ice_features_check - Validate encapsulated packet conforms to limits
9087 * @skb: skb buffer
9088 * @netdev: This port's netdev
9089 * @features: Offload features that the stack believes apply
9090 */
9091static netdev_features_t
9092ice_features_check(struct sk_buff *skb,
9093		   struct net_device __always_unused *netdev,
9094		   netdev_features_t features)
9095{
9096	bool gso = skb_is_gso(skb);
9097	size_t len;
9098
9099	/* No point in doing any of this if neither checksum nor GSO are
9100	 * being requested for this frame. We can rule out both by just
9101	 * checking for CHECKSUM_PARTIAL
9102	 */
9103	if (skb->ip_summed != CHECKSUM_PARTIAL)
9104		return features;
9105
9106	/* We cannot support GSO if the MSS is going to be less than
9107	 * 64 bytes. If it is then we need to drop support for GSO.
9108	 */
9109	if (gso && (skb_shinfo(skb)->gso_size < ICE_TXD_CTX_MIN_MSS))
9110		features &= ~NETIF_F_GSO_MASK;
9111
9112	len = skb_network_offset(skb);
9113	if (len > ICE_TXD_MACLEN_MAX || len & 0x1)
9114		goto out_rm_features;
9115
9116	len = skb_network_header_len(skb);
9117	if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9118		goto out_rm_features;
9119
9120	if (skb->encapsulation) {
9121		/* this must work for VXLAN frames AND IPIP/SIT frames, and in
9122		 * the case of IPIP frames, the transport header pointer is
9123		 * after the inner header! So check to make sure that this
9124		 * is a GRE or UDP_TUNNEL frame before doing that math.
9125		 */
9126		if (gso && (skb_shinfo(skb)->gso_type &
9127			    (SKB_GSO_GRE | SKB_GSO_UDP_TUNNEL))) {
9128			len = skb_inner_network_header(skb) -
9129			      skb_transport_header(skb);
9130			if (len > ICE_TXD_L4LEN_MAX || len & 0x1)
9131				goto out_rm_features;
9132		}
9133
9134		len = skb_inner_network_header_len(skb);
9135		if (len > ICE_TXD_IPLEN_MAX || len & 0x1)
9136			goto out_rm_features;
9137	}
9138
9139	return features;
9140out_rm_features:
9141	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
9142}
9143
9144static const struct net_device_ops ice_netdev_safe_mode_ops = {
9145	.ndo_open = ice_open,
9146	.ndo_stop = ice_stop,
9147	.ndo_start_xmit = ice_start_xmit,
9148	.ndo_set_mac_address = ice_set_mac_address,
9149	.ndo_validate_addr = eth_validate_addr,
9150	.ndo_change_mtu = ice_change_mtu,
9151	.ndo_get_stats64 = ice_get_stats64,
9152	.ndo_tx_timeout = ice_tx_timeout,
9153	.ndo_bpf = ice_xdp_safe_mode,
9154};
9155
9156static const struct net_device_ops ice_netdev_ops = {
9157	.ndo_open = ice_open,
9158	.ndo_stop = ice_stop,
9159	.ndo_start_xmit = ice_start_xmit,
9160	.ndo_select_queue = ice_select_queue,
9161	.ndo_features_check = ice_features_check,
9162	.ndo_fix_features = ice_fix_features,
9163	.ndo_set_rx_mode = ice_set_rx_mode,
9164	.ndo_set_mac_address = ice_set_mac_address,
9165	.ndo_validate_addr = eth_validate_addr,
9166	.ndo_change_mtu = ice_change_mtu,
9167	.ndo_get_stats64 = ice_get_stats64,
9168	.ndo_set_tx_maxrate = ice_set_tx_maxrate,
9169	.ndo_eth_ioctl = ice_eth_ioctl,
9170	.ndo_set_vf_spoofchk = ice_set_vf_spoofchk,
9171	.ndo_set_vf_mac = ice_set_vf_mac,
9172	.ndo_get_vf_config = ice_get_vf_cfg,
9173	.ndo_set_vf_trust = ice_set_vf_trust,
9174	.ndo_set_vf_vlan = ice_set_vf_port_vlan,
9175	.ndo_set_vf_link_state = ice_set_vf_link_state,
9176	.ndo_get_vf_stats = ice_get_vf_stats,
9177	.ndo_set_vf_rate = ice_set_vf_bw,
9178	.ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
9179	.ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
9180	.ndo_setup_tc = ice_setup_tc,
9181	.ndo_set_features = ice_set_features,
9182	.ndo_bridge_getlink = ice_bridge_getlink,
9183	.ndo_bridge_setlink = ice_bridge_setlink,
9184	.ndo_fdb_add = ice_fdb_add,
9185	.ndo_fdb_del = ice_fdb_del,
9186#ifdef CONFIG_RFS_ACCEL
9187	.ndo_rx_flow_steer = ice_rx_flow_steer,
9188#endif
9189	.ndo_tx_timeout = ice_tx_timeout,
9190	.ndo_bpf = ice_xdp,
9191	.ndo_xdp_xmit = ice_xdp_xmit,
9192	.ndo_xsk_wakeup = ice_xsk_wakeup,
9193};