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"
42 /* Initial delay when waiting for event queue init complete event */
43 #define SFC_EVQ_INIT_BACKOFF_START_US (1)
44 /* Maximum delay between event queue polling attempts */
45 #define SFC_EVQ_INIT_BACKOFF_MAX_US (10 * 1000)
46 /* Event queue init approx timeout */
47 #define SFC_EVQ_INIT_TIMEOUT_US (2 * US_PER_S)
49 /* Management event queue polling period in microseconds */
50 #define SFC_MGMT_EV_QPOLL_PERIOD_US (US_PER_S)
54 sfc_ev_initialized(void *arg)
56 struct sfc_evq *evq = arg;
58 /* Init done events may be duplicated on SFN7xxx (SFC bug 31631) */
59 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTING ||
60 evq->init_state == SFC_EVQ_STARTED);
62 evq->init_state = SFC_EVQ_STARTED;
68 sfc_ev_rx(void *arg, __rte_unused uint32_t label, __rte_unused uint32_t id,
69 __rte_unused uint32_t size, __rte_unused uint16_t flags)
71 struct sfc_evq *evq = arg;
73 sfc_err(evq->sa, "EVQ %u unexpected Rx event", evq->evq_index);
78 sfc_ev_tx(void *arg, __rte_unused uint32_t label, __rte_unused uint32_t id)
80 struct sfc_evq *evq = arg;
82 sfc_err(evq->sa, "EVQ %u unexpected Tx event", evq->evq_index);
87 sfc_ev_exception(void *arg, __rte_unused uint32_t code,
88 __rte_unused uint32_t data)
90 struct sfc_evq *evq = arg;
92 if (code == EFX_EXCEPTION_UNKNOWN_SENSOREVT)
95 evq->exception = B_TRUE;
97 "hardware exception %s (code=%u, data=%#x) on EVQ %u;"
99 (code == EFX_EXCEPTION_RX_RECOVERY) ? "RX_RECOVERY" :
100 (code == EFX_EXCEPTION_RX_DSC_ERROR) ? "RX_DSC_ERROR" :
101 (code == EFX_EXCEPTION_TX_DSC_ERROR) ? "TX_DSC_ERROR" :
102 (code == EFX_EXCEPTION_FWALERT_SRAM) ? "FWALERT_SRAM" :
103 (code == EFX_EXCEPTION_UNKNOWN_FWALERT) ? "UNKNOWN_FWALERT" :
104 (code == EFX_EXCEPTION_RX_ERROR) ? "RX_ERROR" :
105 (code == EFX_EXCEPTION_TX_ERROR) ? "TX_ERROR" :
106 (code == EFX_EXCEPTION_EV_ERROR) ? "EV_ERROR" :
108 code, data, evq->evq_index);
114 sfc_ev_rxq_flush_done(void *arg, __rte_unused uint32_t rxq_hw_index)
116 struct sfc_evq *evq = arg;
118 sfc_err(evq->sa, "EVQ %u unexpected Rx flush done event",
124 sfc_ev_rxq_flush_failed(void *arg, __rte_unused uint32_t rxq_hw_index)
126 struct sfc_evq *evq = arg;
128 sfc_err(evq->sa, "EVQ %u unexpected Rx flush failed event",
134 sfc_ev_txq_flush_done(void *arg, __rte_unused uint32_t txq_hw_index)
136 struct sfc_evq *evq = arg;
138 sfc_err(evq->sa, "EVQ %u unexpected Tx flush done event",
144 sfc_ev_software(void *arg, uint16_t magic)
146 struct sfc_evq *evq = arg;
148 sfc_err(evq->sa, "EVQ %u unexpected software event magic=%#.4x",
149 evq->evq_index, magic);
154 sfc_ev_sram(void *arg, uint32_t code)
156 struct sfc_evq *evq = arg;
158 sfc_err(evq->sa, "EVQ %u unexpected SRAM event code=%u",
159 evq->evq_index, code);
164 sfc_ev_wake_up(void *arg, uint32_t index)
166 struct sfc_evq *evq = arg;
168 sfc_err(evq->sa, "EVQ %u unexpected wake up event index=%u",
169 evq->evq_index, index);
174 sfc_ev_timer(void *arg, uint32_t index)
176 struct sfc_evq *evq = arg;
178 sfc_err(evq->sa, "EVQ %u unexpected timer event index=%u",
179 evq->evq_index, index);
184 sfc_ev_link_change(void *arg, efx_link_mode_t link_mode)
186 struct sfc_evq *evq = arg;
187 struct sfc_adapter *sa = evq->sa;
188 struct rte_eth_link *dev_link = &sa->eth_dev->data->dev_link;
189 struct rte_eth_link new_link;
191 EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
193 sfc_port_link_mode_to_info(link_mode, &new_link);
194 rte_atomic64_set((rte_atomic64_t *)dev_link, *(uint64_t *)&new_link);
199 static const efx_ev_callbacks_t sfc_ev_callbacks = {
200 .eec_initialized = sfc_ev_initialized,
203 .eec_exception = sfc_ev_exception,
204 .eec_rxq_flush_done = sfc_ev_rxq_flush_done,
205 .eec_rxq_flush_failed = sfc_ev_rxq_flush_failed,
206 .eec_txq_flush_done = sfc_ev_txq_flush_done,
207 .eec_software = sfc_ev_software,
208 .eec_sram = sfc_ev_sram,
209 .eec_wake_up = sfc_ev_wake_up,
210 .eec_timer = sfc_ev_timer,
211 .eec_link_change = sfc_ev_link_change,
216 sfc_ev_qpoll(struct sfc_evq *evq)
218 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED ||
219 evq->init_state == SFC_EVQ_STARTING);
221 /* Synchronize the DMA memory for reading not required */
223 efx_ev_qpoll(evq->common, &evq->read_ptr, &sfc_ev_callbacks, evq);
225 /* Poll-mode driver does not re-prime the event queue for interrupts */
229 sfc_ev_mgmt_qpoll(struct sfc_adapter *sa)
231 if (rte_spinlock_trylock(&sa->mgmt_evq_lock)) {
232 struct sfc_evq *mgmt_evq = sa->evq_info[sa->mgmt_evq_index].evq;
234 if (mgmt_evq->init_state == SFC_EVQ_STARTED)
235 sfc_ev_qpoll(mgmt_evq);
237 rte_spinlock_unlock(&sa->mgmt_evq_lock);
242 sfc_ev_qprime(struct sfc_evq *evq)
244 SFC_ASSERT(evq->init_state == SFC_EVQ_STARTED);
245 return efx_ev_qprime(evq->common, evq->read_ptr);
249 sfc_ev_qstart(struct sfc_adapter *sa, unsigned int sw_index)
251 const struct sfc_evq_info *evq_info;
254 unsigned int total_delay_us;
255 unsigned int delay_us;
258 sfc_log_init(sa, "sw_index=%u", sw_index);
260 evq_info = &sa->evq_info[sw_index];
264 /* Clear all events */
265 (void)memset((void *)esmp->esm_base, 0xff,
266 EFX_EVQ_SIZE(evq_info->entries));
268 /* Create the common code event queue */
269 rc = efx_ev_qcreate(sa->nic, sw_index, esmp, evq_info->entries,
270 0 /* unused on EF10 */, 0,
271 EFX_EVQ_FLAGS_TYPE_THROUGHPUT |
272 EFX_EVQ_FLAGS_NOTIFY_DISABLED,
275 goto fail_ev_qcreate;
277 evq->init_state = SFC_EVQ_STARTING;
279 /* Wait for the initialization event */
281 delay_us = SFC_EVQ_INIT_BACKOFF_START_US;
283 (void)sfc_ev_qpoll(evq);
285 /* Check to see if the initialization complete indication
286 * posted by the hardware.
288 if (evq->init_state == SFC_EVQ_STARTED)
291 /* Give event queue some time to init */
292 rte_delay_us(delay_us);
294 total_delay_us += delay_us;
296 /* Exponential backoff */
298 if (delay_us > SFC_EVQ_INIT_BACKOFF_MAX_US)
299 delay_us = SFC_EVQ_INIT_BACKOFF_MAX_US;
301 } while (total_delay_us < SFC_EVQ_INIT_TIMEOUT_US);
310 evq->init_state = SFC_EVQ_INITIALIZED;
311 efx_ev_qdestroy(evq->common);
314 sfc_log_init(sa, "failed %d", rc);
319 sfc_ev_qstop(struct sfc_adapter *sa, unsigned int sw_index)
321 const struct sfc_evq_info *evq_info;
324 sfc_log_init(sa, "sw_index=%u", sw_index);
326 SFC_ASSERT(sw_index < sa->evq_count);
328 evq_info = &sa->evq_info[sw_index];
331 if (evq == NULL || evq->init_state != SFC_EVQ_STARTED)
334 evq->init_state = SFC_EVQ_INITIALIZED;
336 evq->exception = B_FALSE;
338 efx_ev_qdestroy(evq->common);
342 sfc_ev_mgmt_periodic_qpoll(void *arg)
344 struct sfc_adapter *sa = arg;
347 sfc_ev_mgmt_qpoll(sa);
349 rc = rte_eal_alarm_set(SFC_MGMT_EV_QPOLL_PERIOD_US,
350 sfc_ev_mgmt_periodic_qpoll, sa);
353 "cannot rearm management EVQ polling alarm (rc=%d)",
358 sfc_ev_mgmt_periodic_qpoll_start(struct sfc_adapter *sa)
360 sfc_ev_mgmt_periodic_qpoll(sa);
364 sfc_ev_mgmt_periodic_qpoll_stop(struct sfc_adapter *sa)
366 rte_eal_alarm_cancel(sfc_ev_mgmt_periodic_qpoll, sa);
370 sfc_ev_start(struct sfc_adapter *sa)
374 sfc_log_init(sa, "entry");
376 rc = efx_ev_init(sa->nic);
380 /* Start management EVQ used for global events */
381 rte_spinlock_lock(&sa->mgmt_evq_lock);
383 rc = sfc_ev_qstart(sa, sa->mgmt_evq_index);
385 goto fail_mgmt_evq_start;
387 rte_spinlock_unlock(&sa->mgmt_evq_lock);
390 * Start management EVQ polling. If interrupts are disabled
391 * (not used), it is required to process link status change
392 * and other device level events to avoid unrecoverable
393 * error because the event queue overflow.
395 sfc_ev_mgmt_periodic_qpoll_start(sa);
398 * Rx/Tx event queues are started/stopped when corresponding
399 * Rx/Tx queue is started/stopped.
405 rte_spinlock_unlock(&sa->mgmt_evq_lock);
406 efx_ev_fini(sa->nic);
409 sfc_log_init(sa, "failed %d", rc);
414 sfc_ev_stop(struct sfc_adapter *sa)
416 unsigned int sw_index;
418 sfc_log_init(sa, "entry");
420 sfc_ev_mgmt_periodic_qpoll_stop(sa);
422 /* Make sure that all event queues are stopped */
423 sw_index = sa->evq_count;
424 while (sw_index-- > 0) {
425 if (sw_index == sa->mgmt_evq_index) {
426 /* Locks are required for the management EVQ */
427 rte_spinlock_lock(&sa->mgmt_evq_lock);
428 sfc_ev_qstop(sa, sa->mgmt_evq_index);
429 rte_spinlock_unlock(&sa->mgmt_evq_lock);
431 sfc_ev_qstop(sa, sw_index);
435 efx_ev_fini(sa->nic);
439 sfc_ev_qinit(struct sfc_adapter *sa, unsigned int sw_index,
440 unsigned int entries, int socket_id)
442 struct sfc_evq_info *evq_info;
446 sfc_log_init(sa, "sw_index=%u", sw_index);
448 evq_info = &sa->evq_info[sw_index];
450 SFC_ASSERT(rte_is_power_of_2(entries));
451 SFC_ASSERT(entries <= evq_info->max_entries);
452 evq_info->entries = entries;
454 evq = rte_zmalloc_socket("sfc-evq", sizeof(*evq), RTE_CACHE_LINE_SIZE,
460 evq->evq_index = sw_index;
462 /* Allocate DMA space */
463 rc = sfc_dma_alloc(sa, "evq", sw_index, EFX_EVQ_SIZE(evq_info->entries),
464 socket_id, &evq->mem);
468 evq->init_state = SFC_EVQ_INITIALIZED;
476 sfc_ev_qfini(struct sfc_adapter *sa, unsigned int sw_index)
480 sfc_log_init(sa, "sw_index=%u", sw_index);
482 evq = sa->evq_info[sw_index].evq;
484 SFC_ASSERT(evq->init_state == SFC_EVQ_INITIALIZED);
486 sa->evq_info[sw_index].evq = NULL;
488 sfc_dma_free(sa, &evq->mem);
494 sfc_ev_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
496 struct sfc_evq_info *evq_info = &sa->evq_info[sw_index];
497 unsigned int max_entries;
499 sfc_log_init(sa, "sw_index=%u", sw_index);
501 max_entries = sfc_evq_max_entries(sa, sw_index);
502 SFC_ASSERT(rte_is_power_of_2(max_entries));
504 evq_info->max_entries = max_entries;
510 sfc_ev_qfini_info(struct sfc_adapter *sa, unsigned int sw_index)
512 sfc_log_init(sa, "sw_index=%u", sw_index);
514 /* Nothing to cleanup */
518 sfc_ev_init(struct sfc_adapter *sa)
521 unsigned int sw_index;
523 sfc_log_init(sa, "entry");
525 sa->evq_count = sfc_ev_qcount(sa);
526 sa->mgmt_evq_index = 0;
527 rte_spinlock_init(&sa->mgmt_evq_lock);
529 /* Allocate EVQ info array */
531 sa->evq_info = rte_calloc_socket("sfc-evqs", sa->evq_count,
532 sizeof(struct sfc_evq_info), 0,
534 if (sa->evq_info == NULL)
535 goto fail_evqs_alloc;
537 for (sw_index = 0; sw_index < sa->evq_count; ++sw_index) {
538 rc = sfc_ev_qinit_info(sa, sw_index);
540 goto fail_ev_qinit_info;
543 rc = sfc_ev_qinit(sa, sa->mgmt_evq_index, SFC_MGMT_EVQ_ENTRIES,
546 goto fail_mgmt_evq_init;
549 * Rx/Tx event queues are created/destroyed when corresponding
550 * Rx/Tx queue is created/destroyed.
557 while (sw_index-- > 0)
558 sfc_ev_qfini_info(sa, sw_index);
560 rte_free(sa->evq_info);
565 sfc_log_init(sa, "failed %d", rc);
570 sfc_ev_fini(struct sfc_adapter *sa)
574 sfc_log_init(sa, "entry");
576 /* Cleanup all event queues */
577 sw_index = sa->evq_count;
578 while (--sw_index >= 0) {
579 if (sa->evq_info[sw_index].evq != NULL)
580 sfc_ev_qfini(sa, sw_index);
581 sfc_ev_qfini_info(sa, sw_index);
584 rte_free(sa->evq_info);