From: Zhi Wang <zhiw@nvidia.com>
To: <rust-for-linux@vger.kernel.org>
Cc: <dakr@kernel.org>, <bhelgaas@google.com>,
<kwilczynski@kernel.org>, <ojeda@kernel.org>,
<alex.gaynor@gmail.com>, <boqun.feng@gmail.com>,
<gary@garyguo.net>, <bjorn3_gh@protonmail.com>,
<lossin@kernel.org>, <a.hindborg@kernel.org>,
<aliceryhl@google.com>, <tmgross@umich.edu>,
<linux-kernel@vger.kernel.org>, <cjia@nvidia.com>,
<smitra@nvidia.com>, <ankita@nvidia.com>, <aniketa@nvidia.com>,
<kwankhede@nvidia.com>, <targupta@nvidia.com>, <zhiw@nvidia.com>,
<zhiwang@kernel.org>, <alwilliamson@nvidia.com>,
<acourbot@nvidia.com>, <joelagnelf@nvidia.com>,
<jhubbard@nvidia.com>, <jgg@nvidia.com>
Subject: [RFC 2/2] samples: rust: fwctl: add sample code for FwCtl
Date: Thu, 30 Oct 2025 16:03:13 +0000 [thread overview]
Message-ID: <20251030160315.451841-3-zhiw@nvidia.com> (raw)
In-Reply-To: <20251030160315.451841-1-zhiw@nvidia.com>
Add sample code for creating a FwCtl device, getting device info and
issuing an RPC.
Signed-off-by: Zhi Wang <zhiw@nvidia.com>
---
include/uapi/fwctl/fwctl.h | 1 +
samples/rust/Kconfig | 11 +++
samples/rust/Makefile | 1 +
samples/rust/rust_driver_fwctl.rs | 123 ++++++++++++++++++++++++++++++
4 files changed, 136 insertions(+)
create mode 100644 samples/rust/rust_driver_fwctl.rs
diff --git a/include/uapi/fwctl/fwctl.h b/include/uapi/fwctl/fwctl.h
index 716ac0eee42d..eea1020ad180 100644
--- a/include/uapi/fwctl/fwctl.h
+++ b/include/uapi/fwctl/fwctl.h
@@ -45,6 +45,7 @@ enum fwctl_device_type {
FWCTL_DEVICE_TYPE_MLX5 = 1,
FWCTL_DEVICE_TYPE_CXL = 2,
FWCTL_DEVICE_TYPE_PDS = 4,
+ FWCTL_DEVICE_TYPE_RUST_FWCTL_TEST = 8,
};
/**
diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
index c376eb899b7a..54ed2b0b86e2 100644
--- a/samples/rust/Kconfig
+++ b/samples/rust/Kconfig
@@ -138,6 +138,17 @@ config SAMPLE_RUST_DRIVER_AUXILIARY
If unsure, say N.
+config SAMPLE_RUST_DRIVER_FWCTL
+ tristate "Fwctl Driver"
+ depends on FWCTL
+ help
+ This option builds the Rust Fwctl driver sample.
+
+ To compile this as a module, choose M here:
+ the module will be called rust_driver_fwctl.
+
+ If unsure, say N.
+
config SAMPLE_RUST_HOSTPROGS
bool "Host programs"
help
diff --git a/samples/rust/Makefile b/samples/rust/Makefile
index cf8422f8f219..643208c2380e 100644
--- a/samples/rust/Makefile
+++ b/samples/rust/Makefile
@@ -12,6 +12,7 @@ obj-$(CONFIG_SAMPLE_RUST_DRIVER_PLATFORM) += rust_driver_platform.o
obj-$(CONFIG_SAMPLE_RUST_DRIVER_USB) += rust_driver_usb.o
obj-$(CONFIG_SAMPLE_RUST_DRIVER_FAUX) += rust_driver_faux.o
obj-$(CONFIG_SAMPLE_RUST_DRIVER_AUXILIARY) += rust_driver_auxiliary.o
+obj-$(CONFIG_SAMPLE_RUST_DRIVER_FWCTL) += rust_driver_fwctl.o
obj-$(CONFIG_SAMPLE_RUST_CONFIGFS) += rust_configfs.o
rust_print-y := rust_print_main.o rust_print_events.o
diff --git a/samples/rust/rust_driver_fwctl.rs b/samples/rust/rust_driver_fwctl.rs
new file mode 100644
index 000000000000..386299eaf82c
--- /dev/null
+++ b/samples/rust/rust_driver_fwctl.rs
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Rust fwctl API test (based on QEMU's `pci-testdev`).
+//!
+//! To make this driver probe, QEMU must be run with `-device pci-testdev`.
+
+use kernel::{
+ bindings, device::Core, fwctl, fwctl::FwCtlOps, fwctl::FwCtlUCtx, pci, prelude::*,
+ sync::aref::ARef,
+};
+
+struct FwCtlSampleUCtx {
+ _drvdata: u32,
+}
+
+struct FwCtlSampleOps;
+
+impl FwCtlOps for FwCtlSampleOps {
+ type UCtx = FwCtlSampleUCtx;
+
+ const DEVICE_TYPE: u32 = bindings::fwctl_device_type_FWCTL_DEVICE_TYPE_RUST_FWCTL_TEST as u32;
+
+ fn open_uctx(uctx: &mut FwCtlUCtx<FwCtlSampleUCtx>) -> Result<(), Error> {
+ let dev = uctx.get_parent_device();
+
+ dev_info!(dev, "fwctl test driver: open_uctx().\n");
+ Ok(())
+ }
+
+ fn close_uctx(uctx: &mut FwCtlUCtx<FwCtlSampleUCtx>) {
+ let dev = uctx.get_parent_device();
+
+ dev_info!(dev, "fwctl test driver: close_uctx().\n");
+ }
+
+ fn info(uctx: &mut FwCtlUCtx<FwCtlSampleUCtx>) -> Result<KVec<u8>, Error> {
+ let dev = uctx.get_parent_device();
+
+ dev_info!(dev, "fwctl test driver: info().\n");
+
+ let mut infobuf = KVec::<u8>::new();
+ infobuf.push(0xef, GFP_KERNEL)?;
+ infobuf.push(0xbe, GFP_KERNEL)?;
+ infobuf.push(0xad, GFP_KERNEL)?;
+ infobuf.push(0xde, GFP_KERNEL)?;
+
+ Ok(infobuf)
+ }
+
+ fn fw_rpc(
+ uctx: &mut FwCtlUCtx<FwCtlSampleUCtx>,
+ scope: u32,
+ rpc_in: &mut [u8],
+ _out_len: *mut usize,
+ ) -> Result<Option<KVec<u8>>, Error> {
+ let dev = uctx.get_parent_device();
+
+ dev_info!(dev, "fwctl test driver: fw_rpc() scope {}.\n", scope);
+
+ if rpc_in.len() != 4 {
+ return Err(EINVAL);
+ }
+
+ dev_info!(
+ dev,
+ "fwctl test driver: inbuf len{} bytes[0-3] {:x} {:x} {:x} {:x}.\n",
+ rpc_in.len(),
+ rpc_in[0],
+ rpc_in[1],
+ rpc_in[2],
+ rpc_in[3]
+ );
+
+ let mut outbuf = KVec::<u8>::new();
+ outbuf.push(0xef, GFP_KERNEL)?;
+ outbuf.push(0xbe, GFP_KERNEL)?;
+ outbuf.push(0xad, GFP_KERNEL)?;
+ outbuf.push(0xde, GFP_KERNEL)?;
+
+ Ok(Some(outbuf))
+ }
+}
+
+#[pin_data]
+struct FwCtlSampleDriver {
+ pdev: ARef<pci::Device>,
+ #[pin]
+ fwctl: fwctl::Registration<FwCtlSampleOps>,
+}
+
+kernel::pci_device_table!(
+ PCI_TABLE,
+ MODULE_PCI_TABLE,
+ <FwCtlSampleDriver as pci::Driver>::IdInfo,
+ [(pci::DeviceId::from_id(pci::Vendor::REDHAT, 0x5), ())]
+);
+
+impl pci::Driver for FwCtlSampleDriver {
+ type IdInfo = ();
+ const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
+
+ fn probe(pdev: &pci::Device<Core>, _info: &Self::IdInfo) -> Result<Pin<KBox<Self>>> {
+ dev_info!(pdev.as_ref(), "Probe fwctl test driver.\n");
+
+ let drvdata = KBox::pin_init(
+ try_pin_init!(Self {
+ pdev: pdev.into(),
+ fwctl <- fwctl::Registration::<FwCtlSampleOps>::new(pdev.as_ref())?,
+ }),
+ GFP_KERNEL,
+ )?;
+
+ Ok(drvdata)
+ }
+}
+
+kernel::module_pci_driver! {
+ type: FwCtlSampleDriver,
+ name: "rust_driver_fwctl",
+ authors: ["Zhi Wang"],
+ description: "Rust fwctl test",
+ license: "GPL v2",
+}
--
2.47.3
next prev parent reply other threads:[~2025-10-30 16:03 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-30 16:03 [RFC 0/2] rust: introduce abstractions for fwctl Zhi Wang
2025-10-30 16:03 ` [RFC 1/2] " Zhi Wang
2025-10-30 16:22 ` Jason Gunthorpe
2025-10-30 17:19 ` Zhi Wang
2025-10-30 17:24 ` Danilo Krummrich
2025-10-30 17:21 ` Danilo Krummrich
2025-10-30 16:47 ` Danilo Krummrich
2025-11-02 17:26 ` Danilo Krummrich
2025-11-02 22:52 ` Jason Gunthorpe
2025-11-02 18:33 ` Danilo Krummrich
2025-11-02 22:55 ` Jason Gunthorpe
2025-11-03 9:55 ` Zhi Wang
2025-11-03 10:36 ` Danilo Krummrich
2025-10-30 16:03 ` Zhi Wang [this message]
2025-10-30 17:29 ` [RFC 0/2] " Zhi Wang
2025-10-30 17:52 ` Danilo Krummrich
2025-10-30 17:54 ` Jason Gunthorpe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251030160315.451841-3-zhiw@nvidia.com \
--to=zhiw@nvidia.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=alwilliamson@nvidia.com \
--cc=aniketa@nvidia.com \
--cc=ankita@nvidia.com \
--cc=bhelgaas@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=cjia@nvidia.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=jgg@nvidia.com \
--cc=jhubbard@nvidia.com \
--cc=joelagnelf@nvidia.com \
--cc=kwankhede@nvidia.com \
--cc=kwilczynski@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=smitra@nvidia.com \
--cc=targupta@nvidia.com \
--cc=tmgross@umich.edu \
--cc=zhiwang@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).