mcdi_port.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2009-2013 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. MCDI_DECLARE_BUF(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. MCDI_DECLARE_BUF(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. return rc;
  80. }
  81. static int efx_mcdi_loopback_modes(struct efx_nic *efx, u64 *loopback_modes)
  82. {
  83. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LOOPBACK_MODES_OUT_LEN);
  84. size_t outlen;
  85. int rc;
  86. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LOOPBACK_MODES, NULL, 0,
  87. outbuf, sizeof(outbuf), &outlen);
  88. if (rc)
  89. goto fail;
  90. if (outlen < (MC_CMD_GET_LOOPBACK_MODES_OUT_SUGGESTED_OFST +
  91. MC_CMD_GET_LOOPBACK_MODES_OUT_SUGGESTED_LEN)) {
  92. rc = -EIO;
  93. goto fail;
  94. }
  95. *loopback_modes = MCDI_QWORD(outbuf, GET_LOOPBACK_MODES_OUT_SUGGESTED);
  96. return 0;
  97. fail:
  98. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  99. return rc;
  100. }
  101. static int efx_mcdi_mdio_read(struct net_device *net_dev,
  102. int prtad, int devad, u16 addr)
  103. {
  104. struct efx_nic *efx = netdev_priv(net_dev);
  105. MCDI_DECLARE_BUF(inbuf, MC_CMD_MDIO_READ_IN_LEN);
  106. MCDI_DECLARE_BUF(outbuf, MC_CMD_MDIO_READ_OUT_LEN);
  107. size_t outlen;
  108. int rc;
  109. MCDI_SET_DWORD(inbuf, MDIO_READ_IN_BUS, efx->mdio_bus);
  110. MCDI_SET_DWORD(inbuf, MDIO_READ_IN_PRTAD, prtad);
  111. MCDI_SET_DWORD(inbuf, MDIO_READ_IN_DEVAD, devad);
  112. MCDI_SET_DWORD(inbuf, MDIO_READ_IN_ADDR, addr);
  113. rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_READ, inbuf, sizeof(inbuf),
  114. outbuf, sizeof(outbuf), &outlen);
  115. if (rc)
  116. return rc;
  117. if (MCDI_DWORD(outbuf, MDIO_READ_OUT_STATUS) !=
  118. MC_CMD_MDIO_STATUS_GOOD)
  119. return -EIO;
  120. return (u16)MCDI_DWORD(outbuf, MDIO_READ_OUT_VALUE);
  121. }
  122. static int efx_mcdi_mdio_write(struct net_device *net_dev,
  123. int prtad, int devad, u16 addr, u16 value)
  124. {
  125. struct efx_nic *efx = netdev_priv(net_dev);
  126. MCDI_DECLARE_BUF(inbuf, MC_CMD_MDIO_WRITE_IN_LEN);
  127. MCDI_DECLARE_BUF(outbuf, MC_CMD_MDIO_WRITE_OUT_LEN);
  128. size_t outlen;
  129. int rc;
  130. MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_BUS, efx->mdio_bus);
  131. MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_PRTAD, prtad);
  132. MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_DEVAD, devad);
  133. MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_ADDR, addr);
  134. MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_VALUE, value);
  135. rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_WRITE, inbuf, sizeof(inbuf),
  136. outbuf, sizeof(outbuf), &outlen);
  137. if (rc)
  138. return rc;
  139. if (MCDI_DWORD(outbuf, MDIO_WRITE_OUT_STATUS) !=
  140. MC_CMD_MDIO_STATUS_GOOD)
  141. return -EIO;
  142. return 0;
  143. }
  144. static u32 mcdi_to_ethtool_cap(u32 media, u32 cap)
  145. {
  146. u32 result = 0;
  147. switch (media) {
  148. case MC_CMD_MEDIA_KX4:
  149. result |= SUPPORTED_Backplane;
  150. if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
  151. result |= SUPPORTED_1000baseKX_Full;
  152. if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
  153. result |= SUPPORTED_10000baseKX4_Full;
  154. if (cap & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
  155. result |= SUPPORTED_40000baseKR4_Full;
  156. break;
  157. case MC_CMD_MEDIA_XFP:
  158. case MC_CMD_MEDIA_SFP_PLUS:
  159. result |= SUPPORTED_FIBRE;
  160. break;
  161. case MC_CMD_MEDIA_QSFP_PLUS:
  162. result |= SUPPORTED_FIBRE;
  163. if (cap & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
  164. result |= SUPPORTED_40000baseCR4_Full;
  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_40000baseCR4_Full | SUPPORTED_40000baseKR4_Full))
  210. result |= (1 << MC_CMD_PHY_CAP_40000FDX_LBN);
  211. if (cap & SUPPORTED_Pause)
  212. result |= (1 << MC_CMD_PHY_CAP_PAUSE_LBN);
  213. if (cap & SUPPORTED_Asym_Pause)
  214. result |= (1 << MC_CMD_PHY_CAP_ASYM_LBN);
  215. if (cap & SUPPORTED_Autoneg)
  216. result |= (1 << MC_CMD_PHY_CAP_AN_LBN);
  217. return result;
  218. }
  219. static u32 efx_get_mcdi_phy_flags(struct efx_nic *efx)
  220. {
  221. struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
  222. enum efx_phy_mode mode, supported;
  223. u32 flags;
  224. /* TODO: Advertise the capabilities supported by this PHY */
  225. supported = 0;
  226. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_TXDIS_LBN))
  227. supported |= PHY_MODE_TX_DISABLED;
  228. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_LOWPOWER_LBN))
  229. supported |= PHY_MODE_LOW_POWER;
  230. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_POWEROFF_LBN))
  231. supported |= PHY_MODE_OFF;
  232. mode = efx->phy_mode & supported;
  233. flags = 0;
  234. if (mode & PHY_MODE_TX_DISABLED)
  235. flags |= (1 << MC_CMD_SET_LINK_IN_TXDIS_LBN);
  236. if (mode & PHY_MODE_LOW_POWER)
  237. flags |= (1 << MC_CMD_SET_LINK_IN_LOWPOWER_LBN);
  238. if (mode & PHY_MODE_OFF)
  239. flags |= (1 << MC_CMD_SET_LINK_IN_POWEROFF_LBN);
  240. return flags;
  241. }
  242. static u32 mcdi_to_ethtool_media(u32 media)
  243. {
  244. switch (media) {
  245. case MC_CMD_MEDIA_XAUI:
  246. case MC_CMD_MEDIA_CX4:
  247. case MC_CMD_MEDIA_KX4:
  248. return PORT_OTHER;
  249. case MC_CMD_MEDIA_XFP:
  250. case MC_CMD_MEDIA_SFP_PLUS:
  251. case MC_CMD_MEDIA_QSFP_PLUS:
  252. return PORT_FIBRE;
  253. case MC_CMD_MEDIA_BASE_T:
  254. return PORT_TP;
  255. default:
  256. return PORT_OTHER;
  257. }
  258. }
  259. static void efx_mcdi_phy_decode_link(struct efx_nic *efx,
  260. struct efx_link_state *link_state,
  261. u32 speed, u32 flags, u32 fcntl)
  262. {
  263. switch (fcntl) {
  264. case MC_CMD_FCNTL_AUTO:
  265. WARN_ON(1); /* This is not a link mode */
  266. link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX;
  267. break;
  268. case MC_CMD_FCNTL_BIDIR:
  269. link_state->fc = EFX_FC_TX | EFX_FC_RX;
  270. break;
  271. case MC_CMD_FCNTL_RESPOND:
  272. link_state->fc = EFX_FC_RX;
  273. break;
  274. default:
  275. WARN_ON(1);
  276. case MC_CMD_FCNTL_OFF:
  277. link_state->fc = 0;
  278. break;
  279. }
  280. link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_OUT_LINK_UP_LBN));
  281. link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_OUT_FULL_DUPLEX_LBN));
  282. link_state->speed = speed;
  283. }
  284. static int efx_mcdi_phy_probe(struct efx_nic *efx)
  285. {
  286. struct efx_mcdi_phy_data *phy_data;
  287. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
  288. u32 caps;
  289. int rc;
  290. /* Initialise and populate phy_data */
  291. phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
  292. if (phy_data == NULL)
  293. return -ENOMEM;
  294. rc = efx_mcdi_get_phy_cfg(efx, phy_data);
  295. if (rc != 0)
  296. goto fail;
  297. /* Read initial link advertisement */
  298. BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
  299. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
  300. outbuf, sizeof(outbuf), NULL);
  301. if (rc)
  302. goto fail;
  303. /* Fill out nic state */
  304. efx->phy_data = phy_data;
  305. efx->phy_type = phy_data->type;
  306. efx->mdio_bus = phy_data->channel;
  307. efx->mdio.prtad = phy_data->port;
  308. efx->mdio.mmds = phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22);
  309. efx->mdio.mode_support = 0;
  310. if (phy_data->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22))
  311. efx->mdio.mode_support |= MDIO_SUPPORTS_C22;
  312. if (phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22))
  313. efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
  314. caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP);
  315. if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN))
  316. efx->link_advertising =
  317. mcdi_to_ethtool_cap(phy_data->media, caps);
  318. else
  319. phy_data->forced_cap = caps;
  320. /* Assert that we can map efx -> mcdi loopback modes */
  321. BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE);
  322. BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA);
  323. BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC);
  324. BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII);
  325. BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS);
  326. BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI);
  327. BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII);
  328. BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII);
  329. BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR);
  330. BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI);
  331. BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR);
  332. BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR);
  333. BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR);
  334. BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR);
  335. BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY);
  336. BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS);
  337. BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS);
  338. BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD);
  339. BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT);
  340. BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS);
  341. BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS);
  342. BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR);
  343. BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR);
  344. BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS);
  345. BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS);
  346. BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR);
  347. BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS);
  348. rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes);
  349. if (rc != 0)
  350. goto fail;
  351. /* The MC indicates that LOOPBACK_NONE is a valid loopback mode,
  352. * but by convention we don't */
  353. efx->loopback_modes &= ~(1 << LOOPBACK_NONE);
  354. /* Set the initial link mode */
  355. efx_mcdi_phy_decode_link(
  356. efx, &efx->link_state,
  357. MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED),
  358. MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS),
  359. MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL));
  360. /* Default to Autonegotiated flow control if the PHY supports it */
  361. efx->wanted_fc = EFX_FC_RX | EFX_FC_TX;
  362. if (phy_data->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
  363. efx->wanted_fc |= EFX_FC_AUTO;
  364. efx_link_set_wanted_fc(efx, efx->wanted_fc);
  365. return 0;
  366. fail:
  367. kfree(phy_data);
  368. return rc;
  369. }
  370. int efx_mcdi_port_reconfigure(struct efx_nic *efx)
  371. {
  372. struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
  373. u32 caps = (efx->link_advertising ?
  374. ethtool_to_mcdi_cap(efx->link_advertising) :
  375. phy_cfg->forced_cap);
  376. return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx),
  377. efx->loopback_mode, 0);
  378. }
  379. /* Verify that the forced flow control settings (!EFX_FC_AUTO) are
  380. * supported by the link partner. Warn the user if this isn't the case
  381. */
  382. static void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa)
  383. {
  384. struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
  385. u32 rmtadv;
  386. /* The link partner capabilities are only relevant if the
  387. * link supports flow control autonegotiation */
  388. if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
  389. return;
  390. /* If flow control autoneg is supported and enabled, then fine */
  391. if (efx->wanted_fc & EFX_FC_AUTO)
  392. return;
  393. rmtadv = 0;
  394. if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
  395. rmtadv |= ADVERTISED_Pause;
  396. if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
  397. rmtadv |= ADVERTISED_Asym_Pause;
  398. if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause)
  399. netif_err(efx, link, efx->net_dev,
  400. "warning: link partner doesn't support pause frames");
  401. }
  402. static bool efx_mcdi_phy_poll(struct efx_nic *efx)
  403. {
  404. struct efx_link_state old_state = efx->link_state;
  405. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
  406. int rc;
  407. WARN_ON(!mutex_is_locked(&efx->mac_lock));
  408. BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
  409. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
  410. outbuf, sizeof(outbuf), NULL);
  411. if (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. 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. MCDI_DECLARE_BUF(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. return;
  448. ecmd->lp_advertising =
  449. mcdi_to_ethtool_cap(phy_cfg->media,
  450. MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP));
  451. }
  452. static int efx_mcdi_phy_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
  453. {
  454. struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
  455. u32 caps;
  456. int rc;
  457. if (ecmd->autoneg) {
  458. caps = (ethtool_to_mcdi_cap(ecmd->advertising) |
  459. 1 << MC_CMD_PHY_CAP_AN_LBN);
  460. } else if (ecmd->duplex) {
  461. switch (ethtool_cmd_speed(ecmd)) {
  462. case 10: caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN; break;
  463. case 100: caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN; break;
  464. case 1000: caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN; break;
  465. case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break;
  466. case 40000: caps = 1 << MC_CMD_PHY_CAP_40000FDX_LBN; break;
  467. default: return -EINVAL;
  468. }
  469. } else {
  470. switch (ethtool_cmd_speed(ecmd)) {
  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. MCDI_DECLARE_BUF(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_OUT_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. MCDI_DECLARE_BUF(inbuf, MC_CMD_START_BIST_IN_LEN);
  524. MCDI_DECLARE_BUF(outbuf, MC_CMD_POLL_BIST_OUT_SFT9001_LEN);
  525. u8 *ptr;
  526. int rc;
  527. BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN != 0);
  528. MCDI_SET_DWORD(inbuf, START_BIST_IN_TYPE, bist_mode);
  529. rc = efx_mcdi_rpc(efx, MC_CMD_START_BIST,
  530. inbuf, MC_CMD_START_BIST_IN_LEN, NULL, 0, NULL);
  531. if (rc)
  532. goto out;
  533. /* Wait up to 10s for BIST to finish */
  534. for (retry = 0; retry < 100; ++retry) {
  535. BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN != 0);
  536. rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
  537. outbuf, sizeof(outbuf), &outlen);
  538. if (rc)
  539. goto out;
  540. status = MCDI_DWORD(outbuf, POLL_BIST_OUT_RESULT);
  541. if (status != MC_CMD_POLL_BIST_RUNNING)
  542. goto finished;
  543. msleep(100);
  544. }
  545. rc = -ETIMEDOUT;
  546. goto out;
  547. finished:
  548. results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1;
  549. /* SFT9001 specific cable diagnostics output */
  550. if (efx->phy_type == PHY_TYPE_SFT9001B &&
  551. (bist_mode == MC_CMD_PHY_BIST_CABLE_SHORT ||
  552. bist_mode == MC_CMD_PHY_BIST_CABLE_LONG)) {
  553. ptr = MCDI_PTR(outbuf, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A);
  554. if (status == MC_CMD_POLL_BIST_PASSED &&
  555. outlen >= MC_CMD_POLL_BIST_OUT_SFT9001_LEN) {
  556. for (i = 0; i < 8; i++) {
  557. results[count + i] =
  558. EFX_DWORD_FIELD(((efx_dword_t *)ptr)[i],
  559. EFX_DWORD_0);
  560. }
  561. }
  562. count += 8;
  563. }
  564. rc = count;
  565. out:
  566. return rc;
  567. }
  568. static int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results,
  569. unsigned flags)
  570. {
  571. struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
  572. u32 mode;
  573. int rc;
  574. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
  575. rc = efx_mcdi_bist(efx, MC_CMD_PHY_BIST, results);
  576. if (rc < 0)
  577. return rc;
  578. results += rc;
  579. }
  580. /* If we support both LONG and SHORT, then run each in response to
  581. * break or not. Otherwise, run the one we support */
  582. mode = 0;
  583. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN)) {
  584. if ((flags & ETH_TEST_FL_OFFLINE) &&
  585. (phy_cfg->flags &
  586. (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN)))
  587. mode = MC_CMD_PHY_BIST_CABLE_LONG;
  588. else
  589. mode = MC_CMD_PHY_BIST_CABLE_SHORT;
  590. } else if (phy_cfg->flags &
  591. (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))
  592. mode = MC_CMD_PHY_BIST_CABLE_LONG;
  593. if (mode != 0) {
  594. rc = efx_mcdi_bist(efx, mode, results);
  595. if (rc < 0)
  596. return rc;
  597. results += rc;
  598. }
  599. return 0;
  600. }
  601. static const char *efx_mcdi_phy_test_name(struct efx_nic *efx,
  602. unsigned int index)
  603. {
  604. struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
  605. if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) {
  606. if (index == 0)
  607. return "bist";
  608. --index;
  609. }
  610. if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN) |
  611. (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))) {
  612. if (index == 0)
  613. return "cable";
  614. --index;
  615. if (efx->phy_type == PHY_TYPE_SFT9001B) {
  616. if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names))
  617. return mcdi_sft9001_cable_diag_names[index];
  618. index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names);
  619. }
  620. }
  621. return NULL;
  622. }
  623. #define SFP_PAGE_SIZE 128
  624. #define SFP_NUM_PAGES 2
  625. static int efx_mcdi_phy_get_module_eeprom(struct efx_nic *efx,
  626. struct ethtool_eeprom *ee, u8 *data)
  627. {
  628. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_MEDIA_INFO_OUT_LENMAX);
  629. MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PHY_MEDIA_INFO_IN_LEN);
  630. size_t outlen;
  631. int rc;
  632. unsigned int payload_len;
  633. unsigned int space_remaining = ee->len;
  634. unsigned int page;
  635. unsigned int page_off;
  636. unsigned int to_copy;
  637. u8 *user_data = data;
  638. BUILD_BUG_ON(SFP_PAGE_SIZE * SFP_NUM_PAGES != ETH_MODULE_SFF_8079_LEN);
  639. page_off = ee->offset % SFP_PAGE_SIZE;
  640. page = ee->offset / SFP_PAGE_SIZE;
  641. while (space_remaining && (page < SFP_NUM_PAGES)) {
  642. MCDI_SET_DWORD(inbuf, GET_PHY_MEDIA_INFO_IN_PAGE, page);
  643. rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_MEDIA_INFO,
  644. inbuf, sizeof(inbuf),
  645. outbuf, sizeof(outbuf),
  646. &outlen);
  647. if (rc)
  648. return rc;
  649. if (outlen < (MC_CMD_GET_PHY_MEDIA_INFO_OUT_DATA_OFST +
  650. SFP_PAGE_SIZE))
  651. return -EIO;
  652. payload_len = MCDI_DWORD(outbuf,
  653. GET_PHY_MEDIA_INFO_OUT_DATALEN);
  654. if (payload_len != SFP_PAGE_SIZE)
  655. return -EIO;
  656. /* Copy as much as we can into data */
  657. payload_len -= page_off;
  658. to_copy = (space_remaining < payload_len) ?
  659. space_remaining : payload_len;
  660. memcpy(user_data,
  661. MCDI_PTR(outbuf, GET_PHY_MEDIA_INFO_OUT_DATA) + page_off,
  662. to_copy);
  663. space_remaining -= to_copy;
  664. user_data += to_copy;
  665. page_off = 0;
  666. page++;
  667. }
  668. return 0;
  669. }
  670. static int efx_mcdi_phy_get_module_info(struct efx_nic *efx,
  671. struct ethtool_modinfo *modinfo)
  672. {
  673. struct efx_mcdi_phy_data *phy_cfg = efx->phy_data;
  674. switch (phy_cfg->media) {
  675. case MC_CMD_MEDIA_SFP_PLUS:
  676. modinfo->type = ETH_MODULE_SFF_8079;
  677. modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
  678. return 0;
  679. default:
  680. return -EOPNOTSUPP;
  681. }
  682. }
  683. static const struct efx_phy_operations efx_mcdi_phy_ops = {
  684. .probe = efx_mcdi_phy_probe,
  685. .init = efx_port_dummy_op_int,
  686. .reconfigure = efx_mcdi_port_reconfigure,
  687. .poll = efx_mcdi_phy_poll,
  688. .fini = efx_port_dummy_op_void,
  689. .remove = efx_mcdi_phy_remove,
  690. .get_settings = efx_mcdi_phy_get_settings,
  691. .set_settings = efx_mcdi_phy_set_settings,
  692. .test_alive = efx_mcdi_phy_test_alive,
  693. .run_tests = efx_mcdi_phy_run_tests,
  694. .test_name = efx_mcdi_phy_test_name,
  695. .get_module_eeprom = efx_mcdi_phy_get_module_eeprom,
  696. .get_module_info = efx_mcdi_phy_get_module_info,
  697. };
  698. u32 efx_mcdi_phy_get_caps(struct efx_nic *efx)
  699. {
  700. struct efx_mcdi_phy_data *phy_data = efx->phy_data;
  701. return phy_data->supported_cap;
  702. }
  703. static unsigned int efx_mcdi_event_link_speed[] = {
  704. [MCDI_EVENT_LINKCHANGE_SPEED_100M] = 100,
  705. [MCDI_EVENT_LINKCHANGE_SPEED_1G] = 1000,
  706. [MCDI_EVENT_LINKCHANGE_SPEED_10G] = 10000,
  707. [MCDI_EVENT_LINKCHANGE_SPEED_40G] = 40000,
  708. };
  709. void efx_mcdi_process_link_change(struct efx_nic *efx, efx_qword_t *ev)
  710. {
  711. u32 flags, fcntl, speed, lpa;
  712. speed = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_SPEED);
  713. EFX_BUG_ON_PARANOID(speed >= ARRAY_SIZE(efx_mcdi_event_link_speed));
  714. speed = efx_mcdi_event_link_speed[speed];
  715. flags = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LINK_FLAGS);
  716. fcntl = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_FCNTL);
  717. lpa = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LP_CAP);
  718. /* efx->link_state is only modified by efx_mcdi_phy_get_link(),
  719. * which is only run after flushing the event queues. Therefore, it
  720. * is safe to modify the link state outside of the mac_lock here.
  721. */
  722. efx_mcdi_phy_decode_link(efx, &efx->link_state, speed, flags, fcntl);
  723. efx_mcdi_phy_check_fcntl(efx, lpa);
  724. efx_link_status_changed(efx);
  725. }
  726. int efx_mcdi_set_mac(struct efx_nic *efx)
  727. {
  728. u32 fcntl;
  729. MCDI_DECLARE_BUF(cmdbytes, MC_CMD_SET_MAC_IN_LEN);
  730. BUILD_BUG_ON(MC_CMD_SET_MAC_OUT_LEN != 0);
  731. /* This has no effect on EF10 */
  732. ether_addr_copy(MCDI_PTR(cmdbytes, SET_MAC_IN_ADDR),
  733. efx->net_dev->dev_addr);
  734. MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_MTU,
  735. EFX_MAX_FRAME_LEN(efx->net_dev->mtu));
  736. MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_DRAIN, 0);
  737. /* Set simple MAC filter for Siena */
  738. MCDI_POPULATE_DWORD_1(cmdbytes, SET_MAC_IN_REJECT,
  739. SET_MAC_IN_REJECT_UNCST, efx->unicast_filter);
  740. switch (efx->wanted_fc) {
  741. case EFX_FC_RX | EFX_FC_TX:
  742. fcntl = MC_CMD_FCNTL_BIDIR;
  743. break;
  744. case EFX_FC_RX:
  745. fcntl = MC_CMD_FCNTL_RESPOND;
  746. break;
  747. default:
  748. fcntl = MC_CMD_FCNTL_OFF;
  749. break;
  750. }
  751. if (efx->wanted_fc & EFX_FC_AUTO)
  752. fcntl = MC_CMD_FCNTL_AUTO;
  753. if (efx->fc_disable)
  754. fcntl = MC_CMD_FCNTL_OFF;
  755. MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_FCNTL, fcntl);
  756. return efx_mcdi_rpc(efx, MC_CMD_SET_MAC, cmdbytes, sizeof(cmdbytes),
  757. NULL, 0, NULL);
  758. }
  759. bool efx_mcdi_mac_check_fault(struct efx_nic *efx)
  760. {
  761. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN);
  762. size_t outlength;
  763. int rc;
  764. BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0);
  765. rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0,
  766. outbuf, sizeof(outbuf), &outlength);
  767. if (rc)
  768. return true;
  769. return MCDI_DWORD(outbuf, GET_LINK_OUT_MAC_FAULT) != 0;
  770. }
  771. enum efx_stats_action {
  772. EFX_STATS_ENABLE,
  773. EFX_STATS_DISABLE,
  774. EFX_STATS_PULL,
  775. };
  776. static int efx_mcdi_mac_stats(struct efx_nic *efx,
  777. enum efx_stats_action action, int clear)
  778. {
  779. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  780. MCDI_DECLARE_BUF(inbuf, MC_CMD_MAC_STATS_IN_LEN);
  781. int rc;
  782. int change = action == EFX_STATS_PULL ? 0 : 1;
  783. int enable = action == EFX_STATS_ENABLE ? 1 : 0;
  784. int period = action == EFX_STATS_ENABLE ? 1000 : 0;
  785. dma_addr_t dma_addr = efx->stats_buffer.dma_addr;
  786. u32 dma_len = action != EFX_STATS_DISABLE ?
  787. MC_CMD_MAC_NSTATS * sizeof(u64) : 0;
  788. BUILD_BUG_ON(MC_CMD_MAC_STATS_OUT_DMA_LEN != 0);
  789. MCDI_SET_QWORD(inbuf, MAC_STATS_IN_DMA_ADDR, dma_addr);
  790. MCDI_POPULATE_DWORD_7(inbuf, MAC_STATS_IN_CMD,
  791. MAC_STATS_IN_DMA, !!enable,
  792. MAC_STATS_IN_CLEAR, clear,
  793. MAC_STATS_IN_PERIODIC_CHANGE, change,
  794. MAC_STATS_IN_PERIODIC_ENABLE, enable,
  795. MAC_STATS_IN_PERIODIC_CLEAR, 0,
  796. MAC_STATS_IN_PERIODIC_NOEVENT, 1,
  797. MAC_STATS_IN_PERIOD_MS, period);
  798. MCDI_SET_DWORD(inbuf, MAC_STATS_IN_DMA_LEN, dma_len);
  799. MCDI_SET_DWORD(inbuf, MAC_STATS_IN_PORT_ID, nic_data->vport_id);
  800. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_MAC_STATS, inbuf, sizeof(inbuf),
  801. NULL, 0, NULL);
  802. /* Expect ENOENT if DMA queues have not been set up */
  803. if (rc && (rc != -ENOENT || atomic_read(&efx->active_queues)))
  804. efx_mcdi_display_error(efx, MC_CMD_MAC_STATS, sizeof(inbuf),
  805. NULL, 0, rc);
  806. return rc;
  807. }
  808. void efx_mcdi_mac_start_stats(struct efx_nic *efx)
  809. {
  810. __le64 *dma_stats = efx->stats_buffer.addr;
  811. dma_stats[MC_CMD_MAC_GENERATION_END] = EFX_MC_STATS_GENERATION_INVALID;
  812. efx_mcdi_mac_stats(efx, EFX_STATS_ENABLE, 0);
  813. }
  814. void efx_mcdi_mac_stop_stats(struct efx_nic *efx)
  815. {
  816. efx_mcdi_mac_stats(efx, EFX_STATS_DISABLE, 0);
  817. }
  818. #define EFX_MAC_STATS_WAIT_US 100
  819. #define EFX_MAC_STATS_WAIT_ATTEMPTS 10
  820. void efx_mcdi_mac_pull_stats(struct efx_nic *efx)
  821. {
  822. __le64 *dma_stats = efx->stats_buffer.addr;
  823. int attempts = EFX_MAC_STATS_WAIT_ATTEMPTS;
  824. dma_stats[MC_CMD_MAC_GENERATION_END] = EFX_MC_STATS_GENERATION_INVALID;
  825. efx_mcdi_mac_stats(efx, EFX_STATS_PULL, 0);
  826. while (dma_stats[MC_CMD_MAC_GENERATION_END] ==
  827. EFX_MC_STATS_GENERATION_INVALID &&
  828. attempts-- != 0)
  829. udelay(EFX_MAC_STATS_WAIT_US);
  830. }
  831. int efx_mcdi_port_probe(struct efx_nic *efx)
  832. {
  833. int rc;
  834. /* Hook in PHY operations table */
  835. efx->phy_op = &efx_mcdi_phy_ops;
  836. /* Set up MDIO structure for PHY */
  837. efx->mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22;
  838. efx->mdio.mdio_read = efx_mcdi_mdio_read;
  839. efx->mdio.mdio_write = efx_mcdi_mdio_write;
  840. /* Fill out MDIO structure, loopback modes, and initial link state */
  841. rc = efx->phy_op->probe(efx);
  842. if (rc != 0)
  843. return rc;
  844. /* Allocate buffer for stats */
  845. rc = efx_nic_alloc_buffer(efx, &efx->stats_buffer,
  846. MC_CMD_MAC_NSTATS * sizeof(u64), GFP_KERNEL);
  847. if (rc)
  848. return rc;
  849. netif_dbg(efx, probe, efx->net_dev,
  850. "stats buffer at %llx (virt %p phys %llx)\n",
  851. (u64)efx->stats_buffer.dma_addr,
  852. efx->stats_buffer.addr,
  853. (u64)virt_to_phys(efx->stats_buffer.addr));
  854. efx_mcdi_mac_stats(efx, EFX_STATS_DISABLE, 1);
  855. return 0;
  856. }
  857. void efx_mcdi_port_remove(struct efx_nic *efx)
  858. {
  859. efx->phy_op->remove(efx);
  860. efx_nic_free_buffer(efx, &efx->stats_buffer);
  861. }
  862. /* Get physical port number (EF10 only; on Siena it is same as PF number) */
  863. int efx_mcdi_port_get_number(struct efx_nic *efx)
  864. {
  865. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN);
  866. int rc;
  867. rc = efx_mcdi_rpc(efx, MC_CMD_GET_PORT_ASSIGNMENT, NULL, 0,
  868. outbuf, sizeof(outbuf), NULL);
  869. if (rc)
  870. return rc;
  871. return MCDI_DWORD(outbuf, GET_PORT_ASSIGNMENT_OUT_PORT);
  872. }