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_qprime(struct sfc_evq *evq)
221 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
222 return efx_ev_qprime(evq->common, evq->read_ptr);
226 sfc_ev_qstart(struct sfc_adapter *sa, unsigned int sw_index)
228 const struct sfc_evq_info *evq_info;
231 unsigned int total_delay_us;
232 unsigned int delay_us;
235 sfc_log_init(sa, "sw_index=%u", sw_index);
237 evq_info = &sa->evq_info[sw_index];
241 /* Clear all events */
242 (void)memset((void *)esmp->esm_base, 0xff,
243 EFX_EVQ_SIZE(evq_info->entries));
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,
252 goto fail_ev_qcreate;
254 evq->init_state = SFC_EVQ_STARTING;
256 /* Wait for the initialization event */
258 delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
260 (void)sfc_ev_qpoll(evq);
262 /* Check to see if the initialization complete indication
263 * posted by the hardware.
265 if (evq->init_state == SFC_EVQ_STARTED)
268 /* Give event queue some time to init */
269 rte_delay_us(delay_us);
271 total_delay_us += delay_us;
273 /* Exponential backoff */
275 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
276 delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
278 } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
287 evq->init_state = SFC_EVQ_INITIALIZED;
288 efx_ev_qdestroy(evq->common);
291 sfc_log_init(sa, "failed %d", rc);
296 sfc_ev_qstop(struct sfc_adapter *sa, unsigned int sw_index)
298 const struct sfc_evq_info *evq_info;
301 sfc_log_init(sa, "sw_index=%u", sw_index);
303 SFC_ASSERT(sw_index < sa->evq_count);
305 evq_info = &sa->evq_info[sw_index];
308 if (evq == NULL || evq->init_state != SFC_EVQ_STARTED)
311 evq->init_state = SFC_EVQ_INITIALIZED;
313 evq->exception = B_FALSE;
315 efx_ev_qdestroy(evq->common);
319 sfc_ev_start(struct sfc_adapter *sa)
323 sfc_log_init(sa, "entry");
325 rc = efx_ev_init(sa->nic);
330 * Rx/Tx event queues are started/stopped when corresponding queue
331 * is started/stopped.
337 sfc_log_init(sa, "failed %d", rc);
342 sfc_ev_stop(struct sfc_adapter *sa)
344 unsigned int sw_index;
346 sfc_log_init(sa, "entry");
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);
353 efx_ev_fini(sa->nic);
357 sfc_ev_qinit(struct sfc_adapter *sa, unsigned int sw_index,
358 unsigned int entries, int socket_id)
360 struct sfc_evq_info *evq_info;
364 sfc_log_init(sa, "sw_index=%u", sw_index);
366 evq_info = &sa->evq_info[sw_index];
368 SFC_ASSERT(rte_is_power_of_2(entries));
369 SFC_ASSERT(entries <= evq_info->max_entries);
370 evq_info->entries = entries;
372 evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
378 evq->evq_index = sw_index;
380 /* Allocate DMA space */
381 rc = sfc_dma_alloc(sa, "evq", sw_index, EFX_EVQ_SIZE(evq_info->entries),
382 socket_id, &evq->mem);
386 evq->init_state = SFC_EVQ_INITIALIZED;
394 sfc_ev_qfini(struct sfc_adapter *sa, unsigned int sw_index)
398 sfc_log_init(sa, "sw_index=%u", sw_index);
400 evq = sa->evq_info[sw_index].evq;
402 SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
404 sa->evq_info[sw_index].evq = NULL;
406 sfc_dma_free(sa, &evq->mem);
412 sfc_ev_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
414 struct sfc_evq_info *evq_info = &sa->evq_info[sw_index];
415 unsigned int max_entries;
417 sfc_log_init(sa, "sw_index=%u", sw_index);
419 max_entries = sfc_evq_max_entries(sa, sw_index);
420 SFC_ASSERT(rte_is_power_of_2(max_entries));
422 evq_info->max_entries = max_entries;
428 sfc_ev_qfini_info(struct sfc_adapter *sa, unsigned int sw_index)
430 sfc_log_init(sa, "sw_index=%u", sw_index);
432 /* Nothing to cleanup */
436 sfc_ev_init(struct sfc_adapter *sa)
439 unsigned int sw_index;
441 sfc_log_init(sa, "entry");
443 sa->evq_count = sfc_ev_qcount(sa);
444 sa->mgmt_evq_index = 0;
446 /* Allocate EVQ info array */
448 sa->evq_info = rte_calloc_socket("sfc-evqs", sa->evq_count,
449 sizeof(struct sfc_evq_info), 0,
451 if (sa->evq_info == NULL)
452 goto fail_evqs_alloc;
454 for (sw_index = 0; sw_index < sa->evq_count; ++sw_index) {
455 rc = sfc_ev_qinit_info(sa, sw_index);
457 goto fail_ev_qinit_info;
461 * Rx/Tx event queues are created/destroyed when corresponding
462 * Rx/Tx queue is created/destroyed.
468 while (sw_index-- > 0)
469 sfc_ev_qfini_info(sa, sw_index);
471 rte_free(sa->evq_info);
476 sfc_log_init(sa, "failed %d", rc);
481 sfc_ev_fini(struct sfc_adapter *sa)
485 sfc_log_init(sa, "entry");
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);
495 rte_free(sa->evq_info);