mdio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * mdio.c: Generic support for MDIO-compatible transceivers
  4. * Copyright 2006-2009 Solarflare Communications Inc.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/capability.h>
  8. #include <linux/errno.h>
  9. #include <linux/ethtool.h>
  10. #include <linux/mdio.h>
  11. #include <linux/module.h>
  12. MODULE_DESCRIPTION("Generic support for MDIO-compatible transceivers");
  13. MODULE_AUTHOR("Copyright 2006-2009 Solarflare Communications Inc.");
  14. MODULE_LICENSE("GPL");
  15. /**
  16. * mdio45_probe - probe for an MDIO (clause 45) device
  17. * @mdio: MDIO interface
  18. * @prtad: Expected PHY address
  19. *
  20. * This sets @prtad and @mmds in the MDIO interface if successful.
  21. * Returns 0 on success, negative on error.
  22. */
  23. int mdio45_probe(struct mdio_if_info *mdio, int prtad)
  24. {
  25. int mmd, stat2, devs1, devs2;
  26. /* Assume PHY must have at least one of PMA/PMD, WIS, PCS, PHY
  27. * XS or DTE XS; give up if none is present. */
  28. for (mmd = 1; mmd <= 5; mmd++) {
  29. /* Is this MMD present? */
  30. stat2 = mdio->mdio_read(mdio->dev, prtad, mmd, MDIO_STAT2);
  31. if (stat2 < 0 ||
  32. (stat2 & MDIO_STAT2_DEVPRST) != MDIO_STAT2_DEVPRST_VAL)
  33. continue;
  34. /* It should tell us about all the other MMDs */
  35. devs1 = mdio->mdio_read(mdio->dev, prtad, mmd, MDIO_DEVS1);
  36. devs2 = mdio->mdio_read(mdio->dev, prtad, mmd, MDIO_DEVS2);
  37. if (devs1 < 0 || devs2 < 0)
  38. continue;
  39. mdio->prtad = prtad;
  40. mdio->mmds = devs1 | (devs2 << 16);
  41. return 0;
  42. }
  43. return -ENODEV;
  44. }
  45. EXPORT_SYMBOL(mdio45_probe);
  46. /**
  47. * mdio_set_flag - set or clear flag in an MDIO register
  48. * @mdio: MDIO interface
  49. * @prtad: PHY address
  50. * @devad: MMD address
  51. * @addr: Register address
  52. * @mask: Mask for flag (single bit set)
  53. * @sense: New value of flag
  54. *
  55. * This debounces changes: it does not write the register if the flag
  56. * already has the proper value. Returns 0 on success, negative on error.
  57. */
  58. int mdio_set_flag(const struct mdio_if_info *mdio,
  59. int prtad, int devad, u16 addr, int mask,
  60. bool sense)
  61. {
  62. int old_val = mdio->mdio_read(mdio->dev, prtad, devad, addr);
  63. int new_val;
  64. if (old_val < 0)
  65. return old_val;
  66. if (sense)
  67. new_val = old_val | mask;
  68. else
  69. new_val = old_val & ~mask;
  70. if (old_val == new_val)
  71. return 0;
  72. return mdio->mdio_write(mdio->dev, prtad, devad, addr, new_val);
  73. }
  74. EXPORT_SYMBOL(mdio_set_flag);
  75. /**
  76. * mdio_link_ok - is link status up/OK
  77. * @mdio: MDIO interface
  78. * @mmd_mask: Mask for MMDs to check
  79. *
  80. * Returns 1 if the PHY reports link status up/OK, 0 otherwise.
  81. * @mmd_mask is normally @mdio->mmds, but if loopback is enabled
  82. * the MMDs being bypassed should be excluded from the mask.
  83. */
  84. int mdio45_links_ok(const struct mdio_if_info *mdio, u32 mmd_mask)
  85. {
  86. int devad, reg;
  87. if (!mmd_mask) {
  88. /* Use absence of XGMII faults in lieu of link state */
  89. reg = mdio->mdio_read(mdio->dev, mdio->prtad,
  90. MDIO_MMD_PHYXS, MDIO_STAT2);
  91. return reg >= 0 && !(reg & MDIO_STAT2_RXFAULT);
  92. }
  93. for (devad = 0; mmd_mask; devad++) {
  94. if (mmd_mask & (1 << devad)) {
  95. mmd_mask &= ~(1 << devad);
  96. /* Reset the latched status and fault flags */
  97. mdio->mdio_read(mdio->dev, mdio->prtad,
  98. devad, MDIO_STAT1);
  99. if (devad == MDIO_MMD_PMAPMD || devad == MDIO_MMD_PCS ||
  100. devad == MDIO_MMD_PHYXS || devad == MDIO_MMD_DTEXS)
  101. mdio->mdio_read(mdio->dev, mdio->prtad,
  102. devad, MDIO_STAT2);
  103. /* Check the current status and fault flags */
  104. reg = mdio->mdio_read(mdio->dev, mdio->prtad,
  105. devad, MDIO_STAT1);
  106. if (reg < 0 ||
  107. (reg & (MDIO_STAT1_FAULT | MDIO_STAT1_LSTATUS)) !=
  108. MDIO_STAT1_LSTATUS)
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. EXPORT_SYMBOL(mdio45_links_ok);
  115. /**
  116. * mdio45_nway_restart - restart auto-negotiation for this interface
  117. * @mdio: MDIO interface
  118. *
  119. * Returns 0 on success, negative on error.
  120. */
  121. int mdio45_nway_restart(const struct mdio_if_info *mdio)
  122. {
  123. if (!(mdio->mmds & MDIO_DEVS_AN))
  124. return -EOPNOTSUPP;
  125. mdio_set_flag(mdio, mdio->prtad, MDIO_MMD_AN, MDIO_CTRL1,
  126. MDIO_AN_CTRL1_RESTART, true);
  127. return 0;
  128. }
  129. EXPORT_SYMBOL(mdio45_nway_restart);
  130. static u32 mdio45_get_an(const struct mdio_if_info *mdio, u16 addr)
  131. {
  132. u32 result = 0;
  133. int reg;
  134. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_AN, addr);
  135. if (reg & ADVERTISE_10HALF)
  136. result |= ADVERTISED_10baseT_Half;
  137. if (reg & ADVERTISE_10FULL)
  138. result |= ADVERTISED_10baseT_Full;
  139. if (reg & ADVERTISE_100HALF)
  140. result |= ADVERTISED_100baseT_Half;
  141. if (reg & ADVERTISE_100FULL)
  142. result |= ADVERTISED_100baseT_Full;
  143. if (reg & ADVERTISE_PAUSE_CAP)
  144. result |= ADVERTISED_Pause;
  145. if (reg & ADVERTISE_PAUSE_ASYM)
  146. result |= ADVERTISED_Asym_Pause;
  147. return result;
  148. }
  149. /**
  150. * mdio45_ethtool_gset_npage - get settings for ETHTOOL_GSET
  151. * @mdio: MDIO interface
  152. * @ecmd: Ethtool request structure
  153. * @npage_adv: Modes currently advertised on next pages
  154. * @npage_lpa: Modes advertised by link partner on next pages
  155. *
  156. * The @ecmd parameter is expected to have been cleared before calling
  157. * mdio45_ethtool_gset_npage().
  158. *
  159. * Since the CSRs for auto-negotiation using next pages are not fully
  160. * standardised, this function does not attempt to decode them. The
  161. * caller must pass them in.
  162. */
  163. void mdio45_ethtool_gset_npage(const struct mdio_if_info *mdio,
  164. struct ethtool_cmd *ecmd,
  165. u32 npage_adv, u32 npage_lpa)
  166. {
  167. int reg;
  168. u32 speed;
  169. BUILD_BUG_ON(MDIO_SUPPORTS_C22 != ETH_MDIO_SUPPORTS_C22);
  170. BUILD_BUG_ON(MDIO_SUPPORTS_C45 != ETH_MDIO_SUPPORTS_C45);
  171. ecmd->transceiver = XCVR_INTERNAL;
  172. ecmd->phy_address = mdio->prtad;
  173. ecmd->mdio_support =
  174. mdio->mode_support & (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22);
  175. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  176. MDIO_CTRL2);
  177. switch (reg & MDIO_PMA_CTRL2_TYPE) {
  178. case MDIO_PMA_CTRL2_10GBT:
  179. case MDIO_PMA_CTRL2_1000BT:
  180. case MDIO_PMA_CTRL2_100BTX:
  181. case MDIO_PMA_CTRL2_10BT:
  182. ecmd->port = PORT_TP;
  183. ecmd->supported = SUPPORTED_TP;
  184. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  185. MDIO_SPEED);
  186. if (reg & MDIO_SPEED_10G)
  187. ecmd->supported |= SUPPORTED_10000baseT_Full;
  188. if (reg & MDIO_PMA_SPEED_1000)
  189. ecmd->supported |= (SUPPORTED_1000baseT_Full |
  190. SUPPORTED_1000baseT_Half);
  191. if (reg & MDIO_PMA_SPEED_100)
  192. ecmd->supported |= (SUPPORTED_100baseT_Full |
  193. SUPPORTED_100baseT_Half);
  194. if (reg & MDIO_PMA_SPEED_10)
  195. ecmd->supported |= (SUPPORTED_10baseT_Full |
  196. SUPPORTED_10baseT_Half);
  197. ecmd->advertising = ADVERTISED_TP;
  198. break;
  199. case MDIO_PMA_CTRL2_10GBCX4:
  200. ecmd->port = PORT_OTHER;
  201. ecmd->supported = 0;
  202. ecmd->advertising = 0;
  203. break;
  204. case MDIO_PMA_CTRL2_10GBKX4:
  205. case MDIO_PMA_CTRL2_10GBKR:
  206. case MDIO_PMA_CTRL2_1000BKX:
  207. ecmd->port = PORT_OTHER;
  208. ecmd->supported = SUPPORTED_Backplane;
  209. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  210. MDIO_PMA_EXTABLE);
  211. if (reg & MDIO_PMA_EXTABLE_10GBKX4)
  212. ecmd->supported |= SUPPORTED_10000baseKX4_Full;
  213. if (reg & MDIO_PMA_EXTABLE_10GBKR)
  214. ecmd->supported |= SUPPORTED_10000baseKR_Full;
  215. if (reg & MDIO_PMA_EXTABLE_1000BKX)
  216. ecmd->supported |= SUPPORTED_1000baseKX_Full;
  217. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  218. MDIO_PMA_10GBR_FECABLE);
  219. if (reg & MDIO_PMA_10GBR_FECABLE_ABLE)
  220. ecmd->supported |= SUPPORTED_10000baseR_FEC;
  221. ecmd->advertising = ADVERTISED_Backplane;
  222. break;
  223. /* All the other defined modes are flavours of optical */
  224. default:
  225. ecmd->port = PORT_FIBRE;
  226. ecmd->supported = SUPPORTED_FIBRE;
  227. ecmd->advertising = ADVERTISED_FIBRE;
  228. break;
  229. }
  230. if (mdio->mmds & MDIO_DEVS_AN) {
  231. ecmd->supported |= SUPPORTED_Autoneg;
  232. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_AN,
  233. MDIO_CTRL1);
  234. if (reg & MDIO_AN_CTRL1_ENABLE) {
  235. ecmd->autoneg = AUTONEG_ENABLE;
  236. ecmd->advertising |=
  237. ADVERTISED_Autoneg |
  238. mdio45_get_an(mdio, MDIO_AN_ADVERTISE) |
  239. npage_adv;
  240. } else {
  241. ecmd->autoneg = AUTONEG_DISABLE;
  242. }
  243. } else {
  244. ecmd->autoneg = AUTONEG_DISABLE;
  245. }
  246. if (ecmd->autoneg) {
  247. u32 modes = 0;
  248. int an_stat = mdio->mdio_read(mdio->dev, mdio->prtad,
  249. MDIO_MMD_AN, MDIO_STAT1);
  250. /* If AN is complete and successful, report best common
  251. * mode, otherwise report best advertised mode. */
  252. if (an_stat & MDIO_AN_STAT1_COMPLETE) {
  253. ecmd->lp_advertising =
  254. mdio45_get_an(mdio, MDIO_AN_LPA) | npage_lpa;
  255. if (an_stat & MDIO_AN_STAT1_LPABLE)
  256. ecmd->lp_advertising |= ADVERTISED_Autoneg;
  257. modes = ecmd->advertising & ecmd->lp_advertising;
  258. }
  259. if ((modes & ~ADVERTISED_Autoneg) == 0)
  260. modes = ecmd->advertising;
  261. if (modes & (ADVERTISED_10000baseT_Full |
  262. ADVERTISED_10000baseKX4_Full |
  263. ADVERTISED_10000baseKR_Full)) {
  264. speed = SPEED_10000;
  265. ecmd->duplex = DUPLEX_FULL;
  266. } else if (modes & (ADVERTISED_1000baseT_Full |
  267. ADVERTISED_1000baseT_Half |
  268. ADVERTISED_1000baseKX_Full)) {
  269. speed = SPEED_1000;
  270. ecmd->duplex = !(modes & ADVERTISED_1000baseT_Half);
  271. } else if (modes & (ADVERTISED_100baseT_Full |
  272. ADVERTISED_100baseT_Half)) {
  273. speed = SPEED_100;
  274. ecmd->duplex = !!(modes & ADVERTISED_100baseT_Full);
  275. } else {
  276. speed = SPEED_10;
  277. ecmd->duplex = !!(modes & ADVERTISED_10baseT_Full);
  278. }
  279. } else {
  280. /* Report forced settings */
  281. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  282. MDIO_CTRL1);
  283. speed = (((reg & MDIO_PMA_CTRL1_SPEED1000) ? 100 : 1)
  284. * ((reg & MDIO_PMA_CTRL1_SPEED100) ? 100 : 10));
  285. ecmd->duplex = (reg & MDIO_CTRL1_FULLDPLX ||
  286. speed == SPEED_10000);
  287. }
  288. ethtool_cmd_speed_set(ecmd, speed);
  289. /* 10GBASE-T MDI/MDI-X */
  290. if (ecmd->port == PORT_TP
  291. && (ethtool_cmd_speed(ecmd) == SPEED_10000)) {
  292. switch (mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  293. MDIO_PMA_10GBT_SWAPPOL)) {
  294. case MDIO_PMA_10GBT_SWAPPOL_ABNX | MDIO_PMA_10GBT_SWAPPOL_CDNX:
  295. ecmd->eth_tp_mdix = ETH_TP_MDI;
  296. break;
  297. case 0:
  298. ecmd->eth_tp_mdix = ETH_TP_MDI_X;
  299. break;
  300. default:
  301. /* It's complicated... */
  302. ecmd->eth_tp_mdix = ETH_TP_MDI_INVALID;
  303. break;
  304. }
  305. }
  306. }
  307. EXPORT_SYMBOL(mdio45_ethtool_gset_npage);
  308. /**
  309. * mdio45_ethtool_ksettings_get_npage - get settings for ETHTOOL_GLINKSETTINGS
  310. * @mdio: MDIO interface
  311. * @cmd: Ethtool request structure
  312. * @npage_adv: Modes currently advertised on next pages
  313. * @npage_lpa: Modes advertised by link partner on next pages
  314. *
  315. * The @cmd parameter is expected to have been cleared before calling
  316. * mdio45_ethtool_ksettings_get_npage().
  317. *
  318. * Since the CSRs for auto-negotiation using next pages are not fully
  319. * standardised, this function does not attempt to decode them. The
  320. * caller must pass them in.
  321. */
  322. void mdio45_ethtool_ksettings_get_npage(const struct mdio_if_info *mdio,
  323. struct ethtool_link_ksettings *cmd,
  324. u32 npage_adv, u32 npage_lpa)
  325. {
  326. int reg;
  327. u32 speed, supported = 0, advertising = 0, lp_advertising = 0;
  328. BUILD_BUG_ON(MDIO_SUPPORTS_C22 != ETH_MDIO_SUPPORTS_C22);
  329. BUILD_BUG_ON(MDIO_SUPPORTS_C45 != ETH_MDIO_SUPPORTS_C45);
  330. cmd->base.phy_address = mdio->prtad;
  331. cmd->base.mdio_support =
  332. mdio->mode_support & (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22);
  333. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  334. MDIO_CTRL2);
  335. switch (reg & MDIO_PMA_CTRL2_TYPE) {
  336. case MDIO_PMA_CTRL2_10GBT:
  337. case MDIO_PMA_CTRL2_1000BT:
  338. case MDIO_PMA_CTRL2_100BTX:
  339. case MDIO_PMA_CTRL2_10BT:
  340. cmd->base.port = PORT_TP;
  341. supported = SUPPORTED_TP;
  342. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  343. MDIO_SPEED);
  344. if (reg & MDIO_SPEED_10G)
  345. supported |= SUPPORTED_10000baseT_Full;
  346. if (reg & MDIO_PMA_SPEED_1000)
  347. supported |= (SUPPORTED_1000baseT_Full |
  348. SUPPORTED_1000baseT_Half);
  349. if (reg & MDIO_PMA_SPEED_100)
  350. supported |= (SUPPORTED_100baseT_Full |
  351. SUPPORTED_100baseT_Half);
  352. if (reg & MDIO_PMA_SPEED_10)
  353. supported |= (SUPPORTED_10baseT_Full |
  354. SUPPORTED_10baseT_Half);
  355. advertising = ADVERTISED_TP;
  356. break;
  357. case MDIO_PMA_CTRL2_10GBCX4:
  358. cmd->base.port = PORT_OTHER;
  359. supported = 0;
  360. advertising = 0;
  361. break;
  362. case MDIO_PMA_CTRL2_10GBKX4:
  363. case MDIO_PMA_CTRL2_10GBKR:
  364. case MDIO_PMA_CTRL2_1000BKX:
  365. cmd->base.port = PORT_OTHER;
  366. supported = SUPPORTED_Backplane;
  367. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  368. MDIO_PMA_EXTABLE);
  369. if (reg & MDIO_PMA_EXTABLE_10GBKX4)
  370. supported |= SUPPORTED_10000baseKX4_Full;
  371. if (reg & MDIO_PMA_EXTABLE_10GBKR)
  372. supported |= SUPPORTED_10000baseKR_Full;
  373. if (reg & MDIO_PMA_EXTABLE_1000BKX)
  374. supported |= SUPPORTED_1000baseKX_Full;
  375. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  376. MDIO_PMA_10GBR_FECABLE);
  377. if (reg & MDIO_PMA_10GBR_FECABLE_ABLE)
  378. supported |= SUPPORTED_10000baseR_FEC;
  379. advertising = ADVERTISED_Backplane;
  380. break;
  381. /* All the other defined modes are flavours of optical */
  382. default:
  383. cmd->base.port = PORT_FIBRE;
  384. supported = SUPPORTED_FIBRE;
  385. advertising = ADVERTISED_FIBRE;
  386. break;
  387. }
  388. if (mdio->mmds & MDIO_DEVS_AN) {
  389. supported |= SUPPORTED_Autoneg;
  390. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_AN,
  391. MDIO_CTRL1);
  392. if (reg & MDIO_AN_CTRL1_ENABLE) {
  393. cmd->base.autoneg = AUTONEG_ENABLE;
  394. advertising |=
  395. ADVERTISED_Autoneg |
  396. mdio45_get_an(mdio, MDIO_AN_ADVERTISE) |
  397. npage_adv;
  398. } else {
  399. cmd->base.autoneg = AUTONEG_DISABLE;
  400. }
  401. } else {
  402. cmd->base.autoneg = AUTONEG_DISABLE;
  403. }
  404. if (cmd->base.autoneg) {
  405. u32 modes = 0;
  406. int an_stat = mdio->mdio_read(mdio->dev, mdio->prtad,
  407. MDIO_MMD_AN, MDIO_STAT1);
  408. /* If AN is complete and successful, report best common
  409. * mode, otherwise report best advertised mode.
  410. */
  411. if (an_stat & MDIO_AN_STAT1_COMPLETE) {
  412. lp_advertising =
  413. mdio45_get_an(mdio, MDIO_AN_LPA) | npage_lpa;
  414. if (an_stat & MDIO_AN_STAT1_LPABLE)
  415. lp_advertising |= ADVERTISED_Autoneg;
  416. modes = advertising & lp_advertising;
  417. }
  418. if ((modes & ~ADVERTISED_Autoneg) == 0)
  419. modes = advertising;
  420. if (modes & (ADVERTISED_10000baseT_Full |
  421. ADVERTISED_10000baseKX4_Full |
  422. ADVERTISED_10000baseKR_Full)) {
  423. speed = SPEED_10000;
  424. cmd->base.duplex = DUPLEX_FULL;
  425. } else if (modes & (ADVERTISED_1000baseT_Full |
  426. ADVERTISED_1000baseT_Half |
  427. ADVERTISED_1000baseKX_Full)) {
  428. speed = SPEED_1000;
  429. cmd->base.duplex = !(modes & ADVERTISED_1000baseT_Half);
  430. } else if (modes & (ADVERTISED_100baseT_Full |
  431. ADVERTISED_100baseT_Half)) {
  432. speed = SPEED_100;
  433. cmd->base.duplex = !!(modes & ADVERTISED_100baseT_Full);
  434. } else {
  435. speed = SPEED_10;
  436. cmd->base.duplex = !!(modes & ADVERTISED_10baseT_Full);
  437. }
  438. } else {
  439. /* Report forced settings */
  440. reg = mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  441. MDIO_CTRL1);
  442. speed = (((reg & MDIO_PMA_CTRL1_SPEED1000) ? 100 : 1)
  443. * ((reg & MDIO_PMA_CTRL1_SPEED100) ? 100 : 10));
  444. cmd->base.duplex = (reg & MDIO_CTRL1_FULLDPLX ||
  445. speed == SPEED_10000);
  446. }
  447. cmd->base.speed = speed;
  448. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  449. supported);
  450. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  451. advertising);
  452. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
  453. lp_advertising);
  454. /* 10GBASE-T MDI/MDI-X */
  455. if (cmd->base.port == PORT_TP && (cmd->base.speed == SPEED_10000)) {
  456. switch (mdio->mdio_read(mdio->dev, mdio->prtad, MDIO_MMD_PMAPMD,
  457. MDIO_PMA_10GBT_SWAPPOL)) {
  458. case MDIO_PMA_10GBT_SWAPPOL_ABNX | MDIO_PMA_10GBT_SWAPPOL_CDNX:
  459. cmd->base.eth_tp_mdix = ETH_TP_MDI;
  460. break;
  461. case 0:
  462. cmd->base.eth_tp_mdix = ETH_TP_MDI_X;
  463. break;
  464. default:
  465. /* It's complicated... */
  466. cmd->base.eth_tp_mdix = ETH_TP_MDI_INVALID;
  467. break;
  468. }
  469. }
  470. }
  471. EXPORT_SYMBOL(mdio45_ethtool_ksettings_get_npage);
  472. /**
  473. * mdio_mii_ioctl - MII ioctl interface for MDIO (clause 22 or 45) PHYs
  474. * @mdio: MDIO interface
  475. * @mii_data: MII ioctl data structure
  476. * @cmd: MII ioctl command
  477. *
  478. * Returns 0 on success, negative on error.
  479. */
  480. int mdio_mii_ioctl(const struct mdio_if_info *mdio,
  481. struct mii_ioctl_data *mii_data, int cmd)
  482. {
  483. int prtad, devad;
  484. u16 addr = mii_data->reg_num;
  485. /* Validate/convert cmd to one of SIOC{G,S}MIIREG */
  486. switch (cmd) {
  487. case SIOCGMIIPHY:
  488. if (mdio->prtad == MDIO_PRTAD_NONE)
  489. return -EOPNOTSUPP;
  490. mii_data->phy_id = mdio->prtad;
  491. cmd = SIOCGMIIREG;
  492. break;
  493. case SIOCGMIIREG:
  494. case SIOCSMIIREG:
  495. break;
  496. default:
  497. return -EOPNOTSUPP;
  498. }
  499. /* Validate/convert phy_id */
  500. if ((mdio->mode_support & MDIO_SUPPORTS_C45) &&
  501. mdio_phy_id_is_c45(mii_data->phy_id)) {
  502. prtad = mdio_phy_id_prtad(mii_data->phy_id);
  503. devad = mdio_phy_id_devad(mii_data->phy_id);
  504. } else if ((mdio->mode_support & MDIO_SUPPORTS_C22) &&
  505. mii_data->phy_id < 0x20) {
  506. prtad = mii_data->phy_id;
  507. devad = MDIO_DEVAD_NONE;
  508. addr &= 0x1f;
  509. } else if ((mdio->mode_support & MDIO_EMULATE_C22) &&
  510. mdio->prtad != MDIO_PRTAD_NONE &&
  511. mii_data->phy_id == mdio->prtad) {
  512. /* Remap commonly-used MII registers. */
  513. prtad = mdio->prtad;
  514. switch (addr) {
  515. case MII_BMCR:
  516. case MII_BMSR:
  517. case MII_PHYSID1:
  518. case MII_PHYSID2:
  519. devad = __ffs(mdio->mmds);
  520. break;
  521. case MII_ADVERTISE:
  522. case MII_LPA:
  523. if (!(mdio->mmds & MDIO_DEVS_AN))
  524. return -EINVAL;
  525. devad = MDIO_MMD_AN;
  526. if (addr == MII_ADVERTISE)
  527. addr = MDIO_AN_ADVERTISE;
  528. else
  529. addr = MDIO_AN_LPA;
  530. break;
  531. default:
  532. return -EINVAL;
  533. }
  534. } else {
  535. return -EINVAL;
  536. }
  537. if (cmd == SIOCGMIIREG) {
  538. int rc = mdio->mdio_read(mdio->dev, prtad, devad, addr);
  539. if (rc < 0)
  540. return rc;
  541. mii_data->val_out = rc;
  542. return 0;
  543. } else {
  544. return mdio->mdio_write(mdio->dev, prtad, devad, addr,
  545. mii_data->val_in);
  546. }
  547. }
  548. EXPORT_SYMBOL(mdio_mii_ioctl);