#!/bin/bash # cmp-gen-switch <3|4> [ ...]: switch links that already had cmp-e1steps this boot between Gen3 and Gen4 by a # config-space retrain only (target link speed N on root port and card, Retrain Link on the port). No BAR0 access. # Checks each link: speed N and the starting width at both ends, card AER UESta 0. Stops at the first link that does not # come up right and leaves its targets at 3. Gen4 needs the card's LnkCap max speed 4 (E1 done this boot). # Useful for A/B tests (Gen3 against Gen4 on the same boot) without a reboot. set -u N=${1:?3|4}; shift; [ "$N" = 3 ] || [ "$N" = 4 ] || { echo "speed must be 3 or 4"; exit 2; } st() { local s; s=$(setpci -s "$1" CAP_EXP+12.w 2>/dev/null); [[ $s =~ ^[[:xdigit:]]{4}$ && ${s,,} != ffff ]] && echo "$s" || echo LOST; } for B in "$@"; do G=0000:${B,,}:00.0; RP=$(basename "$(dirname "$(readlink -f /sys/bus/pci/devices/$G)")") s0=$(st $G); [ "$s0" = LOST ] && { echo "$(date +%T) $B: card unreadable, STOP"; exit 3; }; W0=$(( (16#$s0 >> 4) & 63 )) [ $N = 4 ] && [ $(( 16#$(setpci -s $G CAP_EXP+0c.l) & 15 )) != 4 ] && { echo "$(date +%T) $B: LnkCap max not 4 (no E1 this boot?), STOP"; exit 4; } if [ $(( 16#$s0 & 15 )) = $N ]; then echo "$(date +%T) $B: already Gen$N x$W0"; continue; fi setpci -s $RP CAP_EXP+30.w=000$N:000f; setpci -s $G CAP_EXP+30.w=000$N:000f; setpci -s $RP CAP_EXP+10.w=0020:0020 for i in $(seq 1 30); do sleep 0.1; s=$(st $RP); [ "$s" = LOST ] && break; (( (16#$s >> 11) & 1 )) || break; done; sleep 0.5 s=$(st $G); p=$(st $RP); ue=$(setpci -s $G ECAP_AER+04.l 2>/dev/null) if [ "$s" = LOST ] || [ "$p" = LOST ] || [ $((16#$s & 15)) != $N ] || [ $((16#$p & 15)) != $N ] || [ $(( (16#$s >> 4) & 63 )) -lt $W0 ] || [ "$ue" != 00000000 ]; then echo "$(date +%T) $B: FAILED to reach Gen$N (card $s port $p ue $ue), targets parked at 3, STOP"; setpci -s $RP CAP_EXP+30.w=0003:000f; setpci -s $G CAP_EXP+30.w=0003:000f; exit 5; fi echo "$(date +%T) $B: Gen$N x$(( (16#$s >> 4) & 63 )) both ends (card $s port $p, ce $(setpci -s $G ECAP_AER+10.l)/$(setpci -s $RP ECAP_AER+10.l))" done echo "$(date +%T) all links at Gen$N"