Skip to content

Commit 4b159f5

Browse files
Russell Kingdavem330
authored andcommitted
net: phy: add helpers for comparing phy IDs
There are several places which open code comparing PHY IDs. Provide a couple of helpers to assist with this, using a slightly simpler test than the original: - phy_id_compare() compares two arbitary PHY IDs and a mask of the significant bits in the ID. - phydev_id_compare() compares the bound phydev with the specified PHY ID, using the bound driver's mask. Signed-off-by: Russell King <[email protected]> Reviewed-by: Simon Horman <[email protected]> Reviewed-by: Andrew Lunn <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 8b6b7c1 commit 4b159f5

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

drivers/net/phy/micrel.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ static int ksz8051_ksz8795_match_phy_device(struct phy_device *phydev,
637637
{
638638
int ret;
639639

640-
if ((phydev->phy_id & MICREL_PHY_ID_MASK) != PHY_ID_KSZ8051)
640+
if (!phy_id_compare(phydev->phy_id, PHY_ID_KSZ8051, MICREL_PHY_ID_MASK))
641641
return 0;
642642

643643
ret = phy_read(phydev, MII_BMSR);
@@ -1566,7 +1566,7 @@ static int ksz9x31_cable_test_fault_length(struct phy_device *phydev, u16 stat)
15661566
*
15671567
* distance to fault = (VCT_DATA - 22) * 4 / cable propagation velocity
15681568
*/
1569-
if ((phydev->phy_id & MICREL_PHY_ID_MASK) == PHY_ID_KSZ9131)
1569+
if (phydev_id_compare(phydev, PHY_ID_KSZ9131))
15701570
dt = clamp(dt - 22, 0, 255);
15711571

15721572
return (dt * 400) / 10;
@@ -1998,7 +1998,7 @@ static __always_inline int ksz886x_cable_test_fault_length(struct phy_device *ph
19981998
*/
19991999
dt = FIELD_GET(data_mask, status);
20002000

2001-
if ((phydev->phy_id & MICREL_PHY_ID_MASK) == PHY_ID_LAN8814)
2001+
if (phydev_id_compare(phydev, PHY_ID_LAN8814))
20022002
return ((dt - 22) * 800) / 10;
20032003
else
20042004
return (dt * 400) / 10;

drivers/net/phy/phy_device.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,7 @@ int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask)
454454
fixup = list_entry(pos, struct phy_fixup, list);
455455

456456
if ((!strcmp(fixup->bus_id, bus_id)) &&
457-
((fixup->phy_uid & phy_uid_mask) ==
458-
(phy_uid & phy_uid_mask))) {
457+
phy_id_compare(fixup->phy_uid, phy_uid, phy_uid_mask)) {
459458
list_del(&fixup->list);
460459
kfree(fixup);
461460
ret = 0;
@@ -491,8 +490,8 @@ static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup)
491490
if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0)
492491
return 0;
493492

494-
if ((fixup->phy_uid & fixup->phy_uid_mask) !=
495-
(phydev->phy_id & fixup->phy_uid_mask))
493+
if (!phy_id_compare(phydev->phy_id, fixup->phy_uid,
494+
fixup->phy_uid_mask))
496495
if (fixup->phy_uid != PHY_ANY_UID)
497496
return 0;
498497

@@ -539,15 +538,14 @@ static int phy_bus_match(struct device *dev, struct device_driver *drv)
539538
if (phydev->c45_ids.device_ids[i] == 0xffffffff)
540539
continue;
541540

542-
if ((phydrv->phy_id & phydrv->phy_id_mask) ==
543-
(phydev->c45_ids.device_ids[i] &
544-
phydrv->phy_id_mask))
541+
if (phy_id_compare(phydev->c45_ids.device_ids[i],
542+
phydrv->phy_id, phydrv->phy_id_mask))
545543
return 1;
546544
}
547545
return 0;
548546
} else {
549-
return (phydrv->phy_id & phydrv->phy_id_mask) ==
550-
(phydev->phy_id & phydrv->phy_id_mask);
547+
return phy_id_compare(phydev->phy_id, phydrv->phy_id,
548+
phydrv->phy_id_mask);
551549
}
552550
}
553551

drivers/net/phy/phylink.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3151,8 +3151,8 @@ static void phylink_sfp_link_up(void *upstream)
31513151
*/
31523152
static bool phylink_phy_no_inband(struct phy_device *phy)
31533153
{
3154-
return phy->is_c45 &&
3155-
(phy->c45_ids.device_ids[1] & 0xfffffff0) == 0xae025150;
3154+
return phy->is_c45 && phy_id_compare(phy->c45_ids.device_ids[1],
3155+
0xae025150, 0xfffffff0);
31563156
}
31573157

31583158
static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)

include/linux/phy.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,34 @@ struct phy_driver {
11121112
#define PHY_ID_MATCH_MODEL(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 4)
11131113
#define PHY_ID_MATCH_VENDOR(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 10)
11141114

1115+
/**
1116+
* phy_id_compare - compare @id1 with @id2 taking account of @mask
1117+
* @id1: first PHY ID
1118+
* @id2: second PHY ID
1119+
* @mask: the PHY ID mask, set bits are significant in matching
1120+
*
1121+
* Return true if the bits from @id1 and @id2 specified by @mask match.
1122+
* This uses an equivalent test to (@id & @mask) == (@phy_id & @mask).
1123+
*/
1124+
static inline bool phy_id_compare(u32 id1, u32 id2, u32 mask)
1125+
{
1126+
return !((id1 ^ id2) & mask);
1127+
}
1128+
1129+
/**
1130+
* phydev_id_compare - compare @id with the PHY's Clause 22 ID
1131+
* @phydev: the PHY device
1132+
* @id: the PHY ID to be matched
1133+
*
1134+
* Compare the @phydev clause 22 ID with the provided @id and return true or
1135+
* false depending whether it matches, using the bound driver mask. The
1136+
* @phydev must be bound to a driver.
1137+
*/
1138+
static inline bool phydev_id_compare(struct phy_device *phydev, u32 id)
1139+
{
1140+
return phy_id_compare(id, phydev->phy_id, phydev->drv->phy_id_mask);
1141+
}
1142+
11151143
/* A Structure for boards to register fixups with the PHY Lib */
11161144
struct phy_fixup {
11171145
struct list_head list;

0 commit comments

Comments
 (0)