1374a2455SGustavo Romero#!/usr/bin/env python3 2374a2455SGustavo Romero# 3374a2455SGustavo Romero# The test hotplugs a PCI device and checks it on a Linux guest. 4374a2455SGustavo Romero# 5374a2455SGustavo Romero# Copyright (c) 2025 Linaro Ltd. 6374a2455SGustavo Romero# 7374a2455SGustavo Romero# Author: 8374a2455SGustavo Romero# Gustavo Romero <gustavo.romero@linaro.org> 9374a2455SGustavo Romero# 10374a2455SGustavo Romero# SPDX-License-Identifier: GPL-2.0-or-later 11374a2455SGustavo Romero 12374a2455SGustavo Romerofrom qemu_test import LinuxKernelTest, Asset, exec_command_and_wait_for_pattern 13374a2455SGustavo Romerofrom qemu_test import BUILD_DIR 14374a2455SGustavo Romero 15374a2455SGustavo Romeroclass HotplugPCI(LinuxKernelTest): 16374a2455SGustavo Romero 17374a2455SGustavo Romero ASSET_KERNEL = Asset( 18*621dbaeeSStefan Hajnoczi ('https://ftp.debian.org/debian/dists/bookworm/main/installer-arm64/' 19374a2455SGustavo Romero '20230607+deb12u11/images/netboot/debian-installer/arm64/linux'), 20374a2455SGustavo Romero 'd92a60392ce1e379ca198a1a820899f8f0d39a62d047c41ab79492f81541a9d9') 21374a2455SGustavo Romero 22374a2455SGustavo Romero ASSET_INITRD = Asset( 23*621dbaeeSStefan Hajnoczi ('https://ftp.debian.org/debian/dists/bookworm/main/installer-arm64/' 24374a2455SGustavo Romero '20230607+deb12u11/images/netboot/debian-installer/arm64/initrd.gz'), 25374a2455SGustavo Romero '9f817f76951f3237bca8216bee35267bfb826815687f4b2fcdd5e6c2a917790c') 26374a2455SGustavo Romero 27374a2455SGustavo Romero def test_hotplug_pci(self): 28374a2455SGustavo Romero 29374a2455SGustavo Romero self.set_machine('virt') 30374a2455SGustavo Romero 31374a2455SGustavo Romero self.vm.add_args('-m', '512M', 32374a2455SGustavo Romero '-cpu', 'cortex-a57', 33374a2455SGustavo Romero '-append', 34374a2455SGustavo Romero 'console=ttyAMA0,115200 init=/bin/sh', 35374a2455SGustavo Romero '-device', 36374a2455SGustavo Romero 'pcie-root-port,bus=pcie.0,chassis=1,slot=1,id=pcie.1', 37374a2455SGustavo Romero '-bios', 38374a2455SGustavo Romero self.build_file('pc-bios', 'edk2-aarch64-code.fd')) 39374a2455SGustavo Romero 40374a2455SGustavo Romero # BusyBox prompt 41374a2455SGustavo Romero prompt = "~ #" 42374a2455SGustavo Romero self.launch_kernel(self.ASSET_KERNEL.fetch(), 43374a2455SGustavo Romero self.ASSET_INITRD.fetch(), 44374a2455SGustavo Romero wait_for=prompt) 45374a2455SGustavo Romero 46374a2455SGustavo Romero # Check for initial state: 2 network adapters, lo and enp0s1. 47374a2455SGustavo Romero exec_command_and_wait_for_pattern(self, 48374a2455SGustavo Romero 'ls /sys/class/net | wc -l', 49374a2455SGustavo Romero '2') 50374a2455SGustavo Romero 51374a2455SGustavo Romero # Hotplug one network adapter to the root port, i.e. pcie.1 bus. 52374a2455SGustavo Romero self.vm.cmd('device_add', 53374a2455SGustavo Romero driver='virtio-net-pci', 54374a2455SGustavo Romero bus='pcie.1', 55374a2455SGustavo Romero addr=0, 56374a2455SGustavo Romero id='na') 57374a2455SGustavo Romero # Wait for the kernel to recognize the new device. 58374a2455SGustavo Romero self.wait_for_console_pattern('virtio-pci') 59374a2455SGustavo Romero self.wait_for_console_pattern('virtio_net') 60374a2455SGustavo Romero 61374a2455SGustavo Romero # Check if there is a new network adapter. 62374a2455SGustavo Romero exec_command_and_wait_for_pattern(self, 63374a2455SGustavo Romero 'ls /sys/class/net | wc -l', 64374a2455SGustavo Romero '3') 65374a2455SGustavo Romero 66374a2455SGustavo Romero self.vm.cmd('device_del', id='na') 67374a2455SGustavo Romero exec_command_and_wait_for_pattern(self, 68374a2455SGustavo Romero 'ls /sys/class/net | wc -l', 69374a2455SGustavo Romero '2') 70374a2455SGustavo Romero 71374a2455SGustavo Romeroif __name__ == '__main__': 72374a2455SGustavo Romero LinuxKernelTest.main() 73