net/sfc: implement libefx Rx packets event callbacks
[dpdk.git] / drivers / net / sfc / sfc_ev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2019-2020 Xilinx, Inc.
4  * Copyright(c) 2016-2019 Solarflare Communications Inc.
5  *
6  * This software was jointly developed between OKTET Labs (under contract
7  * for Solarflare) and Solarflare Communications, Inc.
8  */
9
10 #include <rte_debug.h>
11 #include <rte_cycles.h>
12 #include <rte_alarm.h>
13 #include <rte_branch_prediction.h>
14
15 #include "efx.h"
16
17 #include "sfc.h"
18 #include "sfc_debug.h"
19 #include "sfc_log.h"
20 #include "sfc_ev.h"
21 #include "sfc_rx.h"
22 #include "sfc_tx.h"
23 #include "sfc_kvargs.h"
24
25
26 /* Initial delay when waiting for event queue init complete event */
27 #define SFC_EVQ_INIT_BACKOFF_START_US   (1)
28 /* Maximum delay between event queue polling attempts */
29 #define SFC_EVQ_INIT_BACKOFF_MAX_US     (10 * 1000)
30 /* Event queue init approx timeout */
31 #define SFC_EVQ_INIT_TIMEOUT_US         (2 * US_PER_S)
32
33 /* Management event queue polling period in microseconds */
34 #define SFC_MGMT_EV_QPOLL_PERIOD_US     (US_PER_S)
35
36 static const char *
37 sfc_evq_type2str(enum sfc_evq_type type)
38 {
39         switch (type) {
40         case SFC_EVQ_TYPE_MGMT:
41                 return "mgmt-evq";
42         case SFC_EVQ_TYPE_RX:
43                 return "rx-evq";
44         case SFC_EVQ_TYPE_TX:
45                 return "tx-evq";
46         default:
47                 SFC_ASSERT(B_FALSE);
48                 return NULL;
49         }
50 }
51
52 static boolean_t
53 sfc_ev_initialized(void *arg)
54 {
55         struct sfc_evq *evq = arg;
56
57         /* Init done events may be duplicated on SFN7xxx (SFC bug 31631) */
58         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTING ||
59                    evq->init_state == SFC_EVQ_STARTED);
60
61         evq->init_state = SFC_EVQ_STARTED;
62
63         return B_FALSE;
64 }
65
66 static boolean_t
67 sfc_ev_nop_rx(void *arg, uint32_t label, uint32_t id,
68               uint32_t size, uint16_t flags)
69 {
70         struct sfc_evq *evq = arg;
71
72         sfc_err(evq->sa,
73                 "EVQ %u unexpected Rx event label=%u id=%#x size=%u flags=%#x",
74                 evq->evq_index, label, id, size, flags);
75         return B_TRUE;
76 }
77
78 static boolean_t
79 sfc_ev_efx_rx(void *arg, __rte_unused uint32_t label, uint32_t id,
80               uint32_t size, uint16_t flags)
81 {
82         struct sfc_evq *evq = arg;
83         struct sfc_efx_rxq *rxq;
84         unsigned int stop;
85         unsigned int pending_id;
86         unsigned int delta;
87         unsigned int i;
88         struct sfc_efx_rx_sw_desc *rxd;
89
90         if (unlikely(evq->exception))
91                 goto done;
92
93         rxq = sfc_efx_rxq_by_dp_rxq(evq->dp_rxq);
94
95         SFC_ASSERT(rxq != NULL);
96         SFC_ASSERT(rxq->evq == evq);
97         SFC_ASSERT(rxq->flags & SFC_EFX_RXQ_FLAG_STARTED);
98
99         stop = (id + 1) & rxq->ptr_mask;
100         pending_id = rxq->pending & rxq->ptr_mask;
101         delta = (stop >= pending_id) ? (stop - pending_id) :
102                 (rxq->ptr_mask + 1 - pending_id + stop);
103
104         if (delta == 0) {
105                 /*
106                  * Rx event with no new descriptors done and zero length
107                  * is used to abort scattered packet when there is no room
108                  * for the tail.
109                  */
110                 if (unlikely(size != 0)) {
111                         evq->exception = B_TRUE;
112                         sfc_err(evq->sa,
113                                 "EVQ %u RxQ %u invalid RX abort "
114                                 "(id=%#x size=%u flags=%#x); needs restart",
115                                 evq->evq_index, rxq->dp.dpq.queue_id,
116                                 id, size, flags);
117                         goto done;
118                 }
119
120                 /* Add discard flag to the first fragment */
121                 rxq->sw_desc[pending_id].flags |= EFX_DISCARD;
122                 /* Remove continue flag from the last fragment */
123                 rxq->sw_desc[id].flags &= ~EFX_PKT_CONT;
124         } else if (unlikely(delta > rxq->batch_max)) {
125                 evq->exception = B_TRUE;
126
127                 sfc_err(evq->sa,
128                         "EVQ %u RxQ %u completion out of order "
129                         "(id=%#x delta=%u flags=%#x); needs restart",
130                         evq->evq_index, rxq->dp.dpq.queue_id,
131                         id, delta, flags);
132
133                 goto done;
134         }
135
136         for (i = pending_id; i != stop; i = (i + 1) & rxq->ptr_mask) {
137                 rxd = &rxq->sw_desc[i];
138
139                 rxd->flags = flags;
140
141                 SFC_ASSERT(size < (1 << 16));
142                 rxd->size = (uint16_t)size;
143         }
144
145         rxq->pending += delta;
146
147 done:
148         return B_FALSE;
149 }
150
151 static boolean_t
152 sfc_ev_dp_rx(void *arg, __rte_unused uint32_t label, uint32_t id,
153              __rte_unused uint32_t size, __rte_unused uint16_t flags)
154 {
155         struct sfc_evq *evq = arg;
156         struct sfc_dp_rxq *dp_rxq;
157
158         dp_rxq = evq->dp_rxq;
159         SFC_ASSERT(dp_rxq != NULL);
160
161         SFC_ASSERT(evq->sa->priv.dp_rx->qrx_ev != NULL);
162         return evq->sa->priv.dp_rx->qrx_ev(dp_rxq, id);
163 }
164
165 static boolean_t
166 sfc_ev_nop_rx_packets(void *arg, uint32_t label, unsigned int num_packets,
167                       uint32_t flags)
168 {
169         struct sfc_evq *evq = arg;
170
171         sfc_err(evq->sa,
172                 "EVQ %u unexpected Rx packets event label=%u num=%u flags=%#x",
173                 evq->evq_index, label, num_packets, flags);
174         return B_TRUE;
175 }
176
177 static boolean_t
178 sfc_ev_dp_rx_packets(void *arg, __rte_unused uint32_t label,
179                      unsigned int num_packets, __rte_unused uint32_t flags)
180 {
181         struct sfc_evq *evq = arg;
182         struct sfc_dp_rxq *dp_rxq;
183
184         dp_rxq = evq->dp_rxq;
185         SFC_ASSERT(dp_rxq != NULL);
186
187         SFC_ASSERT(evq->sa->priv.dp_rx->qrx_ev != NULL);
188         return evq->sa->priv.dp_rx->qrx_ev(dp_rxq, num_packets);
189 }
190
191 static boolean_t
192 sfc_ev_nop_rx_ps(void *arg, uint32_t label, uint32_t id,
193                  uint32_t pkt_count, uint16_t flags)
194 {
195         struct sfc_evq *evq = arg;
196
197         sfc_err(evq->sa,
198                 "EVQ %u unexpected packed stream Rx event label=%u id=%#x pkt_count=%u flags=%#x",
199                 evq->evq_index, label, id, pkt_count, flags);
200         return B_TRUE;
201 }
202
203 /* It is not actually used on datapath, but required on RxQ flush */
204 static boolean_t
205 sfc_ev_dp_rx_ps(void *arg, __rte_unused uint32_t label, uint32_t id,
206                 __rte_unused uint32_t pkt_count, __rte_unused uint16_t flags)
207 {
208         struct sfc_evq *evq = arg;
209         struct sfc_dp_rxq *dp_rxq;
210
211         dp_rxq = evq->dp_rxq;
212         SFC_ASSERT(dp_rxq != NULL);
213
214         if (evq->sa->priv.dp_rx->qrx_ps_ev != NULL)
215                 return evq->sa->priv.dp_rx->qrx_ps_ev(dp_rxq, id);
216         else
217                 return B_FALSE;
218 }
219
220 static boolean_t
221 sfc_ev_nop_tx(void *arg, uint32_t label, uint32_t id)
222 {
223         struct sfc_evq *evq = arg;
224
225         sfc_err(evq->sa, "EVQ %u unexpected Tx event label=%u id=%#x",
226                 evq->evq_index, label, id);
227         return B_TRUE;
228 }
229
230 static boolean_t
231 sfc_ev_tx(void *arg, __rte_unused uint32_t label, uint32_t id)
232 {
233         struct sfc_evq *evq = arg;
234         struct sfc_dp_txq *dp_txq;
235         struct sfc_efx_txq *txq;
236         unsigned int stop;
237         unsigned int delta;
238
239         dp_txq = evq->dp_txq;
240         SFC_ASSERT(dp_txq != NULL);
241
242         txq = sfc_efx_txq_by_dp_txq(dp_txq);
243         SFC_ASSERT(txq->evq == evq);
244
245         if (unlikely((txq->flags & SFC_EFX_TXQ_FLAG_STARTED) == 0))
246                 goto done;
247
248         stop = (id + 1) & txq->ptr_mask;
249         id = txq->pending & txq->ptr_mask;
250
251         delta = (stop >= id) ? (stop - id) : (txq->ptr_mask + 1 - id + stop);
252
253         txq->pending += delta;
254
255 done:
256         return B_FALSE;
257 }
258
259 static boolean_t
260 sfc_ev_dp_tx(void *arg, __rte_unused uint32_t label, uint32_t id)
261 {
262         struct sfc_evq *evq = arg;
263         struct sfc_dp_txq *dp_txq;
264
265         dp_txq = evq->dp_txq;
266         SFC_ASSERT(dp_txq != NULL);
267
268         SFC_ASSERT(evq->sa->priv.dp_tx->qtx_ev != NULL);
269         return evq->sa->priv.dp_tx->qtx_ev(dp_txq, id);
270 }
271
272 static boolean_t
273 sfc_ev_exception(void *arg, uint32_t code, __rte_unused uint32_t data)
274 {
275         struct sfc_evq *evq = arg;
276
277         if (code == EFX_EXCEPTION_UNKNOWN_SENSOREVT)
278                 return B_FALSE;
279
280         evq->exception = B_TRUE;
281         sfc_warn(evq->sa,
282                  "hardware exception %s (code=%u, data=%#x) on EVQ %u;"
283                  " needs recovery",
284                  (code == EFX_EXCEPTION_RX_RECOVERY) ? "RX_RECOVERY" :
285                  (code == EFX_EXCEPTION_RX_DSC_ERROR) ? "RX_DSC_ERROR" :
286                  (code == EFX_EXCEPTION_TX_DSC_ERROR) ? "TX_DSC_ERROR" :
287                  (code == EFX_EXCEPTION_FWALERT_SRAM) ? "FWALERT_SRAM" :
288                  (code == EFX_EXCEPTION_UNKNOWN_FWALERT) ? "UNKNOWN_FWALERT" :
289                  (code == EFX_EXCEPTION_RX_ERROR) ? "RX_ERROR" :
290                  (code == EFX_EXCEPTION_TX_ERROR) ? "TX_ERROR" :
291                  (code == EFX_EXCEPTION_EV_ERROR) ? "EV_ERROR" :
292                  "UNKNOWN",
293                  code, data, evq->evq_index);
294
295         return B_TRUE;
296 }
297
298 static boolean_t
299 sfc_ev_nop_rxq_flush_done(void *arg, uint32_t rxq_hw_index)
300 {
301         struct sfc_evq *evq = arg;
302
303         sfc_err(evq->sa, "EVQ %u unexpected RxQ %u flush done",
304                 evq->evq_index, rxq_hw_index);
305         return B_TRUE;
306 }
307
308 static boolean_t
309 sfc_ev_rxq_flush_done(void *arg, __rte_unused uint32_t rxq_hw_index)
310 {
311         struct sfc_evq *evq = arg;
312         struct sfc_dp_rxq *dp_rxq;
313         struct sfc_rxq *rxq;
314
315         dp_rxq = evq->dp_rxq;
316         SFC_ASSERT(dp_rxq != NULL);
317
318         rxq = sfc_rxq_by_dp_rxq(dp_rxq);
319         SFC_ASSERT(rxq != NULL);
320         SFC_ASSERT(rxq->hw_index == rxq_hw_index);
321         SFC_ASSERT(rxq->evq == evq);
322         RTE_SET_USED(rxq);
323
324         sfc_rx_qflush_done(sfc_rxq_info_by_dp_rxq(dp_rxq));
325
326         return B_FALSE;
327 }
328
329 static boolean_t
330 sfc_ev_nop_rxq_flush_failed(void *arg, uint32_t rxq_hw_index)
331 {
332         struct sfc_evq *evq = arg;
333
334         sfc_err(evq->sa, "EVQ %u unexpected RxQ %u flush failed",
335                 evq->evq_index, rxq_hw_index);
336         return B_TRUE;
337 }
338
339 static boolean_t
340 sfc_ev_rxq_flush_failed(void *arg, __rte_unused uint32_t rxq_hw_index)
341 {
342         struct sfc_evq *evq = arg;
343         struct sfc_dp_rxq *dp_rxq;
344         struct sfc_rxq *rxq;
345
346         dp_rxq = evq->dp_rxq;
347         SFC_ASSERT(dp_rxq != NULL);
348
349         rxq = sfc_rxq_by_dp_rxq(dp_rxq);
350         SFC_ASSERT(rxq != NULL);
351         SFC_ASSERT(rxq->hw_index == rxq_hw_index);
352         SFC_ASSERT(rxq->evq == evq);
353         RTE_SET_USED(rxq);
354
355         sfc_rx_qflush_failed(sfc_rxq_info_by_dp_rxq(dp_rxq));
356
357         return B_FALSE;
358 }
359
360 static boolean_t
361 sfc_ev_nop_txq_flush_done(void *arg, uint32_t txq_hw_index)
362 {
363         struct sfc_evq *evq = arg;
364
365         sfc_err(evq->sa, "EVQ %u unexpected TxQ %u flush done",
366                 evq->evq_index, txq_hw_index);
367         return B_TRUE;
368 }
369
370 static boolean_t
371 sfc_ev_txq_flush_done(void *arg, __rte_unused uint32_t txq_hw_index)
372 {
373         struct sfc_evq *evq = arg;
374         struct sfc_dp_txq *dp_txq;
375         struct sfc_txq *txq;
376
377         dp_txq = evq->dp_txq;
378         SFC_ASSERT(dp_txq != NULL);
379
380         txq = sfc_txq_by_dp_txq(dp_txq);
381         SFC_ASSERT(txq != NULL);
382         SFC_ASSERT(txq->hw_index == txq_hw_index);
383         SFC_ASSERT(txq->evq == evq);
384         RTE_SET_USED(txq);
385
386         sfc_tx_qflush_done(sfc_txq_info_by_dp_txq(dp_txq));
387
388         return B_FALSE;
389 }
390
391 static boolean_t
392 sfc_ev_software(void *arg, uint16_t magic)
393 {
394         struct sfc_evq *evq = arg;
395
396         sfc_err(evq->sa, "EVQ %u unexpected software event magic=%#.4x",
397                 evq->evq_index, magic);
398         return B_TRUE;
399 }
400
401 static boolean_t
402 sfc_ev_sram(void *arg, uint32_t code)
403 {
404         struct sfc_evq *evq = arg;
405
406         sfc_err(evq->sa, "EVQ %u unexpected SRAM event code=%u",
407                 evq->evq_index, code);
408         return B_TRUE;
409 }
410
411 static boolean_t
412 sfc_ev_wake_up(void *arg, uint32_t index)
413 {
414         struct sfc_evq *evq = arg;
415
416         sfc_err(evq->sa, "EVQ %u unexpected wake up event index=%u",
417                 evq->evq_index, index);
418         return B_TRUE;
419 }
420
421 static boolean_t
422 sfc_ev_timer(void *arg, uint32_t index)
423 {
424         struct sfc_evq *evq = arg;
425
426         sfc_err(evq->sa, "EVQ %u unexpected timer event index=%u",
427                 evq->evq_index, index);
428         return B_TRUE;
429 }
430
431 static boolean_t
432 sfc_ev_nop_link_change(void *arg, __rte_unused efx_link_mode_t link_mode)
433 {
434         struct sfc_evq *evq = arg;
435
436         sfc_err(evq->sa, "EVQ %u unexpected link change event",
437                 evq->evq_index);
438         return B_TRUE;
439 }
440
441 static boolean_t
442 sfc_ev_link_change(void *arg, efx_link_mode_t link_mode)
443 {
444         struct sfc_evq *evq = arg;
445         struct sfc_adapter *sa = evq->sa;
446         struct rte_eth_link new_link;
447
448         sfc_port_link_mode_to_info(link_mode, &new_link);
449         if (rte_eth_linkstatus_set(sa->eth_dev, &new_link) == 0)
450                 evq->sa->port.lsc_seq++;
451
452         return B_FALSE;
453 }
454
455 static const efx_ev_callbacks_t sfc_ev_callbacks = {
456         .eec_initialized        = sfc_ev_initialized,
457         .eec_rx                 = sfc_ev_nop_rx,
458         .eec_rx_packets         = sfc_ev_nop_rx_packets,
459         .eec_rx_ps              = sfc_ev_nop_rx_ps,
460         .eec_tx                 = sfc_ev_nop_tx,
461         .eec_exception          = sfc_ev_exception,
462         .eec_rxq_flush_done     = sfc_ev_nop_rxq_flush_done,
463         .eec_rxq_flush_failed   = sfc_ev_nop_rxq_flush_failed,
464         .eec_txq_flush_done     = sfc_ev_nop_txq_flush_done,
465         .eec_software           = sfc_ev_software,
466         .eec_sram               = sfc_ev_sram,
467         .eec_wake_up            = sfc_ev_wake_up,
468         .eec_timer              = sfc_ev_timer,
469         .eec_link_change        = sfc_ev_link_change,
470 };
471
472 static const efx_ev_callbacks_t sfc_ev_callbacks_efx_rx = {
473         .eec_initialized        = sfc_ev_initialized,
474         .eec_rx                 = sfc_ev_efx_rx,
475         .eec_rx_packets         = sfc_ev_nop_rx_packets,
476         .eec_rx_ps              = sfc_ev_nop_rx_ps,
477         .eec_tx                 = sfc_ev_nop_tx,
478         .eec_exception          = sfc_ev_exception,
479         .eec_rxq_flush_done     = sfc_ev_rxq_flush_done,
480         .eec_rxq_flush_failed   = sfc_ev_rxq_flush_failed,
481         .eec_txq_flush_done     = sfc_ev_nop_txq_flush_done,
482         .eec_software           = sfc_ev_software,
483         .eec_sram               = sfc_ev_sram,
484         .eec_wake_up            = sfc_ev_wake_up,
485         .eec_timer              = sfc_ev_timer,
486         .eec_link_change        = sfc_ev_nop_link_change,
487 };
488
489 static const efx_ev_callbacks_t sfc_ev_callbacks_dp_rx = {
490         .eec_initialized        = sfc_ev_initialized,
491         .eec_rx                 = sfc_ev_dp_rx,
492         .eec_rx_packets         = sfc_ev_dp_rx_packets,
493         .eec_rx_ps              = sfc_ev_dp_rx_ps,
494         .eec_tx                 = sfc_ev_nop_tx,
495         .eec_exception          = sfc_ev_exception,
496         .eec_rxq_flush_done     = sfc_ev_rxq_flush_done,
497         .eec_rxq_flush_failed   = sfc_ev_rxq_flush_failed,
498         .eec_txq_flush_done     = sfc_ev_nop_txq_flush_done,
499         .eec_software           = sfc_ev_software,
500         .eec_sram               = sfc_ev_sram,
501         .eec_wake_up            = sfc_ev_wake_up,
502         .eec_timer              = sfc_ev_timer,
503         .eec_link_change        = sfc_ev_nop_link_change,
504 };
505
506 static const efx_ev_callbacks_t sfc_ev_callbacks_efx_tx = {
507         .eec_initialized        = sfc_ev_initialized,
508         .eec_rx                 = sfc_ev_nop_rx,
509         .eec_rx_packets         = sfc_ev_nop_rx_packets,
510         .eec_rx_ps              = sfc_ev_nop_rx_ps,
511         .eec_tx                 = sfc_ev_tx,
512         .eec_exception          = sfc_ev_exception,
513         .eec_rxq_flush_done     = sfc_ev_nop_rxq_flush_done,
514         .eec_rxq_flush_failed   = sfc_ev_nop_rxq_flush_failed,
515         .eec_txq_flush_done     = sfc_ev_txq_flush_done,
516         .eec_software           = sfc_ev_software,
517         .eec_sram               = sfc_ev_sram,
518         .eec_wake_up            = sfc_ev_wake_up,
519         .eec_timer              = sfc_ev_timer,
520         .eec_link_change        = sfc_ev_nop_link_change,
521 };
522
523 static const efx_ev_callbacks_t sfc_ev_callbacks_dp_tx = {
524         .eec_initialized        = sfc_ev_initialized,
525         .eec_rx                 = sfc_ev_nop_rx,
526         .eec_rx_packets         = sfc_ev_nop_rx_packets,
527         .eec_rx_ps              = sfc_ev_nop_rx_ps,
528         .eec_tx                 = sfc_ev_dp_tx,
529         .eec_exception          = sfc_ev_exception,
530         .eec_rxq_flush_done     = sfc_ev_nop_rxq_flush_done,
531         .eec_rxq_flush_failed   = sfc_ev_nop_rxq_flush_failed,
532         .eec_txq_flush_done     = sfc_ev_txq_flush_done,
533         .eec_software           = sfc_ev_software,
534         .eec_sram               = sfc_ev_sram,
535         .eec_wake_up            = sfc_ev_wake_up,
536         .eec_timer              = sfc_ev_timer,
537         .eec_link_change        = sfc_ev_nop_link_change,
538 };
539
540
541 void
542 sfc_ev_qpoll(struct sfc_evq *evq)
543 {
544         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
545                    evq->init_state == SFC_EVQ_STARTING);
546
547         /* Synchronize the DMA memory for reading not required */
548
549         efx_ev_qpoll(evq->common, &evq->read_ptr, evq->callbacks, evq);
550
551         if (unlikely(evq->exception) && sfc_adapter_trylock(evq->sa)) {
552                 struct sfc_adapter *sa = evq->sa;
553                 int rc;
554
555                 if (evq->dp_rxq != NULL) {
556                         unsigned int rxq_sw_index;
557
558                         rxq_sw_index = evq->dp_rxq->dpq.queue_id;
559
560                         sfc_warn(sa,
561                                  "restart RxQ %u because of exception on its EvQ %u",
562                                  rxq_sw_index, evq->evq_index);
563
564                         sfc_rx_qstop(sa, rxq_sw_index);
565                         rc = sfc_rx_qstart(sa, rxq_sw_index);
566                         if (rc != 0)
567                                 sfc_err(sa, "cannot restart RxQ %u",
568                                         rxq_sw_index);
569                 }
570
571                 if (evq->dp_txq != NULL) {
572                         unsigned int txq_sw_index;
573
574                         txq_sw_index = evq->dp_txq->dpq.queue_id;
575
576                         sfc_warn(sa,
577                                  "restart TxQ %u because of exception on its EvQ %u",
578                                  txq_sw_index, evq->evq_index);
579
580                         sfc_tx_qstop(sa, txq_sw_index);
581                         rc = sfc_tx_qstart(sa, txq_sw_index);
582                         if (rc != 0)
583                                 sfc_err(sa, "cannot restart TxQ %u",
584                                         txq_sw_index);
585                 }
586
587                 if (evq->exception)
588                         sfc_panic(sa, "unrecoverable exception on EvQ %u",
589                                   evq->evq_index);
590
591                 sfc_adapter_unlock(sa);
592         }
593
594         /* Poll-mode driver does not re-prime the event queue for interrupts */
595 }
596
597 void
598 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
599 {
600         if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
601                 if (sa->mgmt_evq_running)
602                         sfc_ev_qpoll(sa->mgmt_evq);
603
604                 rte_spinlock_unlock(&sa->mgmt_evq_lock);
605         }
606 }
607
608 int
609 sfc_ev_qprime(struct sfc_evq *evq)
610 {
611         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
612         return efx_ev_qprime(evq->common, evq->read_ptr);
613 }
614
615 /* Event queue HW index allocation scheme is described in sfc_ev.h. */
616 int
617 sfc_ev_qstart(struct sfc_evq *evq, unsigned int hw_index)
618 {
619         struct sfc_adapter *sa = evq->sa;
620         efsys_mem_t *esmp;
621         uint32_t evq_flags = sa->evq_flags;
622         unsigned int total_delay_us;
623         unsigned int delay_us;
624         int rc;
625
626         sfc_log_init(sa, "hw_index=%u", hw_index);
627
628         esmp = &evq->mem;
629
630         evq->evq_index = hw_index;
631
632         /* Clear all events */
633         (void)memset((void *)esmp->esm_base, 0xff,
634                      efx_evq_size(sa->nic, evq->entries, evq_flags));
635
636         if ((sa->intr.lsc_intr && hw_index == sa->mgmt_evq_index) ||
637             (sa->intr.rxq_intr && evq->dp_rxq != NULL))
638                 evq_flags |= EFX_EVQ_FLAGS_NOTIFY_INTERRUPT;
639         else
640                 evq_flags |= EFX_EVQ_FLAGS_NOTIFY_DISABLED;
641
642         evq->init_state = SFC_EVQ_STARTING;
643
644         /* Create the common code event queue */
645         rc = efx_ev_qcreate(sa->nic, hw_index, esmp, evq->entries,
646                             0 /* unused on EF10 */, 0, evq_flags,
647                             &evq->common);
648         if (rc != 0)
649                 goto fail_ev_qcreate;
650
651         SFC_ASSERT(evq->dp_rxq == NULL || evq->dp_txq == NULL);
652         if (evq->dp_rxq != 0) {
653                 if (strcmp(sa->priv.dp_rx->dp.name,
654                            SFC_KVARG_DATAPATH_EFX) == 0)
655                         evq->callbacks = &sfc_ev_callbacks_efx_rx;
656                 else
657                         evq->callbacks = &sfc_ev_callbacks_dp_rx;
658         } else if (evq->dp_txq != 0) {
659                 if (strcmp(sa->priv.dp_tx->dp.name,
660                            SFC_KVARG_DATAPATH_EFX) == 0)
661                         evq->callbacks = &sfc_ev_callbacks_efx_tx;
662                 else
663                         evq->callbacks = &sfc_ev_callbacks_dp_tx;
664         } else {
665                 evq->callbacks = &sfc_ev_callbacks;
666         }
667
668         /*
669          * Poll once to ensure that eec_initialized callback is invoked in
670          * case if the hardware does not support INIT_DONE events. If the
671          * hardware supports INIT_DONE events, this will do nothing, and the
672          * corresponding event will be processed by sfc_ev_qpoll() below.
673          */
674         efx_ev_qcreate_check_init_done(evq->common, evq->callbacks, evq);
675
676         /* Wait for the initialization event */
677         total_delay_us = 0;
678         delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
679         do {
680                 (void)sfc_ev_qpoll(evq);
681
682                 /* Check to see if the initialization complete indication
683                  * posted by the hardware.
684                  */
685                 if (evq->init_state == SFC_EVQ_STARTED)
686                         goto done;
687
688                 /* Give event queue some time to init */
689                 rte_delay_us(delay_us);
690
691                 total_delay_us += delay_us;
692
693                 /* Exponential backoff */
694                 delay_us *= 2;
695                 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
696                         delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
697
698         } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
699
700         rc = ETIMEDOUT;
701         goto fail_timedout;
702
703 done:
704         return 0;
705
706 fail_timedout:
707         efx_ev_qdestroy(evq->common);
708
709 fail_ev_qcreate:
710         evq->init_state = SFC_EVQ_INITIALIZED;
711         sfc_log_init(sa, "failed %d", rc);
712         return rc;
713 }
714
715 void
716 sfc_ev_qstop(struct sfc_evq *evq)
717 {
718         if (evq == NULL)
719                 return;
720
721         sfc_log_init(evq->sa, "hw_index=%u", evq->evq_index);
722
723         if (evq->init_state != SFC_EVQ_STARTED)
724                 return;
725
726         evq->init_state = SFC_EVQ_INITIALIZED;
727         evq->callbacks = NULL;
728         evq->read_ptr = 0;
729         evq->exception = B_FALSE;
730
731         efx_ev_qdestroy(evq->common);
732
733         evq->evq_index = 0;
734 }
735
736 static void
737 sfc_ev_mgmt_periodic_qpoll(void *arg)
738 {
739         struct sfc_adapter *sa = arg;
740         int rc;
741
742         sfc_ev_mgmt_qpoll(sa);
743
744         rc = rte_eal_alarm_set(SFC_MGMT_EV_QPOLL_PERIOD_US,
745                                sfc_ev_mgmt_periodic_qpoll, sa);
746         if (rc == -ENOTSUP) {
747                 sfc_warn(sa, "alarms are not supported");
748                 sfc_warn(sa, "management EVQ must be polled indirectly using no-wait link status update");
749         } else if (rc != 0) {
750                 sfc_err(sa,
751                         "cannot rearm management EVQ polling alarm (rc=%d)",
752                         rc);
753         }
754 }
755
756 static void
757 sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter *sa)
758 {
759         sfc_ev_mgmt_periodic_qpoll(sa);
760 }
761
762 static void
763 sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter *sa)
764 {
765         rte_eal_alarm_cancel(sfc_ev_mgmt_periodic_qpoll, sa);
766 }
767
768 int
769 sfc_ev_start(struct sfc_adapter *sa)
770 {
771         int rc;
772
773         sfc_log_init(sa, "entry");
774
775         rc = efx_ev_init(sa->nic);
776         if (rc != 0)
777                 goto fail_ev_init;
778
779         /* Start management EVQ used for global events */
780
781         /*
782          * Management event queue start polls the queue, but it cannot
783          * interfere with other polling contexts since mgmt_evq_running
784          * is false yet.
785          */
786         rc = sfc_ev_qstart(sa->mgmt_evq, sa->mgmt_evq_index);
787         if (rc != 0)
788                 goto fail_mgmt_evq_start;
789
790         rte_spinlock_lock(&sa->mgmt_evq_lock);
791         sa->mgmt_evq_running = true;
792         rte_spinlock_unlock(&sa->mgmt_evq_lock);
793
794         if (sa->intr.lsc_intr) {
795                 rc = sfc_ev_qprime(sa->mgmt_evq);
796                 if (rc != 0)
797                         goto fail_mgmt_evq_prime;
798         }
799
800         /*
801          * Start management EVQ polling. If interrupts are disabled
802          * (not used), it is required to process link status change
803          * and other device level events to avoid unrecoverable
804          * error because the event queue overflow.
805          */
806         sfc_ev_mgmt_periodic_qpoll_start(sa);
807
808         /*
809          * Rx/Tx event queues are started/stopped when corresponding
810          * Rx/Tx queue is started/stopped.
811          */
812
813         return 0;
814
815 fail_mgmt_evq_prime:
816         sfc_ev_qstop(sa->mgmt_evq);
817
818 fail_mgmt_evq_start:
819         efx_ev_fini(sa->nic);
820
821 fail_ev_init:
822         sfc_log_init(sa, "failed %d", rc);
823         return rc;
824 }
825
826 void
827 sfc_ev_stop(struct sfc_adapter *sa)
828 {
829         sfc_log_init(sa, "entry");
830
831         sfc_ev_mgmt_periodic_qpoll_stop(sa);
832
833         rte_spinlock_lock(&sa->mgmt_evq_lock);
834         sa->mgmt_evq_running = false;
835         rte_spinlock_unlock(&sa->mgmt_evq_lock);
836
837         sfc_ev_qstop(sa->mgmt_evq);
838
839         efx_ev_fini(sa->nic);
840 }
841
842 int
843 sfc_ev_qinit(struct sfc_adapter *sa,
844              enum sfc_evq_type type, unsigned int type_index,
845              unsigned int entries, int socket_id, struct sfc_evq **evqp)
846 {
847         struct sfc_evq *evq;
848         int rc;
849
850         sfc_log_init(sa, "type=%s type_index=%u",
851                      sfc_evq_type2str(type), type_index);
852
853         SFC_ASSERT(rte_is_power_of_2(entries));
854
855         rc = ENOMEM;
856         evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
857                                  socket_id);
858         if (evq == NULL)
859                 goto fail_evq_alloc;
860
861         evq->sa = sa;
862         evq->type = type;
863         evq->entries = entries;
864
865         /* Allocate DMA space */
866         rc = sfc_dma_alloc(sa, sfc_evq_type2str(type), type_index,
867                            efx_evq_size(sa->nic, evq->entries, sa->evq_flags),
868                            socket_id, &evq->mem);
869         if (rc != 0)
870                 goto fail_dma_alloc;
871
872         evq->init_state = SFC_EVQ_INITIALIZED;
873
874         sa->evq_count++;
875
876         *evqp = evq;
877
878         return 0;
879
880 fail_dma_alloc:
881         rte_free(evq);
882
883 fail_evq_alloc:
884
885         sfc_log_init(sa, "failed %d", rc);
886         return rc;
887 }
888
889 void
890 sfc_ev_qfini(struct sfc_evq *evq)
891 {
892         struct sfc_adapter *sa = evq->sa;
893
894         SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
895
896         sfc_dma_free(sa, &evq->mem);
897
898         rte_free(evq);
899
900         SFC_ASSERT(sa->evq_count > 0);
901         sa->evq_count--;
902 }
903
904 static int
905 sfc_kvarg_perf_profile_handler(__rte_unused const char *key,
906                                const char *value_str, void *opaque)
907 {
908         uint32_t *value = opaque;
909
910         if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_THROUGHPUT) == 0)
911                 *value = EFX_EVQ_FLAGS_TYPE_THROUGHPUT;
912         else if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_LOW_LATENCY) == 0)
913                 *value = EFX_EVQ_FLAGS_TYPE_LOW_LATENCY;
914         else if (strcasecmp(value_str, SFC_KVARG_PERF_PROFILE_AUTO) == 0)
915                 *value = EFX_EVQ_FLAGS_TYPE_AUTO;
916         else
917                 return -EINVAL;
918
919         return 0;
920 }
921
922 int
923 sfc_ev_attach(struct sfc_adapter *sa)
924 {
925         int rc;
926
927         sfc_log_init(sa, "entry");
928
929         sa->evq_flags = EFX_EVQ_FLAGS_TYPE_THROUGHPUT;
930         rc = sfc_kvargs_process(sa, SFC_KVARG_PERF_PROFILE,
931                                 sfc_kvarg_perf_profile_handler,
932                                 &sa->evq_flags);
933         if (rc != 0) {
934                 sfc_err(sa, "invalid %s parameter value",
935                         SFC_KVARG_PERF_PROFILE);
936                 goto fail_kvarg_perf_profile;
937         }
938
939         sa->mgmt_evq_index = 0;
940         rte_spinlock_init(&sa->mgmt_evq_lock);
941
942         rc = sfc_ev_qinit(sa, SFC_EVQ_TYPE_MGMT, 0, sa->evq_min_entries,
943                           sa->socket_id, &sa->mgmt_evq);
944         if (rc != 0)
945                 goto fail_mgmt_evq_init;
946
947         /*
948          * Rx/Tx event queues are created/destroyed when corresponding
949          * Rx/Tx queue is created/destroyed.
950          */
951
952         return 0;
953
954 fail_mgmt_evq_init:
955
956 fail_kvarg_perf_profile:
957         sfc_log_init(sa, "failed %d", rc);
958         return rc;
959 }
960
961 void
962 sfc_ev_detach(struct sfc_adapter *sa)
963 {
964         sfc_log_init(sa, "entry");
965
966         sfc_ev_qfini(sa->mgmt_evq);
967
968         if (sa->evq_count != 0)
969                 sfc_err(sa, "%u EvQs are not destroyed before detach",
970                         sa->evq_count);
971 }