715-17-v6.5-net-phy-add-helpers-for-comparing-phy-IDs.patch 5.0 KB

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