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