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