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