net/sfc: implement device callback to Rx burst of packets
[dpdk.git] / drivers / net / sfc / sfc_ev.c
1 /*-
2  * Copyright (c) 2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * This software was jointly developed between OKTET Labs (under contract
6  * for Solarflare) and Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <rte_debug.h>
31 #include <rte_cycles.h>
32 #include <rte_alarm.h>
33
34 #include "efx.h"
35
36 #include "sfc.h"
37 #include "sfc_debug.h"
38 #include "sfc_log.h"
39 #include "sfc_ev.h"
40 #include "sfc_rx.h"
41
42
43 /* Initial delay when waiting for event queue init complete event */
44 #define SFC_EVQ_INIT_BACKOFF_START_US   (1)
45 /* Maximum delay between event queue polling attempts */
46 #define SFC_EVQ_INIT_BACKOFF_MAX_US     (10 * 1000)
47 /* Event queue init approx timeout */
48 #define SFC_EVQ_INIT_TIMEOUT_US         (2 * US_PER_S)
49
50 /* Management event queue polling period in microseconds */
51 #define SFC_MGMT_EV_QPOLL_PERIOD_US     (US_PER_S)
52
53
54 static boolean_t
55 sfc_ev_initialized(void *arg)
56 {
57         struct sfc_evq *evq = arg;
58
59         /* Init done events may be duplicated on SFN7xxx (SFC bug 31631) */
60         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTING ||
61                    evq->init_state == SFC_EVQ_STARTED);
62
63         evq->init_state = SFC_EVQ_STARTED;
64
65         return B_FALSE;
66 }
67
68 static boolean_t
69 sfc_ev_rx(void *arg, __rte_unused uint32_t label, uint32_t id,
70           uint32_t size, uint16_t flags)
71 {
72         struct sfc_evq *evq = arg;
73         struct sfc_rxq *rxq;
74         unsigned int stop;
75         unsigned int pending_id;
76         unsigned int delta;
77         unsigned int i;
78         struct sfc_rx_sw_desc *rxd;
79
80         if (unlikely(evq->exception))
81                 goto done;
82
83         rxq = evq->rxq;
84
85         SFC_ASSERT(rxq != NULL);
86         SFC_ASSERT(rxq->evq == evq);
87         SFC_ASSERT(rxq->state & SFC_RXQ_STARTED);
88
89         stop = (id + 1) & rxq->ptr_mask;
90         pending_id = rxq->pending & rxq->ptr_mask;
91         delta = (stop >= pending_id) ? (stop - pending_id) :
92                 (rxq->ptr_mask + 1 - pending_id + stop);
93
94         if (unlikely(delta > rxq->batch_max)) {
95                 evq->exception = B_TRUE;
96
97                 sfc_err(evq->sa,
98                         "EVQ %u RxQ %u completion out of order "
99                         "(id=%#x delta=%u flags=%#x); needs restart\n",
100                         evq->evq_index, sfc_rxq_sw_index(rxq), id, delta,
101                         flags);
102
103                 goto done;
104         }
105
106         for (i = pending_id; i != stop; i = (i + 1) & rxq->ptr_mask) {
107                 rxd = &rxq->sw_desc[i];
108
109                 rxd->flags = flags;
110
111                 SFC_ASSERT(size < (1 << 16));
112                 rxd->size = (uint16_t)size;
113         }
114
115         rxq->pending += delta;
116
117 done:
118         return B_FALSE;
119 }
120
121 static boolean_t
122 sfc_ev_tx(void *arg, __rte_unused uint32_t label, __rte_unused uint32_t id)
123 {
124         struct sfc_evq *evq = arg;
125
126         sfc_err(evq->sa, "EVQ %u unexpected Tx event", evq->evq_index);
127         return B_TRUE;
128 }
129
130 static boolean_t
131 sfc_ev_exception(void *arg, __rte_unused uint32_t code,
132                  __rte_unused uint32_t data)
133 {
134         struct sfc_evq *evq = arg;
135
136         if (code == EFX_EXCEPTION_UNKNOWN_SENSOREVT)
137                 return B_FALSE;
138
139         evq->exception = B_TRUE;
140         sfc_warn(evq->sa,
141                  "hardware exception %s (code=%u, data=%#x) on EVQ %u;"
142                  " needs recovery",
143                  (code == EFX_EXCEPTION_RX_RECOVERY) ? "RX_RECOVERY" :
144                  (code == EFX_EXCEPTION_RX_DSC_ERROR) ? "RX_DSC_ERROR" :
145                  (code == EFX_EXCEPTION_TX_DSC_ERROR) ? "TX_DSC_ERROR" :
146                  (code == EFX_EXCEPTION_FWALERT_SRAM) ? "FWALERT_SRAM" :
147                  (code == EFX_EXCEPTION_UNKNOWN_FWALERT) ? "UNKNOWN_FWALERT" :
148                  (code == EFX_EXCEPTION_RX_ERROR) ? "RX_ERROR" :
149                  (code == EFX_EXCEPTION_TX_ERROR) ? "TX_ERROR" :
150                  (code == EFX_EXCEPTION_EV_ERROR) ? "EV_ERROR" :
151                  "UNKNOWN",
152                  code, data, evq->evq_index);
153
154         return B_TRUE;
155 }
156
157 static boolean_t
158 sfc_ev_rxq_flush_done(void *arg, __rte_unused uint32_t rxq_hw_index)
159 {
160         struct sfc_evq *evq = arg;
161         struct sfc_rxq *rxq;
162
163         rxq = evq->rxq;
164         SFC_ASSERT(rxq != NULL);
165         SFC_ASSERT(rxq->hw_index == rxq_hw_index);
166         SFC_ASSERT(rxq->evq == evq);
167         sfc_rx_qflush_done(rxq);
168
169         return B_FALSE;
170 }
171
172 static boolean_t
173 sfc_ev_rxq_flush_failed(void *arg, __rte_unused uint32_t rxq_hw_index)
174 {
175         struct sfc_evq *evq = arg;
176         struct sfc_rxq *rxq;
177
178         rxq = evq->rxq;
179         SFC_ASSERT(rxq != NULL);
180         SFC_ASSERT(rxq->hw_index == rxq_hw_index);
181         SFC_ASSERT(rxq->evq == evq);
182         sfc_rx_qflush_failed(rxq);
183
184         return B_FALSE;
185 }
186
187 static boolean_t
188 sfc_ev_txq_flush_done(void *arg, __rte_unused uint32_t txq_hw_index)
189 {
190         struct sfc_evq *evq = arg;
191
192         sfc_err(evq->sa, "EVQ %u unexpected Tx flush done event",
193                 evq->evq_index);
194         return B_TRUE;
195 }
196
197 static boolean_t
198 sfc_ev_software(void *arg, uint16_t magic)
199 {
200         struct sfc_evq *evq = arg;
201
202         sfc_err(evq->sa, "EVQ %u unexpected software event magic=%#.4x",
203                 evq->evq_index, magic);
204         return B_TRUE;
205 }
206
207 static boolean_t
208 sfc_ev_sram(void *arg, uint32_t code)
209 {
210         struct sfc_evq *evq = arg;
211
212         sfc_err(evq->sa, "EVQ %u unexpected SRAM event code=%u",
213                 evq->evq_index, code);
214         return B_TRUE;
215 }
216
217 static boolean_t
218 sfc_ev_wake_up(void *arg, uint32_t index)
219 {
220         struct sfc_evq *evq = arg;
221
222         sfc_err(evq->sa, "EVQ %u unexpected wake up event index=%u",
223                 evq->evq_index, index);
224         return B_TRUE;
225 }
226
227 static boolean_t
228 sfc_ev_timer(void *arg, uint32_t index)
229 {
230         struct sfc_evq *evq = arg;
231
232         sfc_err(evq->sa, "EVQ %u unexpected timer event index=%u",
233                 evq->evq_index, index);
234         return B_TRUE;
235 }
236
237 static boolean_t
238 sfc_ev_link_change(void *arg, efx_link_mode_t link_mode)
239 {
240         struct sfc_evq *evq = arg;
241         struct sfc_adapter *sa = evq->sa;
242         struct rte_eth_link *dev_link = &sa->eth_dev->data->dev_link;
243         struct rte_eth_link new_link;
244
245         EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
246
247         sfc_port_link_mode_to_info(link_mode, &new_link);
248         rte_atomic64_set((rte_atomic64_t *)dev_link, *(uint64_t *)&new_link);
249
250         return B_FALSE;
251 }
252
253 static const efx_ev_callbacks_t sfc_ev_callbacks = {
254         .eec_initialized        = sfc_ev_initialized,
255         .eec_rx                 = sfc_ev_rx,
256         .eec_tx                 = sfc_ev_tx,
257         .eec_exception          = sfc_ev_exception,
258         .eec_rxq_flush_done     = sfc_ev_rxq_flush_done,
259         .eec_rxq_flush_failed   = sfc_ev_rxq_flush_failed,
260         .eec_txq_flush_done     = sfc_ev_txq_flush_done,
261         .eec_software           = sfc_ev_software,
262         .eec_sram               = sfc_ev_sram,
263         .eec_wake_up            = sfc_ev_wake_up,
264         .eec_timer              = sfc_ev_timer,
265         .eec_link_change        = sfc_ev_link_change,
266 };
267
268
269 void
270 sfc_ev_qpoll(struct sfc_evq *evq)
271 {
272         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
273                    evq->init_state == SFC_EVQ_STARTING);
274
275         /* Synchronize the DMA memory for reading not required */
276
277         efx_ev_qpoll(evq->common, &evq->read_ptr, &sfc_ev_callbacks, evq);
278
279         /* Poll-mode driver does not re-prime the event queue for interrupts */
280 }
281
282 void
283 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
284 {
285         if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
286                 struct sfc_evq *mgmt_evq = sa->evq_info[sa->mgmt_evq_index].evq;
287
288                 if (mgmt_evq->init_state == SFC_EVQ_STARTED)
289                         sfc_ev_qpoll(mgmt_evq);
290
291                 rte_spinlock_unlock(&sa->mgmt_evq_lock);
292         }
293 }
294
295 int
296 sfc_ev_qprime(struct sfc_evq *evq)
297 {
298         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
299         return efx_ev_qprime(evq->common, evq->read_ptr);
300 }
301
302 int
303 sfc_ev_qstart(struct sfc_adapter *sa, unsigned int sw_index)
304 {
305         const struct sfc_evq_info *evq_info;
306         struct sfc_evq *evq;
307         efsys_mem_t *esmp;
308         unsigned int total_delay_us;
309         unsigned int delay_us;
310         int rc;
311
312         sfc_log_init(sa, "sw_index=%u", sw_index);
313
314         evq_info = &sa->evq_info[sw_index];
315         evq = evq_info->evq;
316         esmp = &evq->mem;
317
318         /* Clear all events */
319         (void)memset((void *)esmp->esm_base, 0xff,
320                      EFX_EVQ_SIZE(evq_info->entries));
321
322         /* Create the common code event queue */
323         rc = efx_ev_qcreate(sa->nic, sw_index, esmp, evq_info->entries,
324                             0 /* unused on EF10 */, 0,
325                             EFX_EVQ_FLAGS_TYPE_THROUGHPUT |
326                             EFX_EVQ_FLAGS_NOTIFY_DISABLED,
327                             &evq->common);
328         if (rc != 0)
329                 goto fail_ev_qcreate;
330
331         evq->init_state = SFC_EVQ_STARTING;
332
333         /* Wait for the initialization event */
334         total_delay_us = 0;
335         delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
336         do {
337                 (void)sfc_ev_qpoll(evq);
338
339                 /* Check to see if the initialization complete indication
340                  * posted by the hardware.
341                  */
342                 if (evq->init_state == SFC_EVQ_STARTED)
343                         goto done;
344
345                 /* Give event queue some time to init */
346                 rte_delay_us(delay_us);
347
348                 total_delay_us += delay_us;
349
350                 /* Exponential backoff */
351                 delay_us *= 2;
352                 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
353                         delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
354
355         } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
356
357         rc = ETIMEDOUT;
358         goto fail_timedout;
359
360 done:
361         return 0;
362
363 fail_timedout:
364         evq->init_state = SFC_EVQ_INITIALIZED;
365         efx_ev_qdestroy(evq->common);
366
367 fail_ev_qcreate:
368         sfc_log_init(sa, "failed %d", rc);
369         return rc;
370 }
371
372 void
373 sfc_ev_qstop(struct sfc_adapter *sa, unsigned int sw_index)
374 {
375         const struct sfc_evq_info *evq_info;
376         struct sfc_evq *evq;
377
378         sfc_log_init(sa, "sw_index=%u", sw_index);
379
380         SFC_ASSERT(sw_index < sa->evq_count);
381
382         evq_info = &sa->evq_info[sw_index];
383         evq = evq_info->evq;
384
385         if (evq == NULL || evq->init_state != SFC_EVQ_STARTED)
386                 return;
387
388         evq->init_state = SFC_EVQ_INITIALIZED;
389         evq->read_ptr = 0;
390         evq->exception = B_FALSE;
391
392         efx_ev_qdestroy(evq->common);
393 }
394
395 static void
396 sfc_ev_mgmt_periodic_qpoll(void *arg)
397 {
398         struct sfc_adapter *sa = arg;
399         int rc;
400
401         sfc_ev_mgmt_qpoll(sa);
402
403         rc = rte_eal_alarm_set(SFC_MGMT_EV_QPOLL_PERIOD_US,
404                                sfc_ev_mgmt_periodic_qpoll, sa);
405         if (rc != 0)
406                 sfc_panic(sa,
407                           "cannot rearm management EVQ polling alarm (rc=%d)",
408                           rc);
409 }
410
411 static void
412 sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter *sa)
413 {
414         sfc_ev_mgmt_periodic_qpoll(sa);
415 }
416
417 static void
418 sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter *sa)
419 {
420         rte_eal_alarm_cancel(sfc_ev_mgmt_periodic_qpoll, sa);
421 }
422
423 int
424 sfc_ev_start(struct sfc_adapter *sa)
425 {
426         int rc;
427
428         sfc_log_init(sa, "entry");
429
430         rc = efx_ev_init(sa->nic);
431         if (rc != 0)
432                 goto fail_ev_init;
433
434         /* Start management EVQ used for global events */
435         rte_spinlock_lock(&sa->mgmt_evq_lock);
436
437         rc = sfc_ev_qstart(sa, sa->mgmt_evq_index);
438         if (rc != 0)
439                 goto fail_mgmt_evq_start;
440
441         rte_spinlock_unlock(&sa->mgmt_evq_lock);
442
443         /*
444          * Start management EVQ polling. If interrupts are disabled
445          * (not used), it is required to process link status change
446          * and other device level events to avoid unrecoverable
447          * error because the event queue overflow.
448          */
449         sfc_ev_mgmt_periodic_qpoll_start(sa);
450
451         /*
452          * Rx/Tx event queues are started/stopped when corresponding
453          * Rx/Tx queue is started/stopped.
454          */
455
456         return 0;
457
458 fail_mgmt_evq_start:
459         rte_spinlock_unlock(&sa->mgmt_evq_lock);
460         efx_ev_fini(sa->nic);
461
462 fail_ev_init:
463         sfc_log_init(sa, "failed %d", rc);
464         return rc;
465 }
466
467 void
468 sfc_ev_stop(struct sfc_adapter *sa)
469 {
470         unsigned int sw_index;
471
472         sfc_log_init(sa, "entry");
473
474         sfc_ev_mgmt_periodic_qpoll_stop(sa);
475
476         /* Make sure that all event queues are stopped */
477         sw_index = sa->evq_count;
478         while (sw_index-- > 0) {
479                 if (sw_index == sa->mgmt_evq_index) {
480                         /* Locks are required for the management EVQ */
481                         rte_spinlock_lock(&sa->mgmt_evq_lock);
482                         sfc_ev_qstop(sa, sa->mgmt_evq_index);
483                         rte_spinlock_unlock(&sa->mgmt_evq_lock);
484                 } else {
485                         sfc_ev_qstop(sa, sw_index);
486                 }
487         }
488
489         efx_ev_fini(sa->nic);
490 }
491
492 int
493 sfc_ev_qinit(struct sfc_adapter *sa, unsigned int sw_index,
494              unsigned int entries, int socket_id)
495 {
496         struct sfc_evq_info *evq_info;
497         struct sfc_evq *evq;
498         int rc;
499
500         sfc_log_init(sa, "sw_index=%u", sw_index);
501
502         evq_info = &sa->evq_info[sw_index];
503
504         SFC_ASSERT(rte_is_power_of_2(entries));
505         SFC_ASSERT(entries <= evq_info->max_entries);
506         evq_info->entries = entries;
507
508         evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
509                                  socket_id);
510         if (evq == NULL)
511                 return ENOMEM;
512
513         evq->sa = sa;
514         evq->evq_index = sw_index;
515
516         /* Allocate DMA space */
517         rc = sfc_dma_alloc(sa, "evq", sw_index, EFX_EVQ_SIZE(evq_info->entries),
518                            socket_id, &evq->mem);
519         if (rc != 0)
520                 return rc;
521
522         evq->init_state = SFC_EVQ_INITIALIZED;
523
524         evq_info->evq = evq;
525
526         return 0;
527 }
528
529 void
530 sfc_ev_qfini(struct sfc_adapter *sa, unsigned int sw_index)
531 {
532         struct sfc_evq *evq;
533
534         sfc_log_init(sa, "sw_index=%u", sw_index);
535
536         evq = sa->evq_info[sw_index].evq;
537
538         SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
539
540         sa->evq_info[sw_index].evq = NULL;
541
542         sfc_dma_free(sa, &evq->mem);
543
544         rte_free(evq);
545 }
546
547 static int
548 sfc_ev_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
549 {
550         struct sfc_evq_info *evq_info = &sa->evq_info[sw_index];
551         unsigned int max_entries;
552
553         sfc_log_init(sa, "sw_index=%u", sw_index);
554
555         max_entries = sfc_evq_max_entries(sa, sw_index);
556         SFC_ASSERT(rte_is_power_of_2(max_entries));
557
558         evq_info->max_entries = max_entries;
559
560         return 0;
561 }
562
563 static void
564 sfc_ev_qfini_info(struct sfc_adapter *sa, unsigned int sw_index)
565 {
566         sfc_log_init(sa, "sw_index=%u", sw_index);
567
568         /* Nothing to cleanup */
569 }
570
571 int
572 sfc_ev_init(struct sfc_adapter *sa)
573 {
574         int rc;
575         unsigned int sw_index;
576
577         sfc_log_init(sa, "entry");
578
579         sa->evq_count = sfc_ev_qcount(sa);
580         sa->mgmt_evq_index = 0;
581         rte_spinlock_init(&sa->mgmt_evq_lock);
582
583         /* Allocate EVQ info array */
584         rc = ENOMEM;
585         sa->evq_info = rte_calloc_socket("sfc-evqs", sa->evq_count,
586                                          sizeof(struct sfc_evq_info), 0,
587                                          sa->socket_id);
588         if (sa->evq_info == NULL)
589                 goto fail_evqs_alloc;
590
591         for (sw_index = 0; sw_index < sa->evq_count; ++sw_index) {
592                 rc = sfc_ev_qinit_info(sa, sw_index);
593                 if (rc != 0)
594                         goto fail_ev_qinit_info;
595         }
596
597         rc = sfc_ev_qinit(sa, sa->mgmt_evq_index, SFC_MGMT_EVQ_ENTRIES,
598                           sa->socket_id);
599         if (rc != 0)
600                 goto fail_mgmt_evq_init;
601
602         /*
603          * Rx/Tx event queues are created/destroyed when corresponding
604          * Rx/Tx queue is created/destroyed.
605          */
606
607         return 0;
608
609 fail_mgmt_evq_init:
610 fail_ev_qinit_info:
611         while (sw_index-- > 0)
612                 sfc_ev_qfini_info(sa, sw_index);
613
614         rte_free(sa->evq_info);
615         sa->evq_info = NULL;
616
617 fail_evqs_alloc:
618         sa->evq_count = 0;
619         sfc_log_init(sa, "failed %d", rc);
620         return rc;
621 }
622
623 void
624 sfc_ev_fini(struct sfc_adapter *sa)
625 {
626         int sw_index;
627
628         sfc_log_init(sa, "entry");
629
630         /* Cleanup all event queues */
631         sw_index = sa->evq_count;
632         while (--sw_index >= 0) {
633                 if (sa->evq_info[sw_index].evq != NULL)
634                         sfc_ev_qfini(sa, sw_index);
635                 sfc_ev_qfini_info(sa, sw_index);
636         }
637
638         rte_free(sa->evq_info);
639         sa->evq_info = NULL;
640         sa->evq_count = 0;
641 }