Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 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> |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 12 | #include <fdtdec.h> |
| 13 | #include <malloc.h> |
| 14 | #include <net.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 15 | #include <dm/test.h> |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 16 | #include <dm/device-internal.h> |
| 17 | #include <dm/uclass-internal.h> |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 18 | #include <asm/eth.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 19 | #include <test/ut.h> |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 23 | #define DM_TEST_ETH_NUM 4 |
| 24 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 25 | static int dm_test_eth(struct unit_test_state *uts) |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 26 | { |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 27 | net_ping_ip = string_to_ip("1.1.2.2"); |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 28 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 29 | env_set("ethact", "eth@10002000"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 30 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 31 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 32 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 33 | env_set("ethact", "eth@10003000"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 34 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 35 | ut_asserteq_str("eth@10003000", getenv("ethact")); |
| 36 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 37 | env_set("ethact", "eth@10004000"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 38 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 4c19724 | 2015-03-22 17:09:15 -0500 | [diff] [blame] | 39 | ut_asserteq_str("eth@10004000", getenv("ethact")); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | DM_TEST(dm_test_eth, DM_TESTF_SCAN_FDT); |
Joe Hershberger | 279d2f6 | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 44 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 45 | static int dm_test_eth_alias(struct unit_test_state *uts) |
Joe Hershberger | 279d2f6 | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 46 | { |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 47 | net_ping_ip = string_to_ip("1.1.2.2"); |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 48 | env_set("ethact", "eth0"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 49 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 279d2f6 | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 50 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 51 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 52 | env_set("ethact", "eth1"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 53 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 279d2f6 | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 54 | ut_asserteq_str("eth@10004000", getenv("ethact")); |
| 55 | |
| 56 | /* Expected to fail since eth2 is not defined in the device tree */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 57 | env_set("ethact", "eth2"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 58 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 279d2f6 | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 59 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 60 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 61 | env_set("ethact", "eth5"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 62 | ut_assertok(net_loop(PING)); |
Joe Hershberger | 279d2f6 | 2015-03-22 17:09:16 -0500 | [diff] [blame] | 63 | ut_asserteq_str("eth@10003000", getenv("ethact")); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | DM_TEST(dm_test_eth_alias, DM_TESTF_SCAN_FDT); |
Joe Hershberger | aa52edd | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 68 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 69 | static int dm_test_eth_prime(struct unit_test_state *uts) |
Joe Hershberger | aa52edd | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 70 | { |
Joe Hershberger | 5874dec | 2015-04-08 01:41:01 -0500 | [diff] [blame] | 71 | net_ping_ip = string_to_ip("1.1.2.2"); |
Joe Hershberger | aa52edd | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 72 | |
| 73 | /* Expected to be "eth@10003000" because of ethprime variable */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 74 | env_set("ethact", NULL); |
| 75 | env_set("ethprime", "eth5"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 76 | ut_assertok(net_loop(PING)); |
Joe Hershberger | aa52edd | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 77 | ut_asserteq_str("eth@10003000", getenv("ethact")); |
| 78 | |
| 79 | /* Expected to be "eth@10002000" because it is first */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 80 | env_set("ethact", NULL); |
| 81 | env_set("ethprime", NULL); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 82 | ut_assertok(net_loop(PING)); |
Joe Hershberger | aa52edd | 2015-03-22 17:09:17 -0500 | [diff] [blame] | 83 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | DM_TEST(dm_test_eth_prime, DM_TESTF_SCAN_FDT); |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 88 | |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 89 | /** |
| 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 | */ |
| 103 | static 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 Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 119 | ut_assertok(device_remove(dev[i], DM_REMOVE_NORMAL)); |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 120 | |
| 121 | /* Invalidate MAC address */ |
| 122 | strcpy(ethaddr[i], getenv(addrname[i])); |
| 123 | /* Must disable access protection for ethaddr before clearing */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 124 | env_set(".flags", addrname[i]); |
| 125 | env_set(addrname[i], NULL); |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* Set ethact to "eth@10002000" */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 129 | env_set("ethact", ethname[0]); |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 130 | |
| 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 Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 136 | env_set(".flags", addrname[i]); |
| 137 | env_set(addrname[i], ethaddr[i]); |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 138 | |
| 139 | /* Probe the device again */ |
| 140 | ut_assertok(device_probe(dev[i])); |
| 141 | } |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 142 | env_set(".flags", NULL); |
| 143 | env_set("ethact", NULL); |
Bin Meng | 111473a | 2015-10-07 21:45:43 -0700 | [diff] [blame] | 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | DM_TEST(dm_test_eth_act, DM_TESTF_SCAN_FDT); |
| 148 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 149 | /* The asserts include a return on fail; cleanup in the caller */ |
| 150 | static int _dm_test_eth_rotate1(struct unit_test_state *uts) |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 151 | { |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 152 | /* Make sure that the default is to rotate to the next interface */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 153 | env_set("ethact", "eth@10004000"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 154 | ut_assertok(net_loop(PING)); |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 155 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 156 | |
| 157 | /* If ethrotate is no, then we should fail on a bad MAC */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 158 | env_set("ethact", "eth@10004000"); |
| 159 | env_set("ethrotate", "no"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 160 | ut_asserteq(-EINVAL, net_loop(PING)); |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 161 | ut_asserteq_str("eth@10004000", getenv("ethact")); |
| 162 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 163 | return 0; |
| 164 | } |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 165 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 166 | static int _dm_test_eth_rotate2(struct unit_test_state *uts) |
| 167 | { |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 168 | /* Make sure we can skip invalid devices */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 169 | env_set("ethact", "eth@10004000"); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 170 | ut_assertok(net_loop(PING)); |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 171 | ut_asserteq_str("eth@10004000", getenv("ethact")); |
| 172 | |
Bin Meng | 04a11cb | 2015-08-27 22:25:53 -0700 | [diff] [blame] | 173 | /* Make sure we can handle device name which is not eth# */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 174 | env_set("ethact", "sbe5"); |
Bin Meng | 04a11cb | 2015-08-27 22:25:53 -0700 | [diff] [blame] | 175 | ut_assertok(net_loop(PING)); |
| 176 | ut_asserteq_str("sbe5", getenv("ethact")); |
| 177 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static 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 */ |
| 190 | strcpy(ethaddr, getenv("eth1addr")); |
| 191 | /* Must disable access protection for eth1addr before clearing */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 192 | env_set(".flags", "eth1addr"); |
| 193 | env_set("eth1addr", NULL); |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 194 | |
| 195 | retval = _dm_test_eth_rotate1(uts); |
| 196 | |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 197 | /* Restore the env */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 198 | env_set("eth1addr", ethaddr); |
| 199 | env_set("ethrotate", NULL); |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 200 | |
| 201 | if (!retval) { |
| 202 | /* Invalidate eth0's MAC address */ |
| 203 | strcpy(ethaddr, getenv("ethaddr")); |
| 204 | /* Must disable access protection for ethaddr before clearing */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 205 | env_set(".flags", "ethaddr"); |
| 206 | env_set("ethaddr", NULL); |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 207 | |
| 208 | retval = _dm_test_eth_rotate2(uts); |
| 209 | |
| 210 | /* Restore the env */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 211 | env_set("ethaddr", ethaddr); |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 212 | } |
| 213 | /* Restore the env */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 214 | env_set(".flags", NULL); |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 215 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 216 | return retval; |
Joe Hershberger | f340e54 | 2015-03-22 17:09:18 -0500 | [diff] [blame] | 217 | } |
| 218 | DM_TEST(dm_test_eth_rotate, DM_TESTF_SCAN_FDT); |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 219 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 220 | /* The asserts include a return on fail; cleanup in the caller */ |
| 221 | static int _dm_test_net_retry(struct unit_test_state *uts) |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 222 | { |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 223 | /* |
| 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 Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 228 | env_set("ethact", "eth@10004000"); |
| 229 | env_set("netretry", "yes"); |
Joe Hershberger | 6901f43 | 2015-04-21 13:57:20 -0500 | [diff] [blame] | 230 | sandbox_eth_skip_timeout(); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 231 | ut_assertok(net_loop(PING)); |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 232 | ut_asserteq_str("eth@10002000", getenv("ethact")); |
| 233 | |
| 234 | /* |
| 235 | * eth1 is disabled and netretry is no, so the ping should fail and the |
| 236 | * active device should be eth1 |
| 237 | */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 238 | env_set("ethact", "eth@10004000"); |
| 239 | env_set("netretry", "no"); |
Joe Hershberger | 6901f43 | 2015-04-21 13:57:20 -0500 | [diff] [blame] | 240 | sandbox_eth_skip_timeout(); |
Joe Hershberger | c80b41b0 | 2015-04-08 01:41:21 -0500 | [diff] [blame] | 241 | ut_asserteq(-ETIMEDOUT, net_loop(PING)); |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 242 | ut_asserteq_str("eth@10004000", getenv("ethact")); |
| 243 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static 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 Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 255 | /* Restore the env */ |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame^] | 256 | env_set("netretry", NULL); |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 257 | sandbox_eth_disable_response(1, false); |
| 258 | |
Joe Hershberger | fc755f9 | 2015-05-20 14:27:33 -0500 | [diff] [blame] | 259 | return retval; |
Joe Hershberger | f933a93 | 2015-03-22 17:09:20 -0500 | [diff] [blame] | 260 | } |
| 261 | DM_TEST(dm_test_net_retry, DM_TESTF_SCAN_FDT); |