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