net/sfc: implement EVQ dummy exception handling
[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 int
219 sfc_ev_qprime(struct sfc_evq *evq)
220 {
221         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
222         return efx_ev_qprime(evq->common, evq->read_ptr);
223 }
224
225 int
226 sfc_ev_qstart(struct sfc_adapter *sa, unsigned int sw_index)
227 {
228         const struct sfc_evq_info *evq_info;
229         struct sfc_evq *evq;
230         efsys_mem_t *esmp;
231         unsigned int total_delay_us;
232         unsigned int delay_us;
233         int rc;
234
235         sfc_log_init(sa, "sw_index=%u", sw_index);
236
237         evq_info = &sa->evq_info[sw_index];
238         evq = evq_info->evq;
239         esmp = &evq->mem;
240
241         /* Clear all events */
242         (void)memset((void *)esmp->esm_base, 0xff,
243                      EFX_EVQ_SIZE(evq_info->entries));
244
245         /* Create the common code event queue */
246         rc = efx_ev_qcreate(sa->nic, sw_index, esmp, evq_info->entries,
247                             0 /* unused on EF10 */, 0,
248                             EFX_EVQ_FLAGS_TYPE_THROUGHPUT |
249                             EFX_EVQ_FLAGS_NOTIFY_DISABLED,
250                             &evq->common);
251         if (rc != 0)
252                 goto fail_ev_qcreate;
253
254         evq->init_state = SFC_EVQ_STARTING;
255
256         /* Wait for the initialization event */
257         total_delay_us = 0;
258         delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
259         do {
260                 (void)sfc_ev_qpoll(evq);
261
262                 /* Check to see if the initialization complete indication
263                  * posted by the hardware.
264                  */
265                 if (evq->init_state == SFC_EVQ_STARTED)
266                         goto done;
267
268                 /* Give event queue some time to init */
269                 rte_delay_us(delay_us);
270
271                 total_delay_us += delay_us;
272
273                 /* Exponential backoff */
274                 delay_us *= 2;
275                 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
276                         delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
277
278         } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
279
280         rc = ETIMEDOUT;
281         goto fail_timedout;
282
283 done:
284         return 0;
285
286 fail_timedout:
287         evq->init_state = SFC_EVQ_INITIALIZED;
288         efx_ev_qdestroy(evq->common);
289
290 fail_ev_qcreate:
291         sfc_log_init(sa, "failed %d", rc);
292         return rc;
293 }
294
295 void
296 sfc_ev_qstop(struct sfc_adapter *sa, unsigned int sw_index)
297 {
298         const struct sfc_evq_info *evq_info;
299         struct sfc_evq *evq;
300
301         sfc_log_init(sa, "sw_index=%u", sw_index);
302
303         SFC_ASSERT(sw_index < sa->evq_count);
304
305         evq_info = &sa->evq_info[sw_index];
306         evq = evq_info->evq;
307
308         if (evq == NULL || evq->init_state != SFC_EVQ_STARTED)
309                 return;
310
311         evq->init_state = SFC_EVQ_INITIALIZED;
312         evq->read_ptr = 0;
313         evq->exception = B_FALSE;
314
315         efx_ev_qdestroy(evq->common);
316 }
317
318 int
319 sfc_ev_start(struct sfc_adapter *sa)
320 {
321         int rc;
322
323         sfc_log_init(sa, "entry");
324
325         rc = efx_ev_init(sa->nic);
326         if (rc != 0)
327                 goto fail_ev_init;
328
329         /*
330          * Rx/Tx event queues are started/stopped when corresponding queue
331          * is started/stopped.
332          */
333
334         return 0;
335
336 fail_ev_init:
337         sfc_log_init(sa, "failed %d", rc);
338         return rc;
339 }
340
341 void
342 sfc_ev_stop(struct sfc_adapter *sa)
343 {
344         unsigned int sw_index;
345
346         sfc_log_init(sa, "entry");
347
348         /* Make sure that all event queues are stopped */
349         sw_index = sa->evq_count;
350         while (sw_index-- > 0)
351                 sfc_ev_qstop(sa, sw_index);
352
353         efx_ev_fini(sa->nic);
354 }
355
356 int
357 sfc_ev_qinit(struct sfc_adapter *sa, unsigned int sw_index,
358              unsigned int entries, int socket_id)
359 {
360         struct sfc_evq_info *evq_info;
361         struct sfc_evq *evq;
362         int rc;
363
364         sfc_log_init(sa, "sw_index=%u", sw_index);
365
366         evq_info = &sa->evq_info[sw_index];
367
368         SFC_ASSERT(rte_is_power_of_2(entries));
369         SFC_ASSERT(entries <= evq_info->max_entries);
370         evq_info->entries = entries;
371
372         evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
373                                  socket_id);
374         if (evq == NULL)
375                 return ENOMEM;
376
377         evq->sa = sa;
378         evq->evq_index = sw_index;
379
380         /* Allocate DMA space */
381         rc = sfc_dma_alloc(sa, "evq", sw_index, EFX_EVQ_SIZE(evq_info->entries),
382                            socket_id, &evq->mem);
383         if (rc != 0)
384                 return rc;
385
386         evq->init_state = SFC_EVQ_INITIALIZED;
387
388         evq_info->evq = evq;
389
390         return 0;
391 }
392
393 void
394 sfc_ev_qfini(struct sfc_adapter *sa, unsigned int sw_index)
395 {
396         struct sfc_evq *evq;
397
398         sfc_log_init(sa, "sw_index=%u", sw_index);
399
400         evq = sa->evq_info[sw_index].evq;
401
402         SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
403
404         sa->evq_info[sw_index].evq = NULL;
405
406         sfc_dma_free(sa, &evq->mem);
407
408         rte_free(evq);
409 }
410
411 static int
412 sfc_ev_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
413 {
414         struct sfc_evq_info *evq_info = &sa->evq_info[sw_index];
415         unsigned int max_entries;
416
417         sfc_log_init(sa, "sw_index=%u", sw_index);
418
419         max_entries = sfc_evq_max_entries(sa, sw_index);
420         SFC_ASSERT(rte_is_power_of_2(max_entries));
421
422         evq_info->max_entries = max_entries;
423
424         return 0;
425 }
426
427 static void
428 sfc_ev_qfini_info(struct sfc_adapter *sa, unsigned int sw_index)
429 {
430         sfc_log_init(sa, "sw_index=%u", sw_index);
431
432         /* Nothing to cleanup */
433 }
434
435 int
436 sfc_ev_init(struct sfc_adapter *sa)
437 {
438         int rc;
439         unsigned int sw_index;
440
441         sfc_log_init(sa, "entry");
442
443         sa->evq_count = sfc_ev_qcount(sa);
444         sa->mgmt_evq_index = 0;
445
446         /* Allocate EVQ info array */
447         rc = ENOMEM;
448         sa->evq_info = rte_calloc_socket("sfc-evqs", sa->evq_count,
449                                          sizeof(struct sfc_evq_info), 0,
450                                          sa->socket_id);
451         if (sa->evq_info == NULL)
452                 goto fail_evqs_alloc;
453
454         for (sw_index = 0; sw_index < sa->evq_count; ++sw_index) {
455                 rc = sfc_ev_qinit_info(sa, sw_index);
456                 if (rc != 0)
457                         goto fail_ev_qinit_info;
458         }
459
460         /*
461          * Rx/Tx event queues are created/destroyed when corresponding
462          * Rx/Tx queue is created/destroyed.
463          */
464
465         return 0;
466
467 fail_ev_qinit_info:
468         while (sw_index-- > 0)
469                 sfc_ev_qfini_info(sa, sw_index);
470
471         rte_free(sa->evq_info);
472         sa->evq_info = NULL;
473
474 fail_evqs_alloc:
475         sa->evq_count = 0;
476         sfc_log_init(sa, "failed %d", rc);
477         return rc;
478 }
479
480 void
481 sfc_ev_fini(struct sfc_adapter *sa)
482 {
483         int sw_index;
484
485         sfc_log_init(sa, "entry");
486
487         /* Cleanup all event queues */
488         sw_index = sa->evq_count;
489         while (--sw_index >= 0) {
490                 if (sa->evq_info[sw_index].evq != NULL)
491                         sfc_ev_qfini(sa, sw_index);
492                 sfc_ev_qfini_info(sa, sw_index);
493         }
494
495         rte_free(sa->evq_info);
496         sa->evq_info = NULL;
497         sa->evq_count = 0;
498 }