rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Benno Lossin <benno.lossin@proton.me>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: rust-for-linux@vger.kernel.org, andrew@lunn.ch
Subject: Re: [RFC PATCH v1 1/4] rust: core abstractions for network PHY drivers
Date: Mon, 18 Sep 2023 15:20:53 +0000	[thread overview]
Message-ID: <dfc87664-24c3-88ac-ec34-2b65e898628d@proton.me> (raw)
In-Reply-To: <20230918.220905.1497497848303760774.ubuntu@gmail.com>

On 9/18/23 15:09, FUJITA Tomonori wrote:
> Hi,
> 
> On Mon, 18 Sep 2023 10:22:22 +0000
> Benno Lossin <benno.lossin@proton.me> wrote:
> 
>>   > +/// PHY is internal.
>>   > +pub const PHY_IS_INTERNAL: u32 = 0x00000001;
>>   > +/// PHY needs to be reset after the refclk is enabled.
>>   > +pub const PHY_RST_AFTER_CLK_EN: u32 = 0x00000002;
>>   > +/// Polling is used to detect PHY status changes.
>>   > +pub const PHY_POLL_CABLE_TEST: u32 = 0x00000004;
>>   > +/// Don't suspend.
>>   > +pub const PHY_ALWAYS_CALL_SUSPEND: u32 = 0x00000008;
>>
>> I think we should create a macro for creating flags, it should create a
>> newtype and
>> work similar to an enum declaration. On the newtype it should implement
>> the `Or` trait
>> for allowing `PHY_IS_INTERNAL | PHY_RST_AFTER_CLK_EN`.
> 
> You are thinking about something like:
> https://crates.io/crates/enumflags2
> 
> ?

I would prefer https://crates.io/crates/bitflags
because this library does not create a separate type for multiple flags.

I will look into this and see with what I come up.

