mcdi_phy.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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_data {
  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_data *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. netif_err(efx, hw, efx->net_dev, "%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. netif_err(efx, hw, efx->net_dev, "%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. netif_err(efx, hw, efx->net_dev, "%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. netif_err(efx, hw, efx->net_dev, "%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. netif_err(efx, hw, efx->net_dev, "%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_data *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_data *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. efx_link_set_wanted_fc(efx, efx->wanted_fc);
  338. return 0;
  339. fail:
  340. kfree(phy_data);
  341. return rc;
  342. }
  343. int efx_mcdi_phy_reconfigure(struct efx_nic *efx)
  344. {
  345. struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
  346. u32 caps = (efx->link_advertising ?
  347. ethtool_to_mcdi_cap(efx->link_advertising) :
  348. phy_cfg->forced_cap);
  349. return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
  350. efx->loopback_mode, 0);
  351. }
  352. void efx_mcdi_phy_decode_link(struct efx_nic *efx,
  353. struct efx_link_state *link_state,
  354. u32 speed, u32 flags, u32 fcntl)
  355. {
  356. switch (fcntl) {
  357. case MC_CMD_FCNTL_AUTO:
  358. WARN_ON(1); /* This is not a link mode */
  359. link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
  360. break;
  361. case MC_CMD_FCNTL_BIDIR:
  362. link_state->fc = EFX_FC_TX | EFX_FC_RX;
  363. break;
  364. case MC_CMD_FCNTL_RESPOND:
  365. link_state->fc = EFX_FC_RX;
  366. break;
  367. default:
  368. WARN_ON(1);
  369. case MC_CMD_FCNTL_OFF:
  370. link_state->fc = 0;
  371. break;
  372. }
  373. link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_LINK_UP_LBN));
  374. link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_FULL_DUPLEX_LBN));
  375. link_state->speed = speed;
  376. }
  377. /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
  378. * supported by the link partner. Warn the user if this isn't the case
  379. */
  380. void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
  381. {
  382. struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
  383. u32 rmtadv;
  384. /* The link partner capabilities are only relevent if the
  385. * link supports flow control autonegotiation */
  386. if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
  387. return;
  388. /* If flow control autoneg is supported and enabled, then fine */
  389. if (efx->wanted_fc & EFX_FC_AUTO)
  390. return;
  391. rmtadv = 0;
  392. if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
  393. rmtadv |= ADVERTISED_Pause;
  394. if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
  395. rmtadv |= ADVERTISED_Asym_Pause;
  396. if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
  397. netif_err(efx, link, efx->net_dev,
  398. "warning: link partner doesn't support pause frames");
  399. }
  400. static bool efx_mcdi_phy_poll(struct efx_nic *efx)
  401. {
  402. struct efx_link_state old_state = efx->link_state;
  403. u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
  404. int rc;
  405. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  406. BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
  407. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
  408. outbuf, sizeof(outbuf), NULL);
  409. if (rc) {
  410. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
  411. __func__, rc);
  412. efx->link_state.up = false;
  413. } else {
  414. efx_mcdi_phy_decode_link(
  415. efx, &efx->link_state,
  416. MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
  417. MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
  418. MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
  419. }
  420. return !efx_link_state_equal(&efx->link_state, &old_state);
  421. }
  422. static void efx_mcdi_phy_remove(struct efx_nic *efx)
  423. {
  424. struct efx_mcdi_phy_data *phy_data = efx->phy_data;
  425. efx->phy_data = NULL;
  426. kfree(phy_data);
  427. }
  428. static void efx_mcdi_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
  429. {
  430. struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
  431. u8 outbuf[MC_CMD_GET_LINK_OUT_LEN];
  432. int rc;
  433. ecmd->supported =
  434. mcdi_to_ethtool_cap(phy_cfg->media, phy_cfg->supported_cap);
  435. ecmd->advertising = efx->link_advertising;
  436. ecmd->speed = efx->link_state.speed;
  437. ecmd->duplex = efx->link_state.fd;
  438. ecmd->port = mcdi_to_ethtool_media(phy_cfg->media);
  439. ecmd->phy_address = phy_cfg->port;
  440. ecmd->transceiver = XCVR_INTERNAL;
  441. ecmd->autoneg = !!(efx->link_advertising & ADVERTISED_Autoneg);
  442. ecmd->mdio_support = (efx->mdio.mode_support &
  443. (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22));
  444. BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
  445. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
  446. outbuf, sizeof(outbuf), NULL);
  447. if (rc) {
  448. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n",
  449. __func__, rc);
  450. return;
  451. }
  452. ecmd->lp_advertising =
  453. mcdi_to_ethtool_cap(phy_cfg->media,
  454. MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP));
  455. }
  456. static int efx_mcdi_phy_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
  457. {
  458. struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
  459. u32 caps;
  460. int rc;
  461. if (ecmd->autoneg) {
  462. caps = (ethtool_to_mcdi_cap(ecmd->advertising) |
  463. 1 << MC_CMD_PHY_CAP_AN_LBN);
  464. } else if (ecmd->duplex) {
  465. switch (ecmd->speed) {
  466. case 10: caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN; break;
  467. case 100: caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN; break;
  468. case 1000: caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN; break;
  469. case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break;
  470. default: return -EINVAL;
  471. }
  472. } else {
  473. switch (ecmd->speed) {
  474. case 10: caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN; break;
  475. case 100: caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN; break;
  476. case 1000: caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN; break;
  477. default: return -EINVAL;
  478. }
  479. }
  480. rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
  481. efx->loopback_mode, 0);
  482. if (rc)
  483. return rc;
  484. if (ecmd->autoneg) {
  485. efx_link_set_advertising(
  486. efx, ecmd->advertising | ADVERTISED_Autoneg);
  487. phy_cfg->forced_cap = 0;
  488. } else {
  489. efx_link_set_advertising(efx, 0);
  490. phy_cfg->forced_cap = caps;
  491. }
  492. return 0;
  493. }
  494. static int efx_mcdi_phy_test_alive(struct efx_nic *efx)
  495. {
  496. u8 outbuf[MC_CMD_GET_PHY_STATE_OUT_LEN];
  497. size_t outlen;
  498. int rc;
  499. BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN != 0);
  500. rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_STATE, NULL, 0,
  501. outbuf, sizeof(outbuf), &outlen);
  502. if (rc)
  503. return rc;
  504. if (outlen < MC_CMD_GET_PHY_STATE_OUT_LEN)
  505. return -EIO;
  506. if (MCDI_DWORD(outbuf, GET_PHY_STATE_STATE) != MC_CMD_PHY_STATE_OK)
  507. return -EINVAL;
  508. return 0;
  509. }
  510. static const char *const mcdi_sft9001_cable_diag_names[] = {
  511. "cable.pairA.length",
  512. "cable.pairB.length",
  513. "cable.pairC.length",
  514. "cable.pairD.length",
  515. "cable.pairA.status",
  516. "cable.pairB.status",
  517. "cable.pairC.status",
  518. "cable.pairD.status",
  519. };
  520. static int efx_mcdi_bist(struct efx_nic *efx, unsigned int bist_mode,
  521. int *results)
  522. {
  523. unsigned int retry, i, count = 0;
  524. size_t outlen;
  525. u32 status;
  526. u8 *buf, *ptr;
  527. int rc;
  528. buf = kzalloc(0x100, GFP_KERNEL);
  529. if (buf == NULL)
  530. return -ENOMEM;
  531. BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN != 0);
  532. MCDI_SET_DWORD(buf, START_BIST_IN_TYPE, bist_mode);
  533. rc = efx_mcdi_rpc(efx, MC_CMD_START_BIST, buf, MC_CMD_START_BIST_IN_LEN,
  534. NULL, 0, NULL);
  535. if (rc)
  536. goto out;
  537. /* Wait up to 10s for BIST to finish */
  538. for (retry = 0; retry < 100; ++retry) {
  539. BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN != 0);
  540. rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
  541. buf, 0x100, &outlen);
  542. if (rc)
  543. goto out;
  544. status = MCDI_DWORD(buf, POLL_BIST_OUT_RESULT);
  545. if (status != MC_CMD_POLL_BIST_RUNNING)
  546. goto finished;
  547. msleep(100);
  548. }
  549. rc = -ETIMEDOUT;
  550. goto out;
  551. finished:
  552. results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1;
  553. /* SFT9001 specific cable diagnostics output */
  554. if (efx->phy_type == PHY_TYPE_SFT9001B &&
  555. (bist_mode == MC_CMD_PHY_BIST_CABLE_SHORT ||
  556. bist_mode == MC_CMD_PHY_BIST_CABLE_LONG)) {
  557. ptr = MCDI_PTR(buf, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A);
  558. if (status == MC_CMD_POLL_BIST_PASSED &&
  559. outlen >= MC_CMD_POLL_BIST_OUT_SFT9001_LEN) {
  560. for (i = 0; i < 8; i++) {
  561. results[count + i] =
  562. EFX_DWORD_FIELD(((efx_dword_t *)ptr)[i],
  563. EFX_DWORD_0);
  564. }
  565. }
  566. count += 8;
  567. }
  568. rc = count;
  569. out:
  570. kfree(buf);
  571. return rc;
  572. }
  573. static int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results,
  574. unsigned flags)
  575. {
  576. struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
  577. u32 mode;
  578. int rc;
  579. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_LBN)) {
  580. rc = efx_mcdi_bist(efx, MC_CMD_PHY_BIST, results);
  581. if (rc < 0)
  582. return rc;
  583. results += rc;
  584. }
  585. /* If we support both LONG and SHORT, then run each in response to
  586. * break or not. Otherwise, run the one we support */
  587. mode = 0;
  588. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_SHORT_LBN)) {
  589. if ((flags & ETH_TEST_FL_OFFLINE) &&
  590. (phy_cfg->flags &
  591. (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN)))
  592. mode = MC_CMD_PHY_BIST_CABLE_LONG;
  593. else
  594. mode = MC_CMD_PHY_BIST_CABLE_SHORT;
  595. } else if (phy_cfg->flags &
  596. (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN))
  597. mode = MC_CMD_PHY_BIST_CABLE_LONG;
  598. if (mode != 0) {
  599. rc = efx_mcdi_bist(efx, mode, results);
  600. if (rc < 0)
  601. return rc;
  602. results += rc;
  603. }
  604. return 0;
  605. }
  606. const char *efx_mcdi_phy_test_name(struct efx_nic *efx, unsigned int index)
  607. {
  608. struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
  609. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_BIST_LBN)) {
  610. if (index == 0)
  611. return "bist";
  612. --index;
  613. }
  614. if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_SHORT_LBN) |
  615. (1 << MC_CMD_GET_PHY_CFG_BIST_CABLE_LONG_LBN))) {
  616. if (index == 0)
  617. return "cable";
  618. --index;
  619. if (efx->phy_type == PHY_TYPE_SFT9001B) {
  620. if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names))
  621. return mcdi_sft9001_cable_diag_names[index];
  622. index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names);
  623. }
  624. }
  625. return NULL;
  626. }
  627. struct efx_phy_operations efx_mcdi_phy_ops = {
  628. .probe = efx_mcdi_phy_probe,
  629. .init = efx_port_dummy_op_int,
  630. .reconfigure = efx_mcdi_phy_reconfigure,
  631. .poll = efx_mcdi_phy_poll,
  632. .fini = efx_port_dummy_op_void,
  633. .remove = efx_mcdi_phy_remove,
  634. .get_settings = efx_mcdi_phy_get_settings,
  635. .set_settings = efx_mcdi_phy_set_settings,
  636. .test_alive = efx_mcdi_phy_test_alive,
  637. .run_tests = efx_mcdi_phy_run_tests,
  638. .test_name = efx_mcdi_phy_test_name,
  639. };