1 From 31afd6bc55cc0093c3e5b0a368319e423d4de8ea Mon Sep 17 00:00:00 2001
2 From: Christian Marangi <ansuelsmth@gmail.com>
3 Date: Sat, 17 May 2025 22:13:45 +0200
4 Subject: [PATCH 1/5] net: phy: pass PHY driver to .match_phy_device OP
6 Pass PHY driver pointer to .match_phy_device OP in addition to phydev.
7 Having access to the PHY driver struct might be useful to check the
8 PHY ID of the driver is being matched for in case the PHY ID scanned in
9 the phydev is not consistent.
11 A scenario for this is a PHY that change PHY ID after a firmware is
12 loaded, in such case, the PHY ID stored in PHY device struct is not
13 valid anymore and PHY will manually scan the ID in the match_phy_device
16 Having the PHY driver info is also useful for those PHY driver that
17 implement multiple simple .match_phy_device OP to match specific MMD PHY
18 ID. With this extra info if the parsing logic is the same, the matching
19 function can be generalized by using the phy_id in the PHY driver
20 instead of hardcoding.
22 Rust wrapper callback is updated to align to the new match_phy_device
25 Suggested-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>
26 Reviewed-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk>
27 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
28 Reviewed-by: Benno Lossin <lossin@kernel.org> # for Rust
29 Reviewed-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
30 Link: https://patch.msgid.link/20250517201353.5137-2-ansuelsmth@gmail.com
31 Signed-off-by: Jakub Kicinski <kuba@kernel.org>
33 drivers/net/phy/bcm87xx.c | 6 ++++--
34 drivers/net/phy/icplus.c | 6 ++++--
35 drivers/net/phy/marvell10g.c | 12 ++++++++----
36 drivers/net/phy/micrel.c | 6 ++++--
37 drivers/net/phy/nxp-c45-tja11xx.c | 12 ++++++++----
38 drivers/net/phy/nxp-tja11xx.c | 6 ++++--
39 drivers/net/phy/phy_device.c | 2 +-
40 drivers/net/phy/realtek/realtek_main.c | 27 +++++++++++++++++---------
41 drivers/net/phy/teranetics.c | 3 ++-
42 include/linux/phy.h | 3 ++-
43 rust/kernel/net/phy.rs | 1 +
44 11 files changed, 56 insertions(+), 28 deletions(-)
46 --- a/drivers/net/phy/bcm87xx.c
47 +++ b/drivers/net/phy/bcm87xx.c
48 @@ -185,12 +185,14 @@ static irqreturn_t bcm87xx_handle_interr
52 -static int bcm8706_match_phy_device(struct phy_device *phydev)
53 +static int bcm8706_match_phy_device(struct phy_device *phydev,
54 + const struct phy_driver *phydrv)
56 return phydev->c45_ids.device_ids[4] == PHY_ID_BCM8706;
59 -static int bcm8727_match_phy_device(struct phy_device *phydev)
60 +static int bcm8727_match_phy_device(struct phy_device *phydev,
61 + const struct phy_driver *phydrv)
63 return phydev->c45_ids.device_ids[4] == PHY_ID_BCM8727;
65 --- a/drivers/net/phy/icplus.c
66 +++ b/drivers/net/phy/icplus.c
67 @@ -520,12 +520,14 @@ static int ip101a_g_match_phy_device(str
68 return ip101a == !ret;
71 -static int ip101a_match_phy_device(struct phy_device *phydev)
72 +static int ip101a_match_phy_device(struct phy_device *phydev,
73 + const struct phy_driver *phydrv)
75 return ip101a_g_match_phy_device(phydev, true);
78 -static int ip101g_match_phy_device(struct phy_device *phydev)
79 +static int ip101g_match_phy_device(struct phy_device *phydev,
80 + const struct phy_driver *phydrv)
82 return ip101a_g_match_phy_device(phydev, false);
84 --- a/drivers/net/phy/marvell10g.c
85 +++ b/drivers/net/phy/marvell10g.c
86 @@ -1221,7 +1221,8 @@ static int mv3310_get_number_of_ports(st
90 -static int mv3310_match_phy_device(struct phy_device *phydev)
91 +static int mv3310_match_phy_device(struct phy_device *phydev,
92 + const struct phy_driver *phydrv)
94 if ((phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] &
95 MARVELL_PHY_ID_MASK) != MARVELL_PHY_ID_88X3310)
96 @@ -1230,7 +1231,8 @@ static int mv3310_match_phy_device(struc
97 return mv3310_get_number_of_ports(phydev) == 1;
100 -static int mv3340_match_phy_device(struct phy_device *phydev)
101 +static int mv3340_match_phy_device(struct phy_device *phydev,
102 + const struct phy_driver *phydrv)
104 if ((phydev->c45_ids.device_ids[MDIO_MMD_PMAPMD] &
105 MARVELL_PHY_ID_MASK) != MARVELL_PHY_ID_88X3310)
106 @@ -1254,12 +1256,14 @@ static int mv211x_match_phy_device(struc
107 return !!(val & MDIO_PCS_SPEED_5G) == has_5g;
110 -static int mv2110_match_phy_device(struct phy_device *phydev)
111 +static int mv2110_match_phy_device(struct phy_device *phydev,
112 + const struct phy_driver *phydrv)
114 return mv211x_match_phy_device(phydev, true);
117 -static int mv2111_match_phy_device(struct phy_device *phydev)
118 +static int mv2111_match_phy_device(struct phy_device *phydev,
119 + const struct phy_driver *phydrv)
121 return mv211x_match_phy_device(phydev, false);
123 --- a/drivers/net/phy/micrel.c
124 +++ b/drivers/net/phy/micrel.c
125 @@ -670,7 +670,8 @@ static int ksz8051_ksz8795_match_phy_dev
129 -static int ksz8051_match_phy_device(struct phy_device *phydev)
130 +static int ksz8051_match_phy_device(struct phy_device *phydev,
131 + const struct phy_driver *phydrv)
133 return ksz8051_ksz8795_match_phy_device(phydev, true);
135 @@ -790,7 +791,8 @@ static int ksz8061_config_init(struct ph
136 return kszphy_config_init(phydev);
139 -static int ksz8795_match_phy_device(struct phy_device *phydev)
140 +static int ksz8795_match_phy_device(struct phy_device *phydev,
141 + const struct phy_driver *phydrv)
143 return ksz8051_ksz8795_match_phy_device(phydev, false);
145 --- a/drivers/net/phy/nxp-tja11xx.c
146 +++ b/drivers/net/phy/nxp-tja11xx.c
147 @@ -648,12 +648,14 @@ static int tja1102_match_phy_device(stru
151 -static int tja1102_p0_match_phy_device(struct phy_device *phydev)
152 +static int tja1102_p0_match_phy_device(struct phy_device *phydev,
153 + const struct phy_driver *phydrv)
155 return tja1102_match_phy_device(phydev, true);
158 -static int tja1102_p1_match_phy_device(struct phy_device *phydev)
159 +static int tja1102_p1_match_phy_device(struct phy_device *phydev,
160 + const struct phy_driver *phydrv)
162 return tja1102_match_phy_device(phydev, false);
164 --- a/drivers/net/phy/phy_device.c
165 +++ b/drivers/net/phy/phy_device.c
166 @@ -533,7 +533,7 @@ static int phy_bus_match(struct device *
169 if (phydrv->match_phy_device)
170 - return phydrv->match_phy_device(phydev);
171 + return phydrv->match_phy_device(phydev, phydrv);
173 if (phydev->is_c45) {
174 for (i = 1; i < num_ids; i++) {
175 --- a/drivers/net/phy/realtek/realtek_main.c
176 +++ b/drivers/net/phy/realtek/realtek_main.c
177 @@ -1315,13 +1315,15 @@ static bool rtlgen_supports_mmd(struct p
181 -static int rtlgen_match_phy_device(struct phy_device *phydev)
182 +static int rtlgen_match_phy_device(struct phy_device *phydev,
183 + const struct phy_driver *phydrv)
185 return phydev->phy_id == RTL_GENERIC_PHYID &&
186 !rtlgen_supports_2_5gbps(phydev);
189 -static int rtl8226_match_phy_device(struct phy_device *phydev)
190 +static int rtl8226_match_phy_device(struct phy_device *phydev,
191 + const struct phy_driver *phydrv)
193 return phydev->phy_id == RTL_GENERIC_PHYID &&
194 rtlgen_supports_2_5gbps(phydev) &&
195 @@ -1337,32 +1339,38 @@ static int rtlgen_is_c45_match(struct ph
196 return !is_c45 && (id == phydev->phy_id);
199 -static int rtl8221b_match_phy_device(struct phy_device *phydev)
200 +static int rtl8221b_match_phy_device(struct phy_device *phydev,
201 + const struct phy_driver *phydrv)
203 return phydev->phy_id == RTL_8221B && rtlgen_supports_mmd(phydev);
206 -static int rtl8221b_vb_cg_c22_match_phy_device(struct phy_device *phydev)
207 +static int rtl8221b_vb_cg_c22_match_phy_device(struct phy_device *phydev,
208 + const struct phy_driver *phydrv)
210 return rtlgen_is_c45_match(phydev, RTL_8221B_VB_CG, false);
213 -static int rtl8221b_vb_cg_c45_match_phy_device(struct phy_device *phydev)
214 +static int rtl8221b_vb_cg_c45_match_phy_device(struct phy_device *phydev,
215 + const struct phy_driver *phydrv)
217 return rtlgen_is_c45_match(phydev, RTL_8221B_VB_CG, true);
220 -static int rtl8221b_vn_cg_c22_match_phy_device(struct phy_device *phydev)
221 +static int rtl8221b_vn_cg_c22_match_phy_device(struct phy_device *phydev,
222 + const struct phy_driver *phydrv)
224 return rtlgen_is_c45_match(phydev, RTL_8221B_VN_CG, false);
227 -static int rtl8221b_vn_cg_c45_match_phy_device(struct phy_device *phydev)
228 +static int rtl8221b_vn_cg_c45_match_phy_device(struct phy_device *phydev,
229 + const struct phy_driver *phydrv)
231 return rtlgen_is_c45_match(phydev, RTL_8221B_VN_CG, true);
234 -static int rtl_internal_nbaset_match_phy_device(struct phy_device *phydev)
235 +static int rtl_internal_nbaset_match_phy_device(struct phy_device *phydev,
236 + const struct phy_driver *phydrv)
240 @@ -1381,7 +1389,8 @@ static int rtl_internal_nbaset_match_phy
241 return rtlgen_supports_2_5gbps(phydev) && !rtlgen_supports_mmd(phydev);
244 -static int rtl8251b_c45_match_phy_device(struct phy_device *phydev)
245 +static int rtl8251b_c45_match_phy_device(struct phy_device *phydev,
246 + const struct phy_driver *phydrv)
248 return rtlgen_is_c45_match(phydev, RTL_8251B, true);
250 --- a/drivers/net/phy/teranetics.c
251 +++ b/drivers/net/phy/teranetics.c
252 @@ -67,7 +67,8 @@ static int teranetics_read_status(struct
256 -static int teranetics_match_phy_device(struct phy_device *phydev)
257 +static int teranetics_match_phy_device(struct phy_device *phydev,
258 + const struct phy_driver *phydrv)
260 return phydev->c45_ids.device_ids[3] == PHY_ID_TN2020;
262 --- a/include/linux/phy.h
263 +++ b/include/linux/phy.h
264 @@ -1003,7 +1003,8 @@ struct phy_driver {
265 * driver for the given phydev. If NULL, matching is based on
266 * phy_id and phy_id_mask.
268 - int (*match_phy_device)(struct phy_device *phydev);
269 + int (*match_phy_device)(struct phy_device *phydev,
270 + const struct phy_driver *phydrv);
273 * @set_wol: Some devices (e.g. qnap TS-119P II) require PHY