mii.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. mii.c: MII interface library
  3. Maintained by Jeff Garzik <jgarzik@pobox.com>
  4. Copyright 2001,2002 Jeff Garzik
  5. Various code came from myson803.c and other files by
  6. Donald Becker. Copyright:
  7. Written 1998-2002 by Donald Becker.
  8. This software may be used and distributed according
  9. to the terms of the GNU General Public License (GPL),
  10. incorporated herein by reference. Drivers based on
  11. or derived from this code fall under the GPL and must
  12. retain the authorship, copyright and license notice.
  13. This file is not a complete program and may only be
  14. used when the entire operating system is licensed
  15. under the GPL.
  16. The author may be reached as becker@scyld.com, or C/O
  17. Scyld Computing Corporation
  18. 410 Severn Ave., Suite 210
  19. Annapolis MD 21403
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/netdevice.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/mii.h>
  26. /**
  27. * mii_ethtool_gset - get settings that are specified in @ecmd
  28. * @mii: MII interface
  29. * @ecmd: requested ethtool_cmd
  30. *
  31. * Returns 0 for success, negative on error.
  32. */
  33. int mii_ethtool_gset(struct mii_if_info *mii, struct ethtool_cmd *ecmd)
  34. {
  35. struct net_device *dev = mii->dev;
  36. u32 advert, bmcr, lpa, nego;
  37. u32 advert2 = 0, bmcr2 = 0, lpa2 = 0;
  38. ecmd->supported =
  39. (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
  40. SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
  41. SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
  42. if (mii->supports_gmii)
  43. ecmd->supported |= SUPPORTED_1000baseT_Half |
  44. SUPPORTED_1000baseT_Full;
  45. /* only supports twisted-pair */
  46. ecmd->port = PORT_MII;
  47. /* only supports internal transceiver */
  48. ecmd->transceiver = XCVR_INTERNAL;
  49. /* this isn't fully supported at higher layers */
  50. ecmd->phy_address = mii->phy_id;
  51. ecmd->advertising = ADVERTISED_TP | ADVERTISED_MII;
  52. advert = mii->mdio_read(dev, mii->phy_id, MII_ADVERTISE);
  53. if (mii->supports_gmii)
  54. advert2 = mii->mdio_read(dev, mii->phy_id, MII_CTRL1000);
  55. if (advert & ADVERTISE_10HALF)
  56. ecmd->advertising |= ADVERTISED_10baseT_Half;
  57. if (advert & ADVERTISE_10FULL)
  58. ecmd->advertising |= ADVERTISED_10baseT_Full;
  59. if (advert & ADVERTISE_100HALF)
  60. ecmd->advertising |= ADVERTISED_100baseT_Half;
  61. if (advert & ADVERTISE_100FULL)
  62. ecmd->advertising |= ADVERTISED_100baseT_Full;
  63. if (advert2 & ADVERTISE_1000HALF)
  64. ecmd->advertising |= ADVERTISED_1000baseT_Half;
  65. if (advert2 & ADVERTISE_1000FULL)
  66. ecmd->advertising |= ADVERTISED_1000baseT_Full;
  67. bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR);
  68. lpa = mii->mdio_read(dev, mii->phy_id, MII_LPA);
  69. if (mii->supports_gmii) {
  70. bmcr2 = mii->mdio_read(dev, mii->phy_id, MII_CTRL1000);
  71. lpa2 = mii->mdio_read(dev, mii->phy_id, MII_STAT1000);
  72. }
  73. if (bmcr & BMCR_ANENABLE) {
  74. ecmd->advertising |= ADVERTISED_Autoneg;
  75. ecmd->autoneg = AUTONEG_ENABLE;
  76. nego = mii_nway_result(advert & lpa);
  77. if ((bmcr2 & (ADVERTISE_1000HALF | ADVERTISE_1000FULL)) &
  78. (lpa2 >> 2))
  79. ecmd->speed = SPEED_1000;
  80. else if (nego == LPA_100FULL || nego == LPA_100HALF)
  81. ecmd->speed = SPEED_100;
  82. else
  83. ecmd->speed = SPEED_10;
  84. if ((lpa2 & LPA_1000FULL) || nego == LPA_100FULL ||
  85. nego == LPA_10FULL) {
  86. ecmd->duplex = DUPLEX_FULL;
  87. mii->full_duplex = 1;
  88. } else {
  89. ecmd->duplex = DUPLEX_HALF;
  90. mii->full_duplex = 0;
  91. }
  92. } else {
  93. ecmd->autoneg = AUTONEG_DISABLE;
  94. ecmd->speed = ((bmcr & BMCR_SPEED1000 &&
  95. (bmcr & BMCR_SPEED100) == 0) ? SPEED_1000 :
  96. (bmcr & BMCR_SPEED100) ? SPEED_100 : SPEED_10);
  97. ecmd->duplex = (bmcr & BMCR_FULLDPLX) ? DUPLEX_FULL : DUPLEX_HALF;
  98. }
  99. /* ignore maxtxpkt, maxrxpkt for now */
  100. return 0;
  101. }
  102. /**
  103. * mii_ethtool_sset - set settings that are specified in @ecmd
  104. * @mii: MII interface
  105. * @ecmd: requested ethtool_cmd
  106. *
  107. * Returns 0 for success, negative on error.
  108. */
  109. int mii_ethtool_sset(struct mii_if_info *mii, struct ethtool_cmd *ecmd)
  110. {
  111. struct net_device *dev = mii->dev;
  112. if (ecmd->speed != SPEED_10 &&
  113. ecmd->speed != SPEED_100 &&
  114. ecmd->speed != SPEED_1000)
  115. return -EINVAL;
  116. if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
  117. return -EINVAL;
  118. if (ecmd->port != PORT_MII)
  119. return -EINVAL;
  120. if (ecmd->transceiver != XCVR_INTERNAL)
  121. return -EINVAL;
  122. if (ecmd->phy_address != mii->phy_id)
  123. return -EINVAL;
  124. if (ecmd->autoneg != AUTONEG_DISABLE && ecmd->autoneg != AUTONEG_ENABLE)
  125. return -EINVAL;
  126. if ((ecmd->speed == SPEED_1000) && (!mii->supports_gmii))
  127. return -EINVAL;
  128. /* ignore supported, maxtxpkt, maxrxpkt */
  129. if (ecmd->autoneg == AUTONEG_ENABLE) {
  130. u32 bmcr, advert, tmp;
  131. u32 advert2 = 0, tmp2 = 0;
  132. if ((ecmd->advertising & (ADVERTISED_10baseT_Half |
  133. ADVERTISED_10baseT_Full |
  134. ADVERTISED_100baseT_Half |
  135. ADVERTISED_100baseT_Full |
  136. ADVERTISED_1000baseT_Half |
  137. ADVERTISED_1000baseT_Full)) == 0)
  138. return -EINVAL;
  139. /* advertise only what has been requested */
  140. advert = mii->mdio_read(dev, mii->phy_id, MII_ADVERTISE);
  141. tmp = advert & ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
  142. if (mii->supports_gmii) {
  143. advert2 = mii->mdio_read(dev, mii->phy_id, MII_CTRL1000);
  144. tmp2 = advert2 & ~(ADVERTISE_1000HALF | ADVERTISE_1000FULL);
  145. }
  146. if (ecmd->advertising & ADVERTISED_10baseT_Half)
  147. tmp |= ADVERTISE_10HALF;
  148. if (ecmd->advertising & ADVERTISED_10baseT_Full)
  149. tmp |= ADVERTISE_10FULL;
  150. if (ecmd->advertising & ADVERTISED_100baseT_Half)
  151. tmp |= ADVERTISE_100HALF;
  152. if (ecmd->advertising & ADVERTISED_100baseT_Full)
  153. tmp |= ADVERTISE_100FULL;
  154. if (mii->supports_gmii) {
  155. if (ecmd->advertising & ADVERTISED_1000baseT_Half)
  156. tmp2 |= ADVERTISE_1000HALF;
  157. if (ecmd->advertising & ADVERTISED_1000baseT_Full)
  158. tmp2 |= ADVERTISE_1000FULL;
  159. }
  160. if (advert != tmp) {
  161. mii->mdio_write(dev, mii->phy_id, MII_ADVERTISE, tmp);
  162. mii->advertising = tmp;
  163. }
  164. if ((mii->supports_gmii) && (advert2 != tmp2))
  165. mii->mdio_write(dev, mii->phy_id, MII_CTRL1000, tmp2);
  166. /* turn on autonegotiation, and force a renegotiate */
  167. bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR);
  168. bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
  169. mii->mdio_write(dev, mii->phy_id, MII_BMCR, bmcr);
  170. mii->force_media = 0;
  171. } else {
  172. u32 bmcr, tmp;
  173. /* turn off auto negotiation, set speed and duplexity */
  174. bmcr = mii->mdio_read(dev, mii->phy_id, MII_BMCR);
  175. tmp = bmcr & ~(BMCR_ANENABLE | BMCR_SPEED100 |
  176. BMCR_SPEED1000 | BMCR_FULLDPLX);
  177. if (ecmd->speed == SPEED_1000)
  178. tmp |= BMCR_SPEED1000;
  179. else if (ecmd->speed == SPEED_100)
  180. tmp |= BMCR_SPEED100;
  181. if (ecmd->duplex == DUPLEX_FULL) {
  182. tmp |= BMCR_FULLDPLX;
  183. mii->full_duplex = 1;
  184. } else
  185. mii->full_duplex = 0;
  186. if (bmcr != tmp)
  187. mii->mdio_write(dev, mii->phy_id, MII_BMCR, tmp);
  188. mii->force_media = 1;
  189. }
  190. return 0;
  191. }
  192. /**
  193. * mii_check_gmii_support - check if the MII supports Gb interfaces
  194. * @mii: the MII interface
  195. */
  196. int mii_check_gmii_support(struct mii_if_info *mii)
  197. {
  198. int reg;
  199. reg = mii->mdio_read(mii->dev, mii->phy_id, MII_BMSR);
  200. if (reg & BMSR_ESTATEN) {
  201. reg = mii->mdio_read(mii->dev, mii->phy_id, MII_ESTATUS);
  202. if (reg & (ESTATUS_1000_TFULL | ESTATUS_1000_THALF))
  203. return 1;
  204. }
  205. return 0;
  206. }
  207. /**
  208. * mii_link_ok - is link status up/ok
  209. * @mii: the MII interface
  210. *
  211. * Returns 1 if the MII reports link status up/ok, 0 otherwise.
  212. */
  213. int mii_link_ok (struct mii_if_info *mii)
  214. {
  215. /* first, a dummy read, needed to latch some MII phys */
  216. mii->mdio_read(mii->dev, mii->phy_id, MII_BMSR);
  217. if (mii->mdio_read(mii->dev, mii->phy_id, MII_BMSR) & BMSR_LSTATUS)
  218. return 1;
  219. return 0;
  220. }
  221. /**
  222. * mii_nway_restart - restart NWay (autonegotiation) for this interface
  223. * @mii: the MII interface
  224. *
  225. * Returns 0 on success, negative on error.
  226. */
  227. int mii_nway_restart (struct mii_if_info *mii)
  228. {
  229. int bmcr;
  230. int r = -EINVAL;
  231. /* if autoneg is off, it's an error */
  232. bmcr = mii->mdio_read(mii->dev, mii->phy_id, MII_BMCR);
  233. if (bmcr & BMCR_ANENABLE) {
  234. bmcr |= BMCR_ANRESTART;
  235. mii->mdio_write(mii->dev, mii->phy_id, MII_BMCR, bmcr);
  236. r = 0;
  237. }
  238. return r;
  239. }
  240. /**
  241. * mii_check_link - check MII link status
  242. * @mii: MII interface
  243. *
  244. * If the link status changed (previous != current), call
  245. * netif_carrier_on() if current link status is Up or call
  246. * netif_carrier_off() if current link status is Down.
  247. */
  248. void mii_check_link (struct mii_if_info *mii)
  249. {
  250. int cur_link = mii_link_ok(mii);
  251. int prev_link = netif_carrier_ok(mii->dev);
  252. if (cur_link && !prev_link)
  253. netif_carrier_on(mii->dev);
  254. else if (prev_link && !cur_link)
  255. netif_carrier_off(mii->dev);
  256. }
  257. /**
  258. * mii_check_media - check the MII interface for a duplex change
  259. * @mii: the MII interface
  260. * @ok_to_print: OK to print link up/down messages
  261. * @init_media: OK to save duplex mode in @mii
  262. *
  263. * Returns 1 if the duplex mode changed, 0 if not.
  264. * If the media type is forced, always returns 0.
  265. */
  266. unsigned int mii_check_media (struct mii_if_info *mii,
  267. unsigned int ok_to_print,
  268. unsigned int init_media)
  269. {
  270. unsigned int old_carrier, new_carrier;
  271. int advertise, lpa, media, duplex;
  272. int lpa2 = 0;
  273. /* if forced media, go no further */
  274. if (mii->force_media)
  275. return 0; /* duplex did not change */
  276. /* check current and old link status */
  277. old_carrier = netif_carrier_ok(mii->dev) ? 1 : 0;
  278. new_carrier = (unsigned int) mii_link_ok(mii);
  279. /* if carrier state did not change, this is a "bounce",
  280. * just exit as everything is already set correctly
  281. */
  282. if ((!init_media) && (old_carrier == new_carrier))
  283. return 0; /* duplex did not change */
  284. /* no carrier, nothing much to do */
  285. if (!new_carrier) {
  286. netif_carrier_off(mii->dev);
  287. if (ok_to_print)
  288. printk(KERN_INFO "%s: link down\n", mii->dev->name);
  289. return 0; /* duplex did not change */
  290. }
  291. /*
  292. * we have carrier, see who's on the other end
  293. */
  294. netif_carrier_on(mii->dev);
  295. /* get MII advertise and LPA values */
  296. if ((!init_media) && (mii->advertising))
  297. advertise = mii->advertising;
  298. else {
  299. advertise = mii->mdio_read(mii->dev, mii->phy_id, MII_ADVERTISE);
  300. mii->advertising = advertise;
  301. }
  302. lpa = mii->mdio_read(mii->dev, mii->phy_id, MII_LPA);
  303. if (mii->supports_gmii)
  304. lpa2 = mii->mdio_read(mii->dev, mii->phy_id, MII_STAT1000);
  305. /* figure out media and duplex from advertise and LPA values */
  306. media = mii_nway_result(lpa & advertise);
  307. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  308. if (lpa2 & LPA_1000FULL)
  309. duplex = 1;
  310. if (ok_to_print)
  311. printk(KERN_INFO "%s: link up, %sMbps, %s-duplex, lpa 0x%04X\n",
  312. mii->dev->name,
  313. lpa2 & (LPA_1000FULL | LPA_1000HALF) ? "1000" :
  314. media & (ADVERTISE_100FULL | ADVERTISE_100HALF) ? "100" : "10",
  315. duplex ? "full" : "half",
  316. lpa);
  317. if ((init_media) || (mii->full_duplex != duplex)) {
  318. mii->full_duplex = duplex;
  319. return 1; /* duplex changed */
  320. }
  321. return 0; /* duplex did not change */
  322. }
  323. /**
  324. * generic_mii_ioctl - main MII ioctl interface
  325. * @mii_if: the MII interface
  326. * @mii_data: MII ioctl data structure
  327. * @cmd: MII ioctl command
  328. * @duplex_chg_out: pointer to @duplex_changed status if there was no
  329. * ioctl error
  330. *
  331. * Returns 0 on success, negative on error.
  332. */
  333. int generic_mii_ioctl(struct mii_if_info *mii_if,
  334. struct mii_ioctl_data *mii_data, int cmd,
  335. unsigned int *duplex_chg_out)
  336. {
  337. int rc = 0;
  338. unsigned int duplex_changed = 0;
  339. if (duplex_chg_out)
  340. *duplex_chg_out = 0;
  341. mii_data->phy_id &= mii_if->phy_id_mask;
  342. mii_data->reg_num &= mii_if->reg_num_mask;
  343. switch(cmd) {
  344. case SIOCGMIIPHY:
  345. mii_data->phy_id = mii_if->phy_id;
  346. /* fall through */
  347. case SIOCGMIIREG:
  348. mii_data->val_out =
  349. mii_if->mdio_read(mii_if->dev, mii_data->phy_id,
  350. mii_data->reg_num);
  351. break;
  352. case SIOCSMIIREG: {
  353. u16 val = mii_data->val_in;
  354. if (!capable(CAP_NET_ADMIN))
  355. return -EPERM;
  356. if (mii_data->phy_id == mii_if->phy_id) {
  357. switch(mii_data->reg_num) {
  358. case MII_BMCR: {
  359. unsigned int new_duplex = 0;
  360. if (val & (BMCR_RESET|BMCR_ANENABLE))
  361. mii_if->force_media = 0;
  362. else
  363. mii_if->force_media = 1;
  364. if (mii_if->force_media &&
  365. (val & BMCR_FULLDPLX))
  366. new_duplex = 1;
  367. if (mii_if->full_duplex != new_duplex) {
  368. duplex_changed = 1;
  369. mii_if->full_duplex = new_duplex;
  370. }
  371. break;
  372. }
  373. case MII_ADVERTISE:
  374. mii_if->advertising = val;
  375. break;
  376. default:
  377. /* do nothing */
  378. break;
  379. }
  380. }
  381. mii_if->mdio_write(mii_if->dev, mii_data->phy_id,
  382. mii_data->reg_num, val);
  383. break;
  384. }
  385. default:
  386. rc = -EOPNOTSUPP;
  387. break;
  388. }
  389. if ((rc == 0) && (duplex_chg_out) && (duplex_changed))
  390. *duplex_chg_out = 1;
  391. return rc;
  392. }
  393. MODULE_AUTHOR ("Jeff Garzik <jgarzik@pobox.com>");
  394. MODULE_DESCRIPTION ("MII hardware support library");
  395. MODULE_LICENSE("GPL");
  396. EXPORT_SYMBOL(mii_link_ok);
  397. EXPORT_SYMBOL(mii_nway_restart);
  398. EXPORT_SYMBOL(mii_ethtool_gset);
  399. EXPORT_SYMBOL(mii_ethtool_sset);
  400. EXPORT_SYMBOL(mii_check_link);
  401. EXPORT_SYMBOL(mii_check_media);
  402. EXPORT_SYMBOL(mii_check_gmii_support);
  403. EXPORT_SYMBOL(generic_mii_ioctl);