blob: 4915baf07c357cf1ba595ee3f8e5c7856ad86db7 [file] [log] [blame]
Joe Hershberger4c197242015-03-22 17:09:15 -05001/*
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>
Joe Hershberger4c197242015-03-22 17:09:15 -050012#include <fdtdec.h>
13#include <malloc.h>
14#include <net.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050015#include <dm/test.h>
Bin Meng111473a2015-10-07 21:45:43 -070016#include <dm/device-internal.h>
17#include <dm/uclass-internal.h>
Joe Hershbergerf933a932015-03-22 17:09:20 -050018#include <asm/eth.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050019#include <test/ut.h>
Joe Hershberger4c197242015-03-22 17:09:15 -050020
Bin Meng111473a2015-10-07 21:45:43 -070021#define DM_TEST_ETH_NUM 4
22
Joe Hershberger3a77be52015-05-20 14:27:27 -050023static int dm_test_eth(struct unit_test_state *uts)
Joe Hershberger4c197242015-03-22 17:09:15 -050024{
Joe Hershberger5874dec2015-04-08 01:41:01 -050025 net_ping_ip = string_to_ip("1.1.2.2");
Joe Hershberger4c197242015-03-22 17:09:15 -050026
Simon Glass6a38e412017-08-03 12:22:09 -060027 env_set("ethact", "eth@10002000");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050028 ut_assertok(net_loop(PING));
Simon Glass64b723f2017-08-03 12:22:12 -060029 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershberger4c197242015-03-22 17:09:15 -050030
Simon Glass6a38e412017-08-03 12:22:09 -060031 env_set("ethact", "eth@10003000");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050032 ut_assertok(net_loop(PING));
Simon Glass64b723f2017-08-03 12:22:12 -060033 ut_asserteq_str("eth@10003000", env_get("ethact"));
Joe Hershberger4c197242015-03-22 17:09:15 -050034
Simon Glass6a38e412017-08-03 12:22:09 -060035 env_set("ethact", "eth@10004000");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050036 ut_assertok(net_loop(PING));
Simon Glass64b723f2017-08-03 12:22:12 -060037 ut_asserteq_str("eth@10004000", env_get("ethact"));
Joe Hershberger4c197242015-03-22 17:09:15 -050038
39 return 0;
40}
41DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT);
Joe Hershberger279d2f62015-03-22 17:09:16 -050042
Joe Hershberger3a77be52015-05-20 14:27:27 -050043static int dm_test_eth_alias(struct unit_test_state *uts)
Joe Hershberger279d2f62015-03-22 17:09:16 -050044{
Joe Hershberger5874dec2015-04-08 01:41:01 -050045 net_ping_ip = string_to_ip("1.1.2.2");
Simon Glass6a38e412017-08-03 12:22:09 -060046 env_set("ethact", "eth0");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050047 ut_assertok(net_loop(PING));
Simon Glass64b723f2017-08-03 12:22:12 -060048 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershberger279d2f62015-03-22 17:09:16 -050049
Simon Glass6a38e412017-08-03 12:22:09 -060050 env_set("ethact", "eth1");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050051 ut_assertok(net_loop(PING));
Simon Glass64b723f2017-08-03 12:22:12 -060052 ut_asserteq_str("eth@10004000", env_get("ethact"));
Joe Hershberger279d2f62015-03-22 17:09:16 -050053
54 /* Expected to fail since eth2 is not defined in the device tree */
Simon Glass6a38e412017-08-03 12:22:09 -060055 env_set("ethact", "eth2");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050056 ut_assertok(net_loop(PING));
Simon Glass64b723f2017-08-03 12:22:12 -060057 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershberger279d2f62015-03-22 17:09:16 -050058
Simon Glass6a38e412017-08-03 12:22:09 -060059 env_set("ethact", "eth5");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050060 ut_assertok(net_loop(PING));
Simon Glass64b723f2017-08-03 12:22:12 -060061 ut_asserteq_str("eth@10003000", env_get("ethact"));
Joe Hershberger279d2f62015-03-22 17:09:16 -050062
63 return 0;
64}
65DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT);
Joe Hershbergeraa52edd2015-03-22 17:09:17 -050066
Joe Hershberger3a77be52015-05-20 14:27:27 -050067static int dm_test_eth_prime(struct unit_test_state *uts)
Joe Hershbergeraa52edd2015-03-22 17:09:17 -050068{
Joe Hershberger5874dec2015-04-08 01:41:01 -050069 net_ping_ip = string_to_ip("1.1.2.2");
Joe Hershbergeraa52edd2015-03-22 17:09:17 -050070
71 /* Expected to be "eth@10003000" because of ethprime variable */
Simon Glass6a38e412017-08-03 12:22:09 -060072 env_set("ethact", NULL);
73 env_set("ethprime", "eth5");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050074 ut_assertok(net_loop(PING));
Simon Glass64b723f2017-08-03 12:22:12 -060075 ut_asserteq_str("eth@10003000", env_get("ethact"));
Joe Hershbergeraa52edd2015-03-22 17:09:17 -050076
77 /* Expected to be "eth@10002000" because it is first */
Simon Glass6a38e412017-08-03 12:22:09 -060078 env_set("ethact", NULL);
79 env_set("ethprime", NULL);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -050080 ut_assertok(net_loop(PING));
Simon Glass64b723f2017-08-03 12:22:12 -060081 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershbergeraa52edd2015-03-22 17:09:17 -050082
83 return 0;
84}
85DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT);
Joe Hershbergerf340e542015-03-22 17:09:18 -050086
Bin Meng111473a2015-10-07 21:45:43 -070087/**
88 * This test case is trying to test the following scenario:
89 * - All ethernet devices are not probed
90 * - "ethaddr" for all ethernet devices are not set
91 * - "ethact" is set to a valid ethernet device name
92 *
93 * With Sandbox default test configuration, all ethernet devices are
94 * probed after power-up, so we have to manually create such scenario:
95 * - Remove all ethernet devices
96 * - Remove all "ethaddr" environment variables
97 * - Set "ethact" to the first ethernet device
98 *
99 * Do a ping test to see if anything goes wrong.
100 */
101static int dm_test_eth_act(struct unit_test_state *uts)
102{
103 struct udevice *dev[DM_TEST_ETH_NUM];
104 const char *ethname[DM_TEST_ETH_NUM] = {"eth@10002000", "eth@10003000",
105 "sbe5", "eth@10004000"};
106 const char *addrname[DM_TEST_ETH_NUM] = {"ethaddr", "eth5addr",
107 "eth3addr", "eth1addr"};
108 char ethaddr[DM_TEST_ETH_NUM][18];
109 int i;
110
Tom Rini0ac17d82017-09-26 14:08:30 -0400111 memset(ethaddr, '\0', sizeof(ethaddr));
Bin Meng111473a2015-10-07 21:45:43 -0700112 net_ping_ip = string_to_ip("1.1.2.2");
113
114 /* Prepare the test scenario */
115 for (i = 0; i < DM_TEST_ETH_NUM; i++) {
116 ut_assertok(uclass_find_device_by_name(UCLASS_ETH,
117 ethname[i], &dev[i]));
Stefan Roese80b5bc92017-03-20 12:51:48 +0100118 ut_assertok(device_remove(dev[i], DM_REMOVE_NORMAL));
Bin Meng111473a2015-10-07 21:45:43 -0700119
120 /* Invalidate MAC address */
Tom Rini0ac17d82017-09-26 14:08:30 -0400121 strncpy(ethaddr[i], env_get(addrname[i]), 17);
Bin Meng111473a2015-10-07 21:45:43 -0700122 /* Must disable access protection for ethaddr before clearing */
Simon Glass6a38e412017-08-03 12:22:09 -0600123 env_set(".flags", addrname[i]);
124 env_set(addrname[i], NULL);
Bin Meng111473a2015-10-07 21:45:43 -0700125 }
126
127 /* Set ethact to "eth@10002000" */
Simon Glass6a38e412017-08-03 12:22:09 -0600128 env_set("ethact", ethname[0]);
Bin Meng111473a2015-10-07 21:45:43 -0700129
130 /* Segment fault might happen if something is wrong */
131 ut_asserteq(-ENODEV, net_loop(PING));
132
133 for (i = 0; i < DM_TEST_ETH_NUM; i++) {
134 /* Restore the env */
Simon Glass6a38e412017-08-03 12:22:09 -0600135 env_set(".flags", addrname[i]);
136 env_set(addrname[i], ethaddr[i]);
Bin Meng111473a2015-10-07 21:45:43 -0700137
138 /* Probe the device again */
139 ut_assertok(device_probe(dev[i]));
140 }
Simon Glass6a38e412017-08-03 12:22:09 -0600141 env_set(".flags", NULL);
142 env_set("ethact", NULL);
Bin Meng111473a2015-10-07 21:45:43 -0700143
144 return 0;
145}
146DM_TEST(dm_test_eth_act, DM_TESTF_SCAN_FDT);
147
Joe Hershbergerfc755f92015-05-20 14:27:33 -0500148/* The asserts include a return on fail; cleanup in the caller */
149static int _dm_test_eth_rotate1(struct unit_test_state *uts)
Joe Hershbergerf340e542015-03-22 17:09:18 -0500150{
Joe Hershbergerf340e542015-03-22 17:09:18 -0500151 /* Make sure that the default is to rotate to the next interface */
Simon Glass6a38e412017-08-03 12:22:09 -0600152 env_set("ethact", "eth@10004000");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500153 ut_assertok(net_loop(PING));
Simon Glass64b723f2017-08-03 12:22:12 -0600154 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershbergerf340e542015-03-22 17:09:18 -0500155
156 /* If ethrotate is no, then we should fail on a bad MAC */
Simon Glass6a38e412017-08-03 12:22:09 -0600157 env_set("ethact", "eth@10004000");
158 env_set("ethrotate", "no");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500159 ut_asserteq(-EINVAL, net_loop(PING));
Simon Glass64b723f2017-08-03 12:22:12 -0600160 ut_asserteq_str("eth@10004000", env_get("ethact"));
Joe Hershbergerf340e542015-03-22 17:09:18 -0500161
Joe Hershbergerfc755f92015-05-20 14:27:33 -0500162 return 0;
163}
Joe Hershbergerf340e542015-03-22 17:09:18 -0500164
Joe Hershbergerfc755f92015-05-20 14:27:33 -0500165static int _dm_test_eth_rotate2(struct unit_test_state *uts)
166{
Joe Hershbergerf340e542015-03-22 17:09:18 -0500167 /* Make sure we can skip invalid devices */
Simon Glass6a38e412017-08-03 12:22:09 -0600168 env_set("ethact", "eth@10004000");
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500169 ut_assertok(net_loop(PING));
Simon Glass64b723f2017-08-03 12:22:12 -0600170 ut_asserteq_str("eth@10004000", env_get("ethact"));
Joe Hershbergerf340e542015-03-22 17:09:18 -0500171
Bin Meng04a11cb2015-08-27 22:25:53 -0700172 /* Make sure we can handle device name which is not eth# */
Simon Glass6a38e412017-08-03 12:22:09 -0600173 env_set("ethact", "sbe5");
Bin Meng04a11cb2015-08-27 22:25:53 -0700174 ut_assertok(net_loop(PING));
Simon Glass64b723f2017-08-03 12:22:12 -0600175 ut_asserteq_str("sbe5", env_get("ethact"));
Bin Meng04a11cb2015-08-27 22:25:53 -0700176
Joe Hershbergerfc755f92015-05-20 14:27:33 -0500177 return 0;
178}
179
180static int dm_test_eth_rotate(struct unit_test_state *uts)
181{
182 char ethaddr[18];
183 int retval;
184
185 /* Set target IP to mock ping */
186 net_ping_ip = string_to_ip("1.1.2.2");
187
188 /* Invalidate eth1's MAC address */
Tom Rini0ac17d82017-09-26 14:08:30 -0400189 memset(ethaddr, '\0', sizeof(ethaddr));
190 strncpy(ethaddr, env_get("eth1addr"), 17);
Joe Hershbergerfc755f92015-05-20 14:27:33 -0500191 /* Must disable access protection for eth1addr before clearing */
Simon Glass6a38e412017-08-03 12:22:09 -0600192 env_set(".flags", "eth1addr");
193 env_set("eth1addr", NULL);
Joe Hershbergerfc755f92015-05-20 14:27:33 -0500194
195 retval = _dm_test_eth_rotate1(uts);
196
Joe Hershbergerf340e542015-03-22 17:09:18 -0500197 /* Restore the env */
Simon Glass6a38e412017-08-03 12:22:09 -0600198 env_set("eth1addr", ethaddr);
199 env_set("ethrotate", NULL);
Joe Hershbergerfc755f92015-05-20 14:27:33 -0500200
201 if (!retval) {
202 /* Invalidate eth0's MAC address */
Tom Rini0ac17d82017-09-26 14:08:30 -0400203 strncpy(ethaddr, env_get("ethaddr"), 17);
Joe Hershbergerfc755f92015-05-20 14:27:33 -0500204 /* Must disable access protection for ethaddr before clearing */
Simon Glass6a38e412017-08-03 12:22:09 -0600205 env_set(".flags", "ethaddr");
206 env_set("ethaddr", NULL);
Joe Hershbergerfc755f92015-05-20 14:27:33 -0500207
208 retval = _dm_test_eth_rotate2(uts);
209
210 /* Restore the env */
Simon Glass6a38e412017-08-03 12:22:09 -0600211 env_set("ethaddr", ethaddr);
Joe Hershbergerfc755f92015-05-20 14:27:33 -0500212 }
213 /* Restore the env */
Simon Glass6a38e412017-08-03 12:22:09 -0600214 env_set(".flags", NULL);
Joe Hershbergerf340e542015-03-22 17:09:18 -0500215
Joe Hershbergerfc755f92015-05-20 14:27:33 -0500216 return retval;
Joe Hershbergerf340e542015-03-22 17:09:18 -0500217}
218DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT);
Joe Hershbergerf933a932015-03-22 17:09:20 -0500219
Joe Hershbergerfc755f92015-05-20 14:27:33 -0500220/* The asserts include a return on fail; cleanup in the caller */
221static int _dm_test_net_retry(struct unit_test_state *uts)
Joe Hershbergerf933a932015-03-22 17:09:20 -0500222{
Joe Hershbergerf933a932015-03-22 17:09:20 -0500223 /*
224 * eth1 is disabled and netretry is yes, so the ping should succeed and
225 * the active device should be eth0
226 */
227 sandbox_eth_disable_response(1, true);
Simon Glass6a38e412017-08-03 12:22:09 -0600228 env_set("ethact", "eth@10004000");
229 env_set("netretry", "yes");
Joe Hershberger6901f432015-04-21 13:57:20 -0500230 sandbox_eth_skip_timeout();
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500231 ut_assertok(net_loop(PING));
Simon Glass64b723f2017-08-03 12:22:12 -0600232 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershbergerf933a932015-03-22 17:09:20 -0500233
234 /*
235 * eth1 is disabled and netretry is no, so the ping should fail and the
236 * active device should be eth1
237 */
Simon Glass6a38e412017-08-03 12:22:09 -0600238 env_set("ethact", "eth@10004000");
239 env_set("netretry", "no");
Joe Hershberger6901f432015-04-21 13:57:20 -0500240 sandbox_eth_skip_timeout();
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500241 ut_asserteq(-ETIMEDOUT, net_loop(PING));
Simon Glass64b723f2017-08-03 12:22:12 -0600242 ut_asserteq_str("eth@10004000", env_get("ethact"));
Joe Hershbergerf933a932015-03-22 17:09:20 -0500243
Joe Hershbergerfc755f92015-05-20 14:27:33 -0500244 return 0;
245}
246
247static int dm_test_net_retry(struct unit_test_state *uts)
248{
249 int retval;
250
251 net_ping_ip = string_to_ip("1.1.2.2");
252
253 retval = _dm_test_net_retry(uts);
254
Joe Hershbergerf933a932015-03-22 17:09:20 -0500255 /* Restore the env */
Simon Glass6a38e412017-08-03 12:22:09 -0600256 env_set("netretry", NULL);
Joe Hershbergerf933a932015-03-22 17:09:20 -0500257 sandbox_eth_disable_response(1, false);
258
Joe Hershbergerfc755f92015-05-20 14:27:33 -0500259 return retval;
Joe Hershbergerf933a932015-03-22 17:09:20 -0500260}
261DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT);