xref: /openbmc/u-boot/test/dm/eth.c (revision 0c01c3e8)
1 /*
2  * Copyright (c) 2015 National Instruments
3  *
4  * (C) Copyright 2015
5  * Joe Hershberger <joe.hershberger@ni.com>
6  *
7  * SPDX-License-Identifier:	GPL-2.0
8  */
9 
10 #include <common.h>
11 #include <dm.h>
12 #include <fdtdec.h>
13 #include <malloc.h>
14 #include <net.h>
15 #include <dm/test.h>
16 #include <asm/eth.h>
17 #include <test/ut.h>
18 
19 DECLARE_GLOBAL_DATA_PTR;
20 
21 static int dm_test_eth(struct unit_test_state *uts)
22 {
23 	net_ping_ip = string_to_ip("1.1.2.2");
24 
25 	setenv("ethact", "eth@10002000");
26 	ut_assertok(net_loop(PING));
27 	ut_asserteq_str("eth@10002000", getenv("ethact"));
28 
29 	setenv("ethact", "eth@10003000");
30 	ut_assertok(net_loop(PING));
31 	ut_asserteq_str("eth@10003000", getenv("ethact"));
32 
33 	setenv("ethact", "eth@10004000");
34 	ut_assertok(net_loop(PING));
35 	ut_asserteq_str("eth@10004000", getenv("ethact"));
36 
37 	return 0;
38 }
39 DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT);
40 
41 static int dm_test_eth_alias(struct unit_test_state *uts)
42 {
43 	net_ping_ip = string_to_ip("1.1.2.2");
44 	setenv("ethact", "eth0");
45 	ut_assertok(net_loop(PING));
46 	ut_asserteq_str("eth@10002000", getenv("ethact"));
47 
48 	setenv("ethact", "eth1");
49 	ut_assertok(net_loop(PING));
50 	ut_asserteq_str("eth@10004000", getenv("ethact"));
51 
52 	/* Expected to fail since eth2 is not defined in the device tree */
53 	setenv("ethact", "eth2");
54 	ut_assertok(net_loop(PING));
55 	ut_asserteq_str("eth@10002000", getenv("ethact"));
56 
57 	setenv("ethact", "eth5");
58 	ut_assertok(net_loop(PING));
59 	ut_asserteq_str("eth@10003000", getenv("ethact"));
60 
61 	return 0;
62 }
63 DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT);
64 
65 static int dm_test_eth_prime(struct unit_test_state *uts)
66 {
67 	net_ping_ip = string_to_ip("1.1.2.2");
68 
69 	/* Expected to be "eth@10003000" because of ethprime variable */
70 	setenv("ethact", NULL);
71 	setenv("ethprime", "eth5");
72 	ut_assertok(net_loop(PING));
73 	ut_asserteq_str("eth@10003000", getenv("ethact"));
74 
75 	/* Expected to be "eth@10002000" because it is first */
76 	setenv("ethact", NULL);
77 	setenv("ethprime", NULL);
78 	ut_assertok(net_loop(PING));
79 	ut_asserteq_str("eth@10002000", getenv("ethact"));
80 
81 	return 0;
82 }
83 DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT);
84 
85 /* The asserts include a return on fail; cleanup in the caller */
86 static int _dm_test_eth_rotate1(struct unit_test_state *uts)
87 {
88 	/* Make sure that the default is to rotate to the next interface */
89 	setenv("ethact", "eth@10004000");
90 	ut_assertok(net_loop(PING));
91 	ut_asserteq_str("eth@10002000", getenv("ethact"));
92 
93 	/* If ethrotate is no, then we should fail on a bad MAC */
94 	setenv("ethact", "eth@10004000");
95 	setenv("ethrotate", "no");
96 	ut_asserteq(-EINVAL, net_loop(PING));
97 	ut_asserteq_str("eth@10004000", getenv("ethact"));
98 
99 	return 0;
100 }
101 
102 static int _dm_test_eth_rotate2(struct unit_test_state *uts)
103 {
104 	/* Make sure we can skip invalid devices */
105 	setenv("ethact", "eth@10004000");
106 	ut_assertok(net_loop(PING));
107 	ut_asserteq_str("eth@10004000", getenv("ethact"));
108 
109 	return 0;
110 }
111 
112 static int dm_test_eth_rotate(struct unit_test_state *uts)
113 {
114 	char ethaddr[18];
115 	int retval;
116 
117 	/* Set target IP to mock ping */
118 	net_ping_ip = string_to_ip("1.1.2.2");
119 
120 	/* Invalidate eth1's MAC address */
121 	strcpy(ethaddr, getenv("eth1addr"));
122 	/* Must disable access protection for eth1addr before clearing */
123 	setenv(".flags", "eth1addr");
124 	setenv("eth1addr", NULL);
125 
126 	retval = _dm_test_eth_rotate1(uts);
127 
128 	/* Restore the env */
129 	setenv("eth1addr", ethaddr);
130 	setenv("ethrotate", NULL);
131 
132 	if (!retval) {
133 		/* Invalidate eth0's MAC address */
134 		strcpy(ethaddr, getenv("ethaddr"));
135 		/* Must disable access protection for ethaddr before clearing */
136 		setenv(".flags", "ethaddr");
137 		setenv("ethaddr", NULL);
138 
139 		retval = _dm_test_eth_rotate2(uts);
140 
141 		/* Restore the env */
142 		setenv("ethaddr", ethaddr);
143 	}
144 	/* Restore the env */
145 	setenv(".flags", NULL);
146 
147 	return retval;
148 }
149 DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT);
150 
151 /* The asserts include a return on fail; cleanup in the caller */
152 static int _dm_test_net_retry(struct unit_test_state *uts)
153 {
154 	/*
155 	 * eth1 is disabled and netretry is yes, so the ping should succeed and
156 	 * the active device should be eth0
157 	 */
158 	sandbox_eth_disable_response(1, true);
159 	setenv("ethact", "eth@10004000");
160 	setenv("netretry", "yes");
161 	sandbox_eth_skip_timeout();
162 	ut_assertok(net_loop(PING));
163 	ut_asserteq_str("eth@10002000", getenv("ethact"));
164 
165 	/*
166 	 * eth1 is disabled and netretry is no, so the ping should fail and the
167 	 * active device should be eth1
168 	 */
169 	setenv("ethact", "eth@10004000");
170 	setenv("netretry", "no");
171 	sandbox_eth_skip_timeout();
172 	ut_asserteq(-ETIMEDOUT, net_loop(PING));
173 	ut_asserteq_str("eth@10004000", getenv("ethact"));
174 
175 	return 0;
176 }
177 
178 static int dm_test_net_retry(struct unit_test_state *uts)
179 {
180 	int retval;
181 
182 	net_ping_ip = string_to_ip("1.1.2.2");
183 
184 	retval = _dm_test_net_retry(uts);
185 
186 	/* Restore the env */
187 	setenv("netretry", NULL);
188 	sandbox_eth_disable_response(1, false);
189 
190 	return retval;
191 }
192 DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT);
193