From nobody Tue Dec 2 02:29:52 2025 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 758FC28688C for ; Wed, 19 Nov 2025 22:19:31 +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=1763590775; cv=none; b=qyFpp6ChcZfpVMsJqYep/GZaWsHR8ZX65hbSsURUToGXX7TvkAu5WHm2OODfOXpSLkOHFATmB73hkJj1EcDkspHkBUsL/WydoRuYmY5KzhiEDeDpzqCW4KPcBshfk+6j2HcXwgRzZiYiTzEEXE5KLnBoc0mpCqEhfWgzGyY19rE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763590775; c=relaxed/simple; bh=D99WhKJ/ghNL6aENXqMYUdXI5q5I1XP4JOCtNjQCfz4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hkr4qc+fuRSgsPDxB8tFxpb0KKlz4CQwtyFoRNt9MVpv5XhYRftK56029SHnHJ7/iKA1u7HN0rX2MYrEz9raZAIv37TE8Mk25+oCkIhUi7WjJTMi5RwO/fdjShFAcMycCALK2FvGwzZcOiYpIndoHxQ8iJ/AFBh6jYf13jN5rew= 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=SR1aRkCH; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=CFubLoJo; 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="SR1aRkCH"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="CFubLoJo" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763590770; 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=C1vRHV6Homj57YBK4as5Vvxfa66arqOM/2yw45uMPyw=; b=SR1aRkCHRsebHJVdiZVloKFjD0quEGtufkGhbc0IuNM8Zl1F0uKr2FTLEPbwREC2Td/T8F Dozvgo1Q/xt/DcPbvH0IzCUR6wYvHwN9NANlb4YbOQDbqiS+mydPtLNL6cFjuaNZhWafi0 IIgFQnP1Z/dwF5qle23k9Sl+wEKpVSo= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-512-INC7j8EMPeyaZO24jBSl4g-1; Wed, 19 Nov 2025 17:19:28 -0500 X-MC-Unique: INC7j8EMPeyaZO24jBSl4g-1 X-Mimecast-MFC-AGG-ID: INC7j8EMPeyaZO24jBSl4g_1763590768 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-8823a371984so5401506d6.1 for ; Wed, 19 Nov 2025 14:19:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1763590768; x=1764195568; 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=C1vRHV6Homj57YBK4as5Vvxfa66arqOM/2yw45uMPyw=; b=CFubLoJo1fnvfOUwFJHPdUyUOsH65+iipVR5y2IRzjzGjp4ILW+w3UaKTyRgpfovOT 0cAW2l5DKh/eRSZDiCZCLqLU5+jUa0iz/naA3gop0DUopN3Njc1WzQv5TtLDoQvMIGp0 fTOi5rFCl6prOhIwdghY4JVzA1iHogXoQyu18RkRiZghREQVMFHPwVR9Q09bHc/ITAQ5 1xnHXHnxqEL6d7EJFfvFdcNtvh3P4rzZ8MwD9EFLwpaQxowTm6gV3+hvIvOWhMyPZZxi e+XEXnQuytKBioT6q7pr90l0olOUwO0Epn15Iv6a4Y5YwEXd+2ZCFi/2ZKMshp5RHRZZ f8nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763590768; x=1764195568; 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=C1vRHV6Homj57YBK4as5Vvxfa66arqOM/2yw45uMPyw=; b=SNaIZCu5G4mQExNDwjHLw2kMU7ExhCD13GjeK8rnMseb/xNTqx0y6J2KWUuaRgWWmD LtBkRr+1XDa6wZf5uMNx3vM3VZpp9CCSjbmUckn/Asqj3MhDL1ak1vAhastqhequjRBj SqIxjkREPd/GJGj2lu+C1dile+dOUGah5Mb6uYDy35xKHvqLNTEofmspuzF5xCFvI25U 9Fx5R+IcySnZgEx8coXj8fL0KhL6CzZ2/NykyIVs+KyiRX8Ilos3uOs1mUMtT4uB/HFL PN+9GKeUkc7mu2HrJUZSv5KSdbVGGohF082eYhjnIo/tN899nu4V22wPtYE4gmk6uffs lwjg== X-Forwarded-Encrypted: i=1; AJvYcCW3njw6WDVLR/RCKyQixEutrFjwQgAO0Z3jaG3KY5+3eEptgUq7bjwIm7zrqJz3tJgOzIflr+IUFJFh9ic=@vger.kernel.org X-Gm-Message-State: AOJu0YzUeL1PQEt153BE/nfBcjy0HmgnGqLimCmRaYDqisPqihJcUifm iXO62rxQJYSmyj2veqDvUdG3/U5Hw2WicO9sUbVUi0lt2jJInFO0/Wdlr2McA41c8tpf6ZHZfv1 Y4Xqk22Q+5U2toj0+EULs4WpiUxREpPE4JQ7XWyhhOg9ox6z+QhuhyhmI9sAG/Mu/4A== X-Gm-Gg: ASbGncvddwbYT9ArdbnXpVebf35692iLAI95eSDVyN1KjBK6uAaLo4/h9Jzvj6amChA omLsb/akYPKkX5LLCzNJ+pgu6AwTQxOouEHWLVfrr41+CcdBDbqnAFXRvMj3WOQOszUT4WZv/Q4 CkuyaXgLAk0QhRft/yJbEHRE2yoJ8NipXahJ7EGfkAp7XCiCpHdAK1LCBdmQ+ClniI7FLFSIA1v 1rR1TfhoX5bL3Bx33KwnGuO5JM5z3MFDflBzxOfTZNJWp++zGnwMlxqXPf81VooQKcvt3gTrhoy zqosp2R92yRIX4pUEg5RkHXXwpTQQXcjO95VitQl4qm47J2YFzIP7VLHRJmqla933FF9UAZvuMq l30zqjyaBzgN8iA== X-Received: by 2002:ad4:5f48:0:b0:880:854:908f with SMTP id 6a1803df08f44-8846ee4d842mr9684816d6.38.1763590768228; Wed, 19 Nov 2025 14:19:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IEgX6yxrFC18TElkj+HEVHEmxQ6fPmK7tD7lfiINSPtKVxeJ6AWjy050Wgsa3odgLZhl31R4w== X-Received: by 2002:ad4:5f48:0:b0:880:854:908f with SMTP id 6a1803df08f44-8846ee4d842mr9684146d6.38.1763590767686; Wed, 19 Nov 2025 14:19:27 -0800 (PST) Received: from [172.16.1.8] ([2607:f2c0:b141:ac00:ca1:dc8c:d6d0:7e87]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8846e447304sm4426866d6.4.2025.11.19.14.19.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Nov 2025 14:19:27 -0800 (PST) From: Peter Colberg Date: Wed, 19 Nov 2025 17:19:12 -0500 Subject: [PATCH 8/8] 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: <20251119-rust-pci-sriov-v1-8-883a94599a97@redhat.com> References: <20251119-rust-pci-sriov-v1-0-883a94599a97@redhat.com> In-Reply-To: <20251119-rust-pci-sriov-v1-0-883a94599a97@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 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 , 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 --- MAINTAINERS | 1 + samples/rust/Kconfig | 11 ++++ samples/rust/Makefile | 1 + samples/rust/rust_driver_sriov.rs | 107 ++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 120 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index b71ea515240a4c5db6d932efce5183386f3a3f79..f2b00c6d9feca43443d3618da32= dce2c6ab8c569 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -19945,6 +19945,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 b66bed5d3f36d20302bf586dfdb002d39ed9796e..b6154b6cd91f1d733f05b7529f6= 7a79fe032b50c 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 && BROKEN diff --git a/samples/rust/Makefile b/samples/rust/Makefile index f65885d1d62bf406b0db13121ef3e5b09829cfbc..a4c12c1200949d0233e08a34e86= cea1ff72d0ad7 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..a60d58d00a2d24883ddcb7236e5= 25c448ae86b4f --- /dev/null +++ b/samples/rust/rust_driver_sriov.rs @@ -0,0 +1,107 @@ +// 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}; + +#[pin_data(PinnedDrop)] +struct SampleDriver { + pdev: ARef, +} + +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()?; + assert!(physfn.is_physfn()); + dev_info!( + pdev.as_ref(), + "Parent device is PF (PCI ID: {}, 0x{:x}).\n", + physfn.vendor_id(), + physfn.device_id() + ); + } + + pdev.enable_device_mem()?; + pdev.set_master(); + + Ok(try_pin_init!(Self { pdev: pdev.into() })) + }) + } + + 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.51.1