public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Zhi Wang" <zhiw@nvidia.com>, <rust-for-linux@vger.kernel.org>,
	<linux-pci@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Cc: <dakr@kernel.org>, <aliceryhl@google.com>, <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>, <tmgross@umich.edu>,
	<markus.probst@posteo.de>, <helgaas@kernel.org>,
	<cjia@nvidia.com>, <smitra@nvidia.com>, <ankita@nvidia.com>,
	<aniketa@nvidia.com>, <kwankhede@nvidia.com>,
	<targupta@nvidia.com>, <acourbot@nvidia.com>,
	<joelagnelf@nvidia.com>, <jhubbard@nvidia.com>,
	<zhiwang@kernel.org>, <daniel.almeida@collabora.com>,
	"Jason Gunthorpe" <jgg@nvidia.com>
Subject: Re: [PATCH v2 2/2] samples: rust: fwctl: add sample code for fwctl
Date: Mon, 26 Jan 2026 17:59:06 +0000	[thread overview]
Message-ID: <DFYPSR8ECE0O.2BYC40RIOVHCX@garyguo.net> (raw)
In-Reply-To: <20260122204232.15988-3-zhiw@nvidia.com>

On Thu Jan 22, 2026 at 8:42 PM GMT, Zhi Wang wrote:
> This patch adds a sample Rust driver demonstrating the usage of the fwctl
> Rust abstractions. Add sample code for creating a FwCtl device, getting
> device info and issuing an RPC.
>
> Cc: Danilo Krummrich <dakr@kernel.org>
> Cc: Jason Gunthorpe <jgg@nvidia.com>
> Signed-off-by: Zhi Wang <zhiw@nvidia.com>
> ---
>  samples/rust/Kconfig              |  11 +++
>  samples/rust/Makefile             |   1 +
>  samples/rust/rust_driver_fwctl.rs | 136 ++++++++++++++++++++++++++++++
>  3 files changed, 148 insertions(+)
>  create mode 100644 samples/rust/rust_driver_fwctl.rs
>
> diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
> index c49ab9106345..3d0b223caa89 100644
> --- a/samples/rust/Kconfig
> +++ b/samples/rust/Kconfig
> @@ -172,6 +172,17 @@ config SAMPLE_RUST_SOC
>  
>  	  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 6c0aaa58cccc..6f6030e64727 100644
> --- a/samples/rust/Makefile
> +++ b/samples/rust/Makefile
> @@ -14,6 +14,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
>  obj-$(CONFIG_SAMPLE_RUST_SOC)			+= rust_soc.o
>  
> diff --git a/samples/rust/rust_driver_fwctl.rs b/samples/rust/rust_driver_fwctl.rs
> new file mode 100644
> index 000000000000..ac5f979fd73b
> --- /dev/null
> +++ b/samples/rust/rust_driver_fwctl.rs
> @@ -0,0 +1,136 @@
> +// 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,
> +    device::Core,
> +    devres::Devres,
> +    fwctl,
> +    pci,
> +    prelude::*,
> +    sync::aref::ARef,
> +    types,
> +};
> +
> +struct FwctlSampleUserCtx {
> +    _drvdata: u32,
> +}
> +
> +struct FwctlSampleOps;
> +
> +impl fwctl::Operations for FwctlSampleOps {
> +    type UserCtx = FwctlSampleUserCtx;
> +
> +    const DEVICE_TYPE: fwctl::DeviceType = fwctl::DeviceType::RustFwctlTest;
> +
> +    fn open(
> +        fwctl_uctx: &types::Opaque<bindings::fwctl_uctx>
> +    ) -> Result<impl PinInit<Self::UserCtx, Error>, Error> {
> +        let dev = fwctl::UserCtx::<Self::UserCtx>::parent_device_from_raw(fwctl_uctx);

Why does this raw binding need to be handled by the user?

> +
> +        dev_info!(dev, "fwctl test driver: open_uctx()");
> +
> +        // Return an initializer for the user context.
> +        // The framework will initialize this in-place in the C-allocated memory.
> +        Ok(try_init!(FwctlSampleUserCtx {
> +            _drvdata: 0,
> +        }))
> +    }
> +
> +    fn close(uctx: &mut fwctl::UserCtx<FwctlSampleUserCtx>) {
> +        let dev = uctx.get_parent_device();
> +
> +        dev_info!(dev, "fwctl test driver: close_uctx()");
> +    }
> +
> +    fn info(uctx: &mut fwctl::UserCtx<FwctlSampleUserCtx>) -> Result<KVec<u8>, Error> {

This can just be `Result<KVec<u8>>`.

Best,
Gary

> +        let dev = uctx.get_parent_device();
> +
> +        dev_info!(dev, "fwctl test driver: info()");
> +
> +        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 fwctl::UserCtx<FwctlSampleUserCtx>,
> +        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 {}", scope);
> +
> +        if rpc_in.len() != 4 {
> +            return Err(EINVAL);
> +        }
> +
> +        dev_info!(
> +            dev,
> +            "fwctl test driver: inbuf len{} bytes[0-3] {:x} {:x} {:x} {:x}",
> +            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: Devres<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) -> impl PinInit<Self, Error> {
> +        dev_info!(pdev.as_ref(), "Probe fwctl test driver");
> +
> +        // `pdev` is `Device<Core>`, which derefs to `Device<Bound>` during probe.
> +        let pdev_bound: &pci::Device<device::Bound> = pdev;
> +
> +        try_pin_init!(Self {
> +            pdev: pdev.into(),
> +            fwctl <- fwctl::Registration::<FwctlSampleOps>::new(pdev_bound.as_ref()),
> +        })
> +    }
> +}
> +
> +kernel::module_pci_driver! {
> +    type: FwctlSampleDriver,
> +    name: "rust_driver_fwctl",
> +    authors: ["Zhi Wang"],
> +    description: "Rust fwctl test",
> +    license: "GPL v2",
> +    imports_ns: ["FWCTL"],
> +}


  parent reply	other threads:[~2026-01-26 17:59 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-22 20:42 [PATCH v2 0/2] rust: introduce abstractions for fwctl Zhi Wang
2026-01-22 20:42 ` [PATCH v2 1/2] " Zhi Wang
2026-01-22 21:17   ` Joel Fernandes
2026-01-23 10:25     ` Zhi Wang
2026-01-26 17:48   ` Gary Guo
2026-01-27 19:59     ` Zhi Wang
2026-01-26 18:19   ` Jason Gunthorpe
2026-01-27 19:57     ` Zhi Wang
2026-01-27 20:07       ` Danilo Krummrich
2026-01-28  0:04         ` Jason Gunthorpe
2026-01-28  1:21           ` Danilo Krummrich
2026-01-28 13:20             ` [PATCH v2 1/2] rust: introduce abstractions for fwctlg Jason Gunthorpe
2026-01-28 14:01               ` Danilo Krummrich
2026-01-28 14:59                 ` Jason Gunthorpe
2026-01-28 15:49                   ` Danilo Krummrich
2026-01-28 15:56                     ` Jason Gunthorpe
2026-01-28 16:35                       ` Danilo Krummrich
2026-01-28 16:39                         ` Jason Gunthorpe
2026-01-28 17:26                           ` Zhi Wang
2026-01-28 17:30                         ` Zhi Wang
2026-01-28 17:39                           ` Jason Gunthorpe
2026-01-28 17:40                           ` Danilo Krummrich
2026-01-28 11:36         ` [PATCH v2 1/2] rust: introduce abstractions for fwctl Zhi Wang
2026-01-28 11:41           ` Danilo Krummrich
2026-01-27 20:09       ` Danilo Krummrich
2026-01-22 20:42 ` [PATCH v2 2/2] samples: rust: fwctl: add sample code " Zhi Wang
2026-01-22 20:58   ` Jason Gunthorpe
2026-01-22 21:06     ` Danilo Krummrich
2026-01-22 21:16       ` John Hubbard
2026-01-23 10:23         ` Zhi Wang
2026-01-26 17:59   ` Gary Guo [this message]
2026-01-22 21:32 ` [PATCH v2 0/2] rust: introduce abstractions " Danilo Krummrich
2026-01-23 10:14   ` Zhi Wang

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=DFYPSR8ECE0O.2BYC40RIOVHCX@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.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=daniel.almeida@collabora.com \
    --cc=helgaas@kernel.org \
    --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=linux-pci@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=markus.probst@posteo.de \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=smitra@nvidia.com \
    --cc=targupta@nvidia.com \
    --cc=tmgross@umich.edu \
    --cc=zhiw@nvidia.com \
    --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