Re: [PATCH v2 1/2] rust: introduce abstractions for fwctlg

Danilo Krummrich
2026-01-28T16:35:11Z

On Wed Jan 28, 2026 at 4:56 PM CET, Jason Gunthorpe wrote:
> On Wed, Jan 28, 2026 at 04:49:07PM +0100, Danilo Krummrich wrote:
>
>> // Initialize the `data` initializer within the memory pointed
>> // to by `raw_data`.
>> unsafe { data.__pinned_init(raw_data) }.inspect_err(|_| {
>
>> So, essentially the driver passes an initializer of its private data and we
>> "write" this initializer into the extra memory allocated with
>> _fwctl_alloc_device().
>
> This all seems like the right way to do it!
>
> My only remark it that it still doesn't give an opportunity to call a
> function between init and register.

You would, from a driver side it would look like this:

#[pin_data]
struct MyDriver {
#[pin]
_reg: Devres<fwctl::Registration>,
fwctl: ARef<fwctl::Device>,
}

#[pin_data]
struct FwctlData {
#[pin]
foo: Mutex<Foo>,
...,
}

impl pci::Driver for MyDriver {
fn probe(
pdev: &pci::Device<Core>,
_info: &Self::IdInfo,
) -> impl PinInit<Self, Error> {
let fwctl = fwctl::Device::new(
pdev.as_ref(),
try_pin_init!(FwctlData {
foo <- mutex_new!(Foo::new()),
...,
}),
)?;

// Let's do something with the `fwctl::Device` before we
// register it.
fwctl.do_stuff();

try_pin_init!(Self {
// We could omit this and instead provide
// `fwctl::Registration::register()`, which does only return
// a `Result` and keeps the `fwct::Device` registered until
// driver unbind.
_reg <- fwctl::Registration::new(pdev.as_ref(), fwctl);
fwctl,
})
}
}

> __pinned_init() is taking the T type without access to the initialized
> fwctl_device

If a driver, in order to come up with its private data (FwctlData in the example
above), needs a struct fwctl_device, we could make this happen as well. For
instance, fwctl::Device::new() could take a closure that has a valid
representation of a struct fwctl_device as argument. But I think that's not
necessary.

> So if I add some function drivers need to call between init and register:
>
> fwctl_XYZ(fwctl, ..)
>
> It is not possible?

As you can see from the example above, that's possible.

> Or is it needed to add the typestate?

This is something we should consider when a fwctl::Device would have different
states it can be in, where calling certain methods of a fwctl::Device is only
valid for a certain state and would cause undefined behavior if called from the
wrong state.