Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <rmk+kernel@armlinux.org.uk>
Reviewed-by: Simon Horman <simon.horman@corigine.com>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Russell King and committed by
David S. Miller
4b159f50 8b6b7c11

+40 -14
+3 -3
drivers/net/phy/micrel.c
··· 637 637 { 638 638 int ret; 639 639 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)) 641 641 return 0; 642 642 643 643 ret = phy_read(phydev, MII_BMSR); ··· 1566 1566 * 1567 1567 * distance to fault = (VCT_DATA - 22) * 4 / cable propagation velocity 1568 1568 */ 1569 - if ((phydev->phy_id & MICREL_PHY_ID_MASK) == PHY_ID_KSZ9131) 1569 + if (phydev_id_compare(phydev, PHY_ID_KSZ9131)) 1570 1570 dt = clamp(dt - 22, 0, 255); 1571 1571 1572 1572 return (dt * 400) / 10; ··· 1998 1998 */ 1999 1999 dt = FIELD_GET(data_mask, status); 2000 2000 2001 - if ((phydev->phy_id & MICREL_PHY_ID_MASK) == PHY_ID_LAN8814) 2001 + if (phydev_id_compare(phydev, PHY_ID_LAN8814)) 2002 2002 return ((dt - 22) * 800) / 10; 2003 2003 else 2004 2004 return (dt * 400) / 10;
+7 -9
drivers/net/phy/phy_device.c
··· 454 454 fixup = list_entry(pos, struct phy_fixup, list); 455 455 456 456 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)) { 459 458 list_del(&fixup->list); 460 459 kfree(fixup); 461 460 ret = 0; ··· 490 491 if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0) 491 492 return 0; 492 493 493 - if ((fixup->phy_uid & fixup->phy_uid_mask) != 494 - (phydev->phy_id & fixup->phy_uid_mask)) 494 + if (!phy_id_compare(phydev->phy_id, fixup->phy_uid, 495 + fixup->phy_uid_mask)) 495 496 if (fixup->phy_uid != PHY_ANY_UID) 496 497 return 0; 497 498 ··· 538 539 if (phydev->c45_ids.device_ids[i] == 0xffffffff) 539 540 continue; 540 541 541 - if ((phydrv->phy_id & phydrv->phy_id_mask) == 542 - (phydev->c45_ids.device_ids[i] & 543 - phydrv->phy_id_mask)) 542 + if (phy_id_compare(phydev->c45_ids.device_ids[i], 543 + phydrv->phy_id, phydrv->phy_id_mask)) 544 544 return 1; 545 545 } 546 546 return 0; 547 547 } else { 548 - return (phydrv->phy_id & phydrv->phy_id_mask) == 549 - (phydev->phy_id & phydrv->phy_id_mask); 548 + return phy_id_compare(phydev->phy_id, phydrv->phy_id, 549 + phydrv->phy_id_mask); 550 550 } 551 551 } 552 552
+2 -2
drivers/net/phy/phylink.c
··· 3151 3151 */ 3152 3152 static bool phylink_phy_no_inband(struct phy_device *phy) 3153 3153 { 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); 3156 3156 } 3157 3157 3158 3158 static int phylink_sfp_connect_phy(void *upstream, struct phy_device *phy)
+28
include/linux/phy.h
··· 1112 1112 #define PHY_ID_MATCH_MODEL(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 4) 1113 1113 #define PHY_ID_MATCH_VENDOR(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 10) 1114 1114 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 + 1115 1143 /* A Structure for boards to register fixups with the PHY Lib */ 1116 1144 struct phy_fixup { 1117 1145 struct list_head list;