[U-Boot] [PATCH v2 16/18] net: Fix net buffer initialization

Joe Hershberger joe.hershberger at ni.com
Wed Mar 28 01:43:06 CEST 2012


A new non-static function net_init() will initialize buffers and
read from the environment.  Only update from the env on each entry
to NetLoop().

Signed-off-by: Joe Hershberger <joe.hershberger at ni.com>
Cc: Joe Hershberger <joe.hershberger at gmail.com>
Cc: Simon Glass <sjg at chromium.org>
Cc: Mike Frysinger <vapier at gentoo.org>
---
Changes for v2:
   - Unadded explicit parameter name in net.h for NetLoop
   - Eliminate CamelCase from new functions
   - Changed null pointer print to an assert

 include/net.h |    1 +
 net/net.c     |   44 ++++++++++++++++++++++++++++++--------------
 2 files changed, 31 insertions(+), 14 deletions(-)

diff --git a/include/net.h b/include/net.h
index 33570c7..2c47604 100644
--- a/include/net.h
+++ b/include/net.h
@@ -430,6 +430,7 @@ extern IPaddr_t Mcast_addr;
 #endif
 
 /* Initialize the network adapter */
+extern void net_init(void);
 extern int NetLoop(enum proto_t);
 
 /* Shutdown adapters and cleanup */
diff --git a/net/net.c b/net/net.c
index 2cfb115..01e7b29 100644
--- a/net/net.c
+++ b/net/net.c
@@ -267,6 +267,30 @@ static void net_cleanup_loop(void)
 	net_clear_handlers();
 }
 
+void net_init(void)
+{
+	static int first_call = 1;
+
+	if (first_call) {
+		/*
+		 *	Setup packet buffers, aligned correctly.
+		 */
+		int i;
+		NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
+		NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
+		for (i = 0; i < PKTBUFSRX; i++)
+			NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
+
+		ArpInit();
+		net_clear_handlers();
+
+		/* Only need to setup buffer pointers once. */
+		first_call = 0;
+	}
+
+	NetInitLoop();
+}
+
 /**********************************************************************/
 /*
  *	Main network processing loop.
@@ -274,28 +298,15 @@ static void net_cleanup_loop(void)
 
 int NetLoop(enum proto_t protocol)
 {
-	int	i;
 	bd_t *bd = gd->bd;
 	int ret = -1;
 
 	NetRestarted = 0;
 	NetDevExists = 0;
-
-	NetTxPacket = NULL;
 	NetTryCount = 1;
 
-	ArpInit();
-	net_clear_handlers();
-
-	/*
-	 *	Setup packet buffers, aligned correctly.
-	 */
-	NetTxPacket = &PktBuf[0] + (PKTALIGN - 1);
-	NetTxPacket -= (ulong)NetTxPacket % PKTALIGN;
-	for (i = 0; i < PKTBUFSRX; i++)
-		NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
-
 	bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
+	net_init();
 	eth_halt();
 	eth_set_current();
 	if (eth_init(bd) < 0) {
@@ -627,6 +638,11 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport,
 	int eth_hdr_size;
 	int pkt_hdr_size;
 
+	/* make sure the NetTxPacket is initialized (NetInit() was called) */
+	assert(NetTxPacket != NULL);
+	if (NetTxPacket == NULL)
+		return -1;
+
 	/* convert to new style broadcast */
 	if (dest == 0)
 		dest = 0xFFFFFFFF;
-- 
1.6.0.2



More information about the U-Boot mailing list