net/sfc: factor out libefx-based Rx datapath
[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_efx_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_efx_rxq *rxq;
91         unsigned int stop;
92         unsigned int pending_id;
93         unsigned int delta;
94         unsigned int i;
95         struct sfc_efx_rx_sw_desc *rxd;
96
97         if (unlikely(evq->exception))
98                 goto done;
99
100         rxq = sfc_efx_rxq_by_dp_rxq(evq->dp_rxq);
101
102         SFC_ASSERT(rxq != NULL);
103         SFC_ASSERT(rxq->evq == evq);
104         SFC_ASSERT(rxq->flags & SFC_EFX_RXQ_FLAG_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, rxq->dp.dpq.queue_id,
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, rxq->dp.dpq.queue_id,
138                         id, delta, 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_dp_rxq *dp_rxq;
237         struct sfc_rxq *rxq;
238
239         dp_rxq = evq->dp_rxq;
240         SFC_ASSERT(dp_rxq != NULL);
241
242         rxq = sfc_rxq_by_dp_rxq(dp_rxq);
243         SFC_ASSERT(rxq != NULL);
244         SFC_ASSERT(rxq->hw_index == rxq_hw_index);
245         SFC_ASSERT(rxq->evq == evq);
246         sfc_rx_qflush_done(rxq);
247
248         return B_FALSE;
249 }
250
251 static boolean_t
252 sfc_ev_nop_rxq_flush_failed(void *arg, uint32_t rxq_hw_index)
253 {
254         struct sfc_evq *evq = arg;
255
256         sfc_err(evq->sa, "EVQ %u unexpected RxQ %u flush failed",
257                 evq->evq_index, rxq_hw_index);
258         return B_TRUE;
259 }
260
261 static boolean_t
262 sfc_ev_rxq_flush_failed(void *arg, __rte_unused uint32_t rxq_hw_index)
263 {
264         struct sfc_evq *evq = arg;
265         struct sfc_dp_rxq *dp_rxq;
266         struct sfc_rxq *rxq;
267
268         dp_rxq = evq->dp_rxq;
269         SFC_ASSERT(dp_rxq != NULL);
270
271         rxq = sfc_rxq_by_dp_rxq(dp_rxq);
272         SFC_ASSERT(rxq != NULL);
273         SFC_ASSERT(rxq->hw_index == rxq_hw_index);
274         SFC_ASSERT(rxq->evq == evq);
275         sfc_rx_qflush_failed(rxq);
276
277         return B_FALSE;
278 }
279
280 static boolean_t
281 sfc_ev_nop_txq_flush_done(void *arg, uint32_t txq_hw_index)
282 {
283         struct sfc_evq *evq = arg;
284
285         sfc_err(evq->sa, "EVQ %u unexpected TxQ %u flush done",
286                 evq->evq_index, txq_hw_index);
287         return B_TRUE;
288 }
289
290 static boolean_t
291 sfc_ev_txq_flush_done(void *arg, __rte_unused uint32_t txq_hw_index)
292 {
293         struct sfc_evq *evq = arg;
294         struct sfc_txq *txq;
295
296         txq = evq->txq;
297         SFC_ASSERT(txq != NULL);
298         SFC_ASSERT(txq->hw_index == txq_hw_index);
299         SFC_ASSERT(txq->evq == evq);
300         sfc_tx_qflush_done(txq);
301
302         return B_FALSE;
303 }
304
305 static boolean_t
306 sfc_ev_software(void *arg, uint16_t magic)
307 {
308         struct sfc_evq *evq = arg;
309
310         sfc_err(evq->sa, "EVQ %u unexpected software event magic=%#.4x",
311                 evq->evq_index, magic);
312         return B_TRUE;
313 }
314
315 static boolean_t
316 sfc_ev_sram(void *arg, uint32_t code)
317 {
318         struct sfc_evq *evq = arg;
319
320         sfc_err(evq->sa, "EVQ %u unexpected SRAM event code=%u",
321                 evq->evq_index, code);
322         return B_TRUE;
323 }
324
325 static boolean_t
326 sfc_ev_wake_up(void *arg, uint32_t index)
327 {
328         struct sfc_evq *evq = arg;
329
330         sfc_err(evq->sa, "EVQ %u unexpected wake up event index=%u",
331                 evq->evq_index, index);
332         return B_TRUE;
333 }
334
335 static boolean_t
336 sfc_ev_timer(void *arg, uint32_t index)
337 {
338         struct sfc_evq *evq = arg;
339
340         sfc_err(evq->sa, "EVQ %u unexpected timer event index=%u",
341                 evq->evq_index, index);
342         return B_TRUE;
343 }
344
345 static boolean_t
346 sfc_ev_nop_link_change(void *arg, __rte_unused efx_link_mode_t link_mode)
347 {
348         struct sfc_evq *evq = arg;
349
350         sfc_err(evq->sa, "EVQ %u unexpected link change event",
351                 evq->evq_index);
352         return B_TRUE;
353 }
354
355 static boolean_t
356 sfc_ev_link_change(void *arg, efx_link_mode_t link_mode)
357 {
358         struct sfc_evq *evq = arg;
359         struct sfc_adapter *sa = evq->sa;
360         struct rte_eth_link *dev_link = &sa->eth_dev->data->dev_link;
361         struct rte_eth_link new_link;
362         uint64_t new_link_u64;
363         uint64_t old_link_u64;
364
365         EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
366
367         sfc_port_link_mode_to_info(link_mode, &new_link);
368
369         new_link_u64 = *(uint64_t *)&new_link;
370         do {
371                 old_link_u64 = rte_atomic64_read((rte_atomic64_t *)dev_link);
372                 if (old_link_u64 == new_link_u64)
373                         break;
374
375                 if (rte_atomic64_cmpset((volatile uint64_t *)dev_link,
376                                         old_link_u64, new_link_u64)) {
377                         evq->sa->port.lsc_seq++;
378                         break;
379                 }
380         } while (B_TRUE);
381
382         return B_FALSE;
383 }
384
385 static const efx_ev_callbacks_t sfc_ev_callbacks = {
386         .eec_initialized        = sfc_ev_initialized,
387         .eec_rx                 = sfc_ev_nop_rx,
388         .eec_tx                 = sfc_ev_nop_tx,
389         .eec_exception          = sfc_ev_exception,
390         .eec_rxq_flush_done     = sfc_ev_nop_rxq_flush_done,
391         .eec_rxq_flush_failed   = sfc_ev_nop_rxq_flush_failed,
392         .eec_txq_flush_done     = sfc_ev_nop_txq_flush_done,
393         .eec_software           = sfc_ev_software,
394         .eec_sram               = sfc_ev_sram,
395         .eec_wake_up            = sfc_ev_wake_up,
396         .eec_timer              = sfc_ev_timer,
397         .eec_link_change        = sfc_ev_link_change,
398 };
399
400 static const efx_ev_callbacks_t sfc_ev_callbacks_efx_rx = {
401         .eec_initialized        = sfc_ev_initialized,
402         .eec_rx                 = sfc_ev_efx_rx,
403         .eec_tx                 = sfc_ev_nop_tx,
404         .eec_exception          = sfc_ev_exception,
405         .eec_rxq_flush_done     = sfc_ev_rxq_flush_done,
406         .eec_rxq_flush_failed   = sfc_ev_rxq_flush_failed,
407         .eec_txq_flush_done     = sfc_ev_nop_txq_flush_done,
408         .eec_software           = sfc_ev_software,
409         .eec_sram               = sfc_ev_sram,
410         .eec_wake_up            = sfc_ev_wake_up,
411         .eec_timer              = sfc_ev_timer,
412         .eec_link_change        = sfc_ev_nop_link_change,
413 };
414
415 static const efx_ev_callbacks_t sfc_ev_callbacks_dp_rx = {
416         .eec_initialized        = sfc_ev_initialized,
417         .eec_rx                 = sfc_ev_nop_rx,
418         .eec_tx                 = sfc_ev_nop_tx,
419         .eec_exception          = sfc_ev_exception,
420         .eec_rxq_flush_done     = sfc_ev_rxq_flush_done,
421         .eec_rxq_flush_failed   = sfc_ev_rxq_flush_failed,
422         .eec_txq_flush_done     = sfc_ev_nop_txq_flush_done,
423         .eec_software           = sfc_ev_software,
424         .eec_sram               = sfc_ev_sram,
425         .eec_wake_up            = sfc_ev_wake_up,
426         .eec_timer              = sfc_ev_timer,
427         .eec_link_change        = sfc_ev_nop_link_change,
428 };
429
430 static const efx_ev_callbacks_t sfc_ev_callbacks_tx = {
431         .eec_initialized        = sfc_ev_initialized,
432         .eec_rx                 = sfc_ev_nop_rx,
433         .eec_tx                 = sfc_ev_tx,
434         .eec_exception          = sfc_ev_exception,
435         .eec_rxq_flush_done     = sfc_ev_nop_rxq_flush_done,
436         .eec_rxq_flush_failed   = sfc_ev_nop_rxq_flush_failed,
437         .eec_txq_flush_done     = sfc_ev_txq_flush_done,
438         .eec_software           = sfc_ev_software,
439         .eec_sram               = sfc_ev_sram,
440         .eec_wake_up            = sfc_ev_wake_up,
441         .eec_timer              = sfc_ev_timer,
442         .eec_link_change        = sfc_ev_nop_link_change,
443 };
444
445
446 void
447 sfc_ev_qpoll(struct sfc_evq *evq)
448 {
449         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
450                    evq->init_state == SFC_EVQ_STARTING);
451
452         /* Synchronize the DMA memory for reading not required */
453
454         efx_ev_qpoll(evq->common, &evq->read_ptr, evq->callbacks, evq);
455
456         if (unlikely(evq->exception) && sfc_adapter_trylock(evq->sa)) {
457                 struct sfc_adapter *sa = evq->sa;
458                 int rc;
459
460                 if (evq->dp_rxq != NULL) {
461                         unsigned int rxq_sw_index;
462
463                         rxq_sw_index = evq->dp_rxq->dpq.queue_id;
464
465                         sfc_warn(sa,
466                                  "restart RxQ %u because of exception on its EvQ %u",
467                                  rxq_sw_index, evq->evq_index);
468
469                         sfc_rx_qstop(sa, rxq_sw_index);
470                         rc = sfc_rx_qstart(sa, rxq_sw_index);
471                         if (rc != 0)
472                                 sfc_err(sa, "cannot restart RxQ %u",
473                                         rxq_sw_index);
474                 }
475
476                 if (evq->txq != NULL) {
477                         unsigned int txq_sw_index = sfc_txq_sw_index(evq->txq);
478
479                         sfc_warn(sa,
480                                  "restart TxQ %u because of exception on its EvQ %u",
481                                  txq_sw_index, evq->evq_index);
482
483                         sfc_tx_qstop(sa, txq_sw_index);
484                         rc = sfc_tx_qstart(sa, txq_sw_index);
485                         if (rc != 0)
486                                 sfc_err(sa, "cannot restart TxQ %u",
487                                         txq_sw_index);
488                 }
489
490                 if (evq->exception)
491                         sfc_panic(sa, "unrecoverable exception on EvQ %u",
492                                   evq->evq_index);
493
494                 sfc_adapter_unlock(sa);
495         }
496
497         /* Poll-mode driver does not re-prime the event queue for interrupts */
498 }
499
500 void
501 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
502 {
503         if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
504                 struct sfc_evq *mgmt_evq = sa->evq_info[sa->mgmt_evq_index].evq;
505
506                 if (mgmt_evq->init_state == SFC_EVQ_STARTED)
507                         sfc_ev_qpoll(mgmt_evq);
508
509                 rte_spinlock_unlock(&sa->mgmt_evq_lock);
510         }
511 }
512
513 int
514 sfc_ev_qprime(struct sfc_evq *evq)
515 {
516         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
517         return efx_ev_qprime(evq->common, evq->read_ptr);
518 }
519
520 int
521 sfc_ev_qstart(struct sfc_adapter *sa, unsigned int sw_index)
522 {
523         const struct sfc_evq_info *evq_info;
524         struct sfc_evq *evq;
525         efsys_mem_t *esmp;
526         unsigned int total_delay_us;
527         unsigned int delay_us;
528         int rc;
529
530         sfc_log_init(sa, "sw_index=%u", sw_index);
531
532         evq_info = &sa->evq_info[sw_index];
533         evq = evq_info->evq;
534         esmp = &evq->mem;
535
536         /* Clear all events */
537         (void)memset((void *)esmp->esm_base, 0xff,
538                      EFX_EVQ_SIZE(evq_info->entries));
539
540         /* Create the common code event queue */
541         rc = efx_ev_qcreate(sa->nic, sw_index, esmp, evq_info->entries,
542                             0 /* unused on EF10 */, 0, evq_info->flags,
543                             &evq->common);
544         if (rc != 0)
545                 goto fail_ev_qcreate;
546
547         SFC_ASSERT(evq->dp_rxq == NULL || evq->txq == NULL);
548         if (evq->dp_rxq != 0) {
549                 if (strcmp(sa->dp_rx->dp.name, SFC_KVARG_DATAPATH_EFX) == 0)
550                         evq->callbacks = &sfc_ev_callbacks_efx_rx;
551                 else
552                         evq->callbacks = &sfc_ev_callbacks_dp_rx;
553         } else if (evq->txq != 0) {
554                 evq->callbacks = &sfc_ev_callbacks_tx;
555         } else {
556                 evq->callbacks = &sfc_ev_callbacks;
557         }
558
559         evq->init_state = SFC_EVQ_STARTING;
560
561         /* Wait for the initialization event */
562         total_delay_us = 0;
563         delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
564         do {
565                 (void)sfc_ev_qpoll(evq);
566
567                 /* Check to see if the initialization complete indication
568                  * posted by the hardware.
569                  */
570                 if (evq->init_state == SFC_EVQ_STARTED)
571                         goto done;
572
573                 /* Give event queue some time to init */
574                 rte_delay_us(delay_us);
575
576                 total_delay_us += delay_us;
577
578                 /* Exponential backoff */
579                 delay_us *= 2;
580                 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
581                         delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
582
583         } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
584
585         rc = ETIMEDOUT;
586         goto fail_timedout;
587
588 done:
589         return 0;
590
591 fail_timedout:
592         evq->init_state = SFC_EVQ_INITIALIZED;
593         efx_ev_qdestroy(evq->common);
594
595 fail_ev_qcreate:
596         sfc_log_init(sa, "failed %d", rc);
597         return rc;
598 }
599
600 void
601 sfc_ev_qstop(struct sfc_adapter *sa, unsigned int sw_index)
602 {
603         const struct sfc_evq_info *evq_info;
604         struct sfc_evq *evq;
605
606         sfc_log_init(sa, "sw_index=%u", sw_index);
607
608         SFC_ASSERT(sw_index < sa->evq_count);
609
610         evq_info = &sa->evq_info[sw_index];
611         evq = evq_info->evq;
612
613         if (evq == NULL || evq->init_state != SFC_EVQ_STARTED)
614                 return;
615
616         evq->init_state = SFC_EVQ_INITIALIZED;
617         evq->callbacks = NULL;
618         evq->read_ptr = 0;
619         evq->exception = B_FALSE;
620
621         efx_ev_qdestroy(evq->common);
622 }
623
624 static void
625 sfc_ev_mgmt_periodic_qpoll(void *arg)
626 {
627         struct sfc_adapter *sa = arg;
628         int rc;
629
630         sfc_ev_mgmt_qpoll(sa);
631
632         rc = rte_eal_alarm_set(SFC_MGMT_EV_QPOLL_PERIOD_US,
633                                sfc_ev_mgmt_periodic_qpoll, sa);
634         if (rc == -ENOTSUP) {
635                 sfc_warn(sa, "alarms are not supported");
636                 sfc_warn(sa, "management EVQ must be polled indirectly using no-wait link status update");
637         } else if (rc != 0) {
638                 sfc_err(sa,
639                         "cannot rearm management EVQ polling alarm (rc=%d)",
640                         rc);
641         }
642 }
643
644 static void
645 sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter *sa)
646 {
647         sfc_ev_mgmt_periodic_qpoll(sa);
648 }
649
650 static void
651 sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter *sa)
652 {
653         rte_eal_alarm_cancel(sfc_ev_mgmt_periodic_qpoll, sa);
654 }
655
656 int
657 sfc_ev_start(struct sfc_adapter *sa)
658 {
659         int rc;
660
661         sfc_log_init(sa, "entry");
662
663         rc = efx_ev_init(sa->nic);
664         if (rc != 0)
665                 goto fail_ev_init;
666
667         /* Start management EVQ used for global events */
668         rte_spinlock_lock(&sa->mgmt_evq_lock);
669
670         rc = sfc_ev_qstart(sa, sa->mgmt_evq_index);
671         if (rc != 0)
672                 goto fail_mgmt_evq_start;
673
674         if (sa->intr.lsc_intr) {
675                 rc = sfc_ev_qprime(sa->evq_info[sa->mgmt_evq_index].evq);
676                 if (rc != 0)
677                         goto fail_evq0_prime;
678         }
679
680         rte_spinlock_unlock(&sa->mgmt_evq_lock);
681
682         /*
683          * Start management EVQ polling. If interrupts are disabled
684          * (not used), it is required to process link status change
685          * and other device level events to avoid unrecoverable
686          * error because the event queue overflow.
687          */
688         sfc_ev_mgmt_periodic_qpoll_start(sa);
689
690         /*
691          * Rx/Tx event queues are started/stopped when corresponding
692          * Rx/Tx queue is started/stopped.
693          */
694
695         return 0;
696
697 fail_evq0_prime:
698         sfc_ev_qstop(sa, 0);
699
700 fail_mgmt_evq_start:
701         rte_spinlock_unlock(&sa->mgmt_evq_lock);
702         efx_ev_fini(sa->nic);
703
704 fail_ev_init:
705         sfc_log_init(sa, "failed %d", rc);
706         return rc;
707 }
708
709 void
710 sfc_ev_stop(struct sfc_adapter *sa)
711 {
712         unsigned int sw_index;
713
714         sfc_log_init(sa, "entry");
715
716         sfc_ev_mgmt_periodic_qpoll_stop(sa);
717
718         /* Make sure that all event queues are stopped */
719         sw_index = sa->evq_count;
720         while (sw_index-- > 0) {
721                 if (sw_index == sa->mgmt_evq_index) {
722                         /* Locks are required for the management EVQ */
723                         rte_spinlock_lock(&sa->mgmt_evq_lock);
724                         sfc_ev_qstop(sa, sa->mgmt_evq_index);
725                         rte_spinlock_unlock(&sa->mgmt_evq_lock);
726                 } else {
727                         sfc_ev_qstop(sa, sw_index);
728                 }
729         }
730
731         efx_ev_fini(sa->nic);
732 }
733
734 int
735 sfc_ev_qinit(struct sfc_adapter *sa, unsigned int sw_index,
736              unsigned int entries, int socket_id)
737 {
738         struct sfc_evq_info *evq_info;
739         struct sfc_evq *evq;
740         int rc;
741
742         sfc_log_init(sa, "sw_index=%u", sw_index);
743
744         evq_info = &sa->evq_info[sw_index];
745
746         SFC_ASSERT(rte_is_power_of_2(entries));
747         SFC_ASSERT(entries <= evq_info->max_entries);
748         evq_info->entries = entries;
749
750         evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
751                                  socket_id);
752         if (evq == NULL)
753                 return ENOMEM;
754
755         evq->sa = sa;
756         evq->evq_index = sw_index;
757
758         /* Allocate DMA space */
759         rc = sfc_dma_alloc(sa, "evq", sw_index, EFX_EVQ_SIZE(evq_info->entries),
760                            socket_id, &evq->mem);
761         if (rc != 0)
762                 return rc;
763
764         evq->init_state = SFC_EVQ_INITIALIZED;
765
766         evq_info->evq = evq;
767
768         return 0;
769 }
770
771 void
772 sfc_ev_qfini(struct sfc_adapter *sa, unsigned int sw_index)
773 {
774         struct sfc_evq *evq;
775
776         sfc_log_init(sa, "sw_index=%u", sw_index);
777
778         evq = sa->evq_info[sw_index].evq;
779
780         SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
781
782         sa->evq_info[sw_index].evq = NULL;
783
784         sfc_dma_free(sa, &evq->mem);
785
786         rte_free(evq);
787 }
788
789 static int
790 sfc_ev_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
791 {
792         struct sfc_evq_info *evq_info = &sa->evq_info[sw_index];
793         unsigned int max_entries;
794
795         sfc_log_init(sa, "sw_index=%u", sw_index);
796
797         max_entries = sfc_evq_max_entries(sa, sw_index);
798         SFC_ASSERT(rte_is_power_of_2(max_entries));
799
800         evq_info->max_entries = max_entries;
801         evq_info->flags = sa->evq_flags |
802                 ((sa->intr.lsc_intr && sw_index == sa->mgmt_evq_index) ?
803                         EFX_EVQ_FLAGS_NOTIFY_INTERRUPT :
804                         EFX_EVQ_FLAGS_NOTIFY_DISABLED);
805
806         return 0;
807 }
808
809 static int
810 sfc_kvarg_perf_profile_handler(__rte_unused const char *key,
811                                const char *value_str, void *opaque)
812 {
813         uint64_t *value = opaque;
814
815         if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_THROUGHPUT) == 0)
816                 *value = EFX_EVQ_FLAGS_TYPE_THROUGHPUT;
817         else if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_LOW_LATENCY) == 0)
818                 *value = EFX_EVQ_FLAGS_TYPE_LOW_LATENCY;
819         else if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_AUTO) == 0)
820                 *value = EFX_EVQ_FLAGS_TYPE_AUTO;
821         else
822                 return -EINVAL;
823
824         return 0;
825 }
826
827 static void
828 sfc_ev_qfini_info(struct sfc_adapter *sa, unsigned int sw_index)
829 {
830         sfc_log_init(sa, "sw_index=%u", sw_index);
831
832         /* Nothing to cleanup */
833 }
834
835 int
836 sfc_ev_init(struct sfc_adapter *sa)
837 {
838         int rc;
839         unsigned int sw_index;
840
841         sfc_log_init(sa, "entry");
842
843         sa->evq_flags = EFX_EVQ_FLAGS_TYPE_THROUGHPUT;
844         rc = sfc_kvargs_process(sa, SFC_KVARG_PERF_PROFILE,
845                                 sfc_kvarg_perf_profile_handler,
846                                 &sa->evq_flags);
847         if (rc != 0) {
848                 sfc_err(sa, "invalid %s parameter value",
849                         SFC_KVARG_PERF_PROFILE);
850                 goto fail_kvarg_perf_profile;
851         }
852
853         sa->evq_count = sfc_ev_qcount(sa);
854         sa->mgmt_evq_index = 0;
855         rte_spinlock_init(&sa->mgmt_evq_lock);
856
857         /* Allocate EVQ info array */
858         rc = ENOMEM;
859         sa->evq_info = rte_calloc_socket("sfc-evqs", sa->evq_count,
860                                          sizeof(struct sfc_evq_info), 0,
861                                          sa->socket_id);
862         if (sa->evq_info == NULL)
863                 goto fail_evqs_alloc;
864
865         for (sw_index = 0; sw_index < sa->evq_count; ++sw_index) {
866                 rc = sfc_ev_qinit_info(sa, sw_index);
867                 if (rc != 0)
868                         goto fail_ev_qinit_info;
869         }
870
871         rc = sfc_ev_qinit(sa, sa->mgmt_evq_index, SFC_MGMT_EVQ_ENTRIES,
872                           sa->socket_id);
873         if (rc != 0)
874                 goto fail_mgmt_evq_init;
875
876         /*
877          * Rx/Tx event queues are created/destroyed when corresponding
878          * Rx/Tx queue is created/destroyed.
879          */
880
881         return 0;
882
883 fail_mgmt_evq_init:
884 fail_ev_qinit_info:
885         while (sw_index-- > 0)
886                 sfc_ev_qfini_info(sa, sw_index);
887
888         rte_free(sa->evq_info);
889         sa->evq_info = NULL;
890
891 fail_evqs_alloc:
892         sa->evq_count = 0;
893
894 fail_kvarg_perf_profile:
895         sfc_log_init(sa, "failed %d", rc);
896         return rc;
897 }
898
899 void
900 sfc_ev_fini(struct sfc_adapter *sa)
901 {
902         int sw_index;
903
904         sfc_log_init(sa, "entry");
905
906         /* Cleanup all event queues */
907         sw_index = sa->evq_count;
908         while (--sw_index >= 0) {
909                 if (sa->evq_info[sw_index].evq != NULL)
910                         sfc_ev_qfini(sa, sw_index);
911                 sfc_ev_qfini_info(sa, sw_index);
912         }
913
914         rte_free(sa->evq_info);
915         sa->evq_info = NULL;
916         sa->evq_count = 0;
917 }