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"
43 /* Initial delay when waiting for event queue init complete event */
44 #define SFC_EVQ_INIT_BACKOFF_START_US (1)
45 /* Maximum delay between event queue polling attempts */
46 #define SFC_EVQ_INIT_BACKOFF_MAX_US (10 * 1000)
47 /* Event queue init approx timeout */
48 #define SFC_EVQ_INIT_TIMEOUT_US (2 * US_PER_S)
50 /* Management event queue polling period in microseconds */
51 #define SFC_MGMT_EV_QPOLL_PERIOD_US (US_PER_S)
55 sfc_ev_initialized(void *arg)
57 struct sfc_evq *evq = arg;
59 /* Init done events may be duplicated on SFN7xxx (SFC bug 31631) */
60 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTING ||
61 evq->init_state == SFC_EVQ_STARTED);
63 evq->init_state = SFC_EVQ_STARTED;
69 sfc_ev_rx(void *arg, __rte_unused uint32_t label, __rte_unused uint32_t id,
70 __rte_unused uint32_t size, __rte_unused uint16_t flags)
72 struct sfc_evq *evq = arg;
74 sfc_err(evq->sa, "EVQ %u unexpected Rx event", evq->evq_index);
79 sfc_ev_tx(void *arg, __rte_unused uint32_t label, __rte_unused uint32_t id)
81 struct sfc_evq *evq = arg;
83 sfc_err(evq->sa, "EVQ %u unexpected Tx event", evq->evq_index);
88 sfc_ev_exception(void *arg, __rte_unused uint32_t code,
89 __rte_unused uint32_t data)
91 struct sfc_evq *evq = arg;
93 if (code == EFX_EXCEPTION_UNKNOWN_SENSOREVT)
96 evq->exception = B_TRUE;
98 "hardware exception %s (code=%u, data=%#x) on EVQ %u;"
100 (code == EFX_EXCEPTION_RX_RECOVERY) ? "RX_RECOVERY" :
101 (code == EFX_EXCEPTION_RX_DSC_ERROR) ? "RX_DSC_ERROR" :
102 (code == EFX_EXCEPTION_TX_DSC_ERROR) ? "TX_DSC_ERROR" :
103 (code == EFX_EXCEPTION_FWALERT_SRAM) ? "FWALERT_SRAM" :
104 (code == EFX_EXCEPTION_UNKNOWN_FWALERT) ? "UNKNOWN_FWALERT" :
105 (code == EFX_EXCEPTION_RX_ERROR) ? "RX_ERROR" :
106 (code == EFX_EXCEPTION_TX_ERROR) ? "TX_ERROR" :
107 (code == EFX_EXCEPTION_EV_ERROR) ? "EV_ERROR" :
109 code, data, evq->evq_index);
115 sfc_ev_rxq_flush_done(void *arg, __rte_unused uint32_t rxq_hw_index)
117 struct sfc_evq *evq = arg;
121 SFC_ASSERT(rxq != NULL);
122 SFC_ASSERT(rxq->hw_index == rxq_hw_index);
123 SFC_ASSERT(rxq->evq == evq);
124 sfc_rx_qflush_done(rxq);
130 sfc_ev_rxq_flush_failed(void *arg, __rte_unused uint32_t rxq_hw_index)
132 struct sfc_evq *evq = arg;
136 SFC_ASSERT(rxq != NULL);
137 SFC_ASSERT(rxq->hw_index == rxq_hw_index);
138 SFC_ASSERT(rxq->evq == evq);
139 sfc_rx_qflush_failed(rxq);
145 sfc_ev_txq_flush_done(void *arg, __rte_unused uint32_t txq_hw_index)
147 struct sfc_evq *evq = arg;
149 sfc_err(evq->sa, "EVQ %u unexpected Tx flush done event",
155 sfc_ev_software(void *arg, uint16_t magic)
157 struct sfc_evq *evq = arg;
159 sfc_err(evq->sa, "EVQ %u unexpected software event magic=%#.4x",
160 evq->evq_index, magic);
165 sfc_ev_sram(void *arg, uint32_t code)
167 struct sfc_evq *evq = arg;
169 sfc_err(evq->sa, "EVQ %u unexpected SRAM event code=%u",
170 evq->evq_index, code);
175 sfc_ev_wake_up(void *arg, uint32_t index)
177 struct sfc_evq *evq = arg;
179 sfc_err(evq->sa, "EVQ %u unexpected wake up event index=%u",
180 evq->evq_index, index);
185 sfc_ev_timer(void *arg, uint32_t index)
187 struct sfc_evq *evq = arg;
189 sfc_err(evq->sa, "EVQ %u unexpected timer event index=%u",
190 evq->evq_index, index);
195 sfc_ev_link_change(void *arg, efx_link_mode_t link_mode)
197 struct sfc_evq *evq = arg;
198 struct sfc_adapter *sa = evq->sa;
199 struct rte_eth_link *dev_link = &sa->eth_dev->data->dev_link;
200 struct rte_eth_link new_link;
202 EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
204 sfc_port_link_mode_to_info(link_mode, &new_link);
205 rte_atomic64_set((rte_atomic64_t *)dev_link, *(uint64_t *)&new_link);
210 static const efx_ev_callbacks_t sfc_ev_callbacks = {
211 .eec_initialized = sfc_ev_initialized,
214 .eec_exception = sfc_ev_exception,
215 .eec_rxq_flush_done = sfc_ev_rxq_flush_done,
216 .eec_rxq_flush_failed = sfc_ev_rxq_flush_failed,
217 .eec_txq_flush_done = sfc_ev_txq_flush_done,
218 .eec_software = sfc_ev_software,
219 .eec_sram = sfc_ev_sram,
220 .eec_wake_up = sfc_ev_wake_up,
221 .eec_timer = sfc_ev_timer,
222 .eec_link_change = sfc_ev_link_change,
227 sfc_ev_qpoll(struct sfc_evq *evq)
229 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
230 evq->init_state == SFC_EVQ_STARTING);
232 /* Synchronize the DMA memory for reading not required */
234 efx_ev_qpoll(evq->common, &evq->read_ptr, &sfc_ev_callbacks, evq);
236 /* Poll-mode driver does not re-prime the event queue for interrupts */
240 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
242 if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
243 struct sfc_evq *mgmt_evq = sa->evq_info[sa->mgmt_evq_index].evq;
245 if (mgmt_evq->init_state == SFC_EVQ_STARTED)
246 sfc_ev_qpoll(mgmt_evq);
248 rte_spinlock_unlock(&sa->mgmt_evq_lock);
253 sfc_ev_qprime(struct sfc_evq *evq)
255 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
256 return efx_ev_qprime(evq->common, evq->read_ptr);
260 sfc_ev_qstart(struct sfc_adapter *sa, unsigned int sw_index)
262 const struct sfc_evq_info *evq_info;
265 unsigned int total_delay_us;
266 unsigned int delay_us;
269 sfc_log_init(sa, "sw_index=%u", sw_index);
271 evq_info = &sa->evq_info[sw_index];
275 /* Clear all events */
276 (void)memset((void *)esmp->esm_base, 0xff,
277 EFX_EVQ_SIZE(evq_info->entries));
279 /* Create the common code event queue */
280 rc = efx_ev_qcreate(sa->nic, sw_index, esmp, evq_info->entries,
281 0 /* unused on EF10 */, 0,
282 EFX_EVQ_FLAGS_TYPE_THROUGHPUT |
283 EFX_EVQ_FLAGS_NOTIFY_DISABLED,
286 goto fail_ev_qcreate;
288 evq->init_state = SFC_EVQ_STARTING;
290 /* Wait for the initialization event */
292 delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
294 (void)sfc_ev_qpoll(evq);
296 /* Check to see if the initialization complete indication
297 * posted by the hardware.
299 if (evq->init_state == SFC_EVQ_STARTED)
302 /* Give event queue some time to init */
303 rte_delay_us(delay_us);
305 total_delay_us += delay_us;
307 /* Exponential backoff */
309 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
310 delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
312 } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
321 evq->init_state = SFC_EVQ_INITIALIZED;
322 efx_ev_qdestroy(evq->common);
325 sfc_log_init(sa, "failed %d", rc);
330 sfc_ev_qstop(struct sfc_adapter *sa, unsigned int sw_index)
332 const struct sfc_evq_info *evq_info;
335 sfc_log_init(sa, "sw_index=%u", sw_index);
337 SFC_ASSERT(sw_index < sa->evq_count);
339 evq_info = &sa->evq_info[sw_index];
342 if (evq == NULL || evq->init_state != SFC_EVQ_STARTED)
345 evq->init_state = SFC_EVQ_INITIALIZED;
347 evq->exception = B_FALSE;
349 efx_ev_qdestroy(evq->common);
353 sfc_ev_mgmt_periodic_qpoll(void *arg)
355 struct sfc_adapter *sa = arg;
358 sfc_ev_mgmt_qpoll(sa);
360 rc = rte_eal_alarm_set(SFC_MGMT_EV_QPOLL_PERIOD_US,
361 sfc_ev_mgmt_periodic_qpoll, sa);
364 "cannot rearm management EVQ polling alarm (rc=%d)",
369 sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter *sa)
371 sfc_ev_mgmt_periodic_qpoll(sa);
375 sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter *sa)
377 rte_eal_alarm_cancel(sfc_ev_mgmt_periodic_qpoll, sa);
381 sfc_ev_start(struct sfc_adapter *sa)
385 sfc_log_init(sa, "entry");
387 rc = efx_ev_init(sa->nic);
391 /* Start management EVQ used for global events */
392 rte_spinlock_lock(&sa->mgmt_evq_lock);
394 rc = sfc_ev_qstart(sa, sa->mgmt_evq_index);
396 goto fail_mgmt_evq_start;
398 rte_spinlock_unlock(&sa->mgmt_evq_lock);
401 * Start management EVQ polling. If interrupts are disabled
402 * (not used), it is required to process link status change
403 * and other device level events to avoid unrecoverable
404 * error because the event queue overflow.
406 sfc_ev_mgmt_periodic_qpoll_start(sa);
409 * Rx/Tx event queues are started/stopped when corresponding
410 * Rx/Tx queue is started/stopped.
416 rte_spinlock_unlock(&sa->mgmt_evq_lock);
417 efx_ev_fini(sa->nic);
420 sfc_log_init(sa, "failed %d", rc);
425 sfc_ev_stop(struct sfc_adapter *sa)
427 unsigned int sw_index;
429 sfc_log_init(sa, "entry");
431 sfc_ev_mgmt_periodic_qpoll_stop(sa);
433 /* Make sure that all event queues are stopped */
434 sw_index = sa->evq_count;
435 while (sw_index-- > 0) {
436 if (sw_index == sa->mgmt_evq_index) {
437 /* Locks are required for the management EVQ */
438 rte_spinlock_lock(&sa->mgmt_evq_lock);
439 sfc_ev_qstop(sa, sa->mgmt_evq_index);
440 rte_spinlock_unlock(&sa->mgmt_evq_lock);
442 sfc_ev_qstop(sa, sw_index);
446 efx_ev_fini(sa->nic);
450 sfc_ev_qinit(struct sfc_adapter *sa, unsigned int sw_index,
451 unsigned int entries, int socket_id)
453 struct sfc_evq_info *evq_info;
457 sfc_log_init(sa, "sw_index=%u", sw_index);
459 evq_info = &sa->evq_info[sw_index];
461 SFC_ASSERT(rte_is_power_of_2(entries));
462 SFC_ASSERT(entries <= evq_info->max_entries);
463 evq_info->entries = entries;
465 evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
471 evq->evq_index = sw_index;
473 /* Allocate DMA space */
474 rc = sfc_dma_alloc(sa, "evq", sw_index, EFX_EVQ_SIZE(evq_info->entries),
475 socket_id, &evq->mem);
479 evq->init_state = SFC_EVQ_INITIALIZED;
487 sfc_ev_qfini(struct sfc_adapter *sa, unsigned int sw_index)
491 sfc_log_init(sa, "sw_index=%u", sw_index);
493 evq = sa->evq_info[sw_index].evq;
495 SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
497 sa->evq_info[sw_index].evq = NULL;
499 sfc_dma_free(sa, &evq->mem);
505 sfc_ev_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
507 struct sfc_evq_info *evq_info = &sa->evq_info[sw_index];
508 unsigned int max_entries;
510 sfc_log_init(sa, "sw_index=%u", sw_index);
512 max_entries = sfc_evq_max_entries(sa, sw_index);
513 SFC_ASSERT(rte_is_power_of_2(max_entries));
515 evq_info->max_entries = max_entries;
521 sfc_ev_qfini_info(struct sfc_adapter *sa, unsigned int sw_index)
523 sfc_log_init(sa, "sw_index=%u", sw_index);
525 /* Nothing to cleanup */
529 sfc_ev_init(struct sfc_adapter *sa)
532 unsigned int sw_index;
534 sfc_log_init(sa, "entry");
536 sa->evq_count = sfc_ev_qcount(sa);
537 sa->mgmt_evq_index = 0;
538 rte_spinlock_init(&sa->mgmt_evq_lock);
540 /* Allocate EVQ info array */
542 sa->evq_info = rte_calloc_socket("sfc-evqs", sa->evq_count,
543 sizeof(struct sfc_evq_info), 0,
545 if (sa->evq_info == NULL)
546 goto fail_evqs_alloc;
548 for (sw_index = 0; sw_index < sa->evq_count; ++sw_index) {
549 rc = sfc_ev_qinit_info(sa, sw_index);
551 goto fail_ev_qinit_info;
554 rc = sfc_ev_qinit(sa, sa->mgmt_evq_index, SFC_MGMT_EVQ_ENTRIES,
557 goto fail_mgmt_evq_init;
560 * Rx/Tx event queues are created/destroyed when corresponding
561 * Rx/Tx queue is created/destroyed.
568 while (sw_index-- > 0)
569 sfc_ev_qfini_info(sa, sw_index);
571 rte_free(sa->evq_info);
576 sfc_log_init(sa, "failed %d", rc);
581 sfc_ev_fini(struct sfc_adapter *sa)
585 sfc_log_init(sa, "entry");
587 /* Cleanup all event queues */
588 sw_index = sa->evq_count;
589 while (--sw_index >= 0) {
590 if (sa->evq_info[sw_index].evq != NULL)
591 sfc_ev_qfini(sa, sw_index);
592 sfc_ev_qfini_info(sa, sw_index);
595 rte_free(sa->evq_info);