diff mbox series

[RFC,net-next,25/28] net: ethernet: ti: cpsw-proxy-client: add helpers to (de)register IPv4

Message ID 20240518124234.2671651-26-s-vadapalli@ti.com (mailing list archive)
State RFC
Delegated to: Netdev Maintainers
Headers show
Series Add CPSW Proxy Client driver | expand

Checks

Context Check Description
netdev/series_format fail Series longer than 15 patches
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 24 this patch: 24
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 1 maintainers not CCed: linux-omap@vger.kernel.org
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn fail Errors and warnings before: 23 this patch: 23
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns WARNING: line length of 86 exceeds 80 columns
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Siddharth Vadapalli May 18, 2024, 12:42 p.m. UTC
Add functions "register_ipv4()" and "deregister_ipv4()" to register and
deregister IPv4 Address of the network interface corresponding to the
Virtual Switch Port with EthFw. Registering the IPv4 Address with EthFw
is necessary in the case of the Virtual Switch Port. This is because all
Broadcast packets received on any of the Switch Ports are consumed by
EthFw. This includes the ARP request for the IPv4 Address of the network
interface corresponding to the Virtual Switch Port as well. Thus,
registering the IPv4 Address with EthFw results in EthFw responding to
the ARP request thereby enabling subsequent Unicast communication with
the network interface corresponding to the Virtual Switch Port.

Add a notifier block to register/deregister the IPv4 address with EthFw
corresponding to interface state changes as well as IPv4 Address
changes.

Signed-off-by: Siddharth Vadapalli <s-vadapalli@ti.com>
---
 drivers/net/ethernet/ti/cpsw-proxy-client.c | 121 ++++++++++++++++++++
 1 file changed, 121 insertions(+)
diff mbox series

Patch

diff --git a/drivers/net/ethernet/ti/cpsw-proxy-client.c b/drivers/net/ethernet/ti/cpsw-proxy-client.c
index b42be0d389b8..9ede3e584a06 100644
--- a/drivers/net/ethernet/ti/cpsw-proxy-client.c
+++ b/drivers/net/ethernet/ti/cpsw-proxy-client.c
@@ -8,6 +8,7 @@ 
 #include <linux/etherdevice.h>
 #include <linux/ethtool.h>
 #include <linux/if_vlan.h>
+#include <linux/inetdevice.h>
 #include <linux/kernel.h>
 #include <linux/kmemleak.h>
 #include <linux/module.h>
@@ -106,6 +107,7 @@  struct virtual_port {
 	struct rx_dma_chan		*rx_chans;
 	struct tx_dma_chan		*tx_chans;
 	struct completion		tdown_complete;
+	struct notifier_block		inetaddr_nb;
 	enum virtual_port_type		port_type;
 	atomic_t			tdown_cnt;
 	u32				port_id;
@@ -113,6 +115,7 @@  struct virtual_port {
 	u32				port_features;
 	u32				num_rx_chan;
 	u32				num_tx_chan;
+	u8				ipv4_addr[ETHFW_IPV4ADDRLEN];
 	u8				mac_addr[ETH_ALEN];
 	bool				mac_in_use;
 };
@@ -1952,6 +1955,124 @@  static int register_dma_irq_handlers(struct cpsw_proxy_priv *proxy_priv)
 	return 0;
 }
 
