xref: /linux/tools/testing/selftests/drivers/net/hw/ethtool_extended_state.sh (revision 90d32e92011eaae8e70a9169b4e7acf4ca8f9d3a)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4ALL_TESTS="
5	autoneg
6	autoneg_force_mode
7	no_cable
8"
9
10NUM_NETIFS=2
11lib_dir=$(dirname "$0")
12source "$lib_dir"/../../../net/forwarding/lib.sh
13source ethtool_lib.sh
14
15TIMEOUT=$((WAIT_TIMEOUT * 1000)) # ms
16
17setup_prepare()
18{
19	swp1=${NETIFS[p1]}
20	swp2=${NETIFS[p2]}
21	swp3=$NETIF_NO_CABLE
22}
23
24ethtool_ext_state()
25{
26	local dev=$1; shift
27	local expected_ext_state=$1; shift
28	local expected_ext_substate=${1:-""}; shift
29
30	local ext_state=$(ethtool $dev | grep "Link detected" \
31		| cut -d "(" -f2 | cut -d ")" -f1)
32	local ext_substate=$(echo $ext_state | cut -sd "," -f2 \
33		| sed -e 's/^[[:space:]]*//')
34	ext_state=$(echo $ext_state | cut -d "," -f1)
35
36	if [[ $ext_state != $expected_ext_state ]]; then
37		echo "Expected \"$expected_ext_state\", got \"$ext_state\""
38		return 1
39	fi
40	if [[ $ext_substate != $expected_ext_substate ]]; then
41		echo "Expected \"$expected_ext_substate\", got \"$ext_substate\""
42		return 1
43	fi
44}
45
46autoneg()
47{
48	local msg
49
50	RET=0
51
52	ip link set dev $swp1 up
53
54	msg=$(busywait $TIMEOUT ethtool_ext_state $swp1 \
55			"Autoneg" "No partner detected")
56	check_err $? "$msg"
57
58	log_test "Autoneg, No partner detected"
59
60	ip link set dev $swp1 down
61}
62
63autoneg_force_mode()
64{
65	local msg
66
67	RET=0
68
69	ip link set dev $swp1 up
70	ip link set dev $swp2 up
71
72	local -a speeds_arr=($(different_speeds_get $swp1 $swp2 0 0))
73	local speed1=${speeds_arr[0]}
74	local speed2=${speeds_arr[1]}
75
76	ethtool_set $swp1 speed $speed1 autoneg off
77	ethtool_set $swp2 speed $speed2 autoneg off
78
79	msg=$(busywait $TIMEOUT ethtool_ext_state $swp1 \
80			"Autoneg" "No partner detected during force mode")
81	check_err $? "$msg"
82
83	msg=$(busywait $TIMEOUT ethtool_ext_state $swp2 \
84			"Autoneg" "No partner detected during force mode")
85	check_err $? "$msg"
86
87	log_test "Autoneg, No partner detected during force mode"
88
89	ethtool -s $swp2 autoneg on
90	ethtool -s $swp1 autoneg on
91
92	ip link set dev $swp2 down
93	ip link set dev $swp1 down
94}
95
96no_cable()
97{
98	local msg
99
100	RET=0
101
102	ip link set dev $swp3 up
103
104	msg=$(busywait $TIMEOUT ethtool_ext_state $swp3 "No cable")
105	check_err $? "$msg"
106
107	log_test "No cable"
108
109	ip link set dev $swp3 down
110}
111
112setup_prepare
113
114tests_run
115
116exit $EXIT_STATUS
117