a551f0da2b0a011a5745b7e12584599b241b073e
[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 #if EFSYS_OPT_MCDI
189         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_MCDI);
190 #endif  /* EFSYS_OPT_MCDI */
191         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_PROBE));
192
193         enop = enp->en_enop;
194         if ((rc = enop->eno_probe(enp)) != 0)
195                 goto fail1;
196
197         if ((rc = efx_phy_probe(enp)) != 0)
198                 goto fail2;
199
200         enp->en_mod_flags |= EFX_MOD_PROBE;
201
202         return (0);
203
204 fail2:
205         EFSYS_PROBE(fail2);
206
207         enop->eno_unprobe(enp);
208
209 fail1:
210         EFSYS_PROBE1(fail1, efx_rc_t, rc);
211
212         return (rc);
213 }
214
215         __checkReturn   efx_rc_t
216 efx_nic_set_drv_limits(
217         __inout         efx_nic_t *enp,
218         __in            efx_drv_limits_t *edlp)
219 {
220         const efx_nic_ops_t *enop = enp->en_enop;
221         efx_rc_t rc;
222
223         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
224         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
225
226         if (enop->eno_set_drv_limits != NULL) {
227                 if ((rc = enop->eno_set_drv_limits(enp, edlp)) != 0)
228                         goto fail1;
229         }
230
231         return (0);
232
233 fail1:
234         EFSYS_PROBE1(fail1, efx_rc_t, rc);
235
236         return (rc);
237 }
238
239         __checkReturn   efx_rc_t
240 efx_nic_get_bar_region(
241         __in            efx_nic_t *enp,
242         __in            efx_nic_region_t region,
243         __out           uint32_t *offsetp,
244         __out           size_t *sizep)
245 {
246         const efx_nic_ops_t *enop = enp->en_enop;
247         efx_rc_t rc;
248
249         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
250         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
251         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_NIC);
252
253         if (enop->eno_get_bar_region == NULL) {
254                 rc = ENOTSUP;
255                 goto fail1;
256         }
257         if ((rc = (enop->eno_get_bar_region)(enp,
258                     region, offsetp, sizep)) != 0) {
259                 goto fail2;
260         }
261
262         return (0);
263
264 fail2:
265         EFSYS_PROBE(fail2);
266
267 fail1:
268         EFSYS_PROBE1(fail1, efx_rc_t, rc);
269
270         return (rc);
271 }
272
273
274         __checkReturn   efx_rc_t
275 efx_nic_get_vi_pool(
276         __in            efx_nic_t *enp,
277         __out           uint32_t *evq_countp,
278         __out           uint32_t *rxq_countp,
279         __out           uint32_t *txq_countp)
280 {
281         const efx_nic_ops_t *enop = enp->en_enop;
282         efx_nic_cfg_t *encp = &enp->en_nic_cfg;
283         efx_rc_t rc;
284
285         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
286         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
287         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_NIC);
288
289         if (enop->eno_get_vi_pool != NULL) {
290                 uint32_t vi_count = 0;
291
292                 if ((rc = (enop->eno_get_vi_pool)(enp, &vi_count)) != 0)
293                         goto fail1;
294
295                 *evq_countp = vi_count;
296                 *rxq_countp = vi_count;
297                 *txq_countp = vi_count;
298         } else {
299                 /* Use NIC limits as default value */
300                 *evq_countp = encp->enc_evq_limit;
301                 *rxq_countp = encp->enc_rxq_limit;
302                 *txq_countp = encp->enc_txq_limit;
303         }
304
305         return (0);
306
307 fail1:
308         EFSYS_PROBE1(fail1, efx_rc_t, rc);
309
310         return (rc);
311 }
312
313
314         __checkReturn   efx_rc_t
315 efx_nic_init(
316         __in            efx_nic_t *enp)
317 {
318         const efx_nic_ops_t *enop = enp->en_enop;
319         efx_rc_t rc;
320
321         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
322         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
323
324         if (enp->en_mod_flags & EFX_MOD_NIC) {
325                 rc = EINVAL;
326                 goto fail1;
327         }
328
329         if ((rc = enop->eno_init(enp)) != 0)
330                 goto fail2;
331
332         enp->en_mod_flags |= EFX_MOD_NIC;
333
334         return (0);
335
336 fail2:
337         EFSYS_PROBE(fail2);
338 fail1:
339         EFSYS_PROBE1(fail1, efx_rc_t, rc);
340
341         return (rc);
342 }
343
344                         void
345 efx_nic_fini(
346         __in            efx_nic_t *enp)
347 {
348         const efx_nic_ops_t *enop = enp->en_enop;
349
350         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
351         EFSYS_ASSERT(enp->en_mod_flags & EFX_MOD_PROBE);
352         EFSYS_ASSERT(enp->en_mod_flags & EFX_MOD_NIC);
353         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_INTR));
354         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_EV));
355         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_RX));
356         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_TX));
357
358         enop->eno_fini(enp);
359
360         enp->en_mod_flags &= ~EFX_MOD_NIC;
361 }
362
363                         void
364 efx_nic_unprobe(
365         __in            efx_nic_t *enp)
366 {
367         const efx_nic_ops_t *enop = enp->en_enop;
368
369         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
370 #if EFSYS_OPT_MCDI
371         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_MCDI);
372 #endif  /* EFSYS_OPT_MCDI */
373         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_PROBE);
374         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_NIC));
375         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_INTR));
376         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_EV));
377         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_RX));
378         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_TX));
379
380         efx_phy_unprobe(enp);
381
382         enop->eno_unprobe(enp);
383
384         enp->en_mod_flags &= ~EFX_MOD_PROBE;
385 }
386
387                         void
388 efx_nic_destroy(
389         __in    efx_nic_t *enp)
390 {
391         efsys_identifier_t *esip = enp->en_esip;
392
393         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
394         EFSYS_ASSERT3U(enp->en_mod_flags, ==, 0);
395
396         enp->en_family = EFX_FAMILY_INVALID;
397         enp->en_esip = NULL;
398         enp->en_esbp = NULL;
399         enp->en_eslp = NULL;
400
401         enp->en_enop = NULL;
402
403         enp->en_magic = 0;
404
405         /* Free the NIC object */
406         EFSYS_KMEM_FREE(esip, sizeof (efx_nic_t), enp);
407 }
408
409         __checkReturn   efx_rc_t
410 efx_nic_reset(
411         __in            efx_nic_t *enp)
412 {
413         const efx_nic_ops_t *enop = enp->en_enop;
414         unsigned int mod_flags;
415         efx_rc_t rc;
416
417         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
418         EFSYS_ASSERT(enp->en_mod_flags & EFX_MOD_PROBE);
419         /*
420          * All modules except the MCDI, PROBE, NVRAM, VPD, MON
421          * (which we do not reset here) must have been shut down or never
422          * initialized.
423          *
424          * A rule of thumb here is: If the controller or MC reboots, is *any*
425          * state lost. If it's lost and needs reapplying, then the module
426          * *must* not be initialised during the reset.
427          */
428         mod_flags = enp->en_mod_flags;
429         mod_flags &= ~(EFX_MOD_MCDI | EFX_MOD_PROBE | EFX_MOD_NVRAM |
430                     EFX_MOD_VPD | EFX_MOD_MON);
431         EFSYS_ASSERT3U(mod_flags, ==, 0);
432         if (mod_flags != 0) {
433                 rc = EINVAL;
434                 goto fail1;
435         }
436
437         if ((rc = enop->eno_reset(enp)) != 0)
438                 goto fail2;
439
440         return (0);
441
442 fail2:
443         EFSYS_PROBE(fail2);
444 fail1:
445         EFSYS_PROBE1(fail1, efx_rc_t, rc);
446
447         return (rc);
448 }
449
450                         const efx_nic_cfg_t *
451 efx_nic_cfg_get(
452         __in            efx_nic_t *enp)
453 {
454         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
455
456         return (&(enp->en_nic_cfg));
457 }
458
459         __checkReturn   efx_rc_t
460 efx_nic_calculate_pcie_link_bandwidth(
461         __in            uint32_t pcie_link_width,
462         __in            uint32_t pcie_link_gen,
463         __out           uint32_t *bandwidth_mbpsp)
464 {
465         uint32_t lane_bandwidth;
466         uint32_t total_bandwidth;
467         efx_rc_t rc;
468
469         if ((pcie_link_width == 0) || (pcie_link_width > 16) ||
470             !ISP2(pcie_link_width)) {
471                 rc = EINVAL;
472                 goto fail1;
473         }
474
475         switch (pcie_link_gen) {
476         case EFX_PCIE_LINK_SPEED_GEN1:
477                 /* 2.5 Gb/s raw bandwidth with 8b/10b encoding */
478                 lane_bandwidth = 2000;
479                 break;
480         case EFX_PCIE_LINK_SPEED_GEN2:
481                 /* 5.0 Gb/s raw bandwidth with 8b/10b encoding */
482                 lane_bandwidth = 4000;
483                 break;
484         case EFX_PCIE_LINK_SPEED_GEN3:
485                 /* 8.0 Gb/s raw bandwidth with 128b/130b encoding */
486                 lane_bandwidth = 7877;
487                 break;
488         default:
489                 rc = EINVAL;
490                 goto fail2;
491         }
492
493         total_bandwidth = lane_bandwidth * pcie_link_width;
494         *bandwidth_mbpsp = total_bandwidth;
495
496         return (0);
497
498 fail2:
499         EFSYS_PROBE(fail2);
500 fail1:
501         EFSYS_PROBE1(fail1, efx_rc_t, rc);
502
503         return (rc);
504 }
505
506
507         __checkReturn   efx_rc_t
508 efx_nic_check_pcie_link_speed(
509         __in            efx_nic_t *enp,
510         __in            uint32_t pcie_link_width,
511         __in            uint32_t pcie_link_gen,
512         __out           efx_pcie_link_performance_t *resultp)
513 {
514         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
515         uint32_t bandwidth;
516         efx_pcie_link_performance_t result;
517         efx_rc_t rc;
518
519         if ((encp->enc_required_pcie_bandwidth_mbps == 0) ||
520             (pcie_link_width == 0) || (pcie_link_width == 32) ||
521             (pcie_link_gen == 0)) {
522                 /*
523                  * No usable info on what is required and/or in use. In virtual
524                  * machines, sometimes the PCIe link width is reported as 0 or
525                  * 32, or the speed as 0.
526                  */
527                 result = EFX_PCIE_LINK_PERFORMANCE_UNKNOWN_BANDWIDTH;
528                 goto out;
529         }
530
531         /* Calculate the available bandwidth in megabits per second */
532         rc = efx_nic_calculate_pcie_link_bandwidth(pcie_link_width,
533                                             pcie_link_gen, &bandwidth);
534         if (rc != 0)
535                 goto fail1;
536
537         if (bandwidth < encp->enc_required_pcie_bandwidth_mbps) {
538                 result = EFX_PCIE_LINK_PERFORMANCE_SUBOPTIMAL_BANDWIDTH;
539         } else if (pcie_link_gen < encp->enc_max_pcie_link_gen) {
540                 /* The link provides enough bandwidth but not optimal latency */
541                 result = EFX_PCIE_LINK_PERFORMANCE_SUBOPTIMAL_LATENCY;
542         } else {
543                 result = EFX_PCIE_LINK_PERFORMANCE_OPTIMAL;
544         }
545
546 out:
547         *resultp = result;
548
549         return (0);
550
551 fail1:
552         EFSYS_PROBE1(fail1, efx_rc_t, rc);
553
554         return (rc);
555 }