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