net: lwip: fix initialization sequence before a command
The things that are done prior to executing a network command with
NET_LWIP are not consistent with what is done with NET. It impacts the
selection of the current device, and more precisely if the active device
is invalid NET would return an error while NET_LWIP would try to pick a
new device. This incorrect behavior was detected thanks to the eth_rotate
sandbox test (dm_test_eth_rotate()).
Fix it by re-using a sequence similar to what NET has in net_loop().
This piece of code is inserted in a function called net_lwip_eth_start()
renamed from net_lwip_eth_set_current().
Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
Reviewed-by: Simon Glass <sjg@chromium.org>
diff --git a/net/lwip/net-lwip.c b/net/lwip/net-lwip.c
index 6b7b696..6748008 100644
--- a/net/lwip/net-lwip.c
+++ b/net/lwip/net-lwip.c
@@ -134,18 +134,27 @@
return 0;
}
-/* Initialize the lwIP stack and the ethernet devices and set current device */
-void net_lwip_set_current(void)
+/*
+ * Initialize the network stack if needed and start the current device if valid
+ */
+int net_lwip_eth_start(void)
{
- static bool init_done;
+ int ret;
- if (!init_done) {
- eth_init_rings();
- eth_init();
- lwip_init();
- init_done = true;
+ net_init();
+ if (eth_is_on_demand_init()) {
+ eth_halt();
+ eth_set_current();
+ ret = eth_init();
+ if (ret < 0) {
+ eth_halt();
+ return ret;
+ }
+ } else {
+ eth_init_state_only();
}
- eth_set_current();
+
+ return 0;
}
static struct netif *new_netif(struct udevice *udev, bool with_ip)
@@ -224,11 +233,20 @@
free(netif);
}
+/*
+ * Initialize the network buffers, an ethernet device, and the lwIP stack
+ * (once).
+ */
int net_init(void)
{
- eth_set_current();
+ static bool init_done;
- net_lwip_new_netif(eth_get_dev());
+ if (!init_done) {
+ eth_init_rings();
+ eth_init();
+ lwip_init();
+ init_done = true;
+ }
return 0;
}