Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: hisilicon: hns3: use ethtool string helpers

The latter is the preferred way to copy ethtool strings.

Avoids manually incrementing the pointer. Cleans up the code quite well.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
Reviewed-by: Jijie Shao <shaojijie@huawei.com>
Tested-by: Jijie Shao <shaojijie@huawei.com>
Link: https://patch.msgid.link/20241104204823.297277-1-rosenp@gmail.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Rosen Penev and committed by
Jakub Kicinski
4ea3e221 4069dcb7

+44 -81
+1 -1
drivers/net/ethernet/hisilicon/hns3/hnae3.h
··· 677 677 void (*get_mac_stats)(struct hnae3_handle *handle, 678 678 struct hns3_mac_stats *mac_stats); 679 679 void (*get_strings)(struct hnae3_handle *handle, 680 - u32 stringset, u8 *data); 680 + u32 stringset, u8 **data); 681 681 int (*get_sset_count)(struct hnae3_handle *handle, int stringset); 682 682 683 683 void (*get_regs)(struct hnae3_handle *handle, u32 *version,
+3 -8
drivers/net/ethernet/hisilicon/hns3/hns3_common/hclge_comm_tqp_stats.c
··· 36 36 } 37 37 EXPORT_SYMBOL_GPL(hclge_comm_tqps_get_sset_count); 38 38 39 - u8 *hclge_comm_tqps_get_strings(struct hnae3_handle *handle, u8 *data) 39 + void hclge_comm_tqps_get_strings(struct hnae3_handle *handle, u8 **data) 40 40 { 41 41 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 42 - u8 *buff = data; 43 42 u16 i; 44 43 45 44 for (i = 0; i < kinfo->num_tqps; i++) { 46 45 struct hclge_comm_tqp *tqp = 47 46 container_of(kinfo->tqp[i], struct hclge_comm_tqp, q); 48 - snprintf(buff, ETH_GSTRING_LEN, "txq%u_pktnum_rcd", tqp->index); 49 - buff += ETH_GSTRING_LEN; 47 + ethtool_sprintf(data, "txq%u_pktnum_rcd", tqp->index); 50 48 } 51 49 52 50 for (i = 0; i < kinfo->num_tqps; i++) { 53 51 struct hclge_comm_tqp *tqp = 54 52 container_of(kinfo->tqp[i], struct hclge_comm_tqp, q); 55 - snprintf(buff, ETH_GSTRING_LEN, "rxq%u_pktnum_rcd", tqp->index); 56 - buff += ETH_GSTRING_LEN; 53 + ethtool_sprintf(data, "rxq%u_pktnum_rcd", tqp->index); 57 54 } 58 - 59 - return buff; 60 55 } 61 56 EXPORT_SYMBOL_GPL(hclge_comm_tqps_get_strings); 62 57
+1 -1
drivers/net/ethernet/hisilicon/hns3/hns3_common/hclge_comm_tqp_stats.h
··· 32 32 33 33 u64 *hclge_comm_tqps_get_stats(struct hnae3_handle *handle, u64 *data); 34 34 int hclge_comm_tqps_get_sset_count(struct hnae3_handle *handle); 35 - u8 *hclge_comm_tqps_get_strings(struct hnae3_handle *handle, u8 *data); 35 + void hclge_comm_tqps_get_strings(struct hnae3_handle *handle, u8 **data); 36 36 void hclge_comm_reset_tqp_stats(struct hnae3_handle *handle); 37 37 int hclge_comm_tqps_update_stats(struct hnae3_handle *handle, 38 38 struct hclge_comm_hw *hw);
+17 -37
drivers/net/ethernet/hisilicon/hns3/hns3_ethtool.c
··· 509 509 } 510 510 } 511 511 512 - static void *hns3_update_strings(u8 *data, const struct hns3_stats *stats, 513 - u32 stat_count, u32 num_tqps, const char *prefix) 512 + static void hns3_update_strings(u8 **data, const struct hns3_stats *stats, 513 + u32 stat_count, u32 num_tqps, 514 + const char *prefix) 514 515 { 515 - #define MAX_PREFIX_SIZE (6 + 4) 516 - u32 size_left; 517 516 u32 i, j; 518 - u32 n1; 519 517 520 - for (i = 0; i < num_tqps; i++) { 521 - for (j = 0; j < stat_count; j++) { 522 - data[ETH_GSTRING_LEN - 1] = '\0'; 523 - 524 - /* first, prepend the prefix string */ 525 - n1 = scnprintf(data, MAX_PREFIX_SIZE, "%s%u_", 526 - prefix, i); 527 - size_left = (ETH_GSTRING_LEN - 1) - n1; 528 - 529 - /* now, concatenate the stats string to it */ 530 - strncat(data, stats[j].stats_string, size_left); 531 - data += ETH_GSTRING_LEN; 532 - } 533 - } 534 - 535 - return data; 518 + for (i = 0; i < num_tqps; i++) 519 + for (j = 0; j < stat_count; j++) 520 + ethtool_sprintf(data, "%s%u_%s", prefix, i, 521 + stats[j].stats_string); 536 522 } 537 523 538 - static u8 *hns3_get_strings_tqps(struct hnae3_handle *handle, u8 *data) 524 + static void hns3_get_strings_tqps(struct hnae3_handle *handle, u8 **data) 539 525 { 540 526 struct hnae3_knic_private_info *kinfo = &handle->kinfo; 541 527 const char tx_prefix[] = "txq"; 542 528 const char rx_prefix[] = "rxq"; 543 529 544 530 /* get strings for Tx */ 545 - data = hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT, 546 - kinfo->num_tqps, tx_prefix); 531 + hns3_update_strings(data, hns3_txq_stats, HNS3_TXQ_STATS_COUNT, 532 + kinfo->num_tqps, tx_prefix); 547 533 548 534 /* get strings for Rx */ 549 - data = hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT, 550 - kinfo->num_tqps, rx_prefix); 551 - 552 - return data; 535 + hns3_update_strings(data, hns3_rxq_stats, HNS3_RXQ_STATS_COUNT, 536 + kinfo->num_tqps, rx_prefix); 553 537 } 554 538 555 539 static void hns3_get_strings(struct net_device *netdev, u32 stringset, u8 *data) 556 540 { 557 541 struct hnae3_handle *h = hns3_get_handle(netdev); 558 542 const struct hnae3_ae_ops *ops = h->ae_algo->ops; 559 - char *buff = (char *)data; 560 543 int i; 561 544 562 545 if (!ops->get_strings) ··· 547 564 548 565 switch (stringset) { 549 566 case ETH_SS_STATS: 550 - buff = hns3_get_strings_tqps(h, buff); 551 - ops->get_strings(h, stringset, (u8 *)buff); 567 + hns3_get_strings_tqps(h, &data); 568 + ops->get_strings(h, stringset, &data); 552 569 break; 553 570 case ETH_SS_TEST: 554 - ops->get_strings(h, stringset, data); 571 + ops->get_strings(h, stringset, &data); 555 572 break; 556 573 case ETH_SS_PRIV_FLAGS: 557 - for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) { 558 - snprintf(buff, ETH_GSTRING_LEN, "%s", 559 - hns3_priv_flags[i].name); 560 - buff += ETH_GSTRING_LEN; 561 - } 574 + for (i = 0; i < HNS3_PRIV_FLAGS_LEN; i++) 575 + ethtool_puts(&data, hns3_priv_flags[i].name); 562 576 break; 563 577 default: 564 578 break;
+20 -30
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.c
··· 595 595 return buf; 596 596 } 597 597 598 - static u8 *hclge_comm_get_strings(struct hclge_dev *hdev, u32 stringset, 599 - const struct hclge_comm_stats_str strs[], 600 - int size, u8 *data) 598 + static void hclge_comm_get_strings(struct hclge_dev *hdev, u32 stringset, 599 + const struct hclge_comm_stats_str strs[], 600 + int size, u8 **data) 601 601 { 602 - char *buff = (char *)data; 603 602 u32 i; 604 603 605 604 if (stringset != ETH_SS_STATS) 606 - return buff; 605 + return; 607 606 608 607 for (i = 0; i < size; i++) { 609 608 if (strs[i].stats_num > hdev->ae_dev->dev_specs.mac_stats_num) 610 609 continue; 611 610 612 - snprintf(buff, ETH_GSTRING_LEN, "%s", strs[i].desc); 613 - buff = buff + ETH_GSTRING_LEN; 611 + ethtool_puts(data, strs[i].desc); 614 612 } 615 - 616 - return (u8 *)buff; 617 613 } 618 614 619 615 static void hclge_update_stats_for_all(struct hclge_dev *hdev) ··· 714 718 } 715 719 716 720 static void hclge_get_strings(struct hnae3_handle *handle, u32 stringset, 717 - u8 *data) 721 + u8 **data) 718 722 { 719 723 struct hclge_vport *vport = hclge_get_vport(handle); 720 724 struct hclge_dev *hdev = vport->back; 721 - u8 *p = (char *)data; 725 + const char *str; 722 726 int size; 723 727 724 728 if (stringset == ETH_SS_STATS) { 725 729 size = ARRAY_SIZE(g_mac_stats_string); 726 - p = hclge_comm_get_strings(hdev, stringset, g_mac_stats_string, 727 - size, p); 728 - p = hclge_comm_tqps_get_strings(handle, p); 730 + hclge_comm_get_strings(hdev, stringset, g_mac_stats_string, 731 + size, data); 732 + hclge_comm_tqps_get_strings(handle, data); 729 733 } else if (stringset == ETH_SS_TEST) { 730 734 if (handle->flags & HNAE3_SUPPORT_EXTERNAL_LOOPBACK) { 731 - memcpy(p, hns3_nic_test_strs[HNAE3_LOOP_EXTERNAL], 732 - ETH_GSTRING_LEN); 733 - p += ETH_GSTRING_LEN; 735 + str = hns3_nic_test_strs[HNAE3_LOOP_EXTERNAL]; 736 + ethtool_puts(data, str); 734 737 } 735 738 if (handle->flags & HNAE3_SUPPORT_APP_LOOPBACK) { 736 - memcpy(p, hns3_nic_test_strs[HNAE3_LOOP_APP], 737 - ETH_GSTRING_LEN); 738 - p += ETH_GSTRING_LEN; 739 + str = hns3_nic_test_strs[HNAE3_LOOP_APP]; 740 + ethtool_puts(data, str); 739 741 } 740 742 if (handle->flags & HNAE3_SUPPORT_SERDES_SERIAL_LOOPBACK) { 741 - memcpy(p, hns3_nic_test_strs[HNAE3_LOOP_SERIAL_SERDES], 742 - ETH_GSTRING_LEN); 743 - p += ETH_GSTRING_LEN; 743 + str = hns3_nic_test_strs[HNAE3_LOOP_SERIAL_SERDES]; 744 + ethtool_puts(data, str); 744 745 } 745 746 if (handle->flags & HNAE3_SUPPORT_SERDES_PARALLEL_LOOPBACK) { 746 - memcpy(p, 747 - hns3_nic_test_strs[HNAE3_LOOP_PARALLEL_SERDES], 748 - ETH_GSTRING_LEN); 749 - p += ETH_GSTRING_LEN; 747 + str = hns3_nic_test_strs[HNAE3_LOOP_PARALLEL_SERDES]; 748 + ethtool_puts(data, str); 750 749 } 751 750 if (handle->flags & HNAE3_SUPPORT_PHY_LOOPBACK) { 752 - memcpy(p, hns3_nic_test_strs[HNAE3_LOOP_PHY], 753 - ETH_GSTRING_LEN); 754 - p += ETH_GSTRING_LEN; 751 + str = hns3_nic_test_strs[HNAE3_LOOP_PHY]; 752 + ethtool_puts(data, str); 755 753 } 756 754 } 757 755 }
+2 -4
drivers/net/ethernet/hisilicon/hns3/hns3vf/hclgevf_main.c
··· 130 130 } 131 131 132 132 static void hclgevf_get_strings(struct hnae3_handle *handle, u32 strset, 133 - u8 *data) 133 + u8 **data) 134 134 { 135 - u8 *p = (char *)data; 136 - 137 135 if (strset == ETH_SS_STATS) 138 - p = hclge_comm_tqps_get_strings(handle, p); 136 + hclge_comm_tqps_get_strings(handle, data); 139 137 } 140 138 141 139 static void hclgevf_get_stats(struct hnae3_handle *handle, u64 *data)