From 8408bf6ac03311e5a20f403bc55d88937080f858 Mon Sep 17 00:00:00 2001
From: Stefan Kalkowski <stefan.kalkowski@genode-labs.com>
Date: Mon, 31 May 2021 12:58:16 +0200
Subject: [PATCH] fec_nic_drv: remove nic_server mode

Issue #4133
---
 .../raw/drivers_nic-imx53_qsb/drivers.config  |   1 -
 .../drivers.config                            |   1 -
 .../drivers_nic-imx7d_sabre/drivers.config    |   2 +-
 .../raw/drivers_nic-imx8q_evk/drivers.config  |   1 -
 .../src/drivers/nic/fec/component.cc          | 127 ------------------
 .../dde_linux/src/drivers/nic/fec/component.h | 109 ---------------
 .../dde_linux/src/drivers/nic/fec/lx_emul.cc  |   2 +-
 repos/dde_linux/src/drivers/nic/fec/main.cc   |  38 ++----
 .../dde_linux/src/drivers/nic/fec/target.inc  |   2 +-
 9 files changed, 11 insertions(+), 272 deletions(-)
 delete mode 100644 repos/dde_linux/src/drivers/nic/fec/component.cc
 delete mode 100644 repos/dde_linux/src/drivers/nic/fec/component.h

diff --git a/repos/dde_linux/recipes/raw/drivers_nic-imx53_qsb/drivers.config b/repos/dde_linux/recipes/raw/drivers_nic-imx53_qsb/drivers.config
index 90e1d72369..a1d20777e6 100644
--- a/repos/dde_linux/recipes/raw/drivers_nic-imx53_qsb/drivers.config
+++ b/repos/dde_linux/recipes/raw/drivers_nic-imx53_qsb/drivers.config
@@ -33,7 +33,6 @@
 	<start name="nic_drv" caps="130">
 		<binary name="fec_nic_drv"/>
 		<resource name="RAM" quantum="20M"/>
-		<config mode="uplink_client"/>
 		<route>
 			<service name="Uplink"><parent/> </service>
 			<service name="ROM">   <parent/> </service>
diff --git a/repos/dde_linux/recipes/raw/drivers_nic-imx6q_sabrelite/drivers.config b/repos/dde_linux/recipes/raw/drivers_nic-imx6q_sabrelite/drivers.config
index 7d344ba2f5..b83506be06 100644
--- a/repos/dde_linux/recipes/raw/drivers_nic-imx6q_sabrelite/drivers.config
+++ b/repos/dde_linux/recipes/raw/drivers_nic-imx6q_sabrelite/drivers.config
@@ -34,7 +34,6 @@
 	<start name="nic_drv" caps="130">
 		<binary name="fec_nic_drv"/>
 		<resource name="RAM" quantum="20M"/>
-		<config mode="uplink_client"/>
 		<route>
 			<service name="Uplink"><parent/> </service>
 			<service name="ROM">   <parent/> </service>
diff --git a/repos/dde_linux/recipes/raw/drivers_nic-imx7d_sabre/drivers.config b/repos/dde_linux/recipes/raw/drivers_nic-imx7d_sabre/drivers.config
index 55b75dd3fe..7b216339fc 100644
--- a/repos/dde_linux/recipes/raw/drivers_nic-imx7d_sabre/drivers.config
+++ b/repos/dde_linux/recipes/raw/drivers_nic-imx7d_sabre/drivers.config
@@ -41,7 +41,7 @@
 	<start name="nic_drv" caps="150">
 		<binary name="fec_nic_drv"/>
 		<resource name="RAM" quantum="20M"/>
-		<config mode="uplink_client" uplink_label="fec0"/>
+		<config uplink_label="fec0"/>
 		<route>
 			<service name="Uplink">   <parent/> </service>
 			<service name="ROM">      <parent/> </service>