+static int register_ipv4(struct virtual_port *vport)
+{
+	struct cpsw_proxy_priv *proxy_priv = vport->proxy_priv;
+	struct device *dev = proxy_priv->dev;
+	struct cpsw_proxy_req_params *req_p;
+	struct message resp_msg;
+	int ret;
+
+	mutex_lock(&proxy_priv->req_params_mutex);
+	req_p = &proxy_priv->req_params;
+	req_p->request_type = ETHFW_IPv4_REGISTER;
+	memcpy(req_p->ipv4_addr, vport->ipv4_addr, ETHFW_IPV4ADDRLEN);
+	ether_addr_copy(req_p->mac_addr, vport->mac_addr);
+	ret = send_request_get_response(proxy_priv, &resp_msg);
+	mutex_unlock(&proxy_priv->req_params_mutex);
+
+	if (ret) {
+		dev_err(dev, "failed to register IPv4 Address err: %d\n", ret);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static int deregister_ipv4(struct virtual_port *vport)
+{
+	struct cpsw_proxy_priv *proxy_priv = vport->proxy_priv;
+	struct device *dev = proxy_priv->dev;
+	struct cpsw_proxy_req_params *req_p;
+	struct message resp_msg;
+	int ret;
+
+	mutex_lock(&proxy_priv->req_params_mutex);
+	req_p = &proxy_priv->req_params;
+	req_p->request_type = ETHFW_IPv4_DEREGISTER;
+	memcpy(req_p->ipv4_addr, vport->ipv4_addr, ETHFW_IPV4ADDRLEN);
+	ret = send_request_get_response(proxy_priv, &resp_msg);
+	mutex_unlock(&proxy_priv->req_params_mutex);
+
+	if (ret) {
+		dev_err(dev, "failed to deregister IPv4 Address err: %d\n", ret);
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static bool cpsw_proxy_client_check(const struct net_device *ndev)
+{
+	struct virtual_port *vport = vport_ndev_to_vport(ndev);
+
+	return ndev->netdev_ops == &cpsw_proxy_client_netdev_ops &&
+				   vport->port_type == VIRT_SWITCH_PORT;
+}
+
+static int cpsw_proxy_client_inetaddr(struct notifier_block *unused,
+				      unsigned long event, void *ptr)
+{
+	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
+	struct virtual_port *vport;
+	struct net_device *ndev;
+	int ret = 0;
+
+	ndev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
+	if (!ndev)
+		return NOTIFY_DONE;
+
+	if (!cpsw_proxy_client_check(ndev))
+		return NOTIFY_DONE;
+
+	vport = vport_ndev_to_vport(ndev);
+	memcpy(vport->ipv4_addr, &ifa->ifa_address, ETHFW_IPV4ADDRLEN);
+
+	switch (event) {
+	case NETDEV_UP:
+	case NETDEV_CHANGEADDR:
+		ret = register_ipv4(vport);
+		if (ret)
+			netdev_err(ndev, "IPv4 register failed: %d\n", ret);
+		break;
+
+	case NETDEV_DOWN:
+	case NETDEV_PRE_CHANGEADDR:
+		ret = deregister_ipv4(vport);
+		if (ret)
+			netdev_err(ndev, "IPv4 deregister failed: %d\n", ret);
+		break;
+	}
+
+	return notifier_from_errno(ret);
+}
+
+static void unregister_notifiers(struct cpsw_proxy_priv *proxy_priv)
+{
+	struct virtual_port *vport;
+	u32 i;
+
+	for (i = 0; i < proxy_priv->num_virt_ports; i++) {
+		vport = &proxy_priv->virt_ports[i];
+		if (vport->port_type == VIRT_SWITCH_PORT)
+			unregister_inetaddr_notifier(&vport->inetaddr_nb);
+	}
+}
+
+static void register_notifiers(struct cpsw_proxy_priv *proxy_priv)
+{
+	struct virtual_port *vport;
+	u32 i;
+
+	for (i = 0; i < proxy_priv->num_virt_ports; i++) {
+		vport = &proxy_priv->virt_ports[i];
+		if (vport->port_type == VIRT_SWITCH_PORT) {
+			vport->inetaddr_nb.notifier_call = cpsw_proxy_client_inetaddr;
+			register_inetaddr_notifier(&vport->inetaddr_nb);
+		}
+	}
+}
+
 static int cpsw_proxy_client_probe(struct rpmsg_device *rpdev)
 {
 	struct cpsw_proxy_priv *proxy_priv;