mcdi_phy.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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 <linux/slab.h>
  13. #include "efx.h"
  14. #include "phy.h"
  15. #include "mcdi.h"
  16. #include "mcdi_pcol.h"
  17. #include "mdio_10g.h"
  18. #include "nic.h"
  19. #include "selftest.h"
  20. struct efx_mcdi_phy_cfg {
  21. u32 flags;
  22. u32 type;
  23. u32 supported_cap;
  24. u32 channel;
  25. u32 port;
  26. u32 stats_mask;
  27. u8 name[20];
  28. u32 media;
  29. u32 mmd_mask;
  30. u8 revision[20];
  31. u32 forced_cap;
  32. };
  33. static int
  34. efx_mcdi_get_phy_cfg(struct efx_nic *efx, struct efx_mcdi_phy_cfg *cfg)
  35. {
  36. u8 outbuf[MC_CMD_GET_PHY_CFG_OUT_LEN];
  37. size_t outlen;
  38. int rc;
  39. BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_IN_LEN != 0);
  40. BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_OUT_NAME_LEN != sizeof(cfg->name));
  41. rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_CFG, NULL, 0,
  42. outbuf, sizeof(outbuf), &outlen);
  43. if (rc)
  44. goto fail;
  45. if (outlen < MC_CMD_GET_PHY_CFG_OUT_LEN) {
  46. rc = -EIO;
  47. goto fail;
  48. }
  49. cfg->flags = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_FLAGS);
  50. cfg->type = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_TYPE);
  51. cfg->supported_cap =
  52. MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_SUPPORTED_CAP);
  53. cfg->channel = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_CHANNEL);
  54. cfg->port = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_PRT);
  55. cfg->stats_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_STATS_MASK);
  56. memcpy(cfg->name, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_NAME),
  57. sizeof(cfg->name));
  58. cfg->media = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MEDIA_TYPE);
  59. cfg->mmd_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MMD_MASK);
  60. memcpy(cfg->revision, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_REVISION),
  61. sizeof(cfg->revision));
  62. return 0;
  63. fail:
  64. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  65. return rc;
  66. }
  67. static int efx_mcdi_set_link(struct efx_nic *efx, u32 capabilities,
  68. u32 flags, u32 loopback_mode,
  69. u32 loopback_speed)
  70. {
  71. u8 inbuf[MC_CMD_SET_LINK_IN_LEN];
  72. int rc;
  73. BUILD_BUG_ON(MC_CMD_SET_LINK_OUT_LEN != 0);
  74. MCDI_SET_DWORD(inbuf, SET_LINK_IN_CAP, capabilities);
  75. MCDI_SET_DWORD(inbuf, SET_LINK_IN_FLAGS, flags);
  76. MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_MODE, loopback_mode);
  77. MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_SPEED, loopback_speed);
  78. rc = efx_mcdi_rpc(efx, MC_CMD_SET_LINK, inbuf, sizeof(inbuf),
  79. NULL, 0, NULL);
  80. if (rc)
  81. goto fail;
  82. return 0;
  83. fail:
  84. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  85. return rc;
  86. }
  87. static int efx_mcdi_loopback_modes(struct efx_nic *efx, u64 *loopback_modes)
  88. {
  89. u8 outbuf[MC_CMD_GET_LOOPBACK_MODES_OUT_LEN];
  90. size_t outlen;
  91. int rc;
  92. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LOOPBACK_MODES, NULL, 0,
  93. outbuf, sizeof(outbuf), &outlen);
  94. if (rc)
  95. goto fail;
  96. if (outlen < MC_CMD_GET_LOOPBACK_MODES_OUT_LEN) {
  97. rc = -EIO;
  98. goto fail;
  99. }
  100. *loopback_modes = MCDI_QWORD(outbuf, GET_LOOPBACK_MODES_SUGGESTED);
  101. return 0;
  102. fail:
  103. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  104. return rc;
  105. }
  106. int efx_mcdi_mdio_read(struct efx_nic *efx, unsigned int bus,
  107. unsigned int prtad, unsigned int devad, u16 addr,
  108. u16 *value_out, u32 *status_out)
  109. {
  110. u8 inbuf[MC_CMD_MDIO_READ_IN_LEN];
  111. u8 outbuf[MC_CMD_MDIO_READ_OUT_LEN];
  112. size_t outlen;
  113. int rc;
  114. MCDI_SET_DWORD(inbuf, MDIO_READ_IN_BUS, bus);
  115. MCDI_SET_DWORD(inbuf, MDIO_READ_IN_PRTAD, prtad);
  116. MCDI_SET_DWORD(inbuf, MDIO_READ_IN_DEVAD, devad);
  117. MCDI_SET_DWORD(inbuf, MDIO_READ_IN_ADDR, addr);
  118. rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_READ, inbuf, sizeof(inbuf),
  119. outbuf, sizeof(outbuf), &outlen);
  120. if (rc)
  121. goto fail;
  122. *value_out = (u16)MCDI_DWORD(outbuf, MDIO_READ_OUT_VALUE);
  123. *status_out = MCDI_DWORD(outbuf, MDIO_READ_OUT_STATUS);
  124. return 0;
  125. fail:
  126. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  127. return rc;
  128. }
  129. int efx_mcdi_mdio_write(struct efx_nic *efx, unsigned int bus,
  130. unsigned int prtad, unsigned int devad, u16 addr,
  131. u16 value, u32 *status_out)
  132. {
  133. u8 inbuf[MC_CMD_MDIO_WRITE_IN_LEN];
  134. u8 outbuf[MC_CMD_MDIO_WRITE_OUT_LEN];
  135. size_t outlen;
  136. int rc;
  137. MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_BUS, bus);
  138. MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_PRTAD, prtad);
  139. MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_DEVAD, devad);
  140. MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_ADDR, addr);
  141. MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_VALUE, value);
  142. rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_WRITE, inbuf, sizeof(inbuf),
  143. outbuf, sizeof(outbuf), &outlen);
  144. if (rc)
  145. goto fail;
  146. *status_out = MCDI_DWORD(outbuf, MDIO_WRITE_OUT_STATUS);
  147. return 0;
  148. fail:
  149. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  150. return rc;
  151. }
  152. static u32 mcdi_to_ethtool_cap(u32 media, u32 cap)
  153. {
  154. u32 result = 0;
  155. switch (media) {
  156. case MC_CMD_MEDIA_KX4:
  157. result |= SUPPORTED_Backplane;
  158. if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
  159. result |= SUPPORTED_1000baseKX_Full;
  160. if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
  161. result |= SUPPORTED_10000baseKX4_Full;
  162. break;
  163. case MC_CMD_MEDIA_XFP:
  164. case MC_CMD_MEDIA_SFP_PLUS:
  165. result |= SUPPORTED_FIBRE;
  166. break;
  167. case MC_CMD_MEDIA_BASE_T:
  168. result |= SUPPORTED_TP;
  169. if (cap & (1 << MC_CMD_PHY_CAP_10HDX_LBN))
  170. result |= SUPPORTED_10baseT_Half;
  171. if (cap & (1 << MC_CMD_PHY_CAP_10FDX_LBN))
  172. result |= SUPPORTED_10baseT_Full;
  173. if (cap & (1 << MC_CMD_PHY_CAP_100HDX_LBN))
  174. result |= SUPPORTED_100baseT_Half;
  175. if (cap & (1 << MC_CMD_PHY_CAP_100FDX_LBN))
  176. result |= SUPPORTED_100baseT_Full;
  177. if (cap & (1 << MC_CMD_PHY_CAP_1000HDX_LBN))
  178. result |= SUPPORTED_1000baseT_Half;
  179. if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
  180. result |= SUPPORTED_1000baseT_Full;
  181. if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
  182. result |= SUPPORTED_10000baseT_Full;
  183. break;
  184. }
  185. if (cap & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
  186. result |= SUPPORTED_Pause;
  187. if (cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
  188. result |= SUPPORTED_Asym_Pause;
  189. if (cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
  190. result |= SUPPORTED_Autoneg;
  191. return result;
  192. }
  193. static u32 ethtool_to_mcdi_cap(u32 cap)
  194. {
  195. u32 result = 0;
  196. if (cap & SUPPORTED_10baseT_Half)
  197. result |= (1 << MC_CMD_PHY_CAP_10HDX_LBN);
  198. if (cap & SUPPORTED_10baseT_Full)
  199. result |= (1 << MC_CMD_PHY_CAP_10FDX_LBN);
  200. if (cap & SUPPORTED_100baseT_Half)
  201. result |= (1 << MC_CMD_PHY_CAP_100HDX_LBN);
  202. if (cap & SUPPORTED_100baseT_Full)
  203. result |= (1 << MC_CMD_PHY_CAP_100FDX_LBN);
  204. if (cap & SUPPORTED_1000baseT_Half)
  205. result |= (1 << MC_CMD_PHY_CAP_1000HDX_LBN);
  206. if (cap & (SUPPORTED_1000baseT_Full | SUPPORTED_1000baseKX_Full))
  207. result |= (1 << MC_CMD_PHY_CAP_1000FDX_LBN);
  208. if (cap & (SUPPORTED_10000baseT_Full | SUPPORTED_10000baseKX4_Full))
  209. result |= (1 << MC_CMD_PHY_CAP_10000FDX_LBN);
  210. if (cap & SUPPORTED_Pause)
  211. result |= (1 << MC_CMD_PHY_CAP_PAUSE_LBN);
  212. if (cap & SUPPORTED_Asym_Pause)
  213. result |= (1 << MC_CMD_PHY_CAP_ASYM_LBN);
  214. if (cap & SUPPORTED_Autoneg)
  215. result |= (1 << MC_CMD_PHY_CAP_AN_LBN);
  216. return result;
  217. }
  218. static u32 efx_get_mcdi_phy_flags(struct efx_nic *efx)
  219. {
  220. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  221. enum efx_phy_mode mode, supported;
  222. u32 flags;
  223. /* TODO: Advertise the capabilities supported by this PHY */
  224. supported = 0;
  225. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_TXDIS_LBN))
  226. supported |= PHY_MODE_TX_DISABLED;
  227. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_LOWPOWER_LBN))
  228. supported |= PHY_MODE_LOW_POWER;
  229. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_POWEROFF_LBN))
  230. supported |= PHY_MODE_OFF;
  231. mode = efx->phy_mode & supported;
  232. flags = 0;
  233. if (mode & PHY_MODE_TX_DISABLED)
  234. flags |= (1 << MC_CMD_SET_LINK_TXDIS_LBN);
  235. if (mode & PHY_MODE_LOW_POWER)
  236. flags |= (1 << MC_CMD_SET_LINK_LOWPOWER_LBN);
  237. if (mode & PHY_MODE_OFF)
  238. flags |= (1 << MC_CMD_SET_LINK_POWEROFF_LBN);
  239. return flags;
  240. }
  241. static u32 mcdi_to_ethtool_media(u32 media)
  242. {
  243. switch (media) {
  244. case MC_CMD_MEDIA_XAUI:
  245. case MC_CMD_MEDIA_CX4:
  246. case MC_CMD_MEDIA_KX4:
  247. return PORT_OTHER;
  248. case MC_CMD_MEDIA_XFP:
  249. case MC_CMD_MEDIA_SFP_PLUS:
  250. return PORT_FIBRE;
  251. case MC_CMD_MEDIA_BASE_T:
  252. return PORT_TP;
  253. default:
  254. return PORT_OTHER;
  255. }
  256. }
  257. static int efx_mcdi_phy_probe(struct efx_nic *efx)
  258. {
  259. struct efx_mcdi_phy_cfg *phy_data;
  260. u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
  261. u32 caps;
  262. int rc;
  263. /* Initialise and populate phy_data */
  264. phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
  265. if (phy_data == NULL)
  266. return -ENOMEM;
  267. rc = efx_mcdi_get_phy_cfg(efx, phy_data);
  268. if (rc != 0)
  269. goto fail;
  270. /* Read initial link advertisement */
  271. BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
  272. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
  273. outbuf, sizeof(outbuf), NULL);
  274. if (rc)
  275. goto fail;
  276. /* Fill out nic state */
  277. efx->phy_data = phy_data;
  278. efx->phy_type = phy_data->type;
  279. efx->mdio_bus = phy_data->channel;
  280. efx->mdio.prtad = phy_data->port;
  281. efx->mdio.mmds = phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22);
  282. efx->mdio.mode_support = 0;
  283. if (phy_data->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
  284. efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
  285. if (phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
  286. efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
  287. caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP);
  288. if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN))
  289. efx->link_advertising =
  290. mcdi_to_ethtool_cap(phy_data->media, caps);
  291. else
  292. phy_data->forced_cap = caps;
  293. /* Assert that we can map efx -> mcdi loopback modes */
  294. BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE);
  295. BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA);
  296. BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC);
  297. BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII);
  298. BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS);
  299. BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI);
  300. BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII);
  301. BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII);
  302. BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR);
  303. BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI);
  304. BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR);
  305. BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR);
  306. BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR);
  307. BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR);
  308. BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY);
  309. BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS);
  310. BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS);
  311. BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD);
  312. BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT);
  313. BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS);
  314. BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS);
  315. BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR);
  316. BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR);
  317. BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS);
  318. BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS);
  319. BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR);
  320. BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS);
  321. rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes);
  322. if (rc != 0)
  323. goto fail;
  324. /* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
  325. * but by convention we don't */
  326. efx->loopback_modes &= ~(1 << LOOPBACK_NONE);
  327. /* Set the initial link mode */
  328. efx_mcdi_phy_decode_link(
  329. efx, &efx->link_state,
  330. MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
  331. MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
  332. MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
  333. /* Default to Autonegotiated flow control if the PHY supports it */
  334. efx->wanted_fc = EFX_FC_RX | EFX_FC_TX;
  335. if (phy_data->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
  336. efx->wanted_fc |= EFX_FC_AUTO;
  337. return 0;
  338. fail:
  339. kfree(phy_data);
  340. return rc;
  341. }
  342. int efx_mcdi_phy_reconfigure(struct efx_nic *efx)
  343. {
  344. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  345. u32 caps = (efx->link_advertising ?
  346. ethtool_to_mcdi_cap(efx->link_advertising) :
  347. phy_cfg->forced_cap);
  348. return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
  349. efx->loopback_mode, 0);
  350. }
  351. void efx_mcdi_phy_decode_link(struct efx_nic *efx,
  352. struct efx_link_state *link_state,
  353. u32 speed, u32 flags, u32 fcntl)
  354. {
  355. switch (fcntl) {
  356. case MC_CMD_FCNTL_AUTO:
  357. WARN_ON(1); /* This is not a link mode */
  358. link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
  359. break;
  360. case MC_CMD_FCNTL_BIDIR:
  361. link_state->fc = EFX_FC_TX | EFX_FC_RX;
  362. break;
  363. case MC_CMD_FCNTL_RESPOND:
  364. link_state->fc = EFX_FC_RX;
  365. break;
  366. default:
  367. WARN_ON(1);
  368. case MC_CMD_FCNTL_OFF:
  369. link_state->fc = 0;
  370. break;
  371. }
  372. link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_LINK_UP_LBN));
  373. link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_FULL_DUPLEX_LBN));
  374. link_state->speed = speed;
  375. }
  376. /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
  377. * supported by the link partner. Warn the user if this isn't the case
  378. */
  379. void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
  380. {
  381. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  382. u32 rmtadv;
  383. /* The link partner capabilities are only relevent if the
  384. * link supports flow control autonegotiation */
  385. if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
  386. return;
  387. /* If flow control autoneg is supported and enabled, then fine */
  388. if (efx->wanted_fc & EFX_FC_AUTO)
  389. return;
  390. rmtadv = 0;
  391. if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
  392. rmtadv |= ADVERTISED_Pause;
  393. if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
  394. rmtadv |= ADVERTISED_Asym_Pause;
  395. if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
  396. EFX_ERR(efx, "warning: link partner doesn't support "
  397. "pause frames");
  398. }
  399. static bool efx_mcdi_phy_poll(struct efx_nic *efx)
  400. {
  401. struct efx_link_state old_state = efx->link_state;
  402. u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
  403. int rc;
  404. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  405. BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
  406. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
  407. outbuf, sizeof(outbuf), NULL);
  408. if (rc) {
  409. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  410. efx->link_state.up = false;
  411. } else {
  412. efx_mcdi_phy_decode_link(
  413. efx, &efx->link_state,
  414. MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
  415. MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
  416. MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
  417. }
  418. return !efx_link_state_equal(&efx->link_state, &old_state);
  419. }
  420. static void efx_mcdi_phy_remove(struct efx_nic *efx)
  421. {
  422. struct efx_mcdi_phy_data *phy_data = efx->phy_data;
  423. efx->phy_data = NULL;
  424. kfree(phy_data);
  425. }
  426. static void efx_mcdi_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
  427. {
  428. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  429. u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
  430. int rc;
  431. ecmd->supported =
  432. mcdi_to_ethtool_cap(phy_cfg->media, phy_cfg->supported_cap);
  433. ecmd->advertising = efx->link_advertising;
  434. ecmd->speed = efx->link_state.speed;
  435. ecmd->duplex = efx->link_state.fd;
  436. ecmd->port = mcdi_to_ethtool_media(phy_cfg->media);
  437. ecmd->phy_address = phy_cfg->port;
  438. ecmd->transceiver = XCVR_INTERNAL;
  439. ecmd->autoneg = !!(efx->link_advertising & ADVERTISED_Autoneg);
  440. ecmd->mdio_support = (efx->mdio.mode_support &
  441. (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
  442. BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
  443. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
  444. outbuf, sizeof(outbuf), NULL);
  445. if (rc) {
  446. EFX_ERR(efx, "%s: failed rc=%d\n", __func__, rc);
  447. return;
  448. }
  449. ecmd->lp_advertising =
  450. mcdi_to_ethtool_cap(phy_cfg->media,
  451. MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP));
  452. }
  453. static int efx_mcdi_phy_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
  454. {
  455. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  456. u32 caps;
  457. int rc;
  458. if (ecmd->autoneg) {
  459. caps = (ethtool_to_mcdi_cap(ecmd->advertising) |
  460. 1 << MC_CMD_PHY_CAP_AN_LBN);
  461. } else if (ecmd->duplex) {
  462. switch (ecmd->speed) {
  463. case 10: caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN; break;
  464. case 100: caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN; break;
  465. case 1000: caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN; break;
  466. case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break;
  467. default: return -EINVAL;
  468. }
  469. } else {
  470. switch (ecmd->speed) {
  471. case 10: caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN; break;
  472. case 100: caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN; break;
  473. case 1000: caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN; break;
  474. default: return -EINVAL;
  475. }
  476. }
  477. rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
  478. efx->loopback_mode, 0);
  479. if (rc)
  480. return rc;
  481. if (ecmd->autoneg) {
  482. efx_link_set_advertising(
  483. efx, ecmd->advertising | ADVERTISED_Autoneg);
  484. phy_cfg->forced_cap = 0;
  485. } else {
  486. efx_link_set_advertising(efx, 0);
  487. phy_cfg->forced_cap = caps;
  488. }
  489. return 0;
  490. }
  491. static int efx_mcdi_phy_test_alive(struct efx_nic *efx)
  492. {
  493. u8 outbuf[MC_CMD_GET_PHY_STATE_OUT_LEN];
  494. size_t outlen;
  495. int rc;
  496. BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN != 0);
  497. rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_STATE, NULL, 0,
  498. outbuf, sizeof(outbuf), &outlen);
  499. if (rc)
  500. return rc;
  501. if (outlen < MC_CMD_GET_PHY_STATE_OUT_LEN)
  502. return -EIO;
  503. if (MCDI_DWORD(outbuf, GET_PHY_STATE_STATE) != MC_CMD_PHY_STATE_OK)
  504. return -EINVAL;
  505. return 0;
  506. }
  507. static const char *const mcdi_sft9001_cable_diag_names[] = {
  508. "cable.pairA.length",
  509. "cable.pairB.length",
  510. "cable.pairC.length",
  511. "cable.pairD.length",
  512. "cable.pairA.status",
  513. "cable.pairB.status",
  514. "cable.pairC.status",
  515. "cable.pairD.status",
  516. };
  517. static int efx_mcdi_bist(struct efx_nic *efx, unsigned int bist_mode,
  518. int *results)
  519. {
  520. unsigned int retry, i, count = 0;
  521. size_t outlen;
  522. u32 status;
  523. u8 *buf, *ptr;
  524. int rc;
  525. buf = kzalloc(0x100, GFP_KERNEL);
  526. if (buf == NULL)
  527. return -ENOMEM;
  528. BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN != 0);
  529. MCDI_SET_DWORD(buf, START_BIST_IN_TYPE, bist_mode);
  530. rc = efx_mcdi_rpc(efx, MC_CMD_START_BIST, buf, MC_CMD_START_BIST_IN_LEN,
  531. NULL, 0, NULL);
  532. if (rc)
  533. goto out;
  534. /* Wait up to 10s for BIST to finish */
  535. for (retry = 0; retry < 100; ++retry) {
  536. BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN != 0);
  537. rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
  538. buf, 0x100, &outlen);
  539. if (rc)
  540. goto out;
  541. status = MCDI_DWORD(buf, POLL_BIST_OUT_RESULT);
  542. if (status != MC_CMD_POLL_BIST_RUNNING)
  543. goto finished;
  544. msleep(100);
  545. }
  546. rc = -ETIMEDOUT;
  547. goto out;
  548. finished:
  549. results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1;
  550. /* SFT9001 specific cable diagnostics output */
  551. if (efx->phy_type == PHY_TYPE_SFT9001B &&
  552. (bist_mode == MC_CMD_PHY_BIST_CABLE_SHORT ||
  553. bist_mode == MC_CMD_PHY_BIST_CABLE_LONG)) {
  554. ptr = MCDI_PTR(buf, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A);
  555. if (status == MC_CMD_POLL_BIST_PASSED &&
  556. outlen >= MC_CMD_POLL_BIST_OUT_SFT9001_LEN) {
  557. for (i = 0; i < 8; i++) {
  558. results[count + i] =
  559. EFX_DWORD_FIELD(((efx_dword_t *)ptr)[i],
  560. EFX_DWORD_0);
  561. }
  562. }
  563. count += 8;
  564. }
  565. rc = count;
  566. out:
  567. kfree(buf);
  568. return rc;
  569. }
  570. static int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results,
  571. unsigned flags)
  572. {
  573. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  574. u32 mode;
  575. int rc;
  576. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_LBN)) {
  577. rc = efx_mcdi_bist(efx, MC_CMD_PHY_BIST, results);
  578. if (rc < 0)
  579. return rc;
  580. results += rc;
  581. }
  582. /* If we support both LONG and SHORT, then run each in response to
  583. * break or not. Otherwise, run the one we support */
  584. mode = 0;
  585. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_SHORT_LBN)) {
  586. if ((flags & ETH_TEST_FL_OFFLINE) &&
  587. (phy_cfg->flags &
  588. (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN)))
  589. mode = MC_CMD_PHY_BIST_CABLE_LONG;
  590. else
  591. mode = MC_CMD_PHY_BIST_CABLE_SHORT;
  592. } else if (phy_cfg->flags &
  593. (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN))
  594. mode = MC_CMD_PHY_BIST_CABLE_LONG;
  595. if (mode != 0) {
  596. rc = efx_mcdi_bist(efx, mode, results);
  597. if (rc < 0)
  598. return rc;
  599. results += rc;
  600. }
  601. return 0;
  602. }
  603. const char *efx_mcdi_phy_test_name(struct efx_nic *efx, unsigned int index)
  604. {
  605. struct efx_mcdi_phy_cfg *phy_cfg = efx->phy_data;
  606. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_LBN)) {
  607. if (index == 0)
  608. return "bist";
  609. --index;
  610. }
  611. if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_SHORT_LBN) |
  612. (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN))) {
  613. if (index == 0)
  614. return "cable";
  615. --index;
  616. if (efx->phy_type == PHY_TYPE_SFT9001B) {
  617. if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names))
  618. return mcdi_sft9001_cable_diag_names[index];
  619. index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names);
  620. }
  621. }
  622. return NULL;
  623. }
  624. struct efx_phy_operations efx_mcdi_phy_ops = {
  625. .probe = efx_mcdi_phy_probe,
  626. .init = efx_port_dummy_op_int,
  627. .reconfigure = efx_mcdi_phy_reconfigure,
  628. .poll = efx_mcdi_phy_poll,
  629. .fini = efx_port_dummy_op_void,
  630. .remove = efx_mcdi_phy_remove,
  631. .get_settings = efx_mcdi_phy_get_settings,
  632. .set_settings = efx_mcdi_phy_set_settings,
  633. .test_alive = efx_mcdi_phy_test_alive,
  634. .run_tests = efx_mcdi_phy_run_tests,
  635. .test_name = efx_mcdi_phy_test_name,
  636. };