Skip to content

Instantly share code, notes, and snippets.

@r15ch13
Last active July 23, 2024 00:52
Show Gist options
  • Save r15ch13/ba2d738985fce8990a4e9f32d07c6ada to your computer and use it in GitHub Desktop.
Save r15ch13/ba2d738985fce8990a4e9f32d07c6ada to your computer and use it in GitHub Desktop.
List IOMMU Groups and the connected USB Devices
#!/usr/bin/env bash
shopt -s nullglob
lastgroup=""
for g in `find /sys/kernel/iommu_groups/* -maxdepth 0 -type d | sort -V`; do
for d in $g/devices/*; do
if [ "${g##*/}" != "$lastgroup" ]; then
echo -en "Group ${g##*/}:\t"
else
echo -en "\t\t"
fi
lastgroup=${g##*/}
lspci -nms ${d##*/} | awk -F'"' '{printf "[%s:%s]", $4, $6}'
if [[ -e "$d"/reset ]]; then echo -en " [R] "; else echo -en " "; fi
lspci -mms ${d##*/} | awk -F'"' '{printf "%s %-40s %s\n", $1, $2, $6}'
for u in ${d}/usb*/; do
bus=$(cat "${u}/busnum")
lsusb -s $bus: | \
awk '{gsub(/:/,"",$4); printf "%s|%s %s %s %s|", $6, $1, $2, $3, $4; for(i=7;i<=NF;i++){printf "%s ", $i}; printf "\n"}' | \
awk -F'|' '{printf "USB:\t\t[%s]\t\t %-40s %s\n", $1, $2, $3}'
done
done
done
@r15ch13
Copy link
Author

r15ch13 commented Jul 24, 2021

Requires usbutils and pciutils.

Example output:

Group 0:        [1022:1452]     00:01.0  Host bridge                              Family 17h (Models 00h-1fh) PCIe Dummy Host Bridge
Group 1:        [1022:1453] [R] 00:01.1  PCI bridge                               Family 17h (Models 00h-0fh) PCIe GPP Bridge
Group 2:        [1022:1453] [R] 00:01.3  PCI bridge                               Family 17h (Models 00h-0fh) PCIe GPP Bridge
Group 3:        [1022:1452]     00:02.0  Host bridge                              Family 17h (Models 00h-1fh) PCIe Dummy Host Bridge
Group 4:        [1022:1452]     00:03.0  Host bridge                              Family 17h (Models 00h-1fh) PCIe Dummy Host Bridge
Group 5:        [1022:1453] [R] 00:03.1  PCI bridge                               Family 17h (Models 00h-0fh) PCIe GPP Bridge
Group 6:        [1022:1453] [R] 00:03.2  PCI bridge                               Family 17h (Models 00h-0fh) PCIe GPP Bridge
Group 7:        [1022:1452]     00:04.0  Host bridge                              Family 17h (Models 00h-1fh) PCIe Dummy Host Bridge
Group 8:        [1022:1452]     00:07.0  Host bridge                              Family 17h (Models 00h-1fh) PCIe Dummy Host Bridge
Group 9:        [1022:1454] [R] 00:07.1  PCI bridge                               Family 17h (Models 00h-0fh) Internal PCIe GPP Bridge 0 to Bus B
Group 10:       [1022:1452]     00:08.0  Host bridge                              Family 17h (Models 00h-1fh) PCIe Dummy Host Bridge
Group 11:       [1022:1454] [R] 00:08.1  PCI bridge                               Family 17h (Models 00h-0fh) Internal PCIe GPP Bridge 0 to Bus B
Group 12:       [1022:790b]     00:14.0  SMBus                                    FCH SMBus Controller
                [1022:790e]     00:14.3  ISA bridge                               FCH LPC Bridge
Group 13:       [1022:1460]     00:18.0  Host bridge                              Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 0
                [1022:1461]     00:18.1  Host bridge                              Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 1
                [1022:1462]     00:18.2  Host bridge                              Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 2
                [1022:1463]     00:18.3  Host bridge                              Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 3
                [1022:1464]     00:18.4  Host bridge                              Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 4
                [1022:1465]     00:18.5  Host bridge                              Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 5
                [1022:1466]     00:18.6  Host bridge                              Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 6
                [1022:1467]     00:18.7  Host bridge                              Family 17h (Models 00h-0fh) Data Fabric: Device 18h; Function 7
Group 14:       [144d:a808] [R] 01:00.0  Non-Volatile memory controller           NVMe SSD Controller SM981/PM981/PM983
Group 15:       [1022:43d0] [R] 03:00.0  USB controller                           Device 43d0
USB:            [2972:0047]              Bus 001 Device 003                       FiiO Electronics Technology K3 
USB:            [04d9:0169]              Bus 001 Device 002                       Holtek Semiconductor, Inc. USB Keyboard 
USB:            [1d6b:0002]              Bus 001 Device 001                       Linux Foundation 2.0 root hub 
USB:            [1d6b:0003]              Bus 002 Device 001                       Linux Foundation 3.0 root hub 
                [1022:43c8]     03:00.1  SATA controller                          400 Series Chipset SATA Controller
                [1022:43c6]     03:00.2  PCI bridge                               400 Series Chipset PCIe Bridge
                [1022:43c7] [R] 16:00.0  PCI bridge                               400 Series Chipset PCIe Port
                [1022:43c7]     16:01.0  PCI bridge                               400 Series Chipset PCIe Port
                [1022:43c7]     16:02.0  PCI bridge                               400 Series Chipset PCIe Port
                [1022:43c7]     16:03.0  PCI bridge                               400 Series Chipset PCIe Port
                [1022:43c7]     16:04.0  PCI bridge                               400 Series Chipset PCIe Port
                [8086:1539] [R] 18:00.0  Ethernet controller                      I211 Gigabit Network Connection
                [144d:a808] [R] 1b:00.0  Non-Volatile memory controller           NVMe SSD Controller SM981/PM981/PM983
Group 16:       [1022:1470] [R] 1c:00.0  PCI bridge                               Vega 10 PCIe Bridge
Group 17:       [1022:1471] [R] 1d:00.0  PCI bridge                               Vega 10 PCIe Bridge
Group 18:       [1002:687f] [R] 1e:00.0  VGA compatible controller                Vega 10 XL/XT [Radeon RX Vega 56/64]
Group 19:       [1002:aaf8]     1e:00.1  Audio device                             Vega 10 HDMI Audio [Radeon Vega 56/64]
Group 20:       [10de:1b82] [R] 1f:00.0  VGA compatible controller                GP104 [GeForce GTX 1070 Ti]
                [10de:10f0]     1f:00.1  Audio device                             GP104 High Definition Audio Controller
Group 21:       [1022:145a] [R] 20:00.0  Non-Essential Instrumentation [1300]     Zeppelin/Raven/Raven2 PCIe Dummy Function
Group 22:       [1022:1456]     20:00.2  Encryption controller                    Family 17h (Models 00h-0fh) Platform Security Processor
Group 23:       [1022:145f] [R] 20:00.3  USB controller                           Zeppelin USB 3.0 Host controller
USB:            [046d:c53f]              Bus 003 Device 007                       Logitech, Inc. USB Receiver 
USB:            [1050:0407]              Bus 003 Device 006                       Yubico.com Yubikey 4/5 OTP+U2F+CCID 
USB:            [0bda:5411]              Bus 003 Device 005                       Realtek Semiconductor Corp. RTS5411 Hub 
USB:            [051d:0002]              Bus 003 Device 004                       American Power Conversion Uninterruptible Power Supply 
USB:            [1235:8210]              Bus 003 Device 003                       Focusrite-Novation Scarlett 2i2 Camera 
USB:            [05e3:0610]              Bus 003 Device 002                       Genesys Logic, Inc. Hub 
USB:            [1d6b:0002]              Bus 003 Device 001                       Linux Foundation 2.0 root hub 
USB:            [0781:5581]              Bus 004 Device 004                       SanDisk Corp. Ultra 
USB:            [05e3:0626]              Bus 004 Device 003                       Genesys Logic, Inc. USB3.1 Hub 
USB:            [1d6b:0003]              Bus 004 Device 001                       Linux Foundation 3.0 root hub 
Group 24:       [1022:1455] [R] 21:00.0  Non-Essential Instrumentation [1300]     Zeppelin/Renoir PCIe Dummy Function
Group 25:       [1022:7901]     21:00.2  SATA controller                          FCH SATA Controller [AHCI mode]
Group 26:       [1022:1457]     21:00.3  Audio device                             Family 17h (Models 00h-0fh) HD Audio Controller

@FullStackAlex
Copy link

Nice shit man! Much better than

  shopt -s nullglob
  for d in /sys/kernel/iommu_groups/{0..999}/devices/*; do
      n=${d#*/iommu_groups/*}; n=${n%%/*}
      printf 'IOMMU Group %s ' "$n"
      lspci -nns "${d##*/}"
  done;

Thank you!

@Vincinator
Copy link

Cool, thank you :)

@gnome41
Copy link

gnome41 commented Oct 17, 2022

Crikey!
This is going in my $PATH =]
Thank you very much!

