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