2 * Copyright (c) 2016 Solarflare Communications Inc.
5 * This software was jointly developed between OKTET Labs (under contract
6 * for Solarflare) and Solarflare Communications, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
30 #include <rte_debug.h>
31 #include <rte_cycles.h>
32 #include <rte_alarm.h>
37 #include "sfc_debug.h"
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)
49 /* Management event queue polling period in microseconds */
50 #define SFC_MGMT_EV_QPOLL_PERIOD_US (US_PER_S)
54 sfc_ev_initialized(void *arg)
56 struct sfc_evq *evq = arg;
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);
62 evq->init_state = SFC_EVQ_STARTED;
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)
71 struct sfc_evq *evq = arg;
73 sfc_err(evq->sa, "EVQ %u unexpected Rx event", evq->evq_index);
78 sfc_ev_tx(void *arg, __rte_unused uint32_t label, __rte_unused uint32_t id)
80 struct sfc_evq *evq = arg;
82 sfc_err(evq->sa, "EVQ %u unexpected Tx event", evq->evq_index);
87 sfc_ev_exception(void *arg, __rte_unused uint32_t code,
88 __rte_unused uint32_t data)
90 struct sfc_evq *evq = arg;
92 if (code == EFX_EXCEPTION_UNKNOWN_SENSOREVT)
95 evq->exception = B_TRUE;
97 "hardware exception %s (code=%u, data=%#x) on EVQ %u;"
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" :
108 code, data, evq->evq_index);
114 sfc_ev_rxq_flush_done(void *arg, __rte_unused uint32_t rxq_hw_index)
116 struct sfc_evq *evq = arg;
118 sfc_err(evq->sa, "EVQ %u unexpected Rx flush done event",
124 sfc_ev_rxq_flush_failed(void *arg, __rte_unused uint32_t rxq_hw_index)
126 struct sfc_evq *evq = arg;
128 sfc_err(evq->sa, "EVQ %u unexpected Rx flush failed event",
134 sfc_ev_txq_flush_done(void *arg, __rte_unused uint32_t txq_hw_index)
136 struct sfc_evq *evq = arg;
138 sfc_err(evq->sa, "EVQ %u unexpected Tx flush done event",
144 sfc_ev_software(void *arg, uint16_t magic)
146 struct sfc_evq *evq = arg;
148 sfc_err(evq->sa, "EVQ %u unexpected software event magic=%#.4x",
149 evq->evq_index, magic);
154 sfc_ev_sram(void *arg, uint32_t code)
156 struct sfc_evq *evq = arg;
158 sfc_err(evq->sa, "EVQ %u unexpected SRAM event code=%u",
159 evq->evq_index, code);
164 sfc_ev_wake_up(void *arg, uint32_t index)
166 struct sfc_evq *evq = arg;
168 sfc_err(evq->sa, "EVQ %u unexpected wake up event index=%u",
169 evq->evq_index, index);
174 sfc_ev_timer(void *arg, uint32_t index)
176 struct sfc_evq *evq = arg;
178 sfc_err(evq->sa, "EVQ %u unexpected timer event index=%u",
179 evq->evq_index, index);
184 sfc_ev_link_change(void *arg, __rte_unused efx_link_mode_t link_mode)
186 struct sfc_evq *evq = arg;
188 sfc_err(evq->sa, "EVQ %u unexpected link change",
193 static const efx_ev_callbacks_t sfc_ev_callbacks = {
194 .eec_initialized = sfc_ev_initialized,
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,
210 sfc_ev_qpoll(struct sfc_evq *evq)
212 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
213 evq->init_state == SFC_EVQ_STARTING);
215 /* Synchronize the DMA memory for reading not required */
217 efx_ev_qpoll(evq->common, &evq->read_ptr, &sfc_ev_callbacks, evq);
219 /* Poll-mode driver does not re-prime the event queue for interrupts */
223 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
225 if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
226 struct sfc_evq *mgmt_evq = sa->evq_info[sa->mgmt_evq_index].evq;
228 if (mgmt_evq->init_state == SFC_EVQ_STARTED)
229 sfc_ev_qpoll(mgmt_evq);
231 rte_spinlock_unlock(&sa->mgmt_evq_lock);
236 sfc_ev_qprime(struct sfc_evq *evq)
238 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
239 return efx_ev_qprime(evq->common, evq->read_ptr);
243 sfc_ev_qstart(struct sfc_adapter *sa, unsigned int sw_index)
245 const struct sfc_evq_info *evq_info;
248 unsigned int total_delay_us;
249 unsigned int delay_us;
252 sfc_log_init(sa, "sw_index=%u", sw_index);
254 evq_info = &sa->evq_info[sw_index];
258 /* Clear all events */
259 (void)memset((void *)esmp->esm_base, 0xff,
260 EFX_EVQ_SIZE(evq_info->entries));
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,
269 goto fail_ev_qcreate;
271 evq->init_state = SFC_EVQ_STARTING;
273 /* Wait for the initialization event */
275 delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
277 (void)sfc_ev_qpoll(evq);
279 /* Check to see if the initialization complete indication
280 * posted by the hardware.
282 if (evq->init_state == SFC_EVQ_STARTED)
285 /* Give event queue some time to init */
286 rte_delay_us(delay_us);
288 total_delay_us += delay_us;
290 /* Exponential backoff */
292 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
293 delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
295 } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
304 evq->init_state = SFC_EVQ_INITIALIZED;
305 efx_ev_qdestroy(evq->common);
308 sfc_log_init(sa, "failed %d", rc);
313 sfc_ev_qstop(struct sfc_adapter *sa, unsigned int sw_index)
315 const struct sfc_evq_info *evq_info;
318 sfc_log_init(sa, "sw_index=%u", sw_index);
320 SFC_ASSERT(sw_index < sa->evq_count);
322 evq_info = &sa->evq_info[sw_index];
325 if (evq == NULL || evq->init_state != SFC_EVQ_STARTED)
328 evq->init_state = SFC_EVQ_INITIALIZED;
330 evq->exception = B_FALSE;
332 efx_ev_qdestroy(evq->common);
336 sfc_ev_mgmt_periodic_qpoll(void *arg)
338 struct sfc_adapter *sa = arg;
341 sfc_ev_mgmt_qpoll(sa);
343 rc = rte_eal_alarm_set(SFC_MGMT_EV_QPOLL_PERIOD_US,
344 sfc_ev_mgmt_periodic_qpoll, sa);
347 "cannot rearm management EVQ polling alarm (rc=%d)",
352 sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter *sa)
354 sfc_ev_mgmt_periodic_qpoll(sa);
358 sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter *sa)
360 rte_eal_alarm_cancel(sfc_ev_mgmt_periodic_qpoll, sa);
364 sfc_ev_start(struct sfc_adapter *sa)
368 sfc_log_init(sa, "entry");
370 rc = efx_ev_init(sa->nic);
374 /* Start management EVQ used for global events */
375 rte_spinlock_lock(&sa->mgmt_evq_lock);
377 rc = sfc_ev_qstart(sa, sa->mgmt_evq_index);
379 goto fail_mgmt_evq_start;
381 rte_spinlock_unlock(&sa->mgmt_evq_lock);
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.
389 sfc_ev_mgmt_periodic_qpoll_start(sa);
392 * Rx/Tx event queues are started/stopped when corresponding
393 * Rx/Tx queue is started/stopped.
399 rte_spinlock_unlock(&sa->mgmt_evq_lock);
400 efx_ev_fini(sa->nic);
403 sfc_log_init(sa, "failed %d", rc);
408 sfc_ev_stop(struct sfc_adapter *sa)
410 unsigned int sw_index;
412 sfc_log_init(sa, "entry");
414 sfc_ev_mgmt_periodic_qpoll_stop(sa);
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);
425 sfc_ev_qstop(sa, sw_index);
429 efx_ev_fini(sa->nic);
433 sfc_ev_qinit(struct sfc_adapter *sa, unsigned int sw_index,
434 unsigned int entries, int socket_id)
436 struct sfc_evq_info *evq_info;
440 sfc_log_init(sa, "sw_index=%u", sw_index);
442 evq_info = &sa->evq_info[sw_index];
444 SFC_ASSERT(rte_is_power_of_2(entries));
445 SFC_ASSERT(entries <= evq_info->max_entries);
446 evq_info->entries = entries;
448 evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
454 evq->evq_index = sw_index;
456 /* Allocate DMA space */
457 rc = sfc_dma_alloc(sa, "evq", sw_index, EFX_EVQ_SIZE(evq_info->entries),
458 socket_id, &evq->mem);
462 evq->init_state = SFC_EVQ_INITIALIZED;
470 sfc_ev_qfini(struct sfc_adapter *sa, unsigned int sw_index)
474 sfc_log_init(sa, "sw_index=%u", sw_index);
476 evq = sa->evq_info[sw_index].evq;
478 SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
480 sa->evq_info[sw_index].evq = NULL;
482 sfc_dma_free(sa, &evq->mem);
488 sfc_ev_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
490 struct sfc_evq_info *evq_info = &sa->evq_info[sw_index];
491 unsigned int max_entries;
493 sfc_log_init(sa, "sw_index=%u", sw_index);
495 max_entries = sfc_evq_max_entries(sa, sw_index);
496 SFC_ASSERT(rte_is_power_of_2(max_entries));
498 evq_info->max_entries = max_entries;
504 sfc_ev_qfini_info(struct sfc_adapter *sa, unsigned int sw_index)
506 sfc_log_init(sa, "sw_index=%u", sw_index);
508 /* Nothing to cleanup */
512 sfc_ev_init(struct sfc_adapter *sa)
515 unsigned int sw_index;
517 sfc_log_init(sa, "entry");
519 sa->evq_count = sfc_ev_qcount(sa);
520 sa->mgmt_evq_index = 0;
521 rte_spinlock_init(&sa->mgmt_evq_lock);
523 /* Allocate EVQ info array */
525 sa->evq_info = rte_calloc_socket("sfc-evqs", sa->evq_count,
526 sizeof(struct sfc_evq_info), 0,
528 if (sa->evq_info == NULL)
529 goto fail_evqs_alloc;
531 for (sw_index = 0; sw_index < sa->evq_count; ++sw_index) {
532 rc = sfc_ev_qinit_info(sa, sw_index);
534 goto fail_ev_qinit_info;
537 rc = sfc_ev_qinit(sa, sa->mgmt_evq_index, SFC_MGMT_EVQ_ENTRIES,
540 goto fail_mgmt_evq_init;
543 * Rx/Tx event queues are created/destroyed when corresponding
544 * Rx/Tx queue is created/destroyed.
551 while (sw_index-- > 0)
552 sfc_ev_qfini_info(sa, sw_index);
554 rte_free(sa->evq_info);
559 sfc_log_init(sa, "failed %d", rc);
564 sfc_ev_fini(struct sfc_adapter *sa)
568 sfc_log_init(sa, "entry");
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);
578 rte_free(sa->evq_info);