2018-01-02 21:44:48 +00:00
|
|
|
# Common shell utility functions
|
|
|
|
|
2019-02-07 12:28:49 +00:00
|
|
|
# Check if PCI device is on PCI_WHITELIST and not on PCI_BLACKLIST
|
|
|
|
# Env:
|
|
|
|
# if PCI_WHITELIST is empty assume device is whitelistened
|
|
|
|
# if PCI_BLACKLIST is empty assume device is NOT blacklistened
|
|
|
|
# Params:
|
|
|
|
# $1 - PCI BDF
|
2019-02-07 12:31:06 +00:00
|
|
|
function pci_can_use() {
|
2019-02-07 12:28:49 +00:00
|
|
|
local i
|
|
|
|
|
|
|
|
# The '\ ' part is important
|
|
|
|
if [[ " $PCI_BLACKLIST " =~ \ $1\ ]] ; then
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z "$PCI_WHITELIST" ]]; then
|
|
|
|
#no whitelist specified, bind all devices
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
for i in $PCI_WHITELIST; do
|
|
|
|
if [ "$i" == "$1" ] ; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2019-02-07 12:31:06 +00:00
|
|
|
# This function will ignore PCI PCI_WHITELIST and PCI_BLACKLIST
|
|
|
|
function iter_all_pci_class_code() {
|
2019-10-24 13:58:04 +00:00
|
|
|
local class
|
|
|
|
local subclass
|
|
|
|
local progif
|
|
|
|
class="$(printf %02x $((0x$1)))"
|
|
|
|
subclass="$(printf %02x $((0x$2)))"
|
|
|
|
progif="$(printf %02x $((0x$3)))"
|
2018-01-02 21:44:48 +00:00
|
|
|
|
|
|
|
if hash lspci &>/dev/null; then
|
|
|
|
if [ "$progif" != "00" ]; then
|
|
|
|
lspci -mm -n -D | \
|
|
|
|
grep -i -- "-p${progif}" | \
|
|
|
|
awk -v cc="\"${class}${subclass}\"" -F " " \
|
|
|
|
'{if (cc ~ $2) print $1}' | tr -d '"'
|
|
|
|
else
|
|
|
|
lspci -mm -n -D | \
|
|
|
|
awk -v cc="\"${class}${subclass}\"" -F " " \
|
|
|
|
'{if (cc ~ $2) print $1}' | tr -d '"'
|
|
|
|
fi
|
|
|
|
elif hash pciconf &>/dev/null; then
|
2019-02-07 12:31:06 +00:00
|
|
|
local addr=($(pciconf -l | grep -i "class=0x${class}${subclass}${progif}" | \
|
2018-01-02 21:44:48 +00:00
|
|
|
cut -d$'\t' -f1 | sed -e 's/^[a-zA-Z0-9_]*@pci//g' | tr ':' ' '))
|
|
|
|
printf "%04x:%02x:%02x:%x\n" ${addr[0]} ${addr[1]} ${addr[2]} ${addr[3]}
|
|
|
|
else
|
2020-03-11 05:30:27 +00:00
|
|
|
echo "Missing PCI enumeration utility" >&2
|
2018-01-02 21:44:48 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2019-02-07 12:31:06 +00:00
|
|
|
# This function will ignore PCI PCI_WHITELIST and PCI_BLACKLIST
|
|
|
|
function iter_all_pci_dev_id() {
|
2019-10-24 13:58:04 +00:00
|
|
|
local ven_id
|
|
|
|
local dev_id
|
|
|
|
ven_id="$(printf %04x $((0x$1)))"
|
|
|
|
dev_id="$(printf %04x $((0x$2)))"
|
2018-01-02 21:44:48 +00:00
|
|
|
|
|
|
|
if hash lspci &>/dev/null; then
|
|
|
|
lspci -mm -n -D | awk -v ven="\"$ven_id\"" -v dev="\"${dev_id}\"" -F " " \
|
|
|
|
'{if (ven ~ $3 && dev ~ $4) print $1}' | tr -d '"'
|
|
|
|
elif hash pciconf &>/dev/null; then
|
2019-02-07 12:31:06 +00:00
|
|
|
local addr=($(pciconf -l | grep -i "chip=0x${dev_id}${ven_id}" | \
|
2018-01-02 21:44:48 +00:00
|
|
|
cut -d$'\t' -f1 | sed -e 's/^[a-zA-Z0-9_]*@pci//g' | tr ':' ' '))
|
|
|
|
printf "%04x:%02x:%02x:%x\n" ${addr[0]} ${addr[1]} ${addr[2]} ${addr[3]}
|
|
|
|
else
|
2020-03-11 05:30:27 +00:00
|
|
|
echo "Missing PCI enumeration utility" >&2
|
2018-01-02 21:44:48 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
2019-02-07 12:31:06 +00:00
|
|
|
|
|
|
|
function iter_pci_dev_id() {
|
|
|
|
local bdf=""
|
|
|
|
|
|
|
|
for bdf in $(iter_all_pci_dev_id "$@"); do
|
|
|
|
if pci_can_use "$bdf"; then
|
|
|
|
echo "$bdf"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
# This function will filter out PCI devices using PCI_WHITELIST and PCI_BLACKLIST
|
|
|
|
# See function pci_can_use()
|
|
|
|
function iter_pci_class_code() {
|
|
|
|
local bdf=""
|
|
|
|
|
|
|
|
for bdf in $(iter_all_pci_class_code "$@"); do
|
|
|
|
if pci_can_use "$bdf"; then
|
|
|
|
echo "$bdf"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|