25059dfe12ac6aa81607f29c61b8196fa3201b84
[dpdk.git] / drivers / net / sfc / base / efx_phy.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright (c) 2007-2018 Solarflare Communications Inc.
4  * All rights reserved.
5  */
6
7 #include "efx.h"
8 #include "efx_impl.h"
9
10
11 #if EFSYS_OPT_SIENA
12 static const efx_phy_ops_t      __efx_phy_siena_ops = {
13         siena_phy_power,                /* epo_power */
14         NULL,                           /* epo_reset */
15         siena_phy_reconfigure,          /* epo_reconfigure */
16         siena_phy_verify,               /* epo_verify */
17         siena_phy_oui_get,              /* epo_oui_get */
18         NULL,                           /* epo_fec_type_get */
19 #if EFSYS_OPT_PHY_STATS
20         siena_phy_stats_update,         /* epo_stats_update */
21 #endif  /* EFSYS_OPT_PHY_STATS */
22 #if EFSYS_OPT_BIST
23         NULL,                           /* epo_bist_enable_offline */
24         siena_phy_bist_start,           /* epo_bist_start */
25         siena_phy_bist_poll,            /* epo_bist_poll */
26         siena_phy_bist_stop,            /* epo_bist_stop */
27 #endif  /* EFSYS_OPT_BIST */
28 };
29 #endif  /* EFSYS_OPT_SIENA */
30
31 #if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2
32 static const efx_phy_ops_t      __efx_phy_ef10_ops = {
33         ef10_phy_power,                 /* epo_power */
34         NULL,                           /* epo_reset */
35         ef10_phy_reconfigure,           /* epo_reconfigure */
36         ef10_phy_verify,                /* epo_verify */
37         ef10_phy_oui_get,               /* epo_oui_get */
38         ef10_phy_fec_type_get,          /* epo_fec_type_get */
39 #if EFSYS_OPT_PHY_STATS
40         ef10_phy_stats_update,          /* epo_stats_update */
41 #endif  /* EFSYS_OPT_PHY_STATS */
42 #if EFSYS_OPT_BIST
43         ef10_bist_enable_offline,       /* epo_bist_enable_offline */
44         ef10_bist_start,                /* epo_bist_start */
45         ef10_bist_poll,                 /* epo_bist_poll */
46         ef10_bist_stop,                 /* epo_bist_stop */
47 #endif  /* EFSYS_OPT_BIST */
48 };
49 #endif  /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD || EFSYS_OPT_MEDFORD2 */
50
51         __checkReturn   efx_rc_t
52 efx_phy_probe(
53         __in            efx_nic_t *enp)
54 {
55         efx_port_t *epp = &(enp->en_port);
56         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
57         const efx_phy_ops_t *epop;
58         efx_rc_t rc;
59
60         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
61
62         epp->ep_port = encp->enc_port;
63         epp->ep_phy_type = encp->enc_phy_type;
64
65         /* Hook in operations structure */
66         switch (enp->en_family) {
67 #if EFSYS_OPT_SIENA
68         case EFX_FAMILY_SIENA:
69                 epop = &__efx_phy_siena_ops;
70                 break;
71 #endif  /* EFSYS_OPT_SIENA */
72
73 #if EFSYS_OPT_HUNTINGTON
74         case EFX_FAMILY_HUNTINGTON:
75                 epop = &__efx_phy_ef10_ops;
76                 break;
77 #endif  /* EFSYS_OPT_HUNTINGTON */
78
79 #if EFSYS_OPT_MEDFORD
80         case EFX_FAMILY_MEDFORD:
81                 epop = &__efx_phy_ef10_ops;
82                 break;
83 #endif  /* EFSYS_OPT_MEDFORD */
84
85 #if EFSYS_OPT_MEDFORD2
86         case EFX_FAMILY_MEDFORD2:
87                 epop = &__efx_phy_ef10_ops;
88                 break;
89 #endif  /* EFSYS_OPT_MEDFORD2 */
90
91         default:
92                 rc = ENOTSUP;
93                 goto fail1;
94         }
95
96         epp->ep_epop = epop;
97
98         return (0);
99
100 fail1:
101         EFSYS_PROBE1(fail1, efx_rc_t, rc);
102
103         epp->ep_port = 0;
104         epp->ep_phy_type = 0;
105
106         return (rc);
107 }
108
109         __checkReturn   efx_rc_t
110 efx_phy_verify(
111         __in            efx_nic_t *enp)
112 {
113         efx_port_t *epp = &(enp->en_port);
114         const efx_phy_ops_t *epop = epp->ep_epop;
115
116         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
117         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
118
119         return (epop->epo_verify(enp));
120 }
121
122 #if EFSYS_OPT_PHY_LED_CONTROL
123
124         __checkReturn   efx_rc_t
125 efx_phy_led_set(
126         __in            efx_nic_t *enp,
127         __in            efx_phy_led_mode_t mode)
128 {
129         efx_nic_cfg_t *encp = (&enp->en_nic_cfg);
130         efx_port_t *epp = &(enp->en_port);
131         const efx_phy_ops_t *epop = epp->ep_epop;
132         uint32_t mask;
133         efx_rc_t rc;
134
135         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
136         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
137
138         if (epp->ep_phy_led_mode == mode)
139                 goto done;
140
141         mask = (1 << EFX_PHY_LED_DEFAULT);
142         mask |= encp->enc_led_mask;
143
144         if (!((1 << mode) & mask)) {
145                 rc = ENOTSUP;
146                 goto fail1;
147         }
148
149         EFSYS_ASSERT3U(mode, <, EFX_PHY_LED_NMODES);
150         epp->ep_phy_led_mode = mode;
151
152         if ((rc = epop->epo_reconfigure(enp)) != 0)
153                 goto fail2;
154
155 done:
156         return (0);
157
158 fail2:
159         EFSYS_PROBE(fail2);
160 fail1:
161         EFSYS_PROBE1(fail1, efx_rc_t, rc);
162
163         return (rc);
164 }
165 #endif  /* EFSYS_OPT_PHY_LED_CONTROL */
166
167                         void
168 efx_phy_adv_cap_get(
169         __in            efx_nic_t *enp,
170         __in            uint32_t flag,
171         __out           uint32_t *maskp)
172 {
173         efx_port_t *epp = &(enp->en_port);
174
175         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
176         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
177
178         switch (flag) {
179         case EFX_PHY_CAP_CURRENT:
180                 *maskp = epp->ep_adv_cap_mask;
181                 break;
182         case EFX_PHY_CAP_DEFAULT:
183                 *maskp = epp->ep_default_adv_cap_mask;
184                 break;
185         case EFX_PHY_CAP_PERM:
186                 *maskp = epp->ep_phy_cap_mask;
187                 break;
188         default:
189                 EFSYS_ASSERT(B_FALSE);
190                 *maskp = 0;
191                 break;
192         }
193 }
194
195         __checkReturn   efx_rc_t
196 efx_phy_adv_cap_set(
197         __in            efx_nic_t *enp,
198         __in            uint32_t mask)
199 {
200         efx_port_t *epp = &(enp->en_port);
201         const efx_phy_ops_t *epop = epp->ep_epop;
202         uint32_t old_mask;
203         efx_rc_t rc;
204
205         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
206         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
207
208         if ((mask & ~epp->ep_phy_cap_mask) != 0) {
209                 rc = ENOTSUP;
210                 goto fail1;
211         }
212
213         if (epp->ep_adv_cap_mask == mask)
214                 goto done;
215
216         old_mask = epp->ep_adv_cap_mask;
217         epp->ep_adv_cap_mask = mask;
218
219         if ((rc = epop->epo_reconfigure(enp)) != 0)
220                 goto fail2;
221
222 done:
223         return (0);
224
225 fail2:
226         EFSYS_PROBE(fail2);
227
228         epp->ep_adv_cap_mask = old_mask;
229         /* Reconfigure for robustness */
230         if (epop->epo_reconfigure(enp) != 0) {
231                 /*
232                  * We may have an inconsistent view of our advertised speed
233                  * capabilities.
234                  */
235                 EFSYS_ASSERT(0);
236         }
237
238 fail1:
239         EFSYS_PROBE1(fail1, efx_rc_t, rc);
240
241         return (rc);
242 }
243
244         void
245 efx_phy_lp_cap_get(
246         __in            efx_nic_t *enp,
247         __out           uint32_t *maskp)
248 {
249         efx_port_t *epp = &(enp->en_port);
250
251         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
252         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
253
254         *maskp = epp->ep_lp_cap_mask;
255 }
256
257         __checkReturn   efx_rc_t
258 efx_phy_oui_get(
259         __in            efx_nic_t *enp,
260         __out           uint32_t *ouip)
261 {
262         efx_port_t *epp = &(enp->en_port);
263         const efx_phy_ops_t *epop = epp->ep_epop;
264
265         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
266         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
267
268         return (epop->epo_oui_get(enp, ouip));
269 }
270
271                         void
272 efx_phy_media_type_get(
273         __in            efx_nic_t *enp,
274         __out           efx_phy_media_type_t *typep)
275 {
276         efx_port_t *epp = &(enp->en_port);
277
278         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
279         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
280
281         if (epp->ep_module_type != EFX_PHY_MEDIA_INVALID)
282                 *typep = epp->ep_module_type;
283         else
284                 *typep = epp->ep_fixed_port_type;
285 }
286
287         __checkReturn           efx_rc_t
288 efx_phy_module_get_info(
289         __in                    efx_nic_t *enp,
290         __in                    uint8_t dev_addr,
291         __in                    uint8_t offset,
292         __in                    uint8_t len,
293         __out_bcount(len)       uint8_t *data)
294 {
295         efx_rc_t rc;
296
297         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
298         EFSYS_ASSERT(data != NULL);
299
300         if ((uint32_t)offset + len > 0xff) {
301                 rc = EINVAL;
302                 goto fail1;
303         }
304
305         if ((rc = efx_mcdi_phy_module_get_info(enp, dev_addr,
306             offset, len, data)) != 0)
307                 goto fail2;
308
309         return (0);
310
311 fail2:
312         EFSYS_PROBE(fail2);
313 fail1:
314         EFSYS_PROBE1(fail1, efx_rc_t, rc);
315
316         return (rc);
317 }
318
319         __checkReturn           efx_rc_t
320 efx_phy_fec_type_get(
321         __in            efx_nic_t *enp,
322         __out           efx_phy_fec_type_t *typep)
323 {
324         efx_port_t *epp = &(enp->en_port);
325         const efx_phy_ops_t *epop = epp->ep_epop;
326         efx_rc_t rc;
327
328         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
329
330         if (epop->epo_fec_type_get == NULL) {
331                 rc = ENOTSUP;
332                 goto fail1;
333         }
334
335         if ((rc = epop->epo_fec_type_get(enp, typep)) != 0)
336                 goto fail2;
337
338         return (0);
339
340 fail2:
341         EFSYS_PROBE(fail2);
342 fail1:
343         EFSYS_PROBE1(fail1, efx_rc_t, rc);
344
345         return (rc);
346 }
347
348 #if EFSYS_OPT_PHY_STATS
349
350 #if EFSYS_OPT_NAMES
351
352 /* START MKCONFIG GENERATED PhyStatNamesBlock af9ffa24da3bc100 */
353 static const char * const __efx_phy_stat_name[] = {
354         "oui",
355         "pma_pmd_link_up",
356         "pma_pmd_rx_fault",
357         "pma_pmd_tx_fault",
358         "pma_pmd_rev_a",
359         "pma_pmd_rev_b",
360         "pma_pmd_rev_c",
361         "pma_pmd_rev_d",
362         "pcs_link_up",
363         "pcs_rx_fault",
364         "pcs_tx_fault",
365         "pcs_ber",
366         "pcs_block_errors",
367         "phy_xs_link_up",
368         "phy_xs_rx_fault",
369         "phy_xs_tx_fault",
370         "phy_xs_align",
371         "phy_xs_sync_a",
372         "phy_xs_sync_b",
373         "phy_xs_sync_c",
374         "phy_xs_sync_d",
375         "an_link_up",
376         "an_master",
377         "an_local_rx_ok",
378         "an_remote_rx_ok",
379         "cl22ext_link_up",
380         "snr_a",
381         "snr_b",
382         "snr_c",
383         "snr_d",
384         "pma_pmd_signal_a",
385         "pma_pmd_signal_b",
386         "pma_pmd_signal_c",
387         "pma_pmd_signal_d",
388         "an_complete",
389         "pma_pmd_rev_major",
390         "pma_pmd_rev_minor",
391         "pma_pmd_rev_micro",
392         "pcs_fw_version_0",
393         "pcs_fw_version_1",
394         "pcs_fw_version_2",
395         "pcs_fw_version_3",
396         "pcs_fw_build_yy",
397         "pcs_fw_build_mm",
398         "pcs_fw_build_dd",
399         "pcs_op_mode",
400 };
401
402 /* END MKCONFIG GENERATED PhyStatNamesBlock */
403
404                                         const char *
405 efx_phy_stat_name(
406         __in                            efx_nic_t *enp,
407         __in                            efx_phy_stat_t type)
408 {
409         _NOTE(ARGUNUSED(enp))
410         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
411         EFSYS_ASSERT3U(type, <, EFX_PHY_NSTATS);
412
413         return (__efx_phy_stat_name[type]);
414 }
415
416 #endif  /* EFSYS_OPT_NAMES */
417
418         __checkReturn                   efx_rc_t
419 efx_phy_stats_update(
420         __in                            efx_nic_t *enp,
421         __in                            efsys_mem_t *esmp,
422         __inout_ecount(EFX_PHY_NSTATS)  uint32_t *stat)
423 {
424         efx_port_t *epp = &(enp->en_port);
425         const efx_phy_ops_t *epop = epp->ep_epop;
426
427         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
428         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
429
430         return (epop->epo_stats_update(enp, esmp, stat));
431 }
432
433 #endif  /* EFSYS_OPT_PHY_STATS */
434
435
436 #if EFSYS_OPT_BIST
437
438         __checkReturn           efx_rc_t
439 efx_bist_enable_offline(
440         __in                    efx_nic_t *enp)
441 {
442         efx_port_t *epp = &(enp->en_port);
443         const efx_phy_ops_t *epop = epp->ep_epop;
444         efx_rc_t rc;
445
446         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
447
448         if (epop->epo_bist_enable_offline == NULL) {
449                 rc = ENOTSUP;
450                 goto fail1;
451         }
452
453         if ((rc = epop->epo_bist_enable_offline(enp)) != 0)
454                 goto fail2;
455
456         return (0);
457
458 fail2:
459         EFSYS_PROBE(fail2);
460 fail1:
461         EFSYS_PROBE1(fail1, efx_rc_t, rc);
462
463         return (rc);
464
465 }
466
467         __checkReturn           efx_rc_t
468 efx_bist_start(
469         __in                    efx_nic_t *enp,
470         __in                    efx_bist_type_t type)
471 {
472         efx_port_t *epp = &(enp->en_port);
473         const efx_phy_ops_t *epop = epp->ep_epop;
474         efx_rc_t rc;
475
476         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
477
478         EFSYS_ASSERT3U(type, !=, EFX_BIST_TYPE_UNKNOWN);
479         EFSYS_ASSERT3U(type, <, EFX_BIST_TYPE_NTYPES);
480         EFSYS_ASSERT3U(epp->ep_current_bist, ==, EFX_BIST_TYPE_UNKNOWN);
481
482         if (epop->epo_bist_start == NULL) {
483                 rc = ENOTSUP;
484                 goto fail1;
485         }
486
487         if ((rc = epop->epo_bist_start(enp, type)) != 0)
488                 goto fail2;
489
490         epp->ep_current_bist = type;
491
492         return (0);
493
494 fail2:
495         EFSYS_PROBE(fail2);
496 fail1:
497         EFSYS_PROBE1(fail1, efx_rc_t, rc);
498
499         return (rc);
500 }
501
502         __checkReturn           efx_rc_t
503 efx_bist_poll(
504         __in                    efx_nic_t *enp,
505         __in                    efx_bist_type_t type,
506         __out                   efx_bist_result_t *resultp,
507         __out_opt               uint32_t *value_maskp,
508         __out_ecount_opt(count) unsigned long *valuesp,
509         __in                    size_t count)
510 {
511         efx_port_t *epp = &(enp->en_port);
512         const efx_phy_ops_t *epop = epp->ep_epop;
513         efx_rc_t rc;
514
515         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
516
517         EFSYS_ASSERT3U(type, !=, EFX_BIST_TYPE_UNKNOWN);
518         EFSYS_ASSERT3U(type, <, EFX_BIST_TYPE_NTYPES);
519         EFSYS_ASSERT3U(epp->ep_current_bist, ==, type);
520
521         EFSYS_ASSERT(epop->epo_bist_poll != NULL);
522         if (epop->epo_bist_poll == NULL) {
523                 rc = ENOTSUP;
524                 goto fail1;
525         }
526
527         if ((rc = epop->epo_bist_poll(enp, type, resultp, value_maskp,
528             valuesp, count)) != 0)
529                 goto fail2;
530
531         return (0);
532
533 fail2:
534         EFSYS_PROBE(fail2);
535 fail1:
536         EFSYS_PROBE1(fail1, efx_rc_t, rc);
537
538         return (rc);
539 }
540
541                         void
542 efx_bist_stop(
543         __in            efx_nic_t *enp,
544         __in            efx_bist_type_t type)
545 {
546         efx_port_t *epp = &(enp->en_port);
547         const efx_phy_ops_t *epop = epp->ep_epop;
548
549         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
550
551         EFSYS_ASSERT3U(type, !=, EFX_BIST_TYPE_UNKNOWN);
552         EFSYS_ASSERT3U(type, <, EFX_BIST_TYPE_NTYPES);
553         EFSYS_ASSERT3U(epp->ep_current_bist, ==, type);
554
555         EFSYS_ASSERT(epop->epo_bist_stop != NULL);
556
557         if (epop->epo_bist_stop != NULL)
558                 epop->epo_bist_stop(enp, type);
559
560         epp->ep_current_bist = EFX_BIST_TYPE_UNKNOWN;
561 }
562
563 #endif  /* EFSYS_OPT_BIST */
564                         void
565 efx_phy_unprobe(
566         __in    efx_nic_t *enp)
567 {
568         efx_port_t *epp = &(enp->en_port);
569
570         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
571
572         epp->ep_epop = NULL;
573
574         epp->ep_adv_cap_mask = 0;
575
576         epp->ep_port = 0;
577         epp->ep_phy_type = 0;
578 }