Skip to content

Instantly share code, notes, and snippets.

@jimmygle
Created July 2, 2021 19:36
Show Gist options
  • Save jimmygle/1744b086283772ca4bb4585e47fd9259 to your computer and use it in GitHub Desktop.
Save jimmygle/1744b086283772ca4bb4585e47fd9259 to your computer and use it in GitHub Desktop.
Hot reset PCIe Device
#!/bin/bash
# Performs the function of resetting a PCI device
#
# Get PCI devices (grab the address in first column):
# lspci
#
# Usage:
# ./pci_reset.sh "86:00.0"
#
# Stolen from here: https://alexforencich.com/wiki/en/pcie/hot-reset-linux
dev=$1
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
port=$(basename $(dirname $(readlink "/sys/bus/pci/devices/$dev")))
if [ ! -e "/sys/bus/pci/devices/$port" ]; then
echo "Error: device $port not found"
exit 1
fi
echo "Removing $dev..."
echo 1 > "/sys/bus/pci/devices/$dev/remove"
echo "Performing hot reset of port $port..."
bc=$(setpci -s $port BRIDGE_CONTROL)
echo "Bridge control:" $bc
setpci -s $port BRIDGE_CONTROL=$(printf "%04x" $(("0x$bc" | 0x40)))
sleep 0.01
setpci -s $port BRIDGE_CONTROL=$bc
sleep 0.5
echo "Rescanning bus..."
echo 1 > "/sys/bus/pci/devices/$port/rescan"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment