[PATCH v3 2/3] rust: net::phy represent DeviceId as transparent wrapper over mdio_device_id

FUJITA Tomonori posted 3 patches 3 months ago
There is a newer version of this series
[PATCH v3 2/3] rust: net::phy represent DeviceId as transparent wrapper over mdio_device_id
Posted by FUJITA Tomonori 3 months ago
Refactor the DeviceId struct to be a #[repr(transparent)] wrapper
around the C struct bindings::mdio_device_id.

This refactoring is a preparation for enabling the PHY abstractions to
use device_id trait.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
---
 rust/kernel/net/phy.rs | 53 +++++++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 26 deletions(-)

diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
index 65ac4d59ad77..940972ffadae 100644
--- a/rust/kernel/net/phy.rs
+++ b/rust/kernel/net/phy.rs
@@ -507,7 +507,7 @@ pub const fn create_phy_driver<T: Driver>() -> DriverVTable {
     DriverVTable(Opaque::new(bindings::phy_driver {
         name: T::NAME.as_char_ptr().cast_mut(),
         flags: T::FLAGS,
-        phy_id: T::PHY_DEVICE_ID.id,
+        phy_id: T::PHY_DEVICE_ID.id(),
         phy_id_mask: T::PHY_DEVICE_ID.mask_as_int(),
         soft_reset: if T::HAS_SOFT_RESET {
             Some(Adapter::<T>::soft_reset_callback)
@@ -691,42 +691,41 @@ fn drop(&mut self) {
 ///
 /// Represents the kernel's `struct mdio_device_id`. This is used to find an appropriate
 /// PHY driver.
-pub struct DeviceId {
-    id: u32,
-    mask: DeviceMask,
-}
+#[repr(transparent)]
+#[derive(Clone, Copy)]
+pub struct DeviceId(bindings::mdio_device_id);
 
 impl DeviceId {
     /// Creates a new instance with the exact match mask.
     pub const fn new_with_exact_mask(id: u32) -> Self {
-        DeviceId {
-            id,
-            mask: DeviceMask::Exact,
-        }
+        Self(bindings::mdio_device_id {
+            phy_id: id,
+            phy_id_mask: DeviceMask::Exact.as_int(),
+        })
     }
 
     /// Creates a new instance with the model match mask.
     pub const fn new_with_model_mask(id: u32) -> Self {
-        DeviceId {
-            id,
-            mask: DeviceMask::Model,
-        }
+        Self(bindings::mdio_device_id {
+            phy_id: id,
+            phy_id_mask: DeviceMask::Model.as_int(),
+        })
     }
 
     /// Creates a new instance with the vendor match mask.
     pub const fn new_with_vendor_mask(id: u32) -> Self {
-        DeviceId {
-            id,
-            mask: DeviceMask::Vendor,
-        }
+        Self(bindings::mdio_device_id {
+            phy_id: id,
+            phy_id_mask: DeviceMask::Vendor.as_int(),
+        })
     }
 
     /// Creates a new instance with a custom match mask.
     pub const fn new_with_custom_mask(id: u32, mask: u32) -> Self {
-        DeviceId {
-            id,
-            mask: DeviceMask::Custom(mask),
-        }
+        Self(bindings::mdio_device_id {
+            phy_id: id,
+            phy_id_mask: DeviceMask::Custom(mask).as_int(),
+        })
     }
 
     /// Creates a new instance from [`Driver`].
@@ -734,18 +733,20 @@ pub const fn new_with_driver<T: Driver>() -> Self {
         T::PHY_DEVICE_ID
     }
 
+    /// Get a `phy_id` as u32.
+    pub const fn id(&self) -> u32 {
+        self.0.phy_id
+    }
+
     /// Get a `mask` as u32.
     pub const fn mask_as_int(&self) -> u32 {
-        self.mask.as_int()
+        self.0.phy_id_mask
     }
 
     // macro use only
     #[doc(hidden)]
     pub const fn mdio_device_id(&self) -> bindings::mdio_device_id {
-        bindings::mdio_device_id {
-            phy_id: self.id,
-            phy_id_mask: self.mask.as_int(),
-        }
+        self.0
     }
 }
 
-- 
2.43.0
Re: [PATCH v3 2/3] rust: net::phy represent DeviceId as transparent wrapper over mdio_device_id
Posted by Trevor Gross 3 months ago
On Fri Jul 4, 2025 at 12:10 AM EDT, FUJITA Tomonori wrote:
> Refactor the DeviceId struct to be a #[repr(transparent)] wrapper
> around the C struct bindings::mdio_device_id.
>
> This refactoring is a preparation for enabling the PHY abstractions to
> use device_id trait.

Should this say "the `DeviceId` trait" (different case)?

> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
> ---
>  rust/kernel/net/phy.rs | 53 +++++++++++++++++++++---------------------
>  1 file changed, 27 insertions(+), 26 deletions(-)
>
> diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
> index 65ac4d59ad77..940972ffadae 100644
> --- a/rust/kernel/net/phy.rs
> +++ b/rust/kernel/net/phy.rs
> [...]
> @@ -734,18 +733,20 @@ pub const fn new_with_driver<T: Driver>() -> Self {
>          T::PHY_DEVICE_ID
>      }
>  
> +    /// Get a `phy_id` as u32.
> +    pub const fn id(&self) -> u32 {
> +        self.0.phy_id
> +    }

For the docs maybe just:

    /// Get the MDIO device's phy ID.

Since `as u32` is slightly redundant (it's in the return type, and that
is how it is stored anyway).

>      /// Get a `mask` as u32.
>      pub const fn mask_as_int(&self) -> u32 {
> -        self.mask.as_int()
> +        self.0.phy_id_mask
>      }

One optional nit then:

Reviewed-by: Trevor Gross <tmgross@umich.edu>
Re: [PATCH v3 2/3] rust: net::phy represent DeviceId as transparent wrapper over mdio_device_id
Posted by FUJITA Tomonori 2 months, 4 weeks ago
On Tue, 08 Jul 2025 23:23:20 -0400
"Trevor Gross" <tmgross@umich.edu> wrote:

> On Fri Jul 4, 2025 at 12:10 AM EDT, FUJITA Tomonori wrote:
>> Refactor the DeviceId struct to be a #[repr(transparent)] wrapper
>> around the C struct bindings::mdio_device_id.
>>
>> This refactoring is a preparation for enabling the PHY abstractions to
>> use device_id trait.
> 
> Should this say "the `DeviceId` trait" (different case)?

Ah, I changed it to the RawDeviceId trait.

>> Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
>> ---
>>  rust/kernel/net/phy.rs | 53 +++++++++++++++++++++---------------------
>>  1 file changed, 27 insertions(+), 26 deletions(-)
>>
>> diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs
>> index 65ac4d59ad77..940972ffadae 100644
>> --- a/rust/kernel/net/phy.rs
>> +++ b/rust/kernel/net/phy.rs
>> [...]
>> @@ -734,18 +733,20 @@ pub const fn new_with_driver<T: Driver>() -> Self {
>>          T::PHY_DEVICE_ID
>>      }
>>  
>> +    /// Get a `phy_id` as u32.
>> +    pub const fn id(&self) -> u32 {
>> +        self.0.phy_id
>> +    }
> 
> For the docs maybe just:
> 
>     /// Get the MDIO device's phy ID.
> 
> Since `as u32` is slightly redundant (it's in the return type, and that
> is how it is stored anyway).

Yeah, fixed. I used "PHY" for consistency with other comments.

>>      /// Get a `mask` as u32.
>>      pub const fn mask_as_int(&self) -> u32 {
>> -        self.mask.as_int()
>> +        self.0.phy_id_mask
>>      }

I also updated the above comment

/// Get the MDIO device's match mask.

> One optional nit then:
> 
> Reviewed-by: Trevor Gross <tmgross@umich.edu>

Thanks!