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