From nobody Thu Apr 2 00:08:13 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 7DF5238C2BC for ; Tue, 3 Mar 2026 21:15:37 +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=1772572539; cv=none; b=upN+1yK6wV+SNTGuIpMTCTTQtoCLA5/xgwMXH7O6E4uz8vPRmg/83QEvWHfQ2HI3mVQxjrIsFuL9g1/mA9f3br4EfOUUVXtGyBOVpjCby6zbcj/J/lRXAQs6wMZikKe8J4PhnCBR9rrPtnBEZyVGRelSdePKkb7OlHh0cUpjlSc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772572539; c=relaxed/simple; bh=ax5r1OmCHwEBpaIwBxT5kM3++FSx0BGrf1GkfVvAo60=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Nafe7+kjb3tIE6ef0vtHJ4Y8H/WLdgnrG0wzO3zJel4ZCRRQScko19+YVOBFhxHjchAt5qC6m7Qg9WE9puS+0cqnBJsKwILhLuLkyzLUKs9BwPThvTLiikXC57RPNyf6g7o3yUpBA9BUlF/q/UC+FcqACxuk1p2SuJg8igFxVMk= 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=ZQnk34Oe; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=PeEwA98N; 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="ZQnk34Oe"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="PeEwA98N" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772572536; 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=0eBeiZqE9TXbxZv4w/1cPbOmShRF2lMY9/B+OMDvXLw=; b=ZQnk34Oec2sKe1yWNMaiu969Ky+LleJ+jxvykN1He/Tb/+vEikxfudKai7z/0XIAtre+z6 G+AtPvkxFMJunBaMKMkSr2yr8xsKCjHJbbBrJdkmAmLsSzH1xbw12oXqYvgJY0BoQYityZ 0CrmRWsR6OuhnJ6e9Hn1qy8/B+FnsIw= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-616-ULsCxCPHO-29TC20Fd4Z-A-1; Tue, 03 Mar 2026 16:15:35 -0500 X-MC-Unique: ULsCxCPHO-29TC20Fd4Z-A-1 X-Mimecast-MFC-AGG-ID: ULsCxCPHO-29TC20Fd4Z-A_1772572534 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-506a936d7afso646743891cf.3 for ; Tue, 03 Mar 2026 13:15:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1772572534; x=1773177334; 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=0eBeiZqE9TXbxZv4w/1cPbOmShRF2lMY9/B+OMDvXLw=; b=PeEwA98NBIn4s+Qxxwt/N2rAyTcDdnE73fnBttGH6L/6q5mSeDiaa//hfBP6OzfKkM UHfgX1zGVdHKwWjMhNwmu6AgGOFYYq7V4SgYDEyAUaVuQyjX+Oaz3ouzLERDN9xAaYBR E389rm7ni2SgMpNz68n5aognENtXYBg9ouapAFxM/e4RDwDc7a9LX+8j8FV6Cff5DnbM qQoq4KgOylFYo55kGXno7bSz8tcvRoqUhhDw32377b0JQVOQbmfwDhnTzGJFuHXMa5rI bw0VZAZJ6+NIMNePczAR07URhIINgt4yBeaGMhzU7GsYnZQYznfur4pY9pW1OdlDNurX creA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772572534; x=1773177334; 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=0eBeiZqE9TXbxZv4w/1cPbOmShRF2lMY9/B+OMDvXLw=; b=nWxepBpaSSLG6ONxZlFvSugnq7xXG+Mj/dJKaeQ6ErOOYxoZtiS6Yw4NJM8/960fqo A8w24tKidhAfVyWDwx3b/qSAxVCjudAjjVk6duhPEykwteRhECODyODUh1KBfRiCg4g3 ypbN3ITtjfSxEt9S2T3hYhHovL0P9/M3Qxllhq9HrBMHbdkVyMIsKb2kZWef7B/yphem 2Jo8kpwFH9IgtFqbHIhDn1UwVoepe+visdGBvrgSYrTFFW07X6H6XpnVTWnMSKVXtTGD kcIctsBHIHTOjdXwaeuzGL297bBAuqof6lXqtdHeVNrCB3gBZaaonYcV1UDDuvX1q9w2 4pMA== X-Forwarded-Encrypted: i=1; AJvYcCXu1xUQzkORRdh28FnIFhNaMcsiTMjPppczBi/GzLmtHrUUchjTRdtZ+NPoK7quZayAa5pKfS7W6vYse0c=@vger.kernel.org X-Gm-Message-State: AOJu0YyA531sKAJ1c1KjkBjH1uAMdDFMy61bw4HjnFLVYLxJblgYCw3r vdi039bvn5v27OnqWY7njPNaHOC22xUKNpvy7QlJy6cQvkuoM5T57L0YeN3SeXNIay20DJxTXBk OiTqc5aRxtr+DOHEvw6azLmYMUQrqpNS72gdmLiPm0eR1xZA1JWgR7WTcYyoCrNFrlw== X-Gm-Gg: ATEYQzyRMQwJ09TT1yjHWUBLvsH7OTuJyxSyayczjebdJzgetYQgwC09Z12+mkYwRST Zmn5SAfaSz/RK1gkA4jSJwRI2qfiaXSNm5pWhCCkP9A04bqXBAA7D12mqXaA2nVmxkfDsPtt7Bs wq0zwFnrA+ir+eHTrcB4fvcLAC+zombetVzAeZ4GCa4KrR9ujlTk33EgNV9YkGbgcuGBB73SBUU 8pW1kWtPELvxeO3C2t1mOnCMWRF/54l3heAg5gflGM+3iXR7K37o2m69+OCi42E5OFm5tgPXiSS LQiq0YSGtUm2/NacSZfYX/4a2/B/k6ylX3HySmXsu8UAeRWgNVMF+x7M7N0TBwxWDRDVgX9FkNu imdILRJy7udqSv3FDWbx7JDBKHQ== X-Received: by 2002:ac8:5f8f:0:b0:506:a1a8:c6fb with SMTP id d75a77b69052e-507528765a2mr213886871cf.2.1772572534186; Tue, 03 Mar 2026 13:15:34 -0800 (PST) X-Received: by 2002:ac8:5f8f:0:b0:506:a1a8:c6fb with SMTP id d75a77b69052e-507528765a2mr213886271cf.2.1772572533578; Tue, 03 Mar 2026 13:15:33 -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.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2026 13:15:33 -0800 (PST) From: Peter Colberg Date: Tue, 03 Mar 2026 16:15:21 -0500 Subject: [PATCH v3 01/10] PCI: add driver flag to opt into disabling SR-IOV on remove() 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-1-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 a flag managed_sriov to the pci_driver structure that allows a driver to opt into disabling the Single Root I/O Virtualization (SR-IOV) capability of the device when the driver is unbound. Add a new function pci_iov_disable() that is invoked before the remove() callback of a PCI driver and checks for the presence of the new flag. If the flag is set, invoke the sriov_configure() callback to allow the driver to gracefully disable SR-IOV. Warn if the driver fails to do so and forcibly disable SR-IOV using sriov_disable(). Since a (broken) driver may theoretically re-enable SR-IOV during its remove() callback, extend pci_iov_remove() to forcibly disable SR-IOV after remove() if needed and only if the flag managed_sriov is set. Altogether the flag ensures that when a Virtual Function (VF) is bound to a driver, the corresponding Physical Function (PF) is bound to a driver, too, since the VF devices are destroyed when the PF driver is unbound. This guarantee 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 device private data of the PF device. Suggested-by: Danilo Krummrich Signed-off-by: Peter Colberg --- Changes in v2: - Move logic to disable SR-IOV on remove() from Rust to C. - Add driver flag managed_sriov to opt into disabling SR-IOV on remove(). --- drivers/pci/iov.c | 41 ++++++++++++++++++++++++++++++++++++++++- drivers/pci/pci-driver.c | 3 ++- drivers/pci/pci.h | 2 ++ include/linux/pci.h | 8 ++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c index 91ac4e37ecb9c0c5265aa40c235e84b430f43a96..da64d6ce5d30de8a52089b36fcb= 013937cf8b6fe 100644 --- a/drivers/pci/iov.c +++ b/drivers/pci/iov.c @@ -1010,20 +1010,59 @@ void pci_iov_release(struct pci_dev *dev) sriov_release(dev); } =20 +/** + * pci_iov_disable - disable SR-IOV before PF driver is detached + * @dev: the PCI device + * + * Invoke sriov_configure() callback to allow the driver to gracefully dis= able + * SR-IOV. Warn if the driver fails to do so and forcibly disable SR-IOV. + */ +void pci_iov_disable(struct pci_dev *dev) +{ + struct pci_driver *drv =3D dev->driver; + struct pci_sriov *iov =3D dev->sriov; + + if (WARN_ON(!drv)) + return; + + if (!dev->is_physfn || !iov->num_VFs || !drv->managed_sriov) + return; + + if (!drv->sriov_configure) { + sriov_disable(dev); + return; + } + + drv->sriov_configure(dev, 0); + + if (WARN_ON(iov->num_VFs)) + sriov_disable(dev); +} + /** * pci_iov_remove - clean up SR-IOV state after PF driver is detached * @dev: the PCI device */ void pci_iov_remove(struct pci_dev *dev) { + struct pci_driver *drv =3D dev->driver; struct pci_sriov *iov =3D dev->sriov; =20 + if (WARN_ON(!drv)) + return; + if (!dev->is_physfn) return; =20 iov->driver_max_VFs =3D iov->total_VFs; - if (iov->num_VFs) + + if (iov->num_VFs && !drv->managed_sriov) { pci_warn(dev, "driver left SR-IOV enabled after remove\n"); + return; + } + + if (WARN_ON(iov->num_VFs)) + sriov_disable(dev); } =20 /** diff --git a/drivers/pci/pci-driver.c b/drivers/pci/pci-driver.c index dd9075403987d84e068014b35745e8872e93fdae..3fe43711565a3eb61a06cc3700e= 5ca953961fbe9 100644 --- a/drivers/pci/pci-driver.c +++ b/drivers/pci/pci-driver.c @@ -491,6 +491,7 @@ static void pci_device_remove(struct device *dev) struct pci_dev *pci_dev =3D to_pci_dev(dev); struct pci_driver *drv =3D pci_dev->driver; =20 + pci_iov_disable(pci_dev); if (drv->remove) { pm_runtime_get_sync(dev); /* @@ -504,8 +505,8 @@ static void pci_device_remove(struct device *dev) pm_runtime_put_noidle(dev); } pcibios_free_irq(pci_dev); - pci_dev->driver =3D NULL; pci_iov_remove(pci_dev); + pci_dev->driver =3D NULL; =20 /* Undo the runtime PM settings in local_pci_probe() */ pm_runtime_put_sync(dev); diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 13d998fbacce6698514d92500dfea03cc562cdc2..66308f5126ff9e4bebb537a541f= 1dd8717bccbfa 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -943,6 +943,7 @@ static inline void pci_restore_pasid_state(struct pci_d= ev *pdev) { } #ifdef CONFIG_PCI_IOV int pci_iov_init(struct pci_dev *dev); void pci_iov_release(struct pci_dev *dev); +void pci_iov_disable(struct pci_dev *dev); void pci_iov_remove(struct pci_dev *dev); void pci_iov_update_resource(struct pci_dev *dev, int resno); resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resn= o); @@ -977,6 +978,7 @@ static inline int pci_iov_init(struct pci_dev *dev) return -ENODEV; } static inline void pci_iov_release(struct pci_dev *dev) { } +static inline void pci_iov_disable(struct pci_dev *dev) { } static inline void pci_iov_remove(struct pci_dev *dev) { } static inline void pci_iov_update_resource(struct pci_dev *dev, int resno)= { } static inline resource_size_t pci_sriov_resource_alignment(struct pci_dev = *dev, diff --git a/include/linux/pci.h b/include/linux/pci.h index 1c270f1d512301de4d462fe7e5097c32af5c6f8d..859f767b30f726bd157a6080f59= 77c17c4827a1d 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -1015,6 +1015,13 @@ struct module; * how to manage the DMA themselves and set this flag so that * the IOMMU layer will allow them to setup and manage their * own I/O address space. + * @managed_sriov: Disable SR-IOV on remove(). + * If set, the Single Root I/O Virtualization (SR-IOV) + * capability of the device is disabled when the driver is + * unbound from the device, by calling sriov_configure() + * before remove(). The presence of this flag guarantees + * that when a Virtual Function (VF) is bound to a driver, + * the Physical Function (PF) is bound to a driver, too. */ struct pci_driver { const char *name; @@ -1033,6 +1040,7 @@ struct pci_driver { struct device_driver driver; struct pci_dynids dynids; bool driver_managed_dma; + bool managed_sriov; }; =20 #define to_pci_driver(__drv) \ --=20 2.53.0 From nobody Thu Apr 2 00:08:13 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 2382B3CD8A6 for ; Tue, 3 Mar 2026 21:15:39 +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=aUgLyEDFQmMr60ViVAoxEVqL/jr66hmXCsVnKzfM4KR5GdHJ3fPKgSrcpoAG6XYq+jKw54p0nZaJl8eH1eo0C0Scro6Az7FZRmIOYJFetd1Sd9KcRcM9yRhdzN0wxnzgFXBhUrDHSWLcDdJw80KupQouRMOOQge8lfiC6RMPgcw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772572545; c=relaxed/simple; bh=WKMW8HVlmZaoyJE5jsAXtcRs0N48w7Ua2f/NenCBzOQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GxObK2wuV/GWht6vhdyjXZpBSXZsAOSXPvGLNHlpMSqfz5jf6qMOfe3al/Tf8kMZRRnrwQHELdSCnYpR3dbcA4xDEPj+rtYyNV2D1px7TbIBQ7vsBAhZGpTL8jyDH1Z0wl0j01MjPhPG4aaUpj7bDokO8dSwSeoGXtN4bv0DF70= 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=OCESQA4c; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=LXsthQwA; 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="OCESQA4c"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="LXsthQwA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772572538; 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=irBf4YUdN1+vibh8PKUSXKnT1yGXKOqgKyvxp0mJGPY=; b=OCESQA4clh8Axb1EjczquFMRnGOxVQ0eJn0FXweTVNSIxFkuzc2tNJ9bFvcsjNAOLNxk9z Tzwha96xnUDt8Puun9MztQK61kbNN+O9W/1yep/mDbg01mbH1wtQMk3u13R44FXdv7jg+o TRHQ8bldEBnrh6Kg+VGFCL1XExIfl38= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-621-lPpEFVrbONOiIB7yeq2gtQ-1; Tue, 03 Mar 2026 16:15:37 -0500 X-MC-Unique: lPpEFVrbONOiIB7yeq2gtQ-1 X-Mimecast-MFC-AGG-ID: lPpEFVrbONOiIB7yeq2gtQ_1772572536 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-50341fddb89so461430781cf.3 for ; Tue, 03 Mar 2026 13:15:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1772572536; x=1773177336; 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=irBf4YUdN1+vibh8PKUSXKnT1yGXKOqgKyvxp0mJGPY=; b=LXsthQwAvI+GKiRoth6wdOJ4d7t29LFNUXcVJ7W4ht7TxVbYapOiZeMa03RzTM+PD9 Gp9Tl3ywuJnCudUgdsoXbCtDX69Otr7tPpVE/kWlvKblfsKuQfxSv/EBRgUbAbrrALDG Z/djSv+b/2AZo4oK8tNCMVv3mi5v0Gveo3xEz//MQwR4+9T/blKHIoSy2n/oT21ooltw RFmsGWmfjaHkxtDVWgNSBoKEeOUmNEIhs3DZtjNyvHy3JBDLV0+LnyxoZFMuWN5EHG9K BRsNOAyVtb4MXTkr5Pg8hZZ4KnOKzO8HHIwLTuRUnNdk2ms3l8CovFKVrIQJF2/Bi4TK nfyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772572536; x=1773177336; 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=irBf4YUdN1+vibh8PKUSXKnT1yGXKOqgKyvxp0mJGPY=; b=DTRBzHZYYSHkij3MMTfH+LXsfQirUy1lXhqtrqYv1URWy0ai/KfSuyB3TbGDOeiTNz Xu1MsGmmJ4CpMQbBKhGgQE05ltYWjHbzRirVWrFmI7EU4+ivFWMfWodHk/0PnrLUAniI Uq4leaZ0f4AwDA1dzKnEWDpRsXJNmnnCMg4JGsk9n5Nl+Y21aRwDycrq/wy1PJ66LWjI xDCMn13baGWUKiFVuHNtJW5d3pySAvXDjTIcGNZyOQ0h3XQ34f5ZHlmcC7haVUCnPN42 1Orwiee56GqoP95845dTs68MAOBRbVfbzcniXQIcO3OFOvcx5W0vyVNVI75356zzzvbZ Tv0g== X-Forwarded-Encrypted: i=1; AJvYcCXtOlYYJxWRIKYG2abSMIsFyBd1pWPrryHnCG3cOD8PjNI1e5IDgwcQxx8QFlKd2X1gdvijBAV5GYaTaak=@vger.kernel.org X-Gm-Message-State: AOJu0YxQCWkROqVDISiLuD+65Ox2j/KiZnxXLUlE1SITCzxUF/pn0WA/ NZTKCUTUx95ZzNAyYVUKJEvSVqlMEc5flA90xOGlQ2GF6BKSMpuMkwe1WciWb5pqukxlp7StzJ0 ckNExV5WxRfp1P0hw5DHkOG51/6TMpNKrGbCKABjFGiYO5fBhh2W9x4q5xqWwyKKWMg== X-Gm-Gg: ATEYQzzRQrEY2sS0lutPrFyOOvS9w4OgMxAkwUYBe0DhPJ1kh2RcYcJhZKYjipV5IH8 so0b/0x0oIMfqoI84njGMvFB0VGk9ukEezekEvNOCqPNW4YSfEPRE3aCNMLrn8nNggS2/Yksf4/ jw5wyOUzaRs56SkyQAKusSFGDxguxuVlN5GU8KACAn1pAzXbZ2NBUnJl0erAiHqcoVtcViP1nKT U1fjTP8yIP7P+u+jyC4ZCs5psgaC7f9/fKg0x531QUhAtG4NBKOq7AHMSlXMDjy416ZeJLQm6+n 182KQZxk6wPNcbMB9BxlOn+WufUqmUgAW1V4Kp4gGHu9sDj1wbTAr83S0UnFh14zyOnWjjD+gCy 9ujGhiCtuU3hOtKeu8EE+GhEWSw== X-Received: by 2002:a05:622a:1891:b0:502:9abb:c919 with SMTP id d75a77b69052e-5075287910dmr225478251cf.10.1772572536449; Tue, 03 Mar 2026 13:15:36 -0800 (PST) X-Received: by 2002:a05:622a:1891:b0:502:9abb:c919 with SMTP id d75a77b69052e-5075287910dmr225477351cf.10.1772572535732; Tue, 03 Mar 2026 13:15:35 -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.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2026 13:15:35 -0800 (PST) From: Peter Colberg Date: Tue, 03 Mar 2026 16:15:22 -0500 Subject: [PATCH v3 02/10] fpga: dfl-pci: set driver flag to disable SR-IOV on remove() 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-2-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 Set the flag managed_sriov in the pci_driver structure to show how a PCI driver may opt into disabling the Single Root I/O Virtualization (SR-IOV) capability of the device when the driver is removed. Merge the function cci_remove_feature_devs() into cci_pci_remove(). Signed-off-by: Peter Colberg --- Changes in v2: - Demonstrate flag managed_sriov for dfl-pci driver. --- drivers/fpga/dfl-pci.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/drivers/fpga/dfl-pci.c b/drivers/fpga/dfl-pci.c index e25205c6d8f00cec579016acade28e743812c924..2410e6f3efe100a635eebfdb21f= 28f62a3759890 100644 --- a/drivers/fpga/dfl-pci.c +++ b/drivers/fpga/dfl-pci.c @@ -125,15 +125,6 @@ static int cci_init_drvdata(struct pci_dev *pcidev) return 0; } =20 -static void cci_remove_feature_devs(struct pci_dev *pcidev) -{ - struct cci_drvdata *drvdata =3D pci_get_drvdata(pcidev); - - /* remove all children feature devices */ - dfl_fpga_feature_devs_remove(drvdata->cdev); - cci_pci_free_irq(pcidev); -} - static int *cci_pci_create_irq_table(struct pci_dev *pcidev, unsigned int = nvec) { unsigned int i; @@ -425,10 +416,11 @@ static int cci_pci_sriov_configure(struct pci_dev *pc= idev, int num_vfs) =20 static void cci_pci_remove(struct pci_dev *pcidev) { - if (dev_is_pf(&pcidev->dev)) - cci_pci_sriov_configure(pcidev, 0); + struct cci_drvdata *drvdata =3D pci_get_drvdata(pcidev); =20 - cci_remove_feature_devs(pcidev); + /* remove all children feature devices */ + dfl_fpga_feature_devs_remove(drvdata->cdev); + cci_pci_free_irq(pcidev); } =20 static struct pci_driver cci_pci_driver =3D { @@ -437,6 +429,7 @@ static struct pci_driver cci_pci_driver =3D { .probe =3D cci_pci_probe, .remove =3D cci_pci_remove, .sriov_configure =3D cci_pci_sriov_configure, + .managed_sriov =3D true, }; =20 module_pci_driver(cci_pci_driver); --=20 2.53.0 From nobody Thu Apr 2 00:08:13 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 From nobody Thu Apr 2 00:08:13 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 9972C3CF684 for ; Tue, 3 Mar 2026 21:15:45 +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=1772572547; cv=none; b=Sgk6ZjlWBpW9CDNjnBxTho5MCQQr6PPjWNPdc8u0HZQ6MDfjkOKx75rW0mh+kfHM3DGPZ09cX7tU28HszaiaM/HxNsi/SFBmTVEu/HoU0ulo/k9wOCoRLN39pKbGlfSDnF1whHwQNZQTxGa4weN9PvyzRjTcuMRnDYcCvwQeyNc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772572547; c=relaxed/simple; bh=Ovi7q4LBinaNMgbwhfXjFRVkgoBkC//IcdGA6/LR988=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=R1Eh+PsXeM+CnC9avPMxh4dAeEqLOCOQKnmtoGzdFiJn1ZTttRERHRt2PXYWQWKAl/8mzIB1pybHVkjf0D7Urh48TcKJmZdruk/EfSggb1JUwWZOl26cF++03CfpGB1aMlN9b3ymDmnS/XAhWFFdPAYSc/dglOMu75NWn904B40= 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=X+snYV5Y; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=B0qN0E6i; 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="X+snYV5Y"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="B0qN0E6i" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772572544; 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=nmGZI5WblWd631ssrKt08f4BijC6XK/3q+5rIjFI99c=; b=X+snYV5YDAJDuDwdhgZsnhFnZfstNwyY7PJzJbTCf7i7oUREYurhBsZZqXs8TFjt4XQ3SG pzrkOQ+/uCrutTgiEuBkfK2le0SVVWkltR9l5zrtPz5dL9LluFfjv5g+JqH4Ha7T2k+GtM 9C1mzkHdotWTzvFg6AHomt7t9+MJJaU= 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-139-Uwx_2pxTP76ftItjg4-5XA-1; Tue, 03 Mar 2026 16:15:42 -0500 X-MC-Unique: Uwx_2pxTP76ftItjg4-5XA-1 X-Mimecast-MFC-AGG-ID: Uwx_2pxTP76ftItjg4-5XA_1772572542 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-506bac14430so716535771cf.2 for ; Tue, 03 Mar 2026 13:15:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1772572542; x=1773177342; 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=nmGZI5WblWd631ssrKt08f4BijC6XK/3q+5rIjFI99c=; b=B0qN0E6iHUjtj5H5r8OZlT8CV/1awFd20j8XrWKPadjilWen9mvG5kLn3n1MxJr2q8 uq8rBp8Q9LKycQJLayAaW+tB7SxzLHZoZ2zIyRpFdjtRJUrDI1End5DeZhbu7T3pOqHS hz0TG7fk9XLp1639q6soPgbBYYLFaBq/ls29gYEMsr3g48f33hUPcLbjVnx0MOcXM9Wt 8tMBGR+rmxSc56PnxnwvQXP8IjisR7hO7/+KM7FbBl95pYMEPLxOb3ayMoBmHLnWReIS Dq9+B1WC5NmEgflA/mcRTZgSAQuZDYEDuBHCIhBB5sNKoVBiI+0Gb+8a8e/HnRUtyYro /CNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772572542; x=1773177342; 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=nmGZI5WblWd631ssrKt08f4BijC6XK/3q+5rIjFI99c=; b=IQ8DKL8ubRjLSZLvW0tlfqcJlRWaH2Biaq1hOHvwJ3I1xv+dNxLY0VWC4nZhbJKFTZ dD3tC+sf0sqf2anD0kAl9nSZ5BacD2UCDOT1t3Rf/P4aBJxyuaZQ4DC4scWX4PYxNGu0 8VOStkWbWVRWS0uRsBdjrkOVTyqlLVNOOk0DUWPCXK6zG6BDtgSG2Fu/InRYZbyFNKh/ Z1oDRjOtQCP2fwQ5kokDa1h1sm5bZLwBCwhmr2JK15J5gAxdKdn9h1wMApBFtkl68rHO yu11yEgJuWZPIfOe51ozEV2aEt3MPAylo3/ODVOy1CQDvOIm+aiQgWiDQQCZ5Jkn5W4o BmHg== X-Forwarded-Encrypted: i=1; AJvYcCWsK5JhpHFdVugjAHUW+3rPLphTnI443s8vW3GTAZUyF/W8yvnPfupWsnBVE0/bkc/h/xJqpOqSLY/Koto=@vger.kernel.org X-Gm-Message-State: AOJu0YyS6rm7NFkhzWrztWtbm+Orap4si2Z9ETOGYR7bC0g2s3XRTn3x 48iKHlMg2c8AGTk4Ud6OVk7ccXH7y6tl+ViLn5tad+dVJP7k9Ezj5VmPqgAlQWtta01Sw+OcZRM y5FFE7AQeIFZ+whfMxjr0qRimUPiCDyYN8JD10x32NG08BlH180wZYl6L06SHfK9MXA== X-Gm-Gg: ATEYQzzZKr07u1C/T6qMF11D3+BKBcYn2j8Xk1adMwlHTom2VfREOLNqA5+CxcawaIL MoX3ocJRJZq5e7BvHUNqRaHL/KuzQd6JBnwO8fiH9wTntiaL0t/IfoylJUo0CNvQq157Lo5bX2t O05xLrYdNSVCDZnskuE4slpmWEq1RVdwMal10uip7dhIIiy/XKFUh+8hSSOJiLAPDPS6jYvIhsG +u1ZkXy1gaFI+z5/1MVR/jkdUjBiKKwfdVRK16L59gb+YaMNBLi7tbMXuBOoGujZ0kVVN9R/yON A2nGT/sKBGxGF68bU2tMEInZzmuVVkz2XekrnBum09bAiw6jCqFjJxQ4jz872kYC4s67QjwmYQG LDCw94j4WKZ/9u7DIHBJEOgKB7w== X-Received: by 2002:a05:622a:18a8:b0:502:a1bc:5691 with SMTP id d75a77b69052e-507525a5444mr232050561cf.0.1772572541846; Tue, 03 Mar 2026 13:15:41 -0800 (PST) X-Received: by 2002:a05:622a:18a8:b0:502:a1bc:5691 with SMTP id d75a77b69052e-507525a5444mr232049271cf.0.1772572540438; Tue, 03 Mar 2026 13:15:40 -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.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2026 13:15:40 -0800 (PST) From: Peter Colberg Date: Tue, 03 Mar 2026 16:15:24 -0500 Subject: [PATCH v3 04/10] rust: pci: add vtable attribute to pci::Driver trait 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-4-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 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 --- Changes in v2: - Add missing #[vtable] attribute in PCI driver trait example. - Add missing #[vtable] attribute in nova-core driver. --- 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 5a4cc047bcfc9fcef61373ace84ed43958a3bcbd..66a68048006fb33477bb00f6185= 6c1aa92c0a8f1 100644 --- a/drivers/gpu/nova-core/driver.rs +++ b/drivers/gpu/nova-core/driver.rs @@ -64,6 +64,7 @@ pub(crate) struct NovaCore { ] ); =20 +#[vtable] impl pci::Driver for NovaCore { type IdInfo =3D (); const ID_TABLE: pci::IdTable =3D &PCI_TABLE; diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index e1cab1574a3d309d25bf5267c0b0d8da8fb66d44..a4c27c674bd8bdf5e3316789d38= d566e90b93fe2 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -278,6 +278,7 @@ macro_rules! pci_device_table { /// ] /// ); /// +/// #[vtable] /// impl pci::Driver for MyDriver { /// type IdInfo =3D (); /// const ID_TABLE: pci::IdTable =3D &PCI_TABLE; @@ -292,6 +293,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: Send { /// 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 9c45851c876ef33414eb0071c42a2fb4ac3f1e78..ae6f7328b830e32bcaf7f8b5f8b= 1f117135ebf8e 100644 --- a/samples/rust/rust_dma.rs +++ b/samples/rust/rust_dma.rs @@ -51,6 +51,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 (); const ID_TABLE: pci::IdTable =3D &PCI_TABLE; diff --git a/samples/rust/rust_driver_auxiliary.rs b/samples/rust/rust_driv= er_auxiliary.rs index 5c5a5105a3fff90f5e396186776e1b3ffdf479b4..2f7ac8f7391f45827c086b70495= 0fd01907c1825 100644 --- a/samples/rust/rust_driver_auxiliary.rs +++ b/samples/rust/rust_driver_auxiliary.rs @@ -65,6 +65,7 @@ struct ParentDriver { [(pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5), ())] ); =20 +#[vtable] impl pci::Driver for ParentDriver { type IdInfo =3D (); =20 diff --git a/samples/rust/rust_driver_pci.rs b/samples/rust/rust_driver_pci= .rs index d3d4a7931deb0c1085cfd817990737717f466ea9..27e603a9509c19b6845c10ef06a= 0af897aa0e84b 100644 --- a/samples/rust/rust_driver_pci.rs +++ b/samples/rust/rust_driver_pci.rs @@ -92,6 +92,7 @@ fn config_space(pdev: &pci::Device) { } } =20 +#[vtable] impl pci::Driver for SampleDriver { type IdInfo =3D TestIndex; =20 --=20 2.53.0 From nobody Thu Apr 2 00:08:13 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 B3BA53D3CFD for ; Tue, 3 Mar 2026 21:15:45 +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=1772572547; cv=none; b=Q5OnGIwrFwAfSw9Zwzq8k2FF7JbpXFi6PBYTdlrk+Mh2vWepzrw9VDypj7PerTP+iqoEigB6+ztOQkjUHUdQkqensITc5Dpu9in6X0gRXvjSYzYiTT+0aV5ZeLjhh+okuFU+61hPeZBTakpr14hthQHf1X+giUj/CzznaCb0qK4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772572547; c=relaxed/simple; bh=OY7zfr6p9lvbctn7yr5wWYUOqciQoXU5irFoOySjssk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CmQnpag4W9Ar4cw3aMy/iRtEaQhraKdgzuZnf6Xj2WR+hL8Fgw0E06RQsK52WggRLUq+3vytoWT3A6ET2lxC60+LsgnpyVY+ypVaXB+7xz+hUHEd/d/THLtLii9mysF+rPZ7FQaGiRWTVDh3ZnYBWgaNO+x3u7FWuW8KFMofY5M= 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=UatT3xYS; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=M5wAGXwf; 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="UatT3xYS"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="M5wAGXwf" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772572544; 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=XjMC0WxfV6FSNfe+9wyhxlyiyTMxjfq8oevr0CbOGEQ=; b=UatT3xYSruZfpjqVDf4AAd2t9EzKJjXY55MRpcvFzjVonc0uZncFQejFys8eSBcI1rWH3M LJgBE4oaR+/GpzYhH8O6n8m8t94J6OMCvSH0/UK7sQb+gU/IehSfGxBs3tq/4d18CJww70 LxOFvkEbdZYQY9RX5syMnylNNlHDtuk= 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-344-LVmK_aJUMn6bVgyEunOiKQ-1; Tue, 03 Mar 2026 16:15:43 -0500 X-MC-Unique: LVmK_aJUMn6bVgyEunOiKQ-1 X-Mimecast-MFC-AGG-ID: LVmK_aJUMn6bVgyEunOiKQ_1772572543 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-503342386c7so91934191cf.0 for ; Tue, 03 Mar 2026 13:15:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1772572543; x=1773177343; 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=XjMC0WxfV6FSNfe+9wyhxlyiyTMxjfq8oevr0CbOGEQ=; b=M5wAGXwf7ePyDDNSfoE73pBgvXeejs6u7EDFDXXCvNCouAskISV18rUxssNFTFqMah 2szEdlSal0kuT7MppLbwkI0qMegNcp4HejF+qDWLRgi7PMi2HMyPxEYnHAKt5w1Z7GBQ t0mcCmWYzJ/J3jb0fMuXDlWde+Agc1BnEIvosFbrFFzLXyGuaCGdf/dmGnHmOkDJ4bQ4 boFGh5z3OhsGYj7J2p7VFnMTXJUqvPxBlHBfHmmf/m0n9yxQAow5SbDl+TRLyM7W46NM 266s/r9YMyR48G9K+Hb83MKuCsOmW7XnMeMD3y3qncPSSYiSwM4YzNawJN7mkJzXsuDQ Rc8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772572543; x=1773177343; 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=XjMC0WxfV6FSNfe+9wyhxlyiyTMxjfq8oevr0CbOGEQ=; b=iMTaY/pr8v2Xv3KffVJXcIMKLlsRcPISVIfSQYoVFEapd5li6p3WZ1GuGrYmyJSjXn XaDluvRz+1b3kpI403sHyZoKmZnWhPKhrWDvygt1unPRuZ8YG+vYQzoOJcSHmjVNDO97 S57bl4ERa7QIkK0WoJ7vkmrteoOO0k4YHbDzl+L24O2Jxa/E081WiV0laNunWbgR/Zf9 KHodbRZdwIzN2joPkIZF/dRXD8tbcL9XJw3gwcCKlOBKagDeAwEFyVmyMo4lKJCXVHr4 dPwsCrgBdPpc1ZwIR3Hyz21c53yGM0Yg3hZqawEbhiWdZHgHaGTOUjAcrAsTnAjmp0nM MF0g== X-Forwarded-Encrypted: i=1; AJvYcCUVL/1HtKV+gIPPEeeJ3YkerRWkf3q8jcdnM5BvgCcCsGLxJvCq53JnpSqTWBKSV6rlkvg+zqO1MbcssT0=@vger.kernel.org X-Gm-Message-State: AOJu0YzUQ7gLzWmLISjxn7iA7VE997nWC21DZ8xLaA4GqtjNAUtLKYt+ 2rv1IbftFCeHwOqXvbhb8edALvRJPMwg3KV6h7orvuJm6YtNRxX+e6eFcFAoB1ecOgvLZb6AeqS 2eebcrhtJ2/Ez/ZY/IVwWwWvbVyoKOGiSBS1PBBShJYl08B1QJnFxmNzfDuH/kKoHsw== X-Gm-Gg: ATEYQzzwiGKizuNFx1Wr9XQqmcAZL3sbRGNrJyIEz3Pa/bagKh9g+WWNrh4ZWuRY5gK CZxXhTOmhW7+oKtEFuuOcQYr344vrBhSJnX7LYGWlEl76DdY3QVBJ6G7kxFFG3GJ1/7smvirB9N L4SoZJIAF7+JGLdcKxJ/aRF9O4ZJ4t7GOBq8rk4m5cgy1t87lI81SlB8aq70Zr3BUCM4J0487p9 Im+8MaVeJnywCkX85nPVqeYmchtvgXe9M+JcZ0CeZ5lfHQq6oVaWCljcqHiy78BBLHM+SGypv/S Kj6WpoJw4y5AJkIG6VXMFzlQRNMma3mAgIgAsRmtKYmRxsr8Ajg3lOwZyRQA3RCavpdjxcYLNvA UL4ux/Y2/GW4ZILiNb2Z78PtWGg== X-Received: by 2002:a05:622a:14d2:b0:502:ad63:e15e with SMTP id d75a77b69052e-5075281833dmr229351141cf.55.1772572542973; Tue, 03 Mar 2026 13:15:42 -0800 (PST) X-Received: by 2002:a05:622a:14d2:b0:502:ad63:e15e with SMTP id d75a77b69052e-5075281833dmr229350471cf.55.1772572542372; Tue, 03 Mar 2026 13:15:42 -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.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2026 13:15:42 -0800 (PST) From: Peter Colberg Date: Tue, 03 Mar 2026 16:15:25 -0500 Subject: [PATCH v3 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: <20260303-rust-pci-sriov-v3-5-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 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.53.0 From nobody Thu Apr 2 00:08:13 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 7D26B3DBD63 for ; Tue, 3 Mar 2026 21:15:47 +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=1772572549; cv=none; b=mYh8Wp8GTtMrw+OSBq4Q8yDXvNGgnDhTkupvqULZ5liyWtIL5DHAg1ZF13rQ2iEGZQL+s7vtUufM6UaDmUBWEKd94iFigVnacHReeUW5lKosQap50P2HAyyVKfbyGEz35/dK7OV5ExVXsF0494G3RFJQ2A0/F29YCQ+aVSVUeyY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772572549; c=relaxed/simple; bh=lJwT02KVG7/pESXAOG8cyaW742ucvP9hALO2Uv3N7Ys=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=mQ6UpKIYNUGGtz0DWsaur1PPaxwIyLMv+CPSZrnKZMCz5NpejKItPYoDl5UlV3l51AZvVJ7UYk9cF3yWhZGVRHyrFVLwxFX34ml+M+sg9n+sQHAEKo1oDSa1kfxC33snt/Pcf0HwyRo3EjQQENd4/b2ptgcsqbgjNa4fD1535zM= 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=d1agVTT1; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=M9pAa5ka; 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="d1agVTT1"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="M9pAa5ka" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772572546; 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=Ld6zN/9/7YmG/EDhmcZckbqpb8XwO0ah+2Ai3kR30j8=; b=d1agVTT1mdHe2RTcnyqqu47Hr+4NPzeFPlaO7lQyK+XY4kfWdXgMB6RgxkSerwvoVXRE5B bzQBh2MkjW8m/GDJNVnETrOz8RimqCb8sEYaJAY+ml/0X9z0zZhuljhtltUpDz67JKk+CP zkn9swyNG/bYIi0VCEWI020vrHOh9JY= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-147-T2RB9K5lOseHbX31n3PVPw-1; Tue, 03 Mar 2026 16:15:45 -0500 X-MC-Unique: T2RB9K5lOseHbX31n3PVPw-1 X-Mimecast-MFC-AGG-ID: T2RB9K5lOseHbX31n3PVPw_1772572545 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-899f619264fso177751876d6.0 for ; Tue, 03 Mar 2026 13:15:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1772572545; x=1773177345; 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=Ld6zN/9/7YmG/EDhmcZckbqpb8XwO0ah+2Ai3kR30j8=; b=M9pAa5kamLG4d42fKOUrWooO7+revNokH+wbAuoUluLIDuYwpiIuNgFI5FJouogS1T cPiv5ftgGB1gNgsSR+mQCQRqNq1eiTVP1LNpIoD4jxpabHcsU08junB0y8G+T/U4IOAZ 1HbakjfN/rD3BUDtGIGKQuu00lGe05CbSwslU1W9GDhG2Q/sJDLszCfHBUiYvuO4Wupw +vSfZWjydEIKPzYWQQw1gzfOyhRVN6zXpdoZunfDvGz8qRqlf+Ea2gOtSPKjFpSCkLxY UrcAwqruUX97dmn6ho9CC5kKtatKfY2ycEkgNgfISI6JDmJII5rWcoLdJUyPWjIGFfVu Vk3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772572545; x=1773177345; 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=Ld6zN/9/7YmG/EDhmcZckbqpb8XwO0ah+2Ai3kR30j8=; b=JElK7846EdYkVxSQo7gTdG+Mk0sYiLgAESmJM/tzSmrSkQ2boM4bqmwBWuJKTwiUz5 ScgYy65XNz2bFpa4zs3BZmzkNgkBPYRiHXDiL8Pf9gm2yPep+Y9tMVRqj3824q9kmuBM hwolS9Lct6CbtpbGpGbUXsw+CVpOz6kbiw/BkQAj9P2BVyGJSJ/so5J2tdCFvN2Nnc+0 yuYRGA2Grxan2iKSVxZTdF+lhwYJNC62vwl6q7zwDBKfJUN5rA7FUnN2Hft+n0Lt05br ZoqkhIH2mUBAHGVJ+n1+hpu+gS3uCz/jNySb9IyG1ylWW0eu4qg6bR9JCD4D/u8yEZW0 XDrA== X-Forwarded-Encrypted: i=1; AJvYcCXpyxrvF8ir3tQobxdc9N3HorjTRPJ9OISg/i84lR/JxekAN75s7SfkzTR1J5GqoY5+8aANzxSNjeO+bOY=@vger.kernel.org X-Gm-Message-State: AOJu0Yyt0paF0nJpsQpjclbu6HoyTLq56TKzPY2R8DiyFGyqsM9u/5pP hwuvjhpNGIW5zJn1ArsNCaze/Y0GL68HuiWK3zxEUvrIlHvh7QIgd+2OHYx4SEqLAnIcD0/ADKJ KK+k8lOVI7XrK5bONUy65VaD5Nk4xroHJnNs1BEbtl8MZBEXULjnJ/SERZqxONxb7hw== X-Gm-Gg: ATEYQzzQNTOmHRqv/Uw5AIDquR1VcfIK3xCgBECRZT74F+4prz1ZzT+4ZHldnpZX5AN 7neFeFNgAhF8YngCGSTq8obzIdavAgiut9pf6pRKTgmtvztlT9Yol8T7xLPAzGLP/ZygfThP6GB ZnN9bkXKyRvJNm+ZjBd6cPRMfBt2zvAhRxiSqyoywCMVQpDiir333P2dReaQZT8/9Teoif4Qmun XA8wCuVYBSGOydVRCku/j6abwrn84iZ3ON0ctxY0W8noAnro53N8zx+OEdJahxQtPeZaVU374eR xmgchrPRCye2a5C3fyDiqkDDAxP3MVmwKC2l7UqNTKSER+aSZ47DUwGO//4zOikSuu7RvpM9nO/ VpSkUZJkpOV+qK0kB+54QjiL5CQ== X-Received: by 2002:a05:6214:509e:b0:899:fdd6:9ada with SMTP id 6a1803df08f44-899fdd6a682mr113463496d6.4.1772572544927; Tue, 03 Mar 2026 13:15:44 -0800 (PST) X-Received: by 2002:a05:6214:509e:b0:899:fdd6:9ada with SMTP id 6a1803df08f44-899fdd6a682mr113462916d6.4.1772572544344; Tue, 03 Mar 2026 13:15:44 -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.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2026 13:15:44 -0800 (PST) From: Peter Colberg Date: Tue, 03 Mar 2026 16:15:26 -0500 Subject: [PATCH v3 06/10] rust: pci: add is_virtfn(), to check for VFs 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-6-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 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 --- Changes in v2: - Add #[inline] to is_virtfn(). This patch was originally part of the series "rust: pci: expose is_virtfn() and reject VFs in nova-core" and modified as follows: - Replace true -> `true` in doc comment. - Shorten description and omit justification specific to nova-core. Link: https://lore.kernel.org/rust-for-linux/20250930220759.288528-2-jhubba= rd@nvidia.com/ --- rust/kernel/pci.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 88bd114970431bf8c3edef94c1d48567d895eaf6..db05641186c3a42922e2b6a463d= e9c1b099a4673 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -484,6 +484,13 @@ 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] + 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 } + } + /// 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 Thu Apr 2 00:08:13 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 3291742B73B for ; Tue, 3 Mar 2026 21:15:49 +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=1772572550; cv=none; b=kCGZ2SF9Ab11oY0Qo9UNsvba+o5YEkIcFExzIUrAXRgTxBspFjSOx7StWnCKUAUJgN8h2AmndalKGCo5g75W1V8jaWOndrq+4ng1nuWciFvpig/06VnFu4JVz9XN1YPwwkwjCUQAAPlZ0GHGxhNNoD0Q+oXCAbNDoAh21hRqh30= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772572550; c=relaxed/simple; bh=s6mYU8Hu6u0tbKnwvAlOQsSAlR2qHgbV7fvzmNOhT1I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HJLUmI5U8lmOdCLxVa4dxKbtD29psdrGIpBjgRFDSQv3dllwNZ1kF4eoFB0hbWVDYeVhWVlr2a5Uh8eDRqteMhwjC0ImDXMJvGm1b/vLGH8zX+IA+YSmFsec5+VepwC2CLpf6VOghBmAKtuE0Xqh9EBk2D0vXEkokF3U5kc7DB4= 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=BTkyQ1MI; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=kCAw1JBI; 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="BTkyQ1MI"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="kCAw1JBI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772572548; 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=qLYDkNQ8A4x2T95jGRETa6NwStvleq7OKd2uXV0vocw=; b=BTkyQ1MIwdUr4SBnXaUoXkx2/qFXyrU5CIbfGcjArGrolbt/qJhAi2ziLVJz6vGm6IY23v UryGmw1nwaJVbxhzmYw/2pymwQ8XtHp17lugdIOZ9Q9g6Og820zug/p1Ip5J8SE0Mk91mb 7FMRsKvQOEObo489Ivyt+Wyxi/oCehw= 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-2-g6zWpgz8PTGH22vB1JEVLw-1; Tue, 03 Mar 2026 16:15:47 -0500 X-MC-Unique: g6zWpgz8PTGH22vB1JEVLw-1 X-Mimecast-MFC-AGG-ID: g6zWpgz8PTGH22vB1JEVLw_1772572547 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-506ae021853so72635301cf.1 for ; Tue, 03 Mar 2026 13:15:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1772572547; x=1773177347; 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=qLYDkNQ8A4x2T95jGRETa6NwStvleq7OKd2uXV0vocw=; b=kCAw1JBIAXWRPZw4JUJv49YAWrljxCYbXksrBYpDoKBqfunUfLS7XVJD34AMw7f9pe lBP1ZOc6g2DZt9+ns25+uobgikZUqykOa6qiBIW+8Z6HDs3PQkeOn7oop4BuHgH2fU0+ tyR6uRyuj41vz0FroNhHCjkmE5cabgSEgLWg7iZKShrJDXnc8UMmwJjxTMgiI1P3tsSv x2/6YqkSZgykeNdS9iOKTP25rRy32reg30RkLHB5d9B2be7QENmC9tAVVCikFcbaqii+ 3ExfTwxriP55kHvkyPhwceUhX+3o4RHe6gIsk8tNooyfKnefiYQoBGa7nU78ddbgToCW n2Dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772572547; x=1773177347; 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=qLYDkNQ8A4x2T95jGRETa6NwStvleq7OKd2uXV0vocw=; b=E0oLhvmgutZAL7SrUVWcFDmcNcRchILDDKJxQ/rgLDt31x6ZAYANVVxLQwD3V/xWZu T0tjd6BwOzTXwCt9hJM/MwECyr4d9+EnLR3dhe160gxbJwuoaxHC3CTcy+ccH19fCMdS 1p5kQn3p8DOiJLKmGCab9inhIrPy/59enuzPRTLv4KasYy+B4scbj58bjnf9Qc4yHkBR lcZYNqredMuHz7kyWreP9g7fXvfLgzsSLwJjJFDALRaVfGmMQU1J93BwVnLwFRevxkQH g0m0VquwGjm//lNK0MX5fDOnLmZbV5L9ZoXB2ZKkH4r8jIKAf3ptRu/xaGQ8ouRx8V2F FWMw== X-Forwarded-Encrypted: i=1; AJvYcCXzHxc5JKpSINj7Js2/eyWgOEvpY5wxZYJCF2Vh8kb0N58Rww9WwYFwx45eQQ3Acnk/JFH/RNp5ZpHrZqc=@vger.kernel.org X-Gm-Message-State: AOJu0YwHOdEIQ53JpzCpJa3xC9TI/7TYBmQhVlyj+tQqVf6IJZjPr9Ch ur6IdCJl/t2X00b4ANN8I+uD62Uv9uHMNtJHjPu9kEk25wQJI0pnats+L0bj3YLx+PMCCiYVe5X 3CF1I1e/x8XhvEHWX7l6oq+uGza5vdprkK9vazkPo9ITDMp6MQHWuPI6iz0C32ZLing== X-Gm-Gg: ATEYQzw+pMp9ADYoTTlf0+k1Yr/+cBPvDYolGw+qbPzwdlZV3bcu5jAVRfrlAu8FH3/ ou8bSwvd1RQbarK9GezxbiWFSKyOHhwhUKo3gFmdXoxvtgc2K9CSQDUHOGXimP5UtG3rQsSaPli ggg2XLcbhquqC8EjzwKuTKjp0ToC19XKtZQfax8GYtWoEMBgkncF9/LM50juk58/uVKS54gDZrC pkM22Gj6FbaVM7+3xfY5CSEE+l3bFuEaP2m0PdEf47cXyGCW8z+XIe03LEp+WQkIZcCVj+XUwT8 vFYV6YMBrXSCERKkM/tMx5Sv+QU+j7zha84erf7EfmwfAFg+DbewqBdU8nUpN6L5fDkZ0fwR7vS uYwmQJmvQSWX4/0+iAoR9PLC29A== X-Received: by 2002:a05:622a:64c:b0:4f3:5652:6743 with SMTP id d75a77b69052e-507527cc1c1mr213308831cf.39.1772572546781; Tue, 03 Mar 2026 13:15:46 -0800 (PST) X-Received: by 2002:a05:622a:64c:b0:4f3:5652:6743 with SMTP id d75a77b69052e-507527cc1c1mr213308331cf.39.1772572546254; Tue, 03 Mar 2026 13:15:46 -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.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2026 13:15:45 -0800 (PST) From: Peter Colberg Date: Tue, 03 Mar 2026 16:15:27 -0500 Subject: [PATCH v3 07/10] rust: pci: add is_physfn(), to check for PFs 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-7-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 a method to check if a PCI device is a Physical Function (PF). Reviewed-by: Joel Fernandes Signed-off-by: Peter Colberg --- Changes in v2: - Replace VF -> PF in doc comment of is_physfn(). - Add #[inline] to is_physfn(). --- rust/kernel/pci.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index db05641186c3a42922e2b6a463de9c1b099a4673..df39ad3f0d5fd898b034609efb0= 3368f83c2a2e9 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -484,6 +484,13 @@ 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] + pub 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] pub fn is_virtfn(&self) -> bool { --=20 2.53.0 From nobody Thu Apr 2 00:08:13 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 1BAFA3E5562 for ; Tue, 3 Mar 2026 21:15:51 +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=1772572554; cv=none; b=bISKPn+m36p/kWExO2mcW5CVjJGjmsDShGyRbr+DRHis4dChRoAwjRgBJC9H7p9knJKbQRFqD2HSyAMBuSYRMtXQEYzUulxWNj+gjl1a2SRx5BxqTWyNBfhFZM0i8/zKi9zOKhjjFN5504dnv/dNq9J/BXf3yZorowCcg/G6VCU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772572554; c=relaxed/simple; bh=njU3I9YW/zDKjP+0RxMLdIuH1MmdjiC7YPBjwAwiJ4w=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=uHCwTxpltzq0lpq2CMKsHXI7gBcE2z7DKTRHg5+x0x3SMyQBS/uAAF4IUtEfsWyx6VQijp1oDEJgMzW3E8A2bqN2g2J0OwPlT0uHhoWfcAr/wfW4rEdBoxG7Tr5wEmbkzCPJtMWOBYox/QQI9a6WW8GHCm+NFHQP7Iv/gBO1ymM= 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=dwcPEOXW; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=BwmBIwlY; 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="dwcPEOXW"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="BwmBIwlY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772572551; 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=Nsl81xXoWOrG7fZ6D3nnOVMYFTWE73QzzhAJNIpS3Uk=; b=dwcPEOXWn4opOtp5G4RGm5qe6Qz8rm7pfGdh0iMeUmwtKZG1yxHkIjnz/6sJhozPAg+jGw BPhmntvzfQDpKMFQMutl5w4Jxup0LEhbRpr7gCvdTgViYsgeXzt/BoyBXL0C3zI7P4NxJL YVXY5CJRUTZn9VOUgJbM7LkXFNyw/XU= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-59-oztYZ0TCMrGHZuGN68z-Zw-1; Tue, 03 Mar 2026 16:15:50 -0500 X-MC-Unique: oztYZ0TCMrGHZuGN68z-Zw-1 X-Mimecast-MFC-AGG-ID: oztYZ0TCMrGHZuGN68z-Zw_1772572550 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-50340e2b4dfso653365131cf.3 for ; Tue, 03 Mar 2026 13:15:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1772572550; x=1773177350; 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=Nsl81xXoWOrG7fZ6D3nnOVMYFTWE73QzzhAJNIpS3Uk=; b=BwmBIwlYkgPM4nkteh9HIu+jXvgEbaJgWAQeo4P4bfoSxoTsSw2yHO4p/KeeRtmaFr OIBl1288NNGOuT5B3vU45YNr1CgHAImY3BNMyNOY9UInFpWDcJnsGYGYsfvai85jUyxf LcfJsOKvlKYcRYUTSa3YbLyJbvOV1OcCYDZxoL/qspwKphq8UEDAGDPMWFXdreO69EEB OUnRu4allFNpQ+kRiLMMqCxS9MMAeGFk4VP2w5Tnko6gVPcR0GVcquRUCtpNtNZsyt6s pELoOZ17E7il4HBJul69sT3fXRR7OFjvCy0V1zDO8Q7R5jBazfMIJEJl37jU3TcxoIz1 68Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772572550; x=1773177350; 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=Nsl81xXoWOrG7fZ6D3nnOVMYFTWE73QzzhAJNIpS3Uk=; b=FmsVETrp4vp+5+2TpSl9kricm06FMgk4qdr6vt1KiNoocyLEs00vk5z7lJa9HeovZm dGq8X62x5wSmIdqrYVljpb6VlTcRzW1OW697i263hUGGhEClokS9Qw4Aq2U0QqGKkmQB DUjOfU5NfBEL1sOARGz0Gk2HpBSWzZ8O77Wg2TvrBpkV9jewQGEU8Tkl12TNOTpRKPgd h9HsXhQNEOBM6jeccODOomIAE+udTEv6FqrQeSURT2QNzyEAhT7wS+2yk0t3KZh7NJrq zvTmDKYuoZMmrugUpb3mtwmAaYOXaYQTypcPs8nG4ie1jtW7n6oGYABj34BGPI0VZN60 Nrng== X-Forwarded-Encrypted: i=1; AJvYcCUdUsZUTvqWefclMpWtW5OH0XITLsHjWfFLr2qxcY52DIO3v1nZK19rHBGsjEILoNODQPmQA8ymMkSnNT8=@vger.kernel.org X-Gm-Message-State: AOJu0YxBh4i4FNZv/mqFwTvUx1wgNCmRdjNRh8HONTCHyf83bySpC1bC /E1O6rdP0tnmYPVrm15qnL+Nm10nt+hAi8ZmtRxlg7YRXg5ebHYio4C9tPIsC52QEtBlHCicl9I kKc7qpvyDWWqGqJJYxh/6aHk8I9vOUsVCLsJ4sPZSMxFvDU6LWOfmiS1QsaaKbf5Nbw== X-Gm-Gg: ATEYQzzxsevTOrHttXh4+ydXOfs4V9LObEbfLCccRaZxePtjMPAFOED+4IqTpA9uDsg tWsJsnLIS2cXvJBHNjf5baFcRgv+LPMadxcW0k3q1R74Qz79+Ckl6iscuIdJ0QLmccb0QFIbJve abr9ISmwM5yQ7DpT0oUgcL8NdXUVl2B2ctKvodcLH8YDQbS5HXBcb9UXj9JqdIUOm7k6ayf45z2 hHa0Brg21lQM7uRVEa67Gs1f2AR/JxcY8/E9J4h4LvdX8SmnK1Ik5MnriJ2+Jc/Ra11iU4plVW/ 31uYvP8awYUBIR2mpAXDB+mVKbNOhSpj0LefENoJX68S4Ucm04xdF0xx4jEqaEAEf9CGYr+cGnn cfhFJ0bKf6/61dPl9brttzOHhJg== X-Received: by 2002:a05:622a:1895:b0:4f1:ccea:6744 with SMTP id d75a77b69052e-507528db6e5mr222734381cf.58.1772572549555; Tue, 03 Mar 2026 13:15:49 -0800 (PST) X-Received: by 2002:a05:622a:1895:b0:4f1:ccea:6744 with SMTP id d75a77b69052e-507528db6e5mr222733911cf.58.1772572549028; Tue, 03 Mar 2026 13:15:49 -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.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2026 13:15:47 -0800 (PST) From: Peter Colberg Date: Tue, 03 Mar 2026 16:15:28 -0500 Subject: [PATCH v3 08/10] rust: pci: add num_vf(), to return number of VFs 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-8-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 a method to return the number of Virtual Functions (VF) enabled for a Physical Function (PF). Signed-off-by: Peter Colberg --- rust/kernel/pci.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index df39ad3f0d5fd898b034609efb03368f83c2a2e9..581930d0afe98ccc29d729e4d9a= ab75b4144e46c 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -498,6 +498,13 @@ pub 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)] + 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()) } + } + /// 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 Thu Apr 2 00:08:13 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 455964CA278 for ; Tue, 3 Mar 2026 21:15:54 +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=1772572556; cv=none; b=NIX1TLxhWJuSmrobkIwu/pdGg6MrhEnGy68olpE7lpuqc1Bt2IWp9ilKL+GPVRl7eeUZuEE3kJbVjqo2qpy/6xcMlwgd94ZjL2Kik+69SC5K6Y8MwQvbCSKpoIx/46y3Wl5kIqNioQQ6ZoDsYlzSemasEkEJfz2jxFnAfmFgbVw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772572556; c=relaxed/simple; bh=aIeoCt7uogRc0fVC6+4FHa1SGMowdDhKrl+UTRXZUrA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nl0/Lk0OOsElOpqJgcyr1ky/OCWmCLRA+4q73/amTr/rfiV440uK0KePlG8YyTYdXnO7tElu5irucXWLZ/4g0QJRPsAX38U2hvsLua8OxxEO52PuMMjw809NqgC52uwauCWUD93AdCsxlwHqgJHqEKGM5RT4k8IGqN8Gl+obEpQ= 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=ClSPKbLW; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=BH5zR2yy; 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="ClSPKbLW"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="BH5zR2yy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772572553; 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=62xgaFtJ6odD5MPfFUAjLdRekJQ/qnRaVs4d4H+RycE=; b=ClSPKbLWZNGN7BuHje3qvLZ7qVruJgZQlQ6Dzt90mRjvMLJOgyuwOe+iLQmRQtehu3M1Eu aSQSrjDKm+qViaKWCQQupOBQPImtlkLD1m9E9esi1+5kppZbDOfiVFmiuqnQNeh/b92YUz pxiThj/9P3Lw7yb/ihg7WMAwOf58mYs= 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-253-qRtZdmSKOzOVbXbJfZQ-Bw-1; Tue, 03 Mar 2026 16:15:52 -0500 X-MC-Unique: qRtZdmSKOzOVbXbJfZQ-Bw-1 X-Mimecast-MFC-AGG-ID: qRtZdmSKOzOVbXbJfZQ-Bw_1772572552 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-5033b62efa7so456800781cf.1 for ; Tue, 03 Mar 2026 13:15:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1772572552; x=1773177352; 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=62xgaFtJ6odD5MPfFUAjLdRekJQ/qnRaVs4d4H+RycE=; b=BH5zR2yyYm5YGR4PZLS0xBoOxJrvBLB2Kg101tTeQlqusNMYR0du2T6vMPj1TtGmrX Rv3kaKqZp60D9EuhME6OYwqcd4WtGAqWTpvhDkqLK+D4tUg0ZNm8QMusn4OCTa2E8OrT idjiTIxleiaVzJMmvHGnuvT2EMF8ZkU4zGUNoLDUXiXgj0PGhlolJuWA3kslV9r5RBHN CQljfmcO67ONgTBd6rl+euma9GTjFYqqqWy3/6WJ6wFdjljerOmrVfnj+TP9a4VAKQuR 33cA8QJT2BnYnRXGcZWCfv1FSy8woG2tR7BL9LokePIfKaAFcKj/HzVHCbx9XfdlceRV JGow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772572552; x=1773177352; 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=62xgaFtJ6odD5MPfFUAjLdRekJQ/qnRaVs4d4H+RycE=; b=jhu2XnxjsF/1Lyam0ZrVxgckkAflqZII5/eTRlbd3juCR+x8ZxV6PXPVUbYOpg8v4C U8OKnRms2DX9XiZU/08C+d7z0GBBrxFiFKMJBDq0rweVeoB6mVv+LEDXbxb4Y7e/zPxR FjG8f7m8F8rg5sRQW990BAtKEUEJXPfrkaiB8cJMPZzIb+uJLoSVgKfGrrzeenVqUixw yX6VdhNkljA8s9ucUKmv9IpgR+jznhpLpTp+DbtMYfz4XEtpCPUo+f62vrP2r31etKkx oAkh7NFRorR+aZtv3kcFX8yGuJFgLgCHuamMER1eaAzs3iy6EwJ+6uWTGSaPrf7hv14e wrmw== X-Forwarded-Encrypted: i=1; AJvYcCWHRNPx7OTMSOn/kltyz7UsC0pFBG0wU/WUdQZ9HjQrJCb55q+fBNP8njLFj1WmwIlwB++aQUaoUF1OsU0=@vger.kernel.org X-Gm-Message-State: AOJu0YwgbQzDPlozidTr52SEdMs4iZH2uiUvP0CUo6CO5TaTFETq4neS 9Si8fipGn3Yw1gwCJuVFVLgeKDL0HHvDa2GJhkAewu83Xioq3jkHK87WRQOSuImy30Mw0BZgjIx XY+PsWrZoPCliUNWjS6mdyjtxMNNOf9GBPoPWMviiKSCu4/JshOjwJT1CZlgnmiy8jQ== X-Gm-Gg: ATEYQzzXn5yzHBv1Wy+LfLgP5CU7hag58jvPKS0ykBv4hPPJrXsmSwfiRvbF1kQLtFO W9c65YKqSV8iV7EwF3uo8LNFGPNjsUunE353B5qVuOfGwcPaRqaODVc3sPLSA7U4fS8wCdCRC7s jgAUrvkDGHHET4QS4wG406238rth+pAW1qapc3Io5rgTtkz2sF9dce5QJsoH53HlP9KW2q1inWW Lg8sPpsyOhsJ0FOx7RBXSCUIvl8X95cssbSwp874BJL+QYjqm6Utdxy66E5VX3EAc57AFkoaOIg 06n4ir0Q8cZxcWyoNWHvr0Ws1yBnYOD2C2Fl42H7V3Q5oBNTi8dUnvKIa6CBKXeh51ZMHM7KvoF 7/xSp7LT19n2JHdhLiHqL2xmq8w== X-Received: by 2002:ac8:7d53:0:b0:503:2ea0:ef2a with SMTP id d75a77b69052e-507528a83e7mr213703711cf.18.1772572551545; Tue, 03 Mar 2026 13:15:51 -0800 (PST) X-Received: by 2002:ac8:7d53:0:b0:503:2ea0:ef2a with SMTP id d75a77b69052e-507528a83e7mr213703051cf.18.1772572551032; Tue, 03 Mar 2026 13:15:51 -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.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2026 13:15:50 -0800 (PST) From: Peter Colberg Date: Tue, 03 Mar 2026 16:15:29 -0500 Subject: [PATCH v3 09/10] rust: pci: add physfn(), to return PF device for VF device 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-9-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 a method to return the Physical Function (PF) device for a Virtual Function (VF) device in the bound device context. Unlike for a PCI driver written in C, guarantee that when a VF device is bound to a driver, the underlying PF device is bound to a driver, too, by always setting the flag managed_sriov in the pci_driver structure. In case SR-IOV has been enabled by a C driver that has not set the flag managed_sriov in pci_driver, return an error from physfn(). This change depends on commit a995fe1a3aa7 ("rust: driver: drop device private data post unbind") to also uphold the safety guarantee in case a (broken) PF driver re-enables SR-IOV in its unbind() callback. That commit extends the lifetime of the device private data beyond the remove_callback() wrapper. In particular, that commit ensures that the device private data for the PF device is still alive until after the function pci_iov_remove() is called and forcibly re-disables SR-IOV, which means the data can be safely accessed by VF drivers until then. Suggested-by: Danilo Krummrich Signed-off-by: Peter Colberg --- Changes in v3: - Replace SR_IOV -> SR-IOV in description. Changes in v2: - Uphold safety guarantee when PF driver is written in C. - Let physfn() return error if driver flag managed_sriov is unset. --- rust/kernel/pci.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++= +++ 1 file changed, 53 insertions(+) diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 581930d0afe98ccc29d729e4d9aab75b4144e46c..3b11f73a9f2b69a02fe003b8fea= dd61864adc8c0 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -525,6 +525,59 @@ pub fn pci_class(&self) -> Class { } } =20 +impl Device { + /// Returns the Physical Function (PF) device for a Virtual Function (= VF) device. + /// + /// # Examples + /// + /// The following example illustrates how to obtain the private driver= data of the PF device, + /// where `vf_pdev` is the VF device of reference type `&Device`= or `&Device`. + /// + /// ``` + /// # use kernel::{device::Core, pci}; + /// /// A PCI driver that binds to both the PF and its VF devices. + /// struct MyDriver; + /// + /// impl MyDriver { + /// fn connect(vf_pdev: &pci::Device) -> Result { + /// let pf_pdev =3D vf_pdev.physfn()?; + /// let pf_drvdata =3D pf_pdev.as_ref().drvdata::()?; + /// Ok(()) + /// } + /// } + /// ``` + #[cfg(CONFIG_PCI_IOV)] + pub fn physfn(&self) -> Result<&Device> { + if !self.is_virtfn() { + return Err(EINVAL); + } + + // SAFETY: `self.as_raw()` returns a valid pointer to a `struct pc= i_dev`. + // `physfn` is a valid pointer to a `struct pci_dev` since `is_vir= tfn()` is `true`. + let pf_dev =3D unsafe { (*self.as_raw()).__bindgen_anon_1.physfn }; + + // SAFETY: `pf_dev` is a valid pointer to a `struct pci_dev`. + // `driver` is either NULL or a valid pointer to a `struct pci_dri= ver`. + let pf_drv =3D unsafe { (*pf_dev).driver }; + if pf_drv.is_null() { + return Err(EINVAL); + } + + // SAFETY: `pf_drv` is a valid pointer to a `struct pci_driver`. + if !unsafe { (*pf_drv).managed_sriov } { + return Err(EINVAL); + } + + // SAFETY: `physfn` may be cast to a `Device` since= the + // driver flag `managed_sriov` forces SR-IOV to be disabled when t= he + // PF driver is unbound, i.e., all VF devices are destroyed. This + // guarantees that the underlying PF device is bound to a driver + // when the VF device is bound to a driver, which is the case since + // `Device::physfn()` requires a `&Device` reference. + Ok(unsafe { &*pf_dev.cast() }) + } +} + impl Device { /// Enable memory resources for this device. pub fn enable_device_mem(&self) -> Result { --=20 2.53.0 From nobody Thu Apr 2 00:08:13 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 520804DC52C for ; Tue, 3 Mar 2026 21:15:56 +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=1772572561; cv=none; b=e2rX5PdxbZ7047G405PS5Lfpzm9Iit9yJJzKPC5/qS5jEq7biKyqsLMNLEe9DQpfc7JAZ8yyFfQ2/V5J21EvYBw29FIwDZz2/KDrDDjL31LWeqeLjrwXF+UOiMK2PiaPEHfn23WgfKOkbiffJ+QYkvnLoarTtDbDLX4FJAzdZ8o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772572561; c=relaxed/simple; bh=mDLVxGENeh4Md25iwZTF0VnSkZ32Tz3iO/wNjalXkm4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YM6R1XYLgRVBrtzLhjpTQmwS/5/0/vh1TFa3BIwlQE1NQRbKYZ1fMqIlxL4mcfj6tucOlspJG0WqJu3E/UP/L9SZMmIs/+ECq8v4FqgNznrz9nxyl2NhbiaCPvD07WVyWqB4gPof23Jg8g3a8hwSLnjwuSmogA6F0+5xw/N7XL0= 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=g7FiJDyC; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=AwONCLrt; 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="g7FiJDyC"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="AwONCLrt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1772572555; 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=iFUtofMKhN11SJynmnwd1bk8v/kag6AshDn9xvNUK9Q=; b=g7FiJDyCEV4nBjJ5GEeqwP4/sqGLNKQgLqzVff7r6/MxcGzpwHqT5edfIoHMEWSQ2QNWV9 1YGG3klrAzaE/EJCvswkul8USCrlzgc93chvli/KgvzqV0uNhHhjbck7585KyQYnP+gsnU nUbvYREUuCzvGoNWIpr6Rv0kb+cLDnE= 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-399-ixs7weWrPKalG75iFpcx5Q-1; Tue, 03 Mar 2026 16:15:54 -0500 X-MC-Unique: ixs7weWrPKalG75iFpcx5Q-1 X-Mimecast-MFC-AGG-ID: ixs7weWrPKalG75iFpcx5Q_1772572554 Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-506a9bf9b3aso57694731cf.2 for ; Tue, 03 Mar 2026 13:15:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1772572554; x=1773177354; 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=iFUtofMKhN11SJynmnwd1bk8v/kag6AshDn9xvNUK9Q=; b=AwONCLrtJn0WkNwapgSQ4P5WATF0xLHEX8W4KQfOCO55RWdguOeFXqQ7RlLtbGafrX gF2ILOTF5Bk0/+PxX2ZWp3LFpEev55kvxohKny3adLuF8x/0iqj0gySRg8h1PulOTkGr D4DWt9046l1Xdl3TTLLULJhluzITv3d5myxWmlQLY2rE/8e9hy99dhtkVlqTjUWVJp3O 8WtVxScorLAUSLq1QZvgueNiP3rSp/AdHZLK2LuFhRv+tizuzltz7kZy690AuDKob0jK HfxUGBrNoLmbifDysXgLzo+QXhWtROtF87yDt0Wfk0yaGfcfFF6HmUZfnb9OEdxWJNsK QUtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772572554; x=1773177354; 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=iFUtofMKhN11SJynmnwd1bk8v/kag6AshDn9xvNUK9Q=; b=p6qw28NMqsLRSnxwSIxAr6m8XwV9hn9XJFvPpTfurKPhcJ24yvCJgsiqfgzU7nQ+y1 FdQyTv6UWI7O+iGtt/vvz9LYjQT6mhb6ffNG92Gbf3PTYfq/Nj0yowlozxa38CHEb5GV 1eQPfl1Hc6MkqFB34qBIUye8NwE53QhR24BUBe3C8TJlOzpubcnBoGA5HMMaN5lKXZTD YpyGoDLH6KA2nhXRX+I8p7hUG7D/xYog3A6h3SAZRX31bwY/Si4rYIPkuczmA4BW9OrD VdyGjyNwFk0e5amxrBuKRvgMD78KgQ+n7kQPO0nf2/ajt3onjZRxB+sD1evyoelBG3B1 rFHQ== X-Forwarded-Encrypted: i=1; AJvYcCXhik6G+cIXGYtSamusxNfiEcgqskXa6GjaYqJ2i+zT/hkIlxgl2Iuc2wh+09Hg3UZFDkc9HEx7OmzVvK4=@vger.kernel.org X-Gm-Message-State: AOJu0YxPdQvsv9KbdZgSEHYK+Ma75Ipi5UTvFe5nZebPRM1iXfeCTbYJ /1SAxQeLonhy2NHMjamTnpgiMHHg9FWFgOgpbqlQR4Bv7eTHnaMUkP4bmNYhVAjaxBOtws4YiA5 DNOet9EHEVdNDOAJg+hIEEpMWw5vvDU4innPfGH/kKilJuX1fj0Mv8BkUFNPA6C07sA== X-Gm-Gg: ATEYQzys8zrJKxPIKO0i6ciZCxfxuUmlcXVggNmrZKBpkjv+1TVYHgTRdKAfmbmouGl XJpAPdS5+XkqxSnsXYw5d7RxoZwvZII60JPSF3YWDbcOu8hqMZ6CSeHkuD3DrTl4l9lkfwE8+Yl ZbjTKIuYPF4JfhyExpCG1MMt4bIwXBqHdd5LzHVf2kkwVq1619ZX7FHkmMdLjhUH/gzrnRakAn0 6MeRV9a7aLJPNiFGyUjX3mY3oexwOUA8GlKyygYbp3U86S243tFKJiKl5KsSzjALagqoWrS0bHk arR0wJBw3+tMxRiWlE5JqppusXxpdXUzDaD9Wiv5u0qJe+7j61HnupVqlLdewZzzWmb/q641PnK TKKvGLMyg9Cec3Os2paEwjnoGAg== X-Received: by 2002:ac8:57d0:0:b0:4f1:ba0b:90 with SMTP id d75a77b69052e-50752840191mr212431971cf.8.1772572553724; Tue, 03 Mar 2026 13:15:53 -0800 (PST) X-Received: by 2002:ac8:57d0:0:b0:4f1:ba0b:90 with SMTP id d75a77b69052e-50752840191mr212431321cf.8.1772572553025; Tue, 03 Mar 2026 13:15:53 -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.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2026 13:15:52 -0800 (PST) From: Peter Colberg Date: Tue, 03 Mar 2026 16:15:30 -0500 Subject: [PATCH v3 10/10] samples: rust: add SR-IOV driver sample 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-10-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 a new SR-IOV driver sample that demonstrates how to enable and disable the Single Root I/O Virtualization capability for a PCI device. The sample may be exercised using QEMU's 82576 (igb) emulation. Link: https://www.qemu.org/docs/master/system/devices/igb.html Signed-off-by: Peter Colberg --- Changes in v3: - Drop redundant `.as_ref()` for `dev_*` prints. Changes in v2: - Use "kernel vertical" style on imports. - Demonstrate how to reach driver data of PF device from VF device. --- MAINTAINERS | 1 + samples/rust/Kconfig | 11 ++++ samples/rust/Makefile | 1 + samples/rust/rust_driver_sriov.rs | 127 ++++++++++++++++++++++++++++++++++= ++++ 4 files changed, 140 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 61bf550fd37c274843e516e00068bb2ab1e152ac..8551a9474fc26309d0714aafa10= 4a5e1ed29156b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -20454,6 +20454,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 c49ab910634596aea4a1a73dac87585e084f420a..f244df89c4fc9d741915f581de7= 6107e8eb0121b 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 6c0aaa58ccccfd12ef019f68ca784f6d977bc668..19d700f8210151e298cc049dacc= 249a121d0f2c4 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 0000000000000000000000000000000000000000..a4f7b99d9490f8fed2ab1fedb23= 8c53304af89ee --- /dev/null +++ b/samples/rust/rust_driver_sriov.rs @@ -0,0 +1,127 @@ +// 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::Core, + pci, + prelude::*, + sync::aref::ARef, // +}; + +use core::any::TypeId; + +#[pin_data(PinnedDrop)] +struct SampleDriver { + pdev: ARef, + private: TypeId, +} + +kernel::pci_device_table!( + PCI_TABLE, + MODULE_PCI_TABLE, + ::IdInfo, + [ + // E1000_DEV_ID_82576 + (pci::DeviceId::from_id(pci::Vendor::INTEL, 0x10c9), ()), + // E1000_DEV_ID_82576_VF + (pci::DeviceId::from_id(pci::Vendor::INTEL, 0x10ca), ()) + ] +); + +#[vtable] +impl pci::Driver for SampleDriver { + type IdInfo =3D (); + + const ID_TABLE: pci::IdTable =3D &PCI_TABLE; + + fn probe(pdev: &pci::Device, _info: &Self::IdInfo) -> impl PinIn= it { + pin_init::pin_init_scope(move || { + dev_info!( + pdev, + "Probe Rust SR-IOV driver sample (PCI ID: {}, 0x{:x}).\n", + pdev.vendor_id(), + pdev.device_id() + ); + + if pdev.is_virtfn() { + let physfn =3D pdev.physfn()?; + let drvdata =3D physfn.as_ref().drvdata::()?; + + assert!(physfn.is_physfn()); + + dev_info!( + pdev, + "Parent device is PF (PCI ID: {}, 0x{:x}).\n", + physfn.vendor_id(), + physfn.device_id() + ); + + dev_info!( + pdev, + "We have access to the private data of {:?}.\n", + drvdata.private + ); + } + + pdev.enable_device_mem()?; + pdev.set_master(); + + Ok(try_pin_init!(Self { + pdev: pdev.into(), + private: TypeId::of::() + })) + }) + } + + fn sriov_configure(pdev: &pci::Device, nr_virtfn: i32) -> Result= { + assert!(pdev.is_physfn()); + + if nr_virtfn =3D=3D 0 { + dev_info!( + pdev, + "Disable SR-IOV (PCI ID: {}, 0x{:x}).\n", + pdev.vendor_id(), + pdev.device_id() + ); + pdev.disable_sriov(); + } else { + dev_info!( + pdev, + "Enable SR-IOV (PCI ID: {}, 0x{:x}).\n", + pdev.vendor_id(), + pdev.device_id() + ); + pdev.enable_sriov(nr_virtfn)?; + } + + assert_eq!(pdev.num_vf(), nr_virtfn); + Ok(nr_virtfn) + } +} + +#[pinned_drop] +impl PinnedDrop for SampleDriver { + fn drop(self: Pin<&mut Self>) { + dev_info!(self.pdev, "Remove Rust SR-IOV driver sample.\n"); + } +} + +kernel::module_pci_driver! { + type: SampleDriver, + name: "rust_driver_sriov", + authors: ["Peter Colberg"], + description: "Rust SR-IOV driver", + license: "GPL v2", +} --=20 2.53.0