@relaydenied
Copy link

Kik Azz!

@jtxnn678
Copy link

jtxnn678 commented Dec 15, 2023

Open source router N5105 output:

root@pve ~# ./iommu.sh
Group 0: [8086:4e61] [R] 00:02.0 VGA compatible controller JasperLake [UHD Graphics]
Group 1: [8086:4e24] 00:00.0 Host bridge Device 4e24
Group 2: [8086:4e03] 00:04.0 Signal processing controller Dynamic Tuning service
Group 3: [8086:4ded] 00:14.0 USB controller Device 4ded
USB: [046d:c52b] Bus 001 Device 002 Logitech, Inc. Unifying Receiver
USB: [1d6b:0002] Bus 001 Device 001 Linux Foundation 2.0 root hub
USB: [1d6b:0003] Bus 002 Device 001 Linux Foundation 3.0 root hub
[8086:4def] 00:14.2 RAM memory Device 4def
Group 4: [8086:4de0] 00:16.0 Communication controller Management Engine Interface
Group 5: [8086:4dd3] 00:17.0 SATA controller Device 4dd3
Group 6: [8086:4db8] [R] 00:1c.0 PCI bridge Device 4db8
Group 7: [8086:4dbc] [R] 00:1c.4 PCI bridge Device 4dbc
Group 8: [8086:4dbd] [R] 00:1c.5 PCI bridge Device 4dbd
Group 9: [8086:4dbe] [R] 00:1c.6 PCI bridge Device 4dbe
Group 10: [8086:4dbf] [R] 00:1c.7 PCI bridge Device 4dbf
Group 11: [8086:4d87] 00:1f.0 ISA bridge Device 4d87
[8086:4dc8] 00:1f.3 Audio device Jasper Lake HD Audio
[8086:4da3] 00:1f.4 SMBus Jasper Lake SMBus
[8086:4da4] 00:1f.5 Serial bus controller Jasper Lake SPI Controller
Group 12: [1cc1:8201] [R] 01:00.0 Non-Volatile memory controller XPG SX8200 Pro PCIe Gen3x4 M.2 2280 Solid State Drive
Group 13: [8086:125c] [R] 02:00.0 Ethernet controller Ethernet Controller I226-V
Group 14: [8086:125c] [R] 03:00.0 Ethernet controller Ethernet Controller I226-V
Group 15: [8086:125c] [R] 04:00.0 Ethernet controller Ethernet Controller I226-V
Group 16: [8086:125c] [R] 05:00.0 Ethernet controller Ethernet Controller I226-V

