1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4lib_dir=$(dirname $0)/../../../net/forwarding
5
6ALL_TESTS="
7	autoneg
8	autoneg_force_mode
9"
10
11NUM_NETIFS=2
12: ${TIMEOUT:=30000} # ms
13source $lib_dir/lib.sh
14source $lib_dir/ethtool_lib.sh
15
16setup_prepare()
17{
18	swp1=${NETIFS[p1]}
19	swp2=${NETIFS[p2]}
20
21	ip link set dev $swp1 up
22	ip link set dev $swp2 up
23
24	busywait "$TIMEOUT" wait_for_port_up ethtool $swp2
25	check_err $? "ports did not come up"
26
27	local lanes_exist=$(ethtool $swp1 | grep 'Lanes:')
28	if [[ -z $lanes_exist ]]; then
29		log_test "SKIP: driver does not support lanes setting"
30		exit 1
31	fi
32
33	ip link set dev $swp2 down
34	ip link set dev $swp1 down
35}
36
37check_lanes()
38{
39	local dev=$1; shift
40	local lanes=$1; shift
41	local max_speed=$1; shift
42	local chosen_lanes
43
44	chosen_lanes=$(ethtool $dev | grep 'Lanes:')
45	chosen_lanes=${chosen_lanes#*"Lanes: "}
46
47	((chosen_lanes == lanes))
48	check_err $? "swp1 advertise $max_speed and $lanes, devs sync to $chosen_lanes"
49}
50
51check_unsupported_lanes()
52{
53	local dev=$1; shift
54	local max_speed=$1; shift
55	local max_lanes=$1; shift
56	local autoneg=$1; shift
57	local autoneg_str=""
58
59	local unsupported_lanes=$((max_lanes *= 2))
60
61	if [[ $autoneg -eq 0 ]]; then
62		autoneg_str="autoneg off"
63	fi
64
65	ethtool -s $swp1 speed $max_speed lanes $unsupported_lanes $autoneg_str &> /dev/null
66	check_fail $? "Unsuccessful $unsupported_lanes lanes setting was expected"
67}
68
69max_speed_and_lanes_get()
70{
71	local dev=$1; shift
72	local arr=("$@")
73	local max_lanes
74	local max_speed
75	local -a lanes_arr
76	local -a speeds_arr
77	local -a max_values
78
79	for ((i=0; i<${#arr[@]}; i+=2)); do
80		speeds_arr+=("${arr[$i]}")
81		lanes_arr+=("${arr[i+1]}")
82	done
83
84	max_values+=($(get_max "${speeds_arr[@]}"))
85	max_values+=($(get_max "${lanes_arr[@]}"))
86
87	echo ${max_values[@]}
88}
89
90search_linkmode()
91{
92	local speed=$1; shift
93	local lanes=$1; shift
94	local arr=("$@")
95
96	for ((i=0; i<${#arr[@]}; i+=2)); do
97		if [[ $speed -eq ${arr[$i]} && $lanes -eq ${arr[i+1]} ]]; then
98			return 1
99		fi
100	done
101	return 0
102}
103
104autoneg()
105{
106	RET=0
107
108	local lanes
109	local max_speed
110	local max_lanes
111
112	local -a linkmodes_params=($(dev_linkmodes_params_get $swp1 1))
113	local -a max_values=($(max_speed_and_lanes_get $swp1 "${linkmodes_params[@]}"))
114	max_speed=${max_values[0]}
115	max_lanes=${max_values[1]}
116
117	lanes=$max_lanes
118
119	while [[ $lanes -ge 1 ]]; do
120		search_linkmode $max_speed $lanes "${linkmodes_params[@]}"
121		if [[ $? -eq 1 ]]; then
122			ethtool_set $swp1 speed $max_speed lanes $lanes
123			ip link set dev $swp1 up
124			ip link set dev $swp2 up
125			busywait "$TIMEOUT" wait_for_port_up ethtool $swp2
126			check_err $? "ports did not come up"
127
128			check_lanes $swp1 $lanes $max_speed
129			log_test "$lanes lanes is autonegotiated"
130		fi
131		let $((lanes /= 2))
132	done
133
134	check_unsupported_lanes $swp1 $max_speed $max_lanes 1
135	log_test "Lanes number larger than max width is not set"
136
137	ip link set dev $swp2 down
138	ip link set dev $swp1 down
139}
140
141autoneg_force_mode()
142{
143	RET=0
144
145	local lanes
146	local max_speed
147	local max_lanes
148
149	local -a linkmodes_params=($(dev_linkmodes_params_get $swp1 1))
150	local -a max_values=($(max_speed_and_lanes_get $swp1 "${linkmodes_params[@]}"))
151	max_speed=${max_values[0]}
152	max_lanes=${max_values[1]}
153
154	lanes=$max_lanes
155
156	while [[ $lanes -ge 1 ]]; do
157		search_linkmode $max_speed $lanes "${linkmodes_params[@]}"
158		if [[ $? -eq 1 ]]; then
159			ethtool_set $swp1 speed $max_speed lanes $lanes autoneg off
160			ethtool_set $swp2 speed $max_speed lanes $lanes autoneg off
161			ip link set dev $swp1 up
162			ip link set dev $swp2 up
163			busywait "$TIMEOUT" wait_for_port_up ethtool $swp2
164			check_err $? "ports did not come up"
165
166			check_lanes $swp1 $lanes $max_speed
167			log_test "Autoneg off, $lanes lanes detected during force mode"
168		fi
169		let $((lanes /= 2))
170	done
171
172	check_unsupported_lanes $swp1 $max_speed $max_lanes 0
173	log_test "Lanes number larger than max width is not set"
174
175	ip link set dev $swp2 down
176	ip link set dev $swp1 down
177
178	ethtool -s $swp2 autoneg on
179	ethtool -s $swp1 autoneg on
180}
181
182check_ethtool_lanes_support
183setup_prepare
184
185tests_run
186
187exit $EXIT_STATUS
188