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