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>
33 #include <rte_branch_prediction.h>
38 #include "sfc_debug.h"
45 /* Initial delay when waiting for event queue init complete event */
46 #define SFC_EVQ_INIT_BACKOFF_START_US (1)
47 /* Maximum delay between event queue polling attempts */
48 #define SFC_EVQ_INIT_BACKOFF_MAX_US (10 * 1000)
49 /* Event queue init approx timeout */
50 #define SFC_EVQ_INIT_TIMEOUT_US (2 * US_PER_S)
52 /* Management event queue polling period in microseconds */
53 #define SFC_MGMT_EV_QPOLL_PERIOD_US (US_PER_S)
57 sfc_ev_initialized(void *arg)
59 struct sfc_evq *evq = arg;
61 /* Init done events may be duplicated on SFN7xxx (SFC bug 31631) */
62 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTING ||
63 evq->init_state == SFC_EVQ_STARTED);
65 evq->init_state = SFC_EVQ_STARTED;
71 sfc_ev_rx(void *arg, __rte_unused uint32_t label, uint32_t id,
72 uint32_t size, uint16_t flags)
74 struct sfc_evq *evq = arg;
77 unsigned int pending_id;
80 struct sfc_rx_sw_desc *rxd;
82 if (unlikely(evq->exception))
87 SFC_ASSERT(rxq != NULL);
88 SFC_ASSERT(rxq->evq == evq);
89 SFC_ASSERT(rxq->state & SFC_RXQ_STARTED);
91 stop = (id + 1) & rxq->ptr_mask;
92 pending_id = rxq->pending & rxq->ptr_mask;
93 delta = (stop >= pending_id) ? (stop - pending_id) :
94 (rxq->ptr_mask + 1 - pending_id + stop);
98 * Rx event with no new descriptors done and zero length
99 * is used to abort scattered packet when there is no room
102 if (unlikely(size != 0)) {
103 evq->exception = B_TRUE;
105 "EVQ %u RxQ %u invalid RX abort "
106 "(id=%#x size=%u flags=%#x); needs restart\n",
107 evq->evq_index, sfc_rxq_sw_index(rxq),
112 /* Add discard flag to the first fragment */
113 rxq->sw_desc[pending_id].flags |= EFX_DISCARD;
114 /* Remove continue flag from the last fragment */
115 rxq->sw_desc[id].flags &= ~EFX_PKT_CONT;
116 } else if (unlikely(delta > rxq->batch_max)) {
117 evq->exception = B_TRUE;
120 "EVQ %u RxQ %u completion out of order "
121 "(id=%#x delta=%u flags=%#x); needs restart\n",
122 evq->evq_index, sfc_rxq_sw_index(rxq), id, delta,
128 for (i = pending_id; i != stop; i = (i + 1) & rxq->ptr_mask) {
129 rxd = &rxq->sw_desc[i];
133 SFC_ASSERT(size < (1 << 16));
134 rxd->size = (uint16_t)size;
137 rxq->pending += delta;
144 sfc_ev_tx(void *arg, __rte_unused uint32_t label, uint32_t id)
146 struct sfc_evq *evq = arg;
153 SFC_ASSERT(txq != NULL);
154 SFC_ASSERT(txq->evq == evq);
156 if (unlikely((txq->state & SFC_TXQ_STARTED) == 0))
159 stop = (id + 1) & txq->ptr_mask;
160 id = txq->pending & txq->ptr_mask;
162 delta = (stop >= id) ? (stop - id) : (txq->ptr_mask + 1 - id + stop);
164 txq->pending += delta;
171 sfc_ev_exception(void *arg, __rte_unused uint32_t code,
172 __rte_unused uint32_t data)
174 struct sfc_evq *evq = arg;
176 if (code == EFX_EXCEPTION_UNKNOWN_SENSOREVT)
179 evq->exception = B_TRUE;
181 "hardware exception %s (code=%u, data=%#x) on EVQ %u;"
183 (code == EFX_EXCEPTION_RX_RECOVERY) ? "RX_RECOVERY" :
184 (code == EFX_EXCEPTION_RX_DSC_ERROR) ? "RX_DSC_ERROR" :
185 (code == EFX_EXCEPTION_TX_DSC_ERROR) ? "TX_DSC_ERROR" :
186 (code == EFX_EXCEPTION_FWALERT_SRAM) ? "FWALERT_SRAM" :
187 (code == EFX_EXCEPTION_UNKNOWN_FWALERT) ? "UNKNOWN_FWALERT" :
188 (code == EFX_EXCEPTION_RX_ERROR) ? "RX_ERROR" :
189 (code == EFX_EXCEPTION_TX_ERROR) ? "TX_ERROR" :
190 (code == EFX_EXCEPTION_EV_ERROR) ? "EV_ERROR" :
192 code, data, evq->evq_index);
198 sfc_ev_rxq_flush_done(void *arg, __rte_unused uint32_t rxq_hw_index)
200 struct sfc_evq *evq = arg;
204 SFC_ASSERT(rxq != NULL);
205 SFC_ASSERT(rxq->hw_index == rxq_hw_index);
206 SFC_ASSERT(rxq->evq == evq);
207 sfc_rx_qflush_done(rxq);
213 sfc_ev_rxq_flush_failed(void *arg, __rte_unused uint32_t rxq_hw_index)
215 struct sfc_evq *evq = arg;
219 SFC_ASSERT(rxq != NULL);
220 SFC_ASSERT(rxq->hw_index == rxq_hw_index);
221 SFC_ASSERT(rxq->evq == evq);
222 sfc_rx_qflush_failed(rxq);
228 sfc_ev_txq_flush_done(void *arg, __rte_unused uint32_t txq_hw_index)
230 struct sfc_evq *evq = arg;
234 SFC_ASSERT(txq != NULL);
235 SFC_ASSERT(txq->hw_index == txq_hw_index);
236 SFC_ASSERT(txq->evq == evq);
237 sfc_tx_qflush_done(txq);
243 sfc_ev_software(void *arg, uint16_t magic)
245 struct sfc_evq *evq = arg;
247 sfc_err(evq->sa, "EVQ %u unexpected software event magic=%#.4x",
248 evq->evq_index, magic);
253 sfc_ev_sram(void *arg, uint32_t code)
255 struct sfc_evq *evq = arg;
257 sfc_err(evq->sa, "EVQ %u unexpected SRAM event code=%u",
258 evq->evq_index, code);
263 sfc_ev_wake_up(void *arg, uint32_t index)
265 struct sfc_evq *evq = arg;
267 sfc_err(evq->sa, "EVQ %u unexpected wake up event index=%u",
268 evq->evq_index, index);
273 sfc_ev_timer(void *arg, uint32_t index)
275 struct sfc_evq *evq = arg;
277 sfc_err(evq->sa, "EVQ %u unexpected timer event index=%u",
278 evq->evq_index, index);
283 sfc_ev_link_change(void *arg, efx_link_mode_t link_mode)
285 struct sfc_evq *evq = arg;
286 struct sfc_adapter *sa = evq->sa;
287 struct rte_eth_link *dev_link = &sa->eth_dev->data->dev_link;
288 struct rte_eth_link new_link;
290 EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
292 sfc_port_link_mode_to_info(link_mode, &new_link);
293 rte_atomic64_set((rte_atomic64_t *)dev_link, *(uint64_t *)&new_link);
298 static const efx_ev_callbacks_t sfc_ev_callbacks = {
299 .eec_initialized = sfc_ev_initialized,
302 .eec_exception = sfc_ev_exception,
303 .eec_rxq_flush_done = sfc_ev_rxq_flush_done,
304 .eec_rxq_flush_failed = sfc_ev_rxq_flush_failed,
305 .eec_txq_flush_done = sfc_ev_txq_flush_done,
306 .eec_software = sfc_ev_software,
307 .eec_sram = sfc_ev_sram,
308 .eec_wake_up = sfc_ev_wake_up,
309 .eec_timer = sfc_ev_timer,
310 .eec_link_change = sfc_ev_link_change,
315 sfc_ev_qpoll(struct sfc_evq *evq)
317 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
318 evq->init_state == SFC_EVQ_STARTING);
320 /* Synchronize the DMA memory for reading not required */
322 efx_ev_qpoll(evq->common, &evq->read_ptr, &sfc_ev_callbacks, evq);
324 if (unlikely(evq->exception) && sfc_adapter_trylock(evq->sa)) {
325 struct sfc_adapter *sa = evq->sa;
328 if ((evq->rxq != NULL) && (evq->rxq->state & SFC_RXQ_RUNNING)) {
329 unsigned int rxq_sw_index = sfc_rxq_sw_index(evq->rxq);
332 "restart RxQ %u because of exception on its EvQ %u",
333 rxq_sw_index, evq->evq_index);
335 sfc_rx_qstop(sa, rxq_sw_index);
336 rc = sfc_rx_qstart(sa, rxq_sw_index);
338 sfc_err(sa, "cannot restart RxQ %u",
342 if (evq->txq != NULL) {
343 unsigned int txq_sw_index = sfc_txq_sw_index(evq->txq);
346 "restart TxQ %u because of exception on its EvQ %u",
347 txq_sw_index, evq->evq_index);
349 sfc_tx_qstop(sa, txq_sw_index);
350 rc = sfc_tx_qstart(sa, txq_sw_index);
352 sfc_err(sa, "cannot restart TxQ %u",
357 sfc_panic(sa, "unrecoverable exception on EvQ %u",
360 sfc_adapter_unlock(sa);
363 /* Poll-mode driver does not re-prime the event queue for interrupts */
367 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
369 if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
370 struct sfc_evq *mgmt_evq = sa->evq_info[sa->mgmt_evq_index].evq;
372 if (mgmt_evq->init_state == SFC_EVQ_STARTED)
373 sfc_ev_qpoll(mgmt_evq);
375 rte_spinlock_unlock(&sa->mgmt_evq_lock);
380 sfc_ev_qprime(struct sfc_evq *evq)
382 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
383 return efx_ev_qprime(evq->common, evq->read_ptr);
387 sfc_ev_qstart(struct sfc_adapter *sa, unsigned int sw_index)
389 const struct sfc_evq_info *evq_info;
392 unsigned int total_delay_us;
393 unsigned int delay_us;
396 sfc_log_init(sa, "sw_index=%u", sw_index);
398 evq_info = &sa->evq_info[sw_index];
402 /* Clear all events */
403 (void)memset((void *)esmp->esm_base, 0xff,
404 EFX_EVQ_SIZE(evq_info->entries));
406 /* Create the common code event queue */
407 rc = efx_ev_qcreate(sa->nic, sw_index, esmp, evq_info->entries,
408 0 /* unused on EF10 */, 0,
409 EFX_EVQ_FLAGS_TYPE_THROUGHPUT |
410 EFX_EVQ_FLAGS_NOTIFY_DISABLED,
413 goto fail_ev_qcreate;
415 evq->init_state = SFC_EVQ_STARTING;
417 /* Wait for the initialization event */
419 delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
421 (void)sfc_ev_qpoll(evq);
423 /* Check to see if the initialization complete indication
424 * posted by the hardware.
426 if (evq->init_state == SFC_EVQ_STARTED)
429 /* Give event queue some time to init */
430 rte_delay_us(delay_us);
432 total_delay_us += delay_us;
434 /* Exponential backoff */
436 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
437 delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
439 } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
448 evq->init_state = SFC_EVQ_INITIALIZED;
449 efx_ev_qdestroy(evq->common);
452 sfc_log_init(sa, "failed %d", rc);
457 sfc_ev_qstop(struct sfc_adapter *sa, unsigned int sw_index)
459 const struct sfc_evq_info *evq_info;
462 sfc_log_init(sa, "sw_index=%u", sw_index);
464 SFC_ASSERT(sw_index < sa->evq_count);
466 evq_info = &sa->evq_info[sw_index];
469 if (evq == NULL || evq->init_state != SFC_EVQ_STARTED)
472 evq->init_state = SFC_EVQ_INITIALIZED;
474 evq->exception = B_FALSE;
476 efx_ev_qdestroy(evq->common);
480 sfc_ev_mgmt_periodic_qpoll(void *arg)
482 struct sfc_adapter *sa = arg;
485 sfc_ev_mgmt_qpoll(sa);
487 rc = rte_eal_alarm_set(SFC_MGMT_EV_QPOLL_PERIOD_US,
488 sfc_ev_mgmt_periodic_qpoll, sa);
491 "cannot rearm management EVQ polling alarm (rc=%d)",
496 sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter *sa)
498 sfc_ev_mgmt_periodic_qpoll(sa);
502 sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter *sa)
504 rte_eal_alarm_cancel(sfc_ev_mgmt_periodic_qpoll, sa);
508 sfc_ev_start(struct sfc_adapter *sa)
512 sfc_log_init(sa, "entry");
514 rc = efx_ev_init(sa->nic);
518 /* Start management EVQ used for global events */
519 rte_spinlock_lock(&sa->mgmt_evq_lock);
521 rc = sfc_ev_qstart(sa, sa->mgmt_evq_index);
523 goto fail_mgmt_evq_start;
525 rte_spinlock_unlock(&sa->mgmt_evq_lock);
528 * Start management EVQ polling. If interrupts are disabled
529 * (not used), it is required to process link status change
530 * and other device level events to avoid unrecoverable
531 * error because the event queue overflow.
533 sfc_ev_mgmt_periodic_qpoll_start(sa);
536 * Rx/Tx event queues are started/stopped when corresponding
537 * Rx/Tx queue is started/stopped.
543 rte_spinlock_unlock(&sa->mgmt_evq_lock);
544 efx_ev_fini(sa->nic);
547 sfc_log_init(sa, "failed %d", rc);
552 sfc_ev_stop(struct sfc_adapter *sa)
554 unsigned int sw_index;
556 sfc_log_init(sa, "entry");
558 sfc_ev_mgmt_periodic_qpoll_stop(sa);
560 /* Make sure that all event queues are stopped */
561 sw_index = sa->evq_count;
562 while (sw_index-- > 0) {
563 if (sw_index == sa->mgmt_evq_index) {
564 /* Locks are required for the management EVQ */
565 rte_spinlock_lock(&sa->mgmt_evq_lock);
566 sfc_ev_qstop(sa, sa->mgmt_evq_index);
567 rte_spinlock_unlock(&sa->mgmt_evq_lock);
569 sfc_ev_qstop(sa, sw_index);
573 efx_ev_fini(sa->nic);
577 sfc_ev_qinit(struct sfc_adapter *sa, unsigned int sw_index,
578 unsigned int entries, int socket_id)
580 struct sfc_evq_info *evq_info;
584 sfc_log_init(sa, "sw_index=%u", sw_index);
586 evq_info = &sa->evq_info[sw_index];
588 SFC_ASSERT(rte_is_power_of_2(entries));
589 SFC_ASSERT(entries <= evq_info->max_entries);
590 evq_info->entries = entries;
592 evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
598 evq->evq_index = sw_index;
600 /* Allocate DMA space */
601 rc = sfc_dma_alloc(sa, "evq", sw_index, EFX_EVQ_SIZE(evq_info->entries),
602 socket_id, &evq->mem);
606 evq->init_state = SFC_EVQ_INITIALIZED;
614 sfc_ev_qfini(struct sfc_adapter *sa, unsigned int sw_index)
618 sfc_log_init(sa, "sw_index=%u", sw_index);
620 evq = sa->evq_info[sw_index].evq;
622 SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
624 sa->evq_info[sw_index].evq = NULL;
626 sfc_dma_free(sa, &evq->mem);
632 sfc_ev_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
634 struct sfc_evq_info *evq_info = &sa->evq_info[sw_index];
635 unsigned int max_entries;
637 sfc_log_init(sa, "sw_index=%u", sw_index);
639 max_entries = sfc_evq_max_entries(sa, sw_index);
640 SFC_ASSERT(rte_is_power_of_2(max_entries));
642 evq_info->max_entries = max_entries;
648 sfc_ev_qfini_info(struct sfc_adapter *sa, unsigned int sw_index)
650 sfc_log_init(sa, "sw_index=%u", sw_index);
652 /* Nothing to cleanup */
656 sfc_ev_init(struct sfc_adapter *sa)
659 unsigned int sw_index;
661 sfc_log_init(sa, "entry");
663 sa->evq_count = sfc_ev_qcount(sa);
664 sa->mgmt_evq_index = 0;
665 rte_spinlock_init(&sa->mgmt_evq_lock);
667 /* Allocate EVQ info array */
669 sa->evq_info = rte_calloc_socket("sfc-evqs", sa->evq_count,
670 sizeof(struct sfc_evq_info), 0,
672 if (sa->evq_info == NULL)
673 goto fail_evqs_alloc;
675 for (sw_index = 0; sw_index < sa->evq_count; ++sw_index) {
676 rc = sfc_ev_qinit_info(sa, sw_index);
678 goto fail_ev_qinit_info;
681 rc = sfc_ev_qinit(sa, sa->mgmt_evq_index, SFC_MGMT_EVQ_ENTRIES,
684 goto fail_mgmt_evq_init;
687 * Rx/Tx event queues are created/destroyed when corresponding
688 * Rx/Tx queue is created/destroyed.
695 while (sw_index-- > 0)
696 sfc_ev_qfini_info(sa, sw_index);
698 rte_free(sa->evq_info);
703 sfc_log_init(sa, "failed %d", rc);
708 sfc_ev_fini(struct sfc_adapter *sa)
712 sfc_log_init(sa, "entry");
714 /* Cleanup all event queues */
715 sw_index = sa->evq_count;
716 while (--sw_index >= 0) {
717 if (sa->evq_info[sw_index].evq != NULL)
718 sfc_ev_qfini(sa, sw_index);
719 sfc_ev_qfini_info(sa, sw_index);
722 rte_free(sa->evq_info);