xref: /openbmc/u-boot/test/dm/eth.c (revision c3d89140)
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 	/* Make sure we can handle device name which is not eth# */
110 	setenv("ethact", "sbe5");
111 	ut_assertok(net_loop(PING));
112 	ut_asserteq_str("sbe5", getenv("ethact"));
113 
114 	return 0;
115 }
116 
117 static int dm_test_eth_rotate(struct unit_test_state *uts)
118 {
119 	char ethaddr[18];
120 	int retval;
121 
122 	/* Set target IP to mock ping */
123 	net_ping_ip = string_to_ip("1.1.2.2");
124 
125 	/* Invalidate eth1's MAC address */
126 	strcpy(ethaddr, getenv("eth1addr"));
127 	/* Must disable access protection for eth1addr before clearing */
128 	setenv(".flags", "eth1addr");
129 	setenv("eth1addr", NULL);
130 
131 	retval = _dm_test_eth_rotate1(uts);
132 
133 	/* Restore the env */
134 	setenv("eth1addr", ethaddr);
135 	setenv("ethrotate", NULL);
136 
137 	if (!retval) {
138 		/* Invalidate eth0's MAC address */
139 		strcpy(ethaddr, getenv("ethaddr"));
140 		/* Must disable access protection for ethaddr before clearing */
141 		setenv(".flags", "ethaddr");
142 		setenv("ethaddr", NULL);
143 
144 		retval = _dm_test_eth_rotate2(uts);
145 
146 		/* Restore the env */
147 		setenv("ethaddr", ethaddr);
148 	}
149 	/* Restore the env */
150 	setenv(".flags", NULL);
151 
152 	return retval;
153 }
154 DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT);
155 
156 /* The asserts include a return on fail; cleanup in the caller */
157 static int _dm_test_net_retry(struct unit_test_state *uts)
158 {
159 	/*
160 	 * eth1 is disabled and netretry is yes, so the ping should succeed and
161 	 * the active device should be eth0
162 	 */
163 	sandbox_eth_disable_response(1, true);
164 	setenv("ethact", "eth@10004000");
165 	setenv("netretry", "yes");
166 	sandbox_eth_skip_timeout();
167 	ut_assertok(net_loop(PING));
168 	ut_asserteq_str("eth@10002000", getenv("ethact"));
169 
170 	/*
171 	 * eth1 is disabled and netretry is no, so the ping should fail and the
172 	 * active device should be eth1
173 	 */
174 	setenv("ethact", "eth@10004000");
175 	setenv("netretry", "no");
176 	sandbox_eth_skip_timeout();
177 	ut_asserteq(-ETIMEDOUT, net_loop(PING));
178 	ut_asserteq_str("eth@10004000", getenv("ethact"));
179 
180 	return 0;
181 }
182 
183 static int dm_test_net_retry(struct unit_test_state *uts)
184 {
185 	int retval;
186 
187 	net_ping_ip = string_to_ip("1.1.2.2");
188 
189 	retval = _dm_test_net_retry(uts);
190 
191 	/* Restore the env */
192 	setenv("netretry", NULL);
193 	sandbox_eth_disable_response(1, false);
194 
195 	return retval;
196 }
197 DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT);
198