net/sfc: periodic management EVQ polling using alarm
[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
41
42 /* Initial delay when waiting for event queue init complete event */
43 #define SFC_EVQ_INIT_BACKOFF_START_US   (1)
44 /* Maximum delay between event queue polling attempts */
45 #define SFC_EVQ_INIT_BACKOFF_MAX_US     (10 * 1000)
46 /* Event queue init approx timeout */
47 #define SFC_EVQ_INIT_TIMEOUT_US         (2 * US_PER_S)
48
49 /* Management event queue polling period in microseconds */
50 #define SFC_MGMT_EV_QPOLL_PERIOD_US     (US_PER_S)
51
52
53 static boolean_t
54 sfc_ev_initialized(void *arg)
55 {
56         struct sfc_evq *evq = arg;
57
58         /* Init done events may be duplicated on SFN7xxx (SFC bug 31631) */
59         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTING ||
60                    evq->init_state == SFC_EVQ_STARTED);
61
62         evq->init_state = SFC_EVQ_STARTED;
63
64         return B_FALSE;
65 }
66
67 static boolean_t
68 sfc_ev_rx(void *arg, __rte_unused uint32_t label, __rte_unused uint32_t id,
69           __rte_unused uint32_t size, __rte_unused uint16_t flags)
70 {
71         struct sfc_evq *evq = arg;
72
73         sfc_err(evq->sa, "EVQ %u unexpected Rx event", evq->evq_index);
74         return B_TRUE;
75 }
76
77 static boolean_t
78 sfc_ev_tx(void *arg, __rte_unused uint32_t label, __rte_unused uint32_t id)
79 {
80         struct sfc_evq *evq = arg;
81
82         sfc_err(evq->sa, "EVQ %u unexpected Tx event", evq->evq_index);
83         return B_TRUE;
84 }
85
86 static boolean_t
87 sfc_ev_exception(void *arg, __rte_unused uint32_t code,
88                  __rte_unused uint32_t data)
89 {
90         struct sfc_evq *evq = arg;
91
92         if (code == EFX_EXCEPTION_UNKNOWN_SENSOREVT)
93                 return B_FALSE;
94
95         evq->exception = B_TRUE;
96         sfc_warn(evq->sa,
97                  "hardware exception %s (code=%u, data=%#x) on EVQ %u;"
98                  " needs recovery",
99                  (code == EFX_EXCEPTION_RX_RECOVERY) ? "RX_RECOVERY" :
100                  (code == EFX_EXCEPTION_RX_DSC_ERROR) ? "RX_DSC_ERROR" :
101                  (code == EFX_EXCEPTION_TX_DSC_ERROR) ? "TX_DSC_ERROR" :
102                  (code == EFX_EXCEPTION_FWALERT_SRAM) ? "FWALERT_SRAM" :
103                  (code == EFX_EXCEPTION_UNKNOWN_FWALERT) ? "UNKNOWN_FWALERT" :
104                  (code == EFX_EXCEPTION_RX_ERROR) ? "RX_ERROR" :
105                  (code == EFX_EXCEPTION_TX_ERROR) ? "TX_ERROR" :
106                  (code == EFX_EXCEPTION_EV_ERROR) ? "EV_ERROR" :
107                  "UNKNOWN",
108                  code, data, evq->evq_index);
109
110         return B_TRUE;
111 }
112
113 static boolean_t
114 sfc_ev_rxq_flush_done(void *arg, __rte_unused uint32_t rxq_hw_index)
115 {
116         struct sfc_evq *evq = arg;
117
118         sfc_err(evq->sa, "EVQ %u unexpected Rx flush done event",
119                 evq->evq_index);
120         return B_TRUE;
121 }
122
123 static boolean_t
124 sfc_ev_rxq_flush_failed(void *arg, __rte_unused uint32_t rxq_hw_index)
125 {
126         struct sfc_evq *evq = arg;
127
128         sfc_err(evq->sa, "EVQ %u unexpected Rx flush failed event",
129                 evq->evq_index);
130         return B_TRUE;
131 }
132
133 static boolean_t
134 sfc_ev_txq_flush_done(void *arg, __rte_unused uint32_t txq_hw_index)
135 {
136         struct sfc_evq *evq = arg;
137
138         sfc_err(evq->sa, "EVQ %u unexpected Tx flush done event",
139                 evq->evq_index);
140         return B_TRUE;
141 }
142
143 static boolean_t
144 sfc_ev_software(void *arg, uint16_t magic)
145 {
146         struct sfc_evq *evq = arg;
147
148         sfc_err(evq->sa, "EVQ %u unexpected software event magic=%#.4x",
149                 evq->evq_index, magic);
150         return B_TRUE;
151 }
152
153 static boolean_t
154 sfc_ev_sram(void *arg, uint32_t code)
155 {
156         struct sfc_evq *evq = arg;
157
158         sfc_err(evq->sa, "EVQ %u unexpected SRAM event code=%u",
159                 evq->evq_index, code);
160         return B_TRUE;
161 }
162
163 static boolean_t
164 sfc_ev_wake_up(void *arg, uint32_t index)
165 {
166         struct sfc_evq *evq = arg;
167
168         sfc_err(evq->sa, "EVQ %u unexpected wake up event index=%u",
169                 evq->evq_index, index);
170         return B_TRUE;
171 }
172
173 static boolean_t
174 sfc_ev_timer(void *arg, uint32_t index)
175 {
176         struct sfc_evq *evq = arg;
177
178         sfc_err(evq->sa, "EVQ %u unexpected timer event index=%u",
179                 evq->evq_index, index);
180         return B_TRUE;
181 }
182
183 static boolean_t
184 sfc_ev_link_change(void *arg, __rte_unused efx_link_mode_t link_mode)
185 {
186         struct sfc_evq *evq = arg;
187
188         sfc_err(evq->sa, "EVQ %u unexpected link change",
189                 evq->evq_index);
190         return B_TRUE;
191 }
192
193 static const efx_ev_callbacks_t sfc_ev_callbacks = {
194         .eec_initialized        = sfc_ev_initialized,
195         .eec_rx                 = sfc_ev_rx,
196         .eec_tx                 = sfc_ev_tx,
197         .eec_exception          = sfc_ev_exception,
198         .eec_rxq_flush_done     = sfc_ev_rxq_flush_done,
199         .eec_rxq_flush_failed   = sfc_ev_rxq_flush_failed,
200         .eec_txq_flush_done     = sfc_ev_txq_flush_done,
201         .eec_software           = sfc_ev_software,
202         .eec_sram               = sfc_ev_sram,
203         .eec_wake_up            = sfc_ev_wake_up,
204         .eec_timer              = sfc_ev_timer,
205         .eec_link_change        = sfc_ev_link_change,
206 };
207
208
209 void
210 sfc_ev_qpoll(struct sfc_evq *evq)
211 {
212         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
213                    evq->init_state == SFC_EVQ_STARTING);
214
215         /* Synchronize the DMA memory for reading not required */
216
217         efx_ev_qpoll(evq->common, &evq->read_ptr, &sfc_ev_callbacks, evq);
218
219         /* Poll-mode driver does not re-prime the event queue for interrupts */
220 }
221
222 void
223 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
224 {
225         if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
226                 struct sfc_evq *mgmt_evq = sa->evq_info[sa->mgmt_evq_index].evq;
227
228                 if (mgmt_evq->init_state == SFC_EVQ_STARTED)
229                         sfc_ev_qpoll(mgmt_evq);
230
231                 rte_spinlock_unlock(&sa->mgmt_evq_lock);
232         }
233 }
234
235 int
236 sfc_ev_qprime(struct sfc_evq *evq)
237 {
238         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
239         return efx_ev_qprime(evq->common, evq->read_ptr);
240 }
241
242 int
243 sfc_ev_qstart(struct sfc_adapter *sa, unsigned int sw_index)
244 {
245         const struct sfc_evq_info *evq_info;
246         struct sfc_evq *evq;
247         efsys_mem_t *esmp;
248         unsigned int total_delay_us;
249         unsigned int delay_us;
250         int rc;
251
252         sfc_log_init(sa, "sw_index=%u", sw_index);
253
254         evq_info = &sa->evq_info[sw_index];
255         evq = evq_info->evq;
256         esmp = &evq->mem;
257
258         /* Clear all events */
259         (void)memset((void *)esmp->esm_base, 0xff,
260                      EFX_EVQ_SIZE(evq_info->entries));
261
262         /* Create the common code event queue */
263         rc = efx_ev_qcreate(sa->nic, sw_index, esmp, evq_info->entries,
264                             0 /* unused on EF10 */, 0,
265                             EFX_EVQ_FLAGS_TYPE_THROUGHPUT |
266                             EFX_EVQ_FLAGS_NOTIFY_DISABLED,
267                             &evq->common);
268         if (rc != 0)
269                 goto fail_ev_qcreate;
270
271         evq->init_state = SFC_EVQ_STARTING;
272
273         /* Wait for the initialization event */
274         total_delay_us = 0;
275         delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
276         do {
277                 (void)sfc_ev_qpoll(evq);
278
279                 /* Check to see if the initialization complete indication
280                  * posted by the hardware.
281                  */
282                 if (evq->init_state == SFC_EVQ_STARTED)
283                         goto done;
284
285                 /* Give event queue some time to init */
286                 rte_delay_us(delay_us);
287
288                 total_delay_us += delay_us;
289
290                 /* Exponential backoff */
291                 delay_us *= 2;
292                 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
293                         delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
294
295         } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
296
297         rc = ETIMEDOUT;
298         goto fail_timedout;
299
300 done:
301         return 0;
302
303 fail_timedout:
304         evq->init_state = SFC_EVQ_INITIALIZED;
305         efx_ev_qdestroy(evq->common);
306
307 fail_ev_qcreate:
308         sfc_log_init(sa, "failed %d", rc);
309         return rc;
310 }
311
312 void
313 sfc_ev_qstop(struct sfc_adapter *sa, unsigned int sw_index)
314 {
315         const struct sfc_evq_info *evq_info;
316         struct sfc_evq *evq;
317
318         sfc_log_init(sa, "sw_index=%u", sw_index);
319
320         SFC_ASSERT(sw_index < sa->evq_count);
321
322         evq_info = &sa->evq_info[sw_index];
323         evq = evq_info->evq;
324
325         if (evq == NULL || evq->init_state != SFC_EVQ_STARTED)
326                 return;
327
328         evq->init_state = SFC_EVQ_INITIALIZED;
329         evq->read_ptr = 0;
330         evq->exception = B_FALSE;
331
332         efx_ev_qdestroy(evq->common);
333 }
334
335 static void
336 sfc_ev_mgmt_periodic_qpoll(void *arg)
337 {
338         struct sfc_adapter *sa = arg;
339         int rc;
340
341         sfc_ev_mgmt_qpoll(sa);
342
343         rc = rte_eal_alarm_set(SFC_MGMT_EV_QPOLL_PERIOD_US,
344                                sfc_ev_mgmt_periodic_qpoll, sa);
345         if (rc != 0)
346                 sfc_panic(sa,
347                           "cannot rearm management EVQ polling alarm (rc=%d)",
348                           rc);
349 }
350
351 static void
352 sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter *sa)
353 {
354         sfc_ev_mgmt_periodic_qpoll(sa);
355 }
356
357 static void
358 sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter *sa)
359 {
360         rte_eal_alarm_cancel(sfc_ev_mgmt_periodic_qpoll, sa);
361 }
362
363 int
364 sfc_ev_start(struct sfc_adapter *sa)
365 {
366         int rc;
367
368         sfc_log_init(sa, "entry");
369
370         rc = efx_ev_init(sa->nic);
371         if (rc != 0)
372                 goto fail_ev_init;
373
374         /* Start management EVQ used for global events */
375         rte_spinlock_lock(&sa->mgmt_evq_lock);
376
377         rc = sfc_ev_qstart(sa, sa->mgmt_evq_index);
378         if (rc != 0)
379                 goto fail_mgmt_evq_start;
380
381         rte_spinlock_unlock(&sa->mgmt_evq_lock);
382
383         /*
384          * Start management EVQ polling. If interrupts are disabled
385          * (not used), it is required to process link status change
386          * and other device level events to avoid unrecoverable
387          * error because the event queue overflow.
388          */
389         sfc_ev_mgmt_periodic_qpoll_start(sa);
390
391         /*
392          * Rx/Tx event queues are started/stopped when corresponding
393          * Rx/Tx queue is started/stopped.
394          */
395
396         return 0;
397
398 fail_mgmt_evq_start:
399         rte_spinlock_unlock(&sa->mgmt_evq_lock);
400         efx_ev_fini(sa->nic);
401
402 fail_ev_init:
403         sfc_log_init(sa, "failed %d", rc);
404         return rc;
405 }
406
407 void
408 sfc_ev_stop(struct sfc_adapter *sa)
409 {
410         unsigned int sw_index;
411
412         sfc_log_init(sa, "entry");
413
414         sfc_ev_mgmt_periodic_qpoll_stop(sa);
415
416         /* Make sure that all event queues are stopped */
417         sw_index = sa->evq_count;
418         while (sw_index-- > 0) {
419                 if (sw_index == sa->mgmt_evq_index) {
420                         /* Locks are required for the management EVQ */
421                         rte_spinlock_lock(&sa->mgmt_evq_lock);
422                         sfc_ev_qstop(sa, sa->mgmt_evq_index);
423                         rte_spinlock_unlock(&sa->mgmt_evq_lock);
424                 } else {
425                         sfc_ev_qstop(sa, sw_index);
426                 }
427         }
428
429         efx_ev_fini(sa->nic);
430 }
431
432 int
433 sfc_ev_qinit(struct sfc_adapter *sa, unsigned int sw_index,
434              unsigned int entries, int socket_id)
435 {
436         struct sfc_evq_info *evq_info;
437         struct sfc_evq *evq;
438         int rc;
439
440         sfc_log_init(sa, "sw_index=%u", sw_index);
441
442         evq_info = &sa->evq_info[sw_index];
443
444         SFC_ASSERT(rte_is_power_of_2(entries));
445         SFC_ASSERT(entries <= evq_info->max_entries);
446         evq_info->entries = entries;
447
448         evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
449                                  socket_id);
450         if (evq == NULL)
451                 return ENOMEM;
452
453         evq->sa = sa;
454         evq->evq_index = sw_index;
455
456         /* Allocate DMA space */
457         rc = sfc_dma_alloc(sa, "evq", sw_index, EFX_EVQ_SIZE(evq_info->entries),
458                            socket_id, &evq->mem);
459         if (rc != 0)
460                 return rc;
461
462         evq->init_state = SFC_EVQ_INITIALIZED;
463
464         evq_info->evq = evq;
465
466         return 0;
467 }
468
469 void
470 sfc_ev_qfini(struct sfc_adapter *sa, unsigned int sw_index)
471 {
472         struct sfc_evq *evq;
473
474         sfc_log_init(sa, "sw_index=%u", sw_index);
475
476         evq = sa->evq_info[sw_index].evq;
477
478         SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
479
480         sa->evq_info[sw_index].evq = NULL;
481
482         sfc_dma_free(sa, &evq->mem);
483
484         rte_free(evq);
485 }
486
487 static int
488 sfc_ev_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
489 {
490         struct sfc_evq_info *evq_info = &sa->evq_info[sw_index];
491         unsigned int max_entries;
492
493         sfc_log_init(sa, "sw_index=%u", sw_index);
494
495         max_entries = sfc_evq_max_entries(sa, sw_index);
496         SFC_ASSERT(rte_is_power_of_2(max_entries));
497
498         evq_info->max_entries = max_entries;
499
500         return 0;
501 }
502
503 static void
504 sfc_ev_qfini_info(struct sfc_adapter *sa, unsigned int sw_index)
505 {
506         sfc_log_init(sa, "sw_index=%u", sw_index);
507
508         /* Nothing to cleanup */
509 }
510
511 int
512 sfc_ev_init(struct sfc_adapter *sa)
513 {
514         int rc;
515         unsigned int sw_index;
516
517         sfc_log_init(sa, "entry");
518
519         sa->evq_count = sfc_ev_qcount(sa);
520         sa->mgmt_evq_index = 0;
521         rte_spinlock_init(&sa->mgmt_evq_lock);
522
523         /* Allocate EVQ info array */
524         rc = ENOMEM;
525         sa->evq_info = rte_calloc_socket("sfc-evqs", sa->evq_count,
526                                          sizeof(struct sfc_evq_info), 0,
527                                          sa->socket_id);
528         if (sa->evq_info == NULL)
529                 goto fail_evqs_alloc;
530
531         for (sw_index = 0; sw_index < sa->evq_count; ++sw_index) {
532                 rc = sfc_ev_qinit_info(sa, sw_index);
533                 if (rc != 0)
534                         goto fail_ev_qinit_info;
535         }
536
537         rc = sfc_ev_qinit(sa, sa->mgmt_evq_index, SFC_MGMT_EVQ_ENTRIES,
538                           sa->socket_id);
539         if (rc != 0)
540                 goto fail_mgmt_evq_init;
541
542         /*
543          * Rx/Tx event queues are created/destroyed when corresponding
544          * Rx/Tx queue is created/destroyed.
545          */
546
547         return 0;
548
549 fail_mgmt_evq_init:
550 fail_ev_qinit_info:
551         while (sw_index-- > 0)
552                 sfc_ev_qfini_info(sa, sw_index);
553
554         rte_free(sa->evq_info);
555         sa->evq_info = NULL;
556
557 fail_evqs_alloc:
558         sa->evq_count = 0;
559         sfc_log_init(sa, "failed %d", rc);
560         return rc;
561 }
562
563 void
564 sfc_ev_fini(struct sfc_adapter *sa)
565 {
566         int sw_index;
567
568         sfc_log_init(sa, "entry");
569
570         /* Cleanup all event queues */
571         sw_index = sa->evq_count;
572         while (--sw_index >= 0) {
573                 if (sa->evq_info[sw_index].evq != NULL)
574                         sfc_ev_qfini(sa, sw_index);
575                 sfc_ev_qfini_info(sa, sw_index);
576         }
577
578         rte_free(sa->evq_info);
579         sa->evq_info = NULL;
580         sa->evq_count = 0;
581 }