Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp3246830rdb; Tue, 6 Feb 2024 11:20:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IH9BbXCWt1Jb48mRRJO9d2N8//54XFxI7XxYrU62ue9K8vMwampSuLVC7AUckzkuf4L7oDF X-Received: by 2002:a05:620a:45a9:b0:785:513a:aa83 with SMTP id bp41-20020a05620a45a900b00785513aaa83mr4378123qkb.17.1707247243377; Tue, 06 Feb 2024 11:20:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707247243; cv=pass; d=google.com; s=arc-20160816; b=OUY08VkvNiQmKIamxljSz0z9BTlsaMBOK1xtIfZp+jna7Xi8QPrh/spnVhFq0GPTAH vVr85h5Osu4tOx2J+v2zscLEyv47OM21lPrbCDBC7TdOnu7ng0KBw+pUUw6X05elqkua gyHe7ksRUygCJ8xeUV3lRP0TpNH7nKuZUz5dW+MTGXqIAWwc56UwTCK8NZ/pYQqPbKst lTHYdsxoPx0MovJG8604M5vmeFom47LudeIu9y85UfqaUhmlFZ0wyU9xXCANVFsmKxQY xpnhUuk3OAXBy+dwfL12vgXGckdeRGx57Ux/ejz1Y409C+90gCMdR7htkOpf5pRee/G5 lSaQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=xy6t4mlVHxuazPpBgdQxXfTtOx++6wHuYVh5LN0zmWA=; fh=0YLjDZOZEmCFRYa7Walx2AeXFqav8y6T2WuxMYIh6Fg=; b=Dk/ZTtJc6FgTs7Vv9DDZRmacwKqfZBpAB0iDFXUy89BFOA61ibITgx/U5KJk2xnQlu SZYioeb9ur7IUpgjwuRTu3L+LSPqW0AR81zR+IiB+RMzS6l3NWoXH+UGOggnYscHWs2z PzJUhRdfsvAaP2Rg/sltOt7xcUVI95t3xUgxOwcvhhaApMUhvfZS+tEYRs7kQqxjLHLY 7sjx5EiJCxTgYnMY0ahF6fccOdQbpJ5nP4WGdmWgEoGvB6wCJ+yKjAMpbLb7LUcf5MF8 YRKL44MkX+8mEC2YlVlGkP4HYaTfEY9kDGhmkuvvkAT6GQ68njPE1tU0Mw9/wHdus2aC 6aUQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=christina-quast.de); spf=pass (google.com: domain of linux-kernel+bounces-55550-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55550-linux.lists.archive=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCVzftDs20jOxKkxvY+4bd98KpbGKZBDz0Eu2bYKxJjCZgaWycPWxAiTfUsq7sRwPmVsxUJoMWux8bVOYOCtyxVa/3gTLHD9jWRpq5WzvQ== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id s29-20020a05620a081d00b00783ad67e5c2si2908717qks.552.2024.02.06.11.20.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Feb 2024 11:20:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-55550-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=christina-quast.de); spf=pass (google.com: domain of linux-kernel+bounces-55550-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-55550-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 0F12C1C212C7 for ; Tue, 6 Feb 2024 19:20:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 168BC171D4; Tue, 6 Feb 2024 19:20:11 +0000 (UTC) Received: from mail.someserver.de (mail.someserver.de [116.202.193.223]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ABB591754B; Tue, 6 Feb 2024 19:20:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=116.202.193.223 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707247210; cv=none; b=PXg+HdfPaad9kth18B1gLhHwv1nTg8vpro0sEew25i6w9D3khj9D785LmqWtzRqJpwhogkfi36SN70FqdpTXNiFXew4uymOa7Zl4+nWZIjhDqe3qP1+UW8W8RkojVl1D10/gD0ewOJuFRFVhlOluzYIPvaGr50HLH+B38U514q8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707247210; c=relaxed/simple; bh=3iSqkYE7BU24TdTslIeyhkwASqnpAZB9syTd+oJD1KA=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=bQa7NanO4z/5F+1j6f/StSvJa4eroVgBGigU7L3ObdWpRahUIjTZVbEA3xMnznKZJofga+ehJLoa9BtKEHe36n0g3vePMjcUoJuI5PSRuv1Ain5I4AeYorbfA8BQVoGdMZlevaouZCxfqlvgNhdQOwCDsq33et55A9aQFQ+SXio= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=christina-quast.de; spf=pass smtp.mailfrom=christina-quast.de; arc=none smtp.client-ip=116.202.193.223 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=christina-quast.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=christina-quast.de Received: from [192.168.244.146] (tmo-103-215.customers.d1-online.com [80.187.103.215]) by mail.someserver.de (Postfix) with ESMTPSA id 7BB29A21C5; Tue, 6 Feb 2024 20:19:53 +0100 (CET) Message-ID: Date: Tue, 6 Feb 2024 20:19:50 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 3/3] net: phy: add Rust Rockchip PHY driver Content-Language: en-US To: Trevor Gross , Christina Quast Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , FUJITA Tomonori , Andrew Lunn , Heiner Kallweit , Russell King , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Heiko Stuebner , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org References: <20240201-rockchip-rust-phy_depend-v2-0-c5fa4faab924@christina-quast.de> <20240201-rockchip-rust-phy_depend-v2-3-c5fa4faab924@christina-quast.de> From: Christina Quast In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi Trevor! Thanks a lot for your review, I learned a lot! I felt, from the feedback in the Zulip forum that rewriting more phy drivers might be interesting, but I think I misunderstood something. There is no specific goal behind the rewrite, I just thought it would be useful to bring more Rust into the Kernel. Cheers, Christina On 2/1/24 22:06, Trevor Gross wrote: > On Thu, Feb 1, 2024 at 12:07 PM Christina Quast > wrote: >> +++ b/drivers/net/phy/rockchip_rust.rs >> @@ -0,0 +1,131 @@ >> +// SPDX-License-Identifier: GPL-2.0 >> +// Copyright (C) 2024 Christina Quast >> + >> +//! Rust Rockchip PHY driver >> +//! >> +//! C version of this driver: [`drivers/net/phy/rockchip.c`](./rockchip.c) >> +use kernel::{ >> + c_str, >> + net::phy::{self, DeviceId, Driver}, >> + prelude::*, >> + uapi, >> +}; >> + >> +kernel::module_phy_driver! { >> + drivers: [PhyRockchip], >> + device_table: [ >> + DeviceId::new_with_driver::(), >> + ], >> + name: "rust_asix_phy", >> + author: "FUJITA Tomonori ", > Tomo wrote this? :) > >> + description: "Rust Asix PHYs driver", >> + license: "GPL", >> +} >> + >> + >> +const MII_INTERNAL_CTRL_STATUS: u16 = 17; >> +const SMI_ADDR_TSTCNTL: u16 = 20; >> +const SMI_ADDR_TSTWRITE: u16 = 23; >> + >> +const MII_AUTO_MDIX_EN: u16 = bit(7); >> +const MII_MDIX_EN: u16 = bit(6); >> + >> +const TSTCNTL_WR: u16 = bit(14) | bit(10); >> + >> +const TSTMODE_ENABLE: u16 = 0x400; >> +const TSTMODE_DISABLE: u16 = 0x0; >> + >> +const WR_ADDR_A7CFG: u16 = 0x18; > Most of these are clear enough, but could you add comments about what > the more ambiguous constants are for? (e.g. A7CFG). > >> +struct PhyRockchip; >> + >> +impl PhyRockchip { > Remove the `helper_` prefix for these functions, and change the docs. > Their use as helpers is obvious enough based on where they are called, > better to say what they actually accomplish. > > Since they don't take `self`, these could also just be standalone > functions rather than in an `impl PhyRockchip` block. This makes > calling them a bit cleaner since you don't need the `PhyRockchip::` > prefix. > >> + /// Helper function for helper_integrated_phy_analog_init >> + fn helper_init_tstmode(dev: &mut phy::Device) -> Result { >> + // Enable access to Analog and DSP register banks >> + dev.write(SMI_ADDR_TSTCNTL, TSTMODE_ENABLE)?; >> + dev.write(SMI_ADDR_TSTCNTL, TSTMODE_DISABLE)?; >> + dev.write(SMI_ADDR_TSTCNTL, TSTMODE_ENABLE) >> + } > For consistency, just make the last write also end with `?;` and add a > `Ok(())` line. > >> + >> + /// Helper function for helper_integrated_phy_analog_init >> + fn helper_close_tstmode(dev: &mut phy::Device) -> Result { >> + dev.write(SMI_ADDR_TSTCNTL, TSTMODE_DISABLE) >> + } >> + >> + /// Helper function for rockchip_config_init >> + fn helper_integrated_phy_analog_init(dev: &mut phy::Device) -> Result { >> + Self::helper_init_tstmode(dev)?; >> + dev.write(SMI_ADDR_TSTWRITE, 0xB)?; >> + dev.write(SMI_ADDR_TSTCNTL, TSTCNTL_WR | WR_ADDR_A7CFG)?; >> + Self::helper_close_tstmode(dev) >> + } >> + >> + /// Helper function for config_init >> + fn helper_config_init(dev: &mut phy::Device) -> Result { >> + let val = !MII_AUTO_MDIX_EN & dev.read(MII_INTERNAL_CTRL_STATUS)?; >> + dev.write(MII_INTERNAL_CTRL_STATUS, val)?; >> + Self::helper_integrated_phy_analog_init(dev) >> + } >> + >> + fn helper_set_polarity(dev: &mut phy::Device, polarity: u8) -> Result { >> + let reg = !MII_AUTO_MDIX_EN & dev.read(MII_INTERNAL_CTRL_STATUS)?; >> + let val = match polarity as u32 { >> + // status: MDI; control: force MDI >> + uapi::ETH_TP_MDI => Some(reg & !MII_MDIX_EN), >> + // status: MDI-X; control: force MDI-X >> + uapi::ETH_TP_MDI_X => Some(reg | MII_MDIX_EN), >> + // uapi::ETH_TP_MDI_AUTO => control: auto-select >> + // uapi::ETH_TP_MDI_INVALID => status: unknown; control: unsupported >> + _ => None, > Is receiving an invalid value not an error? I.e. > > uapi::ETH_TP_MDI_AUTO | uapi::ETH_TP_MDI_INVALID => None, > _ => return Err(...) > > I know the current implementation came from the C version, just > wondering about correctness here. > >> + }; >> + if let Some(v) = val { >> + if v != reg { >> + return dev.write(MII_INTERNAL_CTRL_STATUS, v); >> + } >> + } > In the match statement above - I think you can replace `=> None` with > `=> return Ok(())` and drop the `Some(...)` wrappers. Then you don't > need to destructure val here. > >> + Ok(()) >> + >> + } >> +} >> + >> +#[vtable] >> +impl Driver for PhyRockchip { >> + const FLAGS: u32 = 0; >> + const NAME: &'static CStr = c_str!("Rockchip integrated EPHY"); >> + const PHY_DEVICE_ID: DeviceId = DeviceId::new_with_custom_mask(0x1234d400, 0xfffffff0); >> + >> + fn link_change_notify(dev: &mut phy::Device) { >> + // If mode switch happens from 10BT to 100BT, all DSP/AFE >> + // registers are set to default values. So any AFE/DSP >> + // registers have to be re-initialized in this case. > Comment indent > >> + if dev.state() == phy::DeviceState::Running && dev.speed() == uapi::SPEED_100 { >> + if let Err(e) = Self::helper_integrated_phy_analog_init(dev) { >> + pr_err!("rockchip: integrated_phy_analog_init err: {:?}", e); >> + } >> + } >> + } >> + >> + fn soft_reset(dev: &mut phy::Device) -> Result { >> + dev.genphy_soft_reset() >> + } >> + >> + fn config_init(dev: &mut phy::Device) -> Result { >> + PhyRockchip::helper_config_init(dev) >> + } >> + >> + fn config_aneg(dev: &mut phy::Device) -> Result { >> + PhyRockchip::helper_set_polarity(dev, dev.mdix())?; >> + dev.genphy_config_aneg() >> + } >> + >> + fn suspend(dev: &mut phy::Device) -> Result { >> + dev.genphy_suspend() >> + } >> + >> + fn resume(dev: &mut phy::Device) -> Result { >> + let _ = dev.genphy_resume(); > Why not `?` the possible error? > >> + >> + PhyRockchip::helper_config_init(dev) >> + } >> +} >> >> -- >> 2.43.0 >> > As Greg and Dragan mentioned, duplicate drivers are generally not > accepted in-tree to avoid double maintenance and confusing config. Is > there a specific goal? > > It is quite alright to request feedback on Rust drivers (and I have > provided some) or even ask if anyone is willing to help test it out, > but please use RFC PATCH and make it clear that this is for > experimentation rather than upstreaming. > > Netdev has seemed relatively open to adding Rust drivers for new phys > that don't have a C implementation, but these phys are of course > tougher to find. > > Also for future reference, changes intended for the net tree should be > labeled [PATCH v? net-next]. > > Best regards, > Trevor