DPDK patches and discussions
 help / color / mirror / Atom feed
From: "Min Hu (Connor)" <humin29@huawei.com>
To: <dev@dpdk.org>
Cc: <ferruh.yigit@intel.com>, <thomas@monjalon.net>
Subject: [PATCH 03/15] net/hns3: extract a common API to initialize MAC addrs
Date: Fri, 7 Jan 2022 18:15:46 +0800	[thread overview]
Message-ID: <20220107101558.39219-4-humin29@huawei.com> (raw)
In-Reply-To: <20220107101558.39219-1-humin29@huawei.com>

From: Huisong Li <lihuisong@huawei.com>

The code logic to initialize "data->mac_addrs" for PF and VF is similar.
This patch extracts a common API to initialize it to improve code
maintainabiliy.

Signed-off-by: Huisong Li <lihuisong@huawei.com>
---
 drivers/net/hns3/hns3_common.c    | 54 +++++++++++++++++++++++++++++++
 drivers/net/hns3/hns3_common.h    |  1 +
 drivers/net/hns3/hns3_ethdev.c    | 31 +++---------------
 drivers/net/hns3/hns3_ethdev_vf.c | 33 +++----------------
 4 files changed, 63 insertions(+), 56 deletions(-)

diff --git a/drivers/net/hns3/hns3_common.c b/drivers/net/hns3/hns3_common.c
index eac2aa1040..cb581bb25a 100644
--- a/drivers/net/hns3/hns3_common.c
+++ b/drivers/net/hns3/hns3_common.c
@@ -581,6 +581,60 @@ hns3_remove_mac_addr(struct rte_eth_dev *dev, uint32_t idx)
 	}
 }
 
+int
+hns3_init_mac_addrs(struct rte_eth_dev *dev)
+{
+	struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
+	struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
+	const char *memory_name = hns->is_vf ? "hns3vf-mac" : "hns3-mac";
+	uint16_t mac_addrs_capa = hns->is_vf ? HNS3_VF_UC_MACADDR_NUM :
+						HNS3_UC_MACADDR_NUM;
+	char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
+	struct rte_ether_addr *eth_addr;
+
+	/* Allocate memory for storing MAC addresses */
+	dev->data->mac_addrs = rte_zmalloc(memory_name,
+				sizeof(struct rte_ether_addr) * mac_addrs_capa,
+				0);
+	if (dev->data->mac_addrs == NULL) {
+		hns3_err(hw, "failed to allocate %zx bytes needed to store MAC addresses",
+			     sizeof(struct rte_ether_addr) * mac_addrs_capa);
+		return -ENOMEM;
+	}
+
+	eth_addr = (struct rte_ether_addr *)hw->mac.mac_addr;
+	if (!hns->is_vf) {
+		if (!rte_is_valid_assigned_ether_addr(eth_addr)) {
+			rte_eth_random_addr(hw->mac.mac_addr);
+			hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
+				(struct rte_ether_addr *)hw->mac.mac_addr);
+			hns3_warn(hw, "default mac_addr from firmware is an invalid "
+				  "unicast address, using random MAC address %s",
+				  mac_str);
+		}
+	} else {
+		/*
+		 * The hns3 PF ethdev driver in kernel support setting VF MAC
+		 * address on the host by "ip link set ..." command. To avoid
+		 * some incorrect scenes, for example, hns3 VF PMD driver fails
+		 * to receive and send packets after user configure the MAC
+		 * address by using the "ip link set ..." command, hns3 VF PMD
+		 * driver keep the same MAC address strategy as the hns3 kernel
+		 * ethdev driver in the initialization. If user configure a MAC
+		 * address by the ip command for VF device, then hns3 VF PMD
+		 * driver will start with it, otherwise start with a random MAC
+		 * address in the initialization.
+		 */
+		if (rte_is_zero_ether_addr(eth_addr))
+			rte_eth_random_addr(hw->mac.mac_addr);
+	}
+
+	rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
+			    &dev->data->mac_addrs[0]);
+
+	return 0;
+}
+
 int
 hns3_init_ring_with_vector(struct hns3_hw *hw)
 {
diff --git a/drivers/net/hns3/hns3_common.h b/drivers/net/hns3/hns3_common.h
index 0dbb1c0413..a9e8a9cccf 100644
--- a/drivers/net/hns3/hns3_common.h
+++ b/drivers/net/hns3/hns3_common.h
@@ -52,6 +52,7 @@ int hns3_set_mc_mac_addr_list(struct rte_eth_dev *dev,
 			      uint32_t nb_mc_addr);
 void hns3_ether_format_addr(char *buf, uint16_t size,
 			    const struct rte_ether_addr *ether_addr);
+int hns3_init_mac_addrs(struct rte_eth_dev *dev);
 
 int hns3_init_ring_with_vector(struct hns3_hw *hw);
 int hns3_map_rx_interrupt(struct rte_eth_dev *dev);
diff --git a/drivers/net/hns3/hns3_ethdev.c b/drivers/net/hns3/hns3_ethdev.c
index 0bd12907d8..44bd7e8b22 100644
--- a/drivers/net/hns3/hns3_ethdev.c
+++ b/drivers/net/hns3/hns3_ethdev.c
@@ -6623,8 +6623,6 @@ static int
 hns3_dev_init(struct rte_eth_dev *eth_dev)
 {
 	struct hns3_adapter *hns = eth_dev->data->dev_private;
-	char mac_str[RTE_ETHER_ADDR_FMT_SIZE];
-	struct rte_ether_addr *eth_addr;
 	struct hns3_hw *hw = &hns->hw;
 	int ret;
 
@@ -6667,30 +6665,9 @@ hns3_dev_init(struct rte_eth_dev *eth_dev)
 		goto err_init_pf;
 	}
 
-	/* Allocate memory for storing MAC addresses */
-	eth_dev->data->mac_addrs = rte_zmalloc("hns3-mac",
-					       sizeof(struct rte_ether_addr) *
-					       HNS3_UC_MACADDR_NUM, 0);
-	if (eth_dev->data->mac_addrs == NULL) {
-		PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
-			     "to store MAC addresses",
-			     sizeof(struct rte_ether_addr) *
-			     HNS3_UC_MACADDR_NUM);
-		ret = -ENOMEM;
-		goto err_rte_zmalloc;
-	}
-
-	eth_addr = (struct rte_ether_addr *)hw->mac.mac_addr;
-	if (!rte_is_valid_assigned_ether_addr(eth_addr)) {
-		rte_eth_random_addr(hw->mac.mac_addr);
-		hns3_ether_format_addr(mac_str, RTE_ETHER_ADDR_FMT_SIZE,
-				(struct rte_ether_addr *)hw->mac.mac_addr);
-		hns3_warn(hw, "default mac_addr from firmware is an invalid "
-			  "unicast address, using random MAC address %s",
-			  mac_str);
-	}
-	rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
-			    &eth_dev->data->mac_addrs[0]);
+	ret = hns3_init_mac_addrs(eth_dev);
+	if (ret != 0)
+		goto err_init_mac_addrs;
 
 	hw->adapter_state = HNS3_NIC_INITIALIZED;
 
