Rust for Linux List
 help / color / mirror / Atom feed
From: Andrew Lunn <andrew@lunn.ch>
To: FUJITA Tomonori <fujita.tomonori@gmail.com>
Cc: netdev@vger.kernel.org, rust-for-linux@vger.kernel.org,
	tmgross@umich.edu, miguel.ojeda.sandonis@gmail.com,
	benno.lossin@proton.me, aliceryhl@google.com
Subject: Re: [PATCH net-next v3 4/6] rust: net::phy unified read/write API for C22 and C45 registers
Date: Fri, 16 Aug 2024 03:09:31 +0200	[thread overview]
Message-ID: <82db7404-4665-4563-8011-6d2d5e9c2685@lunn.ch> (raw)
In-Reply-To: <20240804233835.223460-5-fujita.tomonori@gmail.com>

> @@ -0,0 +1,193 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +// Copyright (C) 2024 FUJITA Tomonori <fujita.tomonori@gmail.com>
> +
> +//! PHY register interfaces.
> +//!
> +//! This module provides support for accessing PHY registers via Ethernet
> +//! management interface clause 22 and 45, as defined in IEEE 802.3.

Here we need to be very careful. The word `via` in the sentence above
means we are talking about the access mechanism, c22 transfers, or c45
transfers. A PHY driver should not care about the transfer mechanism,
it should just care about the register in the C22 or C45 register
namespace.

> +impl Register for C22 {
> +    fn read(&self, dev: &mut Device) -> Result<u16> {
> +        let phydev = dev.0.get();
> +        // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Device`.
> +        // So it's just an FFI call, open code of `phy_read()` with a valid `phy_device` pointer
> +        // `phydev`.
> +        let ret = unsafe {
> +            bindings::mdiobus_read((*phydev).mdio.bus, (*phydev).mdio.addr, self.0.into())
> +        };
> +        to_result(ret)?;
> +        Ok(ret as u16)
> +    }
> +
> +    fn write(&self, dev: &mut Device, val: u16) -> Result {
> +        let phydev = dev.0.get();
> +        // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Device`.
> +        // So it's just an FFI call, open code of `phy_write()` with a valid `phy_device` pointer
> +        // `phydev`.
> +        to_result(unsafe {
> +            bindings::mdiobus_write((*phydev).mdio.bus, (*phydev).mdio.addr, self.0.into(), val)
> +        })

These two are O.K. You have to use C22 bus transfers to access the C22
register namespace.

> +impl Register for C45 {
> +    fn read(&self, dev: &mut Device) -> Result<u16> {
> +        let phydev = dev.0.get();
> +        // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Device`.
> +        // So it's just an FFI call.
> +        let ret =
> +            unsafe { bindings::phy_read_mmd(phydev, self.devad.0.into(), self.regnum.into()) };
> +        to_result(ret)?;
> +        Ok(ret as u16)
> +    }
> +
> +    fn write(&self, dev: &mut Device, val: u16) -> Result {
> +        let phydev = dev.0.get();
> +        // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Device`.
> +        // So it's just an FFI call.
> +        to_result(unsafe {
> +            bindings::phy_write_mmd(phydev, self.devad.0.into(), self.regnum.into(), val)
> +        })
> +    }

And these are also O.K. There are two mechanisms to access the C45
register namespace. By calling phy_write_mmd()/phy_write_mmd() you are
leaving it upto the core to decide which mechanism to use. The driver
itself does not care.

So the problem is with the comment above. It would be better to say
something like:

This module provides support for accessing PHY registers in the
Ethernet management interface clauses 22 and 45 register namespaces, as
defined in IEEE 802.3.

Dropping the via, and adding register namespace should make it clear
we are talking about the registers themselves, not how you access
them.


    Andrew

---
pw-bot: cr

  reply	other threads:[~2024-08-16  1:09 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-04 23:38 [PATCH net-next v3 0/6] net: phy: add Applied Micro QT2025 PHY driver FUJITA Tomonori
2024-08-04 23:38 ` [PATCH net-next v3 1/6] rust: sizes: add commonly used constants FUJITA Tomonori
2024-08-16  0:37   ` Andrew Lunn
2024-08-16  5:21     ` FUJITA Tomonori
2024-08-04 23:38 ` [PATCH net-next v3 2/6] rust: net::phy support probe callback FUJITA Tomonori
2024-08-16  0:40   ` Andrew Lunn
2024-08-16  5:21     ` FUJITA Tomonori
2024-08-04 23:38 ` [PATCH net-next v3 3/6] rust: net::phy implement AsRef<kernel::device::Device> trait FUJITA Tomonori
2024-08-16  0:41   ` Andrew Lunn
2024-08-04 23:38 ` [PATCH net-next v3 4/6] rust: net::phy unified read/write API for C22 and C45 registers FUJITA Tomonori
2024-08-16  1:09   ` Andrew Lunn [this message]
2024-08-16  5:27     ` FUJITA Tomonori
2024-08-04 23:38 ` [PATCH net-next v3 5/6] rust: net::phy unified genphy_read_status function " FUJITA Tomonori
2024-08-16  1:19   ` Andrew Lunn
2024-08-16  5:30     ` FUJITA Tomonori
2024-08-16 20:41       ` Andrew Lunn
2024-08-04 23:38 ` [PATCH net-next v3 6/6] net: phy: add Applied Micro QT2025 PHY driver FUJITA Tomonori
2024-08-16  1:45   ` Andrew Lunn
2024-08-16  6:17     ` FUJITA Tomonori
2024-08-16 20:47       ` Andrew Lunn
2024-08-17  4:50         ` FUJITA Tomonori
2024-08-05  1:10 ` [PATCH net-next v3 0/6] " Andrew Lunn
2024-08-14  2:57   ` FUJITA Tomonori

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=82db7404-4665-4563-8011-6d2d5e9c2685@lunn.ch \
    --to=andrew@lunn.ch \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=fujita.tomonori@gmail.com \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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