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