net/sfc/base: import SFN7xxx family support
[dpdk.git] / drivers / net / sfc / base / ef10_phy.c
1 /*
2  * Copyright (c) 2012-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_HUNTINGTON || EFSYS_OPT_MEDFORD
35
36 static                  void
37 mcdi_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_40000FDX_LBN))
59                 mask |= (1 << EFX_PHY_CAP_40000FDX);
60         if (mcdi_cap & (1 << MC_CMD_PHY_CAP_PAUSE_LBN))
61                 mask |= (1 << EFX_PHY_CAP_PAUSE);
62         if (mcdi_cap & (1 << MC_CMD_PHY_CAP_ASYM_LBN))
63                 mask |= (1 << EFX_PHY_CAP_ASYM);
64         if (mcdi_cap & (1 << MC_CMD_PHY_CAP_AN_LBN))
65                 mask |= (1 << EFX_PHY_CAP_AN);
66
67         *maskp = mask;
68 }
69
70 static                  void
71 mcdi_phy_decode_link_mode(
72         __in            efx_nic_t *enp,
73         __in            uint32_t link_flags,
74         __in            unsigned int speed,
75         __in            unsigned int fcntl,
76         __out           efx_link_mode_t *link_modep,
77         __out           unsigned int *fcntlp)
78 {
79         boolean_t fd = !!(link_flags &
80                     (1 << MC_CMD_GET_LINK_OUT_FULL_DUPLEX_LBN));
81         boolean_t up = !!(link_flags &
82                     (1 << MC_CMD_GET_LINK_OUT_LINK_UP_LBN));
83
84         _NOTE(ARGUNUSED(enp))
85
86         if (!up)
87                 *link_modep = EFX_LINK_DOWN;
88         else if (speed == 40000 && fd)
89                 *link_modep = EFX_LINK_40000FDX;
90         else if (speed == 10000 && fd)
91                 *link_modep = EFX_LINK_10000FDX;
92         else if (speed == 1000)
93                 *link_modep = fd ? EFX_LINK_1000FDX : EFX_LINK_1000HDX;
94         else if (speed == 100)
95                 *link_modep = fd ? EFX_LINK_100FDX : EFX_LINK_100HDX;
96         else if (speed == 10)
97                 *link_modep = fd ? EFX_LINK_10FDX : EFX_LINK_10HDX;
98         else
99                 *link_modep = EFX_LINK_UNKNOWN;
100
101         if (fcntl == MC_CMD_FCNTL_OFF)
102                 *fcntlp = 0;
103         else if (fcntl == MC_CMD_FCNTL_RESPOND)
104                 *fcntlp = EFX_FCNTL_RESPOND;
105         else if (fcntl == MC_CMD_FCNTL_GENERATE)
106                 *fcntlp = EFX_FCNTL_GENERATE;
107         else if (fcntl == MC_CMD_FCNTL_BIDIR)
108                 *fcntlp = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
109         else {
110                 EFSYS_PROBE1(mc_pcol_error, int, fcntl);
111                 *fcntlp = 0;
112         }
113 }
114
115
116                         void
117 ef10_phy_link_ev(
118         __in            efx_nic_t *enp,
119         __in            efx_qword_t *eqp,
120         __out           efx_link_mode_t *link_modep)
121 {
122         efx_port_t *epp = &(enp->en_port);
123         unsigned int link_flags;
124         unsigned int speed;
125         unsigned int fcntl;
126         efx_link_mode_t link_mode;
127         uint32_t lp_cap_mask;
128
129         /*
130          * Convert the LINKCHANGE speed enumeration into mbit/s, in the
131          * same way as GET_LINK encodes the speed
132          */
133         switch (MCDI_EV_FIELD(eqp, LINKCHANGE_SPEED)) {
134         case MCDI_EVENT_LINKCHANGE_SPEED_100M:
135                 speed = 100;
136                 break;
137         case MCDI_EVENT_LINKCHANGE_SPEED_1G:
138                 speed = 1000;
139                 break;
140         case MCDI_EVENT_LINKCHANGE_SPEED_10G:
141                 speed = 10000;
142                 break;
143         case MCDI_EVENT_LINKCHANGE_SPEED_40G:
144                 speed = 40000;
145                 break;
146         default:
147                 speed = 0;
148                 break;
149         }
150
151         link_flags = MCDI_EV_FIELD(eqp, LINKCHANGE_LINK_FLAGS);
152         mcdi_phy_decode_link_mode(enp, link_flags, speed,
153                                     MCDI_EV_FIELD(eqp, LINKCHANGE_FCNTL),
154                                     &link_mode, &fcntl);
155         mcdi_phy_decode_cap(MCDI_EV_FIELD(eqp, LINKCHANGE_LP_CAP),
156                             &lp_cap_mask);
157
158         /*
159          * It's safe to update ep_lp_cap_mask without the driver's port lock
160          * because presumably any concurrently running efx_port_poll() is
161          * only going to arrive at the same value.
162          *
163          * ep_fcntl has two meanings. It's either the link common fcntl
164          * (if the PHY supports AN), or it's the forced link state. If
165          * the former, it's safe to update the value for the same reason as
166          * for ep_lp_cap_mask. If the latter, then just ignore the value,
167          * because we can race with efx_mac_fcntl_set().
168          */
169         epp->ep_lp_cap_mask = lp_cap_mask;
170         epp->ep_fcntl = fcntl;
171
172         *link_modep = link_mode;
173 }
174
175         __checkReturn   efx_rc_t
176 ef10_phy_power(
177         __in            efx_nic_t *enp,
178         __in            boolean_t power)
179 {
180         efx_rc_t rc;
181
182         if (!power)
183                 return (0);
184
185         /* Check if the PHY is a zombie */
186         if ((rc = ef10_phy_verify(enp)) != 0)
187                 goto fail1;
188
189         enp->en_reset_flags |= EFX_RESET_PHY;
190
191         return (0);
192
193 fail1:
194         EFSYS_PROBE1(fail1, efx_rc_t, rc);
195
196         return (rc);
197 }
198
199         __checkReturn   efx_rc_t
200 ef10_phy_get_link(
201         __in            efx_nic_t *enp,
202         __out           ef10_link_state_t *elsp)
203 {
204         efx_mcdi_req_t req;
205         uint8_t payload[MAX(MC_CMD_GET_LINK_IN_LEN,
206                             MC_CMD_GET_LINK_OUT_LEN)];
207         efx_rc_t rc;
208
209         (void) memset(payload, 0, sizeof (payload));
210         req.emr_cmd = MC_CMD_GET_LINK;
211         req.emr_in_buf = payload;
212         req.emr_in_length = MC_CMD_GET_LINK_IN_LEN;
213         req.emr_out_buf = payload;
214         req.emr_out_length = MC_CMD_GET_LINK_OUT_LEN;
215
216         efx_mcdi_execute(enp, &req);
217
218         if (req.emr_rc != 0) {
219                 rc = req.emr_rc;
220                 goto fail1;
221         }
222
223         if (req.emr_out_length_used < MC_CMD_GET_LINK_OUT_LEN) {
224                 rc = EMSGSIZE;
225                 goto fail2;
226         }
227
228         mcdi_phy_decode_cap(MCDI_OUT_DWORD(req, GET_LINK_OUT_CAP),
229                             &elsp->els_adv_cap_mask);
230         mcdi_phy_decode_cap(MCDI_OUT_DWORD(req, GET_LINK_OUT_LP_CAP),
231                             &elsp->els_lp_cap_mask);
232
233         mcdi_phy_decode_link_mode(enp, MCDI_OUT_DWORD(req, GET_LINK_OUT_FLAGS),
234                             MCDI_OUT_DWORD(req, GET_LINK_OUT_LINK_SPEED),
235                             MCDI_OUT_DWORD(req, GET_LINK_OUT_FCNTL),
236                             &elsp->els_link_mode, &elsp->els_fcntl);
237
238         elsp->els_mac_up = MCDI_OUT_DWORD(req, GET_LINK_OUT_MAC_FAULT) == 0;
239
240         return (0);
241
242 fail2:
243         EFSYS_PROBE(fail2);
244 fail1:
245         EFSYS_PROBE1(fail1, efx_rc_t, rc);
246
247         return (rc);
248 }
249
250         __checkReturn   efx_rc_t
251 ef10_phy_reconfigure(
252         __in            efx_nic_t *enp)
253 {
254         efx_port_t *epp = &(enp->en_port);
255         efx_mcdi_req_t req;
256         uint8_t payload[MAX(MC_CMD_SET_LINK_IN_LEN,
257                             MC_CMD_SET_LINK_OUT_LEN)];
258         uint32_t cap_mask;
259         unsigned int led_mode;
260         unsigned int speed;
261         boolean_t supported;
262         efx_rc_t rc;
263
264         if ((rc = efx_mcdi_link_control_supported(enp, &supported)) != 0)
265                 goto fail1;
266         if (supported == B_FALSE)
267                 goto out;
268
269         (void) memset(payload, 0, sizeof (payload));
270         req.emr_cmd = MC_CMD_SET_LINK;
271         req.emr_in_buf = payload;
272         req.emr_in_length = MC_CMD_SET_LINK_IN_LEN;
273         req.emr_out_buf = payload;
274         req.emr_out_length = MC_CMD_SET_LINK_OUT_LEN;
275
276         cap_mask = epp->ep_adv_cap_mask;
277         MCDI_IN_POPULATE_DWORD_10(req, SET_LINK_IN_CAP,
278                 PHY_CAP_10HDX, (cap_mask >> EFX_PHY_CAP_10HDX) & 0x1,
279                 PHY_CAP_10FDX, (cap_mask >> EFX_PHY_CAP_10FDX) & 0x1,
280                 PHY_CAP_100HDX, (cap_mask >> EFX_PHY_CAP_100HDX) & 0x1,
281                 PHY_CAP_100FDX, (cap_mask >> EFX_PHY_CAP_100FDX) & 0x1,
282                 PHY_CAP_1000HDX, (cap_mask >> EFX_PHY_CAP_1000HDX) & 0x1,
283                 PHY_CAP_1000FDX, (cap_mask >> EFX_PHY_CAP_1000FDX) & 0x1,
284                 PHY_CAP_10000FDX, (cap_mask >> EFX_PHY_CAP_10000FDX) & 0x1,
285                 PHY_CAP_PAUSE, (cap_mask >> EFX_PHY_CAP_PAUSE) & 0x1,
286                 PHY_CAP_ASYM, (cap_mask >> EFX_PHY_CAP_ASYM) & 0x1,
287                 PHY_CAP_AN, (cap_mask >> EFX_PHY_CAP_AN) & 0x1);
288         /* Too many fields for for POPULATE macros, so insert this afterwards */
289         MCDI_IN_SET_DWORD_FIELD(req, SET_LINK_IN_CAP,
290             PHY_CAP_40000FDX, (cap_mask >> EFX_PHY_CAP_40000FDX) & 0x1);
291
292         MCDI_IN_SET_DWORD(req, SET_LINK_IN_LOOPBACK_MODE, MC_CMD_LOOPBACK_NONE);
293         speed = 0;
294         MCDI_IN_SET_DWORD(req, SET_LINK_IN_LOOPBACK_SPEED, speed);
295
296         MCDI_IN_SET_DWORD(req, SET_LINK_IN_FLAGS, 0);
297
298         efx_mcdi_execute(enp, &req);
299
300         if (req.emr_rc != 0) {
301                 rc = req.emr_rc;
302                 goto fail2;
303         }
304
305         /* And set the blink mode */
306         (void) memset(payload, 0, sizeof (payload));
307         req.emr_cmd = MC_CMD_SET_ID_LED;
308         req.emr_in_buf = payload;
309         req.emr_in_length = MC_CMD_SET_ID_LED_IN_LEN;
310         req.emr_out_buf = payload;
311         req.emr_out_length = MC_CMD_SET_ID_LED_OUT_LEN;
312
313         MCDI_IN_SET_DWORD(req, SET_ID_LED_IN_STATE, MC_CMD_LED_DEFAULT);
314
315         efx_mcdi_execute(enp, &req);
316
317         if (req.emr_rc != 0) {
318                 rc = req.emr_rc;
319                 goto fail3;
320         }
321 out:
322         return (0);
323
324 fail3:
325         EFSYS_PROBE(fail3);
326 fail2:
327         EFSYS_PROBE(fail2);
328 fail1:
329         EFSYS_PROBE1(fail1, efx_rc_t, rc);
330
331         return (rc);
332 }
333
334         __checkReturn   efx_rc_t
335 ef10_phy_verify(
336         __in            efx_nic_t *enp)
337 {
338         efx_mcdi_req_t req;
339         uint8_t payload[MAX(MC_CMD_GET_PHY_STATE_IN_LEN,
340                             MC_CMD_GET_PHY_STATE_OUT_LEN)];
341         uint32_t state;
342         efx_rc_t rc;
343
344         (void) memset(payload, 0, sizeof (payload));
345         req.emr_cmd = MC_CMD_GET_PHY_STATE;
346         req.emr_in_buf = payload;
347         req.emr_in_length = MC_CMD_GET_PHY_STATE_IN_LEN;
348         req.emr_out_buf = payload;
349         req.emr_out_length = MC_CMD_GET_PHY_STATE_OUT_LEN;
350
351         efx_mcdi_execute(enp, &req);
352
353         if (req.emr_rc != 0) {
354                 rc = req.emr_rc;
355                 goto fail1;
356         }
357
358         if (req.emr_out_length_used < MC_CMD_GET_PHY_STATE_OUT_LEN) {
359                 rc = EMSGSIZE;
360                 goto fail2;
361         }
362
363         state = MCDI_OUT_DWORD(req, GET_PHY_STATE_OUT_STATE);
364         if (state != MC_CMD_PHY_STATE_OK) {
365                 if (state != MC_CMD_PHY_STATE_ZOMBIE)
366                         EFSYS_PROBE1(mc_pcol_error, int, state);
367                 rc = ENOTACTIVE;
368                 goto fail3;
369         }
370
371         return (0);
372
373 fail3:
374         EFSYS_PROBE(fail3);
375 fail2:
376         EFSYS_PROBE(fail2);
377 fail1:
378         EFSYS_PROBE1(fail1, efx_rc_t, rc);
379
380         return (rc);
381 }
382
383         __checkReturn   efx_rc_t
384 ef10_phy_oui_get(
385         __in            efx_nic_t *enp,
386         __out           uint32_t *ouip)
387 {
388         _NOTE(ARGUNUSED(enp, ouip))
389
390         return (ENOTSUP);
391 }
392
393 #endif  /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD */