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