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