From patchwork Wed Aug 28 07:35:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13780824 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-il1-f178.google.com (mail-il1-f178.google.com [209.85.166.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A55415F33A; Wed, 28 Aug 2024 07:36:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724830611; cv=none; b=sPmnfbDNNR8NuxmlYq6UXjyd3+los7twqyX8S+q3fEj0+yttglV58s47S6uIJ0dpYKBua48WHlT1fLF3cTUV5HcNcLjczJx0kQpfne3HdBiui3gnyQ16FILkoxtckbcPv3TJZU/ZeHj8jAdg/CKD4oADjciElz5YUG+C+83g8Eg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724830611; c=relaxed/simple; bh=Zd8l1q0D19cU6pErw51JdqlQ7c0xNmlvoL/tC1zo+/s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ry8PPNFq2Wx3Ldw6AotoiZ3Oxemmtjnmn5QVzIKbFplkQSatyQmfIV1Z7Z2MLKMfcNcvzMv0wPyeIS1lYj6Y2IyHvqAO8bd36I7TUcc+uWHVhhUn/4lTcuZBuODWkuFKVEneBkDJyIn9tNCVkz2BFuruLEyZhh9mjCCjD9tz+8w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=dKZNfnxe; arc=none smtp.client-ip=209.85.166.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dKZNfnxe" Received: by mail-il1-f178.google.com with SMTP id e9e14a558f8ab-39d2ceca81cso23941025ab.3; Wed, 28 Aug 2024 00:36:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724830608; x=1725435408; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Beto75Mf9prahnTjU1kwOMy7OnVuEGAzz1OuVXCfzHo=; b=dKZNfnxelHCugDk1Rw08dBpae0XVTZ5jtoaamw2QJX34dhwrLKd2AK/8mwy50dqx3A p099AXU+GW93QKU5J4Bid8zYLUYtSvjMpkCFUBxNw5UDJSmYfo/z/kniHklMnAC7jQQ8 Py0Aw0UmcIAep1zu63Z0KQXkra6HBLOL3uwbDB8+Fn07Cg5K6C4+UlWi9U3YuQiloEcM yQcVkdpiUGhwGbg7JqmGscTWTcILHJ+28OZTeyI/m85lgqMS4gUNisrvBXeEbsT39SDY kPI7mLZcEL4yzDZS8zCAv/wS2NjybLQSDMRAoawdkFKMSPxI1XAmNkn2OvOX+UQxQpxH FW1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724830608; x=1725435408; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Beto75Mf9prahnTjU1kwOMy7OnVuEGAzz1OuVXCfzHo=; b=LTYQE+cHyG1Ww4yP9f03s2vloiS6B69IQVOJdLAxI6Fba5DOc6Cfbnmqc3UwQgGr7U PUW9+DbQq11PbaiBm7F6Rfkj+upQ7v6vKPXdKCVwJ8YVjWCssqeEht/9dPQ5qXIjvdpf agned/ikBrltfNKQsNCzSumtY9ABWSJVz+wUwPSYB5t7i8YABfu5f0qU3sUzmQ056UCT feXM1TJ0X+fQf6AdXPm0mqErm3gyMwTmayDa95wtQICM4Zu7zcdtZqGa3liGEuAyhgE+ O5JXSH81pNFuquNmUYOYv0fioUYn/LKB6UuTp5pZhsKtLHZ5lUy7g1comm23mXfZ2cR7 e6mA== X-Forwarded-Encrypted: i=1; AJvYcCVSr+QalA7wmP+Se8cLijcgtpd0+XEiTrmfFOJXWBGkhfD7zkOFAANe7g008xrKf3tzU2C1ub1p+ArMYnOKfA==@vger.kernel.org X-Gm-Message-State: AOJu0YwNEa7dla5LATFoCDPCo7Uxm55o/YQfYaPPl/pQyvzc0HOuq/0F cuJYoaTkkUEoO7XQkIWd569QN5tZ63wbW9csyrbimzADBPdNoxWQmG4MqFSX2J0= X-Google-Smtp-Source: AGHT+IHF+LGeqKsmSTv0OTM0ltqQ9PHeLYWKrHmXe61XjX4/necuf+E920pSmitQWyeQd67P3A9kXw== X-Received: by 2002:a92:ca0c:0:b0:39d:376b:20b9 with SMTP id e9e14a558f8ab-39e3c9c0b0dmr202016575ab.20.1724830608207; Wed, 28 Aug 2024 00:36:48 -0700 (PDT) Received: from localhost.localdomain (p4468007-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.200.7]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7cd9ac98286sm9016225a12.5.2024.08.28.00.36.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 00:36:47 -0700 (PDT) From: FUJITA Tomonori To: netdev@vger.kernel.org Cc: kuba@kernel.org, rust-for-linux@vger.kernel.org, andrew@lunn.ch, tmgross@umich.edu, miguel.ojeda.sandonis@gmail.com, benno.lossin@proton.me, aliceryhl@google.com Subject: [PATCH net-next v8 1/6] rust: sizes: add commonly used constants Date: Wed, 28 Aug 2024 07:35:11 +0000 Message-ID: <20240828073516.128290-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240828073516.128290-1-fujita.tomonori@gmail.com> References: <20240828073516.128290-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Add rust equivalent to include/linux/sizes.h, makes code more readable. Only SZ_*K that QT2025 PHY driver uses are added. Make generated constants accessible with a proper type. Reviewed-by: Alice Ryhl Reviewed-by: Andrew Lunn Reviewed-by: Benno Lossin Reviewed-by: Trevor Gross Signed-off-by: FUJITA Tomonori --- rust/kernel/lib.rs | 1 + rust/kernel/sizes.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 rust/kernel/sizes.rs diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 274bdc1b0a82..58ed400198bf 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -43,6 +43,7 @@ pub mod page; pub mod prelude; pub mod print; +pub mod sizes; mod static_assert; #[doc(hidden)] pub mod std_vendor; diff --git a/rust/kernel/sizes.rs b/rust/kernel/sizes.rs new file mode 100644 index 000000000000..834c343e4170 --- /dev/null +++ b/rust/kernel/sizes.rs @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Commonly used sizes. +//! +//! C headers: [`include/linux/sizes.h`](srctree/include/linux/sizes.h). + +/// 0x00000400 +pub const SZ_1K: usize = bindings::SZ_1K as usize; +/// 0x00000800 +pub const SZ_2K: usize = bindings::SZ_2K as usize; +/// 0x00001000 +pub const SZ_4K: usize = bindings::SZ_4K as usize; +/// 0x00002000 +pub const SZ_8K: usize = bindings::SZ_8K as usize; +/// 0x00004000 +pub const SZ_16K: usize = bindings::SZ_16K as usize; +/// 0x00008000 +pub const SZ_32K: usize = bindings::SZ_32K as usize; +/// 0x00010000 +pub const SZ_64K: usize = bindings::SZ_64K as usize; +/// 0x00020000 +pub const SZ_128K: usize = bindings::SZ_128K as usize; +/// 0x00040000 +pub const SZ_256K: usize = bindings::SZ_256K as usize; +/// 0x00080000 +pub const SZ_512K: usize = bindings::SZ_512K as usize; From patchwork Wed Aug 28 07:35:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13780825 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-ot1-f53.google.com (mail-ot1-f53.google.com [209.85.210.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B9FD0160783; Wed, 28 Aug 2024 07:36:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724830613; cv=none; b=uVhtBxCoCIbJaeYqcdUKTSlp6o1dY1z1SnIhcah17do124aokQR6FZWgN/W9sWB/c3Q+TW8qpyJApq9RXNrLWhQT9GOUCuomitDbExiywe1WgZd1xjclFvMAo1XdVCWe+pfaIr9vnS2gsvkyYLYaJ2sp/uX+bJjOtMJiMMhiqVE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724830613; c=relaxed/simple; bh=Z+3W+TjB/+akxnGD/81jh9rFeSs5dXy3N237wOpbV+M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OH5psDcNWEy0EMGXlY75n6TPEowR4dlo51otpGGwTUfS4pWfW+Hob7jPJWqnIU9vB21pv/0srmF9OyZOgrmRqORewqjmwgQJ9B+mtFtJX0qpoEjjgY0D797wZQQu7jcv0x2jDDjJOz8e5gAhqTG4I3feO08ji3DyyZRUG3TkJRg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Ghm3MdCN; arc=none smtp.client-ip=209.85.210.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Ghm3MdCN" Received: by mail-ot1-f53.google.com with SMTP id 46e09a7af769-70e07c1ab31so4902103a34.0; Wed, 28 Aug 2024 00:36:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724830611; x=1725435411; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rEn+T3edq3AlPKEeI8Gs8XwEjBjg6OpikVH3BRZII5Q=; b=Ghm3MdCNjHU/7UMUxjcKGAGI/ywXR5JB2Apk00xj7NgUwFZ61Pjqi2/2dR1I7V+gKl sJQ0pRDL50BjYdDP55m7a+YP8Rlw7ut161yDzHYkjpEDJ+dHMCxAB2rc4AqgOO8X1UbW z++LHbY3SfPm4vJMuXUImak6eGrdpqZjkdudnCbmOfvv2/FvAFYNJtrk4mvB14XdODXB 3fBIfGY762bKZWIeZLuT8dxV7NlDDJiZekI0Oh6rSyiBfLNP0OSkaoYwFqXKlP73805z sHKl9qHFenTKKC0uxLOO1Cu6eDnXkbky6CwVFKpomwGurf7o/lEbV1mNvFOzLdw1OZ/Q /AZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724830611; x=1725435411; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rEn+T3edq3AlPKEeI8Gs8XwEjBjg6OpikVH3BRZII5Q=; b=e1OgkBi0NrXPIXM8kG3Bf3+IJiMfPPRIPN10XHMs4P8NSd30So0UjplhX5lpXkqfa0 ll7nC+9zgHlI707mlG6bVTYyr5K7WQ3T2+8hshaj39+UynMavlVgR7pyeHJt7nDiSBKZ Ee/3bwYaNZYLJXp+fLgMnYy4GHTNs8nEFnGzyNtopaiM9aTwyiHVSXfObBuLRMh25WOD K8EoypTqxfBVm+1216RwHXW9HQVlyf76C3TVPXPy1imFT7p0MZ6/22S+awKtdEgTjGgk y4Rg1zoU97pftavWS7PI8AEHbMlw2nObDUJn/ZLHhc9kZSfL9CK4DRQ8diDfry1ce61F DPHw== X-Forwarded-Encrypted: i=1; AJvYcCXNDP3E+Q0Dd/LJsHAedgPJXF3sjEHvnmj3mkpea5lkrib8EzyhYrEk7kA6BFr78hszoxk8oYZWUknMzAHHrg==@vger.kernel.org X-Gm-Message-State: AOJu0YyMEYJuudzROST0fDSbQcC9cW2n1eFmJjxKM8ff8A528KC9ROOX Fc22vtBfiIMH+oBIkFMD6CpIThU4OmtYy2aldGeXT6X8lkCC5mhRqUxmTiU5t+M= X-Google-Smtp-Source: AGHT+IGAWIB2Rektk4c8hPNmZsYyrri+3l9GgFv6TaNxQR/UnmGve76gj7tjTTdR54s9YkiIgziUTg== X-Received: by 2002:a05:6830:600c:b0:700:d3b7:4ede with SMTP id 46e09a7af769-70f53ac404cmr1259668a34.27.1724830610631; Wed, 28 Aug 2024 00:36:50 -0700 (PDT) Received: from localhost.localdomain (p4468007-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.200.7]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7cd9ac98286sm9016225a12.5.2024.08.28.00.36.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 00:36:50 -0700 (PDT) From: FUJITA Tomonori To: netdev@vger.kernel.org Cc: kuba@kernel.org, rust-for-linux@vger.kernel.org, andrew@lunn.ch, tmgross@umich.edu, miguel.ojeda.sandonis@gmail.com, benno.lossin@proton.me, aliceryhl@google.com Subject: [PATCH net-next v8 2/6] rust: net::phy support probe callback Date: Wed, 28 Aug 2024 07:35:12 +0000 Message-ID: <20240828073516.128290-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240828073516.128290-1-fujita.tomonori@gmail.com> References: <20240828073516.128290-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Support phy_driver probe callback, used to set up device-specific structures. Reviewed-by: Alice Ryhl Reviewed-by: Andrew Lunn Reviewed-by: Benno Lossin Reviewed-by: Trevor Gross Signed-off-by: FUJITA Tomonori --- rust/kernel/net/phy.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs index fd40b703d224..5e8137a1972f 100644 --- a/rust/kernel/net/phy.rs +++ b/rust/kernel/net/phy.rs @@ -338,6 +338,21 @@ impl Adapter { }) } + /// # Safety + /// + /// `phydev` must be passed by the corresponding callback in `phy_driver`. + unsafe extern "C" fn probe_callback(phydev: *mut bindings::phy_device) -> core::ffi::c_int { + from_result(|| { + // SAFETY: This callback is called only in contexts + // where we can exclusively access `phy_device` because + // it's not published yet, so the accessors on `Device` are okay + // to call. + let dev = unsafe { Device::from_raw(phydev) }; + T::probe(dev)?; + Ok(0) + }) + } + /// # Safety /// /// `phydev` must be passed by the corresponding callback in `phy_driver`. @@ -511,6 +526,11 @@ pub const fn create_phy_driver() -> DriverVTable { } else { None }, + probe: if T::HAS_PROBE { + Some(Adapter::::probe_callback) + } else { + None + }, get_features: if T::HAS_GET_FEATURES { Some(Adapter::::get_features_callback) } else { @@ -583,6 +603,11 @@ fn soft_reset(_dev: &mut Device) -> Result { kernel::build_error(VTABLE_DEFAULT_ERROR) } + /// Sets up device-specific structures during discovery. + fn probe(_dev: &mut Device) -> Result { + kernel::build_error(VTABLE_DEFAULT_ERROR) + } + /// Probes the hardware to determine what abilities it has. fn get_features(_dev: &mut Device) -> Result { kernel::build_error(VTABLE_DEFAULT_ERROR) From patchwork Wed Aug 28 07:35:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13780826 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27704161320; Wed, 28 Aug 2024 07:36:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724830615; cv=none; b=FCgxaB7HlswZ2FzXwST6O6uuy9YB8gXcn2bG0y/RZXyiGuvVlqH6XoNIJ64sZoe9l3GEPm6xD1VS+SgkQsris40mxeQeF61ZuVi9SOoiNFr3iWvuloy7neWro8S0uzFf6DUEJ0EbhkBkApDB19sgeMX6kdSzVO/l4z7fYaPH9zs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724830615; c=relaxed/simple; bh=aHiVGhRwYAsQczP9WqvPUI9lvDsw7q7yZyu98J4LwcI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IQzaJpR//ZQe7ty5GC0pKFIbMFwq/aZG7PAnKGudiXbOkeuTgMu+eeBeb7gS04dMMkNMgzRkME5ObuI/BbuOUQ9LPgPI6R0NbNPWofKgEaibrm7FdQ0vfcfupR4m3Zllz+v6la8Hnxy7ckav0xv13j6ikMptDfj6NsW7RSIuBdY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=eKyoiYso; arc=none smtp.client-ip=209.85.210.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eKyoiYso" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-7143185edf2so5352273b3a.0; Wed, 28 Aug 2024 00:36:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724830613; x=1725435413; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7Mrcl7Z+31Fhv7Hq2xBCP13+zdTE4Tg47VezA00ALMk=; b=eKyoiYsoxmz70tUin25p6ChVVB/RBuuQ0z+hHOiLA+cSuA+knV1LWi3BXiy8AY9Wbr ensrvVF1eU/udfHd+rBZT2daiSFAqmb2ocxC5SuPVwnhukhdphg+GAyxWhmXXBFh8F3m RFpM40FIT47bhyb412ia4dFBbonNz/8hejtWFI7u4hcuTXvrRgmWKapCcUyLbAdWdxlp nhDYq6PezOb8Op0eEM37XeU5xWfTFcmeN0+nS73taMJvZQA+RhqzhrcE65N22UfIZDiX NmbaMlyioZ8/b1dwVaOsumh9eUsl3eNra6I17TI7x5aGY1Tt/+TSLbDf51MD8TBbfMiC RJSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724830613; x=1725435413; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7Mrcl7Z+31Fhv7Hq2xBCP13+zdTE4Tg47VezA00ALMk=; b=RtxM5ePN/9rXzl7Y4+vErpCzElyMgfgY+TA2YEWvlaekd1zevpwR9OqKl/yQPR42Yq vQ1ga3cO8RgBbeI+kcX+CXnKV52SxXDSzs8PA4B9mRyVs+Lkyq+PwnqhRQ/qYvY9qqwX vv0nVH6EVmmlNcAQdgI1Aq8drdWuvmV2I0qRfgoAqBLUpy7fbbzb79mM+U/vXwmk3SOF O9NM85/0Q4NVHj1G5HpbgYXeDi/0L1rSufUayAJf8PuH9g9VfBDMSV5ENxNSefZ4NmoF 2O16MBEK/JdHvPP7wCTqm3NEvUA29HTlILGSU9ZaZ1xCPFLzs6p9JapMjFiZPK0qDwZr jJEg== X-Forwarded-Encrypted: i=1; AJvYcCVMwj4i3FLIKcyjkddzxPUADv/D2t6ylpHCPnPJjqvtOkgFfFAFK1p1Bj0Iss87cSanqnIyoNr8J1Lx0KU8Qg==@vger.kernel.org X-Gm-Message-State: AOJu0Yy7DDr+ffB86SZ7whlfvkc6lPQC6hCWVgon90r38VZhBDv/gdoR 0j/Z6GtNzX98fh1E67a+UgJvtVo5ku8oZa37uGTJyGO6R52zY3gNZokdGFhVR5U= X-Google-Smtp-Source: AGHT+IGCL2tYWSdmcyD/eL5x7RVhP7HCwJUlHKNlCfLy0sjNIU7qA/SzemEv2Mv6EYjEGOC1wq/E7Q== X-Received: by 2002:a05:6a00:847:b0:714:228d:e9f5 with SMTP id d2e1a72fcca58-71445cc26c7mr17790050b3a.2.1724830612978; Wed, 28 Aug 2024 00:36:52 -0700 (PDT) Received: from localhost.localdomain (p4468007-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.200.7]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7cd9ac98286sm9016225a12.5.2024.08.28.00.36.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 00:36:52 -0700 (PDT) From: FUJITA Tomonori To: netdev@vger.kernel.org Cc: kuba@kernel.org, rust-for-linux@vger.kernel.org, andrew@lunn.ch, tmgross@umich.edu, miguel.ojeda.sandonis@gmail.com, benno.lossin@proton.me, aliceryhl@google.com Subject: [PATCH net-next v8 3/6] rust: net::phy implement AsRef trait Date: Wed, 28 Aug 2024 07:35:13 +0000 Message-ID: <20240828073516.128290-4-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240828073516.128290-1-fujita.tomonori@gmail.com> References: <20240828073516.128290-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Implement AsRef trait for Device. A PHY driver needs a reference to device::Device to call the firmware API. Reviewed-by: Alice Ryhl Reviewed-by: Andrew Lunn Reviewed-by: Benno Lossin Reviewed-by: Trevor Gross Signed-off-by: FUJITA Tomonori --- rust/kernel/net/phy.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs index 5e8137a1972f..b16e8c10a0a2 100644 --- a/rust/kernel/net/phy.rs +++ b/rust/kernel/net/phy.rs @@ -7,8 +7,7 @@ //! C headers: [`include/linux/phy.h`](srctree/include/linux/phy.h). use crate::{error::*, prelude::*, types::Opaque}; - -use core::marker::PhantomData; +use core::{marker::PhantomData, ptr::addr_of_mut}; /// PHY state machine states. /// @@ -58,8 +57,9 @@ pub enum DuplexMode { /// /// # Invariants /// -/// Referencing a `phy_device` using this struct asserts that you are in -/// a context where all methods defined on this struct are safe to call. +/// - Referencing a `phy_device` using this struct asserts that you are in +/// a context where all methods defined on this struct are safe to call. +/// - This struct always has a valid `self.0.mdio.dev`. /// /// [`struct phy_device`]: srctree/include/linux/phy.h // During the calls to most functions in [`Driver`], the C side (`PHYLIB`) holds a lock that is @@ -76,9 +76,11 @@ impl Device { /// /// # Safety /// - /// For the duration of 'a, the pointer must point at a valid `phy_device`, - /// and the caller must be in a context where all methods defined on this struct - /// are safe to call. + /// For the duration of `'a`, + /// - the pointer must point at a valid `phy_device`, and the caller + /// must be in a context where all methods defined on this struct + /// are safe to call. + /// - `(*ptr).mdio.dev` must be a valid. unsafe fn from_raw<'a>(ptr: *mut bindings::phy_device) -> &'a mut Self { // CAST: `Self` is a `repr(transparent)` wrapper around `bindings::phy_device`. let ptr = ptr.cast::(); @@ -302,6 +304,14 @@ pub fn genphy_read_abilities(&mut self) -> Result { } } +impl AsRef for Device { + fn as_ref(&self) -> &kernel::device::Device { + let phydev = self.0.get(); + // SAFETY: The struct invariant ensures that `mdio.dev` is valid. + unsafe { kernel::device::Device::as_ref(addr_of_mut!((*phydev).mdio.dev)) } + } +} + /// Defines certain other features this PHY supports (like interrupts). /// /// These flag values are used in [`Driver::FLAGS`]. From patchwork Wed Aug 28 07:35:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13780827 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-oi1-f175.google.com (mail-oi1-f175.google.com [209.85.167.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1E4C1165F15; Wed, 28 Aug 2024 07:36:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724830619; cv=none; b=cZxtwz+P/wQ1pbpKqgFltkcrYsm2AIhZ7n3e4OBf9cdv3H0bXUbKH7IBHycraIwlAr/HH5XbfmXsNFXXCl1tCvmUcKRnv1udtkiRsfOksnuldboNcSFqKw7aqV/MurpJhssf8rJip1u8cQqxj31wo7gS/ZoJDlbPrnnkJWK5QTc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724830619; c=relaxed/simple; bh=56e05tdstgexLmP1+5MIdKv3GhEp1YJ7z7LR0/FHPOM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mdeVAnCvkc/AeRkzNO8/tEwhjaeAuz3EWgeNoc59/IOkK/GfTPpsNEf2lKxliteQdnqsiXQPhY7276AZEub+18k9VKs/xg0JUlngv24rlJkYfwrzjR4mwPIR9D2VBTg6Yc5CwT6LainFlWBx57JP5e3qalgERMSKFCK8LUGclyo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=M73gkQyA; arc=none smtp.client-ip=209.85.167.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="M73gkQyA" Received: by mail-oi1-f175.google.com with SMTP id 5614622812f47-3dc16d00ba6so986440b6e.0; Wed, 28 Aug 2024 00:36:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724830616; x=1725435416; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5haTbZ7ZfXMbo1JOz7kTHDQXYr/nQ+nxqrOAzO6uXug=; b=M73gkQyAKSWGBSKB6l+p+YikIol3vL+ylyd9eeVryD7eFr0WMaRJyxNzidzTIFH5m9 VjYgteDaHJEUKLYQK8j+8KmmVklp8Hxgh2ksLwJOcAVBlRCjUIuigLzECjvZDYQJJ2Ux wSe6J2rP2QyfPW1WIL39RDG6eV/hFeEFSFsKr2cghv58D6YuT4J8bUA1DYhHdE2qojxV xLHHYk+Zw1uC1fAKEzr0MOAjspU9n0l2WKxjQicWY0pum3NTeIK7q38KsVdZ5c1Ic5zj ZNdOiu+rdU1IOxxUU1rLe79niKLEIuWts6xP7pSs4a0lm93CtEvTWPo7XtyA2n4AhxIt i2DA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724830616; x=1725435416; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5haTbZ7ZfXMbo1JOz7kTHDQXYr/nQ+nxqrOAzO6uXug=; b=I2gGSbzhVm6jmofukfB2Q9ASk7naYfNsFjmZbDbQBxL9gaFHXK2NMupq7CsQDRNlvU LSqUnDzLk4Z2E+R9Bm3MRZINV+N5w4UUIYcgdT6II9QDLWm7r0s+gjiyKqTmQMgMWit8 ILymOG4b4Z0ldYWF1Fc2nqNEV32vhm+FtuFDXmvT6rttvqhuH0/ES6Asfh6IaLtAXPn5 F8neB4eZzHNM30dHHeZUeguU3EvYg6dLSVa/VCv0zS+OppZQMrlYN+vOgapea/Rkn+t9 5l0UrJbeK8DEzVW1BUspJf0Bo2E9+pGExJzBHVcs9bifLZ1Xj6vt5WM+322cbuPD9Xqk nryQ== X-Forwarded-Encrypted: i=1; AJvYcCV6J//b2c6Kk0ISGRVe3txHYyjveGw5KHASpXmPloN+/8smPnUS45hvkyAY5x2cP1h3oqQrCqcscwa+rV6Qaw==@vger.kernel.org X-Gm-Message-State: AOJu0Yxyge3YHnFXhClfHsH5+ssKJx9pGyIi0/+mDGCZVz4xILqSXrEz Bz0++2BnlXN4KOQ24qd6ACnoCJfKbl7PMvcwoL+3TcIYcVpg/byrbOGgYO3QaGo= X-Google-Smtp-Source: AGHT+IEuZHz4sUPUU0Ir3841liDOlKXbDdHwAKl8k33GfrPwc/8xLYjuXlFxRtOToc9SaHET8+kM1Q== X-Received: by 2002:a05:687c:2bd6:b0:270:6bae:5573 with SMTP id 586e51a60fabf-2777d1f22eemr1005991fac.28.1724830615419; Wed, 28 Aug 2024 00:36:55 -0700 (PDT) Received: from localhost.localdomain (p4468007-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.200.7]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7cd9ac98286sm9016225a12.5.2024.08.28.00.36.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 00:36:55 -0700 (PDT) From: FUJITA Tomonori To: netdev@vger.kernel.org Cc: kuba@kernel.org, rust-for-linux@vger.kernel.org, andrew@lunn.ch, tmgross@umich.edu, miguel.ojeda.sandonis@gmail.com, benno.lossin@proton.me, aliceryhl@google.com Subject: [PATCH net-next v8 4/6] rust: net::phy unified read/write API for C22 and C45 registers Date: Wed, 28 Aug 2024 07:35:14 +0000 Message-ID: <20240828073516.128290-5-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240828073516.128290-1-fujita.tomonori@gmail.com> References: <20240828073516.128290-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Add the unified read/write API for C22 and C45 registers. The abstractions support access to only C22 registers now. Instead of adding read/write_c45 methods specifically for C45, a new reg module supports the unified API to access C22 and C45 registers with trait, by calling an appropriate phylib functions. Reviewed-by: Trevor Gross Reviewed-by: Benno Lossin Reviewed-by: Andrew Lunn Signed-off-by: FUJITA Tomonori --- MAINTAINERS | 1 + drivers/net/phy/ax88796b_rust.rs | 7 +- rust/kernel/net/phy.rs | 31 ++--- rust/kernel/net/phy/reg.rs | 196 +++++++++++++++++++++++++++++++ rust/uapi/uapi_helper.h | 1 + 5 files changed, 209 insertions(+), 27 deletions(-) create mode 100644 rust/kernel/net/phy/reg.rs diff --git a/MAINTAINERS b/MAINTAINERS index 30a9b9450e11..ec395140668e 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -8357,6 +8357,7 @@ L: netdev@vger.kernel.org L: rust-for-linux@vger.kernel.org S: Maintained F: rust/kernel/net/phy.rs +F: rust/kernel/net/phy/reg.rs EXEC & BINFMT API, ELF R: Eric Biederman diff --git a/drivers/net/phy/ax88796b_rust.rs b/drivers/net/phy/ax88796b_rust.rs index 5c92572962dc..8c7eb009d9fc 100644 --- a/drivers/net/phy/ax88796b_rust.rs +++ b/drivers/net/phy/ax88796b_rust.rs @@ -6,7 +6,7 @@ //! C version of this driver: [`drivers/net/phy/ax88796b.c`](./ax88796b.c) use kernel::{ c_str, - net::phy::{self, DeviceId, Driver}, + net::phy::{self, reg::C22, DeviceId, Driver}, prelude::*, uapi, }; @@ -24,7 +24,6 @@ license: "GPL", } -const MII_BMCR: u16 = uapi::MII_BMCR as u16; const BMCR_SPEED100: u16 = uapi::BMCR_SPEED100 as u16; const BMCR_FULLDPLX: u16 = uapi::BMCR_FULLDPLX as u16; @@ -33,7 +32,7 @@ // Toggle BMCR_RESET bit off to accommodate broken AX8796B PHY implementation // such as used on the Individual Computers' X-Surf 100 Zorro card. fn asix_soft_reset(dev: &mut phy::Device) -> Result { - dev.write(uapi::MII_BMCR as u16, 0)?; + dev.write(C22::BMCR, 0)?; dev.genphy_soft_reset() } @@ -55,7 +54,7 @@ fn read_status(dev: &mut phy::Device) -> Result { } // If MII_LPA is 0, phy_resolve_aneg_linkmode() will fail to resolve // linkmode so use MII_BMCR as default values. - let ret = dev.read(MII_BMCR)?; + let ret = dev.read(C22::BMCR)?; if ret & BMCR_SPEED100 != 0 { dev.set_speed(uapi::SPEED_100); diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs index b16e8c10a0a2..45866db14c76 100644 --- a/rust/kernel/net/phy.rs +++ b/rust/kernel/net/phy.rs @@ -9,6 +9,8 @@ use crate::{error::*, prelude::*, types::Opaque}; use core::{marker::PhantomData, ptr::addr_of_mut}; +pub mod reg; + /// PHY state machine states. /// /// Corresponds to the kernel's [`enum phy_state`]. @@ -177,32 +179,15 @@ pub fn set_duplex(&mut self, mode: DuplexMode) { unsafe { (*phydev).duplex = v }; } - /// Reads a given C22 PHY register. + /// Reads a PHY register. // This function reads a hardware register and updates the stats so takes `&mut self`. - pub fn read(&mut self, regnum: u16) -> Result { - let phydev = self.0.get(); - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`. - // 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, regnum.into()) - }; - if ret < 0 { - Err(Error::from_errno(ret)) - } else { - Ok(ret as u16) - } + pub fn read(&mut self, reg: R) -> Result { + reg.read(self) } - /// Writes a given C22 PHY register. - pub fn write(&mut self, regnum: u16, val: u16) -> Result { - let phydev = self.0.get(); - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`. - // 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, regnum.into(), val) - }) + /// Writes a PHY register. + pub fn write(&mut self, reg: R, val: u16) -> Result { + reg.write(self, val) } /// Reads a paged register. diff --git a/rust/kernel/net/phy/reg.rs b/rust/kernel/net/phy/reg.rs new file mode 100644 index 000000000000..4563737a9675 --- /dev/null +++ b/rust/kernel/net/phy/reg.rs @@ -0,0 +1,196 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 FUJITA Tomonori + +//! PHY register interfaces. +//! +//! 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. + +use super::Device; +use crate::build_assert; +use crate::error::*; +use crate::uapi; + +mod private { + /// Marker that a trait cannot be implemented outside of this crate + pub trait Sealed {} +} + +/// Accesses PHY registers. +/// +/// This trait is used to implement the unified interface to access +/// C22 and C45 PHY registers. +/// +/// # Examples +/// +/// ```ignore +/// fn link_change_notify(dev: &mut Device) { +/// // read C22 BMCR register +/// dev.read(C22::BMCR); +/// // read C45 PMA/PMD control 1 register +/// dev.read(C45::new(Mmd::PMAPMD, 0)); +/// } +/// ``` +pub trait Register: private::Sealed { + /// Reads a PHY register. + fn read(&self, dev: &mut Device) -> Result; + + /// Writes a PHY register. + fn write(&self, dev: &mut Device, val: u16) -> Result; +} + +/// A single MDIO clause 22 register address (5 bits). +#[derive(Copy, Clone, Debug)] +pub struct C22(u8); + +impl C22 { + /// Basic mode control. + pub const BMCR: Self = C22(0x00); + /// Basic mode status. + pub const BMSR: Self = C22(0x01); + /// PHY identifier 1. + pub const PHYSID1: Self = C22(0x02); + /// PHY identifier 2. + pub const PHYSID2: Self = C22(0x03); + /// Auto-negotiation advertisement. + pub const ADVERTISE: Self = C22(0x04); + /// Auto-negotiation link partner base page ability. + pub const LPA: Self = C22(0x05); + /// Auto-negotiation expansion. + pub const EXPANSION: Self = C22(0x06); + /// Auto-negotiation next page transmit. + pub const NEXT_PAGE_TRANSMIT: Self = C22(0x07); + /// Auto-negotiation link partner received next page. + pub const LP_RECEIVED_NEXT_PAGE: Self = C22(0x08); + /// Master-slave control. + pub const MASTER_SLAVE_CONTROL: Self = C22(0x09); + /// Master-slave status. + pub const MASTER_SLAVE_STATUS: Self = C22(0x0a); + /// PSE Control. + pub const PSE_CONTROL: Self = C22(0x0b); + /// PSE Status. + pub const PSE_STATUS: Self = C22(0x0c); + /// MMD Register control. + pub const MMD_CONTROL: Self = C22(0x0d); + /// MMD Register address data. + pub const MMD_DATA: Self = C22(0x0e); + /// Extended status. + pub const EXTENDED_STATUS: Self = C22(0x0f); + + /// Creates a new instance of `C22` with a vendor specific register. + pub const fn vendor_specific() -> Self { + build_assert!( + N > 0x0f && N < 0x20, + "Vendor-specific register address must be between 16 and 31" + ); + C22(N) + } +} + +impl private::Sealed for C22 {} + +impl Register for C22 { + fn read(&self, dev: &mut Device) -> 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_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) + }) + } +} + +/// A single MDIO clause 45 register device and address. +#[derive(Copy, Clone, Debug)] +pub struct Mmd(u8); + +impl Mmd { + /// Physical Medium Attachment/Dependent. + pub const PMAPMD: Self = Mmd(uapi::MDIO_MMD_PMAPMD as u8); + /// WAN interface sublayer. + pub const WIS: Self = Mmd(uapi::MDIO_MMD_WIS as u8); + /// Physical coding sublayer. + pub const PCS: Self = Mmd(uapi::MDIO_MMD_PCS as u8); + /// PHY Extender sublayer. + pub const PHYXS: Self = Mmd(uapi::MDIO_MMD_PHYXS as u8); + /// DTE Extender sublayer. + pub const DTEXS: Self = Mmd(uapi::MDIO_MMD_DTEXS as u8); + /// Transmission convergence. + pub const TC: Self = Mmd(uapi::MDIO_MMD_TC as u8); + /// Auto negotiation. + pub const AN: Self = Mmd(uapi::MDIO_MMD_AN as u8); + /// Separated PMA (1). + pub const SEPARATED_PMA1: Self = Mmd(8); + /// Separated PMA (2). + pub const SEPARATED_PMA2: Self = Mmd(9); + /// Separated PMA (3). + pub const SEPARATED_PMA3: Self = Mmd(10); + /// Separated PMA (4). + pub const SEPARATED_PMA4: Self = Mmd(11); + /// OFDM PMA/PMD. + pub const OFDM_PMAPMD: Self = Mmd(12); + /// Power unit. + pub const POWER_UNIT: Self = Mmd(13); + /// Clause 22 extension. + pub const C22_EXT: Self = Mmd(uapi::MDIO_MMD_C22EXT as u8); + /// Vendor specific 1. + pub const VEND1: Self = Mmd(uapi::MDIO_MMD_VEND1 as u8); + /// Vendor specific 2. + pub const VEND2: Self = Mmd(uapi::MDIO_MMD_VEND2 as u8); +} + +/// A single MDIO clause 45 register device and address. +/// +/// Clause 45 uses a 5-bit device address to access a specific MMD within +/// a port, then a 16-bit register address to access a location within +/// that device. `C45` represents this by storing a [`Mmd`] and +/// a register number. +pub struct C45 { + devad: Mmd, + regnum: u16, +} + +impl C45 { + /// Creates a new instance of `C45`. + pub fn new(devad: Mmd, regnum: u16) -> Self { + Self { devad, regnum } + } +} + +impl private::Sealed for C45 {} + +impl Register for C45 { + fn read(&self, dev: &mut Device) -> 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. + 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) + }) + } +} diff --git a/rust/uapi/uapi_helper.h b/rust/uapi/uapi_helper.h index 08f5e9334c9e..76d3f103e764 100644 --- a/rust/uapi/uapi_helper.h +++ b/rust/uapi/uapi_helper.h @@ -7,5 +7,6 @@ */ #include +#include #include #include From patchwork Wed Aug 28 07:35:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13780828 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EBEC3160783; Wed, 28 Aug 2024 07:36:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724830620; cv=none; b=NekDtIDYzT9gsIULjucf8/t2fYGud4ZXtg3t0sCSOeKLvR3L+yrC1rdHLqaCbgpoJBHwy69xS31kdQImbZfzPFRqvQx+zbz/Jm0VydwlKLLpO2hlL45M0AnGUz87ThLvC5kRMR/2itpyhjT3ap+oKFwTCUW+JU8rhAhRJFwFQsE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724830620; c=relaxed/simple; bh=fmaFkVQcvY6JJhqKe96ZnDZTjLKsGV+aP7hDCXQP1ME=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eA2kg+07HIe90DC047VFuKGwzWEHVziENZZyYflvoJkJfg/wBLB+NDSYVDbKg61EOXIbdDIApJHRDoKkx8jke1nvF5I60r+9nVNy6l48TPjiNJIdd9Tt/tz8CU1WXWlpC5GGWHtKtOO1PG3ZYjbVP/8VaFxeCjDWH2rMpr2giVc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Wdr6swpZ; arc=none smtp.client-ip=209.85.210.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Wdr6swpZ" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-715cdc7a153so261177b3a.0; Wed, 28 Aug 2024 00:36:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724830618; x=1725435418; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0qQxWIg1EEWwKzc/SOJAE4/A/RWLbXcic6JIjq2SU5I=; b=Wdr6swpZOzUr8RDOz15Z56xFRYkBjwZB7n3qKmWxU46vWZUpEt5mv/M6sFteHHxPQg THlMO6HT1nmjlYdJ1A4/nkPAwL98mlSDhaEMSb76EFwiOVOrqOs7zE5InNgeqkF5Bm2U RljPgSskqdHRV9mqTrfsAJgyYXmc4Wq1E+LJx0CBPrQmm1cTBmbQTiglfjt877ZlJG2t oSZ7nGiuuWnnISMg7WVi21AqBHUEbqxqzYGLlxR1EWMWmdvoF0AbVYcJyuoqyaq4Zvp2 pnMLouOaiSSMxp5l7KqqW5JENguuF0S0OE3klUKUSFHOTTpHGmBy1EEYKGNfF0VsJTH5 UF/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724830618; x=1725435418; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0qQxWIg1EEWwKzc/SOJAE4/A/RWLbXcic6JIjq2SU5I=; b=NCiOD+Pd4zzjIG4Yl9pH/jLl2QFES6ES8bjaAJthvS3ldVPRPTImEa8Ao/K2bN2Ysy BWShw6cGowXvYeRn/K667/NrPaISSbZmtii1n5vq1c0OQTYOMrUXFPKnJmFHYUytAgeh VzJF0JlubMbbnDamg87TgK9Jfl0po3PlPyGpTyu9l8v2kRuLvmxxcy0r06s0nECQAT7F LXzJB3tV2f20zxuwAD1zepTMgyQdQJbTbgaiwSsvSWxwxJJuKA+bYRdggn4BU8rOv09b IvqpDw0AGCTYWAqi7ODJKdvp1e2Hanw4COLqWFLb2vuuGZalUkXt8MDaUUiGbeSFQ07j ZvLw== X-Forwarded-Encrypted: i=1; AJvYcCVrw6DtmnNgX9nbz9BdLO0qiwkD4FZ88W3M9F1TaGxf6CLTVfiJxFLdHj96pg4v09QjKGbPRqqpajnzX1zDXw==@vger.kernel.org X-Gm-Message-State: AOJu0YywGPRUi6qE9Z1BB5I0QhXRmAZvWGvvYimtgGPHJQVuc8xv60wc +THfeYOu+Pt2Saaud3DSLBJduIek8mtbaKmIJvRqzN1XoZ10aeb7NY9IichOt6Q= X-Google-Smtp-Source: AGHT+IGfQNI5QlFiL2lIUSDkNAlt6ZJ4nnRGas1yjQojkeqWpTCIShpTqZDaY6VvUnNMjcksEqsffg== X-Received: by 2002:a05:6a20:d808:b0:1c0:f09c:1b98 with SMTP id adf61e73a8af0-1ccd1aae729mr1943814637.16.1724830617747; Wed, 28 Aug 2024 00:36:57 -0700 (PDT) Received: from localhost.localdomain (p4468007-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.200.7]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7cd9ac98286sm9016225a12.5.2024.08.28.00.36.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 00:36:57 -0700 (PDT) From: FUJITA Tomonori To: netdev@vger.kernel.org Cc: kuba@kernel.org, rust-for-linux@vger.kernel.org, andrew@lunn.ch, tmgross@umich.edu, miguel.ojeda.sandonis@gmail.com, benno.lossin@proton.me, aliceryhl@google.com Subject: [PATCH net-next v8 5/6] rust: net::phy unified genphy_read_status function for C22 and C45 registers Date: Wed, 28 Aug 2024 07:35:15 +0000 Message-ID: <20240828073516.128290-6-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240828073516.128290-1-fujita.tomonori@gmail.com> References: <20240828073516.128290-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Add unified genphy_read_status function for C22 and C45 registers. Instead of having genphy_c22 and genphy_c45 methods, this unifies genphy_read_status functions for C22 and C45. Reviewed-by: Trevor Gross Reviewed-by: Benno Lossin Reviewed-by: Andrew Lunn Signed-off-by: FUJITA Tomonori --- rust/kernel/net/phy.rs | 12 ++---------- rust/kernel/net/phy/reg.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs index 45866db14c76..1d47884aa3cf 100644 --- a/rust/kernel/net/phy.rs +++ b/rust/kernel/net/phy.rs @@ -252,16 +252,8 @@ pub fn genphy_suspend(&mut self) -> Result { } /// Checks the link status and updates current link state. - pub fn genphy_read_status(&mut self) -> Result { - let phydev = self.0.get(); - // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`. - // So it's just an FFI call. - let ret = unsafe { bindings::genphy_read_status(phydev) }; - if ret < 0 { - Err(Error::from_errno(ret)) - } else { - Ok(ret as u16) - } + pub fn genphy_read_status(&mut self) -> Result { + R::read_status(self) } /// Updates the link status. diff --git a/rust/kernel/net/phy/reg.rs b/rust/kernel/net/phy/reg.rs index 4563737a9675..a7db0064cb7d 100644 --- a/rust/kernel/net/phy/reg.rs +++ b/rust/kernel/net/phy/reg.rs @@ -31,6 +31,13 @@ pub trait Sealed {} /// dev.read(C22::BMCR); /// // read C45 PMA/PMD control 1 register /// dev.read(C45::new(Mmd::PMAPMD, 0)); +/// +/// // Checks the link status as reported by registers in the C22 namespace +/// // and updates current link state. +/// dev.genphy_read_status::(); +/// // Checks the link status as reported by registers in the C45 namespace +/// // and updates current link state. +/// dev.genphy_read_status::(); /// } /// ``` pub trait Register: private::Sealed { @@ -39,6 +46,9 @@ pub trait Register: private::Sealed { /// Writes a PHY register. fn write(&self, dev: &mut Device, val: u16) -> Result; + + /// Checks the link status and updates current link state. + fn read_status(dev: &mut Device) -> Result; } /// A single MDIO clause 22 register address (5 bits). @@ -113,6 +123,15 @@ fn write(&self, dev: &mut Device, val: u16) -> Result { bindings::mdiobus_write((*phydev).mdio.bus, (*phydev).mdio.addr, self.0.into(), val) }) } + + fn read_status(dev: &mut Device) -> Result { + let phydev = dev.0.get(); + // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`. + // So it's just an FFI call. + let ret = unsafe { bindings::genphy_read_status(phydev) }; + to_result(ret)?; + Ok(ret as u16) + } } /// A single MDIO clause 45 register device and address. @@ -193,4 +212,13 @@ fn write(&self, dev: &mut Device, val: u16) -> Result { bindings::phy_write_mmd(phydev, self.devad.0.into(), self.regnum.into(), val) }) } + + fn read_status(dev: &mut Device) -> Result { + let phydev = dev.0.get(); + // SAFETY: `phydev` is pointing to a valid object by the type invariant of `Self`. + // So it's just an FFI call. + let ret = unsafe { bindings::genphy_c45_read_status(phydev) }; + to_result(ret)?; + Ok(ret as u16) + } } From patchwork Wed Aug 28 07:35:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13780829 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-oi1-f172.google.com (mail-oi1-f172.google.com [209.85.167.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC790169397; Wed, 28 Aug 2024 07:37:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724830623; cv=none; b=bCIsT8KTJdXgoOqMCfNqcY/6YNLJVDIDHFLU9L61pw9PY/KMKrxvaKxxyYJoZf6arW0D1Tl1yFxn0kePuDPzo+tu8i7zGqTw9PEyI2bgEx9qFkRpRuyjzaMQ9JD8+3zcXrBFJIFoEgGqW0fx9rQMk15ZufNIvg2jV4459hHPINI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724830623; c=relaxed/simple; bh=KYwRm9ZyfhvE1zHskeHg6nnihp9Eyn5tBXiBhDzPB2k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SpNOoJAudA4hG5gUwgh1rwOr/8TGaGvtffJobDDevYsYFp/NSX7vIm3zxDZtzQN9gyH4B367np9XxPAuM2aRbe8lubxAKZWgMCDx4K6O9oCEYZ2HtHYcS8/LhvjLZGMDoUhPK9tubFAxMOeDoERNszyKD+AkGhLj99qTJ+o/CsI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=J5vx9tem; arc=none smtp.client-ip=209.85.167.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="J5vx9tem" Received: by mail-oi1-f172.google.com with SMTP id 5614622812f47-3db50abf929so1050748b6e.2; Wed, 28 Aug 2024 00:37:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724830620; x=1725435420; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZWAGYxrSMskp+c+zhbjiskuUgve3O7L3GJPabCGHid8=; b=J5vx9temziDGBb2BWDoD+ZGeMcW+ZhrWCbi6vyNbhqrnqtIKKNm57axS3+dYQkx/tO kOd559cIBSYSokkrLjLdMHi5jkvkGmxGZsHHjilqOzO4QCjYIsZbSaudffbq9aBTp+BW 2NsXQmnZUDPv+rYk6uWv3KYQFt5voW1YWrWwmDU40dnBYNqtg+AtiMel+0QA5dN99jQ/ yKCh8qTMSBj/HrFy9sRR7kbtR5MXi38gQ0AQy3OYsZZ7IC07HdSbCZkpvB7YGV7FSzaX fJp/L58oTCNIftPowPVLMbx6AoFymYMS35Pv0axVy3l2y8T5CnJ4+f7BD7HYG8RWHdkI aqaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724830620; x=1725435420; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZWAGYxrSMskp+c+zhbjiskuUgve3O7L3GJPabCGHid8=; b=xDeIy3dTWZtLxKwRTvrH1J15qnjQHh+efYQGCEz1YjB96f3OpKPcDGcTJhJ6F+NH9i Jewvc5wWEOjVaD2/a5T/HotuMKGJQaAll6rwXXvjLUADqCOsw7O1AXtDBKONkGZ41P0s CZgjWDnnXNIGtsz57aDTFddl4+Ce4ASYQOghXKFXlPv94bEtlDtxYbVdH8fqdEq3cCeO wcnh5AZqGxruEjsbLG/fb9OV+f0LHkQVEu8b4cZ3fwL7krM+U/PpPpsnkSc5AD0aJxFB VVY/9eSU8UWQnWuhuGh9eJSIQXcQqiMWfhYWmqocYS5t5LnJZBS+W82FQ4ozRPMyMULg vkig== X-Forwarded-Encrypted: i=1; AJvYcCV3V8jIbvwEQmWzVMOts9k4Enao+EQpumztfpMDjeR+WOniUabD1EwdR3gAYg9LbpANhwyyb4W2f9s1pq4hmw==@vger.kernel.org X-Gm-Message-State: AOJu0YxAWDACiaJP4wJP+3vgqeTLclZ7H0G5nE/qVxJK9HRNQF3Yua38 oS/Vm98wqLcxRx+kgwQQ50MwzatevvJw99otUWqQ0Oi6aeb6nS6Dh0+PsO3pkxk= X-Google-Smtp-Source: AGHT+IFeBa3QBU6CVeq65aQ6Ykeu1oHNAkKHdvjl4UIwDmjwlcjMlAsNJYn1Y6c/2OdYXqiLz6aKpA== X-Received: by 2002:a05:6808:23d4:b0:3de:218f:b60 with SMTP id 5614622812f47-3de2a887abbmr18832939b6e.17.1724830620310; Wed, 28 Aug 2024 00:37:00 -0700 (PDT) Received: from localhost.localdomain (p4468007-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.200.7]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7cd9ac98286sm9016225a12.5.2024.08.28.00.36.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 00:36:59 -0700 (PDT) From: FUJITA Tomonori To: netdev@vger.kernel.org Cc: kuba@kernel.org, rust-for-linux@vger.kernel.org, andrew@lunn.ch, tmgross@umich.edu, miguel.ojeda.sandonis@gmail.com, benno.lossin@proton.me, aliceryhl@google.com Subject: [PATCH net-next v8 6/6] net: phy: add Applied Micro QT2025 PHY driver Date: Wed, 28 Aug 2024 07:35:16 +0000 Message-ID: <20240828073516.128290-7-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240828073516.128290-1-fujita.tomonori@gmail.com> References: <20240828073516.128290-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org This driver supports Applied Micro Circuits Corporation QT2025 PHY, based on a driver for Tehuti Networks TN40xx chips. The original driver for TN40xx chips supports multiple PHY hardware (AMCC QT2025, TI TLK10232, Aqrate AQR105, and Marvell 88X3120, 88X3310, and MV88E2010). This driver is extracted from the original driver and modified to a PHY driver in Rust. This has been tested with Edimax EN-9320SFP+ 10G network adapter. Reviewed-by: Trevor Gross Reviewed-by: Andrew Lunn Signed-off-by: FUJITA Tomonori --- MAINTAINERS | 8 +++ drivers/net/phy/Kconfig | 7 +++ drivers/net/phy/Makefile | 1 + drivers/net/phy/qt2025.rs | 103 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 drivers/net/phy/qt2025.rs diff --git a/MAINTAINERS b/MAINTAINERS index ec395140668e..2e04958183db 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1609,6 +1609,14 @@ F: Documentation/admin-guide/perf/xgene-pmu.rst F: Documentation/devicetree/bindings/perf/apm-xgene-pmu.txt F: drivers/perf/xgene_pmu.c +APPLIED MICRO QT2025 PHY DRIVER +M: FUJITA Tomonori +R: Trevor Gross +L: netdev@vger.kernel.org +L: rust-for-linux@vger.kernel.org +S: Maintained +F: drivers/net/phy/qt2025.rs + APTINA CAMERA SENSOR PLL M: Laurent Pinchart L: linux-media@vger.kernel.org diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig index f530fcd092fe..01b235b3bb7e 100644 --- a/drivers/net/phy/Kconfig +++ b/drivers/net/phy/Kconfig @@ -112,6 +112,13 @@ config ADIN1100_PHY Currently supports the: - ADIN1100 - Robust,Industrial, Low Power 10BASE-T1L Ethernet PHY +config AMCC_QT2025_PHY + tristate "AMCC QT2025 PHY" + depends on RUST_PHYLIB_ABSTRACTIONS + depends on RUST_FW_LOADER_ABSTRACTIONS + help + Adds support for the Applied Micro Circuits Corporation QT2025 PHY. + source "drivers/net/phy/aquantia/Kconfig" config AX88796B_PHY diff --git a/drivers/net/phy/Makefile b/drivers/net/phy/Makefile index 33adb3df5f64..90f886844381 100644 --- a/drivers/net/phy/Makefile +++ b/drivers/net/phy/Makefile @@ -37,6 +37,7 @@ obj-$(CONFIG_ADIN_PHY) += adin.o obj-$(CONFIG_ADIN1100_PHY) += adin1100.o obj-$(CONFIG_AIR_EN8811H_PHY) += air_en8811h.o obj-$(CONFIG_AMD_PHY) += amd.o +obj-$(CONFIG_AMCC_QT2025_PHY) += qt2025.o obj-$(CONFIG_AQUANTIA_PHY) += aquantia/ ifdef CONFIG_AX88796B_RUST_PHY obj-$(CONFIG_AX88796B_PHY) += ax88796b_rust.o diff --git a/drivers/net/phy/qt2025.rs b/drivers/net/phy/qt2025.rs new file mode 100644 index 000000000000..28d8981f410b --- /dev/null +++ b/drivers/net/phy/qt2025.rs @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) Tehuti Networks Ltd. +// Copyright (C) 2024 FUJITA Tomonori + +//! Applied Micro Circuits Corporation QT2025 PHY driver +//! +//! This driver is based on the vendor driver `QT2025_phy.c`. This source +//! and firmware can be downloaded on the EN-9320SFP+ support site. +//! +//! The QT2025 PHY integrates an Intel 8051 micro-controller. + +use kernel::c_str; +use kernel::error::code; +use kernel::firmware::Firmware; +use kernel::net::phy::{ + self, + reg::{Mmd, C45}, + DeviceId, Driver, +}; +use kernel::prelude::*; +use kernel::sizes::{SZ_16K, SZ_8K}; + +kernel::module_phy_driver! { + drivers: [PhyQT2025], + device_table: [ + DeviceId::new_with_driver::(), + ], + name: "qt2025_phy", + author: "FUJITA Tomonori ", + description: "AMCC QT2025 PHY driver", + license: "GPL", + firmware: ["qt2025-2.0.3.3.fw"], +} + +struct PhyQT2025; + +#[vtable] +impl Driver for PhyQT2025 { + const NAME: &'static CStr = c_str!("QT2025 10Gpbs SFP+"); + const PHY_DEVICE_ID: phy::DeviceId = phy::DeviceId::new_with_exact_mask(0x0043a400); + + fn probe(dev: &mut phy::Device) -> Result<()> { + // Check the hardware revision code. + // Only 0x3b works with this driver and firmware. + let hw_rev = dev.read(C45::new(Mmd::PMAPMD, 0xd001))?; + if (hw_rev >> 8) != 0xb3 { + return Err(code::ENODEV); + } + + // `MICRO_RESETN`: hold the micro-controller in reset while configuring. + dev.write(C45::new(Mmd::PMAPMD, 0xc300), 0x0000)?; + // `SREFCLK_FREQ`: configure clock frequency of the micro-controller. + dev.write(C45::new(Mmd::PMAPMD, 0xc302), 0x0004)?; + // Non loopback mode. + dev.write(C45::new(Mmd::PMAPMD, 0xc319), 0x0038)?; + // `CUS_LAN_WAN_CONFIG`: select between LAN and WAN (WIS) mode. + dev.write(C45::new(Mmd::PMAPMD, 0xc31a), 0x0098)?; + // The following writes use standardized registers (3.38 through + // 3.41 5/10/25GBASE-R PCS test pattern seed B) for something else. + // We don't know what. + dev.write(C45::new(Mmd::PCS, 0x0026), 0x0e00)?; + dev.write(C45::new(Mmd::PCS, 0x0027), 0x0893)?; + dev.write(C45::new(Mmd::PCS, 0x0028), 0xa528)?; + dev.write(C45::new(Mmd::PCS, 0x0029), 0x0003)?; + // Configure transmit and recovered clock. + dev.write(C45::new(Mmd::PMAPMD, 0xa30a), 0x06e1)?; + // `MICRO_RESETN`: release the micro-controller from the reset state. + dev.write(C45::new(Mmd::PMAPMD, 0xc300), 0x0002)?; + // The micro-controller will start running from the boot ROM. + dev.write(C45::new(Mmd::PCS, 0xe854), 0x00c0)?; + + let fw = Firmware::request(c_str!("qt2025-2.0.3.3.fw"), dev.as_ref())?; + if fw.data().len() > SZ_16K + SZ_8K { + return Err(code::EFBIG); + } + + // The 24kB of program memory space is accessible by MDIO. + // The first 16kB of memory is located in the address range 3.8000h - 3.BFFFh. + // The next 8kB of memory is located at 4.8000h - 4.9FFFh. + let mut dst_offset = 0; + let mut dst_mmd = Mmd::PCS; + for (src_idx, val) in fw.data().iter().enumerate() { + if src_idx == SZ_16K { + // Start writing to the next register with no offset + dst_offset = 0; + dst_mmd = Mmd::PHYXS; + } + + dev.write(C45::new(dst_mmd, 0x8000 + dst_offset), (*val).into())?; + + dst_offset += 1; + } + // The micro-controller will start running from SRAM. + dev.write(C45::new(Mmd::PCS, 0xe854), 0x0040)?; + + // TODO: sleep here until the hw becomes ready. + Ok(()) + } + + fn read_status(dev: &mut phy::Device) -> Result { + dev.genphy_read_status::() + } +}