1# Common shell utility functions 2 3# Check if PCI device is on PCI_WHITELIST and not on PCI_BLACKLIST 4# Env: 5# if PCI_WHITELIST is empty assume device is whitelistened 6# if PCI_BLACKLIST is empty assume device is NOT blacklistened 7# Params: 8# $1 - PCI BDF 9function pci_can_use() { 10 local i 11 12 # The '\ ' part is important 13 if [[ " $PCI_BLACKLIST " =~ \ $1\ ]] ; then 14 return 1 15 fi 16 17 if [[ -z "$PCI_WHITELIST" ]]; then 18 #no whitelist specified, bind all devices 19 return 0 20 fi 21 22 for i in $PCI_WHITELIST; do 23 if [ "$i" == "$1" ] ; then 24 return 0 25 fi 26 done 27 28 return 1 29} 30 31# This function will ignore PCI PCI_WHITELIST and PCI_BLACKLIST 32function iter_all_pci_class_code() { 33 local class="$(printf %02x $((0x$1)))" 34 local subclass="$(printf %02x $((0x$2)))" 35 local progif="$(printf %02x $((0x$3)))" 36 37 if hash lspci &>/dev/null; then 38 if [ "$progif" != "00" ]; then 39 lspci -mm -n -D | \ 40 grep -i -- "-p${progif}" | \ 41 awk -v cc="\"${class}${subclass}\"" -F " " \ 42 '{if (cc ~ $2) print $1}' | tr -d '"' 43 else 44 lspci -mm -n -D | \ 45 awk -v cc="\"${class}${subclass}\"" -F " " \ 46 '{if (cc ~ $2) print $1}' | tr -d '"' 47 fi 48 elif hash pciconf &>/dev/null; then 49 local addr=($(pciconf -l | grep -i "class=0x${class}${subclass}${progif}" | \ 50 cut -d$'\t' -f1 | sed -e 's/^[a-zA-Z0-9_]*@pci//g' | tr ':' ' ')) 51 printf "%04x:%02x:%02x:%x\n" ${addr[0]} ${addr[1]} ${addr[2]} ${addr[3]} 52 else 53 echo "Missing PCI enumeration utility" 54 exit 1 55 fi 56} 57 58# This function will ignore PCI PCI_WHITELIST and PCI_BLACKLIST 59function iter_all_pci_dev_id() { 60 local ven_id="$(printf %04x $((0x$1)))" 61 local dev_id="$(printf %04x $((0x$2)))" 62 63 if hash lspci &>/dev/null; then 64 lspci -mm -n -D | awk -v ven="\"$ven_id\"" -v dev="\"${dev_id}\"" -F " " \ 65 '{if (ven ~ $3 && dev ~ $4) print $1}' | tr -d '"' 66 elif hash pciconf &>/dev/null; then 67 local addr=($(pciconf -l | grep -i "chip=0x${dev_id}${ven_id}" | \ 68 cut -d$'\t' -f1 | sed -e 's/^[a-zA-Z0-9_]*@pci//g' | tr ':' ' ')) 69 printf "%04x:%02x:%02x:%x\n" ${addr[0]} ${addr[1]} ${addr[2]} ${addr[3]} 70 else 71 echo "Missing PCI enumeration utility" 72 exit 1 73 fi 74} 75 76function iter_pci_dev_id() { 77 local bdf="" 78 79 for bdf in $(iter_all_pci_dev_id "$@"); do 80 if pci_can_use "$bdf"; then 81 echo "$bdf" 82 fi 83 done 84} 85 86# This function will filter out PCI devices using PCI_WHITELIST and PCI_BLACKLIST 87# See function pci_can_use() 88function iter_pci_class_code() { 89 local bdf="" 90 91 for bdf in $(iter_all_pci_class_code "$@"); do 92 if pci_can_use "$bdf"; then 93 echo "$bdf" 94 fi 95 done 96} 97