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