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);
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++ & (SFXGE_NDESCS - 1);
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 label)
160 struct sfxge_evq *evq;
161 struct sfxge_softc *sc;
162 struct sfxge_rxq *rxq;
166 evq = (struct sfxge_evq *)arg;
168 rxq = sc->rxq[label];
170 KASSERT(rxq != NULL, ("rxq == NULL"));
172 /* Resend a software event on the correct queue */
174 evq = sc->evq[index];
176 KASSERT((label & SFXGE_MAGIC_DMAQ_LABEL_MASK) == label,
177 ("(label & SFXGE_MAGIC_DMAQ_LABEL_MASK) != level"));
178 magic = SFXGE_MAGIC_RX_QFLUSH_DONE | label;
180 KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
181 ("evq not started"));
182 efx_ev_qpost(evq->common, magic);
188 sfxge_ev_rxq_flush_failed(void *arg, uint32_t label)
190 struct sfxge_evq *evq;
191 struct sfxge_softc *sc;
192 struct sfxge_rxq *rxq;
196 evq = (struct sfxge_evq *)arg;
198 rxq = sc->rxq[label];
200 KASSERT(rxq != NULL, ("rxq == NULL"));
202 /* Resend a software event on the correct queue */
204 evq = sc->evq[index];
206 KASSERT((label & SFXGE_MAGIC_DMAQ_LABEL_MASK) == label,
207 ("(label & SFXGE_MAGIC_DMAQ_LABEL_MASK) != label"));
208 magic = SFXGE_MAGIC_RX_QFLUSH_FAILED | label;
210 KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
211 ("evq not started"));
212 efx_ev_qpost(evq->common, magic);
218 sfxge_ev_tx(void *arg, uint32_t label, uint32_t id)
220 struct sfxge_evq *evq;
221 struct sfxge_softc *sc;
222 struct sfxge_txq *txq;
226 evq = (struct sfxge_evq *)arg;
228 txq = sc->txq[label];
230 KASSERT(txq != NULL, ("txq == NULL"));
231 KASSERT(evq->index == txq->evq_index,
232 ("evq->index != txq->evq_index"));
234 if (txq->init_state != SFXGE_TXQ_STARTED)
237 stop = (id + 1) & (SFXGE_NDESCS - 1);
238 id = txq->pending & (SFXGE_NDESCS - 1);
240 delta = (stop >= id) ? (stop - id) : (SFXGE_NDESCS - id + stop);
241 txq->pending += delta;
245 if (txq->next == NULL &&
246 evq->txqs != &(txq->next)) {
248 evq->txqs = &(txq->next);
251 if (txq->pending - txq->completed >= SFXGE_TX_BATCH)
252 sfxge_tx_qcomplete(txq);
255 return (evq->tx_done >= SFXGE_EV_BATCH);
259 sfxge_ev_txq_flush_done(void *arg, uint32_t label)
261 struct sfxge_evq *evq;
262 struct sfxge_softc *sc;
263 struct sfxge_txq *txq;
266 evq = (struct sfxge_evq *)arg;
268 txq = sc->txq[label];
270 KASSERT(txq != NULL, ("txq == NULL"));
271 KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
272 ("txq not initialized"));
274 /* Resend a software event on the correct queue */
275 evq = sc->evq[txq->evq_index];
277 KASSERT((label & SFXGE_MAGIC_DMAQ_LABEL_MASK) == label,
278 ("(label & SFXGE_MAGIC_DMAQ_LABEL_MASK) != label"));
279 magic = SFXGE_MAGIC_TX_QFLUSH_DONE | label;
281 KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
282 ("evq not started"));
283 efx_ev_qpost(evq->common, magic);
289 sfxge_ev_software(void *arg, uint16_t magic)
291 struct sfxge_evq *evq;
292 struct sfxge_softc *sc;
295 evq = (struct sfxge_evq *)arg;
298 label = magic & SFXGE_MAGIC_DMAQ_LABEL_MASK;
299 magic &= ~SFXGE_MAGIC_DMAQ_LABEL_MASK;
302 case SFXGE_MAGIC_RX_QFLUSH_DONE: {
303 struct sfxge_rxq *rxq = sc->rxq[label];
305 KASSERT(rxq != NULL, ("rxq == NULL"));
306 KASSERT(evq->index == rxq->index,
307 ("evq->index != rxq->index"));
309 sfxge_rx_qflush_done(rxq);
312 case SFXGE_MAGIC_RX_QFLUSH_FAILED: {
313 struct sfxge_rxq *rxq = sc->rxq[label];
315 KASSERT(rxq != NULL, ("rxq == NULL"));
316 KASSERT(evq->index == rxq->index,
317 ("evq->index != rxq->index"));
319 sfxge_rx_qflush_failed(rxq);
322 case SFXGE_MAGIC_RX_QREFILL: {
323 struct sfxge_rxq *rxq = sc->rxq[label];
325 KASSERT(rxq != NULL, ("rxq == NULL"));
326 KASSERT(evq->index == rxq->index,
327 ("evq->index != rxq->index"));
329 sfxge_rx_qrefill(rxq);
332 case SFXGE_MAGIC_TX_QFLUSH_DONE: {
333 struct sfxge_txq *txq = sc->txq[label];
335 KASSERT(txq != NULL, ("txq == NULL"));
336 KASSERT(evq->index == txq->evq_index,
337 ("evq->index != txq->evq_index"));
339 sfxge_tx_qflush_done(txq);
350 sfxge_ev_sram(void *arg, uint32_t code)
356 case EFX_SRAM_UPDATE:
357 EFSYS_PROBE(sram_update);
361 EFSYS_PROBE(sram_clear);
364 case EFX_SRAM_ILLEGAL_CLEAR:
365 EFSYS_PROBE(sram_illegal_clear);
369 KASSERT(B_FALSE, ("Impossible SRAM event"));
377 sfxge_ev_timer(void *arg, uint32_t index)
386 sfxge_ev_wake_up(void *arg, uint32_t index)
395 sfxge_ev_stat_update(struct sfxge_softc *sc)
397 struct sfxge_evq *evq;
401 sx_xlock(&sc->softc_lock);
403 if (sc->evq[0]->init_state != SFXGE_EVQ_STARTED)
407 if (now - sc->ev_stats_update_time < hz)
410 sc->ev_stats_update_time = now;
412 /* Add event counts from each event queue in turn */
413 for (index = 0; index < sc->intr.n_alloc; index++) {
414 evq = sc->evq[index];
415 mtx_lock(&evq->lock);
416 efx_ev_qstats_update(evq->common, sc->ev_stats);
417 mtx_unlock(&evq->lock);
420 sx_xunlock(&sc->softc_lock);
424 sfxge_ev_stat_handler(SYSCTL_HANDLER_ARGS)
426 struct sfxge_softc *sc = arg1;
427 unsigned int id = arg2;
429 sfxge_ev_stat_update(sc);
431 return SYSCTL_OUT(req, &sc->ev_stats[id], sizeof(sc->ev_stats[id]));
435 sfxge_ev_stat_init(struct sfxge_softc *sc)
437 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
438 struct sysctl_oid_list *stat_list;
442 stat_list = SYSCTL_CHILDREN(sc->stats_node);
444 for (id = 0; id < EV_NQSTATS; id++) {
445 snprintf(name, sizeof(name), "ev_%s",
446 efx_ev_qstat_name(sc->enp, id));
449 OID_AUTO, name, CTLTYPE_U64|CTLFLAG_RD,
450 sc, id, sfxge_ev_stat_handler, "Q",
456 sfxge_ev_qmoderate(struct sfxge_softc *sc, unsigned int idx, unsigned int us)
458 struct sfxge_evq *evq;
464 KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
465 ("evq->init_state != SFXGE_EVQ_STARTED"));
467 (void)efx_ev_qmoderate(eep, us);
471 sfxge_int_mod_handler(SYSCTL_HANDLER_ARGS)
473 struct sfxge_softc *sc = arg1;
474 struct sfxge_intr *intr = &sc->intr;
475 unsigned int moderation;
479 sx_xlock(&sc->softc_lock);
482 if ((error = SYSCTL_IN(req, &moderation, sizeof(moderation)))
486 /* We may not be calling efx_ev_qmoderate() now,
487 * so we have to range-check the value ourselves.
490 efx_nic_cfg_get(sc->enp)->enc_evq_moderation_max) {
495 sc->ev_moderation = moderation;
496 if (intr->state == SFXGE_INTR_STARTED) {
497 for (index = 0; index < intr->n_alloc; index++)
498 sfxge_ev_qmoderate(sc, index, moderation);
501 error = SYSCTL_OUT(req, &sc->ev_moderation,
502 sizeof(sc->ev_moderation));
506 sx_xunlock(&sc->softc_lock);
512 sfxge_ev_initialized(void *arg)
514 struct sfxge_evq *evq;
516 evq = (struct sfxge_evq *)arg;
518 KASSERT(evq->init_state == SFXGE_EVQ_STARTING,
519 ("evq not starting"));
521 evq->init_state = SFXGE_EVQ_STARTED;
527 sfxge_ev_link_change(void *arg, efx_link_mode_t link_mode)
529 struct sfxge_evq *evq;
530 struct sfxge_softc *sc;
532 evq = (struct sfxge_evq *)arg;
535 sfxge_mac_link_update(sc, link_mode);
540 static const efx_ev_callbacks_t sfxge_ev_callbacks = {
541 .eec_initialized = sfxge_ev_initialized,
542 .eec_rx = sfxge_ev_rx,
543 .eec_tx = sfxge_ev_tx,
544 .eec_exception = sfxge_ev_exception,
545 .eec_rxq_flush_done = sfxge_ev_rxq_flush_done,
546 .eec_rxq_flush_failed = sfxge_ev_rxq_flush_failed,
547 .eec_txq_flush_done = sfxge_ev_txq_flush_done,
548 .eec_software = sfxge_ev_software,
549 .eec_sram = sfxge_ev_sram,
550 .eec_wake_up = sfxge_ev_wake_up,
551 .eec_timer = sfxge_ev_timer,
552 .eec_link_change = sfxge_ev_link_change,
557 sfxge_ev_qpoll(struct sfxge_softc *sc, unsigned int index)
559 struct sfxge_evq *evq;
562 evq = sc->evq[index];
564 mtx_lock(&evq->lock);
566 if (evq->init_state != SFXGE_EVQ_STARTING &&
567 evq->init_state != SFXGE_EVQ_STARTED) {
572 /* Synchronize the DMA memory for reading */
573 bus_dmamap_sync(evq->mem.esm_tag, evq->mem.esm_map,
574 BUS_DMASYNC_POSTREAD);
576 KASSERT(evq->rx_done == 0, ("evq->rx_done != 0"));
577 KASSERT(evq->tx_done == 0, ("evq->tx_done != 0"));
578 KASSERT(evq->txq == NULL, ("evq->txq != NULL"));
579 KASSERT(evq->txqs == &evq->txq, ("evq->txqs != &evq->txq"));
582 efx_ev_qpoll(evq->common, &evq->read_ptr, &sfxge_ev_callbacks, evq);
587 /* Perform any pending completion processing */
588 sfxge_ev_qcomplete(evq, B_TRUE);
590 /* Re-prime the event queue for interrupts */
591 if ((rc = efx_ev_qprime(evq->common, evq->read_ptr)) != 0)
594 mtx_unlock(&evq->lock);
599 mtx_unlock(&(evq->lock));
604 sfxge_ev_qstop(struct sfxge_softc *sc, unsigned int index)
606 struct sfxge_evq *evq;
608 evq = sc->evq[index];
610 KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
611 ("evq->init_state != SFXGE_EVQ_STARTED"));
613 mtx_lock(&evq->lock);
614 evq->init_state = SFXGE_EVQ_INITIALIZED;
616 evq->exception = B_FALSE;
618 /* Add event counts before discarding the common evq state */
619 efx_ev_qstats_update(evq->common, sc->ev_stats);
621 efx_ev_qdestroy(evq->common);
622 efx_sram_buf_tbl_clear(sc->enp, evq->buf_base_id,
623 EFX_EVQ_NBUFS(SFXGE_NEVS));
624 mtx_unlock(&evq->lock);
628 sfxge_ev_qstart(struct sfxge_softc *sc, unsigned int index)
630 struct sfxge_evq *evq;
635 evq = sc->evq[index];
638 KASSERT(evq->init_state == SFXGE_EVQ_INITIALIZED,
639 ("evq->init_state != SFXGE_EVQ_INITIALIZED"));
641 /* Clear all events. */
642 (void)memset(esmp->esm_base, 0xff, EFX_EVQ_SIZE(SFXGE_NEVS));
644 /* Program the buffer table. */
645 if ((rc = efx_sram_buf_tbl_set(sc->enp, evq->buf_base_id, esmp,
646 EFX_EVQ_NBUFS(SFXGE_NEVS))) != 0)
649 /* Create the common code event queue. */
650 if ((rc = efx_ev_qcreate(sc->enp, index, esmp, SFXGE_NEVS,
651 evq->buf_base_id, &evq->common)) != 0)
654 mtx_lock(&evq->lock);
656 /* Set the default moderation */
657 (void)efx_ev_qmoderate(evq->common, sc->ev_moderation);
659 /* Prime the event queue for interrupts */
660 if ((rc = efx_ev_qprime(evq->common, evq->read_ptr)) != 0)
663 evq->init_state = SFXGE_EVQ_STARTING;
665 mtx_unlock(&evq->lock);
667 /* Wait for the initialization event */
670 /* Pause for 100 ms */
671 pause("sfxge evq init", hz / 10);
673 /* Check to see if the test event has been processed */
674 if (evq->init_state == SFXGE_EVQ_STARTED)
677 } while (++count < 20);
686 mtx_lock(&evq->lock);
687 evq->init_state = SFXGE_EVQ_INITIALIZED;
689 mtx_unlock(&evq->lock);
690 efx_ev_qdestroy(evq->common);
692 efx_sram_buf_tbl_clear(sc->enp, evq->buf_base_id,
693 EFX_EVQ_NBUFS(SFXGE_NEVS));
699 sfxge_ev_stop(struct sfxge_softc *sc)
701 struct sfxge_intr *intr;
708 KASSERT(intr->state == SFXGE_INTR_STARTED,
709 ("Interrupts not started"));
711 /* Stop the event queue(s) */
712 index = intr->n_alloc;
714 sfxge_ev_qstop(sc, index);
716 /* Tear down the event module */
721 sfxge_ev_start(struct sfxge_softc *sc)
723 struct sfxge_intr *intr;
729 KASSERT(intr->state == SFXGE_INTR_STARTED,
730 ("intr->state != SFXGE_INTR_STARTED"));
732 /* Initialize the event module */
733 if ((rc = efx_ev_init(sc->enp)) != 0)
736 /* Start the event queues */
737 for (index = 0; index < intr->n_alloc; index++) {
738 if ((rc = sfxge_ev_qstart(sc, index)) != 0)
745 /* Stop the event queue(s) */
747 sfxge_ev_qstop(sc, index);
749 /* Tear down the event module */
750 efx_ev_fini(sc->enp);
756 sfxge_ev_qfini(struct sfxge_softc *sc, unsigned int index)
758 struct sfxge_evq *evq;
760 evq = sc->evq[index];
762 KASSERT(evq->init_state == SFXGE_EVQ_INITIALIZED,
763 ("evq->init_state != SFXGE_EVQ_INITIALIZED"));
764 KASSERT(evq->txqs == &evq->txq, ("evq->txqs != &evq->txq"));
766 sfxge_dma_free(&evq->mem);
768 sc->evq[index] = NULL;
770 mtx_destroy(&evq->lock);
776 sfxge_ev_qinit(struct sfxge_softc *sc, unsigned int index)
778 struct sfxge_evq *evq;
782 KASSERT(index < SFXGE_RX_SCALE_MAX, ("index >= SFXGE_RX_SCALE_MAX"));
784 evq = malloc(sizeof(struct sfxge_evq), M_SFXGE, M_ZERO | M_WAITOK);
787 sc->evq[index] = evq;
790 /* Initialise TX completion list */
791 evq->txqs = &evq->txq;
793 /* Allocate DMA space. */
794 if ((rc = sfxge_dma_alloc(sc, EFX_EVQ_SIZE(SFXGE_NEVS), esmp)) != 0)
797 /* Allocate buffer table entries. */
798 sfxge_sram_buf_tbl_alloc(sc, EFX_EVQ_NBUFS(SFXGE_NEVS),
801 mtx_init(&evq->lock, "evq", NULL, MTX_DEF);
803 evq->init_state = SFXGE_EVQ_INITIALIZED;
809 sfxge_ev_fini(struct sfxge_softc *sc)
811 struct sfxge_intr *intr;
816 KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
817 ("intr->state != SFXGE_INTR_INITIALIZED"));
819 sc->ev_moderation = 0;
821 /* Tear down the event queue(s). */
822 index = intr->n_alloc;
824 sfxge_ev_qfini(sc, index);
828 sfxge_ev_init(struct sfxge_softc *sc)
830 struct sysctl_ctx_list *sysctl_ctx = device_get_sysctl_ctx(sc->dev);
831 struct sysctl_oid *sysctl_tree = device_get_sysctl_tree(sc->dev);
832 struct sfxge_intr *intr;
838 KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
839 ("intr->state != SFXGE_INTR_INITIALIZED"));
841 /* Set default interrupt moderation; add a sysctl to
842 * read and change it.
844 sc->ev_moderation = 30;
845 SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
846 OID_AUTO, "int_mod", CTLTYPE_UINT|CTLFLAG_RW,
847 sc, 0, sfxge_int_mod_handler, "IU",
848 "sfxge interrupt moderation (us)");
851 * Initialize the event queue(s) - one per interrupt.
853 for (index = 0; index < intr->n_alloc; index++) {
854 if ((rc = sfxge_ev_qinit(sc, index)) != 0)
858 sfxge_ev_stat_init(sc);
864 sfxge_ev_qfini(sc, index);