From mboxrd@z Thu Jan 1 00:00:00 1970 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 780DE329E52 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=Ke8JJlf/sCR3zBg7Y5mv2J3x87pMTLZbSwkKzavr4gj1SjvyKwY1Ge/SpYvbqThu0Q50X5UTtlNkf0vuIOV9+lmKQVCpsga3ruuGEJsYzP+SFJ/YEE6c6PzPU7rQnybe2fW727BJ4Qu80aFknELB15sXuxbiZPZFpsztzXV4e5E= 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:Message-Id:References:In-Reply-To: To:Cc:Content-Type; b=u9ZdIbEC1K2ZC+GLJDec1COMhGRrQBQFRtkQAoCKgoLX6Z/gnCb9t6CfKByfhOSEiS2hyE+j13whHl0T48Xv/BoXK5oWTm9sgAoEQgfmpLaHCvo/fzd2aDDgE9LpvX0l+0LgM/7VdLerqGvnIFIHce+O1NKjOP4t5yrCgkhBrGE= 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; 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-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-524-Iv51MSo3NyalQm4AaJaShQ-1; Wed, 19 Nov 2025 17:19:29 -0500 X-MC-Unique: Iv51MSo3NyalQm4AaJaShQ-1 X-Mimecast-MFC-AGG-ID: Iv51MSo3NyalQm4AaJaShQ_1763590768 Received: by mail-qv1-f71.google.com with SMTP id 6a1803df08f44-8823a371984so5401596d6.1 for ; Wed, 19 Nov 2025 14:19:28 -0800 (PST) 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=DD5RGKEu5q5cRgi+b5S0jtB/sTJa4zzjY8eLmffjFUh7xrzuyrA84PS1ZMLEvHO/nd 9RB1s8htIXrxTa/2yLX0cXmaSgbQsUHQrnQYXeGhdFKJAcHc7OB5+VJFFlUIA4CveHhm HuBCylD8OKgwoJxFy9c4/TqK/Z2ciQ4pJCCQ3ZZa27h1gyru8Ems2FZvSQlIRSabl1Tm EQGngm4424LqtHArAzDGS9a/4vT5fou14fFmQDFHgyX+rL7s/4wAg5bSzGeWPde09plr 7RbMKuFPT03SXPYLVb3U/a88LmgFxb/9pbfR0/VB5lCGZiz0lID5XLYoxk+PoI0NLnb+ ysHw== X-Forwarded-Encrypted: i=1; AJvYcCX2/AtIn90pAz+nyc+/n/XMyKNWL0xSzYmlOt0gmTeZl4n4QLuDw04AF3weZDYxyZG4vYPPe81CzjYK72cjZw==@vger.kernel.org X-Gm-Message-State: AOJu0YxsKt8BxhzXIg5ZhVVgnYxRXMVT7AcSNeqPdpyyY22eDJFpn6GF dz0GhGiUlLR1KVRqlnTNLqebQzlg65F7dEgEEimtvTTkpUMoq5DDUc3ydFPDG7QZoT2wKtpPlyj RKJlgRKeP+lsDBhv+uN70sSHQG11egIy5LwgEutqEJ/jvdl/dt2YD0TqFSrTWz4eVVU2A X-Gm-Gg: ASbGncvHyQaFQy+Pp+3hQIoZiTU+OAfQShosZDuGpwYtFkvjkn5AXvCmoNnz/jLQIxs 44X9ZH6CSEkR1jQiUcHVxRqDkfvM8Wm0qJfTWX5RhiqYeEgnb0u3y6/d5IvzbpdZ9haEvhoIvGb zrUqHRe3PxwDGCmM+aLOFHNDGDX14exQMtpSnM4hMElE2dSp71oDAwWn9FFi7uB0xOyYua8vSGk NP7iYnKZ+s/bt25AuRKFwVBj54/VyOtxbA4ertZgXq34zySL95V1HH7ss1hHubafzsqegGeErvK NwkxipjJjRWDIhk2Y0AwxiRtfyCGlwmQ38lKxLS3sEh5NlCI4Rrm6hXbDPrWrpnOW4jCX9sncIw oVexcMzTzsbagUg== X-Received: by 2002:ad4:5f48:0:b0:880:854:908f with SMTP id 6a1803df08f44-8846ee4d842mr9684776d6.38.1763590768224; 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: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 7SA-mFw-rc84uQeZ4cDYIYI76maAv1ETRIg9wI7xhgE_1763590768 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit 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..f2b00c6d9feca43443d3618da32dce2c6ab8c569 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 PCIE BANDWIDTH CONTROLLER M: Ilpo Järvinen diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig index b66bed5d3f36d20302bf586dfdb002d39ed9796e..b6154b6cd91f1d733f05b7529f67a79fe032b50c 100644 --- a/samples/rust/Kconfig +++ b/samples/rust/Kconfig @@ -128,6 +128,17 @@ config SAMPLE_RUST_DRIVER_PLATFORM If unsure, say N. +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 = y && BROKEN diff --git a/samples/rust/Makefile b/samples/rust/Makefile index f65885d1d62bf406b0db13121ef3e5b09829cfbc..a4c12c1200949d0233e08a34e86cea1ff72d0ad7 100644 --- a/samples/rust/Makefile +++ b/samples/rust/Makefile @@ -11,6 +11,7 @@ obj-$(CONFIG_SAMPLE_RUST_DRIVER_I2C) += rust_driver_i2c.o obj-$(CONFIG_SAMPLE_RUST_I2C_CLIENT) += rust_i2c_client.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PCI) += rust_driver_pci.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o +obj-$(CONFIG_SAMPLE_RUST_DRIVER_SRIOV) += rust_driver_sriov.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_USB) += rust_driver_usb.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) += rust_driver_faux.o obj-$(CONFIG_SAMPLE_RUST_DRIVER_AUXILIARY) += rust_driver_auxiliary.o diff --git a/samples/rust/rust_driver_sriov.rs b/samples/rust/rust_driver_sriov.rs new file mode 100644 index 0000000000000000000000000000000000000000..a60d58d00a2d24883ddcb7236e525c448ae86b4f --- /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=kvm,kernel-irqchip=split -device intel-iommu,intremap=on,caching-mode=on` +//! +//! and append `intel_iommu=on` 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 = (); + + const ID_TABLE: pci::IdTable = &PCI_TABLE; + + fn probe(pdev: &pci::Device, _info: &Self::IdInfo) -> impl PinInit { + 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 = 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 == 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", +} -- 2.51.1