From nobody Tue Dec 2 02:29:52 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.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 F0B2030DEAE for ; Wed, 19 Nov 2025 22:19:28 +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=1763590771; cv=none; b=e3/rOZl9glxuUpKPF4Zp5vZec3Idt2DdiAI8wwOCCHX46YJ8OFeBLHYBLPmD9626/hYytI2IINHlhQbW84ckDYetT0OdBeMxcou8jJBVBdqSi2tJ2z/k9J0ZKoe/9tjWs110jDXFAFbqF2S6gD8Fb4GoRBQc6JONAvGZB3YQU1A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763590771; c=relaxed/simple; bh=ePuew1JCM1Hs1e6ZIWmT70f+aea4BDiTcwCKTX9QAMU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dzdijjKNY9c2e2QfzA07yltSfmqrfOVIsvGYlprLQ88fw3mfQjJh1sEARwVvtU6xurOQrz8JeiviVNbRIxT4x+/fDwfVKRPHXWFRSSvcnVQBDdHRxz0aVKEP+IV7WPh+8lXfSvnPr2vMrafp6fSB4haZFmjb1d1T383RjCYlG1Q= 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=YypFePJk; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=OoUWQt7H; 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="YypFePJk"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="OoUWQt7H" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763590767; 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=sRACUSt64wUiVLTpoLMSIYgwAJ0MY3voBS0pEwO7IGQ=; b=YypFePJkq/uwUBvZMzVD9DgjdhRJkuxAnMZjmlo78XbChw2Byvbo0GeYmFIPGmIVATFZ/v eMvJUviaZIs/iNcb9/EboQQlzJHsVPkKPP2jtbXj/ldgwDnoVdmOm+ZbuvCAUwdw7Xfvpm oXZ8fdowDAXHtmStxI7YmgflPI+isUw= 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-401-9kn7GQtgOH-QZfoWEilViw-1; Wed, 19 Nov 2025 17:19:26 -0500 X-MC-Unique: 9kn7GQtgOH-QZfoWEilViw-1 X-Mimecast-MFC-AGG-ID: 9kn7GQtgOH-QZfoWEilViw_1763590765 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-8b24383b680so95335285a.0 for ; Wed, 19 Nov 2025 14:19:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1763590765; x=1764195565; 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=sRACUSt64wUiVLTpoLMSIYgwAJ0MY3voBS0pEwO7IGQ=; b=OoUWQt7H5tRpHDkaqkcCHnEC+CcTyl6JiFC43SAu4W1Zh4rbVgN+Cqsw/736fW/OH8 F7QAZ2bn1GSYMGA0hq+4NWbMoih1ZKKaD1lErPJ9mykwO2c8JdjuDNyGPnhclKq0Kngc ipXmyXSc6KODqvGeUmoZ1QEFMq0wYSO1y9m4z8kaplpGjYEHS1E4jd7vYwoBXy9CThO7 mfH3wgBYWMS5qtPRzcwON+NSABolHhUEyl+daraz62SXHUaiPsu83tTk7Tw30WQ47/2W Vhjp2Tm+cZC5AsF7h9zrUnuFODN0WBQYWSa0wwyqGXYnwguUVY5U7Pv/NksZkYEsLRMH 95iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763590765; x=1764195565; 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=sRACUSt64wUiVLTpoLMSIYgwAJ0MY3voBS0pEwO7IGQ=; b=Tds4a7LSDazwqYErOcavGFxXPfKuCKRyMhxQJ+9v74yF5f/GV6O0oeWiuL7uwQawO6 zmZ64DoKXKaIuVzicbIqaawcI0B8hq83f7lCZJfGwPo4MTGcxZIsYuVCi246EU8HpBG7 2M6B8zRGuzlnq8UVinRvVt1v25b5QzdpVV3Ike1Y0vWM/830cqrQJCRk5sLIVX5CDut1 ZKHvZzdIFdDKovr/DcqVSqh1UdQXz44YgRi6a9Dlp8wj1oeOGI82WEpoYAoH83k9g3+M QEOd0YZ8/Vs1h/m+Z9DhZ+TSYY3Qqi6fB2sl/yUXG/SU67Hwb1DbCMgnOdaprwebBJKD jxFw== X-Forwarded-Encrypted: i=1; AJvYcCWFXCTtHwohPJ/YMD6fPim7hd0gjiTJaigdkiqK3hbHJmLVeQb5Ts2o3S8Bwf4/MX8vvsAaAtYidJBYxBo=@vger.kernel.org X-Gm-Message-State: AOJu0YyCuOrMDbBFOvbfciaiz1RMZevqSwPnFtwENh86103CQuQRFVpJ bAJEQ3gOcNkVxpHr/93xhwfz2iVVZ4+/lkg9o9/b20GEUWY8MdakiBJ21j0tESAi/YDyBH++JkK +NXdOm+0IazgtJm+afJbKB0yh9AoOjirdbevoUq5lsxwU3aYIFMjlC0kN54k+hfyH4A== X-Gm-Gg: ASbGncuVahahJXF1t2EBQBaJ6HHhqbbpUm8nx6ZOI87/Oz0gyr8lPcgvWd17UTpJMwH B4mrdJmeKrCa7a29Dpc2Jp4oouPWX9WwpB3m7r+vD2uwl4pUGV/9oiUD6XKVexckJKoXGtVkvRQ N0JuL8ckDRdH5LgaDJHghr9n78xIxl+SPIOuSOPjr1tpDclpjFsZ1hAnR76RJVjh0hbH6hABLvT hjLi1G5mthiG4oGA9dRo1GHNafztFGchXj82LYzoQgLXk4h7X2TsBkFbJ3Wr7+9NwFIUHX9H2Ti 6xm4tdJ26seZitiBwvaxJ1Qw68E6lKefVylZQAYnW3ZeA6witIVKcZVdMwmHD3rKKhObxVjV4OW pHM63CDb8UThKrQ== X-Received: by 2002:a05:620a:c54:b0:8b2:9765:3a69 with SMTP id af79cd13be357-8b32747f96amr159065285a.68.1763590765328; Wed, 19 Nov 2025 14:19:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IHPsHmN0cQPL5HT5GsktthJovUEqLhZHDaj3qO7Z3zvSReIfEaHAgbYWjYECBY/fX2jHQY1EQ== X-Received: by 2002:a05:620a:c54:b0:8b2:9765:3a69 with SMTP id af79cd13be357-8b32747f96amr159058585a.68.1763590764685; Wed, 19 Nov 2025 14:19:24 -0800 (PST) Received: from [172.16.1.8] ([2607:f2c0:b141:ac00:ca1:dc8c:d6d0:7e87]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8846e447304sm4426866d6.4.2025.11.19.14.19.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Nov 2025 14:19:24 -0800 (PST) From: Peter Colberg Date: Wed, 19 Nov 2025 17:19:10 -0500 Subject: [PATCH 6/8] 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: <20251119-rust-pci-sriov-v1-6-883a94599a97@redhat.com> References: <20251119-rust-pci-sriov-v1-0-883a94599a97@redhat.com> In-Reply-To: <20251119-rust-pci-sriov-v1-0-883a94599a97@redhat.com> To: Danilo Krummrich , Bjorn Helgaas , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Abdiel Janulgue , Daniel Almeida , Robin Murphy , Greg Kroah-Hartman , Dave Ertman , Ira Weiny , Leon Romanovsky Cc: linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alexandre Courbot , Alistair Popple , Joel Fernandes , John Hubbard , Zhi Wang , Peter Colberg , Jason Gunthorpe X-Mailer: b4 0.14.2 Add 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 | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 24dda12f6402098a1a323f3b5aae884201b26d89..f9054c52a3bdff2c42273366a40= 58d943ee5fd76 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -66,6 +66,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 `RegType`. @@ -118,6 +122,20 @@ extern "C" fn remove_callback(pdev: *mut bindings::pci= _dev) { =20 T::unbind(pdev, data.as_ref()); } + + #[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. @@ -307,6 +325,39 @@ 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. + /// + /// 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.51.1