rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: FUJITA Tomonori <fujita.tomonori@gmail.com>
To: andrew@lunn.ch
Cc: fujita.tomonori@gmail.com, rust-for-linux@vger.kernel.org
Subject: Re: [RFC PATCH v1 4/4] sample: rust: add Asix PHY driver
Date: Mon, 18 Sep 2023 17:35:27 +0900 (JST)	[thread overview]
Message-ID: <20230918.173527.1946752214085636243.ubuntu@gmail.com> (raw)
In-Reply-To: <0b6f8110-b4fd-4d20-a97f-9d7df26c23f0@lunn.ch>

Hi,

On Sun, 17 Sep 2023 17:46:39 +0200
Andrew Lunn <andrew@lunn.ch> wrote:

>> >> +    fn read_status(dev: &mut phy::Device) -> Result {
>> >> +        dev.update_link()?;
>> >> +        if !dev.link() {
>> >> +            return Ok(());
>> >> +        }
>> >> +        let ret = dev.read(MII_BMCR)?;
>> >> +
>> >> +        if ret as u32 & BMCR_SPEED100 != 0 {
>> > 
>> > PHY registers are u16. phy_read() returns an int, so it can have
>> > negative return codes. But if it is not negative, the positive values
>> > are in the range of 0 to 65535.

Sorry, I should have commented in the previous reply. ret can't be a
negative here due to how to hanle an error in Rust.

let ret = dev.read(MII_BMCR)?;

Here '?' means that if an error happens, return with an error
immediately. So the ret is set only if dev.read() is sucessful.


>> A negative is an error?
>
> phy_read() can return values like -ETIMEDOUT, -ENODEV, -EIO etc to
> indicate something when wrong. So you should see the general pattern
> of:
> 
>         foo = phy_read(phydev, MII_M1111_PHY_EXT_SR);
>         if (foo < 0)
>                 return foo;
> 
> After probe, the probability of an error is very low, and generally
> there is no recovery. So not all drivers do check the return value.
> 
> There is also a second C problem here. Because a PHY register is a
> u16, you sometimes see code like:
> 
> 	u16 reg
> 
> 	reg = phy_read(phydev, MII_M1111_PHY_EXT_SR);
> 	if (reg < 0)
> 		return reg;
> 
> which compiles cleanly, but is wrong.. The folks running static
> checkers then report the bug.
> 
>> If so, this function should be:
>> 
>> fn read_status(dev: &mut phy::Device) -> Result<u16>
> 
> No, it needs to return the negative error code, or 0 on success. This
> is a very typical pattern in Linux.

This is a way to handle an error in Rust. If a function could return
an error, it returns a Result type, which is either success or
failure. Either could carry an value.

Result<u16> means if success, it carrys u16. If failure, in Rust for
Linux, carrys an error code from C side.


>> >> +    fn suspend(dev: &mut phy::Device) -> Result {
>> >> +        dev.suspend()
>> >> +    }
>> >> +
>> >> +    fn resume(dev: &mut phy::Device) -> Result {
>> >> +        dev.resume()
>> >> +    }
>> > 
>> > Can we avoid this boilerplate? A lot of phy drivers will make use of
>> > genphy_foo calls in their struct phy_driver. It would be annoying if
>> > each driver needs silly little wrappers like this.
>> 
>> you meant that C style like
>> 
>> {
>>         .suspend        = genphy_suspend,
>>         .resume         = genphy_resume,
>>         .soft_reset     = asix_soft_reset,
>> }
>> 
>> is clear than Rust style
>> 
>>     fn suspend(dev: &mut phy::Device) -> Result {
>>         dev.genphy_suspend()
>>     }
>> 
>>     fn resume(dev: &mut phy::Device) -> Result {
>>         dev.genphy_resume()
>>     }
>> 
>> right?
> 
> Yes. If you really must have these wrappers, they are not specific to
> a driver, so should be placed somewhere common where any driver can
> use them. There are around 60 of these genphy_ functions. Because what
> a PHY does is mostly standardised in IEEE 802.3, PHY drivers heavily
> use these functions to do the core of the work, and then deal with
> vendor extensions and vendor bugs.

Understood. I think that there are other places like this in the
kernel, operation function pointers that could use generic functions.

Rust people, Any opinions on a different way than vtable macro?

  reply	other threads:[~2023-09-18  8:35 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
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 [this message]
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=20230918.173527.1946752214085636243.ubuntu@gmail.com \
    --to=fujita.tomonori@gmail.com \
    --cc=andrew@lunn.ch \
    --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).