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>
36 #include "sfc_debug.h"
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)
50 sfc_ev_initialized(void *arg)
52 struct sfc_evq *evq = arg;
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);
58 evq->init_state = SFC_EVQ_STARTED;
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)
67 struct sfc_evq *evq = arg;
69 sfc_err(evq->sa, "EVQ %u unexpected Rx event", evq->evq_index);
74 sfc_ev_tx(void *arg, __rte_unused uint32_t label, __rte_unused uint32_t id)
76 struct sfc_evq *evq = arg;
78 sfc_err(evq->sa, "EVQ %u unexpected Tx event", evq->evq_index);
83 sfc_ev_exception(void *arg, __rte_unused uint32_t code,
84 __rte_unused uint32_t data)
86 struct sfc_evq *evq = arg;
88 if (code == EFX_EXCEPTION_UNKNOWN_SENSOREVT)
91 evq->exception = B_TRUE;
93 "hardware exception %s (code=%u, data=%#x) on EVQ %u;"
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" :
104 code, data, evq->evq_index);
110 sfc_ev_rxq_flush_done(void *arg, __rte_unused uint32_t rxq_hw_index)
112 struct sfc_evq *evq = arg;
114 sfc_err(evq->sa, "EVQ %u unexpected Rx flush done event",
120 sfc_ev_rxq_flush_failed(void *arg, __rte_unused uint32_t rxq_hw_index)
122 struct sfc_evq *evq = arg;
124 sfc_err(evq->sa, "EVQ %u unexpected Rx flush failed event",
130 sfc_ev_txq_flush_done(void *arg, __rte_unused uint32_t txq_hw_index)
132 struct sfc_evq *evq = arg;
134 sfc_err(evq->sa, "EVQ %u unexpected Tx flush done event",
140 sfc_ev_software(void *arg, uint16_t magic)
142 struct sfc_evq *evq = arg;
144 sfc_err(evq->sa, "EVQ %u unexpected software event magic=%#.4x",
145 evq->evq_index, magic);
150 sfc_ev_sram(void *arg, uint32_t code)
152 struct sfc_evq *evq = arg;
154 sfc_err(evq->sa, "EVQ %u unexpected SRAM event code=%u",
155 evq->evq_index, code);
160 sfc_ev_wake_up(void *arg, uint32_t index)
162 struct sfc_evq *evq = arg;
164 sfc_err(evq->sa, "EVQ %u unexpected wake up event index=%u",
165 evq->evq_index, index);
170 sfc_ev_timer(void *arg, uint32_t index)
172 struct sfc_evq *evq = arg;
174 sfc_err(evq->sa, "EVQ %u unexpected timer event index=%u",
175 evq->evq_index, index);
180 sfc_ev_link_change(void *arg, __rte_unused efx_link_mode_t link_mode)
182 struct sfc_evq *evq = arg;
184 sfc_err(evq->sa, "EVQ %u unexpected link change",
189 static const efx_ev_callbacks_t sfc_ev_callbacks = {
190 .eec_initialized = sfc_ev_initialized,
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,
206 sfc_ev_qpoll(struct sfc_evq *evq)
208 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
209 evq->init_state == SFC_EVQ_STARTING);
211 /* Synchronize the DMA memory for reading not required */
213 efx_ev_qpoll(evq->common, &evq->read_ptr, &sfc_ev_callbacks, evq);
215 /* Poll-mode driver does not re-prime the event queue for interrupts */
219 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
221 if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
222 struct sfc_evq *mgmt_evq = sa->evq_info[sa->mgmt_evq_index].evq;
224 if (mgmt_evq->init_state == SFC_EVQ_STARTED)
225 sfc_ev_qpoll(mgmt_evq);
227 rte_spinlock_unlock(&sa->mgmt_evq_lock);
232 sfc_ev_qprime(struct sfc_evq *evq)
234 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
235 return efx_ev_qprime(evq->common, evq->read_ptr);
239 sfc_ev_qstart(struct sfc_adapter *sa, unsigned int sw_index)
241 const struct sfc_evq_info *evq_info;
244 unsigned int total_delay_us;
245 unsigned int delay_us;
248 sfc_log_init(sa, "sw_index=%u", sw_index);
250 evq_info = &sa->evq_info[sw_index];
254 /* Clear all events */
255 (void)memset((void *)esmp->esm_base, 0xff,
256 EFX_EVQ_SIZE(evq_info->entries));
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,
265 goto fail_ev_qcreate;
267 evq->init_state = SFC_EVQ_STARTING;
269 /* Wait for the initialization event */
271 delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
273 (void)sfc_ev_qpoll(evq);
275 /* Check to see if the initialization complete indication
276 * posted by the hardware.
278 if (evq->init_state == SFC_EVQ_STARTED)
281 /* Give event queue some time to init */
282 rte_delay_us(delay_us);
284 total_delay_us += delay_us;
286 /* Exponential backoff */
288 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
289 delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
291 } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
300 evq->init_state = SFC_EVQ_INITIALIZED;
301 efx_ev_qdestroy(evq->common);
304 sfc_log_init(sa, "failed %d", rc);
309 sfc_ev_qstop(struct sfc_adapter *sa, unsigned int sw_index)
311 const struct sfc_evq_info *evq_info;
314 sfc_log_init(sa, "sw_index=%u", sw_index);
316 SFC_ASSERT(sw_index < sa->evq_count);
318 evq_info = &sa->evq_info[sw_index];
321 if (evq == NULL || evq->init_state != SFC_EVQ_STARTED)
324 evq->init_state = SFC_EVQ_INITIALIZED;
326 evq->exception = B_FALSE;
328 efx_ev_qdestroy(evq->common);
332 sfc_ev_start(struct sfc_adapter *sa)
336 sfc_log_init(sa, "entry");
338 rc = efx_ev_init(sa->nic);
342 /* Start management EVQ used for global events */
343 rte_spinlock_lock(&sa->mgmt_evq_lock);
345 rc = sfc_ev_qstart(sa, sa->mgmt_evq_index);
347 goto fail_mgmt_evq_start;
349 rte_spinlock_unlock(&sa->mgmt_evq_lock);
352 * Rx/Tx event queues are started/stopped when corresponding
353 * Rx/Tx queue is started/stopped.
359 rte_spinlock_unlock(&sa->mgmt_evq_lock);
360 efx_ev_fini(sa->nic);
363 sfc_log_init(sa, "failed %d", rc);
368 sfc_ev_stop(struct sfc_adapter *sa)
370 unsigned int sw_index;
372 sfc_log_init(sa, "entry");
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);
383 sfc_ev_qstop(sa, sw_index);
387 efx_ev_fini(sa->nic);
391 sfc_ev_qinit(struct sfc_adapter *sa, unsigned int sw_index,
392 unsigned int entries, int socket_id)
394 struct sfc_evq_info *evq_info;
398 sfc_log_init(sa, "sw_index=%u", sw_index);
400 evq_info = &sa->evq_info[sw_index];
402 SFC_ASSERT(rte_is_power_of_2(entries));
403 SFC_ASSERT(entries <= evq_info->max_entries);
404 evq_info->entries = entries;
406 evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
412 evq->evq_index = sw_index;
414 /* Allocate DMA space */
415 rc = sfc_dma_alloc(sa, "evq", sw_index, EFX_EVQ_SIZE(evq_info->entries),
416 socket_id, &evq->mem);
420 evq->init_state = SFC_EVQ_INITIALIZED;
428 sfc_ev_qfini(struct sfc_adapter *sa, unsigned int sw_index)
432 sfc_log_init(sa, "sw_index=%u", sw_index);
434 evq = sa->evq_info[sw_index].evq;
436 SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
438 sa->evq_info[sw_index].evq = NULL;
440 sfc_dma_free(sa, &evq->mem);
446 sfc_ev_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
448 struct sfc_evq_info *evq_info = &sa->evq_info[sw_index];
449 unsigned int max_entries;
451 sfc_log_init(sa, "sw_index=%u", sw_index);
453 max_entries = sfc_evq_max_entries(sa, sw_index);
454 SFC_ASSERT(rte_is_power_of_2(max_entries));
456 evq_info->max_entries = max_entries;
462 sfc_ev_qfini_info(struct sfc_adapter *sa, unsigned int sw_index)
464 sfc_log_init(sa, "sw_index=%u", sw_index);
466 /* Nothing to cleanup */
470 sfc_ev_init(struct sfc_adapter *sa)
473 unsigned int sw_index;
475 sfc_log_init(sa, "entry");
477 sa->evq_count = sfc_ev_qcount(sa);
478 sa->mgmt_evq_index = 0;
479 rte_spinlock_init(&sa->mgmt_evq_lock);
481 /* Allocate EVQ info array */
483 sa->evq_info = rte_calloc_socket("sfc-evqs", sa->evq_count,
484 sizeof(struct sfc_evq_info), 0,
486 if (sa->evq_info == NULL)
487 goto fail_evqs_alloc;
489 for (sw_index = 0; sw_index < sa->evq_count; ++sw_index) {
490 rc = sfc_ev_qinit_info(sa, sw_index);
492 goto fail_ev_qinit_info;
495 rc = sfc_ev_qinit(sa, sa->mgmt_evq_index, SFC_MGMT_EVQ_ENTRIES,
498 goto fail_mgmt_evq_init;
501 * Rx/Tx event queues are created/destroyed when corresponding
502 * Rx/Tx queue is created/destroyed.
509 while (sw_index-- > 0)
510 sfc_ev_qfini_info(sa, sw_index);
512 rte_free(sa->evq_info);
517 sfc_log_init(sa, "failed %d", rc);
522 sfc_ev_fini(struct sfc_adapter *sa)
526 sfc_log_init(sa, "entry");
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);
536 rte_free(sa->evq_info);