1
0
mirror of https://github.com/corundum/corundum.git synced 2025-01-30 08:32:52 +08:00
corundum/scripts/pcie_set_speed.sh

79 lines
1.4 KiB
Bash
Raw Normal View History

2019-07-15 12:33:35 -07:00
#!/bin/bash
dev=$1
2019-08-03 23:32:02 -07:00
speed=$2
2019-07-15 12:33:35 -07:00
if [ -z "$dev" ]; then
echo "Error: no device specified"
exit 1
fi
if [ ! -e "/sys/bus/pci/devices/$dev" ]; then
dev="0000:$dev"
fi
if [ ! -e "/sys/bus/pci/devices/$dev" ]; then
echo "Error: device $dev not found"
exit 1
fi
pciec=$(setpci -s $dev CAP_EXP+02.W)
2019-11-17 12:51:13 -08:00
pt=$(((0x$pciec & 0xF0) >> 4))
2019-07-15 12:33:35 -07:00
port=$(basename $(dirname $(readlink "/sys/bus/pci/devices/$dev")))
if (($pt == 0)) || (($pt == 1)) || (($pt == 5)); then
dev=$port
2019-07-15 12:33:35 -07:00
fi
2019-08-03 23:32:02 -07:00
lc=$(setpci -s $dev CAP_EXP+0c.L)
ls=$(setpci -s $dev CAP_EXP+12.W)
2019-11-17 12:51:13 -08:00
max_speed=$((0x$lc & 0xF))
2019-08-03 23:32:02 -07:00
echo "Link capabilities:" $lc
echo "Max link speed:" $max_speed
echo "Link status:" $ls
2019-11-17 12:51:13 -08:00
echo "Current link speed:" $((0x$ls & 0xF))
2019-08-03 23:32:02 -07:00
if [ -z "$speed" ]; then
speed=$max_speed
fi
if (($speed > $max_speed)); then
speed=$max_speed
fi
2019-07-15 12:33:35 -07:00
echo "Configuring $dev..."
lc2=$(setpci -s $dev CAP_EXP+30.L)
echo "Original link control 2:" $lc2
2019-11-17 12:51:13 -08:00
echo "Original link target speed:" $((0x$lc2 & 0xF))
2019-07-15 12:33:35 -07:00
2019-11-17 12:51:13 -08:00
lc2n=$(printf "%08x" $(((0x$lc2 & 0xFFFFFFF0) | $speed)))
2019-07-15 12:33:35 -07:00
2019-08-03 23:32:02 -07:00
echo "New target link speed:" $speed
2019-07-15 12:33:35 -07:00
echo "New link control 2:" $lc2n
setpci -s $dev CAP_EXP+30.L=$lc2n
echo "Triggering link retraining..."
lc=$(setpci -s $dev CAP_EXP+10.L)
echo "Original link control:" $lc
2019-11-17 12:51:13 -08:00
lcn=$(printf "%08x" $((0x$lc | 0x20)))
2019-07-15 12:33:35 -07:00
echo "New link control:" $lcn
setpci -s $dev CAP_EXP+10.L=$lcn
2019-08-03 23:32:02 -07:00
sleep 0.1
ls=$(setpci -s $dev CAP_EXP+12.W)
echo "Link status:" $ls
2019-11-17 12:51:13 -08:00
echo "Current link speed:" $((0x$ls & 0xF))