mcdi_port.c 29 KB

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