ee4cd707c6d753ec964b38f3fde3e3cb917f260a
[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, efx_link_mode_t link_mode)
185 {
186         struct sfc_evq *evq = arg;
187         struct sfc_adapter *sa = evq->sa;
188         struct rte_eth_link *dev_link = &sa->eth_dev->data->dev_link;
189         struct rte_eth_link new_link;
190
191         EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
192
193         sfc_port_link_mode_to_info(link_mode, &new_link);
194         rte_atomic64_set((rte_atomic64_t *)dev_link, *(uint64_t *)&new_link);
195
196         return B_FALSE;
197 }
198
199 static const efx_ev_callbacks_t sfc_ev_callbacks = {
200         .eec_initialized        = sfc_ev_initialized,
201         .eec_rx                 = sfc_ev_rx,
202         .eec_tx                 = sfc_ev_tx,
203         .eec_exception          = sfc_ev_exception,
204         .eec_rxq_flush_done     = sfc_ev_rxq_flush_done,
205         .eec_rxq_flush_failed   = sfc_ev_rxq_flush_failed,
206         .eec_txq_flush_done     = sfc_ev_txq_flush_done,
207         .eec_software           = sfc_ev_software,
208         .eec_sram               = sfc_ev_sram,
209         .eec_wake_up            = sfc_ev_wake_up,
210         .eec_timer              = sfc_ev_timer,
211         .eec_link_change        = sfc_ev_link_change,
212 };
213
214
215 void
216 sfc_ev_qpoll(struct sfc_evq *evq)
217 {
218         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
219                    evq->init_state == SFC_EVQ_STARTING);
220
221         /* Synchronize the DMA memory for reading not required */
222
223         efx_ev_qpoll(evq->common, &evq->read_ptr, &sfc_ev_callbacks, evq);
224
225         /* Poll-mode driver does not re-prime the event queue for interrupts */
226 }
227
228 void
229 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
230 {
231         if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
232                 struct sfc_evq *mgmt_evq = sa->evq_info[sa->mgmt_evq_index].evq;
233
234                 if (mgmt_evq->init_state == SFC_EVQ_STARTED)
235                         sfc_ev_qpoll(mgmt_evq);
236
237                 rte_spinlock_unlock(&sa->mgmt_evq_lock);
238         }
239 }
240
241 int
242 sfc_ev_qprime(struct sfc_evq *evq)
243 {
244         SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
245         return efx_ev_qprime(evq->common, evq->read_ptr);
246 }
247
248 int
249 sfc_ev_qstart(struct sfc_adapter *sa, unsigned int sw_index)
250 {
251         const struct sfc_evq_info *evq_info;
252         struct sfc_evq *evq;
253         efsys_mem_t *esmp;
254         unsigned int total_delay_us;
255         unsigned int delay_us;
256         int rc;
257
258         sfc_log_init(sa, "sw_index=%u", sw_index);
259
260         evq_info = &sa->evq_info[sw_index];
261         evq = evq_info->evq;
262         esmp = &evq->mem;
263
264         /* Clear all events */
265         (void)memset((void *)esmp->esm_base, 0xff,
266                      EFX_EVQ_SIZE(evq_info->entries));
267
268         /* Create the common code event queue */
269         rc = efx_ev_qcreate(sa->nic, sw_index, esmp, evq_info->entries,
270                             0 /* unused on EF10 */, 0,
271                             EFX_EVQ_FLAGS_TYPE_THROUGHPUT |
272                             EFX_EVQ_FLAGS_NOTIFY_DISABLED,
273                             &evq->common);
274         if (rc != 0)
275                 goto fail_ev_qcreate;
276
277         evq->init_state = SFC_EVQ_STARTING;
278
279         /* Wait for the initialization event */
280         total_delay_us = 0;
281         delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
282         do {
283                 (void)sfc_ev_qpoll(evq);
284
285                 /* Check to see if the initialization complete indication
286                  * posted by the hardware.
287                  */
288                 if (evq->init_state == SFC_EVQ_STARTED)
289                         goto done;
290
291                 /* Give event queue some time to init */
292                 rte_delay_us(delay_us);
293
294                 total_delay_us += delay_us;
295
296                 /* Exponential backoff */
297                 delay_us *= 2;
298                 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
299                         delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
300
301         } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
302
303         rc = ETIMEDOUT;
304         goto fail_timedout;
305
306 done:
307         return 0;
308
309 fail_timedout:
310         evq->init_state = SFC_EVQ_INITIALIZED;
311         efx_ev_qdestroy(evq->common);
312
313 fail_ev_qcreate:
314         sfc_log_init(sa, "failed %d", rc);
315         return rc;
316 }
317
318 void
319 sfc_ev_qstop(struct sfc_adapter *sa, unsigned int sw_index)
320 {
321         const struct sfc_evq_info *evq_info;
322         struct sfc_evq *evq;
323
324         sfc_log_init(sa, "sw_index=%u", sw_index);
325
326         SFC_ASSERT(sw_index < sa->evq_count);
327
328         evq_info = &sa->evq_info[sw_index];
329         evq = evq_info->evq;
330
331         if (evq == NULL || evq->init_state != SFC_EVQ_STARTED)
332                 return;
333
334         evq->init_state = SFC_EVQ_INITIALIZED;
335         evq->read_ptr = 0;
336         evq->exception = B_FALSE;
337
338         efx_ev_qdestroy(evq->common);
339 }
340
341 static void
342 sfc_ev_mgmt_periodic_qpoll(void *arg)
343 {
344         struct sfc_adapter *sa = arg;
345         int rc;
346
347         sfc_ev_mgmt_qpoll(sa);
348
349         rc = rte_eal_alarm_set(SFC_MGMT_EV_QPOLL_PERIOD_US,
350                                sfc_ev_mgmt_periodic_qpoll, sa);
351         if (rc != 0)
352                 sfc_panic(sa,
353                           "cannot rearm management EVQ polling alarm (rc=%d)",
354                           rc);
355 }
356
357 static void
358 sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter *sa)
359 {
360         sfc_ev_mgmt_periodic_qpoll(sa);
361 }
362
363 static void
364 sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter *sa)
365 {
366         rte_eal_alarm_cancel(sfc_ev_mgmt_periodic_qpoll, sa);
367 }
368
369 int
370 sfc_ev_start(struct sfc_adapter *sa)
371 {
372         int rc;
373
374         sfc_log_init(sa, "entry");
375
376         rc = efx_ev_init(sa->nic);
377         if (rc != 0)
378                 goto fail_ev_init;
379
380         /* Start management EVQ used for global events */
381         rte_spinlock_lock(&sa->mgmt_evq_lock);
382
383         rc = sfc_ev_qstart(sa, sa->mgmt_evq_index);
384         if (rc != 0)
385                 goto fail_mgmt_evq_start;
386
387         rte_spinlock_unlock(&sa->mgmt_evq_lock);
388
389         /*
390          * Start management EVQ polling. If interrupts are disabled
391          * (not used), it is required to process link status change
392          * and other device level events to avoid unrecoverable
393          * error because the event queue overflow.
394          */
395         sfc_ev_mgmt_periodic_qpoll_start(sa);
396
397         /*
398          * Rx/Tx event queues are started/stopped when corresponding
399          * Rx/Tx queue is started/stopped.
400          */
401
402         return 0;
403
404 fail_mgmt_evq_start:
405         rte_spinlock_unlock(&sa->mgmt_evq_lock);
406         efx_ev_fini(sa->nic);
407
408 fail_ev_init:
409         sfc_log_init(sa, "failed %d", rc);
410         return rc;
411 }
412
413 void
414 sfc_ev_stop(struct sfc_adapter *sa)
415 {
416         unsigned int sw_index;
417
418         sfc_log_init(sa, "entry");
419
420         sfc_ev_mgmt_periodic_qpoll_stop(sa);
421
422         /* Make sure that all event queues are stopped */
423         sw_index = sa->evq_count;
424         while (sw_index-- > 0) {
425                 if (sw_index == sa->mgmt_evq_index) {
426                         /* Locks are required for the management EVQ */
427                         rte_spinlock_lock(&sa->mgmt_evq_lock);
428                         sfc_ev_qstop(sa, sa->mgmt_evq_index);
429                         rte_spinlock_unlock(&sa->mgmt_evq_lock);
430                 } else {
431                         sfc_ev_qstop(sa, sw_index);
432                 }
433         }
434
435         efx_ev_fini(sa->nic);
436 }
437
438 int
439 sfc_ev_qinit(struct sfc_adapter *sa, unsigned int sw_index,
440              unsigned int entries, int socket_id)
441 {
442         struct sfc_evq_info *evq_info;
443         struct sfc_evq *evq;
444         int rc;
445
446         sfc_log_init(sa, "sw_index=%u", sw_index);
447
448         evq_info = &sa->evq_info[sw_index];
449
450         SFC_ASSERT(rte_is_power_of_2(entries));
451         SFC_ASSERT(entries <= evq_info->max_entries);
452         evq_info->entries = entries;
453
454         evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
455                                  socket_id);
456         if (evq == NULL)
457                 return ENOMEM;
458
459         evq->sa = sa;
460         evq->evq_index = sw_index;
461
462         /* Allocate DMA space */
463         rc = sfc_dma_alloc(sa, "evq", sw_index, EFX_EVQ_SIZE(evq_info->entries),
464                            socket_id, &evq->mem);
465         if (rc != 0)
466                 return rc;
467
468         evq->init_state = SFC_EVQ_INITIALIZED;
469
470         evq_info->evq = evq;
471
472         return 0;
473 }
474
475 void
476 sfc_ev_qfini(struct sfc_adapter *sa, unsigned int sw_index)
477 {
478         struct sfc_evq *evq;
479
480         sfc_log_init(sa, "sw_index=%u", sw_index);
481
482         evq = sa->evq_info[sw_index].evq;
483
484         SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
485
486         sa->evq_info[sw_index].evq = NULL;
487
488         sfc_dma_free(sa, &evq->mem);
489
490         rte_free(evq);
491 }
492
493 static int
494 sfc_ev_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
495 {
496         struct sfc_evq_info *evq_info = &sa->evq_info[sw_index];
497         unsigned int max_entries;
498
499         sfc_log_init(sa, "sw_index=%u", sw_index);
500
501         max_entries = sfc_evq_max_entries(sa, sw_index);
502         SFC_ASSERT(rte_is_power_of_2(max_entries));
503
504         evq_info->max_entries = max_entries;
505
506         return 0;
507 }
508
509 static void
510 sfc_ev_qfini_info(struct sfc_adapter *sa, unsigned int sw_index)
511 {
512         sfc_log_init(sa, "sw_index=%u", sw_index);
513
514         /* Nothing to cleanup */
515 }
516
517 int
518 sfc_ev_init(struct sfc_adapter *sa)
519 {
520         int rc;
521         unsigned int sw_index;
522
523         sfc_log_init(sa, "entry");
524
525         sa->evq_count = sfc_ev_qcount(sa);
526         sa->mgmt_evq_index = 0;
527         rte_spinlock_init(&sa->mgmt_evq_lock);
528
529         /* Allocate EVQ info array */
530         rc = ENOMEM;
531         sa->evq_info = rte_calloc_socket("sfc-evqs", sa->evq_count,
532                                          sizeof(struct sfc_evq_info), 0,
533                                          sa->socket_id);
534         if (sa->evq_info == NULL)
535                 goto fail_evqs_alloc;
536
537         for (sw_index = 0; sw_index < sa->evq_count; ++sw_index) {
538                 rc = sfc_ev_qinit_info(sa, sw_index);
539                 if (rc != 0)
540                         goto fail_ev_qinit_info;
541         }
542
543         rc = sfc_ev_qinit(sa, sa->mgmt_evq_index, SFC_MGMT_EVQ_ENTRIES,
544                           sa->socket_id);
545         if (rc != 0)
546                 goto fail_mgmt_evq_init;
547
548         /*
549          * Rx/Tx event queues are created/destroyed when corresponding
550          * Rx/Tx queue is created/destroyed.
551          */
552
553         return 0;
554
555 fail_mgmt_evq_init:
556 fail_ev_qinit_info:
557         while (sw_index-- > 0)
558                 sfc_ev_qfini_info(sa, sw_index);
559
560         rte_free(sa->evq_info);
561         sa->evq_info = NULL;
562
563 fail_evqs_alloc:
564         sa->evq_count = 0;
565         sfc_log_init(sa, "failed %d", rc);
566         return rc;
567 }
568
569 void
570 sfc_ev_fini(struct sfc_adapter *sa)
571 {
572         int sw_index;
573
574         sfc_log_init(sa, "entry");
575
576         /* Cleanup all event queues */
577         sw_index = sa->evq_count;
578         while (--sw_index >= 0) {
579                 if (sa->evq_info[sw_index].evq != NULL)
580                         sfc_ev_qfini(sa, sw_index);
581                 sfc_ev_qfini_info(sa, sw_index);
582         }
583
584         rte_free(sa->evq_info);
585         sa->evq_info = NULL;
586         sa->evq_count = 0;
587 }