@anchaides
Copy link

Thank you!

@digitalformula
Copy link

Is there any way to expand this to show more details about the Ethernet controllers e.g. their MAC address? When passing raw devices through in Proxmox etc it is nearly impossible to correctly match IOMMU group to Ethernet controllers as it doesn't show which connected NICs relate to each IOMMU group.

@mitchty
Copy link

mitchty commented Jul 10, 2024

@digitalformula yeah something like this (quick hack):

iommu group 15:
02:00.0 Network controller [0280]: Intel Corporation Wi-Fi 6E(802.11ax) AX210/AX1675* 2x2 [Typhoon Peak] [8086:2725] (rev 1a)
net:    wlp2s0  mac ff:ff:ff:ff:ff:ff

I adapted this script a bit to be posix shell and only what I need, but this is what I abuse:

cur=''
for dir in $(find /sys/kernel/iommu_groups/ -type l | sort -n -k5 -t/); do
  iommu=${dir#*/iommu_groups/*}
  iommu=${iommu%%/*}
  if [ "${iommu}" != "${cur}" ]; then
    printf 'iommu group %s:\n' "${iommu}"
    cur="${iommu}"
  fi

  lspci -nns "${dir##*/}"

  for usb in ${dir}/usb*/; do
    if [ -e "${usb}/busnum" ]; then
      bus=$(cat "${usb}/busnum")
      lsusb -s "${bus}": \
        | awk '{gsub(/:/,"",$4); printf "%s|%s %s %s %s|", $6, $1, $2, $3, $4; for(i=7;i<=NF;i++){printf "%s ", $i}; printf "\n"}' \
        | awk -F'|' '{printf "usb:\t[%s]\t %-40s %s\n", $1, $2, $3}'
    fi
  done

  for net in ${dir}/net/*; do
    if [ -e "${net}/address" ]; then
      name=$(basename "${net}")
      mac=$(cat "${net}/address")
      printf "net:\t%s\tmac %s\n" "${name}" "${mac}"
    fi
  done
done

You could snag more stuff out of the device tree but device name/mac is a decent enough start.

@digitalformula
Copy link

@digitalformula yeah something like this (quick hack):

iommu group 15:
02:00.0 Network controller [0280]: Intel Corporation Wi-Fi 6E(802.11ax) AX210/AX1675* 2x2 [Typhoon Peak] [8086:2725] (rev 1a)
net:    wlp2s0  mac ff:ff:ff:ff:ff:ff

I adapted this script a bit to be posix shell and only what I need, but this is what I abuse:

cur=''
for dir in $(find /sys/kernel/iommu_groups/ -type l | sort -n -k5 -t/); do
  iommu=${dir#*/iommu_groups/*}
  iommu=${iommu%%/*}
  if [ "${iommu}" != "${cur}" ]; then
    printf 'iommu group %s:\n' "${iommu}"
    cur="${iommu}"
  fi

  lspci -nns "${dir##*/}"

  for usb in ${dir}/usb*/; do
    if [ -e "${usb}/busnum" ]; then
      bus=$(cat "${usb}/busnum")
      lsusb -s "${bus}": \
        | awk '{gsub(/:/,"",$4); printf "%s|%s %s %s %s|", $6, $1, $2, $3, $4; for(i=7;i<=NF;i++){printf "%s ", $i}; printf "\n"}' \
        | awk -F'|' '{printf "usb:\t[%s]\t %-40s %s\n", $1, $2, $3}'
    fi
  done

  for net in ${dir}/net/*; do
    if [ -e "${net}/address" ]; then
      name=$(basename "${net}")
      mac=$(cat "${net}/address")
      printf "net:\t%s\tmac %s\n" "${name}" "${mac}"
    fi
  done
done

You could snag more stuff out of the device tree but device name/mac is a decent enough start.

Brilliant, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment