phy.h 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Framework and drivers for configuring and reading different PHYs
  4. * Based on code in sungem_phy.c and (long-removed) gianfar_phy.c
  5. *
  6. * Author: Andy Fleming
  7. *
  8. * Copyright (c) 2004 Freescale Semiconductor, Inc.
  9. */
  10. #ifndef __PHY_H
  11. #define __PHY_H
  12. #include <linux/compiler.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/ethtool.h>
  15. #include <linux/linkmode.h>
  16. #include <linux/netlink.h>
  17. #include <linux/mdio.h>
  18. #include <linux/mii.h>
  19. #include <linux/mii_timestamper.h>
  20. #include <linux/module.h>
  21. #include <linux/timer.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/mod_devicetable.h>
  24. #include <linux/u64_stats_sync.h>
  25. #include <linux/irqreturn.h>
  26. #include <linux/iopoll.h>
  27. #include <linux/refcount.h>
  28. #include <linux/android_kabi.h>
  29. #include <linux/atomic.h>
  30. #define PHY_DEFAULT_FEATURES (SUPPORTED_Autoneg | \
  31. SUPPORTED_TP | \
  32. SUPPORTED_MII)
  33. #define PHY_10BT_FEATURES (SUPPORTED_10baseT_Half | \
  34. SUPPORTED_10baseT_Full)
  35. #define PHY_100BT_FEATURES (SUPPORTED_100baseT_Half | \
  36. SUPPORTED_100baseT_Full)
  37. #define PHY_1000BT_FEATURES (SUPPORTED_1000baseT_Half | \
  38. SUPPORTED_1000baseT_Full)
  39. extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_features) __ro_after_init;
  40. extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_basic_t1_features) __ro_after_init;
  41. extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_features) __ro_after_init;
  42. extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_fibre_features) __ro_after_init;
  43. extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_gbit_all_ports_features) __ro_after_init;
  44. extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_features) __ro_after_init;
  45. extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_fec_features) __ro_after_init;
  46. extern __ETHTOOL_DECLARE_LINK_MODE_MASK(phy_10gbit_full_features) __ro_after_init;
  47. #define PHY_BASIC_FEATURES ((unsigned long *)&phy_basic_features)
  48. #define PHY_BASIC_T1_FEATURES ((unsigned long *)&phy_basic_t1_features)
  49. #define PHY_GBIT_FEATURES ((unsigned long *)&phy_gbit_features)
  50. #define PHY_GBIT_FIBRE_FEATURES ((unsigned long *)&phy_gbit_fibre_features)
  51. #define PHY_GBIT_ALL_PORTS_FEATURES ((unsigned long *)&phy_gbit_all_ports_features)
  52. #define PHY_10GBIT_FEATURES ((unsigned long *)&phy_10gbit_features)
  53. #define PHY_10GBIT_FEC_FEATURES ((unsigned long *)&phy_10gbit_fec_features)
  54. #define PHY_10GBIT_FULL_FEATURES ((unsigned long *)&phy_10gbit_full_features)
  55. extern const int phy_basic_ports_array[3];
  56. extern const int phy_fibre_port_array[1];
  57. extern const int phy_all_ports_features_array[7];
  58. extern const int phy_10_100_features_array[4];
  59. extern const int phy_basic_t1_features_array[2];
  60. extern const int phy_gbit_features_array[2];
  61. extern const int phy_10gbit_features_array[1];
  62. /*
  63. * Set phydev->irq to PHY_POLL if interrupts are not supported,
  64. * or not desired for this PHY. Set to PHY_IGNORE_INTERRUPT if
  65. * the attached driver handles the interrupt
  66. */
  67. #define PHY_POLL -1
  68. #define PHY_IGNORE_INTERRUPT -2
  69. #define PHY_IS_INTERNAL 0x00000001
  70. #define PHY_RST_AFTER_CLK_EN 0x00000002
  71. #define PHY_POLL_CABLE_TEST 0x00000004
  72. #define MDIO_DEVICE_IS_PHY 0x80000000
  73. /**
  74. * enum phy_interface_t - Interface Mode definitions
  75. *
  76. * @PHY_INTERFACE_MODE_NA: Not Applicable - don't touch
  77. * @PHY_INTERFACE_MODE_INTERNAL: No interface, MAC and PHY combined
  78. * @PHY_INTERFACE_MODE_MII: Median-independent interface
  79. * @PHY_INTERFACE_MODE_GMII: Gigabit median-independent interface
  80. * @PHY_INTERFACE_MODE_SGMII: Serial gigabit media-independent interface
  81. * @PHY_INTERFACE_MODE_TBI: Ten Bit Interface
  82. * @PHY_INTERFACE_MODE_REVMII: Reverse Media Independent Interface
  83. * @PHY_INTERFACE_MODE_RMII: Reduced Media Independent Interface
  84. * @PHY_INTERFACE_MODE_RGMII: Reduced gigabit media-independent interface
  85. * @PHY_INTERFACE_MODE_RGMII_ID: RGMII with Internal RX+TX delay
  86. * @PHY_INTERFACE_MODE_RGMII_RXID: RGMII with Internal RX delay
  87. * @PHY_INTERFACE_MODE_RGMII_TXID: RGMII with Internal RX delay
  88. * @PHY_INTERFACE_MODE_RTBI: Reduced TBI
  89. * @PHY_INTERFACE_MODE_SMII: ??? MII
  90. * @PHY_INTERFACE_MODE_XGMII: 10 gigabit media-independent interface
  91. * @PHY_INTERFACE_MODE_XLGMII:40 gigabit media-independent interface
  92. * @PHY_INTERFACE_MODE_MOCA: Multimedia over Coax
  93. * @PHY_INTERFACE_MODE_QSGMII: Quad SGMII
  94. * @PHY_INTERFACE_MODE_TRGMII: Turbo RGMII
  95. * @PHY_INTERFACE_MODE_1000BASEX: 1000 BaseX
  96. * @PHY_INTERFACE_MODE_2500BASEX: 2500 BaseX
  97. * @PHY_INTERFACE_MODE_RXAUI: Reduced XAUI
  98. * @PHY_INTERFACE_MODE_XAUI: 10 Gigabit Attachment Unit Interface
  99. * @PHY_INTERFACE_MODE_10GBASER: 10G BaseR
  100. * @PHY_INTERFACE_MODE_USXGMII: Universal Serial 10GE MII
  101. * @PHY_INTERFACE_MODE_10GKR: 10GBASE-KR - with Clause 73 AN
  102. * @PHY_INTERFACE_MODE_MAX: Book keeping
  103. *
  104. * Describes the interface between the MAC and PHY.
  105. */
  106. typedef enum {
  107. PHY_INTERFACE_MODE_NA,
  108. PHY_INTERFACE_MODE_INTERNAL,
  109. PHY_INTERFACE_MODE_MII,
  110. PHY_INTERFACE_MODE_GMII,
  111. PHY_INTERFACE_MODE_SGMII,
  112. PHY_INTERFACE_MODE_TBI,
  113. PHY_INTERFACE_MODE_REVMII,
  114. PHY_INTERFACE_MODE_RMII,
  115. PHY_INTERFACE_MODE_RGMII,
  116. PHY_INTERFACE_MODE_RGMII_ID,
  117. PHY_INTERFACE_MODE_RGMII_RXID,
  118. PHY_INTERFACE_MODE_RGMII_TXID,
  119. PHY_INTERFACE_MODE_RTBI,
  120. PHY_INTERFACE_MODE_SMII,
  121. PHY_INTERFACE_MODE_XGMII,
  122. PHY_INTERFACE_MODE_XLGMII,
  123. PHY_INTERFACE_MODE_MOCA,
  124. PHY_INTERFACE_MODE_QSGMII,
  125. PHY_INTERFACE_MODE_TRGMII,
  126. PHY_INTERFACE_MODE_1000BASEX,
  127. PHY_INTERFACE_MODE_2500BASEX,
  128. PHY_INTERFACE_MODE_RXAUI,
  129. PHY_INTERFACE_MODE_XAUI,
  130. /* 10GBASE-R, XFI, SFI - single lane 10G Serdes */
  131. PHY_INTERFACE_MODE_10GBASER,
  132. PHY_INTERFACE_MODE_USXGMII,
  133. /* 10GBASE-KR - with Clause 73 AN */
  134. PHY_INTERFACE_MODE_10GKR,
  135. PHY_INTERFACE_MODE_MAX,
  136. } phy_interface_t;
  137. /*
  138. * phy_supported_speeds - return all speeds currently supported by a PHY device
  139. */
  140. unsigned int phy_supported_speeds(struct phy_device *phy,
  141. unsigned int *speeds,
  142. unsigned int size);
  143. /**
  144. * phy_modes - map phy_interface_t enum to device tree binding of phy-mode
  145. * @interface: enum phy_interface_t value
  146. *
  147. * Description: maps enum &phy_interface_t defined in this file
  148. * into the device tree binding of 'phy-mode', so that Ethernet
  149. * device driver can get PHY interface from device tree.
  150. */
  151. static inline const char *phy_modes(phy_interface_t interface)
  152. {
  153. switch (interface) {
  154. case PHY_INTERFACE_MODE_NA:
  155. return "";
  156. case PHY_INTERFACE_MODE_INTERNAL:
  157. return "internal";
  158. case PHY_INTERFACE_MODE_MII:
  159. return "mii";
  160. case PHY_INTERFACE_MODE_GMII:
  161. return "gmii";
  162. case PHY_INTERFACE_MODE_SGMII:
  163. return "sgmii";
  164. case PHY_INTERFACE_MODE_TBI:
  165. return "tbi";
  166. case PHY_INTERFACE_MODE_REVMII:
  167. return "rev-mii";
  168. case PHY_INTERFACE_MODE_RMII:
  169. return "rmii";
  170. case PHY_INTERFACE_MODE_RGMII:
  171. return "rgmii";
  172. case PHY_INTERFACE_MODE_RGMII_ID:
  173. return "rgmii-id";
  174. case PHY_INTERFACE_MODE_RGMII_RXID:
  175. return "rgmii-rxid";
  176. case PHY_INTERFACE_MODE_RGMII_TXID:
  177. return "rgmii-txid";
  178. case PHY_INTERFACE_MODE_RTBI:
  179. return "rtbi";
  180. case PHY_INTERFACE_MODE_SMII:
  181. return "smii";
  182. case PHY_INTERFACE_MODE_XGMII:
  183. return "xgmii";
  184. case PHY_INTERFACE_MODE_XLGMII:
  185. return "xlgmii";
  186. case PHY_INTERFACE_MODE_MOCA:
  187. return "moca";
  188. case PHY_INTERFACE_MODE_QSGMII:
  189. return "qsgmii";
  190. case PHY_INTERFACE_MODE_TRGMII:
  191. return "trgmii";
  192. case PHY_INTERFACE_MODE_1000BASEX:
  193. return "1000base-x";
  194. case PHY_INTERFACE_MODE_2500BASEX:
  195. return "2500base-x";
  196. case PHY_INTERFACE_MODE_RXAUI:
  197. return "rxaui";
  198. case PHY_INTERFACE_MODE_XAUI:
  199. return "xaui";
  200. case PHY_INTERFACE_MODE_10GBASER:
  201. return "10gbase-r";
  202. case PHY_INTERFACE_MODE_USXGMII:
  203. return "usxgmii";
  204. case PHY_INTERFACE_MODE_10GKR:
  205. return "10gbase-kr";
  206. default:
  207. return "unknown";
  208. }
  209. }
  210. #define PHY_INIT_TIMEOUT 100000
  211. #define PHY_FORCE_TIMEOUT 10
  212. #define PHY_MAX_ADDR 32
  213. /* Used when trying to connect to a specific phy (mii bus id:phy device id) */
  214. #define PHY_ID_FMT "%s:%02x"
  215. #define MII_BUS_ID_SIZE 61
  216. struct device;
  217. struct phylink;
  218. struct sfp_bus;
  219. struct sfp_upstream_ops;
  220. struct sk_buff;
  221. /**
  222. * struct mdio_bus_stats - Statistics counters for MDIO busses
  223. * @transfers: Total number of transfers, i.e. @writes + @reads
  224. * @errors: Number of MDIO transfers that returned an error
  225. * @writes: Number of write transfers
  226. * @reads: Number of read transfers
  227. * @syncp: Synchronisation for incrementing statistics
  228. */
  229. struct mdio_bus_stats {
  230. u64_stats_t transfers;
  231. u64_stats_t errors;
  232. u64_stats_t writes;
  233. u64_stats_t reads;
  234. /* Must be last, add new statistics above */
  235. struct u64_stats_sync syncp;
  236. };
  237. /**
  238. * struct phy_package_shared - Shared information in PHY packages
  239. * @addr: Common PHY address used to combine PHYs in one package
  240. * @refcnt: Number of PHYs connected to this shared data
  241. * @flags: Initialization of PHY package
  242. * @priv_size: Size of the shared private data @priv
  243. * @priv: Driver private data shared across a PHY package
  244. *
  245. * Represents a shared structure between different phydev's in the same
  246. * package, for example a quad PHY. See phy_package_join() and
  247. * phy_package_leave().
  248. */
  249. struct phy_package_shared {
  250. int addr;
  251. refcount_t refcnt;
  252. unsigned long flags;
  253. size_t priv_size;
  254. /* private data pointer */
  255. /* note that this pointer is shared between different phydevs and
  256. * the user has to take care of appropriate locking. It is allocated
  257. * and freed automatically by phy_package_join() and
  258. * phy_package_leave().
  259. */
  260. void *priv;
  261. };
  262. /* used as bit number in atomic bitops */
  263. #define PHY_SHARED_F_INIT_DONE 0
  264. #define PHY_SHARED_F_PROBE_DONE 1
  265. /**
  266. * struct mii_bus - Represents an MDIO bus
  267. *
  268. * @owner: Who owns this device
  269. * @name: User friendly name for this MDIO device, or driver name
  270. * @id: Unique identifier for this bus, typical from bus hierarchy
  271. * @priv: Driver private data
  272. *
  273. * The Bus class for PHYs. Devices which provide access to
  274. * PHYs should register using this structure
  275. */
  276. struct mii_bus {
  277. struct module *owner;
  278. const char *name;
  279. char id[MII_BUS_ID_SIZE];
  280. void *priv;
  281. /** @read: Perform a read transfer on the bus */
  282. int (*read)(struct mii_bus *bus, int addr, int regnum);
  283. /** @write: Perform a write transfer on the bus */
  284. int (*write)(struct mii_bus *bus, int addr, int regnum, u16 val);
  285. /** @reset: Perform a reset of the bus */
  286. int (*reset)(struct mii_bus *bus);
  287. /** @stats: Statistic counters per device on the bus */
  288. struct mdio_bus_stats stats[PHY_MAX_ADDR];
  289. /**
  290. * @mdio_lock: A lock to ensure that only one thing can read/write
  291. * the MDIO bus at a time
  292. */
  293. struct mutex mdio_lock;
  294. /** @parent: Parent device of this bus */
  295. struct device *parent;
  296. /** @state: State of bus structure */
  297. enum {
  298. MDIOBUS_ALLOCATED = 1,
  299. MDIOBUS_REGISTERED,
  300. MDIOBUS_UNREGISTERED,
  301. MDIOBUS_RELEASED,
  302. } state;
  303. /** @dev: Kernel device representation */
  304. struct device dev;
  305. /** @mdio_map: list of all MDIO devices on bus */
  306. struct mdio_device *mdio_map[PHY_MAX_ADDR];
  307. /** @phy_mask: PHY addresses to be ignored when probing */
  308. u32 phy_mask;
  309. /** @phy_ignore_ta_mask: PHY addresses to ignore the TA/read failure */
  310. u32 phy_ignore_ta_mask;
  311. /**
  312. * @irq: An array of interrupts, each PHY's interrupt at the index
  313. * matching its address
  314. */
  315. int irq[PHY_MAX_ADDR];
  316. /** @reset_delay_us: GPIO reset pulse width in microseconds */
  317. int reset_delay_us;
  318. /** @reset_post_delay_us: GPIO reset deassert delay in microseconds */
  319. int reset_post_delay_us;
  320. /** @reset_gpiod: Reset GPIO descriptor pointer */
  321. struct gpio_desc *reset_gpiod;
  322. /** @probe_capabilities: bus capabilities, used for probing */
  323. enum {
  324. MDIOBUS_NO_CAP = 0,
  325. MDIOBUS_C22,
  326. MDIOBUS_C45,
  327. MDIOBUS_C22_C45,
  328. } probe_capabilities;
  329. /** @shared_lock: protect access to the shared element */
  330. struct mutex shared_lock;
  331. /** @shared: shared state across different PHYs */
  332. struct phy_package_shared *shared[PHY_MAX_ADDR];
  333. };
  334. #define to_mii_bus(d) container_of(d, struct mii_bus, dev)
  335. struct mii_bus *mdiobus_alloc_size(size_t size);
  336. /**
  337. * mdiobus_alloc - Allocate an MDIO bus structure
  338. *
  339. * The internal state of the MDIO bus will be set of MDIOBUS_ALLOCATED ready
  340. * for the driver to register the bus.
  341. */
  342. static inline struct mii_bus *mdiobus_alloc(void)
  343. {
  344. return mdiobus_alloc_size(0);
  345. }
  346. int __mdiobus_register(struct mii_bus *bus, struct module *owner);
  347. int __devm_mdiobus_register(struct device *dev, struct mii_bus *bus,
  348. struct module *owner);
  349. #define mdiobus_register(bus) __mdiobus_register(bus, THIS_MODULE)
  350. #define devm_mdiobus_register(dev, bus) \
  351. __devm_mdiobus_register(dev, bus, THIS_MODULE)
  352. void mdiobus_unregister(struct mii_bus *bus);
  353. void mdiobus_free(struct mii_bus *bus);
  354. struct mii_bus *devm_mdiobus_alloc_size(struct device *dev, int sizeof_priv);
  355. static inline struct mii_bus *devm_mdiobus_alloc(struct device *dev)
  356. {
  357. return devm_mdiobus_alloc_size(dev, 0);
  358. }
  359. struct mii_bus *mdio_find_bus(const char *mdio_name);
  360. struct phy_device *mdiobus_scan(struct mii_bus *bus, int addr);
  361. #define PHY_INTERRUPT_DISABLED false
  362. #define PHY_INTERRUPT_ENABLED true
  363. /**
  364. * enum phy_state - PHY state machine states:
  365. *
  366. * @PHY_DOWN: PHY device and driver are not ready for anything. probe
  367. * should be called if and only if the PHY is in this state,
  368. * given that the PHY device exists.
  369. * - PHY driver probe function will set the state to @PHY_READY
  370. *
  371. * @PHY_READY: PHY is ready to send and receive packets, but the
  372. * controller is not. By default, PHYs which do not implement
  373. * probe will be set to this state by phy_probe().
  374. * - start will set the state to UP
  375. *
  376. * @PHY_UP: The PHY and attached device are ready to do work.
  377. * Interrupts should be started here.
  378. * - timer moves to @PHY_NOLINK or @PHY_RUNNING
  379. *
  380. * @PHY_NOLINK: PHY is up, but not currently plugged in.
  381. * - irq or timer will set @PHY_RUNNING if link comes back
  382. * - phy_stop moves to @PHY_HALTED
  383. *
  384. * @PHY_RUNNING: PHY is currently up, running, and possibly sending
  385. * and/or receiving packets
  386. * - irq or timer will set @PHY_NOLINK if link goes down
  387. * - phy_stop moves to @PHY_HALTED
  388. *
  389. * @PHY_CABLETEST: PHY is performing a cable test. Packet reception/sending
  390. * is not expected to work, carrier will be indicated as down. PHY will be
  391. * poll once per second, or on interrupt for it current state.
  392. * Once complete, move to UP to restart the PHY.
  393. * - phy_stop aborts the running test and moves to @PHY_HALTED
  394. *
  395. * @PHY_HALTED: PHY is up, but no polling or interrupts are done. Or
  396. * PHY is in an error state.
  397. * - phy_start moves to @PHY_UP
  398. */
  399. enum phy_state {
  400. PHY_DOWN = 0,
  401. PHY_READY,
  402. PHY_HALTED,
  403. PHY_UP,
  404. PHY_RUNNING,
  405. PHY_NOLINK,
  406. PHY_CABLETEST,
  407. };
  408. #define MDIO_MMD_NUM 32
  409. /**
  410. * struct phy_c45_device_ids - 802.3-c45 Device Identifiers
  411. * @devices_in_package: IEEE 802.3 devices in package register value.
  412. * @mmds_present: bit vector of MMDs present.
  413. * @device_ids: The device identifer for each present device.
  414. */
  415. struct phy_c45_device_ids {
  416. u32 devices_in_package;
  417. u32 mmds_present;
  418. u32 device_ids[MDIO_MMD_NUM];
  419. };
  420. struct macsec_context;
  421. struct macsec_ops;
  422. /**
  423. * struct phy_device - An instance of a PHY
  424. *
  425. * @mdio: MDIO bus this PHY is on
  426. * @drv: Pointer to the driver for this PHY instance
  427. * @phy_id: UID for this device found during discovery
  428. * @c45_ids: 802.3-c45 Device Identifiers if is_c45.
  429. * @is_c45: Set to true if this PHY uses clause 45 addressing.
  430. * @is_internal: Set to true if this PHY is internal to a MAC.
  431. * @is_pseudo_fixed_link: Set to true if this PHY is an Ethernet switch, etc.
  432. * @is_gigabit_capable: Set to true if PHY supports 1000Mbps
  433. * @has_fixups: Set to true if this PHY has fixups/quirks.
  434. * @suspended: Set to true if this PHY has been suspended successfully.
  435. * @suspended_by_mdio_bus: Set to true if this PHY was suspended by MDIO bus.
  436. * @sysfs_links: Internal boolean tracking sysfs symbolic links setup/removal.
  437. * @loopback_enabled: Set true if this PHY has been loopbacked successfully.
  438. * @downshifted_rate: Set true if link speed has been downshifted.
  439. * @state: State of the PHY for management purposes
  440. * @dev_flags: Device-specific flags used by the PHY driver.
  441. * @irq: IRQ number of the PHY's interrupt (-1 if none)
  442. * @phy_timer: The timer for handling the state machine
  443. * @phylink: Pointer to phylink instance for this PHY
  444. * @sfp_bus_attached: Flag indicating whether the SFP bus has been attached
  445. * @sfp_bus: SFP bus attached to this PHY's fiber port
  446. * @attached_dev: The attached enet driver's device instance ptr
  447. * @adjust_link: Callback for the enet controller to respond to changes: in the
  448. * link state.
  449. * @phy_link_change: Callback for phylink for notification of link change
  450. * @macsec_ops: MACsec offloading ops.
  451. *
  452. * @speed: Current link speed
  453. * @duplex: Current duplex
  454. * @port: Current port
  455. * @pause: Current pause
  456. * @asym_pause: Current asymmetric pause
  457. * @supported: Combined MAC/PHY supported linkmodes
  458. * @advertising: Currently advertised linkmodes
  459. * @adv_old: Saved advertised while power saving for WoL
  460. * @lp_advertising: Current link partner advertised linkmodes
  461. * @eee_broken_modes: Energy efficient ethernet modes which should be prohibited
  462. * @autoneg: Flag autoneg being used
  463. * @link: Current link state
  464. * @autoneg_complete: Flag auto negotiation of the link has completed
  465. * @mdix: Current crossover
  466. * @mdix_ctrl: User setting of crossover
  467. * @interrupts: Flag interrupts have been enabled
  468. * @interface: enum phy_interface_t value
  469. * @skb: Netlink message for cable diagnostics
  470. * @nest: Netlink nest used for cable diagnostics
  471. * @ehdr: nNtlink header for cable diagnostics
  472. * @phy_led_triggers: Array of LED triggers
  473. * @phy_num_led_triggers: Number of triggers in @phy_led_triggers
  474. * @led_link_trigger: LED trigger for link up/down
  475. * @last_triggered: last LED trigger for link speed
  476. * @master_slave_set: User requested master/slave configuration
  477. * @master_slave_get: Current master/slave advertisement
  478. * @master_slave_state: Current master/slave configuration
  479. * @mii_ts: Pointer to time stamper callbacks
  480. * @lock: Mutex for serialization access to PHY
  481. * @state_queue: Work queue for state machine
  482. * @shared: Pointer to private data shared by phys in one package
  483. * @priv: Pointer to driver private data
  484. *
  485. * interrupts currently only supports enabled or disabled,
  486. * but could be changed in the future to support enabling
  487. * and disabling specific interrupts
  488. *
  489. * Contains some infrastructure for polling and interrupt
  490. * handling, as well as handling shifts in PHY hardware state
  491. */
  492. struct phy_device {
  493. struct mdio_device mdio;
  494. /* Information about the PHY type */
  495. /* And management functions */
  496. struct phy_driver *drv;
  497. u32 phy_id;
  498. struct phy_c45_device_ids c45_ids;
  499. unsigned is_c45:1;
  500. unsigned is_internal:1;
  501. unsigned is_pseudo_fixed_link:1;
  502. unsigned is_gigabit_capable:1;
  503. unsigned has_fixups:1;
  504. unsigned suspended:1;
  505. unsigned suspended_by_mdio_bus:1;
  506. unsigned sysfs_links:1;
  507. unsigned loopback_enabled:1;
  508. unsigned downshifted_rate:1;
  509. unsigned autoneg:1;
  510. /* The most recently read link state */
  511. unsigned link:1;
  512. unsigned autoneg_complete:1;
  513. /* Interrupts are enabled */
  514. unsigned interrupts:1;
  515. enum phy_state state;
  516. u32 dev_flags;
  517. phy_interface_t interface;
  518. /*
  519. * forced speed & duplex (no autoneg)
  520. * partner speed & duplex & pause (autoneg)
  521. */
  522. int speed;
  523. int duplex;
  524. int port;
  525. int pause;
  526. int asym_pause;
  527. u8 master_slave_get;
  528. u8 master_slave_set;
  529. u8 master_slave_state;
  530. /* Union of PHY and Attached devices' supported link modes */
  531. /* See ethtool.h for more info */
  532. __ETHTOOL_DECLARE_LINK_MODE_MASK(supported);
  533. __ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
  534. __ETHTOOL_DECLARE_LINK_MODE_MASK(lp_advertising);
  535. /* used with phy_speed_down */
  536. __ETHTOOL_DECLARE_LINK_MODE_MASK(adv_old);
  537. /* Energy efficient ethernet modes which should be prohibited */
  538. u32 eee_broken_modes;
  539. #ifdef CONFIG_LED_TRIGGER_PHY
  540. struct phy_led_trigger *phy_led_triggers;
  541. unsigned int phy_num_led_triggers;
  542. struct phy_led_trigger *last_triggered;
  543. struct phy_led_trigger *led_link_trigger;
  544. #endif
  545. /*
  546. * Interrupt number for this PHY
  547. * -1 means no interrupt
  548. */
  549. int irq;
  550. /* private data pointer */
  551. /* For use by PHYs to maintain extra state */
  552. void *priv;
  553. /* shared data pointer */
  554. /* For use by PHYs inside the same package that need a shared state. */
  555. struct phy_package_shared *shared;
  556. /* Reporting cable test results */
  557. struct sk_buff *skb;
  558. void *ehdr;
  559. struct nlattr *nest;
  560. /* Interrupt and Polling infrastructure */
  561. struct delayed_work state_queue;
  562. struct mutex lock;
  563. /* This may be modified under the rtnl lock */
  564. bool sfp_bus_attached;
  565. struct sfp_bus *sfp_bus;
  566. struct phylink *phylink;
  567. struct net_device *attached_dev;
  568. struct mii_timestamper *mii_ts;
  569. u8 mdix;
  570. u8 mdix_ctrl;
  571. void (*phy_link_change)(struct phy_device *phydev, bool up);
  572. void (*adjust_link)(struct net_device *dev);
  573. #if IS_ENABLED(CONFIG_MACSEC)
  574. /* MACsec management functions */
  575. const struct macsec_ops *macsec_ops;
  576. #endif
  577. ANDROID_KABI_RESERVE(1);
  578. ANDROID_KABI_RESERVE(2);
  579. ANDROID_KABI_RESERVE(3);
  580. ANDROID_KABI_RESERVE(4);
  581. };
  582. #define to_phy_device(d) container_of(to_mdio_device(d), \
  583. struct phy_device, mdio)
  584. /**
  585. * struct phy_tdr_config - Configuration of a TDR raw test
  586. *
  587. * @first: Distance for first data collection point
  588. * @last: Distance for last data collection point
  589. * @step: Step between data collection points
  590. * @pair: Bitmap of cable pairs to collect data for
  591. *
  592. * A structure containing possible configuration parameters
  593. * for a TDR cable test. The driver does not need to implement
  594. * all the parameters, but should report what is actually used.
  595. * All distances are in centimeters.
  596. */
  597. struct phy_tdr_config {
  598. u32 first;
  599. u32 last;
  600. u32 step;
  601. s8 pair;
  602. };
  603. #define PHY_PAIR_ALL -1
  604. /**
  605. * struct phy_driver - Driver structure for a particular PHY type
  606. *
  607. * @mdiodrv: Data common to all MDIO devices
  608. * @phy_id: The result of reading the UID registers of this PHY
  609. * type, and ANDing them with the phy_id_mask. This driver
  610. * only works for PHYs with IDs which match this field
  611. * @name: The friendly name of this PHY type
  612. * @phy_id_mask: Defines the important bits of the phy_id
  613. * @features: A mandatory list of features (speed, duplex, etc)
  614. * supported by this PHY
  615. * @flags: A bitfield defining certain other features this PHY
  616. * supports (like interrupts)
  617. * @driver_data: Static driver data
  618. *
  619. * All functions are optional. If config_aneg or read_status
  620. * are not implemented, the phy core uses the genphy versions.
  621. * Note that none of these functions should be called from
  622. * interrupt time. The goal is for the bus read/write functions
  623. * to be able to block when the bus transaction is happening,
  624. * and be freed up by an interrupt (The MPC85xx has this ability,
  625. * though it is not currently supported in the driver).
  626. */
  627. struct phy_driver {
  628. struct mdio_driver_common mdiodrv;
  629. u32 phy_id;
  630. char *name;
  631. u32 phy_id_mask;
  632. const unsigned long * const features;
  633. u32 flags;
  634. const void *driver_data;
  635. /**
  636. * @soft_reset: Called to issue a PHY software reset
  637. */
  638. int (*soft_reset)(struct phy_device *phydev);
  639. /**
  640. * @config_init: Called to initialize the PHY,
  641. * including after a reset
  642. */
  643. int (*config_init)(struct phy_device *phydev);
  644. /**
  645. * @probe: Called during discovery. Used to set
  646. * up device-specific structures, if any
  647. */
  648. int (*probe)(struct phy_device *phydev);
  649. /**
  650. * @get_features: Probe the hardware to determine what
  651. * abilities it has. Should only set phydev->supported.
  652. */
  653. int (*get_features)(struct phy_device *phydev);
  654. /* PHY Power Management */
  655. /** @suspend: Suspend the hardware, saving state if needed */
  656. int (*suspend)(struct phy_device *phydev);
  657. /** @resume: Resume the hardware, restoring state if needed */
  658. int (*resume)(struct phy_device *phydev);
  659. /**
  660. * @config_aneg: Configures the advertisement and resets
  661. * autonegotiation if phydev->autoneg is on,
  662. * forces the speed to the current settings in phydev
  663. * if phydev->autoneg is off
  664. */
  665. int (*config_aneg)(struct phy_device *phydev);
  666. /** @aneg_done: Determines the auto negotiation result */
  667. int (*aneg_done)(struct phy_device *phydev);
  668. /** @read_status: Determines the negotiated speed and duplex */
  669. int (*read_status)(struct phy_device *phydev);
  670. /** @ack_interrupt: Clears any pending interrupts */
  671. int (*ack_interrupt)(struct phy_device *phydev);
  672. /** @config_intr: Enables or disables interrupts */
  673. int (*config_intr)(struct phy_device *phydev);
  674. /**
  675. * @did_interrupt: Checks if the PHY generated an interrupt.
  676. * For multi-PHY devices with shared PHY interrupt pin
  677. * Set interrupt bits have to be cleared.
  678. */
  679. int (*did_interrupt)(struct phy_device *phydev);
  680. /** @handle_interrupt: Override default interrupt handling */
  681. irqreturn_t (*handle_interrupt)(struct phy_device *phydev);
  682. /** @remove: Clears up any memory if needed */
  683. void (*remove)(struct phy_device *phydev);
  684. /**
  685. * @match_phy_device: Returns true if this is a suitable
  686. * driver for the given phydev. If NULL, matching is based on
  687. * phy_id and phy_id_mask.
  688. */
  689. int (*match_phy_device)(struct phy_device *phydev);
  690. /**
  691. * @set_wol: Some devices (e.g. qnap TS-119P II) require PHY
  692. * register changes to enable Wake on LAN, so set_wol is
  693. * provided to be called in the ethernet driver's set_wol
  694. * function.
  695. */
  696. int (*set_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
  697. /**
  698. * @get_wol: See set_wol, but for checking whether Wake on LAN
  699. * is enabled.
  700. */
  701. void (*get_wol)(struct phy_device *dev, struct ethtool_wolinfo *wol);
  702. /**
  703. * @link_change_notify: Called to inform a PHY device driver
  704. * when the core is about to change the link state. This
  705. * callback is supposed to be used as fixup hook for drivers
  706. * that need to take action when the link state
  707. * changes. Drivers are by no means allowed to mess with the
  708. * PHY device structure in their implementations.
  709. */
  710. void (*link_change_notify)(struct phy_device *dev);
  711. /**
  712. * @read_mmd: PHY specific driver override for reading a MMD
  713. * register. This function is optional for PHY specific
  714. * drivers. When not provided, the default MMD read function
  715. * will be used by phy_read_mmd(), which will use either a
  716. * direct read for Clause 45 PHYs or an indirect read for
  717. * Clause 22 PHYs. devnum is the MMD device number within the
  718. * PHY device, regnum is the register within the selected MMD
  719. * device.
  720. */
  721. int (*read_mmd)(struct phy_device *dev, int devnum, u16 regnum);
  722. /**
  723. * @write_mmd: PHY specific driver override for writing a MMD
  724. * register. This function is optional for PHY specific
  725. * drivers. When not provided, the default MMD write function
  726. * will be used by phy_write_mmd(), which will use either a
  727. * direct write for Clause 45 PHYs, or an indirect write for
  728. * Clause 22 PHYs. devnum is the MMD device number within the
  729. * PHY device, regnum is the register within the selected MMD
  730. * device. val is the value to be written.
  731. */
  732. int (*write_mmd)(struct phy_device *dev, int devnum, u16 regnum,
  733. u16 val);
  734. /** @read_page: Return the current PHY register page number */
  735. int (*read_page)(struct phy_device *dev);
  736. /** @write_page: Set the current PHY register page number */
  737. int (*write_page)(struct phy_device *dev, int page);
  738. /**
  739. * @module_info: Get the size and type of the eeprom contained
  740. * within a plug-in module
  741. */
  742. int (*module_info)(struct phy_device *dev,
  743. struct ethtool_modinfo *modinfo);
  744. /**
  745. * @module_eeprom: Get the eeprom information from the plug-in
  746. * module
  747. */
  748. int (*module_eeprom)(struct phy_device *dev,
  749. struct ethtool_eeprom *ee, u8 *data);
  750. /** @cable_test_start: Start a cable test */
  751. int (*cable_test_start)(struct phy_device *dev);
  752. /** @cable_test_tdr_start: Start a raw TDR cable test */
  753. int (*cable_test_tdr_start)(struct phy_device *dev,
  754. const struct phy_tdr_config *config);
  755. /**
  756. * @cable_test_get_status: Once per second, or on interrupt,
  757. * request the status of the test.
  758. */
  759. int (*cable_test_get_status)(struct phy_device *dev, bool *finished);
  760. /* Get statistics from the PHY using ethtool */
  761. /** @get_sset_count: Number of statistic counters */
  762. int (*get_sset_count)(struct phy_device *dev);
  763. /** @get_strings: Names of the statistic counters */
  764. void (*get_strings)(struct phy_device *dev, u8 *data);
  765. /** @get_stats: Return the statistic counter values */
  766. void (*get_stats)(struct phy_device *dev,
  767. struct ethtool_stats *stats, u64 *data);
  768. /* Get and Set PHY tunables */
  769. /** @get_tunable: Return the value of a tunable */
  770. int (*get_tunable)(struct phy_device *dev,
  771. struct ethtool_tunable *tuna, void *data);
  772. /** @set_tunable: Set the value of a tunable */
  773. int (*set_tunable)(struct phy_device *dev,
  774. struct ethtool_tunable *tuna,
  775. const void *data);
  776. /** @set_loopback: Set the loopback mood of the PHY */
  777. int (*set_loopback)(struct phy_device *dev, bool enable);
  778. /** @get_sqi: Get the signal quality indication */
  779. int (*get_sqi)(struct phy_device *dev);
  780. /** @get_sqi_max: Get the maximum signal quality indication */
  781. int (*get_sqi_max)(struct phy_device *dev);
  782. ANDROID_KABI_RESERVE(1);
  783. ANDROID_KABI_RESERVE(2);
  784. };
  785. #define to_phy_driver(d) container_of(to_mdio_common_driver(d), \
  786. struct phy_driver, mdiodrv)
  787. #define PHY_ANY_ID "MATCH ANY PHY"
  788. #define PHY_ANY_UID 0xffffffff
  789. #define PHY_ID_MATCH_EXACT(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 0)
  790. #define PHY_ID_MATCH_MODEL(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 4)
  791. #define PHY_ID_MATCH_VENDOR(id) .phy_id = (id), .phy_id_mask = GENMASK(31, 10)
  792. /* A Structure for boards to register fixups with the PHY Lib */
  793. struct phy_fixup {
  794. struct list_head list;
  795. char bus_id[MII_BUS_ID_SIZE + 3];
  796. u32 phy_uid;
  797. u32 phy_uid_mask;
  798. int (*run)(struct phy_device *phydev);
  799. };
  800. const char *phy_speed_to_str(int speed);
  801. const char *phy_duplex_to_str(unsigned int duplex);
  802. /* A structure for mapping a particular speed and duplex
  803. * combination to a particular SUPPORTED and ADVERTISED value
  804. */
  805. struct phy_setting {
  806. u32 speed;
  807. u8 duplex;
  808. u8 bit;
  809. };
  810. const struct phy_setting *
  811. phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
  812. bool exact);
  813. size_t phy_speeds(unsigned int *speeds, size_t size,
  814. unsigned long *mask);
  815. void of_set_phy_supported(struct phy_device *phydev);
  816. void of_set_phy_eee_broken(struct phy_device *phydev);
  817. int phy_speed_down_core(struct phy_device *phydev);
  818. /**
  819. * phy_is_started - Convenience function to check whether PHY is started
  820. * @phydev: The phy_device struct
  821. */
  822. static inline bool phy_is_started(struct phy_device *phydev)
  823. {
  824. return phydev->state >= PHY_UP;
  825. }
  826. void phy_resolve_aneg_pause(struct phy_device *phydev);
  827. void phy_resolve_aneg_linkmode(struct phy_device *phydev);
  828. void phy_check_downshift(struct phy_device *phydev);
  829. /**
  830. * phy_read - Convenience function for reading a given PHY register
  831. * @phydev: the phy_device struct
  832. * @regnum: register number to read
  833. *
  834. * NOTE: MUST NOT be called from interrupt context,
  835. * because the bus read/write functions may wait for an interrupt
  836. * to conclude the operation.
  837. */
  838. static inline int phy_read(struct phy_device *phydev, u32 regnum)
  839. {
  840. return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
  841. }
  842. #define phy_read_poll_timeout(phydev, regnum, val, cond, sleep_us, \
  843. timeout_us, sleep_before_read) \
  844. ({ \
  845. int __ret = read_poll_timeout(phy_read, val, (cond) || val < 0, \
  846. sleep_us, timeout_us, sleep_before_read, phydev, regnum); \
  847. if (val < 0) \
  848. __ret = val; \
  849. if (__ret) \
  850. phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
  851. __ret; \
  852. })
  853. /**
  854. * __phy_read - convenience function for reading a given PHY register
  855. * @phydev: the phy_device struct
  856. * @regnum: register number to read
  857. *
  858. * The caller must have taken the MDIO bus lock.
  859. */
  860. static inline int __phy_read(struct phy_device *phydev, u32 regnum)
  861. {
  862. return __mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
  863. }
  864. /**
  865. * phy_write - Convenience function for writing a given PHY register
  866. * @phydev: the phy_device struct
  867. * @regnum: register number to write
  868. * @val: value to write to @regnum
  869. *
  870. * NOTE: MUST NOT be called from interrupt context,
  871. * because the bus read/write functions may wait for an interrupt
  872. * to conclude the operation.
  873. */
  874. static inline int phy_write(struct phy_device *phydev, u32 regnum, u16 val)
  875. {
  876. return mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum, val);
  877. }
  878. /**
  879. * __phy_write - Convenience function for writing a given PHY register
  880. * @phydev: the phy_device struct
  881. * @regnum: register number to write
  882. * @val: value to write to @regnum
  883. *
  884. * The caller must have taken the MDIO bus lock.
  885. */
  886. static inline int __phy_write(struct phy_device *phydev, u32 regnum, u16 val)
  887. {
  888. return __mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum,
  889. val);
  890. }
  891. /**
  892. * __phy_modify_changed() - Convenience function for modifying a PHY register
  893. * @phydev: a pointer to a &struct phy_device
  894. * @regnum: register number
  895. * @mask: bit mask of bits to clear
  896. * @set: bit mask of bits to set
  897. *
  898. * Unlocked helper function which allows a PHY register to be modified as
  899. * new register value = (old register value & ~mask) | set
  900. *
  901. * Returns negative errno, 0 if there was no change, and 1 in case of change
  902. */
  903. static inline int __phy_modify_changed(struct phy_device *phydev, u32 regnum,
  904. u16 mask, u16 set)
  905. {
  906. return __mdiobus_modify_changed(phydev->mdio.bus, phydev->mdio.addr,
  907. regnum, mask, set);
  908. }
  909. /*
  910. * phy_read_mmd - Convenience function for reading a register
  911. * from an MMD on a given PHY.
  912. */
  913. int phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
  914. /**
  915. * phy_read_mmd_poll_timeout - Periodically poll a PHY register until a
  916. * condition is met or a timeout occurs
  917. *
  918. * @phydev: The phy_device struct
  919. * @devaddr: The MMD to read from
  920. * @regnum: The register on the MMD to read
  921. * @val: Variable to read the register into
  922. * @cond: Break condition (usually involving @val)
  923. * @sleep_us: Maximum time to sleep between reads in us (0
  924. * tight-loops). Should be less than ~20ms since usleep_range
  925. * is used (see Documentation/timers/timers-howto.rst).
  926. * @timeout_us: Timeout in us, 0 means never timeout
  927. * @sleep_before_read: if it is true, sleep @sleep_us before read.
  928. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  929. * case, the last read value at @args is stored in @val. Must not
  930. * be called from atomic context if sleep_us or timeout_us are used.
  931. */
  932. #define phy_read_mmd_poll_timeout(phydev, devaddr, regnum, val, cond, \
  933. sleep_us, timeout_us, sleep_before_read) \
  934. ({ \
  935. int __ret = read_poll_timeout(phy_read_mmd, val, (cond) || val < 0, \
  936. sleep_us, timeout_us, sleep_before_read, \
  937. phydev, devaddr, regnum); \
  938. if (val < 0) \
  939. __ret = val; \
  940. if (__ret) \
  941. phydev_err(phydev, "%s failed: %d\n", __func__, __ret); \
  942. __ret; \
  943. })
  944. /*
  945. * __phy_read_mmd - Convenience function for reading a register
  946. * from an MMD on a given PHY.
  947. */
  948. int __phy_read_mmd(struct phy_device *phydev, int devad, u32 regnum);
  949. /*
  950. * phy_write_mmd - Convenience function for writing a register
  951. * on an MMD on a given PHY.
  952. */
  953. int phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
  954. /*
  955. * __phy_write_mmd - Convenience function for writing a register
  956. * on an MMD on a given PHY.
  957. */
  958. int __phy_write_mmd(struct phy_device *phydev, int devad, u32 regnum, u16 val);
  959. int __phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
  960. u16 set);
  961. int phy_modify_changed(struct phy_device *phydev, u32 regnum, u16 mask,
  962. u16 set);
  963. int __phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
  964. int phy_modify(struct phy_device *phydev, u32 regnum, u16 mask, u16 set);
  965. int __phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
  966. u16 mask, u16 set);
  967. int phy_modify_mmd_changed(struct phy_device *phydev, int devad, u32 regnum,
  968. u16 mask, u16 set);
  969. int __phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
  970. u16 mask, u16 set);
  971. int phy_modify_mmd(struct phy_device *phydev, int devad, u32 regnum,
  972. u16 mask, u16 set);
  973. /**
  974. * __phy_set_bits - Convenience function for setting bits in a PHY register
  975. * @phydev: the phy_device struct
  976. * @regnum: register number to write
  977. * @val: bits to set
  978. *
  979. * The caller must have taken the MDIO bus lock.
  980. */
  981. static inline int __phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
  982. {
  983. return __phy_modify(phydev, regnum, 0, val);
  984. }
  985. /**
  986. * __phy_clear_bits - Convenience function for clearing bits in a PHY register
  987. * @phydev: the phy_device struct
  988. * @regnum: register number to write
  989. * @val: bits to clear
  990. *
  991. * The caller must have taken the MDIO bus lock.
  992. */
  993. static inline int __phy_clear_bits(struct phy_device *phydev, u32 regnum,
  994. u16 val)
  995. {
  996. return __phy_modify(phydev, regnum, val, 0);
  997. }
  998. /**
  999. * phy_set_bits - Convenience function for setting bits in a PHY register
  1000. * @phydev: the phy_device struct
  1001. * @regnum: register number to write
  1002. * @val: bits to set
  1003. */
  1004. static inline int phy_set_bits(struct phy_device *phydev, u32 regnum, u16 val)
  1005. {
  1006. return phy_modify(phydev, regnum, 0, val);
  1007. }
  1008. /**
  1009. * phy_clear_bits - Convenience function for clearing bits in a PHY register
  1010. * @phydev: the phy_device struct
  1011. * @regnum: register number to write
  1012. * @val: bits to clear
  1013. */
  1014. static inline int phy_clear_bits(struct phy_device *phydev, u32 regnum, u16 val)
  1015. {
  1016. return phy_modify(phydev, regnum, val, 0);
  1017. }
  1018. /**
  1019. * __phy_set_bits_mmd - Convenience function for setting bits in a register
  1020. * on MMD
  1021. * @phydev: the phy_device struct
  1022. * @devad: the MMD containing register to modify
  1023. * @regnum: register number to modify
  1024. * @val: bits to set
  1025. *
  1026. * The caller must have taken the MDIO bus lock.
  1027. */
  1028. static inline int __phy_set_bits_mmd(struct phy_device *phydev, int devad,
  1029. u32 regnum, u16 val)
  1030. {
  1031. return __phy_modify_mmd(phydev, devad, regnum, 0, val);
  1032. }
  1033. /**
  1034. * __phy_clear_bits_mmd - Convenience function for clearing bits in a register
  1035. * on MMD
  1036. * @phydev: the phy_device struct
  1037. * @devad: the MMD containing register to modify
  1038. * @regnum: register number to modify
  1039. * @val: bits to clear
  1040. *
  1041. * The caller must have taken the MDIO bus lock.
  1042. */
  1043. static inline int __phy_clear_bits_mmd(struct phy_device *phydev, int devad,
  1044. u32 regnum, u16 val)
  1045. {
  1046. return __phy_modify_mmd(phydev, devad, regnum, val, 0);
  1047. }
  1048. /**
  1049. * phy_set_bits_mmd - Convenience function for setting bits in a register
  1050. * on MMD
  1051. * @phydev: the phy_device struct
  1052. * @devad: the MMD containing register to modify
  1053. * @regnum: register number to modify
  1054. * @val: bits to set
  1055. */
  1056. static inline int phy_set_bits_mmd(struct phy_device *phydev, int devad,
  1057. u32 regnum, u16 val)
  1058. {
  1059. return phy_modify_mmd(phydev, devad, regnum, 0, val);
  1060. }
  1061. /**
  1062. * phy_clear_bits_mmd - Convenience function for clearing bits in a register
  1063. * on MMD
  1064. * @phydev: the phy_device struct
  1065. * @devad: the MMD containing register to modify
  1066. * @regnum: register number to modify
  1067. * @val: bits to clear
  1068. */
  1069. static inline int phy_clear_bits_mmd(struct phy_device *phydev, int devad,
  1070. u32 regnum, u16 val)
  1071. {
  1072. return phy_modify_mmd(phydev, devad, regnum, val, 0);
  1073. }
  1074. /**
  1075. * phy_interrupt_is_valid - Convenience function for testing a given PHY irq
  1076. * @phydev: the phy_device struct
  1077. *
  1078. * NOTE: must be kept in sync with addition/removal of PHY_POLL and
  1079. * PHY_IGNORE_INTERRUPT
  1080. */
  1081. static inline bool phy_interrupt_is_valid(struct phy_device *phydev)
  1082. {
  1083. return phydev->irq != PHY_POLL && phydev->irq != PHY_IGNORE_INTERRUPT;
  1084. }
  1085. /**
  1086. * phy_polling_mode - Convenience function for testing whether polling is
  1087. * used to detect PHY status changes
  1088. * @phydev: the phy_device struct
  1089. */
  1090. static inline bool phy_polling_mode(struct phy_device *phydev)
  1091. {
  1092. if (phydev->state == PHY_CABLETEST)
  1093. if (phydev->drv->flags & PHY_POLL_CABLE_TEST)
  1094. return true;
  1095. return phydev->irq == PHY_POLL;
  1096. }
  1097. /**
  1098. * phy_has_hwtstamp - Tests whether a PHY time stamp configuration.
  1099. * @phydev: the phy_device struct
  1100. */
  1101. static inline bool phy_has_hwtstamp(struct phy_device *phydev)
  1102. {
  1103. return phydev && phydev->mii_ts && phydev->mii_ts->hwtstamp;
  1104. }
  1105. /**
  1106. * phy_has_rxtstamp - Tests whether a PHY supports receive time stamping.
  1107. * @phydev: the phy_device struct
  1108. */
  1109. static inline bool phy_has_rxtstamp(struct phy_device *phydev)
  1110. {
  1111. return phydev && phydev->mii_ts && phydev->mii_ts->rxtstamp;
  1112. }
  1113. /**
  1114. * phy_has_tsinfo - Tests whether a PHY reports time stamping and/or
  1115. * PTP hardware clock capabilities.
  1116. * @phydev: the phy_device struct
  1117. */
  1118. static inline bool phy_has_tsinfo(struct phy_device *phydev)
  1119. {
  1120. return phydev && phydev->mii_ts && phydev->mii_ts->ts_info;
  1121. }
  1122. /**
  1123. * phy_has_txtstamp - Tests whether a PHY supports transmit time stamping.
  1124. * @phydev: the phy_device struct
  1125. */
  1126. static inline bool phy_has_txtstamp(struct phy_device *phydev)
  1127. {
  1128. return phydev && phydev->mii_ts && phydev->mii_ts->txtstamp;
  1129. }
  1130. static inline int phy_hwtstamp(struct phy_device *phydev, struct ifreq *ifr)
  1131. {
  1132. return phydev->mii_ts->hwtstamp(phydev->mii_ts, ifr);
  1133. }
  1134. static inline bool phy_rxtstamp(struct phy_device *phydev, struct sk_buff *skb,
  1135. int type)
  1136. {
  1137. return phydev->mii_ts->rxtstamp(phydev->mii_ts, skb, type);
  1138. }
  1139. static inline int phy_ts_info(struct phy_device *phydev,
  1140. struct ethtool_ts_info *tsinfo)
  1141. {
  1142. return phydev->mii_ts->ts_info(phydev->mii_ts, tsinfo);
  1143. }
  1144. static inline void phy_txtstamp(struct phy_device *phydev, struct sk_buff *skb,
  1145. int type)
  1146. {
  1147. phydev->mii_ts->txtstamp(phydev->mii_ts, skb, type);
  1148. }
  1149. /**
  1150. * phy_is_internal - Convenience function for testing if a PHY is internal
  1151. * @phydev: the phy_device struct
  1152. */
  1153. static inline bool phy_is_internal(struct phy_device *phydev)
  1154. {
  1155. return phydev->is_internal;
  1156. }
  1157. /**
  1158. * phy_interface_mode_is_rgmii - Convenience function for testing if a
  1159. * PHY interface mode is RGMII (all variants)
  1160. * @mode: the &phy_interface_t enum
  1161. */
  1162. static inline bool phy_interface_mode_is_rgmii(phy_interface_t mode)
  1163. {
  1164. return mode >= PHY_INTERFACE_MODE_RGMII &&
  1165. mode <= PHY_INTERFACE_MODE_RGMII_TXID;
  1166. };
  1167. /**
  1168. * phy_interface_mode_is_8023z() - does the PHY interface mode use 802.3z
  1169. * negotiation
  1170. * @mode: one of &enum phy_interface_t
  1171. *
  1172. * Returns true if the PHY interface mode uses the 16-bit negotiation
  1173. * word as defined in 802.3z. (See 802.3-2015 37.2.1 Config_Reg encoding)
  1174. */
  1175. static inline bool phy_interface_mode_is_8023z(phy_interface_t mode)
  1176. {
  1177. return mode == PHY_INTERFACE_MODE_1000BASEX ||
  1178. mode == PHY_INTERFACE_MODE_2500BASEX;
  1179. }
  1180. /**
  1181. * phy_interface_is_rgmii - Convenience function for testing if a PHY interface
  1182. * is RGMII (all variants)
  1183. * @phydev: the phy_device struct
  1184. */
  1185. static inline bool phy_interface_is_rgmii(struct phy_device *phydev)
  1186. {
  1187. return phy_interface_mode_is_rgmii(phydev->interface);
  1188. };
  1189. /**
  1190. * phy_is_pseudo_fixed_link - Convenience function for testing if this
  1191. * PHY is the CPU port facing side of an Ethernet switch, or similar.
  1192. * @phydev: the phy_device struct
  1193. */
  1194. static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev)
  1195. {
  1196. return phydev->is_pseudo_fixed_link;
  1197. }
  1198. int phy_save_page(struct phy_device *phydev);
  1199. int phy_select_page(struct phy_device *phydev, int page);
  1200. int phy_restore_page(struct phy_device *phydev, int oldpage, int ret);
  1201. int phy_read_paged(struct phy_device *phydev, int page, u32 regnum);
  1202. int phy_write_paged(struct phy_device *phydev, int page, u32 regnum, u16 val);
  1203. int phy_modify_paged_changed(struct phy_device *phydev, int page, u32 regnum,
  1204. u16 mask, u16 set);
  1205. int phy_modify_paged(struct phy_device *phydev, int page, u32 regnum,
  1206. u16 mask, u16 set);
  1207. struct phy_device *phy_device_create(struct mii_bus *bus, int addr, u32 phy_id,
  1208. bool is_c45,
  1209. struct phy_c45_device_ids *c45_ids);
  1210. #if IS_ENABLED(CONFIG_PHYLIB)
  1211. struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45);
  1212. int phy_device_register(struct phy_device *phy);
  1213. void phy_device_free(struct phy_device *phydev);
  1214. #else
  1215. static inline
  1216. struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45)
  1217. {
  1218. return NULL;
  1219. }
  1220. static inline int phy_device_register(struct phy_device *phy)
  1221. {
  1222. return 0;
  1223. }
  1224. static inline void phy_device_free(struct phy_device *phydev) { }
  1225. #endif /* CONFIG_PHYLIB */
  1226. void phy_device_remove(struct phy_device *phydev);
  1227. int phy_init_hw(struct phy_device *phydev);
  1228. int phy_suspend(struct phy_device *phydev);
  1229. int phy_resume(struct phy_device *phydev);
  1230. int __phy_resume(struct phy_device *phydev);
  1231. int phy_loopback(struct phy_device *phydev, bool enable);
  1232. void phy_sfp_attach(void *upstream, struct sfp_bus *bus);
  1233. void phy_sfp_detach(void *upstream, struct sfp_bus *bus);
  1234. int phy_sfp_probe(struct phy_device *phydev,
  1235. const struct sfp_upstream_ops *ops);
  1236. struct phy_device *phy_attach(struct net_device *dev, const char *bus_id,
  1237. phy_interface_t interface);
  1238. struct phy_device *phy_find_first(struct mii_bus *bus);
  1239. int phy_attach_direct(struct net_device *dev, struct phy_device *phydev,
  1240. u32 flags, phy_interface_t interface);
  1241. int phy_connect_direct(struct net_device *dev, struct phy_device *phydev,
  1242. void (*handler)(struct net_device *),
  1243. phy_interface_t interface);
  1244. struct phy_device *phy_connect(struct net_device *dev, const char *bus_id,
  1245. void (*handler)(struct net_device *),
  1246. phy_interface_t interface);
  1247. void phy_disconnect(struct phy_device *phydev);
  1248. void phy_detach(struct phy_device *phydev);
  1249. void phy_start(struct phy_device *phydev);
  1250. void phy_stop(struct phy_device *phydev);
  1251. int phy_start_aneg(struct phy_device *phydev);
  1252. int phy_aneg_done(struct phy_device *phydev);
  1253. int phy_speed_down(struct phy_device *phydev, bool sync);
  1254. int phy_speed_up(struct phy_device *phydev);
  1255. int phy_restart_aneg(struct phy_device *phydev);
  1256. int phy_reset_after_clk_enable(struct phy_device *phydev);
  1257. #if IS_ENABLED(CONFIG_PHYLIB)
  1258. int phy_start_cable_test(struct phy_device *phydev,
  1259. struct netlink_ext_ack *extack);
  1260. int phy_start_cable_test_tdr(struct phy_device *phydev,
  1261. struct netlink_ext_ack *extack,
  1262. const struct phy_tdr_config *config);
  1263. #else
  1264. static inline
  1265. int phy_start_cable_test(struct phy_device *phydev,
  1266. struct netlink_ext_ack *extack)
  1267. {
  1268. NL_SET_ERR_MSG(extack, "Kernel not compiled with PHYLIB support");
  1269. return -EOPNOTSUPP;
  1270. }
  1271. static inline
  1272. int phy_start_cable_test_tdr(struct phy_device *phydev,
  1273. struct netlink_ext_ack *extack,
  1274. const struct phy_tdr_config *config)
  1275. {
  1276. NL_SET_ERR_MSG(extack, "Kernel not compiled with PHYLIB support");
  1277. return -EOPNOTSUPP;
  1278. }
  1279. #endif
  1280. int phy_cable_test_result(struct phy_device *phydev, u8 pair, u16 result);
  1281. int phy_cable_test_fault_length(struct phy_device *phydev, u8 pair,
  1282. u16 cm);
  1283. static inline void phy_device_reset(struct phy_device *phydev, int value)
  1284. {
  1285. mdio_device_reset(&phydev->mdio, value);
  1286. }
  1287. #define phydev_err(_phydev, format, args...) \
  1288. dev_err(&_phydev->mdio.dev, format, ##args)
  1289. #define phydev_info(_phydev, format, args...) \
  1290. dev_info(&_phydev->mdio.dev, format, ##args)
  1291. #define phydev_warn(_phydev, format, args...) \
  1292. dev_warn(&_phydev->mdio.dev, format, ##args)
  1293. #define phydev_dbg(_phydev, format, args...) \
  1294. dev_dbg(&_phydev->mdio.dev, format, ##args)
  1295. static inline const char *phydev_name(const struct phy_device *phydev)
  1296. {
  1297. return dev_name(&phydev->mdio.dev);
  1298. }
  1299. static inline void phy_lock_mdio_bus(struct phy_device *phydev)
  1300. {
  1301. mutex_lock(&phydev->mdio.bus->mdio_lock);
  1302. }
  1303. static inline void phy_unlock_mdio_bus(struct phy_device *phydev)
  1304. {
  1305. mutex_unlock(&phydev->mdio.bus->mdio_lock);
  1306. }
  1307. void phy_attached_print(struct phy_device *phydev, const char *fmt, ...)
  1308. __printf(2, 3);
  1309. char *phy_attached_info_irq(struct phy_device *phydev)
  1310. __malloc;
  1311. void phy_attached_info(struct phy_device *phydev);
  1312. /* Clause 22 PHY */
  1313. int genphy_read_abilities(struct phy_device *phydev);
  1314. int genphy_setup_forced(struct phy_device *phydev);
  1315. int genphy_restart_aneg(struct phy_device *phydev);
  1316. int genphy_check_and_restart_aneg(struct phy_device *phydev, bool restart);
  1317. int genphy_config_eee_advert(struct phy_device *phydev);
  1318. int __genphy_config_aneg(struct phy_device *phydev, bool changed);
  1319. int genphy_aneg_done(struct phy_device *phydev);
  1320. int genphy_update_link(struct phy_device *phydev);
  1321. int genphy_read_lpa(struct phy_device *phydev);
  1322. int genphy_read_status_fixed(struct phy_device *phydev);
  1323. int genphy_read_status(struct phy_device *phydev);
  1324. int genphy_suspend(struct phy_device *phydev);
  1325. int genphy_resume(struct phy_device *phydev);
  1326. int genphy_loopback(struct phy_device *phydev, bool enable);
  1327. int genphy_soft_reset(struct phy_device *phydev);
  1328. static inline int genphy_config_aneg(struct phy_device *phydev)
  1329. {
  1330. return __genphy_config_aneg(phydev, false);
  1331. }
  1332. static inline int genphy_no_ack_interrupt(struct phy_device *phydev)
  1333. {
  1334. return 0;
  1335. }
  1336. static inline int genphy_no_config_intr(struct phy_device *phydev)
  1337. {
  1338. return 0;
  1339. }
  1340. int genphy_read_mmd_unsupported(struct phy_device *phdev, int devad,
  1341. u16 regnum);
  1342. int genphy_write_mmd_unsupported(struct phy_device *phdev, int devnum,
  1343. u16 regnum, u16 val);
  1344. /* Clause 37 */
  1345. int genphy_c37_config_aneg(struct phy_device *phydev);
  1346. int genphy_c37_read_status(struct phy_device *phydev);
  1347. /* Clause 45 PHY */
  1348. int genphy_c45_restart_aneg(struct phy_device *phydev);
  1349. int genphy_c45_check_and_restart_aneg(struct phy_device *phydev, bool restart);
  1350. int genphy_c45_aneg_done(struct phy_device *phydev);
  1351. int genphy_c45_read_link(struct phy_device *phydev);
  1352. int genphy_c45_read_lpa(struct phy_device *phydev);
  1353. int genphy_c45_read_pma(struct phy_device *phydev);
  1354. int genphy_c45_pma_setup_forced(struct phy_device *phydev);
  1355. int genphy_c45_an_config_aneg(struct phy_device *phydev);
  1356. int genphy_c45_an_disable_aneg(struct phy_device *phydev);
  1357. int genphy_c45_read_mdix(struct phy_device *phydev);
  1358. int genphy_c45_pma_read_abilities(struct phy_device *phydev);
  1359. int genphy_c45_read_status(struct phy_device *phydev);
  1360. int genphy_c45_config_aneg(struct phy_device *phydev);
  1361. /* Generic C45 PHY driver */
  1362. extern struct phy_driver genphy_c45_driver;
  1363. /* The gen10g_* functions are the old Clause 45 stub */
  1364. int gen10g_config_aneg(struct phy_device *phydev);
  1365. static inline int phy_read_status(struct phy_device *phydev)
  1366. {
  1367. if (!phydev->drv)
  1368. return -EIO;
  1369. if (phydev->drv->read_status)
  1370. return phydev->drv->read_status(phydev);
  1371. else
  1372. return genphy_read_status(phydev);
  1373. }
  1374. void phy_driver_unregister(struct phy_driver *drv);
  1375. void phy_drivers_unregister(struct phy_driver *drv, int n);
  1376. int phy_driver_register(struct phy_driver *new_driver, struct module *owner);
  1377. int phy_drivers_register(struct phy_driver *new_driver, int n,
  1378. struct module *owner);
  1379. void phy_state_machine(struct work_struct *work);
  1380. void phy_queue_state_machine(struct phy_device *phydev, unsigned long jiffies);
  1381. void phy_mac_interrupt(struct phy_device *phydev);
  1382. void phy_start_machine(struct phy_device *phydev);
  1383. void phy_stop_machine(struct phy_device *phydev);
  1384. void phy_ethtool_ksettings_get(struct phy_device *phydev,
  1385. struct ethtool_link_ksettings *cmd);
  1386. int phy_ethtool_ksettings_set(struct phy_device *phydev,
  1387. const struct ethtool_link_ksettings *cmd);
  1388. int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd);
  1389. int phy_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  1390. int phy_do_ioctl_running(struct net_device *dev, struct ifreq *ifr, int cmd);
  1391. int phy_disable_interrupts(struct phy_device *phydev);
  1392. void phy_request_interrupt(struct phy_device *phydev);
  1393. void phy_free_interrupt(struct phy_device *phydev);
  1394. void phy_print_status(struct phy_device *phydev);
  1395. int phy_set_max_speed(struct phy_device *phydev, u32 max_speed);
  1396. void phy_remove_link_mode(struct phy_device *phydev, u32 link_mode);
  1397. void phy_advertise_supported(struct phy_device *phydev);
  1398. void phy_support_sym_pause(struct phy_device *phydev);
  1399. void phy_support_asym_pause(struct phy_device *phydev);
  1400. void phy_set_sym_pause(struct phy_device *phydev, bool rx, bool tx,
  1401. bool autoneg);
  1402. void phy_set_asym_pause(struct phy_device *phydev, bool rx, bool tx);
  1403. bool phy_validate_pause(struct phy_device *phydev,
  1404. struct ethtool_pauseparam *pp);
  1405. void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause);
  1406. s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev,
  1407. const int *delay_values, int size, bool is_rx);
  1408. void phy_resolve_pause(unsigned long *local_adv, unsigned long *partner_adv,
  1409. bool *tx_pause, bool *rx_pause);
  1410. int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask,
  1411. int (*run)(struct phy_device *));
  1412. int phy_register_fixup_for_id(const char *bus_id,
  1413. int (*run)(struct phy_device *));
  1414. int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask,
  1415. int (*run)(struct phy_device *));
  1416. int phy_unregister_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask);
  1417. int phy_unregister_fixup_for_id(const char *bus_id);
  1418. int phy_unregister_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask);
  1419. int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable);
  1420. int phy_get_eee_err(struct phy_device *phydev);
  1421. int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data);
  1422. int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data);
  1423. int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol);
  1424. void phy_ethtool_get_wol(struct phy_device *phydev,
  1425. struct ethtool_wolinfo *wol);
  1426. int phy_ethtool_get_link_ksettings(struct net_device *ndev,
  1427. struct ethtool_link_ksettings *cmd);
  1428. int phy_ethtool_set_link_ksettings(struct net_device *ndev,
  1429. const struct ethtool_link_ksettings *cmd);
  1430. int phy_ethtool_nway_reset(struct net_device *ndev);
  1431. int phy_package_join(struct phy_device *phydev, int addr, size_t priv_size);
  1432. void phy_package_leave(struct phy_device *phydev);
  1433. int devm_phy_package_join(struct device *dev, struct phy_device *phydev,
  1434. int addr, size_t priv_size);
  1435. #if IS_ENABLED(CONFIG_PHYLIB)
  1436. int __init mdio_bus_init(void);
  1437. void mdio_bus_exit(void);
  1438. #endif
  1439. int phy_ethtool_get_strings(struct phy_device *phydev, u8 *data);
  1440. int phy_ethtool_get_sset_count(struct phy_device *phydev);
  1441. int phy_ethtool_get_stats(struct phy_device *phydev,
  1442. struct ethtool_stats *stats, u64 *data);
  1443. static inline int phy_package_read(struct phy_device *phydev, u32 regnum)
  1444. {
  1445. struct phy_package_shared *shared = phydev->shared;
  1446. if (!shared)
  1447. return -EIO;
  1448. return mdiobus_read(phydev->mdio.bus, shared->addr, regnum);
  1449. }
  1450. static inline int __phy_package_read(struct phy_device *phydev, u32 regnum)
  1451. {
  1452. struct phy_package_shared *shared = phydev->shared;
  1453. if (!shared)
  1454. return -EIO;
  1455. return __mdiobus_read(phydev->mdio.bus, shared->addr, regnum);
  1456. }
  1457. static inline int phy_package_write(struct phy_device *phydev,
  1458. u32 regnum, u16 val)
  1459. {
  1460. struct phy_package_shared *shared = phydev->shared;
  1461. if (!shared)
  1462. return -EIO;
  1463. return mdiobus_write(phydev->mdio.bus, shared->addr, regnum, val);
  1464. }
  1465. static inline int __phy_package_write(struct phy_device *phydev,
  1466. u32 regnum, u16 val)
  1467. {
  1468. struct phy_package_shared *shared = phydev->shared;
  1469. if (!shared)
  1470. return -EIO;
  1471. return __mdiobus_write(phydev->mdio.bus, shared->addr, regnum, val);
  1472. }
  1473. static inline bool __phy_package_set_once(struct phy_device *phydev,
  1474. unsigned int b)
  1475. {
  1476. struct phy_package_shared *shared = phydev->shared;
  1477. if (!shared)
  1478. return false;
  1479. return !test_and_set_bit(b, &shared->flags);
  1480. }
  1481. static inline bool phy_package_init_once(struct phy_device *phydev)
  1482. {
  1483. return __phy_package_set_once(phydev, PHY_SHARED_F_INIT_DONE);
  1484. }
  1485. static inline bool phy_package_probe_once(struct phy_device *phydev)
  1486. {
  1487. return __phy_package_set_once(phydev, PHY_SHARED_F_PROBE_DONE);
  1488. }
  1489. extern struct bus_type mdio_bus_type;
  1490. struct mdio_board_info {
  1491. const char *bus_id;
  1492. char modalias[MDIO_NAME_SIZE];
  1493. int mdio_addr;
  1494. const void *platform_data;
  1495. };
  1496. #if IS_ENABLED(CONFIG_MDIO_DEVICE)
  1497. int mdiobus_register_board_info(const struct mdio_board_info *info,
  1498. unsigned int n);
  1499. #else
  1500. static inline int mdiobus_register_board_info(const struct mdio_board_info *i,
  1501. unsigned int n)
  1502. {
  1503. return 0;
  1504. }
  1505. #endif
  1506. /**
  1507. * phy_module_driver() - Helper macro for registering PHY drivers
  1508. * @__phy_drivers: array of PHY drivers to register
  1509. * @__count: Numbers of members in array
  1510. *
  1511. * Helper macro for PHY drivers which do not do anything special in module
  1512. * init/exit. Each module may only use this macro once, and calling it
  1513. * replaces module_init() and module_exit().
  1514. */
  1515. #define phy_module_driver(__phy_drivers, __count) \
  1516. static int __init phy_module_init(void) \
  1517. { \
  1518. return phy_drivers_register(__phy_drivers, __count, THIS_MODULE); \
  1519. } \
  1520. module_init(phy_module_init); \
  1521. static void __exit phy_module_exit(void) \
  1522. { \
  1523. phy_drivers_unregister(__phy_drivers, __count); \
  1524. } \
  1525. module_exit(phy_module_exit)
  1526. #define module_phy_driver(__phy_drivers) \
  1527. phy_module_driver(__phy_drivers, ARRAY_SIZE(__phy_drivers))
  1528. bool phy_driver_is_genphy(struct phy_device *phydev);
  1529. bool phy_driver_is_genphy_10g(struct phy_device *phydev);
  1530. #endif /* __PHY_H */