mdio.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * linux/mdio.h: definitions for MDIO (clause 45) transceivers
  3. * Copyright 2006-2009 Solarflare Communications Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation, incorporated herein by reference.
  8. */
  9. #ifndef __LINUX_MDIO_H__
  10. #define __LINUX_MDIO_H__
  11. #include <uapi/linux/mdio.h>
  12. struct mii_bus;
  13. /* Multiple levels of nesting are possible. However typically this is
  14. * limited to nested DSA like layer, a MUX layer, and the normal
  15. * user. Instead of trying to handle the general case, just define
  16. * these cases.
  17. */
  18. enum mdio_mutex_lock_class {
  19. MDIO_MUTEX_NORMAL,
  20. MDIO_MUTEX_MUX,
  21. MDIO_MUTEX_NESTED,
  22. };
  23. struct mdio_device {
  24. struct device dev;
  25. const struct dev_pm_ops *pm_ops;
  26. struct mii_bus *bus;
  27. int (*bus_match)(struct device *dev, struct device_driver *drv);
  28. void (*device_free)(struct mdio_device *mdiodev);
  29. void (*device_remove)(struct mdio_device *mdiodev);
  30. /* Bus address of the MDIO device (0-31) */
  31. int addr;
  32. int flags;
  33. };
  34. #define to_mdio_device(d) container_of(d, struct mdio_device, dev)
  35. /* struct mdio_driver_common: Common to all MDIO drivers */
  36. struct mdio_driver_common {
  37. struct device_driver driver;
  38. int flags;
  39. };
  40. #define MDIO_DEVICE_FLAG_PHY 1
  41. #define to_mdio_common_driver(d) \
  42. container_of(d, struct mdio_driver_common, driver)
  43. /* struct mdio_driver: Generic MDIO driver */
  44. struct mdio_driver {
  45. struct mdio_driver_common mdiodrv;
  46. /*
  47. * Called during discovery. Used to set
  48. * up device-specific structures, if any
  49. */
  50. int (*probe)(struct mdio_device *mdiodev);
  51. /* Clears up any memory if needed */
  52. void (*remove)(struct mdio_device *mdiodev);
  53. };
  54. #define to_mdio_driver(d) \
  55. container_of(to_mdio_common_driver(d), struct mdio_driver, mdiodrv)
  56. void mdio_device_free(struct mdio_device *mdiodev);
  57. struct mdio_device *mdio_device_create(struct mii_bus *bus, int addr);
  58. int mdio_device_register(struct mdio_device *mdiodev);
  59. void mdio_device_remove(struct mdio_device *mdiodev);
  60. int mdio_driver_register(struct mdio_driver *drv);
  61. void mdio_driver_unregister(struct mdio_driver *drv);
  62. static inline bool mdio_phy_id_is_c45(int phy_id)
  63. {
  64. return (phy_id & MDIO_PHY_ID_C45) && !(phy_id & ~MDIO_PHY_ID_C45_MASK);
  65. }
  66. static inline __u16 mdio_phy_id_prtad(int phy_id)
  67. {
  68. return (phy_id & MDIO_PHY_ID_PRTAD) >> 5;
  69. }
  70. static inline __u16 mdio_phy_id_devad(int phy_id)
  71. {
  72. return phy_id & MDIO_PHY_ID_DEVAD;
  73. }
  74. /**
  75. * struct mdio_if_info - Ethernet controller MDIO interface
  76. * @prtad: PRTAD of the PHY (%MDIO_PRTAD_NONE if not present/unknown)
  77. * @mmds: Mask of MMDs expected to be present in the PHY. This must be
  78. * non-zero unless @prtad = %MDIO_PRTAD_NONE.
  79. * @mode_support: MDIO modes supported. If %MDIO_SUPPORTS_C22 is set then
  80. * MII register access will be passed through with @devad =
  81. * %MDIO_DEVAD_NONE. If %MDIO_EMULATE_C22 is set then access to
  82. * commonly used clause 22 registers will be translated into
  83. * clause 45 registers.
  84. * @dev: Net device structure
  85. * @mdio_read: Register read function; returns value or negative error code
  86. * @mdio_write: Register write function; returns 0 or negative error code
  87. */
  88. struct mdio_if_info {
  89. int prtad;
  90. u32 mmds;
  91. unsigned mode_support;
  92. struct net_device *dev;
  93. int (*mdio_read)(struct net_device *dev, int prtad, int devad,
  94. u16 addr);
  95. int (*mdio_write)(struct net_device *dev, int prtad, int devad,
  96. u16 addr, u16 val);
  97. };
  98. #define MDIO_PRTAD_NONE (-1)
  99. #define MDIO_DEVAD_NONE (-1)
  100. #define MDIO_SUPPORTS_C22 1
  101. #define MDIO_SUPPORTS_C45 2
  102. #define MDIO_EMULATE_C22 4
  103. struct ethtool_cmd;
  104. struct ethtool_pauseparam;
  105. extern int mdio45_probe(struct mdio_if_info *mdio, int prtad);
  106. extern int mdio_set_flag(const struct mdio_if_info *mdio,
  107. int prtad, int devad, u16 addr, int mask,
  108. bool sense);
  109. extern int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmds);
  110. extern int mdio45_nway_restart(const struct mdio_if_info *mdio);
  111. extern void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
  112. struct ethtool_cmd *ecmd,
  113. u32 npage_adv, u32 npage_lpa);
  114. /**
  115. * mdio45_ethtool_gset - get settings for ETHTOOL_GSET
  116. * @mdio: MDIO interface
  117. * @ecmd: Ethtool request structure
  118. *
  119. * Since the CSRs for auto-negotiation using next pages are not fully
  120. * standardised, this function does not attempt to decode them. Use
  121. * mdio45_ethtool_gset_npage() to specify advertisement bits from next
  122. * pages.
  123. */
  124. static inline void mdio45_ethtool_gset(const struct mdio_if_info *mdio,
  125. struct ethtool_cmd *ecmd)
  126. {
  127. mdio45_ethtool_gset_npage(mdio, ecmd, 0, 0);
  128. }
  129. extern int mdio_mii_ioctl(const struct mdio_if_info *mdio,
  130. struct mii_ioctl_data *mii_data, int cmd);
  131. /**
  132. * mmd_eee_cap_to_ethtool_sup_t
  133. * @eee_cap: value of the MMD EEE Capability register
  134. *
  135. * A small helper function that translates MMD EEE Capability (3.20) bits
  136. * to ethtool supported settings.
  137. */
  138. static inline u32 mmd_eee_cap_to_ethtool_sup_t(u16 eee_cap)
  139. {
  140. u32 supported = 0;
  141. if (eee_cap & MDIO_EEE_100TX)
  142. supported |= SUPPORTED_100baseT_Full;
  143. if (eee_cap & MDIO_EEE_1000T)
  144. supported |= SUPPORTED_1000baseT_Full;
  145. if (eee_cap & MDIO_EEE_10GT)
  146. supported |= SUPPORTED_10000baseT_Full;
  147. if (eee_cap & MDIO_EEE_1000KX)
  148. supported |= SUPPORTED_1000baseKX_Full;
  149. if (eee_cap & MDIO_EEE_10GKX4)
  150. supported |= SUPPORTED_10000baseKX4_Full;
  151. if (eee_cap & MDIO_EEE_10GKR)
  152. supported |= SUPPORTED_10000baseKR_Full;
  153. return supported;
  154. }
  155. /**
  156. * mmd_eee_adv_to_ethtool_adv_t
  157. * @eee_adv: value of the MMD EEE Advertisement/Link Partner Ability registers
  158. *
  159. * A small helper function that translates the MMD EEE Advertisment (7.60)
  160. * and MMD EEE Link Partner Ability (7.61) bits to ethtool advertisement
  161. * settings.
  162. */
  163. static inline u32 mmd_eee_adv_to_ethtool_adv_t(u16 eee_adv)
  164. {
  165. u32 adv = 0;
  166. if (eee_adv & MDIO_EEE_100TX)
  167. adv |= ADVERTISED_100baseT_Full;
  168. if (eee_adv & MDIO_EEE_1000T)
  169. adv |= ADVERTISED_1000baseT_Full;
  170. if (eee_adv & MDIO_EEE_10GT)
  171. adv |= ADVERTISED_10000baseT_Full;
  172. if (eee_adv & MDIO_EEE_1000KX)
  173. adv |= ADVERTISED_1000baseKX_Full;
  174. if (eee_adv & MDIO_EEE_10GKX4)
  175. adv |= ADVERTISED_10000baseKX4_Full;
  176. if (eee_adv & MDIO_EEE_10GKR)
  177. adv |= ADVERTISED_10000baseKR_Full;
  178. return adv;
  179. }
  180. /**
  181. * ethtool_adv_to_mmd_eee_adv_t
  182. * @adv: the ethtool advertisement settings
  183. *
  184. * A small helper function that translates ethtool advertisement settings
  185. * to EEE advertisements for the MMD EEE Advertisement (7.60) and
  186. * MMD EEE Link Partner Ability (7.61) registers.
  187. */
  188. static inline u16 ethtool_adv_to_mmd_eee_adv_t(u32 adv)
  189. {
  190. u16 reg = 0;
  191. if (adv & ADVERTISED_100baseT_Full)
  192. reg |= MDIO_EEE_100TX;
  193. if (adv & ADVERTISED_1000baseT_Full)
  194. reg |= MDIO_EEE_1000T;
  195. if (adv & ADVERTISED_10000baseT_Full)
  196. reg |= MDIO_EEE_10GT;
  197. if (adv & ADVERTISED_1000baseKX_Full)
  198. reg |= MDIO_EEE_1000KX;
  199. if (adv & ADVERTISED_10000baseKX4_Full)
  200. reg |= MDIO_EEE_10GKX4;
  201. if (adv & ADVERTISED_10000baseKR_Full)
  202. reg |= MDIO_EEE_10GKR;
  203. return reg;
  204. }
  205. int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum);
  206. int mdiobus_read_nested(struct mii_bus *bus, int addr, u32 regnum);
  207. int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val);
  208. int mdiobus_write_nested(struct mii_bus *bus, int addr, u32 regnum, u16 val);
  209. int mdiobus_register_device(struct mdio_device *mdiodev);
  210. int mdiobus_unregister_device(struct mdio_device *mdiodev);
  211. bool mdiobus_is_registered_device(struct mii_bus *bus, int addr);
  212. struct phy_device *mdiobus_get_phy(struct mii_bus *bus, int addr);
  213. /**
  214. * module_mdio_driver() - Helper macro for registering mdio drivers
  215. *
  216. * Helper macro for MDIO drivers which do not do anything special in module
  217. * init/exit. Each module may only use this macro once, and calling it
  218. * replaces module_init() and module_exit().
  219. */
  220. #define mdio_module_driver(_mdio_driver) \
  221. static int __init mdio_module_init(void) \
  222. { \
  223. return mdio_driver_register(&_mdio_driver); \
  224. } \
  225. module_init(mdio_module_init); \
  226. static void __exit mdio_module_exit(void) \
  227. { \
  228. mdio_driver_unregister(&_mdio_driver); \
  229. } \
  230. module_exit(mdio_module_exit)
  231. #endif /* __LINUX_MDIO_H__ */