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