net/sfc/base: import 5xxx/6xxx family support
[dpdk.git] / drivers / net / sfc / base / efx_nic.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         __checkReturn   efx_rc_t
35 efx_family(
36         __in            uint16_t venid,
37         __in            uint16_t devid,
38         __out           efx_family_t *efp)
39 {
40         if (venid == EFX_PCI_VENID_SFC) {
41                 switch (devid) {
42 #if EFSYS_OPT_SIENA
43                 case EFX_PCI_DEVID_SIENA_F1_UNINIT:
44                         /*
45                          * Hardware default for PF0 of uninitialised Siena.
46                          * manftest must be able to cope with this device id.
47                          */
48                         *efp = EFX_FAMILY_SIENA;
49                         return (0);
50
51                 case EFX_PCI_DEVID_BETHPAGE:
52                 case EFX_PCI_DEVID_SIENA:
53                         *efp = EFX_FAMILY_SIENA;
54                         return (0);
55 #endif /* EFSYS_OPT_SIENA */
56
57                 case EFX_PCI_DEVID_FALCON:      /* Obsolete, not supported */
58                 default:
59                         break;
60                 }
61         }
62
63         *efp = EFX_FAMILY_INVALID;
64         return (ENOTSUP);
65 }
66
67
68 #define EFX_BIU_MAGIC0  0x01234567
69 #define EFX_BIU_MAGIC1  0xfedcba98
70
71         __checkReturn   efx_rc_t
72 efx_nic_biu_test(
73         __in            efx_nic_t *enp)
74 {
75         efx_oword_t oword;
76         efx_rc_t rc;
77
78         /*
79          * Write magic values to scratch registers 0 and 1, then
80          * verify that the values were written correctly.  Interleave
81          * the accesses to ensure that the BIU is not just reading
82          * back the cached value that was last written.
83          */
84         EFX_POPULATE_OWORD_1(oword, FRF_AZ_DRIVER_DW0, EFX_BIU_MAGIC0);
85         EFX_BAR_TBL_WRITEO(enp, FR_AZ_DRIVER_REG, 0, &oword, B_TRUE);
86
87         EFX_POPULATE_OWORD_1(oword, FRF_AZ_DRIVER_DW0, EFX_BIU_MAGIC1);
88         EFX_BAR_TBL_WRITEO(enp, FR_AZ_DRIVER_REG, 1, &oword, B_TRUE);
89
90         EFX_BAR_TBL_READO(enp, FR_AZ_DRIVER_REG, 0, &oword, B_TRUE);
91         if (EFX_OWORD_FIELD(oword, FRF_AZ_DRIVER_DW0) != EFX_BIU_MAGIC0) {
92                 rc = EIO;
93                 goto fail1;
94         }
95
96         EFX_BAR_TBL_READO(enp, FR_AZ_DRIVER_REG, 1, &oword, B_TRUE);
97         if (EFX_OWORD_FIELD(oword, FRF_AZ_DRIVER_DW0) != EFX_BIU_MAGIC1) {
98                 rc = EIO;
99                 goto fail2;
100         }
101
102         /*
103          * Perform the same test, with the values swapped.  This
104          * ensures that subsequent tests don't start with the correct
105          * values already written into the scratch registers.
106          */
107         EFX_POPULATE_OWORD_1(oword, FRF_AZ_DRIVER_DW0, EFX_BIU_MAGIC1);
108         EFX_BAR_TBL_WRITEO(enp, FR_AZ_DRIVER_REG, 0, &oword, B_TRUE);
109
110         EFX_POPULATE_OWORD_1(oword, FRF_AZ_DRIVER_DW0, EFX_BIU_MAGIC0);
111         EFX_BAR_TBL_WRITEO(enp, FR_AZ_DRIVER_REG, 1, &oword, B_TRUE);
112
113         EFX_BAR_TBL_READO(enp, FR_AZ_DRIVER_REG, 0, &oword, B_TRUE);
114         if (EFX_OWORD_FIELD(oword, FRF_AZ_DRIVER_DW0) != EFX_BIU_MAGIC1) {
115                 rc = EIO;
116                 goto fail3;
117         }
118
119         EFX_BAR_TBL_READO(enp, FR_AZ_DRIVER_REG, 1, &oword, B_TRUE);
120         if (EFX_OWORD_FIELD(oword, FRF_AZ_DRIVER_DW0) != EFX_BIU_MAGIC0) {
121                 rc = EIO;
122                 goto fail4;
123         }
124
125         return (0);
126
127 fail4:
128         EFSYS_PROBE(fail4);
129 fail3:
130         EFSYS_PROBE(fail3);
131 fail2:
132         EFSYS_PROBE(fail2);
133 fail1:
134         EFSYS_PROBE1(fail1, efx_rc_t, rc);
135
136         return (rc);
137 }
138
139 #if EFSYS_OPT_SIENA
140
141 static const efx_nic_ops_t      __efx_nic_siena_ops = {
142         siena_nic_probe,                /* eno_probe */
143         NULL,                           /* eno_board_cfg */
144         NULL,                           /* eno_set_drv_limits */
145         siena_nic_reset,                /* eno_reset */
146         siena_nic_init,                 /* eno_init */
147         NULL,                           /* eno_get_vi_pool */
148         NULL,                           /* eno_get_bar_region */
149         siena_nic_fini,                 /* eno_fini */
150         siena_nic_unprobe,              /* eno_unprobe */
151 };
152
153 #endif  /* EFSYS_OPT_SIENA */
154
155
156         __checkReturn   efx_rc_t
157 efx_nic_create(
158         __in            efx_family_t family,
159         __in            efsys_identifier_t *esip,
160         __in            efsys_bar_t *esbp,
161         __in            efsys_lock_t *eslp,
162         __deref_out     efx_nic_t **enpp)
163 {
164         efx_nic_t *enp;
165         efx_rc_t rc;
166
167         EFSYS_ASSERT3U(family, >, EFX_FAMILY_INVALID);
168         EFSYS_ASSERT3U(family, <, EFX_FAMILY_NTYPES);
169
170         /* Allocate a NIC object */
171         EFSYS_KMEM_ALLOC(esip, sizeof (efx_nic_t), enp);
172
173         if (enp == NULL) {
174                 rc = ENOMEM;
175                 goto fail1;
176         }
177
178         enp->en_magic = EFX_NIC_MAGIC;
179
180         switch (family) {
181 #if EFSYS_OPT_SIENA
182         case EFX_FAMILY_SIENA:
183                 enp->en_enop = &__efx_nic_siena_ops;
184                 enp->en_features =
185                     EFX_FEATURE_IPV6 |
186                     EFX_FEATURE_LFSR_HASH_INSERT |
187                     EFX_FEATURE_LINK_EVENTS |
188                     EFX_FEATURE_PERIODIC_MAC_STATS |
189                     EFX_FEATURE_MCDI |
190                     EFX_FEATURE_LOOKAHEAD_SPLIT |
191                     EFX_FEATURE_MAC_HEADER_FILTERS |
192                     EFX_FEATURE_TX_SRC_FILTERS;
193                 break;
194 #endif  /* EFSYS_OPT_SIENA */
195
196         default:
197                 rc = ENOTSUP;
198                 goto fail2;
199         }
200
201         enp->en_family = family;
202         enp->en_esip = esip;
203         enp->en_esbp = esbp;
204         enp->en_eslp = eslp;
205
206         *enpp = enp;
207
208         return (0);
209
210 fail2:
211         EFSYS_PROBE(fail2);
212
213         enp->en_magic = 0;
214
215         /* Free the NIC object */
216         EFSYS_KMEM_FREE(esip, sizeof (efx_nic_t), enp);
217
218 fail1:
219         EFSYS_PROBE1(fail1, efx_rc_t, rc);
220
221         return (rc);
222 }
223
224         __checkReturn   efx_rc_t
225 efx_nic_probe(
226         __in            efx_nic_t *enp)
227 {
228         const efx_nic_ops_t *enop;
229         efx_rc_t rc;
230
231         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
232 #if EFSYS_OPT_MCDI
233         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_MCDI);
234 #endif  /* EFSYS_OPT_MCDI */
235         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_PROBE));
236
237         enop = enp->en_enop;
238         if ((rc = enop->eno_probe(enp)) != 0)
239                 goto fail1;
240
241         if ((rc = efx_phy_probe(enp)) != 0)
242                 goto fail2;
243
244         enp->en_mod_flags |= EFX_MOD_PROBE;
245
246         return (0);
247
248 fail2:
249         EFSYS_PROBE(fail2);
250
251         enop->eno_unprobe(enp);
252
253 fail1:
254         EFSYS_PROBE1(fail1, efx_rc_t, rc);
255
256         return (rc);
257 }
258
259         __checkReturn   efx_rc_t
260 efx_nic_set_drv_limits(
261         __inout         efx_nic_t *enp,
262         __in            efx_drv_limits_t *edlp)
263 {
264         const efx_nic_ops_t *enop = enp->en_enop;
265         efx_rc_t rc;
266
267         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
268         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
269
270         if (enop->eno_set_drv_limits != NULL) {
271                 if ((rc = enop->eno_set_drv_limits(enp, edlp)) != 0)
272                         goto fail1;
273         }
274
275         return (0);
276
277 fail1:
278         EFSYS_PROBE1(fail1, efx_rc_t, rc);
279
280         return (rc);
281 }
282
283         __checkReturn   efx_rc_t
284 efx_nic_get_bar_region(
285         __in            efx_nic_t *enp,
286         __in            efx_nic_region_t region,
287         __out           uint32_t *offsetp,
288         __out           size_t *sizep)
289 {
290         const efx_nic_ops_t *enop = enp->en_enop;
291         efx_rc_t rc;
292
293         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
294         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
295         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_NIC);
296
297         if (enop->eno_get_bar_region == NULL) {
298                 rc = ENOTSUP;
299                 goto fail1;
300         }
301         if ((rc = (enop->eno_get_bar_region)(enp,
302                     region, offsetp, sizep)) != 0) {
303                 goto fail2;
304         }
305
306         return (0);
307
308 fail2:
309         EFSYS_PROBE(fail2);
310
311 fail1:
312         EFSYS_PROBE1(fail1, efx_rc_t, rc);
313
314         return (rc);
315 }
316
317
318         __checkReturn   efx_rc_t
319 efx_nic_get_vi_pool(
320         __in            efx_nic_t *enp,
321         __out           uint32_t *evq_countp,
322         __out           uint32_t *rxq_countp,
323         __out           uint32_t *txq_countp)
324 {
325         const efx_nic_ops_t *enop = enp->en_enop;
326         efx_nic_cfg_t *encp = &enp->en_nic_cfg;
327         efx_rc_t rc;
328
329         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
330         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
331         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_NIC);
332
333         if (enop->eno_get_vi_pool != NULL) {
334                 uint32_t vi_count = 0;
335
336                 if ((rc = (enop->eno_get_vi_pool)(enp, &vi_count)) != 0)
337                         goto fail1;
338
339                 *evq_countp = vi_count;
340                 *rxq_countp = vi_count;
341                 *txq_countp = vi_count;
342         } else {
343                 /* Use NIC limits as default value */
344                 *evq_countp = encp->enc_evq_limit;
345                 *rxq_countp = encp->enc_rxq_limit;
346                 *txq_countp = encp->enc_txq_limit;
347         }
348
349         return (0);
350
351 fail1:
352         EFSYS_PROBE1(fail1, efx_rc_t, rc);
353
354         return (rc);
355 }
356
357
358         __checkReturn   efx_rc_t
359 efx_nic_init(
360         __in            efx_nic_t *enp)
361 {
362         const efx_nic_ops_t *enop = enp->en_enop;
363         efx_rc_t rc;
364
365         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
366         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
367
368         if (enp->en_mod_flags & EFX_MOD_NIC) {
369                 rc = EINVAL;
370                 goto fail1;
371         }
372
373         if ((rc = enop->eno_init(enp)) != 0)
374                 goto fail2;
375
376         enp->en_mod_flags |= EFX_MOD_NIC;
377
378         return (0);
379
380 fail2:
381         EFSYS_PROBE(fail2);
382 fail1:
383         EFSYS_PROBE1(fail1, efx_rc_t, rc);
384
385         return (rc);
386 }
387
388                         void
389 efx_nic_fini(
390         __in            efx_nic_t *enp)
391 {
392         const efx_nic_ops_t *enop = enp->en_enop;
393
394         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
395         EFSYS_ASSERT(enp->en_mod_flags & EFX_MOD_PROBE);
396         EFSYS_ASSERT(enp->en_mod_flags & EFX_MOD_NIC);
397         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_INTR));
398         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_EV));
399         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_RX));
400         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_TX));
401
402         enop->eno_fini(enp);
403
404         enp->en_mod_flags &= ~EFX_MOD_NIC;
405 }
406
407                         void
408 efx_nic_unprobe(
409         __in            efx_nic_t *enp)
410 {
411         const efx_nic_ops_t *enop = enp->en_enop;
412
413         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
414 #if EFSYS_OPT_MCDI
415         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_MCDI);
416 #endif  /* EFSYS_OPT_MCDI */
417         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
418         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_NIC));
419         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_INTR));
420         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_EV));
421         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_RX));
422         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_TX));
423
424         efx_phy_unprobe(enp);
425
426         enop->eno_unprobe(enp);
427
428         enp->en_mod_flags &= ~EFX_MOD_PROBE;
429 }
430
431                         void
432 efx_nic_destroy(
433         __in    efx_nic_t *enp)
434 {
435         efsys_identifier_t *esip = enp->en_esip;
436
437         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
438         EFSYS_ASSERT3U(enp->en_mod_flags, ==, 0);
439
440         enp->en_family = EFX_FAMILY_INVALID;
441         enp->en_esip = NULL;
442         enp->en_esbp = NULL;
443         enp->en_eslp = NULL;
444
445         enp->en_enop = NULL;
446
447         enp->en_magic = 0;
448
449         /* Free the NIC object */
450         EFSYS_KMEM_FREE(esip, sizeof (efx_nic_t), enp);
451 }
452
453         __checkReturn   efx_rc_t
454 efx_nic_reset(
455         __in            efx_nic_t *enp)
456 {
457         const efx_nic_ops_t *enop = enp->en_enop;
458         unsigned int mod_flags;
459         efx_rc_t rc;
460
461         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
462         EFSYS_ASSERT(enp->en_mod_flags & EFX_MOD_PROBE);
463         /*
464          * All modules except the MCDI, PROBE, NVRAM, VPD, MON
465          * (which we do not reset here) must have been shut down or never
466          * initialized.
467          *
468          * A rule of thumb here is: If the controller or MC reboots, is *any*
469          * state lost. If it's lost and needs reapplying, then the module
470          * *must* not be initialised during the reset.
471          */
472         mod_flags = enp->en_mod_flags;
473         mod_flags &= ~(EFX_MOD_MCDI | EFX_MOD_PROBE | EFX_MOD_NVRAM |
474                     EFX_MOD_VPD | EFX_MOD_MON);
475         EFSYS_ASSERT3U(mod_flags, ==, 0);
476         if (mod_flags != 0) {
477                 rc = EINVAL;
478                 goto fail1;
479         }
480
481         if ((rc = enop->eno_reset(enp)) != 0)
482                 goto fail2;
483
484         return (0);
485
486 fail2:
487         EFSYS_PROBE(fail2);
488 fail1:
489         EFSYS_PROBE1(fail1, efx_rc_t, rc);
490
491         return (rc);
492 }
493
494                         const efx_nic_cfg_t *
495 efx_nic_cfg_get(
496         __in            efx_nic_t *enp)
497 {
498         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
499
500         return (&(enp->en_nic_cfg));
501 }
502
503         __checkReturn   efx_rc_t
504 efx_nic_calculate_pcie_link_bandwidth(
505         __in            uint32_t pcie_link_width,
506         __in            uint32_t pcie_link_gen,
507         __out           uint32_t *bandwidth_mbpsp)
508 {
509         uint32_t lane_bandwidth;
510         uint32_t total_bandwidth;
511         efx_rc_t rc;
512
513         if ((pcie_link_width == 0) || (pcie_link_width > 16) ||
514             !ISP2(pcie_link_width)) {
515                 rc = EINVAL;
516                 goto fail1;
517         }
518
519         switch (pcie_link_gen) {
520         case EFX_PCIE_LINK_SPEED_GEN1:
521                 /* 2.5 Gb/s raw bandwidth with 8b/10b encoding */
522                 lane_bandwidth = 2000;
523                 break;
524         case EFX_PCIE_LINK_SPEED_GEN2:
525                 /* 5.0 Gb/s raw bandwidth with 8b/10b encoding */
526                 lane_bandwidth = 4000;
527                 break;
528         case EFX_PCIE_LINK_SPEED_GEN3:
529                 /* 8.0 Gb/s raw bandwidth with 128b/130b encoding */
530                 lane_bandwidth = 7877;
531                 break;
532         default:
533                 rc = EINVAL;
534                 goto fail2;
535         }
536
537         total_bandwidth = lane_bandwidth * pcie_link_width;
538         *bandwidth_mbpsp = total_bandwidth;
539
540         return (0);
541
542 fail2:
543         EFSYS_PROBE(fail2);
544 fail1:
545         EFSYS_PROBE1(fail1, efx_rc_t, rc);
546
547         return (rc);
548 }
549
550
551         __checkReturn   efx_rc_t
552 efx_nic_check_pcie_link_speed(
553         __in            efx_nic_t *enp,
554         __in            uint32_t pcie_link_width,
555         __in            uint32_t pcie_link_gen,
556         __out           efx_pcie_link_performance_t *resultp)
557 {
558         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
559         uint32_t bandwidth;
560         efx_pcie_link_performance_t result;
561         efx_rc_t rc;
562
563         if ((encp->enc_required_pcie_bandwidth_mbps == 0) ||
564             (pcie_link_width == 0) || (pcie_link_width == 32) ||
565             (pcie_link_gen == 0)) {
566                 /*
567                  * No usable info on what is required and/or in use. In virtual
568                  * machines, sometimes the PCIe link width is reported as 0 or
569                  * 32, or the speed as 0.
570                  */
571                 result = EFX_PCIE_LINK_PERFORMANCE_UNKNOWN_BANDWIDTH;
572                 goto out;
573         }
574
575         /* Calculate the available bandwidth in megabits per second */
576         rc = efx_nic_calculate_pcie_link_bandwidth(pcie_link_width,
577                                             pcie_link_gen, &bandwidth);
578         if (rc != 0)
579                 goto fail1;
580
581         if (bandwidth < encp->enc_required_pcie_bandwidth_mbps) {
582                 result = EFX_PCIE_LINK_PERFORMANCE_SUBOPTIMAL_BANDWIDTH;
583         } else if (pcie_link_gen < encp->enc_max_pcie_link_gen) {
584                 /* The link provides enough bandwidth but not optimal latency */
585                 result = EFX_PCIE_LINK_PERFORMANCE_SUBOPTIMAL_LATENCY;
586         } else {
587                 result = EFX_PCIE_LINK_PERFORMANCE_OPTIMAL;
588         }
589
590 out:
591         *resultp = result;
592
593         return (0);
594
595 fail1:
596         EFSYS_PROBE1(fail1, efx_rc_t, rc);
597
598         return (rc);
599 }