1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2018, Intel Corporation. */ 3 4 /* ethtool support for ice */ 5 6 #include "ice.h" 7 #include "ice_flow.h" 8 #include "ice_fltr.h" 9 #include "ice_lib.h" 10 #include "ice_dcb_lib.h" 11 12 struct ice_stats { 13 char stat_string[ETH_GSTRING_LEN]; 14 int sizeof_stat; 15 int stat_offset; 16 }; 17 18 #define ICE_STAT(_type, _name, _stat) { \ 19 .stat_string = _name, \ 20 .sizeof_stat = sizeof_field(_type, _stat), \ 21 .stat_offset = offsetof(_type, _stat) \ 22 } 23 24 #define ICE_VSI_STAT(_name, _stat) \ 25 ICE_STAT(struct ice_vsi, _name, _stat) 26 #define ICE_PF_STAT(_name, _stat) \ 27 ICE_STAT(struct ice_pf, _name, _stat) 28 29 static int ice_q_stats_len(struct net_device *netdev) 30 { 31 struct ice_netdev_priv *np = netdev_priv(netdev); 32 33 return ((np->vsi->alloc_txq + np->vsi->alloc_rxq) * 34 (sizeof(struct ice_q_stats) / sizeof(u64))); 35 } 36 37 #define ICE_PF_STATS_LEN ARRAY_SIZE(ice_gstrings_pf_stats) 38 #define ICE_VSI_STATS_LEN ARRAY_SIZE(ice_gstrings_vsi_stats) 39 40 #define ICE_PFC_STATS_LEN ( \ 41 (sizeof_field(struct ice_pf, stats.priority_xoff_rx) + \ 42 sizeof_field(struct ice_pf, stats.priority_xon_rx) + \ 43 sizeof_field(struct ice_pf, stats.priority_xoff_tx) + \ 44 sizeof_field(struct ice_pf, stats.priority_xon_tx)) \ 45 / sizeof(u64)) 46 #define ICE_ALL_STATS_LEN(n) (ICE_PF_STATS_LEN + ICE_PFC_STATS_LEN + \ 47 ICE_VSI_STATS_LEN + ice_q_stats_len(n)) 48 49 static const struct ice_stats ice_gstrings_vsi_stats[] = { 50 ICE_VSI_STAT("rx_unicast", eth_stats.rx_unicast), 51 ICE_VSI_STAT("tx_unicast", eth_stats.tx_unicast), 52 ICE_VSI_STAT("rx_multicast", eth_stats.rx_multicast), 53 ICE_VSI_STAT("tx_multicast", eth_stats.tx_multicast), 54 ICE_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast), 55 ICE_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast), 56 ICE_VSI_STAT("rx_bytes", eth_stats.rx_bytes), 57 ICE_VSI_STAT("tx_bytes", eth_stats.tx_bytes), 58 ICE_VSI_STAT("rx_dropped", eth_stats.rx_discards), 59 ICE_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol), 60 ICE_VSI_STAT("rx_alloc_fail", rx_buf_failed), 61 ICE_VSI_STAT("rx_pg_alloc_fail", rx_page_failed), 62 ICE_VSI_STAT("tx_errors", eth_stats.tx_errors), 63 ICE_VSI_STAT("tx_linearize", tx_linearize), 64 }; 65 66 enum ice_ethtool_test_id { 67 ICE_ETH_TEST_REG = 0, 68 ICE_ETH_TEST_EEPROM, 69 ICE_ETH_TEST_INTR, 70 ICE_ETH_TEST_LOOP, 71 ICE_ETH_TEST_LINK, 72 }; 73 74 static const char ice_gstrings_test[][ETH_GSTRING_LEN] = { 75 "Register test (offline)", 76 "EEPROM test (offline)", 77 "Interrupt test (offline)", 78 "Loopback test (offline)", 79 "Link test (on/offline)", 80 }; 81 82 #define ICE_TEST_LEN (sizeof(ice_gstrings_test) / ETH_GSTRING_LEN) 83 84 /* These PF_STATs might look like duplicates of some NETDEV_STATs, 85 * but they aren't. This device is capable of supporting multiple 86 * VSIs/netdevs on a single PF. The NETDEV_STATs are for individual 87 * netdevs whereas the PF_STATs are for the physical function that's 88 * hosting these netdevs. 89 * 90 * The PF_STATs are appended to the netdev stats only when ethtool -S 91 * is queried on the base PF netdev. 92 */ 93 static const struct ice_stats ice_gstrings_pf_stats[] = { 94 ICE_PF_STAT("rx_bytes.nic", stats.eth.rx_bytes), 95 ICE_PF_STAT("tx_bytes.nic", stats.eth.tx_bytes), 96 ICE_PF_STAT("rx_unicast.nic", stats.eth.rx_unicast), 97 ICE_PF_STAT("tx_unicast.nic", stats.eth.tx_unicast), 98 ICE_PF_STAT("rx_multicast.nic", stats.eth.rx_multicast), 99 ICE_PF_STAT("tx_multicast.nic", stats.eth.tx_multicast), 100 ICE_PF_STAT("rx_broadcast.nic", stats.eth.rx_broadcast), 101 ICE_PF_STAT("tx_broadcast.nic", stats.eth.tx_broadcast), 102 ICE_PF_STAT("tx_errors.nic", stats.eth.tx_errors), 103 ICE_PF_STAT("rx_size_64.nic", stats.rx_size_64), 104 ICE_PF_STAT("tx_size_64.nic", stats.tx_size_64), 105 ICE_PF_STAT("rx_size_127.nic", stats.rx_size_127), 106 ICE_PF_STAT("tx_size_127.nic", stats.tx_size_127), 107 ICE_PF_STAT("rx_size_255.nic", stats.rx_size_255), 108 ICE_PF_STAT("tx_size_255.nic", stats.tx_size_255), 109 ICE_PF_STAT("rx_size_511.nic", stats.rx_size_511), 110 ICE_PF_STAT("tx_size_511.nic", stats.tx_size_511), 111 ICE_PF_STAT("rx_size_1023.nic", stats.rx_size_1023), 112 ICE_PF_STAT("tx_size_1023.nic", stats.tx_size_1023), 113 ICE_PF_STAT("rx_size_1522.nic", stats.rx_size_1522), 114 ICE_PF_STAT("tx_size_1522.nic", stats.tx_size_1522), 115 ICE_PF_STAT("rx_size_big.nic", stats.rx_size_big), 116 ICE_PF_STAT("tx_size_big.nic", stats.tx_size_big), 117 ICE_PF_STAT("link_xon_rx.nic", stats.link_xon_rx), 118 ICE_PF_STAT("link_xon_tx.nic", stats.link_xon_tx), 119 ICE_PF_STAT("link_xoff_rx.nic", stats.link_xoff_rx), 120 ICE_PF_STAT("link_xoff_tx.nic", stats.link_xoff_tx), 121 ICE_PF_STAT("tx_dropped_link_down.nic", stats.tx_dropped_link_down), 122 ICE_PF_STAT("rx_undersize.nic", stats.rx_undersize), 123 ICE_PF_STAT("rx_fragments.nic", stats.rx_fragments), 124 ICE_PF_STAT("rx_oversize.nic", stats.rx_oversize), 125 ICE_PF_STAT("rx_jabber.nic", stats.rx_jabber), 126 ICE_PF_STAT("rx_csum_bad.nic", hw_csum_rx_error), 127 ICE_PF_STAT("rx_length_errors.nic", stats.rx_len_errors), 128 ICE_PF_STAT("rx_dropped.nic", stats.eth.rx_discards), 129 ICE_PF_STAT("rx_crc_errors.nic", stats.crc_errors), 130 ICE_PF_STAT("illegal_bytes.nic", stats.illegal_bytes), 131 ICE_PF_STAT("mac_local_faults.nic", stats.mac_local_faults), 132 ICE_PF_STAT("mac_remote_faults.nic", stats.mac_remote_faults), 133 ICE_PF_STAT("fdir_sb_match.nic", stats.fd_sb_match), 134 ICE_PF_STAT("fdir_sb_status.nic", stats.fd_sb_status), 135 }; 136 137 static const u32 ice_regs_dump_list[] = { 138 PFGEN_STATE, 139 PRTGEN_STATUS, 140 QRX_CTRL(0), 141 QINT_TQCTL(0), 142 QINT_RQCTL(0), 143 PFINT_OICR_ENA, 144 QRX_ITR(0), 145 }; 146 147 struct ice_priv_flag { 148 char name[ETH_GSTRING_LEN]; 149 u32 bitno; /* bit position in pf->flags */ 150 }; 151 152 #define ICE_PRIV_FLAG(_name, _bitno) { \ 153 .name = _name, \ 154 .bitno = _bitno, \ 155 } 156 157 static const struct ice_priv_flag ice_gstrings_priv_flags[] = { 158 ICE_PRIV_FLAG("link-down-on-close", ICE_FLAG_LINK_DOWN_ON_CLOSE_ENA), 159 ICE_PRIV_FLAG("fw-lldp-agent", ICE_FLAG_FW_LLDP_AGENT), 160 ICE_PRIV_FLAG("vf-true-promisc-support", 161 ICE_FLAG_VF_TRUE_PROMISC_ENA), 162 ICE_PRIV_FLAG("mdd-auto-reset-vf", ICE_FLAG_MDD_AUTO_RESET_VF), 163 ICE_PRIV_FLAG("legacy-rx", ICE_FLAG_LEGACY_RX), 164 }; 165 166 #define ICE_PRIV_FLAG_ARRAY_SIZE ARRAY_SIZE(ice_gstrings_priv_flags) 167 168 static void 169 ice_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo) 170 { 171 struct ice_netdev_priv *np = netdev_priv(netdev); 172 struct ice_vsi *vsi = np->vsi; 173 struct ice_pf *pf = vsi->back; 174 struct ice_hw *hw = &pf->hw; 175 struct ice_orom_info *orom; 176 struct ice_nvm_info *nvm; 177 178 nvm = &hw->nvm; 179 orom = &nvm->orom; 180 181 strscpy(drvinfo->driver, KBUILD_MODNAME, sizeof(drvinfo->driver)); 182 183 /* Display NVM version (from which the firmware version can be 184 * determined) which contains more pertinent information. 185 */ 186 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), 187 "%x.%02x 0x%x %d.%d.%d", nvm->major_ver, nvm->minor_ver, 188 nvm->eetrack, orom->major, orom->build, orom->patch); 189 190 strscpy(drvinfo->bus_info, pci_name(pf->pdev), 191 sizeof(drvinfo->bus_info)); 192 drvinfo->n_priv_flags = ICE_PRIV_FLAG_ARRAY_SIZE; 193 } 194 195 static int ice_get_regs_len(struct net_device __always_unused *netdev) 196 { 197 return sizeof(ice_regs_dump_list); 198 } 199 200 static void 201 ice_get_regs(struct net_device *netdev, struct ethtool_regs *regs, void *p) 202 { 203 struct ice_netdev_priv *np = netdev_priv(netdev); 204 struct ice_pf *pf = np->vsi->back; 205 struct ice_hw *hw = &pf->hw; 206 u32 *regs_buf = (u32 *)p; 207 unsigned int i; 208 209 regs->version = 1; 210 211 for (i = 0; i < ARRAY_SIZE(ice_regs_dump_list); ++i) 212 regs_buf[i] = rd32(hw, ice_regs_dump_list[i]); 213 } 214 215 static u32 ice_get_msglevel(struct net_device *netdev) 216 { 217 struct ice_netdev_priv *np = netdev_priv(netdev); 218 struct ice_pf *pf = np->vsi->back; 219 220 #ifndef CONFIG_DYNAMIC_DEBUG 221 if (pf->hw.debug_mask) 222 netdev_info(netdev, "hw debug_mask: 0x%llX\n", 223 pf->hw.debug_mask); 224 #endif /* !CONFIG_DYNAMIC_DEBUG */ 225 226 return pf->msg_enable; 227 } 228 229 static void ice_set_msglevel(struct net_device *netdev, u32 data) 230 { 231 struct ice_netdev_priv *np = netdev_priv(netdev); 232 struct ice_pf *pf = np->vsi->back; 233 234 #ifndef CONFIG_DYNAMIC_DEBUG 235 if (ICE_DBG_USER & data) 236 pf->hw.debug_mask = data; 237 else 238 pf->msg_enable = data; 239 #else 240 pf->msg_enable = data; 241 #endif /* !CONFIG_DYNAMIC_DEBUG */ 242 } 243 244 static int ice_get_eeprom_len(struct net_device *netdev) 245 { 246 struct ice_netdev_priv *np = netdev_priv(netdev); 247 struct ice_pf *pf = np->vsi->back; 248 249 return (int)pf->hw.nvm.flash_size; 250 } 251 252 static int 253 ice_get_eeprom(struct net_device *netdev, struct ethtool_eeprom *eeprom, 254 u8 *bytes) 255 { 256 struct ice_netdev_priv *np = netdev_priv(netdev); 257 struct ice_vsi *vsi = np->vsi; 258 struct ice_pf *pf = vsi->back; 259 struct ice_hw *hw = &pf->hw; 260 enum ice_status status; 261 struct device *dev; 262 int ret = 0; 263 u8 *buf; 264 265 dev = ice_pf_to_dev(pf); 266 267 eeprom->magic = hw->vendor_id | (hw->device_id << 16); 268 netdev_dbg(netdev, "GEEPROM cmd 0x%08x, offset 0x%08x, len 0x%08x\n", 269 eeprom->cmd, eeprom->offset, eeprom->len); 270 271 buf = kzalloc(eeprom->len, GFP_KERNEL); 272 if (!buf) 273 return -ENOMEM; 274 275 status = ice_acquire_nvm(hw, ICE_RES_READ); 276 if (status) { 277 dev_err(dev, "ice_acquire_nvm failed, err %s aq_err %s\n", 278 ice_stat_str(status), 279 ice_aq_str(hw->adminq.sq_last_status)); 280 ret = -EIO; 281 goto out; 282 } 283 284 status = ice_read_flat_nvm(hw, eeprom->offset, &eeprom->len, buf, 285 false); 286 if (status) { 287 dev_err(dev, "ice_read_flat_nvm failed, err %s aq_err %s\n", 288 ice_stat_str(status), 289 ice_aq_str(hw->adminq.sq_last_status)); 290 ret = -EIO; 291 goto release; 292 } 293 294 memcpy(bytes, buf, eeprom->len); 295 release: 296 ice_release_nvm(hw); 297 out: 298 kfree(buf); 299 return ret; 300 } 301 302 /** 303 * ice_active_vfs - check if there are any active VFs 304 * @pf: board private structure 305 * 306 * Returns true if an active VF is found, otherwise returns false 307 */ 308 static bool ice_active_vfs(struct ice_pf *pf) 309 { 310 unsigned int i; 311 312 ice_for_each_vf(pf, i) { 313 struct ice_vf *vf = &pf->vf[i]; 314 315 if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) 316 return true; 317 } 318 319 return false; 320 } 321 322 /** 323 * ice_link_test - perform a link test on a given net_device 324 * @netdev: network interface device structure 325 * 326 * This function performs one of the self-tests required by ethtool. 327 * Returns 0 on success, non-zero on failure. 328 */ 329 static u64 ice_link_test(struct net_device *netdev) 330 { 331 struct ice_netdev_priv *np = netdev_priv(netdev); 332 enum ice_status status; 333 bool link_up = false; 334 335 netdev_info(netdev, "link test\n"); 336 status = ice_get_link_status(np->vsi->port_info, &link_up); 337 if (status) { 338 netdev_err(netdev, "link query error, status = %s\n", 339 ice_stat_str(status)); 340 return 1; 341 } 342 343 if (!link_up) 344 return 2; 345 346 return 0; 347 } 348 349 /** 350 * ice_eeprom_test - perform an EEPROM test on a given net_device 351 * @netdev: network interface device structure 352 * 353 * This function performs one of the self-tests required by ethtool. 354 * Returns 0 on success, non-zero on failure. 355 */ 356 static u64 ice_eeprom_test(struct net_device *netdev) 357 { 358 struct ice_netdev_priv *np = netdev_priv(netdev); 359 struct ice_pf *pf = np->vsi->back; 360 361 netdev_info(netdev, "EEPROM test\n"); 362 return !!(ice_nvm_validate_checksum(&pf->hw)); 363 } 364 365 /** 366 * ice_reg_pattern_test 367 * @hw: pointer to the HW struct 368 * @reg: reg to be tested 369 * @mask: bits to be touched 370 */ 371 static int ice_reg_pattern_test(struct ice_hw *hw, u32 reg, u32 mask) 372 { 373 struct ice_pf *pf = (struct ice_pf *)hw->back; 374 struct device *dev = ice_pf_to_dev(pf); 375 static const u32 patterns[] = { 376 0x5A5A5A5A, 0xA5A5A5A5, 377 0x00000000, 0xFFFFFFFF 378 }; 379 u32 val, orig_val; 380 unsigned int i; 381 382 orig_val = rd32(hw, reg); 383 for (i = 0; i < ARRAY_SIZE(patterns); ++i) { 384 u32 pattern = patterns[i] & mask; 385 386 wr32(hw, reg, pattern); 387 val = rd32(hw, reg); 388 if (val == pattern) 389 continue; 390 dev_err(dev, "%s: reg pattern test failed - reg 0x%08x pat 0x%08x val 0x%08x\n" 391 , __func__, reg, pattern, val); 392 return 1; 393 } 394 395 wr32(hw, reg, orig_val); 396 val = rd32(hw, reg); 397 if (val != orig_val) { 398 dev_err(dev, "%s: reg restore test failed - reg 0x%08x orig 0x%08x val 0x%08x\n" 399 , __func__, reg, orig_val, val); 400 return 1; 401 } 402 403 return 0; 404 } 405 406 /** 407 * ice_reg_test - perform a register test on a given net_device 408 * @netdev: network interface device structure 409 * 410 * This function performs one of the self-tests required by ethtool. 411 * Returns 0 on success, non-zero on failure. 412 */ 413 static u64 ice_reg_test(struct net_device *netdev) 414 { 415 struct ice_netdev_priv *np = netdev_priv(netdev); 416 struct ice_hw *hw = np->vsi->port_info->hw; 417 u32 int_elements = hw->func_caps.common_cap.num_msix_vectors ? 418 hw->func_caps.common_cap.num_msix_vectors - 1 : 1; 419 struct ice_diag_reg_test_info { 420 u32 address; 421 u32 mask; 422 u32 elem_num; 423 u32 elem_size; 424 } ice_reg_list[] = { 425 {GLINT_ITR(0, 0), 0x00000fff, int_elements, 426 GLINT_ITR(0, 1) - GLINT_ITR(0, 0)}, 427 {GLINT_ITR(1, 0), 0x00000fff, int_elements, 428 GLINT_ITR(1, 1) - GLINT_ITR(1, 0)}, 429 {GLINT_ITR(0, 0), 0x00000fff, int_elements, 430 GLINT_ITR(2, 1) - GLINT_ITR(2, 0)}, 431 {GLINT_CTL, 0xffff0001, 1, 0} 432 }; 433 unsigned int i; 434 435 netdev_dbg(netdev, "Register test\n"); 436 for (i = 0; i < ARRAY_SIZE(ice_reg_list); ++i) { 437 u32 j; 438 439 for (j = 0; j < ice_reg_list[i].elem_num; ++j) { 440 u32 mask = ice_reg_list[i].mask; 441 u32 reg = ice_reg_list[i].address + 442 (j * ice_reg_list[i].elem_size); 443 444 /* bail on failure (non-zero return) */ 445 if (ice_reg_pattern_test(hw, reg, mask)) 446 return 1; 447 } 448 } 449 450 return 0; 451 } 452 453 /** 454 * ice_lbtest_prepare_rings - configure Tx/Rx test rings 455 * @vsi: pointer to the VSI structure 456 * 457 * Function configures rings of a VSI for loopback test without 458 * enabling interrupts or informing the kernel about new queues. 459 * 460 * Returns 0 on success, negative on failure. 461 */ 462 static int ice_lbtest_prepare_rings(struct ice_vsi *vsi) 463 { 464 int status; 465 466 status = ice_vsi_setup_tx_rings(vsi); 467 if (status) 468 goto err_setup_tx_ring; 469 470 status = ice_vsi_setup_rx_rings(vsi); 471 if (status) 472 goto err_setup_rx_ring; 473 474 status = ice_vsi_cfg(vsi); 475 if (status) 476 goto err_setup_rx_ring; 477 478 status = ice_vsi_start_all_rx_rings(vsi); 479 if (status) 480 goto err_start_rx_ring; 481 482 return status; 483 484 err_start_rx_ring: 485 ice_vsi_free_rx_rings(vsi); 486 err_setup_rx_ring: 487 ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0); 488 err_setup_tx_ring: 489 ice_vsi_free_tx_rings(vsi); 490 491 return status; 492 } 493 494 /** 495 * ice_lbtest_disable_rings - disable Tx/Rx test rings after loopback test 496 * @vsi: pointer to the VSI structure 497 * 498 * Function stops and frees VSI rings after a loopback test. 499 * Returns 0 on success, negative on failure. 500 */ 501 static int ice_lbtest_disable_rings(struct ice_vsi *vsi) 502 { 503 int status; 504 505 status = ice_vsi_stop_lan_tx_rings(vsi, ICE_NO_RESET, 0); 506 if (status) 507 netdev_err(vsi->netdev, "Failed to stop Tx rings, VSI %d error %d\n", 508 vsi->vsi_num, status); 509 510 status = ice_vsi_stop_all_rx_rings(vsi); 511 if (status) 512 netdev_err(vsi->netdev, "Failed to stop Rx rings, VSI %d error %d\n", 513 vsi->vsi_num, status); 514 515 ice_vsi_free_tx_rings(vsi); 516 ice_vsi_free_rx_rings(vsi); 517 518 return status; 519 } 520 521 /** 522 * ice_lbtest_create_frame - create test packet 523 * @pf: pointer to the PF structure 524 * @ret_data: allocated frame buffer 525 * @size: size of the packet data 526 * 527 * Function allocates a frame with a test pattern on specific offsets. 528 * Returns 0 on success, non-zero on failure. 529 */ 530 static int ice_lbtest_create_frame(struct ice_pf *pf, u8 **ret_data, u16 size) 531 { 532 u8 *data; 533 534 if (!pf) 535 return -EINVAL; 536 537 data = devm_kzalloc(ice_pf_to_dev(pf), size, GFP_KERNEL); 538 if (!data) 539 return -ENOMEM; 540 541 /* Since the ethernet test frame should always be at least 542 * 64 bytes long, fill some octets in the payload with test data. 543 */ 544 memset(data, 0xFF, size); 545 data[32] = 0xDE; 546 data[42] = 0xAD; 547 data[44] = 0xBE; 548 data[46] = 0xEF; 549 550 *ret_data = data; 551 552 return 0; 553 } 554 555 /** 556 * ice_lbtest_check_frame - verify received loopback frame 557 * @frame: pointer to the raw packet data 558 * 559 * Function verifies received test frame with a pattern. 560 * Returns true if frame matches the pattern, false otherwise. 561 */ 562 static bool ice_lbtest_check_frame(u8 *frame) 563 { 564 /* Validate bytes of a frame under offsets chosen earlier */ 565 if (frame[32] == 0xDE && 566 frame[42] == 0xAD && 567 frame[44] == 0xBE && 568 frame[46] == 0xEF && 569 frame[48] == 0xFF) 570 return true; 571 572 return false; 573 } 574 575 /** 576 * ice_diag_send - send test frames to the test ring 577 * @tx_ring: pointer to the transmit ring 578 * @data: pointer to the raw packet data 579 * @size: size of the packet to send 580 * 581 * Function sends loopback packets on a test Tx ring. 582 */ 583 static int ice_diag_send(struct ice_ring *tx_ring, u8 *data, u16 size) 584 { 585 struct ice_tx_desc *tx_desc; 586 struct ice_tx_buf *tx_buf; 587 dma_addr_t dma; 588 u64 td_cmd; 589 590 tx_desc = ICE_TX_DESC(tx_ring, tx_ring->next_to_use); 591 tx_buf = &tx_ring->tx_buf[tx_ring->next_to_use]; 592 593 dma = dma_map_single(tx_ring->dev, data, size, DMA_TO_DEVICE); 594 if (dma_mapping_error(tx_ring->dev, dma)) 595 return -EINVAL; 596 597 tx_desc->buf_addr = cpu_to_le64(dma); 598 599 /* These flags are required for a descriptor to be pushed out */ 600 td_cmd = (u64)(ICE_TX_DESC_CMD_EOP | ICE_TX_DESC_CMD_RS); 601 tx_desc->cmd_type_offset_bsz = 602 cpu_to_le64(ICE_TX_DESC_DTYPE_DATA | 603 (td_cmd << ICE_TXD_QW1_CMD_S) | 604 ((u64)0 << ICE_TXD_QW1_OFFSET_S) | 605 ((u64)size << ICE_TXD_QW1_TX_BUF_SZ_S) | 606 ((u64)0 << ICE_TXD_QW1_L2TAG1_S)); 607 608 tx_buf->next_to_watch = tx_desc; 609 610 /* Force memory write to complete before letting h/w know 611 * there are new descriptors to fetch. 612 */ 613 wmb(); 614 615 tx_ring->next_to_use++; 616 if (tx_ring->next_to_use >= tx_ring->count) 617 tx_ring->next_to_use = 0; 618 619 writel_relaxed(tx_ring->next_to_use, tx_ring->tail); 620 621 /* Wait until the packets get transmitted to the receive queue. */ 622 usleep_range(1000, 2000); 623 dma_unmap_single(tx_ring->dev, dma, size, DMA_TO_DEVICE); 624 625 return 0; 626 } 627 628 #define ICE_LB_FRAME_SIZE 64 629 /** 630 * ice_lbtest_receive_frames - receive and verify test frames 631 * @rx_ring: pointer to the receive ring 632 * 633 * Function receives loopback packets and verify their correctness. 634 * Returns number of received valid frames. 635 */ 636 static int ice_lbtest_receive_frames(struct ice_ring *rx_ring) 637 { 638 struct ice_rx_buf *rx_buf; 639 int valid_frames, i; 640 u8 *received_buf; 641 642 valid_frames = 0; 643 644 for (i = 0; i < rx_ring->count; i++) { 645 union ice_32b_rx_flex_desc *rx_desc; 646 647 rx_desc = ICE_RX_DESC(rx_ring, i); 648 649 if (!(rx_desc->wb.status_error0 & 650 cpu_to_le16(ICE_TX_DESC_CMD_EOP | ICE_TX_DESC_CMD_RS))) 651 continue; 652 653 rx_buf = &rx_ring->rx_buf[i]; 654 received_buf = page_address(rx_buf->page) + rx_buf->page_offset; 655 656 if (ice_lbtest_check_frame(received_buf)) 657 valid_frames++; 658 } 659 660 return valid_frames; 661 } 662 663 /** 664 * ice_loopback_test - perform a loopback test on a given net_device 665 * @netdev: network interface device structure 666 * 667 * This function performs one of the self-tests required by ethtool. 668 * Returns 0 on success, non-zero on failure. 669 */ 670 static u64 ice_loopback_test(struct net_device *netdev) 671 { 672 struct ice_netdev_priv *np = netdev_priv(netdev); 673 struct ice_vsi *orig_vsi = np->vsi, *test_vsi; 674 struct ice_pf *pf = orig_vsi->back; 675 struct ice_ring *tx_ring, *rx_ring; 676 u8 broadcast[ETH_ALEN], ret = 0; 677 int num_frames, valid_frames; 678 struct device *dev; 679 u8 *tx_frame; 680 int i; 681 682 dev = ice_pf_to_dev(pf); 683 netdev_info(netdev, "loopback test\n"); 684 685 test_vsi = ice_lb_vsi_setup(pf, pf->hw.port_info); 686 if (!test_vsi) { 687 netdev_err(netdev, "Failed to create a VSI for the loopback test\n"); 688 return 1; 689 } 690 691 test_vsi->netdev = netdev; 692 tx_ring = test_vsi->tx_rings[0]; 693 rx_ring = test_vsi->rx_rings[0]; 694 695 if (ice_lbtest_prepare_rings(test_vsi)) { 696 ret = 2; 697 goto lbtest_vsi_close; 698 } 699 700 if (ice_alloc_rx_bufs(rx_ring, rx_ring->count)) { 701 ret = 3; 702 goto lbtest_rings_dis; 703 } 704 705 /* Enable MAC loopback in firmware */ 706 if (ice_aq_set_mac_loopback(&pf->hw, true, NULL)) { 707 ret = 4; 708 goto lbtest_mac_dis; 709 } 710 711 /* Test VSI needs to receive broadcast packets */ 712 eth_broadcast_addr(broadcast); 713 if (ice_fltr_add_mac(test_vsi, broadcast, ICE_FWD_TO_VSI)) { 714 ret = 5; 715 goto lbtest_mac_dis; 716 } 717 718 if (ice_lbtest_create_frame(pf, &tx_frame, ICE_LB_FRAME_SIZE)) { 719 ret = 7; 720 goto remove_mac_filters; 721 } 722 723 num_frames = min_t(int, tx_ring->count, 32); 724 for (i = 0; i < num_frames; i++) { 725 if (ice_diag_send(tx_ring, tx_frame, ICE_LB_FRAME_SIZE)) { 726 ret = 8; 727 goto lbtest_free_frame; 728 } 729 } 730 731 valid_frames = ice_lbtest_receive_frames(rx_ring); 732 if (!valid_frames) 733 ret = 9; 734 else if (valid_frames != num_frames) 735 ret = 10; 736 737 lbtest_free_frame: 738 devm_kfree(dev, tx_frame); 739 remove_mac_filters: 740 if (ice_fltr_remove_mac(test_vsi, broadcast, ICE_FWD_TO_VSI)) 741 netdev_err(netdev, "Could not remove MAC filter for the test VSI\n"); 742 lbtest_mac_dis: 743 /* Disable MAC loopback after the test is completed. */ 744 if (ice_aq_set_mac_loopback(&pf->hw, false, NULL)) 745 netdev_err(netdev, "Could not disable MAC loopback\n"); 746 lbtest_rings_dis: 747 if (ice_lbtest_disable_rings(test_vsi)) 748 netdev_err(netdev, "Could not disable test rings\n"); 749 lbtest_vsi_close: 750 test_vsi->netdev = NULL; 751 if (ice_vsi_release(test_vsi)) 752 netdev_err(netdev, "Failed to remove the test VSI\n"); 753 754 return ret; 755 } 756 757 /** 758 * ice_intr_test - perform an interrupt test on a given net_device 759 * @netdev: network interface device structure 760 * 761 * This function performs one of the self-tests required by ethtool. 762 * Returns 0 on success, non-zero on failure. 763 */ 764 static u64 ice_intr_test(struct net_device *netdev) 765 { 766 struct ice_netdev_priv *np = netdev_priv(netdev); 767 struct ice_pf *pf = np->vsi->back; 768 u16 swic_old = pf->sw_int_count; 769 770 netdev_info(netdev, "interrupt test\n"); 771 772 wr32(&pf->hw, GLINT_DYN_CTL(pf->oicr_idx), 773 GLINT_DYN_CTL_SW_ITR_INDX_M | 774 GLINT_DYN_CTL_INTENA_MSK_M | 775 GLINT_DYN_CTL_SWINT_TRIG_M); 776 777 usleep_range(1000, 2000); 778 return (swic_old == pf->sw_int_count); 779 } 780 781 /** 782 * ice_self_test - handler function for performing a self-test by ethtool 783 * @netdev: network interface device structure 784 * @eth_test: ethtool_test structure 785 * @data: required by ethtool.self_test 786 * 787 * This function is called after invoking 'ethtool -t devname' command where 788 * devname is the name of the network device on which ethtool should operate. 789 * It performs a set of self-tests to check if a device works properly. 790 */ 791 static void 792 ice_self_test(struct net_device *netdev, struct ethtool_test *eth_test, 793 u64 *data) 794 { 795 struct ice_netdev_priv *np = netdev_priv(netdev); 796 bool if_running = netif_running(netdev); 797 struct ice_pf *pf = np->vsi->back; 798 struct device *dev; 799 800 dev = ice_pf_to_dev(pf); 801 802 if (eth_test->flags == ETH_TEST_FL_OFFLINE) { 803 netdev_info(netdev, "offline testing starting\n"); 804 805 set_bit(__ICE_TESTING, pf->state); 806 807 if (ice_active_vfs(pf)) { 808 dev_warn(dev, "Please take active VFs and Netqueues offline and restart the adapter before running NIC diagnostics\n"); 809 data[ICE_ETH_TEST_REG] = 1; 810 data[ICE_ETH_TEST_EEPROM] = 1; 811 data[ICE_ETH_TEST_INTR] = 1; 812 data[ICE_ETH_TEST_LOOP] = 1; 813 data[ICE_ETH_TEST_LINK] = 1; 814 eth_test->flags |= ETH_TEST_FL_FAILED; 815 clear_bit(__ICE_TESTING, pf->state); 816 goto skip_ol_tests; 817 } 818 /* If the device is online then take it offline */ 819 if (if_running) 820 /* indicate we're in test mode */ 821 ice_stop(netdev); 822 823 data[ICE_ETH_TEST_LINK] = ice_link_test(netdev); 824 data[ICE_ETH_TEST_EEPROM] = ice_eeprom_test(netdev); 825 data[ICE_ETH_TEST_INTR] = ice_intr_test(netdev); 826 data[ICE_ETH_TEST_LOOP] = ice_loopback_test(netdev); 827 data[ICE_ETH_TEST_REG] = ice_reg_test(netdev); 828 829 if (data[ICE_ETH_TEST_LINK] || 830 data[ICE_ETH_TEST_EEPROM] || 831 data[ICE_ETH_TEST_LOOP] || 832 data[ICE_ETH_TEST_INTR] || 833 data[ICE_ETH_TEST_REG]) 834 eth_test->flags |= ETH_TEST_FL_FAILED; 835 836 clear_bit(__ICE_TESTING, pf->state); 837 838 if (if_running) { 839 int status = ice_open(netdev); 840 841 if (status) { 842 dev_err(dev, "Could not open device %s, err %d\n", 843 pf->int_name, status); 844 } 845 } 846 } else { 847 /* Online tests */ 848 netdev_info(netdev, "online testing starting\n"); 849 850 data[ICE_ETH_TEST_LINK] = ice_link_test(netdev); 851 if (data[ICE_ETH_TEST_LINK]) 852 eth_test->flags |= ETH_TEST_FL_FAILED; 853 854 /* Offline only tests, not run in online; pass by default */ 855 data[ICE_ETH_TEST_REG] = 0; 856 data[ICE_ETH_TEST_EEPROM] = 0; 857 data[ICE_ETH_TEST_INTR] = 0; 858 data[ICE_ETH_TEST_LOOP] = 0; 859 } 860 861 skip_ol_tests: 862 netdev_info(netdev, "testing finished\n"); 863 } 864 865 static void ice_get_strings(struct net_device *netdev, u32 stringset, u8 *data) 866 { 867 struct ice_netdev_priv *np = netdev_priv(netdev); 868 struct ice_vsi *vsi = np->vsi; 869 char *p = (char *)data; 870 unsigned int i; 871 872 switch (stringset) { 873 case ETH_SS_STATS: 874 for (i = 0; i < ICE_VSI_STATS_LEN; i++) { 875 snprintf(p, ETH_GSTRING_LEN, "%s", 876 ice_gstrings_vsi_stats[i].stat_string); 877 p += ETH_GSTRING_LEN; 878 } 879 880 ice_for_each_alloc_txq(vsi, i) { 881 snprintf(p, ETH_GSTRING_LEN, 882 "tx_queue_%u_packets", i); 883 p += ETH_GSTRING_LEN; 884 snprintf(p, ETH_GSTRING_LEN, "tx_queue_%u_bytes", i); 885 p += ETH_GSTRING_LEN; 886 } 887 888 ice_for_each_alloc_rxq(vsi, i) { 889 snprintf(p, ETH_GSTRING_LEN, 890 "rx_queue_%u_packets", i); 891 p += ETH_GSTRING_LEN; 892 snprintf(p, ETH_GSTRING_LEN, "rx_queue_%u_bytes", i); 893 p += ETH_GSTRING_LEN; 894 } 895 896 if (vsi->type != ICE_VSI_PF) 897 return; 898 899 for (i = 0; i < ICE_PF_STATS_LEN; i++) { 900 snprintf(p, ETH_GSTRING_LEN, "%s", 901 ice_gstrings_pf_stats[i].stat_string); 902 p += ETH_GSTRING_LEN; 903 } 904 905 for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) { 906 snprintf(p, ETH_GSTRING_LEN, 907 "tx_priority_%u_xon.nic", i); 908 p += ETH_GSTRING_LEN; 909 snprintf(p, ETH_GSTRING_LEN, 910 "tx_priority_%u_xoff.nic", i); 911 p += ETH_GSTRING_LEN; 912 } 913 for (i = 0; i < ICE_MAX_USER_PRIORITY; i++) { 914 snprintf(p, ETH_GSTRING_LEN, 915 "rx_priority_%u_xon.nic", i); 916 p += ETH_GSTRING_LEN; 917 snprintf(p, ETH_GSTRING_LEN, 918 "rx_priority_%u_xoff.nic", i); 919 p += ETH_GSTRING_LEN; 920 } 921 break; 922 case ETH_SS_TEST: 923 memcpy(data, ice_gstrings_test, ICE_TEST_LEN * ETH_GSTRING_LEN); 924 break; 925 case ETH_SS_PRIV_FLAGS: 926 for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) { 927 snprintf(p, ETH_GSTRING_LEN, "%s", 928 ice_gstrings_priv_flags[i].name); 929 p += ETH_GSTRING_LEN; 930 } 931 break; 932 default: 933 break; 934 } 935 } 936 937 static int 938 ice_set_phys_id(struct net_device *netdev, enum ethtool_phys_id_state state) 939 { 940 struct ice_netdev_priv *np = netdev_priv(netdev); 941 bool led_active; 942 943 switch (state) { 944 case ETHTOOL_ID_ACTIVE: 945 led_active = true; 946 break; 947 case ETHTOOL_ID_INACTIVE: 948 led_active = false; 949 break; 950 default: 951 return -EINVAL; 952 } 953 954 if (ice_aq_set_port_id_led(np->vsi->port_info, !led_active, NULL)) 955 return -EIO; 956 957 return 0; 958 } 959 960 /** 961 * ice_set_fec_cfg - Set link FEC options 962 * @netdev: network interface device structure 963 * @req_fec: FEC mode to configure 964 */ 965 static int ice_set_fec_cfg(struct net_device *netdev, enum ice_fec_mode req_fec) 966 { 967 struct ice_netdev_priv *np = netdev_priv(netdev); 968 struct ice_aqc_set_phy_cfg_data config = { 0 }; 969 struct ice_aqc_get_phy_caps_data *caps; 970 struct ice_vsi *vsi = np->vsi; 971 u8 sw_cfg_caps, sw_cfg_fec; 972 struct ice_port_info *pi; 973 enum ice_status status; 974 int err = 0; 975 976 pi = vsi->port_info; 977 if (!pi) 978 return -EOPNOTSUPP; 979 980 /* Changing the FEC parameters is not supported if not the PF VSI */ 981 if (vsi->type != ICE_VSI_PF) { 982 netdev_info(netdev, "Changing FEC parameters only supported for PF VSI\n"); 983 return -EOPNOTSUPP; 984 } 985 986 /* Get last SW configuration */ 987 caps = kzalloc(sizeof(*caps), GFP_KERNEL); 988 if (!caps) 989 return -ENOMEM; 990 991 status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, 992 caps, NULL); 993 if (status) { 994 err = -EAGAIN; 995 goto done; 996 } 997 998 /* Copy SW configuration returned from PHY caps to PHY config */ 999 ice_copy_phy_caps_to_cfg(caps, &config); 1000 sw_cfg_caps = caps->caps; 1001 sw_cfg_fec = caps->link_fec_options; 1002 1003 /* Get toloplogy caps, then copy PHY FEC topoloy caps to PHY config */ 1004 memset(caps, 0, sizeof(*caps)); 1005 1006 status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP, 1007 caps, NULL); 1008 if (status) { 1009 err = -EAGAIN; 1010 goto done; 1011 } 1012 1013 config.caps |= (caps->caps & ICE_AQC_PHY_EN_AUTO_FEC); 1014 config.link_fec_opt = caps->link_fec_options; 1015 1016 ice_cfg_phy_fec(&config, req_fec); 1017 1018 /* If FEC mode has changed, then set PHY configuration and enable AN. */ 1019 if ((config.caps & ICE_AQ_PHY_ENA_AUTO_FEC) != 1020 (sw_cfg_caps & ICE_AQC_PHY_EN_AUTO_FEC) || 1021 config.link_fec_opt != sw_cfg_fec) { 1022 if (caps->caps & ICE_AQC_PHY_AN_MODE) 1023 config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT; 1024 1025 status = ice_aq_set_phy_cfg(pi->hw, pi->lport, &config, NULL); 1026 1027 if (status) 1028 err = -EAGAIN; 1029 } 1030 1031 done: 1032 kfree(caps); 1033 return err; 1034 } 1035 1036 /** 1037 * ice_set_fecparam - Set FEC link options 1038 * @netdev: network interface device structure 1039 * @fecparam: Ethtool structure to retrieve FEC parameters 1040 */ 1041 static int 1042 ice_set_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam) 1043 { 1044 struct ice_netdev_priv *np = netdev_priv(netdev); 1045 struct ice_vsi *vsi = np->vsi; 1046 enum ice_fec_mode fec; 1047 1048 switch (fecparam->fec) { 1049 case ETHTOOL_FEC_AUTO: 1050 fec = ICE_FEC_AUTO; 1051 break; 1052 case ETHTOOL_FEC_RS: 1053 fec = ICE_FEC_RS; 1054 break; 1055 case ETHTOOL_FEC_BASER: 1056 fec = ICE_FEC_BASER; 1057 break; 1058 case ETHTOOL_FEC_OFF: 1059 case ETHTOOL_FEC_NONE: 1060 fec = ICE_FEC_NONE; 1061 break; 1062 default: 1063 dev_warn(ice_pf_to_dev(vsi->back), "Unsupported FEC mode: %d\n", 1064 fecparam->fec); 1065 return -EINVAL; 1066 } 1067 1068 return ice_set_fec_cfg(netdev, fec); 1069 } 1070 1071 /** 1072 * ice_get_fecparam - Get link FEC options 1073 * @netdev: network interface device structure 1074 * @fecparam: Ethtool structure to retrieve FEC parameters 1075 */ 1076 static int 1077 ice_get_fecparam(struct net_device *netdev, struct ethtool_fecparam *fecparam) 1078 { 1079 struct ice_netdev_priv *np = netdev_priv(netdev); 1080 struct ice_aqc_get_phy_caps_data *caps; 1081 struct ice_link_status *link_info; 1082 struct ice_vsi *vsi = np->vsi; 1083 struct ice_port_info *pi; 1084 enum ice_status status; 1085 int err = 0; 1086 1087 pi = vsi->port_info; 1088 1089 if (!pi) 1090 return -EOPNOTSUPP; 1091 link_info = &pi->phy.link_info; 1092 1093 /* Set FEC mode based on negotiated link info */ 1094 switch (link_info->fec_info) { 1095 case ICE_AQ_LINK_25G_KR_FEC_EN: 1096 fecparam->active_fec = ETHTOOL_FEC_BASER; 1097 break; 1098 case ICE_AQ_LINK_25G_RS_528_FEC_EN: 1099 case ICE_AQ_LINK_25G_RS_544_FEC_EN: 1100 fecparam->active_fec = ETHTOOL_FEC_RS; 1101 break; 1102 default: 1103 fecparam->active_fec = ETHTOOL_FEC_OFF; 1104 break; 1105 } 1106 1107 caps = kzalloc(sizeof(*caps), GFP_KERNEL); 1108 if (!caps) 1109 return -ENOMEM; 1110 1111 status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_TOPO_CAP, 1112 caps, NULL); 1113 if (status) { 1114 err = -EAGAIN; 1115 goto done; 1116 } 1117 1118 /* Set supported/configured FEC modes based on PHY capability */ 1119 if (caps->caps & ICE_AQC_PHY_EN_AUTO_FEC) 1120 fecparam->fec |= ETHTOOL_FEC_AUTO; 1121 if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN || 1122 caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ || 1123 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN || 1124 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ) 1125 fecparam->fec |= ETHTOOL_FEC_BASER; 1126 if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ || 1127 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ || 1128 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN) 1129 fecparam->fec |= ETHTOOL_FEC_RS; 1130 if (caps->link_fec_options == 0) 1131 fecparam->fec |= ETHTOOL_FEC_OFF; 1132 1133 done: 1134 kfree(caps); 1135 return err; 1136 } 1137 1138 /** 1139 * ice_nway_reset - restart autonegotiation 1140 * @netdev: network interface device structure 1141 */ 1142 static int ice_nway_reset(struct net_device *netdev) 1143 { 1144 struct ice_netdev_priv *np = netdev_priv(netdev); 1145 struct ice_vsi *vsi = np->vsi; 1146 struct ice_port_info *pi; 1147 enum ice_status status; 1148 1149 pi = vsi->port_info; 1150 /* If VSI state is up, then restart autoneg with link up */ 1151 if (!test_bit(__ICE_DOWN, vsi->back->state)) 1152 status = ice_aq_set_link_restart_an(pi, true, NULL); 1153 else 1154 status = ice_aq_set_link_restart_an(pi, false, NULL); 1155 1156 if (status) { 1157 netdev_info(netdev, "link restart failed, err %s aq_err %s\n", 1158 ice_stat_str(status), 1159 ice_aq_str(pi->hw->adminq.sq_last_status)); 1160 return -EIO; 1161 } 1162 1163 return 0; 1164 } 1165 1166 /** 1167 * ice_get_priv_flags - report device private flags 1168 * @netdev: network interface device structure 1169 * 1170 * The get string set count and the string set should be matched for each 1171 * flag returned. Add new strings for each flag to the ice_gstrings_priv_flags 1172 * array. 1173 * 1174 * Returns a u32 bitmap of flags. 1175 */ 1176 static u32 ice_get_priv_flags(struct net_device *netdev) 1177 { 1178 struct ice_netdev_priv *np = netdev_priv(netdev); 1179 struct ice_vsi *vsi = np->vsi; 1180 struct ice_pf *pf = vsi->back; 1181 u32 i, ret_flags = 0; 1182 1183 for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) { 1184 const struct ice_priv_flag *priv_flag; 1185 1186 priv_flag = &ice_gstrings_priv_flags[i]; 1187 1188 if (test_bit(priv_flag->bitno, pf->flags)) 1189 ret_flags |= BIT(i); 1190 } 1191 1192 return ret_flags; 1193 } 1194 1195 /** 1196 * ice_set_priv_flags - set private flags 1197 * @netdev: network interface device structure 1198 * @flags: bit flags to be set 1199 */ 1200 static int ice_set_priv_flags(struct net_device *netdev, u32 flags) 1201 { 1202 struct ice_netdev_priv *np = netdev_priv(netdev); 1203 DECLARE_BITMAP(change_flags, ICE_PF_FLAGS_NBITS); 1204 DECLARE_BITMAP(orig_flags, ICE_PF_FLAGS_NBITS); 1205 struct ice_vsi *vsi = np->vsi; 1206 struct ice_pf *pf = vsi->back; 1207 struct device *dev; 1208 int ret = 0; 1209 u32 i; 1210 1211 if (flags > BIT(ICE_PRIV_FLAG_ARRAY_SIZE)) 1212 return -EINVAL; 1213 1214 dev = ice_pf_to_dev(pf); 1215 set_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags); 1216 1217 bitmap_copy(orig_flags, pf->flags, ICE_PF_FLAGS_NBITS); 1218 for (i = 0; i < ICE_PRIV_FLAG_ARRAY_SIZE; i++) { 1219 const struct ice_priv_flag *priv_flag; 1220 1221 priv_flag = &ice_gstrings_priv_flags[i]; 1222 1223 if (flags & BIT(i)) 1224 set_bit(priv_flag->bitno, pf->flags); 1225 else 1226 clear_bit(priv_flag->bitno, pf->flags); 1227 } 1228 1229 bitmap_xor(change_flags, pf->flags, orig_flags, ICE_PF_FLAGS_NBITS); 1230 1231 if (test_bit(ICE_FLAG_FW_LLDP_AGENT, change_flags)) { 1232 if (!test_bit(ICE_FLAG_FW_LLDP_AGENT, pf->flags)) { 1233 enum ice_status status; 1234 1235 /* Disable FW LLDP engine */ 1236 status = ice_cfg_lldp_mib_change(&pf->hw, false); 1237 1238 /* If unregistering for LLDP events fails, this is 1239 * not an error state, as there shouldn't be any 1240 * events to respond to. 1241 */ 1242 if (status) 1243 dev_info(dev, "Failed to unreg for LLDP events\n"); 1244 1245 /* The AQ call to stop the FW LLDP agent will generate 1246 * an error if the agent is already stopped. 1247 */ 1248 status = ice_aq_stop_lldp(&pf->hw, true, true, NULL); 1249 if (status) 1250 dev_warn(dev, "Fail to stop LLDP agent\n"); 1251 /* Use case for having the FW LLDP agent stopped 1252 * will likely not need DCB, so failure to init is 1253 * not a concern of ethtool 1254 */ 1255 status = ice_init_pf_dcb(pf, true); 1256 if (status) 1257 dev_warn(dev, "Fail to init DCB\n"); 1258 } else { 1259 enum ice_status status; 1260 bool dcbx_agent_status; 1261 1262 /* AQ command to start FW LLDP agent will return an 1263 * error if the agent is already started 1264 */ 1265 status = ice_aq_start_lldp(&pf->hw, true, NULL); 1266 if (status) 1267 dev_warn(dev, "Fail to start LLDP Agent\n"); 1268 1269 /* AQ command to start FW DCBX agent will fail if 1270 * the agent is already started 1271 */ 1272 status = ice_aq_start_stop_dcbx(&pf->hw, true, 1273 &dcbx_agent_status, 1274 NULL); 1275 if (status) 1276 dev_dbg(dev, "Failed to start FW DCBX\n"); 1277 1278 dev_info(dev, "FW DCBX agent is %s\n", 1279 dcbx_agent_status ? "ACTIVE" : "DISABLED"); 1280 1281 /* Failure to configure MIB change or init DCB is not 1282 * relevant to ethtool. Print notification that 1283 * registration/init failed but do not return error 1284 * state to ethtool 1285 */ 1286 status = ice_init_pf_dcb(pf, true); 1287 if (status) 1288 dev_dbg(dev, "Fail to init DCB\n"); 1289 1290 /* Remove rule to direct LLDP packets to default VSI. 1291 * The FW LLDP engine will now be consuming them. 1292 */ 1293 ice_cfg_sw_lldp(vsi, false, false); 1294 1295 /* Register for MIB change events */ 1296 status = ice_cfg_lldp_mib_change(&pf->hw, true); 1297 if (status) 1298 dev_dbg(dev, "Fail to enable MIB change events\n"); 1299 1300 ice_nway_reset(netdev); 1301 } 1302 } 1303 if (test_bit(ICE_FLAG_LEGACY_RX, change_flags)) { 1304 /* down and up VSI so that changes of Rx cfg are reflected. */ 1305 ice_down(vsi); 1306 ice_up(vsi); 1307 } 1308 /* don't allow modification of this flag when a single VF is in 1309 * promiscuous mode because it's not supported 1310 */ 1311 if (test_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, change_flags) && 1312 ice_is_any_vf_in_promisc(pf)) { 1313 dev_err(dev, "Changing vf-true-promisc-support flag while VF(s) are in promiscuous mode not supported\n"); 1314 /* toggle bit back to previous state */ 1315 change_bit(ICE_FLAG_VF_TRUE_PROMISC_ENA, pf->flags); 1316 ret = -EAGAIN; 1317 } 1318 clear_bit(ICE_FLAG_ETHTOOL_CTXT, pf->flags); 1319 return ret; 1320 } 1321 1322 static int ice_get_sset_count(struct net_device *netdev, int sset) 1323 { 1324 switch (sset) { 1325 case ETH_SS_STATS: 1326 /* The number (and order) of strings reported *must* remain 1327 * constant for a given netdevice. This function must not 1328 * report a different number based on run time parameters 1329 * (such as the number of queues in use, or the setting of 1330 * a private ethtool flag). This is due to the nature of the 1331 * ethtool stats API. 1332 * 1333 * Userspace programs such as ethtool must make 3 separate 1334 * ioctl requests, one for size, one for the strings, and 1335 * finally one for the stats. Since these cross into 1336 * userspace, changes to the number or size could result in 1337 * undefined memory access or incorrect string<->value 1338 * correlations for statistics. 1339 * 1340 * Even if it appears to be safe, changes to the size or 1341 * order of strings will suffer from race conditions and are 1342 * not safe. 1343 */ 1344 return ICE_ALL_STATS_LEN(netdev); 1345 case ETH_SS_TEST: 1346 return ICE_TEST_LEN; 1347 case ETH_SS_PRIV_FLAGS: 1348 return ICE_PRIV_FLAG_ARRAY_SIZE; 1349 default: 1350 return -EOPNOTSUPP; 1351 } 1352 } 1353 1354 static void 1355 ice_get_ethtool_stats(struct net_device *netdev, 1356 struct ethtool_stats __always_unused *stats, u64 *data) 1357 { 1358 struct ice_netdev_priv *np = netdev_priv(netdev); 1359 struct ice_vsi *vsi = np->vsi; 1360 struct ice_pf *pf = vsi->back; 1361 struct ice_ring *ring; 1362 unsigned int j; 1363 int i = 0; 1364 char *p; 1365 1366 ice_update_pf_stats(pf); 1367 ice_update_vsi_stats(vsi); 1368 1369 for (j = 0; j < ICE_VSI_STATS_LEN; j++) { 1370 p = (char *)vsi + ice_gstrings_vsi_stats[j].stat_offset; 1371 data[i++] = (ice_gstrings_vsi_stats[j].sizeof_stat == 1372 sizeof(u64)) ? *(u64 *)p : *(u32 *)p; 1373 } 1374 1375 /* populate per queue stats */ 1376 rcu_read_lock(); 1377 1378 ice_for_each_alloc_txq(vsi, j) { 1379 ring = READ_ONCE(vsi->tx_rings[j]); 1380 if (ring) { 1381 data[i++] = ring->stats.pkts; 1382 data[i++] = ring->stats.bytes; 1383 } else { 1384 data[i++] = 0; 1385 data[i++] = 0; 1386 } 1387 } 1388 1389 ice_for_each_alloc_rxq(vsi, j) { 1390 ring = READ_ONCE(vsi->rx_rings[j]); 1391 if (ring) { 1392 data[i++] = ring->stats.pkts; 1393 data[i++] = ring->stats.bytes; 1394 } else { 1395 data[i++] = 0; 1396 data[i++] = 0; 1397 } 1398 } 1399 1400 rcu_read_unlock(); 1401 1402 if (vsi->type != ICE_VSI_PF) 1403 return; 1404 1405 for (j = 0; j < ICE_PF_STATS_LEN; j++) { 1406 p = (char *)pf + ice_gstrings_pf_stats[j].stat_offset; 1407 data[i++] = (ice_gstrings_pf_stats[j].sizeof_stat == 1408 sizeof(u64)) ? *(u64 *)p : *(u32 *)p; 1409 } 1410 1411 for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) { 1412 data[i++] = pf->stats.priority_xon_tx[j]; 1413 data[i++] = pf->stats.priority_xoff_tx[j]; 1414 } 1415 1416 for (j = 0; j < ICE_MAX_USER_PRIORITY; j++) { 1417 data[i++] = pf->stats.priority_xon_rx[j]; 1418 data[i++] = pf->stats.priority_xoff_rx[j]; 1419 } 1420 } 1421 1422 /** 1423 * ice_phy_type_to_ethtool - convert the phy_types to ethtool link modes 1424 * @netdev: network interface device structure 1425 * @ks: ethtool link ksettings struct to fill out 1426 */ 1427 static void 1428 ice_phy_type_to_ethtool(struct net_device *netdev, 1429 struct ethtool_link_ksettings *ks) 1430 { 1431 struct ice_netdev_priv *np = netdev_priv(netdev); 1432 struct ice_link_status *hw_link_info; 1433 bool need_add_adv_mode = false; 1434 struct ice_vsi *vsi = np->vsi; 1435 u64 phy_types_high; 1436 u64 phy_types_low; 1437 1438 hw_link_info = &vsi->port_info->phy.link_info; 1439 phy_types_low = vsi->port_info->phy.phy_type_low; 1440 phy_types_high = vsi->port_info->phy.phy_type_high; 1441 1442 ethtool_link_ksettings_zero_link_mode(ks, supported); 1443 ethtool_link_ksettings_zero_link_mode(ks, advertising); 1444 1445 if (phy_types_low & ICE_PHY_TYPE_LOW_100BASE_TX || 1446 phy_types_low & ICE_PHY_TYPE_LOW_100M_SGMII) { 1447 ethtool_link_ksettings_add_link_mode(ks, supported, 1448 100baseT_Full); 1449 if (!hw_link_info->req_speeds || 1450 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100MB) 1451 ethtool_link_ksettings_add_link_mode(ks, advertising, 1452 100baseT_Full); 1453 } 1454 if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_T || 1455 phy_types_low & ICE_PHY_TYPE_LOW_1G_SGMII) { 1456 ethtool_link_ksettings_add_link_mode(ks, supported, 1457 1000baseT_Full); 1458 if (!hw_link_info->req_speeds || 1459 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB) 1460 ethtool_link_ksettings_add_link_mode(ks, advertising, 1461 1000baseT_Full); 1462 } 1463 if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_KX) { 1464 ethtool_link_ksettings_add_link_mode(ks, supported, 1465 1000baseKX_Full); 1466 if (!hw_link_info->req_speeds || 1467 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB) 1468 ethtool_link_ksettings_add_link_mode(ks, advertising, 1469 1000baseKX_Full); 1470 } 1471 if (phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_SX || 1472 phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_LX) { 1473 ethtool_link_ksettings_add_link_mode(ks, supported, 1474 1000baseX_Full); 1475 if (!hw_link_info->req_speeds || 1476 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_1000MB) 1477 ethtool_link_ksettings_add_link_mode(ks, advertising, 1478 1000baseX_Full); 1479 } 1480 if (phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_T) { 1481 ethtool_link_ksettings_add_link_mode(ks, supported, 1482 2500baseT_Full); 1483 if (!hw_link_info->req_speeds || 1484 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_2500MB) 1485 ethtool_link_ksettings_add_link_mode(ks, advertising, 1486 2500baseT_Full); 1487 } 1488 if (phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_X || 1489 phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_KX) { 1490 ethtool_link_ksettings_add_link_mode(ks, supported, 1491 2500baseX_Full); 1492 if (!hw_link_info->req_speeds || 1493 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_2500MB) 1494 ethtool_link_ksettings_add_link_mode(ks, advertising, 1495 2500baseX_Full); 1496 } 1497 if (phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_T || 1498 phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_KR) { 1499 ethtool_link_ksettings_add_link_mode(ks, supported, 1500 5000baseT_Full); 1501 if (!hw_link_info->req_speeds || 1502 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_5GB) 1503 ethtool_link_ksettings_add_link_mode(ks, advertising, 1504 5000baseT_Full); 1505 } 1506 if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_T || 1507 phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_DA || 1508 phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_AOC_ACC || 1509 phy_types_low & ICE_PHY_TYPE_LOW_10G_SFI_C2C) { 1510 ethtool_link_ksettings_add_link_mode(ks, supported, 1511 10000baseT_Full); 1512 if (!hw_link_info->req_speeds || 1513 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB) 1514 ethtool_link_ksettings_add_link_mode(ks, advertising, 1515 10000baseT_Full); 1516 } 1517 if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_KR_CR1) { 1518 ethtool_link_ksettings_add_link_mode(ks, supported, 1519 10000baseKR_Full); 1520 if (!hw_link_info->req_speeds || 1521 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB) 1522 ethtool_link_ksettings_add_link_mode(ks, advertising, 1523 10000baseKR_Full); 1524 } 1525 if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_SR) { 1526 ethtool_link_ksettings_add_link_mode(ks, supported, 1527 10000baseSR_Full); 1528 if (!hw_link_info->req_speeds || 1529 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB) 1530 ethtool_link_ksettings_add_link_mode(ks, advertising, 1531 10000baseSR_Full); 1532 } 1533 if (phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_LR) { 1534 ethtool_link_ksettings_add_link_mode(ks, supported, 1535 10000baseLR_Full); 1536 if (!hw_link_info->req_speeds || 1537 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_10GB) 1538 ethtool_link_ksettings_add_link_mode(ks, advertising, 1539 10000baseLR_Full); 1540 } 1541 if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_T || 1542 phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR || 1543 phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR_S || 1544 phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR1 || 1545 phy_types_low & ICE_PHY_TYPE_LOW_25G_AUI_AOC_ACC || 1546 phy_types_low & ICE_PHY_TYPE_LOW_25G_AUI_C2C) { 1547 ethtool_link_ksettings_add_link_mode(ks, supported, 1548 25000baseCR_Full); 1549 if (!hw_link_info->req_speeds || 1550 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB) 1551 ethtool_link_ksettings_add_link_mode(ks, advertising, 1552 25000baseCR_Full); 1553 } 1554 if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_SR || 1555 phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_LR) { 1556 ethtool_link_ksettings_add_link_mode(ks, supported, 1557 25000baseSR_Full); 1558 if (!hw_link_info->req_speeds || 1559 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB) 1560 ethtool_link_ksettings_add_link_mode(ks, advertising, 1561 25000baseSR_Full); 1562 } 1563 if (phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR || 1564 phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR_S || 1565 phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR1) { 1566 ethtool_link_ksettings_add_link_mode(ks, supported, 1567 25000baseKR_Full); 1568 if (!hw_link_info->req_speeds || 1569 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_25GB) 1570 ethtool_link_ksettings_add_link_mode(ks, advertising, 1571 25000baseKR_Full); 1572 } 1573 if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_KR4) { 1574 ethtool_link_ksettings_add_link_mode(ks, supported, 1575 40000baseKR4_Full); 1576 if (!hw_link_info->req_speeds || 1577 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB) 1578 ethtool_link_ksettings_add_link_mode(ks, advertising, 1579 40000baseKR4_Full); 1580 } 1581 if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_CR4 || 1582 phy_types_low & ICE_PHY_TYPE_LOW_40G_XLAUI_AOC_ACC || 1583 phy_types_low & ICE_PHY_TYPE_LOW_40G_XLAUI) { 1584 ethtool_link_ksettings_add_link_mode(ks, supported, 1585 40000baseCR4_Full); 1586 if (!hw_link_info->req_speeds || 1587 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB) 1588 ethtool_link_ksettings_add_link_mode(ks, advertising, 1589 40000baseCR4_Full); 1590 } 1591 if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_SR4) { 1592 ethtool_link_ksettings_add_link_mode(ks, supported, 1593 40000baseSR4_Full); 1594 if (!hw_link_info->req_speeds || 1595 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB) 1596 ethtool_link_ksettings_add_link_mode(ks, advertising, 1597 40000baseSR4_Full); 1598 } 1599 if (phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_LR4) { 1600 ethtool_link_ksettings_add_link_mode(ks, supported, 1601 40000baseLR4_Full); 1602 if (!hw_link_info->req_speeds || 1603 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_40GB) 1604 ethtool_link_ksettings_add_link_mode(ks, advertising, 1605 40000baseLR4_Full); 1606 } 1607 if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CR2 || 1608 phy_types_low & ICE_PHY_TYPE_LOW_50G_LAUI2_AOC_ACC || 1609 phy_types_low & ICE_PHY_TYPE_LOW_50G_LAUI2 || 1610 phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI2_AOC_ACC || 1611 phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI2 || 1612 phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CP || 1613 phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_SR || 1614 phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI1_AOC_ACC || 1615 phy_types_low & ICE_PHY_TYPE_LOW_50G_AUI1) { 1616 ethtool_link_ksettings_add_link_mode(ks, supported, 1617 50000baseCR2_Full); 1618 if (!hw_link_info->req_speeds || 1619 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_50GB) 1620 ethtool_link_ksettings_add_link_mode(ks, advertising, 1621 50000baseCR2_Full); 1622 } 1623 if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR2 || 1624 phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4) { 1625 ethtool_link_ksettings_add_link_mode(ks, supported, 1626 50000baseKR2_Full); 1627 if (!hw_link_info->req_speeds || 1628 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_50GB) 1629 ethtool_link_ksettings_add_link_mode(ks, advertising, 1630 50000baseKR2_Full); 1631 } 1632 if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_SR2 || 1633 phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_LR2 || 1634 phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_FR || 1635 phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_LR) { 1636 ethtool_link_ksettings_add_link_mode(ks, supported, 1637 50000baseSR2_Full); 1638 if (!hw_link_info->req_speeds || 1639 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_50GB) 1640 ethtool_link_ksettings_add_link_mode(ks, advertising, 1641 50000baseSR2_Full); 1642 } 1643 if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CR4 || 1644 phy_types_low & ICE_PHY_TYPE_LOW_100G_CAUI4_AOC_ACC || 1645 phy_types_low & ICE_PHY_TYPE_LOW_100G_CAUI4 || 1646 phy_types_low & ICE_PHY_TYPE_LOW_100G_AUI4_AOC_ACC || 1647 phy_types_low & ICE_PHY_TYPE_LOW_100G_AUI4 || 1648 phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CR_PAM4 || 1649 phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CP2 || 1650 phy_types_high & ICE_PHY_TYPE_HIGH_100G_CAUI2_AOC_ACC || 1651 phy_types_high & ICE_PHY_TYPE_HIGH_100G_CAUI2 || 1652 phy_types_high & ICE_PHY_TYPE_HIGH_100G_AUI2_AOC_ACC || 1653 phy_types_high & ICE_PHY_TYPE_HIGH_100G_AUI2) { 1654 ethtool_link_ksettings_add_link_mode(ks, supported, 1655 100000baseCR4_Full); 1656 if (!hw_link_info->req_speeds || 1657 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB) 1658 need_add_adv_mode = true; 1659 } 1660 if (need_add_adv_mode) { 1661 need_add_adv_mode = false; 1662 ethtool_link_ksettings_add_link_mode(ks, advertising, 1663 100000baseCR4_Full); 1664 } 1665 if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_SR4 || 1666 phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_SR2) { 1667 ethtool_link_ksettings_add_link_mode(ks, supported, 1668 100000baseSR4_Full); 1669 if (!hw_link_info->req_speeds || 1670 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB) 1671 need_add_adv_mode = true; 1672 } 1673 if (need_add_adv_mode) { 1674 need_add_adv_mode = false; 1675 ethtool_link_ksettings_add_link_mode(ks, advertising, 1676 100000baseSR4_Full); 1677 } 1678 if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_LR4 || 1679 phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_DR) { 1680 ethtool_link_ksettings_add_link_mode(ks, supported, 1681 100000baseLR4_ER4_Full); 1682 if (!hw_link_info->req_speeds || 1683 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB) 1684 need_add_adv_mode = true; 1685 } 1686 if (need_add_adv_mode) { 1687 need_add_adv_mode = false; 1688 ethtool_link_ksettings_add_link_mode(ks, advertising, 1689 100000baseLR4_ER4_Full); 1690 } 1691 if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR4 || 1692 phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4 || 1693 phy_types_high & ICE_PHY_TYPE_HIGH_100GBASE_KR2_PAM4) { 1694 ethtool_link_ksettings_add_link_mode(ks, supported, 1695 100000baseKR4_Full); 1696 if (!hw_link_info->req_speeds || 1697 hw_link_info->req_speeds & ICE_AQ_LINK_SPEED_100GB) 1698 need_add_adv_mode = true; 1699 } 1700 if (need_add_adv_mode) 1701 ethtool_link_ksettings_add_link_mode(ks, advertising, 1702 100000baseKR4_Full); 1703 1704 /* Autoneg PHY types */ 1705 if (phy_types_low & ICE_PHY_TYPE_LOW_100BASE_TX || 1706 phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_T || 1707 phy_types_low & ICE_PHY_TYPE_LOW_1000BASE_KX || 1708 phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_T || 1709 phy_types_low & ICE_PHY_TYPE_LOW_2500BASE_KX || 1710 phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_T || 1711 phy_types_low & ICE_PHY_TYPE_LOW_5GBASE_KR || 1712 phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_T || 1713 phy_types_low & ICE_PHY_TYPE_LOW_10GBASE_KR_CR1 || 1714 phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_T || 1715 phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR || 1716 phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR_S || 1717 phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_CR1 || 1718 phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR || 1719 phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR_S || 1720 phy_types_low & ICE_PHY_TYPE_LOW_25GBASE_KR1 || 1721 phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_CR4 || 1722 phy_types_low & ICE_PHY_TYPE_LOW_40GBASE_KR4) { 1723 ethtool_link_ksettings_add_link_mode(ks, supported, 1724 Autoneg); 1725 ethtool_link_ksettings_add_link_mode(ks, advertising, 1726 Autoneg); 1727 } 1728 if (phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CR2 || 1729 phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR2 || 1730 phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_CP || 1731 phy_types_low & ICE_PHY_TYPE_LOW_50GBASE_KR_PAM4) { 1732 ethtool_link_ksettings_add_link_mode(ks, supported, 1733 Autoneg); 1734 ethtool_link_ksettings_add_link_mode(ks, advertising, 1735 Autoneg); 1736 } 1737 if (phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CR4 || 1738 phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR4 || 1739 phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_KR_PAM4 || 1740 phy_types_low & ICE_PHY_TYPE_LOW_100GBASE_CP2) { 1741 ethtool_link_ksettings_add_link_mode(ks, supported, 1742 Autoneg); 1743 ethtool_link_ksettings_add_link_mode(ks, advertising, 1744 Autoneg); 1745 } 1746 } 1747 1748 #define TEST_SET_BITS_TIMEOUT 50 1749 #define TEST_SET_BITS_SLEEP_MAX 2000 1750 #define TEST_SET_BITS_SLEEP_MIN 1000 1751 1752 /** 1753 * ice_get_settings_link_up - Get Link settings for when link is up 1754 * @ks: ethtool ksettings to fill in 1755 * @netdev: network interface device structure 1756 */ 1757 static void 1758 ice_get_settings_link_up(struct ethtool_link_ksettings *ks, 1759 struct net_device *netdev) 1760 { 1761 struct ice_netdev_priv *np = netdev_priv(netdev); 1762 struct ice_port_info *pi = np->vsi->port_info; 1763 struct ice_link_status *link_info; 1764 struct ice_vsi *vsi = np->vsi; 1765 1766 link_info = &vsi->port_info->phy.link_info; 1767 1768 /* Get supported and advertised settings from PHY ability with media */ 1769 ice_phy_type_to_ethtool(netdev, ks); 1770 1771 switch (link_info->link_speed) { 1772 case ICE_AQ_LINK_SPEED_100GB: 1773 ks->base.speed = SPEED_100000; 1774 break; 1775 case ICE_AQ_LINK_SPEED_50GB: 1776 ks->base.speed = SPEED_50000; 1777 break; 1778 case ICE_AQ_LINK_SPEED_40GB: 1779 ks->base.speed = SPEED_40000; 1780 break; 1781 case ICE_AQ_LINK_SPEED_25GB: 1782 ks->base.speed = SPEED_25000; 1783 break; 1784 case ICE_AQ_LINK_SPEED_20GB: 1785 ks->base.speed = SPEED_20000; 1786 break; 1787 case ICE_AQ_LINK_SPEED_10GB: 1788 ks->base.speed = SPEED_10000; 1789 break; 1790 case ICE_AQ_LINK_SPEED_5GB: 1791 ks->base.speed = SPEED_5000; 1792 break; 1793 case ICE_AQ_LINK_SPEED_2500MB: 1794 ks->base.speed = SPEED_2500; 1795 break; 1796 case ICE_AQ_LINK_SPEED_1000MB: 1797 ks->base.speed = SPEED_1000; 1798 break; 1799 case ICE_AQ_LINK_SPEED_100MB: 1800 ks->base.speed = SPEED_100; 1801 break; 1802 default: 1803 netdev_info(netdev, "WARNING: Unrecognized link_speed (0x%x).\n", 1804 link_info->link_speed); 1805 break; 1806 } 1807 ks->base.duplex = DUPLEX_FULL; 1808 1809 if (link_info->an_info & ICE_AQ_AN_COMPLETED) 1810 ethtool_link_ksettings_add_link_mode(ks, lp_advertising, 1811 Autoneg); 1812 1813 /* Set flow control negotiated Rx/Tx pause */ 1814 switch (pi->fc.current_mode) { 1815 case ICE_FC_FULL: 1816 ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause); 1817 break; 1818 case ICE_FC_TX_PAUSE: 1819 ethtool_link_ksettings_add_link_mode(ks, lp_advertising, Pause); 1820 ethtool_link_ksettings_add_link_mode(ks, lp_advertising, 1821 Asym_Pause); 1822 break; 1823 case ICE_FC_RX_PAUSE: 1824 ethtool_link_ksettings_add_link_mode(ks, lp_advertising, 1825 Asym_Pause); 1826 break; 1827 case ICE_FC_PFC: 1828 default: 1829 ethtool_link_ksettings_del_link_mode(ks, lp_advertising, Pause); 1830 ethtool_link_ksettings_del_link_mode(ks, lp_advertising, 1831 Asym_Pause); 1832 break; 1833 } 1834 } 1835 1836 /** 1837 * ice_get_settings_link_down - Get the Link settings when link is down 1838 * @ks: ethtool ksettings to fill in 1839 * @netdev: network interface device structure 1840 * 1841 * Reports link settings that can be determined when link is down 1842 */ 1843 static void 1844 ice_get_settings_link_down(struct ethtool_link_ksettings *ks, 1845 struct net_device *netdev) 1846 { 1847 /* link is down and the driver needs to fall back on 1848 * supported PHY types to figure out what info to display 1849 */ 1850 ice_phy_type_to_ethtool(netdev, ks); 1851 1852 /* With no link, speed and duplex are unknown */ 1853 ks->base.speed = SPEED_UNKNOWN; 1854 ks->base.duplex = DUPLEX_UNKNOWN; 1855 } 1856 1857 /** 1858 * ice_get_link_ksettings - Get Link Speed and Duplex settings 1859 * @netdev: network interface device structure 1860 * @ks: ethtool ksettings 1861 * 1862 * Reports speed/duplex settings based on media_type 1863 */ 1864 static int 1865 ice_get_link_ksettings(struct net_device *netdev, 1866 struct ethtool_link_ksettings *ks) 1867 { 1868 struct ice_netdev_priv *np = netdev_priv(netdev); 1869 struct ice_aqc_get_phy_caps_data *caps; 1870 struct ice_link_status *hw_link_info; 1871 struct ice_vsi *vsi = np->vsi; 1872 enum ice_status status; 1873 int err = 0; 1874 1875 ethtool_link_ksettings_zero_link_mode(ks, supported); 1876 ethtool_link_ksettings_zero_link_mode(ks, advertising); 1877 ethtool_link_ksettings_zero_link_mode(ks, lp_advertising); 1878 hw_link_info = &vsi->port_info->phy.link_info; 1879 1880 /* set speed and duplex */ 1881 if (hw_link_info->link_info & ICE_AQ_LINK_UP) 1882 ice_get_settings_link_up(ks, netdev); 1883 else 1884 ice_get_settings_link_down(ks, netdev); 1885 1886 /* set autoneg settings */ 1887 ks->base.autoneg = (hw_link_info->an_info & ICE_AQ_AN_COMPLETED) ? 1888 AUTONEG_ENABLE : AUTONEG_DISABLE; 1889 1890 /* set media type settings */ 1891 switch (vsi->port_info->phy.media_type) { 1892 case ICE_MEDIA_FIBER: 1893 ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE); 1894 ks->base.port = PORT_FIBRE; 1895 break; 1896 case ICE_MEDIA_BASET: 1897 ethtool_link_ksettings_add_link_mode(ks, supported, TP); 1898 ethtool_link_ksettings_add_link_mode(ks, advertising, TP); 1899 ks->base.port = PORT_TP; 1900 break; 1901 case ICE_MEDIA_BACKPLANE: 1902 ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg); 1903 ethtool_link_ksettings_add_link_mode(ks, supported, Backplane); 1904 ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg); 1905 ethtool_link_ksettings_add_link_mode(ks, advertising, 1906 Backplane); 1907 ks->base.port = PORT_NONE; 1908 break; 1909 case ICE_MEDIA_DA: 1910 ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE); 1911 ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE); 1912 ks->base.port = PORT_DA; 1913 break; 1914 default: 1915 ks->base.port = PORT_OTHER; 1916 break; 1917 } 1918 1919 /* flow control is symmetric and always supported */ 1920 ethtool_link_ksettings_add_link_mode(ks, supported, Pause); 1921 1922 caps = kzalloc(sizeof(*caps), GFP_KERNEL); 1923 if (!caps) 1924 return -ENOMEM; 1925 1926 status = ice_aq_get_phy_caps(vsi->port_info, false, 1927 ICE_AQC_REPORT_SW_CFG, caps, NULL); 1928 if (status) { 1929 err = -EIO; 1930 goto done; 1931 } 1932 1933 /* Set the advertised flow control based on the PHY capability */ 1934 if ((caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) && 1935 (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE)) { 1936 ethtool_link_ksettings_add_link_mode(ks, advertising, Pause); 1937 ethtool_link_ksettings_add_link_mode(ks, advertising, 1938 Asym_Pause); 1939 } else if (caps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) { 1940 ethtool_link_ksettings_add_link_mode(ks, advertising, 1941 Asym_Pause); 1942 } else if (caps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE) { 1943 ethtool_link_ksettings_add_link_mode(ks, advertising, Pause); 1944 ethtool_link_ksettings_add_link_mode(ks, advertising, 1945 Asym_Pause); 1946 } else { 1947 ethtool_link_ksettings_del_link_mode(ks, advertising, Pause); 1948 ethtool_link_ksettings_del_link_mode(ks, advertising, 1949 Asym_Pause); 1950 } 1951 1952 /* Set advertised FEC modes based on PHY capability */ 1953 ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_NONE); 1954 1955 if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_REQ || 1956 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_REQ) 1957 ethtool_link_ksettings_add_link_mode(ks, advertising, 1958 FEC_BASER); 1959 if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_528_REQ || 1960 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_544_REQ) 1961 ethtool_link_ksettings_add_link_mode(ks, advertising, FEC_RS); 1962 1963 status = ice_aq_get_phy_caps(vsi->port_info, false, 1964 ICE_AQC_REPORT_TOPO_CAP, caps, NULL); 1965 if (status) { 1966 err = -EIO; 1967 goto done; 1968 } 1969 1970 /* Set supported FEC modes based on PHY capability */ 1971 ethtool_link_ksettings_add_link_mode(ks, supported, FEC_NONE); 1972 1973 if (caps->link_fec_options & ICE_AQC_PHY_FEC_10G_KR_40G_KR4_EN || 1974 caps->link_fec_options & ICE_AQC_PHY_FEC_25G_KR_CLAUSE74_EN) 1975 ethtool_link_ksettings_add_link_mode(ks, supported, FEC_BASER); 1976 if (caps->link_fec_options & ICE_AQC_PHY_FEC_25G_RS_CLAUSE91_EN) 1977 ethtool_link_ksettings_add_link_mode(ks, supported, FEC_RS); 1978 1979 done: 1980 kfree(caps); 1981 return err; 1982 } 1983 1984 /** 1985 * ice_ksettings_find_adv_link_speed - Find advertising link speed 1986 * @ks: ethtool ksettings 1987 */ 1988 static u16 1989 ice_ksettings_find_adv_link_speed(const struct ethtool_link_ksettings *ks) 1990 { 1991 u16 adv_link_speed = 0; 1992 1993 if (ethtool_link_ksettings_test_link_mode(ks, advertising, 1994 100baseT_Full)) 1995 adv_link_speed |= ICE_AQ_LINK_SPEED_100MB; 1996 if (ethtool_link_ksettings_test_link_mode(ks, advertising, 1997 1000baseX_Full)) 1998 adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB; 1999 if (ethtool_link_ksettings_test_link_mode(ks, advertising, 2000 1000baseT_Full) || 2001 ethtool_link_ksettings_test_link_mode(ks, advertising, 2002 1000baseKX_Full)) 2003 adv_link_speed |= ICE_AQ_LINK_SPEED_1000MB; 2004 if (ethtool_link_ksettings_test_link_mode(ks, advertising, 2005 2500baseT_Full)) 2006 adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB; 2007 if (ethtool_link_ksettings_test_link_mode(ks, advertising, 2008 2500baseX_Full)) 2009 adv_link_speed |= ICE_AQ_LINK_SPEED_2500MB; 2010 if (ethtool_link_ksettings_test_link_mode(ks, advertising, 2011 5000baseT_Full)) 2012 adv_link_speed |= ICE_AQ_LINK_SPEED_5GB; 2013 if (ethtool_link_ksettings_test_link_mode(ks, advertising, 2014 10000baseT_Full) || 2015 ethtool_link_ksettings_test_link_mode(ks, advertising, 2016 10000baseKR_Full)) 2017 adv_link_speed |= ICE_AQ_LINK_SPEED_10GB; 2018 if (ethtool_link_ksettings_test_link_mode(ks, advertising, 2019 10000baseSR_Full) || 2020 ethtool_link_ksettings_test_link_mode(ks, advertising, 2021 10000baseLR_Full)) 2022 adv_link_speed |= ICE_AQ_LINK_SPEED_10GB; 2023 if (ethtool_link_ksettings_test_link_mode(ks, advertising, 2024 25000baseCR_Full) || 2025 ethtool_link_ksettings_test_link_mode(ks, advertising, 2026 25000baseSR_Full) || 2027 ethtool_link_ksettings_test_link_mode(ks, advertising, 2028 25000baseKR_Full)) 2029 adv_link_speed |= ICE_AQ_LINK_SPEED_25GB; 2030 if (ethtool_link_ksettings_test_link_mode(ks, advertising, 2031 40000baseCR4_Full) || 2032 ethtool_link_ksettings_test_link_mode(ks, advertising, 2033 40000baseSR4_Full) || 2034 ethtool_link_ksettings_test_link_mode(ks, advertising, 2035 40000baseLR4_Full) || 2036 ethtool_link_ksettings_test_link_mode(ks, advertising, 2037 40000baseKR4_Full)) 2038 adv_link_speed |= ICE_AQ_LINK_SPEED_40GB; 2039 if (ethtool_link_ksettings_test_link_mode(ks, advertising, 2040 50000baseCR2_Full) || 2041 ethtool_link_ksettings_test_link_mode(ks, advertising, 2042 50000baseKR2_Full)) 2043 adv_link_speed |= ICE_AQ_LINK_SPEED_50GB; 2044 if (ethtool_link_ksettings_test_link_mode(ks, advertising, 2045 50000baseSR2_Full)) 2046 adv_link_speed |= ICE_AQ_LINK_SPEED_50GB; 2047 if (ethtool_link_ksettings_test_link_mode(ks, advertising, 2048 100000baseCR4_Full) || 2049 ethtool_link_ksettings_test_link_mode(ks, advertising, 2050 100000baseSR4_Full) || 2051 ethtool_link_ksettings_test_link_mode(ks, advertising, 2052 100000baseLR4_ER4_Full) || 2053 ethtool_link_ksettings_test_link_mode(ks, advertising, 2054 100000baseKR4_Full)) 2055 adv_link_speed |= ICE_AQ_LINK_SPEED_100GB; 2056 2057 return adv_link_speed; 2058 } 2059 2060 /** 2061 * ice_setup_autoneg 2062 * @p: port info 2063 * @ks: ethtool_link_ksettings 2064 * @config: configuration that will be sent down to FW 2065 * @autoneg_enabled: autonegotiation is enabled or not 2066 * @autoneg_changed: will there a change in autonegotiation 2067 * @netdev: network interface device structure 2068 * 2069 * Setup PHY autonegotiation feature 2070 */ 2071 static int 2072 ice_setup_autoneg(struct ice_port_info *p, struct ethtool_link_ksettings *ks, 2073 struct ice_aqc_set_phy_cfg_data *config, 2074 u8 autoneg_enabled, u8 *autoneg_changed, 2075 struct net_device *netdev) 2076 { 2077 int err = 0; 2078 2079 *autoneg_changed = 0; 2080 2081 /* Check autoneg */ 2082 if (autoneg_enabled == AUTONEG_ENABLE) { 2083 /* If autoneg was not already enabled */ 2084 if (!(p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED)) { 2085 /* If autoneg is not supported, return error */ 2086 if (!ethtool_link_ksettings_test_link_mode(ks, 2087 supported, 2088 Autoneg)) { 2089 netdev_info(netdev, "Autoneg not supported on this phy.\n"); 2090 err = -EINVAL; 2091 } else { 2092 /* Autoneg is allowed to change */ 2093 config->caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT; 2094 *autoneg_changed = 1; 2095 } 2096 } 2097 } else { 2098 /* If autoneg is currently enabled */ 2099 if (p->phy.link_info.an_info & ICE_AQ_AN_COMPLETED) { 2100 /* If autoneg is supported 10GBASE_T is the only PHY 2101 * that can disable it, so otherwise return error 2102 */ 2103 if (ethtool_link_ksettings_test_link_mode(ks, 2104 supported, 2105 Autoneg)) { 2106 netdev_info(netdev, "Autoneg cannot be disabled on this phy\n"); 2107 err = -EINVAL; 2108 } else { 2109 /* Autoneg is allowed to change */ 2110 config->caps &= ~ICE_AQ_PHY_ENA_AUTO_LINK_UPDT; 2111 *autoneg_changed = 1; 2112 } 2113 } 2114 } 2115 2116 return err; 2117 } 2118 2119 /** 2120 * ice_set_link_ksettings - Set Speed and Duplex 2121 * @netdev: network interface device structure 2122 * @ks: ethtool ksettings 2123 * 2124 * Set speed/duplex per media_types advertised/forced 2125 */ 2126 static int 2127 ice_set_link_ksettings(struct net_device *netdev, 2128 const struct ethtool_link_ksettings *ks) 2129 { 2130 u8 autoneg, timeout = TEST_SET_BITS_TIMEOUT, lport = 0; 2131 struct ice_netdev_priv *np = netdev_priv(netdev); 2132 struct ethtool_link_ksettings safe_ks, copy_ks; 2133 struct ice_aqc_get_phy_caps_data *abilities; 2134 u16 adv_link_speed, curr_link_speed, idx; 2135 struct ice_aqc_set_phy_cfg_data config; 2136 struct ice_pf *pf = np->vsi->back; 2137 struct ice_port_info *p; 2138 u8 autoneg_changed = 0; 2139 enum ice_status status; 2140 u64 phy_type_high; 2141 u64 phy_type_low; 2142 int err = 0; 2143 bool linkup; 2144 2145 p = np->vsi->port_info; 2146 2147 if (!p) 2148 return -EOPNOTSUPP; 2149 2150 /* Check if this is LAN VSI */ 2151 ice_for_each_vsi(pf, idx) 2152 if (pf->vsi[idx]->type == ICE_VSI_PF) { 2153 if (np->vsi != pf->vsi[idx]) 2154 return -EOPNOTSUPP; 2155 break; 2156 } 2157 2158 if (p->phy.media_type != ICE_MEDIA_BASET && 2159 p->phy.media_type != ICE_MEDIA_FIBER && 2160 p->phy.media_type != ICE_MEDIA_BACKPLANE && 2161 p->phy.media_type != ICE_MEDIA_DA && 2162 p->phy.link_info.link_info & ICE_AQ_LINK_UP) 2163 return -EOPNOTSUPP; 2164 2165 /* copy the ksettings to copy_ks to avoid modifying the original */ 2166 memcpy(©_ks, ks, sizeof(copy_ks)); 2167 2168 /* save autoneg out of ksettings */ 2169 autoneg = copy_ks.base.autoneg; 2170 2171 memset(&safe_ks, 0, sizeof(safe_ks)); 2172 2173 /* Get link modes supported by hardware.*/ 2174 ice_phy_type_to_ethtool(netdev, &safe_ks); 2175 2176 /* and check against modes requested by user. 2177 * Return an error if unsupported mode was set. 2178 */ 2179 if (!bitmap_subset(copy_ks.link_modes.advertising, 2180 safe_ks.link_modes.supported, 2181 __ETHTOOL_LINK_MODE_MASK_NBITS)) 2182 return -EINVAL; 2183 2184 /* get our own copy of the bits to check against */ 2185 memset(&safe_ks, 0, sizeof(safe_ks)); 2186 safe_ks.base.cmd = copy_ks.base.cmd; 2187 safe_ks.base.link_mode_masks_nwords = 2188 copy_ks.base.link_mode_masks_nwords; 2189 ice_get_link_ksettings(netdev, &safe_ks); 2190 2191 /* set autoneg back to what it currently is */ 2192 copy_ks.base.autoneg = safe_ks.base.autoneg; 2193 /* we don't compare the speed */ 2194 copy_ks.base.speed = safe_ks.base.speed; 2195 2196 /* If copy_ks.base and safe_ks.base are not the same now, then they are 2197 * trying to set something that we do not support. 2198 */ 2199 if (memcmp(©_ks.base, &safe_ks.base, sizeof(copy_ks.base))) 2200 return -EOPNOTSUPP; 2201 2202 while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) { 2203 timeout--; 2204 if (!timeout) 2205 return -EBUSY; 2206 usleep_range(TEST_SET_BITS_SLEEP_MIN, TEST_SET_BITS_SLEEP_MAX); 2207 } 2208 2209 abilities = kzalloc(sizeof(*abilities), GFP_KERNEL); 2210 if (!abilities) 2211 return -ENOMEM; 2212 2213 /* Get the current PHY config */ 2214 status = ice_aq_get_phy_caps(p, false, ICE_AQC_REPORT_SW_CFG, abilities, 2215 NULL); 2216 if (status) { 2217 err = -EAGAIN; 2218 goto done; 2219 } 2220 2221 /* Copy abilities to config in case autoneg is not set below */ 2222 memset(&config, 0, sizeof(config)); 2223 config.caps = abilities->caps & ~ICE_AQC_PHY_AN_MODE; 2224 if (abilities->caps & ICE_AQC_PHY_AN_MODE) 2225 config.caps |= ICE_AQ_PHY_ENA_AUTO_LINK_UPDT; 2226 2227 /* Check autoneg */ 2228 err = ice_setup_autoneg(p, &safe_ks, &config, autoneg, &autoneg_changed, 2229 netdev); 2230 2231 if (err) 2232 goto done; 2233 2234 /* Call to get the current link speed */ 2235 p->phy.get_link_info = true; 2236 status = ice_get_link_status(p, &linkup); 2237 if (status) { 2238 err = -EAGAIN; 2239 goto done; 2240 } 2241 2242 curr_link_speed = p->phy.link_info.link_speed; 2243 adv_link_speed = ice_ksettings_find_adv_link_speed(ks); 2244 2245 /* If speed didn't get set, set it to what it currently is. 2246 * This is needed because if advertise is 0 (as it is when autoneg 2247 * is disabled) then speed won't get set. 2248 */ 2249 if (!adv_link_speed) 2250 adv_link_speed = curr_link_speed; 2251 2252 /* Convert the advertise link speeds to their corresponded PHY_TYPE */ 2253 ice_update_phy_type(&phy_type_low, &phy_type_high, adv_link_speed); 2254 2255 if (!autoneg_changed && adv_link_speed == curr_link_speed) { 2256 netdev_info(netdev, "Nothing changed, exiting without setting anything.\n"); 2257 goto done; 2258 } 2259 2260 /* copy over the rest of the abilities */ 2261 config.low_power_ctrl = abilities->low_power_ctrl; 2262 config.eee_cap = abilities->eee_cap; 2263 config.eeer_value = abilities->eeer_value; 2264 config.link_fec_opt = abilities->link_fec_options; 2265 2266 /* save the requested speeds */ 2267 p->phy.link_info.req_speeds = adv_link_speed; 2268 2269 /* set link and auto negotiation so changes take effect */ 2270 config.caps |= ICE_AQ_PHY_ENA_LINK; 2271 2272 if (phy_type_low || phy_type_high) { 2273 config.phy_type_high = cpu_to_le64(phy_type_high) & 2274 abilities->phy_type_high; 2275 config.phy_type_low = cpu_to_le64(phy_type_low) & 2276 abilities->phy_type_low; 2277 } else { 2278 err = -EAGAIN; 2279 netdev_info(netdev, "Nothing changed. No PHY_TYPE is corresponded to advertised link speed.\n"); 2280 goto done; 2281 } 2282 2283 /* If link is up put link down */ 2284 if (p->phy.link_info.link_info & ICE_AQ_LINK_UP) { 2285 /* Tell the OS link is going down, the link will go 2286 * back up when fw says it is ready asynchronously 2287 */ 2288 ice_print_link_msg(np->vsi, false); 2289 netif_carrier_off(netdev); 2290 netif_tx_stop_all_queues(netdev); 2291 } 2292 2293 /* make the aq call */ 2294 status = ice_aq_set_phy_cfg(&pf->hw, lport, &config, NULL); 2295 if (status) { 2296 netdev_info(netdev, "Set phy config failed,\n"); 2297 err = -EAGAIN; 2298 } 2299 2300 done: 2301 kfree(abilities); 2302 clear_bit(__ICE_CFG_BUSY, pf->state); 2303 2304 return err; 2305 } 2306 2307 /** 2308 * ice_parse_hdrs - parses headers from RSS hash input 2309 * @nfc: ethtool rxnfc command 2310 * 2311 * This function parses the rxnfc command and returns intended 2312 * header types for RSS configuration 2313 */ 2314 static u32 ice_parse_hdrs(struct ethtool_rxnfc *nfc) 2315 { 2316 u32 hdrs = ICE_FLOW_SEG_HDR_NONE; 2317 2318 switch (nfc->flow_type) { 2319 case TCP_V4_FLOW: 2320 hdrs |= ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV4; 2321 break; 2322 case UDP_V4_FLOW: 2323 hdrs |= ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV4; 2324 break; 2325 case SCTP_V4_FLOW: 2326 hdrs |= ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV4; 2327 break; 2328 case TCP_V6_FLOW: 2329 hdrs |= ICE_FLOW_SEG_HDR_TCP | ICE_FLOW_SEG_HDR_IPV6; 2330 break; 2331 case UDP_V6_FLOW: 2332 hdrs |= ICE_FLOW_SEG_HDR_UDP | ICE_FLOW_SEG_HDR_IPV6; 2333 break; 2334 case SCTP_V6_FLOW: 2335 hdrs |= ICE_FLOW_SEG_HDR_SCTP | ICE_FLOW_SEG_HDR_IPV6; 2336 break; 2337 default: 2338 break; 2339 } 2340 return hdrs; 2341 } 2342 2343 #define ICE_FLOW_HASH_FLD_IPV4_SA BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_SA) 2344 #define ICE_FLOW_HASH_FLD_IPV6_SA BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_SA) 2345 #define ICE_FLOW_HASH_FLD_IPV4_DA BIT_ULL(ICE_FLOW_FIELD_IDX_IPV4_DA) 2346 #define ICE_FLOW_HASH_FLD_IPV6_DA BIT_ULL(ICE_FLOW_FIELD_IDX_IPV6_DA) 2347 #define ICE_FLOW_HASH_FLD_TCP_SRC_PORT BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_SRC_PORT) 2348 #define ICE_FLOW_HASH_FLD_TCP_DST_PORT BIT_ULL(ICE_FLOW_FIELD_IDX_TCP_DST_PORT) 2349 #define ICE_FLOW_HASH_FLD_UDP_SRC_PORT BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_SRC_PORT) 2350 #define ICE_FLOW_HASH_FLD_UDP_DST_PORT BIT_ULL(ICE_FLOW_FIELD_IDX_UDP_DST_PORT) 2351 #define ICE_FLOW_HASH_FLD_SCTP_SRC_PORT \ 2352 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_SRC_PORT) 2353 #define ICE_FLOW_HASH_FLD_SCTP_DST_PORT \ 2354 BIT_ULL(ICE_FLOW_FIELD_IDX_SCTP_DST_PORT) 2355 2356 /** 2357 * ice_parse_hash_flds - parses hash fields from RSS hash input 2358 * @nfc: ethtool rxnfc command 2359 * 2360 * This function parses the rxnfc command and returns intended 2361 * hash fields for RSS configuration 2362 */ 2363 static u64 ice_parse_hash_flds(struct ethtool_rxnfc *nfc) 2364 { 2365 u64 hfld = ICE_HASH_INVALID; 2366 2367 if (nfc->data & RXH_IP_SRC || nfc->data & RXH_IP_DST) { 2368 switch (nfc->flow_type) { 2369 case TCP_V4_FLOW: 2370 case UDP_V4_FLOW: 2371 case SCTP_V4_FLOW: 2372 if (nfc->data & RXH_IP_SRC) 2373 hfld |= ICE_FLOW_HASH_FLD_IPV4_SA; 2374 if (nfc->data & RXH_IP_DST) 2375 hfld |= ICE_FLOW_HASH_FLD_IPV4_DA; 2376 break; 2377 case TCP_V6_FLOW: 2378 case UDP_V6_FLOW: 2379 case SCTP_V6_FLOW: 2380 if (nfc->data & RXH_IP_SRC) 2381 hfld |= ICE_FLOW_HASH_FLD_IPV6_SA; 2382 if (nfc->data & RXH_IP_DST) 2383 hfld |= ICE_FLOW_HASH_FLD_IPV6_DA; 2384 break; 2385 default: 2386 break; 2387 } 2388 } 2389 2390 if (nfc->data & RXH_L4_B_0_1 || nfc->data & RXH_L4_B_2_3) { 2391 switch (nfc->flow_type) { 2392 case TCP_V4_FLOW: 2393 case TCP_V6_FLOW: 2394 if (nfc->data & RXH_L4_B_0_1) 2395 hfld |= ICE_FLOW_HASH_FLD_TCP_SRC_PORT; 2396 if (nfc->data & RXH_L4_B_2_3) 2397 hfld |= ICE_FLOW_HASH_FLD_TCP_DST_PORT; 2398 break; 2399 case UDP_V4_FLOW: 2400 case UDP_V6_FLOW: 2401 if (nfc->data & RXH_L4_B_0_1) 2402 hfld |= ICE_FLOW_HASH_FLD_UDP_SRC_PORT; 2403 if (nfc->data & RXH_L4_B_2_3) 2404 hfld |= ICE_FLOW_HASH_FLD_UDP_DST_PORT; 2405 break; 2406 case SCTP_V4_FLOW: 2407 case SCTP_V6_FLOW: 2408 if (nfc->data & RXH_L4_B_0_1) 2409 hfld |= ICE_FLOW_HASH_FLD_SCTP_SRC_PORT; 2410 if (nfc->data & RXH_L4_B_2_3) 2411 hfld |= ICE_FLOW_HASH_FLD_SCTP_DST_PORT; 2412 break; 2413 default: 2414 break; 2415 } 2416 } 2417 2418 return hfld; 2419 } 2420 2421 /** 2422 * ice_set_rss_hash_opt - Enable/Disable flow types for RSS hash 2423 * @vsi: the VSI being configured 2424 * @nfc: ethtool rxnfc command 2425 * 2426 * Returns Success if the flow input set is supported. 2427 */ 2428 static int 2429 ice_set_rss_hash_opt(struct ice_vsi *vsi, struct ethtool_rxnfc *nfc) 2430 { 2431 struct ice_pf *pf = vsi->back; 2432 enum ice_status status; 2433 struct device *dev; 2434 u64 hashed_flds; 2435 u32 hdrs; 2436 2437 dev = ice_pf_to_dev(pf); 2438 if (ice_is_safe_mode(pf)) { 2439 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n", 2440 vsi->vsi_num); 2441 return -EINVAL; 2442 } 2443 2444 hashed_flds = ice_parse_hash_flds(nfc); 2445 if (hashed_flds == ICE_HASH_INVALID) { 2446 dev_dbg(dev, "Invalid hash fields, vsi num = %d\n", 2447 vsi->vsi_num); 2448 return -EINVAL; 2449 } 2450 2451 hdrs = ice_parse_hdrs(nfc); 2452 if (hdrs == ICE_FLOW_SEG_HDR_NONE) { 2453 dev_dbg(dev, "Header type is not valid, vsi num = %d\n", 2454 vsi->vsi_num); 2455 return -EINVAL; 2456 } 2457 2458 status = ice_add_rss_cfg(&pf->hw, vsi->idx, hashed_flds, hdrs); 2459 if (status) { 2460 dev_dbg(dev, "ice_add_rss_cfg failed, vsi num = %d, error = %s\n", 2461 vsi->vsi_num, ice_stat_str(status)); 2462 return -EINVAL; 2463 } 2464 2465 return 0; 2466 } 2467 2468 /** 2469 * ice_get_rss_hash_opt - Retrieve hash fields for a given flow-type 2470 * @vsi: the VSI being configured 2471 * @nfc: ethtool rxnfc command 2472 */ 2473 static void 2474 ice_get_rss_hash_opt(struct ice_vsi *vsi, struct ethtool_rxnfc *nfc) 2475 { 2476 struct ice_pf *pf = vsi->back; 2477 struct device *dev; 2478 u64 hash_flds; 2479 u32 hdrs; 2480 2481 dev = ice_pf_to_dev(pf); 2482 2483 nfc->data = 0; 2484 if (ice_is_safe_mode(pf)) { 2485 dev_dbg(dev, "Advanced RSS disabled. Package download failed, vsi num = %d\n", 2486 vsi->vsi_num); 2487 return; 2488 } 2489 2490 hdrs = ice_parse_hdrs(nfc); 2491 if (hdrs == ICE_FLOW_SEG_HDR_NONE) { 2492 dev_dbg(dev, "Header type is not valid, vsi num = %d\n", 2493 vsi->vsi_num); 2494 return; 2495 } 2496 2497 hash_flds = ice_get_rss_cfg(&pf->hw, vsi->idx, hdrs); 2498 if (hash_flds == ICE_HASH_INVALID) { 2499 dev_dbg(dev, "No hash fields found for the given header type, vsi num = %d\n", 2500 vsi->vsi_num); 2501 return; 2502 } 2503 2504 if (hash_flds & ICE_FLOW_HASH_FLD_IPV4_SA || 2505 hash_flds & ICE_FLOW_HASH_FLD_IPV6_SA) 2506 nfc->data |= (u64)RXH_IP_SRC; 2507 2508 if (hash_flds & ICE_FLOW_HASH_FLD_IPV4_DA || 2509 hash_flds & ICE_FLOW_HASH_FLD_IPV6_DA) 2510 nfc->data |= (u64)RXH_IP_DST; 2511 2512 if (hash_flds & ICE_FLOW_HASH_FLD_TCP_SRC_PORT || 2513 hash_flds & ICE_FLOW_HASH_FLD_UDP_SRC_PORT || 2514 hash_flds & ICE_FLOW_HASH_FLD_SCTP_SRC_PORT) 2515 nfc->data |= (u64)RXH_L4_B_0_1; 2516 2517 if (hash_flds & ICE_FLOW_HASH_FLD_TCP_DST_PORT || 2518 hash_flds & ICE_FLOW_HASH_FLD_UDP_DST_PORT || 2519 hash_flds & ICE_FLOW_HASH_FLD_SCTP_DST_PORT) 2520 nfc->data |= (u64)RXH_L4_B_2_3; 2521 } 2522 2523 /** 2524 * ice_set_rxnfc - command to set Rx flow rules. 2525 * @netdev: network interface device structure 2526 * @cmd: ethtool rxnfc command 2527 * 2528 * Returns 0 for success and negative values for errors 2529 */ 2530 static int ice_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd) 2531 { 2532 struct ice_netdev_priv *np = netdev_priv(netdev); 2533 struct ice_vsi *vsi = np->vsi; 2534 2535 switch (cmd->cmd) { 2536 case ETHTOOL_SRXCLSRLINS: 2537 return ice_add_fdir_ethtool(vsi, cmd); 2538 case ETHTOOL_SRXCLSRLDEL: 2539 return ice_del_fdir_ethtool(vsi, cmd); 2540 case ETHTOOL_SRXFH: 2541 return ice_set_rss_hash_opt(vsi, cmd); 2542 default: 2543 break; 2544 } 2545 return -EOPNOTSUPP; 2546 } 2547 2548 /** 2549 * ice_get_rxnfc - command to get Rx flow classification rules 2550 * @netdev: network interface device structure 2551 * @cmd: ethtool rxnfc command 2552 * @rule_locs: buffer to rturn Rx flow classification rules 2553 * 2554 * Returns Success if the command is supported. 2555 */ 2556 static int 2557 ice_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd, 2558 u32 __always_unused *rule_locs) 2559 { 2560 struct ice_netdev_priv *np = netdev_priv(netdev); 2561 struct ice_vsi *vsi = np->vsi; 2562 int ret = -EOPNOTSUPP; 2563 struct ice_hw *hw; 2564 2565 hw = &vsi->back->hw; 2566 2567 switch (cmd->cmd) { 2568 case ETHTOOL_GRXRINGS: 2569 cmd->data = vsi->rss_size; 2570 ret = 0; 2571 break; 2572 case ETHTOOL_GRXCLSRLCNT: 2573 cmd->rule_cnt = hw->fdir_active_fltr; 2574 /* report total rule count */ 2575 cmd->data = ice_get_fdir_cnt_all(hw); 2576 ret = 0; 2577 break; 2578 case ETHTOOL_GRXCLSRULE: 2579 ret = ice_get_ethtool_fdir_entry(hw, cmd); 2580 break; 2581 case ETHTOOL_GRXCLSRLALL: 2582 ret = ice_get_fdir_fltr_ids(hw, cmd, (u32 *)rule_locs); 2583 break; 2584 case ETHTOOL_GRXFH: 2585 ice_get_rss_hash_opt(vsi, cmd); 2586 ret = 0; 2587 break; 2588 default: 2589 break; 2590 } 2591 2592 return ret; 2593 } 2594 2595 static void 2596 ice_get_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring) 2597 { 2598 struct ice_netdev_priv *np = netdev_priv(netdev); 2599 struct ice_vsi *vsi = np->vsi; 2600 2601 ring->rx_max_pending = ICE_MAX_NUM_DESC; 2602 ring->tx_max_pending = ICE_MAX_NUM_DESC; 2603 ring->rx_pending = vsi->rx_rings[0]->count; 2604 ring->tx_pending = vsi->tx_rings[0]->count; 2605 2606 /* Rx mini and jumbo rings are not supported */ 2607 ring->rx_mini_max_pending = 0; 2608 ring->rx_jumbo_max_pending = 0; 2609 ring->rx_mini_pending = 0; 2610 ring->rx_jumbo_pending = 0; 2611 } 2612 2613 static int 2614 ice_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring) 2615 { 2616 struct ice_ring *tx_rings = NULL, *rx_rings = NULL; 2617 struct ice_netdev_priv *np = netdev_priv(netdev); 2618 struct ice_ring *xdp_rings = NULL; 2619 struct ice_vsi *vsi = np->vsi; 2620 struct ice_pf *pf = vsi->back; 2621 int i, timeout = 50, err = 0; 2622 u16 new_rx_cnt, new_tx_cnt; 2623 2624 if (ring->tx_pending > ICE_MAX_NUM_DESC || 2625 ring->tx_pending < ICE_MIN_NUM_DESC || 2626 ring->rx_pending > ICE_MAX_NUM_DESC || 2627 ring->rx_pending < ICE_MIN_NUM_DESC) { 2628 netdev_err(netdev, "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d] (increment %d)\n", 2629 ring->tx_pending, ring->rx_pending, 2630 ICE_MIN_NUM_DESC, ICE_MAX_NUM_DESC, 2631 ICE_REQ_DESC_MULTIPLE); 2632 return -EINVAL; 2633 } 2634 2635 new_tx_cnt = ALIGN(ring->tx_pending, ICE_REQ_DESC_MULTIPLE); 2636 if (new_tx_cnt != ring->tx_pending) 2637 netdev_info(netdev, "Requested Tx descriptor count rounded up to %d\n", 2638 new_tx_cnt); 2639 new_rx_cnt = ALIGN(ring->rx_pending, ICE_REQ_DESC_MULTIPLE); 2640 if (new_rx_cnt != ring->rx_pending) 2641 netdev_info(netdev, "Requested Rx descriptor count rounded up to %d\n", 2642 new_rx_cnt); 2643 2644 /* if nothing to do return success */ 2645 if (new_tx_cnt == vsi->tx_rings[0]->count && 2646 new_rx_cnt == vsi->rx_rings[0]->count) { 2647 netdev_dbg(netdev, "Nothing to change, descriptor count is same as requested\n"); 2648 return 0; 2649 } 2650 2651 /* If there is a AF_XDP UMEM attached to any of Rx rings, 2652 * disallow changing the number of descriptors -- regardless 2653 * if the netdev is running or not. 2654 */ 2655 if (ice_xsk_any_rx_ring_ena(vsi)) 2656 return -EBUSY; 2657 2658 while (test_and_set_bit(__ICE_CFG_BUSY, pf->state)) { 2659 timeout--; 2660 if (!timeout) 2661 return -EBUSY; 2662 usleep_range(1000, 2000); 2663 } 2664 2665 /* set for the next time the netdev is started */ 2666 if (!netif_running(vsi->netdev)) { 2667 for (i = 0; i < vsi->alloc_txq; i++) 2668 vsi->tx_rings[i]->count = new_tx_cnt; 2669 for (i = 0; i < vsi->alloc_rxq; i++) 2670 vsi->rx_rings[i]->count = new_rx_cnt; 2671 if (ice_is_xdp_ena_vsi(vsi)) 2672 for (i = 0; i < vsi->num_xdp_txq; i++) 2673 vsi->xdp_rings[i]->count = new_tx_cnt; 2674 vsi->num_tx_desc = (u16)new_tx_cnt; 2675 vsi->num_rx_desc = (u16)new_rx_cnt; 2676 netdev_dbg(netdev, "Link is down, descriptor count change happens when link is brought up\n"); 2677 goto done; 2678 } 2679 2680 if (new_tx_cnt == vsi->tx_rings[0]->count) 2681 goto process_rx; 2682 2683 /* alloc updated Tx resources */ 2684 netdev_info(netdev, "Changing Tx descriptor count from %d to %d\n", 2685 vsi->tx_rings[0]->count, new_tx_cnt); 2686 2687 tx_rings = kcalloc(vsi->num_txq, sizeof(*tx_rings), GFP_KERNEL); 2688 if (!tx_rings) { 2689 err = -ENOMEM; 2690 goto done; 2691 } 2692 2693 ice_for_each_txq(vsi, i) { 2694 /* clone ring and setup updated count */ 2695 tx_rings[i] = *vsi->tx_rings[i]; 2696 tx_rings[i].count = new_tx_cnt; 2697 tx_rings[i].desc = NULL; 2698 tx_rings[i].tx_buf = NULL; 2699 err = ice_setup_tx_ring(&tx_rings[i]); 2700 if (err) { 2701 while (i--) 2702 ice_clean_tx_ring(&tx_rings[i]); 2703 kfree(tx_rings); 2704 goto done; 2705 } 2706 } 2707 2708 if (!ice_is_xdp_ena_vsi(vsi)) 2709 goto process_rx; 2710 2711 /* alloc updated XDP resources */ 2712 netdev_info(netdev, "Changing XDP descriptor count from %d to %d\n", 2713 vsi->xdp_rings[0]->count, new_tx_cnt); 2714 2715 xdp_rings = kcalloc(vsi->num_xdp_txq, sizeof(*xdp_rings), GFP_KERNEL); 2716 if (!xdp_rings) { 2717 err = -ENOMEM; 2718 goto free_tx; 2719 } 2720 2721 for (i = 0; i < vsi->num_xdp_txq; i++) { 2722 /* clone ring and setup updated count */ 2723 xdp_rings[i] = *vsi->xdp_rings[i]; 2724 xdp_rings[i].count = new_tx_cnt; 2725 xdp_rings[i].desc = NULL; 2726 xdp_rings[i].tx_buf = NULL; 2727 err = ice_setup_tx_ring(&xdp_rings[i]); 2728 if (err) { 2729 while (i--) 2730 ice_clean_tx_ring(&xdp_rings[i]); 2731 kfree(xdp_rings); 2732 goto free_tx; 2733 } 2734 ice_set_ring_xdp(&xdp_rings[i]); 2735 } 2736 2737 process_rx: 2738 if (new_rx_cnt == vsi->rx_rings[0]->count) 2739 goto process_link; 2740 2741 /* alloc updated Rx resources */ 2742 netdev_info(netdev, "Changing Rx descriptor count from %d to %d\n", 2743 vsi->rx_rings[0]->count, new_rx_cnt); 2744 2745 rx_rings = kcalloc(vsi->num_rxq, sizeof(*rx_rings), GFP_KERNEL); 2746 if (!rx_rings) { 2747 err = -ENOMEM; 2748 goto done; 2749 } 2750 2751 ice_for_each_rxq(vsi, i) { 2752 /* clone ring and setup updated count */ 2753 rx_rings[i] = *vsi->rx_rings[i]; 2754 rx_rings[i].count = new_rx_cnt; 2755 rx_rings[i].desc = NULL; 2756 rx_rings[i].rx_buf = NULL; 2757 /* this is to allow wr32 to have something to write to 2758 * during early allocation of Rx buffers 2759 */ 2760 rx_rings[i].tail = vsi->back->hw.hw_addr + PRTGEN_STATUS; 2761 2762 err = ice_setup_rx_ring(&rx_rings[i]); 2763 if (err) 2764 goto rx_unwind; 2765 2766 /* allocate Rx buffers */ 2767 err = ice_alloc_rx_bufs(&rx_rings[i], 2768 ICE_DESC_UNUSED(&rx_rings[i])); 2769 rx_unwind: 2770 if (err) { 2771 while (i) { 2772 i--; 2773 ice_free_rx_ring(&rx_rings[i]); 2774 } 2775 kfree(rx_rings); 2776 err = -ENOMEM; 2777 goto free_tx; 2778 } 2779 } 2780 2781 process_link: 2782 /* Bring interface down, copy in the new ring info, then restore the 2783 * interface. if VSI is up, bring it down and then back up 2784 */ 2785 if (!test_and_set_bit(__ICE_DOWN, vsi->state)) { 2786 ice_down(vsi); 2787 2788 if (tx_rings) { 2789 ice_for_each_txq(vsi, i) { 2790 ice_free_tx_ring(vsi->tx_rings[i]); 2791 *vsi->tx_rings[i] = tx_rings[i]; 2792 } 2793 kfree(tx_rings); 2794 } 2795 2796 if (rx_rings) { 2797 ice_for_each_rxq(vsi, i) { 2798 ice_free_rx_ring(vsi->rx_rings[i]); 2799 /* copy the real tail offset */ 2800 rx_rings[i].tail = vsi->rx_rings[i]->tail; 2801 /* this is to fake out the allocation routine 2802 * into thinking it has to realloc everything 2803 * but the recycling logic will let us re-use 2804 * the buffers allocated above 2805 */ 2806 rx_rings[i].next_to_use = 0; 2807 rx_rings[i].next_to_clean = 0; 2808 rx_rings[i].next_to_alloc = 0; 2809 *vsi->rx_rings[i] = rx_rings[i]; 2810 } 2811 kfree(rx_rings); 2812 } 2813 2814 if (xdp_rings) { 2815 for (i = 0; i < vsi->num_xdp_txq; i++) { 2816 ice_free_tx_ring(vsi->xdp_rings[i]); 2817 *vsi->xdp_rings[i] = xdp_rings[i]; 2818 } 2819 kfree(xdp_rings); 2820 } 2821 2822 vsi->num_tx_desc = new_tx_cnt; 2823 vsi->num_rx_desc = new_rx_cnt; 2824 ice_up(vsi); 2825 } 2826 goto done; 2827 2828 free_tx: 2829 /* error cleanup if the Rx allocations failed after getting Tx */ 2830 if (tx_rings) { 2831 ice_for_each_txq(vsi, i) 2832 ice_free_tx_ring(&tx_rings[i]); 2833 kfree(tx_rings); 2834 } 2835 2836 done: 2837 clear_bit(__ICE_CFG_BUSY, pf->state); 2838 return err; 2839 } 2840 2841 /** 2842 * ice_get_pauseparam - Get Flow Control status 2843 * @netdev: network interface device structure 2844 * @pause: ethernet pause (flow control) parameters 2845 * 2846 * Get requested flow control status from PHY capability. 2847 * If autoneg is true, then ethtool will send the ETHTOOL_GSET ioctl which 2848 * is handled by ice_get_link_ksettings. ice_get_link_ksettings will report 2849 * the negotiated Rx/Tx pause via lp_advertising. 2850 */ 2851 static void 2852 ice_get_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause) 2853 { 2854 struct ice_netdev_priv *np = netdev_priv(netdev); 2855 struct ice_port_info *pi = np->vsi->port_info; 2856 struct ice_aqc_get_phy_caps_data *pcaps; 2857 struct ice_dcbx_cfg *dcbx_cfg; 2858 enum ice_status status; 2859 2860 /* Initialize pause params */ 2861 pause->rx_pause = 0; 2862 pause->tx_pause = 0; 2863 2864 dcbx_cfg = &pi->local_dcbx_cfg; 2865 2866 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL); 2867 if (!pcaps) 2868 return; 2869 2870 /* Get current PHY config */ 2871 status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps, 2872 NULL); 2873 if (status) 2874 goto out; 2875 2876 pause->autoneg = ((pcaps->caps & ICE_AQC_PHY_AN_MODE) ? 2877 AUTONEG_ENABLE : AUTONEG_DISABLE); 2878 2879 if (dcbx_cfg->pfc.pfcena) 2880 /* PFC enabled so report LFC as off */ 2881 goto out; 2882 2883 if (pcaps->caps & ICE_AQC_PHY_EN_TX_LINK_PAUSE) 2884 pause->tx_pause = 1; 2885 if (pcaps->caps & ICE_AQC_PHY_EN_RX_LINK_PAUSE) 2886 pause->rx_pause = 1; 2887 2888 out: 2889 kfree(pcaps); 2890 } 2891 2892 /** 2893 * ice_set_pauseparam - Set Flow Control parameter 2894 * @netdev: network interface device structure 2895 * @pause: return Tx/Rx flow control status 2896 */ 2897 static int 2898 ice_set_pauseparam(struct net_device *netdev, struct ethtool_pauseparam *pause) 2899 { 2900 struct ice_netdev_priv *np = netdev_priv(netdev); 2901 struct ice_aqc_get_phy_caps_data *pcaps; 2902 struct ice_link_status *hw_link_info; 2903 struct ice_pf *pf = np->vsi->back; 2904 struct ice_dcbx_cfg *dcbx_cfg; 2905 struct ice_vsi *vsi = np->vsi; 2906 struct ice_hw *hw = &pf->hw; 2907 struct ice_port_info *pi; 2908 enum ice_status status; 2909 u8 aq_failures; 2910 bool link_up; 2911 int err = 0; 2912 u32 is_an; 2913 2914 pi = vsi->port_info; 2915 hw_link_info = &pi->phy.link_info; 2916 dcbx_cfg = &pi->local_dcbx_cfg; 2917 link_up = hw_link_info->link_info & ICE_AQ_LINK_UP; 2918 2919 /* Changing the port's flow control is not supported if this isn't the 2920 * PF VSI 2921 */ 2922 if (vsi->type != ICE_VSI_PF) { 2923 netdev_info(netdev, "Changing flow control parameters only supported for PF VSI\n"); 2924 return -EOPNOTSUPP; 2925 } 2926 2927 /* Get pause param reports configured and negotiated flow control pause 2928 * when ETHTOOL_GLINKSETTINGS is defined. Since ETHTOOL_GLINKSETTINGS is 2929 * defined get pause param pause->autoneg reports SW configured setting, 2930 * so compare pause->autoneg with SW configured to prevent the user from 2931 * using set pause param to chance autoneg. 2932 */ 2933 pcaps = kzalloc(sizeof(*pcaps), GFP_KERNEL); 2934 if (!pcaps) 2935 return -ENOMEM; 2936 2937 /* Get current PHY config */ 2938 status = ice_aq_get_phy_caps(pi, false, ICE_AQC_REPORT_SW_CFG, pcaps, 2939 NULL); 2940 if (status) { 2941 kfree(pcaps); 2942 return -EIO; 2943 } 2944 2945 is_an = ((pcaps->caps & ICE_AQC_PHY_AN_MODE) ? 2946 AUTONEG_ENABLE : AUTONEG_DISABLE); 2947 2948 kfree(pcaps); 2949 2950 if (pause->autoneg != is_an) { 2951 netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n"); 2952 return -EOPNOTSUPP; 2953 } 2954 2955 /* If we have link and don't have autoneg */ 2956 if (!test_bit(__ICE_DOWN, pf->state) && 2957 !(hw_link_info->an_info & ICE_AQ_AN_COMPLETED)) { 2958 /* Send message that it might not necessarily work*/ 2959 netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n"); 2960 } 2961 2962 if (dcbx_cfg->pfc.pfcena) { 2963 netdev_info(netdev, "Priority flow control enabled. Cannot set link flow control.\n"); 2964 return -EOPNOTSUPP; 2965 } 2966 if (pause->rx_pause && pause->tx_pause) 2967 pi->fc.req_mode = ICE_FC_FULL; 2968 else if (pause->rx_pause && !pause->tx_pause) 2969 pi->fc.req_mode = ICE_FC_RX_PAUSE; 2970 else if (!pause->rx_pause && pause->tx_pause) 2971 pi->fc.req_mode = ICE_FC_TX_PAUSE; 2972 else if (!pause->rx_pause && !pause->tx_pause) 2973 pi->fc.req_mode = ICE_FC_NONE; 2974 else 2975 return -EINVAL; 2976 2977 /* Set the FC mode and only restart AN if link is up */ 2978 status = ice_set_fc(pi, &aq_failures, link_up); 2979 2980 if (aq_failures & ICE_SET_FC_AQ_FAIL_GET) { 2981 netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %s aq_err %s\n", 2982 ice_stat_str(status), 2983 ice_aq_str(hw->adminq.sq_last_status)); 2984 err = -EAGAIN; 2985 } else if (aq_failures & ICE_SET_FC_AQ_FAIL_SET) { 2986 netdev_info(netdev, "Set fc failed on the set_phy_config call with err %s aq_err %s\n", 2987 ice_stat_str(status), 2988 ice_aq_str(hw->adminq.sq_last_status)); 2989 err = -EAGAIN; 2990 } else if (aq_failures & ICE_SET_FC_AQ_FAIL_UPDATE) { 2991 netdev_info(netdev, "Set fc failed on the get_link_info call with err %s aq_err %s\n", 2992 ice_stat_str(status), 2993 ice_aq_str(hw->adminq.sq_last_status)); 2994 err = -EAGAIN; 2995 } 2996 2997 return err; 2998 } 2999 3000 /** 3001 * ice_get_rxfh_key_size - get the RSS hash key size 3002 * @netdev: network interface device structure 3003 * 3004 * Returns the table size. 3005 */ 3006 static u32 ice_get_rxfh_key_size(struct net_device __always_unused *netdev) 3007 { 3008 return ICE_VSIQF_HKEY_ARRAY_SIZE; 3009 } 3010 3011 /** 3012 * ice_get_rxfh_indir_size - get the Rx flow hash indirection table size 3013 * @netdev: network interface device structure 3014 * 3015 * Returns the table size. 3016 */ 3017 static u32 ice_get_rxfh_indir_size(struct net_device *netdev) 3018 { 3019 struct ice_netdev_priv *np = netdev_priv(netdev); 3020 3021 return np->vsi->rss_table_size; 3022 } 3023 3024 /** 3025 * ice_get_rxfh - get the Rx flow hash indirection table 3026 * @netdev: network interface device structure 3027 * @indir: indirection table 3028 * @key: hash key 3029 * @hfunc: hash function 3030 * 3031 * Reads the indirection table directly from the hardware. 3032 */ 3033 static int 3034 ice_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key, u8 *hfunc) 3035 { 3036 struct ice_netdev_priv *np = netdev_priv(netdev); 3037 struct ice_vsi *vsi = np->vsi; 3038 struct ice_pf *pf = vsi->back; 3039 int ret = 0, i; 3040 u8 *lut; 3041 3042 if (hfunc) 3043 *hfunc = ETH_RSS_HASH_TOP; 3044 3045 if (!indir) 3046 return 0; 3047 3048 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) { 3049 /* RSS not supported return error here */ 3050 netdev_warn(netdev, "RSS is not configured on this VSI!\n"); 3051 return -EIO; 3052 } 3053 3054 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL); 3055 if (!lut) 3056 return -ENOMEM; 3057 3058 if (ice_get_rss(vsi, key, lut, vsi->rss_table_size)) { 3059 ret = -EIO; 3060 goto out; 3061 } 3062 3063 for (i = 0; i < vsi->rss_table_size; i++) 3064 indir[i] = (u32)(lut[i]); 3065 3066 out: 3067 kfree(lut); 3068 return ret; 3069 } 3070 3071 /** 3072 * ice_set_rxfh - set the Rx flow hash indirection table 3073 * @netdev: network interface device structure 3074 * @indir: indirection table 3075 * @key: hash key 3076 * @hfunc: hash function 3077 * 3078 * Returns -EINVAL if the table specifies an invalid queue ID, otherwise 3079 * returns 0 after programming the table. 3080 */ 3081 static int 3082 ice_set_rxfh(struct net_device *netdev, const u32 *indir, const u8 *key, 3083 const u8 hfunc) 3084 { 3085 struct ice_netdev_priv *np = netdev_priv(netdev); 3086 struct ice_vsi *vsi = np->vsi; 3087 struct ice_pf *pf = vsi->back; 3088 struct device *dev; 3089 u8 *seed = NULL; 3090 3091 dev = ice_pf_to_dev(pf); 3092 if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP) 3093 return -EOPNOTSUPP; 3094 3095 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) { 3096 /* RSS not supported return error here */ 3097 netdev_warn(netdev, "RSS is not configured on this VSI!\n"); 3098 return -EIO; 3099 } 3100 3101 if (key) { 3102 if (!vsi->rss_hkey_user) { 3103 vsi->rss_hkey_user = 3104 devm_kzalloc(dev, ICE_VSIQF_HKEY_ARRAY_SIZE, 3105 GFP_KERNEL); 3106 if (!vsi->rss_hkey_user) 3107 return -ENOMEM; 3108 } 3109 memcpy(vsi->rss_hkey_user, key, ICE_VSIQF_HKEY_ARRAY_SIZE); 3110 seed = vsi->rss_hkey_user; 3111 } 3112 3113 if (!vsi->rss_lut_user) { 3114 vsi->rss_lut_user = devm_kzalloc(dev, vsi->rss_table_size, 3115 GFP_KERNEL); 3116 if (!vsi->rss_lut_user) 3117 return -ENOMEM; 3118 } 3119 3120 /* Each 32 bits pointed by 'indir' is stored with a lut entry */ 3121 if (indir) { 3122 int i; 3123 3124 for (i = 0; i < vsi->rss_table_size; i++) 3125 vsi->rss_lut_user[i] = (u8)(indir[i]); 3126 } else { 3127 ice_fill_rss_lut(vsi->rss_lut_user, vsi->rss_table_size, 3128 vsi->rss_size); 3129 } 3130 3131 if (ice_set_rss(vsi, seed, vsi->rss_lut_user, vsi->rss_table_size)) 3132 return -EIO; 3133 3134 return 0; 3135 } 3136 3137 /** 3138 * ice_get_max_txq - return the maximum number of Tx queues for in a PF 3139 * @pf: PF structure 3140 */ 3141 static int ice_get_max_txq(struct ice_pf *pf) 3142 { 3143 return min_t(int, num_online_cpus(), 3144 pf->hw.func_caps.common_cap.num_txq); 3145 } 3146 3147 /** 3148 * ice_get_max_rxq - return the maximum number of Rx queues for in a PF 3149 * @pf: PF structure 3150 */ 3151 static int ice_get_max_rxq(struct ice_pf *pf) 3152 { 3153 return min_t(int, num_online_cpus(), 3154 pf->hw.func_caps.common_cap.num_rxq); 3155 } 3156 3157 /** 3158 * ice_get_combined_cnt - return the current number of combined channels 3159 * @vsi: PF VSI pointer 3160 * 3161 * Go through all queue vectors and count ones that have both Rx and Tx ring 3162 * attached 3163 */ 3164 static u32 ice_get_combined_cnt(struct ice_vsi *vsi) 3165 { 3166 u32 combined = 0; 3167 int q_idx; 3168 3169 ice_for_each_q_vector(vsi, q_idx) { 3170 struct ice_q_vector *q_vector = vsi->q_vectors[q_idx]; 3171 3172 if (q_vector->rx.ring && q_vector->tx.ring) 3173 combined++; 3174 } 3175 3176 return combined; 3177 } 3178 3179 /** 3180 * ice_get_channels - get the current and max supported channels 3181 * @dev: network interface device structure 3182 * @ch: ethtool channel data structure 3183 */ 3184 static void 3185 ice_get_channels(struct net_device *dev, struct ethtool_channels *ch) 3186 { 3187 struct ice_netdev_priv *np = netdev_priv(dev); 3188 struct ice_vsi *vsi = np->vsi; 3189 struct ice_pf *pf = vsi->back; 3190 3191 /* report maximum channels */ 3192 ch->max_rx = ice_get_max_rxq(pf); 3193 ch->max_tx = ice_get_max_txq(pf); 3194 ch->max_combined = min_t(int, ch->max_rx, ch->max_tx); 3195 3196 /* report current channels */ 3197 ch->combined_count = ice_get_combined_cnt(vsi); 3198 ch->rx_count = vsi->num_rxq - ch->combined_count; 3199 ch->tx_count = vsi->num_txq - ch->combined_count; 3200 3201 /* report other queues */ 3202 ch->other_count = test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1 : 0; 3203 ch->max_other = ch->other_count; 3204 } 3205 3206 /** 3207 * ice_vsi_set_dflt_rss_lut - set default RSS LUT with requested RSS size 3208 * @vsi: VSI to reconfigure RSS LUT on 3209 * @req_rss_size: requested range of queue numbers for hashing 3210 * 3211 * Set the VSI's RSS parameters, configure the RSS LUT based on these. 3212 */ 3213 static int ice_vsi_set_dflt_rss_lut(struct ice_vsi *vsi, int req_rss_size) 3214 { 3215 struct ice_pf *pf = vsi->back; 3216 enum ice_status status; 3217 struct device *dev; 3218 struct ice_hw *hw; 3219 int err = 0; 3220 u8 *lut; 3221 3222 dev = ice_pf_to_dev(pf); 3223 hw = &pf->hw; 3224 3225 if (!req_rss_size) 3226 return -EINVAL; 3227 3228 lut = kzalloc(vsi->rss_table_size, GFP_KERNEL); 3229 if (!lut) 3230 return -ENOMEM; 3231 3232 /* set RSS LUT parameters */ 3233 if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) { 3234 vsi->rss_size = 1; 3235 } else { 3236 struct ice_hw_common_caps *caps = &hw->func_caps.common_cap; 3237 3238 vsi->rss_size = min_t(int, req_rss_size, 3239 BIT(caps->rss_table_entry_width)); 3240 } 3241 3242 /* create/set RSS LUT */ 3243 ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size); 3244 status = ice_aq_set_rss_lut(hw, vsi->idx, vsi->rss_lut_type, lut, 3245 vsi->rss_table_size); 3246 if (status) { 3247 dev_err(dev, "Cannot set RSS lut, err %s aq_err %s\n", 3248 ice_stat_str(status), 3249 ice_aq_str(hw->adminq.sq_last_status)); 3250 err = -EIO; 3251 } 3252 3253 kfree(lut); 3254 return err; 3255 } 3256 3257 /** 3258 * ice_set_channels - set the number channels 3259 * @dev: network interface device structure 3260 * @ch: ethtool channel data structure 3261 */ 3262 static int ice_set_channels(struct net_device *dev, struct ethtool_channels *ch) 3263 { 3264 struct ice_netdev_priv *np = netdev_priv(dev); 3265 struct ice_vsi *vsi = np->vsi; 3266 struct ice_pf *pf = vsi->back; 3267 int new_rx = 0, new_tx = 0; 3268 u32 curr_combined; 3269 3270 /* do not support changing channels in Safe Mode */ 3271 if (ice_is_safe_mode(pf)) { 3272 netdev_err(dev, "Changing channel in Safe Mode is not supported\n"); 3273 return -EOPNOTSUPP; 3274 } 3275 /* do not support changing other_count */ 3276 if (ch->other_count != (test_bit(ICE_FLAG_FD_ENA, pf->flags) ? 1U : 0U)) 3277 return -EINVAL; 3278 3279 if (test_bit(ICE_FLAG_FD_ENA, pf->flags) && pf->hw.fdir_active_fltr) { 3280 netdev_err(dev, "Cannot set channels when Flow Director filters are active\n"); 3281 return -EOPNOTSUPP; 3282 } 3283 3284 curr_combined = ice_get_combined_cnt(vsi); 3285 3286 /* these checks are for cases where user didn't specify a particular 3287 * value on cmd line but we get non-zero value anyway via 3288 * get_channels(); look at ethtool.c in ethtool repository (the user 3289 * space part), particularly, do_schannels() routine 3290 */ 3291 if (ch->rx_count == vsi->num_rxq - curr_combined) 3292 ch->rx_count = 0; 3293 if (ch->tx_count == vsi->num_txq - curr_combined) 3294 ch->tx_count = 0; 3295 if (ch->combined_count == curr_combined) 3296 ch->combined_count = 0; 3297 3298 if (!(ch->combined_count || (ch->rx_count && ch->tx_count))) { 3299 netdev_err(dev, "Please specify at least 1 Rx and 1 Tx channel\n"); 3300 return -EINVAL; 3301 } 3302 3303 new_rx = ch->combined_count + ch->rx_count; 3304 new_tx = ch->combined_count + ch->tx_count; 3305 3306 if (new_rx > ice_get_max_rxq(pf)) { 3307 netdev_err(dev, "Maximum allowed Rx channels is %d\n", 3308 ice_get_max_rxq(pf)); 3309 return -EINVAL; 3310 } 3311 if (new_tx > ice_get_max_txq(pf)) { 3312 netdev_err(dev, "Maximum allowed Tx channels is %d\n", 3313 ice_get_max_txq(pf)); 3314 return -EINVAL; 3315 } 3316 3317 ice_vsi_recfg_qs(vsi, new_rx, new_tx); 3318 3319 if (new_rx && !netif_is_rxfh_configured(dev)) 3320 return ice_vsi_set_dflt_rss_lut(vsi, new_rx); 3321 3322 return 0; 3323 } 3324 3325 enum ice_container_type { 3326 ICE_RX_CONTAINER, 3327 ICE_TX_CONTAINER, 3328 }; 3329 3330 /** 3331 * ice_get_rc_coalesce - get ITR values for specific ring container 3332 * @ec: ethtool structure to fill with driver's coalesce settings 3333 * @c_type: container type, Rx or Tx 3334 * @rc: ring container that the ITR values will come from 3335 * 3336 * Query the device for ice_ring_container specific ITR values. This is 3337 * done per ice_ring_container because each q_vector can have 1 or more rings 3338 * and all of said ring(s) will have the same ITR values. 3339 * 3340 * Returns 0 on success, negative otherwise. 3341 */ 3342 static int 3343 ice_get_rc_coalesce(struct ethtool_coalesce *ec, enum ice_container_type c_type, 3344 struct ice_ring_container *rc) 3345 { 3346 struct ice_pf *pf; 3347 3348 if (!rc->ring) 3349 return -EINVAL; 3350 3351 pf = rc->ring->vsi->back; 3352 3353 switch (c_type) { 3354 case ICE_RX_CONTAINER: 3355 ec->use_adaptive_rx_coalesce = ITR_IS_DYNAMIC(rc->itr_setting); 3356 ec->rx_coalesce_usecs = rc->itr_setting & ~ICE_ITR_DYNAMIC; 3357 ec->rx_coalesce_usecs_high = rc->ring->q_vector->intrl; 3358 break; 3359 case ICE_TX_CONTAINER: 3360 ec->use_adaptive_tx_coalesce = ITR_IS_DYNAMIC(rc->itr_setting); 3361 ec->tx_coalesce_usecs = rc->itr_setting & ~ICE_ITR_DYNAMIC; 3362 break; 3363 default: 3364 dev_dbg(ice_pf_to_dev(pf), "Invalid c_type %d\n", c_type); 3365 return -EINVAL; 3366 } 3367 3368 return 0; 3369 } 3370 3371 /** 3372 * ice_get_q_coalesce - get a queue's ITR/INTRL (coalesce) settings 3373 * @vsi: VSI associated to the queue for getting ITR/INTRL (coalesce) settings 3374 * @ec: coalesce settings to program the device with 3375 * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index 3376 * 3377 * Return 0 on success, and negative under the following conditions: 3378 * 1. Getting Tx or Rx ITR/INTRL (coalesce) settings failed. 3379 * 2. The q_num passed in is not a valid number/index for Tx and Rx rings. 3380 */ 3381 static int 3382 ice_get_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num) 3383 { 3384 if (q_num < vsi->num_rxq && q_num < vsi->num_txq) { 3385 if (ice_get_rc_coalesce(ec, ICE_RX_CONTAINER, 3386 &vsi->rx_rings[q_num]->q_vector->rx)) 3387 return -EINVAL; 3388 if (ice_get_rc_coalesce(ec, ICE_TX_CONTAINER, 3389 &vsi->tx_rings[q_num]->q_vector->tx)) 3390 return -EINVAL; 3391 } else if (q_num < vsi->num_rxq) { 3392 if (ice_get_rc_coalesce(ec, ICE_RX_CONTAINER, 3393 &vsi->rx_rings[q_num]->q_vector->rx)) 3394 return -EINVAL; 3395 } else if (q_num < vsi->num_txq) { 3396 if (ice_get_rc_coalesce(ec, ICE_TX_CONTAINER, 3397 &vsi->tx_rings[q_num]->q_vector->tx)) 3398 return -EINVAL; 3399 } else { 3400 return -EINVAL; 3401 } 3402 3403 return 0; 3404 } 3405 3406 /** 3407 * __ice_get_coalesce - get ITR/INTRL values for the device 3408 * @netdev: pointer to the netdev associated with this query 3409 * @ec: ethtool structure to fill with driver's coalesce settings 3410 * @q_num: queue number to get the coalesce settings for 3411 * 3412 * If the caller passes in a negative q_num then we return coalesce settings 3413 * based on queue number 0, else use the actual q_num passed in. 3414 */ 3415 static int 3416 __ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec, 3417 int q_num) 3418 { 3419 struct ice_netdev_priv *np = netdev_priv(netdev); 3420 struct ice_vsi *vsi = np->vsi; 3421 3422 if (q_num < 0) 3423 q_num = 0; 3424 3425 if (ice_get_q_coalesce(vsi, ec, q_num)) 3426 return -EINVAL; 3427 3428 return 0; 3429 } 3430 3431 static int 3432 ice_get_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec) 3433 { 3434 return __ice_get_coalesce(netdev, ec, -1); 3435 } 3436 3437 static int 3438 ice_get_per_q_coalesce(struct net_device *netdev, u32 q_num, 3439 struct ethtool_coalesce *ec) 3440 { 3441 return __ice_get_coalesce(netdev, ec, q_num); 3442 } 3443 3444 /** 3445 * ice_set_rc_coalesce - set ITR values for specific ring container 3446 * @c_type: container type, Rx or Tx 3447 * @ec: ethtool structure from user to update ITR settings 3448 * @rc: ring container that the ITR values will come from 3449 * @vsi: VSI associated to the ring container 3450 * 3451 * Set specific ITR values. This is done per ice_ring_container because each 3452 * q_vector can have 1 or more rings and all of said ring(s) will have the same 3453 * ITR values. 3454 * 3455 * Returns 0 on success, negative otherwise. 3456 */ 3457 static int 3458 ice_set_rc_coalesce(enum ice_container_type c_type, struct ethtool_coalesce *ec, 3459 struct ice_ring_container *rc, struct ice_vsi *vsi) 3460 { 3461 const char *c_type_str = (c_type == ICE_RX_CONTAINER) ? "rx" : "tx"; 3462 u32 use_adaptive_coalesce, coalesce_usecs; 3463 struct ice_pf *pf = vsi->back; 3464 u16 itr_setting; 3465 3466 if (!rc->ring) 3467 return -EINVAL; 3468 3469 switch (c_type) { 3470 case ICE_RX_CONTAINER: 3471 if (ec->rx_coalesce_usecs_high > ICE_MAX_INTRL || 3472 (ec->rx_coalesce_usecs_high && 3473 ec->rx_coalesce_usecs_high < pf->hw.intrl_gran)) { 3474 netdev_info(vsi->netdev, "Invalid value, %s-usecs-high valid values are 0 (disabled), %d-%d\n", 3475 c_type_str, pf->hw.intrl_gran, 3476 ICE_MAX_INTRL); 3477 return -EINVAL; 3478 } 3479 if (ec->rx_coalesce_usecs_high != rc->ring->q_vector->intrl) { 3480 rc->ring->q_vector->intrl = ec->rx_coalesce_usecs_high; 3481 wr32(&pf->hw, GLINT_RATE(rc->ring->q_vector->reg_idx), 3482 ice_intrl_usec_to_reg(ec->rx_coalesce_usecs_high, 3483 pf->hw.intrl_gran)); 3484 } 3485 3486 use_adaptive_coalesce = ec->use_adaptive_rx_coalesce; 3487 coalesce_usecs = ec->rx_coalesce_usecs; 3488 3489 break; 3490 case ICE_TX_CONTAINER: 3491 use_adaptive_coalesce = ec->use_adaptive_tx_coalesce; 3492 coalesce_usecs = ec->tx_coalesce_usecs; 3493 3494 break; 3495 default: 3496 dev_dbg(ice_pf_to_dev(pf), "Invalid container type %d\n", 3497 c_type); 3498 return -EINVAL; 3499 } 3500 3501 itr_setting = rc->itr_setting & ~ICE_ITR_DYNAMIC; 3502 if (coalesce_usecs != itr_setting && use_adaptive_coalesce) { 3503 netdev_info(vsi->netdev, "%s interrupt throttling cannot be changed if adaptive-%s is enabled\n", 3504 c_type_str, c_type_str); 3505 return -EINVAL; 3506 } 3507 3508 if (coalesce_usecs > ICE_ITR_MAX) { 3509 netdev_info(vsi->netdev, "Invalid value, %s-usecs range is 0-%d\n", 3510 c_type_str, ICE_ITR_MAX); 3511 return -EINVAL; 3512 } 3513 3514 if (use_adaptive_coalesce) { 3515 rc->itr_setting |= ICE_ITR_DYNAMIC; 3516 } else { 3517 /* save the user set usecs */ 3518 rc->itr_setting = coalesce_usecs; 3519 /* device ITR granularity is in 2 usec increments */ 3520 rc->target_itr = ITR_REG_ALIGN(rc->itr_setting); 3521 } 3522 3523 return 0; 3524 } 3525 3526 /** 3527 * ice_set_q_coalesce - set a queue's ITR/INTRL (coalesce) settings 3528 * @vsi: VSI associated to the queue that need updating 3529 * @ec: coalesce settings to program the device with 3530 * @q_num: update ITR/INTRL (coalesce) settings for this queue number/index 3531 * 3532 * Return 0 on success, and negative under the following conditions: 3533 * 1. Setting Tx or Rx ITR/INTRL (coalesce) settings failed. 3534 * 2. The q_num passed in is not a valid number/index for Tx and Rx rings. 3535 */ 3536 static int 3537 ice_set_q_coalesce(struct ice_vsi *vsi, struct ethtool_coalesce *ec, int q_num) 3538 { 3539 if (q_num < vsi->num_rxq && q_num < vsi->num_txq) { 3540 if (ice_set_rc_coalesce(ICE_RX_CONTAINER, ec, 3541 &vsi->rx_rings[q_num]->q_vector->rx, 3542 vsi)) 3543 return -EINVAL; 3544 3545 if (ice_set_rc_coalesce(ICE_TX_CONTAINER, ec, 3546 &vsi->tx_rings[q_num]->q_vector->tx, 3547 vsi)) 3548 return -EINVAL; 3549 } else if (q_num < vsi->num_rxq) { 3550 if (ice_set_rc_coalesce(ICE_RX_CONTAINER, ec, 3551 &vsi->rx_rings[q_num]->q_vector->rx, 3552 vsi)) 3553 return -EINVAL; 3554 } else if (q_num < vsi->num_txq) { 3555 if (ice_set_rc_coalesce(ICE_TX_CONTAINER, ec, 3556 &vsi->tx_rings[q_num]->q_vector->tx, 3557 vsi)) 3558 return -EINVAL; 3559 } else { 3560 return -EINVAL; 3561 } 3562 3563 return 0; 3564 } 3565 3566 /** 3567 * ice_print_if_odd_usecs - print message if user tries to set odd [tx|rx]-usecs 3568 * @netdev: netdev used for print 3569 * @itr_setting: previous user setting 3570 * @use_adaptive_coalesce: if adaptive coalesce is enabled or being enabled 3571 * @coalesce_usecs: requested value of [tx|rx]-usecs 3572 * @c_type_str: either "rx" or "tx" to match user set field of [tx|rx]-usecs 3573 */ 3574 static void 3575 ice_print_if_odd_usecs(struct net_device *netdev, u16 itr_setting, 3576 u32 use_adaptive_coalesce, u32 coalesce_usecs, 3577 const char *c_type_str) 3578 { 3579 if (use_adaptive_coalesce) 3580 return; 3581 3582 itr_setting = ITR_TO_REG(itr_setting); 3583 3584 if (itr_setting != coalesce_usecs && (coalesce_usecs % 2)) 3585 netdev_info(netdev, "User set %s-usecs to %d, device only supports even values. Rounding down and attempting to set %s-usecs to %d\n", 3586 c_type_str, coalesce_usecs, c_type_str, 3587 ITR_REG_ALIGN(coalesce_usecs)); 3588 } 3589 3590 /** 3591 * __ice_set_coalesce - set ITR/INTRL values for the device 3592 * @netdev: pointer to the netdev associated with this query 3593 * @ec: ethtool structure to fill with driver's coalesce settings 3594 * @q_num: queue number to get the coalesce settings for 3595 * 3596 * If the caller passes in a negative q_num then we set the coalesce settings 3597 * for all Tx/Rx queues, else use the actual q_num passed in. 3598 */ 3599 static int 3600 __ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec, 3601 int q_num) 3602 { 3603 struct ice_netdev_priv *np = netdev_priv(netdev); 3604 struct ice_vsi *vsi = np->vsi; 3605 3606 if (q_num < 0) { 3607 struct ice_q_vector *q_vector = vsi->q_vectors[0]; 3608 int v_idx; 3609 3610 if (q_vector) { 3611 ice_print_if_odd_usecs(netdev, q_vector->rx.itr_setting, 3612 ec->use_adaptive_rx_coalesce, 3613 ec->rx_coalesce_usecs, "rx"); 3614 3615 ice_print_if_odd_usecs(netdev, q_vector->tx.itr_setting, 3616 ec->use_adaptive_tx_coalesce, 3617 ec->tx_coalesce_usecs, "tx"); 3618 } 3619 3620 ice_for_each_q_vector(vsi, v_idx) { 3621 /* In some cases if DCB is configured the num_[rx|tx]q 3622 * can be less than vsi->num_q_vectors. This check 3623 * accounts for that so we don't report a false failure 3624 */ 3625 if (v_idx >= vsi->num_rxq && v_idx >= vsi->num_txq) 3626 goto set_complete; 3627 3628 if (ice_set_q_coalesce(vsi, ec, v_idx)) 3629 return -EINVAL; 3630 } 3631 goto set_complete; 3632 } 3633 3634 if (ice_set_q_coalesce(vsi, ec, q_num)) 3635 return -EINVAL; 3636 3637 set_complete: 3638 3639 return 0; 3640 } 3641 3642 static int 3643 ice_set_coalesce(struct net_device *netdev, struct ethtool_coalesce *ec) 3644 { 3645 return __ice_set_coalesce(netdev, ec, -1); 3646 } 3647 3648 static int 3649 ice_set_per_q_coalesce(struct net_device *netdev, u32 q_num, 3650 struct ethtool_coalesce *ec) 3651 { 3652 return __ice_set_coalesce(netdev, ec, q_num); 3653 } 3654 3655 #define ICE_I2C_EEPROM_DEV_ADDR 0xA0 3656 #define ICE_I2C_EEPROM_DEV_ADDR2 0xA2 3657 #define ICE_MODULE_TYPE_SFP 0x03 3658 #define ICE_MODULE_TYPE_QSFP_PLUS 0x0D 3659 #define ICE_MODULE_TYPE_QSFP28 0x11 3660 #define ICE_MODULE_SFF_ADDR_MODE 0x04 3661 #define ICE_MODULE_SFF_DIAG_CAPAB 0x40 3662 #define ICE_MODULE_REVISION_ADDR 0x01 3663 #define ICE_MODULE_SFF_8472_COMP 0x5E 3664 #define ICE_MODULE_SFF_8472_SWAP 0x5C 3665 #define ICE_MODULE_QSFP_MAX_LEN 640 3666 3667 /** 3668 * ice_get_module_info - get SFF module type and revision information 3669 * @netdev: network interface device structure 3670 * @modinfo: module EEPROM size and layout information structure 3671 */ 3672 static int 3673 ice_get_module_info(struct net_device *netdev, 3674 struct ethtool_modinfo *modinfo) 3675 { 3676 struct ice_netdev_priv *np = netdev_priv(netdev); 3677 struct ice_vsi *vsi = np->vsi; 3678 struct ice_pf *pf = vsi->back; 3679 struct ice_hw *hw = &pf->hw; 3680 enum ice_status status; 3681 u8 sff8472_comp = 0; 3682 u8 sff8472_swap = 0; 3683 u8 sff8636_rev = 0; 3684 u8 value = 0; 3685 3686 status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR, 0x00, 0x00, 3687 0, &value, 1, 0, NULL); 3688 if (status) 3689 return -EIO; 3690 3691 switch (value) { 3692 case ICE_MODULE_TYPE_SFP: 3693 status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR, 3694 ICE_MODULE_SFF_8472_COMP, 0x00, 0, 3695 &sff8472_comp, 1, 0, NULL); 3696 if (status) 3697 return -EIO; 3698 status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR, 3699 ICE_MODULE_SFF_8472_SWAP, 0x00, 0, 3700 &sff8472_swap, 1, 0, NULL); 3701 if (status) 3702 return -EIO; 3703 3704 if (sff8472_swap & ICE_MODULE_SFF_ADDR_MODE) { 3705 modinfo->type = ETH_MODULE_SFF_8079; 3706 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN; 3707 } else if (sff8472_comp && 3708 (sff8472_swap & ICE_MODULE_SFF_DIAG_CAPAB)) { 3709 modinfo->type = ETH_MODULE_SFF_8472; 3710 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN; 3711 } else { 3712 modinfo->type = ETH_MODULE_SFF_8079; 3713 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN; 3714 } 3715 break; 3716 case ICE_MODULE_TYPE_QSFP_PLUS: 3717 case ICE_MODULE_TYPE_QSFP28: 3718 status = ice_aq_sff_eeprom(hw, 0, ICE_I2C_EEPROM_DEV_ADDR, 3719 ICE_MODULE_REVISION_ADDR, 0x00, 0, 3720 &sff8636_rev, 1, 0, NULL); 3721 if (status) 3722 return -EIO; 3723 /* Check revision compliance */ 3724 if (sff8636_rev > 0x02) { 3725 /* Module is SFF-8636 compliant */ 3726 modinfo->type = ETH_MODULE_SFF_8636; 3727 modinfo->eeprom_len = ICE_MODULE_QSFP_MAX_LEN; 3728 } else { 3729 modinfo->type = ETH_MODULE_SFF_8436; 3730 modinfo->eeprom_len = ICE_MODULE_QSFP_MAX_LEN; 3731 } 3732 break; 3733 default: 3734 netdev_warn(netdev, "SFF Module Type not recognized.\n"); 3735 return -EINVAL; 3736 } 3737 return 0; 3738 } 3739 3740 /** 3741 * ice_get_module_eeprom - fill buffer with SFF EEPROM contents 3742 * @netdev: network interface device structure 3743 * @ee: EEPROM dump request structure 3744 * @data: buffer to be filled with EEPROM contents 3745 */ 3746 static int 3747 ice_get_module_eeprom(struct net_device *netdev, 3748 struct ethtool_eeprom *ee, u8 *data) 3749 { 3750 struct ice_netdev_priv *np = netdev_priv(netdev); 3751 u8 addr = ICE_I2C_EEPROM_DEV_ADDR; 3752 struct ice_vsi *vsi = np->vsi; 3753 struct ice_pf *pf = vsi->back; 3754 struct ice_hw *hw = &pf->hw; 3755 enum ice_status status; 3756 bool is_sfp = false; 3757 unsigned int i; 3758 u16 offset = 0; 3759 u8 value = 0; 3760 u8 page = 0; 3761 3762 status = ice_aq_sff_eeprom(hw, 0, addr, offset, page, 0, 3763 &value, 1, 0, NULL); 3764 if (status) 3765 return -EIO; 3766 3767 if (!ee || !ee->len || !data) 3768 return -EINVAL; 3769 3770 if (value == ICE_MODULE_TYPE_SFP) 3771 is_sfp = true; 3772 3773 for (i = 0; i < ee->len; i++) { 3774 offset = i + ee->offset; 3775 3776 /* Check if we need to access the other memory page */ 3777 if (is_sfp) { 3778 if (offset >= ETH_MODULE_SFF_8079_LEN) { 3779 offset -= ETH_MODULE_SFF_8079_LEN; 3780 addr = ICE_I2C_EEPROM_DEV_ADDR2; 3781 } 3782 } else { 3783 while (offset >= ETH_MODULE_SFF_8436_LEN) { 3784 /* Compute memory page number and offset. */ 3785 offset -= ETH_MODULE_SFF_8436_LEN / 2; 3786 page++; 3787 } 3788 } 3789 3790 status = ice_aq_sff_eeprom(hw, 0, addr, offset, page, !is_sfp, 3791 &value, 1, 0, NULL); 3792 if (status) 3793 value = 0; 3794 data[i] = value; 3795 } 3796 return 0; 3797 } 3798 3799 static const struct ethtool_ops ice_ethtool_ops = { 3800 .supported_coalesce_params = ETHTOOL_COALESCE_USECS | 3801 ETHTOOL_COALESCE_USE_ADAPTIVE | 3802 ETHTOOL_COALESCE_RX_USECS_HIGH, 3803 .get_link_ksettings = ice_get_link_ksettings, 3804 .set_link_ksettings = ice_set_link_ksettings, 3805 .get_drvinfo = ice_get_drvinfo, 3806 .get_regs_len = ice_get_regs_len, 3807 .get_regs = ice_get_regs, 3808 .get_msglevel = ice_get_msglevel, 3809 .set_msglevel = ice_set_msglevel, 3810 .self_test = ice_self_test, 3811 .get_link = ethtool_op_get_link, 3812 .get_eeprom_len = ice_get_eeprom_len, 3813 .get_eeprom = ice_get_eeprom, 3814 .get_coalesce = ice_get_coalesce, 3815 .set_coalesce = ice_set_coalesce, 3816 .get_strings = ice_get_strings, 3817 .set_phys_id = ice_set_phys_id, 3818 .get_ethtool_stats = ice_get_ethtool_stats, 3819 .get_priv_flags = ice_get_priv_flags, 3820 .set_priv_flags = ice_set_priv_flags, 3821 .get_sset_count = ice_get_sset_count, 3822 .get_rxnfc = ice_get_rxnfc, 3823 .set_rxnfc = ice_set_rxnfc, 3824 .get_ringparam = ice_get_ringparam, 3825 .set_ringparam = ice_set_ringparam, 3826 .nway_reset = ice_nway_reset, 3827 .get_pauseparam = ice_get_pauseparam, 3828 .set_pauseparam = ice_set_pauseparam, 3829 .get_rxfh_key_size = ice_get_rxfh_key_size, 3830 .get_rxfh_indir_size = ice_get_rxfh_indir_size, 3831 .get_rxfh = ice_get_rxfh, 3832 .set_rxfh = ice_set_rxfh, 3833 .get_channels = ice_get_channels, 3834 .set_channels = ice_set_channels, 3835 .get_ts_info = ethtool_op_get_ts_info, 3836 .get_per_queue_coalesce = ice_get_per_q_coalesce, 3837 .set_per_queue_coalesce = ice_set_per_q_coalesce, 3838 .get_fecparam = ice_get_fecparam, 3839 .set_fecparam = ice_set_fecparam, 3840 .get_module_info = ice_get_module_info, 3841 .get_module_eeprom = ice_get_module_eeprom, 3842 }; 3843 3844 static const struct ethtool_ops ice_ethtool_safe_mode_ops = { 3845 .get_link_ksettings = ice_get_link_ksettings, 3846 .set_link_ksettings = ice_set_link_ksettings, 3847 .get_drvinfo = ice_get_drvinfo, 3848 .get_regs_len = ice_get_regs_len, 3849 .get_regs = ice_get_regs, 3850 .get_msglevel = ice_get_msglevel, 3851 .set_msglevel = ice_set_msglevel, 3852 .get_link = ethtool_op_get_link, 3853 .get_eeprom_len = ice_get_eeprom_len, 3854 .get_eeprom = ice_get_eeprom, 3855 .get_strings = ice_get_strings, 3856 .get_ethtool_stats = ice_get_ethtool_stats, 3857 .get_sset_count = ice_get_sset_count, 3858 .get_ringparam = ice_get_ringparam, 3859 .set_ringparam = ice_set_ringparam, 3860 .nway_reset = ice_nway_reset, 3861 .get_channels = ice_get_channels, 3862 }; 3863 3864 /** 3865 * ice_set_ethtool_safe_mode_ops - setup safe mode ethtool ops 3866 * @netdev: network interface device structure 3867 */ 3868 void ice_set_ethtool_safe_mode_ops(struct net_device *netdev) 3869 { 3870 netdev->ethtool_ops = &ice_ethtool_safe_mode_ops; 3871 } 3872 3873 /** 3874 * ice_set_ethtool_ops - setup netdev ethtool ops 3875 * @netdev: network interface device structure 3876 * 3877 * setup netdev ethtool ops with ice specific ops 3878 */ 3879 void ice_set_ethtool_ops(struct net_device *netdev) 3880 { 3881 netdev->ethtool_ops = &ice_ethtool_ops; 3882 } 3883