]>
Commit | Line | Data |
---|---|---|
afd4aea0 BH |
1 | /**************************************************************************** |
2 | * Driver for Solarflare Solarstorm network controllers and boards | |
0a6f40c6 | 3 | * Copyright 2009-2010 Solarflare Communications Inc. |
afd4aea0 BH |
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 | /* | |
11 | * Driver for PHY related operations via MCDI. | |
12 | */ | |
13 | ||
5a0e3ad6 | 14 | #include <linux/slab.h> |
afd4aea0 BH |
15 | #include "efx.h" |
16 | #include "phy.h" | |
17 | #include "mcdi.h" | |
18 | #include "mcdi_pcol.h" | |
affaf485 SH |
19 | #include "nic.h" |
20 | #include "selftest.h" | |
afd4aea0 | 21 | |
3bd93035 | 22 | struct efx_mcdi_phy_data { |
afd4aea0 BH |
23 | u32 flags; |
24 | u32 type; | |
25 | u32 supported_cap; | |
26 | u32 channel; | |
27 | u32 port; | |
28 | u32 stats_mask; | |
29 | u8 name[20]; | |
30 | u32 media; | |
31 | u32 mmd_mask; | |
32 | u8 revision[20]; | |
33 | u32 forced_cap; | |
34 | }; | |
35 | ||
36 | static int | |
3bd93035 | 37 | efx_mcdi_get_phy_cfg(struct efx_nic *efx, struct efx_mcdi_phy_data *cfg) |
afd4aea0 | 38 | { |
59cfc479 | 39 | MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_CFG_OUT_LEN); |
afd4aea0 BH |
40 | size_t outlen; |
41 | int rc; | |
42 | ||
43 | BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_IN_LEN != 0); | |
44 | BUILD_BUG_ON(MC_CMD_GET_PHY_CFG_OUT_NAME_LEN != sizeof(cfg->name)); | |
45 | ||
46 | rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_CFG, NULL, 0, | |
47 | outbuf, sizeof(outbuf), &outlen); | |
48 | if (rc) | |
49 | goto fail; | |
50 | ||
51 | if (outlen < MC_CMD_GET_PHY_CFG_OUT_LEN) { | |
00bbb4a5 | 52 | rc = -EIO; |
afd4aea0 BH |
53 | goto fail; |
54 | } | |
55 | ||
56 | cfg->flags = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_FLAGS); | |
57 | cfg->type = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_TYPE); | |
58 | cfg->supported_cap = | |
59 | MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_SUPPORTED_CAP); | |
60 | cfg->channel = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_CHANNEL); | |
61 | cfg->port = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_PRT); | |
62 | cfg->stats_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_STATS_MASK); | |
63 | memcpy(cfg->name, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_NAME), | |
64 | sizeof(cfg->name)); | |
65 | cfg->media = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MEDIA_TYPE); | |
66 | cfg->mmd_mask = MCDI_DWORD(outbuf, GET_PHY_CFG_OUT_MMD_MASK); | |
67 | memcpy(cfg->revision, MCDI_PTR(outbuf, GET_PHY_CFG_OUT_REVISION), | |
68 | sizeof(cfg->revision)); | |
69 | ||
70 | return 0; | |
71 | ||
72 | fail: | |
62776d03 | 73 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
afd4aea0 BH |
74 | return rc; |
75 | } | |
76 | ||
77 | static int efx_mcdi_set_link(struct efx_nic *efx, u32 capabilities, | |
78 | u32 flags, u32 loopback_mode, | |
79 | u32 loopback_speed) | |
80 | { | |
59cfc479 | 81 | MCDI_DECLARE_BUF(inbuf, MC_CMD_SET_LINK_IN_LEN); |
afd4aea0 BH |
82 | int rc; |
83 | ||
84 | BUILD_BUG_ON(MC_CMD_SET_LINK_OUT_LEN != 0); | |
85 | ||
86 | MCDI_SET_DWORD(inbuf, SET_LINK_IN_CAP, capabilities); | |
87 | MCDI_SET_DWORD(inbuf, SET_LINK_IN_FLAGS, flags); | |
88 | MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_MODE, loopback_mode); | |
89 | MCDI_SET_DWORD(inbuf, SET_LINK_IN_LOOPBACK_SPEED, loopback_speed); | |
90 | ||
91 | rc = efx_mcdi_rpc(efx, MC_CMD_SET_LINK, inbuf, sizeof(inbuf), | |
92 | NULL, 0, NULL); | |
93 | if (rc) | |
94 | goto fail; | |
95 | ||
96 | return 0; | |
97 | ||
98 | fail: | |
62776d03 | 99 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
afd4aea0 BH |
100 | return rc; |
101 | } | |
102 | ||
103 | static int efx_mcdi_loopback_modes(struct efx_nic *efx, u64 *loopback_modes) | |
104 | { | |
59cfc479 | 105 | MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LOOPBACK_MODES_OUT_LEN); |
afd4aea0 BH |
106 | size_t outlen; |
107 | int rc; | |
108 | ||
109 | rc = efx_mcdi_rpc(efx, MC_CMD_GET_LOOPBACK_MODES, NULL, 0, | |
110 | outbuf, sizeof(outbuf), &outlen); | |
111 | if (rc) | |
112 | goto fail; | |
113 | ||
f2b0befd BH |
114 | if (outlen < (MC_CMD_GET_LOOPBACK_MODES_OUT_SUGGESTED_OFST + |
115 | MC_CMD_GET_LOOPBACK_MODES_OUT_SUGGESTED_LEN)) { | |
00bbb4a5 | 116 | rc = -EIO; |
afd4aea0 BH |
117 | goto fail; |
118 | } | |
119 | ||
05a9320f | 120 | *loopback_modes = MCDI_QWORD(outbuf, GET_LOOPBACK_MODES_OUT_SUGGESTED); |
afd4aea0 BH |
121 | |
122 | return 0; | |
123 | ||
124 | fail: | |
62776d03 | 125 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
afd4aea0 BH |
126 | return rc; |
127 | } | |
128 | ||
43f775b2 BH |
129 | static int efx_mcdi_mdio_read(struct net_device *net_dev, |
130 | int prtad, int devad, u16 addr) | |
afd4aea0 | 131 | { |
43f775b2 | 132 | struct efx_nic *efx = netdev_priv(net_dev); |
59cfc479 BH |
133 | MCDI_DECLARE_BUF(inbuf, MC_CMD_MDIO_READ_IN_LEN); |
134 | MCDI_DECLARE_BUF(outbuf, MC_CMD_MDIO_READ_OUT_LEN); | |
afd4aea0 BH |
135 | size_t outlen; |
136 | int rc; | |
137 | ||
43f775b2 | 138 | MCDI_SET_DWORD(inbuf, MDIO_READ_IN_BUS, efx->mdio_bus); |
afd4aea0 BH |
139 | MCDI_SET_DWORD(inbuf, MDIO_READ_IN_PRTAD, prtad); |
140 | MCDI_SET_DWORD(inbuf, MDIO_READ_IN_DEVAD, devad); | |
141 | MCDI_SET_DWORD(inbuf, MDIO_READ_IN_ADDR, addr); | |
142 | ||
143 | rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_READ, inbuf, sizeof(inbuf), | |
144 | outbuf, sizeof(outbuf), &outlen); | |
145 | if (rc) | |
146 | goto fail; | |
147 | ||
43f775b2 BH |
148 | if (MCDI_DWORD(outbuf, MDIO_READ_OUT_STATUS) != |
149 | MC_CMD_MDIO_STATUS_GOOD) | |
150 | return -EIO; | |
151 | ||
152 | return (u16)MCDI_DWORD(outbuf, MDIO_READ_OUT_VALUE); | |
afd4aea0 BH |
153 | |
154 | fail: | |
62776d03 | 155 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
afd4aea0 BH |
156 | return rc; |
157 | } | |
158 | ||
43f775b2 BH |
159 | static int efx_mcdi_mdio_write(struct net_device *net_dev, |
160 | int prtad, int devad, u16 addr, u16 value) | |
afd4aea0 | 161 | { |
43f775b2 | 162 | struct efx_nic *efx = netdev_priv(net_dev); |
59cfc479 BH |
163 | MCDI_DECLARE_BUF(inbuf, MC_CMD_MDIO_WRITE_IN_LEN); |
164 | MCDI_DECLARE_BUF(outbuf, MC_CMD_MDIO_WRITE_OUT_LEN); | |
afd4aea0 BH |
165 | size_t outlen; |
166 | int rc; | |
167 | ||
43f775b2 | 168 | MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_BUS, efx->mdio_bus); |
afd4aea0 BH |
169 | MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_PRTAD, prtad); |
170 | MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_DEVAD, devad); | |
171 | MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_ADDR, addr); | |
172 | MCDI_SET_DWORD(inbuf, MDIO_WRITE_IN_VALUE, value); | |
173 | ||
174 | rc = efx_mcdi_rpc(efx, MC_CMD_MDIO_WRITE, inbuf, sizeof(inbuf), | |
175 | outbuf, sizeof(outbuf), &outlen); | |
176 | if (rc) | |
177 | goto fail; | |
178 | ||
43f775b2 BH |
179 | if (MCDI_DWORD(outbuf, MDIO_WRITE_OUT_STATUS) != |
180 | MC_CMD_MDIO_STATUS_GOOD) | |
181 | return -EIO; | |
182 | ||
afd4aea0 BH |
183 | return 0; |
184 | ||
185 | fail: | |
62776d03 | 186 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc); |
afd4aea0 BH |
187 | return rc; |
188 | } | |
189 | ||
190 | static u32 mcdi_to_ethtool_cap(u32 media, u32 cap) | |
191 | { | |
192 | u32 result = 0; | |
193 | ||
194 | switch (media) { | |
195 | case MC_CMD_MEDIA_KX4: | |
196 | result |= SUPPORTED_Backplane; | |
197 | if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN)) | |
198 | result |= SUPPORTED_1000baseKX_Full; | |
199 | if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN)) | |
200 | result |= SUPPORTED_10000baseKX4_Full; | |
201 | break; | |
202 | ||
203 | case MC_CMD_MEDIA_XFP: | |
204 | case MC_CMD_MEDIA_SFP_PLUS: | |
205 | result |= SUPPORTED_FIBRE; | |
206 | break; | |
207 | ||
208 | case MC_CMD_MEDIA_BASE_T: | |
209 | result |= SUPPORTED_TP; | |
210 | if (cap & (1 << MC_CMD_PHY_CAP_10HDX_LBN)) | |
211 | result |= SUPPORTED_10baseT_Half; | |
212 | if (cap & (1 << MC_CMD_PHY_CAP_10FDX_LBN)) | |
213 | result |= SUPPORTED_10baseT_Full; | |
214 | if (cap & (1 << MC_CMD_PHY_CAP_100HDX_LBN)) | |
215 | result |= SUPPORTED_100baseT_Half; | |
216 | if (cap & (1 << MC_CMD_PHY_CAP_100FDX_LBN)) | |
217 | result |= SUPPORTED_100baseT_Full; | |
218 | if (cap & (1 << MC_CMD_PHY_CAP_1000HDX_LBN)) | |
219 | result |= SUPPORTED_1000baseT_Half; | |
220 | if (cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN)) | |
221 | result |= SUPPORTED_1000baseT_Full; | |
222 | if (cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN)) | |
223 | result |= SUPPORTED_10000baseT_Full; | |
224 | break; | |
225 | } | |
226 | ||
227 | if (cap & (1 << MC_CMD_PHY_CAP_PAUSE_LBN)) | |
228 | result |= SUPPORTED_Pause; | |
229 | if (cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN)) | |
230 | result |= SUPPORTED_Asym_Pause; | |
231 | if (cap & (1 << MC_CMD_PHY_CAP_AN_LBN)) | |
232 | result |= SUPPORTED_Autoneg; | |
233 | ||
234 | return result; | |
235 | } | |
236 | ||
237 | static u32 ethtool_to_mcdi_cap(u32 cap) | |
238 | { | |
239 | u32 result = 0; | |
240 | ||
241 | if (cap & SUPPORTED_10baseT_Half) | |
242 | result |= (1 << MC_CMD_PHY_CAP_10HDX_LBN); | |
243 | if (cap & SUPPORTED_10baseT_Full) | |
244 | result |= (1 << MC_CMD_PHY_CAP_10FDX_LBN); | |
245 | if (cap & SUPPORTED_100baseT_Half) | |
246 | result |= (1 << MC_CMD_PHY_CAP_100HDX_LBN); | |
247 | if (cap & SUPPORTED_100baseT_Full) | |
248 | result |= (1 << MC_CMD_PHY_CAP_100FDX_LBN); | |
249 | if (cap & SUPPORTED_1000baseT_Half) | |
250 | result |= (1 << MC_CMD_PHY_CAP_1000HDX_LBN); | |
251 | if (cap & (SUPPORTED_1000baseT_Full | SUPPORTED_1000baseKX_Full)) | |
252 | result |= (1 << MC_CMD_PHY_CAP_1000FDX_LBN); | |
253 | if (cap & (SUPPORTED_10000baseT_Full | SUPPORTED_10000baseKX4_Full)) | |
254 | result |= (1 << MC_CMD_PHY_CAP_10000FDX_LBN); | |
255 | if (cap & SUPPORTED_Pause) | |
256 | result |= (1 << MC_CMD_PHY_CAP_PAUSE_LBN); | |
257 | if (cap & SUPPORTED_Asym_Pause) | |
258 | result |= (1 << MC_CMD_PHY_CAP_ASYM_LBN); | |
259 | if (cap & SUPPORTED_Autoneg) | |
260 | result |= (1 << MC_CMD_PHY_CAP_AN_LBN); | |
261 | ||
262 | return result; | |
263 | } | |
264 | ||
265 | static u32 efx_get_mcdi_phy_flags(struct efx_nic *efx) | |
266 | { | |
3bd93035 | 267 | struct efx_mcdi_phy_data *phy_cfg = efx->phy_data; |
afd4aea0 BH |
268 | enum efx_phy_mode mode, supported; |
269 | u32 flags; | |
270 | ||
271 | /* TODO: Advertise the capabilities supported by this PHY */ | |
272 | supported = 0; | |
05a9320f | 273 | if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_TXDIS_LBN)) |
afd4aea0 | 274 | supported |= PHY_MODE_TX_DISABLED; |
05a9320f | 275 | if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_LOWPOWER_LBN)) |
afd4aea0 | 276 | supported |= PHY_MODE_LOW_POWER; |
05a9320f | 277 | if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_POWEROFF_LBN)) |
afd4aea0 BH |
278 | supported |= PHY_MODE_OFF; |
279 | ||
280 | mode = efx->phy_mode & supported; | |
281 | ||
282 | flags = 0; | |
283 | if (mode & PHY_MODE_TX_DISABLED) | |
05a9320f | 284 | flags |= (1 << MC_CMD_SET_LINK_IN_TXDIS_LBN); |
afd4aea0 | 285 | if (mode & PHY_MODE_LOW_POWER) |
05a9320f | 286 | flags |= (1 << MC_CMD_SET_LINK_IN_LOWPOWER_LBN); |
afd4aea0 | 287 | if (mode & PHY_MODE_OFF) |
05a9320f | 288 | flags |= (1 << MC_CMD_SET_LINK_IN_POWEROFF_LBN); |
afd4aea0 BH |
289 | |
290 | return flags; | |
291 | } | |
292 | ||
293 | static u32 mcdi_to_ethtool_media(u32 media) | |
294 | { | |
295 | switch (media) { | |
296 | case MC_CMD_MEDIA_XAUI: | |
297 | case MC_CMD_MEDIA_CX4: | |
298 | case MC_CMD_MEDIA_KX4: | |
299 | return PORT_OTHER; | |
300 | ||
301 | case MC_CMD_MEDIA_XFP: | |
302 | case MC_CMD_MEDIA_SFP_PLUS: | |
303 | return PORT_FIBRE; | |
304 | ||
305 | case MC_CMD_MEDIA_BASE_T: | |
306 | return PORT_TP; | |
307 | ||
308 | default: | |
309 | return PORT_OTHER; | |
310 | } | |
311 | } | |
312 | ||
43f775b2 BH |
313 | static void efx_mcdi_phy_decode_link(struct efx_nic *efx, |
314 | struct efx_link_state *link_state, | |
315 | u32 speed, u32 flags, u32 fcntl) | |
316 | { | |
317 | switch (fcntl) { | |
318 | case MC_CMD_FCNTL_AUTO: | |
319 | WARN_ON(1); /* This is not a link mode */ | |
320 | link_state->fc = EFX_FC_AUTO | EFX_FC_TX | EFX_FC_RX; | |
321 | break; | |
322 | case MC_CMD_FCNTL_BIDIR: | |
323 | link_state->fc = EFX_FC_TX | EFX_FC_RX; | |
324 | break; | |
325 | case MC_CMD_FCNTL_RESPOND: | |
326 | link_state->fc = EFX_FC_RX; | |
327 | break; | |
328 | default: | |
329 | WARN_ON(1); | |
330 | case MC_CMD_FCNTL_OFF: | |
331 | link_state->fc = 0; | |
332 | break; | |
333 | } | |
334 | ||
335 | link_state->up = !!(flags & (1 << MC_CMD_GET_LINK_OUT_LINK_UP_LBN)); | |
336 | link_state->fd = !!(flags & (1 << MC_CMD_GET_LINK_OUT_FULL_DUPLEX_LBN)); | |
337 | link_state->speed = speed; | |
338 | } | |
339 | ||
afd4aea0 BH |
340 | static int efx_mcdi_phy_probe(struct efx_nic *efx) |
341 | { | |
3bd93035 | 342 | struct efx_mcdi_phy_data *phy_data; |
59cfc479 | 343 | MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN); |
ff3b00a0 | 344 | u32 caps; |
afd4aea0 BH |
345 | int rc; |
346 | ||
ff3b00a0 SH |
347 | /* Initialise and populate phy_data */ |
348 | phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL); | |
349 | if (phy_data == NULL) | |
350 | return -ENOMEM; | |
351 | ||
352 | rc = efx_mcdi_get_phy_cfg(efx, phy_data); | |
afd4aea0 BH |
353 | if (rc != 0) |
354 | goto fail; | |
355 | ||
ff3b00a0 SH |
356 | /* Read initial link advertisement */ |
357 | BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0); | |
358 | rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0, | |
359 | outbuf, sizeof(outbuf), NULL); | |
360 | if (rc) | |
361 | goto fail; | |
362 | ||
363 | /* Fill out nic state */ | |
364 | efx->phy_data = phy_data; | |
365 | efx->phy_type = phy_data->type; | |
afd4aea0 | 366 | |
ff3b00a0 SH |
367 | efx->mdio_bus = phy_data->channel; |
368 | efx->mdio.prtad = phy_data->port; | |
369 | efx->mdio.mmds = phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22); | |
afd4aea0 | 370 | efx->mdio.mode_support = 0; |
ff3b00a0 | 371 | if (phy_data->mmd_mask & (1 << MC_CMD_MMD_CLAUSE22)) |
afd4aea0 | 372 | efx->mdio.mode_support |= MDIO_SUPPORTS_C22; |
ff3b00a0 | 373 | if (phy_data->mmd_mask & ~(1 << MC_CMD_MMD_CLAUSE22)) |
afd4aea0 BH |
374 | efx->mdio.mode_support |= MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22; |
375 | ||
ff3b00a0 SH |
376 | caps = MCDI_DWORD(outbuf, GET_LINK_OUT_CAP); |
377 | if (caps & (1 << MC_CMD_PHY_CAP_AN_LBN)) | |
378 | efx->link_advertising = | |
379 | mcdi_to_ethtool_cap(phy_data->media, caps); | |
380 | else | |
381 | phy_data->forced_cap = caps; | |
382 | ||
afd4aea0 BH |
383 | /* Assert that we can map efx -> mcdi loopback modes */ |
384 | BUILD_BUG_ON(LOOPBACK_NONE != MC_CMD_LOOPBACK_NONE); | |
385 | BUILD_BUG_ON(LOOPBACK_DATA != MC_CMD_LOOPBACK_DATA); | |
386 | BUILD_BUG_ON(LOOPBACK_GMAC != MC_CMD_LOOPBACK_GMAC); | |
387 | BUILD_BUG_ON(LOOPBACK_XGMII != MC_CMD_LOOPBACK_XGMII); | |
388 | BUILD_BUG_ON(LOOPBACK_XGXS != MC_CMD_LOOPBACK_XGXS); | |
389 | BUILD_BUG_ON(LOOPBACK_XAUI != MC_CMD_LOOPBACK_XAUI); | |
390 | BUILD_BUG_ON(LOOPBACK_GMII != MC_CMD_LOOPBACK_GMII); | |
391 | BUILD_BUG_ON(LOOPBACK_SGMII != MC_CMD_LOOPBACK_SGMII); | |
392 | BUILD_BUG_ON(LOOPBACK_XGBR != MC_CMD_LOOPBACK_XGBR); | |
393 | BUILD_BUG_ON(LOOPBACK_XFI != MC_CMD_LOOPBACK_XFI); | |
394 | BUILD_BUG_ON(LOOPBACK_XAUI_FAR != MC_CMD_LOOPBACK_XAUI_FAR); | |
395 | BUILD_BUG_ON(LOOPBACK_GMII_FAR != MC_CMD_LOOPBACK_GMII_FAR); | |
396 | BUILD_BUG_ON(LOOPBACK_SGMII_FAR != MC_CMD_LOOPBACK_SGMII_FAR); | |
397 | BUILD_BUG_ON(LOOPBACK_XFI_FAR != MC_CMD_LOOPBACK_XFI_FAR); | |
398 | BUILD_BUG_ON(LOOPBACK_GPHY != MC_CMD_LOOPBACK_GPHY); | |
399 | BUILD_BUG_ON(LOOPBACK_PHYXS != MC_CMD_LOOPBACK_PHYXS); | |
400 | BUILD_BUG_ON(LOOPBACK_PCS != MC_CMD_LOOPBACK_PCS); | |
401 | BUILD_BUG_ON(LOOPBACK_PMAPMD != MC_CMD_LOOPBACK_PMAPMD); | |
402 | BUILD_BUG_ON(LOOPBACK_XPORT != MC_CMD_LOOPBACK_XPORT); | |
403 | BUILD_BUG_ON(LOOPBACK_XGMII_WS != MC_CMD_LOOPBACK_XGMII_WS); | |
404 | BUILD_BUG_ON(LOOPBACK_XAUI_WS != MC_CMD_LOOPBACK_XAUI_WS); | |
405 | BUILD_BUG_ON(LOOPBACK_XAUI_WS_FAR != MC_CMD_LOOPBACK_XAUI_WS_FAR); | |
406 | BUILD_BUG_ON(LOOPBACK_XAUI_WS_NEAR != MC_CMD_LOOPBACK_XAUI_WS_NEAR); | |
407 | BUILD_BUG_ON(LOOPBACK_GMII_WS != MC_CMD_LOOPBACK_GMII_WS); | |
408 | BUILD_BUG_ON(LOOPBACK_XFI_WS != MC_CMD_LOOPBACK_XFI_WS); | |
409 | BUILD_BUG_ON(LOOPBACK_XFI_WS_FAR != MC_CMD_LOOPBACK_XFI_WS_FAR); | |
410 | BUILD_BUG_ON(LOOPBACK_PHYXS_WS != MC_CMD_LOOPBACK_PHYXS_WS); | |
411 | ||
412 | rc = efx_mcdi_loopback_modes(efx, &efx->loopback_modes); | |
413 | if (rc != 0) | |
414 | goto fail; | |
415 | /* The MC indicates that LOOPBACK_NONE is a valid loopback mode, | |
416 | * but by convention we don't */ | |
417 | efx->loopback_modes &= ~(1 << LOOPBACK_NONE); | |
418 | ||
7a6b8f6f SH |
419 | /* Set the initial link mode */ |
420 | efx_mcdi_phy_decode_link( | |
421 | efx, &efx->link_state, | |
422 | MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED), | |
423 | MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS), | |
424 | MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL)); | |
425 | ||
426 | /* Default to Autonegotiated flow control if the PHY supports it */ | |
427 | efx->wanted_fc = EFX_FC_RX | EFX_FC_TX; | |
428 | if (phy_data->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN)) | |
429 | efx->wanted_fc |= EFX_FC_AUTO; | |
cffe9d4c | 430 | efx_link_set_wanted_fc(efx, efx->wanted_fc); |
7a6b8f6f | 431 | |
afd4aea0 BH |
432 | return 0; |
433 | ||
434 | fail: | |
435 | kfree(phy_data); | |
436 | return rc; | |
437 | } | |
438 | ||
43f775b2 | 439 | int efx_mcdi_port_reconfigure(struct efx_nic *efx) |
afd4aea0 | 440 | { |
3bd93035 | 441 | struct efx_mcdi_phy_data *phy_cfg = efx->phy_data; |
afd4aea0 BH |
442 | u32 caps = (efx->link_advertising ? |
443 | ethtool_to_mcdi_cap(efx->link_advertising) : | |
444 | phy_cfg->forced_cap); | |
445 | ||
446 | return efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx), | |
447 | efx->loopback_mode, 0); | |
448 | } | |
449 | ||
afd4aea0 BH |
450 | /* Verify that the forced flow control settings (!EFX_FC_AUTO) are |
451 | * supported by the link partner. Warn the user if this isn't the case | |
452 | */ | |
43f775b2 | 453 | static void efx_mcdi_phy_check_fcntl(struct efx_nic *efx, u32 lpa) |
afd4aea0 | 454 | { |
3bd93035 | 455 | struct efx_mcdi_phy_data *phy_cfg = efx->phy_data; |
afd4aea0 BH |
456 | u32 rmtadv; |
457 | ||
25985edc | 458 | /* The link partner capabilities are only relevant if the |
afd4aea0 | 459 | * link supports flow control autonegotiation */ |
7a6b8f6f | 460 | if (~phy_cfg->supported_cap & (1 << MC_CMD_PHY_CAP_AN_LBN)) |
afd4aea0 BH |
461 | return; |
462 | ||
463 | /* If flow control autoneg is supported and enabled, then fine */ | |
464 | if (efx->wanted_fc & EFX_FC_AUTO) | |
465 | return; | |
466 | ||
467 | rmtadv = 0; | |
468 | if (lpa & (1 << MC_CMD_PHY_CAP_PAUSE_LBN)) | |
469 | rmtadv |= ADVERTISED_Pause; | |
470 | if (lpa & (1 << MC_CMD_PHY_CAP_ASYM_LBN)) | |
471 | rmtadv |= ADVERTISED_Asym_Pause; | |
472 | ||
473 | if ((efx->wanted_fc & EFX_FC_TX) && rmtadv == ADVERTISED_Asym_Pause) | |
62776d03 BH |
474 | netif_err(efx, link, efx->net_dev, |
475 | "warning: link partner doesn't support pause frames"); | |
afd4aea0 BH |
476 | } |
477 | ||
478 | static bool efx_mcdi_phy_poll(struct efx_nic *efx) | |
479 | { | |
480 | struct efx_link_state old_state = efx->link_state; | |
59cfc479 | 481 | MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN); |
afd4aea0 BH |
482 | int rc; |
483 | ||
484 | WARN_ON(!mutex_is_locked(&efx->mac_lock)); | |
485 | ||
486 | BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0); | |
487 | ||
488 | rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0, | |
489 | outbuf, sizeof(outbuf), NULL); | |
490 | if (rc) { | |
62776d03 BH |
491 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", |
492 | __func__, rc); | |
afd4aea0 BH |
493 | efx->link_state.up = false; |
494 | } else { | |
495 | efx_mcdi_phy_decode_link( | |
496 | efx, &efx->link_state, | |
497 | MCDI_DWORD(outbuf, GET_LINK_OUT_LINK_SPEED), | |
498 | MCDI_DWORD(outbuf, GET_LINK_OUT_FLAGS), | |
499 | MCDI_DWORD(outbuf, GET_LINK_OUT_FCNTL)); | |
500 | } | |
501 | ||
502 | return !efx_link_state_equal(&efx->link_state, &old_state); | |
503 | } | |
504 | ||
ff3b00a0 | 505 | static void efx_mcdi_phy_remove(struct efx_nic *efx) |
afd4aea0 BH |
506 | { |
507 | struct efx_mcdi_phy_data *phy_data = efx->phy_data; | |
508 | ||
509 | efx->phy_data = NULL; | |
510 | kfree(phy_data); | |
511 | } | |
512 | ||
513 | static void efx_mcdi_phy_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd) | |
514 | { | |
3bd93035 | 515 | struct efx_mcdi_phy_data *phy_cfg = efx->phy_data; |
59cfc479 | 516 | MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN); |
afd4aea0 BH |
517 | int rc; |
518 | ||
519 | ecmd->supported = | |
520 | mcdi_to_ethtool_cap(phy_cfg->media, phy_cfg->supported_cap); | |
521 | ecmd->advertising = efx->link_advertising; | |
70739497 | 522 | ethtool_cmd_speed_set(ecmd, efx->link_state.speed); |
afd4aea0 BH |
523 | ecmd->duplex = efx->link_state.fd; |
524 | ecmd->port = mcdi_to_ethtool_media(phy_cfg->media); | |
525 | ecmd->phy_address = phy_cfg->port; | |
526 | ecmd->transceiver = XCVR_INTERNAL; | |
527 | ecmd->autoneg = !!(efx->link_advertising & ADVERTISED_Autoneg); | |
528 | ecmd->mdio_support = (efx->mdio.mode_support & | |
529 | (MDIO_SUPPORTS_C45 | MDIO_SUPPORTS_C22)); | |
530 | ||
531 | BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0); | |
532 | rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0, | |
533 | outbuf, sizeof(outbuf), NULL); | |
534 | if (rc) { | |
62776d03 BH |
535 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", |
536 | __func__, rc); | |
afd4aea0 BH |
537 | return; |
538 | } | |
539 | ecmd->lp_advertising = | |
540 | mcdi_to_ethtool_cap(phy_cfg->media, | |
541 | MCDI_DWORD(outbuf, GET_LINK_OUT_LP_CAP)); | |
542 | } | |
543 | ||
544 | static int efx_mcdi_phy_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd) | |
545 | { | |
3bd93035 | 546 | struct efx_mcdi_phy_data *phy_cfg = efx->phy_data; |
afd4aea0 BH |
547 | u32 caps; |
548 | int rc; | |
549 | ||
550 | if (ecmd->autoneg) { | |
551 | caps = (ethtool_to_mcdi_cap(ecmd->advertising) | | |
552 | 1 << MC_CMD_PHY_CAP_AN_LBN); | |
553 | } else if (ecmd->duplex) { | |
25db0338 | 554 | switch (ethtool_cmd_speed(ecmd)) { |
afd4aea0 BH |
555 | case 10: caps = 1 << MC_CMD_PHY_CAP_10FDX_LBN; break; |
556 | case 100: caps = 1 << MC_CMD_PHY_CAP_100FDX_LBN; break; | |
557 | case 1000: caps = 1 << MC_CMD_PHY_CAP_1000FDX_LBN; break; | |
558 | case 10000: caps = 1 << MC_CMD_PHY_CAP_10000FDX_LBN; break; | |
559 | default: return -EINVAL; | |
560 | } | |
561 | } else { | |
25db0338 | 562 | switch (ethtool_cmd_speed(ecmd)) { |
afd4aea0 BH |
563 | case 10: caps = 1 << MC_CMD_PHY_CAP_10HDX_LBN; break; |
564 | case 100: caps = 1 << MC_CMD_PHY_CAP_100HDX_LBN; break; | |
565 | case 1000: caps = 1 << MC_CMD_PHY_CAP_1000HDX_LBN; break; | |
566 | default: return -EINVAL; | |
567 | } | |
568 | } | |
569 | ||
570 | rc = efx_mcdi_set_link(efx, caps, efx_get_mcdi_phy_flags(efx), | |
571 | efx->loopback_mode, 0); | |
572 | if (rc) | |
573 | return rc; | |
574 | ||
575 | if (ecmd->autoneg) { | |
576 | efx_link_set_advertising( | |
577 | efx, ecmd->advertising | ADVERTISED_Autoneg); | |
578 | phy_cfg->forced_cap = 0; | |
579 | } else { | |
580 | efx_link_set_advertising(efx, 0); | |
581 | phy_cfg->forced_cap = caps; | |
582 | } | |
583 | return 0; | |
584 | } | |
585 | ||
4f16c073 BH |
586 | static int efx_mcdi_phy_test_alive(struct efx_nic *efx) |
587 | { | |
59cfc479 | 588 | MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_STATE_OUT_LEN); |
4f16c073 BH |
589 | size_t outlen; |
590 | int rc; | |
591 | ||
592 | BUILD_BUG_ON(MC_CMD_GET_PHY_STATE_IN_LEN != 0); | |
593 | ||
594 | rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_STATE, NULL, 0, | |
595 | outbuf, sizeof(outbuf), &outlen); | |
596 | if (rc) | |
597 | return rc; | |
598 | ||
599 | if (outlen < MC_CMD_GET_PHY_STATE_OUT_LEN) | |
00bbb4a5 | 600 | return -EIO; |
05a9320f | 601 | if (MCDI_DWORD(outbuf, GET_PHY_STATE_OUT_STATE) != MC_CMD_PHY_STATE_OK) |
4f16c073 BH |
602 | return -EINVAL; |
603 | ||
604 | return 0; | |
605 | } | |
606 | ||
affaf485 SH |
607 | static const char *const mcdi_sft9001_cable_diag_names[] = { |
608 | "cable.pairA.length", | |
609 | "cable.pairB.length", | |
610 | "cable.pairC.length", | |
611 | "cable.pairD.length", | |
612 | "cable.pairA.status", | |
613 | "cable.pairB.status", | |
614 | "cable.pairC.status", | |
615 | "cable.pairD.status", | |
616 | }; | |
617 | ||
618 | static int efx_mcdi_bist(struct efx_nic *efx, unsigned int bist_mode, | |
619 | int *results) | |
620 | { | |
621 | unsigned int retry, i, count = 0; | |
622 | size_t outlen; | |
623 | u32 status; | |
c5bb0e98 BH |
624 | MCDI_DECLARE_BUF(inbuf, MC_CMD_START_BIST_IN_LEN); |
625 | MCDI_DECLARE_BUF(outbuf, MC_CMD_POLL_BIST_OUT_SFT9001_LEN); | |
626 | u8 *ptr; | |
affaf485 SH |
627 | int rc; |
628 | ||
affaf485 | 629 | BUILD_BUG_ON(MC_CMD_START_BIST_OUT_LEN != 0); |
c5bb0e98 BH |
630 | MCDI_SET_DWORD(inbuf, START_BIST_IN_TYPE, bist_mode); |
631 | rc = efx_mcdi_rpc(efx, MC_CMD_START_BIST, | |
632 | inbuf, MC_CMD_START_BIST_IN_LEN, NULL, 0, NULL); | |
affaf485 SH |
633 | if (rc) |
634 | goto out; | |
635 | ||
636 | /* Wait up to 10s for BIST to finish */ | |
637 | for (retry = 0; retry < 100; ++retry) { | |
638 | BUILD_BUG_ON(MC_CMD_POLL_BIST_IN_LEN != 0); | |
639 | rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0, | |
c5bb0e98 | 640 | outbuf, sizeof(outbuf), &outlen); |
affaf485 SH |
641 | if (rc) |
642 | goto out; | |
643 | ||
c5bb0e98 | 644 | status = MCDI_DWORD(outbuf, POLL_BIST_OUT_RESULT); |
affaf485 SH |
645 | if (status != MC_CMD_POLL_BIST_RUNNING) |
646 | goto finished; | |
647 | ||
648 | msleep(100); | |
649 | } | |
650 | ||
651 | rc = -ETIMEDOUT; | |
652 | goto out; | |
653 | ||
654 | finished: | |
655 | results[count++] = (status == MC_CMD_POLL_BIST_PASSED) ? 1 : -1; | |
656 | ||
657 | /* SFT9001 specific cable diagnostics output */ | |
658 | if (efx->phy_type == PHY_TYPE_SFT9001B && | |
659 | (bist_mode == MC_CMD_PHY_BIST_CABLE_SHORT || | |
660 | bist_mode == MC_CMD_PHY_BIST_CABLE_LONG)) { | |
c5bb0e98 | 661 | ptr = MCDI_PTR(outbuf, POLL_BIST_OUT_SFT9001_CABLE_LENGTH_A); |
affaf485 SH |
662 | if (status == MC_CMD_POLL_BIST_PASSED && |
663 | outlen >= MC_CMD_POLL_BIST_OUT_SFT9001_LEN) { | |
664 | for (i = 0; i < 8; i++) { | |
665 | results[count + i] = | |
666 | EFX_DWORD_FIELD(((efx_dword_t *)ptr)[i], | |
667 | EFX_DWORD_0); | |
668 | } | |
669 | } | |
670 | count += 8; | |
671 | } | |
672 | rc = count; | |
673 | ||
674 | out: | |
affaf485 SH |
675 | return rc; |
676 | } | |
677 | ||
678 | static int efx_mcdi_phy_run_tests(struct efx_nic *efx, int *results, | |
679 | unsigned flags) | |
680 | { | |
3bd93035 | 681 | struct efx_mcdi_phy_data *phy_cfg = efx->phy_data; |
affaf485 SH |
682 | u32 mode; |
683 | int rc; | |
684 | ||
05a9320f | 685 | if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) { |
affaf485 SH |
686 | rc = efx_mcdi_bist(efx, MC_CMD_PHY_BIST, results); |
687 | if (rc < 0) | |
688 | return rc; | |
689 | ||
690 | results += rc; | |
691 | } | |
692 | ||
693 | /* If we support both LONG and SHORT, then run each in response to | |
694 | * break or not. Otherwise, run the one we support */ | |
695 | mode = 0; | |
05a9320f | 696 | if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN)) { |
affaf485 SH |
697 | if ((flags & ETH_TEST_FL_OFFLINE) && |
698 | (phy_cfg->flags & | |
05a9320f | 699 | (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))) |
affaf485 SH |
700 | mode = MC_CMD_PHY_BIST_CABLE_LONG; |
701 | else | |
702 | mode = MC_CMD_PHY_BIST_CABLE_SHORT; | |
703 | } else if (phy_cfg->flags & | |
05a9320f | 704 | (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN)) |
affaf485 SH |
705 | mode = MC_CMD_PHY_BIST_CABLE_LONG; |
706 | ||
707 | if (mode != 0) { | |
708 | rc = efx_mcdi_bist(efx, mode, results); | |
709 | if (rc < 0) | |
710 | return rc; | |
711 | results += rc; | |
712 | } | |
713 | ||
714 | return 0; | |
715 | } | |
716 | ||
d215697f | 717 | static const char *efx_mcdi_phy_test_name(struct efx_nic *efx, |
718 | unsigned int index) | |
affaf485 | 719 | { |
3bd93035 | 720 | struct efx_mcdi_phy_data *phy_cfg = efx->phy_data; |
affaf485 | 721 | |
05a9320f | 722 | if (phy_cfg->flags & (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_LBN)) { |
affaf485 SH |
723 | if (index == 0) |
724 | return "bist"; | |
725 | --index; | |
726 | } | |
727 | ||
05a9320f BH |
728 | if (phy_cfg->flags & ((1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_SHORT_LBN) | |
729 | (1 << MC_CMD_GET_PHY_CFG_OUT_BIST_CABLE_LONG_LBN))) { | |
affaf485 SH |
730 | if (index == 0) |
731 | return "cable"; | |
732 | --index; | |
733 | ||
734 | if (efx->phy_type == PHY_TYPE_SFT9001B) { | |
735 | if (index < ARRAY_SIZE(mcdi_sft9001_cable_diag_names)) | |
736 | return mcdi_sft9001_cable_diag_names[index]; | |
737 | index -= ARRAY_SIZE(mcdi_sft9001_cable_diag_names); | |
738 | } | |
739 | } | |
740 | ||
741 | return NULL; | |
742 | } | |
743 | ||
c087bd2c SH |
744 | #define SFP_PAGE_SIZE 128 |
745 | #define SFP_NUM_PAGES 2 | |
746 | static int efx_mcdi_phy_get_module_eeprom(struct efx_nic *efx, | |
747 | struct ethtool_eeprom *ee, u8 *data) | |
748 | { | |
59cfc479 BH |
749 | MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PHY_MEDIA_INFO_OUT_LENMAX); |
750 | MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PHY_MEDIA_INFO_IN_LEN); | |
c087bd2c SH |
751 | size_t outlen; |
752 | int rc; | |
753 | unsigned int payload_len; | |
754 | unsigned int space_remaining = ee->len; | |
755 | unsigned int page; | |
756 | unsigned int page_off; | |
757 | unsigned int to_copy; | |
758 | u8 *user_data = data; | |
759 | ||
760 | BUILD_BUG_ON(SFP_PAGE_SIZE * SFP_NUM_PAGES != ETH_MODULE_SFF_8079_LEN); | |
761 | ||
762 | page_off = ee->offset % SFP_PAGE_SIZE; | |
763 | page = ee->offset / SFP_PAGE_SIZE; | |
764 | ||
765 | while (space_remaining && (page < SFP_NUM_PAGES)) { | |
766 | MCDI_SET_DWORD(inbuf, GET_PHY_MEDIA_INFO_IN_PAGE, page); | |
767 | ||
768 | rc = efx_mcdi_rpc(efx, MC_CMD_GET_PHY_MEDIA_INFO, | |
769 | inbuf, sizeof(inbuf), | |
770 | outbuf, sizeof(outbuf), | |
771 | &outlen); | |
772 | if (rc) | |
773 | return rc; | |
774 | ||
775 | if (outlen < (MC_CMD_GET_PHY_MEDIA_INFO_OUT_DATA_OFST + | |
776 | SFP_PAGE_SIZE)) | |
777 | return -EIO; | |
778 | ||
779 | payload_len = MCDI_DWORD(outbuf, | |
780 | GET_PHY_MEDIA_INFO_OUT_DATALEN); | |
781 | if (payload_len != SFP_PAGE_SIZE) | |
782 | return -EIO; | |
783 | ||
784 | /* Copy as much as we can into data */ | |
785 | payload_len -= page_off; | |
786 | to_copy = (space_remaining < payload_len) ? | |
787 | space_remaining : payload_len; | |
788 | ||
789 | memcpy(user_data, | |
c5bb0e98 | 790 | MCDI_PTR(outbuf, GET_PHY_MEDIA_INFO_OUT_DATA) + page_off, |
c087bd2c SH |
791 | to_copy); |
792 | ||
793 | space_remaining -= to_copy; | |
794 | user_data += to_copy; | |
795 | page_off = 0; | |
796 | page++; | |
797 | } | |
798 | ||
799 | return 0; | |
800 | } | |
801 | ||
802 | static int efx_mcdi_phy_get_module_info(struct efx_nic *efx, | |
803 | struct ethtool_modinfo *modinfo) | |
804 | { | |
805 | struct efx_mcdi_phy_data *phy_cfg = efx->phy_data; | |
806 | ||
807 | switch (phy_cfg->media) { | |
808 | case MC_CMD_MEDIA_SFP_PLUS: | |
809 | modinfo->type = ETH_MODULE_SFF_8079; | |
810 | modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN; | |
811 | return 0; | |
812 | default: | |
813 | return -EOPNOTSUPP; | |
814 | } | |
815 | } | |
816 | ||
43f775b2 | 817 | static const struct efx_phy_operations efx_mcdi_phy_ops = { |
afd4aea0 | 818 | .probe = efx_mcdi_phy_probe, |
9c636baf | 819 | .init = efx_port_dummy_op_int, |
43f775b2 | 820 | .reconfigure = efx_mcdi_port_reconfigure, |
afd4aea0 | 821 | .poll = efx_mcdi_phy_poll, |
ff3b00a0 SH |
822 | .fini = efx_port_dummy_op_void, |
823 | .remove = efx_mcdi_phy_remove, | |
afd4aea0 BH |
824 | .get_settings = efx_mcdi_phy_get_settings, |
825 | .set_settings = efx_mcdi_phy_set_settings, | |
4f16c073 | 826 | .test_alive = efx_mcdi_phy_test_alive, |
affaf485 SH |
827 | .run_tests = efx_mcdi_phy_run_tests, |
828 | .test_name = efx_mcdi_phy_test_name, | |
c087bd2c SH |
829 | .get_module_eeprom = efx_mcdi_phy_get_module_eeprom, |
830 | .get_module_info = efx_mcdi_phy_get_module_info, | |
afd4aea0 | 831 | }; |
43f775b2 BH |
832 | |
833 | static unsigned int efx_mcdi_event_link_speed[] = { | |
834 | [MCDI_EVENT_LINKCHANGE_SPEED_100M] = 100, | |
835 | [MCDI_EVENT_LINKCHANGE_SPEED_1G] = 1000, | |
836 | [MCDI_EVENT_LINKCHANGE_SPEED_10G] = 10000, | |
837 | }; | |
838 | ||
839 | void efx_mcdi_process_link_change(struct efx_nic *efx, efx_qword_t *ev) | |
840 | { | |
841 | u32 flags, fcntl, speed, lpa; | |
842 | ||
843 | speed = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_SPEED); | |
844 | EFX_BUG_ON_PARANOID(speed >= ARRAY_SIZE(efx_mcdi_event_link_speed)); | |
845 | speed = efx_mcdi_event_link_speed[speed]; | |
846 | ||
847 | flags = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LINK_FLAGS); | |
848 | fcntl = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_FCNTL); | |
849 | lpa = EFX_QWORD_FIELD(*ev, MCDI_EVENT_LINKCHANGE_LP_CAP); | |
850 | ||
851 | /* efx->link_state is only modified by efx_mcdi_phy_get_link(), | |
852 | * which is only run after flushing the event queues. Therefore, it | |
853 | * is safe to modify the link state outside of the mac_lock here. | |
854 | */ | |
855 | efx_mcdi_phy_decode_link(efx, &efx->link_state, speed, flags, fcntl); | |
856 | ||
857 | efx_mcdi_phy_check_fcntl(efx, lpa); | |
858 | ||
859 | efx_link_status_changed(efx); | |
860 | } | |
861 | ||
862 | int efx_mcdi_set_mac(struct efx_nic *efx) | |
863 | { | |
864 | u32 reject, fcntl; | |
865 | MCDI_DECLARE_BUF(cmdbytes, MC_CMD_SET_MAC_IN_LEN); | |
866 | ||
867 | BUILD_BUG_ON(MC_CMD_SET_MAC_OUT_LEN != 0); | |
868 | ||
869 | memcpy(MCDI_PTR(cmdbytes, SET_MAC_IN_ADDR), | |
870 | efx->net_dev->dev_addr, ETH_ALEN); | |
871 | ||
872 | MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_MTU, | |
873 | EFX_MAX_FRAME_LEN(efx->net_dev->mtu)); | |
874 | MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_DRAIN, 0); | |
875 | ||
876 | /* The MCDI command provides for controlling accept/reject | |
877 | * of broadcast packets too, but the driver doesn't currently | |
878 | * expose this. */ | |
879 | reject = (efx->promiscuous) ? 0 : | |
880 | (1 << MC_CMD_SET_MAC_IN_REJECT_UNCST_LBN); | |
881 | MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_REJECT, reject); | |
882 | ||
883 | switch (efx->wanted_fc) { | |
884 | case EFX_FC_RX | EFX_FC_TX: | |
885 | fcntl = MC_CMD_FCNTL_BIDIR; | |
886 | break; | |
887 | case EFX_FC_RX: | |
888 | fcntl = MC_CMD_FCNTL_RESPOND; | |
889 | break; | |
890 | default: | |
891 | fcntl = MC_CMD_FCNTL_OFF; | |
892 | break; | |
893 | } | |
894 | if (efx->wanted_fc & EFX_FC_AUTO) | |
895 | fcntl = MC_CMD_FCNTL_AUTO; | |
896 | if (efx->fc_disable) | |
897 | fcntl = MC_CMD_FCNTL_OFF; | |
898 | ||
899 | MCDI_SET_DWORD(cmdbytes, SET_MAC_IN_FCNTL, fcntl); | |
900 | ||
901 | return efx_mcdi_rpc(efx, MC_CMD_SET_MAC, cmdbytes, sizeof(cmdbytes), | |
902 | NULL, 0, NULL); | |
903 | } | |
904 | ||
905 | bool efx_mcdi_mac_check_fault(struct efx_nic *efx) | |
906 | { | |
907 | MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_LINK_OUT_LEN); | |
908 | size_t outlength; | |
909 | int rc; | |
910 | ||
911 | BUILD_BUG_ON(MC_CMD_GET_LINK_IN_LEN != 0); | |
912 | ||
913 | rc = efx_mcdi_rpc(efx, MC_CMD_GET_LINK, NULL, 0, | |
914 | outbuf, sizeof(outbuf), &outlength); | |
915 | if (rc) { | |
916 | netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", | |
917 | __func__, rc); | |
918 | return true; | |
919 | } | |
920 | ||
921 | return MCDI_DWORD(outbuf, GET_LINK_OUT_MAC_FAULT) != 0; | |
922 | } | |
923 | ||
924 | static int efx_mcdi_mac_stats(struct efx_nic *efx, dma_addr_t dma_addr, | |
925 | u32 dma_len, int enable, int clear) | |
926 | { | |
927 | MCDI_DECLARE_BUF(inbuf, MC_CMD_MAC_STATS_IN_LEN); | |
928 | int rc; | |
929 | efx_dword_t *cmd_ptr; | |
930 | int period = enable ? 1000 : 0; | |
931 | ||
932 | BUILD_BUG_ON(MC_CMD_MAC_STATS_OUT_DMA_LEN != 0); | |
933 | ||
934 | MCDI_SET_QWORD(inbuf, MAC_STATS_IN_DMA_ADDR, dma_addr); | |
935 | cmd_ptr = (efx_dword_t *)MCDI_PTR(inbuf, MAC_STATS_IN_CMD); | |
936 | EFX_POPULATE_DWORD_7(*cmd_ptr, | |
937 | MC_CMD_MAC_STATS_IN_DMA, !!enable, | |
938 | MC_CMD_MAC_STATS_IN_CLEAR, clear, | |
939 | MC_CMD_MAC_STATS_IN_PERIODIC_CHANGE, 1, | |
940 | MC_CMD_MAC_STATS_IN_PERIODIC_ENABLE, !!enable, | |
941 | MC_CMD_MAC_STATS_IN_PERIODIC_CLEAR, 0, | |
942 | MC_CMD_MAC_STATS_IN_PERIODIC_NOEVENT, 1, | |
943 | MC_CMD_MAC_STATS_IN_PERIOD_MS, period); | |
944 | MCDI_SET_DWORD(inbuf, MAC_STATS_IN_DMA_LEN, dma_len); | |
945 | ||
946 | rc = efx_mcdi_rpc(efx, MC_CMD_MAC_STATS, inbuf, sizeof(inbuf), | |
947 | NULL, 0, NULL); | |
948 | if (rc) | |
949 | goto fail; | |
950 | ||
951 | return 0; | |
952 | ||
953 | fail: | |
954 | netif_err(efx, hw, efx->net_dev, "%s: %s failed rc=%d\n", | |
955 | __func__, enable ? "enable" : "disable", rc); | |
956 | return rc; | |
957 | } | |
958 | ||
959 | void efx_mcdi_mac_start_stats(struct efx_nic *efx) | |
960 | { | |
961 | __le64 *dma_stats = efx->stats_buffer.addr; | |
962 | ||
963 | dma_stats[MC_CMD_MAC_GENERATION_END] = EFX_MC_STATS_GENERATION_INVALID; | |
964 | ||
965 | efx_mcdi_mac_stats(efx, efx->stats_buffer.dma_addr, | |
966 | MC_CMD_MAC_NSTATS * sizeof(u64), 1, 0); | |
967 | } | |
968 | ||
969 | void efx_mcdi_mac_stop_stats(struct efx_nic *efx) | |
970 | { | |
971 | efx_mcdi_mac_stats(efx, efx->stats_buffer.dma_addr, 0, 0, 0); | |
972 | } | |
973 | ||
974 | int efx_mcdi_port_probe(struct efx_nic *efx) | |
975 | { | |
976 | int rc; | |
977 | ||
978 | /* Hook in PHY operations table */ | |
979 | efx->phy_op = &efx_mcdi_phy_ops; | |
980 | ||
981 | /* Set up MDIO structure for PHY */ | |
982 | efx->mdio.mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22; | |
983 | efx->mdio.mdio_read = efx_mcdi_mdio_read; | |
984 | efx->mdio.mdio_write = efx_mcdi_mdio_write; | |
985 | ||
986 | /* Fill out MDIO structure, loopback modes, and initial link state */ | |
987 | rc = efx->phy_op->probe(efx); | |
988 | if (rc != 0) | |
989 | return rc; | |
990 | ||
991 | /* Allocate buffer for stats */ | |
992 | rc = efx_nic_alloc_buffer(efx, &efx->stats_buffer, | |
0d19a540 | 993 | MC_CMD_MAC_NSTATS * sizeof(u64), GFP_KERNEL); |
43f775b2 BH |
994 | if (rc) |
995 | return rc; | |
996 | netif_dbg(efx, probe, efx->net_dev, | |
997 | "stats buffer at %llx (virt %p phys %llx)\n", | |
998 | (u64)efx->stats_buffer.dma_addr, | |
999 | efx->stats_buffer.addr, | |
1000 | (u64)virt_to_phys(efx->stats_buffer.addr)); | |
1001 | ||
1002 | efx_mcdi_mac_stats(efx, efx->stats_buffer.dma_addr, 0, 0, 1); | |
1003 | ||
1004 | return 0; | |
1005 | } | |
1006 | ||
1007 | void efx_mcdi_port_remove(struct efx_nic *efx) | |
1008 | { | |
1009 | efx->phy_op->remove(efx); | |
1010 | efx_nic_free_buffer(efx, &efx->stats_buffer); | |
1011 | } |