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