From nobody Thu Apr 2 01:54:50 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D78483BED40 for ; Tue, 3 Mar 2026 21:15:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772572545; cv=none; b=qD2639b6+w9OrRtEU8XQXdIET+ye87beNovKYsUFUZbkbrbkEW4UawdvLo445+HQr8ropGmuXuUwlxWXLUR+5rCfEVMp0rrtGW1yMLspX8M+U8gMG+nIc19HjnYMWbtZS3AKmgcioTFDsa42rOl4d53+h0TBZhl916lnuijwxRc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772572545; c=relaxed/simple; bh=degmGH7wPwx8NSuUy3FBbvJDakmae95tdtBgxpe2RUg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oqwYL5SBCBK3/godmdL3/YFNmTI/1n8HN8G6SwKvVP/kAAi/esvVbYpBrJ/o/rO4j4Mgf2Q81j75lTGDMKMbUfRkSwN1ExxHS2vpi2QdE+YUysyV4mWCZHL/ys2gPEVgPIJaF3H9utVjTEXCKLj9w/OHzBuYRLt5CUVYDNYK4dk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=hk/Bq9AX; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=fKA3pGY5; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="hk/Bq9AX"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="fKA3pGY5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772572543; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dKSiyMf0ynPRuLtK//gTM+BGK6ixt+B/8DTHGEJv69g=; b=hk/Bq9AXNqatD2OoTsTgdgkc/4i0lXlHwhrJg/WhI6n1yrvuZLHW70GRiVXG5yW6f7DL7m sl99igEK9yfxbsuWYc1kmDMK7iX0/Lkii/Lfx8zfbcBFAfTCBHmfLdIcNlo38sx+62Rwlp fDI39qNaAY3NP7a3erhSWPIpdaiwCdk= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-283-IqjeN6vRMxqbX-cGuhc2Qw-1; Tue, 03 Mar 2026 16:15:40 -0500 X-MC-Unique: IqjeN6vRMxqbX-cGuhc2Qw-1 X-Mimecast-MFC-AGG-ID: IqjeN6vRMxqbX-cGuhc2Qw_1772572539 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-506b3fb32a1so929735191cf.0 for ; Tue, 03 Mar 2026 13:15:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1772572539; x=1773177339; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=dKSiyMf0ynPRuLtK//gTM+BGK6ixt+B/8DTHGEJv69g=; b=fKA3pGY5MsDcul/N6psTEi/A9rBUoHj5oPEMPx8tGSJGL2Ulbzyitd2T+Y/hlgYs1S s5jlQft7U0tyeoz11d2MrekvYoohJ0eiXQOCNpeYa0vGucRwlJSyIp6NbU4a1RCABhMc zLVUfQXlH7Q/MpKBcXbMWYDmzm1mNOYJ2/kbzL2fpLBOIhBoAJi3JgtUq9KHCE5UpQEd GdJA2IYTwUp7Nr7XowlfbkX+EjEuDxeFM1dPgjzQQtpT29T/D/3/SjBX7Nc5a9T9iRV7 Z4P+9jPuzxY0aJ5UcBq/e6bGcP+oPQbbkGj4BVtAYGWtKQ9ZZoyv4s0eDlfmJPI+lo03 5Clg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772572539; x=1773177339; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=dKSiyMf0ynPRuLtK//gTM+BGK6ixt+B/8DTHGEJv69g=; b=vO9Qp1bdH0P6tQUKxw4Xj257UQ1U7ScsGLBnCanvXNfSp5gNqm/P7mebcSd7LdbkL6 pFK24peiSAa1Vb80P1KjEI53YAqmFCJT8SDRLOOFW9Ex7UL7osvFAfAD5J/5FbJZFKOf /k5+43+z+yVaVLdqk6bRGe8jbI+BN3OqMNwWonk8mwwVD89KaPaNV8qF5ivhygTqZw/k Mkt7pyqbwO9IY+HXWK4Uv22DPGIzG7QmAqOMBgK9K0BFQzI5el1kkhuUyiLtFt9yGdQN mR4qFzNTk1e1FJKqDlMxNf7pmJiQqoca1LIGbcVyrzrXXA8l7jfF8eJ1/4L+Y4IdKaHu Cbwg== X-Forwarded-Encrypted: i=1; AJvYcCWq01tQmj+r5pFEs3mwrufz+oXDSNio8sUVmvJiIZNkMiM80ITZy29U1jzWvJiudAjhKPiL6LJKf9avJz4=@vger.kernel.org X-Gm-Message-State: AOJu0YytqUOMCEIw79/O5Wll+ZAiJUaRhVqiVOz6ylRlmv5DRLNyyMWr N7oCc6kcUcqx9gwnWgH3LXxtakbEwjihPutaUXiJDd2ndWPoF1wg/YvtT9/pLZS/LElqlWDX/NO DhuP0FHg/wT+0p4y+U1B5T7eONeGvO6ybEeviuxAVAl8VMekB5Xllg2xdzsb9QhB3FA== X-Gm-Gg: ATEYQzwt6Moqp8tzro7PRCQ+8HZqIgvD8fItQQVPxlmh/PE8ej8GJaXxZmg5K0Sj/UU wureNy9K7n40lRqZB15BCLNHZlJ0cNu5dqD8GJ/ovTBsuaVr0PnBSt66LxhHoSN2I//N2ZhMZRm rq4HjijHtqm9oYixvMH+fWHfAVs5XRBX7d2izFrpNb7uBMN29CXrcxs0SGSN95olRAvlZc9nAON omzU75hdH+2y8EgBQpnPs472eo+VGH0Bo/G/J1pFMYUXind9l6+0s1wZWCyd47nMbXWJ2O0OYff LhTgstEAD44naR4hKMJ/JPvL+m9+n4KR0Is2OzkpTPfn1UznxPe6A5OJCJHmkR0UdI40CAn3dx7 KWHb+EnWEDPMmBjqW7Fk3/CpN+Q== X-Received: by 2002:a05:622a:50b:b0:4f1:b9ec:f6a4 with SMTP id d75a77b69052e-507527b8798mr223872591cf.33.1772572539246; Tue, 03 Mar 2026 13:15:39 -0800 (PST) X-Received: by 2002:a05:622a:50b:b0:4f1:b9ec:f6a4 with SMTP id d75a77b69052e-507527b8798mr223871711cf.33.1772572538483; Tue, 03 Mar 2026 13:15:38 -0800 (PST) Received: from [172.16.1.8] ([2607:f2c0:b1e3:9a00:3c7:56c2:f819:96d2]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-5074481c0e5sm156286991cf.0.2026.03.03.13.15.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2026 13:15:38 -0800 (PST) From: Peter Colberg Date: Tue, 03 Mar 2026 16:15:23 -0500 Subject: [PATCH v3 03/10] rust: pci: add {enable,disable}_sriov(), to control SR-IOV capability Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260303-rust-pci-sriov-v3-3-4443c35f0c88@redhat.com> References: <20260303-rust-pci-sriov-v3-0-4443c35f0c88@redhat.com> In-Reply-To: <20260303-rust-pci-sriov-v3-0-4443c35f0c88@redhat.com> To: Danilo Krummrich , Bjorn Helgaas , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Abdiel Janulgue , Daniel Almeida , Robin Murphy , Greg Kroah-Hartman , Dave Ertman , Ira Weiny , Leon Romanovsky , David Airlie , Simona Vetter , Jonathan Corbet , Xu Yilun , Tom Rix , Moritz Fischer , "Rafael J. Wysocki" , Boqun Feng Cc: linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Courbot , Alistair Popple , Joel Fernandes , John Hubbard , Zhi Wang , nouveau@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-fpga@vger.kernel.org, driver-core@lists.linux.dev, Peter Colberg , Jason Gunthorpe X-Mailer: b4 0.14.2 Add 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. Set the flag managed_sriov to always disable SR-IOV when a Rust PCI driver is unbound from a PF device. 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 --- Changes in v2: - Set flag managed_sriov to disable SR-IOV on remove(). - Use to_result() to handle error in enable_sriov(). - Note Bound device context in SAFETY comments. --- rust/kernel/pci.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index af74ddff6114db3c2ce8e228c5a953cd0769e8a5..e1cab1574a3d309d25bf5267c0b= 0d8da8fb66d44 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -82,6 +82,7 @@ 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(); + (*pdrv.get()).managed_sriov =3D true; } =20 // SAFETY: `pdrv` is guaranteed to be a valid `DriverType`. @@ -458,6 +459,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