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