| 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # The test hotplugs a PCI device and checks it on a Linux guest. |
| 4 | # |
| 5 | # Copyright (c) 2025 Linaro Ltd. |
| 6 | # |
| 7 | # Author: |
| 8 | # Gustavo Romero <gustavo.romero@linaro.org> |
| 9 | # |
| 10 | # SPDX-License-Identifier: GPL-2.0-or-later |
| 11 | |
| 12 | from qemu_test import LinuxKernelTest, Asset, exec_command_and_wait_for_pattern |
| 13 | from qemu_test import BUILD_DIR |
| 14 | |
| 15 | class HotplugPCI(LinuxKernelTest): |
| 16 | |
| 17 | ASSET_KERNEL = Asset( |
| 18 | ('https://ftp.debian.org/debian/dists/trixie/main/installer-arm64/' |
| 19 | '20250803/images/netboot/debian-installer/arm64/linux'), |
| 20 | '93a6e4f9627d759375d28f863437a86a0659e125792a435f8e526dda006b7d5e') |
| 21 | |
| 22 | ASSET_INITRD = Asset( |
| 23 | ('https://ftp.debian.org/debian/dists/trixie/main/installer-arm64/' |
| 24 | '20250803/images/netboot/debian-installer/arm64/initrd.gz'), |
| 25 | 'f6c78af7078ca67638ef3a50c926cd3c1485673243f8b37952e6bd854d6ba007') |
| 26 | |
| 27 | def test_hotplug_pci(self): |
| 28 | |
| 29 | self.set_machine('virt') |
| 30 | self.require_accelerator('tcg') |
| 31 | |
| 32 | self.vm.add_args('-m', '512M', |
| 33 | '-cpu', 'cortex-a57', |
| 34 | '-append', |
| 35 | 'console=ttyAMA0,115200 init=/bin/sh', |
| 36 | '-device', |
| 37 | 'pcie-root-port,bus=pcie.0,chassis=1,slot=1,id=pcie.1', |
| 38 | '-bios', |
| 39 | self.build_file('pc-bios', 'edk2-aarch64-code.fd')) |
| 40 | |
| 41 | # BusyBox prompt |
| 42 | prompt = "~ #" |
| 43 | self.launch_kernel(self.ASSET_KERNEL.fetch(), |
| 44 | self.ASSET_INITRD.fetch(), |
| 45 | wait_for=prompt) |
| 46 | |
| 47 | # Check for initial state: 2 network adapters, lo and enp0s1. |
| 48 | exec_command_and_wait_for_pattern(self, |
| 49 | 'ls /sys/class/net | wc -l', |
| 50 | '2') |
| 51 | |
| 52 | # Hotplug one network adapter to the root port, i.e. pcie.1 bus. |
| 53 | self.vm.cmd('device_add', |
| 54 | driver='virtio-net-pci', |
| 55 | bus='pcie.1', |
| 56 | addr=0, |
| 57 | id='na') |
| 58 | # Wait for the kernel to recognize the new device. |
| 59 | self.wait_for_console_pattern('virtio-pci') |
| 60 | self.wait_for_console_pattern('virtio_net') |
| 61 | |
| 62 | # Check if there is a new network adapter. |
| 63 | exec_command_and_wait_for_pattern(self, |
| 64 | 'ls /sys/class/net | wc -l', |
| 65 | '3') |
| 66 | |
| 67 | self.vm.cmd('device_del', id='na') |
| 68 | exec_command_and_wait_for_pattern(self, |
| 69 | 'ls /sys/class/net | wc -l', |
| 70 | '2') |
| 71 | |
| 72 | if __name__ == '__main__': |
| 73 | LinuxKernelTest.main() |