From nobody Mon Feb 9 21:22:11 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89A38332EDD for ; Thu, 5 Feb 2026 21:00:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770325236; cv=none; b=XmnwDKUOyCus2+nXUND9nGKrZUh1qRWWJ1bES5L5J+xNkfaLI8Ol+nGf+nf9apAc2qhB9TBHUfH6f62RSSG0kHUqB+/Ro3yOMKXWs7ylSr+rguUjsLRMg8XWn3tNBeXQoiFczCVL+P+JBPVKZbS3s9Hz6V/NRiAdeD1kba+SqqE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770325236; c=relaxed/simple; bh=mGjHnViOpaBiawwKiFLL50tHLKnItFMnpZghT+ZkbZU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=axuTIYiZrD3xde40G9GHdtcQhmj7lJqGvCaShJ70QbhWztNyQA4OuBtWDox2aeHTKMtz6r6EZ0Mvx/4oxs+rEo74pTscueQhGcMj+Sq7dk5H/X7llH+dKggIePQ2NuRbtGy6Jro/Ojrz1MoRAGoRhdP5lLZPj3AvZOPFTatyzh0= 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=BkR6J3qx; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=NyH4KrWd; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="BkR6J3qx"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="NyH4KrWd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1770325235; 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=uO9Iu/KQjA0pe5Uf917htRnrSxjGLQG0IESxKRcUlqo=; b=BkR6J3qxMh/xW/fYGRiRqIKN6RxaOCkv+xwYzN2Z1uTge58PmFwQHOR6Gv8v6Eym1mGTB2 DwKN2yI8T+wj9Du0l8KIWMGNA8v8u6UAbLXPnFsBAr4Oet/zrpDofqQ+zM+TsjfBVTLqHL okDU7i0MmY43yoCJ93N2ENZcqs4NsTc= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-434-0EHZsyjJNU6pyON6z_rAbQ-1; Thu, 05 Feb 2026 16:00:34 -0500 X-MC-Unique: 0EHZsyjJNU6pyON6z_rAbQ-1 X-Mimecast-MFC-AGG-ID: 0EHZsyjJNU6pyON6z_rAbQ_1770325234 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-8c7166a4643so399880185a.0 for ; Thu, 05 Feb 2026 13:00:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1770325234; x=1770930034; 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=uO9Iu/KQjA0pe5Uf917htRnrSxjGLQG0IESxKRcUlqo=; b=NyH4KrWd6cNBomc8qRF6hx6sAp1857MB7L0Ph/btt9d++Nxjm1FeiL8WsvTLTCUWcH uXzcYcylzL/QJDuTFBx0tO3Sk8PM++945NqeW1j5NG8IDVXM2rlTCwEBR0LITTlOF+f6 a4QzmGV4gY2wTSMmAf5P55/+sLvEdIl7q9WsqjUJU5OkeV//n6C+rYilwASX7sx6DuyC zo/47iuc5EJzXjkAWEczYQBPXXnpQqyjY8nm9V3vKUVzqJ8+L58hDooDq3JHH7zF2OsX R0lIjhiyg6CQvb1Adyuft8MPNTN45z5SZj0NOi7Molf7Qb1VPrXzNGgf3uwUmVo8W3H8 LCyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770325234; x=1770930034; 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=uO9Iu/KQjA0pe5Uf917htRnrSxjGLQG0IESxKRcUlqo=; b=Gs60kVor7xuIQcZh1Sw43m1ZSW/STvioKrxEAoKdsEluff9m9OcaQVV+MLlp8wkLKd P0Pyf7O8Gzu7ZxCsyCtAg18cImg6ZK+SgiTBqB7lzXFytxZ8vXIhziuME0yTynYj9aP/ UrVCUJhIUrdepy2uLCC3R+5WSuj1dItv1boJ98yLj54ditM0I8SrrLpqQdHNWLhgPdJw OSnEji4ic0h275Klhb57lT4wLUTuISndKnVvrBQ4/N3ASpKe23Fo3u8yHCPf1UkUrcFA Km2h7EjgrAtELWfSr/lQv6UBykP6Lxlddh7mfkMOpEaMPp5V9HMnbyba3ysDqNjeIcqM Jvfg== X-Forwarded-Encrypted: i=1; AJvYcCVcxbZaN3mt4fd60nosy6CkMeQAHJLDs6yHu+JxQIHAf0DsBNcxX5mIRHHveMjcpD3PQEK+egAoqZzSijw=@vger.kernel.org X-Gm-Message-State: AOJu0Yy2BfXm1R96ZHxJC9ja1eQgZxhl/6BdDnUtyxIcDTsxIoxDTUPm w5kcENIcXDFEUsec9Jpojt22amu9ZajQCyUNlCoUL8Y5Ncfqwr3Q/ZLvr/CfW2Yh4LEeLonTGSP wLv4/LtWrrp+e+Zx5g9xCix1aW88HfyS+c0Oy7werkiFCuuUD8nuzk/3aG2scBTEETQ== X-Gm-Gg: AZuq6aKD+4WwjpCz6QPTZzLMsAWWTxRFdXWd9HeS5hn/lqavn+K29p6q9hiPLV+AQAo 9zFRkc7iEwouC5awZdr5Qwxe7l2KkZiNnSI2bXBgtbdCRAtdJhGghRdt06hCVGOTEyQ1g/kY/5s S5bcw5PDQ3/XYXZVrMKqLVtJV0lb8PYI2iKIQH9aLfQXjKfJ3HxBr4SO8zm3h/LLtAXGe9+Eql6 4w0LcO2Omrjum8M/bd6GuHWXRSK48y8TNpJwv1W9sEA9b/jJfK8QWqGlu5bQTaGdLUs+gnPfqr9 GsiXPIEFa4sy8aB+Fuq1Fy7rQRLiFg/6dUd3g6w3Y7a6FAwvTTgcYxek5kEXUrOGCpWAuKCu2RG EPcpulVekxbaP5JQ= X-Received: by 2002:a05:620a:bc9:b0:8b2:f35c:16df with SMTP id af79cd13be357-8caf1bc2af6mr57816285a.79.1770325232255; Thu, 05 Feb 2026 13:00:32 -0800 (PST) X-Received: by 2002:a05:620a:bc9:b0:8b2:f35c:16df with SMTP id af79cd13be357-8caf1bc2af6mr57795985a.79.1770325230865; Thu, 05 Feb 2026 13:00:30 -0800 (PST) Received: from [172.16.1.8] ([2607:f2c0:b010:9000:4c85:f148:4c91:943a]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8caf9fdf692sm17571285a.44.2026.02.05.13.00.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Feb 2026 13:00:30 -0800 (PST) From: Peter Colberg Date: Thu, 05 Feb 2026 15:59:52 -0500 Subject: [PATCH v2 05/10] rust: pci: add bus callback sriov_configure(), to control SR-IOV from sysfs Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260205-rust-pci-sriov-v2-5-ef9400c7767b@redhat.com> References: <20260205-rust-pci-sriov-v2-0-ef9400c7767b@redhat.com> In-Reply-To: <20260205-rust-pci-sriov-v2-0-ef9400c7767b@redhat.com> To: Danilo Krummrich , Bjorn Helgaas , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Abdiel Janulgue , Daniel Almeida , Robin Murphy , Greg Kroah-Hartman , Dave Ertman , Ira Weiny , Leon Romanovsky , David Airlie , Simona Vetter , Jonathan Corbet , Xu Yilun , Tom Rix , Moritz Fischer , "Rafael J. Wysocki" Cc: linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Courbot , Alistair Popple , Joel Fernandes , John Hubbard , Zhi Wang , nouveau@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-doc@vger.kernel.org, linux-fpga@vger.kernel.org, driver-core@lists.linux.dev, Peter Colberg , Jason Gunthorpe X-Mailer: b4 0.14.2 Add 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. Suggested-by: Danilo Krummrich Signed-off-by: Peter Colberg --- rust/kernel/pci.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 56 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index a4c27c674bd8bdf5e3316789d38d566e90b93fe2..88bd114970431bf8c3edef94c1d= 48567d895eaf6 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -83,6 +83,10 @@ unsafe fn register( (*pdrv.get()).remove =3D Some(Self::remove_callback); (*pdrv.get()).id_table =3D T::ID_TABLE.as_ptr(); (*pdrv.get()).managed_sriov =3D true; + #[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,6 +139,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. @@ -325,6 +343,44 @@ pub trait Driver: Send { fn unbind(dev: &Device, this: Pin<&Self>) { 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) -> Res= ult { + /// 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) -> Resu= lt { + let _ =3D (dev, nr_virtfn); + build_error!(crate::error::VTABLE_DEFAULT_ERROR) + } } =20 /// The PCI device representation. --=20 2.52.0