> 
>>   > +/// Registration structure for a PHY driver.
>>   > +///
>>   > +/// `Registration` can keep multiple `phy_drivers` object because
>>   > +/// commonly one module implements multiple PHYs drivers.
>>   > +pub struct Registration<const N: usize> {
>>   > +    module: &'static crate::ThisModule,
>>   > +    drivers: [Option<Box<bindings::phy_driver>>; N],
>>
>> Why is this not a vector? You have to allocate in `register` anyways.
>> You could also use `Vec::try_with_capacity(N)` to initialize the vector with
>> capacity for N elements.
> 
> I thought that this is simpler. What are advantages of Vec over this?
> 

Pushing a new value is a lot simpler, since it only is a single function 
call and not a loop. You also do not need a generic parameter.

> 
>>   > +impl<const N: usize> Registration<{ N }> {
>>   > +    /// Creates a new `Registration` instance.
>>   > +    pub fn new(module: &'static crate::ThisModule) -> Self {
>>   > +        const NONE: Option<Box<bindings::phy_driver>> = None;
>>   > +        Registration {
>>   > +            module,
>>   > +            drivers: [NONE; N],
>>   > +        }
>>   > +    }
>>   > +
>>   > +    /// Registers a PHY driver.
>>   > +    pub fn register<T: Driver>(&mut self, adapter: &Adapter<T>) ->
>> Result {
>>
>> The way this function is used in the driver makes me think that the
>> `Adapter`
>> type does not have to be public. So I would suggest to change the
>> signature to
>> `pub fn register<T: Driver>(&mut self, name: &'static CStr) -> Result`.
> 
> Ah, right. I'll in the next round.
> 
>>
>>   > +        for i in 0..N {
>>   > +            if self.drivers[i].is_none() {
>>   > +                let mut drv = Box::try_new(adapter.driver())?;
>>   > +                // SAFETY: Just an FFI call.
>>   > +                let ret = unsafe {
>> bindings::phy_driver_register(drv.as_mut(), self.module.0) };
>>
>> This call exposes the driver to the C side and thus it is able to be
>> modified at any time,
>> which means that in Rust it should be put into an `UnsafeCell`, better
>> even in this case it
>> should just be `Opaque`, so the `drivers` fields should be of type
>> `[Option<Box<Opaque<bindings::phy_driver>>>]`.
> 
> Understood.

Actually, I talked with Gary and what I suggested is actually not 
sufficient, because `Box` means that we have a unique pointer, even 
though it contains an Opaque. So what we would suggest would be to use 
`Box::into_raw` then both store that pointer and give it to 
`phy_driver_register`.

> 
>> It would also be a good idea to pin it, since the C side will rely on
>> the pointee not moving
>> and it will prevent accidentally moving it.
> 
> The above drv is touched by only this file. What possible cases where
> it would be moved?
> 

If you do what I wrote above, then this is not a problem. The rationale 
behind this suggestion was to make it impossible to move the value in a 
future modification of this file.


  reply	other threads:[~2023-09-18 16:39 UTC|newest]

Thread overview: 79+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-13 13:36 [RFC PATCH v1 0/4] Rust abstractions for network PHY drivers FUJITA Tomonori
2023-09-13 13:36 ` [RFC PATCH v1 1/4] rust: core " FUJITA Tomonori
2023-09-13 20:11   ` Andrew Lunn
2023-09-13 20:49     ` Boqun Feng
2023-09-13 21:05       ` Andrew Lunn
2023-09-13 21:32         ` Boqun Feng
2023-09-14  4:10           ` Trevor Gross
2023-09-13 22:14     ` Miguel Ojeda
2023-09-14  0:30       ` Andrew Lunn
2023-09-14 11:03         ` Miguel Ojeda
2023-09-14 12:24           ` Andrew Lunn
2023-09-17  9:44           ` FUJITA Tomonori
2023-09-17 10:17     ` FUJITA Tomonori
2023-09-17 15:06       ` Andrew Lunn
2023-09-17 18:42         ` Trevor Gross
2023-09-17 19:08           ` Andrew Lunn
2023-09-18  0:49             ` Trevor Gross
2023-09-18  1:18               ` Andrew Lunn
2023-09-18  2:22                 ` Trevor Gross
2023-09-18  3:44                   ` FUJITA Tomonori
2023-09-18 13:13                     ` Andrew Lunn
2023-09-18  6:01         ` FUJITA Tomonori
2023-09-14  5:47   ` Trevor Gross
2023-09-14 10:17     ` Jarkko Sakkinen
2023-09-14 19:46       ` Trevor Gross
2023-09-14 12:39     ` Andrew Lunn
2023-09-14 19:42       ` Trevor Gross
2023-09-14 19:53         ` Trevor Gross
2023-09-18  9:56   ` Finn Behrens
2023-09-18 13:22     ` Andrew Lunn
2023-09-18 10:22   ` Benno Lossin
2023-09-18 13:09     ` FUJITA Tomonori
2023-09-18 15:20       ` Benno Lossin [this message]
2023-09-19 10:26         ` FUJITA Tomonori
2023-09-20 13:24           ` Benno Lossin
2023-09-13 13:36 ` [RFC PATCH v1 2/4] rust: phy: add module device table support FUJITA Tomonori
2023-09-14  6:26   ` Trevor Gross
2023-09-14  7:23     ` Trevor Gross
2023-09-17  6:30       ` FUJITA Tomonori
2023-09-17 15:13         ` Andrew Lunn
2023-09-13 13:36 ` [RFC PATCH v1 3/4] MAINTAINERS: add Rust PHY abstractions file to the ETHERNET PHY LIBRARY FUJITA Tomonori
2023-09-13 18:57   ` Andrew Lunn
2023-09-17 12:32     ` FUJITA Tomonori
2023-09-19 12:06       ` Miguel Ojeda
2023-09-19 16:33         ` Andrew Lunn
2023-09-22 23:17           ` Trevor Gross
2023-09-23  0:05           ` Miguel Ojeda
2023-09-23  1:36             ` Andrew Lunn
2023-09-23 10:19               ` Miguel Ojeda
2023-09-23 14:42                 ` Andrew Lunn
2023-10-09 12:26                   ` Miguel Ojeda
2023-09-13 13:36 ` [RFC PATCH v1 4/4] sample: rust: add Asix PHY driver FUJITA Tomonori
2023-09-13 14:11   ` Andrew Lunn
2023-09-13 16:53     ` Greg KH
2023-09-13 18:50       ` Andrew Lunn
2023-09-13 18:59         ` Greg KH
2023-09-13 20:20           ` Andrew Lunn
2023-09-13 20:38             ` Greg KH
2023-09-13 16:56   ` Greg KH
2023-09-13 18:43     ` Andrew Lunn
2023-09-13 19:15   ` Andrew Lunn
2023-09-17 11:28     ` FUJITA Tomonori
2023-09-17 15:46       ` Andrew Lunn
2023-09-18  8:35         ` FUJITA Tomonori
2023-09-18 13:37           ` Andrew Lunn
2023-09-24  9:12             ` FUJITA Tomonori
2023-09-24  9:59               ` Miguel Ojeda
2023-09-24 15:31               ` Andrew Lunn
2023-09-24 17:31                 ` Miguel Ojeda
2023-09-24 17:44                   ` Andrew Lunn
2023-09-24 18:44                     ` Miguel Ojeda
2023-09-18 22:23 ` [RFC PATCH v1 0/4] Rust abstractions for network PHY drivers Trevor Gross
2023-09-18 22:48   ` Andrew Lunn
2023-09-18 23:46     ` Trevor Gross
2023-09-19  6:24       ` FUJITA Tomonori
2023-09-19  7:41         ` Trevor Gross
2023-09-19 16:12         ` Andrew Lunn
2023-09-19  6:16   ` FUJITA Tomonori
2023-09-19  8:05     ` Trevor Gross

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=dfc87664-24c3-88ac-ec34-2b65e898628d@proton.me \
    --to=benno.lossin@proton.me \
    --cc=andrew@lunn.ch \
    --cc=fujita.tomonori@gmail.com \
    --cc=rust-for-linux@vger.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).