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