From nobody Mon Feb 9 16:19:51 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 441413358B0 for ; Thu, 5 Feb 2026 21:00:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770325244; cv=none; b=BfiFBNntH5o1WPjZ7E+uIxZ/qo78TzLPX41eSKPnUt7V6VFfZZCQhyPI4td18r2/D6k10JsF+VFEeLkhfI8GpqbZWLN7JwldFrGLD+yfnEU7drOXhnM3rTYj4soUYd1LfU60nInjv1ZDDp6BbqOAKFnsJRrIrBAgvHNm9xdusws= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770325244; c=relaxed/simple; bh=DsoH5kljF8z69G5m/2pqjtsvxAd0CBzUg2OXfZWFlWQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oHaIAGG4/fXHy18ZRwTeqxhxnSMX45qhU10OlCs0h7dfYu3NeDqLJ5zpPU+ufqQ1+M/2P73z05PoD+NOy63XSmzpY9M7WxArfPGp44RTizk8pEVqNHuuJP9tomfThgixgDzB6rYSAVQCNIbRCuJe0x1AZuvRWkpApTlC0z5dgRc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=FPMn4KQw; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=CjE4iDbo; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="FPMn4KQw"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="CjE4iDbo" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1770325243; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BMqbQ59ZEPX4CdhRYiH6E8cBHnvUegI817wkrdE94SY=; b=FPMn4KQwmZNXeuAr8sNW6WPxBDpRpriID8WzbvdMYaxY0NX0l6kzkYcm6LvAIxlEZn2Cac SGl4ptaD5jgKBvN5RUtuSUWO8v+CgqVrU7fV7CaVglrt9/SdpMP1WI7I7b5AYrtNhpvGpP Jg0CvBm+5xr6fWqiWCBO2ZIk7vZVnJY= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-540-iWFiV1QvNaqwExO4GvqTew-1; Thu, 05 Feb 2026 16:00:42 -0500 X-MC-Unique: iWFiV1QvNaqwExO4GvqTew-1 X-Mimecast-MFC-AGG-ID: iWFiV1QvNaqwExO4GvqTew_1770325242 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-8c882774f0dso375467785a.2 for ; Thu, 05 Feb 2026 13:00:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1770325242; x=1770930042; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=BMqbQ59ZEPX4CdhRYiH6E8cBHnvUegI817wkrdE94SY=; b=CjE4iDbooIbm7LD2lFBUtHrDtjNdg6ej4gJapSytdW3ZHcfY81WAGs76gRo+IhcRom oZtGSpz9mdDjhSfkPyBDBxryBh6nCIq2Q6u+ClWtnxQDwdTgtJZuDN1rW0WdW1yqwDm/ 7S7m9ug1WNAwnZ8oq6LX/fHq+DzaG4nE3MRsJOuG9Zv+6l/uoGpwX1KxOXRjthEV39ZV iMUf5Xyxc+v7g0Q/J1jeFOiV4J0+GH9kS7Q+Z2clN1X/MnkI6OAwAcJhuXB/WgdV1BVu fddIDwvUc1TEgoaF4nuwf31zjYcmpU5Tk1OThAo1gr7ATQqWW5pxXGbfEzgB+e0Wm07u vBuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770325242; x=1770930042; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=BMqbQ59ZEPX4CdhRYiH6E8cBHnvUegI817wkrdE94SY=; b=bg+9u3bAkf/v6xLRBu03tZtcasKMFzFI+v3UqqFnppt1yvQ7a6pSXPNg6fJXMiSbU1 kNQHDDSt31kSZAvLud3BP/CnGQeUpqT2q76H/BombmnQhbXugtrEUzDFsMole7JXwWhI E75ECoFLVB+HwBN8klJFnkVRbG67/U+Q/HUfYIsFw3b8yjU4zIQLI5hWfCZMmvZ5n5AB 6xYTce56FQTJvt1XrFagHf2HRcvR+tH7YI7milNfR0FqIu1ers3Nx0hQ9wDszminv3Dx bqy4eZ6KWBClnSaX19inZdi5KgX5oZ1gDZKIWAU5CwwOaPh8gFCEOfqwwpRKf4ADyMv6 I0Mg== X-Forwarded-Encrypted: i=1; AJvYcCXWJYuX6REVfI0fXpnMfooJ0BwAtxYGN+Y+dOeKdwp5xgoOG6ts8wKS+ICxTGQHWdbmx7ucgsg5Fh+7Z0k=@vger.kernel.org X-Gm-Message-State: AOJu0YwHMH6D44QrzwKgchHkbV9hSPS067j6xAiK7RUUujmf4MjhPbCf MgLNzNhogMXhyjFdAW6CqQtmx1CSSsTdKoJNxPt1Cfey8qZIaW93wxgsOIfMEq2thMXolS1aWdi RXEblVm7jaXmBUk54V1RYHav3qmQOI88hWLr5PQ0UdbjUm7W0REJj5hEbWSR47taugw== X-Gm-Gg: AZuq6aIPlSIzUPfReXTyzD7NeegiszML8b9WOC3GsLRX2KIGH7BXq2zVVGUZWZ/FA5/ YROrkyOAHxAduh5f6iT0Vm9yE7tzZo4SCaGrEI1KQmD2AL2+n3TQF2hJ5BrOemPpgBSYmaKNY9O 6mqc0Y/4Is9DpkT8J0RaFIPuie5y7FCl/a4vxmpYuId5Zup8pY6tYGDxKhjHEZquHHvgKtRDcIH i9DwRjMXrAJbAoEywyw9WxL9uJX4D/2sULC9ZgrxG9hWcItNVyVgndR50lz3VoQcgPmy9vQhezV LlFs+GXUR/MDm7PUY857kloECiNiouE92Z1a6c1Vlbx73qtFMpDn7Z3zSEh46VM96oeP8bLJkYM K/OnVNlYvm7kPFjs= X-Received: by 2002:a05:620a:28c5:b0:8ca:3854:8110 with SMTP id af79cd13be357-8caf130789cmr57432885a.72.1770325241417; Thu, 05 Feb 2026 13:00:41 -0800 (PST) X-Received: by 2002:a05:620a:28c5:b0:8ca:3854:8110 with SMTP id af79cd13be357-8caf130789cmr57425985a.72.1770325240837; Thu, 05 Feb 2026 13:00:40 -0800 (PST) Received: from [172.16.1.8] ([2607:f2c0:b010:9000:4c85:f148:4c91:943a]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8caf9fdf692sm17571285a.44.2026.02.05.13.00.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Feb 2026 13:00:40 -0800 (PST) From: Peter Colberg Date: Thu, 05 Feb 2026 15:59:57 -0500 Subject: [PATCH v2 10/10] samples: rust: add SR-IOV driver sample Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260205-rust-pci-sriov-v2-10-ef9400c7767b@redhat.com> References: <20260205-rust-pci-sriov-v2-0-ef9400c7767b@redhat.com> In-Reply-To: <20260205-rust-pci-sriov-v2-0-ef9400c7767b@redhat.com> To: Danilo Krummrich , Bjorn Helgaas , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Abdiel Janulgue , Daniel Almeida , Robin Murphy , Greg Kroah-Hartman , Dave Ertman , Ira Weiny , Leon Romanovsky , David Airlie , Simona Vetter , Jonathan Corbet , Xu Yilun , Tom Rix , Moritz Fischer , "Rafael J. Wysocki" Cc: linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Courbot , Alistair Popple , Joel Fernandes , John Hubbard , Zhi Wang , nouveau@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-fpga@vger.kernel.org, driver-core@lists.linux.dev, Peter Colberg , Jason Gunthorpe X-Mailer: b4 0.14.2 Add a new SR-IOV driver sample that demonstrates how to enable and disable the Single Root I/O Virtualization capability for a PCI device. The sample may be exercised using QEMU's 82576 (igb) emulation. Link: https://www.qemu.org/docs/master/system/devices/igb.html Signed-off-by: Peter Colberg --- Changes in v2: - Use "kernel vertical" style on imports. - Demonstrate how to reach driver data of PF device from VF device. --- MAINTAINERS | 1 + samples/rust/Kconfig | 11 ++++ samples/rust/Makefile | 1 + samples/rust/rust_driver_sriov.rs | 127 ++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 140 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index b277baee5eb6725b3a5126cefb6eef6190b02413..96dffd777c24473063baaeda017= f64b15e8c0ab9 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -20242,6 +20242,7 @@ F: rust/helpers/pci.c F: rust/kernel/pci.rs F: rust/kernel/pci/ F: samples/rust/rust_driver_pci.rs +F: samples/rust/rust_driver_sriov.rs =20 PCIE BANDWIDTH CONTROLLER M: Ilpo J=C3=A4rvinen diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig index c49ab910634596aea4a1a73dac87585e084f420a..f244df89c4fc9d741915f581de7= 6107e8eb0121b 100644 --- a/samples/rust/Kconfig +++ b/samples/rust/Kconfig @@ -128,6 +128,17 @@ config SAMPLE_RUST_DRIVER_PLATFORM =20 If unsure, say N. =20 +config SAMPLE_RUST_DRIVER_SRIOV + tristate "SR-IOV Driver" + depends on PCI_IOV + help + This option builds the Rust SR-IOV driver sample. + + To compile this as a module, choose M here: + the module will be called rust_driver_sriov. + + If unsure, say N. + config SAMPLE_RUST_DRIVER_USB tristate "USB Driver" depends on USB =3D y diff --git a/samples/rust/Makefile b/samples/rust/Makefile index 6c0aaa58ccccfd12ef019f68ca784f6d977bc668..19d700f8210151e298cc049dacc= 249a121d0f2c4 100644 --- a/samples/rust/Makefile +++ b/samples/rust/Makefile @@ -11,6 +11,7 @@ obj-$(CONFIG_SAMPLE_RUST_DRIVER_I2C) +=3D rust_driver_i2= c.o obj-$(CONFIG_SAMPLE_RUST_I2C_CLIENT) +=3D rust_i2c_client.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) +=3D rust_driver_pci.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) +=3D rust_driver_platform.o +obj-$(CONFIG_SAMPLE_RUST_DRIVER_SRIOV) +=3D rust_driver_sriov.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_USB) +=3D rust_driver_usb.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) +=3D rust_driver_faux.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_AUXILIARY) +=3D rust_driver_auxiliary.o diff --git a/samples/rust/rust_driver_sriov.rs b/samples/rust/rust_driver_s= riov.rs new file mode 100644 index 0000000000000000000000000000000000000000..84d057629c7b03d743179a4e05c= cc092f814bf6b --- /dev/null +++ b/samples/rust/rust_driver_sriov.rs @@ -0,0 +1,127 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust SR-IOV driver sample based on QEMU's 82576 ([igb]) emulation. +//! +//! To make this driver probe, QEMU must be run with `-device igb`. +//! +//! Further, enable [vIOMMU] with interrupt remapping using, e.g., +//! +//! `-M q35,accel=3Dkvm,kernel-irqchip=3Dsplit -device intel-iommu,intrema= p=3Don,caching-mode=3Don` +//! +//! and append `intel_iommu=3Don` to the guest kernel arguments. +//! +//! [igb]: https://www.qemu.org/docs/master/system/devices/igb.html +//! [vIOMMU]: https://wiki.qemu.org/Features/VT-d + +use kernel::{ + device::Core, + pci, + prelude::*, + sync::aref::ARef, // +}; + +use core::any::TypeId; + +#[pin_data(PinnedDrop)] +struct SampleDriver { + pdev: ARef, + private: TypeId, +} + +kernel::pci_device_table!( + PCI_TABLE, + MODULE_PCI_TABLE, + ::IdInfo, + [ + // E1000_DEV_ID_82576 + (pci::DeviceId::from_id(pci::Vendor::INTEL, 0x10c9), ()), + // E1000_DEV_ID_82576_VF + (pci::DeviceId::from_id(pci::Vendor::INTEL, 0x10ca), ()) + ] +); + +#[vtable] +impl pci::Driver for SampleDriver { + type IdInfo =3D (); + + const ID_TABLE: pci::IdTable =3D &PCI_TABLE; + + fn probe(pdev: &pci::Device, _info: &Self::IdInfo) -> impl PinIn= it { + pin_init::pin_init_scope(move || { + dev_info!( + pdev.as_ref(), + "Probe Rust SR-IOV driver sample (PCI ID: {}, 0x{:x}).\n", + pdev.vendor_id(), + pdev.device_id() + ); + + if pdev.is_virtfn() { + let physfn =3D pdev.physfn()?; + let drvdata =3D physfn.as_ref().drvdata::()?; + + assert!(physfn.is_physfn()); + + dev_info!( + pdev.as_ref(), + "Parent device is PF (PCI ID: {}, 0x{:x}).\n", + physfn.vendor_id(), + physfn.device_id() + ); + + dev_info!( + pdev.as_ref(), + "We have access to the private data of {:?}.\n", + drvdata.private + ); + } + + pdev.enable_device_mem()?; + pdev.set_master(); + + Ok(try_pin_init!(Self { + pdev: pdev.into(), + private: TypeId::of::() + })) + }) + } + + fn sriov_configure(pdev: &pci::Device, nr_virtfn: i32) -> Result= { + assert!(pdev.is_physfn()); + + if nr_virtfn =3D=3D 0 { + dev_info!( + pdev.as_ref(), + "Disable SR-IOV (PCI ID: {}, 0x{:x}).\n", + pdev.vendor_id(), + pdev.device_id() + ); + pdev.disable_sriov(); + } else { + dev_info!( + pdev.as_ref(), + "Enable SR-IOV (PCI ID: {}, 0x{:x}).\n", + pdev.vendor_id(), + pdev.device_id() + ); + pdev.enable_sriov(nr_virtfn)?; + } + + assert_eq!(pdev.num_vf(), nr_virtfn); + Ok(nr_virtfn) + } +} + +#[pinned_drop] +impl PinnedDrop for SampleDriver { + fn drop(self: Pin<&mut Self>) { + dev_info!(self.pdev.as_ref(), "Remove Rust SR-IOV driver sample.\n= "); + } +} + +kernel::module_pci_driver! { + type: SampleDriver, + name: "rust_driver_sriov", + authors: ["Peter Colberg"], + description: "Rust SR-IOV driver", + license: "GPL v2", +} --=20 2.52.0