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 "ice.h" 9 #include "ice_base.h" 10 #include "ice_lib.h" 11 #include "ice_dcb_lib.h" 12 #include "ice_dcb_nl.h" 13 14 #define DRV_VERSION_MAJOR 0 15 #define DRV_VERSION_MINOR 8 16 #define DRV_VERSION_BUILD 2 17 18 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \ 19 __stringify(DRV_VERSION_MINOR) "." \ 20 __stringify(DRV_VERSION_BUILD) "-k" 21 #define DRV_SUMMARY "Intel(R) Ethernet Connection E800 Series Linux Driver" 22 const char ice_drv_ver[] = DRV_VERSION; 23 static const char ice_driver_string[] = DRV_SUMMARY; 24 static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation."; 25 26 /* DDP Package file located in firmware search paths (e.g. /lib/firmware/) */ 27 #define ICE_DDP_PKG_PATH "intel/ice/ddp/" 28 #define ICE_DDP_PKG_FILE ICE_DDP_PKG_PATH "ice.pkg" 29 30 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>"); 31 MODULE_DESCRIPTION(DRV_SUMMARY); 32 MODULE_LICENSE("GPL v2"); 33 MODULE_VERSION(DRV_VERSION); 34 MODULE_FIRMWARE(ICE_DDP_PKG_FILE); 35 36 static int debug = -1; 37 module_param(debug, int, 0644); 38 #ifndef CONFIG_DYNAMIC_DEBUG 39 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)"); 40 #else 41 MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)"); 42 #endif /* !CONFIG_DYNAMIC_DEBUG */ 43 44 static struct workqueue_struct *ice_wq; 45 static const struct net_device_ops ice_netdev_safe_mode_ops; 46 static const struct net_device_ops ice_netdev_ops; 47 static int ice_vsi_open(struct ice_vsi *vsi); 48 49 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type); 50 51 static void ice_vsi_release_all(struct ice_pf *pf); 52 53 /** 54 * ice_get_tx_pending - returns number of Tx descriptors not processed 55 * @ring: the ring of descriptors 56 */ 57 static u16 ice_get_tx_pending(struct ice_ring *ring) 58 { 59 u16 head, tail; 60 61 head = ring->next_to_clean; 62 tail = ring->next_to_use; 63 64 if (head != tail) 65 return (head < tail) ? 66 tail - head : (tail + ring->count - head); 67 return 0; 68 } 69 70 /** 71 * ice_check_for_hang_subtask - check for and recover hung queues 72 * @pf: pointer to PF struct 73 */ 74 static void ice_check_for_hang_subtask(struct ice_pf *pf) 75 { 76 struct ice_vsi *vsi = NULL; 77 struct ice_hw *hw; 78 unsigned int i; 79 int packets; 80 u32 v; 81 82 ice_for_each_vsi(pf, v) 83 if (pf->vsi[v] && pf->vsi[v]->type == ICE_VSI_PF) { 84 vsi = pf->vsi[v]; 85 break; 86 } 87 88 if (!vsi || test_bit(__ICE_DOWN, vsi->state)) 89 return; 90 91 if (!(vsi->netdev && netif_carrier_ok(vsi->netdev))) 92 return; 93 94 hw = &vsi->back->hw; 95 96 for (i = 0; i < vsi->num_txq; i++) { 97 struct ice_ring *tx_ring = vsi->tx_rings[i]; 98 99 if (tx_ring && tx_ring->desc) { 100 /* If packet counter has not changed the queue is 101 * likely stalled, so force an interrupt for this 102 * queue. 103 * 104 * prev_pkt would be negative if there was no 105 * pending work. 106 */ 107 packets = tx_ring->stats.pkts & INT_MAX; 108 if (tx_ring->tx_stats.prev_pkt == packets) { 109 /* Trigger sw interrupt to revive the queue */ 110 ice_trigger_sw_intr(hw, tx_ring->q_vector); 111 continue; 112 } 113 114 /* Memory barrier between read of packet count and call 115 * to ice_get_tx_pending() 116 */ 117 smp_rmb(); 118 tx_ring->tx_stats.prev_pkt = 119 ice_get_tx_pending(tx_ring) ? packets : -1; 120 } 121 } 122 } 123 124 /** 125 * ice_init_mac_fltr - Set initial MAC filters 126 * @pf: board private structure 127 * 128 * Set initial set of MAC filters for PF VSI; configure filters for permanent 129 * address and broadcast address. If an error is encountered, netdevice will be 130 * unregistered. 131 */ 132 static int ice_init_mac_fltr(struct ice_pf *pf) 133 { 134 enum ice_status status; 135 u8 broadcast[ETH_ALEN]; 136 struct ice_vsi *vsi; 137 138 vsi = ice_get_main_vsi(pf); 139 if (!vsi) 140 return -EINVAL; 141 142 /* To add a MAC filter, first add the MAC to a list and then 143 * pass the list to ice_add_mac. 144 */ 145 146 /* Add a unicast MAC filter so the VSI can get its packets */ 147 status = ice_vsi_cfg_mac_fltr(vsi, vsi->port_info->mac.perm_addr, true); 148 if (status) 149 goto unregister; 150 151 /* VSI needs to receive broadcast traffic, so add the broadcast 152 * MAC address to the list as well. 153 */ 154 eth_broadcast_addr(broadcast); 155 status = ice_vsi_cfg_mac_fltr(vsi, broadcast, true); 156 if (status) 157 goto unregister; 158 159 return 0; 160 unregister: 161 /* We aren't useful with no MAC filters, so unregister if we 162 * had an error 163 */ 164 if (status && vsi->netdev->reg_state == NETREG_REGISTERED) { 165 dev_err(ice_pf_to_dev(pf), 166 "Could not add MAC filters error %d. Unregistering device\n", 167 status); 168 unregister_netdev(vsi->netdev); 169 free_netdev(vsi->netdev); 170 vsi->netdev = NULL; 171 } 172 173 return -EIO; 174 } 175 176 /** 177 * ice_add_mac_to_sync_list - creates list of MAC addresses to be synced 178 * @netdev: the net device on which the sync is happening 179 * @addr: MAC address to sync 180 * 181 * This is a callback function which is called by the in kernel device sync 182 * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only 183 * populates the tmp_sync_list, which is later used by ice_add_mac to add the 184 * MAC filters from the hardware. 185 */ 186 static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr) 187 { 188 struct ice_netdev_priv *np = netdev_priv(netdev); 189 struct ice_vsi *vsi = np->vsi; 190 191 if (ice_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr)) 192 return -EINVAL; 193 194 return 0; 195 } 196 197 /** 198 * ice_add_mac_to_unsync_list - creates list of MAC addresses to be unsynced 199 * @netdev: the net device on which the unsync is happening 200 * @addr: MAC address to unsync 201 * 202 * This is a callback function which is called by the in kernel device unsync 203 * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only 204 * populates the tmp_unsync_list, which is later used by ice_remove_mac to 205 * delete the MAC filters from the hardware. 206 */ 207 static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr) 208 { 209 struct ice_netdev_priv *np = netdev_priv(netdev); 210 struct ice_vsi *vsi = np->vsi; 211 212 if (ice_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr)) 213 return -EINVAL; 214 215 return 0; 216 } 217 218 /** 219 * ice_vsi_fltr_changed - check if filter state changed 220 * @vsi: VSI to be checked 221 * 222 * returns true if filter state has changed, false otherwise. 223 */ 224 static bool ice_vsi_fltr_changed(struct ice_vsi *vsi) 225 { 226 return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) || 227 test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) || 228 test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags); 229 } 230 231 /** 232 * ice_cfg_promisc - Enable or disable promiscuous mode for a given PF 233 * @vsi: the VSI being configured 234 * @promisc_m: mask of promiscuous config bits 235 * @set_promisc: enable or disable promisc flag request 236 * 237 */ 238 static int ice_cfg_promisc(struct ice_vsi *vsi, u8 promisc_m, bool set_promisc) 239 { 240 struct ice_hw *hw = &vsi->back->hw; 241 enum ice_status status = 0; 242 243 if (vsi->type != ICE_VSI_PF) 244 return 0; 245 246 if (vsi->vlan_ena) { 247 status = ice_set_vlan_vsi_promisc(hw, vsi->idx, promisc_m, 248 set_promisc); 249 } else { 250 if (set_promisc) 251 status = ice_set_vsi_promisc(hw, vsi->idx, promisc_m, 252 0); 253 else 254 status = ice_clear_vsi_promisc(hw, vsi->idx, promisc_m, 255 0); 256 } 257 258 if (status) 259 return -EIO; 260 261 return 0; 262 } 263 264 /** 265 * ice_vsi_sync_fltr - Update the VSI filter list to the HW 266 * @vsi: ptr to the VSI 267 * 268 * Push any outstanding VSI filter changes through the AdminQ. 269 */ 270 static int ice_vsi_sync_fltr(struct ice_vsi *vsi) 271 { 272 struct device *dev = &vsi->back->pdev->dev; 273 struct net_device *netdev = vsi->netdev; 274 bool promisc_forced_on = false; 275 struct ice_pf *pf = vsi->back; 276 struct ice_hw *hw = &pf->hw; 277 enum ice_status status = 0; 278 u32 changed_flags = 0; 279 u8 promisc_m; 280 int err = 0; 281 282 if (!vsi->netdev) 283 return -EINVAL; 284 285 while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state)) 286 usleep_range(1000, 2000); 287 288 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags; 289 vsi->current_netdev_flags = vsi->netdev->flags; 290 291 INIT_LIST_HEAD(&vsi->tmp_sync_list); 292 INIT_LIST_HEAD(&vsi->tmp_unsync_list); 293 294 if (ice_vsi_fltr_changed(vsi)) { 295 clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags); 296 clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags); 297 clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags); 298 299 /* grab the netdev's addr_list_lock */ 300 netif_addr_lock_bh(netdev); 301 __dev_uc_sync(netdev, ice_add_mac_to_sync_list, 302 ice_add_mac_to_unsync_list); 303 __dev_mc_sync(netdev, ice_add_mac_to_sync_list, 304 ice_add_mac_to_unsync_list); 305 /* our temp lists are populated. release lock */ 306 netif_addr_unlock_bh(netdev); 307 } 308 309 /* Remove MAC addresses in the unsync list */ 310 status = ice_remove_mac(hw, &vsi->tmp_unsync_list); 311 ice_free_fltr_list(dev, &vsi->tmp_unsync_list); 312 if (status) { 313 netdev_err(netdev, "Failed to delete MAC filters\n"); 314 /* if we failed because of alloc failures, just bail */ 315 if (status == ICE_ERR_NO_MEMORY) { 316 err = -ENOMEM; 317 goto out; 318 } 319 } 320 321 /* Add MAC addresses in the sync list */ 322 status = ice_add_mac(hw, &vsi->tmp_sync_list); 323 ice_free_fltr_list(dev, &vsi->tmp_sync_list); 324 /* If filter is added successfully or already exists, do not go into 325 * 'if' condition and report it as error. Instead continue processing 326 * rest of the function. 327 */ 328 if (status && status != ICE_ERR_ALREADY_EXISTS) { 329 netdev_err(netdev, "Failed to add MAC filters\n"); 330 /* If there is no more space for new umac filters, VSI 331 * should go into promiscuous mode. There should be some 332 * space reserved for promiscuous filters. 333 */ 334 if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC && 335 !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC, 336 vsi->state)) { 337 promisc_forced_on = true; 338 netdev_warn(netdev, 339 "Reached MAC filter limit, forcing promisc mode on VSI %d\n", 340 vsi->vsi_num); 341 } else { 342 err = -EIO; 343 goto out; 344 } 345 } 346 /* check for changes in promiscuous modes */ 347 if (changed_flags & IFF_ALLMULTI) { 348 if (vsi->current_netdev_flags & IFF_ALLMULTI) { 349 if (vsi->vlan_ena) 350 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS; 351 else 352 promisc_m = ICE_MCAST_PROMISC_BITS; 353 354 err = ice_cfg_promisc(vsi, promisc_m, true); 355 if (err) { 356 netdev_err(netdev, "Error setting Multicast promiscuous mode on VSI %i\n", 357 vsi->vsi_num); 358 vsi->current_netdev_flags &= ~IFF_ALLMULTI; 359 goto out_promisc; 360 } 361 } else if (!(vsi->current_netdev_flags & IFF_ALLMULTI)) { 362 if (vsi->vlan_ena) 363 promisc_m = ICE_MCAST_VLAN_PROMISC_BITS; 364 else 365 promisc_m = ICE_MCAST_PROMISC_BITS; 366 367 err = ice_cfg_promisc(vsi, promisc_m, false); 368 if (err) { 369 netdev_err(netdev, "Error clearing Multicast promiscuous mode on VSI %i\n", 370 vsi->vsi_num); 371 vsi->current_netdev_flags |= IFF_ALLMULTI; 372 goto out_promisc; 373 } 374 } 375 } 376 377 if (((changed_flags & IFF_PROMISC) || promisc_forced_on) || 378 test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) { 379 clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags); 380 if (vsi->current_netdev_flags & IFF_PROMISC) { 381 /* Apply Rx filter rule to get traffic from wire */ 382 if (!ice_is_dflt_vsi_in_use(pf->first_sw)) { 383 err = ice_set_dflt_vsi(pf->first_sw, vsi); 384 if (err && err != -EEXIST) { 385 netdev_err(netdev, 386 "Error %d setting default VSI %i Rx rule\n", 387 err, vsi->vsi_num); 388 vsi->current_netdev_flags &= 389 ~IFF_PROMISC; 390 goto out_promisc; 391 } 392 } 393 } else { 394 /* Clear Rx filter to remove traffic from wire */ 395 if (ice_is_vsi_dflt_vsi(pf->first_sw, vsi)) { 396 err = ice_clear_dflt_vsi(pf->first_sw); 397 if (err) { 398 netdev_err(netdev, 399 "Error %d clearing default VSI %i Rx rule\n", 400 err, vsi->vsi_num); 401 vsi->current_netdev_flags |= 402 IFF_PROMISC; 403 goto out_promisc; 404 } 405 } 406 } 407 } 408 goto exit; 409 410 out_promisc: 411 set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags); 412 goto exit; 413 out: 414 /* if something went wrong then set the changed flag so we try again */ 415 set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags); 416 set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags); 417 exit: 418 clear_bit(__ICE_CFG_BUSY, vsi->state); 419 return err; 420 } 421 422 /** 423 * ice_sync_fltr_subtask - Sync the VSI filter list with HW 424 * @pf: board private structure 425 */ 426 static void ice_sync_fltr_subtask(struct ice_pf *pf) 427 { 428 int v; 429 430 if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags))) 431 return; 432 433 clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags); 434 435 ice_for_each_vsi(pf, v) 436 if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) && 437 ice_vsi_sync_fltr(pf->vsi[v])) { 438 /* come back and try again later */ 439 set_bit(ICE_FLAG_FLTR_SYNC, pf->flags); 440 break; 441 } 442 } 443 444 /** 445 * ice_pf_dis_all_vsi - Pause all VSIs on a PF 446 * @pf: the PF 447 * @locked: is the rtnl_lock already held 448 */ 449 static void ice_pf_dis_all_vsi(struct ice_pf *pf, bool locked) 450 { 451 int v; 452 453 ice_for_each_vsi(pf, v) 454 if (pf->vsi[v]) 455 ice_dis_vsi(pf->vsi[v], locked); 456 } 457 458 /** 459 * ice_prepare_for_reset - prep for the core to reset 460 * @pf: board private structure 461 * 462 * Inform or close all dependent features in prep for reset. 463 */ 464 static void 465 ice_prepare_for_reset(struct ice_pf *pf) 466 { 467 struct ice_hw *hw = &pf->hw; 468 int i; 469 470 /* already prepared for reset */ 471 if (test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) 472 return; 473 474 /* Notify VFs of impending reset */ 475 if (ice_check_sq_alive(hw, &hw->mailboxq)) 476 ice_vc_notify_reset(pf); 477 478 /* Disable VFs until reset is completed */ 479 ice_for_each_vf(pf, i) 480 ice_set_vf_state_qs_dis(&pf->vf[i]); 481 482 /* clear SW filtering DB */ 483 ice_clear_hw_tbls(hw); 484 /* disable the VSIs and their queues that are not already DOWN */ 485 ice_pf_dis_all_vsi(pf, false); 486 487 if (hw->port_info) 488 ice_sched_clear_port(hw->port_info); 489 490 ice_shutdown_all_ctrlq(hw); 491 492 set_bit(__ICE_PREPARED_FOR_RESET, pf->state); 493 } 494 495 /** 496 * ice_do_reset - Initiate one of many types of resets 497 * @pf: board private structure 498 * @reset_type: reset type requested 499 * before this function was called. 500 */ 501 static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type) 502 { 503 struct device *dev = ice_pf_to_dev(pf); 504 struct ice_hw *hw = &pf->hw; 505 506 dev_dbg(dev, "reset_type 0x%x requested\n", reset_type); 507 WARN_ON(in_interrupt()); 508 509 ice_prepare_for_reset(pf); 510 511 /* trigger the reset */ 512 if (ice_reset(hw, reset_type)) { 513 dev_err(dev, "reset %d failed\n", reset_type); 514 set_bit(__ICE_RESET_FAILED, pf->state); 515 clear_bit(__ICE_RESET_OICR_RECV, pf->state); 516 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state); 517 clear_bit(__ICE_PFR_REQ, pf->state); 518 clear_bit(__ICE_CORER_REQ, pf->state); 519 clear_bit(__ICE_GLOBR_REQ, pf->state); 520 return; 521 } 522 523 /* PFR is a bit of a special case because it doesn't result in an OICR 524 * interrupt. So for PFR, rebuild after the reset and clear the reset- 525 * associated state bits. 526 */ 527 if (reset_type == ICE_RESET_PFR) { 528 pf->pfr_count++; 529 ice_rebuild(pf, reset_type); 530 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state); 531 clear_bit(__ICE_PFR_REQ, pf->state); 532 ice_reset_all_vfs(pf, true); 533 } 534 } 535 536 /** 537 * ice_reset_subtask - Set up for resetting the device and driver 538 * @pf: board private structure 539 */ 540 static void ice_reset_subtask(struct ice_pf *pf) 541 { 542 enum ice_reset_req reset_type = ICE_RESET_INVAL; 543 544 /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an 545 * OICR interrupt. The OICR handler (ice_misc_intr) determines what type 546 * of reset is pending and sets bits in pf->state indicating the reset 547 * type and __ICE_RESET_OICR_RECV. So, if the latter bit is set 548 * prepare for pending reset if not already (for PF software-initiated 549 * global resets the software should already be prepared for it as 550 * indicated by __ICE_PREPARED_FOR_RESET; for global resets initiated 551 * by firmware or software on other PFs, that bit is not set so prepare 552 * for the reset now), poll for reset done, rebuild and return. 553 */ 554 if (test_bit(__ICE_RESET_OICR_RECV, pf->state)) { 555 /* Perform the largest reset requested */ 556 if (test_and_clear_bit(__ICE_CORER_RECV, pf->state)) 557 reset_type = ICE_RESET_CORER; 558 if (test_and_clear_bit(__ICE_GLOBR_RECV, pf->state)) 559 reset_type = ICE_RESET_GLOBR; 560 if (test_and_clear_bit(__ICE_EMPR_RECV, pf->state)) 561 reset_type = ICE_RESET_EMPR; 562 /* return if no valid reset type requested */ 563 if (reset_type == ICE_RESET_INVAL) 564 return; 565 ice_prepare_for_reset(pf); 566 567 /* make sure we are ready to rebuild */ 568 if (ice_check_reset(&pf->hw)) { 569 set_bit(__ICE_RESET_FAILED, pf->state); 570 } else { 571 /* done with reset. start rebuild */ 572 pf->hw.reset_ongoing = false; 573 ice_rebuild(pf, reset_type); 574 /* clear bit to resume normal operations, but 575 * ICE_NEEDS_RESTART bit is set in case rebuild failed 576 */ 577 clear_bit(__ICE_RESET_OICR_RECV, pf->state); 578 clear_bit(__ICE_PREPARED_FOR_RESET, pf->state); 579 clear_bit(__ICE_PFR_REQ, pf->state); 580 clear_bit(__ICE_CORER_REQ, pf->state); 581 clear_bit(__ICE_GLOBR_REQ, pf->state); 582 ice_reset_all_vfs(pf, true); 583 } 584 585 return; 586 } 587 588 /* No pending resets to finish processing. Check for new resets */ 589 if (test_bit(__ICE_PFR_REQ, pf->state)) 590 reset_type = ICE_RESET_PFR; 591 if (test_bit(__ICE_CORER_REQ, pf->state)) 592 reset_type = ICE_RESET_CORER; 593 if (test_bit(__ICE_GLOBR_REQ, pf->state)) 594 reset_type = ICE_RESET_GLOBR; 595 /* If no valid reset type requested just return */ 596 if (reset_type == ICE_RESET_INVAL) 597 return; 598 599 /* reset if not already down or busy */ 600 if (!test_bit(__ICE_DOWN, pf->state) && 601 !test_bit(__ICE_CFG_BUSY, pf->state)) { 602 ice_do_reset(pf, reset_type); 603 } 604 } 605 606 /** 607 * ice_print_topo_conflict - print topology conflict message 608 * @vsi: the VSI whose topology status is being checked 609 */ 610 static void ice_print_topo_conflict(struct ice_vsi *vsi) 611 { 612 switch (vsi->port_info->phy.link_info.topo_media_conflict) { 613 case ICE_AQ_LINK_TOPO_CONFLICT: 614 case ICE_AQ_LINK_MEDIA_CONFLICT: 615 case ICE_AQ_LINK_TOPO_UNREACH_PRT: 616 case ICE_AQ_LINK_TOPO_UNDRUTIL_PRT: 617 case ICE_AQ_LINK_TOPO_UNDRUTIL_MEDIA: 618 netdev_info(vsi->netdev, "Possible mis-configuration of the Ethernet port detected, please use the Intel(R) Ethernet Port Configuration Tool application to address the issue.\n"); 619 break; 620 case ICE_AQ_LINK_TOPO_UNSUPP_MEDIA: 621 netdev_info(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"); 622 break; 623 default: 624 break; 625 } 626 } 627 628 /** 629 * ice_print_link_msg - print link up or down message 630 * @vsi: the VSI whose link status is being queried 631 * @isup: boolean for if the link is now up or down 632 */ 633 void ice_print_link_msg(struct ice_vsi *vsi, bool isup) 634 { 635 struct ice_aqc_get_phy_caps_data *caps; 636 enum ice_status status; 637 const char *fec_req; 638 const char *speed; 639 const char *fec; 640 const char *fc; 641 const char *an; 642 643 if (!vsi) 644 return; 645 646 if (vsi->current_isup == isup) 647 return; 648 649 vsi->current_isup = isup; 650 651 if (!isup) { 652 netdev_info(vsi->netdev, "NIC Link is Down\n"); 653 return; 654 } 655 656 switch (vsi->port_info->phy.link_info.link_speed) { 657 case ICE_AQ_LINK_SPEED_100GB: 658 speed = "100 G"; 659 break; 660 case ICE_AQ_LINK_SPEED_50GB: 661 speed = "50 G"; 662 break; 663 case ICE_AQ_LINK_SPEED_40GB: 664 speed = "40 G"; 665 break; 666 case ICE_AQ_LINK_SPEED_25GB: 667 speed = "25 G"; 668 break; 669 case ICE_AQ_LINK_SPEED_20GB: 670 speed = "20 G"; 671 break; 672 case ICE_AQ_LINK_SPEED_10GB: 673 speed = "10 G"; 674 break; 675 case ICE_AQ_LINK_SPEED_5GB: 676 speed = "5 G"; 677 break; 678 case ICE_AQ_LINK_SPEED_2500MB: 679 speed = "2.5 G"; 680 break; 681 case ICE_AQ_LINK_SPEED_1000MB: 682 speed = "1 G"; 683 break; 684 case ICE_AQ_LINK_SPEED_100MB: 685 speed = "100 M"; 686 break; 687 default: 688 speed = "Unknown"; 689 break; 690 } 691 692 switch (vsi->port_info->fc.current_mode) { 693 case ICE_FC_FULL: 694 fc = "Rx/Tx"; 695 break; 696 case ICE_FC_TX_PAUSE: 697 fc = "Tx"; 698 break; 699 case ICE_FC_RX_PAUSE: 700 fc = "Rx"; 701 break; 702 case ICE_FC_NONE: 703 fc = "None"; 704 break; 705 default: 706 fc = "Unknown"; 707 break; 708 } 709 710 /* Get FEC mode based on negotiated link info */ 711 switch (vsi->port_info->phy.link_info.fec_info) { 712 case ICE_AQ_LINK_25G_RS_528_FEC_EN: 713 /* fall through */ 714 case ICE_AQ_LINK_25G_RS_544_FEC_EN: 715 fec = "RS-FEC"; 716 break; 717 case ICE_AQ_LINK_25G_KR_FEC_EN: 718 fec = "FC-FEC/BASE-R"; 719 break; 720 default: 721 fec = "NONE"; 722 break; 723 } 724 725 /* check if autoneg completed, might be false due to not supported */ 726 if (vsi->port_info->phy.link_info.an_info & ICE_AQ_AN_COMPLETED) 727 an = "True"; 728 else 729 an = "False"; 730 731 /* Get FEC mode requested based on PHY caps last SW configuration */ 732 caps = kzalloc(sizeof(*caps), GFP_KERNEL); 733 if (!caps) { 734 fec_req = "Unknown"; 735 goto done; 736 } 737 738 status = ice_aq_get_phy_caps(vsi->port_info, false, 739 ICE_AQC_REPORT_SW_CFG, caps, NULL); 740 if (status) 741 netdev_info(vsi->netdev, "Get phy capability failed.\n"); 742 743 if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ || 744 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ) 745 fec_req = "RS-FEC"; 746 else if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ || 747 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ) 748 fec_req = "FC-FEC/BASE-R"; 749 else 750 fec_req = "NONE"; 751 752 kfree(caps); 753 754 done: 755 netdev_info(vsi->netdev, "NIC Link is up %sbps, Requested FEC: %s, FEC: %s, Autoneg: %s, Flow Control: %s\n", 756 speed, fec_req, fec, an, fc); 757 ice_print_topo_conflict(vsi); 758 } 759 760 /** 761 * ice_vsi_link_event - update the VSI's netdev 762 * @vsi: the VSI on which the link event occurred 763 * @link_up: whether or not the VSI needs to be set up or down 764 */ 765 static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up) 766 { 767 if (!vsi) 768 return; 769 770 if (test_bit(__ICE_DOWN, vsi->state) || !vsi->netdev) 771 return; 772 773 if (vsi->type == ICE_VSI_PF) { 774 if (link_up == netif_carrier_ok(vsi->netdev)) 775 return; 776 777 if (link_up) { 778 netif_carrier_on(vsi->netdev); 779 netif_tx_wake_all_queues(vsi->netdev); 780 } else { 781 netif_carrier_off(vsi->netdev); 782 netif_tx_stop_all_queues(vsi->netdev); 783 } 784 } 785 } 786 787 /** 788 * ice_link_event - process the link event 789 * @pf: PF that the link event is associated with 790 * @pi: port_info for the port that the link event is associated with 791 * @link_up: true if the physical link is up and false if it is down 792 * @link_speed: current link speed received from the link event 793 * 794 * Returns 0 on success and negative on failure 795 */ 796 static int 797 ice_link_event(struct ice_pf *pf, struct ice_port_info *pi, bool link_up, 798 u16 link_speed) 799 { 800 struct device *dev = ice_pf_to_dev(pf); 801 struct ice_phy_info *phy_info; 802 struct ice_vsi *vsi; 803 u16 old_link_speed; 804 bool old_link; 805 int result; 806 807 phy_info = &pi->phy; 808 phy_info->link_info_old = phy_info->link_info; 809 810 old_link = !!(phy_info->link_info_old.link_info & ICE_AQ_LINK_UP); 811 old_link_speed = phy_info->link_info_old.link_speed; 812 813 /* update the link info structures and re-enable link events, 814 * don't bail on failure due to other book keeping needed 815 */ 816 result = ice_update_link_info(pi); 817 if (result) 818 dev_dbg(dev, 819 "Failed to update link status and re-enable link events for port %d\n", 820 pi->lport); 821 822 /* if the old link up/down and speed is the same as the new */ 823 if (link_up == old_link && link_speed == old_link_speed) 824 return result; 825 826 vsi = ice_get_main_vsi(pf); 827 if (!vsi || !vsi->port_info) 828 return -EINVAL; 829 830 /* turn off PHY if media was removed */ 831 if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) && 832 !(pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE)) { 833 set_bit(ICE_FLAG_NO_MEDIA, pf->flags); 834 835 result = ice_aq_set_link_restart_an(pi, false, NULL); 836 if (result) { 837 dev_dbg(dev, 838 "Failed to set link down, VSI %d error %d\n", 839 vsi->vsi_num, result); 840 return result; 841 } 842 } 843 844 ice_vsi_link_event(vsi, link_up); 845 ice_print_link_msg(vsi, link_up); 846 847 ice_vc_notify_link_state(pf); 848 849 return result; 850 } 851 852 /** 853 * ice_watchdog_subtask - periodic tasks not using event driven scheduling 854 * @pf: board private structure 855 */ 856 static void ice_watchdog_subtask(struct ice_pf *pf) 857 { 858 int i; 859 860 /* if interface is down do nothing */ 861 if (test_bit(__ICE_DOWN, pf->state) || 862 test_bit(__ICE_CFG_BUSY, pf->state)) 863 return; 864 865 /* make sure we don't do these things too often */ 866 if (time_before(jiffies, 867 pf->serv_tmr_prev + pf->serv_tmr_period)) 868 return; 869 870 pf->serv_tmr_prev = jiffies; 871 872 /* Update the stats for active netdevs so the network stack 873 * can look at updated numbers whenever it cares to 874 */ 875 ice_update_pf_stats(pf); 876 ice_for_each_vsi(pf, i) 877 if (pf->vsi[i] && pf->vsi[i]->netdev) 878 ice_update_vsi_stats(pf->vsi[i]); 879 } 880 881 /** 882 * ice_init_link_events - enable/initialize link events 883 * @pi: pointer to the port_info instance 884 * 885 * Returns -EIO on failure, 0 on success 886 */ 887 static int ice_init_link_events(struct ice_port_info *pi) 888 { 889 u16 mask; 890 891 mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA | 892 ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL)); 893 894 if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) { 895 dev_dbg(ice_hw_to_dev(pi->hw), 896 "Failed to set link event mask for port %d\n", 897 pi->lport); 898 return -EIO; 899 } 900 901 if (ice_aq_get_link_info(pi, true, NULL, NULL)) { 902 dev_dbg(ice_hw_to_dev(pi->hw), 903 "Failed to enable link events for port %d\n", 904 pi->lport); 905 return -EIO; 906 } 907 908 return 0; 909 } 910 911 /** 912 * ice_handle_link_event - handle link event via ARQ 913 * @pf: PF that the link event is associated with 914 * @event: event structure containing link status info 915 */ 916 static int 917 ice_handle_link_event(struct ice_pf *pf, struct ice_rq_event_info *event) 918 { 919 struct ice_aqc_get_link_status_data *link_data; 920 struct ice_port_info *port_info; 921 int status; 922 923 link_data = (struct ice_aqc_get_link_status_data *)event->msg_buf; 924 port_info = pf->hw.port_info; 925 if (!port_info) 926 return -EINVAL; 927 928 status = ice_link_event(pf, port_info, 929 !!(link_data->link_info & ICE_AQ_LINK_UP), 930 le16_to_cpu(link_data->link_speed)); 931 if (status) 932 dev_dbg(ice_pf_to_dev(pf), 933 "Could not process link event, error %d\n", status); 934 935 return status; 936 } 937 938 /** 939 * __ice_clean_ctrlq - helper function to clean controlq rings 940 * @pf: ptr to struct ice_pf 941 * @q_type: specific Control queue type 942 */ 943 static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type) 944 { 945 struct device *dev = ice_pf_to_dev(pf); 946 struct ice_rq_event_info event; 947 struct ice_hw *hw = &pf->hw; 948 struct ice_ctl_q_info *cq; 949 u16 pending, i = 0; 950 const char *qtype; 951 u32 oldval, val; 952 953 /* Do not clean control queue if/when PF reset fails */ 954 if (test_bit(__ICE_RESET_FAILED, pf->state)) 955 return 0; 956 957 switch (q_type) { 958 case ICE_CTL_Q_ADMIN: 959 cq = &hw->adminq; 960 qtype = "Admin"; 961 break; 962 case ICE_CTL_Q_MAILBOX: 963 cq = &hw->mailboxq; 964 qtype = "Mailbox"; 965 break; 966 default: 967 dev_warn(dev, "Unknown control queue type 0x%x\n", q_type); 968 return 0; 969 } 970 971 /* check for error indications - PF_xx_AxQLEN register layout for 972 * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN. 973 */ 974 val = rd32(hw, cq->rq.len); 975 if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M | 976 PF_FW_ARQLEN_ARQCRIT_M)) { 977 oldval = val; 978 if (val & PF_FW_ARQLEN_ARQVFE_M) 979 dev_dbg(dev, "%s Receive Queue VF Error detected\n", 980 qtype); 981 if (val & PF_FW_ARQLEN_ARQOVFL_M) { 982 dev_dbg(dev, 983 "%s Receive Queue Overflow Error detected\n", 984 qtype); 985 } 986 if (val & PF_FW_ARQLEN_ARQCRIT_M) 987 dev_dbg(dev, 988 "%s Receive Queue Critical Error detected\n", 989 qtype); 990 val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M | 991 PF_FW_ARQLEN_ARQCRIT_M); 992 if (oldval != val) 993 wr32(hw, cq->rq.len, val); 994 } 995 996 val = rd32(hw, cq->sq.len); 997 if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M | 998 PF_FW_ATQLEN_ATQCRIT_M)) { 999 oldval = val; 1000 if (val & PF_FW_ATQLEN_ATQVFE_M) 1001 dev_dbg(dev, 1002 "%s Send Queue VF Error detected\n", qtype); 1003 if (val & PF_FW_ATQLEN_ATQOVFL_M) { 1004 dev_dbg(dev, "%s Send Queue Overflow Error detected\n", 1005 qtype); 1006 } 1007 if (val & PF_FW_ATQLEN_ATQCRIT_M) 1008 dev_dbg(dev, "%s Send Queue Critical Error detected\n", 1009 qtype); 1010 val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M | 1011 PF_FW_ATQLEN_ATQCRIT_M); 1012 if (oldval != val) 1013 wr32(hw, cq->sq.len, val); 1014 } 1015 1016 event.buf_len = cq->rq_buf_size; 1017 event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL); 1018 if (!event.msg_buf) 1019 return 0; 1020 1021 do { 1022 enum ice_status ret; 1023 u16 opcode; 1024 1025 ret = ice_clean_rq_elem(hw, cq, &event, &pending); 1026 if (ret == ICE_ERR_AQ_NO_WORK) 1027 break; 1028 if (ret) { 1029 dev_err(dev, "%s Receive Queue event error %d\n", qtype, 1030 ret); 1031 break; 1032 } 1033 1034 opcode = le16_to_cpu(event.desc.opcode); 1035 1036 switch (opcode) { 1037 case ice_aqc_opc_get_link_status: 1038 if (ice_handle_link_event(pf, &event)) 1039 dev_err(dev, "Could not handle link event\n"); 1040 break; 1041 case ice_mbx_opc_send_msg_to_pf: 1042 ice_vc_process_vf_msg(pf, &event); 1043 break; 1044 case ice_aqc_opc_fw_logging: 1045 ice_output_fw_log(hw, &event.desc, event.msg_buf); 1046 break; 1047 case ice_aqc_opc_lldp_set_mib_change: 1048 ice_dcb_process_lldp_set_mib_change(pf, &event); 1049 break; 1050 default: 1051 dev_dbg(dev, 1052 "%s Receive Queue unknown event 0x%04x ignored\n", 1053 qtype, opcode); 1054 break; 1055 } 1056 } while (pending && (i++ < ICE_DFLT_IRQ_WORK)); 1057 1058 kfree(event.msg_buf); 1059 1060 return pending && (i == ICE_DFLT_IRQ_WORK); 1061 } 1062 1063 /** 1064 * ice_ctrlq_pending - check if there is a difference between ntc and ntu 1065 * @hw: pointer to hardware info 1066 * @cq: control queue information 1067 * 1068 * returns true if there are pending messages in a queue, false if there aren't 1069 */ 1070 static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq) 1071 { 1072 u16 ntu; 1073 1074 ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask); 1075 return cq->rq.next_to_clean != ntu; 1076 } 1077 1078 /** 1079 * ice_clean_adminq_subtask - clean the AdminQ rings 1080 * @pf: board private structure 1081 */ 1082 static void ice_clean_adminq_subtask(struct ice_pf *pf) 1083 { 1084 struct ice_hw *hw = &pf->hw; 1085 1086 if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state)) 1087 return; 1088 1089 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN)) 1090 return; 1091 1092 clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state); 1093 1094 /* There might be a situation where new messages arrive to a control 1095 * queue between processing the last message and clearing the 1096 * EVENT_PENDING bit. So before exiting, check queue head again (using 1097 * ice_ctrlq_pending) and process new messages if any. 1098 */ 1099 if (ice_ctrlq_pending(hw, &hw->adminq)) 1100 __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN); 1101 1102 ice_flush(hw); 1103 } 1104 1105 /** 1106 * ice_clean_mailboxq_subtask - clean the MailboxQ rings 1107 * @pf: board private structure 1108 */ 1109 static void ice_clean_mailboxq_subtask(struct ice_pf *pf) 1110 { 1111 struct ice_hw *hw = &pf->hw; 1112 1113 if (!test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state)) 1114 return; 1115 1116 if (__ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX)) 1117 return; 1118 1119 clear_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state); 1120 1121 if (ice_ctrlq_pending(hw, &hw->mailboxq)) 1122 __ice_clean_ctrlq(pf, ICE_CTL_Q_MAILBOX); 1123 1124 ice_flush(hw); 1125 } 1126 1127 /** 1128 * ice_service_task_schedule - schedule the service task to wake up 1129 * @pf: board private structure 1130 * 1131 * If not already scheduled, this puts the task into the work queue. 1132 */ 1133 static void ice_service_task_schedule(struct ice_pf *pf) 1134 { 1135 if (!test_bit(__ICE_SERVICE_DIS, pf->state) && 1136 !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state) && 1137 !test_bit(__ICE_NEEDS_RESTART, pf->state)) 1138 queue_work(ice_wq, &pf->serv_task); 1139 } 1140 1141 /** 1142 * ice_service_task_complete - finish up the service task 1143 * @pf: board private structure 1144 */ 1145 static void ice_service_task_complete(struct ice_pf *pf) 1146 { 1147 WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state)); 1148 1149 /* force memory (pf->state) to sync before next service task */ 1150 smp_mb__before_atomic(); 1151 clear_bit(__ICE_SERVICE_SCHED, pf->state); 1152 } 1153 1154 /** 1155 * ice_service_task_stop - stop service task and cancel works 1156 * @pf: board private structure 1157 */ 1158 static void ice_service_task_stop(struct ice_pf *pf) 1159 { 1160 set_bit(__ICE_SERVICE_DIS, pf->state); 1161 1162 if (pf->serv_tmr.function) 1163 del_timer_sync(&pf->serv_tmr); 1164 if (pf->serv_task.func) 1165 cancel_work_sync(&pf->serv_task); 1166 1167 clear_bit(__ICE_SERVICE_SCHED, pf->state); 1168 } 1169 1170 /** 1171 * ice_service_task_restart - restart service task and schedule works 1172 * @pf: board private structure 1173 * 1174 * This function is needed for suspend and resume works (e.g WoL scenario) 1175 */ 1176 static void ice_service_task_restart(struct ice_pf *pf) 1177 { 1178 clear_bit(__ICE_SERVICE_DIS, pf->state); 1179 ice_service_task_schedule(pf); 1180 } 1181 1182 /** 1183 * ice_service_timer - timer callback to schedule service task 1184 * @t: pointer to timer_list 1185 */ 1186 static void ice_service_timer(struct timer_list *t) 1187 { 1188 struct ice_pf *pf = from_timer(pf, t, serv_tmr); 1189 1190 mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies)); 1191 ice_service_task_schedule(pf); 1192 } 1193 1194 /** 1195 * ice_handle_mdd_event - handle malicious driver detect event 1196 * @pf: pointer to the PF structure 1197 * 1198 * Called from service task. OICR interrupt handler indicates MDD event 1199 */ 1200 static void ice_handle_mdd_event(struct ice_pf *pf) 1201 { 1202 struct device *dev = ice_pf_to_dev(pf); 1203 struct ice_hw *hw = &pf->hw; 1204 bool mdd_detected = false; 1205 u32 reg; 1206 int i; 1207 1208 if (!test_and_clear_bit(__ICE_MDD_EVENT_PENDING, pf->state)) 1209 return; 1210 1211 /* find what triggered the MDD event */ 1212 reg = rd32(hw, GL_MDET_TX_PQM); 1213 if (reg & GL_MDET_TX_PQM_VALID_M) { 1214 u8 pf_num = (reg & GL_MDET_TX_PQM_PF_NUM_M) >> 1215 GL_MDET_TX_PQM_PF_NUM_S; 1216 u16 vf_num = (reg & GL_MDET_TX_PQM_VF_NUM_M) >> 1217 GL_MDET_TX_PQM_VF_NUM_S; 1218 u8 event = (reg & GL_MDET_TX_PQM_MAL_TYPE_M) >> 1219 GL_MDET_TX_PQM_MAL_TYPE_S; 1220 u16 queue = ((reg & GL_MDET_TX_PQM_QNUM_M) >> 1221 GL_MDET_TX_PQM_QNUM_S); 1222 1223 if (netif_msg_tx_err(pf)) 1224 dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n", 1225 event, queue, pf_num, vf_num); 1226 wr32(hw, GL_MDET_TX_PQM, 0xffffffff); 1227 mdd_detected = true; 1228 } 1229 1230 reg = rd32(hw, GL_MDET_TX_TCLAN); 1231 if (reg & GL_MDET_TX_TCLAN_VALID_M) { 1232 u8 pf_num = (reg & GL_MDET_TX_TCLAN_PF_NUM_M) >> 1233 GL_MDET_TX_TCLAN_PF_NUM_S; 1234 u16 vf_num = (reg & GL_MDET_TX_TCLAN_VF_NUM_M) >> 1235 GL_MDET_TX_TCLAN_VF_NUM_S; 1236 u8 event = (reg & GL_MDET_TX_TCLAN_MAL_TYPE_M) >> 1237 GL_MDET_TX_TCLAN_MAL_TYPE_S; 1238 u16 queue = ((reg & GL_MDET_TX_TCLAN_QNUM_M) >> 1239 GL_MDET_TX_TCLAN_QNUM_S); 1240 1241 if (netif_msg_rx_err(pf)) 1242 dev_info(dev, "Malicious Driver Detection event %d on TX queue %d PF# %d VF# %d\n", 1243 event, queue, pf_num, vf_num); 1244 wr32(hw, GL_MDET_TX_TCLAN, 0xffffffff); 1245 mdd_detected = true; 1246 } 1247 1248 reg = rd32(hw, GL_MDET_RX); 1249 if (reg & GL_MDET_RX_VALID_M) { 1250 u8 pf_num = (reg & GL_MDET_RX_PF_NUM_M) >> 1251 GL_MDET_RX_PF_NUM_S; 1252 u16 vf_num = (reg & GL_MDET_RX_VF_NUM_M) >> 1253 GL_MDET_RX_VF_NUM_S; 1254 u8 event = (reg & GL_MDET_RX_MAL_TYPE_M) >> 1255 GL_MDET_RX_MAL_TYPE_S; 1256 u16 queue = ((reg & GL_MDET_RX_QNUM_M) >> 1257 GL_MDET_RX_QNUM_S); 1258 1259 if (netif_msg_rx_err(pf)) 1260 dev_info(dev, "Malicious Driver Detection event %d on RX queue %d PF# %d VF# %d\n", 1261 event, queue, pf_num, vf_num); 1262 wr32(hw, GL_MDET_RX, 0xffffffff); 1263 mdd_detected = true; 1264 } 1265 1266 if (mdd_detected) { 1267 bool pf_mdd_detected = false; 1268 1269 reg = rd32(hw, PF_MDET_TX_PQM); 1270 if (reg & PF_MDET_TX_PQM_VALID_M) { 1271 wr32(hw, PF_MDET_TX_PQM, 0xFFFF); 1272 dev_info(dev, "TX driver issue detected, PF reset issued\n"); 1273 pf_mdd_detected = true; 1274 } 1275 1276 reg = rd32(hw, PF_MDET_TX_TCLAN); 1277 if (reg & PF_MDET_TX_TCLAN_VALID_M) { 1278 wr32(hw, PF_MDET_TX_TCLAN, 0xFFFF); 1279 dev_info(dev, "TX driver issue detected, PF reset issued\n"); 1280 pf_mdd_detected = true; 1281 } 1282 1283 reg = rd32(hw, PF_MDET_RX); 1284 if (reg & PF_MDET_RX_VALID_M) { 1285 wr32(hw, PF_MDET_RX, 0xFFFF); 1286 dev_info(dev, "RX driver issue detected, PF reset issued\n"); 1287 pf_mdd_detected = true; 1288 } 1289 /* Queue belongs to the PF initiate a reset */ 1290 if (pf_mdd_detected) { 1291 set_bit(__ICE_NEEDS_RESTART, pf->state); 1292 ice_service_task_schedule(pf); 1293 } 1294 } 1295 1296 /* check to see if one of the VFs caused the MDD */ 1297 ice_for_each_vf(pf, i) { 1298 struct ice_vf *vf = &pf->vf[i]; 1299 1300 bool vf_mdd_detected = false; 1301 1302 reg = rd32(hw, VP_MDET_TX_PQM(i)); 1303 if (reg & VP_MDET_TX_PQM_VALID_M) { 1304 wr32(hw, VP_MDET_TX_PQM(i), 0xFFFF); 1305 vf_mdd_detected = true; 1306 dev_info(dev, "TX driver issue detected on VF %d\n", 1307 i); 1308 } 1309 1310 reg = rd32(hw, VP_MDET_TX_TCLAN(i)); 1311 if (reg & VP_MDET_TX_TCLAN_VALID_M) { 1312 wr32(hw, VP_MDET_TX_TCLAN(i), 0xFFFF); 1313 vf_mdd_detected = true; 1314 dev_info(dev, "TX driver issue detected on VF %d\n", 1315 i); 1316 } 1317 1318 reg = rd32(hw, VP_MDET_TX_TDPU(i)); 1319 if (reg & VP_MDET_TX_TDPU_VALID_M) { 1320 wr32(hw, VP_MDET_TX_TDPU(i), 0xFFFF); 1321 vf_mdd_detected = true; 1322 dev_info(dev, "TX driver issue detected on VF %d\n", 1323 i); 1324 } 1325 1326 reg = rd32(hw, VP_MDET_RX(i)); 1327 if (reg & VP_MDET_RX_VALID_M) { 1328 wr32(hw, VP_MDET_RX(i), 0xFFFF); 1329 vf_mdd_detected = true; 1330 dev_info(dev, "RX driver issue detected on VF %d\n", 1331 i); 1332 } 1333 1334 if (vf_mdd_detected) { 1335 vf->num_mdd_events++; 1336 if (vf->num_mdd_events && 1337 vf->num_mdd_events <= ICE_MDD_EVENTS_THRESHOLD) 1338 dev_info(dev, 1339 "VF %d has had %llu MDD events since last boot, Admin might need to reload AVF driver with this number of events\n", 1340 i, vf->num_mdd_events); 1341 } 1342 } 1343 } 1344 1345 /** 1346 * ice_force_phys_link_state - Force the physical link state 1347 * @vsi: VSI to force the physical link state to up/down 1348 * @link_up: true/false indicates to set the physical link to up/down 1349 * 1350 * Force the physical link state by getting the current PHY capabilities from 1351 * hardware and setting the PHY config based on the determined capabilities. If 1352 * link changes a link event will be triggered because both the Enable Automatic 1353 * Link Update and LESM Enable bits are set when setting the PHY capabilities. 1354 * 1355 * Returns 0 on success, negative on failure 1356 */ 1357 static int ice_force_phys_link_state(struct ice_vsi *vsi, bool link_up) 1358 { 1359 struct ice_aqc_get_phy_caps_data *pcaps; 1360 struct ice_aqc_set_phy_cfg_data *cfg; 1361 struct ice_port_info *pi; 1362 struct device *dev; 1363 int retcode; 1364 1365 if (!vsi || !vsi->port_info || !vsi->back) 1366 return -EINVAL; 1367 if (vsi->type != ICE_VSI_PF) 1368 return 0; 1369 1370 dev = &vsi->back->pdev->dev; 1371 1372 pi = vsi->port_info; 1373 1374 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL); 1375 if (!pcaps) 1376 return -ENOMEM; 1377 1378 retcode = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps, 1379 NULL); 1380 if (retcode) { 1381 dev_err(dev, 1382 "Failed to get phy capabilities, VSI %d error %d\n", 1383 vsi->vsi_num, retcode); 1384 retcode = -EIO; 1385 goto out; 1386 } 1387 1388 /* No change in link */ 1389 if (link_up == !!(pcaps->caps & ICE_AQC_PHY_EN_LINK) && 1390 link_up == !!(pi->phy.link_info.link_info & ICE_AQ_LINK_UP)) 1391 goto out; 1392 1393 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL); 1394 if (!cfg) { 1395 retcode = -ENOMEM; 1396 goto out; 1397 } 1398 1399 cfg->phy_type_low = pcaps->phy_type_low; 1400 cfg->phy_type_high = pcaps->phy_type_high; 1401 cfg->caps = pcaps->caps | ICE_AQ_PHY_ENA_AUTO_LINK_UPDT; 1402 cfg->low_power_ctrl = pcaps->low_power_ctrl; 1403 cfg->eee_cap = pcaps->eee_cap; 1404 cfg->eeer_value = pcaps->eeer_value; 1405 cfg->link_fec_opt = pcaps->link_fec_options; 1406 if (link_up) 1407 cfg->caps |= ICE_AQ_PHY_ENA_LINK; 1408 else 1409 cfg->caps &= ~ICE_AQ_PHY_ENA_LINK; 1410 1411 retcode = ice_aq_set_phy_cfg(&vsi->back->hw, pi->lport, cfg, NULL); 1412 if (retcode) { 1413 dev_err(dev, "Failed to set phy config, VSI %d error %d\n", 1414 vsi->vsi_num, retcode); 1415 retcode = -EIO; 1416 } 1417 1418 kfree(cfg); 1419 out: 1420 kfree(pcaps); 1421 return retcode; 1422 } 1423 1424 /** 1425 * ice_check_media_subtask - Check for media; bring link up if detected. 1426 * @pf: pointer to PF struct 1427 */ 1428 static void ice_check_media_subtask(struct ice_pf *pf) 1429 { 1430 struct ice_port_info *pi; 1431 struct ice_vsi *vsi; 1432 int err; 1433 1434 vsi = ice_get_main_vsi(pf); 1435 if (!vsi) 1436 return; 1437 1438 /* No need to check for media if it's already present or the interface 1439 * is down 1440 */ 1441 if (!test_bit(ICE_FLAG_NO_MEDIA, pf->flags) || 1442 test_bit(__ICE_DOWN, vsi->state)) 1443 return; 1444 1445 /* Refresh link info and check if media is present */ 1446 pi = vsi->port_info; 1447 err = ice_update_link_info(pi); 1448 if (err) 1449 return; 1450 1451 if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) { 1452 err = ice_force_phys_link_state(vsi, true); 1453 if (err) 1454 return; 1455 clear_bit(ICE_FLAG_NO_MEDIA, pf->flags); 1456 1457 /* A Link Status Event will be generated; the event handler 1458 * will complete bringing the interface up 1459 */ 1460 } 1461 } 1462 1463 /** 1464 * ice_service_task - manage and run subtasks 1465 * @work: pointer to work_struct contained by the PF struct 1466 */ 1467 static void ice_service_task(struct work_struct *work) 1468 { 1469 struct ice_pf *pf = container_of(work, struct ice_pf, serv_task); 1470 unsigned long start_time = jiffies; 1471 1472 /* subtasks */ 1473 1474 /* process reset requests first */ 1475 ice_reset_subtask(pf); 1476 1477 /* bail if a reset/recovery cycle is pending or rebuild failed */ 1478 if (ice_is_reset_in_progress(pf->state) || 1479 test_bit(__ICE_SUSPENDED, pf->state) || 1480 test_bit(__ICE_NEEDS_RESTART, pf->state)) { 1481 ice_service_task_complete(pf); 1482 return; 1483 } 1484 1485 ice_clean_adminq_subtask(pf); 1486 ice_check_media_subtask(pf); 1487 ice_check_for_hang_subtask(pf); 1488 ice_sync_fltr_subtask(pf); 1489 ice_handle_mdd_event(pf); 1490 ice_watchdog_subtask(pf); 1491 1492 if (ice_is_safe_mode(pf)) { 1493 ice_service_task_complete(pf); 1494 return; 1495 } 1496 1497 ice_process_vflr_event(pf); 1498 ice_clean_mailboxq_subtask(pf); 1499 1500 /* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */ 1501 ice_service_task_complete(pf); 1502 1503 /* If the tasks have taken longer than one service timer period 1504 * or there is more work to be done, reset the service timer to 1505 * schedule the service task now. 1506 */ 1507 if (time_after(jiffies, (start_time + pf->serv_tmr_period)) || 1508 test_bit(__ICE_MDD_EVENT_PENDING, pf->state) || 1509 test_bit(__ICE_VFLR_EVENT_PENDING, pf->state) || 1510 test_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state) || 1511 test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state)) 1512 mod_timer(&pf->serv_tmr, jiffies); 1513 } 1514 1515 /** 1516 * ice_set_ctrlq_len - helper function to set controlq length 1517 * @hw: pointer to the HW instance 1518 */ 1519 static void ice_set_ctrlq_len(struct ice_hw *hw) 1520 { 1521 hw->adminq.num_rq_entries = ICE_AQ_LEN; 1522 hw->adminq.num_sq_entries = ICE_AQ_LEN; 1523 hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN; 1524 hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN; 1525 hw->mailboxq.num_rq_entries = ICE_MBXRQ_LEN; 1526 hw->mailboxq.num_sq_entries = ICE_MBXSQ_LEN; 1527 hw->mailboxq.rq_buf_size = ICE_MBXQ_MAX_BUF_LEN; 1528 hw->mailboxq.sq_buf_size = ICE_MBXQ_MAX_BUF_LEN; 1529 } 1530 1531 /** 1532 * ice_schedule_reset - schedule a reset 1533 * @pf: board private structure 1534 * @reset: reset being requested 1535 */ 1536 int ice_schedule_reset(struct ice_pf *pf, enum ice_reset_req reset) 1537 { 1538 struct device *dev = ice_pf_to_dev(pf); 1539 1540 /* bail out if earlier reset has failed */ 1541 if (test_bit(__ICE_RESET_FAILED, pf->state)) { 1542 dev_dbg(dev, "earlier reset has failed\n"); 1543 return -EIO; 1544 } 1545 /* bail if reset/recovery already in progress */ 1546 if (ice_is_reset_in_progress(pf->state)) { 1547 dev_dbg(dev, "Reset already in progress\n"); 1548 return -EBUSY; 1549 } 1550 1551 switch (reset) { 1552 case ICE_RESET_PFR: 1553 set_bit(__ICE_PFR_REQ, pf->state); 1554 break; 1555 case ICE_RESET_CORER: 1556 set_bit(__ICE_CORER_REQ, pf->state); 1557 break; 1558 case ICE_RESET_GLOBR: 1559 set_bit(__ICE_GLOBR_REQ, pf->state); 1560 break; 1561 default: 1562 return -EINVAL; 1563 } 1564 1565 ice_service_task_schedule(pf); 1566 return 0; 1567 } 1568 1569 /** 1570 * ice_irq_affinity_notify - Callback for affinity changes 1571 * @notify: context as to what irq was changed 1572 * @mask: the new affinity mask 1573 * 1574 * This is a callback function used by the irq_set_affinity_notifier function 1575 * so that we may register to receive changes to the irq affinity masks. 1576 */ 1577 static void 1578 ice_irq_affinity_notify(struct irq_affinity_notify *notify, 1579 const cpumask_t *mask) 1580 { 1581 struct ice_q_vector *q_vector = 1582 container_of(notify, struct ice_q_vector, affinity_notify); 1583 1584 cpumask_copy(&q_vector->affinity_mask, mask); 1585 } 1586 1587 /** 1588 * ice_irq_affinity_release - Callback for affinity notifier release 1589 * @ref: internal core kernel usage 1590 * 1591 * This is a callback function used by the irq_set_affinity_notifier function 1592 * to inform the current notification subscriber that they will no longer 1593 * receive notifications. 1594 */ 1595 static void ice_irq_affinity_release(struct kref __always_unused *ref) {} 1596 1597 /** 1598 * ice_vsi_ena_irq - Enable IRQ for the given VSI 1599 * @vsi: the VSI being configured 1600 */ 1601 static int ice_vsi_ena_irq(struct ice_vsi *vsi) 1602 { 1603 struct ice_hw *hw = &vsi->back->hw; 1604 int i; 1605 1606 ice_for_each_q_vector(vsi, i) 1607 ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]); 1608 1609 ice_flush(hw); 1610 return 0; 1611 } 1612 1613 /** 1614 * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI 1615 * @vsi: the VSI being configured 1616 * @basename: name for the vector 1617 */ 1618 static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename) 1619 { 1620 int q_vectors = vsi->num_q_vectors; 1621 struct ice_pf *pf = vsi->back; 1622 int base = vsi->base_vector; 1623 struct device *dev; 1624 int rx_int_idx = 0; 1625 int tx_int_idx = 0; 1626 int vector, err; 1627 int irq_num; 1628 1629 dev = ice_pf_to_dev(pf); 1630 for (vector = 0; vector < q_vectors; vector++) { 1631 struct ice_q_vector *q_vector = vsi->q_vectors[vector]; 1632 1633 irq_num = pf->msix_entries[base + vector].vector; 1634 1635 if (q_vector->tx.ring && q_vector->rx.ring) { 1636 snprintf(q_vector->name, sizeof(q_vector->name) - 1, 1637 "%s-%s-%d", basename, "TxRx", rx_int_idx++); 1638 tx_int_idx++; 1639 } else if (q_vector->rx.ring) { 1640 snprintf(q_vector->name, sizeof(q_vector->name) - 1, 1641 "%s-%s-%d", basename, "rx", rx_int_idx++); 1642 } else if (q_vector->tx.ring) { 1643 snprintf(q_vector->name, sizeof(q_vector->name) - 1, 1644 "%s-%s-%d", basename, "tx", tx_int_idx++); 1645 } else { 1646 /* skip this unused q_vector */ 1647 continue; 1648 } 1649 err = devm_request_irq(dev, irq_num, vsi->irq_handler, 0, 1650 q_vector->name, q_vector); 1651 if (err) { 1652 netdev_err(vsi->netdev, 1653 "MSIX request_irq failed, error: %d\n", err); 1654 goto free_q_irqs; 1655 } 1656 1657 /* register for affinity change notifications */ 1658 q_vector->affinity_notify.notify = ice_irq_affinity_notify; 1659 q_vector->affinity_notify.release = ice_irq_affinity_release; 1660 irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify); 1661 1662 /* assign the mask for this irq */ 1663 irq_set_affinity_hint(irq_num, &q_vector->affinity_mask); 1664 } 1665 1666 vsi->irqs_ready = true; 1667 return 0; 1668 1669 free_q_irqs: 1670 while (vector) { 1671 vector--; 1672 irq_num = pf->msix_entries[base + vector].vector, 1673 irq_set_affinity_notifier(irq_num, NULL); 1674 irq_set_affinity_hint(irq_num, NULL); 1675 devm_free_irq(dev, irq_num, &vsi->q_vectors[vector]); 1676 } 1677 return err; 1678 } 1679 1680 /** 1681 * ice_xdp_alloc_setup_rings - Allocate and setup Tx rings for XDP 1682 * @vsi: VSI to setup Tx rings used by XDP 1683 * 1684 * Return 0 on success and negative value on error 1685 */ 1686 static int ice_xdp_alloc_setup_rings(struct ice_vsi *vsi) 1687 { 1688 struct device *dev = &vsi->back->pdev->dev; 1689 int i; 1690 1691 for (i = 0; i < vsi->num_xdp_txq; i++) { 1692 u16 xdp_q_idx = vsi->alloc_txq + i; 1693 struct ice_ring *xdp_ring; 1694 1695 xdp_ring = kzalloc(sizeof(*xdp_ring), GFP_KERNEL); 1696 1697 if (!xdp_ring) 1698 goto free_xdp_rings; 1699 1700 xdp_ring->q_index = xdp_q_idx; 1701 xdp_ring->reg_idx = vsi->txq_map[xdp_q_idx]; 1702 xdp_ring->ring_active = false; 1703 xdp_ring->vsi = vsi; 1704 xdp_ring->netdev = NULL; 1705 xdp_ring->dev = dev; 1706 xdp_ring->count = vsi->num_tx_desc; 1707 vsi->xdp_rings[i] = xdp_ring; 1708 if (ice_setup_tx_ring(xdp_ring)) 1709 goto free_xdp_rings; 1710 ice_set_ring_xdp(xdp_ring); 1711 xdp_ring->xsk_umem = ice_xsk_umem(xdp_ring); 1712 } 1713 1714 return 0; 1715 1716 free_xdp_rings: 1717 for (; i >= 0; i--) 1718 if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc) 1719 ice_free_tx_ring(vsi->xdp_rings[i]); 1720 return -ENOMEM; 1721 } 1722 1723 /** 1724 * ice_vsi_assign_bpf_prog - set or clear bpf prog pointer on VSI 1725 * @vsi: VSI to set the bpf prog on 1726 * @prog: the bpf prog pointer 1727 */ 1728 static void ice_vsi_assign_bpf_prog(struct ice_vsi *vsi, struct bpf_prog *prog) 1729 { 1730 struct bpf_prog *old_prog; 1731 int i; 1732 1733 old_prog = xchg(&vsi->xdp_prog, prog); 1734 if (old_prog) 1735 bpf_prog_put(old_prog); 1736 1737 ice_for_each_rxq(vsi, i) 1738 WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog); 1739 } 1740 1741 /** 1742 * ice_prepare_xdp_rings - Allocate, configure and setup Tx rings for XDP 1743 * @vsi: VSI to bring up Tx rings used by XDP 1744 * @prog: bpf program that will be assigned to VSI 1745 * 1746 * Return 0 on success and negative value on error 1747 */ 1748 int ice_prepare_xdp_rings(struct ice_vsi *vsi, struct bpf_prog *prog) 1749 { 1750 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 }; 1751 int xdp_rings_rem = vsi->num_xdp_txq; 1752 struct ice_pf *pf = vsi->back; 1753 struct ice_qs_cfg xdp_qs_cfg = { 1754 .qs_mutex = &pf->avail_q_mutex, 1755 .pf_map = pf->avail_txqs, 1756 .pf_map_size = pf->max_pf_txqs, 1757 .q_count = vsi->num_xdp_txq, 1758 .scatter_count = ICE_MAX_SCATTER_TXQS, 1759 .vsi_map = vsi->txq_map, 1760 .vsi_map_offset = vsi->alloc_txq, 1761 .mapping_mode = ICE_VSI_MAP_CONTIG 1762 }; 1763 enum ice_status status; 1764 struct device *dev; 1765 int i, v_idx; 1766 1767 dev = ice_pf_to_dev(pf); 1768 vsi->xdp_rings = devm_kcalloc(dev, vsi->num_xdp_txq, 1769 sizeof(*vsi->xdp_rings), GFP_KERNEL); 1770 if (!vsi->xdp_rings) 1771 return -ENOMEM; 1772 1773 vsi->xdp_mapping_mode = xdp_qs_cfg.mapping_mode; 1774 if (__ice_vsi_get_qs(&xdp_qs_cfg)) 1775 goto err_map_xdp; 1776 1777 if (ice_xdp_alloc_setup_rings(vsi)) 1778 goto clear_xdp_rings; 1779 1780 /* follow the logic from ice_vsi_map_rings_to_vectors */ 1781 ice_for_each_q_vector(vsi, v_idx) { 1782 struct ice_q_vector *q_vector = vsi->q_vectors[v_idx]; 1783 int xdp_rings_per_v, q_id, q_base; 1784 1785 xdp_rings_per_v = DIV_ROUND_UP(xdp_rings_rem, 1786 vsi->num_q_vectors - v_idx); 1787 q_base = vsi->num_xdp_txq - xdp_rings_rem; 1788 1789 for (q_id = q_base; q_id < (q_base + xdp_rings_per_v); q_id++) { 1790 struct ice_ring *xdp_ring = vsi->xdp_rings[q_id]; 1791 1792 xdp_ring->q_vector = q_vector; 1793 xdp_ring->next = q_vector->tx.ring; 1794 q_vector->tx.ring = xdp_ring; 1795 } 1796 xdp_rings_rem -= xdp_rings_per_v; 1797 } 1798 1799 /* omit the scheduler update if in reset path; XDP queues will be 1800 * taken into account at the end of ice_vsi_rebuild, where 1801 * ice_cfg_vsi_lan is being called 1802 */ 1803 if (ice_is_reset_in_progress(pf->state)) 1804 return 0; 1805 1806 /* tell the Tx scheduler that right now we have 1807 * additional queues 1808 */ 1809 for (i = 0; i < vsi->tc_cfg.numtc; i++) 1810 max_txqs[i] = vsi->num_txq + vsi->num_xdp_txq; 1811 1812 status = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc, 1813 max_txqs); 1814 if (status) { 1815 dev_err(dev, "Failed VSI LAN queue config for XDP, error:%d\n", 1816 status); 1817 goto clear_xdp_rings; 1818 } 1819 ice_vsi_assign_bpf_prog(vsi, prog); 1820 1821 return 0; 1822 clear_xdp_rings: 1823 for (i = 0; i < vsi->num_xdp_txq; i++) 1824 if (vsi->xdp_rings[i]) { 1825 kfree_rcu(vsi->xdp_rings[i], rcu); 1826 vsi->xdp_rings[i] = NULL; 1827 } 1828 1829 err_map_xdp: 1830 mutex_lock(&pf->avail_q_mutex); 1831 for (i = 0; i < vsi->num_xdp_txq; i++) { 1832 clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs); 1833 vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX; 1834 } 1835 mutex_unlock(&pf->avail_q_mutex); 1836 1837 devm_kfree(dev, vsi->xdp_rings); 1838 return -ENOMEM; 1839 } 1840 1841 /** 1842 * ice_destroy_xdp_rings - undo the configuration made by ice_prepare_xdp_rings 1843 * @vsi: VSI to remove XDP rings 1844 * 1845 * Detach XDP rings from irq vectors, clean up the PF bitmap and free 1846 * resources 1847 */ 1848 int ice_destroy_xdp_rings(struct ice_vsi *vsi) 1849 { 1850 u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 }; 1851 struct ice_pf *pf = vsi->back; 1852 int i, v_idx; 1853 1854 /* q_vectors are freed in reset path so there's no point in detaching 1855 * rings; in case of rebuild being triggered not from reset reset bits 1856 * in pf->state won't be set, so additionally check first q_vector 1857 * against NULL 1858 */ 1859 if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0]) 1860 goto free_qmap; 1861 1862 ice_for_each_q_vector(vsi, v_idx) { 1863 struct ice_q_vector *q_vector = vsi->q_vectors[v_idx]; 1864 struct ice_ring *ring; 1865 1866 ice_for_each_ring(ring, q_vector->tx) 1867 if (!ring->tx_buf || !ice_ring_is_xdp(ring)) 1868 break; 1869 1870 /* restore the value of last node prior to XDP setup */ 1871 q_vector->tx.ring = ring; 1872 } 1873 1874 free_qmap: 1875 mutex_lock(&pf->avail_q_mutex); 1876 for (i = 0; i < vsi->num_xdp_txq; i++) { 1877 clear_bit(vsi->txq_map[i + vsi->alloc_txq], pf->avail_txqs); 1878 vsi->txq_map[i + vsi->alloc_txq] = ICE_INVAL_Q_INDEX; 1879 } 1880 mutex_unlock(&pf->avail_q_mutex); 1881 1882 for (i = 0; i < vsi->num_xdp_txq; i++) 1883 if (vsi->xdp_rings[i]) { 1884 if (vsi->xdp_rings[i]->desc) 1885 ice_free_tx_ring(vsi->xdp_rings[i]); 1886 kfree_rcu(vsi->xdp_rings[i], rcu); 1887 vsi->xdp_rings[i] = NULL; 1888 } 1889 1890 devm_kfree(ice_pf_to_dev(pf), vsi->xdp_rings); 1891 vsi->xdp_rings = NULL; 1892 1893 if (ice_is_reset_in_progress(pf->state) || !vsi->q_vectors[0]) 1894 return 0; 1895 1896 ice_vsi_assign_bpf_prog(vsi, NULL); 1897 1898 /* notify Tx scheduler that we destroyed XDP queues and bring 1899 * back the old number of child nodes 1900 */ 1901 for (i = 0; i < vsi->tc_cfg.numtc; i++) 1902 max_txqs[i] = vsi->num_txq; 1903 1904 return ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc, 1905 max_txqs); 1906 } 1907 1908 /** 1909 * ice_xdp_setup_prog - Add or remove XDP eBPF program 1910 * @vsi: VSI to setup XDP for 1911 * @prog: XDP program 1912 * @extack: netlink extended ack 1913 */ 1914 static int 1915 ice_xdp_setup_prog(struct ice_vsi *vsi, struct bpf_prog *prog, 1916 struct netlink_ext_ack *extack) 1917 { 1918 int frame_size = vsi->netdev->mtu + ICE_ETH_PKT_HDR_PAD; 1919 bool if_running = netif_running(vsi->netdev); 1920 int ret = 0, xdp_ring_err = 0; 1921 1922 if (frame_size > vsi->rx_buf_len) { 1923 NL_SET_ERR_MSG_MOD(extack, "MTU too large for loading XDP"); 1924 return -EOPNOTSUPP; 1925 } 1926 1927 /* need to stop netdev while setting up the program for Rx rings */ 1928 if (if_running && !test_and_set_bit(__ICE_DOWN, vsi->state)) { 1929 ret = ice_down(vsi); 1930 if (ret) { 1931 NL_SET_ERR_MSG_MOD(extack, 1932 "Preparing device for XDP attach failed"); 1933 return ret; 1934 } 1935 } 1936 1937 if (!ice_is_xdp_ena_vsi(vsi) && prog) { 1938 vsi->num_xdp_txq = vsi->alloc_txq; 1939 xdp_ring_err = ice_prepare_xdp_rings(vsi, prog); 1940 if (xdp_ring_err) 1941 NL_SET_ERR_MSG_MOD(extack, 1942 "Setting up XDP Tx resources failed"); 1943 } else if (ice_is_xdp_ena_vsi(vsi) && !prog) { 1944 xdp_ring_err = ice_destroy_xdp_rings(vsi); 1945 if (xdp_ring_err) 1946 NL_SET_ERR_MSG_MOD(extack, 1947 "Freeing XDP Tx resources failed"); 1948 } else { 1949 ice_vsi_assign_bpf_prog(vsi, prog); 1950 } 1951 1952 if (if_running) 1953 ret = ice_up(vsi); 1954 1955 if (!ret && prog && vsi->xsk_umems) { 1956 int i; 1957 1958 ice_for_each_rxq(vsi, i) { 1959 struct ice_ring *rx_ring = vsi->rx_rings[i]; 1960 1961 if (rx_ring->xsk_umem) 1962 napi_schedule(&rx_ring->q_vector->napi); 1963 } 1964 } 1965 1966 return (ret || xdp_ring_err) ? -ENOMEM : 0; 1967 } 1968 1969 /** 1970 * ice_xdp - implements XDP handler 1971 * @dev: netdevice 1972 * @xdp: XDP command 1973 */ 1974 static int ice_xdp(struct net_device *dev, struct netdev_bpf *xdp) 1975 { 1976 struct ice_netdev_priv *np = netdev_priv(dev); 1977 struct ice_vsi *vsi = np->vsi; 1978 1979 if (vsi->type != ICE_VSI_PF) { 1980 NL_SET_ERR_MSG_MOD(xdp->extack, 1981 "XDP can be loaded only on PF VSI"); 1982 return -EINVAL; 1983 } 1984 1985 switch (xdp->command) { 1986 case XDP_SETUP_PROG: 1987 return ice_xdp_setup_prog(vsi, xdp->prog, xdp->extack); 1988 case XDP_QUERY_PROG: 1989 xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0; 1990 return 0; 1991 case XDP_SETUP_XSK_UMEM: 1992 return ice_xsk_umem_setup(vsi, xdp->xsk.umem, 1993 xdp->xsk.queue_id); 1994 default: 1995 return -EINVAL; 1996 } 1997 } 1998 1999 /** 2000 * ice_ena_misc_vector - enable the non-queue interrupts 2001 * @pf: board private structure 2002 */ 2003 static void ice_ena_misc_vector(struct ice_pf *pf) 2004 { 2005 struct ice_hw *hw = &pf->hw; 2006 u32 val; 2007 2008 /* clear things first */ 2009 wr32(hw, PFINT_OICR_ENA, 0); /* disable all */ 2010 rd32(hw, PFINT_OICR); /* read to clear */ 2011 2012 val = (PFINT_OICR_ECC_ERR_M | 2013 PFINT_OICR_MAL_DETECT_M | 2014 PFINT_OICR_GRST_M | 2015 PFINT_OICR_PCI_EXCEPTION_M | 2016 PFINT_OICR_VFLR_M | 2017 PFINT_OICR_HMC_ERR_M | 2018 PFINT_OICR_PE_CRITERR_M); 2019 2020 wr32(hw, PFINT_OICR_ENA, val); 2021 2022 /* SW_ITR_IDX = 0, but don't change INTENA */ 2023 wr32(hw, GLINT_DYN_CTL(pf->oicr_idx), 2024 GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M); 2025 } 2026 2027 /** 2028 * ice_misc_intr - misc interrupt handler 2029 * @irq: interrupt number 2030 * @data: pointer to a q_vector 2031 */ 2032 static irqreturn_t ice_misc_intr(int __always_unused irq, void *data) 2033 { 2034 struct ice_pf *pf = (struct ice_pf *)data; 2035 struct ice_hw *hw = &pf->hw; 2036 irqreturn_t ret = IRQ_NONE; 2037 struct device *dev; 2038 u32 oicr, ena_mask; 2039 2040 dev = ice_pf_to_dev(pf); 2041 set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state); 2042 set_bit(__ICE_MAILBOXQ_EVENT_PENDING, pf->state); 2043 2044 oicr = rd32(hw, PFINT_OICR); 2045 ena_mask = rd32(hw, PFINT_OICR_ENA); 2046 2047 if (oicr & PFINT_OICR_SWINT_M) { 2048 ena_mask &= ~PFINT_OICR_SWINT_M; 2049 pf->sw_int_count++; 2050 } 2051 2052 if (oicr & PFINT_OICR_MAL_DETECT_M) { 2053 ena_mask &= ~PFINT_OICR_MAL_DETECT_M; 2054 set_bit(__ICE_MDD_EVENT_PENDING, pf->state); 2055 } 2056 if (oicr & PFINT_OICR_VFLR_M) { 2057 ena_mask &= ~PFINT_OICR_VFLR_M; 2058 set_bit(__ICE_VFLR_EVENT_PENDING, pf->state); 2059 } 2060 2061 if (oicr & PFINT_OICR_GRST_M) { 2062 u32 reset; 2063 2064 /* we have a reset warning */ 2065 ena_mask &= ~PFINT_OICR_GRST_M; 2066 reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >> 2067 GLGEN_RSTAT_RESET_TYPE_S; 2068 2069 if (reset == ICE_RESET_CORER) 2070 pf->corer_count++; 2071 else if (reset == ICE_RESET_GLOBR) 2072 pf->globr_count++; 2073 else if (reset == ICE_RESET_EMPR) 2074 pf->empr_count++; 2075 else 2076 dev_dbg(dev, "Invalid reset type %d\n", reset); 2077 2078 /* If a reset cycle isn't already in progress, we set a bit in 2079 * pf->state so that the service task can start a reset/rebuild. 2080 * We also make note of which reset happened so that peer 2081 * devices/drivers can be informed. 2082 */ 2083 if (!test_and_set_bit(__ICE_RESET_OICR_RECV, pf->state)) { 2084 if (reset == ICE_RESET_CORER) 2085 set_bit(__ICE_CORER_RECV, pf->state); 2086 else if (reset == ICE_RESET_GLOBR) 2087 set_bit(__ICE_GLOBR_RECV, pf->state); 2088 else 2089 set_bit(__ICE_EMPR_RECV, pf->state); 2090 2091 /* There are couple of different bits at play here. 2092 * hw->reset_ongoing indicates whether the hardware is 2093 * in reset. This is set to true when a reset interrupt 2094 * is received and set back to false after the driver 2095 * has determined that the hardware is out of reset. 2096 * 2097 * __ICE_RESET_OICR_RECV in pf->state indicates 2098 * that a post reset rebuild is required before the 2099 * driver is operational again. This is set above. 2100 * 2101 * As this is the start of the reset/rebuild cycle, set 2102 * both to indicate that. 2103 */ 2104 hw->reset_ongoing = true; 2105 } 2106 } 2107 2108 if (oicr & PFINT_OICR_HMC_ERR_M) { 2109 ena_mask &= ~PFINT_OICR_HMC_ERR_M; 2110 dev_dbg(dev, "HMC Error interrupt - info 0x%x, data 0x%x\n", 2111 rd32(hw, PFHMC_ERRORINFO), 2112 rd32(hw, PFHMC_ERRORDATA)); 2113 } 2114 2115 /* Report any remaining unexpected interrupts */ 2116 oicr &= ena_mask; 2117 if (oicr) { 2118 dev_dbg(dev, "unhandled interrupt oicr=0x%08x\n", oicr); 2119 /* If a critical error is pending there is no choice but to 2120 * reset the device. 2121 */ 2122 if (oicr & (PFINT_OICR_PE_CRITERR_M | 2123 PFINT_OICR_PCI_EXCEPTION_M | 2124 PFINT_OICR_ECC_ERR_M)) { 2125 set_bit(__ICE_PFR_REQ, pf->state); 2126 ice_service_task_schedule(pf); 2127 } 2128 } 2129 ret = IRQ_HANDLED; 2130 2131 if (!test_bit(__ICE_DOWN, pf->state)) { 2132 ice_service_task_schedule(pf); 2133 ice_irq_dynamic_ena(hw, NULL, NULL); 2134 } 2135 2136 return ret; 2137 } 2138 2139 /** 2140 * ice_dis_ctrlq_interrupts - disable control queue interrupts 2141 * @hw: pointer to HW structure 2142 */ 2143 static void ice_dis_ctrlq_interrupts(struct ice_hw *hw) 2144 { 2145 /* disable Admin queue Interrupt causes */ 2146 wr32(hw, PFINT_FW_CTL, 2147 rd32(hw, PFINT_FW_CTL) & ~PFINT_FW_CTL_CAUSE_ENA_M); 2148 2149 /* disable Mailbox queue Interrupt causes */ 2150 wr32(hw, PFINT_MBX_CTL, 2151 rd32(hw, PFINT_MBX_CTL) & ~PFINT_MBX_CTL_CAUSE_ENA_M); 2152 2153 /* disable Control queue Interrupt causes */ 2154 wr32(hw, PFINT_OICR_CTL, 2155 rd32(hw, PFINT_OICR_CTL) & ~PFINT_OICR_CTL_CAUSE_ENA_M); 2156 2157 ice_flush(hw); 2158 } 2159 2160 /** 2161 * ice_free_irq_msix_misc - Unroll misc vector setup 2162 * @pf: board private structure 2163 */ 2164 static void ice_free_irq_msix_misc(struct ice_pf *pf) 2165 { 2166 struct ice_hw *hw = &pf->hw; 2167 2168 ice_dis_ctrlq_interrupts(hw); 2169 2170 /* disable OICR interrupt */ 2171 wr32(hw, PFINT_OICR_ENA, 0); 2172 ice_flush(hw); 2173 2174 if (pf->msix_entries) { 2175 synchronize_irq(pf->msix_entries[pf->oicr_idx].vector); 2176 devm_free_irq(ice_pf_to_dev(pf), 2177 pf->msix_entries[pf->oicr_idx].vector, pf); 2178 } 2179 2180 pf->num_avail_sw_msix += 1; 2181 ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID); 2182 } 2183 2184 /** 2185 * ice_ena_ctrlq_interrupts - enable control queue interrupts 2186 * @hw: pointer to HW structure 2187 * @reg_idx: HW vector index to associate the control queue interrupts with 2188 */ 2189 static void ice_ena_ctrlq_interrupts(struct ice_hw *hw, u16 reg_idx) 2190 { 2191 u32 val; 2192 2193 val = ((reg_idx & PFINT_OICR_CTL_MSIX_INDX_M) | 2194 PFINT_OICR_CTL_CAUSE_ENA_M); 2195 wr32(hw, PFINT_OICR_CTL, val); 2196 2197 /* enable Admin queue Interrupt causes */ 2198 val = ((reg_idx & PFINT_FW_CTL_MSIX_INDX_M) | 2199 PFINT_FW_CTL_CAUSE_ENA_M); 2200 wr32(hw, PFINT_FW_CTL, val); 2201 2202 /* enable Mailbox queue Interrupt causes */ 2203 val = ((reg_idx & PFINT_MBX_CTL_MSIX_INDX_M) | 2204 PFINT_MBX_CTL_CAUSE_ENA_M); 2205 wr32(hw, PFINT_MBX_CTL, val); 2206 2207 ice_flush(hw); 2208 } 2209 2210 /** 2211 * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events 2212 * @pf: board private structure 2213 * 2214 * This sets up the handler for MSIX 0, which is used to manage the 2215 * non-queue interrupts, e.g. AdminQ and errors. This is not used 2216 * when in MSI or Legacy interrupt mode. 2217 */ 2218 static int ice_req_irq_msix_misc(struct ice_pf *pf) 2219 { 2220 struct device *dev = ice_pf_to_dev(pf); 2221 struct ice_hw *hw = &pf->hw; 2222 int oicr_idx, err = 0; 2223 2224 if (!pf->int_name[0]) 2225 snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc", 2226 dev_driver_string(dev), dev_name(dev)); 2227 2228 /* Do not request IRQ but do enable OICR interrupt since settings are 2229 * lost during reset. Note that this function is called only during 2230 * rebuild path and not while reset is in progress. 2231 */ 2232 if (ice_is_reset_in_progress(pf->state)) 2233 goto skip_req_irq; 2234 2235 /* reserve one vector in irq_tracker for misc interrupts */ 2236 oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID); 2237 if (oicr_idx < 0) 2238 return oicr_idx; 2239 2240 pf->num_avail_sw_msix -= 1; 2241 pf->oicr_idx = oicr_idx; 2242 2243 err = devm_request_irq(dev, pf->msix_entries[pf->oicr_idx].vector, 2244 ice_misc_intr, 0, pf->int_name, pf); 2245 if (err) { 2246 dev_err(dev, "devm_request_irq for %s failed: %d\n", 2247 pf->int_name, err); 2248 ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID); 2249 pf->num_avail_sw_msix += 1; 2250 return err; 2251 } 2252 2253 skip_req_irq: 2254 ice_ena_misc_vector(pf); 2255 2256 ice_ena_ctrlq_interrupts(hw, pf->oicr_idx); 2257 wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx), 2258 ITR_REG_ALIGN(ICE_ITR_8K) >> ICE_ITR_GRAN_S); 2259 2260 ice_flush(hw); 2261 ice_irq_dynamic_ena(hw, NULL, NULL); 2262 2263 return 0; 2264 } 2265 2266 /** 2267 * ice_napi_add - register NAPI handler for the VSI 2268 * @vsi: VSI for which NAPI handler is to be registered 2269 * 2270 * This function is only called in the driver's load path. Registering the NAPI 2271 * handler is done in ice_vsi_alloc_q_vector() for all other cases (i.e. resume, 2272 * reset/rebuild, etc.) 2273 */ 2274 static void ice_napi_add(struct ice_vsi *vsi) 2275 { 2276 int v_idx; 2277 2278 if (!vsi->netdev) 2279 return; 2280 2281 ice_for_each_q_vector(vsi, v_idx) 2282 netif_napi_add(vsi->netdev, &vsi->q_vectors[v_idx]->napi, 2283 ice_napi_poll, NAPI_POLL_WEIGHT); 2284 } 2285 2286 /** 2287 * ice_set_ops - set netdev and ethtools ops for the given netdev 2288 * @netdev: netdev instance 2289 */ 2290 static void ice_set_ops(struct net_device *netdev) 2291 { 2292 struct ice_pf *pf = ice_netdev_to_pf(netdev); 2293 2294 if (ice_is_safe_mode(pf)) { 2295 netdev->netdev_ops = &ice_netdev_safe_mode_ops; 2296 ice_set_ethtool_safe_mode_ops(netdev); 2297 return; 2298 } 2299 2300 netdev->netdev_ops = &ice_netdev_ops; 2301 ice_set_ethtool_ops(netdev); 2302 } 2303 2304 /** 2305 * ice_set_netdev_features - set features for the given netdev 2306 * @netdev: netdev instance 2307 */ 2308 static void ice_set_netdev_features(struct net_device *netdev) 2309 { 2310 struct ice_pf *pf = ice_netdev_to_pf(netdev); 2311 netdev_features_t csumo_features; 2312 netdev_features_t vlano_features; 2313 netdev_features_t dflt_features; 2314 netdev_features_t tso_features; 2315 2316 if (ice_is_safe_mode(pf)) { 2317 /* safe mode */ 2318 netdev->features = NETIF_F_SG | NETIF_F_HIGHDMA; 2319 netdev->hw_features = netdev->features; 2320 return; 2321 } 2322 2323 dflt_features = NETIF_F_SG | 2324 NETIF_F_HIGHDMA | 2325 NETIF_F_RXHASH; 2326 2327 csumo_features = NETIF_F_RXCSUM | 2328 NETIF_F_IP_CSUM | 2329 NETIF_F_SCTP_CRC | 2330 NETIF_F_IPV6_CSUM; 2331 2332 vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER | 2333 NETIF_F_HW_VLAN_CTAG_TX | 2334 NETIF_F_HW_VLAN_CTAG_RX; 2335 2336 tso_features = NETIF_F_TSO | 2337 NETIF_F_GSO_UDP_L4; 2338 2339 /* set features that user can change */ 2340 netdev->hw_features = dflt_features | csumo_features | 2341 vlano_features | tso_features; 2342 2343 /* enable features */ 2344 netdev->features |= netdev->hw_features; 2345 /* encap and VLAN devices inherit default, csumo and tso features */ 2346 netdev->hw_enc_features |= dflt_features | csumo_features | 2347 tso_features; 2348 netdev->vlan_features |= dflt_features | csumo_features | 2349 tso_features; 2350 } 2351 2352 /** 2353 * ice_cfg_netdev - Allocate, configure and register a netdev 2354 * @vsi: the VSI associated with the new netdev 2355 * 2356 * Returns 0 on success, negative value on failure 2357 */ 2358 static int ice_cfg_netdev(struct ice_vsi *vsi) 2359 { 2360 struct ice_pf *pf = vsi->back; 2361 struct ice_netdev_priv *np; 2362 struct net_device *netdev; 2363 u8 mac_addr[ETH_ALEN]; 2364 int err; 2365 2366 netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq, 2367 vsi->alloc_rxq); 2368 if (!netdev) 2369 return -ENOMEM; 2370 2371 vsi->netdev = netdev; 2372 np = netdev_priv(netdev); 2373 np->vsi = vsi; 2374 2375 ice_set_netdev_features(netdev); 2376 2377 ice_set_ops(netdev); 2378 2379 if (vsi->type == ICE_VSI_PF) { 2380 SET_NETDEV_DEV(netdev, ice_pf_to_dev(pf)); 2381 ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr); 2382 ether_addr_copy(netdev->dev_addr, mac_addr); 2383 ether_addr_copy(netdev->perm_addr, mac_addr); 2384 } 2385 2386 netdev->priv_flags |= IFF_UNICAST_FLT; 2387 2388 /* Setup netdev TC information */ 2389 ice_vsi_cfg_netdev_tc(vsi, vsi->tc_cfg.ena_tc); 2390 2391 /* setup watchdog timeout value to be 5 second */ 2392 netdev->watchdog_timeo = 5 * HZ; 2393 2394 netdev->min_mtu = ETH_MIN_MTU; 2395 netdev->max_mtu = ICE_MAX_MTU; 2396 2397 err = register_netdev(vsi->netdev); 2398 if (err) 2399 return err; 2400 2401 netif_carrier_off(vsi->netdev); 2402 2403 /* make sure transmit queues start off as stopped */ 2404 netif_tx_stop_all_queues(vsi->netdev); 2405 2406 return 0; 2407 } 2408 2409 /** 2410 * ice_fill_rss_lut - Fill the RSS lookup table with default values 2411 * @lut: Lookup table 2412 * @rss_table_size: Lookup table size 2413 * @rss_size: Range of queue number for hashing 2414 */ 2415 void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size) 2416 { 2417 u16 i; 2418 2419 for (i = 0; i < rss_table_size; i++) 2420 lut[i] = i % rss_size; 2421 } 2422 2423 /** 2424 * ice_pf_vsi_setup - Set up a PF VSI 2425 * @pf: board private structure 2426 * @pi: pointer to the port_info instance 2427 * 2428 * Returns pointer to the successfully allocated VSI software struct 2429 * on success, otherwise returns NULL on failure. 2430 */ 2431 static struct ice_vsi * 2432 ice_pf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi) 2433 { 2434 return ice_vsi_setup(pf, pi, ICE_VSI_PF, ICE_INVAL_VFID); 2435 } 2436 2437 /** 2438 * ice_lb_vsi_setup - Set up a loopback VSI 2439 * @pf: board private structure 2440 * @pi: pointer to the port_info instance 2441 * 2442 * Returns pointer to the successfully allocated VSI software struct 2443 * on success, otherwise returns NULL on failure. 2444 */ 2445 struct ice_vsi * 2446 ice_lb_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi) 2447 { 2448 return ice_vsi_setup(pf, pi, ICE_VSI_LB, ICE_INVAL_VFID); 2449 } 2450 2451 /** 2452 * ice_vlan_rx_add_vid - Add a VLAN ID filter to HW offload 2453 * @netdev: network interface to be adjusted 2454 * @proto: unused protocol 2455 * @vid: VLAN ID to be added 2456 * 2457 * net_device_ops implementation for adding VLAN IDs 2458 */ 2459 static int 2460 ice_vlan_rx_add_vid(struct net_device *netdev, __always_unused __be16 proto, 2461 u16 vid) 2462 { 2463 struct ice_netdev_priv *np = netdev_priv(netdev); 2464 struct ice_vsi *vsi = np->vsi; 2465 int ret; 2466 2467 if (vid >= VLAN_N_VID) { 2468 netdev_err(netdev, "VLAN id requested %d is out of range %d\n", 2469 vid, VLAN_N_VID); 2470 return -EINVAL; 2471 } 2472 2473 if (vsi->info.pvid) 2474 return -EINVAL; 2475 2476 /* Enable VLAN pruning when VLAN 0 is added */ 2477 if (unlikely(!vid)) { 2478 ret = ice_cfg_vlan_pruning(vsi, true, false); 2479 if (ret) 2480 return ret; 2481 } 2482 2483 /* Add all VLAN IDs including 0 to the switch filter. VLAN ID 0 is 2484 * needed to continue allowing all untagged packets since VLAN prune 2485 * list is applied to all packets by the switch 2486 */ 2487 ret = ice_vsi_add_vlan(vsi, vid); 2488 if (!ret) { 2489 vsi->vlan_ena = true; 2490 set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags); 2491 } 2492 2493 return ret; 2494 } 2495 2496 /** 2497 * ice_vlan_rx_kill_vid - Remove a VLAN ID filter from HW offload 2498 * @netdev: network interface to be adjusted 2499 * @proto: unused protocol 2500 * @vid: VLAN ID to be removed 2501 * 2502 * net_device_ops implementation for removing VLAN IDs 2503 */ 2504 static int 2505 ice_vlan_rx_kill_vid(struct net_device *netdev, __always_unused __be16 proto, 2506 u16 vid) 2507 { 2508 struct ice_netdev_priv *np = netdev_priv(netdev); 2509 struct ice_vsi *vsi = np->vsi; 2510 int ret; 2511 2512 if (vsi->info.pvid) 2513 return -EINVAL; 2514 2515 /* Make sure ice_vsi_kill_vlan is successful before updating VLAN 2516 * information 2517 */ 2518 ret = ice_vsi_kill_vlan(vsi, vid); 2519 if (ret) 2520 return ret; 2521 2522 /* Disable VLAN pruning when VLAN 0 is removed */ 2523 if (unlikely(!vid)) 2524 ret = ice_cfg_vlan_pruning(vsi, false, false); 2525 2526 vsi->vlan_ena = false; 2527 set_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags); 2528 return ret; 2529 } 2530 2531 /** 2532 * ice_setup_pf_sw - Setup the HW switch on startup or after reset 2533 * @pf: board private structure 2534 * 2535 * Returns 0 on success, negative value on failure 2536 */ 2537 static int ice_setup_pf_sw(struct ice_pf *pf) 2538 { 2539 struct ice_vsi *vsi; 2540 int status = 0; 2541 2542 if (ice_is_reset_in_progress(pf->state)) 2543 return -EBUSY; 2544 2545 vsi = ice_pf_vsi_setup(pf, pf->hw.port_info); 2546 if (!vsi) { 2547 status = -ENOMEM; 2548 goto unroll_vsi_setup; 2549 } 2550 2551 status = ice_cfg_netdev(vsi); 2552 if (status) { 2553 status = -ENODEV; 2554 goto unroll_vsi_setup; 2555 } 2556 /* netdev has to be configured before setting frame size */ 2557 ice_vsi_cfg_frame_size(vsi); 2558 2559 /* Setup DCB netlink interface */ 2560 ice_dcbnl_setup(vsi); 2561 2562 /* registering the NAPI handler requires both the queues and 2563 * netdev to be created, which are done in ice_pf_vsi_setup() 2564 * and ice_cfg_netdev() respectively 2565 */ 2566 ice_napi_add(vsi); 2567 2568 status = ice_init_mac_fltr(pf); 2569 if (status) 2570 goto unroll_napi_add; 2571 2572 return status; 2573 2574 unroll_napi_add: 2575 if (vsi) { 2576 ice_napi_del(vsi); 2577 if (vsi->netdev) { 2578 if (vsi->netdev->reg_state == NETREG_REGISTERED) 2579 unregister_netdev(vsi->netdev); 2580 free_netdev(vsi->netdev); 2581 vsi->netdev = NULL; 2582 } 2583 } 2584 2585 unroll_vsi_setup: 2586 if (vsi) { 2587 ice_vsi_free_q_vectors(vsi); 2588 ice_vsi_delete(vsi); 2589 ice_vsi_put_qs(vsi); 2590 ice_vsi_clear(vsi); 2591 } 2592 return status; 2593 } 2594 2595 /** 2596 * ice_get_avail_q_count - Get count of queues in use 2597 * @pf_qmap: bitmap to get queue use count from 2598 * @lock: pointer to a mutex that protects access to pf_qmap 2599 * @size: size of the bitmap 2600 */ 2601 static u16 2602 ice_get_avail_q_count(unsigned long *pf_qmap, struct mutex *lock, u16 size) 2603 { 2604 u16 count = 0, bit; 2605 2606 mutex_lock(lock); 2607 for_each_clear_bit(bit, pf_qmap, size) 2608 count++; 2609 mutex_unlock(lock); 2610 2611 return count; 2612 } 2613 2614 /** 2615 * ice_get_avail_txq_count - Get count of Tx queues in use 2616 * @pf: pointer to an ice_pf instance 2617 */ 2618 u16 ice_get_avail_txq_count(struct ice_pf *pf) 2619 { 2620 return ice_get_avail_q_count(pf->avail_txqs, &pf->avail_q_mutex, 2621 pf->max_pf_txqs); 2622 } 2623 2624 /** 2625 * ice_get_avail_rxq_count - Get count of Rx queues in use 2626 * @pf: pointer to an ice_pf instance 2627 */ 2628 u16 ice_get_avail_rxq_count(struct ice_pf *pf) 2629 { 2630 return ice_get_avail_q_count(pf->avail_rxqs, &pf->avail_q_mutex, 2631 pf->max_pf_rxqs); 2632 } 2633 2634 /** 2635 * ice_deinit_pf - Unrolls initialziations done by ice_init_pf 2636 * @pf: board private structure to initialize 2637 */ 2638 static void ice_deinit_pf(struct ice_pf *pf) 2639 { 2640 ice_service_task_stop(pf); 2641 mutex_destroy(&pf->sw_mutex); 2642 mutex_destroy(&pf->tc_mutex); 2643 mutex_destroy(&pf->avail_q_mutex); 2644 2645 if (pf->avail_txqs) { 2646 bitmap_free(pf->avail_txqs); 2647 pf->avail_txqs = NULL; 2648 } 2649 2650 if (pf->avail_rxqs) { 2651 bitmap_free(pf->avail_rxqs); 2652 pf->avail_rxqs = NULL; 2653 } 2654 } 2655 2656 /** 2657 * ice_set_pf_caps - set PFs capability flags 2658 * @pf: pointer to the PF instance 2659 */ 2660 static void ice_set_pf_caps(struct ice_pf *pf) 2661 { 2662 struct ice_hw_func_caps *func_caps = &pf->hw.func_caps; 2663 2664 clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags); 2665 if (func_caps->common_cap.dcb) 2666 set_bit(ICE_FLAG_DCB_CAPABLE, pf->flags); 2667 #ifdef CONFIG_PCI_IOV 2668 clear_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags); 2669 if (func_caps->common_cap.sr_iov_1_1) { 2670 set_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags); 2671 pf->num_vfs_supported = min_t(int, func_caps->num_allocd_vfs, 2672 ICE_MAX_VF_COUNT); 2673 } 2674 #endif /* CONFIG_PCI_IOV */ 2675 clear_bit(ICE_FLAG_RSS_ENA, pf->flags); 2676 if (func_caps->common_cap.rss_table_size) 2677 set_bit(ICE_FLAG_RSS_ENA, pf->flags); 2678 2679 pf->max_pf_txqs = func_caps->common_cap.num_txq; 2680 pf->max_pf_rxqs = func_caps->common_cap.num_rxq; 2681 } 2682 2683 /** 2684 * ice_init_pf - Initialize general software structures (struct ice_pf) 2685 * @pf: board private structure to initialize 2686 */ 2687 static int ice_init_pf(struct ice_pf *pf) 2688 { 2689 ice_set_pf_caps(pf); 2690 2691 mutex_init(&pf->sw_mutex); 2692 mutex_init(&pf->tc_mutex); 2693 2694 /* setup service timer and periodic service task */ 2695 timer_setup(&pf->serv_tmr, ice_service_timer, 0); 2696 pf->serv_tmr_period = HZ; 2697 INIT_WORK(&pf->serv_task, ice_service_task); 2698 clear_bit(__ICE_SERVICE_SCHED, pf->state); 2699 2700 mutex_init(&pf->avail_q_mutex); 2701 pf->avail_txqs = bitmap_zalloc(pf->max_pf_txqs, GFP_KERNEL); 2702 if (!pf->avail_txqs) 2703 return -ENOMEM; 2704 2705 pf->avail_rxqs = bitmap_zalloc(pf->max_pf_rxqs, GFP_KERNEL); 2706 if (!pf->avail_rxqs) { 2707 devm_kfree(ice_pf_to_dev(pf), pf->avail_txqs); 2708 pf->avail_txqs = NULL; 2709 return -ENOMEM; 2710 } 2711 2712 return 0; 2713 } 2714 2715 /** 2716 * ice_ena_msix_range - Request a range of MSIX vectors from the OS 2717 * @pf: board private structure 2718 * 2719 * compute the number of MSIX vectors required (v_budget) and request from 2720 * the OS. Return the number of vectors reserved or negative on failure 2721 */ 2722 static int ice_ena_msix_range(struct ice_pf *pf) 2723 { 2724 struct device *dev = ice_pf_to_dev(pf); 2725 int v_left, v_actual, v_budget = 0; 2726 int needed, err, i; 2727 2728 v_left = pf->hw.func_caps.common_cap.num_msix_vectors; 2729 2730 /* reserve one vector for miscellaneous handler */ 2731 needed = 1; 2732 if (v_left < needed) 2733 goto no_hw_vecs_left_err; 2734 v_budget += needed; 2735 v_left -= needed; 2736 2737 /* reserve vectors for LAN traffic */ 2738 needed = min_t(int, num_online_cpus(), v_left); 2739 if (v_left < needed) 2740 goto no_hw_vecs_left_err; 2741 pf->num_lan_msix = needed; 2742 v_budget += needed; 2743 v_left -= needed; 2744 2745 pf->msix_entries = devm_kcalloc(dev, v_budget, 2746 sizeof(*pf->msix_entries), GFP_KERNEL); 2747 2748 if (!pf->msix_entries) { 2749 err = -ENOMEM; 2750 goto exit_err; 2751 } 2752 2753 for (i = 0; i < v_budget; i++) 2754 pf->msix_entries[i].entry = i; 2755 2756 /* actually reserve the vectors */ 2757 v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries, 2758 ICE_MIN_MSIX, v_budget); 2759 2760 if (v_actual < 0) { 2761 dev_err(dev, "unable to reserve MSI-X vectors\n"); 2762 err = v_actual; 2763 goto msix_err; 2764 } 2765 2766 if (v_actual < v_budget) { 2767 dev_warn(dev, 2768 "not enough OS MSI-X vectors. requested = %d, obtained = %d\n", 2769 v_budget, v_actual); 2770 /* 2 vectors for LAN (traffic + OICR) */ 2771 #define ICE_MIN_LAN_VECS 2 2772 2773 if (v_actual < ICE_MIN_LAN_VECS) { 2774 /* error if we can't get minimum vectors */ 2775 pci_disable_msix(pf->pdev); 2776 err = -ERANGE; 2777 goto msix_err; 2778 } else { 2779 pf->num_lan_msix = ICE_MIN_LAN_VECS; 2780 } 2781 } 2782 2783 return v_actual; 2784 2785 msix_err: 2786 devm_kfree(dev, pf->msix_entries); 2787 goto exit_err; 2788 2789 no_hw_vecs_left_err: 2790 dev_err(dev, 2791 "not enough device MSI-X vectors. requested = %d, available = %d\n", 2792 needed, v_left); 2793 err = -ERANGE; 2794 exit_err: 2795 pf->num_lan_msix = 0; 2796 return err; 2797 } 2798 2799 /** 2800 * ice_dis_msix - Disable MSI-X interrupt setup in OS 2801 * @pf: board private structure 2802 */ 2803 static void ice_dis_msix(struct ice_pf *pf) 2804 { 2805 pci_disable_msix(pf->pdev); 2806 devm_kfree(ice_pf_to_dev(pf), pf->msix_entries); 2807 pf->msix_entries = NULL; 2808 } 2809 2810 /** 2811 * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme 2812 * @pf: board private structure 2813 */ 2814 static void ice_clear_interrupt_scheme(struct ice_pf *pf) 2815 { 2816 ice_dis_msix(pf); 2817 2818 if (pf->irq_tracker) { 2819 devm_kfree(ice_pf_to_dev(pf), pf->irq_tracker); 2820 pf->irq_tracker = NULL; 2821 } 2822 } 2823 2824 /** 2825 * ice_init_interrupt_scheme - Determine proper interrupt scheme 2826 * @pf: board private structure to initialize 2827 */ 2828 static int ice_init_interrupt_scheme(struct ice_pf *pf) 2829 { 2830 int vectors; 2831 2832 vectors = ice_ena_msix_range(pf); 2833 2834 if (vectors < 0) 2835 return vectors; 2836 2837 /* set up vector assignment tracking */ 2838 pf->irq_tracker = 2839 devm_kzalloc(ice_pf_to_dev(pf), sizeof(*pf->irq_tracker) + 2840 (sizeof(u16) * vectors), GFP_KERNEL); 2841 if (!pf->irq_tracker) { 2842 ice_dis_msix(pf); 2843 return -ENOMEM; 2844 } 2845 2846 /* populate SW interrupts pool with number of OS granted IRQs. */ 2847 pf->num_avail_sw_msix = vectors; 2848 pf->irq_tracker->num_entries = vectors; 2849 pf->irq_tracker->end = pf->irq_tracker->num_entries; 2850 2851 return 0; 2852 } 2853 2854 /** 2855 * ice_vsi_recfg_qs - Change the number of queues on a VSI 2856 * @vsi: VSI being changed 2857 * @new_rx: new number of Rx queues 2858 * @new_tx: new number of Tx queues 2859 * 2860 * Only change the number of queues if new_tx, or new_rx is non-0. 2861 * 2862 * Returns 0 on success. 2863 */ 2864 int ice_vsi_recfg_qs(struct ice_vsi *vsi, int new_rx, int new_tx) 2865 { 2866 struct ice_pf *pf = vsi->back; 2867 int err = 0, timeout = 50; 2868 2869 if (!new_rx && !new_tx) 2870 return -EINVAL; 2871 2872 while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) { 2873 timeout--; 2874 if (!timeout) 2875 return -EBUSY; 2876 usleep_range(1000, 2000); 2877 } 2878 2879 if (new_tx) 2880 vsi->req_txq = new_tx; 2881 if (new_rx) 2882 vsi->req_rxq = new_rx; 2883 2884 /* set for the next time the netdev is started */ 2885 if (!netif_running(vsi->netdev)) { 2886 ice_vsi_rebuild(vsi, false); 2887 dev_dbg(ice_pf_to_dev(pf), "Link is down, queue count change happens when link is brought up\n"); 2888 goto done; 2889 } 2890 2891 ice_vsi_close(vsi); 2892 ice_vsi_rebuild(vsi, false); 2893 ice_pf_dcb_recfg(pf); 2894 ice_vsi_open(vsi); 2895 done: 2896 clear_bit(__ICE_CFG_BUSY, pf->state); 2897 return err; 2898 } 2899 2900 /** 2901 * ice_log_pkg_init - log result of DDP package load 2902 * @hw: pointer to hardware info 2903 * @status: status of package load 2904 */ 2905 static void 2906 ice_log_pkg_init(struct ice_hw *hw, enum ice_status *status) 2907 { 2908 struct ice_pf *pf = (struct ice_pf *)hw->back; 2909 struct device *dev = ice_pf_to_dev(pf); 2910 2911 switch (*status) { 2912 case ICE_SUCCESS: 2913 /* The package download AdminQ command returned success because 2914 * this download succeeded or ICE_ERR_AQ_NO_WORK since there is 2915 * already a package loaded on the device. 2916 */ 2917 if (hw->pkg_ver.major == hw->active_pkg_ver.major && 2918 hw->pkg_ver.minor == hw->active_pkg_ver.minor && 2919 hw->pkg_ver.update == hw->active_pkg_ver.update && 2920 hw->pkg_ver.draft == hw->active_pkg_ver.draft && 2921 !memcmp(hw->pkg_name, hw->active_pkg_name, 2922 sizeof(hw->pkg_name))) { 2923 if (hw->pkg_dwnld_status == ICE_AQ_RC_EEXIST) 2924 dev_info(dev, 2925 "DDP package already present on device: %s version %d.%d.%d.%d\n", 2926 hw->active_pkg_name, 2927 hw->active_pkg_ver.major, 2928 hw->active_pkg_ver.minor, 2929 hw->active_pkg_ver.update, 2930 hw->active_pkg_ver.draft); 2931 else 2932 dev_info(dev, 2933 "The DDP package was successfully loaded: %s version %d.%d.%d.%d\n", 2934 hw->active_pkg_name, 2935 hw->active_pkg_ver.major, 2936 hw->active_pkg_ver.minor, 2937 hw->active_pkg_ver.update, 2938 hw->active_pkg_ver.draft); 2939 } else if (hw->active_pkg_ver.major != ICE_PKG_SUPP_VER_MAJ || 2940 hw->active_pkg_ver.minor != ICE_PKG_SUPP_VER_MNR) { 2941 dev_err(dev, 2942 "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", 2943 hw->active_pkg_name, 2944 hw->active_pkg_ver.major, 2945 hw->active_pkg_ver.minor, 2946 ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR); 2947 *status = ICE_ERR_NOT_SUPPORTED; 2948 } else if (hw->active_pkg_ver.major == ICE_PKG_SUPP_VER_MAJ && 2949 hw->active_pkg_ver.minor == ICE_PKG_SUPP_VER_MNR) { 2950 dev_info(dev, 2951 "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", 2952 hw->active_pkg_name, 2953 hw->active_pkg_ver.major, 2954 hw->active_pkg_ver.minor, 2955 hw->active_pkg_ver.update, 2956 hw->active_pkg_ver.draft, 2957 hw->pkg_name, 2958 hw->pkg_ver.major, 2959 hw->pkg_ver.minor, 2960 hw->pkg_ver.update, 2961 hw->pkg_ver.draft); 2962 } else { 2963 dev_err(dev, 2964 "An unknown error occurred when loading the DDP package, please reboot the system. If the problem persists, update the NVM. Entering Safe Mode.\n"); 2965 *status = ICE_ERR_NOT_SUPPORTED; 2966 } 2967 break; 2968 case ICE_ERR_BUF_TOO_SHORT: 2969 /* fall-through */ 2970 case ICE_ERR_CFG: 2971 dev_err(dev, 2972 "The DDP package file is invalid. Entering Safe Mode.\n"); 2973 break; 2974 case ICE_ERR_NOT_SUPPORTED: 2975 /* Package File version not supported */ 2976 if (hw->pkg_ver.major > ICE_PKG_SUPP_VER_MAJ || 2977 (hw->pkg_ver.major == ICE_PKG_SUPP_VER_MAJ && 2978 hw->pkg_ver.minor > ICE_PKG_SUPP_VER_MNR)) 2979 dev_err(dev, 2980 "The DDP package file version is higher than the driver supports. Please use an updated driver. Entering Safe Mode.\n"); 2981 else if (hw->pkg_ver.major < ICE_PKG_SUPP_VER_MAJ || 2982 (hw->pkg_ver.major == ICE_PKG_SUPP_VER_MAJ && 2983 hw->pkg_ver.minor < ICE_PKG_SUPP_VER_MNR)) 2984 dev_err(dev, 2985 "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", 2986 ICE_PKG_SUPP_VER_MAJ, ICE_PKG_SUPP_VER_MNR); 2987 break; 2988 case ICE_ERR_AQ_ERROR: 2989 switch (hw->pkg_dwnld_status) { 2990 case ICE_AQ_RC_ENOSEC: 2991 case ICE_AQ_RC_EBADSIG: 2992 dev_err(dev, 2993 "The DDP package could not be loaded because its signature is not valid. Please use a valid DDP Package. Entering Safe Mode.\n"); 2994 return; 2995 case ICE_AQ_RC_ESVN: 2996 dev_err(dev, 2997 "The DDP Package could not be loaded because its security revision is too low. Please use an updated DDP Package. Entering Safe Mode.\n"); 2998 return; 2999 case ICE_AQ_RC_EBADMAN: 3000 case ICE_AQ_RC_EBADBUF: 3001 dev_err(dev, 3002 "An error occurred on the device while loading the DDP package. The device will be reset.\n"); 3003 return; 3004 default: 3005 break; 3006 } 3007 /* fall-through */ 3008 default: 3009 dev_err(dev, 3010 "An unknown error (%d) occurred when loading the DDP package. Entering Safe Mode.\n", 3011 *status); 3012 break; 3013 } 3014 } 3015 3016 /** 3017 * ice_load_pkg - load/reload the DDP Package file 3018 * @firmware: firmware structure when firmware requested or NULL for reload 3019 * @pf: pointer to the PF instance 3020 * 3021 * Called on probe and post CORER/GLOBR rebuild to load DDP Package and 3022 * initialize HW tables. 3023 */ 3024 static void 3025 ice_load_pkg(const struct firmware *firmware, struct ice_pf *pf) 3026 { 3027 enum ice_status status = ICE_ERR_PARAM; 3028 struct device *dev = ice_pf_to_dev(pf); 3029 struct ice_hw *hw = &pf->hw; 3030 3031 /* Load DDP Package */ 3032 if (firmware && !hw->pkg_copy) { 3033 status = ice_copy_and_init_pkg(hw, firmware->data, 3034 firmware->size); 3035 ice_log_pkg_init(hw, &status); 3036 } else if (!firmware && hw->pkg_copy) { 3037 /* Reload package during rebuild after CORER/GLOBR reset */ 3038 status = ice_init_pkg(hw, hw->pkg_copy, hw->pkg_size); 3039 ice_log_pkg_init(hw, &status); 3040 } else { 3041 dev_err(dev, 3042 "The DDP package file failed to load. Entering Safe Mode.\n"); 3043 } 3044 3045 if (status) { 3046 /* Safe Mode */ 3047 clear_bit(ICE_FLAG_ADV_FEATURES, pf->flags); 3048 return; 3049 } 3050 3051 /* Successful download package is the precondition for advanced 3052 * features, hence setting the ICE_FLAG_ADV_FEATURES flag 3053 */ 3054 set_bit(ICE_FLAG_ADV_FEATURES, pf->flags); 3055 } 3056 3057 /** 3058 * ice_verify_cacheline_size - verify driver's assumption of 64 Byte cache lines 3059 * @pf: pointer to the PF structure 3060 * 3061 * There is no error returned here because the driver should be able to handle 3062 * 128 Byte cache lines, so we only print a warning in case issues are seen, 3063 * specifically with Tx. 3064 */ 3065 static void ice_verify_cacheline_size(struct ice_pf *pf) 3066 { 3067 if (rd32(&pf->hw, GLPCI_CNF2) & GLPCI_CNF2_CACHELINE_SIZE_M) 3068 dev_warn(ice_pf_to_dev(pf), 3069 "%d Byte cache line assumption is invalid, driver may have Tx timeouts!\n", 3070 ICE_CACHE_LINE_BYTES); 3071 } 3072 3073 /** 3074 * ice_send_version - update firmware with driver version 3075 * @pf: PF struct 3076 * 3077 * Returns ICE_SUCCESS on success, else error code 3078 */ 3079 static enum ice_status ice_send_version(struct ice_pf *pf) 3080 { 3081 struct ice_driver_ver dv; 3082 3083 dv.major_ver = DRV_VERSION_MAJOR; 3084 dv.minor_ver = DRV_VERSION_MINOR; 3085 dv.build_ver = DRV_VERSION_BUILD; 3086 dv.subbuild_ver = 0; 3087 strscpy((char *)dv.driver_string, DRV_VERSION, 3088 sizeof(dv.driver_string)); 3089 return ice_aq_send_driver_ver(&pf->hw, &dv, NULL); 3090 } 3091 3092 /** 3093 * ice_get_opt_fw_name - return optional firmware file name or NULL 3094 * @pf: pointer to the PF instance 3095 */ 3096 static char *ice_get_opt_fw_name(struct ice_pf *pf) 3097 { 3098 /* Optional firmware name same as default with additional dash 3099 * followed by a EUI-64 identifier (PCIe Device Serial Number) 3100 */ 3101 struct pci_dev *pdev = pf->pdev; 3102 char *opt_fw_filename = NULL; 3103 u32 dword; 3104 u8 dsn[8]; 3105 int pos; 3106 3107 /* Determine the name of the optional file using the DSN (two 3108 * dwords following the start of the DSN Capability). 3109 */ 3110 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_DSN); 3111 if (pos) { 3112 opt_fw_filename = kzalloc(NAME_MAX, GFP_KERNEL); 3113 if (!opt_fw_filename) 3114 return NULL; 3115 3116 pci_read_config_dword(pdev, pos + 4, &dword); 3117 put_unaligned_le32(dword, &dsn[0]); 3118 pci_read_config_dword(pdev, pos + 8, &dword); 3119 put_unaligned_le32(dword, &dsn[4]); 3120 snprintf(opt_fw_filename, NAME_MAX, 3121 "%sice-%02x%02x%02x%02x%02x%02x%02x%02x.pkg", 3122 ICE_DDP_PKG_PATH, 3123 dsn[7], dsn[6], dsn[5], dsn[4], 3124 dsn[3], dsn[2], dsn[1], dsn[0]); 3125 } 3126 3127 return opt_fw_filename; 3128 } 3129 3130 /** 3131 * ice_request_fw - Device initialization routine 3132 * @pf: pointer to the PF instance 3133 */ 3134 static void ice_request_fw(struct ice_pf *pf) 3135 { 3136 char *opt_fw_filename = ice_get_opt_fw_name(pf); 3137 const struct firmware *firmware = NULL; 3138 struct device *dev = ice_pf_to_dev(pf); 3139 int err = 0; 3140 3141 /* optional device-specific DDP (if present) overrides the default DDP 3142 * package file. kernel logs a debug message if the file doesn't exist, 3143 * and warning messages for other errors. 3144 */ 3145 if (opt_fw_filename) { 3146 err = firmware_request_nowarn(&firmware, opt_fw_filename, dev); 3147 if (err) { 3148 kfree(opt_fw_filename); 3149 goto dflt_pkg_load; 3150 } 3151 3152 /* request for firmware was successful. Download to device */ 3153 ice_load_pkg(firmware, pf); 3154 kfree(opt_fw_filename); 3155 release_firmware(firmware); 3156 return; 3157 } 3158 3159 dflt_pkg_load: 3160 err = request_firmware(&firmware, ICE_DDP_PKG_FILE, dev); 3161 if (err) { 3162 dev_err(dev, 3163 "The DDP package file was not found or could not be read. Entering Safe Mode\n"); 3164 return; 3165 } 3166 3167 /* request for firmware was successful. Download to device */ 3168 ice_load_pkg(firmware, pf); 3169 release_firmware(firmware); 3170 } 3171 3172 /** 3173 * ice_probe - Device initialization routine 3174 * @pdev: PCI device information struct 3175 * @ent: entry in ice_pci_tbl 3176 * 3177 * Returns 0 on success, negative on failure 3178 */ 3179 static int 3180 ice_probe(struct pci_dev *pdev, const struct pci_device_id __always_unused *ent) 3181 { 3182 struct device *dev = &pdev->dev; 3183 struct ice_pf *pf; 3184 struct ice_hw *hw; 3185 int err; 3186 3187 /* this driver uses devres, see Documentation/driver-api/driver-model/devres.rst */ 3188 err = pcim_enable_device(pdev); 3189 if (err) 3190 return err; 3191 3192 err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev)); 3193 if (err) { 3194 dev_err(dev, "BAR0 I/O map error %d\n", err); 3195 return err; 3196 } 3197 3198 pf = devm_kzalloc(dev, sizeof(*pf), GFP_KERNEL); 3199 if (!pf) 3200 return -ENOMEM; 3201 3202 /* set up for high or low DMA */ 3203 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); 3204 if (err) 3205 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); 3206 if (err) { 3207 dev_err(dev, "DMA configuration failed: 0x%x\n", err); 3208 return err; 3209 } 3210 3211 pci_enable_pcie_error_reporting(pdev); 3212 pci_set_master(pdev); 3213 3214 pf->pdev = pdev; 3215 pci_set_drvdata(pdev, pf); 3216 set_bit(__ICE_DOWN, pf->state); 3217 /* Disable service task until DOWN bit is cleared */ 3218 set_bit(__ICE_SERVICE_DIS, pf->state); 3219 3220 hw = &pf->hw; 3221 hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0]; 3222 pci_save_state(pdev); 3223 3224 hw->back = pf; 3225 hw->vendor_id = pdev->vendor; 3226 hw->device_id = pdev->device; 3227 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id); 3228 hw->subsystem_vendor_id = pdev->subsystem_vendor; 3229 hw->subsystem_device_id = pdev->subsystem_device; 3230 hw->bus.device = PCI_SLOT(pdev->devfn); 3231 hw->bus.func = PCI_FUNC(pdev->devfn); 3232 ice_set_ctrlq_len(hw); 3233 3234 pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M); 3235 3236 #ifndef CONFIG_DYNAMIC_DEBUG 3237 if (debug < -1) 3238 hw->debug_mask = debug; 3239 #endif 3240 3241 err = ice_init_hw(hw); 3242 if (err) { 3243 dev_err(dev, "ice_init_hw failed: %d\n", err); 3244 err = -EIO; 3245 goto err_exit_unroll; 3246 } 3247 3248 dev_info(dev, "firmware %d.%d.%d api %d.%d.%d nvm %s build 0x%08x\n", 3249 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch, 3250 hw->api_maj_ver, hw->api_min_ver, hw->api_patch, 3251 ice_nvm_version_str(hw), hw->fw_build); 3252 3253 ice_request_fw(pf); 3254 3255 /* if ice_request_fw fails, ICE_FLAG_ADV_FEATURES bit won't be 3256 * set in pf->state, which will cause ice_is_safe_mode to return 3257 * true 3258 */ 3259 if (ice_is_safe_mode(pf)) { 3260 dev_err(dev, 3261 "Package download failed. Advanced features disabled - Device now in Safe Mode\n"); 3262 /* we already got function/device capabilities but these don't 3263 * reflect what the driver needs to do in safe mode. Instead of 3264 * adding conditional logic everywhere to ignore these 3265 * device/function capabilities, override them. 3266 */ 3267 ice_set_safe_mode_caps(hw); 3268 } 3269 3270 err = ice_init_pf(pf); 3271 if (err) { 3272 dev_err(dev, "ice_init_pf failed: %d\n", err); 3273 goto err_init_pf_unroll; 3274 } 3275 3276 pf->num_alloc_vsi = hw->func_caps.guar_num_vsi; 3277 if (!pf->num_alloc_vsi) { 3278 err = -EIO; 3279 goto err_init_pf_unroll; 3280 } 3281 3282 pf->vsi = devm_kcalloc(dev, pf->num_alloc_vsi, sizeof(*pf->vsi), 3283 GFP_KERNEL); 3284 if (!pf->vsi) { 3285 err = -ENOMEM; 3286 goto err_init_pf_unroll; 3287 } 3288 3289 err = ice_init_interrupt_scheme(pf); 3290 if (err) { 3291 dev_err(dev, "ice_init_interrupt_scheme failed: %d\n", err); 3292 err = -EIO; 3293 goto err_init_interrupt_unroll; 3294 } 3295 3296 /* Driver is mostly up */ 3297 clear_bit(__ICE_DOWN, pf->state); 3298 3299 /* In case of MSIX we are going to setup the misc vector right here 3300 * to handle admin queue events etc. In case of legacy and MSI 3301 * the misc functionality and queue processing is combined in 3302 * the same vector and that gets setup at open. 3303 */ 3304 err = ice_req_irq_msix_misc(pf); 3305 if (err) { 3306 dev_err(dev, "setup of misc vector failed: %d\n", err); 3307 goto err_init_interrupt_unroll; 3308 } 3309 3310 /* create switch struct for the switch element created by FW on boot */ 3311 pf->first_sw = devm_kzalloc(dev, sizeof(*pf->first_sw), GFP_KERNEL); 3312 if (!pf->first_sw) { 3313 err = -ENOMEM; 3314 goto err_msix_misc_unroll; 3315 } 3316 3317 if (hw->evb_veb) 3318 pf->first_sw->bridge_mode = BRIDGE_MODE_VEB; 3319 else 3320 pf->first_sw->bridge_mode = BRIDGE_MODE_VEPA; 3321 3322 pf->first_sw->pf = pf; 3323 3324 /* record the sw_id available for later use */ 3325 pf->first_sw->sw_id = hw->port_info->sw_id; 3326 3327 err = ice_setup_pf_sw(pf); 3328 if (err) { 3329 dev_err(dev, "probe failed due to setup PF switch: %d\n", err); 3330 goto err_alloc_sw_unroll; 3331 } 3332 3333 clear_bit(__ICE_SERVICE_DIS, pf->state); 3334 3335 /* tell the firmware we are up */ 3336 err = ice_send_version(pf); 3337 if (err) { 3338 dev_err(dev, 3339 "probe failed sending driver version %s. error: %d\n", 3340 ice_drv_ver, err); 3341 goto err_alloc_sw_unroll; 3342 } 3343 3344 /* since everything is good, start the service timer */ 3345 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period)); 3346 3347 err = ice_init_link_events(pf->hw.port_info); 3348 if (err) { 3349 dev_err(dev, "ice_init_link_events failed: %d\n", err); 3350 goto err_alloc_sw_unroll; 3351 } 3352 3353 ice_verify_cacheline_size(pf); 3354 3355 /* If no DDP driven features have to be setup, return here */ 3356 if (ice_is_safe_mode(pf)) 3357 return 0; 3358 3359 /* initialize DDP driven features */ 3360 3361 /* Note: DCB init failure is non-fatal to load */ 3362 if (ice_init_pf_dcb(pf, false)) { 3363 clear_bit(ICE_FLAG_DCB_CAPABLE, pf->flags); 3364 clear_bit(ICE_FLAG_DCB_ENA, pf->flags); 3365 } else { 3366 ice_cfg_lldp_mib_change(&pf->hw, true); 3367 } 3368 3369 /* print PCI link speed and width */ 3370 pcie_print_link_status(pf->pdev); 3371 3372 return 0; 3373 3374 err_alloc_sw_unroll: 3375 set_bit(__ICE_SERVICE_DIS, pf->state); 3376 set_bit(__ICE_DOWN, pf->state); 3377 devm_kfree(dev, pf->first_sw); 3378 err_msix_misc_unroll: 3379 ice_free_irq_msix_misc(pf); 3380 err_init_interrupt_unroll: 3381 ice_clear_interrupt_scheme(pf); 3382 devm_kfree(dev, pf->vsi); 3383 err_init_pf_unroll: 3384 ice_deinit_pf(pf); 3385 ice_deinit_hw(hw); 3386 err_exit_unroll: 3387 pci_disable_pcie_error_reporting(pdev); 3388 return err; 3389 } 3390 3391 /** 3392 * ice_remove - Device removal routine 3393 * @pdev: PCI device information struct 3394 */ 3395 static void ice_remove(struct pci_dev *pdev) 3396 { 3397 struct ice_pf *pf = pci_get_drvdata(pdev); 3398 int i; 3399 3400 if (!pf) 3401 return; 3402 3403 for (i = 0; i < ICE_MAX_RESET_WAIT; i++) { 3404 if (!ice_is_reset_in_progress(pf->state)) 3405 break; 3406 msleep(100); 3407 } 3408 3409 set_bit(__ICE_DOWN, pf->state); 3410 ice_service_task_stop(pf); 3411 3412 if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) 3413 ice_free_vfs(pf); 3414 ice_vsi_release_all(pf); 3415 ice_free_irq_msix_misc(pf); 3416 ice_for_each_vsi(pf, i) { 3417 if (!pf->vsi[i]) 3418 continue; 3419 ice_vsi_free_q_vectors(pf->vsi[i]); 3420 } 3421 ice_deinit_pf(pf); 3422 ice_deinit_hw(&pf->hw); 3423 /* Issue a PFR as part of the prescribed driver unload flow. Do not 3424 * do it via ice_schedule_reset() since there is no need to rebuild 3425 * and the service task is already stopped. 3426 */ 3427 ice_reset(&pf->hw, ICE_RESET_PFR); 3428 pci_wait_for_pending_transaction(pdev); 3429 ice_clear_interrupt_scheme(pf); 3430 pci_disable_pcie_error_reporting(pdev); 3431 } 3432 3433 /** 3434 * ice_pci_err_detected - warning that PCI error has been detected 3435 * @pdev: PCI device information struct 3436 * @err: the type of PCI error 3437 * 3438 * Called to warn that something happened on the PCI bus and the error handling 3439 * is in progress. Allows the driver to gracefully prepare/handle PCI errors. 3440 */ 3441 static pci_ers_result_t 3442 ice_pci_err_detected(struct pci_dev *pdev, enum pci_channel_state err) 3443 { 3444 struct ice_pf *pf = pci_get_drvdata(pdev); 3445 3446 if (!pf) { 3447 dev_err(&pdev->dev, "%s: unrecoverable device error %d\n", 3448 __func__, err); 3449 return PCI_ERS_RESULT_DISCONNECT; 3450 } 3451 3452 if (!test_bit(__ICE_SUSPENDED, pf->state)) { 3453 ice_service_task_stop(pf); 3454 3455 if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) { 3456 set_bit(__ICE_PFR_REQ, pf->state); 3457 ice_prepare_for_reset(pf); 3458 } 3459 } 3460 3461 return PCI_ERS_RESULT_NEED_RESET; 3462 } 3463 3464 /** 3465 * ice_pci_err_slot_reset - a PCI slot reset has just happened 3466 * @pdev: PCI device information struct 3467 * 3468 * Called to determine if the driver can recover from the PCI slot reset by 3469 * using a register read to determine if the device is recoverable. 3470 */ 3471 static pci_ers_result_t ice_pci_err_slot_reset(struct pci_dev *pdev) 3472 { 3473 struct ice_pf *pf = pci_get_drvdata(pdev); 3474 pci_ers_result_t result; 3475 int err; 3476 u32 reg; 3477 3478 err = pci_enable_device_mem(pdev); 3479 if (err) { 3480 dev_err(&pdev->dev, 3481 "Cannot re-enable PCI device after reset, error %d\n", 3482 err); 3483 result = PCI_ERS_RESULT_DISCONNECT; 3484 } else { 3485 pci_set_master(pdev); 3486 pci_restore_state(pdev); 3487 pci_save_state(pdev); 3488 pci_wake_from_d3(pdev, false); 3489 3490 /* Check for life */ 3491 reg = rd32(&pf->hw, GLGEN_RTRIG); 3492 if (!reg) 3493 result = PCI_ERS_RESULT_RECOVERED; 3494 else 3495 result = PCI_ERS_RESULT_DISCONNECT; 3496 } 3497 3498 err = pci_cleanup_aer_uncorrect_error_status(pdev); 3499 if (err) 3500 dev_dbg(&pdev->dev, 3501 "pci_cleanup_aer_uncorrect_error_status failed, error %d\n", 3502 err); 3503 /* non-fatal, continue */ 3504 3505 return result; 3506 } 3507 3508 /** 3509 * ice_pci_err_resume - restart operations after PCI error recovery 3510 * @pdev: PCI device information struct 3511 * 3512 * Called to allow the driver to bring things back up after PCI error and/or 3513 * reset recovery have finished 3514 */ 3515 static void ice_pci_err_resume(struct pci_dev *pdev) 3516 { 3517 struct ice_pf *pf = pci_get_drvdata(pdev); 3518 3519 if (!pf) { 3520 dev_err(&pdev->dev, 3521 "%s failed, device is unrecoverable\n", __func__); 3522 return; 3523 } 3524 3525 if (test_bit(__ICE_SUSPENDED, pf->state)) { 3526 dev_dbg(&pdev->dev, "%s failed to resume normal operations!\n", 3527 __func__); 3528 return; 3529 } 3530 3531 ice_do_reset(pf, ICE_RESET_PFR); 3532 ice_service_task_restart(pf); 3533 mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period)); 3534 } 3535 3536 /** 3537 * ice_pci_err_reset_prepare - prepare device driver for PCI reset 3538 * @pdev: PCI device information struct 3539 */ 3540 static void ice_pci_err_reset_prepare(struct pci_dev *pdev) 3541 { 3542 struct ice_pf *pf = pci_get_drvdata(pdev); 3543 3544 if (!test_bit(__ICE_SUSPENDED, pf->state)) { 3545 ice_service_task_stop(pf); 3546 3547 if (!test_bit(__ICE_PREPARED_FOR_RESET, pf->state)) { 3548 set_bit(__ICE_PFR_REQ, pf->state); 3549 ice_prepare_for_reset(pf); 3550 } 3551 } 3552 } 3553 3554 /** 3555 * ice_pci_err_reset_done - PCI reset done, device driver reset can begin 3556 * @pdev: PCI device information struct 3557 */ 3558 static void ice_pci_err_reset_done(struct pci_dev *pdev) 3559 { 3560 ice_pci_err_resume(pdev); 3561 } 3562 3563 /* ice_pci_tbl - PCI Device ID Table 3564 * 3565 * Wildcard entries (PCI_ANY_ID) should come last 3566 * Last entry must be all 0s 3567 * 3568 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, 3569 * Class, Class Mask, private data (not used) } 3570 */ 3571 static const struct pci_device_id ice_pci_tbl[] = { 3572 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_BACKPLANE), 0 }, 3573 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_QSFP), 0 }, 3574 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E810C_SFP), 0 }, 3575 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_BACKPLANE), 0 }, 3576 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_QSFP), 0 }, 3577 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SFP), 0 }, 3578 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_10G_BASE_T), 0 }, 3579 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822C_SGMII), 0 }, 3580 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822X_BACKPLANE), 0 }, 3581 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SFP), 0 }, 3582 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_10G_BASE_T), 0 }, 3583 { PCI_VDEVICE(INTEL, ICE_DEV_ID_E822L_SGMII), 0 }, 3584 /* required last entry */ 3585 { 0, } 3586 }; 3587 MODULE_DEVICE_TABLE(pci, ice_pci_tbl); 3588 3589 static const struct pci_error_handlers ice_pci_err_handler = { 3590 .error_detected = ice_pci_err_detected, 3591 .slot_reset = ice_pci_err_slot_reset, 3592 .reset_prepare = ice_pci_err_reset_prepare, 3593 .reset_done = ice_pci_err_reset_done, 3594 .resume = ice_pci_err_resume 3595 }; 3596 3597 static struct pci_driver ice_driver = { 3598 .name = KBUILD_MODNAME, 3599 .id_table = ice_pci_tbl, 3600 .probe = ice_probe, 3601 .remove = ice_remove, 3602 .sriov_configure = ice_sriov_configure, 3603 .err_handler = &ice_pci_err_handler 3604 }; 3605 3606 /** 3607 * ice_module_init - Driver registration routine 3608 * 3609 * ice_module_init is the first routine called when the driver is 3610 * loaded. All it does is register with the PCI subsystem. 3611 */ 3612 static int __init ice_module_init(void) 3613 { 3614 int status; 3615 3616 pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver); 3617 pr_info("%s\n", ice_copyright); 3618 3619 ice_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, KBUILD_MODNAME); 3620 if (!ice_wq) { 3621 pr_err("Failed to create workqueue\n"); 3622 return -ENOMEM; 3623 } 3624 3625 status = pci_register_driver(&ice_driver); 3626 if (status) { 3627 pr_err("failed to register PCI driver, err %d\n", status); 3628 destroy_workqueue(ice_wq); 3629 } 3630 3631 return status; 3632 } 3633 module_init(ice_module_init); 3634 3635 /** 3636 * ice_module_exit - Driver exit cleanup routine 3637 * 3638 * ice_module_exit is called just before the driver is removed 3639 * from memory. 3640 */ 3641 static void __exit ice_module_exit(void) 3642 { 3643 pci_unregister_driver(&ice_driver); 3644 destroy_workqueue(ice_wq); 3645 pr_info("module unloaded\n"); 3646 } 3647 module_exit(ice_module_exit); 3648 3649 /** 3650 * ice_set_mac_address - NDO callback to set MAC address 3651 * @netdev: network interface device structure 3652 * @pi: pointer to an address structure 3653 * 3654 * Returns 0 on success, negative on failure 3655 */ 3656 static int ice_set_mac_address(struct net_device *netdev, void *pi) 3657 { 3658 struct ice_netdev_priv *np = netdev_priv(netdev); 3659 struct ice_vsi *vsi = np->vsi; 3660 struct ice_pf *pf = vsi->back; 3661 struct ice_hw *hw = &pf->hw; 3662 struct sockaddr *addr = pi; 3663 enum ice_status status; 3664 u8 flags = 0; 3665 int err = 0; 3666 u8 *mac; 3667 3668 mac = (u8 *)addr->sa_data; 3669 3670 if (!is_valid_ether_addr(mac)) 3671 return -EADDRNOTAVAIL; 3672 3673 if (ether_addr_equal(netdev->dev_addr, mac)) { 3674 netdev_warn(netdev, "already using mac %pM\n", mac); 3675 return 0; 3676 } 3677 3678 if (test_bit(__ICE_DOWN, pf->state) || 3679 ice_is_reset_in_progress(pf->state)) { 3680 netdev_err(netdev, "can't set mac %pM. device not ready\n", 3681 mac); 3682 return -EBUSY; 3683 } 3684 3685 /* When we change the MAC address we also have to change the MAC address 3686 * based filter rules that were created previously for the old MAC 3687 * address. So first, we remove the old filter rule using ice_remove_mac 3688 * and then create a new filter rule using ice_add_mac via 3689 * ice_vsi_cfg_mac_fltr function call for both add and/or remove 3690 * filters. 3691 */ 3692 status = ice_vsi_cfg_mac_fltr(vsi, netdev->dev_addr, false); 3693 if (status) { 3694 err = -EADDRNOTAVAIL; 3695 goto err_update_filters; 3696 } 3697 3698 status = ice_vsi_cfg_mac_fltr(vsi, mac, true); 3699 if (status) { 3700 err = -EADDRNOTAVAIL; 3701 goto err_update_filters; 3702 } 3703 3704 err_update_filters: 3705 if (err) { 3706 netdev_err(netdev, "can't set MAC %pM. filter update failed\n", 3707 mac); 3708 return err; 3709 } 3710 3711 /* change the netdev's MAC address */ 3712 memcpy(netdev->dev_addr, mac, netdev->addr_len); 3713 netdev_dbg(vsi->netdev, "updated MAC address to %pM\n", 3714 netdev->dev_addr); 3715 3716 /* write new MAC address to the firmware */ 3717 flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL; 3718 status = ice_aq_manage_mac_write(hw, mac, flags, NULL); 3719 if (status) { 3720 netdev_err(netdev, "can't set MAC %pM. write to firmware failed error %d\n", 3721 mac, status); 3722 } 3723 return 0; 3724 } 3725 3726 /** 3727 * ice_set_rx_mode - NDO callback to set the netdev filters 3728 * @netdev: network interface device structure 3729 */ 3730 static void ice_set_rx_mode(struct net_device *netdev) 3731 { 3732 struct ice_netdev_priv *np = netdev_priv(netdev); 3733 struct ice_vsi *vsi = np->vsi; 3734 3735 if (!vsi) 3736 return; 3737 3738 /* Set the flags to synchronize filters 3739 * ndo_set_rx_mode may be triggered even without a change in netdev 3740 * flags 3741 */ 3742 set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags); 3743 set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags); 3744 set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags); 3745 3746 /* schedule our worker thread which will take care of 3747 * applying the new filter changes 3748 */ 3749 ice_service_task_schedule(vsi->back); 3750 } 3751 3752 /** 3753 * ice_set_tx_maxrate - NDO callback to set the maximum per-queue bitrate 3754 * @netdev: network interface device structure 3755 * @queue_index: Queue ID 3756 * @maxrate: maximum bandwidth in Mbps 3757 */ 3758 static int 3759 ice_set_tx_maxrate(struct net_device *netdev, int queue_index, u32 maxrate) 3760 { 3761 struct ice_netdev_priv *np = netdev_priv(netdev); 3762 struct ice_vsi *vsi = np->vsi; 3763 enum ice_status status; 3764 u16 q_handle; 3765 u8 tc; 3766 3767 /* Validate maxrate requested is within permitted range */ 3768 if (maxrate && (maxrate > (ICE_SCHED_MAX_BW / 1000))) { 3769 netdev_err(netdev, 3770 "Invalid max rate %d specified for the queue %d\n", 3771 maxrate, queue_index); 3772 return -EINVAL; 3773 } 3774 3775 q_handle = vsi->tx_rings[queue_index]->q_handle; 3776 tc = ice_dcb_get_tc(vsi, queue_index); 3777 3778 /* Set BW back to default, when user set maxrate to 0 */ 3779 if (!maxrate) 3780 status = ice_cfg_q_bw_dflt_lmt(vsi->port_info, vsi->idx, tc, 3781 q_handle, ICE_MAX_BW); 3782 else 3783 status = ice_cfg_q_bw_lmt(vsi->port_info, vsi->idx, tc, 3784 q_handle, ICE_MAX_BW, maxrate * 1000); 3785 if (status) { 3786 netdev_err(netdev, 3787 "Unable to set Tx max rate, error %d\n", status); 3788 return -EIO; 3789 } 3790 3791 return 0; 3792 } 3793 3794 /** 3795 * ice_fdb_add - add an entry to the hardware database 3796 * @ndm: the input from the stack 3797 * @tb: pointer to array of nladdr (unused) 3798 * @dev: the net device pointer 3799 * @addr: the MAC address entry being added 3800 * @vid: VLAN ID 3801 * @flags: instructions from stack about fdb operation 3802 * @extack: netlink extended ack 3803 */ 3804 static int 3805 ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[], 3806 struct net_device *dev, const unsigned char *addr, u16 vid, 3807 u16 flags, struct netlink_ext_ack __always_unused *extack) 3808 { 3809 int err; 3810 3811 if (vid) { 3812 netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n"); 3813 return -EINVAL; 3814 } 3815 if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) { 3816 netdev_err(dev, "FDB only supports static addresses\n"); 3817 return -EINVAL; 3818 } 3819 3820 if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) 3821 err = dev_uc_add_excl(dev, addr); 3822 else if (is_multicast_ether_addr(addr)) 3823 err = dev_mc_add_excl(dev, addr); 3824 else 3825 err = -EINVAL; 3826 3827 /* Only return duplicate errors if NLM_F_EXCL is set */ 3828 if (err == -EEXIST && !(flags & NLM_F_EXCL)) 3829 err = 0; 3830 3831 return err; 3832 } 3833 3834 /** 3835 * ice_fdb_del - delete an entry from the hardware database 3836 * @ndm: the input from the stack 3837 * @tb: pointer to array of nladdr (unused) 3838 * @dev: the net device pointer 3839 * @addr: the MAC address entry being added 3840 * @vid: VLAN ID 3841 */ 3842 static int 3843 ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[], 3844 struct net_device *dev, const unsigned char *addr, 3845 __always_unused u16 vid) 3846 { 3847 int err; 3848 3849 if (ndm->ndm_state & NUD_PERMANENT) { 3850 netdev_err(dev, "FDB only supports static addresses\n"); 3851 return -EINVAL; 3852 } 3853 3854 if (is_unicast_ether_addr(addr)) 3855 err = dev_uc_del(dev, addr); 3856 else if (is_multicast_ether_addr(addr)) 3857 err = dev_mc_del(dev, addr); 3858 else 3859 err = -EINVAL; 3860 3861 return err; 3862 } 3863 3864 /** 3865 * ice_set_features - set the netdev feature flags 3866 * @netdev: ptr to the netdev being adjusted 3867 * @features: the feature set that the stack is suggesting 3868 */ 3869 static int 3870 ice_set_features(struct net_device *netdev, netdev_features_t features) 3871 { 3872 struct ice_netdev_priv *np = netdev_priv(netdev); 3873 struct ice_vsi *vsi = np->vsi; 3874 struct ice_pf *pf = vsi->back; 3875 int ret = 0; 3876 3877 /* Don't set any netdev advanced features with device in Safe Mode */ 3878 if (ice_is_safe_mode(vsi->back)) { 3879 dev_err(&vsi->back->pdev->dev, 3880 "Device is in Safe Mode - not enabling advanced netdev features\n"); 3881 return ret; 3882 } 3883 3884 /* Do not change setting during reset */ 3885 if (ice_is_reset_in_progress(pf->state)) { 3886 dev_err(&vsi->back->pdev->dev, 3887 "Device is resetting, changing advanced netdev features temporarily unavailable.\n"); 3888 return -EBUSY; 3889 } 3890 3891 /* Multiple features can be changed in one call so keep features in 3892 * separate if/else statements to guarantee each feature is checked 3893 */ 3894 if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH)) 3895 ret = ice_vsi_manage_rss_lut(vsi, true); 3896 else if (!(features & NETIF_F_RXHASH) && 3897 netdev->features & NETIF_F_RXHASH) 3898 ret = ice_vsi_manage_rss_lut(vsi, false); 3899 3900 if ((features & NETIF_F_HW_VLAN_CTAG_RX) && 3901 !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX)) 3902 ret = ice_vsi_manage_vlan_stripping(vsi, true); 3903 else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) && 3904 (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)) 3905 ret = ice_vsi_manage_vlan_stripping(vsi, false); 3906 3907 if ((features & NETIF_F_HW_VLAN_CTAG_TX) && 3908 !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) 3909 ret = ice_vsi_manage_vlan_insertion(vsi); 3910 else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) && 3911 (netdev->features & NETIF_F_HW_VLAN_CTAG_TX)) 3912 ret = ice_vsi_manage_vlan_insertion(vsi); 3913 3914 if ((features & NETIF_F_HW_VLAN_CTAG_FILTER) && 3915 !(netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)) 3916 ret = ice_cfg_vlan_pruning(vsi, true, false); 3917 else if (!(features & NETIF_F_HW_VLAN_CTAG_FILTER) && 3918 (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER)) 3919 ret = ice_cfg_vlan_pruning(vsi, false, false); 3920 3921 return ret; 3922 } 3923 3924 /** 3925 * ice_vsi_vlan_setup - Setup VLAN offload properties on a VSI 3926 * @vsi: VSI to setup VLAN properties for 3927 */ 3928 static int ice_vsi_vlan_setup(struct ice_vsi *vsi) 3929 { 3930 int ret = 0; 3931 3932 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX) 3933 ret = ice_vsi_manage_vlan_stripping(vsi, true); 3934 if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX) 3935 ret = ice_vsi_manage_vlan_insertion(vsi); 3936 3937 return ret; 3938 } 3939 3940 /** 3941 * ice_vsi_cfg - Setup the VSI 3942 * @vsi: the VSI being configured 3943 * 3944 * Return 0 on success and negative value on error 3945 */ 3946 int ice_vsi_cfg(struct ice_vsi *vsi) 3947 { 3948 int err; 3949 3950 if (vsi->netdev) { 3951 ice_set_rx_mode(vsi->netdev); 3952 3953 err = ice_vsi_vlan_setup(vsi); 3954 3955 if (err) 3956 return err; 3957 } 3958 ice_vsi_cfg_dcb_rings(vsi); 3959 3960 err = ice_vsi_cfg_lan_txqs(vsi); 3961 if (!err && ice_is_xdp_ena_vsi(vsi)) 3962 err = ice_vsi_cfg_xdp_txqs(vsi); 3963 if (!err) 3964 err = ice_vsi_cfg_rxqs(vsi); 3965 3966 return err; 3967 } 3968 3969 /** 3970 * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI 3971 * @vsi: the VSI being configured 3972 */ 3973 static void ice_napi_enable_all(struct ice_vsi *vsi) 3974 { 3975 int q_idx; 3976 3977 if (!vsi->netdev) 3978 return; 3979 3980 ice_for_each_q_vector(vsi, q_idx) { 3981 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx]; 3982 3983 if (q_vector->rx.ring || q_vector->tx.ring) 3984 napi_enable(&q_vector->napi); 3985 } 3986 } 3987 3988 /** 3989 * ice_up_complete - Finish the last steps of bringing up a connection 3990 * @vsi: The VSI being configured 3991 * 3992 * Return 0 on success and negative value on error 3993 */ 3994 static int ice_up_complete(struct ice_vsi *vsi) 3995 { 3996 struct ice_pf *pf = vsi->back; 3997 int err; 3998 3999 ice_vsi_cfg_msix(vsi); 4000 4001 /* Enable only Rx rings, Tx rings were enabled by the FW when the 4002 * Tx queue group list was configured and the context bits were 4003 * programmed using ice_vsi_cfg_txqs 4004 */ 4005 err = ice_vsi_start_rx_rings(vsi); 4006 if (err) 4007 return err; 4008 4009 clear_bit(__ICE_DOWN, vsi->state); 4010 ice_napi_enable_all(vsi); 4011 ice_vsi_ena_irq(vsi); 4012 4013 if (vsi->port_info && 4014 (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) && 4015 vsi->netdev) { 4016 ice_print_link_msg(vsi, true); 4017 netif_tx_start_all_queues(vsi->netdev); 4018 netif_carrier_on(vsi->netdev); 4019 } 4020 4021 ice_service_task_schedule(pf); 4022 4023 return 0; 4024 } 4025 4026 /** 4027 * ice_up - Bring the connection back up after being down 4028 * @vsi: VSI being configured 4029 */ 4030 int ice_up(struct ice_vsi *vsi) 4031 { 4032 int err; 4033 4034 err = ice_vsi_cfg(vsi); 4035 if (!err) 4036 err = ice_up_complete(vsi); 4037 4038 return err; 4039 } 4040 4041 /** 4042 * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring 4043 * @ring: Tx or Rx ring to read stats from 4044 * @pkts: packets stats counter 4045 * @bytes: bytes stats counter 4046 * 4047 * This function fetches stats from the ring considering the atomic operations 4048 * that needs to be performed to read u64 values in 32 bit machine. 4049 */ 4050 static void 4051 ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts, u64 *bytes) 4052 { 4053 unsigned int start; 4054 *pkts = 0; 4055 *bytes = 0; 4056 4057 if (!ring) 4058 return; 4059 do { 4060 start = u64_stats_fetch_begin_irq(&ring->syncp); 4061 *pkts = ring->stats.pkts; 4062 *bytes = ring->stats.bytes; 4063 } while (u64_stats_fetch_retry_irq(&ring->syncp, start)); 4064 } 4065 4066 /** 4067 * ice_update_vsi_ring_stats - Update VSI stats counters 4068 * @vsi: the VSI to be updated 4069 */ 4070 static void ice_update_vsi_ring_stats(struct ice_vsi *vsi) 4071 { 4072 struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats; 4073 struct ice_ring *ring; 4074 u64 pkts, bytes; 4075 int i; 4076 4077 /* reset netdev stats */ 4078 vsi_stats->tx_packets = 0; 4079 vsi_stats->tx_bytes = 0; 4080 vsi_stats->rx_packets = 0; 4081 vsi_stats->rx_bytes = 0; 4082 4083 /* reset non-netdev (extended) stats */ 4084 vsi->tx_restart = 0; 4085 vsi->tx_busy = 0; 4086 vsi->tx_linearize = 0; 4087 vsi->rx_buf_failed = 0; 4088 vsi->rx_page_failed = 0; 4089 4090 rcu_read_lock(); 4091 4092 /* update Tx rings counters */ 4093 ice_for_each_txq(vsi, i) { 4094 ring = READ_ONCE(vsi->tx_rings[i]); 4095 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes); 4096 vsi_stats->tx_packets += pkts; 4097 vsi_stats->tx_bytes += bytes; 4098 vsi->tx_restart += ring->tx_stats.restart_q; 4099 vsi->tx_busy += ring->tx_stats.tx_busy; 4100 vsi->tx_linearize += ring->tx_stats.tx_linearize; 4101 } 4102 4103 /* update Rx rings counters */ 4104 ice_for_each_rxq(vsi, i) { 4105 ring = READ_ONCE(vsi->rx_rings[i]); 4106 ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes); 4107 vsi_stats->rx_packets += pkts; 4108 vsi_stats->rx_bytes += bytes; 4109 vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed; 4110 vsi->rx_page_failed += ring->rx_stats.alloc_page_failed; 4111 } 4112 4113 rcu_read_unlock(); 4114 } 4115 4116 /** 4117 * ice_update_vsi_stats - Update VSI stats counters 4118 * @vsi: the VSI to be updated 4119 */ 4120 void ice_update_vsi_stats(struct ice_vsi *vsi) 4121 { 4122 struct rtnl_link_stats64 *cur_ns = &vsi->net_stats; 4123 struct ice_eth_stats *cur_es = &vsi->eth_stats; 4124 struct ice_pf *pf = vsi->back; 4125 4126 if (test_bit(__ICE_DOWN, vsi->state) || 4127 test_bit(__ICE_CFG_BUSY, pf->state)) 4128 return; 4129 4130 /* get stats as recorded by Tx/Rx rings */ 4131 ice_update_vsi_ring_stats(vsi); 4132 4133 /* get VSI stats as recorded by the hardware */ 4134 ice_update_eth_stats(vsi); 4135 4136 cur_ns->tx_errors = cur_es->tx_errors; 4137 cur_ns->rx_dropped = cur_es->rx_discards; 4138 cur_ns->tx_dropped = cur_es->tx_discards; 4139 cur_ns->multicast = cur_es->rx_multicast; 4140 4141 /* update some more netdev stats if this is main VSI */ 4142 if (vsi->type == ICE_VSI_PF) { 4143 cur_ns->rx_crc_errors = pf->stats.crc_errors; 4144 cur_ns->rx_errors = pf->stats.crc_errors + 4145 pf->stats.illegal_bytes; 4146 cur_ns->rx_length_errors = pf->stats.rx_len_errors; 4147 /* record drops from the port level */ 4148 cur_ns->rx_missed_errors = pf->stats.eth.rx_discards; 4149 } 4150 } 4151 4152 /** 4153 * ice_update_pf_stats - Update PF port stats counters 4154 * @pf: PF whose stats needs to be updated 4155 */ 4156 void ice_update_pf_stats(struct ice_pf *pf) 4157 { 4158 struct ice_hw_port_stats *prev_ps, *cur_ps; 4159 struct ice_hw *hw = &pf->hw; 4160 u8 port; 4161 4162 port = hw->port_info->lport; 4163 prev_ps = &pf->stats_prev; 4164 cur_ps = &pf->stats; 4165 4166 ice_stat_update40(hw, GLPRT_GORCL(port), pf->stat_prev_loaded, 4167 &prev_ps->eth.rx_bytes, 4168 &cur_ps->eth.rx_bytes); 4169 4170 ice_stat_update40(hw, GLPRT_UPRCL(port), pf->stat_prev_loaded, 4171 &prev_ps->eth.rx_unicast, 4172 &cur_ps->eth.rx_unicast); 4173 4174 ice_stat_update40(hw, GLPRT_MPRCL(port), pf->stat_prev_loaded, 4175 &prev_ps->eth.rx_multicast, 4176 &cur_ps->eth.rx_multicast); 4177 4178 ice_stat_update40(hw, GLPRT_BPRCL(port), pf->stat_prev_loaded, 4179 &prev_ps->eth.rx_broadcast, 4180 &cur_ps->eth.rx_broadcast); 4181 4182 ice_stat_update32(hw, PRTRPB_RDPC, pf->stat_prev_loaded, 4183 &prev_ps->eth.rx_discards, 4184 &cur_ps->eth.rx_discards); 4185 4186 ice_stat_update40(hw, GLPRT_GOTCL(port), pf->stat_prev_loaded, 4187 &prev_ps->eth.tx_bytes, 4188 &cur_ps->eth.tx_bytes); 4189 4190 ice_stat_update40(hw, GLPRT_UPTCL(port), pf->stat_prev_loaded, 4191 &prev_ps->eth.tx_unicast, 4192 &cur_ps->eth.tx_unicast); 4193 4194 ice_stat_update40(hw, GLPRT_MPTCL(port), pf->stat_prev_loaded, 4195 &prev_ps->eth.tx_multicast, 4196 &cur_ps->eth.tx_multicast); 4197 4198 ice_stat_update40(hw, GLPRT_BPTCL(port), pf->stat_prev_loaded, 4199 &prev_ps->eth.tx_broadcast, 4200 &cur_ps->eth.tx_broadcast); 4201 4202 ice_stat_update32(hw, GLPRT_TDOLD(port), pf->stat_prev_loaded, 4203 &prev_ps->tx_dropped_link_down, 4204 &cur_ps->tx_dropped_link_down); 4205 4206 ice_stat_update40(hw, GLPRT_PRC64L(port), pf->stat_prev_loaded, 4207 &prev_ps->rx_size_64, &cur_ps->rx_size_64); 4208 4209 ice_stat_update40(hw, GLPRT_PRC127L(port), pf->stat_prev_loaded, 4210 &prev_ps->rx_size_127, &cur_ps->rx_size_127); 4211 4212 ice_stat_update40(hw, GLPRT_PRC255L(port), pf->stat_prev_loaded, 4213 &prev_ps->rx_size_255, &cur_ps->rx_size_255); 4214 4215 ice_stat_update40(hw, GLPRT_PRC511L(port), pf->stat_prev_loaded, 4216 &prev_ps->rx_size_511, &cur_ps->rx_size_511); 4217 4218 ice_stat_update40(hw, GLPRT_PRC1023L(port), pf->stat_prev_loaded, 4219 &prev_ps->rx_size_1023, &cur_ps->rx_size_1023); 4220 4221 ice_stat_update40(hw, GLPRT_PRC1522L(port), pf->stat_prev_loaded, 4222 &prev_ps->rx_size_1522, &cur_ps->rx_size_1522); 4223 4224 ice_stat_update40(hw, GLPRT_PRC9522L(port), pf->stat_prev_loaded, 4225 &prev_ps->rx_size_big, &cur_ps->rx_size_big); 4226 4227 ice_stat_update40(hw, GLPRT_PTC64L(port), pf->stat_prev_loaded, 4228 &prev_ps->tx_size_64, &cur_ps->tx_size_64); 4229 4230 ice_stat_update40(hw, GLPRT_PTC127L(port), pf->stat_prev_loaded, 4231 &prev_ps->tx_size_127, &cur_ps->tx_size_127); 4232 4233 ice_stat_update40(hw, GLPRT_PTC255L(port), pf->stat_prev_loaded, 4234 &prev_ps->tx_size_255, &cur_ps->tx_size_255); 4235 4236 ice_stat_update40(hw, GLPRT_PTC511L(port), pf->stat_prev_loaded, 4237 &prev_ps->tx_size_511, &cur_ps->tx_size_511); 4238 4239 ice_stat_update40(hw, GLPRT_PTC1023L(port), pf->stat_prev_loaded, 4240 &prev_ps->tx_size_1023, &cur_ps->tx_size_1023); 4241 4242 ice_stat_update40(hw, GLPRT_PTC1522L(port), pf->stat_prev_loaded, 4243 &prev_ps->tx_size_1522, &cur_ps->tx_size_1522); 4244 4245 ice_stat_update40(hw, GLPRT_PTC9522L(port), pf->stat_prev_loaded, 4246 &prev_ps->tx_size_big, &cur_ps->tx_size_big); 4247 4248 ice_stat_update32(hw, GLPRT_LXONRXC(port), pf->stat_prev_loaded, 4249 &prev_ps->link_xon_rx, &cur_ps->link_xon_rx); 4250 4251 ice_stat_update32(hw, GLPRT_LXOFFRXC(port), pf->stat_prev_loaded, 4252 &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx); 4253 4254 ice_stat_update32(hw, GLPRT_LXONTXC(port), pf->stat_prev_loaded, 4255 &prev_ps->link_xon_tx, &cur_ps->link_xon_tx); 4256 4257 ice_stat_update32(hw, GLPRT_LXOFFTXC(port), pf->stat_prev_loaded, 4258 &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx); 4259 4260 ice_update_dcb_stats(pf); 4261 4262 ice_stat_update32(hw, GLPRT_CRCERRS(port), pf->stat_prev_loaded, 4263 &prev_ps->crc_errors, &cur_ps->crc_errors); 4264 4265 ice_stat_update32(hw, GLPRT_ILLERRC(port), pf->stat_prev_loaded, 4266 &prev_ps->illegal_bytes, &cur_ps->illegal_bytes); 4267 4268 ice_stat_update32(hw, GLPRT_MLFC(port), pf->stat_prev_loaded, 4269 &prev_ps->mac_local_faults, 4270 &cur_ps->mac_local_faults); 4271 4272 ice_stat_update32(hw, GLPRT_MRFC(port), pf->stat_prev_loaded, 4273 &prev_ps->mac_remote_faults, 4274 &cur_ps->mac_remote_faults); 4275 4276 ice_stat_update32(hw, GLPRT_RLEC(port), pf->stat_prev_loaded, 4277 &prev_ps->rx_len_errors, &cur_ps->rx_len_errors); 4278 4279 ice_stat_update32(hw, GLPRT_RUC(port), pf->stat_prev_loaded, 4280 &prev_ps->rx_undersize, &cur_ps->rx_undersize); 4281 4282 ice_stat_update32(hw, GLPRT_RFC(port), pf->stat_prev_loaded, 4283 &prev_ps->rx_fragments, &cur_ps->rx_fragments); 4284 4285 ice_stat_update32(hw, GLPRT_ROC(port), pf->stat_prev_loaded, 4286 &prev_ps->rx_oversize, &cur_ps->rx_oversize); 4287 4288 ice_stat_update32(hw, GLPRT_RJC(port), pf->stat_prev_loaded, 4289 &prev_ps->rx_jabber, &cur_ps->rx_jabber); 4290 4291 pf->stat_prev_loaded = true; 4292 } 4293 4294 /** 4295 * ice_get_stats64 - get statistics for network device structure 4296 * @netdev: network interface device structure 4297 * @stats: main device statistics structure 4298 */ 4299 static 4300 void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats) 4301 { 4302 struct ice_netdev_priv *np = netdev_priv(netdev); 4303 struct rtnl_link_stats64 *vsi_stats; 4304 struct ice_vsi *vsi = np->vsi; 4305 4306 vsi_stats = &vsi->net_stats; 4307 4308 if (!vsi->num_txq || !vsi->num_rxq) 4309 return; 4310 4311 /* netdev packet/byte stats come from ring counter. These are obtained 4312 * by summing up ring counters (done by ice_update_vsi_ring_stats). 4313 * But, only call the update routine and read the registers if VSI is 4314 * not down. 4315 */ 4316 if (!test_bit(__ICE_DOWN, vsi->state)) 4317 ice_update_vsi_ring_stats(vsi); 4318 stats->tx_packets = vsi_stats->tx_packets; 4319 stats->tx_bytes = vsi_stats->tx_bytes; 4320 stats->rx_packets = vsi_stats->rx_packets; 4321 stats->rx_bytes = vsi_stats->rx_bytes; 4322 4323 /* The rest of the stats can be read from the hardware but instead we 4324 * just return values that the watchdog task has already obtained from 4325 * the hardware. 4326 */ 4327 stats->multicast = vsi_stats->multicast; 4328 stats->tx_errors = vsi_stats->tx_errors; 4329 stats->tx_dropped = vsi_stats->tx_dropped; 4330 stats->rx_errors = vsi_stats->rx_errors; 4331 stats->rx_dropped = vsi_stats->rx_dropped; 4332 stats->rx_crc_errors = vsi_stats->rx_crc_errors; 4333 stats->rx_length_errors = vsi_stats->rx_length_errors; 4334 } 4335 4336 /** 4337 * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI 4338 * @vsi: VSI having NAPI disabled 4339 */ 4340 static void ice_napi_disable_all(struct ice_vsi *vsi) 4341 { 4342 int q_idx; 4343 4344 if (!vsi->netdev) 4345 return; 4346 4347 ice_for_each_q_vector(vsi, q_idx) { 4348 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx]; 4349 4350 if (q_vector->rx.ring || q_vector->tx.ring) 4351 napi_disable(&q_vector->napi); 4352 } 4353 } 4354 4355 /** 4356 * ice_down - Shutdown the connection 4357 * @vsi: The VSI being stopped 4358 */ 4359 int ice_down(struct ice_vsi *vsi) 4360 { 4361 int i, tx_err, rx_err, link_err = 0; 4362 4363 /* Caller of this function is expected to set the 4364 * vsi->state __ICE_DOWN bit 4365 */ 4366 if (vsi->netdev) { 4367 netif_carrier_off(vsi->netdev); 4368 netif_tx_disable(vsi->netdev); 4369 } 4370 4371 ice_vsi_dis_irq(vsi); 4372 4373 tx_err = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0); 4374 if (tx_err) 4375 netdev_err(vsi->netdev, 4376 "Failed stop Tx rings, VSI %d error %d\n", 4377 vsi->vsi_num, tx_err); 4378 if (!tx_err && ice_is_xdp_ena_vsi(vsi)) { 4379 tx_err = ice_vsi_stop_xdp_tx_rings(vsi); 4380 if (tx_err) 4381 netdev_err(vsi->netdev, 4382 "Failed stop XDP rings, VSI %d error %d\n", 4383 vsi->vsi_num, tx_err); 4384 } 4385 4386 rx_err = ice_vsi_stop_rx_rings(vsi); 4387 if (rx_err) 4388 netdev_err(vsi->netdev, 4389 "Failed stop Rx rings, VSI %d error %d\n", 4390 vsi->vsi_num, rx_err); 4391 4392 ice_napi_disable_all(vsi); 4393 4394 if (test_bit(ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags)) { 4395 link_err = ice_force_phys_link_state(vsi, false); 4396 if (link_err) 4397 netdev_err(vsi->netdev, 4398 "Failed to set physical link down, VSI %d error %d\n", 4399 vsi->vsi_num, link_err); 4400 } 4401 4402 ice_for_each_txq(vsi, i) 4403 ice_clean_tx_ring(vsi->tx_rings[i]); 4404 4405 ice_for_each_rxq(vsi, i) 4406 ice_clean_rx_ring(vsi->rx_rings[i]); 4407 4408 if (tx_err || rx_err || link_err) { 4409 netdev_err(vsi->netdev, 4410 "Failed to close VSI 0x%04X on switch 0x%04X\n", 4411 vsi->vsi_num, vsi->vsw->sw_id); 4412 return -EIO; 4413 } 4414 4415 return 0; 4416 } 4417 4418 /** 4419 * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources 4420 * @vsi: VSI having resources allocated 4421 * 4422 * Return 0 on success, negative on failure 4423 */ 4424 int ice_vsi_setup_tx_rings(struct ice_vsi *vsi) 4425 { 4426 int i, err = 0; 4427 4428 if (!vsi->num_txq) { 4429 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n", 4430 vsi->vsi_num); 4431 return -EINVAL; 4432 } 4433 4434 ice_for_each_txq(vsi, i) { 4435 struct ice_ring *ring = vsi->tx_rings[i]; 4436 4437 if (!ring) 4438 return -EINVAL; 4439 4440 ring->netdev = vsi->netdev; 4441 err = ice_setup_tx_ring(ring); 4442 if (err) 4443 break; 4444 } 4445 4446 return err; 4447 } 4448 4449 /** 4450 * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources 4451 * @vsi: VSI having resources allocated 4452 * 4453 * Return 0 on success, negative on failure 4454 */ 4455 int ice_vsi_setup_rx_rings(struct ice_vsi *vsi) 4456 { 4457 int i, err = 0; 4458 4459 if (!vsi->num_rxq) { 4460 dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n", 4461 vsi->vsi_num); 4462 return -EINVAL; 4463 } 4464 4465 ice_for_each_rxq(vsi, i) { 4466 struct ice_ring *ring = vsi->rx_rings[i]; 4467 4468 if (!ring) 4469 return -EINVAL; 4470 4471 ring->netdev = vsi->netdev; 4472 err = ice_setup_rx_ring(ring); 4473 if (err) 4474 break; 4475 } 4476 4477 return err; 4478 } 4479 4480 /** 4481 * ice_vsi_open - Called when a network interface is made active 4482 * @vsi: the VSI to open 4483 * 4484 * Initialization of the VSI 4485 * 4486 * Returns 0 on success, negative value on error 4487 */ 4488 static int ice_vsi_open(struct ice_vsi *vsi) 4489 { 4490 char int_name[ICE_INT_NAME_STR_LEN]; 4491 struct ice_pf *pf = vsi->back; 4492 int err; 4493 4494 /* allocate descriptors */ 4495 err = ice_vsi_setup_tx_rings(vsi); 4496 if (err) 4497 goto err_setup_tx; 4498 4499 err = ice_vsi_setup_rx_rings(vsi); 4500 if (err) 4501 goto err_setup_rx; 4502 4503 err = ice_vsi_cfg(vsi); 4504 if (err) 4505 goto err_setup_rx; 4506 4507 snprintf(int_name, sizeof(int_name) - 1, "%s-%s", 4508 dev_driver_string(ice_pf_to_dev(pf)), vsi->netdev->name); 4509 err = ice_vsi_req_irq_msix(vsi, int_name); 4510 if (err) 4511 goto err_setup_rx; 4512 4513 /* Notify the stack of the actual queue counts. */ 4514 err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq); 4515 if (err) 4516 goto err_set_qs; 4517 4518 err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq); 4519 if (err) 4520 goto err_set_qs; 4521 4522 err = ice_up_complete(vsi); 4523 if (err) 4524 goto err_up_complete; 4525 4526 return 0; 4527 4528 err_up_complete: 4529 ice_down(vsi); 4530 err_set_qs: 4531 ice_vsi_free_irq(vsi); 4532 err_setup_rx: 4533 ice_vsi_free_rx_rings(vsi); 4534 err_setup_tx: 4535 ice_vsi_free_tx_rings(vsi); 4536 4537 return err; 4538 } 4539 4540 /** 4541 * ice_vsi_release_all - Delete all VSIs 4542 * @pf: PF from which all VSIs are being removed 4543 */ 4544 static void ice_vsi_release_all(struct ice_pf *pf) 4545 { 4546 int err, i; 4547 4548 if (!pf->vsi) 4549 return; 4550 4551 ice_for_each_vsi(pf, i) { 4552 if (!pf->vsi[i]) 4553 continue; 4554 4555 err = ice_vsi_release(pf->vsi[i]); 4556 if (err) 4557 dev_dbg(ice_pf_to_dev(pf), 4558 "Failed to release pf->vsi[%d], err %d, vsi_num = %d\n", 4559 i, err, pf->vsi[i]->vsi_num); 4560 } 4561 } 4562 4563 /** 4564 * ice_vsi_rebuild_by_type - Rebuild VSI of a given type 4565 * @pf: pointer to the PF instance 4566 * @type: VSI type to rebuild 4567 * 4568 * Iterates through the pf->vsi array and rebuilds VSIs of the requested type 4569 */ 4570 static int ice_vsi_rebuild_by_type(struct ice_pf *pf, enum ice_vsi_type type) 4571 { 4572 struct device *dev = ice_pf_to_dev(pf); 4573 enum ice_status status; 4574 int i, err; 4575 4576 ice_for_each_vsi(pf, i) { 4577 struct ice_vsi *vsi = pf->vsi[i]; 4578 4579 if (!vsi || vsi->type != type) 4580 continue; 4581 4582 /* rebuild the VSI */ 4583 err = ice_vsi_rebuild(vsi, true); 4584 if (err) { 4585 dev_err(dev, 4586 "rebuild VSI failed, err %d, VSI index %d, type %s\n", 4587 err, vsi->idx, ice_vsi_type_str(type)); 4588 return err; 4589 } 4590 4591 /* replay filters for the VSI */ 4592 status = ice_replay_vsi(&pf->hw, vsi->idx); 4593 if (status) { 4594 dev_err(dev, 4595 "replay VSI failed, status %d, VSI index %d, type %s\n", 4596 status, vsi->idx, ice_vsi_type_str(type)); 4597 return -EIO; 4598 } 4599 4600 /* Re-map HW VSI number, using VSI handle that has been 4601 * previously validated in ice_replay_vsi() call above 4602 */ 4603 vsi->vsi_num = ice_get_hw_vsi_num(&pf->hw, vsi->idx); 4604 4605 /* enable the VSI */ 4606 err = ice_ena_vsi(vsi, false); 4607 if (err) { 4608 dev_err(dev, 4609 "enable VSI failed, err %d, VSI index %d, type %s\n", 4610 err, vsi->idx, ice_vsi_type_str(type)); 4611 return err; 4612 } 4613 4614 dev_info(dev, "VSI rebuilt. VSI index %d, type %s\n", vsi->idx, 4615 ice_vsi_type_str(type)); 4616 } 4617 4618 return 0; 4619 } 4620 4621 /** 4622 * ice_update_pf_netdev_link - Update PF netdev link status 4623 * @pf: pointer to the PF instance 4624 */ 4625 static void ice_update_pf_netdev_link(struct ice_pf *pf) 4626 { 4627 bool link_up; 4628 int i; 4629 4630 ice_for_each_vsi(pf, i) { 4631 struct ice_vsi *vsi = pf->vsi[i]; 4632 4633 if (!vsi || vsi->type != ICE_VSI_PF) 4634 return; 4635 4636 ice_get_link_status(pf->vsi[i]->port_info, &link_up); 4637 if (link_up) { 4638 netif_carrier_on(pf->vsi[i]->netdev); 4639 netif_tx_wake_all_queues(pf->vsi[i]->netdev); 4640 } else { 4641 netif_carrier_off(pf->vsi[i]->netdev); 4642 netif_tx_stop_all_queues(pf->vsi[i]->netdev); 4643 } 4644 } 4645 } 4646 4647 /** 4648 * ice_rebuild - rebuild after reset 4649 * @pf: PF to rebuild 4650 * @reset_type: type of reset 4651 */ 4652 static void ice_rebuild(struct ice_pf *pf, enum ice_reset_req reset_type) 4653 { 4654 struct device *dev = ice_pf_to_dev(pf); 4655 struct ice_hw *hw = &pf->hw; 4656 enum ice_status ret; 4657 int err; 4658 4659 if (test_bit(__ICE_DOWN, pf->state)) 4660 goto clear_recovery; 4661 4662 dev_dbg(dev, "rebuilding PF after reset_type=%d\n", reset_type); 4663 4664 ret = ice_init_all_ctrlq(hw); 4665 if (ret) { 4666 dev_err(dev, "control queues init failed %d\n", ret); 4667 goto err_init_ctrlq; 4668 } 4669 4670 /* if DDP was previously loaded successfully */ 4671 if (!ice_is_safe_mode(pf)) { 4672 /* reload the SW DB of filter tables */ 4673 if (reset_type == ICE_RESET_PFR) 4674 ice_fill_blk_tbls(hw); 4675 else 4676 /* Reload DDP Package after CORER/GLOBR reset */ 4677 ice_load_pkg(NULL, pf); 4678 } 4679 4680 ret = ice_clear_pf_cfg(hw); 4681 if (ret) { 4682 dev_err(dev, "clear PF configuration failed %d\n", ret); 4683 goto err_init_ctrlq; 4684 } 4685 4686 if (pf->first_sw->dflt_vsi_ena) 4687 dev_info(dev, 4688 "Clearing default VSI, re-enable after reset completes\n"); 4689 /* clear the default VSI configuration if it exists */ 4690 pf->first_sw->dflt_vsi = NULL; 4691 pf->first_sw->dflt_vsi_ena = false; 4692 4693 ice_clear_pxe_mode(hw); 4694 4695 ret = ice_get_caps(hw); 4696 if (ret) { 4697 dev_err(dev, "ice_get_caps failed %d\n", ret); 4698 goto err_init_ctrlq; 4699 } 4700 4701 err = ice_sched_init_port(hw->port_info); 4702 if (err) 4703 goto err_sched_init_port; 4704 4705 err = ice_update_link_info(hw->port_info); 4706 if (err) 4707 dev_err(dev, "Get link status error %d\n", err); 4708 4709 /* start misc vector */ 4710 err = ice_req_irq_msix_misc(pf); 4711 if (err) { 4712 dev_err(dev, "misc vector setup failed: %d\n", err); 4713 goto err_sched_init_port; 4714 } 4715 4716 if (test_bit(ICE_FLAG_DCB_ENA, pf->flags)) 4717 ice_dcb_rebuild(pf); 4718 4719 /* rebuild PF VSI */ 4720 err = ice_vsi_rebuild_by_type(pf, ICE_VSI_PF); 4721 if (err) { 4722 dev_err(dev, "PF VSI rebuild failed: %d\n", err); 4723 goto err_vsi_rebuild; 4724 } 4725 4726 if (test_bit(ICE_FLAG_SRIOV_ENA, pf->flags)) { 4727 err = ice_vsi_rebuild_by_type(pf, ICE_VSI_VF); 4728 if (err) { 4729 dev_err(dev, "VF VSI rebuild failed: %d\n", err); 4730 goto err_vsi_rebuild; 4731 } 4732 } 4733 4734 ice_update_pf_netdev_link(pf); 4735 4736 /* tell the firmware we are up */ 4737 ret = ice_send_version(pf); 4738 if (ret) { 4739 dev_err(dev, 4740 "Rebuild failed due to error sending driver version: %d\n", 4741 ret); 4742 goto err_vsi_rebuild; 4743 } 4744 4745 ice_replay_post(hw); 4746 4747 /* if we get here, reset flow is successful */ 4748 clear_bit(__ICE_RESET_FAILED, pf->state); 4749 return; 4750 4751 err_vsi_rebuild: 4752 err_sched_init_port: 4753 ice_sched_cleanup_all(hw); 4754 err_init_ctrlq: 4755 ice_shutdown_all_ctrlq(hw); 4756 set_bit(__ICE_RESET_FAILED, pf->state); 4757 clear_recovery: 4758 /* set this bit in PF state to control service task scheduling */ 4759 set_bit(__ICE_NEEDS_RESTART, pf->state); 4760 dev_err(dev, "Rebuild failed, unload and reload driver\n"); 4761 } 4762 4763 /** 4764 * ice_max_xdp_frame_size - returns the maximum allowed frame size for XDP 4765 * @vsi: Pointer to VSI structure 4766 */ 4767 static int ice_max_xdp_frame_size(struct ice_vsi *vsi) 4768 { 4769 if (PAGE_SIZE >= 8192 || test_bit(ICE_FLAG_LEGACY_RX, vsi->back->flags)) 4770 return ICE_RXBUF_2048 - XDP_PACKET_HEADROOM; 4771 else 4772 return ICE_RXBUF_3072; 4773 } 4774 4775 /** 4776 * ice_change_mtu - NDO callback to change the MTU 4777 * @netdev: network interface device structure 4778 * @new_mtu: new value for maximum frame size 4779 * 4780 * Returns 0 on success, negative on failure 4781 */ 4782 static int ice_change_mtu(struct net_device *netdev, int new_mtu) 4783 { 4784 struct ice_netdev_priv *np = netdev_priv(netdev); 4785 struct ice_vsi *vsi = np->vsi; 4786 struct ice_pf *pf = vsi->back; 4787 u8 count = 0; 4788 4789 if (new_mtu == netdev->mtu) { 4790 netdev_warn(netdev, "MTU is already %u\n", netdev->mtu); 4791 return 0; 4792 } 4793 4794 if (ice_is_xdp_ena_vsi(vsi)) { 4795 int frame_size = ice_max_xdp_frame_size(vsi); 4796 4797 if (new_mtu + ICE_ETH_PKT_HDR_PAD > frame_size) { 4798 netdev_err(netdev, "max MTU for XDP usage is %d\n", 4799 frame_size - ICE_ETH_PKT_HDR_PAD); 4800 return -EINVAL; 4801 } 4802 } 4803 4804 if (new_mtu < netdev->min_mtu) { 4805 netdev_err(netdev, "new MTU invalid. min_mtu is %d\n", 4806 netdev->min_mtu); 4807 return -EINVAL; 4808 } else if (new_mtu > netdev->max_mtu) { 4809 netdev_err(netdev, "new MTU invalid. max_mtu is %d\n", 4810 netdev->min_mtu); 4811 return -EINVAL; 4812 } 4813 /* if a reset is in progress, wait for some time for it to complete */ 4814 do { 4815 if (ice_is_reset_in_progress(pf->state)) { 4816 count++; 4817 usleep_range(1000, 2000); 4818 } else { 4819 break; 4820 } 4821 4822 } while (count < 100); 4823 4824 if (count == 100) { 4825 netdev_err(netdev, "can't change MTU. Device is busy\n"); 4826 return -EBUSY; 4827 } 4828 4829 netdev->mtu = new_mtu; 4830 4831 /* if VSI is up, bring it down and then back up */ 4832 if (!test_and_set_bit(__ICE_DOWN, vsi->state)) { 4833 int err; 4834 4835 err = ice_down(vsi); 4836 if (err) { 4837 netdev_err(netdev, "change MTU if_up err %d\n", err); 4838 return err; 4839 } 4840 4841 err = ice_up(vsi); 4842 if (err) { 4843 netdev_err(netdev, "change MTU if_up err %d\n", err); 4844 return err; 4845 } 4846 } 4847 4848 netdev_dbg(netdev, "changed MTU to %d\n", new_mtu); 4849 return 0; 4850 } 4851 4852 /** 4853 * ice_set_rss - Set RSS keys and lut 4854 * @vsi: Pointer to VSI structure 4855 * @seed: RSS hash seed 4856 * @lut: Lookup table 4857 * @lut_size: Lookup table size 4858 * 4859 * Returns 0 on success, negative on failure 4860 */ 4861 int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size) 4862 { 4863 struct ice_pf *pf = vsi->back; 4864 struct ice_hw *hw = &pf->hw; 4865 enum ice_status status; 4866 struct device *dev; 4867 4868 dev = ice_pf_to_dev(pf); 4869 if (seed) { 4870 struct ice_aqc_get_set_rss_keys *buf = 4871 (struct ice_aqc_get_set_rss_keys *)seed; 4872 4873 status = ice_aq_set_rss_key(hw, vsi->idx, buf); 4874 4875 if (status) { 4876 dev_err(dev, "Cannot set RSS key, err %d aq_err %d\n", 4877 status, hw->adminq.rq_last_status); 4878 return -EIO; 4879 } 4880 } 4881 4882 if (lut) { 4883 status = ice_aq_set_rss_lut(hw, vsi->idx, vsi->rss_lut_type, 4884 lut, lut_size); 4885 if (status) { 4886 dev_err(dev, "Cannot set RSS lut, err %d aq_err %d\n", 4887 status, hw->adminq.rq_last_status); 4888 return -EIO; 4889 } 4890 } 4891 4892 return 0; 4893 } 4894 4895 /** 4896 * ice_get_rss - Get RSS keys and lut 4897 * @vsi: Pointer to VSI structure 4898 * @seed: Buffer to store the keys 4899 * @lut: Buffer to store the lookup table entries 4900 * @lut_size: Size of buffer to store the lookup table entries 4901 * 4902 * Returns 0 on success, negative on failure 4903 */ 4904 int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size) 4905 { 4906 struct ice_pf *pf = vsi->back; 4907 struct ice_hw *hw = &pf->hw; 4908 enum ice_status status; 4909 struct device *dev; 4910 4911 dev = ice_pf_to_dev(pf); 4912 if (seed) { 4913 struct ice_aqc_get_set_rss_keys *buf = 4914 (struct ice_aqc_get_set_rss_keys *)seed; 4915 4916 status = ice_aq_get_rss_key(hw, vsi->idx, buf); 4917 if (status) { 4918 dev_err(dev, "Cannot get RSS key, err %d aq_err %d\n", 4919 status, hw->adminq.rq_last_status); 4920 return -EIO; 4921 } 4922 } 4923 4924 if (lut) { 4925 status = ice_aq_get_rss_lut(hw, vsi->idx, vsi->rss_lut_type, 4926 lut, lut_size); 4927 if (status) { 4928 dev_err(dev, "Cannot get RSS lut, err %d aq_err %d\n", 4929 status, hw->adminq.rq_last_status); 4930 return -EIO; 4931 } 4932 } 4933 4934 return 0; 4935 } 4936 4937 /** 4938 * ice_bridge_getlink - Get the hardware bridge mode 4939 * @skb: skb buff 4940 * @pid: process ID 4941 * @seq: RTNL message seq 4942 * @dev: the netdev being configured 4943 * @filter_mask: filter mask passed in 4944 * @nlflags: netlink flags passed in 4945 * 4946 * Return the bridge mode (VEB/VEPA) 4947 */ 4948 static int 4949 ice_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 4950 struct net_device *dev, u32 filter_mask, int nlflags) 4951 { 4952 struct ice_netdev_priv *np = netdev_priv(dev); 4953 struct ice_vsi *vsi = np->vsi; 4954 struct ice_pf *pf = vsi->back; 4955 u16 bmode; 4956 4957 bmode = pf->first_sw->bridge_mode; 4958 4959 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bmode, 0, 0, nlflags, 4960 filter_mask, NULL); 4961 } 4962 4963 /** 4964 * ice_vsi_update_bridge_mode - Update VSI for switching bridge mode (VEB/VEPA) 4965 * @vsi: Pointer to VSI structure 4966 * @bmode: Hardware bridge mode (VEB/VEPA) 4967 * 4968 * Returns 0 on success, negative on failure 4969 */ 4970 static int ice_vsi_update_bridge_mode(struct ice_vsi *vsi, u16 bmode) 4971 { 4972 struct ice_aqc_vsi_props *vsi_props; 4973 struct ice_hw *hw = &vsi->back->hw; 4974 struct ice_vsi_ctx *ctxt; 4975 enum ice_status status; 4976 int ret = 0; 4977 4978 vsi_props = &vsi->info; 4979 4980 ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL); 4981 if (!ctxt) 4982 return -ENOMEM; 4983 4984 ctxt->info = vsi->info; 4985 4986 if (bmode == BRIDGE_MODE_VEB) 4987 /* change from VEPA to VEB mode */ 4988 ctxt->info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB; 4989 else 4990 /* change from VEB to VEPA mode */ 4991 ctxt->info.sw_flags &= ~ICE_AQ_VSI_SW_FLAG_ALLOW_LB; 4992 ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SW_VALID); 4993 4994 status = ice_update_vsi(hw, vsi->idx, ctxt, NULL); 4995 if (status) { 4996 dev_err(&vsi->back->pdev->dev, "update VSI for bridge mode failed, bmode = %d err %d aq_err %d\n", 4997 bmode, status, hw->adminq.sq_last_status); 4998 ret = -EIO; 4999 goto out; 5000 } 5001 /* Update sw flags for book keeping */ 5002 vsi_props->sw_flags = ctxt->info.sw_flags; 5003 5004 out: 5005 kfree(ctxt); 5006 return ret; 5007 } 5008 5009 /** 5010 * ice_bridge_setlink - Set the hardware bridge mode 5011 * @dev: the netdev being configured 5012 * @nlh: RTNL message 5013 * @flags: bridge setlink flags 5014 * @extack: netlink extended ack 5015 * 5016 * Sets the bridge mode (VEB/VEPA) of the switch to which the netdev (VSI) is 5017 * hooked up to. Iterates through the PF VSI list and sets the loopback mode (if 5018 * not already set for all VSIs connected to this switch. And also update the 5019 * unicast switch filter rules for the corresponding switch of the netdev. 5020 */ 5021 static int 5022 ice_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh, 5023 u16 __always_unused flags, 5024 struct netlink_ext_ack __always_unused *extack) 5025 { 5026 struct ice_netdev_priv *np = netdev_priv(dev); 5027 struct ice_pf *pf = np->vsi->back; 5028 struct nlattr *attr, *br_spec; 5029 struct ice_hw *hw = &pf->hw; 5030 enum ice_status status; 5031 struct ice_sw *pf_sw; 5032 int rem, v, err = 0; 5033 5034 pf_sw = pf->first_sw; 5035 /* find the attribute in the netlink message */ 5036 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 5037 5038 nla_for_each_nested(attr, br_spec, rem) { 5039 __u16 mode; 5040 5041 if (nla_type(attr) != IFLA_BRIDGE_MODE) 5042 continue; 5043 mode = nla_get_u16(attr); 5044 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB) 5045 return -EINVAL; 5046 /* Continue if bridge mode is not being flipped */ 5047 if (mode == pf_sw->bridge_mode) 5048 continue; 5049 /* Iterates through the PF VSI list and update the loopback 5050 * mode of the VSI 5051 */ 5052 ice_for_each_vsi(pf, v) { 5053 if (!pf->vsi[v]) 5054 continue; 5055 err = ice_vsi_update_bridge_mode(pf->vsi[v], mode); 5056 if (err) 5057 return err; 5058 } 5059 5060 hw->evb_veb = (mode == BRIDGE_MODE_VEB); 5061 /* Update the unicast switch filter rules for the corresponding 5062 * switch of the netdev 5063 */ 5064 status = ice_update_sw_rule_bridge_mode(hw); 5065 if (status) { 5066 netdev_err(dev, "switch rule update failed, mode = %d err %d aq_err %d\n", 5067 mode, status, hw->adminq.sq_last_status); 5068 /* revert hw->evb_veb */ 5069 hw->evb_veb = (pf_sw->bridge_mode == BRIDGE_MODE_VEB); 5070 return -EIO; 5071 } 5072 5073 pf_sw->bridge_mode = mode; 5074 } 5075 5076 return 0; 5077 } 5078 5079 /** 5080 * ice_tx_timeout - Respond to a Tx Hang 5081 * @netdev: network interface device structure 5082 */ 5083 static void ice_tx_timeout(struct net_device *netdev, unsigned int txqueue) 5084 { 5085 struct ice_netdev_priv *np = netdev_priv(netdev); 5086 struct ice_ring *tx_ring = NULL; 5087 struct ice_vsi *vsi = np->vsi; 5088 struct ice_pf *pf = vsi->back; 5089 u32 i; 5090 5091 pf->tx_timeout_count++; 5092 5093 /* now that we have an index, find the tx_ring struct */ 5094 for (i = 0; i < vsi->num_txq; i++) 5095 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) 5096 if (txqueue == vsi->tx_rings[i]->q_index) { 5097 tx_ring = vsi->tx_rings[i]; 5098 break; 5099 } 5100 5101 /* Reset recovery level if enough time has elapsed after last timeout. 5102 * Also ensure no new reset action happens before next timeout period. 5103 */ 5104 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ * 20))) 5105 pf->tx_timeout_recovery_level = 1; 5106 else if (time_before(jiffies, (pf->tx_timeout_last_recovery + 5107 netdev->watchdog_timeo))) 5108 return; 5109 5110 if (tx_ring) { 5111 struct ice_hw *hw = &pf->hw; 5112 u32 head, val = 0; 5113 5114 head = (rd32(hw, QTX_COMM_HEAD(vsi->txq_map[txqueue])) & 5115 QTX_COMM_HEAD_HEAD_M) >> QTX_COMM_HEAD_HEAD_S; 5116 /* Read interrupt register */ 5117 val = rd32(hw, GLINT_DYN_CTL(tx_ring->q_vector->reg_idx)); 5118 5119 netdev_info(netdev, "tx_timeout: VSI_num: %d, Q %d, NTC: 0x%x, HW_HEAD: 0x%x, NTU: 0x%x, INT: 0x%x\n", 5120 vsi->vsi_num, txqueue, tx_ring->next_to_clean, 5121 head, tx_ring->next_to_use, val); 5122 } 5123 5124 pf->tx_timeout_last_recovery = jiffies; 5125 netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n", 5126 pf->tx_timeout_recovery_level, txqueue); 5127 5128 switch (pf->tx_timeout_recovery_level) { 5129 case 1: 5130 set_bit(__ICE_PFR_REQ, pf->state); 5131 break; 5132 case 2: 5133 set_bit(__ICE_CORER_REQ, pf->state); 5134 break; 5135 case 3: 5136 set_bit(__ICE_GLOBR_REQ, pf->state); 5137 break; 5138 default: 5139 netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in unrecoverable state.\n"); 5140 set_bit(__ICE_DOWN, pf->state); 5141 set_bit(__ICE_NEEDS_RESTART, vsi->state); 5142 set_bit(__ICE_SERVICE_DIS, pf->state); 5143 break; 5144 } 5145 5146 ice_service_task_schedule(pf); 5147 pf->tx_timeout_recovery_level++; 5148 } 5149 5150 /** 5151 * ice_open - Called when a network interface becomes active 5152 * @netdev: network interface device structure 5153 * 5154 * The open entry point is called when a network interface is made 5155 * active by the system (IFF_UP). At this point all resources needed 5156 * for transmit and receive operations are allocated, the interrupt 5157 * handler is registered with the OS, the netdev watchdog is enabled, 5158 * and the stack is notified that the interface is ready. 5159 * 5160 * Returns 0 on success, negative value on failure 5161 */ 5162 int ice_open(struct net_device *netdev) 5163 { 5164 struct ice_netdev_priv *np = netdev_priv(netdev); 5165 struct ice_vsi *vsi = np->vsi; 5166 struct ice_port_info *pi; 5167 int err; 5168 5169 if (test_bit(__ICE_NEEDS_RESTART, vsi->back->state)) { 5170 netdev_err(netdev, "driver needs to be unloaded and reloaded\n"); 5171 return -EIO; 5172 } 5173 5174 netif_carrier_off(netdev); 5175 5176 pi = vsi->port_info; 5177 err = ice_update_link_info(pi); 5178 if (err) { 5179 netdev_err(netdev, "Failed to get link info, error %d\n", 5180 err); 5181 return err; 5182 } 5183 5184 /* Set PHY if there is media, otherwise, turn off PHY */ 5185 if (pi->phy.link_info.link_info & ICE_AQ_MEDIA_AVAILABLE) { 5186 err = ice_force_phys_link_state(vsi, true); 5187 if (err) { 5188 netdev_err(netdev, 5189 "Failed to set physical link up, error %d\n", 5190 err); 5191 return err; 5192 } 5193 } else { 5194 err = ice_aq_set_link_restart_an(pi, false, NULL); 5195 if (err) { 5196 netdev_err(netdev, "Failed to set PHY state, VSI %d error %d\n", 5197 vsi->vsi_num, err); 5198 return err; 5199 } 5200 set_bit(ICE_FLAG_NO_MEDIA, vsi->back->flags); 5201 } 5202 5203 err = ice_vsi_open(vsi); 5204 if (err) 5205 netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n", 5206 vsi->vsi_num, vsi->vsw->sw_id); 5207 return err; 5208 } 5209 5210 /** 5211 * ice_stop - Disables a network interface 5212 * @netdev: network interface device structure 5213 * 5214 * The stop entry point is called when an interface is de-activated by the OS, 5215 * and the netdevice enters the DOWN state. The hardware is still under the 5216 * driver's control, but the netdev interface is disabled. 5217 * 5218 * Returns success only - not allowed to fail 5219 */ 5220 int ice_stop(struct net_device *netdev) 5221 { 5222 struct ice_netdev_priv *np = netdev_priv(netdev); 5223 struct ice_vsi *vsi = np->vsi; 5224 5225 ice_vsi_close(vsi); 5226 5227 return 0; 5228 } 5229 5230 /** 5231 * ice_features_check - Validate encapsulated packet conforms to limits 5232 * @skb: skb buffer 5233 * @netdev: This port's netdev 5234 * @features: Offload features that the stack believes apply 5235 */ 5236 static netdev_features_t 5237 ice_features_check(struct sk_buff *skb, 5238 struct net_device __always_unused *netdev, 5239 netdev_features_t features) 5240 { 5241 size_t len; 5242 5243 /* No point in doing any of this if neither checksum nor GSO are 5244 * being requested for this frame. We can rule out both by just 5245 * checking for CHECKSUM_PARTIAL 5246 */ 5247 if (skb->ip_summed != CHECKSUM_PARTIAL) 5248 return features; 5249 5250 /* We cannot support GSO if the MSS is going to be less than 5251 * 64 bytes. If it is then we need to drop support for GSO. 5252 */ 5253 if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64)) 5254 features &= ~NETIF_F_GSO_MASK; 5255 5256 len = skb_network_header(skb) - skb->data; 5257 if (len & ~(ICE_TXD_MACLEN_MAX)) 5258 goto out_rm_features; 5259 5260 len = skb_transport_header(skb) - skb_network_header(skb); 5261 if (len & ~(ICE_TXD_IPLEN_MAX)) 5262 goto out_rm_features; 5263 5264 if (skb->encapsulation) { 5265 len = skb_inner_network_header(skb) - skb_transport_header(skb); 5266 if (len & ~(ICE_TXD_L4LEN_MAX)) 5267 goto out_rm_features; 5268 5269 len = skb_inner_transport_header(skb) - 5270 skb_inner_network_header(skb); 5271 if (len & ~(ICE_TXD_IPLEN_MAX)) 5272 goto out_rm_features; 5273 } 5274 5275 return features; 5276 out_rm_features: 5277 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 5278 } 5279 5280 static const struct net_device_ops ice_netdev_safe_mode_ops = { 5281 .ndo_open = ice_open, 5282 .ndo_stop = ice_stop, 5283 .ndo_start_xmit = ice_start_xmit, 5284 .ndo_set_mac_address = ice_set_mac_address, 5285 .ndo_validate_addr = eth_validate_addr, 5286 .ndo_change_mtu = ice_change_mtu, 5287 .ndo_get_stats64 = ice_get_stats64, 5288 .ndo_tx_timeout = ice_tx_timeout, 5289 }; 5290 5291 static const struct net_device_ops ice_netdev_ops = { 5292 .ndo_open = ice_open, 5293 .ndo_stop = ice_stop, 5294 .ndo_start_xmit = ice_start_xmit, 5295 .ndo_features_check = ice_features_check, 5296 .ndo_set_rx_mode = ice_set_rx_mode, 5297 .ndo_set_mac_address = ice_set_mac_address, 5298 .ndo_validate_addr = eth_validate_addr, 5299 .ndo_change_mtu = ice_change_mtu, 5300 .ndo_get_stats64 = ice_get_stats64, 5301 .ndo_set_tx_maxrate = ice_set_tx_maxrate, 5302 .ndo_set_vf_spoofchk = ice_set_vf_spoofchk, 5303 .ndo_set_vf_mac = ice_set_vf_mac, 5304 .ndo_get_vf_config = ice_get_vf_cfg, 5305 .ndo_set_vf_trust = ice_set_vf_trust, 5306 .ndo_set_vf_vlan = ice_set_vf_port_vlan, 5307 .ndo_set_vf_link_state = ice_set_vf_link_state, 5308 .ndo_get_vf_stats = ice_get_vf_stats, 5309 .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid, 5310 .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid, 5311 .ndo_set_features = ice_set_features, 5312 .ndo_bridge_getlink = ice_bridge_getlink, 5313 .ndo_bridge_setlink = ice_bridge_setlink, 5314 .ndo_fdb_add = ice_fdb_add, 5315 .ndo_fdb_del = ice_fdb_del, 5316 .ndo_tx_timeout = ice_tx_timeout, 5317 .ndo_bpf = ice_xdp, 5318 .ndo_xdp_xmit = ice_xdp_xmit, 5319 .ndo_xsk_wakeup = ice_xsk_wakeup, 5320 }; 5321