net/sfc: use different callbacks for event queues
[dpdk.git] / drivers / net / sfc / sfc_ev.c
1 /*-
2  *   BSD LICENSE
3  *
4  * Copyright (c) 2016-2017 Solarflare Communications Inc.
5  * All rights reserved.
6  *
7  * This software was jointly developed between OKTET Labs (under contract
8  * for Solarflare) and Solarflare Communications, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <rte_debug.h>
33 #include <rte_cycles.h>
34 #include <rte_alarm.h>
35 #include <rte_branch_prediction.h>
36
37 #include "efx.h"
38
39 #include "sfc.h"
40 #include "sfc_debug.h"
41 #include "sfc_log.h"
42 #include "sfc_ev.h"
43 #include "sfc_rx.h"
44 #include "sfc_tx.h"
45 #include "sfc_kvargs.h"
46
47
48 /* Initial delay when waiting for event queue init complete event */
49 #define SFC_EVQ_INIT_BACKOFF_START_US   (1)
50 /* Maximum delay between event queue polling attempts */
51 #define SFC_EVQ_INIT_BACKOFF_MAX_US     (10 * 1000)
52 /* Event queue init approx timeout */
53 #define SFC_EVQ_INIT_TIMEOUT_US         (2 * US_PER_S)
54
55 /* Management event queue polling period in microseconds */
56 #define SFC_MGMT_EV_QPOLL_PERIOD_US     (US_PER_S)
57
58
59 static boolean_t
60 sfc_ev_initialized(void *arg)
61 {
62         struct sfc_evq *evq = arg;
63
64         /* Init done events may be duplicated on SFN7xxx (SFC bug 31631) */
65         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTING ||
66                    evq->init_state == SFC_EVQ_STARTED);
67
68         evq->init_state = SFC_EVQ_STARTED;
69
70         return B_FALSE;
71 }
72
73 static boolean_t
74 sfc_ev_nop_rx(void *arg, uint32_t label, uint32_t id,
75               uint32_t size, uint16_t flags)
76 {
77         struct sfc_evq *evq = arg;
78
79         sfc_err(evq->sa,
80                 "EVQ %u unexpected Rx event label=%u id=%#x size=%u flags=%#x",
81                 evq->evq_index, label, id, size, flags);
82         return B_TRUE;
83 }
84
85 static boolean_t
86 sfc_ev_rx(void *arg, __rte_unused uint32_t label, uint32_t id,
87           uint32_t size, uint16_t flags)
88 {
89         struct sfc_evq *evq = arg;
90         struct sfc_rxq *rxq;
91         unsigned int stop;
92         unsigned int pending_id;
93         unsigned int delta;
94         unsigned int i;
95         struct sfc_rx_sw_desc *rxd;
96
97         if (unlikely(evq->exception))
98                 goto done;
99
100         rxq = evq->rxq;
101
102         SFC_ASSERT(rxq != NULL);
103         SFC_ASSERT(rxq->evq == evq);
104         SFC_ASSERT(rxq->state & SFC_RXQ_STARTED);
105
106         stop = (id + 1) & rxq->ptr_mask;
107         pending_id = rxq->pending & rxq->ptr_mask;
108         delta = (stop >= pending_id) ? (stop - pending_id) :
109                 (rxq->ptr_mask + 1 - pending_id + stop);
110
111         if (delta == 0) {
112                 /*
113                  * Rx event with no new descriptors done and zero length
114                  * is used to abort scattered packet when there is no room
115                  * for the tail.
116                  */
117                 if (unlikely(size != 0)) {
118                         evq->exception = B_TRUE;
119                         sfc_err(evq->sa,
120                                 "EVQ %u RxQ %u invalid RX abort "
121                                 "(id=%#x size=%u flags=%#x); needs restart",
122                                 evq->evq_index, sfc_rxq_sw_index(rxq),
123                                 id, size, flags);
124                         goto done;
125                 }
126
127                 /* Add discard flag to the first fragment */
128                 rxq->sw_desc[pending_id].flags |= EFX_DISCARD;
129                 /* Remove continue flag from the last fragment */
130                 rxq->sw_desc[id].flags &= ~EFX_PKT_CONT;
131         } else if (unlikely(delta > rxq->batch_max)) {
132                 evq->exception = B_TRUE;
133
134                 sfc_err(evq->sa,
135                         "EVQ %u RxQ %u completion out of order "
136                         "(id=%#x delta=%u flags=%#x); needs restart",
137                         evq->evq_index, sfc_rxq_sw_index(rxq), id, delta,
138                         flags);
139
140                 goto done;
141         }
142
143         for (i = pending_id; i != stop; i = (i + 1) & rxq->ptr_mask) {
144                 rxd = &rxq->sw_desc[i];
145
146                 rxd->flags = flags;
147
148                 SFC_ASSERT(size < (1 << 16));
149                 rxd->size = (uint16_t)size;
150         }
151
152         rxq->pending += delta;
153
154 done:
155         return B_FALSE;
156 }
157
158 static boolean_t
159 sfc_ev_nop_tx(void *arg, uint32_t label, uint32_t id)
160 {
161         struct sfc_evq *evq = arg;
162
163         sfc_err(evq->sa, "EVQ %u unexpected Tx event label=%u id=%#x",
164                 evq->evq_index, label, id);
165         return B_TRUE;
166 }
167
168 static boolean_t
169 sfc_ev_tx(void *arg, __rte_unused uint32_t label, uint32_t id)
170 {
171         struct sfc_evq *evq = arg;
172         struct sfc_txq *txq;
173         unsigned int stop;
174         unsigned int delta;
175
176         txq = evq->txq;
177
178         SFC_ASSERT(txq != NULL);
179         SFC_ASSERT(txq->evq == evq);
180
181         if (unlikely((txq->state & SFC_TXQ_STARTED) == 0))
182                 goto done;
183
184         stop = (id + 1) & txq->ptr_mask;
185         id = txq->pending & txq->ptr_mask;
186
187         delta = (stop >= id) ? (stop - id) : (txq->ptr_mask + 1 - id + stop);
188
189         txq->pending += delta;
190
191 done:
192         return B_FALSE;
193 }
194
195 static boolean_t
196 sfc_ev_exception(void *arg, __rte_unused uint32_t code,
197                  __rte_unused uint32_t data)
198 {
199         struct sfc_evq *evq = arg;
200
201         if (code == EFX_EXCEPTION_UNKNOWN_SENSOREVT)
202                 return B_FALSE;
203
204         evq->exception = B_TRUE;
205         sfc_warn(evq->sa,
206                  "hardware exception %s (code=%u, data=%#x) on EVQ %u;"
207                  " needs recovery",
208                  (code == EFX_EXCEPTION_RX_RECOVERY) ? "RX_RECOVERY" :
209                  (code == EFX_EXCEPTION_RX_DSC_ERROR) ? "RX_DSC_ERROR" :
210                  (code == EFX_EXCEPTION_TX_DSC_ERROR) ? "TX_DSC_ERROR" :
211                  (code == EFX_EXCEPTION_FWALERT_SRAM) ? "FWALERT_SRAM" :
212                  (code == EFX_EXCEPTION_UNKNOWN_FWALERT) ? "UNKNOWN_FWALERT" :
213                  (code == EFX_EXCEPTION_RX_ERROR) ? "RX_ERROR" :
214                  (code == EFX_EXCEPTION_TX_ERROR) ? "TX_ERROR" :
215                  (code == EFX_EXCEPTION_EV_ERROR) ? "EV_ERROR" :
216                  "UNKNOWN",
217                  code, data, evq->evq_index);
218
219         return B_TRUE;
220 }
221
222 static boolean_t
223 sfc_ev_nop_rxq_flush_done(void *arg, uint32_t rxq_hw_index)
224 {
225         struct sfc_evq *evq = arg;
226
227         sfc_err(evq->sa, "EVQ %u unexpected RxQ %u flush done",
228                 evq->evq_index, rxq_hw_index);
229         return B_TRUE;
230 }
231
232 static boolean_t
233 sfc_ev_rxq_flush_done(void *arg, __rte_unused uint32_t rxq_hw_index)
234 {
235         struct sfc_evq *evq = arg;
236         struct sfc_rxq *rxq;
237
238         rxq = evq->rxq;
239         SFC_ASSERT(rxq != NULL);
240         SFC_ASSERT(rxq->hw_index == rxq_hw_index);
241         SFC_ASSERT(rxq->evq == evq);
242         sfc_rx_qflush_done(rxq);
243
244         return B_FALSE;
245 }
246
247 static boolean_t
248 sfc_ev_nop_rxq_flush_failed(void *arg, uint32_t rxq_hw_index)
249 {
250         struct sfc_evq *evq = arg;
251
252         sfc_err(evq->sa, "EVQ %u unexpected RxQ %u flush failed",
253                 evq->evq_index, rxq_hw_index);
254         return B_TRUE;
255 }
256
257 static boolean_t
258 sfc_ev_rxq_flush_failed(void *arg, __rte_unused uint32_t rxq_hw_index)
259 {
260         struct sfc_evq *evq = arg;
261         struct sfc_rxq *rxq;
262
263         rxq = evq->rxq;
264         SFC_ASSERT(rxq != NULL);
265         SFC_ASSERT(rxq->hw_index == rxq_hw_index);
266         SFC_ASSERT(rxq->evq == evq);
267         sfc_rx_qflush_failed(rxq);
268
269         return B_FALSE;
270 }
271
272 static boolean_t
273 sfc_ev_nop_txq_flush_done(void *arg, uint32_t txq_hw_index)
274 {
275         struct sfc_evq *evq = arg;
276
277         sfc_err(evq->sa, "EVQ %u unexpected TxQ %u flush done",
278                 evq->evq_index, txq_hw_index);
279         return B_TRUE;
280 }
281
282 static boolean_t
283 sfc_ev_txq_flush_done(void *arg, __rte_unused uint32_t txq_hw_index)
284 {
285         struct sfc_evq *evq = arg;
286         struct sfc_txq *txq;
287
288         txq = evq->txq;
289         SFC_ASSERT(txq != NULL);
290         SFC_ASSERT(txq->hw_index == txq_hw_index);
291         SFC_ASSERT(txq->evq == evq);
292         sfc_tx_qflush_done(txq);
293
294         return B_FALSE;
295 }
296
297 static boolean_t
298 sfc_ev_software(void *arg, uint16_t magic)
299 {
300         struct sfc_evq *evq = arg;
301
302         sfc_err(evq->sa, "EVQ %u unexpected software event magic=%#.4x",
303                 evq->evq_index, magic);
304         return B_TRUE;
305 }
306
307 static boolean_t
308 sfc_ev_sram(void *arg, uint32_t code)
309 {
310         struct sfc_evq *evq = arg;
311
312         sfc_err(evq->sa, "EVQ %u unexpected SRAM event code=%u",
313                 evq->evq_index, code);
314         return B_TRUE;
315 }
316
317 static boolean_t
318 sfc_ev_wake_up(void *arg, uint32_t index)
319 {
320         struct sfc_evq *evq = arg;
321
322         sfc_err(evq->sa, "EVQ %u unexpected wake up event index=%u",
323                 evq->evq_index, index);
324         return B_TRUE;
325 }
326
327 static boolean_t
328 sfc_ev_timer(void *arg, uint32_t index)
329 {
330         struct sfc_evq *evq = arg;
331
332         sfc_err(evq->sa, "EVQ %u unexpected timer event index=%u",
333                 evq->evq_index, index);
334         return B_TRUE;
335 }
336
337 static boolean_t
338 sfc_ev_nop_link_change(void *arg, __rte_unused efx_link_mode_t link_mode)
339 {
340         struct sfc_evq *evq = arg;
341
342         sfc_err(evq->sa, "EVQ %u unexpected link change event",
343                 evq->evq_index);
344         return B_TRUE;
345 }
346
347 static boolean_t
348 sfc_ev_link_change(void *arg, efx_link_mode_t link_mode)
349 {
350         struct sfc_evq *evq = arg;
351         struct sfc_adapter *sa = evq->sa;
352         struct rte_eth_link *dev_link = &sa->eth_dev->data->dev_link;
353         struct rte_eth_link new_link;
354         uint64_t new_link_u64;
355         uint64_t old_link_u64;
356
357         EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
358
359         sfc_port_link_mode_to_info(link_mode, &new_link);
360
361         new_link_u64 = *(uint64_t *)&new_link;
362         do {
363                 old_link_u64 = rte_atomic64_read((rte_atomic64_t *)dev_link);
364                 if (old_link_u64 == new_link_u64)
365                         break;
366
367                 if (rte_atomic64_cmpset((volatile uint64_t *)dev_link,
368                                         old_link_u64, new_link_u64)) {
369                         evq->sa->port.lsc_seq++;
370                         break;
371                 }
372         } while (B_TRUE);
373
374         return B_FALSE;
375 }
376
377 static const efx_ev_callbacks_t sfc_ev_callbacks = {
378         .eec_initialized        = sfc_ev_initialized,
379         .eec_rx                 = sfc_ev_nop_rx,
380         .eec_tx                 = sfc_ev_nop_tx,
381         .eec_exception          = sfc_ev_exception,
382         .eec_rxq_flush_done     = sfc_ev_nop_rxq_flush_done,
383         .eec_rxq_flush_failed   = sfc_ev_nop_rxq_flush_failed,
384         .eec_txq_flush_done     = sfc_ev_nop_txq_flush_done,
385         .eec_software           = sfc_ev_software,
386         .eec_sram               = sfc_ev_sram,
387         .eec_wake_up            = sfc_ev_wake_up,
388         .eec_timer              = sfc_ev_timer,
389         .eec_link_change        = sfc_ev_link_change,
390 };
391
392 static const efx_ev_callbacks_t sfc_ev_callbacks_rx = {
393         .eec_initialized        = sfc_ev_initialized,
394         .eec_rx                 = sfc_ev_rx,
395         .eec_tx                 = sfc_ev_nop_tx,
396         .eec_exception          = sfc_ev_exception,
397         .eec_rxq_flush_done     = sfc_ev_rxq_flush_done,
398         .eec_rxq_flush_failed   = sfc_ev_rxq_flush_failed,
399         .eec_txq_flush_done     = sfc_ev_nop_txq_flush_done,
400         .eec_software           = sfc_ev_software,
401         .eec_sram               = sfc_ev_sram,
402         .eec_wake_up            = sfc_ev_wake_up,
403         .eec_timer              = sfc_ev_timer,
404         .eec_link_change        = sfc_ev_nop_link_change,
405 };
406
407 static const efx_ev_callbacks_t sfc_ev_callbacks_tx = {
408         .eec_initialized        = sfc_ev_initialized,
409         .eec_rx                 = sfc_ev_nop_rx,
410         .eec_tx                 = sfc_ev_tx,
411         .eec_exception          = sfc_ev_exception,
412         .eec_rxq_flush_done     = sfc_ev_nop_rxq_flush_done,
413         .eec_rxq_flush_failed   = sfc_ev_nop_rxq_flush_failed,
414         .eec_txq_flush_done     = sfc_ev_txq_flush_done,
415         .eec_software           = sfc_ev_software,
416         .eec_sram               = sfc_ev_sram,
417         .eec_wake_up            = sfc_ev_wake_up,
418         .eec_timer              = sfc_ev_timer,
419         .eec_link_change        = sfc_ev_nop_link_change,
420 };
421
422
423 void
424 sfc_ev_qpoll(struct sfc_evq *evq)
425 {
426         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
427                    evq->init_state == SFC_EVQ_STARTING);
428
429         /* Synchronize the DMA memory for reading not required */
430
431         efx_ev_qpoll(evq->common, &evq->read_ptr, evq->callbacks, evq);
432
433         if (unlikely(evq->exception) && sfc_adapter_trylock(evq->sa)) {
434                 struct sfc_adapter *sa = evq->sa;
435                 int rc;
436
437                 if ((evq->rxq != NULL) && (evq->rxq->state & SFC_RXQ_RUNNING)) {
438                         unsigned int rxq_sw_index = sfc_rxq_sw_index(evq->rxq);
439
440                         sfc_warn(sa,
441                                  "restart RxQ %u because of exception on its EvQ %u",
442                                  rxq_sw_index, evq->evq_index);
443
444                         sfc_rx_qstop(sa, rxq_sw_index);
445                         rc = sfc_rx_qstart(sa, rxq_sw_index);
446                         if (rc != 0)
447                                 sfc_err(sa, "cannot restart RxQ %u",
448                                         rxq_sw_index);
449                 }
450
451                 if (evq->txq != NULL) {
452                         unsigned int txq_sw_index = sfc_txq_sw_index(evq->txq);
453
454                         sfc_warn(sa,
455                                  "restart TxQ %u because of exception on its EvQ %u",
456                                  txq_sw_index, evq->evq_index);
457
458                         sfc_tx_qstop(sa, txq_sw_index);
459                         rc = sfc_tx_qstart(sa, txq_sw_index);
460                         if (rc != 0)
461                                 sfc_err(sa, "cannot restart TxQ %u",
462                                         txq_sw_index);
463                 }
464
465                 if (evq->exception)
466                         sfc_panic(sa, "unrecoverable exception on EvQ %u",
467                                   evq->evq_index);
468
469                 sfc_adapter_unlock(sa);
470         }
471
472         /* Poll-mode driver does not re-prime the event queue for interrupts */
473 }
474
475 void
476 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
477 {
478         if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
479                 struct sfc_evq *mgmt_evq = sa->evq_info[sa->mgmt_evq_index].evq;
480
481                 if (mgmt_evq->init_state == SFC_EVQ_STARTED)
482                         sfc_ev_qpoll(mgmt_evq);
483
484                 rte_spinlock_unlock(&sa->mgmt_evq_lock);
485         }
486 }
487
488 int
489 sfc_ev_qprime(struct sfc_evq *evq)
490 {
491         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
492         return efx_ev_qprime(evq->common, evq->read_ptr);
493 }
494
495 int
496 sfc_ev_qstart(struct sfc_adapter *sa, unsigned int sw_index)
497 {
498         const struct sfc_evq_info *evq_info;
499         struct sfc_evq *evq;
500         efsys_mem_t *esmp;
501         unsigned int total_delay_us;
502         unsigned int delay_us;
503         int rc;
504
505         sfc_log_init(sa, "sw_index=%u", sw_index);
506
507         evq_info = &sa->evq_info[sw_index];
508         evq = evq_info->evq;
509         esmp = &evq->mem;
510
511         /* Clear all events */
512         (void)memset((void *)esmp->esm_base, 0xff,
513                      EFX_EVQ_SIZE(evq_info->entries));
514
515         /* Create the common code event queue */
516         rc = efx_ev_qcreate(sa->nic, sw_index, esmp, evq_info->entries,
517                             0 /* unused on EF10 */, 0, evq_info->flags,
518                             &evq->common);
519         if (rc != 0)
520                 goto fail_ev_qcreate;
521
522         SFC_ASSERT(evq->rxq == NULL || evq->txq == NULL);
523         if (evq->rxq != 0)
524                 evq->callbacks = &sfc_ev_callbacks_rx;
525         else if (evq->txq != 0)
526                 evq->callbacks = &sfc_ev_callbacks_tx;
527         else
528                 evq->callbacks = &sfc_ev_callbacks;
529
530         evq->init_state = SFC_EVQ_STARTING;
531
532         /* Wait for the initialization event */
533         total_delay_us = 0;
534         delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
535         do {
536                 (void)sfc_ev_qpoll(evq);
537
538                 /* Check to see if the initialization complete indication
539                  * posted by the hardware.
540                  */
541                 if (evq->init_state == SFC_EVQ_STARTED)
542                         goto done;
543
544                 /* Give event queue some time to init */
545                 rte_delay_us(delay_us);
546
547                 total_delay_us += delay_us;
548
549                 /* Exponential backoff */
550                 delay_us *= 2;
551                 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
552                         delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
553
554         } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
555
556         rc = ETIMEDOUT;
557         goto fail_timedout;
558
559 done:
560         return 0;
561
562 fail_timedout:
563         evq->init_state = SFC_EVQ_INITIALIZED;
564         efx_ev_qdestroy(evq->common);
565
566 fail_ev_qcreate:
567         sfc_log_init(sa, "failed %d", rc);
568         return rc;
569 }
570
571 void
572 sfc_ev_qstop(struct sfc_adapter *sa, unsigned int sw_index)
573 {
574         const struct sfc_evq_info *evq_info;
575         struct sfc_evq *evq;
576
577         sfc_log_init(sa, "sw_index=%u", sw_index);
578
579         SFC_ASSERT(sw_index < sa->evq_count);
580
581         evq_info = &sa->evq_info[sw_index];
582         evq = evq_info->evq;
583
584         if (evq == NULL || evq->init_state != SFC_EVQ_STARTED)
585                 return;
586
587         evq->init_state = SFC_EVQ_INITIALIZED;
588         evq->callbacks = NULL;
589         evq->read_ptr = 0;
590         evq->exception = B_FALSE;
591
592         efx_ev_qdestroy(evq->common);
593 }
594
595 static void
596 sfc_ev_mgmt_periodic_qpoll(void *arg)
597 {
598         struct sfc_adapter *sa = arg;
599         int rc;
600
601         sfc_ev_mgmt_qpoll(sa);
602
603         rc = rte_eal_alarm_set(SFC_MGMT_EV_QPOLL_PERIOD_US,
604                                sfc_ev_mgmt_periodic_qpoll, sa);
605         if (rc == -ENOTSUP) {
606                 sfc_warn(sa, "alarms are not supported");
607                 sfc_warn(sa, "management EVQ must be polled indirectly using no-wait link status update");
608         } else if (rc != 0) {
609                 sfc_err(sa,
610                         "cannot rearm management EVQ polling alarm (rc=%d)",
611                         rc);
612         }
613 }
614
615 static void
616 sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter *sa)
617 {
618         sfc_ev_mgmt_periodic_qpoll(sa);
619 }
620
621 static void
622 sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter *sa)
623 {
624         rte_eal_alarm_cancel(sfc_ev_mgmt_periodic_qpoll, sa);
625 }
626
627 int
628 sfc_ev_start(struct sfc_adapter *sa)
629 {
630         int rc;
631
632         sfc_log_init(sa, "entry");
633
634         rc = efx_ev_init(sa->nic);
635         if (rc != 0)
636                 goto fail_ev_init;
637
638         /* Start management EVQ used for global events */
639         rte_spinlock_lock(&sa->mgmt_evq_lock);
640
641         rc = sfc_ev_qstart(sa, sa->mgmt_evq_index);
642         if (rc != 0)
643                 goto fail_mgmt_evq_start;
644
645         if (sa->intr.lsc_intr) {
646                 rc = sfc_ev_qprime(sa->evq_info[sa->mgmt_evq_index].evq);
647                 if (rc != 0)
648                         goto fail_evq0_prime;
649         }
650
651         rte_spinlock_unlock(&sa->mgmt_evq_lock);
652
653         /*
654          * Start management EVQ polling. If interrupts are disabled
655          * (not used), it is required to process link status change
656          * and other device level events to avoid unrecoverable
657          * error because the event queue overflow.
658          */
659         sfc_ev_mgmt_periodic_qpoll_start(sa);
660
661         /*
662          * Rx/Tx event queues are started/stopped when corresponding
663          * Rx/Tx queue is started/stopped.
664          */
665
666         return 0;
667
668 fail_evq0_prime:
669         sfc_ev_qstop(sa, 0);
670
671 fail_mgmt_evq_start:
672         rte_spinlock_unlock(&sa->mgmt_evq_lock);
673         efx_ev_fini(sa->nic);
674
675 fail_ev_init:
676         sfc_log_init(sa, "failed %d", rc);
677         return rc;
678 }
679
680 void
681 sfc_ev_stop(struct sfc_adapter *sa)
682 {
683         unsigned int sw_index;
684
685         sfc_log_init(sa, "entry");
686
687         sfc_ev_mgmt_periodic_qpoll_stop(sa);
688
689         /* Make sure that all event queues are stopped */
690         sw_index = sa->evq_count;
691         while (sw_index-- > 0) {
692                 if (sw_index == sa->mgmt_evq_index) {
693                         /* Locks are required for the management EVQ */
694                         rte_spinlock_lock(&sa->mgmt_evq_lock);
695                         sfc_ev_qstop(sa, sa->mgmt_evq_index);
696                         rte_spinlock_unlock(&sa->mgmt_evq_lock);
697                 } else {
698                         sfc_ev_qstop(sa, sw_index);
699                 }
700         }
701
702         efx_ev_fini(sa->nic);
703 }
704
705 int
706 sfc_ev_qinit(struct sfc_adapter *sa, unsigned int sw_index,
707              unsigned int entries, int socket_id)
708 {
709         struct sfc_evq_info *evq_info;
710         struct sfc_evq *evq;
711         int rc;
712
713         sfc_log_init(sa, "sw_index=%u", sw_index);
714
715         evq_info = &sa->evq_info[sw_index];
716
717         SFC_ASSERT(rte_is_power_of_2(entries));
718         SFC_ASSERT(entries <= evq_info->max_entries);
719         evq_info->entries = entries;
720
721         evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
722                                  socket_id);
723         if (evq == NULL)
724                 return ENOMEM;
725
726         evq->sa = sa;
727         evq->evq_index = sw_index;
728
729         /* Allocate DMA space */
730         rc = sfc_dma_alloc(sa, "evq", sw_index, EFX_EVQ_SIZE(evq_info->entries),
731                            socket_id, &evq->mem);
732         if (rc != 0)
733                 return rc;
734
735         evq->init_state = SFC_EVQ_INITIALIZED;
736
737         evq_info->evq = evq;
738
739         return 0;
740 }
741
742 void
743 sfc_ev_qfini(struct sfc_adapter *sa, unsigned int sw_index)
744 {
745         struct sfc_evq *evq;
746
747         sfc_log_init(sa, "sw_index=%u", sw_index);
748
749         evq = sa->evq_info[sw_index].evq;
750
751         SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
752
753         sa->evq_info[sw_index].evq = NULL;
754
755         sfc_dma_free(sa, &evq->mem);
756
757         rte_free(evq);
758 }
759
760 static int
761 sfc_ev_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
762 {
763         struct sfc_evq_info *evq_info = &sa->evq_info[sw_index];
764         unsigned int max_entries;
765
766         sfc_log_init(sa, "sw_index=%u", sw_index);
767
768         max_entries = sfc_evq_max_entries(sa, sw_index);
769         SFC_ASSERT(rte_is_power_of_2(max_entries));
770
771         evq_info->max_entries = max_entries;
772         evq_info->flags = sa->evq_flags |
773                 ((sa->intr.lsc_intr && sw_index == sa->mgmt_evq_index) ?
774                         EFX_EVQ_FLAGS_NOTIFY_INTERRUPT :
775                         EFX_EVQ_FLAGS_NOTIFY_DISABLED);
776
777         return 0;
778 }
779
780 static int
781 sfc_kvarg_perf_profile_handler(__rte_unused const char *key,
782                                const char *value_str, void *opaque)
783 {
784         uint64_t *value = opaque;
785
786         if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_THROUGHPUT) == 0)
787                 *value = EFX_EVQ_FLAGS_TYPE_THROUGHPUT;
788         else if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_LOW_LATENCY) == 0)
789                 *value = EFX_EVQ_FLAGS_TYPE_LOW_LATENCY;
790         else if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_AUTO) == 0)
791                 *value = EFX_EVQ_FLAGS_TYPE_AUTO;
792         else
793                 return -EINVAL;
794
795         return 0;
796 }
797
798 static void
799 sfc_ev_qfini_info(struct sfc_adapter *sa, unsigned int sw_index)
800 {
801         sfc_log_init(sa, "sw_index=%u", sw_index);
802
803         /* Nothing to cleanup */
804 }
805
806 int
807 sfc_ev_init(struct sfc_adapter *sa)
808 {
809         int rc;
810         unsigned int sw_index;
811
812         sfc_log_init(sa, "entry");
813
814         sa->evq_flags = EFX_EVQ_FLAGS_TYPE_THROUGHPUT;
815         rc = sfc_kvargs_process(sa, SFC_KVARG_PERF_PROFILE,
816                                 sfc_kvarg_perf_profile_handler,
817                                 &sa->evq_flags);
818         if (rc != 0) {
819                 sfc_err(sa, "invalid %s parameter value",
820                         SFC_KVARG_PERF_PROFILE);
821                 goto fail_kvarg_perf_profile;
822         }
823
824         sa->evq_count = sfc_ev_qcount(sa);
825         sa->mgmt_evq_index = 0;
826         rte_spinlock_init(&sa->mgmt_evq_lock);
827
828         /* Allocate EVQ info array */
829         rc = ENOMEM;
830         sa->evq_info = rte_calloc_socket("sfc-evqs", sa->evq_count,
831                                          sizeof(struct sfc_evq_info), 0,
832                                          sa->socket_id);
833         if (sa->evq_info == NULL)
834                 goto fail_evqs_alloc;
835
836         for (sw_index = 0; sw_index < sa->evq_count; ++sw_index) {
837                 rc = sfc_ev_qinit_info(sa, sw_index);
838                 if (rc != 0)
839                         goto fail_ev_qinit_info;
840         }
841
842         rc = sfc_ev_qinit(sa, sa->mgmt_evq_index, SFC_MGMT_EVQ_ENTRIES,
843                           sa->socket_id);
844         if (rc != 0)
845                 goto fail_mgmt_evq_init;
846
847         /*
848          * Rx/Tx event queues are created/destroyed when corresponding
849          * Rx/Tx queue is created/destroyed.
850          */
851
852         return 0;
853
854 fail_mgmt_evq_init:
855 fail_ev_qinit_info:
856         while (sw_index-- > 0)
857                 sfc_ev_qfini_info(sa, sw_index);
858
859         rte_free(sa->evq_info);
860         sa->evq_info = NULL;
861
862 fail_evqs_alloc:
863         sa->evq_count = 0;
864
865 fail_kvarg_perf_profile:
866         sfc_log_init(sa, "failed %d", rc);
867         return rc;
868 }
869
870 void
871 sfc_ev_fini(struct sfc_adapter *sa)
872 {
873         int sw_index;
874
875         sfc_log_init(sa, "entry");
876
877         /* Cleanup all event queues */
878         sw_index = sa->evq_count;
879         while (--sw_index >= 0) {
880                 if (sa->evq_info[sw_index].evq != NULL)
881                         sfc_ev_qfini(sa, sw_index);
882                 sfc_ev_qfini_info(sa, sw_index);
883         }
884
885         rte_free(sa->evq_info);
886         sa->evq_info = NULL;
887         sa->evq_count = 0;
888 }