mcdi_phy.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /****************************************************************************
  2. * Driver for Solarflare Solarstorm network controllers and boards
  3. * Copyright 2009 Solarflare Communications Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation, incorporated herein by reference.
  8. */
  9. /*
  10. * Driver for PHY related operations via MCDI.
  11. */
  12. #include "efx.h"
  13. #include "phy.h"
  14. #include "mcdi.h"
  15. #include "mcdi_pcol.h"
  16. #include "mdio_10g.h"
  17. struct efx_mcdi_phy_cfg {
  18. u32 flags;
  19. u32 type;
  20. u32 supported_cap;
  21. u32 channel;
  22. u32 port;
  23. u32 stats_mask;
  24. u8 name[20];
  25. u32 media;
  26. u32 mmd_mask;
  27. u8 revision[20];
  28. u32 forced_cap;
  29. };
  30. static int
  31. efx_mcdi_get_phy_cfg(struct efx_nic *efx, struct efx_mcdi_phy_cfg *cfg)
  32. {
  33. u8 outbuf[MC_CMD_GET_PHY_CFG_OUT_LEN];
  34. size_t outlen;
  35. int rc;
  36. BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_IN_LEN != 0);
  37. BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_OUT_NAME_LEN != sizeof(cfg->name));
  38. rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_CFG, NULL, 0,
  39. outbuf, sizeof(outbuf), &outlen);
  40. if (rc)
  41. goto fail;
  42. if (outlen < MC_CMD_GET_PHY_CFG_OUT_LEN) {
  43. rc = -EMSGSIZE;
  44. goto fail;
  45. }
  46. cfg->flags = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_FLAGS);
  47. cfg->type = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_TYPE);
  48. cfg->supported_cap =
  49. MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_SUPPORTED_CAP);
  50. cfg->channel = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_CHANNEL);
  51. cfg->port = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_PRT);
  52. cfg->stats_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_STATS_MASK);
  53. memcpy(cfg->name, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_NAME),
  54. sizeof(cfg->name));
  55. cfg->media = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MEDIA_TYPE);
  56. cfg->mmd_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MMD_MASK);
  57. memcpy(cfg->revision, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_REVISION),
  58. sizeof(cfg->revision));
  59. return 0;
  60. fail:
  61. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  62. return rc;
  63. }
  64. static int efx_mcdi_set_link(struct efx_nic *efx, u32 capabilities,
  65. u32 flags, u32 loopback_mode,
  66. u32 loopback_speed)
  67. {
  68. u8 inbuf[MC_CMD_SET_LINK_IN_LEN];
  69. int rc;
  70. BUILD_BUG_ON(MC_CMD_SET_LINK_OUT_LEN != 0);
  71. MCDI_SET_DWORD(inbuf, SET_LINK_IN_CAP, capabilities);
  72. MCDI_SET_DWORD(inbuf, SET_LINK_IN_FLAGS, flags);
  73. MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_MODE, loopback_mode);
  74. MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_SPEED, loopback_speed);
  75. rc = efx_mcdi_rpc(efx, MC_CMD_SET_LINK, inbuf, sizeof(inbuf),
  76. NULL, 0, NULL);
  77. if (rc)
  78. goto fail;
  79. return 0;
  80. fail:
  81. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  82. return rc;
  83. }
  84. static int efx_mcdi_loopback_modes(struct efx_nic *efx, u64 *loopback_modes)
  85. {
  86. u8 outbuf[MC_CMD_GET_LOOPBACK_MODES_OUT_LEN];
  87. size_t outlen;
  88. int rc;
  89. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LOOPBACK_MODES, NULL, 0,
  90. outbuf, sizeof(outbuf), &outlen);
  91. if (rc)
  92. goto fail;
  93. if (outlen < MC_CMD_GET_LOOPBACK_MODES_OUT_LEN) {
  94. rc = -EMSGSIZE;
  95. goto fail;
  96. }
  97. *loopback_modes = MCDI_QWORD(outbuf, GET_LOOPBACK_MODES_SUGGESTED);
  98. return 0;
  99. fail:
  100. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  101. return rc;
  102. }
  103. int efx_mcdi_mdio_read(struct efx_nic *efx, unsigned int bus,
  104. unsigned int prtad, unsigned int devad, u16 addr,
  105. u16 *value_out, u32 *status_out)
  106. {
  107. u8 inbuf[MC_CMD_MDIO_READ_IN_LEN];
  108. u8 outbuf[MC_CMD_MDIO_READ_OUT_LEN];
  109. size_t outlen;
  110. int rc;
  111. MCDI_SET_DWORD(inbuf, MDIO_READ_IN_BUS, bus);
  112. MCDI_SET_DWORD(inbuf, MDIO_READ_IN_PRTAD, prtad);
  113. MCDI_SET_DWORD(inbuf, MDIO_READ_IN_DEVAD, devad);
  114. MCDI_SET_DWORD(inbuf, MDIO_READ_IN_ADDR, addr);
  115. rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_READ, inbuf, sizeof(inbuf),
  116. outbuf, sizeof(outbuf), &outlen);
  117. if (rc)
  118. goto fail;
  119. *value_out = (u16)MCDI_DWORD(outbuf, MDIO_READ_OUT_VALUE);
  120. *status_out = MCDI_DWORD(outbuf, MDIO_READ_OUT_STATUS);
  121. return 0;
  122. fail:
  123. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  124. return rc;
  125. }
  126. int efx_mcdi_mdio_write(struct efx_nic *efx, unsigned int bus,
  127. unsigned int prtad, unsigned int devad, u16 addr,
  128. u16 value, u32 *status_out)
  129. {
  130. u8 inbuf[MC_CMD_MDIO_WRITE_IN_LEN];
  131. u8 outbuf[MC_CMD_MDIO_WRITE_OUT_LEN];
  132. size_t outlen;
  133. int rc;
  134. MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_BUS, bus);
  135. MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_PRTAD, prtad);
  136. MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_DEVAD, devad);
  137. MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_ADDR, addr);
  138. MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_VALUE, value);
  139. rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_WRITE, inbuf, sizeof(inbuf),
  140. outbuf, sizeof(outbuf), &outlen);
  141. if (rc)
  142. goto fail;
  143. *status_out = MCDI_DWORD(outbuf, MDIO_WRITE_OUT_STATUS);
  144. return 0;
  145. fail:
  146. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  147. return rc;
  148. }
  149. static u32 mcdi_to_ethtool_cap(u32 media, u32 cap)
  150. {
  151. u32 result = 0;
  152. switch (media) {
  153. case MC_CMD_MEDIA_KX4:
  154. result |= SUPPORTED_Backplane;
  155. if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
  156. result |= SUPPORTED_1000baseKX_Full;
  157. if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
  158. result |= SUPPORTED_10000baseKX4_Full;
  159. break;
  160. case MC_CMD_MEDIA_XFP:
  161. case MC_CMD_MEDIA_SFP_PLUS:
  162. result |= SUPPORTED_FIBRE;
  163. break;
  164. case MC_CMD_MEDIA_BASE_T:
  165. result |= SUPPORTED_TP;
  166. if (cap & (1 << MC_CMD_PHY_CAP_10HDX_LBN))
  167. result |= SUPPORTED_10baseT_Half;
  168. if (cap & (1 << MC_CMD_PHY_CAP_10FDX_LBN))
  169. result |= SUPPORTED_10baseT_Full;
  170. if (cap & (1 << MC_CMD_PHY_CAP_100HDX_LBN))
  171. result |= SUPPORTED_100baseT_Half;
  172. if (cap & (1 << MC_CMD_PHY_CAP_100FDX_LBN))
  173. result |= SUPPORTED_100baseT_Full;
  174. if (cap & (1 << MC_CMD_PHY_CAP_1000HDX_LBN))
  175. result |= SUPPORTED_1000baseT_Half;
  176. if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
  177. result |= SUPPORTED_1000baseT_Full;
  178. if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
  179. result |= SUPPORTED_10000baseT_Full;
  180. break;
  181. }
  182. if (cap & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
  183. result |= SUPPORTED_Pause;
  184. if (cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
  185. result |= SUPPORTED_Asym_Pause;
  186. if (cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
  187. result |= SUPPORTED_Autoneg;
  188. return result;
  189. }
  190. static u32 ethtool_to_mcdi_cap(u32 cap)
  191. {
  192. u32 result = 0;
  193. if (cap & SUPPORTED_10baseT_Half)
  194. result |= (1 << MC_CMD_PHY_CAP_10HDX_LBN);
  195. if (cap & SUPPORTED_10baseT_Full)
  196. result |= (1 << MC_CMD_PHY_CAP_10FDX_LBN);
  197. if (cap & SUPPORTED_100baseT_Half)
  198. result |= (1 << MC_CMD_PHY_CAP_100HDX_LBN);
  199. if (cap & SUPPORTED_100baseT_Full)
  200. result |= (1 << MC_CMD_PHY_CAP_100FDX_LBN);
  201. if (cap & SUPPORTED_1000baseT_Half)
  202. result |= (1 << MC_CMD_PHY_CAP_1000HDX_LBN);
  203. if (cap & (SUPPORTED_1000baseT_Full | SUPPORTED_1000baseKX_Full))
  204. result |= (1 << MC_CMD_PHY_CAP_1000FDX_LBN);
  205. if (cap & (SUPPORTED_10000baseT_Full | SUPPORTED_10000baseKX4_Full))
  206. result |= (1 << MC_CMD_PHY_CAP_10000FDX_LBN);
  207. if (cap & SUPPORTED_Pause)
  208. result |= (1 << MC_CMD_PHY_CAP_PAUSE_LBN);
  209. if (cap & SUPPORTED_Asym_Pause)
  210. result |= (1 << MC_CMD_PHY_CAP_ASYM_LBN);
  211. if (cap & SUPPORTED_Autoneg)
  212. result |= (1 << MC_CMD_PHY_CAP_AN_LBN);
  213. return result;
  214. }
  215. static u32 efx_get_mcdi_phy_flags(struct efx_nic *efx)
  216. {
  217. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  218. enum efx_phy_mode mode, supported;
  219. u32 flags;
  220. /* TODO: Advertise the capabilities supported by this PHY */
  221. supported = 0;
  222. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_TXDIS_LBN))
  223. supported |= PHY_MODE_TX_DISABLED;
  224. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_LOWPOWER_LBN))
  225. supported |= PHY_MODE_LOW_POWER;
  226. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_POWEROFF_LBN))
  227. supported |= PHY_MODE_OFF;
  228. mode = efx->phy_mode & supported;
  229. flags = 0;
  230. if (mode & PHY_MODE_TX_DISABLED)
  231. flags |= (1 << MC_CMD_SET_LINK_TXDIS_LBN);
  232. if (mode & PHY_MODE_LOW_POWER)
  233. flags |= (1 << MC_CMD_SET_LINK_LOWPOWER_LBN);
  234. if (mode & PHY_MODE_OFF)
  235. flags |= (1 << MC_CMD_SET_LINK_POWEROFF_LBN);
  236. return flags;
  237. }
  238. static u32 mcdi_to_ethtool_media(u32 media)
  239. {
  240. switch (media) {
  241. case MC_CMD_MEDIA_XAUI:
  242. case MC_CMD_MEDIA_CX4:
  243. case MC_CMD_MEDIA_KX4:
  244. return PORT_OTHER;
  245. case MC_CMD_MEDIA_XFP:
  246. case MC_CMD_MEDIA_SFP_PLUS:
  247. return PORT_FIBRE;
  248. case MC_CMD_MEDIA_BASE_T:
  249. return PORT_TP;
  250. default:
  251. return PORT_OTHER;
  252. }
  253. }
  254. static int efx_mcdi_phy_probe(struct efx_nic *efx)
  255. {
  256. struct efx_mcdi_phy_cfg *phy_data;
  257. u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
  258. u32 caps;
  259. int rc;
  260. /* Initialise and populate phy_data */
  261. phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
  262. if (phy_data == NULL)
  263. return -ENOMEM;
  264. rc = efx_mcdi_get_phy_cfg(efx, phy_data);
  265. if (rc != 0)
  266. goto fail;
  267. /* Read initial link advertisement */
  268. BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
  269. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
  270. outbuf, sizeof(outbuf), NULL);
  271. if (rc)
  272. goto fail;
  273. /* Fill out nic state */
  274. efx->phy_data = phy_data;
  275. efx->phy_type = phy_data->type;
  276. efx->mdio_bus = phy_data->channel;
  277. efx->mdio.prtad = phy_data->port;
  278. efx->mdio.mmds = phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22);
  279. efx->mdio.mode_support = 0;
  280. if (phy_data->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
  281. efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
  282. if (phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
  283. efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
  284. caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP);
  285. if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN))
  286. efx->link_advertising =
  287. mcdi_to_ethtool_cap(phy_data->media, caps);
  288. else
  289. phy_data->forced_cap = caps;
  290. /* Assert that we can map efx -> mcdi loopback modes */
  291. BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE);
  292. BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA);
  293. BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC);
  294. BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII);
  295. BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS);
  296. BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI);
  297. BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII);
  298. BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII);
  299. BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR);
  300. BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI);
  301. BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR);
  302. BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR);
  303. BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR);
  304. BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR);
  305. BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY);
  306. BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS);
  307. BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS);
  308. BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD);
  309. BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT);
  310. BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS);
  311. BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS);
  312. BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR);
  313. BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR);
  314. BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS);
  315. BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS);
  316. BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR);
  317. BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS);
  318. rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes);
  319. if (rc != 0)
  320. goto fail;
  321. /* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
  322. * but by convention we don't */
  323. efx->loopback_modes &= ~(1 << LOOPBACK_NONE);
  324. return 0;
  325. fail:
  326. kfree(phy_data);
  327. return rc;
  328. }
  329. int efx_mcdi_phy_reconfigure(struct efx_nic *efx)
  330. {
  331. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  332. u32 caps = (efx->link_advertising ?
  333. ethtool_to_mcdi_cap(efx->link_advertising) :
  334. phy_cfg->forced_cap);
  335. return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
  336. efx->loopback_mode, 0);
  337. }
  338. void efx_mcdi_phy_decode_link(struct efx_nic *efx,
  339. struct efx_link_state *link_state,
  340. u32 speed, u32 flags, u32 fcntl)
  341. {
  342. switch (fcntl) {
  343. case MC_CMD_FCNTL_AUTO:
  344. WARN_ON(1); /* This is not a link mode */
  345. link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
  346. break;
  347. case MC_CMD_FCNTL_BIDIR:
  348. link_state->fc = EFX_FC_TX | EFX_FC_RX;
  349. break;
  350. case MC_CMD_FCNTL_RESPOND:
  351. link_state->fc = EFX_FC_RX;
  352. break;
  353. default:
  354. WARN_ON(1);
  355. case MC_CMD_FCNTL_OFF:
  356. link_state->fc = 0;
  357. break;
  358. }
  359. link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_LINK_UP_LBN));
  360. link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_FULL_DUPLEX_LBN));
  361. link_state->speed = speed;
  362. }
  363. /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
  364. * supported by the link partner. Warn the user if this isn't the case
  365. */
  366. void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
  367. {
  368. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  369. u32 rmtadv;
  370. /* The link partner capabilities are only relevent if the
  371. * link supports flow control autonegotiation */
  372. if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
  373. return;
  374. /* If flow control autoneg is supported and enabled, then fine */
  375. if (efx->wanted_fc & EFX_FC_AUTO)
  376. return;
  377. rmtadv = 0;
  378. if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
  379. rmtadv |= ADVERTISED_Pause;
  380. if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
  381. rmtadv |= ADVERTISED_Asym_Pause;
  382. if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
  383. EFX_ERR(efx, "warning: link partner doesn't support "
  384. "pause frames");
  385. }
  386. static bool efx_mcdi_phy_poll(struct efx_nic *efx)
  387. {
  388. struct efx_link_state old_state = efx->link_state;
  389. u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
  390. int rc;
  391. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  392. BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
  393. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
  394. outbuf, sizeof(outbuf), NULL);
  395. if (rc) {
  396. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  397. efx->link_state.up = false;
  398. } else {
  399. efx_mcdi_phy_decode_link(
  400. efx, &efx->link_state,
  401. MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
  402. MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
  403. MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
  404. }
  405. return !efx_link_state_equal(&efx->link_state, &old_state);
  406. }
  407. static void efx_mcdi_phy_remove(struct efx_nic *efx)
  408. {
  409. struct efx_mcdi_phy_data *phy_data = efx->phy_data;
  410. efx->phy_data = NULL;
  411. kfree(phy_data);
  412. }
  413. static void efx_mcdi_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
  414. {
  415. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  416. u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
  417. int rc;
  418. ecmd->supported =
  419. mcdi_to_ethtool_cap(phy_cfg->media, phy_cfg->supported_cap);
  420. ecmd->advertising = efx->link_advertising;
  421. ecmd->speed = efx->link_state.speed;
  422. ecmd->duplex = efx->link_state.fd;
  423. ecmd->port = mcdi_to_ethtool_media(phy_cfg->media);
  424. ecmd->phy_address = phy_cfg->port;
  425. ecmd->transceiver = XCVR_INTERNAL;
  426. ecmd->autoneg = !!(efx->link_advertising & ADVERTISED_Autoneg);
  427. ecmd->mdio_support = (efx->mdio.mode_support &
  428. (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
  429. BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
  430. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
  431. outbuf, sizeof(outbuf), NULL);
  432. if (rc) {
  433. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  434. return;
  435. }
  436. ecmd->lp_advertising =
  437. mcdi_to_ethtool_cap(phy_cfg->media,
  438. MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP));
  439. }
  440. static int efx_mcdi_phy_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
  441. {
  442. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  443. u32 caps;
  444. int rc;
  445. if (ecmd->autoneg) {
  446. caps = (ethtool_to_mcdi_cap(ecmd->advertising) |
  447. 1 << MC_CMD_PHY_CAP_AN_LBN);
  448. } else if (ecmd->duplex) {
  449. switch (ecmd->speed) {
  450. case 10: caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN; break;
  451. case 100: caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN; break;
  452. case 1000: caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN; break;
  453. case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break;
  454. default: return -EINVAL;
  455. }
  456. } else {
  457. switch (ecmd->speed) {
  458. case 10: caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN; break;
  459. case 100: caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN; break;
  460. case 1000: caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN; break;
  461. default: return -EINVAL;
  462. }
  463. }
  464. rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
  465. efx->loopback_mode, 0);
  466. if (rc)
  467. return rc;
  468. if (ecmd->autoneg) {
  469. efx_link_set_advertising(
  470. efx, ecmd->advertising | ADVERTISED_Autoneg);
  471. phy_cfg->forced_cap = 0;
  472. } else {
  473. efx_link_set_advertising(efx, 0);
  474. phy_cfg->forced_cap = caps;
  475. }
  476. return 0;
  477. }
  478. struct efx_phy_operations efx_mcdi_phy_ops = {
  479. .probe = efx_mcdi_phy_probe,
  480. .init = efx_port_dummy_op_int,
  481. .reconfigure = efx_mcdi_phy_reconfigure,
  482. .poll = efx_mcdi_phy_poll,
  483. .fini = efx_port_dummy_op_void,
  484. .remove = efx_mcdi_phy_remove,
  485. .get_settings = efx_mcdi_phy_get_settings,
  486. .set_settings = efx_mcdi_phy_set_settings,
  487. .run_tests = NULL,
  488. .test_name = NULL,
  489. };