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