xref: /freebsd/sys/dev/sfxge/sfxge_ev.c (revision edca4938f74db18d091868237592abbf7e718669)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2010-2016 Solarflare Communications Inc.
5  * All rights reserved.
6  *
7  * This software was developed in part by Philip Paeps under contract for
8  * Solarflare Communications, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * The views and conclusions contained in the software and documentation are
32  * those of the authors and should not be interpreted as representing official
33  * policies, either expressed or implied, of the FreeBSD Project.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/param.h>
43 #include <sys/queue.h>
44 #include <sys/systm.h>
45 #include <sys/taskqueue.h>
46 
47 #include "common/efx.h"
48 
49 #include "sfxge.h"
50 
51 static void
52 sfxge_ev_qcomplete(struct sfxge_evq *evq, boolean_t eop)
53 {
54 	struct sfxge_softc *sc;
55 	unsigned int index;
56 	struct sfxge_rxq *rxq;
57 	struct sfxge_txq *txq;
58 
59 	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
60 
61 	sc = evq->sc;
62 	index = evq->index;
63 	rxq = sc->rxq[index];
64 
65 	if ((txq = evq->txq) != NULL) {
66 		evq->txq = NULL;
67 		evq->txqs = &(evq->txq);
68 
69 		do {
70 			struct sfxge_txq *next;
71 
72 			next = txq->next;
73 			txq->next = NULL;
74 
75 			KASSERT(txq->evq_index == index,
76 			    ("txq->evq_index != index"));
77 
78 			if (txq->pending != txq->completed)
79 				sfxge_tx_qcomplete(txq, evq);
80 
81 			txq = next;
82 		} while (txq != NULL);
83 	}
84 
85 	if (rxq->pending != rxq->completed)
86 		sfxge_rx_qcomplete(rxq, eop);
87 }
88 
89 static struct sfxge_rxq *
90 sfxge_get_rxq_by_label(struct sfxge_evq *evq, uint32_t label)
91 {
92 	struct sfxge_rxq *rxq;
93 
94 	KASSERT(label == 0, ("unexpected rxq label != 0"));
95 
96 	rxq = evq->sc->rxq[evq->index];
97 
98 	KASSERT(rxq != NULL, ("rxq == NULL"));
99 	KASSERT(evq->index == rxq->index, ("evq->index != rxq->index"));
100 
101 	return (rxq);
102 }
103 
104 static boolean_t
105 sfxge_ev_rx(void *arg, uint32_t label, uint32_t id, uint32_t size,
106 	    uint16_t flags)
107 {
108 	struct sfxge_evq *evq;
109 	struct sfxge_softc *sc;
110 	struct sfxge_rxq *rxq;
111 	unsigned int stop;
112 	unsigned int delta;
113 	struct sfxge_rx_sw_desc *rx_desc;
114 
115 	evq = arg;
116 	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
117 
118 	sc = evq->sc;
119 
120 	if (evq->exception)
121 		goto done;
122 
123 	rxq = sfxge_get_rxq_by_label(evq, label);
124 	if (__predict_false(rxq->init_state != SFXGE_RXQ_STARTED))
125 		goto done;
126 
127 	stop = (id + 1) & rxq->ptr_mask;
128 	id = rxq->pending & rxq->ptr_mask;
129 	delta = (stop >= id) ? (stop - id) : (rxq->entries - id + stop);
130 	rxq->pending += delta;
131 
132 	if (delta != 1) {
133 		if ((delta <= 0) ||
134 		    (delta > efx_nic_cfg_get(sc->enp)->enc_rx_batch_max)) {
135 			evq->exception = B_TRUE;
136 
137 			device_printf(sc->dev, "RX completion out of order"
138 						  " (id=%#x delta=%u flags=%#x); resetting\n",
139 						  id, delta, flags);
140 			sfxge_schedule_reset(sc);
141 
142 			goto done;
143 		}
144 	}
145 
146 	rx_desc = &rxq->queue[id];
147 
148 	prefetch_read_many(rx_desc->mbuf);
149 
150 	for (; id != stop; id = (id + 1) & rxq->ptr_mask) {
151 		rx_desc = &rxq->queue[id];
152 		KASSERT(rx_desc->flags == EFX_DISCARD,
153 				("rx_desc->flags != EFX_DISCARD"));
154 		rx_desc->flags = flags;
155 
156 		KASSERT(size < (1 << 16), ("size > (1 << 16)"));
157 		rx_desc->size = (uint16_t)size;
158 	}
159 
160 	evq->rx_done++;
161 
162 	if (rxq->pending - rxq->completed >= SFXGE_RX_BATCH)
163 		sfxge_ev_qcomplete(evq, B_FALSE);
164 
165 done:
166 	return (evq->rx_done >= SFXGE_EV_BATCH);
167 }
168 
169 static boolean_t
170 sfxge_ev_exception(void *arg, uint32_t code, uint32_t data)
171 {
172 	struct sfxge_evq *evq;
173 	struct sfxge_softc *sc;
174 
175 	evq = (struct sfxge_evq *)arg;
176 	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
177 
178 	sc = evq->sc;
179 
180 	DBGPRINT(sc->dev, "[%d] %s", evq->index,
181 			  (code == EFX_EXCEPTION_RX_RECOVERY) ? "RX_RECOVERY" :
182 			  (code == EFX_EXCEPTION_RX_DSC_ERROR) ? "RX_DSC_ERROR" :
183 			  (code == EFX_EXCEPTION_TX_DSC_ERROR) ? "TX_DSC_ERROR" :
184 			  (code == EFX_EXCEPTION_UNKNOWN_SENSOREVT) ? "UNKNOWN_SENSOREVT" :
185 			  (code == EFX_EXCEPTION_FWALERT_SRAM) ? "FWALERT_SRAM" :
186 			  (code == EFX_EXCEPTION_UNKNOWN_FWALERT) ? "UNKNOWN_FWALERT" :
187 			  (code == EFX_EXCEPTION_RX_ERROR) ? "RX_ERROR" :
188 			  (code == EFX_EXCEPTION_TX_ERROR) ? "TX_ERROR" :
189 			  (code == EFX_EXCEPTION_EV_ERROR) ? "EV_ERROR" :
190 			  "UNKNOWN");
191 
192 	evq->exception = B_TRUE;
193 
194 	if (code != EFX_EXCEPTION_UNKNOWN_SENSOREVT) {
195 		device_printf(sc->dev,
196 			      "hardware exception (code=%u); resetting\n",
197 			      code);
198 		sfxge_schedule_reset(sc);
199 	}
200 
201 	return (B_FALSE);
202 }
203 
204 static boolean_t
205 sfxge_ev_rxq_flush_done(void *arg, uint32_t rxq_index)
206 {
207 	struct sfxge_evq *evq;
208 	struct sfxge_softc *sc;
209 	struct sfxge_rxq *rxq;
210 	unsigned int index;
211 	uint16_t magic;
212 
213 	evq = (struct sfxge_evq *)arg;
214 	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
215 
216 	sc = evq->sc;
217 	rxq = sc->rxq[rxq_index];
218 
219 	KASSERT(rxq != NULL, ("rxq == NULL"));
220 
221 	/* Resend a software event on the correct queue */
222 	index = rxq->index;
223 	if (index == evq->index) {
224 		sfxge_rx_qflush_done(rxq);
225 		return (B_FALSE);
226 	}
227 
228 	evq = sc->evq[index];
229 	magic = sfxge_sw_ev_rxq_magic(SFXGE_SW_EV_RX_QFLUSH_DONE, rxq);
230 
231 	KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
232 	    ("evq not started"));
233 	efx_ev_qpost(evq->common, magic);
234 
235 	return (B_FALSE);
236 }
237 
238 static boolean_t
239 sfxge_ev_rxq_flush_failed(void *arg, uint32_t rxq_index)
240 {
241 	struct sfxge_evq *evq;
242 	struct sfxge_softc *sc;
243 	struct sfxge_rxq *rxq;
244 	unsigned int index;
245 	uint16_t magic;
246 
247 	evq = (struct sfxge_evq *)arg;
248 	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
249 
250 	sc = evq->sc;
251 	rxq = sc->rxq[rxq_index];
252 
253 	KASSERT(rxq != NULL, ("rxq == NULL"));
254 
255 	/* Resend a software event on the correct queue */
256 	index = rxq->index;
257 	evq = sc->evq[index];
258 	magic = sfxge_sw_ev_rxq_magic(SFXGE_SW_EV_RX_QFLUSH_FAILED, rxq);
259 
260 	KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
261 	    ("evq not started"));
262 	efx_ev_qpost(evq->common, magic);
263 
264 	return (B_FALSE);
265 }
266 
267 static struct sfxge_txq *
268 sfxge_get_txq_by_label(struct sfxge_evq *evq, enum sfxge_txq_type label)
269 {
270 	unsigned int index;
271 
272 	KASSERT((evq->sc->txq_dynamic_cksum_toggle_supported) ? (label == 0) :
273 		((evq->index == 0 && label < SFXGE_TXQ_NTYPES) ||
274 		 (label == SFXGE_TXQ_IP_TCP_UDP_CKSUM)),
275 		("unexpected txq label"));
276 
277 	index = (evq->index == 0) ?
278 		label : (evq->index - 1 + SFXGE_EVQ0_N_TXQ(evq->sc));
279 	return (evq->sc->txq[index]);
280 }
281 
282 static boolean_t
283 sfxge_ev_tx(void *arg, uint32_t label, uint32_t id)
284 {
285 	struct sfxge_evq *evq;
286 	struct sfxge_txq *txq;
287 	unsigned int stop;
288 	unsigned int delta;
289 
290 	evq = (struct sfxge_evq *)arg;
291 	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
292 
293 	txq = sfxge_get_txq_by_label(evq, label);
294 
295 	KASSERT(txq != NULL, ("txq == NULL"));
296 	KASSERT(evq->index == txq->evq_index,
297 	    ("evq->index != txq->evq_index"));
298 
299 	if (__predict_false(txq->init_state != SFXGE_TXQ_STARTED))
300 		goto done;
301 
302 	stop = (id + 1) & txq->ptr_mask;
303 	id = txq->pending & txq->ptr_mask;
304 
305 	delta = (stop >= id) ? (stop - id) : (txq->entries - id + stop);
306 	txq->pending += delta;
307 
308 	evq->tx_done++;
309 
310 	if (txq->next == NULL &&
311 	    evq->txqs != &(txq->next)) {
312 		*(evq->txqs) = txq;
313 		evq->txqs = &(txq->next);
314 	}
315 
316 	if (txq->pending - txq->completed >= SFXGE_TX_BATCH)
317 		sfxge_tx_qcomplete(txq, evq);
318 
319 done:
320 	return (evq->tx_done >= SFXGE_EV_BATCH);
321 }
322 
323 static boolean_t
324 sfxge_ev_txq_flush_done(void *arg, uint32_t txq_index)
325 {
326 	struct sfxge_evq *evq;
327 	struct sfxge_softc *sc;
328 	struct sfxge_txq *txq;
329 	uint16_t magic;
330 
331 	evq = (struct sfxge_evq *)arg;
332 	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
333 
334 	sc = evq->sc;
335 	txq = sc->txq[txq_index];
336 
337 	KASSERT(txq != NULL, ("txq == NULL"));
338 	KASSERT(txq->init_state == SFXGE_TXQ_INITIALIZED,
339 	    ("txq not initialized"));
340 
341 	if (txq->evq_index == evq->index) {
342 		sfxge_tx_qflush_done(txq);
343 		return (B_FALSE);
344 	}
345 
346 	/* Resend a software event on the correct queue */
347 	evq = sc->evq[txq->evq_index];
348 	magic = sfxge_sw_ev_txq_magic(SFXGE_SW_EV_TX_QFLUSH_DONE, txq);
349 
350 	KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
351 	    ("evq not started"));
352 	efx_ev_qpost(evq->common, magic);
353 
354 	return (B_FALSE);
355 }
356 
357 static boolean_t
358 sfxge_ev_software(void *arg, uint16_t magic)
359 {
360 	struct sfxge_evq *evq;
361 	struct sfxge_softc *sc;
362 	unsigned int label;
363 
364 	evq = (struct sfxge_evq *)arg;
365 	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
366 
367 	sc = evq->sc;
368 
369 	label = magic & SFXGE_MAGIC_DMAQ_LABEL_MASK;
370 	magic &= ~SFXGE_MAGIC_DMAQ_LABEL_MASK;
371 
372 	switch (magic) {
373 	case SFXGE_SW_EV_MAGIC(SFXGE_SW_EV_RX_QFLUSH_DONE):
374 		sfxge_rx_qflush_done(sfxge_get_rxq_by_label(evq, label));
375 		break;
376 
377 	case SFXGE_SW_EV_MAGIC(SFXGE_SW_EV_RX_QFLUSH_FAILED):
378 		sfxge_rx_qflush_failed(sfxge_get_rxq_by_label(evq, label));
379 		break;
380 
381 	case SFXGE_SW_EV_MAGIC(SFXGE_SW_EV_RX_QREFILL):
382 		sfxge_rx_qrefill(sfxge_get_rxq_by_label(evq, label));
383 		break;
384 
385 	case SFXGE_SW_EV_MAGIC(SFXGE_SW_EV_TX_QFLUSH_DONE): {
386 		struct sfxge_txq *txq = sfxge_get_txq_by_label(evq, label);
387 
388 		KASSERT(txq != NULL, ("txq == NULL"));
389 		KASSERT(evq->index == txq->evq_index,
390 		    ("evq->index != txq->evq_index"));
391 
392 		sfxge_tx_qflush_done(txq);
393 		break;
394 	}
395 	default:
396 		break;
397 	}
398 
399 	return (B_FALSE);
400 }
401 
402 static boolean_t
403 sfxge_ev_sram(void *arg, uint32_t code)
404 {
405 	(void)arg;
406 	(void)code;
407 
408 	switch (code) {
409 	case EFX_SRAM_UPDATE:
410 		EFSYS_PROBE(sram_update);
411 		break;
412 
413 	case EFX_SRAM_CLEAR:
414 		EFSYS_PROBE(sram_clear);
415 		break;
416 
417 	case EFX_SRAM_ILLEGAL_CLEAR:
418 		EFSYS_PROBE(sram_illegal_clear);
419 		break;
420 
421 	default:
422 		KASSERT(B_FALSE, ("Impossible SRAM event"));
423 		break;
424 	}
425 
426 	return (B_FALSE);
427 }
428 
429 static boolean_t
430 sfxge_ev_timer(void *arg, uint32_t index)
431 {
432 	(void)arg;
433 	(void)index;
434 
435 	return (B_FALSE);
436 }
437 
438 static boolean_t
439 sfxge_ev_wake_up(void *arg, uint32_t index)
440 {
441 	(void)arg;
442 	(void)index;
443 
444 	return (B_FALSE);
445 }
446 
447 #if EFSYS_OPT_QSTATS
448 
449 static void
450 sfxge_evq_stat_update(struct sfxge_evq *evq)
451 {
452 	clock_t now;
453 
454 	SFXGE_EVQ_LOCK(evq);
455 
456 	if (__predict_false(evq->init_state != SFXGE_EVQ_STARTED))
457 		goto out;
458 
459 	now = ticks;
460 	if ((unsigned int)(now - evq->stats_update_time) < (unsigned int)hz)
461 		goto out;
462 
463 	evq->stats_update_time = now;
464 	efx_ev_qstats_update(evq->common, evq->stats);
465 
466 out:
467 	SFXGE_EVQ_UNLOCK(evq);
468 }
469 
470 static int
471 sfxge_evq_stat_handler(SYSCTL_HANDLER_ARGS)
472 {
473 	struct sfxge_evq *evq = arg1;
474 	struct sfxge_softc *sc = evq->sc;
475 	unsigned int id = arg2;
476 
477 	SFXGE_ADAPTER_LOCK(sc);
478 
479 	sfxge_evq_stat_update(evq);
480 
481 	SFXGE_ADAPTER_UNLOCK(sc);
482 
483 	return (SYSCTL_OUT(req, &evq->stats[id], sizeof(evq->stats[id])));
484 }
485 
486 static int
487 sfxge_evq_stat_init(struct sfxge_evq *evq)
488 {
489 	struct sfxge_softc *sc = evq->sc;
490 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
491 	char name[16];
492 	struct sysctl_oid *evq_stats_node;
493 	unsigned int id;
494 
495 	snprintf(name, sizeof(name), "%u", evq->index);
496 	evq_stats_node = SYSCTL_ADD_NODE(ctx,
497 					 SYSCTL_CHILDREN(sc->evqs_stats_node),
498 					 OID_AUTO, name, CTLFLAG_RD, NULL, "");
499 	if (evq_stats_node == NULL)
500 		return (ENOMEM);
501 
502 	for (id = 0; id < EV_NQSTATS; id++) {
503 		SYSCTL_ADD_PROC(
504 			ctx, SYSCTL_CHILDREN(evq_stats_node),
505 			OID_AUTO, efx_ev_qstat_name(sc->enp, id),
506 			CTLTYPE_U64|CTLFLAG_RD,
507 			evq, id, sfxge_evq_stat_handler, "Q",
508 			"");
509 	}
510 
511 	return (0);
512 }
513 
514 static void
515 sfxge_ev_stat_update(struct sfxge_softc *sc)
516 {
517 	struct sfxge_evq *evq;
518 	unsigned int index;
519 	clock_t now;
520 	unsigned int id;
521 
522 	SFXGE_ADAPTER_LOCK(sc);
523 
524 	now = ticks;
525 	if ((unsigned int)(now - sc->ev_stats_update_time) < (unsigned int)hz)
526 		goto out;
527 
528 	sc->ev_stats_update_time = now;
529 
530 	memset(sc->ev_stats, 0, sizeof(sc->ev_stats));
531 
532 	/* Update and add event counts from each event queue in turn */
533 	for (index = 0; index < sc->evq_count; index++) {
534 		evq = sc->evq[index];
535 		sfxge_evq_stat_update(evq);
536 		for (id = 0; id < EV_NQSTATS; id++)
537 			sc->ev_stats[id] += evq->stats[id];
538 	}
539 out:
540 	SFXGE_ADAPTER_UNLOCK(sc);
541 }
542 
543 static int
544 sfxge_ev_stat_handler(SYSCTL_HANDLER_ARGS)
545 {
546 	struct sfxge_softc *sc = arg1;
547 	unsigned int id = arg2;
548 
549 	sfxge_ev_stat_update(sc);
550 
551 	return (SYSCTL_OUT(req, &sc->ev_stats[id], sizeof(sc->ev_stats[id])));
552 }
553 
554 static void
555 sfxge_ev_stat_init(struct sfxge_softc *sc)
556 {
557 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
558 	struct sysctl_oid_list *stat_list;
559 	unsigned int id;
560 	char name[40];
561 
562 	stat_list = SYSCTL_CHILDREN(sc->stats_node);
563 
564 	for (id = 0; id < EV_NQSTATS; id++) {
565 		snprintf(name, sizeof(name), "ev_%s",
566 			 efx_ev_qstat_name(sc->enp, id));
567 		SYSCTL_ADD_PROC(
568 			ctx, stat_list,
569 			OID_AUTO, name, CTLTYPE_U64|CTLFLAG_RD,
570 			sc, id, sfxge_ev_stat_handler, "Q",
571 			"");
572 	}
573 }
574 
575 #endif /* EFSYS_OPT_QSTATS */
576 
577 static void
578 sfxge_ev_qmoderate(struct sfxge_softc *sc, unsigned int idx, unsigned int us)
579 {
580 	struct sfxge_evq *evq;
581 	efx_evq_t *eep;
582 
583 	evq = sc->evq[idx];
584 	eep = evq->common;
585 
586 	KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
587 	    ("evq->init_state != SFXGE_EVQ_STARTED"));
588 
589 	(void)efx_ev_qmoderate(eep, us);
590 }
591 
592 static int
593 sfxge_int_mod_handler(SYSCTL_HANDLER_ARGS)
594 {
595 	struct sfxge_softc *sc = arg1;
596 	struct sfxge_intr *intr = &sc->intr;
597 	unsigned int moderation;
598 	int error;
599 	unsigned int index;
600 
601 	SFXGE_ADAPTER_LOCK(sc);
602 
603 	if (req->newptr != NULL) {
604 		if ((error = SYSCTL_IN(req, &moderation, sizeof(moderation)))
605 		    != 0)
606 			goto out;
607 
608 		/* We may not be calling efx_ev_qmoderate() now,
609 		 * so we have to range-check the value ourselves.
610 		 */
611 		if (moderation >
612 		    efx_nic_cfg_get(sc->enp)->enc_evq_timer_max_us) {
613 			error = EINVAL;
614 			goto out;
615 		}
616 
617 		sc->ev_moderation = moderation;
618 		if (intr->state == SFXGE_INTR_STARTED) {
619 			for (index = 0; index < sc->evq_count; index++)
620 				sfxge_ev_qmoderate(sc, index, moderation);
621 		}
622 	} else {
623 		error = SYSCTL_OUT(req, &sc->ev_moderation,
624 				   sizeof(sc->ev_moderation));
625 	}
626 
627 out:
628 	SFXGE_ADAPTER_UNLOCK(sc);
629 
630 	return (error);
631 }
632 
633 static boolean_t
634 sfxge_ev_initialized(void *arg)
635 {
636 	struct sfxge_evq *evq;
637 
638 	evq = (struct sfxge_evq *)arg;
639 	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
640 
641 	/* Init done events may be duplicated on 7xxx */
642 	KASSERT(evq->init_state == SFXGE_EVQ_STARTING ||
643 		evq->init_state == SFXGE_EVQ_STARTED,
644 	    ("evq not starting"));
645 
646 	evq->init_state = SFXGE_EVQ_STARTED;
647 
648 	return (0);
649 }
650 
651 static boolean_t
652 sfxge_ev_link_change(void *arg, efx_link_mode_t	link_mode)
653 {
654 	struct sfxge_evq *evq;
655 	struct sfxge_softc *sc;
656 
657 	evq = (struct sfxge_evq *)arg;
658 	SFXGE_EVQ_LOCK_ASSERT_OWNED(evq);
659 
660 	sc = evq->sc;
661 
662 	sfxge_mac_link_update(sc, link_mode);
663 
664 	return (0);
665 }
666 
667 static const efx_ev_callbacks_t sfxge_ev_callbacks = {
668 	.eec_initialized	= sfxge_ev_initialized,
669 	.eec_rx			= sfxge_ev_rx,
670 	.eec_tx			= sfxge_ev_tx,
671 	.eec_exception		= sfxge_ev_exception,
672 	.eec_rxq_flush_done	= sfxge_ev_rxq_flush_done,
673 	.eec_rxq_flush_failed	= sfxge_ev_rxq_flush_failed,
674 	.eec_txq_flush_done	= sfxge_ev_txq_flush_done,
675 	.eec_software		= sfxge_ev_software,
676 	.eec_sram		= sfxge_ev_sram,
677 	.eec_wake_up		= sfxge_ev_wake_up,
678 	.eec_timer		= sfxge_ev_timer,
679 	.eec_link_change	= sfxge_ev_link_change,
680 };
681 
682 
683 int
684 sfxge_ev_qpoll(struct sfxge_evq *evq)
685 {
686 	int rc;
687 
688 	SFXGE_EVQ_LOCK(evq);
689 
690 	if (__predict_false(evq->init_state != SFXGE_EVQ_STARTING &&
691 			    evq->init_state != SFXGE_EVQ_STARTED)) {
692 		rc = EINVAL;
693 		goto fail;
694 	}
695 
696 	/* Synchronize the DMA memory for reading */
697 	bus_dmamap_sync(evq->mem.esm_tag, evq->mem.esm_map,
698 	    BUS_DMASYNC_POSTREAD);
699 
700 	KASSERT(evq->rx_done == 0, ("evq->rx_done != 0"));
701 	KASSERT(evq->tx_done == 0, ("evq->tx_done != 0"));
702 	KASSERT(evq->txq == NULL, ("evq->txq != NULL"));
703 	KASSERT(evq->txqs == &evq->txq, ("evq->txqs != &evq->txq"));
704 
705 	/* Poll the queue */
706 	efx_ev_qpoll(evq->common, &evq->read_ptr, &sfxge_ev_callbacks, evq);
707 
708 	evq->rx_done = 0;
709 	evq->tx_done = 0;
710 
711 	/* Perform any pending completion processing */
712 	sfxge_ev_qcomplete(evq, B_TRUE);
713 
714 	/* Re-prime the event queue for interrupts */
715 	if ((rc = efx_ev_qprime(evq->common, evq->read_ptr)) != 0)
716 		goto fail;
717 
718 	SFXGE_EVQ_UNLOCK(evq);
719 
720 	return (0);
721 
722 fail:
723 	SFXGE_EVQ_UNLOCK(evq);
724 	return (rc);
725 }
726 
727 static void
728 sfxge_ev_qstop(struct sfxge_softc *sc, unsigned int index)
729 {
730 	struct sfxge_evq *evq;
731 
732 	evq = sc->evq[index];
733 
734 	KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
735 	    ("evq->init_state != SFXGE_EVQ_STARTED"));
736 
737 	SFXGE_EVQ_LOCK(evq);
738 	evq->init_state = SFXGE_EVQ_INITIALIZED;
739 	evq->read_ptr = 0;
740 	evq->exception = B_FALSE;
741 
742 #if EFSYS_OPT_QSTATS
743 	/* Add event counts before discarding the common evq state */
744 	efx_ev_qstats_update(evq->common, evq->stats);
745 #endif
746 
747 	efx_ev_qdestroy(evq->common);
748 	efx_sram_buf_tbl_clear(sc->enp, evq->buf_base_id,
749 	    EFX_EVQ_NBUFS(evq->entries));
750 	SFXGE_EVQ_UNLOCK(evq);
751 }
752 
753 static int
754 sfxge_ev_qstart(struct sfxge_softc *sc, unsigned int index)
755 {
756 	struct sfxge_evq *evq;
757 	efsys_mem_t *esmp;
758 	int count;
759 	int rc;
760 
761 	evq = sc->evq[index];
762 	esmp = &evq->mem;
763 
764 	KASSERT(evq->init_state == SFXGE_EVQ_INITIALIZED,
765 	    ("evq->init_state != SFXGE_EVQ_INITIALIZED"));
766 
767 	/* Clear all events. */
768 	(void)memset(esmp->esm_base, 0xff, EFX_EVQ_SIZE(evq->entries));
769 
770 	/* Program the buffer table. */
771 	if ((rc = efx_sram_buf_tbl_set(sc->enp, evq->buf_base_id, esmp,
772 	    EFX_EVQ_NBUFS(evq->entries))) != 0)
773 		return (rc);
774 
775 	/* Create the common code event queue. */
776 	if ((rc = efx_ev_qcreate(sc->enp, index, esmp, evq->entries,
777 	    evq->buf_base_id, sc->ev_moderation, EFX_EVQ_FLAGS_TYPE_AUTO,
778 	    &evq->common)) != 0)
779 		goto fail;
780 
781 	SFXGE_EVQ_LOCK(evq);
782 
783 	/* Prime the event queue for interrupts */
784 	if ((rc = efx_ev_qprime(evq->common, evq->read_ptr)) != 0)
785 		goto fail2;
786 
787 	evq->init_state = SFXGE_EVQ_STARTING;
788 
789 	SFXGE_EVQ_UNLOCK(evq);
790 
791 	/* Wait for the initialization event */
792 	count = 0;
793 	do {
794 		/* Pause for 100 ms */
795 		pause("sfxge evq init", hz / 10);
796 
797 		/* Check to see if the test event has been processed */
798 		if (evq->init_state == SFXGE_EVQ_STARTED)
799 			goto done;
800 
801 	} while (++count < 20);
802 
803 	rc = ETIMEDOUT;
804 	goto fail3;
805 
806 done:
807 	return (0);
808 
809 fail3:
810 	SFXGE_EVQ_LOCK(evq);
811 	evq->init_state = SFXGE_EVQ_INITIALIZED;
812 fail2:
813 	SFXGE_EVQ_UNLOCK(evq);
814 	efx_ev_qdestroy(evq->common);
815 fail:
816 	efx_sram_buf_tbl_clear(sc->enp, evq->buf_base_id,
817 	    EFX_EVQ_NBUFS(evq->entries));
818 
819 	return (rc);
820 }
821 
822 void
823 sfxge_ev_stop(struct sfxge_softc *sc)
824 {
825 	struct sfxge_intr *intr;
826 	efx_nic_t *enp;
827 	int index;
828 
829 	intr = &sc->intr;
830 	enp = sc->enp;
831 
832 	KASSERT(intr->state == SFXGE_INTR_STARTED,
833 	    ("Interrupts not started"));
834 
835 	/* Stop the event queue(s) */
836 	index = sc->evq_count;
837 	while (--index >= 0)
838 		sfxge_ev_qstop(sc, index);
839 
840 	/* Tear down the event module */
841 	efx_ev_fini(enp);
842 }
843 
844 int
845 sfxge_ev_start(struct sfxge_softc *sc)
846 {
847 	struct sfxge_intr *intr;
848 	int index;
849 	int rc;
850 
851 	intr = &sc->intr;
852 
853 	KASSERT(intr->state == SFXGE_INTR_STARTED,
854 	    ("intr->state != SFXGE_INTR_STARTED"));
855 
856 	/* Initialize the event module */
857 	if ((rc = efx_ev_init(sc->enp)) != 0)
858 		return (rc);
859 
860 	/* Start the event queues */
861 	for (index = 0; index < sc->evq_count; index++) {
862 		if ((rc = sfxge_ev_qstart(sc, index)) != 0)
863 			goto fail;
864 	}
865 
866 	return (0);
867 
868 fail:
869 	/* Stop the event queue(s) */
870 	while (--index >= 0)
871 		sfxge_ev_qstop(sc, index);
872 
873 	/* Tear down the event module */
874 	efx_ev_fini(sc->enp);
875 
876 	return (rc);
877 }
878 
879 static void
880 sfxge_ev_qfini(struct sfxge_softc *sc, unsigned int index)
881 {
882 	struct sfxge_evq *evq;
883 
884 	evq = sc->evq[index];
885 
886 	KASSERT(evq->init_state == SFXGE_EVQ_INITIALIZED,
887 	    ("evq->init_state != SFXGE_EVQ_INITIALIZED"));
888 	KASSERT(evq->txqs == &evq->txq, ("evq->txqs != &evq->txq"));
889 
890 	sfxge_dma_free(&evq->mem);
891 
892 	sc->evq[index] = NULL;
893 
894 	SFXGE_EVQ_LOCK_DESTROY(evq);
895 
896 	free(evq, M_SFXGE);
897 }
898 
899 static int
900 sfxge_ev_qinit(struct sfxge_softc *sc, unsigned int index)
901 {
902 	struct sfxge_evq *evq;
903 	efsys_mem_t *esmp;
904 	int rc;
905 
906 	KASSERT(index < SFXGE_RX_SCALE_MAX, ("index >= SFXGE_RX_SCALE_MAX"));
907 
908 	evq = malloc(sizeof(struct sfxge_evq), M_SFXGE, M_ZERO | M_WAITOK);
909 	evq->sc = sc;
910 	evq->index = index;
911 	sc->evq[index] = evq;
912 	esmp = &evq->mem;
913 
914 	/* Build an event queue with room for one event per tx and rx buffer,
915 	 * plus some extra for link state events and MCDI completions.
916 	 * There are three tx queues in the first event queue and one in
917 	 * other.
918 	 */
919 	if (index == 0)
920 		evq->entries =
921 			ROUNDUP_POW_OF_TWO(sc->rxq_entries +
922 					   3 * sc->txq_entries +
923 					   128);
924 	else
925 		evq->entries =
926 			ROUNDUP_POW_OF_TWO(sc->rxq_entries +
927 					   sc->txq_entries +
928 					   128);
929 
930 	/* Initialise TX completion list */
931 	evq->txqs = &evq->txq;
932 
933 	/* Allocate DMA space. */
934 	if ((rc = sfxge_dma_alloc(sc, EFX_EVQ_SIZE(evq->entries), esmp)) != 0)
935 		return (rc);
936 
937 	/* Allocate buffer table entries. */
938 	sfxge_sram_buf_tbl_alloc(sc, EFX_EVQ_NBUFS(evq->entries),
939 				 &evq->buf_base_id);
940 
941 	SFXGE_EVQ_LOCK_INIT(evq, device_get_nameunit(sc->dev), index);
942 
943 	evq->init_state = SFXGE_EVQ_INITIALIZED;
944 
945 #if EFSYS_OPT_QSTATS
946 	rc = sfxge_evq_stat_init(evq);
947 	if (rc != 0)
948 		goto fail_evq_stat_init;
949 #endif
950 
951 	return (0);
952 
953 #if EFSYS_OPT_QSTATS
954 fail_evq_stat_init:
955 	evq->init_state = SFXGE_EVQ_UNINITIALIZED;
956 	SFXGE_EVQ_LOCK_DESTROY(evq);
957 	sfxge_dma_free(esmp);
958 	sc->evq[index] = NULL;
959 	free(evq, M_SFXGE);
960 
961 	return (rc);
962 #endif
963 }
964 
965 void
966 sfxge_ev_fini(struct sfxge_softc *sc)
967 {
968 	struct sfxge_intr *intr;
969 	int index;
970 
971 	intr = &sc->intr;
972 
973 	KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
974 	    ("intr->state != SFXGE_INTR_INITIALIZED"));
975 
976 	sc->ev_moderation = 0;
977 
978 	/* Tear down the event queue(s). */
979 	index = sc->evq_count;
980 	while (--index >= 0)
981 		sfxge_ev_qfini(sc, index);
982 
983 	sc->evq_count = 0;
984 }
985 
986 int
987 sfxge_ev_init(struct sfxge_softc *sc)
988 {
989 	struct sysctl_ctx_list *sysctl_ctx = device_get_sysctl_ctx(sc->dev);
990 	struct sysctl_oid *sysctl_tree = device_get_sysctl_tree(sc->dev);
991 	struct sfxge_intr *intr;
992 	int index;
993 	int rc;
994 
995 	intr = &sc->intr;
996 
997 	sc->evq_count = intr->n_alloc;
998 
999 	KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
1000 	    ("intr->state != SFXGE_INTR_INITIALIZED"));
1001 
1002 	/* Set default interrupt moderation; add a sysctl to
1003 	 * read and change it.
1004 	 */
1005 	sc->ev_moderation = SFXGE_MODERATION;
1006 	SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
1007 			OID_AUTO, "int_mod", CTLTYPE_UINT|CTLFLAG_RW,
1008 			sc, 0, sfxge_int_mod_handler, "IU",
1009 			"sfxge interrupt moderation (us)");
1010 
1011 #if EFSYS_OPT_QSTATS
1012 	sc->evqs_stats_node = SYSCTL_ADD_NODE(
1013 		device_get_sysctl_ctx(sc->dev), SYSCTL_CHILDREN(sc->stats_node),
1014 		OID_AUTO, "evq", CTLFLAG_RD, NULL, "Event queues stats");
1015 	if (sc->evqs_stats_node == NULL) {
1016 		rc = ENOMEM;
1017 		goto fail_evqs_stats_node;
1018 	}
1019 #endif
1020 
1021 	/*
1022 	 * Initialize the event queue(s) - one per interrupt.
1023 	 */
1024 	for (index = 0; index < sc->evq_count; index++) {
1025 		if ((rc = sfxge_ev_qinit(sc, index)) != 0)
1026 			goto fail;
1027 	}
1028 
1029 #if EFSYS_OPT_QSTATS
1030 	sfxge_ev_stat_init(sc);
1031 #endif
1032 
1033 	return (0);
1034 
1035 fail:
1036 	while (--index >= 0)
1037 		sfxge_ev_qfini(sc, index);
1038 
1039 #if EFSYS_OPT_QSTATS
1040 fail_evqs_stats_node:
1041 #endif
1042 	sc->evq_count = 0;
1043 	return (rc);
1044 }
1045