net/sfc/base: import 5xxx/6xxx family support
[dpdk.git] / drivers / net / sfc / base / siena_phy.c
1 /*
2  * Copyright (c) 2009-2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are
27  * those of the authors and should not be interpreted as representing official
28  * policies, either expressed or implied, of the FreeBSD Project.
29  */
30
31 #include "efx.h"
32 #include "efx_impl.h"
33
34 #if EFSYS_OPT_SIENA
35
36 static                  void
37 siena_phy_decode_cap(
38         __in            uint32_t mcdi_cap,
39         __out           uint32_t *maskp)
40 {
41         uint32_t mask;
42
43         mask = 0;
44         if (mcdi_cap & (1 << MC_CMD_PHY_CAP_10HDX_LBN))
45                 mask |= (1 << EFX_PHY_CAP_10HDX);
46         if (mcdi_cap & (1 << MC_CMD_PHY_CAP_10FDX_LBN))
47                 mask |= (1 << EFX_PHY_CAP_10FDX);
48         if (mcdi_cap & (1 << MC_CMD_PHY_CAP_100HDX_LBN))
49                 mask |= (1 << EFX_PHY_CAP_100HDX);
50         if (mcdi_cap & (1 << MC_CMD_PHY_CAP_100FDX_LBN))
51                 mask |= (1 << EFX_PHY_CAP_100FDX);
52         if (mcdi_cap & (1 << MC_CMD_PHY_CAP_1000HDX_LBN))
53                 mask |= (1 << EFX_PHY_CAP_1000HDX);
54         if (mcdi_cap & (1 << MC_CMD_PHY_CAP_1000FDX_LBN))
55                 mask |= (1 << EFX_PHY_CAP_1000FDX);
56         if (mcdi_cap & (1 << MC_CMD_PHY_CAP_10000FDX_LBN))
57                 mask |= (1 << EFX_PHY_CAP_10000FDX);
58         if (mcdi_cap & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
59                 mask |= (1 << EFX_PHY_CAP_PAUSE);
60         if (mcdi_cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
61                 mask |= (1 << EFX_PHY_CAP_ASYM);
62         if (mcdi_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
63                 mask |= (1 << EFX_PHY_CAP_AN);
64
65         *maskp = mask;
66 }
67
68 static                  void
69 siena_phy_decode_link_mode(
70         __in            efx_nic_t *enp,
71         __in            uint32_t link_flags,
72         __in            unsigned int speed,
73         __in            unsigned int fcntl,
74         __out           efx_link_mode_t *link_modep,
75         __out           unsigned int *fcntlp)
76 {
77         boolean_t fd = !!(link_flags &
78                     (1 << MC_CMD_GET_LINK_OUT_FULL_DUPLEX_LBN));
79         boolean_t up = !!(link_flags &
80                     (1 << MC_CMD_GET_LINK_OUT_LINK_UP_LBN));
81
82         _NOTE(ARGUNUSED(enp))
83
84         if (!up)
85                 *link_modep = EFX_LINK_DOWN;
86         else if (speed == 10000 && fd)
87                 *link_modep = EFX_LINK_10000FDX;
88         else if (speed == 1000)
89                 *link_modep = fd ? EFX_LINK_1000FDX : EFX_LINK_1000HDX;
90         else if (speed == 100)
91                 *link_modep = fd ? EFX_LINK_100FDX : EFX_LINK_100HDX;
92         else if (speed == 10)
93                 *link_modep = fd ? EFX_LINK_10FDX : EFX_LINK_10HDX;
94         else
95                 *link_modep = EFX_LINK_UNKNOWN;
96
97         if (fcntl == MC_CMD_FCNTL_OFF)
98                 *fcntlp = 0;
99         else if (fcntl == MC_CMD_FCNTL_RESPOND)
100                 *fcntlp = EFX_FCNTL_RESPOND;
101         else if (fcntl == MC_CMD_FCNTL_BIDIR)
102                 *fcntlp = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
103         else {
104                 EFSYS_PROBE1(mc_pcol_error, int, fcntl);
105                 *fcntlp = 0;
106         }
107 }
108
109                         void
110 siena_phy_link_ev(
111         __in            efx_nic_t *enp,
112         __in            efx_qword_t *eqp,
113         __out           efx_link_mode_t *link_modep)
114 {
115         efx_port_t *epp = &(enp->en_port);
116         unsigned int link_flags;
117         unsigned int speed;
118         unsigned int fcntl;
119         efx_link_mode_t link_mode;
120         uint32_t lp_cap_mask;
121
122         /*
123          * Convert the LINKCHANGE speed enumeration into mbit/s, in the
124          * same way as GET_LINK encodes the speed
125          */
126         switch (MCDI_EV_FIELD(eqp, LINKCHANGE_SPEED)) {
127         case MCDI_EVENT_LINKCHANGE_SPEED_100M:
128                 speed = 100;
129                 break;
130         case MCDI_EVENT_LINKCHANGE_SPEED_1G:
131                 speed = 1000;
132                 break;
133         case MCDI_EVENT_LINKCHANGE_SPEED_10G:
134                 speed = 10000;
135                 break;
136         default:
137                 speed = 0;
138                 break;
139         }
140
141         link_flags = MCDI_EV_FIELD(eqp, LINKCHANGE_LINK_FLAGS);
142         siena_phy_decode_link_mode(enp, link_flags, speed,
143                                     MCDI_EV_FIELD(eqp, LINKCHANGE_FCNTL),
144                                     &link_mode, &fcntl);
145         siena_phy_decode_cap(MCDI_EV_FIELD(eqp, LINKCHANGE_LP_CAP),
146                             &lp_cap_mask);
147
148         /*
149          * It's safe to update ep_lp_cap_mask without the driver's port lock
150          * because presumably any concurrently running efx_port_poll() is
151          * only going to arrive at the same value.
152          *
153          * ep_fcntl has two meanings. It's either the link common fcntl
154          * (if the PHY supports AN), or it's the forced link state. If
155          * the former, it's safe to update the value for the same reason as
156          * for ep_lp_cap_mask. If the latter, then just ignore the value,
157          * because we can race with efx_mac_fcntl_set().
158          */
159         epp->ep_lp_cap_mask = lp_cap_mask;
160         if (epp->ep_phy_cap_mask & (1 << EFX_PHY_CAP_AN))
161                 epp->ep_fcntl = fcntl;
162
163         *link_modep = link_mode;
164 }
165
166         __checkReturn   efx_rc_t
167 siena_phy_power(
168         __in            efx_nic_t *enp,
169         __in            boolean_t power)
170 {
171         efx_rc_t rc;
172
173         if (!power)
174                 return (0);
175
176         /* Check if the PHY is a zombie */
177         if ((rc = siena_phy_verify(enp)) != 0)
178                 goto fail1;
179
180         enp->en_reset_flags |= EFX_RESET_PHY;
181
182         return (0);
183
184 fail1:
185         EFSYS_PROBE1(fail1, efx_rc_t, rc);
186
187         return (rc);
188 }
189
190         __checkReturn   efx_rc_t
191 siena_phy_get_link(
192         __in            efx_nic_t *enp,
193         __out           siena_link_state_t *slsp)
194 {
195         efx_mcdi_req_t req;
196         uint8_t payload[MAX(MC_CMD_GET_LINK_IN_LEN,
197                             MC_CMD_GET_LINK_OUT_LEN)];
198         efx_rc_t rc;
199
200         (void) memset(payload, 0, sizeof (payload));
201         req.emr_cmd = MC_CMD_GET_LINK;
202         req.emr_in_buf = payload;
203         req.emr_in_length = MC_CMD_GET_LINK_IN_LEN;
204         req.emr_out_buf = payload;
205         req.emr_out_length = MC_CMD_GET_LINK_OUT_LEN;
206
207         efx_mcdi_execute(enp, &req);
208
209         if (req.emr_rc != 0) {
210                 rc = req.emr_rc;
211                 goto fail1;
212         }
213
214         if (req.emr_out_length_used < MC_CMD_GET_LINK_OUT_LEN) {
215                 rc = EMSGSIZE;
216                 goto fail2;
217         }
218
219         siena_phy_decode_cap(MCDI_OUT_DWORD(req, GET_LINK_OUT_CAP),
220                             &slsp->sls_adv_cap_mask);
221         siena_phy_decode_cap(MCDI_OUT_DWORD(req, GET_LINK_OUT_LP_CAP),
222                             &slsp->sls_lp_cap_mask);
223
224         siena_phy_decode_link_mode(enp, MCDI_OUT_DWORD(req, GET_LINK_OUT_FLAGS),
225                             MCDI_OUT_DWORD(req, GET_LINK_OUT_LINK_SPEED),
226                             MCDI_OUT_DWORD(req, GET_LINK_OUT_FCNTL),
227                             &slsp->sls_link_mode, &slsp->sls_fcntl);
228
229         slsp->sls_mac_up = MCDI_OUT_DWORD(req, GET_LINK_OUT_MAC_FAULT) == 0;
230
231         return (0);
232
233 fail2:
234         EFSYS_PROBE(fail2);
235 fail1:
236         EFSYS_PROBE1(fail1, efx_rc_t, rc);
237
238         return (rc);
239 }
240
241         __checkReturn   efx_rc_t
242 siena_phy_reconfigure(
243         __in            efx_nic_t *enp)
244 {
245         efx_port_t *epp = &(enp->en_port);
246         efx_mcdi_req_t req;
247         uint8_t payload[MAX(MAX(MC_CMD_SET_ID_LED_IN_LEN,
248                                 MC_CMD_SET_ID_LED_OUT_LEN),
249                             MAX(MC_CMD_SET_LINK_IN_LEN,
250                                 MC_CMD_SET_LINK_OUT_LEN))];
251         uint32_t cap_mask;
252         unsigned int led_mode;
253         unsigned int speed;
254         efx_rc_t rc;
255
256         (void) memset(payload, 0, sizeof (payload));
257         req.emr_cmd = MC_CMD_SET_LINK;
258         req.emr_in_buf = payload;
259         req.emr_in_length = MC_CMD_SET_LINK_IN_LEN;
260         req.emr_out_buf = payload;
261         req.emr_out_length = MC_CMD_SET_LINK_OUT_LEN;
262
263         cap_mask = epp->ep_adv_cap_mask;
264         MCDI_IN_POPULATE_DWORD_10(req, SET_LINK_IN_CAP,
265                 PHY_CAP_10HDX, (cap_mask >> EFX_PHY_CAP_10HDX) & 0x1,
266                 PHY_CAP_10FDX, (cap_mask >> EFX_PHY_CAP_10FDX) & 0x1,
267                 PHY_CAP_100HDX, (cap_mask >> EFX_PHY_CAP_100HDX) & 0x1,
268                 PHY_CAP_100FDX, (cap_mask >> EFX_PHY_CAP_100FDX) & 0x1,
269                 PHY_CAP_1000HDX, (cap_mask >> EFX_PHY_CAP_1000HDX) & 0x1,
270                 PHY_CAP_1000FDX, (cap_mask >> EFX_PHY_CAP_1000FDX) & 0x1,
271                 PHY_CAP_10000FDX, (cap_mask >> EFX_PHY_CAP_10000FDX) & 0x1,
272                 PHY_CAP_PAUSE, (cap_mask >> EFX_PHY_CAP_PAUSE) & 0x1,
273                 PHY_CAP_ASYM, (cap_mask >> EFX_PHY_CAP_ASYM) & 0x1,
274                 PHY_CAP_AN, (cap_mask >> EFX_PHY_CAP_AN) & 0x1);
275
276         MCDI_IN_SET_DWORD(req, SET_LINK_IN_LOOPBACK_MODE, MC_CMD_LOOPBACK_NONE);
277         speed = 0;
278         MCDI_IN_SET_DWORD(req, SET_LINK_IN_LOOPBACK_SPEED, speed);
279
280         MCDI_IN_SET_DWORD(req, SET_LINK_IN_FLAGS, 0);
281
282         efx_mcdi_execute(enp, &req);
283
284         if (req.emr_rc != 0) {
285                 rc = req.emr_rc;
286                 goto fail1;
287         }
288
289         /* And set the blink mode */
290         (void) memset(payload, 0, sizeof (payload));
291         req.emr_cmd = MC_CMD_SET_ID_LED;
292         req.emr_in_buf = payload;
293         req.emr_in_length = MC_CMD_SET_ID_LED_IN_LEN;
294         req.emr_out_buf = payload;
295         req.emr_out_length = MC_CMD_SET_ID_LED_OUT_LEN;
296
297         MCDI_IN_SET_DWORD(req, SET_ID_LED_IN_STATE, MC_CMD_LED_DEFAULT);
298
299         efx_mcdi_execute(enp, &req);
300
301         if (req.emr_rc != 0) {
302                 rc = req.emr_rc;
303                 goto fail2;
304         }
305
306         return (0);
307
308 fail2:
309         EFSYS_PROBE(fail2);
310 fail1:
311         EFSYS_PROBE1(fail1, efx_rc_t, rc);
312
313         return (rc);
314 }
315
316         __checkReturn   efx_rc_t
317 siena_phy_verify(
318         __in            efx_nic_t *enp)
319 {
320         efx_mcdi_req_t req;
321         uint8_t payload[MAX(MC_CMD_GET_PHY_STATE_IN_LEN,
322                             MC_CMD_GET_PHY_STATE_OUT_LEN)];
323         uint32_t state;
324         efx_rc_t rc;
325
326         (void) memset(payload, 0, sizeof (payload));
327         req.emr_cmd = MC_CMD_GET_PHY_STATE;
328         req.emr_in_buf = payload;
329         req.emr_in_length = MC_CMD_GET_PHY_STATE_IN_LEN;
330         req.emr_out_buf = payload;
331         req.emr_out_length = MC_CMD_GET_PHY_STATE_OUT_LEN;
332
333         efx_mcdi_execute(enp, &req);
334
335         if (req.emr_rc != 0) {
336                 rc = req.emr_rc;
337                 goto fail1;
338         }
339
340         if (req.emr_out_length_used < MC_CMD_GET_PHY_STATE_OUT_LEN) {
341                 rc = EMSGSIZE;
342                 goto fail2;
343         }
344
345         state = MCDI_OUT_DWORD(req, GET_PHY_STATE_OUT_STATE);
346         if (state != MC_CMD_PHY_STATE_OK) {
347                 if (state != MC_CMD_PHY_STATE_ZOMBIE)
348                         EFSYS_PROBE1(mc_pcol_error, int, state);
349                 rc = ENOTACTIVE;
350                 goto fail3;
351         }
352
353         return (0);
354
355 fail3:
356         EFSYS_PROBE(fail3);
357 fail2:
358         EFSYS_PROBE(fail2);
359 fail1:
360         EFSYS_PROBE1(fail1, efx_rc_t, rc);
361
362         return (rc);
363 }
364
365         __checkReturn   efx_rc_t
366 siena_phy_oui_get(
367         __in            efx_nic_t *enp,
368         __out           uint32_t *ouip)
369 {
370         _NOTE(ARGUNUSED(enp, ouip))
371
372         return (ENOTSUP);
373 }
374
375 #endif  /* EFSYS_OPT_SIENA */