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