Skip to content

Commit 838f12c

Browse files
ij-intelbjorn-helgaas
authored andcommitted
selftests/pcie_bwctrl: Create selftests
Create selftests for PCIe BW control through the PCIe cooling device sysfs interface. First, the BW control selftest finds the PCIe Port to test with. By default, the PCIe Port with the highest Link Speed is selected but another PCIe Port can be provided with -d parameter. The actual test steps the cur_state of the cooling device one-by-one from max_state to what the cur_state was initially. The speed change is confirmed by observing the current_link_speed for the corresponding PCIe Port. Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Ilpo Järvinen <[email protected]> Signed-off-by: Bjorn Helgaas <[email protected]> Reviewed-by: Jonathan Cameron <[email protected]>
1 parent d278b09 commit 838f12c

File tree

5 files changed

+193
-0
lines changed

5 files changed

+193
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17940,6 +17940,7 @@ S: Supported
1794017940
F: drivers/pci/pcie/bwctrl.c
1794117941
F: drivers/thermal/pcie_cooling.c
1794217942
F: include/linux/pci-bwctrl.h
17943+
F: tools/testing/selftests/pcie_bwctrl/
1794317944

1794417945
PCIE DRIVER FOR AMAZON ANNAPURNA LABS
1794517946
M: Jonathan Chocron <[email protected]>

tools/testing/selftests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ TARGETS += net/packetdrill
7272
TARGETS += net/rds
7373
TARGETS += net/tcp_ao
7474
TARGETS += nsfs
75+
TARGETS += pcie_bwctrl
7576
TARGETS += perf_events
7677
TARGETS += pidfd
7778
TARGETS += pid_namespace
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
TEST_PROGS = set_pcie_cooling_state.sh
2+
include ../lib.mk
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0-or-later
3+
4+
SYSFS=
5+
# Kselftest framework requirement - SKIP code is 4.
6+
ksft_skip=4
7+
retval=0
8+
skipmsg="skip all tests:"
9+
10+
PCIEPORTTYPE="PCIe_Port_Link_Speed"
11+
12+
prerequisite()
13+
{
14+
local ports
15+
16+
if [ $UID != 0 ]; then
17+
echo $skipmsg must be run as root >&2
18+
exit $ksft_skip
19+
fi
20+
21+
SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
22+
23+
if [ ! -d "$SYSFS" ]; then
24+
echo $skipmsg sysfs is not mounted >&2
25+
exit $ksft_skip
26+
fi
27+
28+
if ! ls $SYSFS/class/thermal/cooling_device* > /dev/null 2>&1; then
29+
echo $skipmsg thermal cooling devices missing >&2
30+
exit $ksft_skip
31+
fi
32+
33+
ports=`grep -e "^$PCIEPORTTYPE" $SYSFS/class/thermal/cooling_device*/type | wc -l`
34+
if [ $ports -eq 0 ]; then
35+
echo $skipmsg pcie cooling devices missing >&2
36+
exit $ksft_skip
37+
fi
38+
}
39+
40+
testport=
41+
find_pcie_port()
42+
{
43+
local patt="$1"
44+
local pcieports
45+
local max
46+
local cur
47+
local delta
48+
local bestdelta=-1
49+
50+
pcieports=`grep -l -F -e "$patt" /sys/class/thermal/cooling_device*/type`
51+
if [ -z "$pcieports" ]; then
52+
return
53+
fi
54+
pcieports=${pcieports//\/type/}
55+
# Find the port with the highest PCIe Link Speed
56+
for port in $pcieports; do
57+
max=`cat $port/max_state`
58+
cur=`cat $port/cur_state`
59+
delta=$((max-cur))
60+
if [ $delta -gt $bestdelta ]; then
61+
testport="$port"
62+
bestdelta=$delta
63+
fi
64+
done
65+
}
66+
67+
sysfspcidev=
68+
find_sysfs_pci_dev()
69+
{
70+
local typefile="$1/type"
71+
local pcidir
72+
73+
pcidir="$SYSFS/bus/pci/devices/`sed -e "s|^${PCIEPORTTYPE}_||g" $typefile`"
74+
75+
if [ -r "$pcidir/current_link_speed" ]; then
76+
sysfspcidev="$pcidir/current_link_speed"
77+
fi
78+
}
79+
80+
usage()
81+
{
82+
echo "Usage $0 [ -d dev ]"
83+
echo -e "\t-d: PCIe port BDF string (e.g., 0000:00:04.0)"
84+
}
85+
86+
pattern="$PCIEPORTTYPE"
87+
parse_arguments()
88+
{
89+
while getopts d:h opt; do
90+
case $opt in
91+
h)
92+
usage "$0"
93+
exit 0
94+
;;
95+
d)
96+
pattern="$PCIEPORTTYPE_$OPTARG"
97+
;;
98+
*)
99+
usage "$0"
100+
exit 0
101+
;;
102+
esac
103+
done
104+
}
105+
106+
parse_arguments "$@"
107+
prerequisite
108+
find_pcie_port "$pattern"
109+
if [ -z "$testport" ]; then
110+
echo $skipmsg "pcie cooling device not found from sysfs" >&2
111+
exit $ksft_skip
112+
fi
113+
find_sysfs_pci_dev "$testport"
114+
if [ -z "$sysfspcidev" ]; then
115+
echo $skipmsg "PCIe port device not found from sysfs" >&2
116+
exit $ksft_skip
117+
fi
118+
119+
./set_pcie_speed.sh "$testport" "$sysfspcidev"
120+
retval=$?
121+
122+
exit $retval
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0-or-later
3+
4+
set -e
5+
6+
TESTNAME=set_pcie_speed
7+
8+
declare -a PCIELINKSPEED=(
9+
"2.5 GT/s PCIe"
10+
"5.0 GT/s PCIe"
11+
"8.0 GT/s PCIe"
12+
"16.0 GT/s PCIe"
13+
"32.0 GT/s PCIe"
14+
"64.0 GT/s PCIe"
15+
)
16+
17+
# Kselftest framework requirement - SKIP code is 4.
18+
ksft_skip=4
19+
retval=0
20+
21+
coolingdev="$1"
22+
statefile="$coolingdev/cur_state"
23+
maxfile="$coolingdev/max_state"
24+
linkspeedfile="$2"
25+
26+
oldstate=`cat $statefile`
27+
maxstate=`cat $maxfile`
28+
29+
set_state()
30+
{
31+
local state=$1
32+
local linkspeed
33+
local expected_linkspeed
34+
35+
echo $state > $statefile
36+
37+
sleep 1
38+
39+
linkspeed="`cat $linkspeedfile`"
40+
expected_linkspeed=$((maxstate-state))
41+
expected_str="${PCIELINKSPEED[$expected_linkspeed]}"
42+
if [ ! "${expected_str}" = "${linkspeed}" ]; then
43+
echo "$TESTNAME failed: expected: ${expected_str}; got ${linkspeed}"
44+
retval=1
45+
fi
46+
}
47+
48+
cleanup_skip ()
49+
{
50+
set_state $oldstate
51+
exit $ksft_skip
52+
}
53+
54+
trap cleanup_skip EXIT
55+
56+
echo "$TESTNAME: testing states $maxstate .. $oldstate with $coolingdev"
57+
for i in $(seq $maxstate -1 $oldstate); do
58+
set_state "$i"
59+
done
60+
61+
trap EXIT
62+
if [ $retval -eq 0 ]; then
63+
echo "$TESTNAME [PASS]"
64+
else
65+
echo "$TESTNAME [FAIL]"
66+
fi
67+
exit $retval

0 commit comments

Comments
 (0)