mcdi_phy.c 21 KB

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