numam-spdk/scripts/common.sh
Daniel Verkamp f0c2093477 scripts: factor out iter_pci helper functions
Add a new file, scripts/common.sh, that can be shared between not only
autotest scripts, but also scripts/setup.sh, scripts/gen_nvme.sh, etc.,
and move the PCI iteration functions there.

The iterators are also expanded to work identically for both dev_id and
class_code on Linux and FreeBSD.

Change-Id: I98423cd06242e78535f5da4fce82166812ea96a8
Signed-off-by: Daniel Verkamp <daniel.verkamp@intel.com>
Reviewed-on: https://review.gerrithub.io/393416
Tested-by: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: Ben Walker <benjamin.walker@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
2018-01-03 14:09:22 -05:00

45 lines
1.4 KiB
Bash

# Common shell utility functions
function iter_pci_class_code() {
local class="$(printf %02x $((0x$1)))"
local subclass="$(printf %02x $((0x$2)))"
local progif="$(printf %02x $((0x$3)))"
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
addr=($(pciconf -l | grep -i "class=0x${class}${subclass}${progif}" | \
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
echo "Missing PCI enumeration utility"
exit 1
fi
}
function iter_pci_dev_id() {
local ven_id="$(printf %04x $((0x$1)))"
local dev_id="$(printf %04x $((0x$2)))"
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
addr=($(pciconf -l | grep -i "chip=0x${dev_id}${ven_id}" | \
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
echo "Missing PCI enumeration utility"
exit 1
fi
}