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