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