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