From nobody Fri Sep 25 06:47:06 2026 Received: from CO1PR03CU002.outbound.protection.outlook.com (mail-westus2azon11010071.outbound.protection.outlook.com [52.101.46.71]) (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 78A614BB27F; Thu, 24 Sep 2026 19:06:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.46.71 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276821; cv=fail; b=sBfYZyhPVaNXrapVs/VHZGfIBrr5pTMBwfi0NzzlnHSe3jnuyNg+0/vtmQPzG2Z4e3rAae7aC2EQKthDv4AvU/rd2QYYznZ3+pnOtC15wHhB3STwRTp+hKXot8LpVnhK87wjPhKb81Y8c7RSzNcCxEj0M6LtFk1KeaQ8gIjIibA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276821; c=relaxed/simple; bh=HySFwJ+bTWWCMNzSE9PYNuJC7fqNyaeGtxOeFVnqWOo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=f7fpAEoNl0ZEPpiHAylaoLkUn1tbsu45NbrefZZtWylowo0T4YOkQAFknOWs8v8GGxBn5W8lw6O9aB1DuyDw5np7ahmEr+tJmW1lJVY+wd4jiYRVHxBtnevAHWjQCA2i1rI8FDlRXe+hBRKYYCpz/LS3MTcmtv7C5ePG7DtE6aM= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=IOodaql9; arc=fail smtp.client-ip=52.101.46.71 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="IOodaql9" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=T8U5CE0y8w9eDrKdAWXgefNMPyNTKdFPl9cIs0VHu3A9Uc39Us+TtRA/uL88YHGhdHt7NHTJvq8lcr/7f5ahhaEOiVZ1A1P89fWaA44sUs2ziP/cAPdzoxhn0f+jgCvY35czLBfel9DYaQnKjzga4odlJnUE5Zo8SkByysNEOy5LF/ayPxQFX4U6qkVq4orCCqX7VPrzGarbEH/Y63sXbmfF+ScsExtpSk25JC62BhlVEvfdUZXBmh6sQAJrVWKk6sU3uKgQmxT9Y10SSxt21cCNCEXCenJJpBLn/aN9GP4oimXeQWttFCydPAOVrMQQET67ICUXPjidR2Vezpn/ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=aOMJ9op30Bw2OBasDyaAjoaqg5PyG8/Lnub/mucEVFA=; b=LTRAnZPMHMmksC4YC0RxaZfLvsju/SeDik4OfMeDMN5LIBJg2mnfqaJrxuc0EY0/SiCpIX70JGXRSMy0qHKqLDgNQyi1tCLhNjuxGmdg7q8fAftkWYbl784sd++giBMpmEtS4kUE808apNPomrlSe7ONe9tK43JeBD7AkYI5WbWPi72BOg9pJmM/K6VXq0+9eFqU1OVhjxAmQVaUABZ4W+ByzMSWYnswTZx91JoN+SLPF8kR09okD2On0Gn+6Qztcg1dT6tEEkcOt4DwyFggrrwnwgm3TbU8ca/Z83OYRtTGHdJ6pJmK4aPozQqV4J29u7JY6LjbxNeiYFmc7rD25Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=aOMJ9op30Bw2OBasDyaAjoaqg5PyG8/Lnub/mucEVFA=; b=IOodaql9cCggLXeqlkJ6BVBXCy5TWT3hmnPf6ryrLhob2Oa3/Fvzyp0WdvPsUCQS4xsTAlviDL9rG+u938LLB1Na0X/xTjFOmXXgKOu2akT4VAhlOFTGJXgvGfS3BjIb4DPiAn3TR7DG6z7SrFXr+AiIVNZEHB8veCERjc6WG+Uk23moAfLSZewTDGQCVkOw74qi2ioRbE0WY5TEPIIm+1ExRqeKbvpdltgd18K8LAvWGjq3m8/mFrs9UX15lybgNJB1lXQEhIlKSxjNAQkEU65ooXEcybaRiJK1+13JkZVjWnwmW5OHaZAP2Gxue7UY5b9XAZxS4iuafpGnwQOsNA== Received: from YT4PR01CA0474.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:d6::28) by IA1PR12MB7543.namprd12.prod.outlook.com (2603:10b6:208:42d::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.18; Thu, 24 Sep 2026 19:06:54 +0000 Received: from BN2PEPF000044A3.namprd02.prod.outlook.com (2603:10b6:b01:d6:cafe::49) by YT4PR01CA0474.outlook.office365.com (2603:10b6:b01:d6::28) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.451.18 via Frontend Transport; Thu, 24 Sep 2026 19:06:54 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BN2PEPF000044A3.mail.protection.outlook.com (10.167.243.154) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.8 via Frontend Transport; Thu, 24 Sep 2026 19:06:54 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:06:19 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:06:19 -0700 Received: from inno-dell.home (10.127.8.10) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.2562.49 via Frontend Transport; Thu, 24 Sep 2026 12:06:10 -0700 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Peter Colberg , Zhi Wang Subject: [PATCH v2 1/8] rust: pci: add {enable,disable}_sriov(), to control SR-IOV capability Date: Thu, 24 Sep 2026 22:05:48 +0300 Message-ID: <20260924190556.1620886-2-zhiw@nvidia.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260924190556.1620886-1-zhiw@nvidia.com> References: <20260924190556.1620886-1-zhiw@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN2PEPF000044A3:EE_|IA1PR12MB7543:EE_ X-MS-Office365-Filtering-Correlation-Id: e9f7ea27-fa76-4e45-5b8d-08df1a6eff84 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|82310400026|1800799024|23010399003|376014|7416014|36860700016|3023799007|10067099003|11063799006|56012099006|6133799003|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: ++bSDMG+l+J5mZNcRlnOtIZcJuLKlqnTujyyj50BkPUtX7OMKG7+1C4+LzzX2SLh05kv27s+MGy4AB0lKzjhL+8SSSe5sHLwGo87BCZgbWp6TXjdBLxZdv1itXmeChuYNEDxdv1oCrdgjmunvTU8T+77egIZ1NLuOex/qzLZg6ng0qKUqP75Qp0Sv0GLZHpkQttT72UcAqQWiyvtvdzU8hNkxRfUQnaMhGvYBMq1lqvWcvRFWe/f0dAtGgbJLHuAYbuzvTHoTKGLdrpQOymxmNMVEVZFR1GA7AhAbWHRBpyhnm6IKQLFGD0f86ZZ+FUvi01WN/5LMHVj+tz5HnYM5L4q7pTvUUrEbFZSjl5NgITnb9wE35M+CfgC/MlYOZal4yp65qMYxvbt02Jvr9iYAdcwNcRNO/hdaYKy35CILHhF682E3ctiGfFOpJbzM2VkRG5jmYcnVN+iVh1IPsmgshTH+K3E8NA6vESw1TyFqqlczV9mwwbvUm490VaRlOm6yI2RLZutc2mvovSnyH70+DKG83ee7XuduDZd3Jyl4xLDYLmil8MO49rEguosfuFo4GPo1yLiMBO3+nb77RVEEqANgeWIPHpA+BvZHcQJRAUcFFDK7sGTxnGtPp8AWviyEah1b1CXZSjVtqPShU/tycLF6vfl3RKIU4ofG6r7wLEtunoDYH2HRbda6+q3U4Hx5PIQFXUKVOHDHYrrb4WXwQ== X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230040)(82310400026)(1800799024)(23010399003)(376014)(7416014)(36860700016)(3023799007)(10067099003)(11063799006)(56012099006)(6133799003)(18002099003)(22082099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TO22sgmPzTbICIdUhb+K+14mjoAYID6yJ564v2yfOl+ucNgRopGF9XqnYLOacIfNuL7TKfjlsZeCYfRhwfAG0oO6xX3J368+/pCeQlt6/yijn6kfEg4viYnQdiMHFJNWdCnbyXsphGUGwpGleKNunKRgtMWLrKOmKKWBOQZOjIscmV5zIt2ItZ6quA31V+ku61GtfewdGCnIaIgHwUOOSXQJs1RqHbxHccdn1cc6Gkm/7DSMImk0dYDjwfLhqCMKEKJe+qXmEp0u2AT/R0ejuijHI6GKf4y6WJP6RVf8o3Cgo1apYwHdviouMixGWHlnAc0blVhbJxdCHZkcb9foxKXHCANNijuPfW8pgWDLUCkFfg1noCismHKDqKeed0A+OMeVxgjG/yfqXCnBw50YdsII/OOvlkqTUsi8X5AkNDK1ZArrXA737oE2rXyHemSg X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Sep 2026 19:06:54.3675 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e9f7ea27-fa76-4e45-5b8d-08df1a6eff84 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN2PEPF000044A3.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB7543 Content-Type: text/plain; charset="utf-8" From: Peter Colberg Add methods to enable and disable the Single Root I/O Virtualization (SR-IOV) capability for a PCI device. The wrapped C methods take care of validating whether the device is a Physical Function (PF), whether SR-IOV is currently disabled (or enabled), and whether the number of requested VFs does not exceed the total number of supported VFs. Synchronously disable SR-IOV in the Rust PCI remove callback before unbinding the PF driver. This ensures that when a Virtual Function (VF) is bound to a driver, the corresponding Physical Function (PF) is bound to a driver, too, which is a prerequisite for exposing a safe Rust API that allows a VF driver to obtain the PF device for a VF device and subsequently access the private data of the PF driver. Suggested-by: Danilo Krummrich Signed-off-by: Peter Colberg Signed-off-by: Zhi Wang --- rust/kernel/pci.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 3ec897709e89..e6dac919f02d 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -133,6 +133,10 @@ extern "C" fn remove_callback(pdev: *mut bindings::pci= _dev) { // INVARIANT: `pdev` is valid for the duration of `remove_callback= ()`. let pdev =3D unsafe { &*pdev.cast::>>() }; =20 + // Keep PF data installed until all VF remove callbacks have compl= eted. + #[cfg(CONFIG_PCI_IOV)] + pdev.disable_sriov(); + // SAFETY: `remove_callback` is only ever called after a successfu= l call to // `probe_callback`, hence it's guaranteed that `Device::set_drvda= ta()` has been called // and stored a `Pin>>`. @@ -472,6 +476,38 @@ pub fn set_master(&self) { // SAFETY: `self.as_raw` is guaranteed to be a pointer to a valid = `struct pci_dev`. unsafe { bindings::pci_set_master(self.as_raw()) }; } + + /// Enable the Single Root I/O Virtualization (SR-IOV) capability for = this device, + /// where `nr_virtfn` is number of Virtual Functions (VF) to enable. + #[cfg(CONFIG_PCI_IOV)] + pub fn enable_sriov(&self, nr_virtfn: i32) -> Result { + // SAFETY: + // `self.as_raw` returns a valid pointer to a `struct pci_dev`. + // + // `pci_enable_sriov()` checks that the enable operation is valid: + // - the device is a Physical Function (PF), + // - SR-IOV is currently disabled, and + // - `nr_virtfn` does not exceed the total number of supported VFs. + // + // The Core device context inherits from the Bound device context, + // which guarantees that the PF device is bound to a driver. + to_result(unsafe { bindings::pci_enable_sriov(self.as_raw(), nr_vi= rtfn) }) + } + + /// Disable the Single Root I/O Virtualization (SR-IOV) capability for= this device. + #[cfg(CONFIG_PCI_IOV)] + pub fn disable_sriov(&self) { + // SAFETY: + // `self.as_raw` returns a valid pointer to a `struct pci_dev`. + // + // `pci_disable_sriov()` checks that the disable operation is vali= d: + // - the device is a Physical Function (PF), and + // - SR-IOV is currently enabled. + // + // The Core device context inherits from the Bound device context, + // which guarantees that the PF device is bound to a driver. + unsafe { bindings::pci_disable_sriov(self.as_raw()) }; + } } =20 // SAFETY: `pci::Device` is a transparent wrapper of `struct pci_dev`. --=20 2.53.0 From nobody Fri Sep 25 06:47:06 2026 Received: from CH5PR02CU005.outbound.protection.outlook.com (mail-northcentralusazon11012031.outbound.protection.outlook.com [40.107.200.31]) (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 B9D7034DB56; Thu, 24 Sep 2026 19:06:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.200.31 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276818; cv=fail; b=giuhL0gUVbXKq92NWxJx5HMIepsEXJ2c/YkgV0f0iQzz4m/EYgX+FhQMRPBiGsGfKf6TuCx9h7DZWkXYWNToGcNJfxyBf/i5MCQrpCuVh8JnLUicHMo1ELjelkBWwfaTxmwUAFUvUxUtV/lSLXxP3iwf9NSDaSMhyRJRJBUrUDA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276818; c=relaxed/simple; bh=967fZ5alGimzDyohJpCrReF+KAlbLzuwgtAUFdCiRPo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=QCO8TeWsrLpfqhSQXaHRy9i8iqs8pISU9A0GCS5D9WVxPBVNLXjVFq69YohX7jK8vkoBKhV7EuWimW1gkWC4KRl+Yl7LIoX+78H46BdGgxDu62+2bMIiQRe86GNzwFLrj5aGqbC/dTx+w4qOMT9nlJ2BWHBbT4GAXF6SFTh4HJY= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=fWx4Wz7i; arc=fail smtp.client-ip=40.107.200.31 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="fWx4Wz7i" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=nY7iXKt/soYhQpksvFxlwC7DY2JydHg+2gQAipUlkRw4uS5l167ZDC609Fyx249IMY+zYWM6xflwNA30u5TinfpZew5DBtHW+J+38uslcW7Rn2E/pcTNxqbkXIGpgv8lOJhJ4nvZ5zjbU5DsHO/PCviCpmgzWALMCt6Sd3GB2v8SrxiB04ln9sxA75g+VrrM0/+fk3NpB+KB+OVr0GZWhoouxibqyYLJUZ+0Ya7eFQY+ZZWaI32yW02J5iJCkCP8vhMDCDBPQamROyOEVNfqv2Gi9DUgmanRmIMcgPkQb36Kg5Ylc7Yz2ZLbCjwmAwB4lJvOU9W/tO2HkPoxido+/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=QmUN+LkMZgTbh4GfcnyTU8LPhmSr3EvIKaH37PBdKkk=; b=VwLxSRcOjdxrx8GADu24+jxTrs0u9kTWFGB5YpbcgVbYDbhratEPoL3BwR2Crjp1lLTAEqJzZCOqrdKFdMJAtkwQtDBLGIXCIP+IwLArqYHRlt1NhFnGeLYLLdrn5WQa15XUZCFMTeLt9xvUjWmew6df1olAIUkaTnGuGiTE3hcpO4m5uOqZYbtW37FkzCsy+fGh0Ros7k3Al/MmjyFfcgIqRbj5jRfyGG3uwn0AFBUJIqFkSQQ/LWoT3iJiNE2sdeVgwhEHOOQeAYI04rdMM7xbazVYayYPMf+PnPd35y9hP7zY8uOoL4aIrBuyjEgOzuMUtPaiR3ocQ6Fb/eZOhg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=QmUN+LkMZgTbh4GfcnyTU8LPhmSr3EvIKaH37PBdKkk=; b=fWx4Wz7ifektIKZ6Fe/Ohr8cZ4EvK2uHXI9YmEIiElYS1JJd3I5WsEl96CN7nTLYNAuhmoPx+v+LGdT9XbuwtBcsBUqj2W5ba1x3mq8Ft0dUcpO2oManLJ4kplkTMiojJAVxemN+vqQ++DVEd0HXoANAHack4GewXU+PbLhHwaRVvNOvUk/vIPcN9zDYmratYRIWGVnTEMOJJ+qj+abH1Badf8lLkSSdQ/V1yC8O7sSV+kQ///lzlCeNjvEc4BKi9v130Fqx/l0Jy76qxXUbaPy68yS224EBftSomYtOTqXq1oAmFV6IzfHmN7W9Q7jpqNqR6Q9HVFg/6wezS7IxCw== Received: from YT4PR01CA0109.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:d7::29) by MW5PR12MB5598.namprd12.prod.outlook.com (2603:10b6:303:193::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.18; Thu, 24 Sep 2026 19:06:49 +0000 Received: from BL6PEPF00020E60.namprd04.prod.outlook.com (2603:10b6:b01:d7:cafe::22) by YT4PR01CA0109.outlook.office365.com (2603:10b6:b01:d7::29) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.451.18 via Frontend Transport; Thu, 24 Sep 2026 19:06:48 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BL6PEPF00020E60.mail.protection.outlook.com (10.167.249.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.8 via Frontend Transport; Thu, 24 Sep 2026 19:06:48 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:06:30 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:06:30 -0700 Received: from inno-dell.home (10.127.8.10) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.2562.49 via Frontend Transport; Thu, 24 Sep 2026 12:06:20 -0700 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Peter Colberg , Zhi Wang Subject: [PATCH v2 2/8] rust: pci: add vtable attribute to pci::Driver trait Date: Thu, 24 Sep 2026 22:05:49 +0300 Message-ID: <20260924190556.1620886-3-zhiw@nvidia.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260924190556.1620886-1-zhiw@nvidia.com> References: <20260924190556.1620886-1-zhiw@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL6PEPF00020E60:EE_|MW5PR12MB5598:EE_ X-MS-Office365-Filtering-Correlation-Id: ab6d10f5-1f48-4ddd-3ae4-08df1a6efc04 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|1800799024|376014|23010399003|7416014|82310400026|10067099003|11063799006|56012099006|22082099003|18002099003; X-Microsoft-Antispam-Message-Info: g+XcgNJy91ZbsBmRf3lPquMrOBH3V9pfpsDsirVQjQ2g2w+/XW4TC2qxQyJ2yfeLAelGnL1NPvfNKxkVlM3MuJ5+A5XSIE9xQFqkU9f4KxLE5cWQxt0KYd9BuSPAR8vGKqPUZnpmAugbZrUqmvdh/SJyteq6OKNl20L/JnSsBTwodRzjhd1mIZHdL3yz/nxSva0Rim0mtMz64B6kC6z1jiFIn9zVNDt8aCkp0S+AwpelZ1taDQeKmZ8WDG3x6BM+yO3KXQtGSB58BN3+2yqiw4VzC8dVUJNbe3IFaH7K8/suThk+SIfzXiA/yrnd+hZxBkc4xvgwXSwCislLAmRhwrkvuc5+iKEkb7pHBsBFseq5tWabcB8mNM9K80UfDMIOsNuWFR87yfkLlYDIPo8UIODaeKosJ4TXEP2drZxRhE/PDbjcxNSykdIuFCG3AyApuFh6f92uINDFvlbHLZw4Qn9eL945einCp9QxzifWkdr6uzU5pSOZIX+n+U3YegaeNIWDuybiDbcuvnD4rdeyP48NQmzw2b1NDKX43KEW3RfaAkeFSj0IVZacRXdVFQX8aFD2sxV+MascJgnufiEB+BbVMpKBcn/XbAVUCcgUTRGxUZYT2oY0XmcxXO3AwivsZ4vyzE/iV4hLAiDDO95kMjJYOWqS74lLGfugM5nQRLaLDDOSC2HG0d/vmxEOhdCIFMe757Pt+xm7NQVQ87/7Og== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(36860700016)(1800799024)(376014)(23010399003)(7416014)(82310400026)(10067099003)(11063799006)(56012099006)(22082099003)(18002099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: UX5TqntBkPCCCFn39uSceFcZRSAQYkgzLCCcR9FkZcJbhDMwVgGFGqfdjsFm2Wzj7X8pyFaexUVJ4BEYgayqYr48IhPA77oFez+z22Ob/H3nJC5RI6LPj3fHW7l2AsLSM0qKQbTBMReSgPqg5l5B1LYf5+qYrd2dEE8Qmh05A3tewhHs3Xo4pIlrLYebqdGyG0pDqjXYjs043zT5IuIIFpICfLMrxvaC2GvPCc9pyV4We6vl6T/4wJbMXYq2xwXAcqBVo0geKfwCMT1jAbpFHInRAIHtPmH4DDIcnZzzqiZLDCnpnN6D60hSfQ3YweZW6kU+1h56o+2Q3DrlwloOtmZjNZnidLBAL+2yAK2UEIcUnfJfUh1uwwu+pN01BRqTN620dT2XHoUHHQPf4RfLg/KxQYQw+56t2CbuYjX/3yGdtY66A44LXoNpHajQK7rI X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Sep 2026 19:06:48.5486 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ab6d10f5-1f48-4ddd-3ae4-08df1a6efc04 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL6PEPF00020E60.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW5PR12MB5598 Content-Type: text/plain; charset="utf-8" From: Peter Colberg Add the #[vtable] attribute to pci::Driver trait and implementations, to prepare a subsequent patch that adds an optional bus callback sriov_configure() to enable or disable the SR-IOV capability. Suggested-by: Danilo Krummrich Signed-off-by: Peter Colberg Signed-off-by: Zhi Wang --- drivers/gpu/nova-core/driver.rs | 1 + rust/kernel/pci.rs | 2 ++ samples/rust/rust_dma.rs | 1 + samples/rust/rust_driver_auxiliary.rs | 1 + samples/rust/rust_driver_pci.rs | 1 + 5 files changed, 6 insertions(+) diff --git a/drivers/gpu/nova-core/driver.rs b/drivers/gpu/nova-core/driver= .rs index cf3534dd47d4..47a599c9ea4f 100644 --- a/drivers/gpu/nova-core/driver.rs +++ b/drivers/gpu/nova-core/driver.rs @@ -63,6 +63,7 @@ pub(crate) struct NovaCore<'bound> { ] ); =20 +#[vtable] impl pci::Driver for NovaCoreDriver { type IdInfo =3D (); type Data<'bound> =3D NovaCore<'bound>; diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index e6dac919f02d..622cf91fe5e3 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -276,6 +276,7 @@ macro_rules! pci_device_table { /// ] /// ); /// +/// #[vtable] /// impl pci::Driver for MyDriver { /// type IdInfo =3D (); /// type Data<'bound> =3D Self; @@ -291,6 +292,7 @@ macro_rules! pci_device_table { ///``` /// Drivers must implement this trait in order to get a PCI driver registe= red. Please refer to the /// `Adapter` documentation for an example. +#[vtable] pub trait Driver { /// The type holding information about each device id supported by the= driver. // TODO: Use `associated_type_defaults` once stabilized: diff --git a/samples/rust/rust_dma.rs b/samples/rust/rust_dma.rs index ffb693544673..804d55a63005 100644 --- a/samples/rust/rust_dma.rs +++ b/samples/rust/rust_dma.rs @@ -69,6 +69,7 @@ unsafe impl kernel::transmute::FromBytes for MyStruct {} [(pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5), ())] ); =20 +#[vtable] impl pci::Driver for DmaSampleDriver { type IdInfo =3D (); type Data<'bound> =3D DmaSampleData<'bound>; diff --git a/samples/rust/rust_driver_auxiliary.rs b/samples/rust/rust_driv= er_auxiliary.rs index 0bee16faecc6..6cff9bf8c80c 100644 --- a/samples/rust/rust_driver_auxiliary.rs +++ b/samples/rust/rust_driver_auxiliary.rs @@ -90,6 +90,7 @@ struct ParentData<'bound> { [(pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5), ())] ); =20 +#[vtable] impl pci::Driver for ParentDriver { type IdInfo =3D (); type Data<'bound> =3D ParentData<'bound>; diff --git a/samples/rust/rust_driver_pci.rs b/samples/rust/rust_driver_pci= .rs index 13b035a95756..50c8709739c6 100644 --- a/samples/rust/rust_driver_pci.rs +++ b/samples/rust/rust_driver_pci.rs @@ -139,6 +139,7 @@ fn config_space(pdev: &pci::Device) { } } =20 +#[vtable] impl pci::Driver for SampleDriver { type IdInfo =3D TestIndex; type Data<'bound> =3D SampleDriverData<'bound>; --=20 2.53.0 From nobody Fri Sep 25 06:47:06 2026 Received: from MW6PR02CU001.outbound.protection.outlook.com (mail-westus2azon11012049.outbound.protection.outlook.com [52.101.48.49]) (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 1D530382380; Thu, 24 Sep 2026 19:07:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.48.49 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276836; cv=fail; b=elcYlhLfBPTooMr1MCEhfhm9NN+K2+dx5sVByDO4Pgn6QWbJfdSZjjibmfDzRFF54rs7Bee9aGm1BxM1Ota1sA7RHBL/FWD7mSRw+Cg5SqTkyWknLyObM9FPq4ZLWkn0Pd5lWqxJdP4V/eZD23x8yXqOexck3B9I0Afvn3t/H04= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276836; c=relaxed/simple; bh=xwm+9/Cuk4VzmU96SYp5yyEq/3PKjzHk/+BAqgdzPGo=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=li35r1aaD81nvaLC1nk6+z0Z7QU5KuG64zG4QeX0GtMle++mA9ht/Q0ba4w/fwUdTQn3BL5u6rMFUcKs4O5/V5M+NKrq6rjg/nMjgPggK2+f/kH0ImPmTq9XT1ds1wK1J2OD7EF9fuG1PNOgaueRqMBhX61jjPhiMqHnwvHA608= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=bJlihB0m; arc=fail smtp.client-ip=52.101.48.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="bJlihB0m" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=xgkyfgPAFeApZjQOdW+NExEgYX/R0APomKVT7VNRCSQ9D9gSL+j5P1LEajC7UQecJEMvfUCSoNyKI/rK1rUe7avSs1Y3HM99IXizvQy4TJAaq4pWuFYYGMpv1iYePbNfsT88vT1XbMGE5RQ97OsPSE04VNC+j0TE6rSruxC6SF1h4/cMqB1lYrfgqLFph5DsT9ugLTerNwDe//NTaS6hxYI6ZVpnQY0L9q/HkZVyOdW5pd0c+cQKywR2vUwR62ywm8lx/j0HZVUkv1aGMKt1rJyp+stWk9K+sWRjsTEZKaK12kS4VXppZss2nLuUcJkMslVUjnJveCey0L1yRHjQqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=pRCFtJKoF0s+7zF80Gjuk28/XggOa3OmzW9/aOQ0a5g=; b=oxoObQl0xFcqMSNzQFK0nEz+Zfg6ZsSfy5Usc+k48oapH0OZZ+drEl3duB/8+ImIbeKwYTb/FyxXsI1VTeFOfxpuCffeVSWOdtWGL8M2hDU6ManAk5IjYzO9G/n1CPDNUg6J5+CTmsE3uCtxPPT8KBt3uhxcwSJp+yWxZqzT7sqlnMUqUYT7xOHIniZiwTzSauIMd3OgVTsSlYulIgDq+yo53f4KFco7Uhwx50wkiO+407Geaap+90qlfVMIGwQU62GWJPDq1pFQYnUbpTbU6BTdmD99i3K5VcFilHz7oDdEuJ/rXlqR1ZImX3ngiWAm4Vwcq7Dc+Lanqutvvrhb8A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=pRCFtJKoF0s+7zF80Gjuk28/XggOa3OmzW9/aOQ0a5g=; b=bJlihB0mNlKecBhmfVRmjzu/9o7JXVWWSEgX8K1EQJHaJ7VLchMkSzAuFzdVjvP25bKkli1eGsxh3+I9+Y9Wjku1Y0hm32+R7eVyLQdp84HleU3F5deCko9lPwEmS3s9+jcxPWNn4iYNVTxenIXcxh0BhsUR+ZdSAKqwXYdBmkkbnwEcSjHOd0EFyVRIipCwoptux70i8Fzm/iEOdTKRHyIySriHz1TV89vSxlC2HsSYve+1QCZEmfpNcIXoBpl9G4QJiwS7WxXsT2NZ4NYMxqgXqAfVacZ/ENsskfz+YOGJKMZlmQvwcOc5ojynngp6kThZSHAGNR9eutnBDft/sQ== Received: from BL1PR13CA0125.namprd13.prod.outlook.com (2603:10b6:208:2bb::10) by MW6PR12MB8706.namprd12.prod.outlook.com (2603:10b6:303:249::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.18; Thu, 24 Sep 2026 19:07:11 +0000 Received: from BN2PEPF0000449D.namprd02.prod.outlook.com (2603:10b6:208:2bb:cafe::39) by BL1PR13CA0125.outlook.office365.com (2603:10b6:208:2bb::10) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.451.7 via Frontend Transport; Thu, 24 Sep 2026 19:07:09 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BN2PEPF0000449D.mail.protection.outlook.com (10.167.243.148) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.8 via Frontend Transport; Thu, 24 Sep 2026 19:07:08 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:06:42 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:06:41 -0700 Received: from inno-dell.home (10.127.8.10) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.2562.49 via Frontend Transport; Thu, 24 Sep 2026 12:06:30 -0700 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Alistair Popple , Joel Fernandes , Peter Colberg , Zhi Wang Subject: [PATCH v2 3/8] rust: pci: add is_virtfn(), to check for VFs Date: Thu, 24 Sep 2026 22:05:50 +0300 Message-ID: <20260924190556.1620886-4-zhiw@nvidia.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260924190556.1620886-1-zhiw@nvidia.com> References: <20260924190556.1620886-1-zhiw@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN2PEPF0000449D:EE_|MW6PR12MB8706:EE_ X-MS-Office365-Filtering-Correlation-Id: 3f28c723-e715-4256-534b-08df1a6f083a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|23010399003|82310400026|376014|7416014|36860700016|56012099006|11063799006|6133799003|10067099003|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: j+L559AOaJwuY7JMrw9pyxxp43536M9Es/GYhFcOIZrT5BGIWAuSaYyl2c3CyobGTSkWBQzfdKZPhZ3TTgdF5qzRGyMqtOkHoeqg0WXI5LnzdmNiLC5QOxAbQebkrw07SJnes5GzFNE4vSTW+salD52jEUlWe5tQb5lJ1lmEn51beu36ZNLFy5UvI5EzR1dLsCNzE3DfRw3s85uKNHFeGqmO+wPHi+IW18XLlhq9H37hh50/0aGncx2ckY7QSj47M2tunLWTWrm0N6SWnKcV+WlATIysrE9fum54LZyWYAcHwLSmjslm+a80DxEY6ZwsrvfzDkrvHoH5Bax/2zFRZTFuIy5FYcv8dyweSlSZXTN4AFUx95bReK846eK0Pj+DVeEBgx7vzT5BuH1dlRfUipKW9ItdTenS6mQgs6lbRs4q1at9wRs7wf0WGEJn5Z7VoF/jqD9uH074XzdqROAa5pTE3SfS9Wt+imiQxBKFeB4jgIH7G3qIsBXW0ag9NYYor6hIdcJqkr2kH25boGwhg/uH4qjBumguBOI3vz4LJOhIOesXstX4QAgYixGgwBwyowcxQGSodxZ9YNMeSJbMQbB5UYEPj7ekorkUfy5BkyIt2GHFt1EnaZZbyBbhI5oF0kL+MqtzmDl1TI/C4zTZPdNIKaYVZZg7EMj2onfWADMRBwtd+aDPuGhgi3jg0cS2vpNKzSVxbRt7txlHdNPwMg== X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230040)(1800799024)(23010399003)(82310400026)(376014)(7416014)(36860700016)(56012099006)(11063799006)(6133799003)(10067099003)(18002099003)(22082099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: K2JAcU6gHHtz9lBvEsCrIOrS1PWMx+faHakQjOjaCNmTa5yXOdDSHWQiKRasZiiIA03vrj39YhMnYRnot/ONpJzMdX4JMLOhDIzsf1JvIS0MXAdBONWJRtJ99TePvxxoVHKHV2GIAkSyszQLo1q1LbvizyLibgfY9NpgblF1ggBnYQab9kCNEgkz6byZljqb4EiHrGkItO55u2gsXRKxbqHVXwEcGZwdQlond66/c1rGstF6iF0VMN+q0YOpl88js+SjGxVrz3YB5P5EbrE2wxPTIi3MlZwe3ixvymjU/2W9KeKgcwQhDOsFRsiOFASQXDifWesLR+yv7mhJxdC9WpSnl/Ck5k+O/H2P2Hs7jDAa/h4hUWNrVmojEqy8PdM9vV50KQus2zI2MlKbBINVfWsxzSNkTOXsYvANYbiA/7Xth51ZXI23RyZh/C6zFJXF X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Sep 2026 19:07:08.9771 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 3f28c723-e715-4256-534b-08df1a6f083a X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN2PEPF0000449D.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW6PR12MB8706 Content-Type: text/plain; charset="utf-8" From: John Hubbard Add a method to check if a PCI device is a Virtual Function (VF) created through Single Root I/O Virtualization (SR-IOV). Signed-off-by: John Hubbard Reviewed-by: Alistair Popple Reviewed-by: Joel Fernandes Signed-off-by: Peter Colberg Signed-off-by: Zhi Wang --- rust/kernel/pci.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 622cf91fe5e3..b04f6aff6c34 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -433,6 +433,14 @@ pub fn resource_start(&self, bar: u32) -> Result { Ok(unsafe { bindings::pci_resource_start(self.as_raw(), bar.try_in= to()?) }) } =20 + /// Returns `true` if this device is a Virtual Function (VF). + #[inline] + #[expect(dead_code)] + pub(crate) fn is_virtfn(&self) -> bool { + // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. + unsafe { (*self.as_raw()).is_virtfn() !=3D 0 } + } + /// Returns the size of the given PCI BAR resource. pub fn resource_len(&self, bar: u32) -> Result { if !Bar::index_is_valid(bar) { --=20 2.53.0 From nobody Fri Sep 25 06:47:06 2026 Received: from PH0PR06CU001.outbound.protection.outlook.com (mail-westus3azon11011044.outbound.protection.outlook.com [40.107.208.44]) (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 7DB7D34DB56; Thu, 24 Sep 2026 19:07:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.208.44 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276851; cv=fail; b=NaCawiJGDrBMJKsuPFbIj+KGW329d9bGPmyUNUR62X2H2aRu5XzihKMvJwdcYyBRGt8SfoxsE8YX9SxUua42iS3vOrplx4Nl+aL00JJ60/5ZfwXw2T7KONeZZA8AB4zb/mX3rg9f6n304X7J1HRkYN1QZ/MzN3Okb/Oh4GMAo9w= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276851; c=relaxed/simple; bh=Q5wSfVryd8HuQWa25zGJl8Uo+ebV4iXvLs6TwF9v0SQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=MTNiCRzU6hLEPAnW0cOaK8dktGCnOwpXUKDzPZRzEhHKGcimpXiFi66BSKwWcVq0nntOnuG1wGsyvvNPwD12ubfAQ/Nw80ayIXbI34oV/653QKqejvC2Z62eNwR4L4nrMrll+caaLpc7J3Xy/kDgQ66Jp3lD3vZGJTWFKuTcyOs= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=jxScxVDb; arc=fail smtp.client-ip=40.107.208.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="jxScxVDb" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=TR00bXKPfXS+MCjvC6qgUtMMsAHFZnWik8IWl9N0iJwLQX2inddldvpPSiZiK038UfV591jEQrAq2TmqebCeB+vKCbcZmfLtnmIbGqnnWDZmrTqfXyJvRHOVmXgzseut7G8aXacfw39mJlvNwhJ0JghvxCJ3GaESmuws2AnqaIelH1JUTHthf5cEdPLX2aUXDG+/Hae5QypCUBoquizSgvTliTd5XARf4T81ZNCm+esgBIb8dBTzEazDRRvIyemiVIZEyhszFKCrk/9XCbj4kDJjENysi20o51zLC+pxQb3JKAxZ0TyNGNVLVn06tjIuqUZkaQDhqZutFsHTUTtxTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Y0lzOpd2G06u9CQRr/UyTRKpzAJvPOeEklzmW1CCYIU=; b=Tz8x5rS3Zc+G+l66t8f7dSpZi44GOVgge4g4cIAk5b7r8zAxtOyF6pT+HFEt0TqJtZJM/8jbvMVIa086dX7rYSpTPMNeOiz5/IWGXEREoCSYKwm9KVOxjm1p0BG12dzdz1Lyq8nlvDZTHv15Xu/i3SguIItbtCOMW4NWX/SuMs+sQz/yoqULWGMj4CiTEaqlmYUhYJWSd6FPsOHhltsNG9gLJiS9bQ0nb/U/1ll1/bYuuD5vI3HCCCEU939bcngSHDay4+m9ecIQOUbnjGOpvEVX4U5I/qkPCUOuBYNLBZBNwEC5aaTQ39Q6myrQT4qLWKn/4qfQMN4BX732zm6OyQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Y0lzOpd2G06u9CQRr/UyTRKpzAJvPOeEklzmW1CCYIU=; b=jxScxVDbzE8TdpxmFGN1L5opkcSFEXhxNkgdhAtqUKeQrYokE1tfZx+FAe5GNrPD8T4Yeq/rsocpSOFXXodD04Fjq890L91oFOagT11ncYFlVL04peI1K8jm5ZPp9A+6g3FELYRmYfyTWFKAII0TXi701IXzOQWBhGazjXWZAl1occD20xNz5Z0TTk872WKF/JhIsAocQqmJscUZqvxR6N4xnkECabxNJYMEmNvH0SMGnbJW6zo/eBzM0RyfAhGhdhJJud7IoGBepws/VuKVVG4FR4wz+XstVIkxlPsZbne1RZi5yb9Dma6vSoO+NeOXrFIxWb1kokoxjYLGRfRFlQ== Received: from SA1P222CA0182.NAMP222.PROD.OUTLOOK.COM (2603:10b6:806:3c4::20) by IA0PR12MB8896.namprd12.prod.outlook.com (2603:10b6:208:493::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.18; Thu, 24 Sep 2026 19:07:23 +0000 Received: from BN2PEPF000044A4.namprd02.prod.outlook.com (2603:10b6:806:3c4:cafe::a6) by SA1P222CA0182.outlook.office365.com (2603:10b6:806:3c4::20) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.451.18 via Frontend Transport; Thu, 24 Sep 2026 19:07:23 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BN2PEPF000044A4.mail.protection.outlook.com (10.167.243.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.8 via Frontend Transport; Thu, 24 Sep 2026 19:07:22 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:06:52 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:06:52 -0700 Received: from inno-dell.home (10.127.8.10) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.2562.49 via Frontend Transport; Thu, 24 Sep 2026 12:06:42 -0700 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Peter Colberg , Joel Fernandes , Zhi Wang Subject: [PATCH v2 4/8] rust: pci: add is_physfn(), to check for PFs Date: Thu, 24 Sep 2026 22:05:51 +0300 Message-ID: <20260924190556.1620886-5-zhiw@nvidia.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260924190556.1620886-1-zhiw@nvidia.com> References: <20260924190556.1620886-1-zhiw@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN2PEPF000044A4:EE_|IA0PR12MB8896:EE_ X-MS-Office365-Filtering-Correlation-Id: 4570c9d2-9c9f-4516-5da7-08df1a6f1071 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|36860700016|82310400026|7416014|1800799024|23010399003|10067099003|11063799006|56012099006|6133799003|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: O0IT3JVkPSLnGvd3Hnrw5jdJn0TcKJDLALK7xolOa56zwWYCGc7hdvRAlOt/fKOyfzds9H5fNA2nUykJzsa8av/9Ej+GOW0k06cjf0S5i11t62WDIhDs3S0ahWgZEHfKc8wpJm0pSem8URZsAV+zuqJp/csIC+JjQ8+pTbX3i9EyIvDLYHEMtW/vIAG4fna6yb3bCn0Lck3iezxoiAXGwmNBc+UhhLwO2mU7QYX7sOI9FQGFRG1RZ0xGeSpu4GKi41A2Tvd+AWRwHbfYPZrTbtAQfKzeeWo9BsIBc+Ve0315/4AsRSzTj5qLpY/U/FqZPcIkt3T12ZxID1uwReOc+MMjFGyXpR44ne03wtYE8jhdY5ehLs7v34NgGpxG9EdgXekt7yYP9PKI98xrNyfb/9HfpcllbQZkX0dEHkX7Y7od6L8BDpA4x9MepHsfRGOtUSoJ7XQW45qCihksdEH8le/Fty/VnzqiNz14bnwCD8+Wm2NHAoK/nHIm3ankq1bjh/CeuVmKeRwIykYNVbrvlwq6yrE2DYWvRL0fVmbxiUlc4N2be7PggLEkjtfRavOJYyCYi+kvHh3EATWuHqTI3QyNVuQ7zEZ+MzrbJYY3rKUqQ1ZqDHwriLLtJaGfRsgsLWd53zAeCKNxa7MijkhXag8qP1PgJ9BpXSinyGQXy3iWPcKN4MzBtpWEVwYaXjoeODdN3E0/E30+eUbV/FpigA== X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230040)(376014)(36860700016)(82310400026)(7416014)(1800799024)(23010399003)(10067099003)(11063799006)(56012099006)(6133799003)(18002099003)(22082099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: DWaOtMmHBPtuC5dmXcTKaAI5muCInDFUnhz0dGGjcQvETA+2qdJSEJ0oQCPAxbxj5zpn16MntSkNNZSSI016QGAycwf8E+eiTNkX5910nkjiMif9Rpe+8aAXEpvtK7S5sq6IePOuCTLBFrd9t0HUcNoYdotyyRHxaHq6c0PYhXCT4E10nYiwlLNGORHKW+kUH0kY8aUzOf475XTsm1X234IZrUuAl1hf5w64Lpr/Lj1mzfNSV7N4zHm8DkLMOffFBlc/OkoFGWZOhb1os3bNrAfVrLa37Ymrx0z/6NYjEHyOGfuxo7kxKJ3vcxXWU8RqbwpiVuREunxWSr18TcQb/G3UuHT5VR1bMsUefMvTJe8zQ5KFuxWQVk+oMZIvFWMOS+Al7N7K5tYCDr3HKdW75WPiNVrQ/CAsJalEuEFVcOrXm4khR6reztMmZVFxF5OL X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Sep 2026 19:07:22.7662 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4570c9d2-9c9f-4516-5da7-08df1a6f1071 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN2PEPF000044A4.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8896 Content-Type: text/plain; charset="utf-8" From: Peter Colberg Add a method to check if a PCI device is a Physical Function (PF). zhiw: Keep this role check private to the PCI SR-IOV implementation instead of extending the public `Device` API. Add a dead-code expectation because this standalone API commit has no in-tree caller. Reviewed-by: Joel Fernandes Signed-off-by: Peter Colberg Signed-off-by: Zhi Wang --- rust/kernel/pci.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index b04f6aff6c34..ca009775bd61 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -433,6 +433,14 @@ pub fn resource_start(&self, bar: u32) -> Result { Ok(unsafe { bindings::pci_resource_start(self.as_raw(), bar.try_in= to()?) }) } =20 + /// Returns `true` if this device is a Physical Function (PF). + #[inline] + #[expect(dead_code)] + pub(crate) fn is_physfn(&self) -> bool { + // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. + unsafe { (*self.as_raw()).is_physfn() !=3D 0 } + } + /// Returns `true` if this device is a Virtual Function (VF). #[inline] #[expect(dead_code)] --=20 2.53.0 From nobody Fri Sep 25 06:47:06 2026 Received: from MW6PR02CU001.outbound.protection.outlook.com (mail-westus2azon11012043.outbound.protection.outlook.com [52.101.48.43]) (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 C8AD44B0CAD; Thu, 24 Sep 2026 19:07:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.48.43 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276859; cv=fail; b=Qp+dxT8NB8EZNdqaF92saaAuvn3iMrBE3pxuZEbyM0a1zfLM0iywTNzdA1RrA3kZVeSIosBTrkfo2LgJ9uYZPkAzxBElYR+4grHc/wkMp5KVSg1zsabpvhWxtINJh+/yXkw0cqHO2eJqlr//yU2sB22JSNQZhgcy5yNR6hls4hQ= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276859; c=relaxed/simple; bh=pyZXgEuijsareWbZI/CH3LHEdGlYVJWrJ79CxeSOPR4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Xn2ZWAfKwKbCQEmdr9tA+i0y7HHmA/vX8uwdUwuivNiJJbTUvnIudiqqIjYZPzIgKLtLChaHLQW2PNG24XkyF0sZ7qdwHhFJC8KeShU4Poblo/eTRAumnKIfPaRzTX7nAI42J9v/PXhkQw7KDsvXFpNLDMi8d6Lx2wFAJDAbkwk= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=jpCbTFLf; arc=fail smtp.client-ip=52.101.48.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="jpCbTFLf" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Sr9REjZa4o7xrWr2UG6mke9fyIMSylWhCc9BKJtyxqvGdERYqdBnFLHtQ3+bIpgeCCvYKuHriVG+U7YyeOJ1alhLug8BFHk7fcRkLoMLl8xduaQQ1eFObr7Vw9CA6WWmLqWkLOVb0wd5412DRRXm51aWayHV+XfWUtSQym9VO9fuasdUzk8jk6uvOBmqlhGdTwcYPsXubYC0Rc2tHfzpis2cVernyQbBWD2dhS9YvIjfJMD6/BLm+3DJvJy8acHPjkxH+6WLdXf72ZQbXO2j/Gs4pIfPI+oJ5Je5oyUa6w+yiCgHg5TpKb8CL1Q/CoC2Mhfkzv3BHahpqEtNuf6BqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=hVN0IoAghKBaeJtRY3Cz7QPdZUxSy/hdNR8e1Iy2kPg=; b=K+ZBNvyUZV3pBUwAOBtmZdjb06K+LMK1tE7OSVmf72KtLREH6cZhJpySUDBS3+GPnEg9TTnl/uvtnE2hf1Wi92iq+TnseV+r56kiksZ0r/QjuzCSuuMRkm1qC88sPlMWwAUCK2Bd0e2cs0+7Ton0hkwF8zJGqPdoVvOGNaZU5tbpZ9TNrgLo6HYB13lp221wHmmQ3hWuB7y0+cDYQD/l2ShHqC7TgW2HvLgB9Lgd4ssYKxkdV7MGMh9Zgw2xwLzLwUsVP5ZdxKEVc7nP+Qj5tkB7z//OSV8v0+GW3TsR23lerWG+aKnU4YeRp/5Wff8+4zmyYD5PGrnZTi2CED/09A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=hVN0IoAghKBaeJtRY3Cz7QPdZUxSy/hdNR8e1Iy2kPg=; b=jpCbTFLfVUtdV85va3dSmHXj+dtYxVJyKkTjDRbhdMlV+hd53UPE7wa4G1WAnV36w54os5EzuLZ7OnlWVj9mzj7q5QdHirnGBTysXr0bJafYj1wnhGcHWfRfHvl+tryYkqtIdhhhoUGJwSny1FzhL8UnbD/PejrP1ASi5tqE2r3IQl7vKeu942thzhCylxHEzPsWIuc3RhlC5JzwxbeBANob1iYh1oIFxIg2k7g1R3nU0i5cM12prrxWBRd+aoSu7eGHqGI/EGK447sl5ft2BJflfEfJ0m9mQcHCNPy2oO00V7O6Exm0DawWbVz9yoJR3NoLYuAFQQfFplWvmFwizw== Received: from YT4PR01CA0468.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:d6::10) by SJ2PR12MB8882.namprd12.prod.outlook.com (2603:10b6:a03:537::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.18; Thu, 24 Sep 2026 19:07:29 +0000 Received: from BN2PEPF000044A3.namprd02.prod.outlook.com (2603:10b6:b01:d6:cafe::93) by YT4PR01CA0468.outlook.office365.com (2603:10b6:b01:d6::10) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.451.18 via Frontend Transport; Thu, 24 Sep 2026 19:07:28 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BN2PEPF000044A3.mail.protection.outlook.com (10.167.243.154) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.8 via Frontend Transport; Thu, 24 Sep 2026 19:07:27 +0000 Received: from rnnvmail204.nvidia.com (10.129.68.6) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:07:02 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail204.nvidia.com (10.129.68.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:07:01 -0700 Received: from inno-dell.home (10.127.8.10) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.2562.49 via Frontend Transport; Thu, 24 Sep 2026 12:06:52 -0700 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Peter Colberg , Zhi Wang Subject: [PATCH v2 5/8] rust: pci: add num_vf(), to return number of VFs Date: Thu, 24 Sep 2026 22:05:52 +0300 Message-ID: <20260924190556.1620886-6-zhiw@nvidia.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260924190556.1620886-1-zhiw@nvidia.com> References: <20260924190556.1620886-1-zhiw@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN2PEPF000044A3:EE_|SJ2PR12MB8882:EE_ X-MS-Office365-Filtering-Correlation-Id: cc8e0614-4abc-41cd-5597-08df1a6f1380 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|36860700016|82310400026|23010399003|376014|7416014|1800799024|10067099003|11063799006|6133799003|22082099003|18002099003|56012099006; X-Microsoft-Antispam-Message-Info: QIJGsvkA7QMrmePI65KYi78ktZTOzHPi5H7+NpfVsxUuJWNRLmDDi8BMOiGzAkghH5QIELQlHWOa13oPIzzXCI8P3+1hUVawpBj0yIbdFsSI2hs7+vqOhNqPCy6JjSZXvGWEBv1FdGmMFhQYbuWt8e6mTt0KE3ybpH4+VI6Dj/dzqc23Dgxcxh5nOKYdk6p96f7bEyWQWeyS9d9jrDGKYXZqOTzvuDPLqrUQ4jbYhig/2voZRE+0ZJT1yi4GkMftLFlxAuDdqJPrDK6uHFvobwWl0kO0pGDGyRjitcN0F41pXukf4SvmPdh7qk7NqfND6QMWEU+PvoU3eCodHDjV2jjk28DqsvshztthlH7tYAMbvYcNuSPVAsv4At8xap0JmKWydr17kw4ePTvvV+OxusFx4wj09X2p4t2+04AGrGPAd7T49Krk8NfWLe9jtLrQzXOnrMhOZEBqHrWky6pU07fxGSaeklwJA/r2HHRwNk5SF0CYoyKoDDVdaDCgsbVtbvJTqHcYlI0zqlbyoTELQ5Wf3eFG61RCeG312oja9/VbVQgMw8AOGdj4tIFTAtyNMnV1rPbs/6hRPTt6jCSGwqfi/VHtbGiUsHj1KdLEZS+s/wT8I4ENrbjcRVoJYq1OgzuUnh4ux/Dawuyy/xRKOMaEdwWR96+KJR4vgZ/VKKb+jz6uCvTEdhSNeGf1oAMf64TlWJp8z4OoV38MzKl5iQ== X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230040)(36860700016)(82310400026)(23010399003)(376014)(7416014)(1800799024)(10067099003)(11063799006)(6133799003)(22082099003)(18002099003)(56012099006);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: kfFL/q+67Fu0zZH+CrIMZRa/A2a3lld/6QUmoa92EEzOyGtkZpAgW3xwPG+jdoCETQv0LB7SRlee+/pepXgKBfyhhgCqhpXKAM3BTda1tYtGhORJQacQFhcuC4U7V3pxfN+EfELl5vW+bo8wh41lwodcx6MP37ia/L1/57xqsrFkbv29rCv/QSncWQ2ZPly7PROhtcdHstqAKyO6qua0Tcqc0z+DM3oh/ee/WbHIkq05xPq329IZ7aEUPvsQ6Xe7PaUC9jyoL1yj068muKXGuRRpEpbm/CpRSpDFqID4sJ0KNpHAejzk0Trasdvk6JEi8IBsnhxE9mw44xV5C37cyPq0X5S3PIUv5iHugvfESe7DjZTzqkpUQjnXcygzYal6iqn6FvnUwloVGdX24MIcq/ufW85g0Fo5ZCLHmia1KBeQ9XpVOze/3lY4BeqD5jTe X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Sep 2026 19:07:27.8872 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cc8e0614-4abc-41cd-5597-08df1a6f1380 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN2PEPF000044A3.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ2PR12MB8882 Content-Type: text/plain; charset="utf-8" From: Peter Colberg Add a method to return the number of Virtual Functions (VF) enabled for a Physical Function (PF). Signed-off-by: Peter Colberg Signed-off-by: Zhi Wang --- rust/kernel/pci.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index ca009775bd61..db0554f14afc 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -449,6 +449,14 @@ pub(crate) fn is_virtfn(&self) -> bool { unsafe { (*self.as_raw()).is_virtfn() !=3D 0 } } =20 + /// Returns the number of Virtual Functions (VF) enabled for a Physica= l Function (PF). + #[cfg(CONFIG_PCI_IOV)] + #[expect(dead_code)] + pub(crate) fn num_vf(&self) -> i32 { + // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. + unsafe { bindings::pci_num_vf(self.as_raw()) } + } + /// Returns the size of the given PCI BAR resource. pub fn resource_len(&self, bar: u32) -> Result { if !Bar::index_is_valid(bar) { --=20 2.53.0 From nobody Fri Sep 25 06:47:06 2026 Received: from DM5PR21CU001.outbound.protection.outlook.com (mail-centralusazon11011050.outbound.protection.outlook.com [52.101.62.50]) (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 B59A43F9F50; Thu, 24 Sep 2026 19:07:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.62.50 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276870; cv=fail; b=oN6I6Rvu+JDYBwm5z1LpzKm8Od59DIXgo++ybPSQvtr2JqjSoQp01FgTVdPTUx2vPr0GQ+fWsD6zN6yw0TSBm0oG92MAGVVPWKwu8+QmgnqpMxrgH8lYugvhHwMFFlnQnSRGzmtKSFWlvLYwfTHnQGYWxH1640zXlf5Vrp5VO0o= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276870; c=relaxed/simple; bh=f+ZHcGAe3RPREUx4I86MnJT9JN9iBSYp+SMbF8TPrhs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iEhQr2JtT4mJXZRoYf1waSyh5i9bDv2cMWEJXzyRwvGBiuhIAyz26vectmaR2QY2vJz5rzTwPsX69N5qYLChQDmxz9UISeEbhvCFuY2UuIBk5E0VGiWvZ8twWEOnMZC1Rc6jmFvgkX2qxULxtlN7BST98u7D8Hffta92DUxYU7I= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=JHKjKwQF; arc=fail smtp.client-ip=52.101.62.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="JHKjKwQF" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=x6pKeYxdN01ql0ETYI5y8eqaeOHJgVPVsAqBgLTM92EJ0e/CBPfG/wcIP2+QS0X33fH1mpZKOS2clSNMzNfUGqbQ2RHs9h1beePThHduEwMilaC6yRI8N/19r4vWApaP/4Y5xrclq9918FAK6ha9IgPjPip1xRWC+MmRbL+j6lpKECuMtHLUsE32ZGlR0iOvAQAieFCIPd32YafvO2R75V5vqf8rGHxrj6PJxvNtxkOu1p8ZLYGU/fNleeNPkKwHe9mTYlxAKuR5D5HMQPgRHHrRIb9WZ/60Bm59kInsX+b10NzpC3M9jZ01HpgBvH5bKMYq5udbas/3ljy8NDJuhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=PEmOlZloYa0Suoqd/BzIhj/o30FzOMwZu17xGA2Hs3o=; b=ywLc/CNFOZWzNuce/b2rGwCjKSHrt4lXl+oADn3Dz3sXvdcOBGdS/8KmthULfS4b/a0TobSi+rsQJ/mgMQh/7tBpWe5LU6UmOsTxZVUNsWcrDCjPMwIVTWTvPUUTMTpmNZKt4fsXggFszYfqhu0OBi8zr88Dp8HqoFq+ndRqAYmC+o05yf6+AReBMwVL4q/5nSDqUQXX9asA5Z333Y5aEPPIU7oYN2buNQ4yqbl0PEYzJLbfh7SR+FqPV9o3QSVOZUtc7tSqcwcVffFIcL07T4mMf7nns2FIxXIp9bRWjr8u4qKKKx8+bHTrgYYJ2D/DExdfSnJgX5xgXHRj+e6Pmw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=PEmOlZloYa0Suoqd/BzIhj/o30FzOMwZu17xGA2Hs3o=; b=JHKjKwQFAF80WYo9wUdfRcOLMJTqZzFWSCeqZV4sCp+Q9tpg+ACTNVFtqL4BK8y6jsR9ycwaXyaFjMSRrUaekgo1fNkzY+steZeAh6SqDnpDwPM5/TqPnXvZzQNIVVUIqnXYx4EwGeeEyqBWr/OriwVCKaajYBMA0QwXGHsr3d7BBLmPMwaet8fPf08EqNwpOEChxYqIbttekffReJUgC5CVI4p22WqlwbDeMAhF+qO224I7QfNx731wsHDvZic+PD9IFCUpd/5O+n+bzX3lW+NPkUlc/ZnSaPYSNe3KF8SqZ7R1I6LLXLLK4y9qzugI1MRga0qu4jQT2mqUX+JqHw== Received: from YT3PR01CA0099.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:85::19) by MN2PR12MB4253.namprd12.prod.outlook.com (2603:10b6:208:1de::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.18; Thu, 24 Sep 2026 19:07:36 +0000 Received: from BL6PEPF00020E63.namprd04.prod.outlook.com (2603:10b6:b01:85:cafe::4) by YT3PR01CA0099.outlook.office365.com (2603:10b6:b01:85::19) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.451.16 via Frontend Transport; Thu, 24 Sep 2026 19:07:36 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by BL6PEPF00020E63.mail.protection.outlook.com (10.167.249.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.8 via Frontend Transport; Thu, 24 Sep 2026 19:07:36 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:07:12 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:07:11 -0700 Received: from inno-dell.home (10.127.8.10) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.2562.49 via Frontend Transport; Thu, 24 Sep 2026 12:07:02 -0700 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Peter Colberg , Zhi Wang Subject: [PATCH v2 6/8] rust: pci: add bus callback sriov_configure(), to control SR-IOV from sysfs Date: Thu, 24 Sep 2026 22:05:53 +0300 Message-ID: <20260924190556.1620886-7-zhiw@nvidia.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260924190556.1620886-1-zhiw@nvidia.com> References: <20260924190556.1620886-1-zhiw@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BL6PEPF00020E63:EE_|MN2PR12MB4253:EE_ X-MS-Office365-Filtering-Correlation-Id: 2ed124f5-a84f-4a27-39a6-08df1a6f1857 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|82310400026|23010399003|7416014|376014|36860700016|13003099007|6133799003|56012099006|10067099003|18002099003|22082099003|11063799006|5023799004; X-Microsoft-Antispam-Message-Info: j0gzwaje9MJMbLaaXFzbytc8yJHshTdRSKa52pA5/vv7zkbkAQ08qI60Nqe0VW8wsxbZ910ru+Xmf3JnQrviJXrmWGnmDLCzTmRggDski/3V/6GqoRLPvIA4Et0Yrw5E8q4KcFY4mMOQApXYham9XcNZzE8twUs76xduulnj1T1VOUxJbJsYoYUxHKxESAKvVpzoMlp18HBf/e4p0396QMaT0CtqtaQOj4xDgKmi1L1ln8f43/bTng6dL5bCPcT6EDRJW3zQYEi0fC8SUBI8yXBRoCyz6nri4qnbtGEwjg7a2MiwTPddCsDpCUfJfWBv6kxDgKbG7hQDemYTRpMnB3YGXODhiDhyzoxcqLq36EN8z31XHINRv/1itV6wjuGekezGbpdJTzbCz3B3hPei8UEbu3RJF/wB0BtA8OOsweebWPiqAbjZjnftNzhc65EnJRNelNRwsrJ0V/gUEY85aVQT/npeyJOHXvKeNLRBhXMhXQW3ONP5kH0oML/3LCKIjoHLwQi0sl2NwYV6TVvKVNPIkMvYVUnXMxZ5iOWL2sIfQZkBXYDibMxsS23Ks+fsCyeO3HPaiz2wuLdBLbILLNODq5Dq/jzHGTj7NQyu9kfMV0pWH+f0wxJFbngETbwMIIC4SBT0bWyG6WrTofoh3Y0WkJVx+dULpBrf2Q2ki1Hfqe9j/kHDYSmW2szPNBZgUEg4Fv8yyeTCGCwiY587AQ== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230040)(1800799024)(82310400026)(23010399003)(7416014)(376014)(36860700016)(13003099007)(6133799003)(56012099006)(10067099003)(18002099003)(22082099003)(11063799006)(5023799004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: pgdKM3lqohMPlJFU5fDX53TQhOlHBi4aRzN5bZTHqHgxhq02h2/vexfojo0+51jv96Vc4/VDg0rFSPXaxFw/Jlw1ketIYiJGadEwIg/dLVg38QYjpYOfk8Msr8wWBXBFNe/ShWF/yUFVbqruzGglxS8NxJApg+hfLGf5KMbpCls7Z0ObHOmuRWZXr5AbMKmnXM+TLd+qOc4/nv6Dc055tvlKzN00pMglOY+daU4zeB7pBq2VVimV0wO9/Rgr+J1zkX6q81rCUqXsV0aosih8fWiOlhjsrL+u0yKBLK0W0jHtAMFOkQrf/XCgzZ8icq8ICAiP0aKAXSFaEUOpMEdJyaXB4NTaAyA7pvZXf2hrZ7tKh71hLrdHlr3ROOyNhm6vqGCP46AHNX6ckLSWy+Ha13jkbmbda/lT5aiMeIevFUc7ooR6tIbDeU38dldu5By3 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Sep 2026 19:07:36.0183 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 2ed124f5-a84f-4a27-39a6-08df1a6f1857 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BL6PEPF00020E63.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4253 Content-Type: text/plain; charset="utf-8" From: Peter Colberg Add an optional bus callback sriov_configure() to pci::Driver trait, using the vtable attribute to query if the driver implements the callback. The callback is invoked when a user-space application writes the number of VFs to the sysfs file `sriov_numvfs` to enable SR-IOV, or zero to disable SR-IOV for a PCI device. Invoke the callback with zero before PF unbind so drivers can release their SR-IOV resources, including runtime-PM references. Disable any remaining VFs before releasing the PF driver data. Suggested-by: Danilo Krummrich Signed-off-by: Peter Colberg Signed-off-by: Zhi Wang --- rust/kernel/pci.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index db0554f14afc..cb3ed2207075 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -84,6 +84,10 @@ unsafe fn register( (*pdrv.get()).probe =3D Some(Self::probe_callback); (*pdrv.get()).remove =3D Some(Self::remove_callback); (*pdrv.get()).id_table =3D T::ID_TABLE.as_ptr(); + #[cfg(CONFIG_PCI_IOV)] + if T::HAS_SRIOV_CONFIGURE { + (*pdrv.get()).sriov_configure =3D Some(Self::sriov_configu= re_callback); + } } =20 // SAFETY: `pdrv` is guaranteed to be a valid `DriverType`. @@ -135,7 +139,13 @@ extern "C" fn remove_callback(pdev: *mut bindings::pci= _dev) { =20 // Keep PF data installed until all VF remove callbacks have compl= eted. #[cfg(CONFIG_PCI_IOV)] - pdev.disable_sriov(); + if pdev.num_vf() !=3D 0 { + if T::HAS_SRIOV_CONFIGURE { + Self::sriov_configure_callback(pdev.as_raw(), 0); + crate::warn_on!(pdev.num_vf() !=3D 0); + } + pdev.disable_sriov(); + } =20 // SAFETY: `remove_callback` is only ever called after a successfu= l call to // `probe_callback`, hence it's guaranteed that `Device::set_drvda= ta()` has been called @@ -144,6 +154,20 @@ extern "C" fn remove_callback(pdev: *mut bindings::pci= _dev) { =20 T::unbind(pdev, data); } + + #[cfg(CONFIG_PCI_IOV)] + extern "C" fn sriov_configure_callback( + pdev: *mut bindings::pci_dev, + nr_virtfn: c_int, + ) -> c_int { + // SAFETY: The PCI bus only ever calls the sriov_configure callbac= k with a valid pointer to + // a `struct pci_dev`. + // + // INVARIANT: `pdev` is valid for the duration of `sriov_configure= _callback()`. + let pdev =3D unsafe { &*pdev.cast::>>() }; + + from_result(|| T::sriov_configure(pdev, nr_virtfn)) + } } =20 /// Declares a kernel module that exposes a single PCI driver. @@ -330,6 +354,44 @@ fn probe<'bound>( fn unbind<'bound>(dev: &'bound Device>, this: Pin<&Se= lf::Data<'bound>>) { let _ =3D (dev, this); } + + /// Single Root I/O Virtualization (SR-IOV) configure. + /// + /// Called when a user-space application enables or disables the SR-IO= V capability for a + /// [`Device`] by writing the number of Virtual Functions (VF), `nr_vi= rtfn` or zero to the + /// sysfs file `sriov_numvfs` for this device. Implementing this callb= ack is optional. + /// + /// Further, and unlike for a PCI driver written in C, when a PF devic= e with enabled VFs is + /// unbound from its bound [`Driver`], the `sriov_configure()` callbac= k is invoked to disable + /// SR-IOV before the `unbind()` callback. This guarantees that when a= VF device is bound to a + /// driver, the underlying PF device is bound to a driver, too. + /// + /// Upon success, this callback must return the number of VFs that wer= e enabled, or zero if + /// SR-IOV was disabled. + /// + /// See [PCI Express I/O Virtualization]. + /// + /// [PCI Express I/O Virtualization]: https://docs.kernel.org/PCI/pci-= iov-howto.html + /// + /// # Examples + /// + /// ``` + /// # use kernel::{device::Core, pci, prelude::*}; + /// #[cfg(CONFIG_PCI_IOV)] + /// fn sriov_configure(dev: &pci::Device>, nr_virtfn: i32) ->= Result { + /// if nr_virtfn =3D=3D 0 { + /// dev.disable_sriov(); + /// } else { + /// dev.enable_sriov(nr_virtfn)?; + /// } + /// Ok(nr_virtfn) + /// } + /// ``` + #[cfg(CONFIG_PCI_IOV)] + fn sriov_configure(dev: &Device>, nr_virtfn: i32) -> = Result { + let _ =3D (dev, nr_virtfn); + build_error!(crate::error::VTABLE_DEFAULT_ERROR) + } } =20 /// The PCI device representation. @@ -451,7 +513,6 @@ pub(crate) fn is_virtfn(&self) -> bool { =20 /// Returns the number of Virtual Functions (VF) enabled for a Physica= l Function (PF). #[cfg(CONFIG_PCI_IOV)] - #[expect(dead_code)] pub(crate) fn num_vf(&self) -> i32 { // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. unsafe { bindings::pci_num_vf(self.as_raw()) } --=20 2.53.0 From nobody Fri Sep 25 06:47:06 2026 Received: from CH4PR04CU002.outbound.protection.outlook.com (mail-northcentralusazon11013022.outbound.protection.outlook.com [40.107.201.22]) (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 6D0484B487D; Thu, 24 Sep 2026 19:07:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.201.22 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276877; cv=fail; b=Td7p5aY+D/swf3kug3XAwFsO+EeOahI3q2REOKxxKnlpxWign6Xe5ryF0u5Z3dseexueEmur7LsaLgkw8zma2kf8mTcHRgZjEtbD4KeGLrE0Sa03n1lSw38SQAu6FuecpE/umbLDIGZftqrhX1u2bmJNQScoiIKBSzPOuOyN/wk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276877; c=relaxed/simple; bh=iyNvbW89tvWNs4m3UxJ3EMLsZ2Wt0PkyjzOIw8iSHpg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=O485Jw3ev8iUSoGZFMs3PRbh2X3+3THH0iRe9m64ZpzyU9kcjYLyTZ8GT23Ad77rHDUG0/2UmUebw2T+s7w9+f6D3eQXzZEZCdwIGOWVGm49rUMLJN1qqkOqg8lbMr03gByKlUisx1jIGGZVcagGQHiLpyw0uRaAeTs8PmScMf0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=oOEbfTP+; arc=fail smtp.client-ip=40.107.201.22 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="oOEbfTP+" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=MmYxQ2P2DLoeINKWJlPZDKIVwiCekEBLMUbU3bKdBceVlA1xSTPJcVHYNOxO6ULUTieela51QTsVKuyIzmGZ/rdjQ67K9AxsXkMpcYUL6hZgJbGHhgkxgIOIrx/fTHM9gqLk81G4eI/Nw7XpoOs/8B169WtS+2CBZ+YfnnSmmelQniI0aJLQXRI6EHjWv0GbdJ5mU89F71MP//bt5ACIY2k8vUCgBFJHJC6Z89ZTJ1UM/L2FrHGlzbINMvAhFiKmQJt0/ymbLlu/02OfhZdK/dtBMyiR/MEB8sL6hKQpW2jM37TyoAh1uWHc1c0WwKFPUHNPvMmurUl+/YVg621J0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=s5LKA1KDw2zuiiTgzTZYZztwAheOENhlkciplxAfIDk=; b=gKW5SmQ6GoyPG3jcof4o2ih3LdWu7uEHf4FjMmZbRbVQw4Q09LZkEsknjei42+27q09MMwe4TCn0oYQ3tnNHLaTyCB9fBTm7MK0f5l+fvgAoCelsrbf81WleBN1uh6yO+/8mnpkAMNjtTNbCIWrVmy6uUuPNILhqtgaDe1eJlkLztm1NCRyhHF38J9IIcTxjN6KWuT3gaxFiGiYsm+xCShSmpELQxOFtBIKhijpKSV10aLdb2ZAMhGqFIQ54P1zd9JJ3qnQ03fguXoeSJaZs5QocmKW7kfiL/AfCZ8QeyxT5uW3SU5UZ3KqRGxR+2JanQZJRNGMokJcO5HawS3ApBw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=s5LKA1KDw2zuiiTgzTZYZztwAheOENhlkciplxAfIDk=; b=oOEbfTP+Vs0lTW491MZrCSixDsNifkKusqoy/v6te/Jeq9LqknY7PGKBQJvzWpKl9c2NATG5GcNj9k+tCHoK2LeoInzQD81ItttAyZOa/Q0/mndp+UNyZfnnDLz28UU9kJH6n+AjjYy7P9JCjdj/JvWA0zwwTfw6Tur1cgVG9xa2KSd0wp7AhSKJm0pOXnZgDNyh/Zh33xbnh+16L+aFSAL2zqRcYkqgoQvcYsiraoTJDVsnUDUZS5H+GOqtV0Z4cK7prA+0zB2/QPzw78ekJkGWsjMGomH53XXP/NEmftWHwQIkfabr+z5oF0qVh9YAzokHKIxvNb+7U7AAsssBEA== Received: from CH2PR19CA0005.namprd19.prod.outlook.com (2603:10b6:610:4d::15) by CH2PR12MB4197.namprd12.prod.outlook.com (2603:10b6:610:ab::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.18; Thu, 24 Sep 2026 19:07:44 +0000 Received: from BN2PEPF000044A1.namprd02.prod.outlook.com (2603:10b6:610:4d:cafe::8c) by CH2PR19CA0005.outlook.office365.com (2603:10b6:610:4d::15) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.451.18 via Frontend Transport; Thu, 24 Sep 2026 19:07:44 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BN2PEPF000044A1.mail.protection.outlook.com (10.167.243.152) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.8 via Frontend Transport; Thu, 24 Sep 2026 19:07:43 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:07:21 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:07:20 -0700 Received: from inno-dell.home (10.127.8.10) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.2562.49 via Frontend Transport; Thu, 24 Sep 2026 12:07:12 -0700 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Zhi Wang Subject: [PATCH v2 7/8] rust: pci: add typed SR-IOV PF registration data Date: Thu, 24 Sep 2026 22:05:54 +0300 Message-ID: <20260924190556.1620886-8-zhiw@nvidia.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260924190556.1620886-1-zhiw@nvidia.com> References: <20260924190556.1620886-1-zhiw@nvidia.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN2PEPF000044A1:EE_|CH2PR12MB4197:EE_ X-MS-Office365-Filtering-Correlation-Id: 4e9a7c1e-fcc0-4a19-655e-08df1a6f1d13 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|7416014|376014|36860700016|82310400026|1800799024|23010399003|10067099003|56012099006|5023799004|11063799006|3023799007|6133799003|18002099003|22082099003; X-Microsoft-Antispam-Message-Info: I8ZvptI9VvS7Ssc3E5wcrVEZTGGlnCa/rLbh2TzOyh2s0PEBT6Pm07buIyE8bRMG4s07nWqxHvbFX8JnlHN2vJlTiuML1vhK5jEZ+4Nnk4d4apefMF8ow+4AT8NjtFajcLJNVH0U5D5LANT+UneHALjZCJYlmjN9Tp/pNl5YuQXxEhkKFar3VnZx8nTSE5Pe01CZcv9TzbngrAEBuL4SEJdXKYrx2o1DlHs/TFSyDY2K/AHEQuw4N1wLWkpBMkSeYHRGeOh1zwKDVe0wnCJFCoS5pKIHyGD2/6J1YvSQ6OqvHorxsV8sMauaNjI5g2f8CzF2/A14jQK3OJsOQmwJ8k15CVjGf6nS6S0AA4RVqdAfi83vDdPmU6JHnJMjUhAwpSxPNJGQHC5GCWUWv/NTXyAW3WlZL3RdcFZ8eUhvRfGNvKtoci8UhzWm/JiIzGLf7d2YWZ/9d+DKQhZm5eKiT/hBKMXFj96kbRCdep/+U+WyHGJm/ib+7P+y/8/pVIlpzWWKKC5fzhwgaH1zWSH4Yn5H7f1K3zPsO1y7kNh1+SF24yCJ+yyAdQvYj8DbIuELno9FE/66aQW8G8Ow9U+gYkz9WjapiY+lf/JpLdH8Jmg4Gta9vCffvW91e6trdZoA2KHwWQGGk85EtSIdyPjSfU73JabGtTh8gKlnR1Y6iVT1EdBO+Okb22XUFKFxy6ZwiQ2ZbYdKsXLHfDLnKzCNtw== X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230040)(7416014)(376014)(36860700016)(82310400026)(1800799024)(23010399003)(10067099003)(56012099006)(5023799004)(11063799006)(3023799007)(6133799003)(18002099003)(22082099003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: AgLYnWA/vmtePdx+6CLOaz+lBbkKXzYQmbd6ce1Vw84vj+kCgPCFmD6X8xYdIZfbHPgcaKuhffcfS7ZOLPD4G8J4tkXHsgjqjh2N2hLapZzJTpQDC4mVW8bk/kFCN9DNOTLl28Olx0hNtDqh2wUfqbTU4/PxMp69amjHTgT922J17FUiI+IBd+XIBVePJm4YovcfcW1IM0zeikkI1rp0gO+2M2zZTP9VQ+kZM7QZfbP5ryuFV3e3XJLfs12/B9jHOXKIegtIib+DYT69HTFGtwN2UmOvKdVjpbhE07IfLU1JkspJaNl42ziEm/qwX/90CqyvBIiopTrMPwEJ6tM9oyxBr6KVM4BwxRbG0yapQFpYOumrEx05i7yNIClx95/Q4ZffVh8UMAMdBFS9UiV8XiNjSkz73fKJNov+C8lYmYC9xvUDE0UlPEsqkwyNhcG+ X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Sep 2026 19:07:43.9569 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4e9a7c1e-fcc0-4a19-655e-08df1a6f1d13 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN2PEPF000044A1.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4197 Content-Type: text/plain; charset="utf-8" Rust PF and VF drivers bind to separate PCI devices and may reside in different modules. A VF driver that calls PF operations needs typed access to the data exposed by the PF driver. This data must remain valid until VF driver removal completes. Add `VfRegistration` to register a pinned Rust object in the PF's driver data. Add accessors for VF drivers to borrow this object after checking the requested Rust type. During registration teardown, disable SR-IOV and wait for VF remove callbacks to finish before dropping the object. Co-developed-by: Danilo Krummrich Signed-off-by: Danilo Krummrich Signed-off-by: Zhi Wang --- include/linux/pci.h | 7 ++ rust/kernel/pci.rs | 38 ++++-- rust/kernel/pci/sriov.rs | 253 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 290 insertions(+), 8 deletions(-) create mode 100644 rust/kernel/pci/sriov.rs diff --git a/include/linux/pci.h b/include/linux/pci.h index d31a8d107b1e..9b6ae544469e 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -551,6 +551,13 @@ struct pci_dev { u16 ats_cap; /* ATS Capability offset */ u8 ats_stu; /* ATS Smallest Translation Unit */ #endif +#if defined(CONFIG_PCI_IOV) && defined(CONFIG_RUST) + /* + * Private data owned by the PF's Rust driver, readable by VF drivers + * through the PCI VF registration data Rust abstraction. + */ + void *vf_registration_data_rust; +#endif #ifdef CONFIG_PCI_PRI u16 pri_cap; /* PRI Capability offset */ u32 pri_reqs_alloc; /* Number of PRI requests allocated */ diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index cb3ed2207075..831b76744a30 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -35,6 +35,8 @@ mod id; mod io; mod irq; +#[cfg(CONFIG_PCI_IOV)] +pub mod sriov; =20 pub use self::id::{ Class, @@ -55,6 +57,8 @@ IrqVector, IrqVectorRegistration, // }; +#[cfg(CONFIG_PCI_IOV)] +pub use self::sriov::VfRegistration; =20 /// An adapter for the registration of PCI drivers. pub struct Adapter(T); @@ -166,7 +170,16 @@ extern "C" fn sriov_configure_callback( // INVARIANT: `pdev` is valid for the duration of `sriov_configure= _callback()`. let pdev =3D unsafe { &*pdev.cast::>>() }; =20 - from_result(|| T::sriov_configure(pdev, nr_virtfn)) + // SAFETY: `sriov_configure` is called only after a successful pro= be and before unbind, so + // the stored pointer has type `T::Data<'_>` and remains valid thr= oughout this callback. + let data =3D unsafe { pdev.as_ref().drvdata_borrow::>(= ) }; + + from_result(|| { + if !pdev.is_physfn() { + return Err(ENODEV); + } + T::sriov_configure(pdev, data, nr_virtfn) + }) } } =20 @@ -377,8 +390,13 @@ fn unbind<'bound>(dev: &'bound Device= >, this: Pin<&Self::Data<' /// /// ``` /// # use kernel::{device::Core, pci, prelude::*}; + /// # struct Data; /// #[cfg(CONFIG_PCI_IOV)] - /// fn sriov_configure(dev: &pci::Device>, nr_virtfn: i32) ->= Result { + /// fn sriov_configure( + /// dev: &pci::Device>, + /// _this: Pin<&Data>, + /// nr_virtfn: i32, + /// ) -> Result { /// if nr_virtfn =3D=3D 0 { /// dev.disable_sriov(); /// } else { @@ -388,8 +406,12 @@ fn unbind<'bound>(dev: &'bound Device= >, this: Pin<&Self::Data<' /// } /// ``` #[cfg(CONFIG_PCI_IOV)] - fn sriov_configure(dev: &Device>, nr_virtfn: i32) -> = Result { - let _ =3D (dev, nr_virtfn); + fn sriov_configure<'bound>( + dev: &'bound Device>, + this: Pin<&Self::Data<'bound>>, + nr_virtfn: i32, + ) -> Result { + let _ =3D (dev, this, nr_virtfn); build_error!(crate::error::VTABLE_DEFAULT_ERROR) } } @@ -496,24 +518,24 @@ pub fn resource_start(&self, bar: u32) -> Result { } =20 /// Returns `true` if this device is a Physical Function (PF). + #[cfg(CONFIG_PCI_IOV)] #[inline] - #[expect(dead_code)] pub(crate) fn is_physfn(&self) -> bool { // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. unsafe { (*self.as_raw()).is_physfn() !=3D 0 } } =20 /// Returns `true` if this device is a Virtual Function (VF). + #[cfg(CONFIG_PCI_IOV)] #[inline] - #[expect(dead_code)] - pub(crate) fn is_virtfn(&self) -> bool { + pub fn is_virtfn(&self) -> bool { // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. unsafe { (*self.as_raw()).is_virtfn() !=3D 0 } } =20 /// Returns the number of Virtual Functions (VF) enabled for a Physica= l Function (PF). #[cfg(CONFIG_PCI_IOV)] - pub(crate) fn num_vf(&self) -> i32 { + pub fn num_vf(&self) -> i32 { // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. unsafe { bindings::pci_num_vf(self.as_raw()) } } diff --git a/rust/kernel/pci/sriov.rs b/rust/kernel/pci/sriov.rs new file mode 100644 index 000000000000..90abe878f67c --- /dev/null +++ b/rust/kernel/pci/sriov.rs @@ -0,0 +1,253 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Abstractions for PCI Single Root I/O Virtualization (SR-IOV) drivers. + +use super::Device as PciDevice; +use crate::{ + bindings, + device, // + prelude::*, + types::{ + CovariantForLt, + ForLt, // + }, +}; +use core::{ + any::TypeId, + marker::PhantomPinned, // +}; + +/// Wrapper for VF registration data stored inside a [`VfRegistration`]. +/// +/// Stores a [`TypeId`] header (derived from `F`) followed by the pinned d= ata, +/// so that [`PciDevice::vf_registration_data_with()`] can verify the type= at +/// runtime. +#[repr(C)] +#[pin_data] +struct VfRegistrationData<'a, F: ForLt + 'static> { + type_id: TypeId, + #[pin] + data: F::Of<'a>, +} + +static_assert!( + core::mem::offset_of!(VfRegistrationData<'static, CovariantForLt!(())>= , type_id) =3D=3D 0 +); + +impl<'a, F: ForLt + 'static> VfRegistrationData<'a, F> { + /// Pin-initializer for the registration data. + fn new(data: D) -> impl PinInit + use<'a, D, F> + where + D: PinInit, Error> + 'a, + { + try_pin_init!(Self { + type_id: TypeId::of::(), + data <- data, + }) + } +} + +/// SR-IOV VF registration on a PF device. +/// +/// Owns the registration data that VF drivers access via +/// [`PciDevice::vf_registration_data_with()`] and [`PciDevice::vf_registr= ation_data()`]. +/// +/// The Rust PCI adapter removes all VFs before invoking the PF driver's u= nbind callback or +/// dropping its data. Drop of a published registration calls `pci_disable= _sriov()` before +/// clearing the pointer and letting the data fields drop. +#[pin_data(PinnedDrop)] +pub struct VfRegistration<'a, F: ForLt + 'static> { + pdev: &'a PciDevice, + #[pin] + inner: VfRegistrationData<'a, F>, + published: bool, + #[pin] + _pin: PhantomPinned, +} + +impl<'a, F: ForLt + 'static> VfRegistration<'a, F> +where + for<'b> F::Of<'b>: Send + Sync, +{ + /// Create a new VF registration. + /// + /// Returns a pin-initializer so the registration can be embedded dire= ctly + /// in the PF driver's bus device private data. + /// + /// # Safety + /// + /// The caller must ensure that the containing struct's field ordering= drops + /// this `VfRegistration` before any resources that the registration d= ata + /// borrows. + /// + /// The caller must invoke this during the PCI driver's probe and embe= d the result in the driver + /// data. On an SR-IOV PF, no VF may be enabled before probe successfu= lly installs the complete + /// driver data. + pub unsafe fn new<'core, D>( + pdev: &'a PciDevice>, + data: D, + ) -> impl PinInit + use<'a, 'core, D, F> + where + D: PinInit, Error> + 'a, + { + pin_init::pin_init_scope(move || { + if pdev.is_virtfn() { + return Err(ENODEV); + } + + let published =3D pdev.is_physfn(); + if published { + if pdev.num_vf() !=3D 0 { + return Err(EBUSY); + } + + if !pdev.vf_registration_data_rust().is_null() { + return Err(EBUSY); + } + } + + Ok(try_pin_init!(Self { + pdev, + inner <- VfRegistrationData::new(data), + published, + _pin: PhantomPinned, + _: { + if *published { + // Store the pointer to the pinned `VfRegistration= Data` + // on the PCI device so VF drivers can find it. + pdev.set_vf_registration_data_rust( + core::ptr::from_ref(inner.as_ref().get_ref()).= cast_mut().cast(), + ); + } + }, + })) + }) + } +} + +#[pinned_drop] +impl PinnedDrop for VfRegistration<'_, F> { + fn drop(self: Pin<&mut Self>) { + if !self.published { + return; + } + + // SAFETY: `pci_disable_sriov()` is safe to call on any `pci_dev`;= it + // is a no-op if the device has no VFs enabled. When VFs are enabl= ed, + // this blocks until all VF `remove()` callbacks complete. + unsafe { bindings::pci_disable_sriov(self.pdev.as_raw()) }; + + // After `pci_disable_sriov()` all VFs are gone, so no one can read + // the pointer anymore. + self.pdev + .set_vf_registration_data_rust(core::ptr::null_mut()); + + // The pinned `inner` field is dropped automatically after this re= turns. + } +} + +// SAFETY: The inner data is `Send` (enforced by the bound), and `&PciDevi= ce` is `Send + Sync`. +unsafe impl Send for VfRegistration<'_, F> where for<'a> F::Of<'= a>: Send {} + +// SAFETY: The inner data is `Send + Sync`. `VfRegistration` doesn't expos= e mutable access; +// VF drivers only read the data through an immutable pinned reference. +unsafe impl Sync for VfRegistration<'_, F> where for<'a> F::Of<'= a>: Send + Sync {} + +impl PciDevice { + /// Returns the raw `vf_registration_data_rust` pointer from this devi= ce. + fn vf_registration_data_rust(&self) -> *mut core::ffi::c_void { + // SAFETY: `self.as_raw()` is valid. + unsafe { (*self.as_raw()).vf_registration_data_rust } + } + + /// Sets the `vf_registration_data_rust` pointer on this device. + fn set_vf_registration_data_rust(&self, ptr: *mut core::ffi::c_void) { + // SAFETY: `self.as_raw()` is valid. PCI probe publishes the data = before enabling VFs; + // teardown removes all VFs before withdrawing it. + unsafe { (*self.as_raw()).vf_registration_data_rust =3D ptr }; + } +} + +impl PciDevice { + /// Returns the PF for this VF, or [`ENODEV`] if this is not a VF. + fn physfn(&self) -> Result<&PciDevice> { + if !self.is_virtfn() { + return Err(ENODEV); + } + + // SAFETY: `self.as_raw()` is valid and this VF uses the `physfn` = union field. + let pf =3D unsafe { (*self.as_raw()).__bindgen_anon_1.physfn }; + if pf.is_null() { + return Err(ENODEV); + } + + // SAFETY: PCI holds a PF reference until VF removal completes. Th= e returned borrow + // cannot outlive this bound VF, and `PciDevice` is a transparent = wrapper of `pci_dev`. + Ok(unsafe { &*pf.cast() }) + } + + /// Internal helper: reads the `vf_registration_data_rust` pointer fro= m the + /// PF, checks the `TypeId`, and returns a pinned reference. + /// + /// # Safety + /// + /// The returned borrow must be confined by a closure higher-ranked in= dependently over its + /// borrow and data lifetimes, or `F` must be covariant in its encoded= lifetime. + unsafe fn vf_registration_data_pinned(&self) -> Re= sult>> { + let pf =3D self.physfn()?; + + let ptr =3D pf.vf_registration_data_rust(); + if ptr.is_null() { + return Err(ENOENT); + } + + // SAFETY: The Rust PCI adapter keeps the PF data installed until = VF removal completes. + // `ptr` points to a `VfRegistrationData` whose first field is a `= TypeId`. + let type_id =3D unsafe { ptr.cast::().read() }; + if type_id !=3D TypeId::of::() { + return Err(EINVAL); + } + + // SAFETY: TypeId check confirms the stored type matches `F`. The = data + // is pinned inside the PF's driver data struct. Lifetime shorteni= ng + // from the PF's binding scope to `'_` is layout-compatible. + let data_ptr =3D unsafe { + let vfrd =3D ptr.cast::>(); + &raw const (*vfrd).data + }; + + // SAFETY: `data` is structurally pinned inside `VfRegistrationDat= a`. + Ok(unsafe { Pin::new_unchecked(&*data_ptr) }) + } + + /// Access the VF registration data through a closure with an HRTB lif= etime. + /// + /// `F` is the [`ForLt`](trait@ForLt) encoding of the data type. Retur= ns + /// [`ENODEV`] if this is not a VF, [`ENOENT`] if no data was register= ed, + /// or [`EINVAL`] if `F` does not match the type registered by the PF. + /// + /// The closure's borrow and the registration data's lifetime are inde= pendent, so a borrow of + /// the context cannot be stored in invariant registration data. + pub fn vf_registration_data_with( + &self, + f: impl for<'borrow, 'data> FnOnce(Pin<&'borrow F::Of<'data>>) -> = R, + ) -> Result { + // SAFETY: The higher-ranked closure prevents the borrow from esca= ping or being stored in + // invariant data by keeping its lifetime independent of the erase= d data lifetime. + let pinned =3D unsafe { self.vf_registration_data_pinned::()? }; + Ok(f(pinned)) + } + + /// Returns a pinned reference to the VF registration data. + /// + /// Available only when `F` implements [`CovariantForLt`](trait@crate:= :types::CovariantForLt), + /// guaranteeing that shortening the PF data lifetime is sound. + /// + /// For non-covariant types, use [`Self::vf_registration_data_with()`]. + /// + /// It returns the same errors as [`Self::vf_registration_data_with()`= ]. + pub fn vf_registration_data(&self) -> Res= ult>> { + // SAFETY: `CovariantForLt` permits shortening the encoded lifetim= e to this borrow. + unsafe { self.vf_registration_data_pinned::() } + } +} --=20 2.53.0 From nobody Fri Sep 25 06:47:06 2026 Received: from BYAPR05CU005.outbound.protection.outlook.com (mail-westusazon11010033.outbound.protection.outlook.com [52.101.85.33]) (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 B750A4AD4CD; Thu, 24 Sep 2026 19:08:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.101.85.33 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276887; cv=fail; b=eFNaeBCdpbUwTkttfXZR23sfxq9v/C2bKp6ChgfY2oOgkxVPwGxXxhqB60qYXn+fPkPuCXzd0aIoMv8WyPyV2M5p14kExjpuSESOr1osyFnnC6ij3yQn7tJrGJdENBaaxladQcItHlhaGcpqt/413tY6ESRi5eRMqQuyzVBA0C0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790276887; c=relaxed/simple; bh=wjrza9kDnYzWfXeOgvf53NvPW1mE+LMIbD+2MU3SiJc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=IiA8ifeO+6MqNnP6QeQMpA/q4UP3hkPpOXYPs+hddUyEuhzWQykULIMqzlOdu62ojoaChqgNkSASoRvog1AfH9q6yOchfu59zEVIy7NH34bIluTp3LLt6lqQt0imGBlgOEAkrLbojz5ZkNXv7bvR2Sba0XxibfCB61Uk5C97vh0= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com; spf=fail smtp.mailfrom=nvidia.com; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b=kRIL9shv; arc=fail smtp.client-ip=52.101.85.33 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=nvidia.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=nvidia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=Nvidia.com header.i=@Nvidia.com header.b="kRIL9shv" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=cP12Ig0Dv8cCOEFy3v3IAEQrz5nMNXN5rdEkBqLeVa0DXFzOENKabjRZqqNCdApLqkXB6iuaXUekiHcQw7SoMduNkuiarsbOEqfFUQFjZ1816Tu8X51mjvpQe2O8cIUrrn4W7yNJYHGZn1gY+vd4vZSvLbisdPEWYbotQtBks6DDzWYLbKVnE5Iw1OwsTvNCzyd4TFKza0LyhET5qOaXymyBg4KC05+pxDk3o5MnEkAOUHgyfjFIKI1AFW8oKPoy3QOR/aBqVyM6JWaNexy7Tt+E3BvCjPJTsD8NtAzsJxh0jnF/Z9QoqRJMeEX5suFy2pbNobntOT4XBkNC29S2sA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=PTOy9G26PSLALBjWTjYulrGvyt4/5ZIu4yE+eBaXZkU=; b=o7StbCvaFfojv/4Xy++OJFvrZPBmBYb2DeBtO57I3lAjOcUkiVuJCVma1TKj8zHmzb0X0pmrrkNEM0MLPjYg99UhNOvCeDjH5WGHRd+0a6j3Vk80ix7b8+mJ5yqrcBgv+PzrLAGeO6Zat3JDk+8x5KfQE8/tXcZ+WjgmT6kt/l47y5lVq0lkfANdMMeUOueDpvPUUnv75yt+jNi9jia8mCyAnKEfdDx0cKHH5+BycnF97d+M2NWg2GbWvbeaC48nzA7p3mTknHA1O714lP7mmic2MFAUwqJymSWMLhDgWPlLiqJOEC+nZBi82A039Z6XYANFPje+S7jael89zwcKLg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=PTOy9G26PSLALBjWTjYulrGvyt4/5ZIu4yE+eBaXZkU=; b=kRIL9shvbiXON57yQSbaLddW8R07u75kyX8yIQYI4yWfpNFs8Q53rr93eayn2/O3SDoNWTIwm2cuW0dADDnMdUhrrTEa0CoDpOucl9KQTfPcxO/1KUIUuF9Y1WU7S92Inm6btkF4noJm6GuJ3vmu8d4NbGVUv3oPLk21APY70tT8cklZ21g22BUJ/EuYyHORNBRoGI7p9YHO1UQ7GTDYHZpkUMmJO296lumPrNl25eNDRFTjvc1wZfeBpmZtOmu+Q0FpvrNowP0oTrtMN1BCXUppgiJC/40HNZcHYNxwlPoCYeP7uibTp8pOjKcwWt/sqAI2kHC8+hDuTlwpAUQ8dw== Received: from DS3P221CA0014.NAMP221.PROD.OUTLOOK.COM (2603:10b6:8:45e::18) by CH2PR12MB4150.namprd12.prod.outlook.com (2603:10b6:610:a6::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.18; Thu, 24 Sep 2026 19:07:55 +0000 Received: from BN2PEPF0000449E.namprd02.prod.outlook.com (2603:10b6:8:45e:cafe::24) by DS3P221CA0014.outlook.office365.com (2603:10b6:8:45e::18) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.21.451.18 via Frontend Transport; Thu, 24 Sep 2026 19:07:54 +0000 X-MS-Exchange-Authentication-Results: mx.microsoft.com 1; spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by BN2PEPF0000449E.mail.protection.outlook.com (10.167.243.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.21.451.8 via Frontend Transport; Thu, 24 Sep 2026 19:07:53 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:07:31 -0700 Received: from rnnvmail205.nvidia.com (10.129.68.10) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.49; Thu, 24 Sep 2026 12:07:30 -0700 Received: from inno-dell.home (10.127.8.10) by mail.nvidia.com (10.129.68.10) with Microsoft SMTP Server id 15.2.2562.49 via Frontend Transport; Thu, 24 Sep 2026 12:07:21 -0700 From: Zhi Wang To: , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , Peter Colberg , Zhi Wang Subject: [PATCH v2 8/8] samples: rust: add Rust SR-IOV VF driver sample Date: Thu, 24 Sep 2026 22:05:55 +0300 Message-ID: <20260924190556.1620886-9-zhiw@nvidia.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260924190556.1620886-1-zhiw@nvidia.com> References: <20260924190556.1620886-1-zhiw@nvidia.com> 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 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BN2PEPF0000449E:EE_|CH2PR12MB4150:EE_ X-MS-Office365-Filtering-Correlation-Id: 018a6122-499d-451f-3673-08df1a6f22fd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|82310400026|36860700016|376014|7416014|23010399003|10067099003|56012099006|6133799003|22082099003|18002099003|5023799004|11063799006|13003099007; X-Microsoft-Antispam-Message-Info: hsXQiQNCFBg+tJsIUTwz2qE3suF2Rd2yltTlo4He7OD4y5tYKJFMQ/L0/qtYqsKfhOk2cOXzpuvYZn7tfWzbJtngTDofuov136RvGRtJaBvKJ/6iy0E+QZtobXM+VhgyKmcqxKpasC/mZhKsChAt9Xliyrmd0sYecCgA1GUjZLIz3nPXsC4VGMwZKDWsGnSEmWODOAcJsWB8YTtvNPI69sjN54KlnPkGRbyysmrC4sTuzH23E+1v1ZyualTdlfo6PWmq8DnGV1RxAPjvsfyHEM2NgYKgrlbNTCYRrxkiy5NBbvhis8Vd6XAkFtZ1Rsx4EtYMQlP6K4ZoXJwxHrtlX9ZtwKl978RH+ML4OV28WUpKo9hoM6n0Qe/d6l58nOibwnv4jGMqUMOztKrZ5V1uRFopyTqgQfksqJ7PyQw8qpwlrgdKfwuQzpNbgHtxTfQzMQrK9Nhy5PILtMNKwBMo8Pm4GFVbIrXFkrbMhRLOG5Oc6HindyIVcFdcRClIXXqcYz4qmAGrEJ9Knywy00P7abBDUT8uYIjl9jjxrvCHlNRXDSLpaIlqjLEIvowUqBzAXwOPWFzK/CnuFnHf1v3aeipQENsWhXN6v63wL63AleDQQTRQwp8WtGdNggTYzznO3tYcgts93OSx0pbo6mcfg7kOHWf85XOjoqjVqDWoo/+7psYHuVEEinDW5bCGUvecZzGzcChaja8Wps6DOXMKUw== X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230040)(1800799024)(82310400026)(36860700016)(376014)(7416014)(23010399003)(10067099003)(56012099006)(6133799003)(22082099003)(18002099003)(5023799004)(11063799006)(13003099007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: WnEVQYsl/r0CbbaQjQPNb1sMozS1zaTMvaN7XcQ5W/y86Hg4G+Rj9O+VLFKTaOG1U/I1NI+DHLeKDnuJx98lA9UP24J/aKR4bI4NYROj5unac87CY0gMvJACglZx5wjcJnRoXYHfx78Eq1QMOc3DKXc7O/eaZF9vJlnBa7L4qMUbak9dtiBWL1mzpSaMJLYOpQZfGFIfdzL+WJqotKiHhIP/GrS2kzKjqsGpgQJIn1Dvr0B0NqGDIvog0TQnH9pjQW3zLi8L+GzVzuMA9VNk8761OpHo9bGHL6RVI1cGUVUGybIcZNdzJnN0sRWBpsCthxg7M8QCzsRqK9UcF5knlUfx4JAZt0AFYS462jsWN86QTivsA6qUw23uouJu3wpMok8MnJP5WjRAohaqqvXTOEDYai0e16EXB0Rtuk13mS/2Qx7/BJx9Y+K9U6jtkivD X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Sep 2026 19:07:53.9176 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 018a6122-499d-451f-3673-08df1a6f22fd X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN2PEPF0000449E.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4150 From: Peter Colberg 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. Implement the PF and VF as ordinary PCI drivers. Publish pinned PF data with a mutex-protected counter inline in the PF driver data, then borrow and invoke it explicitly from VF probe. Register the VF driver first so it is ready before the PF can enable VFs. Link: https://www.qemu.org/docs/master/system/devices/igb.html Signed-off-by: Peter Colberg Co-developed-by: Zhi Wang Signed-off-by: Zhi Wang --- MAINTAINERS | 1 + samples/rust/Kconfig | 11 ++ samples/rust/Makefile | 1 + samples/rust/rust_driver_sriov.rs | 241 ++++++++++++++++++++++++++++++ 4 files changed, 254 insertions(+) create mode 100644 samples/rust/rust_driver_sriov.rs diff --git a/MAINTAINERS b/MAINTAINERS index f7a513c4ba24..da6c34bc3dcd 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -21129,6 +21129,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 31d62533ef25..737670fd68f8 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 b986b681cde5..238a11d5ec39 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 000000000000..c5fcf4a0c62e --- /dev/null +++ b/samples/rust/rust_driver_sriov.rs @@ -0,0 +1,241 @@ +// 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::{ + Bound, + Core, // + }, + driver, + new_mutex, + pci, + prelude::*, + sync::{ + aref::ARef, + Mutex, // + }, + types::CovariantForLt, + InPlaceModule, // +}; + +const PF_DRIVER_NAME: &CStr =3D c"rust_driver_sriov_pf"; +const VF_DRIVER_NAME: &CStr =3D c"rust_driver_sriov_vf"; + +struct SamplePfDriver; +struct SampleVfDriver; + +#[pin_data] +struct PfApi<'bound> { + pdev: &'bound pci::Device, + #[pin] + requests: Mutex, +} + +type PfApiForLt =3D CovariantForLt!(PfApi<'_>); + +impl PfApi<'_> { + fn submit(self: Pin<&Self>, vf: &pci::Device) -> Result { + let mut requests =3D self.requests.lock(); + let request =3D (*requests).checked_add(1).ok_or(EOVERFLOW)?; + *requests =3D request; + drop(requests); + + dev_info!( + self.pdev, + "Handle PF request {} from VF devfn {:#x}.\n", + request, + vf.dev_id() + ); + + Ok(request) + } +} + +#[pin_data(PinnedDrop)] +struct PfDriverData<'bound> { + // Keep the device alive until the registration stops exposing `PfApi:= :pdev`. + #[pin] + _registration: pci::VfRegistration<'bound, PfApiForLt>, + pdev: ARef, +} + +#[pin_data(PinnedDrop)] +struct VfDriverData { + pdev: ARef, +} + +kernel::pci_device_table!( + PF_TABLE, + ::IdInfo, + [( + // E1000_DEV_ID_82576 + pci::DeviceId::from_id(pci::Vendor::INTEL, 0x10c9), + () + )] +); + +kernel::pci_device_table!( + VF_TABLE, + ::IdInfo, + [( + // E1000_DEV_ID_82576_VF + pci::DeviceId::from_id(pci::Vendor::INTEL, 0x10ca), + () + )] +); + +#[vtable] +impl pci::Driver for SamplePfDriver { + type IdInfo =3D (); + type Data<'bound> =3D PfDriverData<'bound>; + + const ID_TABLE: pci::IdTable =3D &PF_TABLE; + + fn probe<'bound>( + pdev: &'bound pci::Device>, + _info: Option<&'bound Self::IdInfo>, + ) -> impl PinInit, Error> + 'bound { + pin_init::pin_init_scope(move || { + dev_info!( + pdev, + "Probe Rust SR-IOV PF sample (PCI ID: {}, 0x{:x}).\n", + pdev.vendor_id(), + pdev.device_id() + ); + + pdev.enable_device_mem()?; + pdev.set_master(); + + Ok(try_pin_init!(PfDriverData { + // SAFETY: + // - probe has exclusive access to this PF before SR-IOV i= s enabled; + // - the registration is pinned in the PF driver data and = dropped before `pdev`; + // - no other registration is created for this PF; and + // - VFs are enabled only after probe by `sriov_configure`. + _registration <- unsafe { + pci::VfRegistration::new( + pdev, + try_pin_init!(PfApi { + pdev, + requests <- new_mutex!(0), + }), + ) + }, + pdev: pdev.into(), + })) + }) + } + + fn sriov_configure<'bound>( + dev: &'bound pci::Device>, + this: Pin<&Self::Data<'bound>>, + nr_virtfn: i32, + ) -> Result { + if nr_virtfn =3D=3D 0 { + dev_info!( + this.pdev, + "Disable SR-IOV (PCI ID: {}, 0x{:x}).\n", + this.pdev.vendor_id(), + this.pdev.device_id() + ); + dev.disable_sriov(); + } else { + dev_info!( + this.pdev, + "Enable SR-IOV (PCI ID: {}, 0x{:x}).\n", + this.pdev.vendor_id(), + this.pdev.device_id() + ); + dev.enable_sriov(nr_virtfn)?; + } + + assert_eq!(dev.num_vf(), nr_virtfn); + Ok(nr_virtfn) + } +} + +#[vtable] +impl pci::Driver for SampleVfDriver { + type IdInfo =3D (); + type Data<'bound> =3D VfDriverData; + + const ID_TABLE: pci::IdTable =3D &VF_TABLE; + + fn probe<'bound>( + pdev: &'bound pci::Device>, + _info: Option<&'bound Self::IdInfo>, + ) -> impl PinInit, Error> + 'bound { + pin_init::pin_init_scope(move || { + dev_info!( + pdev, + "Probe Rust SR-IOV VF sample (PCI ID: {}, 0x{:x}).\n", + pdev.vendor_id(), + pdev.device_id() + ); + + let pdev_bound: &'bound pci::Device =3D pdev; + let pf_api =3D pdev_bound.vf_registration_data::()= ?; + + pdev.enable_device_mem()?; + pdev.set_master(); + + let request =3D pf_api.submit(pdev)?; + dev_info!(pdev, "Submitted request {} through PF data.\n", req= uest); + + Ok(try_pin_init!(VfDriverData { pdev: pdev.into() })) + }) + } +} + +#[pinned_drop] +impl PinnedDrop for PfDriverData<'_> { + fn drop(self: Pin<&mut Self>) { + dev_info!(self.pdev, "Remove Rust SR-IOV PF sample.\n"); + } +} + +#[pinned_drop] +impl PinnedDrop for VfDriverData { + fn drop(self: Pin<&mut Self>) { + dev_info!(self.pdev, "Remove Rust SR-IOV VF sample.\n"); + } +} + +#[pin_data] +struct SampleModule { + // Keep the VF driver registered while PF removal tears down its VFs. + #[pin] + _pf: driver::Registration>, + #[pin] + _vf: driver::Registration>, +} + +impl InPlaceModule for SampleModule { + fn init(module: &'static ThisModule) -> impl PinInit { + try_pin_init!(Self { + // The VF driver must be ready before the PF can enable VFs. + _vf <- driver::Registration::new(VF_DRIVER_NAME, module), + _pf <- driver::Registration::new(PF_DRIVER_NAME, module), + }) + } +} + +module! { + type: SampleModule, + name: "rust_driver_sriov", + authors: ["Peter Colberg"], + description: "Rust SR-IOV driver", + license: "GPL v2", +} --=20 2.53.0