]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/sfxge/sfxge_ev.c
sfxge: Change sfxge_ev_qpoll() proto to avoid EVQ pointers array access
[FreeBSD/FreeBSD.git] / sys / dev / sfxge / sfxge_ev.c
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++ & rxq->ptr_mask;
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) & txq->ptr_mask;
251         id = txq->pending & txq->ptr_mask;
252
253         delta = (stop >= id) ? (stop - id) : (txq->entries - 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 != NULL) {
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_evq *evq)
573 {
574         int rc;
575
576         mtx_lock(&evq->lock);
577
578         if (evq->init_state != SFXGE_EVQ_STARTING &&
579             evq->init_state != SFXGE_EVQ_STARTED) {
580                 rc = EINVAL;
581                 goto fail;
582         }
583
584         /* Synchronize the DMA memory for reading */
585         bus_dmamap_sync(evq->mem.esm_tag, evq->mem.esm_map,
586             BUS_DMASYNC_POSTREAD);
587
588         KASSERT(evq->rx_done == 0, ("evq->rx_done != 0"));
589         KASSERT(evq->tx_done == 0, ("evq->tx_done != 0"));
590         KASSERT(evq->txq == NULL, ("evq->txq != NULL"));
591         KASSERT(evq->txqs == &evq->txq, ("evq->txqs != &evq->txq"));
592
593         /* Poll the queue */
594         efx_ev_qpoll(evq->common, &evq->read_ptr, &sfxge_ev_callbacks, evq);
595
596         evq->rx_done = 0;
597         evq->tx_done = 0;
598
599         /* Perform any pending completion processing */
600         sfxge_ev_qcomplete(evq, B_TRUE);
601
602         /* Re-prime the event queue for interrupts */
603         if ((rc = efx_ev_qprime(evq->common, evq->read_ptr)) != 0)
604                 goto fail;
605
606         mtx_unlock(&evq->lock);
607
608         return (0);
609
610 fail:
611         mtx_unlock(&(evq->lock));
612         return (rc);
613 }
614
615 static void
616 sfxge_ev_qstop(struct sfxge_softc *sc, unsigned int index)
617 {
618         struct sfxge_evq *evq;
619
620         evq = sc->evq[index];
621
622         KASSERT(evq->init_state == SFXGE_EVQ_STARTED,
623             ("evq->init_state != SFXGE_EVQ_STARTED"));
624
625         mtx_lock(&evq->lock);
626         evq->init_state = SFXGE_EVQ_INITIALIZED;
627         evq->read_ptr = 0;
628         evq->exception = B_FALSE;
629
630         /* Add event counts before discarding the common evq state */
631         efx_ev_qstats_update(evq->common, sc->ev_stats);
632
633         efx_ev_qdestroy(evq->common);
634         efx_sram_buf_tbl_clear(sc->enp, evq->buf_base_id,
635             EFX_EVQ_NBUFS(evq->entries));
636         mtx_unlock(&evq->lock);
637 }
638
639 static int
640 sfxge_ev_qstart(struct sfxge_softc *sc, unsigned int index)
641 {
642         struct sfxge_evq *evq;
643         efsys_mem_t *esmp;
644         int count;
645         int rc;
646
647         evq = sc->evq[index];
648         esmp = &evq->mem;
649
650         KASSERT(evq->init_state == SFXGE_EVQ_INITIALIZED,
651             ("evq->init_state != SFXGE_EVQ_INITIALIZED"));
652
653         /* Clear all events. */
654         (void)memset(esmp->esm_base, 0xff, EFX_EVQ_SIZE(evq->entries));
655
656         /* Program the buffer table. */
657         if ((rc = efx_sram_buf_tbl_set(sc->enp, evq->buf_base_id, esmp,
658             EFX_EVQ_NBUFS(evq->entries))) != 0)
659                 return (rc);
660
661         /* Create the common code event queue. */
662         if ((rc = efx_ev_qcreate(sc->enp, index, esmp, evq->entries,
663             evq->buf_base_id, &evq->common)) != 0)
664                 goto fail;
665
666         mtx_lock(&evq->lock);
667
668         /* Set the default moderation */
669         (void)efx_ev_qmoderate(evq->common, sc->ev_moderation);
670
671         /* Prime the event queue for interrupts */
672         if ((rc = efx_ev_qprime(evq->common, evq->read_ptr)) != 0)
673                 goto fail2;
674
675         evq->init_state = SFXGE_EVQ_STARTING;
676
677         mtx_unlock(&evq->lock);
678
679         /* Wait for the initialization event */
680         count = 0;
681         do {
682                 /* Pause for 100 ms */
683                 pause("sfxge evq init", hz / 10);
684
685                 /* Check to see if the test event has been processed */
686                 if (evq->init_state == SFXGE_EVQ_STARTED)
687                         goto done;
688
689         } while (++count < 20);
690
691         rc = ETIMEDOUT;
692         goto fail3;
693
694 done:
695         return (0);
696
697 fail3:
698         mtx_lock(&evq->lock);
699         evq->init_state = SFXGE_EVQ_INITIALIZED;
700 fail2:
701         mtx_unlock(&evq->lock);
702         efx_ev_qdestroy(evq->common);
703 fail:
704         efx_sram_buf_tbl_clear(sc->enp, evq->buf_base_id,
705             EFX_EVQ_NBUFS(evq->entries));
706
707         return (rc);
708 }
709
710 void
711 sfxge_ev_stop(struct sfxge_softc *sc)
712 {
713         struct sfxge_intr *intr;
714         efx_nic_t *enp;
715         int index;
716
717         intr = &sc->intr;
718         enp = sc->enp;
719
720         KASSERT(intr->state == SFXGE_INTR_STARTED,
721             ("Interrupts not started"));
722
723         /* Stop the event queue(s) */
724         index = intr->n_alloc;
725         while (--index >= 0)
726                 sfxge_ev_qstop(sc, index);
727
728         /* Tear down the event module */
729         efx_ev_fini(enp);
730 }
731
732 int
733 sfxge_ev_start(struct sfxge_softc *sc)
734 {
735         struct sfxge_intr *intr;
736         int index;
737         int rc;
738
739         intr = &sc->intr;
740
741         KASSERT(intr->state == SFXGE_INTR_STARTED,
742             ("intr->state != SFXGE_INTR_STARTED"));
743
744         /* Initialize the event module */
745         if ((rc = efx_ev_init(sc->enp)) != 0)
746                 return (rc);
747
748         /* Start the event queues */
749         for (index = 0; index < intr->n_alloc; index++) {
750                 if ((rc = sfxge_ev_qstart(sc, index)) != 0)
751                         goto fail;
752         }
753
754         return (0);
755
756 fail:
757         /* Stop the event queue(s) */
758         while (--index >= 0)
759                 sfxge_ev_qstop(sc, index);
760
761         /* Tear down the event module */
762         efx_ev_fini(sc->enp);
763
764         return (rc);
765 }
766
767 static void
768 sfxge_ev_qfini(struct sfxge_softc *sc, unsigned int index)
769 {
770         struct sfxge_evq *evq;
771
772         evq = sc->evq[index];
773
774         KASSERT(evq->init_state == SFXGE_EVQ_INITIALIZED,
775             ("evq->init_state != SFXGE_EVQ_INITIALIZED"));
776         KASSERT(evq->txqs == &evq->txq, ("evq->txqs != &evq->txq"));
777
778         sfxge_dma_free(&evq->mem);
779
780         sc->evq[index] = NULL;
781
782         mtx_destroy(&evq->lock);
783
784         free(evq, M_SFXGE);
785 }
786
787 static int
788 sfxge_ev_qinit(struct sfxge_softc *sc, unsigned int index)
789 {
790         struct sfxge_evq *evq;
791         efsys_mem_t *esmp;
792         int rc;
793
794         KASSERT(index < SFXGE_RX_SCALE_MAX, ("index >= SFXGE_RX_SCALE_MAX"));
795
796         evq = malloc(sizeof(struct sfxge_evq), M_SFXGE, M_ZERO | M_WAITOK);
797         evq->sc = sc;
798         evq->index = index;
799         sc->evq[index] = evq;
800         esmp = &evq->mem;
801
802         /* Build an event queue with room for one event per tx and rx buffer,
803          * plus some extra for link state events and MCDI completions.
804          * There are three tx queues in the first event queue and one in
805          * other.
806          */
807         if (index == 0)
808                 evq->entries =
809                         ROUNDUP_POW_OF_TWO(sc->rxq_entries +
810                                            3 * sc->txq_entries +
811                                            128);
812         else
813                 evq->entries =
814                         ROUNDUP_POW_OF_TWO(sc->rxq_entries +
815                                            sc->txq_entries +
816                                            128);
817
818         /* Initialise TX completion list */
819         evq->txqs = &evq->txq;
820
821         /* Allocate DMA space. */
822         if ((rc = sfxge_dma_alloc(sc, EFX_EVQ_SIZE(evq->entries), esmp)) != 0)
823                 return (rc);
824
825         /* Allocate buffer table entries. */
826         sfxge_sram_buf_tbl_alloc(sc, EFX_EVQ_NBUFS(evq->entries),
827                                  &evq->buf_base_id);
828
829         mtx_init(&evq->lock, "evq", NULL, MTX_DEF);
830
831         evq->init_state = SFXGE_EVQ_INITIALIZED;
832
833         return (0);
834 }
835
836 void
837 sfxge_ev_fini(struct sfxge_softc *sc)
838 {
839         struct sfxge_intr *intr;
840         int index;
841
842         intr = &sc->intr;
843
844         KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
845             ("intr->state != SFXGE_INTR_INITIALIZED"));
846
847         sc->ev_moderation = 0;
848
849         /* Tear down the event queue(s). */
850         index = intr->n_alloc;
851         while (--index >= 0)
852                 sfxge_ev_qfini(sc, index);
853 }
854
855 int
856 sfxge_ev_init(struct sfxge_softc *sc)
857 {
858         struct sysctl_ctx_list *sysctl_ctx = device_get_sysctl_ctx(sc->dev);
859         struct sysctl_oid *sysctl_tree = device_get_sysctl_tree(sc->dev);
860         struct sfxge_intr *intr;
861         int index;
862         int rc;
863
864         intr = &sc->intr;
865
866         KASSERT(intr->state == SFXGE_INTR_INITIALIZED,
867             ("intr->state != SFXGE_INTR_INITIALIZED"));
868
869         /* Set default interrupt moderation; add a sysctl to
870          * read and change it.
871          */
872         sc->ev_moderation = 30;
873         SYSCTL_ADD_PROC(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
874                         OID_AUTO, "int_mod", CTLTYPE_UINT|CTLFLAG_RW,
875                         sc, 0, sfxge_int_mod_handler, "IU",
876                         "sfxge interrupt moderation (us)");
877
878         /*
879          * Initialize the event queue(s) - one per interrupt.
880          */
881         for (index = 0; index < intr->n_alloc; index++) {
882                 if ((rc = sfxge_ev_qinit(sc, index)) != 0)
883                         goto fail;
884         }
885
886         sfxge_ev_stat_init(sc);
887
888         return (0);
889
890 fail:
891         while (--index >= 0)
892                 sfxge_ev_qfini(sc, index);
893
894         return (rc);
895 }