Commit 82dc8c56 authored by Junxin Chen's avatar Junxin Chen Committed by mufengyan
Browse files

unic: deleted redundant code and added default processing

driver inclusion
category: cleancode
bugzilla: https://gitee.com/openeuler/kernel/issues/IAKMX4


CVE: NA

----------------------------------------------------------------

Currently, there are some issues in the driver code, such as unused
function parameters, redundant code implementation, and undefined
default branches in switches.
This patch addresses these issues while also fixing some problems
that do not affect functionality but do affect code readability,
such as relocating the unic debugfs and fastpatch code.

Fixes: 1cdab773 ("UNIC: Supports query, configuration, and management of IP entry")
Signed-off-by: default avatarHaibin Lu <luhaibin10@hisilicon.com>
Signed-off-by: default avatarJunxin Chen <chenjunxin1@huawei.com>
parent c12d1b0e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -33,6 +33,6 @@ hclge-objs = hns3pf/hclge_main.o hns3pf/hclge_mdio.o hns3pf/hclge_tm.o hns3pf/hc
hclge-objs += hns3pf/hclge_ext.o

hclge-$(CONFIG_HNS3_UBL) += hns3_common/hclge_comm_unic_addr.o hns3pf/hclge_unic_ip.o hns3pf/hclge_unic_guid.o \
			hns3pf/hclge_unic_addr.o
			hns3pf/hclge_unic_addr.o hns3pf/hclge_unic_debugfs.o
hclge-$(CONFIG_UB_UDMA_HNS3) += hns3pf/hclge_udma.o
hclge-$(CONFIG_HNS3_DCB) += hns3pf/hclge_dcb.o
+2 −0
Original line number Diff line number Diff line
@@ -383,11 +383,13 @@ enum hnae3_dbg_cmd {
	HNAE3_DBG_CMD_PAGE_POOL_INFO,
	HNAE3_DBG_CMD_COAL_INFO,
	HNAE3_DBG_CMD_WOL_INFO,
#ifdef CONFIG_HNS3_UBL
	HNAE3_DBG_CMD_IP_SPEC,
	HNAE3_DBG_CMD_GUID_SPEC,
	HNAE3_DBG_CMD_IP_LIST,
	HNAE3_DBG_CMD_GUID_LIST,
	HNAE3_DBG_CMD_FASTPATH_INFO,
#endif
	HNAE3_DBG_CMD_UNKNOWN,
};

+2 −0
Original line number Diff line number Diff line
@@ -53,6 +53,8 @@ hclge_comm_unic_update_addr_node(struct hclge_comm_unic_addr_node *addr_node,
		if (addr_node->state == HCLGE_COMM_UNIC_ADDR_TO_ADD)
			addr_node->state = HCLGE_COMM_UNIC_ADDR_ACTIVE;
		break;
	default:
		break;
	}
}

+9 −9
Original line number Diff line number Diff line
@@ -101,8 +101,8 @@ u8 hns3_unic_get_l3_type(struct net_device *netdev, u32 ol_info, u32 l234info)
	return UB_UNKNOWN_CFG_TYPE;
}

static int addr_event(struct notifier_block *nb, unsigned long event,
		      struct sockaddr *sa, struct net_device *ndev)
static int addr_event(unsigned long event, struct sockaddr *sa,
		      struct net_device *ndev)
{
	struct hnae3_handle *handle;
	int ret;
@@ -153,7 +153,7 @@ static int unic_inetaddr_event(struct notifier_block *this, unsigned long event,
	in.sin_family = AF_INET;
	in.sin_addr.s_addr = ifa4->ifa_address;

	return addr_event(this, event, (struct sockaddr *)&in, ndev);
	return addr_event(event, (struct sockaddr *)&in, ndev);
}

static int unic_inet6addr_event(struct notifier_block *this, unsigned long event,
@@ -166,7 +166,7 @@ static int unic_inet6addr_event(struct notifier_block *this, unsigned long event
	in6.sin6_family = AF_INET6;
	in6.sin6_addr = ifa6->addr;

	return addr_event(this, event, (struct sockaddr *)&in6, ndev);
	return addr_event(event, (struct sockaddr *)&in6, ndev);
}

static struct notifier_block unic_inetaddr_notifier = {
@@ -179,14 +179,14 @@ static struct notifier_block unic_inet6addr_notifier = {

void register_ipaddr_notifier(void)
{
	register_inetaddr_notifier(&unic_inetaddr_notifier);
	register_inet6addr_notifier(&unic_inet6addr_notifier);
	(void)register_inetaddr_notifier(&unic_inetaddr_notifier);
	(void)register_inet6addr_notifier(&unic_inet6addr_notifier);
}

void unregister_ipaddr_notifier(void)
{
	unregister_inetaddr_notifier(&unic_inetaddr_notifier);
	unregister_inet6addr_notifier(&unic_inet6addr_notifier);
	(void)unregister_inetaddr_notifier(&unic_inetaddr_notifier);
	(void)unregister_inet6addr_notifier(&unic_inet6addr_notifier);
}

#define UNIC_DHCPV4_PROTO 0x0100
@@ -251,7 +251,7 @@ void hns3_unic_lb_check_skb_data(struct hns3_enet_ring *ring,
	if (is_success)
		tqp_vector->rx_group.total_packets++;
	else
		print_hex_dump(KERN_ERR, "ubl selftest:", DUMP_PREFIX_OFFSET,
		print_hex_dump(KERN_ERR, "selftest:", DUMP_PREFIX_OFFSET,
			       HNS3_UNIC_DUMP_ROW_SIZE, 1, skb->data, len, true);

	dev_kfree_skb_any(skb);
+7 −180
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@
#include "hclge_debugfs.h"
#include "hclge_err.h"
#include "hclge_main.h"
#include "hclge_comm_unic_addr.h"
#include "hclge_unic_debugfs.h"
#include "hclge_regs.h"
#include "hclge_tm.h"
#include "hclge_udma.h"
@@ -726,7 +726,7 @@ static const struct hclge_dbg_reg_type_info hclge_dbg_reg_info[] = {
 * for example, name = "pkt_num"(len: 7), the prototype of item data is u32,
 * and print as "%u"(maxlen: 10), so the interval should be at least 5.
 */
static void hclge_dbg_fill_content(char *content, u16 len,
void hclge_dbg_fill_content(char *content, u16 len,
			    const struct hclge_dbg_item *items,
			    const char **result, u16 size)
{
@@ -764,7 +764,7 @@ static void hclge_dbg_fill_content(char *content, u16 len,
	*pos++ = '\0';
}

static char *hclge_dbg_get_func_id_str(char *buf, u8 id)
char *hclge_dbg_get_func_id_str(char *buf, u8 id)
{
	if (id)
		sprintf(buf, "vf%u", id - 1U);
@@ -3221,181 +3221,6 @@ static int hclge_dbg_dump_wol_info(struct hclge_dev *hdev, char *buf, int len)
	return 0;
}

static int hclge_dbg_dump_ip_spec(struct hclge_dev *hdev, char *buf, int len)
{
	struct unic_ip_table_info *iptbl_info = &hdev->iptbl_info;
	u8 func_num = pci_num_vf(hdev->pdev) + 1;
	struct hclge_vport *vport;
	int pos = 0;
	u8 i;

	pos += scnprintf(buf, len, "num_alloc_vport       : %u\n",
			 hdev->num_alloc_vport);
	pos += scnprintf(buf + pos, len - pos, "max_ip_table_size     : %u\n",
			 iptbl_info->max_iptbl_size);
	pos += scnprintf(buf + pos, len - pos, "priv_ip_table_size    : %u\n",
			 iptbl_info->priv_iptbl_size);

	mutex_lock(&hdev->vport_lock);
	pos += scnprintf(buf + pos, len - pos, "share_ip_table_size   : %u\n",
			 iptbl_info->share_iptbl_size);
	for (i = 0; i < func_num; i++) {
		vport = &hdev->vport[i];
		pos += scnprintf(buf + pos, len - pos,
				 "vport(%u) used_ip_table_num : %u\n",
				 i, vport->used_iptbl_num);
	}
	mutex_unlock(&hdev->vport_lock);

	return 0;
}

static int hclge_dbg_dump_guid_spec(struct hclge_dev *hdev, char *buf, int len)
{
	u16 mc_guid_tbl_size;

	mc_guid_tbl_size = min(HCLGE_UNIC_MC_GUID_NUM,
			       hdev->ae_dev->dev_specs.guid_tbl_space -
			       HCLGE_VPORT_NUM);
	scnprintf(buf, len, "function guid tbl size: %u\nmc guid tbl size: %u\n",
		  HCLGE_VPORT_NUM, mc_guid_tbl_size);

	return 0;
}

#define HCLGE_UNIC_DBG_DATA_STR_LEN	50
#define HCLGE_UNIC_IPV6_LEN		16

static const struct hclge_dbg_item ip_list_items[] = {
	{ "FUNC_ID", 2 },
	{ "IP_ADDR", 34 },
	{ "STATE", 2 },
};

static int hclge_dbg_dump_ip_list(struct hclge_dev *hdev, char *buf, int len)
{
	char data_str[ARRAY_SIZE(ip_list_items)][HCLGE_UNIC_DBG_DATA_STR_LEN];
	char content[HCLGE_DBG_INFO_LEN], str_id[HCLGE_DBG_ID_LEN];
	struct hclge_comm_unic_addr_node *ip_node, *tmp;
	char *result[ARRAY_SIZE(ip_list_items)];
	struct hclge_vport *vport;
	struct list_head *list;
	u16 used_iptbl_num = 0;
	u32 func_id;
	int pos = 0;
	int i;

	for (i = 0; i < ARRAY_SIZE(ip_list_items); i++)
		result[i] = &data_str[i][0];

	for (i = 0; i < hdev->num_alloc_vport; i++)
		used_iptbl_num += hdev->vport[i].used_iptbl_num;

	pos += scnprintf(buf + pos, len - pos, "used ip number: %u\n",
			 used_iptbl_num);

	hclge_dbg_fill_content(content, sizeof(content), ip_list_items,
			       NULL, ARRAY_SIZE(ip_list_items));
	pos += scnprintf(buf + pos, len - pos, "%s", content);

	for (func_id = 0; func_id < hdev->num_alloc_vport; func_id++) {
		vport = &hdev->vport[func_id];
		list = &vport->ip_list;
		spin_lock_bh(&vport->ip_list_lock);
		list_for_each_entry_safe(ip_node, tmp, list, node) {
			i = 0;
			result[i++] = hclge_dbg_get_func_id_str(str_id,
								func_id);
			sprintf(result[i++], "%pI6c", &ip_node->ip_addr.s6_addr);
			sprintf(result[i++], "%5s",
				hclge_entry_state_str[ip_node->state]);
			hclge_dbg_fill_content(content, sizeof(content),
					       ip_list_items,
					       (const char **)result,
					       ARRAY_SIZE(ip_list_items));

			if (len - pos < strlen(content)) {
				spin_unlock_bh(&vport->ip_list_lock);
				dev_warn(&hdev->pdev->dev,
					 "Warning: IP list debugfs buffer overflow.\n");
				return 0;
			}

			pos += scnprintf(buf + pos, len - pos, "%s", content);
		}
		spin_unlock_bh(&vport->ip_list_lock);
	}
	return 0;
}

static int hclge_dbg_dump_guid_list(struct hclge_dev *hdev, char *buf, int len)
{
	char format_guid_addr[HCLGE_COMM_FORMAT_GUID_ADDR_LEN];
	struct hclge_comm_unic_addr_node *guid_node, *tmp;
	char str_id[HCLGE_DBG_ID_LEN];
	struct hclge_vport *vport;
	struct list_head *list;
	u16 func_id;
	int pos = 0;
	u16 i;

	pos += scnprintf(buf + pos, len - pos, "used mc guid number: %u\n",
			 hdev->used_mc_guid_num);
	pos += scnprintf(buf + pos, len - pos, "mc guid table bitmap: ");
	for (i = 0; i < BITS_TO_LONGS(HCLGE_UNIC_MC_GUID_NUM); i++)
		pos += scnprintf(buf + pos, len - pos, "%lx ",
				 hdev->mc_guid_tbl_bmap[i]);
	pos += scnprintf(buf + pos, len - pos, "\nMC GUID LIST:\n");
	pos += scnprintf(buf + pos, len - pos, "No. FUNC_ID %-48s STATE\n", "MC_GUID");
	for (func_id = 0, i = 0; func_id < hdev->num_alloc_vport; func_id++) {
		vport = &hdev->vport[func_id];
		list = &vport->mc_guid_list;
		spin_lock_bh(&vport->mguid_list_lock);
		list_for_each_entry_safe(guid_node, tmp, list, node) {
			hclge_comm_format_guid_addr(format_guid_addr,
						    guid_node->mguid);
			pos += scnprintf(buf + pos, len - pos,
					 "%-3d %-7s %-48s %s\n", i++,
					 hclge_dbg_get_func_id_str(str_id, func_id),
					 format_guid_addr,
					 hclge_entry_state_str[guid_node->state]);
		}
		spin_unlock_bh(&vport->mguid_list_lock);
	}
	return 0;
}

static int hclge_dbg_dump_fastpath_info(struct hclge_dev *hdev, char *buf,
					int len)
{
	struct hclge_config_fastpath_cmd *fp_info;
	struct hclge_desc desc;
	int pos = 0;
	int ret;

	hclge_cmd_setup_basic_desc(&desc, HCLGE_OPC_COMM_CFG_FASTPATH, true);

	ret = hclge_cmd_send(&hdev->hw, &desc, 1);
	if (ret) {
		dev_err(&hdev->pdev->dev,
			"failed to dump fastpath_info, ret = %d\n", ret);
		return ret;
	}

	fp_info = (struct hclge_config_fastpath_cmd *)desc.data;

	pos += scnprintf(buf + pos, len - pos, "fastpath_en: %u\n",
			 fp_info->fastpath_en);
	pos += scnprintf(buf + pos, len - pos, "ssu_cfg_status: 0x%x\n",
			 le32_to_cpu(fp_info->ssu_cfg_status));
	pos += scnprintf(buf + pos, len - pos, "igu_cfg_status: 0x%x\n",
			 le32_to_cpu(fp_info->igu_cfg_status));
	pos += scnprintf(buf + pos, len - pos, "ppp_cfg_status: 0x%x\n",
			 le32_to_cpu(fp_info->ppp_cfg_status));

	return 0;
}

static const struct hclge_dbg_func hclge_dbg_cmd_func[] = {
	{
		.cmd = HNAE3_DBG_CMD_TM_NODES,
@@ -3549,6 +3374,7 @@ static const struct hclge_dbg_func hclge_dbg_cmd_func[] = {
		.cmd = HNAE3_DBG_CMD_WOL_INFO,
		.dbg_dump = hclge_dbg_dump_wol_info,
	},
#ifdef CONFIG_HNS3_UBL
	{
		.cmd = HNAE3_DBG_CMD_IP_SPEC,
		.dbg_dump = hclge_dbg_dump_ip_spec,
@@ -3569,6 +3395,7 @@ static const struct hclge_dbg_func hclge_dbg_cmd_func[] = {
		.cmd = HNAE3_DBG_CMD_FASTPATH_INFO,
		.dbg_dump = hclge_dbg_dump_fastpath_info,
	},
#endif
};

int hclge_dbg_read_cmd(struct hnae3_handle *handle, enum hnae3_dbg_cmd cmd,
Loading