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, uint32_t id,
70 uint32_t size, uint16_t flags)
72 struct sfc_evq *evq = arg;
75 unsigned int pending_id;
78 struct sfc_rx_sw_desc *rxd;
80 if (unlikely(evq->exception))
85 SFC_ASSERT(rxq != NULL);
86 SFC_ASSERT(rxq->evq == evq);
87 SFC_ASSERT(rxq->state & SFC_RXQ_STARTED);
89 stop = (id + 1) & rxq->ptr_mask;
90 pending_id = rxq->pending & rxq->ptr_mask;
91 delta = (stop >= pending_id) ? (stop - pending_id) :
92 (rxq->ptr_mask + 1 - pending_id + stop);
94 if (unlikely(delta > rxq->batch_max)) {
95 evq->exception = B_TRUE;
98 "EVQ %u RxQ %u completion out of order "
99 "(id=%#x delta=%u flags=%#x); needs restart\n",
100 evq->evq_index, sfc_rxq_sw_index(rxq), id, delta,
106 for (i = pending_id; i != stop; i = (i + 1) & rxq->ptr_mask) {
107 rxd = &rxq->sw_desc[i];
111 SFC_ASSERT(size < (1 << 16));
112 rxd->size = (uint16_t)size;
115 rxq->pending += delta;
122 sfc_ev_tx(void *arg, __rte_unused uint32_t label, __rte_unused uint32_t id)
124 struct sfc_evq *evq = arg;
126 sfc_err(evq->sa, "EVQ %u unexpected Tx event", evq->evq_index);
131 sfc_ev_exception(void *arg, __rte_unused uint32_t code,
132 __rte_unused uint32_t data)
134 struct sfc_evq *evq = arg;
136 if (code == EFX_EXCEPTION_UNKNOWN_SENSOREVT)
139 evq->exception = B_TRUE;
141 "hardware exception %s (code=%u, data=%#x) on EVQ %u;"
143 (code == EFX_EXCEPTION_RX_RECOVERY) ? "RX_RECOVERY" :
144 (code == EFX_EXCEPTION_RX_DSC_ERROR) ? "RX_DSC_ERROR" :
145 (code == EFX_EXCEPTION_TX_DSC_ERROR) ? "TX_DSC_ERROR" :
146 (code == EFX_EXCEPTION_FWALERT_SRAM) ? "FWALERT_SRAM" :
147 (code == EFX_EXCEPTION_UNKNOWN_FWALERT) ? "UNKNOWN_FWALERT" :
148 (code == EFX_EXCEPTION_RX_ERROR) ? "RX_ERROR" :
149 (code == EFX_EXCEPTION_TX_ERROR) ? "TX_ERROR" :
150 (code == EFX_EXCEPTION_EV_ERROR) ? "EV_ERROR" :
152 code, data, evq->evq_index);
158 sfc_ev_rxq_flush_done(void *arg, __rte_unused uint32_t rxq_hw_index)
160 struct sfc_evq *evq = arg;
164 SFC_ASSERT(rxq != NULL);
165 SFC_ASSERT(rxq->hw_index == rxq_hw_index);
166 SFC_ASSERT(rxq->evq == evq);
167 sfc_rx_qflush_done(rxq);
173 sfc_ev_rxq_flush_failed(void *arg, __rte_unused uint32_t rxq_hw_index)
175 struct sfc_evq *evq = arg;
179 SFC_ASSERT(rxq != NULL);
180 SFC_ASSERT(rxq->hw_index == rxq_hw_index);
181 SFC_ASSERT(rxq->evq == evq);
182 sfc_rx_qflush_failed(rxq);
188 sfc_ev_txq_flush_done(void *arg, __rte_unused uint32_t txq_hw_index)
190 struct sfc_evq *evq = arg;
192 sfc_err(evq->sa, "EVQ %u unexpected Tx flush done event",
198 sfc_ev_software(void *arg, uint16_t magic)
200 struct sfc_evq *evq = arg;
202 sfc_err(evq->sa, "EVQ %u unexpected software event magic=%#.4x",
203 evq->evq_index, magic);
208 sfc_ev_sram(void *arg, uint32_t code)
210 struct sfc_evq *evq = arg;
212 sfc_err(evq->sa, "EVQ %u unexpected SRAM event code=%u",
213 evq->evq_index, code);
218 sfc_ev_wake_up(void *arg, uint32_t index)
220 struct sfc_evq *evq = arg;
222 sfc_err(evq->sa, "EVQ %u unexpected wake up event index=%u",
223 evq->evq_index, index);
228 sfc_ev_timer(void *arg, uint32_t index)
230 struct sfc_evq *evq = arg;
232 sfc_err(evq->sa, "EVQ %u unexpected timer event index=%u",
233 evq->evq_index, index);
238 sfc_ev_link_change(void *arg, efx_link_mode_t link_mode)
240 struct sfc_evq *evq = arg;
241 struct sfc_adapter *sa = evq->sa;
242 struct rte_eth_link *dev_link = &sa->eth_dev->data->dev_link;
243 struct rte_eth_link new_link;
245 EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
247 sfc_port_link_mode_to_info(link_mode, &new_link);
248 rte_atomic64_set((rte_atomic64_t *)dev_link, *(uint64_t *)&new_link);
253 static const efx_ev_callbacks_t sfc_ev_callbacks = {
254 .eec_initialized = sfc_ev_initialized,
257 .eec_exception = sfc_ev_exception,
258 .eec_rxq_flush_done = sfc_ev_rxq_flush_done,
259 .eec_rxq_flush_failed = sfc_ev_rxq_flush_failed,
260 .eec_txq_flush_done = sfc_ev_txq_flush_done,
261 .eec_software = sfc_ev_software,
262 .eec_sram = sfc_ev_sram,
263 .eec_wake_up = sfc_ev_wake_up,
264 .eec_timer = sfc_ev_timer,
265 .eec_link_change = sfc_ev_link_change,
270 sfc_ev_qpoll(struct sfc_evq *evq)
272 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
273 evq->init_state == SFC_EVQ_STARTING);
275 /* Synchronize the DMA memory for reading not required */
277 efx_ev_qpoll(evq->common, &evq->read_ptr, &sfc_ev_callbacks, evq);
279 /* Poll-mode driver does not re-prime the event queue for interrupts */
283 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
285 if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
286 struct sfc_evq *mgmt_evq = sa->evq_info[sa->mgmt_evq_index].evq;
288 if (mgmt_evq->init_state == SFC_EVQ_STARTED)
289 sfc_ev_qpoll(mgmt_evq);
291 rte_spinlock_unlock(&sa->mgmt_evq_lock);
296 sfc_ev_qprime(struct sfc_evq *evq)
298 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
299 return efx_ev_qprime(evq->common, evq->read_ptr);
303 sfc_ev_qstart(struct sfc_adapter *sa, unsigned int sw_index)
305 const struct sfc_evq_info *evq_info;
308 unsigned int total_delay_us;
309 unsigned int delay_us;
312 sfc_log_init(sa, "sw_index=%u", sw_index);
314 evq_info = &sa->evq_info[sw_index];
318 /* Clear all events */
319 (void)memset((void *)esmp->esm_base, 0xff,
320 EFX_EVQ_SIZE(evq_info->entries));
322 /* Create the common code event queue */
323 rc = efx_ev_qcreate(sa->nic, sw_index, esmp, evq_info->entries,
324 0 /* unused on EF10 */, 0,
325 EFX_EVQ_FLAGS_TYPE_THROUGHPUT |
326 EFX_EVQ_FLAGS_NOTIFY_DISABLED,
329 goto fail_ev_qcreate;
331 evq->init_state = SFC_EVQ_STARTING;
333 /* Wait for the initialization event */
335 delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
337 (void)sfc_ev_qpoll(evq);
339 /* Check to see if the initialization complete indication
340 * posted by the hardware.
342 if (evq->init_state == SFC_EVQ_STARTED)
345 /* Give event queue some time to init */
346 rte_delay_us(delay_us);
348 total_delay_us += delay_us;
350 /* Exponential backoff */
352 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
353 delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
355 } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
364 evq->init_state = SFC_EVQ_INITIALIZED;
365 efx_ev_qdestroy(evq->common);
368 sfc_log_init(sa, "failed %d", rc);
373 sfc_ev_qstop(struct sfc_adapter *sa, unsigned int sw_index)
375 const struct sfc_evq_info *evq_info;
378 sfc_log_init(sa, "sw_index=%u", sw_index);
380 SFC_ASSERT(sw_index < sa->evq_count);
382 evq_info = &sa->evq_info[sw_index];
385 if (evq == NULL || evq->init_state != SFC_EVQ_STARTED)
388 evq->init_state = SFC_EVQ_INITIALIZED;
390 evq->exception = B_FALSE;
392 efx_ev_qdestroy(evq->common);
396 sfc_ev_mgmt_periodic_qpoll(void *arg)
398 struct sfc_adapter *sa = arg;
401 sfc_ev_mgmt_qpoll(sa);
403 rc = rte_eal_alarm_set(SFC_MGMT_EV_QPOLL_PERIOD_US,
404 sfc_ev_mgmt_periodic_qpoll, sa);
407 "cannot rearm management EVQ polling alarm (rc=%d)",
412 sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter *sa)
414 sfc_ev_mgmt_periodic_qpoll(sa);
418 sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter *sa)
420 rte_eal_alarm_cancel(sfc_ev_mgmt_periodic_qpoll, sa);
424 sfc_ev_start(struct sfc_adapter *sa)
428 sfc_log_init(sa, "entry");
430 rc = efx_ev_init(sa->nic);
434 /* Start management EVQ used for global events */
435 rte_spinlock_lock(&sa->mgmt_evq_lock);
437 rc = sfc_ev_qstart(sa, sa->mgmt_evq_index);
439 goto fail_mgmt_evq_start;
441 rte_spinlock_unlock(&sa->mgmt_evq_lock);
444 * Start management EVQ polling. If interrupts are disabled
445 * (not used), it is required to process link status change
446 * and other device level events to avoid unrecoverable
447 * error because the event queue overflow.
449 sfc_ev_mgmt_periodic_qpoll_start(sa);
452 * Rx/Tx event queues are started/stopped when corresponding
453 * Rx/Tx queue is started/stopped.
459 rte_spinlock_unlock(&sa->mgmt_evq_lock);
460 efx_ev_fini(sa->nic);
463 sfc_log_init(sa, "failed %d", rc);
468 sfc_ev_stop(struct sfc_adapter *sa)
470 unsigned int sw_index;
472 sfc_log_init(sa, "entry");
474 sfc_ev_mgmt_periodic_qpoll_stop(sa);
476 /* Make sure that all event queues are stopped */
477 sw_index = sa->evq_count;
478 while (sw_index-- > 0) {
479 if (sw_index == sa->mgmt_evq_index) {
480 /* Locks are required for the management EVQ */
481 rte_spinlock_lock(&sa->mgmt_evq_lock);
482 sfc_ev_qstop(sa, sa->mgmt_evq_index);
483 rte_spinlock_unlock(&sa->mgmt_evq_lock);
485 sfc_ev_qstop(sa, sw_index);
489 efx_ev_fini(sa->nic);
493 sfc_ev_qinit(struct sfc_adapter *sa, unsigned int sw_index,
494 unsigned int entries, int socket_id)
496 struct sfc_evq_info *evq_info;
500 sfc_log_init(sa, "sw_index=%u", sw_index);
502 evq_info = &sa->evq_info[sw_index];
504 SFC_ASSERT(rte_is_power_of_2(entries));
505 SFC_ASSERT(entries <= evq_info->max_entries);
506 evq_info->entries = entries;
508 evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
514 evq->evq_index = sw_index;
516 /* Allocate DMA space */
517 rc = sfc_dma_alloc(sa, "evq", sw_index, EFX_EVQ_SIZE(evq_info->entries),
518 socket_id, &evq->mem);
522 evq->init_state = SFC_EVQ_INITIALIZED;
530 sfc_ev_qfini(struct sfc_adapter *sa, unsigned int sw_index)
534 sfc_log_init(sa, "sw_index=%u", sw_index);
536 evq = sa->evq_info[sw_index].evq;
538 SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
540 sa->evq_info[sw_index].evq = NULL;
542 sfc_dma_free(sa, &evq->mem);
548 sfc_ev_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
550 struct sfc_evq_info *evq_info = &sa->evq_info[sw_index];
551 unsigned int max_entries;
553 sfc_log_init(sa, "sw_index=%u", sw_index);
555 max_entries = sfc_evq_max_entries(sa, sw_index);
556 SFC_ASSERT(rte_is_power_of_2(max_entries));
558 evq_info->max_entries = max_entries;
564 sfc_ev_qfini_info(struct sfc_adapter *sa, unsigned int sw_index)
566 sfc_log_init(sa, "sw_index=%u", sw_index);
568 /* Nothing to cleanup */
572 sfc_ev_init(struct sfc_adapter *sa)
575 unsigned int sw_index;
577 sfc_log_init(sa, "entry");
579 sa->evq_count = sfc_ev_qcount(sa);
580 sa->mgmt_evq_index = 0;
581 rte_spinlock_init(&sa->mgmt_evq_lock);
583 /* Allocate EVQ info array */
585 sa->evq_info = rte_calloc_socket("sfc-evqs", sa->evq_count,
586 sizeof(struct sfc_evq_info), 0,
588 if (sa->evq_info == NULL)
589 goto fail_evqs_alloc;
591 for (sw_index = 0; sw_index < sa->evq_count; ++sw_index) {
592 rc = sfc_ev_qinit_info(sa, sw_index);
594 goto fail_ev_qinit_info;
597 rc = sfc_ev_qinit(sa, sa->mgmt_evq_index, SFC_MGMT_EVQ_ENTRIES,
600 goto fail_mgmt_evq_init;
603 * Rx/Tx event queues are created/destroyed when corresponding
604 * Rx/Tx queue is created/destroyed.
611 while (sw_index-- > 0)
612 sfc_ev_qfini_info(sa, sw_index);
614 rte_free(sa->evq_info);
619 sfc_log_init(sa, "failed %d", rc);
624 sfc_ev_fini(struct sfc_adapter *sa)
628 sfc_log_init(sa, "entry");
630 /* Cleanup all event queues */
631 sw_index = sa->evq_count;
632 while (--sw_index >= 0) {
633 if (sa->evq_info[sw_index].evq != NULL)
634 sfc_ev_qfini(sa, sw_index);
635 sfc_ev_qfini_info(sa, sw_index);
638 rte_free(sa->evq_info);