xref: /freebsd/sys/dev/sfxge/sfxge_ev.c (revision 38f0b757fd84d17d0fc24739a7cda160c4516d81)
1 /*-
2  * Copyright (c) 2010-2011 Solarflare Communications, Inc.
3  * All rights reserved.
4  *
5  * This software was developed in part by Philip Paeps under contract for
6  * Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
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>
40 
41 #include "common/efx.h"
42 
43 #include "sfxge.h"
44 
45 static void
46 sfxge_ev_qcomplete(struct sfxge_evq *evq, boolean_t eop)
47 {
48 	struct sfxge_softc *sc;
49 	unsigned int index;
50 	struct sfxge_rxq *rxq;
51 	struct sfxge_txq *txq;
52 
53 	sc = evq->sc;
54 	index = evq->index;
55 	rxq = sc->rxq[index];
56 
57 	if ((txq = evq->txq) != NULL) {
58 		evq->txq = NULL;
59 		evq->txqs = &(evq->txq);
60 
61 		do {
62 			struct sfxge_txq *next;
63 
64 			next = txq->next;
65 			txq->next = NULL;
66 
67 			KASSERT(txq->evq_index == index,
68 			    ("txq->evq_index != index"));
69 
70 			if (txq->pending != txq->completed)
71 				sfxge_tx_qcomplete(txq);
72 
73 			txq = next;
74 		} while (txq != NULL);
75 	}
76 
77 	if (rxq->pending != rxq->completed)
78 		sfxge_rx_qcomplete(rxq, eop);
79 }
80 
81 static boolean_t
82 sfxge_ev_rx(void *arg, uint32_t label, uint32_t id, uint32_t size,
83     uint16_t flags)
84 {
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;
90 
91 	evq = arg;
92 	sc = evq->sc;
93 
94 	if (evq->exception)
95 		goto done;
96 
97 	rxq = sc->rxq[label];
98 	KASSERT(rxq != NULL, ("rxq == NULL"));
99 	KASSERT(evq->index == rxq->index,
100 	    ("evq->index != rxq->index"));
101 
102 	if (rxq->init_state != SFXGE_RXQ_STARTED)
103 		goto done;
104 
105 	expected = rxq->pending++ & (SFXGE_NDESCS - 1);
106 	if (id != expected) {
107 		evq->exception = B_TRUE;
108 
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);
113 
114 		goto done;
115 	}
116 
117 	rx_desc = &rxq->queue[id];
118 
119 	KASSERT(rx_desc->flags == EFX_DISCARD,
120 	    ("rx_desc->flags != EFX_DISCARD"));
121 	rx_desc->flags = flags;
122 
123 	KASSERT(size < (1 << 16), ("size > (1 << 16)"));
124 	rx_desc->size = (uint16_t)size;
125 	prefetch_read_many(rx_desc->mbuf);
126 
127 	evq->rx_done++;
128 
129 	if (rxq->pending - rxq->completed >= SFXGE_RX_BATCH)
130 		sfxge_ev_qcomplete(evq, B_FALSE);
131 
132 done:
133 	return (evq->rx_done >= SFXGE_EV_BATCH);
134 }
135 
136 static boolean_t
137 sfxge_ev_exception(void *arg, uint32_t code, uint32_t data)
138 {
139 	struct sfxge_evq *evq;
140 	struct sfxge_softc *sc;
141 
142 	evq = (struct sfxge_evq *)arg;
143 	sc = evq->sc;
144 
145 	evq->exception = B_TRUE;
146 
147 	if (code != EFX_EXCEPTION_UNKNOWN_SENSOREVT) {
148 		device_printf(sc->dev,
149 			      "hardware exception (code=%u); resetting\n",
150 			      code);
151 		sfxge_schedule_reset(sc);
152 	}
153 
154 	return (B_FALSE);
155 }
156 
157 static boolean_t
158 sfxge_ev_rxq_flush_done(void *arg, uint32_t rxq_index)
159 {
160 	struct sfxge_evq *evq;
161 	struct sfxge_softc *sc;
162 	struct sfxge_rxq *rxq;
163 	unsigned int index;
164 	unsigned int label;
165 	uint16_t magic;
166 
167 	evq = (struct sfxge_evq *)arg;
168 	sc = evq->sc;
169 	rxq = sc->rxq[rxq_index];
170 
171 	KASSERT(rxq != NULL, ("rxq == NULL"));
172 
173 	/* Resend a software event on the correct queue */
174 	index = rxq->index;
175 	evq = sc->evq[index];
176 
177 	label = rxq_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;
181 
182 	KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
183 	    ("evq not started"));
184 	efx_ev_qpost(evq->common, magic);
185 
186 	return (B_FALSE);
187 }
188 
189 static boolean_t
190 sfxge_ev_rxq_flush_failed(void *arg, uint32_t rxq_index)
191 {
192 	struct sfxge_evq *evq;
193 	struct sfxge_softc *sc;
194 	struct sfxge_rxq *rxq;
195 	unsigned int index;
196 	unsigned int label;
197 	uint16_t magic;
198 
199 	evq = (struct sfxge_evq *)arg;
200 	sc = evq->sc;
201 	rxq = sc->rxq[rxq_index];
202 
203 	KASSERT(rxq != NULL, ("rxq == NULL"));
204 
205 	/* Resend a software event on the correct queue */
206 	index = rxq->index;
207 	evq = sc->evq[index];
208 
209 	label = rxq_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;
213 
214 	KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
215 	    ("evq not started"));
216 	efx_ev_qpost(evq->common, magic);
217 
218 	return (B_FALSE);
219 }
220 
221 static struct sfxge_txq *
222 sfxge_get_txq_by_label(struct sfxge_evq *evq, enum sfxge_txq_type label)
223 {
224 	unsigned int index;
225 
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];
230 }
231 
232 static boolean_t
233 sfxge_ev_tx(void *arg, uint32_t label, uint32_t id)
234 {
235 	struct sfxge_evq *evq;
236 	struct sfxge_txq *txq;
237 	unsigned int stop;
238 	unsigned int delta;
239 
240 	evq = (struct sfxge_evq *)arg;
241 	txq = sfxge_get_txq_by_label(evq, label);
242 
243 	KASSERT(txq != NULL, ("txq == NULL"));
244 	KASSERT(evq->index == txq->evq_index,
245 	    ("evq->index != txq->evq_index"));
246 
247 	if (txq->init_state != SFXGE_TXQ_STARTED)
248 		goto done;
249 
250 	stop = (id + 1) & (SFXGE_NDESCS - 1);
251 	id = txq->pending & (SFXGE_NDESCS - 1);
252 
253 	delta = (stop >= id) ? (stop - id) : (SFXGE_NDESCS - id + stop);
254 	txq->pending += delta;
255 
256 	evq->tx_done++;
257 
258 	if (txq->next == NULL &&
259 	    evq->txqs != &(txq->next)) {
260 		*(evq->txqs) = txq;
261 		evq->txqs = &(txq->next);
262 	}
263 
264 	if (txq->pending - txq->completed >= SFXGE_TX_BATCH)
265 		sfxge_tx_qcomplete(txq);
266 
267 done:
268 	return (evq->tx_done >= SFXGE_EV_BATCH);
269 }
270 
271 static boolean_t
272 sfxge_ev_txq_flush_done(void *arg, uint32_t txq_index)
273 {
274 	struct sfxge_evq *evq;
275 	struct sfxge_softc *sc;
276 	struct sfxge_txq *txq;
277 	unsigned int label;
278 	uint16_t magic;
279 
280 	evq = (struct sfxge_evq *)arg;
281 	sc = evq->sc;
282 	txq = sc->txq[txq_index];
283 
284 	KASSERT(txq != NULL, ("txq == NULL"));
285 	KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
286 	    ("txq not initialized"));
287 
288 	/* Resend a software event on the correct queue */
289 	evq = sc->evq[txq->evq_index];
290 
291 	label = txq->type;
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;
295 
296 	KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
297 	    ("evq not started"));
298 	efx_ev_qpost(evq->common, magic);
299 
300 	return (B_FALSE);
301 }
302 
303 static boolean_t
304 sfxge_ev_software(void *arg, uint16_t magic)
305 {
306 	struct sfxge_evq *evq;
307 	struct sfxge_softc *sc;
308 	unsigned int label;
309 
310 	evq = (struct sfxge_evq *)arg;
311 	sc = evq->sc;
312 
313 	label = magic & SFXGE_MAGIC_DMAQ_LABEL_MASK;
314 	magic &= ~SFXGE_MAGIC_DMAQ_LABEL_MASK;
315 
316 	switch (magic) {
317 	case SFXGE_MAGIC_RX_QFLUSH_DONE: {
318 		struct sfxge_rxq *rxq = sc->rxq[label];
319 
320 		KASSERT(rxq != NULL, ("rxq == NULL"));
321 		KASSERT(evq->index == rxq->index,
322 		    ("evq->index != rxq->index"));
323 
324 		sfxge_rx_qflush_done(rxq);
325 		break;
326 	}
327 	case SFXGE_MAGIC_RX_QFLUSH_FAILED: {
328 		struct sfxge_rxq *rxq = sc->rxq[label];
329 
330 		KASSERT(rxq != NULL, ("rxq == NULL"));
331 		KASSERT(evq->index == rxq->index,
332 		    ("evq->index != rxq->index"));
333 
334 		sfxge_rx_qflush_failed(rxq);
335 		break;
336 	}
337 	case SFXGE_MAGIC_RX_QREFILL: {
338 		struct sfxge_rxq *rxq = sc->rxq[label];
339 
340 		KASSERT(rxq != NULL, ("rxq == NULL"));
341 		KASSERT(evq->index == rxq->index,
342 		    ("evq->index != rxq->index"));
343 
344 		sfxge_rx_qrefill(rxq);
345 		break;
346 	}
347 	case SFXGE_MAGIC_TX_QFLUSH_DONE: {
348 		struct sfxge_txq *txq = sfxge_get_txq_by_label(evq, label);
349 
350 		KASSERT(txq != NULL, ("txq == NULL"));
351 		KASSERT(evq->index == txq->evq_index,
352 		    ("evq->index != txq->evq_index"));
353 
354 		sfxge_tx_qflush_done(txq);
355 		break;
356 	}
357 	default:
358 		break;
359 	}
360 
361 	return (B_FALSE);
362 }
363 
364 static boolean_t
365 sfxge_ev_sram(void *arg, uint32_t code)
366 {
367 	(void)arg;
368 	(void)code;
369 
370 	switch (code) {
371 	case EFX_SRAM_UPDATE:
372 		EFSYS_PROBE(sram_update);
373 		break;
374 
375 	case EFX_SRAM_CLEAR:
376 		EFSYS_PROBE(sram_clear);
377 		break;
378 
379 	case EFX_SRAM_ILLEGAL_CLEAR:
380 		EFSYS_PROBE(sram_illegal_clear);
381 		break;
382 
383 	default:
384 		KASSERT(B_FALSE, ("Impossible SRAM event"));
385 		break;
386 	}
387 
388 	return (B_FALSE);
389 }
390 
391 static boolean_t
392 sfxge_ev_timer(void *arg, uint32_t index)
393 {
394 	(void)arg;
395 	(void)index;
396 
397 	return (B_FALSE);
398 }
399 
400 static boolean_t
401 sfxge_ev_wake_up(void *arg, uint32_t index)
402 {
403 	(void)arg;
404 	(void)index;
405 
406 	return (B_FALSE);
407 }
408 
409 static void
410 sfxge_ev_stat_update(struct sfxge_softc *sc)
411 {
412 	struct sfxge_evq *evq;
413 	unsigned int index;
414 	clock_t now;
415 
416 	sx_xlock(&sc->softc_lock);
417 
418 	if (sc->evq[0]->init_state != SFXGE_EVQ_STARTED)
419 		goto out;
420 
421 	now = ticks;
422 	if (now - sc->ev_stats_update_time < hz)
423 		goto out;
424 
425 	sc->ev_stats_update_time = now;
426 
427 	/* Add event counts from each event queue in turn */
428 	for (index = 0; index < sc->intr.n_alloc; index++) {
429 		evq = sc->evq[index];
430 		mtx_lock(&evq->lock);
431 		efx_ev_qstats_update(evq->common, sc->ev_stats);
432 		mtx_unlock(&evq->lock);
433 	}
434 out:
435 	sx_xunlock(&sc->softc_lock);
436 }
437 
438 static int
439 sfxge_ev_stat_handler(SYSCTL_HANDLER_ARGS)
440 {
441 	struct sfxge_softc *sc = arg1;
442 	unsigned int id = arg2;
443 
444 	sfxge_ev_stat_update(sc);
445 
446 	return SYSCTL_OUT(req, &sc->ev_stats[id], sizeof(sc->ev_stats[id]));
447 }
448 
449 static void
450 sfxge_ev_stat_init(struct sfxge_softc *sc)
451 {
452 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
453 	struct sysctl_oid_list *stat_list;
454 	unsigned int id;
455 	char name[40];
456 
457 	stat_list = SYSCTL_CHILDREN(sc->stats_node);
458 
459 	for (id = 0; id < EV_NQSTATS; id++) {
460 		snprintf(name, sizeof(name), "ev_%s",
461 			 efx_ev_qstat_name(sc->enp, id));
462 		SYSCTL_ADD_PROC(
463 			ctx, stat_list,
464 			OID_AUTO, name, CTLTYPE_U64|CTLFLAG_RD,
465 			sc, id, sfxge_ev_stat_handler, "Q",
466 			"");
467 	}
468 }
469 
470 static void
471 sfxge_ev_qmoderate(struct sfxge_softc *sc, unsigned int idx, unsigned int us)
472 {
473 	struct sfxge_evq *evq;
474 	efx_evq_t *eep;
475 
476 	evq = sc->evq[idx];
477 	eep = evq->common;
478 
479 	KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
480 	    ("evq->init_state != SFXGE_EVQ_STARTED"));
481 
482 	(void)efx_ev_qmoderate(eep, us);
483 }
484 
485 static int
486 sfxge_int_mod_handler(SYSCTL_HANDLER_ARGS)
487 {
488 	struct sfxge_softc *sc = arg1;
489 	struct sfxge_intr *intr = &sc->intr;
490 	unsigned int moderation;
491 	int error;
492 	int index;
493 
494 	sx_xlock(&sc->softc_lock);
495 
496 	if (req->newptr) {
497 		if ((error = SYSCTL_IN(req, &moderation, sizeof(moderation)))
498 		    != 0)
499 			goto out;
500 
501 		/* We may not be calling efx_ev_qmoderate() now,
502 		 * so we have to range-check the value ourselves.
503 		 */
504 		if (moderation >
505 		    efx_nic_cfg_get(sc->enp)->enc_evq_moderation_max) {
506 			error = EINVAL;
507 			goto out;
508 		}
509 
510 		sc->ev_moderation = moderation;
511 		if (intr->state == SFXGE_INTR_STARTED) {
512 			for (index = 0; index < intr->n_alloc; index++)
513 				sfxge_ev_qmoderate(sc, index, moderation);
514 		}
515 	} else {
516 		error = SYSCTL_OUT(req, &sc->ev_moderation,
517 				   sizeof(sc->ev_moderation));
518 	}
519 
520 out:
521 	sx_xunlock(&sc->softc_lock);
522 
523 	return error;
524 }
525 
526 static boolean_t
527 sfxge_ev_initialized(void *arg)
528 {
529 	struct sfxge_evq *evq;
530 
531 	evq = (struct sfxge_evq *)arg;
532 
533 	KASSERT(evq->init_state == SFXGE_EVQ_STARTING,
534 	    ("evq not starting"));
535 
536 	evq->init_state = SFXGE_EVQ_STARTED;
537 
538 	return (0);
539 }
540 
541 static boolean_t
542 sfxge_ev_link_change(void *arg, efx_link_mode_t	link_mode)
543 {
544 	struct sfxge_evq *evq;
545 	struct sfxge_softc *sc;
546 
547 	evq = (struct sfxge_evq *)arg;
548 	sc = evq->sc;
549 
550 	sfxge_mac_link_update(sc, link_mode);
551 
552 	return (0);
553 }
554 
555 static const efx_ev_callbacks_t sfxge_ev_callbacks = {
556 	.eec_initialized	= sfxge_ev_initialized,
557 	.eec_rx			= sfxge_ev_rx,
558 	.eec_tx			= sfxge_ev_tx,
559 	.eec_exception		= sfxge_ev_exception,
560 	.eec_rxq_flush_done	= sfxge_ev_rxq_flush_done,
561 	.eec_rxq_flush_failed	= sfxge_ev_rxq_flush_failed,
562 	.eec_txq_flush_done	= sfxge_ev_txq_flush_done,
563 	.eec_software		= sfxge_ev_software,
564 	.eec_sram		= sfxge_ev_sram,
565 	.eec_wake_up		= sfxge_ev_wake_up,
566 	.eec_timer		= sfxge_ev_timer,
567 	.eec_link_change	= sfxge_ev_link_change,
568 };
569 
570 
571 int
572 sfxge_ev_qpoll(struct sfxge_softc *sc, unsigned int index)
573 {
574 	struct sfxge_evq *evq;
575 	int rc;
576 
577 	evq = sc->evq[index];
578 
579 	mtx_lock(&evq->lock);
580 
581 	if (evq->init_state != SFXGE_EVQ_STARTING &&
582 	    evq->init_state != SFXGE_EVQ_STARTED) {
583 		rc = EINVAL;
584 		goto fail;
585 	}
586 
587 	/* Synchronize the DMA memory for reading */
588 	bus_dmamap_sync(evq->mem.esm_tag, evq->mem.esm_map,
589 	    BUS_DMASYNC_POSTREAD);
590 
591 	KASSERT(evq->rx_done == 0, ("evq->rx_done != 0"));
592 	KASSERT(evq->tx_done == 0, ("evq->tx_done != 0"));
593 	KASSERT(evq->txq == NULL, ("evq->txq != NULL"));
594 	KASSERT(evq->txqs == &evq->txq, ("evq->txqs != &evq->txq"));
595 
596 	/* Poll the queue */
597 	efx_ev_qpoll(evq->common, &evq->read_ptr, &sfxge_ev_callbacks, evq);
598 
599 	evq->rx_done = 0;
600 	evq->tx_done = 0;
601 
602 	/* Perform any pending completion processing */
603 	sfxge_ev_qcomplete(evq, B_TRUE);
604 
605 	/* Re-prime the event queue for interrupts */
606 	if ((rc = efx_ev_qprime(evq->common, evq->read_ptr)) != 0)
607 		goto fail;
608 
609 	mtx_unlock(&evq->lock);
610 
611 	return (0);
612 
613 fail:
614 	mtx_unlock(&(evq->lock));
615 	return (rc);
616 }
617 
618 static void
619 sfxge_ev_qstop(struct sfxge_softc *sc, unsigned int index)
620 {
621 	struct sfxge_evq *evq;
622 
623 	evq = sc->evq[index];
624 
625 	KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
626 	    ("evq->init_state != SFXGE_EVQ_STARTED"));
627 
628 	mtx_lock(&evq->lock);
629 	evq->init_state = SFXGE_EVQ_INITIALIZED;
630 	evq->read_ptr = 0;
631 	evq->exception = B_FALSE;
632 
633 	/* Add event counts before discarding the common evq state */
634 	efx_ev_qstats_update(evq->common, sc->ev_stats);
635 
636 	efx_ev_qdestroy(evq->common);
637 	efx_sram_buf_tbl_clear(sc->enp, evq->buf_base_id,
638 	    EFX_EVQ_NBUFS(SFXGE_NEVS));
639 	mtx_unlock(&evq->lock);
640 }
641 
642 static int
643 sfxge_ev_qstart(struct sfxge_softc *sc, unsigned int index)
644 {
645 	struct sfxge_evq *evq;
646 	efsys_mem_t *esmp;
647 	int count;
648 	int rc;
649 
650 	evq = sc->evq[index];
651 	esmp = &evq->mem;
652 
653 	KASSERT(evq->init_state == SFXGE_EVQ_INITIALIZED,
654 	    ("evq->init_state != SFXGE_EVQ_INITIALIZED"));
655 
656 	/* Clear all events. */
657 	(void)memset(esmp->esm_base, 0xff, EFX_EVQ_SIZE(SFXGE_NEVS));
658 
659 	/* Program the buffer table. */
660 	if ((rc = efx_sram_buf_tbl_set(sc->enp, evq->buf_base_id, esmp,
661 	    EFX_EVQ_NBUFS(SFXGE_NEVS))) != 0)
662 		return rc;
663 
664 	/* Create the common code event queue. */
665 	if ((rc = efx_ev_qcreate(sc->enp, index, esmp, SFXGE_NEVS,
666 	    evq->buf_base_id, &evq->common)) != 0)
667 		goto fail;
668 
669 	mtx_lock(&evq->lock);
670 
671 	/* Set the default moderation */
672 	(void)efx_ev_qmoderate(evq->common, sc->ev_moderation);
673 
674 	/* Prime the event queue for interrupts */
675 	if ((rc = efx_ev_qprime(evq->common, evq->read_ptr)) != 0)
676 		goto fail2;
677 
678 	evq->init_state = SFXGE_EVQ_STARTING;
679 
680 	mtx_unlock(&evq->lock);
681 
682 	/* Wait for the initialization event */
683 	count = 0;
684 	do {
685 		/* Pause for 100 ms */
686 		pause("sfxge evq init", hz / 10);
687 
688 		/* Check to see if the test event has been processed */
689 		if (evq->init_state == SFXGE_EVQ_STARTED)
690 			goto done;
691 
692 	} while (++count < 20);
693 
694 	rc = ETIMEDOUT;
695 	goto fail3;
696 
697 done:
698 	return (0);
699 
700 fail3:
701 	mtx_lock(&evq->lock);
702 	evq->init_state = SFXGE_EVQ_INITIALIZED;
703 fail2:
704 	mtx_unlock(&evq->lock);
705 	efx_ev_qdestroy(evq->common);
706 fail:
707 	efx_sram_buf_tbl_clear(sc->enp, evq->buf_base_id,
708 	    EFX_EVQ_NBUFS(SFXGE_NEVS));
709 
710 	return (rc);
711 }
712 
713 void
714 sfxge_ev_stop(struct sfxge_softc *sc)
715 {
716 	struct sfxge_intr *intr;
717 	efx_nic_t *enp;
718 	int index;
719 
720 	intr = &sc->intr;
721 	enp = sc->enp;
722 
723 	KASSERT(intr->state == SFXGE_INTR_STARTED,
724 	    ("Interrupts not started"));
725 
726 	/* Stop the event queue(s) */
727 	index = intr->n_alloc;
728 	while (--index >= 0)
729 		sfxge_ev_qstop(sc, index);
730 
731 	/* Tear down the event module */
732 	efx_ev_fini(enp);
733 }
734 
735 int
736 sfxge_ev_start(struct sfxge_softc *sc)
737 {
738 	struct sfxge_intr *intr;
739 	int index;
740 	int rc;
741 
742 	intr = &sc->intr;
743 
744 	KASSERT(intr->state == SFXGE_INTR_STARTED,
745 	    ("intr->state != SFXGE_INTR_STARTED"));
746 
747 	/* Initialize the event module */
748 	if ((rc = efx_ev_init(sc->enp)) != 0)
749 		return rc;
750 
751 	/* Start the event queues */
752 	for (index = 0; index < intr->n_alloc; index++) {
753 		if ((rc = sfxge_ev_qstart(sc, index)) != 0)
754 			goto fail;
755 	}
756 
757 	return (0);
758 
759 fail:
760 	/* Stop the event queue(s) */
761 	while (--index >= 0)
762 		sfxge_ev_qstop(sc, index);
763 
764 	/* Tear down the event module */
765 	efx_ev_fini(sc->enp);
766 
767 	return (rc);
768 }
769 
770 static void
771 sfxge_ev_qfini(struct sfxge_softc *sc, unsigned int index)
772 {
773 	struct sfxge_evq *evq;
774 
775 	evq = sc->evq[index];
776 
777 	KASSERT(evq->init_state == SFXGE_EVQ_INITIALIZED,
778 	    ("evq->init_state != SFXGE_EVQ_INITIALIZED"));
779 	KASSERT(evq->txqs == &evq->txq, ("evq->txqs != &evq->txq"));
780 
781 	sfxge_dma_free(&evq->mem);
782 
783 	sc->evq[index] = NULL;
784 
785 	mtx_destroy(&evq->lock);
786 
787 	free(evq, M_SFXGE);
788 }
789 
790 static int
791 sfxge_ev_qinit(struct sfxge_softc *sc, unsigned int index)
792 {
793 	struct sfxge_evq *evq;
794 	efsys_mem_t *esmp;
795 	int rc;
796 
797 	KASSERT(index < SFXGE_RX_SCALE_MAX, ("index >= SFXGE_RX_SCALE_MAX"));
798 
799 	evq = malloc(sizeof(struct sfxge_evq), M_SFXGE, M_ZERO | M_WAITOK);
800 	evq->sc = sc;
801 	evq->index = index;
802 	sc->evq[index] = evq;
803 	esmp = &evq->mem;
804 
805 	/* Initialise TX completion list */
806 	evq->txqs = &evq->txq;
807 
808 	/* Allocate DMA space. */
809 	if ((rc = sfxge_dma_alloc(sc, EFX_EVQ_SIZE(SFXGE_NEVS), esmp)) != 0)
810 		return (rc);
811 
812 	/* Allocate buffer table entries. */
813 	sfxge_sram_buf_tbl_alloc(sc, EFX_EVQ_NBUFS(SFXGE_NEVS),
814 				 &evq->buf_base_id);
815 
816 	mtx_init(&evq->lock, "evq", NULL, MTX_DEF);
817 
818 	evq->init_state = SFXGE_EVQ_INITIALIZED;
819 
820 	return (0);
821 }
822 
823 void
824 sfxge_ev_fini(struct sfxge_softc *sc)
825 {
826 	struct sfxge_intr *intr;
827 	int index;
828 
829 	intr = &sc->intr;
830 
831 	KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
832 	    ("intr->state != SFXGE_INTR_INITIALIZED"));
833 
834 	sc->ev_moderation = 0;
835 
836 	/* Tear down the event queue(s). */
837 	index = intr->n_alloc;
838 	while (--index >= 0)
839 		sfxge_ev_qfini(sc, index);
840 }
841 
842 int
843 sfxge_ev_init(struct sfxge_softc *sc)
844 {
845 	struct sysctl_ctx_list *sysctl_ctx = device_get_sysctl_ctx(sc->dev);
846 	struct sysctl_oid *sysctl_tree = device_get_sysctl_tree(sc->dev);
847 	struct sfxge_intr *intr;
848 	int index;
849 	int rc;
850 
851 	intr = &sc->intr;
852 
853 	KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
854 	    ("intr->state != SFXGE_INTR_INITIALIZED"));
855 
856 	/* Set default interrupt moderation; add a sysctl to
857 	 * read and change it.
858 	 */
859 	sc->ev_moderation = 30;
860 	SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
861 			OID_AUTO, "int_mod", CTLTYPE_UINT|CTLFLAG_RW,
862 			sc, 0, sfxge_int_mod_handler, "IU",
863 			"sfxge interrupt moderation (us)");
864 
865 	/*
866 	 * Initialize the event queue(s) - one per interrupt.
867 	 */
868 	for (index = 0; index < intr->n_alloc; index++) {
869 		if ((rc = sfxge_ev_qinit(sc, index)) != 0)
870 			goto fail;
871 	}
872 
873 	sfxge_ev_stat_init(sc);
874 
875 	return (0);
876 
877 fail:
878 	while (--index >= 0)
879 		sfxge_ev_qfini(sc, index);
880 
881 	return (rc);
882 }
883