mii.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * linux/mii.h: definitions for MII-compatible transceivers
  3. * Originally drivers/net/sunhme.h.
  4. *
  5. * Copyright (C) 1996, 1999, 2001 David S. Miller (davem@redhat.com)
  6. */
  7. #ifndef __LINUX_MII_H__
  8. #define __LINUX_MII_H__
  9. #include <linux/types.h>
  10. #include <linux/ethtool.h>
  11. /* Generic MII registers. */
  12. #define MII_BMCR 0x00 /* Basic mode control register */
  13. #define MII_BMSR 0x01 /* Basic mode status register */
  14. #define MII_PHYSID1 0x02 /* PHYS ID 1 */
  15. #define MII_PHYSID2 0x03 /* PHYS ID 2 */
  16. #define MII_ADVERTISE 0x04 /* Advertisement control reg */
  17. #define MII_LPA 0x05 /* Link partner ability reg */
  18. #define MII_EXPANSION 0x06 /* Expansion register */
  19. #define MII_CTRL1000 0x09 /* 1000BASE-T control */
  20. #define MII_STAT1000 0x0a /* 1000BASE-T status */
  21. #define MII_MMD_CTRL 0x0d /* MMD Access Control Register */
  22. #define MII_MMD_DATA 0x0e /* MMD Access Data Register */
  23. #define MII_ESTATUS 0x0f /* Extended Status */
  24. #define MII_DCOUNTER 0x12 /* Disconnect counter */
  25. #define MII_FCSCOUNTER 0x13 /* False carrier counter */
  26. #define MII_NWAYTEST 0x14 /* N-way auto-neg test reg */
  27. #define MII_RERRCOUNTER 0x15 /* Receive error counter */
  28. #define MII_SREVISION 0x16 /* Silicon revision */
  29. #define MII_RESV1 0x17 /* Reserved... */
  30. #define MII_LBRERROR 0x18 /* Lpback, rx, bypass error */
  31. #define MII_PHYADDR 0x19 /* PHY address */
  32. #define MII_RESV2 0x1a /* Reserved... */
  33. #define MII_TPISTATUS 0x1b /* TPI status for 10mbps */
  34. #define MII_NCONFIG 0x1c /* Network interface config */
  35. /* Basic mode control register. */
  36. #define BMCR_RESV 0x003f /* Unused... */
  37. #define BMCR_SPEED1000 0x0040 /* MSB of Speed (1000) */
  38. #define BMCR_CTST 0x0080 /* Collision test */
  39. #define BMCR_FULLDPLX 0x0100 /* Full duplex */
  40. #define BMCR_ANRESTART 0x0200 /* Auto negotiation restart */
  41. #define BMCR_ISOLATE 0x0400 /* Isolate data paths from MII */
  42. #define BMCR_PDOWN 0x0800 /* Enable low power state */
  43. #define BMCR_ANENABLE 0x1000 /* Enable auto negotiation */
  44. #define BMCR_SPEED100 0x2000 /* Select 100Mbps */
  45. #define BMCR_LOOPBACK 0x4000 /* TXD loopback bits */
  46. #define BMCR_RESET 0x8000 /* Reset to default state */
  47. /* Basic mode status register. */
  48. #define BMSR_ERCAP 0x0001 /* Ext-reg capability */
  49. #define BMSR_JCD 0x0002 /* Jabber detected */
  50. #define BMSR_LSTATUS 0x0004 /* Link status */
  51. #define BMSR_ANEGCAPABLE 0x0008 /* Able to do auto-negotiation */
  52. #define BMSR_RFAULT 0x0010 /* Remote fault detected */
  53. #define BMSR_ANEGCOMPLETE 0x0020 /* Auto-negotiation complete */
  54. #define BMSR_RESV 0x00c0 /* Unused... */
  55. #define BMSR_ESTATEN 0x0100 /* Extended Status in R15 */
  56. #define BMSR_100HALF2 0x0200 /* Can do 100BASE-T2 HDX */
  57. #define BMSR_100FULL2 0x0400 /* Can do 100BASE-T2 FDX */
  58. #define BMSR_10HALF 0x0800 /* Can do 10mbps, half-duplex */
  59. #define BMSR_10FULL 0x1000 /* Can do 10mbps, full-duplex */
  60. #define BMSR_100HALF 0x2000 /* Can do 100mbps, half-duplex */
  61. #define BMSR_100FULL 0x4000 /* Can do 100mbps, full-duplex */
  62. #define BMSR_100BASE4 0x8000 /* Can do 100mbps, 4k packets */
  63. /* Advertisement control register. */
  64. #define ADVERTISE_SLCT 0x001f /* Selector bits */
  65. #define ADVERTISE_CSMA 0x0001 /* Only selector supported */
  66. #define ADVERTISE_10HALF 0x0020 /* Try for 10mbps half-duplex */
  67. #define ADVERTISE_1000XFULL 0x0020 /* Try for 1000BASE-X full-duplex */
  68. #define ADVERTISE_10FULL 0x0040 /* Try for 10mbps full-duplex */
  69. #define ADVERTISE_1000XHALF 0x0040 /* Try for 1000BASE-X half-duplex */
  70. #define ADVERTISE_100HALF 0x0080 /* Try for 100mbps half-duplex */
  71. #define ADVERTISE_1000XPAUSE 0x0080 /* Try for 1000BASE-X pause */
  72. #define ADVERTISE_100FULL 0x0100 /* Try for 100mbps full-duplex */
  73. #define ADVERTISE_1000XPSE_ASYM 0x0100 /* Try for 1000BASE-X asym pause */
  74. #define ADVERTISE_100BASE4 0x0200 /* Try for 100mbps 4k packets */
  75. #define ADVERTISE_PAUSE_CAP 0x0400 /* Try for pause */
  76. #define ADVERTISE_PAUSE_ASYM 0x0800 /* Try for asymetric pause */
  77. #define ADVERTISE_RESV 0x1000 /* Unused... */
  78. #define ADVERTISE_RFAULT 0x2000 /* Say we can detect faults */
  79. #define ADVERTISE_LPACK 0x4000 /* Ack link partners response */
  80. #define ADVERTISE_NPAGE 0x8000 /* Next page bit */
  81. #define ADVERTISE_FULL (ADVERTISE_100FULL | ADVERTISE_10FULL | \
  82. ADVERTISE_CSMA)
  83. #define ADVERTISE_ALL (ADVERTISE_10HALF | ADVERTISE_10FULL | \
  84. ADVERTISE_100HALF | ADVERTISE_100FULL)
  85. /* Link partner ability register. */
  86. #define LPA_SLCT 0x001f /* Same as advertise selector */
  87. #define LPA_10HALF 0x0020 /* Can do 10mbps half-duplex */
  88. #define LPA_1000XFULL 0x0020 /* Can do 1000BASE-X full-duplex */
  89. #define LPA_10FULL 0x0040 /* Can do 10mbps full-duplex */
  90. #define LPA_1000XHALF 0x0040 /* Can do 1000BASE-X half-duplex */
  91. #define LPA_100HALF 0x0080 /* Can do 100mbps half-duplex */
  92. #define LPA_1000XPAUSE 0x0080 /* Can do 1000BASE-X pause */
  93. #define LPA_100FULL 0x0100 /* Can do 100mbps full-duplex */
  94. #define LPA_1000XPAUSE_ASYM 0x0100 /* Can do 1000BASE-X pause asym*/
  95. #define LPA_100BASE4 0x0200 /* Can do 100mbps 4k packets */
  96. #define LPA_PAUSE_CAP 0x0400 /* Can pause */
  97. #define LPA_PAUSE_ASYM 0x0800 /* Can pause asymetrically */
  98. #define LPA_RESV 0x1000 /* Unused... */
  99. #define LPA_RFAULT 0x2000 /* Link partner faulted */
  100. #define LPA_LPACK 0x4000 /* Link partner acked us */
  101. #define LPA_NPAGE 0x8000 /* Next page bit */
  102. #define LPA_DUPLEX (LPA_10FULL | LPA_100FULL)
  103. #define LPA_100 (LPA_100FULL | LPA_100HALF | LPA_100BASE4)
  104. /* Expansion register for auto-negotiation. */
  105. #define EXPANSION_NWAY 0x0001 /* Can do N-way auto-nego */
  106. #define EXPANSION_LCWP 0x0002 /* Got new RX page code word */
  107. #define EXPANSION_ENABLENPAGE 0x0004 /* This enables npage words */
  108. #define EXPANSION_NPCAPABLE 0x0008 /* Link partner supports npage */
  109. #define EXPANSION_MFAULTS 0x0010 /* Multiple faults detected */
  110. #define EXPANSION_RESV 0xffe0 /* Unused... */
  111. #define ESTATUS_1000_TFULL 0x2000 /* Can do 1000BT Full */
  112. #define ESTATUS_1000_THALF 0x1000 /* Can do 1000BT Half */
  113. /* N-way test register. */
  114. #define NWAYTEST_RESV1 0x00ff /* Unused... */
  115. #define NWAYTEST_LOOPBACK 0x0100 /* Enable loopback for N-way */
  116. #define NWAYTEST_RESV2 0xfe00 /* Unused... */
  117. /* 1000BASE-T Control register */
  118. #define ADVERTISE_1000FULL 0x0200 /* Advertise 1000BASE-T full duplex */
  119. #define ADVERTISE_1000HALF 0x0100 /* Advertise 1000BASE-T half duplex */
  120. #define CTL1000_AS_MASTER 0x0800
  121. #define CTL1000_ENABLE_MASTER 0x1000
  122. /* 1000BASE-T Status register */
  123. #define LPA_1000LOCALRXOK 0x2000 /* Link partner local receiver status */
  124. #define LPA_1000REMRXOK 0x1000 /* Link partner remote receiver status */
  125. #define LPA_1000FULL 0x0800 /* Link partner 1000BASE-T full duplex */
  126. #define LPA_1000HALF 0x0400 /* Link partner 1000BASE-T half duplex */
  127. /* Flow control flags */
  128. #define FLOW_CTRL_TX 0x01
  129. #define FLOW_CTRL_RX 0x02
  130. /* MMD Access Control register fields */
  131. #define MII_MMD_CTRL_DEVAD_MASK 0x1f /* Mask MMD DEVAD*/
  132. #define MII_MMD_CTRL_ADDR 0x0000 /* Address */
  133. #define MII_MMD_CTRL_NOINCR 0x4000 /* no post increment */
  134. #define MII_MMD_CTRL_INCR_RDWT 0x8000 /* post increment on reads & writes */
  135. #define MII_MMD_CTRL_INCR_ON_WT 0xC000 /* post increment on writes only */
  136. /* This structure is used in all SIOCxMIIxxx ioctl calls */
  137. struct mii_ioctl_data {
  138. __u16 phy_id;
  139. __u16 reg_num;
  140. __u16 val_in;
  141. __u16 val_out;
  142. };
  143. #ifdef __KERNEL__
  144. #include <linux/if.h>
  145. struct ethtool_cmd;
  146. struct mii_if_info {
  147. int phy_id;
  148. int advertising;
  149. int phy_id_mask;
  150. int reg_num_mask;
  151. unsigned int full_duplex : 1; /* is full duplex? */
  152. unsigned int force_media : 1; /* is autoneg. disabled? */
  153. unsigned int supports_gmii : 1; /* are GMII registers supported? */
  154. struct net_device *dev;
  155. int (*mdio_read) (struct net_device *dev, int phy_id, int location);
  156. void (*mdio_write) (struct net_device *dev, int phy_id, int location, int val);
  157. };
  158. extern int mii_link_ok (struct mii_if_info *mii);
  159. extern int mii_nway_restart (struct mii_if_info *mii);
  160. extern int mii_ethtool_gset(struct mii_if_info *mii, struct ethtool_cmd *ecmd);
  161. extern int mii_ethtool_sset(struct mii_if_info *mii, struct ethtool_cmd *ecmd);
  162. extern int mii_check_gmii_support(struct mii_if_info *mii);
  163. extern void mii_check_link (struct mii_if_info *mii);
  164. extern unsigned int mii_check_media (struct mii_if_info *mii,
  165. unsigned int ok_to_print,
  166. unsigned int init_media);
  167. extern int generic_mii_ioctl(struct mii_if_info *mii_if,
  168. struct mii_ioctl_data *mii_data, int cmd,
  169. unsigned int *duplex_changed);
  170. static inline struct mii_ioctl_data *if_mii(struct ifreq *rq)
  171. {
  172. return (struct mii_ioctl_data *) &rq->ifr_ifru;
  173. }
  174. /**
  175. * mii_nway_result
  176. * @negotiated: value of MII ANAR and'd with ANLPAR
  177. *
  178. * Given a set of MII abilities, check each bit and returns the
  179. * currently supported media, in the priority order defined by
  180. * IEEE 802.3u. We use LPA_xxx constants but note this is not the
  181. * value of LPA solely, as described above.
  182. *
  183. * The one exception to IEEE 802.3u is that 100baseT4 is placed
  184. * between 100T-full and 100T-half. If your phy does not support
  185. * 100T4 this is fine. If your phy places 100T4 elsewhere in the
  186. * priority order, you will need to roll your own function.
  187. */
  188. static inline unsigned int mii_nway_result (unsigned int negotiated)
  189. {
  190. unsigned int ret;
  191. if (negotiated & LPA_100FULL)
  192. ret = LPA_100FULL;
  193. else if (negotiated & LPA_100BASE4)
  194. ret = LPA_100BASE4;
  195. else if (negotiated & LPA_100HALF)
  196. ret = LPA_100HALF;
  197. else if (negotiated & LPA_10FULL)
  198. ret = LPA_10FULL;
  199. else
  200. ret = LPA_10HALF;
  201. return ret;
  202. }
  203. /**
  204. * mii_duplex
  205. * @duplex_lock: Non-zero if duplex is locked at full
  206. * @negotiated: value of MII ANAR and'd with ANLPAR
  207. *
  208. * A small helper function for a common case. Returns one
  209. * if the media is operating or locked at full duplex, and
  210. * returns zero otherwise.
  211. */
  212. static inline unsigned int mii_duplex (unsigned int duplex_lock,
  213. unsigned int negotiated)
  214. {
  215. if (duplex_lock)
  216. return 1;
  217. if (mii_nway_result(negotiated) & LPA_DUPLEX)
  218. return 1;
  219. return 0;
  220. }
  221. /**
  222. * ethtool_adv_to_mii_adv_t
  223. * @ethadv: the ethtool advertisement settings
  224. *
  225. * A small helper function that translates ethtool advertisement
  226. * settings to phy autonegotiation advertisements for the
  227. * MII_ADVERTISE register.
  228. */
  229. static inline u32 ethtool_adv_to_mii_adv_t(u32 ethadv)
  230. {
  231. u32 result = 0;
  232. if (ethadv & ADVERTISED_10baseT_Half)
  233. result |= ADVERTISE_10HALF;
  234. if (ethadv & ADVERTISED_10baseT_Full)
  235. result |= ADVERTISE_10FULL;
  236. if (ethadv & ADVERTISED_100baseT_Half)
  237. result |= ADVERTISE_100HALF;
  238. if (ethadv & ADVERTISED_100baseT_Full)
  239. result |= ADVERTISE_100FULL;
  240. if (ethadv & ADVERTISED_Pause)
  241. result |= ADVERTISE_PAUSE_CAP;
  242. if (ethadv & ADVERTISED_Asym_Pause)
  243. result |= ADVERTISE_PAUSE_ASYM;
  244. return result;
  245. }
  246. /**
  247. * mii_adv_to_ethtool_adv_t
  248. * @adv: value of the MII_ADVERTISE register
  249. *
  250. * A small helper function that translates MII_ADVERTISE bits
  251. * to ethtool advertisement settings.
  252. */
  253. static inline u32 mii_adv_to_ethtool_adv_t(u32 adv)
  254. {
  255. u32 result = 0;
  256. if (adv & ADVERTISE_10HALF)
  257. result |= ADVERTISED_10baseT_Half;
  258. if (adv & ADVERTISE_10FULL)
  259. result |= ADVERTISED_10baseT_Full;
  260. if (adv & ADVERTISE_100HALF)
  261. result |= ADVERTISED_100baseT_Half;
  262. if (adv & ADVERTISE_100FULL)
  263. result |= ADVERTISED_100baseT_Full;
  264. if (adv & ADVERTISE_PAUSE_CAP)
  265. result |= ADVERTISED_Pause;
  266. if (adv & ADVERTISE_PAUSE_ASYM)
  267. result |= ADVERTISED_Asym_Pause;
  268. return result;
  269. }
  270. /**
  271. * ethtool_adv_to_mii_ctrl1000_t
  272. * @ethadv: the ethtool advertisement settings
  273. *
  274. * A small helper function that translates ethtool advertisement
  275. * settings to phy autonegotiation advertisements for the
  276. * MII_CTRL1000 register when in 1000T mode.
  277. */
  278. static inline u32 ethtool_adv_to_mii_ctrl1000_t(u32 ethadv)
  279. {
  280. u32 result = 0;
  281. if (ethadv & ADVERTISED_1000baseT_Half)
  282. result |= ADVERTISE_1000HALF;
  283. if (ethadv & ADVERTISED_1000baseT_Full)
  284. result |= ADVERTISE_1000FULL;
  285. return result;
  286. }
  287. /**
  288. * mii_ctrl1000_to_ethtool_adv_t
  289. * @adv: value of the MII_CTRL1000 register
  290. *
  291. * A small helper function that translates MII_CTRL1000
  292. * bits, when in 1000Base-T mode, to ethtool
  293. * advertisement settings.
  294. */
  295. static inline u32 mii_ctrl1000_to_ethtool_adv_t(u32 adv)
  296. {
  297. u32 result = 0;
  298. if (adv & ADVERTISE_1000HALF)
  299. result |= ADVERTISED_1000baseT_Half;
  300. if (adv & ADVERTISE_1000FULL)
  301. result |= ADVERTISED_1000baseT_Full;
  302. return result;
  303. }
  304. /**
  305. * mii_lpa_to_ethtool_lpa_t
  306. * @adv: value of the MII_LPA register
  307. *
  308. * A small helper function that translates MII_LPA
  309. * bits, when in 1000Base-T mode, to ethtool
  310. * LP advertisement settings.
  311. */
  312. static inline u32 mii_lpa_to_ethtool_lpa_t(u32 lpa)
  313. {
  314. u32 result = 0;
  315. if (lpa & LPA_LPACK)
  316. result |= ADVERTISED_Autoneg;
  317. return result | mii_adv_to_ethtool_adv_t(lpa);
  318. }
  319. /**
  320. * mii_stat1000_to_ethtool_lpa_t
  321. * @adv: value of the MII_STAT1000 register
  322. *
  323. * A small helper function that translates MII_STAT1000
  324. * bits, when in 1000Base-T mode, to ethtool
  325. * advertisement settings.
  326. */
  327. static inline u32 mii_stat1000_to_ethtool_lpa_t(u32 lpa)
  328. {
  329. u32 result = 0;
  330. if (lpa & LPA_1000HALF)
  331. result |= ADVERTISED_1000baseT_Half;
  332. if (lpa & LPA_1000FULL)
  333. result |= ADVERTISED_1000baseT_Full;
  334. return result;
  335. }
  336. /**
  337. * ethtool_adv_to_mii_adv_x
  338. * @ethadv: the ethtool advertisement settings
  339. *
  340. * A small helper function that translates ethtool advertisement
  341. * settings to phy autonegotiation advertisements for the
  342. * MII_CTRL1000 register when in 1000Base-X mode.
  343. */
  344. static inline u32 ethtool_adv_to_mii_adv_x(u32 ethadv)
  345. {
  346. u32 result = 0;
  347. if (ethadv & ADVERTISED_1000baseT_Half)
  348. result |= ADVERTISE_1000XHALF;
  349. if (ethadv & ADVERTISED_1000baseT_Full)
  350. result |= ADVERTISE_1000XFULL;
  351. if (ethadv & ADVERTISED_Pause)
  352. result |= ADVERTISE_1000XPAUSE;
  353. if (ethadv & ADVERTISED_Asym_Pause)
  354. result |= ADVERTISE_1000XPSE_ASYM;
  355. return result;
  356. }
  357. /**
  358. * mii_adv_to_ethtool_adv_x
  359. * @adv: value of the MII_CTRL1000 register
  360. *
  361. * A small helper function that translates MII_CTRL1000
  362. * bits, when in 1000Base-X mode, to ethtool
  363. * advertisement settings.
  364. */
  365. static inline u32 mii_adv_to_ethtool_adv_x(u32 adv)
  366. {
  367. u32 result = 0;
  368. if (adv & ADVERTISE_1000XHALF)
  369. result |= ADVERTISED_1000baseT_Half;
  370. if (adv & ADVERTISE_1000XFULL)
  371. result |= ADVERTISED_1000baseT_Full;
  372. if (adv & ADVERTISE_1000XPAUSE)
  373. result |= ADVERTISED_Pause;
  374. if (adv & ADVERTISE_1000XPSE_ASYM)
  375. result |= ADVERTISED_Asym_Pause;
  376. return result;
  377. }
  378. /**
  379. * mii_lpa_to_ethtool_lpa_x
  380. * @adv: value of the MII_LPA register
  381. *
  382. * A small helper function that translates MII_LPA
  383. * bits, when in 1000Base-X mode, to ethtool
  384. * LP advertisement settings.
  385. */
  386. static inline u32 mii_lpa_to_ethtool_lpa_x(u32 lpa)
  387. {
  388. u32 result = 0;
  389. if (lpa & LPA_LPACK)
  390. result |= ADVERTISED_Autoneg;
  391. return result | mii_adv_to_ethtool_adv_x(lpa);
  392. }
  393. /**
  394. * mii_advertise_flowctrl - get flow control advertisement flags
  395. * @cap: Flow control capabilities (FLOW_CTRL_RX, FLOW_CTRL_TX or both)
  396. */
  397. static inline u16 mii_advertise_flowctrl(int cap)
  398. {
  399. u16 adv = 0;
  400. if (cap & FLOW_CTRL_RX)
  401. adv = ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  402. if (cap & FLOW_CTRL_TX)
  403. adv ^= ADVERTISE_PAUSE_ASYM;
  404. return adv;
  405. }
  406. /**
  407. * mii_resolve_flowctrl_fdx
  408. * @lcladv: value of MII ADVERTISE register
  409. * @rmtadv: value of MII LPA register
  410. *
  411. * Resolve full duplex flow control as per IEEE 802.3-2005 table 28B-3
  412. */
  413. static inline u8 mii_resolve_flowctrl_fdx(u16 lcladv, u16 rmtadv)
  414. {
  415. u8 cap = 0;
  416. if (lcladv & rmtadv & ADVERTISE_PAUSE_CAP) {
  417. cap = FLOW_CTRL_TX | FLOW_CTRL_RX;
  418. } else if (lcladv & rmtadv & ADVERTISE_PAUSE_ASYM) {
  419. if (lcladv & ADVERTISE_PAUSE_CAP)
  420. cap = FLOW_CTRL_RX;
  421. else if (rmtadv & ADVERTISE_PAUSE_CAP)
  422. cap = FLOW_CTRL_TX;
  423. }
  424. return cap;
  425. }
  426. #endif /* __KERNEL__ */
  427. #endif /* __LINUX_MII_H__ */