412645a0e2a018722f7e7831f74ffbea07954b17
[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_rx(void *arg, __rte_unused uint32_t label, uint32_t id,
75           uint32_t size, uint16_t flags)
76 {
77         struct sfc_evq *evq = arg;
78         struct sfc_rxq *rxq;
79         unsigned int stop;
80         unsigned int pending_id;
81         unsigned int delta;
82         unsigned int i;
83         struct sfc_rx_sw_desc *rxd;
84
85         if (unlikely(evq->exception))
86                 goto done;
87
88         rxq = evq->rxq;
89
90         SFC_ASSERT(rxq != NULL);
91         SFC_ASSERT(rxq->evq == evq);
92         SFC_ASSERT(rxq->state & SFC_RXQ_STARTED);
93
94         stop = (id + 1) & rxq->ptr_mask;
95         pending_id = rxq->pending & rxq->ptr_mask;
96         delta = (stop >= pending_id) ? (stop - pending_id) :
97                 (rxq->ptr_mask + 1 - pending_id + stop);
98
99         if (delta == 0) {
100                 /*
101                  * Rx event with no new descriptors done and zero length
102                  * is used to abort scattered packet when there is no room
103                  * for the tail.
104                  */
105                 if (unlikely(size != 0)) {
106                         evq->exception = B_TRUE;
107                         sfc_err(evq->sa,
108                                 "EVQ %u RxQ %u invalid RX abort "
109                                 "(id=%#x size=%u flags=%#x); needs restart",
110                                 evq->evq_index, sfc_rxq_sw_index(rxq),
111                                 id, size, flags);
112                         goto done;
113                 }
114
115                 /* Add discard flag to the first fragment */
116                 rxq->sw_desc[pending_id].flags |= EFX_DISCARD;
117                 /* Remove continue flag from the last fragment */
118                 rxq->sw_desc[id].flags &= ~EFX_PKT_CONT;
119         } else if (unlikely(delta > rxq->batch_max)) {
120                 evq->exception = B_TRUE;
121
122                 sfc_err(evq->sa,
123                         "EVQ %u RxQ %u completion out of order "
124                         "(id=%#x delta=%u flags=%#x); needs restart",
125                         evq->evq_index, sfc_rxq_sw_index(rxq), id, delta,
126                         flags);
127
128                 goto done;
129         }
130
131         for (i = pending_id; i != stop; i = (i + 1) & rxq->ptr_mask) {
132                 rxd = &rxq->sw_desc[i];
133
134                 rxd->flags = flags;
135
136                 SFC_ASSERT(size < (1 << 16));
137                 rxd->size = (uint16_t)size;
138         }
139
140         rxq->pending += delta;
141
142 done:
143         return B_FALSE;
144 }
145
146 static boolean_t
147 sfc_ev_tx(void *arg, __rte_unused uint32_t label, uint32_t id)
148 {
149         struct sfc_evq *evq = arg;
150         struct sfc_txq *txq;
151         unsigned int stop;
152         unsigned int delta;
153
154         txq = evq->txq;
155
156         SFC_ASSERT(txq != NULL);
157         SFC_ASSERT(txq->evq == evq);
158
159         if (unlikely((txq->state & SFC_TXQ_STARTED) == 0))
160                 goto done;
161
162         stop = (id + 1) & txq->ptr_mask;
163         id = txq->pending & txq->ptr_mask;
164
165         delta = (stop >= id) ? (stop - id) : (txq->ptr_mask + 1 - id + stop);
166
167         txq->pending += delta;
168
169 done:
170         return B_FALSE;
171 }
172
173 static boolean_t
174 sfc_ev_exception(void *arg, __rte_unused uint32_t code,
175                  __rte_unused uint32_t data)
176 {
177         struct sfc_evq *evq = arg;
178
179         if (code == EFX_EXCEPTION_UNKNOWN_SENSOREVT)
180                 return B_FALSE;
181
182         evq->exception = B_TRUE;
183         sfc_warn(evq->sa,
184                  "hardware exception %s (code=%u, data=%#x) on EVQ %u;"
185                  " needs recovery",
186                  (code == EFX_EXCEPTION_RX_RECOVERY) ? "RX_RECOVERY" :
187                  (code == EFX_EXCEPTION_RX_DSC_ERROR) ? "RX_DSC_ERROR" :
188                  (code == EFX_EXCEPTION_TX_DSC_ERROR) ? "TX_DSC_ERROR" :
189                  (code == EFX_EXCEPTION_FWALERT_SRAM) ? "FWALERT_SRAM" :
190                  (code == EFX_EXCEPTION_UNKNOWN_FWALERT) ? "UNKNOWN_FWALERT" :
191                  (code == EFX_EXCEPTION_RX_ERROR) ? "RX_ERROR" :
192                  (code == EFX_EXCEPTION_TX_ERROR) ? "TX_ERROR" :
193                  (code == EFX_EXCEPTION_EV_ERROR) ? "EV_ERROR" :
194                  "UNKNOWN",
195                  code, data, evq->evq_index);
196
197         return B_TRUE;
198 }
199
200 static boolean_t
201 sfc_ev_rxq_flush_done(void *arg, __rte_unused uint32_t rxq_hw_index)
202 {
203         struct sfc_evq *evq = arg;
204         struct sfc_rxq *rxq;
205
206         rxq = evq->rxq;
207         SFC_ASSERT(rxq != NULL);
208         SFC_ASSERT(rxq->hw_index == rxq_hw_index);
209         SFC_ASSERT(rxq->evq == evq);
210         sfc_rx_qflush_done(rxq);
211
212         return B_FALSE;
213 }
214
215 static boolean_t
216 sfc_ev_rxq_flush_failed(void *arg, __rte_unused uint32_t rxq_hw_index)
217 {
218         struct sfc_evq *evq = arg;
219         struct sfc_rxq *rxq;
220
221         rxq = evq->rxq;
222         SFC_ASSERT(rxq != NULL);
223         SFC_ASSERT(rxq->hw_index == rxq_hw_index);
224         SFC_ASSERT(rxq->evq == evq);
225         sfc_rx_qflush_failed(rxq);
226
227         return B_FALSE;
228 }
229
230 static boolean_t
231 sfc_ev_txq_flush_done(void *arg, __rte_unused uint32_t txq_hw_index)
232 {
233         struct sfc_evq *evq = arg;
234         struct sfc_txq *txq;
235
236         txq = evq->txq;
237         SFC_ASSERT(txq != NULL);
238         SFC_ASSERT(txq->hw_index == txq_hw_index);
239         SFC_ASSERT(txq->evq == evq);
240         sfc_tx_qflush_done(txq);
241
242         return B_FALSE;
243 }
244
245 static boolean_t
246 sfc_ev_software(void *arg, uint16_t magic)
247 {
248         struct sfc_evq *evq = arg;
249
250         sfc_err(evq->sa, "EVQ %u unexpected software event magic=%#.4x",
251                 evq->evq_index, magic);
252         return B_TRUE;
253 }
254
255 static boolean_t
256 sfc_ev_sram(void *arg, uint32_t code)
257 {
258         struct sfc_evq *evq = arg;
259
260         sfc_err(evq->sa, "EVQ %u unexpected SRAM event code=%u",
261                 evq->evq_index, code);
262         return B_TRUE;
263 }
264
265 static boolean_t
266 sfc_ev_wake_up(void *arg, uint32_t index)
267 {
268         struct sfc_evq *evq = arg;
269
270         sfc_err(evq->sa, "EVQ %u unexpected wake up event index=%u",
271                 evq->evq_index, index);
272         return B_TRUE;
273 }
274
275 static boolean_t
276 sfc_ev_timer(void *arg, uint32_t index)
277 {
278         struct sfc_evq *evq = arg;
279
280         sfc_err(evq->sa, "EVQ %u unexpected timer event index=%u",
281                 evq->evq_index, index);
282         return B_TRUE;
283 }
284
285 static boolean_t
286 sfc_ev_link_change(void *arg, efx_link_mode_t link_mode)
287 {
288         struct sfc_evq *evq = arg;
289         struct sfc_adapter *sa = evq->sa;
290         struct rte_eth_link *dev_link = &sa->eth_dev->data->dev_link;
291         struct rte_eth_link new_link;
292         uint64_t new_link_u64;
293         uint64_t old_link_u64;
294
295         EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
296
297         sfc_port_link_mode_to_info(link_mode, &new_link);
298
299         new_link_u64 = *(uint64_t *)&new_link;
300         do {
301                 old_link_u64 = rte_atomic64_read((rte_atomic64_t *)dev_link);
302                 if (old_link_u64 == new_link_u64)
303                         break;
304
305                 if (rte_atomic64_cmpset((volatile uint64_t *)dev_link,
306                                         old_link_u64, new_link_u64)) {
307                         evq->sa->port.lsc_seq++;
308                         break;
309                 }
310         } while (B_TRUE);
311
312         return B_FALSE;
313 }
314
315 static const efx_ev_callbacks_t sfc_ev_callbacks = {
316         .eec_initialized        = sfc_ev_initialized,
317         .eec_rx                 = sfc_ev_rx,
318         .eec_tx                 = sfc_ev_tx,
319         .eec_exception          = sfc_ev_exception,
320         .eec_rxq_flush_done     = sfc_ev_rxq_flush_done,
321         .eec_rxq_flush_failed   = sfc_ev_rxq_flush_failed,
322         .eec_txq_flush_done     = sfc_ev_txq_flush_done,
323         .eec_software           = sfc_ev_software,
324         .eec_sram               = sfc_ev_sram,
325         .eec_wake_up            = sfc_ev_wake_up,
326         .eec_timer              = sfc_ev_timer,
327         .eec_link_change        = sfc_ev_link_change,
328 };
329
330
331 void
332 sfc_ev_qpoll(struct sfc_evq *evq)
333 {
334         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
335                    evq->init_state == SFC_EVQ_STARTING);
336
337         /* Synchronize the DMA memory for reading not required */
338
339         efx_ev_qpoll(evq->common, &evq->read_ptr, &sfc_ev_callbacks, evq);
340
341         if (unlikely(evq->exception) && sfc_adapter_trylock(evq->sa)) {
342                 struct sfc_adapter *sa = evq->sa;
343                 int rc;
344
345                 if ((evq->rxq != NULL) && (evq->rxq->state & SFC_RXQ_RUNNING)) {
346                         unsigned int rxq_sw_index = sfc_rxq_sw_index(evq->rxq);
347
348                         sfc_warn(sa,
349                                  "restart RxQ %u because of exception on its EvQ %u",
350                                  rxq_sw_index, evq->evq_index);
351
352                         sfc_rx_qstop(sa, rxq_sw_index);
353                         rc = sfc_rx_qstart(sa, rxq_sw_index);
354                         if (rc != 0)
355                                 sfc_err(sa, "cannot restart RxQ %u",
356                                         rxq_sw_index);
357                 }
358
359                 if (evq->txq != NULL) {
360                         unsigned int txq_sw_index = sfc_txq_sw_index(evq->txq);
361
362                         sfc_warn(sa,
363                                  "restart TxQ %u because of exception on its EvQ %u",
364                                  txq_sw_index, evq->evq_index);
365
366                         sfc_tx_qstop(sa, txq_sw_index);
367                         rc = sfc_tx_qstart(sa, txq_sw_index);
368                         if (rc != 0)
369                                 sfc_err(sa, "cannot restart TxQ %u",
370                                         txq_sw_index);
371                 }
372
373                 if (evq->exception)
374                         sfc_panic(sa, "unrecoverable exception on EvQ %u",
375                                   evq->evq_index);
376
377                 sfc_adapter_unlock(sa);
378         }
379
380         /* Poll-mode driver does not re-prime the event queue for interrupts */
381 }
382
383 void
384 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
385 {
386         if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
387                 struct sfc_evq *mgmt_evq = sa->evq_info[sa->mgmt_evq_index].evq;
388
389                 if (mgmt_evq->init_state == SFC_EVQ_STARTED)
390                         sfc_ev_qpoll(mgmt_evq);
391
392                 rte_spinlock_unlock(&sa->mgmt_evq_lock);
393         }
394 }
395
396 int
397 sfc_ev_qprime(struct sfc_evq *evq)
398 {
399         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
400         return efx_ev_qprime(evq->common, evq->read_ptr);
401 }
402
403 int
404 sfc_ev_qstart(struct sfc_adapter *sa, unsigned int sw_index)
405 {
406         const struct sfc_evq_info *evq_info;
407         struct sfc_evq *evq;
408         efsys_mem_t *esmp;
409         unsigned int total_delay_us;
410         unsigned int delay_us;
411         int rc;
412
413         sfc_log_init(sa, "sw_index=%u", sw_index);
414
415         evq_info = &sa->evq_info[sw_index];
416         evq = evq_info->evq;
417         esmp = &evq->mem;
418
419         /* Clear all events */
420         (void)memset((void *)esmp->esm_base, 0xff,
421                      EFX_EVQ_SIZE(evq_info->entries));
422
423         /* Create the common code event queue */
424         rc = efx_ev_qcreate(sa->nic, sw_index, esmp, evq_info->entries,
425                             0 /* unused on EF10 */, 0, evq_info->flags,
426                             &evq->common);
427         if (rc != 0)
428                 goto fail_ev_qcreate;
429
430         evq->init_state = SFC_EVQ_STARTING;
431
432         /* Wait for the initialization event */
433         total_delay_us = 0;
434         delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
435         do {
436                 (void)sfc_ev_qpoll(evq);
437
438                 /* Check to see if the initialization complete indication
439                  * posted by the hardware.
440                  */
441                 if (evq->init_state == SFC_EVQ_STARTED)
442                         goto done;
443
444                 /* Give event queue some time to init */
445                 rte_delay_us(delay_us);
446
447                 total_delay_us += delay_us;
448
449                 /* Exponential backoff */
450                 delay_us *= 2;
451                 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
452                         delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
453
454         } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
455
456         rc = ETIMEDOUT;
457         goto fail_timedout;
458
459 done:
460         return 0;
461
462 fail_timedout:
463         evq->init_state = SFC_EVQ_INITIALIZED;
464         efx_ev_qdestroy(evq->common);
465
466 fail_ev_qcreate:
467         sfc_log_init(sa, "failed %d", rc);
468         return rc;
469 }
470
471 void
472 sfc_ev_qstop(struct sfc_adapter *sa, unsigned int sw_index)
473 {
474         const struct sfc_evq_info *evq_info;
475         struct sfc_evq *evq;
476
477         sfc_log_init(sa, "sw_index=%u", sw_index);
478
479         SFC_ASSERT(sw_index < sa->evq_count);
480
481         evq_info = &sa->evq_info[sw_index];
482         evq = evq_info->evq;
483
484         if (evq == NULL || evq->init_state != SFC_EVQ_STARTED)
485                 return;
486
487         evq->init_state = SFC_EVQ_INITIALIZED;
488         evq->read_ptr = 0;
489         evq->exception = B_FALSE;
490
491         efx_ev_qdestroy(evq->common);
492 }
493
494 static void
495 sfc_ev_mgmt_periodic_qpoll(void *arg)
496 {
497         struct sfc_adapter *sa = arg;
498         int rc;
499
500         sfc_ev_mgmt_qpoll(sa);
501
502         rc = rte_eal_alarm_set(SFC_MGMT_EV_QPOLL_PERIOD_US,
503                                sfc_ev_mgmt_periodic_qpoll, sa);
504         if (rc == -ENOTSUP) {
505                 sfc_warn(sa, "alarms are not supported");
506                 sfc_warn(sa, "management EVQ must be polled indirectly using no-wait link status update");
507         } else if (rc != 0) {
508                 sfc_err(sa,
509                         "cannot rearm management EVQ polling alarm (rc=%d)",
510                         rc);
511         }
512 }
513
514 static void
515 sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter *sa)
516 {
517         sfc_ev_mgmt_periodic_qpoll(sa);
518 }
519
520 static void
521 sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter *sa)
522 {
523         rte_eal_alarm_cancel(sfc_ev_mgmt_periodic_qpoll, sa);
524 }
525
526 int
527 sfc_ev_start(struct sfc_adapter *sa)
528 {
529         int rc;
530
531         sfc_log_init(sa, "entry");
532
533         rc = efx_ev_init(sa->nic);
534         if (rc != 0)
535                 goto fail_ev_init;
536
537         /* Start management EVQ used for global events */
538         rte_spinlock_lock(&sa->mgmt_evq_lock);
539
540         rc = sfc_ev_qstart(sa, sa->mgmt_evq_index);
541         if (rc != 0)
542                 goto fail_mgmt_evq_start;
543
544         if (sa->intr.lsc_intr) {
545                 rc = sfc_ev_qprime(sa->evq_info[sa->mgmt_evq_index].evq);
546                 if (rc != 0)
547                         goto fail_evq0_prime;
548         }
549
550         rte_spinlock_unlock(&sa->mgmt_evq_lock);
551
552         /*
553          * Start management EVQ polling. If interrupts are disabled
554          * (not used), it is required to process link status change
555          * and other device level events to avoid unrecoverable
556          * error because the event queue overflow.
557          */
558         sfc_ev_mgmt_periodic_qpoll_start(sa);
559
560         /*
561          * Rx/Tx event queues are started/stopped when corresponding
562          * Rx/Tx queue is started/stopped.
563          */
564
565         return 0;
566
567 fail_evq0_prime:
568         sfc_ev_qstop(sa, 0);
569
570 fail_mgmt_evq_start:
571         rte_spinlock_unlock(&sa->mgmt_evq_lock);
572         efx_ev_fini(sa->nic);
573
574 fail_ev_init:
575         sfc_log_init(sa, "failed %d", rc);
576         return rc;
577 }
578
579 void
580 sfc_ev_stop(struct sfc_adapter *sa)
581 {
582         unsigned int sw_index;
583
584         sfc_log_init(sa, "entry");
585
586         sfc_ev_mgmt_periodic_qpoll_stop(sa);
587
588         /* Make sure that all event queues are stopped */
589         sw_index = sa->evq_count;
590         while (sw_index-- > 0) {
591                 if (sw_index == sa->mgmt_evq_index) {
592                         /* Locks are required for the management EVQ */
593                         rte_spinlock_lock(&sa->mgmt_evq_lock);
594                         sfc_ev_qstop(sa, sa->mgmt_evq_index);
595                         rte_spinlock_unlock(&sa->mgmt_evq_lock);
596                 } else {
597                         sfc_ev_qstop(sa, sw_index);
598                 }
599         }
600
601         efx_ev_fini(sa->nic);
602 }
603
604 int
605 sfc_ev_qinit(struct sfc_adapter *sa, unsigned int sw_index,
606              unsigned int entries, int socket_id)
607 {
608         struct sfc_evq_info *evq_info;
609         struct sfc_evq *evq;
610         int rc;
611
612         sfc_log_init(sa, "sw_index=%u", sw_index);
613
614         evq_info = &sa->evq_info[sw_index];
615
616         SFC_ASSERT(rte_is_power_of_2(entries));
617         SFC_ASSERT(entries <= evq_info->max_entries);
618         evq_info->entries = entries;
619
620         evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
621                                  socket_id);
622         if (evq == NULL)
623                 return ENOMEM;
624
625         evq->sa = sa;
626         evq->evq_index = sw_index;
627
628         /* Allocate DMA space */
629         rc = sfc_dma_alloc(sa, "evq", sw_index, EFX_EVQ_SIZE(evq_info->entries),
630                            socket_id, &evq->mem);
631         if (rc != 0)
632                 return rc;
633
634         evq->init_state = SFC_EVQ_INITIALIZED;
635
636         evq_info->evq = evq;
637
638         return 0;
639 }
640
641 void
642 sfc_ev_qfini(struct sfc_adapter *sa, unsigned int sw_index)
643 {
644         struct sfc_evq *evq;
645
646         sfc_log_init(sa, "sw_index=%u", sw_index);
647
648         evq = sa->evq_info[sw_index].evq;
649
650         SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
651
652         sa->evq_info[sw_index].evq = NULL;
653
654         sfc_dma_free(sa, &evq->mem);
655
656         rte_free(evq);
657 }
658
659 static int
660 sfc_ev_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
661 {
662         struct sfc_evq_info *evq_info = &sa->evq_info[sw_index];
663         unsigned int max_entries;
664
665         sfc_log_init(sa, "sw_index=%u", sw_index);
666
667         max_entries = sfc_evq_max_entries(sa, sw_index);
668         SFC_ASSERT(rte_is_power_of_2(max_entries));
669
670         evq_info->max_entries = max_entries;
671         evq_info->flags = sa->evq_flags |
672                 ((sa->intr.lsc_intr && sw_index == sa->mgmt_evq_index) ?
673                         EFX_EVQ_FLAGS_NOTIFY_INTERRUPT :
674                         EFX_EVQ_FLAGS_NOTIFY_DISABLED);
675
676         return 0;
677 }
678
679 static int
680 sfc_kvarg_perf_profile_handler(__rte_unused const char *key,
681                                const char *value_str, void *opaque)
682 {
683         uint64_t *value = opaque;
684
685         if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_THROUGHPUT) == 0)
686                 *value = EFX_EVQ_FLAGS_TYPE_THROUGHPUT;
687         else if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_LOW_LATENCY) == 0)
688                 *value = EFX_EVQ_FLAGS_TYPE_LOW_LATENCY;
689         else if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_AUTO) == 0)
690                 *value = EFX_EVQ_FLAGS_TYPE_AUTO;
691         else
692                 return -EINVAL;
693
694         return 0;
695 }
696
697 static void
698 sfc_ev_qfini_info(struct sfc_adapter *sa, unsigned int sw_index)
699 {
700         sfc_log_init(sa, "sw_index=%u", sw_index);
701
702         /* Nothing to cleanup */
703 }
704
705 int
706 sfc_ev_init(struct sfc_adapter *sa)
707 {
708         int rc;
709         unsigned int sw_index;
710
711         sfc_log_init(sa, "entry");
712
713         sa->evq_flags = EFX_EVQ_FLAGS_TYPE_THROUGHPUT;
714         rc = sfc_kvargs_process(sa, SFC_KVARG_PERF_PROFILE,
715                                 sfc_kvarg_perf_profile_handler,
716                                 &sa->evq_flags);
717         if (rc != 0) {
718                 sfc_err(sa, "invalid %s parameter value",
719                         SFC_KVARG_PERF_PROFILE);
720                 goto fail_kvarg_perf_profile;
721         }
722
723         sa->evq_count = sfc_ev_qcount(sa);
724         sa->mgmt_evq_index = 0;
725         rte_spinlock_init(&sa->mgmt_evq_lock);
726
727         /* Allocate EVQ info array */
728         rc = ENOMEM;
729         sa->evq_info = rte_calloc_socket("sfc-evqs", sa->evq_count,
730                                          sizeof(struct sfc_evq_info), 0,
731                                          sa->socket_id);
732         if (sa->evq_info == NULL)
733                 goto fail_evqs_alloc;
734
735         for (sw_index = 0; sw_index < sa->evq_count; ++sw_index) {
736                 rc = sfc_ev_qinit_info(sa, sw_index);
737                 if (rc != 0)
738                         goto fail_ev_qinit_info;
739         }
740
741         rc = sfc_ev_qinit(sa, sa->mgmt_evq_index, SFC_MGMT_EVQ_ENTRIES,
742                           sa->socket_id);
743         if (rc != 0)
744                 goto fail_mgmt_evq_init;
745
746         /*
747          * Rx/Tx event queues are created/destroyed when corresponding
748          * Rx/Tx queue is created/destroyed.
749          */
750
751         return 0;
752
753 fail_mgmt_evq_init:
754 fail_ev_qinit_info:
755         while (sw_index-- > 0)
756                 sfc_ev_qfini_info(sa, sw_index);
757
758         rte_free(sa->evq_info);
759         sa->evq_info = NULL;
760
761 fail_evqs_alloc:
762         sa->evq_count = 0;
763
764 fail_kvarg_perf_profile:
765         sfc_log_init(sa, "failed %d", rc);
766         return rc;
767 }
768
769 void
770 sfc_ev_fini(struct sfc_adapter *sa)
771 {
772         int sw_index;
773
774         sfc_log_init(sa, "entry");
775
776         /* Cleanup all event queues */
777         sw_index = sa->evq_count;
778         while (--sw_index >= 0) {
779                 if (sa->evq_info[sw_index].evq != NULL)
780                         sfc_ev_qfini(sa, sw_index);
781                 sfc_ev_qfini_info(sa, sw_index);
782         }
783
784         rte_free(sa->evq_info);
785         sa->evq_info = NULL;
786         sa->evq_count = 0;
787 }