blob: 122fab924d6ac4491cb87907029b50a3d5cd259e [file] [log] [blame]
Joe Hershbergerbfacad72015-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 Hershbergerbfacad72015-03-22 17:09:15 -050012#include <fdtdec.h>
13#include <malloc.h>
14#include <net.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050015#include <dm/test.h>
Bin Meng6d9764c2015-10-07 21:45:43 -070016#include <dm/device-internal.h>
17#include <dm/uclass-internal.h>
Joe Hershberger7ece1c62015-03-22 17:09:20 -050018#include <asm/eth.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050019#include <test/ut.h>
Joe Hershbergerbfacad72015-03-22 17:09:15 -050020
21DECLARE_GLOBAL_DATA_PTR;
22
Bin Meng6d9764c2015-10-07 21:45:43 -070023#define DM_TEST_ETH_NUM 4
24
Joe Hershbergere721b882015-05-20 14:27:27 -050025static int dm_test_eth(struct unit_test_state *uts)
Joe Hershbergerbfacad72015-03-22 17:09:15 -050026{
Joe Hershberger049a95a2015-04-08 01:41:01 -050027 net_ping_ip = string_to_ip("1.1.2.2");
Joe Hershbergerbfacad72015-03-22 17:09:15 -050028
Simon Glass382bee52017-08-03 12:22:09 -060029 env_set("ethact", "eth@10002000");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050030 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060031 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershbergerbfacad72015-03-22 17:09:15 -050032
Simon Glass382bee52017-08-03 12:22:09 -060033 env_set("ethact", "eth@10003000");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050034 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060035 ut_asserteq_str("eth@10003000", env_get("ethact"));
Joe Hershbergerbfacad72015-03-22 17:09:15 -050036
Simon Glass382bee52017-08-03 12:22:09 -060037 env_set("ethact", "eth@10004000");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050038 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060039 ut_asserteq_str("eth@10004000", env_get("ethact"));
Joe Hershbergerbfacad72015-03-22 17:09:15 -050040
41 return 0;
42}
43DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT);
Joe Hershbergere58780d2015-03-22 17:09:16 -050044
Joe Hershbergere721b882015-05-20 14:27:27 -050045static int dm_test_eth_alias(struct unit_test_state *uts)
Joe Hershbergere58780d2015-03-22 17:09:16 -050046{
Joe Hershberger049a95a2015-04-08 01:41:01 -050047 net_ping_ip = string_to_ip("1.1.2.2");
Simon Glass382bee52017-08-03 12:22:09 -060048 env_set("ethact", "eth0");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050049 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060050 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershbergere58780d2015-03-22 17:09:16 -050051
Simon Glass382bee52017-08-03 12:22:09 -060052 env_set("ethact", "eth1");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050053 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060054 ut_asserteq_str("eth@10004000", env_get("ethact"));
Joe Hershbergere58780d2015-03-22 17:09:16 -050055
56 /* Expected to fail since eth2 is not defined in the device tree */
Simon Glass382bee52017-08-03 12:22:09 -060057 env_set("ethact", "eth2");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050058 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060059 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershbergere58780d2015-03-22 17:09:16 -050060
Simon Glass382bee52017-08-03 12:22:09 -060061 env_set("ethact", "eth5");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050062 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060063 ut_asserteq_str("eth@10003000", env_get("ethact"));
Joe Hershbergere58780d2015-03-22 17:09:16 -050064
65 return 0;
66}
67DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT);
Joe Hershberger6536b9b2015-03-22 17:09:17 -050068
Joe Hershbergere721b882015-05-20 14:27:27 -050069static int dm_test_eth_prime(struct unit_test_state *uts)
Joe Hershberger6536b9b2015-03-22 17:09:17 -050070{
Joe Hershberger049a95a2015-04-08 01:41:01 -050071 net_ping_ip = string_to_ip("1.1.2.2");
Joe Hershberger6536b9b2015-03-22 17:09:17 -050072
73 /* Expected to be "eth@10003000" because of ethprime variable */
Simon Glass382bee52017-08-03 12:22:09 -060074 env_set("ethact", NULL);
75 env_set("ethprime", "eth5");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050076 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060077 ut_asserteq_str("eth@10003000", env_get("ethact"));
Joe Hershberger6536b9b2015-03-22 17:09:17 -050078
79 /* Expected to be "eth@10002000" because it is first */
Simon Glass382bee52017-08-03 12:22:09 -060080 env_set("ethact", NULL);
81 env_set("ethprime", NULL);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -050082 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -060083 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershberger6536b9b2015-03-22 17:09:17 -050084
85 return 0;
86}
87DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT);
Joe Hershberger7d104ea2015-03-22 17:09:18 -050088
Bin Meng6d9764c2015-10-07 21:45:43 -070089/**
90 * This test case is trying to test the following scenario:
91 * - All ethernet devices are not probed
92 * - "ethaddr" for all ethernet devices are not set
93 * - "ethact" is set to a valid ethernet device name
94 *
95 * With Sandbox default test configuration, all ethernet devices are
96 * probed after power-up, so we have to manually create such scenario:
97 * - Remove all ethernet devices
98 * - Remove all "ethaddr" environment variables
99 * - Set "ethact" to the first ethernet device
100 *
101 * Do a ping test to see if anything goes wrong.
102 */
103static int dm_test_eth_act(struct unit_test_state *uts)
104{
105 struct udevice *dev[DM_TEST_ETH_NUM];
106 const char *ethname[DM_TEST_ETH_NUM] = {"eth@10002000", "eth@10003000",
107 "sbe5", "eth@10004000"};
108 const char *addrname[DM_TEST_ETH_NUM] = {"ethaddr", "eth5addr",
109 "eth3addr", "eth1addr"};
110 char ethaddr[DM_TEST_ETH_NUM][18];
111 int i;
112
113 net_ping_ip = string_to_ip("1.1.2.2");
114
115 /* Prepare the test scenario */
116 for (i = 0; i < DM_TEST_ETH_NUM; i++) {
117 ut_assertok(uclass_find_device_by_name(UCLASS_ETH,
118 ethname[i], &dev[i]));
Stefan Roese706865a2017-03-20 12:51:48 +0100119 ut_assertok(device_remove(dev[i], DM_REMOVE_NORMAL));
Bin Meng6d9764c2015-10-07 21:45:43 -0700120
121 /* Invalidate MAC address */
Simon Glass00caae62017-08-03 12:22:12 -0600122 strcpy(ethaddr[i], env_get(addrname[i]));
Bin Meng6d9764c2015-10-07 21:45:43 -0700123 /* Must disable access protection for ethaddr before clearing */
Simon Glass382bee52017-08-03 12:22:09 -0600124 env_set(".flags", addrname[i]);
125 env_set(addrname[i], NULL);
Bin Meng6d9764c2015-10-07 21:45:43 -0700126 }
127
128 /* Set ethact to "eth@10002000" */
Simon Glass382bee52017-08-03 12:22:09 -0600129 env_set("ethact", ethname[0]);
Bin Meng6d9764c2015-10-07 21:45:43 -0700130
131 /* Segment fault might happen if something is wrong */
132 ut_asserteq(-ENODEV, net_loop(PING));
133
134 for (i = 0; i < DM_TEST_ETH_NUM; i++) {
135 /* Restore the env */
Simon Glass382bee52017-08-03 12:22:09 -0600136 env_set(".flags", addrname[i]);
137 env_set(addrname[i], ethaddr[i]);
Bin Meng6d9764c2015-10-07 21:45:43 -0700138
139 /* Probe the device again */
140 ut_assertok(device_probe(dev[i]));
141 }
Simon Glass382bee52017-08-03 12:22:09 -0600142 env_set(".flags", NULL);
143 env_set("ethact", NULL);
Bin Meng6d9764c2015-10-07 21:45:43 -0700144
145 return 0;
146}
147DM_TEST(dm_test_eth_act, DM_TESTF_SCAN_FDT);
148
Joe Hershberger09129be2015-05-20 14:27:33 -0500149/* The asserts include a return on fail; cleanup in the caller */
150static int _dm_test_eth_rotate1(struct unit_test_state *uts)
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500151{
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500152 /* Make sure that the default is to rotate to the next interface */
Simon Glass382bee52017-08-03 12:22:09 -0600153 env_set("ethact", "eth@10004000");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500154 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -0600155 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500156
157 /* If ethrotate is no, then we should fail on a bad MAC */
Simon Glass382bee52017-08-03 12:22:09 -0600158 env_set("ethact", "eth@10004000");
159 env_set("ethrotate", "no");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500160 ut_asserteq(-EINVAL, net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -0600161 ut_asserteq_str("eth@10004000", env_get("ethact"));
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500162
Joe Hershberger09129be2015-05-20 14:27:33 -0500163 return 0;
164}
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500165
Joe Hershberger09129be2015-05-20 14:27:33 -0500166static int _dm_test_eth_rotate2(struct unit_test_state *uts)
167{
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500168 /* Make sure we can skip invalid devices */
Simon Glass382bee52017-08-03 12:22:09 -0600169 env_set("ethact", "eth@10004000");
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500170 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -0600171 ut_asserteq_str("eth@10004000", env_get("ethact"));
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500172
Bin Meng71d79712015-08-27 22:25:53 -0700173 /* Make sure we can handle device name which is not eth# */
Simon Glass382bee52017-08-03 12:22:09 -0600174 env_set("ethact", "sbe5");
Bin Meng71d79712015-08-27 22:25:53 -0700175 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -0600176 ut_asserteq_str("sbe5", env_get("ethact"));
Bin Meng71d79712015-08-27 22:25:53 -0700177
Joe Hershberger09129be2015-05-20 14:27:33 -0500178 return 0;
179}
180
181static int dm_test_eth_rotate(struct unit_test_state *uts)
182{
183 char ethaddr[18];
184 int retval;
185
186 /* Set target IP to mock ping */
187 net_ping_ip = string_to_ip("1.1.2.2");
188
189 /* Invalidate eth1's MAC address */
Simon Glass00caae62017-08-03 12:22:12 -0600190 strcpy(ethaddr, env_get("eth1addr"));
Joe Hershberger09129be2015-05-20 14:27:33 -0500191 /* Must disable access protection for eth1addr before clearing */
Simon Glass382bee52017-08-03 12:22:09 -0600192 env_set(".flags", "eth1addr");
193 env_set("eth1addr", NULL);
Joe Hershberger09129be2015-05-20 14:27:33 -0500194
195 retval = _dm_test_eth_rotate1(uts);
196
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500197 /* Restore the env */
Simon Glass382bee52017-08-03 12:22:09 -0600198 env_set("eth1addr", ethaddr);
199 env_set("ethrotate", NULL);
Joe Hershberger09129be2015-05-20 14:27:33 -0500200
201 if (!retval) {
202 /* Invalidate eth0's MAC address */
Simon Glass00caae62017-08-03 12:22:12 -0600203 strcpy(ethaddr, env_get("ethaddr"));
Joe Hershberger09129be2015-05-20 14:27:33 -0500204 /* Must disable access protection for ethaddr before clearing */
Simon Glass382bee52017-08-03 12:22:09 -0600205 env_set(".flags", "ethaddr");
206 env_set("ethaddr", NULL);
Joe Hershberger09129be2015-05-20 14:27:33 -0500207
208 retval = _dm_test_eth_rotate2(uts);
209
210 /* Restore the env */
Simon Glass382bee52017-08-03 12:22:09 -0600211 env_set("ethaddr", ethaddr);
Joe Hershberger09129be2015-05-20 14:27:33 -0500212 }
213 /* Restore the env */
Simon Glass382bee52017-08-03 12:22:09 -0600214 env_set(".flags", NULL);
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500215
Joe Hershberger09129be2015-05-20 14:27:33 -0500216 return retval;
Joe Hershberger7d104ea2015-03-22 17:09:18 -0500217}
218DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT);
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500219
Joe Hershberger09129be2015-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 Hershberger7ece1c62015-03-22 17:09:20 -0500222{
Joe Hershberger7ece1c62015-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 Glass382bee52017-08-03 12:22:09 -0600228 env_set("ethact", "eth@10004000");
229 env_set("netretry", "yes");
Joe Hershberger172a31b2015-04-21 13:57:20 -0500230 sandbox_eth_skip_timeout();
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500231 ut_assertok(net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -0600232 ut_asserteq_str("eth@10002000", env_get("ethact"));
Joe Hershberger7ece1c62015-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 Glass382bee52017-08-03 12:22:09 -0600238 env_set("ethact", "eth@10004000");
239 env_set("netretry", "no");
Joe Hershberger172a31b2015-04-21 13:57:20 -0500240 sandbox_eth_skip_timeout();
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500241 ut_asserteq(-ETIMEDOUT, net_loop(PING));
Simon Glass00caae62017-08-03 12:22:12 -0600242 ut_asserteq_str("eth@10004000", env_get("ethact"));
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500243
Joe Hershberger09129be2015-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 Hershberger7ece1c62015-03-22 17:09:20 -0500255 /* Restore the env */
Simon Glass382bee52017-08-03 12:22:09 -0600256 env_set("netretry", NULL);
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500257 sandbox_eth_disable_response(1, false);
258
Joe Hershberger09129be2015-05-20 14:27:33 -0500259 return retval;
Joe Hershberger7ece1c62015-03-22 17:09:20 -0500260}
261DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT);