2 * Copyright (c) 2010-2011 Solarflare Communications, Inc.
5 * This software was developed in part by Philip Paeps under contract for
6 * Solarflare Communications, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/systm.h>
39 #include <sys/taskqueue.h>
41 #include "common/efx.h"
46 sfxge_ev_qcomplete(struct sfxge_evq *evq, boolean_t eop)
48 struct sfxge_softc *sc;
50 struct sfxge_rxq *rxq;
51 struct sfxge_txq *txq;
57 if ((txq = evq->txq) != NULL) {
59 evq->txqs = &(evq->txq);
62 struct sfxge_txq *next;
67 KASSERT(txq->evq_index == index,
68 ("txq->evq_index != index"));
70 if (txq->pending != txq->completed)
71 sfxge_tx_qcomplete(txq, evq);
74 } while (txq != NULL);
77 if (rxq->pending != rxq->completed)
78 sfxge_rx_qcomplete(rxq, eop);
82 sfxge_ev_rx(void *arg, uint32_t label, uint32_t id, uint32_t size,
85 struct sfxge_evq *evq;
86 struct sfxge_softc *sc;
87 struct sfxge_rxq *rxq;
88 unsigned int expected;
89 struct sfxge_rx_sw_desc *rx_desc;
98 KASSERT(rxq != NULL, ("rxq == NULL"));
99 KASSERT(evq->index == rxq->index,
100 ("evq->index != rxq->index"));
102 if (rxq->init_state != SFXGE_RXQ_STARTED)
105 expected = rxq->pending++ & rxq->ptr_mask;
106 if (id != expected) {
107 evq->exception = B_TRUE;
109 device_printf(sc->dev, "RX completion out of order"
110 " (id=%#x expected=%#x flags=%#x); resetting\n",
111 id, expected, flags);
112 sfxge_schedule_reset(sc);
117 rx_desc = &rxq->queue[id];
119 KASSERT(rx_desc->flags == EFX_DISCARD,
120 ("rx_desc->flags != EFX_DISCARD"));
121 rx_desc->flags = flags;
123 KASSERT(size < (1 << 16), ("size > (1 << 16)"));
124 rx_desc->size = (uint16_t)size;
125 prefetch_read_many(rx_desc->mbuf);
129 if (rxq->pending - rxq->completed >= SFXGE_RX_BATCH)
130 sfxge_ev_qcomplete(evq, B_FALSE);
133 return (evq->rx_done >= SFXGE_EV_BATCH);
137 sfxge_ev_exception(void *arg, uint32_t code, uint32_t data)
139 struct sfxge_evq *evq;
140 struct sfxge_softc *sc;
142 evq = (struct sfxge_evq *)arg;
145 evq->exception = B_TRUE;
147 if (code != EFX_EXCEPTION_UNKNOWN_SENSOREVT) {
148 device_printf(sc->dev,
149 "hardware exception (code=%u); resetting\n",
151 sfxge_schedule_reset(sc);
158 sfxge_ev_rxq_flush_done(void *arg, uint32_t rxq_index)
160 struct sfxge_evq *evq;
161 struct sfxge_softc *sc;
162 struct sfxge_rxq *rxq;
167 evq = (struct sfxge_evq *)arg;
169 rxq = sc->rxq[rxq_index];
171 KASSERT(rxq != NULL, ("rxq == NULL"));
173 /* Resend a software event on the correct queue */
175 evq = sc->evq[index];
178 KASSERT((label & SFXGE_MAGIC_DMAQ_LABEL_MASK) == label,
179 ("(label & SFXGE_MAGIC_DMAQ_LABEL_MASK) != level"));
180 magic = SFXGE_MAGIC_RX_QFLUSH_DONE | label;
182 KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
183 ("evq not started"));
184 efx_ev_qpost(evq->common, magic);
190 sfxge_ev_rxq_flush_failed(void *arg, uint32_t rxq_index)
192 struct sfxge_evq *evq;
193 struct sfxge_softc *sc;
194 struct sfxge_rxq *rxq;
199 evq = (struct sfxge_evq *)arg;
201 rxq = sc->rxq[rxq_index];
203 KASSERT(rxq != NULL, ("rxq == NULL"));
205 /* Resend a software event on the correct queue */
207 evq = sc->evq[index];
210 KASSERT((label & SFXGE_MAGIC_DMAQ_LABEL_MASK) == label,
211 ("(label & SFXGE_MAGIC_DMAQ_LABEL_MASK) != label"));
212 magic = SFXGE_MAGIC_RX_QFLUSH_FAILED | label;
214 KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
215 ("evq not started"));
216 efx_ev_qpost(evq->common, magic);
221 static struct sfxge_txq *
222 sfxge_get_txq_by_label(struct sfxge_evq *evq, enum sfxge_txq_type label)
226 KASSERT((evq->index == 0 && label < SFXGE_TXQ_NTYPES) ||
227 (label == SFXGE_TXQ_IP_TCP_UDP_CKSUM), ("unexpected txq label"));
228 index = (evq->index == 0) ? label : (evq->index - 1 + SFXGE_TXQ_NTYPES);
229 return (evq->sc->txq[index]);
233 sfxge_ev_tx(void *arg, uint32_t label, uint32_t id)
235 struct sfxge_evq *evq;
236 struct sfxge_txq *txq;
240 evq = (struct sfxge_evq *)arg;
241 txq = sfxge_get_txq_by_label(evq, label);
243 KASSERT(txq != NULL, ("txq == NULL"));
244 KASSERT(evq->index == txq->evq_index,
245 ("evq->index != txq->evq_index"));
247 if (txq->init_state != SFXGE_TXQ_STARTED)
250 stop = (id + 1) & txq->ptr_mask;
251 id = txq->pending & txq->ptr_mask;
253 delta = (stop >= id) ? (stop - id) : (txq->entries - id + stop);
254 txq->pending += delta;
258 if (txq->next == NULL &&
259 evq->txqs != &(txq->next)) {
261 evq->txqs = &(txq->next);
264 if (txq->pending - txq->completed >= SFXGE_TX_BATCH)
265 sfxge_tx_qcomplete(txq, evq);
268 return (evq->tx_done >= SFXGE_EV_BATCH);
272 sfxge_ev_txq_flush_done(void *arg, uint32_t txq_index)
274 struct sfxge_evq *evq;
275 struct sfxge_softc *sc;
276 struct sfxge_txq *txq;
280 evq = (struct sfxge_evq *)arg;
282 txq = sc->txq[txq_index];
284 KASSERT(txq != NULL, ("txq == NULL"));
285 KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
286 ("txq not initialized"));
288 /* Resend a software event on the correct queue */
289 evq = sc->evq[txq->evq_index];
292 KASSERT((label & SFXGE_MAGIC_DMAQ_LABEL_MASK) == label,
293 ("(label & SFXGE_MAGIC_DMAQ_LABEL_MASK) != label"));
294 magic = SFXGE_MAGIC_TX_QFLUSH_DONE | label;
296 KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
297 ("evq not started"));
298 efx_ev_qpost(evq->common, magic);
304 sfxge_ev_software(void *arg, uint16_t magic)
306 struct sfxge_evq *evq;
307 struct sfxge_softc *sc;
310 evq = (struct sfxge_evq *)arg;
313 label = magic & SFXGE_MAGIC_DMAQ_LABEL_MASK;
314 magic &= ~SFXGE_MAGIC_DMAQ_LABEL_MASK;
317 case SFXGE_MAGIC_RX_QFLUSH_DONE: {
318 struct sfxge_rxq *rxq = sc->rxq[label];
320 KASSERT(rxq != NULL, ("rxq == NULL"));
321 KASSERT(evq->index == rxq->index,
322 ("evq->index != rxq->index"));
324 sfxge_rx_qflush_done(rxq);
327 case SFXGE_MAGIC_RX_QFLUSH_FAILED: {
328 struct sfxge_rxq *rxq = sc->rxq[label];
330 KASSERT(rxq != NULL, ("rxq == NULL"));
331 KASSERT(evq->index == rxq->index,
332 ("evq->index != rxq->index"));
334 sfxge_rx_qflush_failed(rxq);
337 case SFXGE_MAGIC_RX_QREFILL: {
338 struct sfxge_rxq *rxq = sc->rxq[label];
340 KASSERT(rxq != NULL, ("rxq == NULL"));
341 KASSERT(evq->index == rxq->index,
342 ("evq->index != rxq->index"));
344 sfxge_rx_qrefill(rxq);
347 case SFXGE_MAGIC_TX_QFLUSH_DONE: {
348 struct sfxge_txq *txq = sfxge_get_txq_by_label(evq, label);
350 KASSERT(txq != NULL, ("txq == NULL"));
351 KASSERT(evq->index == txq->evq_index,
352 ("evq->index != txq->evq_index"));
354 sfxge_tx_qflush_done(txq);
365 sfxge_ev_sram(void *arg, uint32_t code)
371 case EFX_SRAM_UPDATE:
372 EFSYS_PROBE(sram_update);
376 EFSYS_PROBE(sram_clear);
379 case EFX_SRAM_ILLEGAL_CLEAR:
380 EFSYS_PROBE(sram_illegal_clear);
384 KASSERT(B_FALSE, ("Impossible SRAM event"));
392 sfxge_ev_timer(void *arg, uint32_t index)
401 sfxge_ev_wake_up(void *arg, uint32_t index)
412 sfxge_ev_stat_update(struct sfxge_softc *sc)
414 struct sfxge_evq *evq;
418 SFXGE_ADAPTER_LOCK(sc);
420 if (sc->evq[0]->init_state != SFXGE_EVQ_STARTED)
424 if (now - sc->ev_stats_update_time < hz)
427 sc->ev_stats_update_time = now;
429 /* Add event counts from each event queue in turn */
430 for (index = 0; index < sc->intr.n_alloc; index++) {
431 evq = sc->evq[index];
433 efx_ev_qstats_update(evq->common, sc->ev_stats);
434 SFXGE_EVQ_UNLOCK(evq);
437 SFXGE_ADAPTER_UNLOCK(sc);
441 sfxge_ev_stat_handler(SYSCTL_HANDLER_ARGS)
443 struct sfxge_softc *sc = arg1;
444 unsigned int id = arg2;
446 sfxge_ev_stat_update(sc);
448 return (SYSCTL_OUT(req, &sc->ev_stats[id], sizeof(sc->ev_stats[id])));
452 sfxge_ev_stat_init(struct sfxge_softc *sc)
454 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
455 struct sysctl_oid_list *stat_list;
459 stat_list = SYSCTL_CHILDREN(sc->stats_node);
461 for (id = 0; id < EV_NQSTATS; id++) {
462 snprintf(name, sizeof(name), "ev_%s",
463 efx_ev_qstat_name(sc->enp, id));
466 OID_AUTO, name, CTLTYPE_U64|CTLFLAG_RD,
467 sc, id, sfxge_ev_stat_handler, "Q",
472 #endif /* EFSYS_OPT_QSTATS */
475 sfxge_ev_qmoderate(struct sfxge_softc *sc, unsigned int idx, unsigned int us)
477 struct sfxge_evq *evq;
483 KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
484 ("evq->init_state != SFXGE_EVQ_STARTED"));
486 (void)efx_ev_qmoderate(eep, us);
490 sfxge_int_mod_handler(SYSCTL_HANDLER_ARGS)
492 struct sfxge_softc *sc = arg1;
493 struct sfxge_intr *intr = &sc->intr;
494 unsigned int moderation;
498 SFXGE_ADAPTER_LOCK(sc);
500 if (req->newptr != NULL) {
501 if ((error = SYSCTL_IN(req, &moderation, sizeof(moderation)))
505 /* We may not be calling efx_ev_qmoderate() now,
506 * so we have to range-check the value ourselves.
509 efx_nic_cfg_get(sc->enp)->enc_evq_moderation_max) {
514 sc->ev_moderation = moderation;
515 if (intr->state == SFXGE_INTR_STARTED) {
516 for (index = 0; index < intr->n_alloc; index++)
517 sfxge_ev_qmoderate(sc, index, moderation);
520 error = SYSCTL_OUT(req, &sc->ev_moderation,
521 sizeof(sc->ev_moderation));
525 SFXGE_ADAPTER_UNLOCK(sc);
531 sfxge_ev_initialized(void *arg)
533 struct sfxge_evq *evq;
535 evq = (struct sfxge_evq *)arg;
537 KASSERT(evq->init_state == SFXGE_EVQ_STARTING,
538 ("evq not starting"));
540 evq->init_state = SFXGE_EVQ_STARTED;
546 sfxge_ev_link_change(void *arg, efx_link_mode_t link_mode)
548 struct sfxge_evq *evq;
549 struct sfxge_softc *sc;
551 evq = (struct sfxge_evq *)arg;
554 sfxge_mac_link_update(sc, link_mode);
559 static const efx_ev_callbacks_t sfxge_ev_callbacks = {
560 .eec_initialized = sfxge_ev_initialized,
561 .eec_rx = sfxge_ev_rx,
562 .eec_tx = sfxge_ev_tx,
563 .eec_exception = sfxge_ev_exception,
564 .eec_rxq_flush_done = sfxge_ev_rxq_flush_done,
565 .eec_rxq_flush_failed = sfxge_ev_rxq_flush_failed,
566 .eec_txq_flush_done = sfxge_ev_txq_flush_done,
567 .eec_software = sfxge_ev_software,
568 .eec_sram = sfxge_ev_sram,
569 .eec_wake_up = sfxge_ev_wake_up,
570 .eec_timer = sfxge_ev_timer,
571 .eec_link_change = sfxge_ev_link_change,
576 sfxge_ev_qpoll(struct sfxge_evq *evq)
582 if (evq->init_state != SFXGE_EVQ_STARTING &&
583 evq->init_state != SFXGE_EVQ_STARTED) {
588 /* Synchronize the DMA memory for reading */
589 bus_dmamap_sync(evq->mem.esm_tag, evq->mem.esm_map,
590 BUS_DMASYNC_POSTREAD);
592 KASSERT(evq->rx_done == 0, ("evq->rx_done != 0"));
593 KASSERT(evq->tx_done == 0, ("evq->tx_done != 0"));
594 KASSERT(evq->txq == NULL, ("evq->txq != NULL"));
595 KASSERT(evq->txqs == &evq->txq, ("evq->txqs != &evq->txq"));
598 efx_ev_qpoll(evq->common, &evq->read_ptr, &sfxge_ev_callbacks, evq);
603 /* Perform any pending completion processing */
604 sfxge_ev_qcomplete(evq, B_TRUE);
606 /* Re-prime the event queue for interrupts */
607 if ((rc = efx_ev_qprime(evq->common, evq->read_ptr)) != 0)
610 SFXGE_EVQ_UNLOCK(evq);
615 SFXGE_EVQ_UNLOCK(evq);
620 sfxge_ev_qstop(struct sfxge_softc *sc, unsigned int index)
622 struct sfxge_evq *evq;
624 evq = sc->evq[index];
626 KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
627 ("evq->init_state != SFXGE_EVQ_STARTED"));
630 evq->init_state = SFXGE_EVQ_INITIALIZED;
632 evq->exception = B_FALSE;
635 /* Add event counts before discarding the common evq state */
636 efx_ev_qstats_update(evq->common, sc->ev_stats);
639 efx_ev_qdestroy(evq->common);
640 efx_sram_buf_tbl_clear(sc->enp, evq->buf_base_id,
641 EFX_EVQ_NBUFS(evq->entries));
642 SFXGE_EVQ_UNLOCK(evq);
646 sfxge_ev_qstart(struct sfxge_softc *sc, unsigned int index)
648 struct sfxge_evq *evq;
653 evq = sc->evq[index];
656 KASSERT(evq->init_state == SFXGE_EVQ_INITIALIZED,
657 ("evq->init_state != SFXGE_EVQ_INITIALIZED"));
659 /* Clear all events. */
660 (void)memset(esmp->esm_base, 0xff, EFX_EVQ_SIZE(evq->entries));
662 /* Program the buffer table. */
663 if ((rc = efx_sram_buf_tbl_set(sc->enp, evq->buf_base_id, esmp,
664 EFX_EVQ_NBUFS(evq->entries))) != 0)
667 /* Create the common code event queue. */
668 if ((rc = efx_ev_qcreate(sc->enp, index, esmp, evq->entries,
669 evq->buf_base_id, &evq->common)) != 0)
674 /* Set the default moderation */
675 (void)efx_ev_qmoderate(evq->common, sc->ev_moderation);
677 /* Prime the event queue for interrupts */
678 if ((rc = efx_ev_qprime(evq->common, evq->read_ptr)) != 0)
681 evq->init_state = SFXGE_EVQ_STARTING;
683 SFXGE_EVQ_UNLOCK(evq);
685 /* Wait for the initialization event */
688 /* Pause for 100 ms */
689 pause("sfxge evq init", hz / 10);
691 /* Check to see if the test event has been processed */
692 if (evq->init_state == SFXGE_EVQ_STARTED)
695 } while (++count < 20);
705 evq->init_state = SFXGE_EVQ_INITIALIZED;
707 SFXGE_EVQ_UNLOCK(evq);
708 efx_ev_qdestroy(evq->common);
710 efx_sram_buf_tbl_clear(sc->enp, evq->buf_base_id,
711 EFX_EVQ_NBUFS(evq->entries));
717 sfxge_ev_stop(struct sfxge_softc *sc)
719 struct sfxge_intr *intr;
726 KASSERT(intr->state == SFXGE_INTR_STARTED,
727 ("Interrupts not started"));
729 /* Stop the event queue(s) */
730 index = intr->n_alloc;
732 sfxge_ev_qstop(sc, index);
734 /* Tear down the event module */
739 sfxge_ev_start(struct sfxge_softc *sc)
741 struct sfxge_intr *intr;
747 KASSERT(intr->state == SFXGE_INTR_STARTED,
748 ("intr->state != SFXGE_INTR_STARTED"));
750 /* Initialize the event module */
751 if ((rc = efx_ev_init(sc->enp)) != 0)
754 /* Start the event queues */
755 for (index = 0; index < intr->n_alloc; index++) {
756 if ((rc = sfxge_ev_qstart(sc, index)) != 0)
763 /* Stop the event queue(s) */
765 sfxge_ev_qstop(sc, index);
767 /* Tear down the event module */
768 efx_ev_fini(sc->enp);
774 sfxge_ev_qfini(struct sfxge_softc *sc, unsigned int index)
776 struct sfxge_evq *evq;
778 evq = sc->evq[index];
780 KASSERT(evq->init_state == SFXGE_EVQ_INITIALIZED,
781 ("evq->init_state != SFXGE_EVQ_INITIALIZED"));
782 KASSERT(evq->txqs == &evq->txq, ("evq->txqs != &evq->txq"));
784 sfxge_dma_free(&evq->mem);
786 sc->evq[index] = NULL;
788 SFXGE_EVQ_LOCK_DESTROY(evq);
794 sfxge_ev_qinit(struct sfxge_softc *sc, unsigned int index)
796 struct sfxge_evq *evq;
800 KASSERT(index < SFXGE_RX_SCALE_MAX, ("index >= SFXGE_RX_SCALE_MAX"));
802 evq = malloc(sizeof(struct sfxge_evq), M_SFXGE, M_ZERO | M_WAITOK);
805 sc->evq[index] = evq;
808 /* Build an event queue with room for one event per tx and rx buffer,
809 * plus some extra for link state events and MCDI completions.
810 * There are three tx queues in the first event queue and one in
815 ROUNDUP_POW_OF_TWO(sc->rxq_entries +
816 3 * sc->txq_entries +
820 ROUNDUP_POW_OF_TWO(sc->rxq_entries +
824 /* Initialise TX completion list */
825 evq->txqs = &evq->txq;
827 /* Allocate DMA space. */
828 if ((rc = sfxge_dma_alloc(sc, EFX_EVQ_SIZE(evq->entries), esmp)) != 0)
831 /* Allocate buffer table entries. */
832 sfxge_sram_buf_tbl_alloc(sc, EFX_EVQ_NBUFS(evq->entries),
835 SFXGE_EVQ_LOCK_INIT(evq, "evq");
837 evq->init_state = SFXGE_EVQ_INITIALIZED;
843 sfxge_ev_fini(struct sfxge_softc *sc)
845 struct sfxge_intr *intr;
850 KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
851 ("intr->state != SFXGE_INTR_INITIALIZED"));
853 sc->ev_moderation = 0;
855 /* Tear down the event queue(s). */
856 index = intr->n_alloc;
858 sfxge_ev_qfini(sc, index);
862 sfxge_ev_init(struct sfxge_softc *sc)
864 struct sysctl_ctx_list *sysctl_ctx = device_get_sysctl_ctx(sc->dev);
865 struct sysctl_oid *sysctl_tree = device_get_sysctl_tree(sc->dev);
866 struct sfxge_intr *intr;
872 KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
873 ("intr->state != SFXGE_INTR_INITIALIZED"));
875 /* Set default interrupt moderation; add a sysctl to
876 * read and change it.
878 sc->ev_moderation = SFXGE_MODERATION;
879 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
880 OID_AUTO, "int_mod", CTLTYPE_UINT|CTLFLAG_RW,
881 sc, 0, sfxge_int_mod_handler, "IU",
882 "sfxge interrupt moderation (us)");
885 * Initialize the event queue(s) - one per interrupt.
887 for (index = 0; index < intr->n_alloc; index++) {
888 if ((rc = sfxge_ev_qinit(sc, index)) != 0)
893 sfxge_ev_stat_init(sc);
900 sfxge_ev_qfini(sc, index);