net/sfc/base: import SFN7xxx family support
[dpdk.git] / drivers / net / sfc / base / efx_intr.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
35 #if EFSYS_OPT_SIENA
36
37 static  __checkReturn   efx_rc_t
38 siena_intr_init(
39         __in            efx_nic_t *enp,
40         __in            efx_intr_type_t type,
41         __in            efsys_mem_t *esmp);
42
43 static                  void
44 siena_intr_enable(
45         __in            efx_nic_t *enp);
46
47 static                  void
48 siena_intr_disable(
49         __in            efx_nic_t *enp);
50
51 static                  void
52 siena_intr_disable_unlocked(
53         __in            efx_nic_t *enp);
54
55 static  __checkReturn   efx_rc_t
56 siena_intr_trigger(
57         __in            efx_nic_t *enp,
58         __in            unsigned int level);
59
60 static                  void
61 siena_intr_fini(
62         __in            efx_nic_t *enp);
63
64 static                  void
65 siena_intr_status_line(
66         __in            efx_nic_t *enp,
67         __out           boolean_t *fatalp,
68         __out           uint32_t *qmaskp);
69
70 static                  void
71 siena_intr_status_message(
72         __in            efx_nic_t *enp,
73         __in            unsigned int message,
74         __out           boolean_t *fatalp);
75
76 static                  void
77 siena_intr_fatal(
78         __in            efx_nic_t *enp);
79
80 static  __checkReturn   boolean_t
81 siena_intr_check_fatal(
82         __in            efx_nic_t *enp);
83
84
85 #endif /* EFSYS_OPT_SIENA */
86
87
88 #if EFSYS_OPT_SIENA
89 static const efx_intr_ops_t     __efx_intr_siena_ops = {
90         siena_intr_init,                /* eio_init */
91         siena_intr_enable,              /* eio_enable */
92         siena_intr_disable,             /* eio_disable */
93         siena_intr_disable_unlocked,    /* eio_disable_unlocked */
94         siena_intr_trigger,             /* eio_trigger */
95         siena_intr_status_line,         /* eio_status_line */
96         siena_intr_status_message,      /* eio_status_message */
97         siena_intr_fatal,               /* eio_fatal */
98         siena_intr_fini,                /* eio_fini */
99 };
100 #endif  /* EFSYS_OPT_SIENA */
101
102 #if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
103 static const efx_intr_ops_t     __efx_intr_ef10_ops = {
104         ef10_intr_init,                 /* eio_init */
105         ef10_intr_enable,               /* eio_enable */
106         ef10_intr_disable,              /* eio_disable */
107         ef10_intr_disable_unlocked,     /* eio_disable_unlocked */
108         ef10_intr_trigger,              /* eio_trigger */
109         ef10_intr_status_line,          /* eio_status_line */
110         ef10_intr_status_message,       /* eio_status_message */
111         ef10_intr_fatal,                /* eio_fatal */
112         ef10_intr_fini,                 /* eio_fini */
113 };
114 #endif  /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD */
115
116         __checkReturn   efx_rc_t
117 efx_intr_init(
118         __in            efx_nic_t *enp,
119         __in            efx_intr_type_t type,
120         __in            efsys_mem_t *esmp)
121 {
122         efx_intr_t *eip = &(enp->en_intr);
123         const efx_intr_ops_t *eiop;
124         efx_rc_t rc;
125
126         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
127         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_NIC);
128
129         if (enp->en_mod_flags & EFX_MOD_INTR) {
130                 rc = EINVAL;
131                 goto fail1;
132         }
133
134         eip->ei_esmp = esmp;
135         eip->ei_type = type;
136         eip->ei_level = 0;
137
138         enp->en_mod_flags |= EFX_MOD_INTR;
139
140         switch (enp->en_family) {
141 #if EFSYS_OPT_SIENA
142         case EFX_FAMILY_SIENA:
143                 eiop = &__efx_intr_siena_ops;
144                 break;
145 #endif  /* EFSYS_OPT_SIENA */
146
147 #if EFSYS_OPT_HUNTINGTON
148         case EFX_FAMILY_HUNTINGTON:
149                 eiop = &__efx_intr_ef10_ops;
150                 break;
151 #endif  /* EFSYS_OPT_HUNTINGTON */
152
153         default:
154                 EFSYS_ASSERT(B_FALSE);
155                 rc = ENOTSUP;
156                 goto fail2;
157         }
158
159         if ((rc = eiop->eio_init(enp, type, esmp)) != 0)
160                 goto fail3;
161
162         eip->ei_eiop = eiop;
163
164         return (0);
165
166 fail3:
167         EFSYS_PROBE(fail3);
168 fail2:
169         EFSYS_PROBE(fail2);
170 fail1:
171         EFSYS_PROBE1(fail1, efx_rc_t, rc);
172
173         return (rc);
174 }
175
176                 void
177 efx_intr_fini(
178         __in    efx_nic_t *enp)
179 {
180         efx_intr_t *eip = &(enp->en_intr);
181         const efx_intr_ops_t *eiop = eip->ei_eiop;
182
183         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
184         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_NIC);
185         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_INTR);
186
187         eiop->eio_fini(enp);
188
189         enp->en_mod_flags &= ~EFX_MOD_INTR;
190 }
191
192                         void
193 efx_intr_enable(
194         __in            efx_nic_t *enp)
195 {
196         efx_intr_t *eip = &(enp->en_intr);
197         const efx_intr_ops_t *eiop = eip->ei_eiop;
198
199         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
200         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_INTR);
201
202         eiop->eio_enable(enp);
203 }
204
205                         void
206 efx_intr_disable(
207         __in            efx_nic_t *enp)
208 {
209         efx_intr_t *eip = &(enp->en_intr);
210         const efx_intr_ops_t *eiop = eip->ei_eiop;
211
212         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
213         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_INTR);
214
215         eiop->eio_disable(enp);
216 }
217
218                         void
219 efx_intr_disable_unlocked(
220         __in            efx_nic_t *enp)
221 {
222         efx_intr_t *eip = &(enp->en_intr);
223         const efx_intr_ops_t *eiop = eip->ei_eiop;
224
225         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
226         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_INTR);
227
228         eiop->eio_disable_unlocked(enp);
229 }
230
231
232         __checkReturn   efx_rc_t
233 efx_intr_trigger(
234         __in            efx_nic_t *enp,
235         __in            unsigned int level)
236 {
237         efx_intr_t *eip = &(enp->en_intr);
238         const efx_intr_ops_t *eiop = eip->ei_eiop;
239
240         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
241         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_INTR);
242
243         return (eiop->eio_trigger(enp, level));
244 }
245
246                         void
247 efx_intr_status_line(
248         __in            efx_nic_t *enp,
249         __out           boolean_t *fatalp,
250         __out           uint32_t *qmaskp)
251 {
252         efx_intr_t *eip = &(enp->en_intr);
253         const efx_intr_ops_t *eiop = eip->ei_eiop;
254
255         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
256         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_INTR);
257
258         eiop->eio_status_line(enp, fatalp, qmaskp);
259 }
260
261                         void
262 efx_intr_status_message(
263         __in            efx_nic_t *enp,
264         __in            unsigned int message,
265         __out           boolean_t *fatalp)
266 {
267         efx_intr_t *eip = &(enp->en_intr);
268         const efx_intr_ops_t *eiop = eip->ei_eiop;
269
270         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
271         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_INTR);
272
273         eiop->eio_status_message(enp, message, fatalp);
274 }
275
276                 void
277 efx_intr_fatal(
278         __in    efx_nic_t *enp)
279 {
280         efx_intr_t *eip = &(enp->en_intr);
281         const efx_intr_ops_t *eiop = eip->ei_eiop;
282
283         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
284         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_INTR);
285
286         eiop->eio_fatal(enp);
287 }
288
289
290 /* ************************************************************************* */
291 /* ************************************************************************* */
292 /* ************************************************************************* */
293
294 #if EFSYS_OPT_SIENA
295
296 static  __checkReturn   efx_rc_t
297 siena_intr_init(
298         __in            efx_nic_t *enp,
299         __in            efx_intr_type_t type,
300         __in            efsys_mem_t *esmp)
301 {
302         efx_intr_t *eip = &(enp->en_intr);
303         efx_oword_t oword;
304
305         /*
306          * bug17213 workaround.
307          *
308          * Under legacy interrupts, don't share a level between fatal
309          * interrupts and event queue interrupts. Under MSI-X, they
310          * must share, or we won't get an interrupt.
311          */
312         if (enp->en_family == EFX_FAMILY_SIENA &&
313             eip->ei_type == EFX_INTR_LINE)
314                 eip->ei_level = 0x1f;
315         else
316                 eip->ei_level = 0;
317
318         /* Enable all the genuinely fatal interrupts */
319         EFX_SET_OWORD(oword);
320         EFX_SET_OWORD_FIELD(oword, FRF_AZ_ILL_ADR_INT_KER_EN, 0);
321         EFX_SET_OWORD_FIELD(oword, FRF_AZ_RBUF_OWN_INT_KER_EN, 0);
322         EFX_SET_OWORD_FIELD(oword, FRF_AZ_TBUF_OWN_INT_KER_EN, 0);
323         if (enp->en_family >= EFX_FAMILY_SIENA)
324                 EFX_SET_OWORD_FIELD(oword, FRF_CZ_SRAM_PERR_INT_P_KER_EN, 0);
325         EFX_BAR_WRITEO(enp, FR_AZ_FATAL_INTR_REG_KER, &oword);
326
327         /* Set up the interrupt address register */
328         EFX_POPULATE_OWORD_3(oword,
329             FRF_AZ_NORM_INT_VEC_DIS_KER, (type == EFX_INTR_MESSAGE) ? 1 : 0,
330             FRF_AZ_INT_ADR_KER_DW0, EFSYS_MEM_ADDR(esmp) & 0xffffffff,
331             FRF_AZ_INT_ADR_KER_DW1, EFSYS_MEM_ADDR(esmp) >> 32);
332         EFX_BAR_WRITEO(enp, FR_AZ_INT_ADR_REG_KER, &oword);
333
334         return (0);
335 }
336
337 static                  void
338 siena_intr_enable(
339         __in            efx_nic_t *enp)
340 {
341         efx_intr_t *eip = &(enp->en_intr);
342         efx_oword_t oword;
343
344         EFX_BAR_READO(enp, FR_AZ_INT_EN_REG_KER, &oword);
345
346         EFX_SET_OWORD_FIELD(oword, FRF_AZ_KER_INT_LEVE_SEL, eip->ei_level);
347         EFX_SET_OWORD_FIELD(oword, FRF_AZ_DRV_INT_EN_KER, 1);
348         EFX_BAR_WRITEO(enp, FR_AZ_INT_EN_REG_KER, &oword);
349 }
350
351 static                  void
352 siena_intr_disable(
353         __in            efx_nic_t *enp)
354 {
355         efx_oword_t oword;
356
357         EFX_BAR_READO(enp, FR_AZ_INT_EN_REG_KER, &oword);
358         EFX_SET_OWORD_FIELD(oword, FRF_AZ_DRV_INT_EN_KER, 0);
359         EFX_BAR_WRITEO(enp, FR_AZ_INT_EN_REG_KER, &oword);
360
361         EFSYS_SPIN(10);
362 }
363
364 static                  void
365 siena_intr_disable_unlocked(
366         __in            efx_nic_t *enp)
367 {
368         efx_oword_t oword;
369
370         EFSYS_BAR_READO(enp->en_esbp, FR_AZ_INT_EN_REG_KER_OFST,
371                         &oword, B_FALSE);
372         EFX_SET_OWORD_FIELD(oword, FRF_AZ_DRV_INT_EN_KER, 0);
373         EFSYS_BAR_WRITEO(enp->en_esbp, FR_AZ_INT_EN_REG_KER_OFST,
374             &oword, B_FALSE);
375 }
376
377 static  __checkReturn   efx_rc_t
378 siena_intr_trigger(
379         __in            efx_nic_t *enp,
380         __in            unsigned int level)
381 {
382         efx_intr_t *eip = &(enp->en_intr);
383         efx_oword_t oword;
384         unsigned int count;
385         uint32_t sel;
386         efx_rc_t rc;
387
388         /* bug16757: No event queues can be initialized */
389         EFSYS_ASSERT(!(enp->en_mod_flags & EFX_MOD_EV));
390
391         if (level >= EFX_NINTR_SIENA) {
392                 rc = EINVAL;
393                 goto fail1;
394         }
395
396         if (level > EFX_MASK32(FRF_AZ_KER_INT_LEVE_SEL))
397                 return (ENOTSUP); /* avoid EFSYS_PROBE() */
398
399         sel = level;
400
401         /* Trigger a test interrupt */
402         EFX_BAR_READO(enp, FR_AZ_INT_EN_REG_KER, &oword);
403         EFX_SET_OWORD_FIELD(oword, FRF_AZ_KER_INT_LEVE_SEL, sel);
404         EFX_SET_OWORD_FIELD(oword, FRF_AZ_KER_INT_KER, 1);
405         EFX_BAR_WRITEO(enp, FR_AZ_INT_EN_REG_KER, &oword);
406
407         /*
408          * Wait up to 100ms for the interrupt to be raised before restoring
409          * KER_INT_LEVE_SEL. Ignore a failure to raise (the caller will
410          * observe this soon enough anyway), but always reset KER_INT_LEVE_SEL
411          */
412         count = 0;
413         do {
414                 EFSYS_SPIN(100);        /* 100us */
415
416                 EFX_BAR_READO(enp, FR_AZ_INT_EN_REG_KER, &oword);
417         } while (EFX_OWORD_FIELD(oword, FRF_AZ_KER_INT_KER) && ++count < 1000);
418
419         EFX_SET_OWORD_FIELD(oword, FRF_AZ_KER_INT_LEVE_SEL, eip->ei_level);
420         EFX_BAR_WRITEO(enp, FR_AZ_INT_EN_REG_KER, &oword);
421
422         return (0);
423
424 fail1:
425         EFSYS_PROBE1(fail1, efx_rc_t, rc);
426
427         return (rc);
428 }
429
430 static  __checkReturn   boolean_t
431 siena_intr_check_fatal(
432         __in            efx_nic_t *enp)
433 {
434         efx_intr_t *eip = &(enp->en_intr);
435         efsys_mem_t *esmp = eip->ei_esmp;
436         efx_oword_t oword;
437
438         /* Read the syndrome */
439         EFSYS_MEM_READO(esmp, 0, &oword);
440
441         if (EFX_OWORD_FIELD(oword, FSF_AZ_NET_IVEC_FATAL_INT) != 0) {
442                 EFSYS_PROBE(fatal);
443
444                 /* Clear the fatal interrupt condition */
445                 EFX_SET_OWORD_FIELD(oword, FSF_AZ_NET_IVEC_FATAL_INT, 0);
446                 EFSYS_MEM_WRITEO(esmp, 0, &oword);
447
448                 return (B_TRUE);
449         }
450
451         return (B_FALSE);
452 }
453
454 static                  void
455 siena_intr_status_line(
456         __in            efx_nic_t *enp,
457         __out           boolean_t *fatalp,
458         __out           uint32_t *qmaskp)
459 {
460         efx_intr_t *eip = &(enp->en_intr);
461         efx_dword_t dword;
462
463         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
464         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_INTR);
465
466         /*
467          * Read the queue mask and implicitly acknowledge the
468          * interrupt.
469          */
470         EFX_BAR_READD(enp, FR_BZ_INT_ISR0_REG, &dword, B_FALSE);
471         *qmaskp = EFX_DWORD_FIELD(dword, EFX_DWORD_0);
472
473         EFSYS_PROBE1(qmask, uint32_t, *qmaskp);
474
475         if (*qmaskp & (1U << eip->ei_level))
476                 *fatalp = siena_intr_check_fatal(enp);
477         else
478                 *fatalp = B_FALSE;
479 }
480
481 static                  void
482 siena_intr_status_message(
483         __in            efx_nic_t *enp,
484         __in            unsigned int message,
485         __out           boolean_t *fatalp)
486 {
487         efx_intr_t *eip = &(enp->en_intr);
488
489         EFSYS_ASSERT3U(enp->en_magic, ==, EFX_NIC_MAGIC);
490         EFSYS_ASSERT3U(enp->en_mod_flags, &, EFX_MOD_INTR);
491
492         if (message == eip->ei_level)
493                 *fatalp = siena_intr_check_fatal(enp);
494         else
495                 *fatalp = B_FALSE;
496 }
497
498
499 static          void
500 siena_intr_fatal(
501         __in    efx_nic_t *enp)
502 {
503 #if EFSYS_OPT_DECODE_INTR_FATAL
504         efx_oword_t fatal;
505         efx_oword_t mem_per;
506
507         EFX_BAR_READO(enp, FR_AZ_FATAL_INTR_REG_KER, &fatal);
508         EFX_ZERO_OWORD(mem_per);
509
510         if (EFX_OWORD_FIELD(fatal, FRF_AZ_SRM_PERR_INT_KER) != 0 ||
511             EFX_OWORD_FIELD(fatal, FRF_AZ_MEM_PERR_INT_KER) != 0)
512                 EFX_BAR_READO(enp, FR_AZ_MEM_STAT_REG, &mem_per);
513
514         if (EFX_OWORD_FIELD(fatal, FRF_AZ_SRAM_OOB_INT_KER) != 0)
515                 EFSYS_ERR(enp->en_esip, EFX_ERR_SRAM_OOB, 0, 0);
516
517         if (EFX_OWORD_FIELD(fatal, FRF_AZ_BUFID_DC_OOB_INT_KER) != 0)
518                 EFSYS_ERR(enp->en_esip, EFX_ERR_BUFID_DC_OOB, 0, 0);
519
520         if (EFX_OWORD_FIELD(fatal, FRF_AZ_MEM_PERR_INT_KER) != 0)
521                 EFSYS_ERR(enp->en_esip, EFX_ERR_MEM_PERR,
522                     EFX_OWORD_FIELD(mem_per, EFX_DWORD_0),
523                     EFX_OWORD_FIELD(mem_per, EFX_DWORD_1));
524
525         if (EFX_OWORD_FIELD(fatal, FRF_AZ_RBUF_OWN_INT_KER) != 0)
526                 EFSYS_ERR(enp->en_esip, EFX_ERR_RBUF_OWN, 0, 0);
527
528         if (EFX_OWORD_FIELD(fatal, FRF_AZ_TBUF_OWN_INT_KER) != 0)
529                 EFSYS_ERR(enp->en_esip, EFX_ERR_TBUF_OWN, 0, 0);
530
531         if (EFX_OWORD_FIELD(fatal, FRF_AZ_RDESCQ_OWN_INT_KER) != 0)
532                 EFSYS_ERR(enp->en_esip, EFX_ERR_RDESQ_OWN, 0, 0);
533
534         if (EFX_OWORD_FIELD(fatal, FRF_AZ_TDESCQ_OWN_INT_KER) != 0)
535                 EFSYS_ERR(enp->en_esip, EFX_ERR_TDESQ_OWN, 0, 0);
536
537         if (EFX_OWORD_FIELD(fatal, FRF_AZ_EVQ_OWN_INT_KER) != 0)
538                 EFSYS_ERR(enp->en_esip, EFX_ERR_EVQ_OWN, 0, 0);
539
540         if (EFX_OWORD_FIELD(fatal, FRF_AZ_EVF_OFLO_INT_KER) != 0)
541                 EFSYS_ERR(enp->en_esip, EFX_ERR_EVFF_OFLO, 0, 0);
542
543         if (EFX_OWORD_FIELD(fatal, FRF_AZ_ILL_ADR_INT_KER) != 0)
544                 EFSYS_ERR(enp->en_esip, EFX_ERR_ILL_ADDR, 0, 0);
545
546         if (EFX_OWORD_FIELD(fatal, FRF_AZ_SRM_PERR_INT_KER) != 0)
547                 EFSYS_ERR(enp->en_esip, EFX_ERR_SRAM_PERR,
548                     EFX_OWORD_FIELD(mem_per, EFX_DWORD_0),
549                     EFX_OWORD_FIELD(mem_per, EFX_DWORD_1));
550 #else
551         EFSYS_ASSERT(0);
552 #endif
553 }
554
555 static          void
556 siena_intr_fini(
557         __in    efx_nic_t *enp)
558 {
559         efx_oword_t oword;
560
561         /* Clear the interrupt address register */
562         EFX_ZERO_OWORD(oword);
563         EFX_BAR_WRITEO(enp, FR_AZ_INT_ADR_REG_KER, &oword);
564 }
565
566 #endif /* EFSYS_OPT_SIENA */