diff --git a/repos/dde_linux/recipes/raw/drivers_nic-imx8q_evk/drivers.config b/repos/dde_linux/recipes/raw/drivers_nic-imx8q_evk/drivers.config
index c6ff9ec18f..4360290ce5 100644
--- a/repos/dde_linux/recipes/raw/drivers_nic-imx8q_evk/drivers.config
+++ b/repos/dde_linux/recipes/raw/drivers_nic-imx8q_evk/drivers.config
@@ -33,7 +33,6 @@
 	<start name="nic_drv" caps="130">
 		<binary name="fec_nic_drv"/>
 		<resource name="RAM" quantum="20M"/>
-		<config mode="uplink_client"/>
 		<route>
 			<service name="ROM">      <parent/> </service>
 			<service name="PD">       <parent/> </service>
diff --git a/repos/dde_linux/src/drivers/nic/fec/component.cc b/repos/dde_linux/src/drivers/nic/fec/component.cc
deleted file mode 100644
index 6f131c5c9a..0000000000
--- a/repos/dde_linux/src/drivers/nic/fec/component.cc
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * \brief  Freescale ethernet driver component
- * \author Stefan Kalkowski
- * \date   2017-11-01
- */
-
-/*
- * Copyright (C) 2017 Genode Labs GmbH
- *
- * This file is distributed under the terms of the GNU General Public License
- * version 2.
- */
-
-#include <component.h>
-#include <lx_emul.h>
-
-extern "C" {
-#include <lxc.h>
-};
-
-
-bool Session_component::_send()
-{
-	using namespace Genode;
-
-	/*
-	 * We must not be called from another task, just from the
-	 * packet stream dispatcher.
-	 */
-	if (Lx::scheduler().active()) {
-		warning("scheduler active");
-		return false;
-	}
-
-	if (!_tx.sink()->ready_to_ack()) { return false; }
-	if (!_tx.sink()->packet_avail()) { return false; }
-
-	Packet_descriptor packet = _tx.sink()->get_packet();
-	if (!packet.size() || !_tx.sink()->packet_valid(packet)) {
-		warning("invalid tx packet");
-		return true;
-	}
-
-	struct sk_buff *skb = lxc_alloc_skb(packet.size() + HEAD_ROOM, HEAD_ROOM);
-
-	unsigned char *data = lxc_skb_put(skb, packet.size());
-	Genode::memcpy(data, _tx.sink()->packet_content(packet), packet.size());
-
-	_tx_data.ndev = _ndev;
-	_tx_data.skb  = skb;
-
-	_tx_task.unblock();
-	Lx::scheduler().schedule();
-
-	_tx.sink()->acknowledge_packet(packet);
-
-	return true;
-}
-
-
-void Session_component::_handle_rx()
-{
-	while (_rx.source()->ack_avail())
-		_rx.source()->release_packet(_rx.source()->get_acked_packet());
-}
-
-
-void Session_component::_handle_packet_stream()
-{
-	_handle_rx();
-
-	while (_send()) continue;
-}
-
-
-Nic::Mac_address Session_component::mac_address()
-{
-	return _ndev ? Nic::Mac_address(_ndev->dev_addr) : Nic::Mac_address();
-}
-
-
-void Session_component::receive(struct sk_buff *skb)
-{
-	_handle_rx();
-
-	if (!_rx.source()->ready_to_submit()) {
-		Genode::warning("not ready to receive packet");
-			return;
-	}
-
-	Skb s = skb_helper(skb);
-
-	try {
-		Nic::Packet_descriptor p = _rx.source()->alloc_packet(s.packet_size + s.frag_size);
-		void *buffer = _rx.source()->packet_content(p);
-		memcpy(buffer, s.packet, s.packet_size);
-
-		if (s.frag_size)
-			memcpy((char *)buffer + s.packet_size, s.frag, s.frag_size);
-
-		_rx.source()->submit_packet(p);
-	} catch (...) {
-		Genode::warning("failed to process received packet");
-	}
-}
-
-
-void Session_component::link_state(bool link)
-{
-	if (link == _has_link) return;
-
-	_has_link = link;
-	_link_state_changed();
-}
-
-
-Session_component::Session_component(Genode::size_t        const  tx_buf_size,
-                                     Genode::size_t        const  rx_buf_size,
-                                     Genode::Allocator           &rx_block_md_alloc,
-                                     Genode::Env                 &env,
-                                     Genode::Session_label const &label)
-:
-	Nic::Session_component     { tx_buf_size, rx_buf_size, Genode::CACHED,
-	                             rx_block_md_alloc, env },
-	Linux_network_session_base { label },
-	_has_link                  { _read_link_state_from_ndev() }
-{ }
diff --git a/repos/dde_linux/src/drivers/nic/fec/component.h b/repos/dde_linux/src/drivers/nic/fec/component.h
deleted file mode 100644
index 55ab7c8139..0000000000
--- a/repos/dde_linux/src/drivers/nic/fec/component.h
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * \brief  Freescale ethernet driver component
- * \author Stefan Kalkowski
- * \date   2017-11-01
- */
-
-/*
- * Copyright (C) 2017 Genode Labs GmbH
- *
- * This file is distributed under the terms of the GNU General Public License
- * version 2.
- */
-
-#ifndef _SRC__DRIVERS__NIC__FEC__COMPONENT_H_
-#define _SRC__DRIVERS__NIC__FEC__COMPONENT_H_
-
-/* Genode includes */
-#include <nic/component.h>
-#include <root/component.h>
-
-/* local includes */
-#include <linux_network_session_base.h>
-
-
-class Session_component : public Nic::Session_component,
-                          public Linux_network_session_base
-{
-	private:
-
-		bool _has_link = false;
-
-		bool _send();
-		void _handle_rx();
-		void _handle_packet_stream() override;
-
-	public:
-
-		Session_component(Genode::size_t        const  tx_buf_size,
-		                  Genode::size_t        const  rx_buf_size,
-		                  Genode::Allocator           &rx_block_md_alloc,
-		                  Genode::Env                 &env,
-		                  Genode::Session_label const &label);
-
-
-		/****************************
-		 ** Nic::Session_component **
-		 ****************************/
-
-		Nic::Mac_address mac_address() override;
-		bool link_state() override { return _has_link; }
-
-
-		/********************************
-		 ** Linux_network_session_base **
-		 ********************************/
-
-		void link_state(bool link) override;
-		void receive(struct sk_buff *skb) override;
-};
-
-
-class Root : public Genode::Root_component<Session_component>
-{
-	private:
-
-		Genode::Env       &_env;
-		Genode::Allocator &_md_alloc;
-
-	protected:
-
-		Session_component *_create_session(const char *args)
-		{
-			using namespace Genode;
-
-			Session_label const label = label_from_args(args);
-
-			size_t ram_quota   = Arg_string::find_arg(args, "ram_quota"  ).ulong_value(0);
-			size_t tx_buf_size = Arg_string::find_arg(args, "tx_buf_size").ulong_value(0);
-			size_t rx_buf_size = Arg_string::find_arg(args, "rx_buf_size").ulong_value(0);
-
-			/* deplete ram quota by the memory needed for the session structure */
-			size_t session_size = max(4096UL, (unsigned long)sizeof(Session_component));
-
-			/*
-			 * Check if donated ram quota suffices for both communication
-			 * buffers and check for overflow
-			 */
-			if ((ram_quota < session_size) ||
-			    (tx_buf_size + rx_buf_size < tx_buf_size ||
-			     tx_buf_size + rx_buf_size > ram_quota - session_size)) {
-				Genode::error("insufficient 'ram_quota', got ", ram_quota, ", "
-				              "need ", tx_buf_size + rx_buf_size + session_size);
-				throw Genode::Insufficient_ram_quota();
-			}
-
-			return new (Root::md_alloc())
-			            Session_component(tx_buf_size, rx_buf_size,
-			                             _md_alloc, _env, label);
-		}
-
-	public:
-
-		Root(Genode::Env &env, Genode::Allocator &md_alloc)
-		: Genode::Root_component<Session_component>(&env.ep().rpc_ep(), &md_alloc),
-			_env(env), _md_alloc(md_alloc)
-		{ }
-};
-
-#endif /* _SRC__DRIVERS__NIC__FEC__COMPONENT_H_ */
diff --git a/repos/dde_linux/src/drivers/nic/fec/lx_emul.cc b/repos/dde_linux/src/drivers/nic/fec/lx_emul.cc
index ab7b2175ad..772991bff6 100644
--- a/repos/dde_linux/src/drivers/nic/fec/lx_emul.cc
+++ b/repos/dde_linux/src/drivers/nic/fec/lx_emul.cc
@@ -23,7 +23,7 @@
 #include <irq_session/client.h>
 #include <platform_session/device.h>
 
