1 From b84acdb07222a701bfc6403b374249c86f97d18d Mon Sep 17 00:00:00 2001
2 From: Russell King <rmk+kernel@armlinux.org.uk>
3 Date: Fri, 19 May 2023 14:03:59 +0100
4 Subject: [PATCH 15/21] net: phy: add helpers for comparing phy IDs
6 There are several places which open code comparing PHY IDs. Provide a
7 couple of helpers to assist with this, using a slightly simpler test
10 - phy_id_compare() compares two arbitary PHY IDs and a mask of the
11 significant bits in the ID.
12 - phydev_id_compare() compares the bound phydev with the specified
13 PHY ID, using the bound driver's mask.
15 Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
16 Reviewed-by: Simon Horman <simon.horman@corigine.com>
17 Reviewed-by: Andrew Lunn <andrew@lunn.ch>
18 Signed-off-by: David S. Miller <davem@davemloft.net>
20 drivers/net/phy/micrel.c | 6 +++---
21 drivers/net/phy/phy_device.c | 16 +++++++---------
22 drivers/net/phy/phylink.c | 4 ++--
23 include/linux/phy.h | 28 ++++++++++++++++++++++++++++
24 4 files changed, 40 insertions(+), 14 deletions(-)
26 --- a/drivers/net/phy/micrel.c
27 +++ b/drivers/net/phy/micrel.c
28 @@ -620,7 +620,7 @@ static int ksz8051_ksz8795_match_phy_dev
32 - if ((phydev->phy_id & MICREL_PHY_ID_MASK) != PHY_ID_KSZ8051)
33 + if (!phy_id_compare(phydev->phy_id, PHY_ID_KSZ8051, MICREL_PHY_ID_MASK))
36 ret = phy_read(phydev, MII_BMSR);
37 @@ -1455,7 +1455,7 @@ static int ksz9x31_cable_test_fault_leng
39 * distance to fault = (VCT_DATA - 22) * 4 / cable propagation velocity
41 - if ((phydev->phy_id & MICREL_PHY_ID_MASK) == PHY_ID_KSZ9131)
42 + if (phydev_id_compare(phydev, PHY_ID_KSZ9131))
43 dt = clamp(dt - 22, 0, 255);
45 return (dt * 400) / 10;
46 @@ -1887,7 +1887,7 @@ static __always_inline int ksz886x_cable
48 dt = FIELD_GET(data_mask, status);
50 - if ((phydev->phy_id & MICREL_PHY_ID_MASK) == PHY_ID_LAN8814)
51 + if (phydev_id_compare(phydev, PHY_ID_LAN8814))
52 return ((dt - 22) * 800) / 10;
54 return (dt * 400) / 10;
55 --- a/drivers/net/phy/phy_device.c
56 +++ b/drivers/net/phy/phy_device.c
57 @@ -422,8 +422,7 @@ int phy_unregister_fixup(const char *bus
58 fixup = list_entry(pos, struct phy_fixup, list);
60 if ((!strcmp(fixup->bus_id, bus_id)) &&
61 - ((fixup->phy_uid & phy_uid_mask) ==
62 - (phy_uid & phy_uid_mask))) {
63 + phy_id_compare(fixup->phy_uid, phy_uid, phy_uid_mask)) {
64 list_del(&fixup->list);
67 @@ -459,8 +458,8 @@ static int phy_needs_fixup(struct phy_de
68 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
71 - if ((fixup->phy_uid & fixup->phy_uid_mask) !=
72 - (phydev->phy_id & fixup->phy_uid_mask))
73 + if (!phy_id_compare(phydev->phy_id, fixup->phy_uid,
74 + fixup->phy_uid_mask))
75 if (fixup->phy_uid != PHY_ANY_UID)
78 @@ -507,15 +506,14 @@ static int phy_bus_match(struct device *
79 if (phydev->c45_ids.device_ids[i] == 0xffffffff)
82 - if ((phydrv->phy_id & phydrv->phy_id_mask) ==
83 - (phydev->c45_ids.device_ids[i] &
84 - phydrv->phy_id_mask))
85 + if (phy_id_compare(phydev->c45_ids.device_ids[i],
86 + phydrv->phy_id, phydrv->phy_id_mask))
91 - return (phydrv->phy_id & phydrv->phy_id_mask) ==
92 - (phydev->phy_id & phydrv->phy_id_mask);
93 + return phy_id_compare(phydev->phy_id, phydrv->phy_id,
94 + phydrv->phy_id_mask);
98 --- a/drivers/net/phy/phylink.c
99 +++ b/drivers/net/phy/phylink.c
100 @@ -3151,8 +3151,8 @@ static void phylink_sfp_link_up(void *up
102 static bool phylink_phy_no_inband(struct phy_device *phy)
104 - return phy->is_c45 &&
105 - (phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
106 + return phy->is_c45 && phy_id_compare(phy->c45_ids.device_ids[1],
107 + 0xae025150, 0xfffffff0);
110 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
111 --- a/include/linux/phy.h
112 +++ b/include/linux/phy.h
113 @@ -993,6 +993,34 @@ struct phy_driver {
114 #define PHY_ID_MATCH_MODEL(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 4)
115 #define PHY_ID_MATCH_VENDOR(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 10)
118 + * phy_id_compare - compare @id1 with @id2 taking account of @mask
119 + * @id1: first PHY ID
120 + * @id2: second PHY ID
121 + * @mask: the PHY ID mask, set bits are significant in matching
123 + * Return true if the bits from @id1 and @id2 specified by @mask match.
124 + * This uses an equivalent test to (@id & @mask) == (@phy_id & @mask).
126 +static inline bool phy_id_compare(u32 id1, u32 id2, u32 mask)
128 + return !((id1 ^ id2) & mask);
132 + * phydev_id_compare - compare @id with the PHY's Clause 22 ID
133 + * @phydev: the PHY device
134 + * @id: the PHY ID to be matched
136 + * Compare the @phydev clause 22 ID with the provided @id and return true or
137 + * false depending whether it matches, using the bound driver mask. The
138 + * @phydev must be bound to a driver.
140 +static inline bool phydev_id_compare(struct phy_device *phydev, u32 id)
142 + return phy_id_compare(id, phydev->phy_id, phydev->drv->phy_id_mask);
145 /* A Structure for boards to register fixups with the PHY Lib */
147 struct list_head list;