a6a2af4f3476ddd2911d33fc0fa9ac519223cfda
[dpdk.git] / drivers / net / sfc / base / efx_phy.c
1 /*
2  * Copyright (c) 2007-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
35 #if EFSYS_OPT_SIENA
36 static const efx_phy_ops_t      __efx_phy_siena_ops = {
37         siena_phy_power,                /* epo_power */
38         NULL,                           /* epo_reset */
39         siena_phy_reconfigure,          /* epo_reconfigure */
40         siena_phy_verify,               /* epo_verify */
41         siena_phy_oui_get,              /* epo_oui_get */
42 };
43 #endif  /* EFSYS_OPT_SIENA */
44
45         __checkReturn   efx_rc_t
46 efx_phy_probe(
47         __in            efx_nic_t *enp)
48 {
49         efx_port_t *epp = &(enp->en_port);
50         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
51         const efx_phy_ops_t *epop;
52         efx_rc_t rc;
53
54         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
55
56         epp->ep_port = encp->enc_port;
57         epp->ep_phy_type = encp->enc_phy_type;
58
59         /* Hook in operations structure */
60         switch (enp->en_family) {
61 #if EFSYS_OPT_SIENA
62         case EFX_FAMILY_SIENA:
63                 epop = &__efx_phy_siena_ops;
64                 break;
65 #endif  /* EFSYS_OPT_SIENA */
66         default:
67                 rc = ENOTSUP;
68                 goto fail1;
69         }
70
71         epp->ep_epop = epop;
72
73         return (0);
74
75 fail1:
76         EFSYS_PROBE1(fail1, efx_rc_t, rc);
77
78         epp->ep_port = 0;
79         epp->ep_phy_type = 0;
80
81         return (rc);
82 }
83
84         __checkReturn   efx_rc_t
85 efx_phy_verify(
86         __in            efx_nic_t *enp)
87 {
88         efx_port_t *epp = &(enp->en_port);
89         const efx_phy_ops_t *epop = epp->ep_epop;
90
91         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
92         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
93
94         return (epop->epo_verify(enp));
95 }
96
97                         void
98 efx_phy_adv_cap_get(
99         __in            efx_nic_t *enp,
100         __in            uint32_t flag,
101         __out           uint32_t *maskp)
102 {
103         efx_port_t *epp = &(enp->en_port);
104
105         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
106         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
107
108         switch (flag) {
109         case EFX_PHY_CAP_CURRENT:
110                 *maskp = epp->ep_adv_cap_mask;
111                 break;
112         case EFX_PHY_CAP_DEFAULT:
113                 *maskp = epp->ep_default_adv_cap_mask;
114                 break;
115         case EFX_PHY_CAP_PERM:
116                 *maskp = epp->ep_phy_cap_mask;
117                 break;
118         default:
119                 EFSYS_ASSERT(B_FALSE);
120                 break;
121         }
122 }
123
124         __checkReturn   efx_rc_t
125 efx_phy_adv_cap_set(
126         __in            efx_nic_t *enp,
127         __in            uint32_t mask)
128 {
129         efx_port_t *epp = &(enp->en_port);
130         const efx_phy_ops_t *epop = epp->ep_epop;
131         uint32_t old_mask;
132         efx_rc_t rc;
133
134         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
135         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
136
137         if ((mask & ~epp->ep_phy_cap_mask) != 0) {
138                 rc = ENOTSUP;
139                 goto fail1;
140         }
141
142         if (epp->ep_adv_cap_mask == mask)
143                 goto done;
144
145         old_mask = epp->ep_adv_cap_mask;
146         epp->ep_adv_cap_mask = mask;
147
148         if ((rc = epop->epo_reconfigure(enp)) != 0)
149                 goto fail2;
150
151 done:
152         return (0);
153
154 fail2:
155         EFSYS_PROBE(fail2);
156
157         epp->ep_adv_cap_mask = old_mask;
158         /* Reconfigure for robustness */
159         if (epop->epo_reconfigure(enp) != 0) {
160                 /*
161                  * We may have an inconsistent view of our advertised speed
162                  * capabilities.
163                  */
164                 EFSYS_ASSERT(0);
165         }
166
167 fail1:
168         EFSYS_PROBE1(fail1, efx_rc_t, rc);
169
170         return (rc);
171 }
172
173         void
174 efx_phy_lp_cap_get(
175         __in            efx_nic_t *enp,
176         __out           uint32_t *maskp)
177 {
178         efx_port_t *epp = &(enp->en_port);
179
180         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
181         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
182
183         *maskp = epp->ep_lp_cap_mask;
184 }
185
186         __checkReturn   efx_rc_t
187 efx_phy_oui_get(
188         __in            efx_nic_t *enp,
189         __out           uint32_t *ouip)
190 {
191         efx_port_t *epp = &(enp->en_port);
192         const efx_phy_ops_t *epop = epp->ep_epop;
193
194         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
195         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
196
197         return (epop->epo_oui_get(enp, ouip));
198 }
199
200                         void
201 efx_phy_media_type_get(
202         __in            efx_nic_t *enp,
203         __out           efx_phy_media_type_t *typep)
204 {
205         efx_port_t *epp = &(enp->en_port);
206
207         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
208         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
209
210         if (epp->ep_module_type != EFX_PHY_MEDIA_INVALID)
211                 *typep = epp->ep_module_type;
212         else
213                 *typep = epp->ep_fixed_port_type;
214 }
215
216         __checkReturn   efx_rc_t
217 efx_phy_module_get_info(
218         __in                    efx_nic_t *enp,
219         __in                    uint8_t dev_addr,
220         __in                    uint8_t offset,
221         __in                    uint8_t len,
222         __out_bcount(len)       uint8_t *data)
223 {
224         efx_rc_t rc;
225
226         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
227         EFSYS_ASSERT(data != NULL);
228
229         if ((uint32_t)offset + len > 0xff) {
230                 rc = EINVAL;
231                 goto fail1;
232         }
233
234         if ((rc = efx_mcdi_phy_module_get_info(enp, dev_addr,
235             offset, len, data)) != 0)
236                 goto fail2;
237
238         return (0);
239
240 fail2:
241         EFSYS_PROBE(fail2);
242 fail1:
243         EFSYS_PROBE1(fail1, efx_rc_t, rc);
244
245         return (rc);
246 }
247
248
249                         void
250 efx_phy_unprobe(
251         __in    efx_nic_t *enp)
252 {
253         efx_port_t *epp = &(enp->en_port);
254
255         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
256
257         epp->ep_epop = NULL;
258
259         epp->ep_adv_cap_mask = 0;
260
261         epp->ep_port = 0;
262         epp->ep_phy_type = 0;
263 }