@@ -6706,7 +6683,7 @@ hns3_dev_init(struct rte_eth_dev *eth_dev)
 	hns3_info(hw, "hns3 dev initialization successful!");
 	return 0;
 
-err_rte_zmalloc:
+err_init_mac_addrs:
 	hns3_uninit_pf(eth_dev);
 
 err_init_pf:
diff --git a/drivers/net/hns3/hns3_ethdev_vf.c b/drivers/net/hns3/hns3_ethdev_vf.c
index 805abd4543..58b4a107d3 100644
--- a/drivers/net/hns3/hns3_ethdev_vf.c
+++ b/drivers/net/hns3/hns3_ethdev_vf.c
@@ -2400,34 +2400,9 @@ hns3vf_dev_init(struct rte_eth_dev *eth_dev)
 		goto err_init_vf;
 	}
 
-	/* Allocate memory for storing MAC addresses */
-	eth_dev->data->mac_addrs = rte_zmalloc("hns3vf-mac",
-					       sizeof(struct rte_ether_addr) *
-					       HNS3_VF_UC_MACADDR_NUM, 0);
-	if (eth_dev->data->mac_addrs == NULL) {
-		PMD_INIT_LOG(ERR, "Failed to allocate %zx bytes needed "
-			     "to store MAC addresses",
-			     sizeof(struct rte_ether_addr) *
-			     HNS3_VF_UC_MACADDR_NUM);
-		ret = -ENOMEM;
-		goto err_rte_zmalloc;
-	}
-
-	/*
-	 * The hns3 PF ethdev driver in kernel support setting VF MAC address
-	 * on the host by "ip link set ..." command. To avoid some incorrect
-	 * scenes, for example, hns3 VF PMD fails to receive and send
-	 * packets after user configure the MAC address by using the
-	 * "ip link set ..." command, hns3 VF PMD keep the same MAC
-	 * address strategy as the hns3 kernel ethdev driver in the
-	 * initialization. If user configure a MAC address by the ip command
-	 * for VF device, then hns3 VF PMD will start with it, otherwise
-	 * start with a random MAC address in the initialization.
-	 */
-	if (rte_is_zero_ether_addr((struct rte_ether_addr *)hw->mac.mac_addr))
-		rte_eth_random_addr(hw->mac.mac_addr);
-	rte_ether_addr_copy((struct rte_ether_addr *)hw->mac.mac_addr,
-			    &eth_dev->data->mac_addrs[0]);
+	ret = hns3_init_mac_addrs(eth_dev);
+	if (ret != 0)
+		goto err_init_mac_addrs;
 
 	hw->adapter_state = HNS3_NIC_INITIALIZED;
 
@@ -2443,7 +2418,7 @@ hns3vf_dev_init(struct rte_eth_dev *eth_dev)
 			  eth_dev);
 	return 0;
 
