7c341e42947b7fb37261039988138b638d6b9d7f
[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 #define EFX_PHY_CAP_FEC_REQ_MASK                        \
196         (1U << EFX_PHY_CAP_BASER_FEC_REQUESTED) |       \
197         (1U << EFX_PHY_CAP_RS_FEC_REQUESTED)    |       \
198         (1U << EFX_PHY_CAP_25G_BASER_FEC_REQUESTED)
199
200         __checkReturn   efx_rc_t
201 efx_phy_adv_cap_set(
202         __in            efx_nic_t *enp,
203         __in            uint32_t mask)
204 {
205         efx_port_t *epp = &(enp->en_port);
206         const efx_phy_ops_t *epop = epp->ep_epop;
207         uint32_t old_mask;
208         efx_rc_t rc;
209
210         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
211         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
212
213         /* Ignore don't care bits of FEC (FEC EFX_PHY_CAP_*_REQUESTED) */
214         if ((mask & ~(epp->ep_phy_cap_mask | EFX_PHY_CAP_FEC_REQ_MASK)) != 0) {
215                 rc = ENOTSUP;
216                 goto fail1;
217         }
218
219         if (epp->ep_adv_cap_mask == mask)
220                 goto done;
221
222         old_mask = epp->ep_adv_cap_mask;
223         epp->ep_adv_cap_mask = mask;
224
225         if ((rc = epop->epo_reconfigure(enp)) != 0)
226                 goto fail2;
227
228 done:
229         return (0);
230
231 fail2:
232         EFSYS_PROBE(fail2);
233
234         epp->ep_adv_cap_mask = old_mask;
235         /* Reconfigure for robustness */
236         if (epop->epo_reconfigure(enp) != 0) {
237                 /*
238                  * We may have an inconsistent view of our advertised speed
239                  * capabilities.
240                  */
241                 EFSYS_ASSERT(0);
242         }
243
244 fail1:
245         EFSYS_PROBE1(fail1, efx_rc_t, rc);
246
247         return (rc);
248 }
249
250         void
251 efx_phy_lp_cap_get(
252         __in            efx_nic_t *enp,
253         __out           uint32_t *maskp)
254 {
255         efx_port_t *epp = &(enp->en_port);
256
257         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
258         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
259
260         *maskp = epp->ep_lp_cap_mask;
261 }
262
263         __checkReturn   efx_rc_t
264 efx_phy_oui_get(
265         __in            efx_nic_t *enp,
266         __out           uint32_t *ouip)
267 {
268         efx_port_t *epp = &(enp->en_port);
269         const efx_phy_ops_t *epop = epp->ep_epop;
270
271         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
272         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
273
274         return (epop->epo_oui_get(enp, ouip));
275 }
276
277                         void
278 efx_phy_media_type_get(
279         __in            efx_nic_t *enp,
280         __out           efx_phy_media_type_t *typep)
281 {
282         efx_port_t *epp = &(enp->en_port);
283
284         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
285         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
286
287         if (epp->ep_module_type != EFX_PHY_MEDIA_INVALID)
288                 *typep = epp->ep_module_type;
289         else
290                 *typep = epp->ep_fixed_port_type;
291 }
292
293         __checkReturn           efx_rc_t
294 efx_phy_module_get_info(
295         __in                    efx_nic_t *enp,
296         __in                    uint8_t dev_addr,
297         __in                    uint8_t offset,
298         __in                    uint8_t len,
299         __out_bcount(len)       uint8_t *data)
300 {
301         efx_rc_t rc;
302
303         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
304         EFSYS_ASSERT(data != NULL);
305
306         if ((uint32_t)offset + len > 0xff) {
307                 rc = EINVAL;
308                 goto fail1;
309         }
310
311         if ((rc = efx_mcdi_phy_module_get_info(enp, dev_addr,
312             offset, len, data)) != 0)
313                 goto fail2;
314
315         return (0);
316
317 fail2:
318         EFSYS_PROBE(fail2);
319 fail1:
320         EFSYS_PROBE1(fail1, efx_rc_t, rc);
321
322         return (rc);
323 }
324
325         __checkReturn           efx_rc_t
326 efx_phy_fec_type_get(
327         __in            efx_nic_t *enp,
328         __out           efx_phy_fec_type_t *typep)
329 {
330         efx_port_t *epp = &(enp->en_port);
331         const efx_phy_ops_t *epop = epp->ep_epop;
332         efx_rc_t rc;
333
334         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
335
336         if (epop->epo_fec_type_get == NULL) {
337                 rc = ENOTSUP;
338                 goto fail1;
339         }
340
341         if ((rc = epop->epo_fec_type_get(enp, typep)) != 0)
342                 goto fail2;
343
344         return (0);
345
346 fail2:
347         EFSYS_PROBE(fail2);
348 fail1:
349         EFSYS_PROBE1(fail1, efx_rc_t, rc);
350
351         return (rc);
352 }
353
354 #if EFSYS_OPT_PHY_STATS
355
356 #if EFSYS_OPT_NAMES
357
358 /* START MKCONFIG GENERATED PhyStatNamesBlock af9ffa24da3bc100 */
359 static const char * const __efx_phy_stat_name[] = {
360         "oui",
361         "pma_pmd_link_up",
362         "pma_pmd_rx_fault",
363         "pma_pmd_tx_fault",
364         "pma_pmd_rev_a",
365         "pma_pmd_rev_b",
366         "pma_pmd_rev_c",
367         "pma_pmd_rev_d",
368         "pcs_link_up",
369         "pcs_rx_fault",
370         "pcs_tx_fault",
371         "pcs_ber",
372         "pcs_block_errors",
373         "phy_xs_link_up",
374         "phy_xs_rx_fault",
375         "phy_xs_tx_fault",
376         "phy_xs_align",
377         "phy_xs_sync_a",
378         "phy_xs_sync_b",
379         "phy_xs_sync_c",
380         "phy_xs_sync_d",
381         "an_link_up",
382         "an_master",
383         "an_local_rx_ok",
384         "an_remote_rx_ok",
385         "cl22ext_link_up",
386         "snr_a",
387         "snr_b",
388         "snr_c",
389         "snr_d",
390         "pma_pmd_signal_a",
391         "pma_pmd_signal_b",
392         "pma_pmd_signal_c",
393         "pma_pmd_signal_d",
394         "an_complete",
395         "pma_pmd_rev_major",
396         "pma_pmd_rev_minor",
397         "pma_pmd_rev_micro",
398         "pcs_fw_version_0",
399         "pcs_fw_version_1",
400         "pcs_fw_version_2",
401         "pcs_fw_version_3",
402         "pcs_fw_build_yy",
403         "pcs_fw_build_mm",
404         "pcs_fw_build_dd",
405         "pcs_op_mode",
406 };
407
408 /* END MKCONFIG GENERATED PhyStatNamesBlock */
409
410                                         const char *
411 efx_phy_stat_name(
412         __in                            efx_nic_t *enp,
413         __in                            efx_phy_stat_t type)
414 {
415         _NOTE(ARGUNUSED(enp))
416         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
417         EFSYS_ASSERT3U(type, <, EFX_PHY_NSTATS);
418
419         return (__efx_phy_stat_name[type]);
420 }
421
422 #endif  /* EFSYS_OPT_NAMES */
423
424         __checkReturn                   efx_rc_t
425 efx_phy_stats_update(
426         __in                            efx_nic_t *enp,
427         __in                            efsys_mem_t *esmp,
428         __inout_ecount(EFX_PHY_NSTATS)  uint32_t *stat)
429 {
430         efx_port_t *epp = &(enp->en_port);
431         const efx_phy_ops_t *epop = epp->ep_epop;
432
433         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
434         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PORT);
435
436         return (epop->epo_stats_update(enp, esmp, stat));
437 }
438
439 #endif  /* EFSYS_OPT_PHY_STATS */
440
441
442 #if EFSYS_OPT_BIST
443
444         __checkReturn           efx_rc_t
445 efx_bist_enable_offline(
446         __in                    efx_nic_t *enp)
447 {
448         efx_port_t *epp = &(enp->en_port);
449         const efx_phy_ops_t *epop = epp->ep_epop;
450         efx_rc_t rc;
451
452         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
453
454         if (epop->epo_bist_enable_offline == NULL) {
455                 rc = ENOTSUP;
456                 goto fail1;
457         }
458
459         if ((rc = epop->epo_bist_enable_offline(enp)) != 0)
460                 goto fail2;
461
462         return (0);
463
464 fail2:
465         EFSYS_PROBE(fail2);
466 fail1:
467         EFSYS_PROBE1(fail1, efx_rc_t, rc);
468
469         return (rc);
470
471 }
472
473         __checkReturn           efx_rc_t
474 efx_bist_start(
475         __in                    efx_nic_t *enp,
476         __in                    efx_bist_type_t type)
477 {
478         efx_port_t *epp = &(enp->en_port);
479         const efx_phy_ops_t *epop = epp->ep_epop;
480         efx_rc_t rc;
481
482         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
483
484         EFSYS_ASSERT3U(type, !=, EFX_BIST_TYPE_UNKNOWN);
485         EFSYS_ASSERT3U(type, <, EFX_BIST_TYPE_NTYPES);
486         EFSYS_ASSERT3U(epp->ep_current_bist, ==, EFX_BIST_TYPE_UNKNOWN);
487
488         if (epop->epo_bist_start == NULL) {
489                 rc = ENOTSUP;
490                 goto fail1;
491         }
492
493         if ((rc = epop->epo_bist_start(enp, type)) != 0)
494                 goto fail2;
495
496         epp->ep_current_bist = type;
497
498         return (0);
499
500 fail2:
501         EFSYS_PROBE(fail2);
502 fail1:
503         EFSYS_PROBE1(fail1, efx_rc_t, rc);
504
505         return (rc);
506 }
507
508         __checkReturn           efx_rc_t
509 efx_bist_poll(
510         __in                    efx_nic_t *enp,
511         __in                    efx_bist_type_t type,
512         __out                   efx_bist_result_t *resultp,
513         __out_opt               uint32_t *value_maskp,
514         __out_ecount_opt(count) unsigned long *valuesp,
515         __in                    size_t count)
516 {
517         efx_port_t *epp = &(enp->en_port);
518         const efx_phy_ops_t *epop = epp->ep_epop;
519         efx_rc_t rc;
520
521         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
522
523         EFSYS_ASSERT3U(type, !=, EFX_BIST_TYPE_UNKNOWN);
524         EFSYS_ASSERT3U(type, <, EFX_BIST_TYPE_NTYPES);
525         EFSYS_ASSERT3U(epp->ep_current_bist, ==, type);
526
527         EFSYS_ASSERT(epop->epo_bist_poll != NULL);
528         if (epop->epo_bist_poll == NULL) {
529                 rc = ENOTSUP;
530                 goto fail1;
531         }
532
533         if ((rc = epop->epo_bist_poll(enp, type, resultp, value_maskp,
534             valuesp, count)) != 0)
535                 goto fail2;
536
537         return (0);
538
539 fail2:
540         EFSYS_PROBE(fail2);
541 fail1:
542         EFSYS_PROBE1(fail1, efx_rc_t, rc);
543
544         return (rc);
545 }
546
547                         void
548 efx_bist_stop(
549         __in            efx_nic_t *enp,
550         __in            efx_bist_type_t type)
551 {
552         efx_port_t *epp = &(enp->en_port);
553         const efx_phy_ops_t *epop = epp->ep_epop;
554
555         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
556
557         EFSYS_ASSERT3U(type, !=, EFX_BIST_TYPE_UNKNOWN);
558         EFSYS_ASSERT3U(type, <, EFX_BIST_TYPE_NTYPES);
559         EFSYS_ASSERT3U(epp->ep_current_bist, ==, type);
560
561         EFSYS_ASSERT(epop->epo_bist_stop != NULL);
562
563         if (epop->epo_bist_stop != NULL)
564                 epop->epo_bist_stop(enp, type);
565
566         epp->ep_current_bist = EFX_BIST_TYPE_UNKNOWN;
567 }
568
569 #endif  /* EFSYS_OPT_BIST */
570                         void
571 efx_phy_unprobe(
572         __in    efx_nic_t *enp)
573 {
574         efx_port_t *epp = &(enp->en_port);
575
576         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
577
578         epp->ep_epop = NULL;
579
580         epp->ep_adv_cap_mask = 0;
581
582         epp->ep_port = 0;
583         epp->ep_phy_type = 0;
584 }