xref: /linux/tools/testing/selftests/pcie_bwctrl/set_pcie_cooling_state.sh (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0-or-later
3
4SYSFS=
5# Kselftest framework requirement - SKIP code is 4.
6ksft_skip=4
7retval=0
8skipmsg="skip all tests:"
9
10PCIEPORTTYPE="PCIe_Port_Link_Speed"
11
12prerequisite()
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
40testport=
41find_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
67sysfspcidev=
68find_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
80usage()
81{
82	echo "Usage $0 [ -d dev ]"
83	echo -e "\t-d: PCIe port BDF string (e.g., 0000:00:04.0)"
84}
85
86pattern="$PCIEPORTTYPE"
87parse_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
106parse_arguments "$@"
107prerequisite
108find_pcie_port "$pattern"
109if [ -z "$testport" ]; then
110	echo $skipmsg "pcie cooling device not found from sysfs" >&2
111	exit $ksft_skip
112fi
113find_sysfs_pci_dev "$testport"
114if [ -z "$sysfspcidev" ]; then
115	echo $skipmsg "PCIe port device not found from sysfs" >&2
116	exit $ksft_skip
117fi
118
119./set_pcie_speed.sh "$testport" "$sysfspcidev"
120retval=$?
121
122exit $retval
123