-err_rte_zmalloc:
+err_init_mac_addrs:
 	hns3vf_uninit_vf(eth_dev);
 
 err_init_vf:
-- 
2.33.0


  parent reply	other threads:[~2022-01-07 10:16 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-07 10:15 [PATCH 00/15] fix and feature for hns3 PMD Min Hu (Connor)
2022-01-07 10:15 ` [PATCH 01/15] net/hns3: remove unnecessary assignment Min Hu (Connor)
2022-01-07 10:15 ` [PATCH 02/15] net/hns3: fix a misjudgment expression Min Hu (Connor)
2022-01-07 10:15 ` Min Hu (Connor) [this message]
2022-01-07 10:15 ` [PATCH 04/15] net/hns3: remove unnecessary 'inline' Min Hu (Connor)
2022-01-07 10:15 ` [PATCH 05/15] net/hns3: remove unnecessary black lines Min Hu (Connor)
2022-01-07 10:15 ` [PATCH 06/15] net/hns3: extract a function to handle reset fail Min Hu (Connor)
2022-01-07 10:15 ` [PATCH 07/15] net/hns3: extract functions to create RSS and FDIR flow rule Min Hu (Connor)
2022-01-07 10:15 ` [PATCH 08/15] net/hns3: remove unused variables Min Hu (Connor)
2022-01-07 10:15 ` [PATCH 09/15] net/hns3: remove the number of queue descriptors Min Hu (Connor)
2022-01-07 10:15 ` [PATCH 10/15] net/hns3: remove the printing of memory addresses Min Hu (Connor)
2022-01-07 10:15 ` [PATCH 11/15] net/hns3: extract a common interface to obtain revision ID Min Hu (Connor)
2022-01-07 10:15 ` [PATCH 12/15] net/hns3: remove invalid encapsulation function Min Hu (Connor)
2022-01-07 10:15 ` [PATCH 13/15] net/hns3: delete strerror invoke Min Hu (Connor)
2022-01-07 10:15 ` [PATCH 14/15] net/hns3: rename function Min Hu (Connor)
2022-01-07 10:15 ` [PATCH 15/15] net/hns3: support indirect counter action Min Hu (Connor)
2022-01-21 17:29 ` [PATCH 00/15] fix and feature for hns3 PMD Ferruh Yigit
2022-01-22  1:52   ` Min Hu (Connor)
2022-01-22  1:51 ` [PATCH v2 " Min Hu (Connor)
2022-01-22  1:51   ` [PATCH v2 01/15] net/hns3: remove unnecessary assignment Min Hu (Connor)
2022-01-22  1:51   ` [PATCH v2 02/15] net/hns3: fix a misjudgment expression Min Hu (Connor)
2022-01-22  1:51   ` [PATCH v2 03/15] net/hns3: extract a common API to initialize MAC addrs Min Hu (Connor)
2022-01-22  1:51   ` [PATCH v2 04/15] net/hns3: remove unnecessary 'inline' Min Hu (Connor)
2022-01-22  1:51   ` [PATCH v2 05/15] net/hns3: remove unnecessary black lines Min Hu (Connor)
2022-01-22  1:51   ` [PATCH v2 06/15] net/hns3: extract a function to handle reset fail Min Hu (Connor)
2022-01-22  1:51   ` [PATCH v2 07/15] net/hns3: remove unused variables Min Hu (Connor)
2022-01-22  1:51   ` [PATCH v2 08/15] net/hns3: remove the number of queue descriptors Min Hu (Connor)
2022-01-22  1:51   ` [PATCH v2 09/15] net/hns3: remove the printing of memory addresses Min Hu (Connor)
2022-01-22  1:51   ` [PATCH v2 10/15] net/hns3: extract a common interface to obtain revision ID Min Hu (Connor)
2022-01-22  1:51   ` [PATCH v2 11/15] net/hns3: remove invalid encapsulation function Min Hu (Connor)
2022-01-27 13:04     ` Ferruh Yigit
2022-01-22  1:51   ` [PATCH v2 12/15] net/hns3: delete strerror invoke Min Hu (Connor)
2022-01-22  1:51   ` [PATCH v2 13/15] net/hns3: rename function Min Hu (Connor)
2022-01-22  1:51   ` [PATCH v2 14/15] net/hns3: extract functions to create RSS and FDIR flow rule Min Hu (Connor)
2022-01-22  1:51   ` [PATCH v2 15/15] net/hns3: support indirect counter action Min Hu (Connor)
2022-01-27 12:49   ` [PATCH v2 00/15] fix and feature for hns3 PMD Ferruh Yigit
2022-01-27 13:50     ` Ferruh Yigit
2022-01-28  0:40       ` Min Hu (Connor)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220107101558.39219-4-humin29@huawei.com \
    --to=humin29@huawei.com \
    --cc=dev@dpdk.org \
    --cc=ferruh.yigit@intel.com \
    --cc=thomas@monjalon.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).