714-v6.8-02-net-phy-extend-PHY-package-API-to-support-multiple-g.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. From 9eea577eb1155fe4a183bc5e7bf269b0b2e7a6ba Mon Sep 17 00:00:00 2001
  2. From: Christian Marangi <[email protected]>
  3. Date: Fri, 15 Dec 2023 14:15:32 +0100
  4. Subject: [PATCH 2/4] net: phy: extend PHY package API to support multiple
  5. global address
  6. Current API for PHY package are limited to single address to configure
  7. global settings for the PHY package.
  8. It was found that some PHY package (for example the qca807x, a PHY
  9. package that is shipped with a bundle of 5 PHY) requires multiple PHY
  10. address to configure global settings. An example scenario is a PHY that
  11. have a dedicated PHY for PSGMII/serdes calibrarion and have a specific
  12. PHY in the package where the global PHY mode is set and affects every
  13. other PHY in the package.
  14. Change the API in the following way:
  15. - Change phy_package_join() to take the base addr of the PHY package
  16. instead of the global PHY addr.
  17. - Make __/phy_package_write/read() require an additional arg that
  18. select what global PHY address to use by passing the offset from the
  19. base addr passed on phy_package_join().
  20. Each user of this API is updated to follow this new implementation
  21. following a pattern where an enum is defined to declare the offset of the
  22. addr.
  23. We also drop the check if shared is defined as any user of the
  24. phy_package_read/write is expected to use phy_package_join first. Misuse
  25. of this will correctly trigger a kernel panic for NULL pointer
  26. exception.
  27. Signed-off-by: Christian Marangi <[email protected]>
  28. Signed-off-by: David S. Miller <[email protected]>
  29. ---
  30. drivers/net/phy/bcm54140.c | 16 ++++++--
  31. drivers/net/phy/mscc/mscc.h | 5 +++
  32. drivers/net/phy/mscc/mscc_main.c | 4 +-
  33. drivers/net/phy/phy_device.c | 35 +++++++++--------
  34. include/linux/phy.h | 64 +++++++++++++++++++++-----------
  35. 5 files changed, 80 insertions(+), 44 deletions(-)
  36. --- a/drivers/net/phy/bcm54140.c
  37. +++ b/drivers/net/phy/bcm54140.c
  38. @@ -128,6 +128,10 @@
  39. #define BCM54140_DEFAULT_DOWNSHIFT 5
  40. #define BCM54140_MAX_DOWNSHIFT 9
  41. +enum bcm54140_global_phy {
  42. + BCM54140_BASE_ADDR = 0,
  43. +};
  44. +
  45. struct bcm54140_priv {
  46. int port;
  47. int base_addr;
  48. @@ -429,11 +433,13 @@ static int bcm54140_base_read_rdb(struct
  49. int ret;
  50. phy_lock_mdio_bus(phydev);
  51. - ret = __phy_package_write(phydev, MII_BCM54XX_RDB_ADDR, rdb);
  52. + ret = __phy_package_write(phydev, BCM54140_BASE_ADDR,
  53. + MII_BCM54XX_RDB_ADDR, rdb);
  54. if (ret < 0)
  55. goto out;
  56. - ret = __phy_package_read(phydev, MII_BCM54XX_RDB_DATA);
  57. + ret = __phy_package_read(phydev, BCM54140_BASE_ADDR,
  58. + MII_BCM54XX_RDB_DATA);
  59. out:
  60. phy_unlock_mdio_bus(phydev);
  61. @@ -446,11 +452,13 @@ static int bcm54140_base_write_rdb(struc
  62. int ret;
  63. phy_lock_mdio_bus(phydev);
  64. - ret = __phy_package_write(phydev, MII_BCM54XX_RDB_ADDR, rdb);
  65. + ret = __phy_package_write(phydev, BCM54140_BASE_ADDR,
  66. + MII_BCM54XX_RDB_ADDR, rdb);
  67. if (ret < 0)
  68. goto out;
  69. - ret = __phy_package_write(phydev, MII_BCM54XX_RDB_DATA, val);
  70. + ret = __phy_package_write(phydev, BCM54140_BASE_ADDR,
  71. + MII_BCM54XX_RDB_DATA, val);
  72. out:
  73. phy_unlock_mdio_bus(phydev);
  74. --- a/drivers/net/phy/mscc/mscc.h
  75. +++ b/drivers/net/phy/mscc/mscc.h
  76. @@ -416,6 +416,11 @@ struct vsc8531_private {
  77. * gpio_lock: used for PHC operations. Common for all PHYs as the load/save GPIO
  78. * is shared.
  79. */
  80. +
  81. +enum vsc85xx_global_phy {
  82. + VSC88XX_BASE_ADDR = 0,
  83. +};
  84. +
  85. struct vsc85xx_shared_private {
  86. struct mutex gpio_lock;
  87. };
  88. --- a/drivers/net/phy/mscc/mscc_main.c
  89. +++ b/drivers/net/phy/mscc/mscc_main.c
  90. @@ -711,7 +711,7 @@ int phy_base_write(struct phy_device *ph
  91. dump_stack();
  92. }
  93. - return __phy_package_write(phydev, regnum, val);
  94. + return __phy_package_write(phydev, VSC88XX_BASE_ADDR, regnum, val);
  95. }
  96. /* phydev->bus->mdio_lock should be locked when using this function */
  97. @@ -722,7 +722,7 @@ int phy_base_read(struct phy_device *phy
  98. dump_stack();
  99. }
  100. - return __phy_package_read(phydev, regnum);
  101. + return __phy_package_read(phydev, VSC88XX_BASE_ADDR, regnum);
  102. }
  103. u32 vsc85xx_csr_read(struct phy_device *phydev,
  104. --- a/drivers/net/phy/phy_device.c
  105. +++ b/drivers/net/phy/phy_device.c
  106. @@ -1650,20 +1650,22 @@ EXPORT_SYMBOL_GPL(phy_driver_is_genphy_1
  107. /**
  108. * phy_package_join - join a common PHY group
  109. * @phydev: target phy_device struct
  110. - * @addr: cookie and PHY address for global register access
  111. + * @base_addr: cookie and base PHY address of PHY package for offset
  112. + * calculation of global register access
  113. * @priv_size: if non-zero allocate this amount of bytes for private data
  114. *
  115. * This joins a PHY group and provides a shared storage for all phydevs in
  116. * this group. This is intended to be used for packages which contain
  117. * more than one PHY, for example a quad PHY transceiver.
  118. *
  119. - * The addr parameter serves as a cookie which has to have the same value
  120. - * for all members of one group and as a PHY address to access generic
  121. - * registers of a PHY package. Usually, one of the PHY addresses of the
  122. - * different PHYs in the package provides access to these global registers.
  123. + * The base_addr parameter serves as cookie which has to have the same values
  124. + * for all members of one group and as the base PHY address of the PHY package
  125. + * for offset calculation to access generic registers of a PHY package.
  126. + * Usually, one of the PHY addresses of the different PHYs in the package
  127. + * provides access to these global registers.
  128. * The address which is given here, will be used in the phy_package_read()
  129. - * and phy_package_write() convenience functions. If your PHY doesn't have
  130. - * global registers you can just pick any of the PHY addresses.
  131. + * and phy_package_write() convenience functions as base and added to the
  132. + * passed offset in those functions.
  133. *
  134. * This will set the shared pointer of the phydev to the shared storage.
  135. * If this is the first call for a this cookie the shared storage will be
  136. @@ -1673,17 +1675,17 @@ EXPORT_SYMBOL_GPL(phy_driver_is_genphy_1
  137. * Returns < 1 on error, 0 on success. Esp. calling phy_package_join()
  138. * with the same cookie but a different priv_size is an error.
  139. */
  140. -int phy_package_join(struct phy_device *phydev, int addr, size_t priv_size)
  141. +int phy_package_join(struct phy_device *phydev, int base_addr, size_t priv_size)
  142. {
  143. struct mii_bus *bus = phydev->mdio.bus;
  144. struct phy_package_shared *shared;
  145. int ret;
  146. - if (addr < 0 || addr >= PHY_MAX_ADDR)
  147. + if (base_addr < 0 || base_addr >= PHY_MAX_ADDR)
  148. return -EINVAL;
  149. mutex_lock(&bus->shared_lock);
  150. - shared = bus->shared[addr];
  151. + shared = bus->shared[base_addr];
  152. if (!shared) {
  153. ret = -ENOMEM;
  154. shared = kzalloc(sizeof(*shared), GFP_KERNEL);
  155. @@ -1695,9 +1697,9 @@ int phy_package_join(struct phy_device *
  156. goto err_free;
  157. shared->priv_size = priv_size;
  158. }
  159. - shared->addr = addr;
  160. + shared->base_addr = base_addr;
  161. refcount_set(&shared->refcnt, 1);
  162. - bus->shared[addr] = shared;
  163. + bus->shared[base_addr] = shared;
  164. } else {
  165. ret = -EINVAL;
  166. if (priv_size && priv_size != shared->priv_size)
  167. @@ -1735,7 +1737,7 @@ void phy_package_leave(struct phy_device
  168. return;
  169. if (refcount_dec_and_mutex_lock(&shared->refcnt, &bus->shared_lock)) {
  170. - bus->shared[shared->addr] = NULL;
  171. + bus->shared[shared->base_addr] = NULL;
  172. mutex_unlock(&bus->shared_lock);
  173. kfree(shared->priv);
  174. kfree(shared);
  175. @@ -1754,7 +1756,8 @@ static void devm_phy_package_leave(struc
  176. * devm_phy_package_join - resource managed phy_package_join()
  177. * @dev: device that is registering this PHY package
  178. * @phydev: target phy_device struct
  179. - * @addr: cookie and PHY address for global register access
  180. + * @base_addr: cookie and base PHY address of PHY package for offset
  181. + * calculation of global register access
  182. * @priv_size: if non-zero allocate this amount of bytes for private data
  183. *
  184. * Managed phy_package_join(). Shared storage fetched by this function,
  185. @@ -1762,7 +1765,7 @@ static void devm_phy_package_leave(struc
  186. * phy_package_join() for more information.
  187. */
  188. int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
  189. - int addr, size_t priv_size)
  190. + int base_addr, size_t priv_size)
  191. {
  192. struct phy_device **ptr;
  193. int ret;
  194. @@ -1772,7 +1775,7 @@ int devm_phy_package_join(struct device
  195. if (!ptr)
  196. return -ENOMEM;
  197. - ret = phy_package_join(phydev, addr, priv_size);
  198. + ret = phy_package_join(phydev, base_addr, priv_size);
  199. if (!ret) {
  200. *ptr = phydev;
  201. --- a/include/linux/phy.h
  202. +++ b/include/linux/phy.h
  203. @@ -327,7 +327,8 @@ struct mdio_bus_stats {
  204. /**
  205. * struct phy_package_shared - Shared information in PHY packages
  206. - * @addr: Common PHY address used to combine PHYs in one package
  207. + * @base_addr: Base PHY address of PHY package used to combine PHYs
  208. + * in one package and for offset calculation of phy_package_read/write
  209. * @refcnt: Number of PHYs connected to this shared data
  210. * @flags: Initialization of PHY package
  211. * @priv_size: Size of the shared private data @priv
  212. @@ -338,7 +339,7 @@ struct mdio_bus_stats {
  213. * phy_package_leave().
  214. */
  215. struct phy_package_shared {
  216. - u8 addr;
  217. + u8 base_addr;
  218. refcount_t refcnt;
  219. unsigned long flags;
  220. size_t priv_size;
  221. @@ -1969,10 +1970,10 @@ int phy_ethtool_get_link_ksettings(struc
  222. int phy_ethtool_set_link_ksettings(struct net_device *ndev,
  223. const struct ethtool_link_ksettings *cmd);
  224. int phy_ethtool_nway_reset(struct net_device *ndev);
  225. -int phy_package_join(struct phy_device *phydev, int addr, size_t priv_size);
  226. +int phy_package_join(struct phy_device *phydev, int base_addr, size_t priv_size);
  227. void phy_package_leave(struct phy_device *phydev);
  228. int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
  229. - int addr, size_t priv_size);
  230. + int base_addr, size_t priv_size);
  231. int __init mdio_bus_init(void);
  232. void mdio_bus_exit(void);
  233. @@ -1995,46 +1996,65 @@ int __phy_hwtstamp_set(struct phy_device
  234. struct kernel_hwtstamp_config *config,
  235. struct netlink_ext_ack *extack);
  236. -static inline int phy_package_read(struct phy_device *phydev, u32 regnum)
  237. +static inline int phy_package_address(struct phy_device *phydev,
  238. + unsigned int addr_offset)
  239. {
  240. struct phy_package_shared *shared = phydev->shared;
  241. + u8 base_addr = shared->base_addr;
  242. - if (!shared)
  243. + if (addr_offset >= PHY_MAX_ADDR - base_addr)
  244. return -EIO;
  245. - return mdiobus_read(phydev->mdio.bus, shared->addr, regnum);
  246. + /* we know that addr will be in the range 0..31 and thus the
  247. + * implicit cast to a signed int is not a problem.
  248. + */
  249. + return base_addr + addr_offset;
  250. }
  251. -static inline int __phy_package_read(struct phy_device *phydev, u32 regnum)
  252. +static inline int phy_package_read(struct phy_device *phydev,
  253. + unsigned int addr_offset, u32 regnum)
  254. {
  255. - struct phy_package_shared *shared = phydev->shared;
  256. + int addr = phy_package_address(phydev, addr_offset);
  257. - if (!shared)
  258. - return -EIO;
  259. + if (addr < 0)
  260. + return addr;
  261. +
  262. + return mdiobus_read(phydev->mdio.bus, addr, regnum);
  263. +}
  264. +
  265. +static inline int __phy_package_read(struct phy_device *phydev,
  266. + unsigned int addr_offset, u32 regnum)
  267. +{
  268. + int addr = phy_package_address(phydev, addr_offset);
  269. +
  270. + if (addr < 0)
  271. + return addr;
  272. - return __mdiobus_read(phydev->mdio.bus, shared->addr, regnum);
  273. + return __mdiobus_read(phydev->mdio.bus, addr, regnum);
  274. }
  275. static inline int phy_package_write(struct phy_device *phydev,
  276. - u32 regnum, u16 val)
  277. + unsigned int addr_offset, u32 regnum,
  278. + u16 val)
  279. {
  280. - struct phy_package_shared *shared = phydev->shared;
  281. + int addr = phy_package_address(phydev, addr_offset);
  282. - if (!shared)
  283. - return -EIO;
  284. + if (addr < 0)
  285. + return addr;
  286. - return mdiobus_write(phydev->mdio.bus, shared->addr, regnum, val);
  287. + return mdiobus_write(phydev->mdio.bus, addr, regnum, val);
  288. }
  289. static inline int __phy_package_write(struct phy_device *phydev,
  290. - u32 regnum, u16 val)
  291. + unsigned int addr_offset, u32 regnum,
  292. + u16 val)
  293. {
  294. - struct phy_package_shared *shared = phydev->shared;
  295. + int addr = phy_package_address(phydev, addr_offset);
  296. - if (!shared)
  297. - return -EIO;
  298. + if (addr < 0)
  299. + return addr;
  300. - return __mdiobus_write(phydev->mdio.bus, shared->addr, regnum, val);
  301. + return __mdiobus_write(phydev->mdio.bus, addr, regnum, val);
  302. }
  303. static inline bool __phy_package_set_once(struct phy_device *phydev,