-#include <component.h>
+#include <uplink_client.h>
 #include <lx_emul.h>
 
 #if DEBUG
diff --git a/repos/dde_linux/src/drivers/nic/fec/main.cc b/repos/dde_linux/src/drivers/nic/fec/main.cc
index e77eaa347a..f05d504874 100644
--- a/repos/dde_linux/src/drivers/nic/fec/main.cc
+++ b/repos/dde_linux/src/drivers/nic/fec/main.cc
@@ -17,12 +17,8 @@
 #include <base/component.h>
 #include <base/heap.h>
 
-/* NIC driver includes */
-#include <drivers/nic/mode.h>
-
 /* local includes */
 #include <uplink_client.h>
-#include <component.h>
 
 /* Linux emulation environment includes */
 #include <lx_emul.h>
@@ -48,13 +44,11 @@ unsigned long jiffies;
 
 struct Main
 {
-	Genode::Env                          &env;
-	Genode::Entrypoint                   &ep            { env.ep() };
-	Genode::Attached_rom_dataspace        config_rom    { env, "config" };
-	Genode::Nic_driver_mode const         mode          { read_nic_driver_mode(config_rom.xml()) };
-	Genode::Heap                          heap          { env.ram(), env.rm() };
-	Genode::Constructible<Root>           root          { };
-	Genode::Constructible<Genode::Uplink_client>  uplink_client { };
+	Genode::Env                                & env;
+	Genode::Entrypoint                         & ep            { env.ep() };
+	Genode::Attached_rom_dataspace               config_rom    { env, "config" };
+	Genode::Heap                                 heap          { env.ram(), env.rm() };
+	Genode::Constructible<Genode::Uplink_client> uplink_client { };
 
 	/* Linux task that handles the initialization */
 	Genode::Constructible<Lx::Task> linux;
@@ -63,11 +57,6 @@ struct Main
 	{
 		Genode::log("--- freescale ethernet driver ---");
 
-		if (mode == Genode::Nic_driver_mode::NIC_SERVER) {
-
-			root.construct(env, heap);
-		}
-
 		Lx_kit::construct_env(env);
 
 		LX_MUTEX_INIT(mdio_board_lock);
@@ -96,20 +85,9 @@ struct Main
 
 	void announce()
 	{
-		switch (mode) {
-		case Genode::Nic_driver_mode::NIC_SERVER:
-
-			env.parent().announce(ep.manage(*root));
-			break;
-
-		case Genode::Nic_driver_mode::UPLINK_CLIENT:
-
-			uplink_client.construct(
-				env, heap,
-				config_rom.xml().attribute_value(
-					"uplink_label", Genode::Session_label::String { "" }));
-			break;
-		}
+		uplink_client.construct( env, heap,
+			config_rom.xml().attribute_value(
+				"uplink_label", Genode::Session_label::String { "" }));
 	}
 
 	Lx::Task &linux_task() { return *linux; }
diff --git a/repos/dde_linux/src/drivers/nic/fec/target.inc b/repos/dde_linux/src/drivers/nic/fec/target.inc
index 7d4d7b8480..dbcf0de581 100644
--- a/repos/dde_linux/src/drivers/nic/fec/target.inc
+++ b/repos/dde_linux/src/drivers/nic/fec/target.inc
@@ -1,6 +1,6 @@
 TARGET   = fec_nic_drv
 LIBS     = base lx_kit_setjmp fec_nic_include nic_driver
-SRC_CC  += main.cc platform.cc lx_emul.cc component.cc uplink_client.cc
+SRC_CC  += main.cc platform.cc lx_emul.cc uplink_client.cc
 SRC_CC  += linux_network_session_base.cc
 SRC_C   += dummy.c lxc.c
 INC_DIR += $(PRG_DIR)/../..