mcdi_phy.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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_cfg;
  257. int rc;
  258. /* TODO: Move phy_data initialisation to
  259. * phy_op->probe/remove, rather than init/fini */
  260. phy_cfg = kzalloc(sizeof(*phy_cfg), GFP_KERNEL);
  261. if (phy_cfg == NULL) {
  262. rc = -ENOMEM;
  263. goto fail_alloc;
  264. }
  265. rc = efx_mcdi_get_phy_cfg(efx, phy_cfg);
  266. if (rc != 0)
  267. goto fail;
  268. efx->phy_type = phy_cfg->type;
  269. efx->mdio_bus = phy_cfg->channel;
  270. efx->mdio.prtad = phy_cfg->port;
  271. efx->mdio.mmds = phy_cfg->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22);
  272. efx->mdio.mode_support = 0;
  273. if (phy_cfg->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
  274. efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
  275. if (phy_cfg->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
  276. efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
  277. /* Assert that we can map efx -> mcdi loopback modes */
  278. BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE);
  279. BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA);
  280. BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC);
  281. BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII);
  282. BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS);
  283. BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI);
  284. BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII);
  285. BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII);
  286. BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR);
  287. BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI);
  288. BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR);
  289. BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR);
  290. BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR);
  291. BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR);
  292. BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY);
  293. BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS);
  294. BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS);
  295. BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD);
  296. BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT);
  297. BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS);
  298. BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS);
  299. BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR);
  300. BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR);
  301. BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS);
  302. BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS);
  303. BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR);
  304. BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS);
  305. rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes);
  306. if (rc != 0)
  307. goto fail;
  308. /* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
  309. * but by convention we don't */
  310. efx->loopback_modes &= ~(1 << LOOPBACK_NONE);
  311. kfree(phy_cfg);
  312. return 0;
  313. fail:
  314. kfree(phy_cfg);
  315. fail_alloc:
  316. return rc;
  317. }
  318. static int efx_mcdi_phy_init(struct efx_nic *efx)
  319. {
  320. struct efx_mcdi_phy_cfg *phy_data;
  321. u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
  322. u32 caps;
  323. int rc;
  324. phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
  325. if (phy_data == NULL)
  326. return -ENOMEM;
  327. rc = efx_mcdi_get_phy_cfg(efx, phy_data);
  328. if (rc != 0)
  329. goto fail;
  330. efx->phy_data = phy_data;
  331. BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
  332. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
  333. outbuf, sizeof(outbuf), NULL);
  334. if (rc)
  335. goto fail;
  336. caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP);
  337. if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN))
  338. efx->link_advertising =
  339. mcdi_to_ethtool_cap(phy_data->media, caps);
  340. else
  341. phy_data->forced_cap = caps;
  342. return 0;
  343. fail:
  344. kfree(phy_data);
  345. return rc;
  346. }
  347. int efx_mcdi_phy_reconfigure(struct efx_nic *efx)
  348. {
  349. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  350. u32 caps = (efx->link_advertising ?
  351. ethtool_to_mcdi_cap(efx->link_advertising) :
  352. phy_cfg->forced_cap);
  353. return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
  354. efx->loopback_mode, 0);
  355. }
  356. void efx_mcdi_phy_decode_link(struct efx_nic *efx,
  357. struct efx_link_state *link_state,
  358. u32 speed, u32 flags, u32 fcntl)
  359. {
  360. switch (fcntl) {
  361. case MC_CMD_FCNTL_AUTO:
  362. WARN_ON(1); /* This is not a link mode */
  363. link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
  364. break;
  365. case MC_CMD_FCNTL_BIDIR:
  366. link_state->fc = EFX_FC_TX | EFX_FC_RX;
  367. break;
  368. case MC_CMD_FCNTL_RESPOND:
  369. link_state->fc = EFX_FC_RX;
  370. break;
  371. default:
  372. WARN_ON(1);
  373. case MC_CMD_FCNTL_OFF:
  374. link_state->fc = 0;
  375. break;
  376. }
  377. link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_LINK_UP_LBN));
  378. link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_FULL_DUPLEX_LBN));
  379. link_state->speed = speed;
  380. }
  381. /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
  382. * supported by the link partner. Warn the user if this isn't the case
  383. */
  384. void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
  385. {
  386. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  387. u32 rmtadv;
  388. /* The link partner capabilities are only relevent if the
  389. * link supports flow control autonegotiation */
  390. if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
  391. return;
  392. /* If flow control autoneg is supported and enabled, then fine */
  393. if (efx->wanted_fc & EFX_FC_AUTO)
  394. return;
  395. rmtadv = 0;
  396. if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
  397. rmtadv |= ADVERTISED_Pause;
  398. if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
  399. rmtadv |= ADVERTISED_Asym_Pause;
  400. if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
  401. EFX_ERR(efx, "warning: link partner doesn't support "
  402. "pause frames");
  403. }
  404. static bool efx_mcdi_phy_poll(struct efx_nic *efx)
  405. {
  406. struct efx_link_state old_state = efx->link_state;
  407. u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
  408. int rc;
  409. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  410. BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
  411. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
  412. outbuf, sizeof(outbuf), NULL);
  413. if (rc) {
  414. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  415. efx->link_state.up = false;
  416. } else {
  417. efx_mcdi_phy_decode_link(
  418. efx, &efx->link_state,
  419. MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
  420. MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
  421. MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
  422. }
  423. return !efx_link_state_equal(&efx->link_state, &old_state);
  424. }
  425. static void efx_mcdi_phy_fini(struct efx_nic *efx)
  426. {
  427. struct efx_mcdi_phy_data *phy_data = efx->phy_data;
  428. efx->phy_data = NULL;
  429. kfree(phy_data);
  430. }
  431. static void efx_mcdi_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
  432. {
  433. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  434. u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
  435. int rc;
  436. ecmd->supported =
  437. mcdi_to_ethtool_cap(phy_cfg->media, phy_cfg->supported_cap);
  438. ecmd->advertising = efx->link_advertising;
  439. ecmd->speed = efx->link_state.speed;
  440. ecmd->duplex = efx->link_state.fd;
  441. ecmd->port = mcdi_to_ethtool_media(phy_cfg->media);
  442. ecmd->phy_address = phy_cfg->port;
  443. ecmd->transceiver = XCVR_INTERNAL;
  444. ecmd->autoneg = !!(efx->link_advertising & ADVERTISED_Autoneg);
  445. ecmd->mdio_support = (efx->mdio.mode_support &
  446. (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
  447. BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
  448. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
  449. outbuf, sizeof(outbuf), NULL);
  450. if (rc) {
  451. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  452. return;
  453. }
  454. ecmd->lp_advertising =
  455. mcdi_to_ethtool_cap(phy_cfg->media,
  456. MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP));
  457. }
  458. static int efx_mcdi_phy_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
  459. {
  460. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  461. u32 caps;
  462. int rc;
  463. if (ecmd->autoneg) {
  464. caps = (ethtool_to_mcdi_cap(ecmd->advertising) |
  465. 1 << MC_CMD_PHY_CAP_AN_LBN);
  466. } else if (ecmd->duplex) {
  467. switch (ecmd->speed) {
  468. case 10: caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN; break;
  469. case 100: caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN; break;
  470. case 1000: caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN; break;
  471. case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break;
  472. default: return -EINVAL;
  473. }
  474. } else {
  475. switch (ecmd->speed) {
  476. case 10: caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN; break;
  477. case 100: caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN; break;
  478. case 1000: caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN; break;
  479. default: return -EINVAL;
  480. }
  481. }
  482. rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
  483. efx->loopback_mode, 0);
  484. if (rc)
  485. return rc;
  486. if (ecmd->autoneg) {
  487. efx_link_set_advertising(
  488. efx, ecmd->advertising | ADVERTISED_Autoneg);
  489. phy_cfg->forced_cap = 0;
  490. } else {
  491. efx_link_set_advertising(efx, 0);
  492. phy_cfg->forced_cap = caps;
  493. }
  494. return 0;
  495. }
  496. struct efx_phy_operations efx_mcdi_phy_ops = {
  497. .probe = efx_mcdi_phy_probe,
  498. .init = efx_mcdi_phy_init,
  499. .reconfigure = efx_mcdi_phy_reconfigure,
  500. .poll = efx_mcdi_phy_poll,
  501. .fini = efx_mcdi_phy_fini,
  502. .get_settings = efx_mcdi_phy_get_settings,
  503. .set_settings = efx_mcdi_phy_set_settings,
  504. .run_tests = NULL,
  505. .test_name = NULL,
  506. };