xref: /freebsd/sys/dev/ath/if_ath_rx_edma.c (revision eb3821e6d92fa45ece7ec08efde45e35be1bdc95)
1f8cc9b09SAdrian Chadd /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4f8cc9b09SAdrian Chadd  * Copyright (c) 2012 Adrian Chadd <adrian@FreeBSD.org>
5f8cc9b09SAdrian Chadd  * All rights reserved.
6f8cc9b09SAdrian Chadd  *
7f8cc9b09SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
8f8cc9b09SAdrian Chadd  * modification, are permitted provided that the following conditions
9f8cc9b09SAdrian Chadd  * are met:
10f8cc9b09SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
11f8cc9b09SAdrian Chadd  *    notice, this list of conditions and the following disclaimer,
12f8cc9b09SAdrian Chadd  *    without modification.
13f8cc9b09SAdrian Chadd  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14f8cc9b09SAdrian Chadd  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15f8cc9b09SAdrian Chadd  *    redistribution must be conditioned upon including a substantially
16f8cc9b09SAdrian Chadd  *    similar Disclaimer requirement for further binary redistribution.
17f8cc9b09SAdrian Chadd  *
18f8cc9b09SAdrian Chadd  * NO WARRANTY
19f8cc9b09SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20f8cc9b09SAdrian Chadd  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21f8cc9b09SAdrian Chadd  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22f8cc9b09SAdrian Chadd  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23f8cc9b09SAdrian Chadd  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24f8cc9b09SAdrian Chadd  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f8cc9b09SAdrian Chadd  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f8cc9b09SAdrian Chadd  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27f8cc9b09SAdrian Chadd  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f8cc9b09SAdrian Chadd  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29f8cc9b09SAdrian Chadd  * THE POSSIBILITY OF SUCH DAMAGES.
30f8cc9b09SAdrian Chadd  */
31f8cc9b09SAdrian Chadd 
32f8cc9b09SAdrian Chadd #include <sys/cdefs.h>
33f8cc9b09SAdrian Chadd /*
34f8cc9b09SAdrian Chadd  * Driver for the Atheros Wireless LAN controller.
35f8cc9b09SAdrian Chadd  *
36f8cc9b09SAdrian Chadd  * This software is derived from work of Atsushi Onoe; his contribution
37f8cc9b09SAdrian Chadd  * is greatly appreciated.
38f8cc9b09SAdrian Chadd  */
39f8cc9b09SAdrian Chadd 
40f8cc9b09SAdrian Chadd #include "opt_inet.h"
41f8cc9b09SAdrian Chadd #include "opt_ath.h"
42f8cc9b09SAdrian Chadd /*
43f8cc9b09SAdrian Chadd  * This is needed for register operations which are performed
44f8cc9b09SAdrian Chadd  * by the driver - eg, calls to ath_hal_gettsf32().
45f8cc9b09SAdrian Chadd  *
46f8cc9b09SAdrian Chadd  * It's also required for any AH_DEBUG checks in here, eg the
47f8cc9b09SAdrian Chadd  * module dependencies.
48f8cc9b09SAdrian Chadd  */
49f8cc9b09SAdrian Chadd #include "opt_ah.h"
50f8cc9b09SAdrian Chadd #include "opt_wlan.h"
51f8cc9b09SAdrian Chadd 
52f8cc9b09SAdrian Chadd #include <sys/param.h>
53f8cc9b09SAdrian Chadd #include <sys/systm.h>
54f8cc9b09SAdrian Chadd #include <sys/sysctl.h>
55f8cc9b09SAdrian Chadd #include <sys/mbuf.h>
56f8cc9b09SAdrian Chadd #include <sys/malloc.h>
57f8cc9b09SAdrian Chadd #include <sys/lock.h>
58f8cc9b09SAdrian Chadd #include <sys/mutex.h>
59f8cc9b09SAdrian Chadd #include <sys/kernel.h>
60f8cc9b09SAdrian Chadd #include <sys/socket.h>
61f8cc9b09SAdrian Chadd #include <sys/sockio.h>
62f8cc9b09SAdrian Chadd #include <sys/errno.h>
63f8cc9b09SAdrian Chadd #include <sys/callout.h>
64f8cc9b09SAdrian Chadd #include <sys/bus.h>
65f8cc9b09SAdrian Chadd #include <sys/endian.h>
66f8cc9b09SAdrian Chadd #include <sys/kthread.h>
67f8cc9b09SAdrian Chadd #include <sys/taskqueue.h>
68f8cc9b09SAdrian Chadd #include <sys/priv.h>
69f8cc9b09SAdrian Chadd #include <sys/module.h>
70f8cc9b09SAdrian Chadd #include <sys/ktr.h>
71f8cc9b09SAdrian Chadd #include <sys/smp.h>	/* for mp_ncpus */
72f8cc9b09SAdrian Chadd 
73f8cc9b09SAdrian Chadd #include <machine/bus.h>
74f8cc9b09SAdrian Chadd 
75f8cc9b09SAdrian Chadd #include <net/if.h>
7676039bc8SGleb Smirnoff #include <net/if_var.h>
77f8cc9b09SAdrian Chadd #include <net/if_dl.h>
78f8cc9b09SAdrian Chadd #include <net/if_media.h>
79f8cc9b09SAdrian Chadd #include <net/if_types.h>
80f8cc9b09SAdrian Chadd #include <net/if_arp.h>
81f8cc9b09SAdrian Chadd #include <net/ethernet.h>
82f8cc9b09SAdrian Chadd #include <net/if_llc.h>
83f8cc9b09SAdrian Chadd 
84f8cc9b09SAdrian Chadd #include <net80211/ieee80211_var.h>
85f8cc9b09SAdrian Chadd #include <net80211/ieee80211_regdomain.h>
86f8cc9b09SAdrian Chadd #ifdef IEEE80211_SUPPORT_SUPERG
87f8cc9b09SAdrian Chadd #include <net80211/ieee80211_superg.h>
88f8cc9b09SAdrian Chadd #endif
89f8cc9b09SAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
90f8cc9b09SAdrian Chadd #include <net80211/ieee80211_tdma.h>
91f8cc9b09SAdrian Chadd #endif
92f8cc9b09SAdrian Chadd 
93f8cc9b09SAdrian Chadd #include <net/bpf.h>
94f8cc9b09SAdrian Chadd 
95f8cc9b09SAdrian Chadd #ifdef INET
96f8cc9b09SAdrian Chadd #include <netinet/in.h>
97f8cc9b09SAdrian Chadd #include <netinet/if_ether.h>
98f8cc9b09SAdrian Chadd #endif
99f8cc9b09SAdrian Chadd 
100f8cc9b09SAdrian Chadd #include <dev/ath/if_athvar.h>
101f8cc9b09SAdrian Chadd #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
102f8cc9b09SAdrian Chadd #include <dev/ath/ath_hal/ah_diagcodes.h>
103f8cc9b09SAdrian Chadd 
104f8cc9b09SAdrian Chadd #include <dev/ath/if_ath_debug.h>
105f8cc9b09SAdrian Chadd #include <dev/ath/if_ath_misc.h>
106f8cc9b09SAdrian Chadd #include <dev/ath/if_ath_tsf.h>
107f8cc9b09SAdrian Chadd #include <dev/ath/if_ath_tx.h>
108f8cc9b09SAdrian Chadd #include <dev/ath/if_ath_sysctl.h>
109f8cc9b09SAdrian Chadd #include <dev/ath/if_ath_led.h>
110f8cc9b09SAdrian Chadd #include <dev/ath/if_ath_keycache.h>
111f8cc9b09SAdrian Chadd #include <dev/ath/if_ath_rx.h>
112f8cc9b09SAdrian Chadd #include <dev/ath/if_ath_beacon.h>
113f8cc9b09SAdrian Chadd #include <dev/ath/if_athdfs.h>
114b45de1ebSAdrian Chadd #include <dev/ath/if_ath_descdma.h>
115f8cc9b09SAdrian Chadd 
116f8cc9b09SAdrian Chadd #ifdef ATH_TX99_DIAG
117f8cc9b09SAdrian Chadd #include <dev/ath/ath_tx99/ath_tx99.h>
118f8cc9b09SAdrian Chadd #endif
119f8cc9b09SAdrian Chadd 
120f8cc9b09SAdrian Chadd #include <dev/ath/if_ath_rx_edma.h>
121f8cc9b09SAdrian Chadd 
122b69b0dccSAdrian Chadd #ifdef	ATH_DEBUG_ALQ
123b69b0dccSAdrian Chadd #include <dev/ath/if_ath_alq.h>
124b69b0dccSAdrian Chadd #endif
125b69b0dccSAdrian Chadd 
12699e8d8c3SAdrian Chadd /*
12799e8d8c3SAdrian Chadd  * some general macros
12899e8d8c3SAdrian Chadd   */
12999e8d8c3SAdrian Chadd #define	INCR(_l, _sz)		(_l) ++; (_l) &= ((_sz) - 1)
13099e8d8c3SAdrian Chadd #define	DECR(_l, _sz)		(_l) --; (_l) &= ((_sz) - 1)
13199e8d8c3SAdrian Chadd 
13299e8d8c3SAdrian Chadd MALLOC_DECLARE(M_ATHDEV);
13399e8d8c3SAdrian Chadd 
13499e8d8c3SAdrian Chadd /*
13599e8d8c3SAdrian Chadd  * XXX TODO:
13699e8d8c3SAdrian Chadd  *
13799e8d8c3SAdrian Chadd  * + Make sure the FIFO is correctly flushed and reinitialised
13899e8d8c3SAdrian Chadd  *   through a reset;
13999e8d8c3SAdrian Chadd  * + Verify multi-descriptor frames work!
14099e8d8c3SAdrian Chadd  * + There's a "memory use after free" which needs to be tracked down
14199e8d8c3SAdrian Chadd  *   and fixed ASAP.  I've seen this in the legacy path too, so it
14299e8d8c3SAdrian Chadd  *   may be a generic RX path issue.
14399e8d8c3SAdrian Chadd  */
14499e8d8c3SAdrian Chadd 
14599e8d8c3SAdrian Chadd /*
14699e8d8c3SAdrian Chadd  * XXX shuffle the function orders so these pre-declarations aren't
14799e8d8c3SAdrian Chadd  * required!
14899e8d8c3SAdrian Chadd  */
14999e8d8c3SAdrian Chadd static	int ath_edma_rxfifo_alloc(struct ath_softc *sc, HAL_RX_QUEUE qtype,
15099e8d8c3SAdrian Chadd 	    int nbufs);
15199e8d8c3SAdrian Chadd static	int ath_edma_rxfifo_flush(struct ath_softc *sc, HAL_RX_QUEUE qtype);
15299e8d8c3SAdrian Chadd static	void ath_edma_rxbuf_free(struct ath_softc *sc, struct ath_buf *bf);
153f0db652cSAdrian Chadd static	void ath_edma_recv_proc_queue(struct ath_softc *sc,
154f0db652cSAdrian Chadd 	    HAL_RX_QUEUE qtype, int dosched);
155f0db652cSAdrian Chadd static	int ath_edma_recv_proc_deferred_queue(struct ath_softc *sc,
15662206b76SAdrian Chadd 	    HAL_RX_QUEUE qtype, int dosched);
15799e8d8c3SAdrian Chadd 
158f8cc9b09SAdrian Chadd static void
ath_edma_stoprecv(struct ath_softc * sc,int dodelay)159f8cc9b09SAdrian Chadd ath_edma_stoprecv(struct ath_softc *sc, int dodelay)
160f8cc9b09SAdrian Chadd {
161f8cc9b09SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
162f8cc9b09SAdrian Chadd 
163a100c050SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called, dodelay=%d\n",
164a100c050SAdrian Chadd 	    __func__, dodelay);
165a100c050SAdrian Chadd 
1660b59717bSAdrian Chadd 	ATH_RX_LOCK(sc);
16717bb5fd1SAdrian Chadd 
168f8cc9b09SAdrian Chadd 	ath_hal_stoppcurecv(ah);
169f8cc9b09SAdrian Chadd 	ath_hal_setrxfilter(ah, 0);
170f8cc9b09SAdrian Chadd 
17117bb5fd1SAdrian Chadd 	/*
17217bb5fd1SAdrian Chadd 	 *
17317bb5fd1SAdrian Chadd 	 */
17417bb5fd1SAdrian Chadd 	if (ath_hal_stopdmarecv(ah) == AH_TRUE)
17517bb5fd1SAdrian Chadd 		sc->sc_rx_stopped = 1;
17617bb5fd1SAdrian Chadd 
17717bb5fd1SAdrian Chadd 	/*
17817bb5fd1SAdrian Chadd 	 * Give the various bus FIFOs (not EDMA descriptor FIFO)
17917bb5fd1SAdrian Chadd 	 * time to finish flushing out data.
18017bb5fd1SAdrian Chadd 	 */
181f8cc9b09SAdrian Chadd 	DELAY(3000);
182f8cc9b09SAdrian Chadd 
18399e8d8c3SAdrian Chadd 	/* Flush RX pending for each queue */
18499e8d8c3SAdrian Chadd 	/* XXX should generic-ify this */
18599e8d8c3SAdrian Chadd 	if (sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending) {
18699e8d8c3SAdrian Chadd 		m_freem(sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending);
18799e8d8c3SAdrian Chadd 		sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending = NULL;
188f8cc9b09SAdrian Chadd 	}
189f8cc9b09SAdrian Chadd 
19099e8d8c3SAdrian Chadd 	if (sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending) {
19199e8d8c3SAdrian Chadd 		m_freem(sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending);
19299e8d8c3SAdrian Chadd 		sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending = NULL;
19399e8d8c3SAdrian Chadd 	}
1940b59717bSAdrian Chadd 	ATH_RX_UNLOCK(sc);
195a100c050SAdrian Chadd 
196a100c050SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: done\n", __func__);
197f8cc9b09SAdrian Chadd }
198f8cc9b09SAdrian Chadd 
19999e8d8c3SAdrian Chadd /*
20062206b76SAdrian Chadd  * Re-initialise the FIFO given the current buffer contents.
20162206b76SAdrian Chadd  * Specifically, walk from head -> tail, pushing the FIFO contents
20262206b76SAdrian Chadd  * back into the FIFO.
20362206b76SAdrian Chadd  */
20462206b76SAdrian Chadd static void
ath_edma_reinit_fifo(struct ath_softc * sc,HAL_RX_QUEUE qtype)20562206b76SAdrian Chadd ath_edma_reinit_fifo(struct ath_softc *sc, HAL_RX_QUEUE qtype)
20662206b76SAdrian Chadd {
20762206b76SAdrian Chadd 	struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
20862206b76SAdrian Chadd 	struct ath_buf *bf;
20962206b76SAdrian Chadd 	int i, j;
21062206b76SAdrian Chadd 
211a100c050SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called\n", __func__);
212a100c050SAdrian Chadd 
2130b59717bSAdrian Chadd 	ATH_RX_LOCK_ASSERT(sc);
2140b59717bSAdrian Chadd 
21562206b76SAdrian Chadd 	i = re->m_fifo_head;
21662206b76SAdrian Chadd 	for (j = 0; j < re->m_fifo_depth; j++) {
21762206b76SAdrian Chadd 		bf = re->m_fifo[i];
218fda21122SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_EDMA_RX,
219f5afad73SJohn Baldwin 		    "%s: Q%d: pos=%i, addr=0x%jx\n",
220fda21122SAdrian Chadd 		    __func__,
221fda21122SAdrian Chadd 		    qtype,
222fda21122SAdrian Chadd 		    i,
223f5afad73SJohn Baldwin 		    (uintmax_t)bf->bf_daddr);
22462206b76SAdrian Chadd 		ath_hal_putrxbuf(sc->sc_ah, bf->bf_daddr, qtype);
22562206b76SAdrian Chadd 		INCR(i, re->m_fifolen);
22662206b76SAdrian Chadd 	}
22762206b76SAdrian Chadd 
22862206b76SAdrian Chadd 	/* Ensure this worked out right */
22962206b76SAdrian Chadd 	if (i != re->m_fifo_tail) {
23062206b76SAdrian Chadd 		device_printf(sc->sc_dev, "%s: i (%d) != tail! (%d)\n",
23162206b76SAdrian Chadd 		    __func__,
23262206b76SAdrian Chadd 		    i,
23362206b76SAdrian Chadd 		    re->m_fifo_tail);
23462206b76SAdrian Chadd 	}
235a100c050SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: done\n", __func__);
23662206b76SAdrian Chadd }
23762206b76SAdrian Chadd 
23862206b76SAdrian Chadd /*
23999e8d8c3SAdrian Chadd  * Start receive.
24099e8d8c3SAdrian Chadd  */
241f8cc9b09SAdrian Chadd static int
ath_edma_startrecv(struct ath_softc * sc)242f8cc9b09SAdrian Chadd ath_edma_startrecv(struct ath_softc *sc)
243f8cc9b09SAdrian Chadd {
244f8cc9b09SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
245f8cc9b09SAdrian Chadd 
246a100c050SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_EDMA_RX,
247a100c050SAdrian Chadd 	    "%s: called; resetted=%d, stopped=%d\n", __func__,
248a100c050SAdrian Chadd 	    sc->sc_rx_resetted, sc->sc_rx_stopped);
249a100c050SAdrian Chadd 
2500b59717bSAdrian Chadd 	ATH_RX_LOCK(sc);
2510b59717bSAdrian Chadd 
25217bb5fd1SAdrian Chadd 	/*
25317bb5fd1SAdrian Chadd 	 * Sanity check - are we being called whilst RX
25417bb5fd1SAdrian Chadd 	 * isn't stopped?  If so, we may end up pushing
25517bb5fd1SAdrian Chadd 	 * too many entries into the RX FIFO and
25617bb5fd1SAdrian Chadd 	 * badness occurs.
25717bb5fd1SAdrian Chadd 	 */
25817bb5fd1SAdrian Chadd 
25999e8d8c3SAdrian Chadd 	/* Enable RX FIFO */
26099e8d8c3SAdrian Chadd 	ath_hal_rxena(ah);
261f8cc9b09SAdrian Chadd 
26299e8d8c3SAdrian Chadd 	/*
26317bb5fd1SAdrian Chadd 	 * In theory the hardware has been initialised, right?
26499e8d8c3SAdrian Chadd 	 */
265a100c050SAdrian Chadd 	if (sc->sc_rx_resetted == 1 || sc->sc_rx_stopped == 1) {
26662206b76SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_EDMA_RX,
26762206b76SAdrian Chadd 		    "%s: Re-initing HP FIFO\n", __func__);
26862206b76SAdrian Chadd 		ath_edma_reinit_fifo(sc, HAL_RX_QUEUE_HP);
26962206b76SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_EDMA_RX,
27062206b76SAdrian Chadd 		    "%s: Re-initing LP FIFO\n", __func__);
27162206b76SAdrian Chadd 		ath_edma_reinit_fifo(sc, HAL_RX_QUEUE_LP);
27217bb5fd1SAdrian Chadd 		sc->sc_rx_resetted = 0;
27317bb5fd1SAdrian Chadd 	} else {
27417bb5fd1SAdrian Chadd 		device_printf(sc->sc_dev,
275a100c050SAdrian Chadd 		    "%s: called without resetting chip? "
276a100c050SAdrian Chadd 		    "resetted=%d, stopped=%d\n",
277a100c050SAdrian Chadd 		    __func__,
278a100c050SAdrian Chadd 		    sc->sc_rx_resetted,
279a100c050SAdrian Chadd 		    sc->sc_rx_stopped);
28062206b76SAdrian Chadd 	}
28199e8d8c3SAdrian Chadd 
28299e8d8c3SAdrian Chadd 	/* Add up to m_fifolen entries in each queue */
28399e8d8c3SAdrian Chadd 	/*
28499e8d8c3SAdrian Chadd 	 * These must occur after the above write so the FIFO buffers
28599e8d8c3SAdrian Chadd 	 * are pushed/tracked in the same order as the hardware will
28699e8d8c3SAdrian Chadd 	 * process them.
28717bb5fd1SAdrian Chadd 	 *
28817bb5fd1SAdrian Chadd 	 * XXX TODO: is this really necessary? We should've stopped
28917bb5fd1SAdrian Chadd 	 * the hardware already and reinitialised it, so it's a no-op.
29099e8d8c3SAdrian Chadd 	 */
29199e8d8c3SAdrian Chadd 	ath_edma_rxfifo_alloc(sc, HAL_RX_QUEUE_HP,
29299e8d8c3SAdrian Chadd 	    sc->sc_rxedma[HAL_RX_QUEUE_HP].m_fifolen);
29399e8d8c3SAdrian Chadd 
29499e8d8c3SAdrian Chadd 	ath_edma_rxfifo_alloc(sc, HAL_RX_QUEUE_LP,
29599e8d8c3SAdrian Chadd 	    sc->sc_rxedma[HAL_RX_QUEUE_LP].m_fifolen);
29699e8d8c3SAdrian Chadd 
297f8cc9b09SAdrian Chadd 	ath_mode_init(sc);
298a8083b9cSAdrian Chadd 	ath_hal_startpcurecv(ah, (!! sc->sc_scanning));
2990b59717bSAdrian Chadd 
30017bb5fd1SAdrian Chadd 	/*
30117bb5fd1SAdrian Chadd 	 * We're now doing RX DMA!
30217bb5fd1SAdrian Chadd 	 */
30317bb5fd1SAdrian Chadd 	sc->sc_rx_stopped = 0;
30417bb5fd1SAdrian Chadd 
3050b59717bSAdrian Chadd 	ATH_RX_UNLOCK(sc);
306a100c050SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: ready\n", __func__);
3070b59717bSAdrian Chadd 
308f8cc9b09SAdrian Chadd 	return (0);
309f8cc9b09SAdrian Chadd }
310f8cc9b09SAdrian Chadd 
311f8cc9b09SAdrian Chadd static void
ath_edma_recv_sched_queue(struct ath_softc * sc,HAL_RX_QUEUE qtype,int dosched)312f0db652cSAdrian Chadd ath_edma_recv_sched_queue(struct ath_softc *sc, HAL_RX_QUEUE qtype,
313f0db652cSAdrian Chadd     int dosched)
314f0db652cSAdrian Chadd {
315a100c050SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called; qtype=%d, dosched=%d\n",
316a100c050SAdrian Chadd 	    __func__, qtype, dosched);
317f0db652cSAdrian Chadd 
318f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
319f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
320f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
321f5c30c4eSAdrian Chadd 
322f0db652cSAdrian Chadd 	ath_edma_recv_proc_queue(sc, qtype, dosched);
323f5c30c4eSAdrian Chadd 
324f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
325f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
326f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
327f5c30c4eSAdrian Chadd 
328a100c050SAdrian Chadd 	/* XXX TODO: methodize */
329f0db652cSAdrian Chadd 	taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
330a100c050SAdrian Chadd 
331a100c050SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: done\n", __func__);
332f0db652cSAdrian Chadd }
333f0db652cSAdrian Chadd 
334f0db652cSAdrian Chadd static void
ath_edma_recv_sched(struct ath_softc * sc,int dosched)335f0db652cSAdrian Chadd ath_edma_recv_sched(struct ath_softc *sc, int dosched)
336f0db652cSAdrian Chadd {
337f0db652cSAdrian Chadd 
338a100c050SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called; dosched=%d\n",
339a100c050SAdrian Chadd 	    __func__, dosched);
340a100c050SAdrian Chadd 
341f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
342f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
343f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
344f5c30c4eSAdrian Chadd 
345f0db652cSAdrian Chadd 	ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_HP, dosched);
346f0db652cSAdrian Chadd 	ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_LP, dosched);
347f5c30c4eSAdrian Chadd 
348f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
349f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
350f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
351f5c30c4eSAdrian Chadd 
352a100c050SAdrian Chadd 	/* XXX TODO: methodize */
353f0db652cSAdrian Chadd 	taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
354a100c050SAdrian Chadd 
355a100c050SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: done\n", __func__);
356f0db652cSAdrian Chadd }
357f0db652cSAdrian Chadd 
358f0db652cSAdrian Chadd static void
ath_edma_recv_flush(struct ath_softc * sc)359f8cc9b09SAdrian Chadd ath_edma_recv_flush(struct ath_softc *sc)
360f8cc9b09SAdrian Chadd {
361f8cc9b09SAdrian Chadd 
362a100c050SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RECV | ATH_DEBUG_EDMA_RX, "%s: called\n", __func__);
36399e8d8c3SAdrian Chadd 
3642e7620b6SAdrian Chadd 	ATH_PCU_LOCK(sc);
3652e7620b6SAdrian Chadd 	sc->sc_rxproc_cnt++;
3662e7620b6SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
3672e7620b6SAdrian Chadd 
368a100c050SAdrian Chadd 	// XXX TODO: methodize; make it an RX stop/block
369a100c050SAdrian Chadd 	while (taskqueue_cancel(sc->sc_tq, &sc->sc_rxtask, NULL) != 0) {
370a100c050SAdrian Chadd 		taskqueue_drain(sc->sc_tq, &sc->sc_rxtask);
371a100c050SAdrian Chadd 	}
372a100c050SAdrian Chadd 
373f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
374f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
375f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
376f5c30c4eSAdrian Chadd 
377f0db652cSAdrian Chadd 	/*
378f0db652cSAdrian Chadd 	 * Flush any active frames from FIFO -> deferred list
379f0db652cSAdrian Chadd 	 */
38062206b76SAdrian Chadd 	ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_HP, 0);
38162206b76SAdrian Chadd 	ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_LP, 0);
3822e7620b6SAdrian Chadd 
383f0db652cSAdrian Chadd 	/*
384f0db652cSAdrian Chadd 	 * Process what's in the deferred queue
385f0db652cSAdrian Chadd 	 */
386f5c30c4eSAdrian Chadd 	/*
387f5c30c4eSAdrian Chadd 	 * XXX: If we read the tsf/channoise here and then pass it in,
388f5c30c4eSAdrian Chadd 	 * we could restore the power state before processing
389f5c30c4eSAdrian Chadd 	 * the deferred queue.
390f5c30c4eSAdrian Chadd 	 */
391f0db652cSAdrian Chadd 	ath_edma_recv_proc_deferred_queue(sc, HAL_RX_QUEUE_HP, 0);
392f0db652cSAdrian Chadd 	ath_edma_recv_proc_deferred_queue(sc, HAL_RX_QUEUE_LP, 0);
393f0db652cSAdrian Chadd 
394f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
395f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
396f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
397f5c30c4eSAdrian Chadd 
3982e7620b6SAdrian Chadd 	ATH_PCU_LOCK(sc);
3992e7620b6SAdrian Chadd 	sc->sc_rxproc_cnt--;
4002e7620b6SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
401a100c050SAdrian Chadd 
402a100c050SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_RECV | ATH_DEBUG_EDMA_RX, "%s: done\n", __func__);
40399e8d8c3SAdrian Chadd }
40499e8d8c3SAdrian Chadd 
40599e8d8c3SAdrian Chadd /*
406f0db652cSAdrian Chadd  * Process frames from the current queue into the deferred queue.
40799e8d8c3SAdrian Chadd  */
408f0db652cSAdrian Chadd static void
ath_edma_recv_proc_queue(struct ath_softc * sc,HAL_RX_QUEUE qtype,int dosched)40962206b76SAdrian Chadd ath_edma_recv_proc_queue(struct ath_softc *sc, HAL_RX_QUEUE qtype,
41062206b76SAdrian Chadd     int dosched)
41199e8d8c3SAdrian Chadd {
41299e8d8c3SAdrian Chadd 	struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
41399e8d8c3SAdrian Chadd 	struct ath_rx_status *rs;
41499e8d8c3SAdrian Chadd 	struct ath_desc *ds;
41599e8d8c3SAdrian Chadd 	struct ath_buf *bf;
41699e8d8c3SAdrian Chadd 	struct mbuf *m;
41799e8d8c3SAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
41899e8d8c3SAdrian Chadd 	uint64_t tsf;
419f0db652cSAdrian Chadd 	uint16_t nf;
420f0db652cSAdrian Chadd 	int npkts = 0;
42199e8d8c3SAdrian Chadd 
42299e8d8c3SAdrian Chadd 	tsf = ath_hal_gettsf64(ah);
42399e8d8c3SAdrian Chadd 	nf = ath_hal_getchannoise(ah, sc->sc_curchan);
42499e8d8c3SAdrian Chadd 	sc->sc_stats.ast_rx_noise = nf;
42599e8d8c3SAdrian Chadd 
426a100c050SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called; qtype=%d, dosched=%d\n", __func__, qtype, dosched);
427a100c050SAdrian Chadd 
4280b59717bSAdrian Chadd 	ATH_RX_LOCK(sc);
4290b59717bSAdrian Chadd 
43017bb5fd1SAdrian Chadd #if 1
43117bb5fd1SAdrian Chadd 	if (sc->sc_rx_resetted == 1) {
43217bb5fd1SAdrian Chadd 		/*
43317bb5fd1SAdrian Chadd 		 * XXX We shouldn't ever be scheduled if
43417bb5fd1SAdrian Chadd 		 * receive has been stopped - so complain
43517bb5fd1SAdrian Chadd 		 * loudly!
43617bb5fd1SAdrian Chadd 		 */
43717bb5fd1SAdrian Chadd 		device_printf(sc->sc_dev,
43817bb5fd1SAdrian Chadd 		    "%s: sc_rx_resetted=1! Bad!\n",
43917bb5fd1SAdrian Chadd 		    __func__);
44017bb5fd1SAdrian Chadd 		ATH_RX_UNLOCK(sc);
44117bb5fd1SAdrian Chadd 		return;
44217bb5fd1SAdrian Chadd 	}
44317bb5fd1SAdrian Chadd #endif
44417bb5fd1SAdrian Chadd 
44599e8d8c3SAdrian Chadd 	do {
44699e8d8c3SAdrian Chadd 		bf = re->m_fifo[re->m_fifo_head];
44799e8d8c3SAdrian Chadd 		/* This shouldn't occur! */
44899e8d8c3SAdrian Chadd 		if (bf == NULL) {
44999e8d8c3SAdrian Chadd 			device_printf(sc->sc_dev, "%s: Q%d: NULL bf?\n",
45099e8d8c3SAdrian Chadd 			    __func__,
45199e8d8c3SAdrian Chadd 			    qtype);
45299e8d8c3SAdrian Chadd 			break;
45399e8d8c3SAdrian Chadd 		}
45499e8d8c3SAdrian Chadd 		m = bf->bf_m;
45599e8d8c3SAdrian Chadd 		ds = bf->bf_desc;
45699e8d8c3SAdrian Chadd 
45799e8d8c3SAdrian Chadd 		/*
45899e8d8c3SAdrian Chadd 		 * Sync descriptor memory - this also syncs the buffer for us.
45999e8d8c3SAdrian Chadd 		 * EDMA descriptors are in cached memory.
46099e8d8c3SAdrian Chadd 		 */
46199e8d8c3SAdrian Chadd 		bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
4628cc724d9SAdrian Chadd 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
46399e8d8c3SAdrian Chadd 		rs = &bf->bf_status.ds_rxstat;
4640b59717bSAdrian Chadd 		bf->bf_rxstatus = ath_hal_rxprocdesc(ah, ds, bf->bf_daddr,
4650b59717bSAdrian Chadd 		    NULL, rs);
466b819e1edSAdrian Chadd 		if (bf->bf_rxstatus == HAL_EINPROGRESS)
467b819e1edSAdrian Chadd 			break;
46899e8d8c3SAdrian Chadd #ifdef	ATH_DEBUG
46999e8d8c3SAdrian Chadd 		if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
4700b59717bSAdrian Chadd 			ath_printrxbuf(sc, bf, 0, bf->bf_rxstatus == HAL_OK);
471b69b0dccSAdrian Chadd #endif /* ATH_DEBUG */
472b69b0dccSAdrian Chadd #ifdef	ATH_DEBUG_ALQ
473b69b0dccSAdrian Chadd 		if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_EDMA_RXSTATUS))
474b69b0dccSAdrian Chadd 			if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_RXSTATUS,
475b69b0dccSAdrian Chadd 			    sc->sc_rx_statuslen, (char *) ds);
476b69b0dccSAdrian Chadd #endif /* ATH_DEBUG */
47799e8d8c3SAdrian Chadd 
47899e8d8c3SAdrian Chadd 		/*
47999e8d8c3SAdrian Chadd 		 * Completed descriptor.
48099e8d8c3SAdrian Chadd 		 */
48199e8d8c3SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_EDMA_RX,
48299e8d8c3SAdrian Chadd 		    "%s: Q%d: completed!\n", __func__, qtype);
4832e7620b6SAdrian Chadd 		npkts++;
48499e8d8c3SAdrian Chadd 
48599e8d8c3SAdrian Chadd 		/*
4868cc724d9SAdrian Chadd 		 * We've been synced already, so unmap.
4878cc724d9SAdrian Chadd 		 */
4888cc724d9SAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
4898cc724d9SAdrian Chadd 
4908cc724d9SAdrian Chadd 		/*
4910b59717bSAdrian Chadd 		 * Remove the FIFO entry and place it on the completion
4920b59717bSAdrian Chadd 		 * queue.
49399e8d8c3SAdrian Chadd 		 */
49499e8d8c3SAdrian Chadd 		re->m_fifo[re->m_fifo_head] = NULL;
4955d4dedadSAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_rx_rxlist[qtype], bf, bf_list);
49699e8d8c3SAdrian Chadd 
49799e8d8c3SAdrian Chadd 		/* Bump the descriptor FIFO stats */
49899e8d8c3SAdrian Chadd 		INCR(re->m_fifo_head, re->m_fifolen);
49999e8d8c3SAdrian Chadd 		re->m_fifo_depth--;
50099e8d8c3SAdrian Chadd 		/* XXX check it doesn't fall below 0 */
50199e8d8c3SAdrian Chadd 	} while (re->m_fifo_depth > 0);
50299e8d8c3SAdrian Chadd 
5030b59717bSAdrian Chadd 	/* Append some more fresh frames to the FIFO */
5040b59717bSAdrian Chadd 	if (dosched)
5050b59717bSAdrian Chadd 		ath_edma_rxfifo_alloc(sc, qtype, re->m_fifolen);
5060b59717bSAdrian Chadd 
5070b59717bSAdrian Chadd 	ATH_RX_UNLOCK(sc);
5080b59717bSAdrian Chadd 
509f0db652cSAdrian Chadd 	/* rx signal state monitoring */
510f0db652cSAdrian Chadd 	ath_hal_rxmonitor(ah, &sc->sc_halstats, sc->sc_curchan);
511f0db652cSAdrian Chadd 
512f0db652cSAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1,
513f0db652cSAdrian Chadd 	    "ath edma rx proc: npkts=%d\n",
514f0db652cSAdrian Chadd 	    npkts);
515f0db652cSAdrian Chadd 
516f0db652cSAdrian Chadd 	return;
517f0db652cSAdrian Chadd }
518f0db652cSAdrian Chadd 
519f0db652cSAdrian Chadd /*
520f0db652cSAdrian Chadd  * Flush the deferred queue.
521f0db652cSAdrian Chadd  *
522f0db652cSAdrian Chadd  * This destructively flushes the deferred queue - it doesn't
523f0db652cSAdrian Chadd  * call the wireless stack on each mbuf.
524f0db652cSAdrian Chadd  */
525f0db652cSAdrian Chadd static void
ath_edma_flush_deferred_queue(struct ath_softc * sc)526f0db652cSAdrian Chadd ath_edma_flush_deferred_queue(struct ath_softc *sc)
527f0db652cSAdrian Chadd {
5282aeb1b35SAdrian Chadd 	struct ath_buf *bf;
529f0db652cSAdrian Chadd 
530f0db652cSAdrian Chadd 	ATH_RX_LOCK_ASSERT(sc);
5315d4dedadSAdrian Chadd 
532f0db652cSAdrian Chadd 	/* Free in one set, inside the lock */
5332aeb1b35SAdrian Chadd 	while (! TAILQ_EMPTY(&sc->sc_rx_rxlist[HAL_RX_QUEUE_LP])) {
5342aeb1b35SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_rx_rxlist[HAL_RX_QUEUE_LP]);
5352aeb1b35SAdrian Chadd 		TAILQ_REMOVE(&sc->sc_rx_rxlist[HAL_RX_QUEUE_LP], bf, bf_list);
5365d4dedadSAdrian Chadd 		/* Free the buffer/mbuf */
5375d4dedadSAdrian Chadd 		ath_edma_rxbuf_free(sc, bf);
5385d4dedadSAdrian Chadd 	}
5392aeb1b35SAdrian Chadd 	while (! TAILQ_EMPTY(&sc->sc_rx_rxlist[HAL_RX_QUEUE_HP])) {
5402aeb1b35SAdrian Chadd 		bf = TAILQ_FIRST(&sc->sc_rx_rxlist[HAL_RX_QUEUE_HP]);
5412aeb1b35SAdrian Chadd 		TAILQ_REMOVE(&sc->sc_rx_rxlist[HAL_RX_QUEUE_HP], bf, bf_list);
542f0db652cSAdrian Chadd 		/* Free the buffer/mbuf */
543f0db652cSAdrian Chadd 		ath_edma_rxbuf_free(sc, bf);
544f0db652cSAdrian Chadd 	}
545f0db652cSAdrian Chadd }
546f0db652cSAdrian Chadd 
547f0db652cSAdrian Chadd static int
ath_edma_recv_proc_deferred_queue(struct ath_softc * sc,HAL_RX_QUEUE qtype,int dosched)548f0db652cSAdrian Chadd ath_edma_recv_proc_deferred_queue(struct ath_softc *sc, HAL_RX_QUEUE qtype,
549f0db652cSAdrian Chadd     int dosched)
550f0db652cSAdrian Chadd {
551f0db652cSAdrian Chadd 	int ngood = 0;
552f0db652cSAdrian Chadd 	uint64_t tsf;
553f0db652cSAdrian Chadd 	struct ath_buf *bf, *next;
554f0db652cSAdrian Chadd 	struct ath_rx_status *rs;
555f0db652cSAdrian Chadd 	int16_t nf;
556f0db652cSAdrian Chadd 	ath_bufhead rxlist;
5578cc724d9SAdrian Chadd 	struct mbuf *m;
558f0db652cSAdrian Chadd 
559f0db652cSAdrian Chadd 	TAILQ_INIT(&rxlist);
560f0db652cSAdrian Chadd 
561f0db652cSAdrian Chadd 	nf = ath_hal_getchannoise(sc->sc_ah, sc->sc_curchan);
562f0db652cSAdrian Chadd 	/*
563f0db652cSAdrian Chadd 	 * XXX TODO: the NF/TSF should be stamped on the bufs themselves,
564f0db652cSAdrian Chadd 	 * otherwise we may end up adding in the wrong values if this
565f0db652cSAdrian Chadd 	 * is delayed too far..
566f0db652cSAdrian Chadd 	 */
567f0db652cSAdrian Chadd 	tsf = ath_hal_gettsf64(sc->sc_ah);
568f0db652cSAdrian Chadd 
569f0db652cSAdrian Chadd 	/* Copy the list over */
570f0db652cSAdrian Chadd 	ATH_RX_LOCK(sc);
5715d4dedadSAdrian Chadd 	TAILQ_CONCAT(&rxlist, &sc->sc_rx_rxlist[qtype], bf_list);
572f0db652cSAdrian Chadd 	ATH_RX_UNLOCK(sc);
573f0db652cSAdrian Chadd 
5740b59717bSAdrian Chadd 	/* Handle the completed descriptors */
5752aeb1b35SAdrian Chadd 	/*
5762aeb1b35SAdrian Chadd 	 * XXX is this SAFE call needed? The ath_buf entries
5772aeb1b35SAdrian Chadd 	 * aren't modified by ath_rx_pkt, right?
5782aeb1b35SAdrian Chadd 	 */
5790b59717bSAdrian Chadd 	TAILQ_FOREACH_SAFE(bf, &rxlist, bf_list, next) {
5800b59717bSAdrian Chadd 		/*
5810b59717bSAdrian Chadd 		 * Skip the RX descriptor status - start at the data offset
5820b59717bSAdrian Chadd 		 */
5830b59717bSAdrian Chadd 		m_adj(bf->bf_m, sc->sc_rx_statuslen);
5840b59717bSAdrian Chadd 
5850b59717bSAdrian Chadd 		/* Handle the frame */
5868cc724d9SAdrian Chadd 
5879a5a667dSAdrian Chadd 		rs = &bf->bf_status.ds_rxstat;
5888cc724d9SAdrian Chadd 		m = bf->bf_m;
5898cc724d9SAdrian Chadd 		bf->bf_m = NULL;
5908cc724d9SAdrian Chadd 		if (ath_rx_pkt(sc, rs, bf->bf_rxstatus, tsf, nf, qtype, bf, m))
5910b59717bSAdrian Chadd 			ngood++;
5920b59717bSAdrian Chadd 	}
5930b59717bSAdrian Chadd 
594f0db652cSAdrian Chadd 	if (ngood) {
595f0db652cSAdrian Chadd 		sc->sc_lastrx = tsf;
596f0db652cSAdrian Chadd 	}
597f0db652cSAdrian Chadd 
598f0db652cSAdrian Chadd 	ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1,
599f0db652cSAdrian Chadd 	    "ath edma rx deferred proc: ngood=%d\n",
600f0db652cSAdrian Chadd 	    ngood);
601f0db652cSAdrian Chadd 
6020b59717bSAdrian Chadd 	/* Free in one set, inside the lock */
6030b59717bSAdrian Chadd 	ATH_RX_LOCK(sc);
6042aeb1b35SAdrian Chadd 	while (! TAILQ_EMPTY(&rxlist)) {
6052aeb1b35SAdrian Chadd 		bf = TAILQ_FIRST(&rxlist);
6062aeb1b35SAdrian Chadd 		TAILQ_REMOVE(&rxlist, bf, bf_list);
6070b59717bSAdrian Chadd 		/* Free the buffer/mbuf */
6080b59717bSAdrian Chadd 		ath_edma_rxbuf_free(sc, bf);
6090b59717bSAdrian Chadd 	}
6100b59717bSAdrian Chadd 	ATH_RX_UNLOCK(sc);
6110b59717bSAdrian Chadd 
61262206b76SAdrian Chadd 	return (ngood);
613f8cc9b09SAdrian Chadd }
614f8cc9b09SAdrian Chadd 
615f8cc9b09SAdrian Chadd static void
ath_edma_recv_tasklet(void * arg,int npending)616f8cc9b09SAdrian Chadd ath_edma_recv_tasklet(void *arg, int npending)
617f8cc9b09SAdrian Chadd {
618f8cc9b09SAdrian Chadd 	struct ath_softc *sc = (struct ath_softc *) arg;
6197e3fc039SAdrian Chadd #ifdef	IEEE80211_SUPPORT_SUPERG
6207a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
6217e3fc039SAdrian Chadd #endif
622f8cc9b09SAdrian Chadd 
62399e8d8c3SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called; npending=%d\n",
624f8cc9b09SAdrian Chadd 	    __func__,
625f8cc9b09SAdrian Chadd 	    npending);
62699e8d8c3SAdrian Chadd 
62762206b76SAdrian Chadd 	ATH_PCU_LOCK(sc);
62862206b76SAdrian Chadd 	if (sc->sc_inreset_cnt > 0) {
62962206b76SAdrian Chadd 		device_printf(sc->sc_dev, "%s: sc_inreset_cnt > 0; skipping\n",
63062206b76SAdrian Chadd 		    __func__);
63162206b76SAdrian Chadd 		ATH_PCU_UNLOCK(sc);
63262206b76SAdrian Chadd 		return;
63362206b76SAdrian Chadd 	}
6342e7620b6SAdrian Chadd 	sc->sc_rxproc_cnt++;
63562206b76SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
63662206b76SAdrian Chadd 
637f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
638f5c30c4eSAdrian Chadd 	ath_power_set_power_state(sc, HAL_PM_AWAKE);
639f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
640f5c30c4eSAdrian Chadd 
641f0db652cSAdrian Chadd 	ath_edma_recv_proc_deferred_queue(sc, HAL_RX_QUEUE_HP, 1);
642f0db652cSAdrian Chadd 	ath_edma_recv_proc_deferred_queue(sc, HAL_RX_QUEUE_LP, 1);
643f0db652cSAdrian Chadd 
644f5c30c4eSAdrian Chadd 	/*
645f5c30c4eSAdrian Chadd 	 * XXX: If we read the tsf/channoise here and then pass it in,
646f5c30c4eSAdrian Chadd 	 * we could restore the power state before processing
647f5c30c4eSAdrian Chadd 	 * the deferred queue.
648f5c30c4eSAdrian Chadd 	 */
649f5c30c4eSAdrian Chadd 	ATH_LOCK(sc);
650f5c30c4eSAdrian Chadd 	ath_power_restore_power_state(sc);
651f5c30c4eSAdrian Chadd 	ATH_UNLOCK(sc);
652f5c30c4eSAdrian Chadd 
6532e7620b6SAdrian Chadd #ifdef	IEEE80211_SUPPORT_SUPERG
6542e7620b6SAdrian Chadd 	ieee80211_ff_age_all(ic, 100);
6552e7620b6SAdrian Chadd #endif
6562e7620b6SAdrian Chadd 	if (ath_dfs_tasklet_needed(sc, sc->sc_curchan))
6572e7620b6SAdrian Chadd 		taskqueue_enqueue(sc->sc_tq, &sc->sc_dfstask);
6582e7620b6SAdrian Chadd 
6592e7620b6SAdrian Chadd 	ATH_PCU_LOCK(sc);
6602e7620b6SAdrian Chadd 	sc->sc_rxproc_cnt--;
6612e7620b6SAdrian Chadd 	ATH_PCU_UNLOCK(sc);
662a100c050SAdrian Chadd 
663a100c050SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called; done!\n", __func__);
664f8cc9b09SAdrian Chadd }
665f8cc9b09SAdrian Chadd 
66699e8d8c3SAdrian Chadd /*
66799e8d8c3SAdrian Chadd  * Allocate an RX mbuf for the given ath_buf and initialise
66899e8d8c3SAdrian Chadd  * it for EDMA.
66999e8d8c3SAdrian Chadd  *
67099e8d8c3SAdrian Chadd  * + Allocate a 4KB mbuf;
67199e8d8c3SAdrian Chadd  * + Setup the DMA map for the given buffer;
67299e8d8c3SAdrian Chadd  * + Return that.
67399e8d8c3SAdrian Chadd  */
674f8cc9b09SAdrian Chadd static int
ath_edma_rxbuf_init(struct ath_softc * sc,struct ath_buf * bf)675f8cc9b09SAdrian Chadd ath_edma_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
676f8cc9b09SAdrian Chadd {
677f8cc9b09SAdrian Chadd 
67899e8d8c3SAdrian Chadd 	struct mbuf *m;
67999e8d8c3SAdrian Chadd 	int error;
68099e8d8c3SAdrian Chadd 	int len;
68199e8d8c3SAdrian Chadd 
6820b59717bSAdrian Chadd 	ATH_RX_LOCK_ASSERT(sc);
6830b59717bSAdrian Chadd 
684c6499eccSGleb Smirnoff 	m = m_getm(NULL, sc->sc_edma_bufsize, M_NOWAIT, MT_DATA);
68599e8d8c3SAdrian Chadd 	if (! m)
68699e8d8c3SAdrian Chadd 		return (ENOBUFS);		/* XXX ?*/
68799e8d8c3SAdrian Chadd 
68899e8d8c3SAdrian Chadd 	/* XXX warn/enforce alignment */
68999e8d8c3SAdrian Chadd 
69099e8d8c3SAdrian Chadd 	len = m->m_ext.ext_size;
69199e8d8c3SAdrian Chadd #if 0
69299e8d8c3SAdrian Chadd 	device_printf(sc->sc_dev, "%s: called: m=%p, size=%d, mtod=%p\n",
69399e8d8c3SAdrian Chadd 	    __func__,
69499e8d8c3SAdrian Chadd 	    m,
69599e8d8c3SAdrian Chadd 	    len,
69699e8d8c3SAdrian Chadd 	    mtod(m, char *));
69799e8d8c3SAdrian Chadd #endif
69899e8d8c3SAdrian Chadd 
69999e8d8c3SAdrian Chadd 	m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
70099e8d8c3SAdrian Chadd 
70199e8d8c3SAdrian Chadd 	/*
7028cc724d9SAdrian Chadd 	 * Populate ath_buf fields.
7038cc724d9SAdrian Chadd 	 */
7048cc724d9SAdrian Chadd 	bf->bf_desc = mtod(m, struct ath_desc *);
7058cc724d9SAdrian Chadd 	bf->bf_lastds = bf->bf_desc;	/* XXX only really for TX? */
7068cc724d9SAdrian Chadd 	bf->bf_m = m;
7078cc724d9SAdrian Chadd 
7088cc724d9SAdrian Chadd 	/*
7098cc724d9SAdrian Chadd 	 * Zero the descriptor and ensure it makes it out to the
7108cc724d9SAdrian Chadd 	 * bounce buffer if one is required.
7118cc724d9SAdrian Chadd 	 *
7128cc724d9SAdrian Chadd 	 * XXX PREWRITE will copy the whole buffer; we only needed it
7138cc724d9SAdrian Chadd 	 * to sync the first 32 DWORDS.  Oh well.
7148cc724d9SAdrian Chadd 	 */
7158cc724d9SAdrian Chadd 	memset(bf->bf_desc, '\0', sc->sc_rx_statuslen);
7168cc724d9SAdrian Chadd 
7178cc724d9SAdrian Chadd 	/*
71899e8d8c3SAdrian Chadd 	 * Create DMA mapping.
71999e8d8c3SAdrian Chadd 	 */
72099e8d8c3SAdrian Chadd 	error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
72199e8d8c3SAdrian Chadd 	    bf->bf_dmamap, m, bf->bf_segs, &bf->bf_nseg, BUS_DMA_NOWAIT);
7228cc724d9SAdrian Chadd 
72399e8d8c3SAdrian Chadd 	if (error != 0) {
72499e8d8c3SAdrian Chadd 		device_printf(sc->sc_dev, "%s: failed; error=%d\n",
72599e8d8c3SAdrian Chadd 		    __func__,
72699e8d8c3SAdrian Chadd 		    error);
72799e8d8c3SAdrian Chadd 		m_freem(m);
72899e8d8c3SAdrian Chadd 		return (error);
72999e8d8c3SAdrian Chadd 	}
73099e8d8c3SAdrian Chadd 
73199e8d8c3SAdrian Chadd 	/*
7328cc724d9SAdrian Chadd 	 * Set daddr to the physical mapping page.
73399e8d8c3SAdrian Chadd 	 */
73499e8d8c3SAdrian Chadd 	bf->bf_daddr = bf->bf_segs[0].ds_addr;
73599e8d8c3SAdrian Chadd 
73699e8d8c3SAdrian Chadd 	/*
7378cc724d9SAdrian Chadd 	 * Prepare for the upcoming read.
7388cc724d9SAdrian Chadd 	 *
7398cc724d9SAdrian Chadd 	 * We need to both sync some data into the buffer (the zero'ed
7408cc724d9SAdrian Chadd 	 * descriptor payload) and also prepare for the read that's going
7418cc724d9SAdrian Chadd 	 * to occur.
74299e8d8c3SAdrian Chadd 	 */
7438cc724d9SAdrian Chadd 	bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
7448cc724d9SAdrian Chadd 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
74599e8d8c3SAdrian Chadd 
74699e8d8c3SAdrian Chadd 	/* Finish! */
74799e8d8c3SAdrian Chadd 	return (0);
74899e8d8c3SAdrian Chadd }
74999e8d8c3SAdrian Chadd 
7508cc724d9SAdrian Chadd /*
7518cc724d9SAdrian Chadd  * Allocate a RX buffer.
7528cc724d9SAdrian Chadd  */
75399e8d8c3SAdrian Chadd static struct ath_buf *
ath_edma_rxbuf_alloc(struct ath_softc * sc)75499e8d8c3SAdrian Chadd ath_edma_rxbuf_alloc(struct ath_softc *sc)
75599e8d8c3SAdrian Chadd {
75699e8d8c3SAdrian Chadd 	struct ath_buf *bf;
75799e8d8c3SAdrian Chadd 	int error;
75899e8d8c3SAdrian Chadd 
7590b59717bSAdrian Chadd 	ATH_RX_LOCK_ASSERT(sc);
7600b59717bSAdrian Chadd 
76199e8d8c3SAdrian Chadd 	/* Allocate buffer */
76299e8d8c3SAdrian Chadd 	bf = TAILQ_FIRST(&sc->sc_rxbuf);
76399e8d8c3SAdrian Chadd 	/* XXX shouldn't happen upon startup? */
7648cc724d9SAdrian Chadd 	if (bf == NULL) {
76584c956dfSKyle Evans 		DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: nothing on rxbuf?!\n",
7668cc724d9SAdrian Chadd 		    __func__);
76799e8d8c3SAdrian Chadd 		return (NULL);
7688cc724d9SAdrian Chadd 	}
76999e8d8c3SAdrian Chadd 
77099e8d8c3SAdrian Chadd 	/* Remove it from the free list */
77199e8d8c3SAdrian Chadd 	TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
77299e8d8c3SAdrian Chadd 
77399e8d8c3SAdrian Chadd 	/* Assign RX mbuf to it */
77499e8d8c3SAdrian Chadd 	error = ath_edma_rxbuf_init(sc, bf);
77599e8d8c3SAdrian Chadd 	if (error != 0) {
77699e8d8c3SAdrian Chadd 		device_printf(sc->sc_dev,
77799e8d8c3SAdrian Chadd 		    "%s: bf=%p, rxbuf alloc failed! error=%d\n",
77899e8d8c3SAdrian Chadd 		    __func__,
77999e8d8c3SAdrian Chadd 		    bf,
78099e8d8c3SAdrian Chadd 		    error);
78199e8d8c3SAdrian Chadd 		TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
78299e8d8c3SAdrian Chadd 		return (NULL);
78399e8d8c3SAdrian Chadd 	}
78499e8d8c3SAdrian Chadd 
78599e8d8c3SAdrian Chadd 	return (bf);
78699e8d8c3SAdrian Chadd }
78799e8d8c3SAdrian Chadd 
78899e8d8c3SAdrian Chadd static void
ath_edma_rxbuf_free(struct ath_softc * sc,struct ath_buf * bf)78999e8d8c3SAdrian Chadd ath_edma_rxbuf_free(struct ath_softc *sc, struct ath_buf *bf)
79099e8d8c3SAdrian Chadd {
79199e8d8c3SAdrian Chadd 
7920b59717bSAdrian Chadd 	ATH_RX_LOCK_ASSERT(sc);
7930b59717bSAdrian Chadd 
79480b87f18SAdrian Chadd 	/*
79580b87f18SAdrian Chadd 	 * Only unload the frame if we haven't consumed
79680b87f18SAdrian Chadd 	 * the mbuf via ath_rx_pkt().
79780b87f18SAdrian Chadd 	 */
79899e8d8c3SAdrian Chadd 	if (bf->bf_m) {
79980b87f18SAdrian Chadd 		bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
80099e8d8c3SAdrian Chadd 		m_freem(bf->bf_m);
80199e8d8c3SAdrian Chadd 		bf->bf_m = NULL;
80299e8d8c3SAdrian Chadd 	}
80399e8d8c3SAdrian Chadd 
80499e8d8c3SAdrian Chadd 	/* XXX lock? */
80599e8d8c3SAdrian Chadd 	TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
80699e8d8c3SAdrian Chadd }
80799e8d8c3SAdrian Chadd 
80899e8d8c3SAdrian Chadd /*
80999e8d8c3SAdrian Chadd  * Allocate up to 'n' entries and push them onto the hardware FIFO.
81099e8d8c3SAdrian Chadd  *
81199e8d8c3SAdrian Chadd  * Return how many entries were successfully pushed onto the
81299e8d8c3SAdrian Chadd  * FIFO.
81399e8d8c3SAdrian Chadd  */
81499e8d8c3SAdrian Chadd static int
ath_edma_rxfifo_alloc(struct ath_softc * sc,HAL_RX_QUEUE qtype,int nbufs)81599e8d8c3SAdrian Chadd ath_edma_rxfifo_alloc(struct ath_softc *sc, HAL_RX_QUEUE qtype, int nbufs)
81699e8d8c3SAdrian Chadd {
81799e8d8c3SAdrian Chadd 	struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
81899e8d8c3SAdrian Chadd 	struct ath_buf *bf;
81999e8d8c3SAdrian Chadd 	int i;
82099e8d8c3SAdrian Chadd 
8210b59717bSAdrian Chadd 	ATH_RX_LOCK_ASSERT(sc);
8220b59717bSAdrian Chadd 
82399e8d8c3SAdrian Chadd 	/*
82499e8d8c3SAdrian Chadd 	 * Allocate buffers until the FIFO is full or nbufs is reached.
82599e8d8c3SAdrian Chadd 	 */
82699e8d8c3SAdrian Chadd 	for (i = 0; i < nbufs && re->m_fifo_depth < re->m_fifolen; i++) {
82799e8d8c3SAdrian Chadd 		/* Ensure the FIFO is already blank, complain loudly! */
82899e8d8c3SAdrian Chadd 		if (re->m_fifo[re->m_fifo_tail] != NULL) {
82999e8d8c3SAdrian Chadd 			device_printf(sc->sc_dev,
83099e8d8c3SAdrian Chadd 			    "%s: Q%d: fifo[%d] != NULL (%p)\n",
83199e8d8c3SAdrian Chadd 			    __func__,
83299e8d8c3SAdrian Chadd 			    qtype,
83399e8d8c3SAdrian Chadd 			    re->m_fifo_tail,
83499e8d8c3SAdrian Chadd 			    re->m_fifo[re->m_fifo_tail]);
83599e8d8c3SAdrian Chadd 
83699e8d8c3SAdrian Chadd 			/* Free the slot */
83799e8d8c3SAdrian Chadd 			ath_edma_rxbuf_free(sc, re->m_fifo[re->m_fifo_tail]);
83899e8d8c3SAdrian Chadd 			re->m_fifo_depth--;
83999e8d8c3SAdrian Chadd 			/* XXX check it's not < 0 */
84099e8d8c3SAdrian Chadd 			re->m_fifo[re->m_fifo_tail] = NULL;
84199e8d8c3SAdrian Chadd 		}
84299e8d8c3SAdrian Chadd 
84399e8d8c3SAdrian Chadd 		bf = ath_edma_rxbuf_alloc(sc);
84499e8d8c3SAdrian Chadd 		/* XXX should ensure the FIFO is not NULL? */
84599e8d8c3SAdrian Chadd 		if (bf == NULL) {
84684c956dfSKyle Evans 			DPRINTF(sc, ATH_DEBUG_EDMA_RX,
847d50e882aSAdrian Chadd 			    "%s: Q%d: alloc failed: i=%d, nbufs=%d?\n",
84899e8d8c3SAdrian Chadd 			    __func__,
849d50e882aSAdrian Chadd 			    qtype,
850d50e882aSAdrian Chadd 			    i,
851d50e882aSAdrian Chadd 			    nbufs);
85299e8d8c3SAdrian Chadd 			break;
85399e8d8c3SAdrian Chadd 		}
85499e8d8c3SAdrian Chadd 
85599e8d8c3SAdrian Chadd 		re->m_fifo[re->m_fifo_tail] = bf;
85699e8d8c3SAdrian Chadd 
85799e8d8c3SAdrian Chadd 		/* Write to the RX FIFO */
8588cc724d9SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_EDMA_RX,
8598cc724d9SAdrian Chadd 		    "%s: Q%d: putrxbuf=%p (0x%jx)\n",
86099e8d8c3SAdrian Chadd 		    __func__,
86199e8d8c3SAdrian Chadd 		    qtype,
8628cc724d9SAdrian Chadd 		    bf->bf_desc,
8638cc724d9SAdrian Chadd 		    (uintmax_t) bf->bf_daddr);
86499e8d8c3SAdrian Chadd 		ath_hal_putrxbuf(sc->sc_ah, bf->bf_daddr, qtype);
86599e8d8c3SAdrian Chadd 
86699e8d8c3SAdrian Chadd 		re->m_fifo_depth++;
86799e8d8c3SAdrian Chadd 		INCR(re->m_fifo_tail, re->m_fifolen);
86899e8d8c3SAdrian Chadd 	}
86999e8d8c3SAdrian Chadd 
87099e8d8c3SAdrian Chadd 	/*
87199e8d8c3SAdrian Chadd 	 * Return how many were allocated.
87299e8d8c3SAdrian Chadd 	 */
87399e8d8c3SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: Q%d: nbufs=%d, nalloced=%d\n",
87499e8d8c3SAdrian Chadd 	    __func__,
87599e8d8c3SAdrian Chadd 	    qtype,
87699e8d8c3SAdrian Chadd 	    nbufs,
87799e8d8c3SAdrian Chadd 	    i);
87899e8d8c3SAdrian Chadd 	return (i);
87999e8d8c3SAdrian Chadd }
88099e8d8c3SAdrian Chadd 
88199e8d8c3SAdrian Chadd static int
ath_edma_rxfifo_flush(struct ath_softc * sc,HAL_RX_QUEUE qtype)88299e8d8c3SAdrian Chadd ath_edma_rxfifo_flush(struct ath_softc *sc, HAL_RX_QUEUE qtype)
88399e8d8c3SAdrian Chadd {
88499e8d8c3SAdrian Chadd 	struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
88599e8d8c3SAdrian Chadd 	int i;
88699e8d8c3SAdrian Chadd 
8870b59717bSAdrian Chadd 	ATH_RX_LOCK_ASSERT(sc);
8880b59717bSAdrian Chadd 
88999e8d8c3SAdrian Chadd 	for (i = 0; i < re->m_fifolen; i++) {
89099e8d8c3SAdrian Chadd 		if (re->m_fifo[i] != NULL) {
89199e8d8c3SAdrian Chadd #ifdef	ATH_DEBUG
8920f078d63SJohn Baldwin 			struct ath_buf *bf = re->m_fifo[i];
8930f078d63SJohn Baldwin 
89499e8d8c3SAdrian Chadd 			if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
89599e8d8c3SAdrian Chadd 				ath_printrxbuf(sc, bf, 0, HAL_OK);
89699e8d8c3SAdrian Chadd #endif
89799e8d8c3SAdrian Chadd 			ath_edma_rxbuf_free(sc, re->m_fifo[i]);
89899e8d8c3SAdrian Chadd 			re->m_fifo[i] = NULL;
89999e8d8c3SAdrian Chadd 			re->m_fifo_depth--;
90099e8d8c3SAdrian Chadd 		}
90199e8d8c3SAdrian Chadd 	}
90299e8d8c3SAdrian Chadd 
90399e8d8c3SAdrian Chadd 	if (re->m_rxpending != NULL) {
90499e8d8c3SAdrian Chadd 		m_freem(re->m_rxpending);
90599e8d8c3SAdrian Chadd 		re->m_rxpending = NULL;
90699e8d8c3SAdrian Chadd 	}
90799e8d8c3SAdrian Chadd 	re->m_fifo_head = re->m_fifo_tail = re->m_fifo_depth = 0;
90899e8d8c3SAdrian Chadd 
90999e8d8c3SAdrian Chadd 	return (0);
91099e8d8c3SAdrian Chadd }
91199e8d8c3SAdrian Chadd 
91299e8d8c3SAdrian Chadd /*
91399e8d8c3SAdrian Chadd  * Setup the initial RX FIFO structure.
91499e8d8c3SAdrian Chadd  */
91599e8d8c3SAdrian Chadd static int
ath_edma_setup_rxfifo(struct ath_softc * sc,HAL_RX_QUEUE qtype)91699e8d8c3SAdrian Chadd ath_edma_setup_rxfifo(struct ath_softc *sc, HAL_RX_QUEUE qtype)
91799e8d8c3SAdrian Chadd {
91899e8d8c3SAdrian Chadd 	struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
91999e8d8c3SAdrian Chadd 
9200b59717bSAdrian Chadd 	ATH_RX_LOCK_ASSERT(sc);
9210b59717bSAdrian Chadd 
92299e8d8c3SAdrian Chadd 	if (! ath_hal_getrxfifodepth(sc->sc_ah, qtype, &re->m_fifolen)) {
92399e8d8c3SAdrian Chadd 		device_printf(sc->sc_dev, "%s: qtype=%d, failed\n",
92499e8d8c3SAdrian Chadd 		    __func__,
92599e8d8c3SAdrian Chadd 		    qtype);
92699e8d8c3SAdrian Chadd 		return (-EINVAL);
92799e8d8c3SAdrian Chadd 	}
928516a0ac2SAdrian Chadd 
929516a0ac2SAdrian Chadd 	if (bootverbose)
930516a0ac2SAdrian Chadd 		device_printf(sc->sc_dev,
931516a0ac2SAdrian Chadd 		    "%s: type=%d, FIFO depth = %d entries\n",
93299e8d8c3SAdrian Chadd 		    __func__,
93399e8d8c3SAdrian Chadd 		    qtype,
93499e8d8c3SAdrian Chadd 		    re->m_fifolen);
93599e8d8c3SAdrian Chadd 
93699e8d8c3SAdrian Chadd 	/* Allocate ath_buf FIFO array, pre-zero'ed */
937ac2fffa4SPedro F. Giffuni 	re->m_fifo = malloc(sizeof(struct ath_buf *) * re->m_fifolen,
938ac2fffa4SPedro F. Giffuni 	    M_ATHDEV,
939ac2fffa4SPedro F. Giffuni 	    M_NOWAIT | M_ZERO);
94099e8d8c3SAdrian Chadd 	if (re->m_fifo == NULL) {
94199e8d8c3SAdrian Chadd 		device_printf(sc->sc_dev, "%s: malloc failed\n",
94299e8d8c3SAdrian Chadd 		    __func__);
94399e8d8c3SAdrian Chadd 		return (-ENOMEM);
94499e8d8c3SAdrian Chadd 	}
94599e8d8c3SAdrian Chadd 
94699e8d8c3SAdrian Chadd 	/*
94799e8d8c3SAdrian Chadd 	 * Set initial "empty" state.
94899e8d8c3SAdrian Chadd 	 */
94999e8d8c3SAdrian Chadd 	re->m_rxpending = NULL;
95099e8d8c3SAdrian Chadd 	re->m_fifo_head = re->m_fifo_tail = re->m_fifo_depth = 0;
95199e8d8c3SAdrian Chadd 
95299e8d8c3SAdrian Chadd 	return (0);
95399e8d8c3SAdrian Chadd }
95499e8d8c3SAdrian Chadd 
95599e8d8c3SAdrian Chadd static int
ath_edma_rxfifo_free(struct ath_softc * sc,HAL_RX_QUEUE qtype)95699e8d8c3SAdrian Chadd ath_edma_rxfifo_free(struct ath_softc *sc, HAL_RX_QUEUE qtype)
95799e8d8c3SAdrian Chadd {
95899e8d8c3SAdrian Chadd 	struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
95999e8d8c3SAdrian Chadd 
96099e8d8c3SAdrian Chadd 	device_printf(sc->sc_dev, "%s: called; qtype=%d\n",
96199e8d8c3SAdrian Chadd 	    __func__,
96299e8d8c3SAdrian Chadd 	    qtype);
96399e8d8c3SAdrian Chadd 
96499e8d8c3SAdrian Chadd 	free(re->m_fifo, M_ATHDEV);
96599e8d8c3SAdrian Chadd 
96699e8d8c3SAdrian Chadd 	return (0);
96799e8d8c3SAdrian Chadd }
96899e8d8c3SAdrian Chadd 
96999e8d8c3SAdrian Chadd static int
ath_edma_dma_rxsetup(struct ath_softc * sc)97099e8d8c3SAdrian Chadd ath_edma_dma_rxsetup(struct ath_softc *sc)
97199e8d8c3SAdrian Chadd {
97299e8d8c3SAdrian Chadd 	int error;
97399e8d8c3SAdrian Chadd 
97439abbd9bSAdrian Chadd 	/*
97539abbd9bSAdrian Chadd 	 * Create RX DMA tag and buffers.
97639abbd9bSAdrian Chadd 	 */
97739abbd9bSAdrian Chadd 	error = ath_descdma_setup_rx_edma(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
97839abbd9bSAdrian Chadd 	    "rx", ath_rxbuf, sc->sc_rx_statuslen);
97999e8d8c3SAdrian Chadd 	if (error != 0)
98099e8d8c3SAdrian Chadd 		return error;
98199e8d8c3SAdrian Chadd 
9820b59717bSAdrian Chadd 	ATH_RX_LOCK(sc);
98399e8d8c3SAdrian Chadd 	(void) ath_edma_setup_rxfifo(sc, HAL_RX_QUEUE_HP);
98499e8d8c3SAdrian Chadd 	(void) ath_edma_setup_rxfifo(sc, HAL_RX_QUEUE_LP);
9850b59717bSAdrian Chadd 	ATH_RX_UNLOCK(sc);
98699e8d8c3SAdrian Chadd 
98799e8d8c3SAdrian Chadd 	return (0);
98899e8d8c3SAdrian Chadd }
98999e8d8c3SAdrian Chadd 
99099e8d8c3SAdrian Chadd static int
ath_edma_dma_rxteardown(struct ath_softc * sc)99199e8d8c3SAdrian Chadd ath_edma_dma_rxteardown(struct ath_softc *sc)
99299e8d8c3SAdrian Chadd {
99399e8d8c3SAdrian Chadd 
9940b59717bSAdrian Chadd 	ATH_RX_LOCK(sc);
995f0db652cSAdrian Chadd 	ath_edma_flush_deferred_queue(sc);
99699e8d8c3SAdrian Chadd 	ath_edma_rxfifo_flush(sc, HAL_RX_QUEUE_HP);
99799e8d8c3SAdrian Chadd 	ath_edma_rxfifo_free(sc, HAL_RX_QUEUE_HP);
99899e8d8c3SAdrian Chadd 
99999e8d8c3SAdrian Chadd 	ath_edma_rxfifo_flush(sc, HAL_RX_QUEUE_LP);
100099e8d8c3SAdrian Chadd 	ath_edma_rxfifo_free(sc, HAL_RX_QUEUE_LP);
10010b59717bSAdrian Chadd 	ATH_RX_UNLOCK(sc);
100299e8d8c3SAdrian Chadd 
100399e8d8c3SAdrian Chadd 	/* Free RX ath_buf */
100499e8d8c3SAdrian Chadd 	/* Free RX DMA tag */
100599e8d8c3SAdrian Chadd 	if (sc->sc_rxdma.dd_desc_len != 0)
100699e8d8c3SAdrian Chadd 		ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
100799e8d8c3SAdrian Chadd 
100899e8d8c3SAdrian Chadd 	return (0);
1009f8cc9b09SAdrian Chadd }
1010f8cc9b09SAdrian Chadd 
1011f8cc9b09SAdrian Chadd void
ath_recv_setup_edma(struct ath_softc * sc)1012f8cc9b09SAdrian Chadd ath_recv_setup_edma(struct ath_softc *sc)
1013f8cc9b09SAdrian Chadd {
1014f8cc9b09SAdrian Chadd 
101599e8d8c3SAdrian Chadd 	/* Set buffer size to 4k */
101699e8d8c3SAdrian Chadd 	sc->sc_edma_bufsize = 4096;
101799e8d8c3SAdrian Chadd 
101899e8d8c3SAdrian Chadd 	/* Fetch EDMA field and buffer sizes */
101999e8d8c3SAdrian Chadd 	(void) ath_hal_getrxstatuslen(sc->sc_ah, &sc->sc_rx_statuslen);
102099e8d8c3SAdrian Chadd 
102139abbd9bSAdrian Chadd 	/* Configure the hardware with the RX buffer size */
102239abbd9bSAdrian Chadd 	(void) ath_hal_setrxbufsize(sc->sc_ah, sc->sc_edma_bufsize -
102339abbd9bSAdrian Chadd 	    sc->sc_rx_statuslen);
102439abbd9bSAdrian Chadd 
1025516a0ac2SAdrian Chadd 	if (bootverbose) {
102699e8d8c3SAdrian Chadd 		device_printf(sc->sc_dev, "RX status length: %d\n",
102799e8d8c3SAdrian Chadd 		    sc->sc_rx_statuslen);
10283fdfc330SAdrian Chadd 		device_printf(sc->sc_dev, "RX buffer size: %d\n",
102999e8d8c3SAdrian Chadd 		    sc->sc_edma_bufsize);
1030516a0ac2SAdrian Chadd 	}
103199e8d8c3SAdrian Chadd 
1032f8cc9b09SAdrian Chadd 	sc->sc_rx.recv_stop = ath_edma_stoprecv;
1033f8cc9b09SAdrian Chadd 	sc->sc_rx.recv_start = ath_edma_startrecv;
1034f8cc9b09SAdrian Chadd 	sc->sc_rx.recv_flush = ath_edma_recv_flush;
1035f8cc9b09SAdrian Chadd 	sc->sc_rx.recv_tasklet = ath_edma_recv_tasklet;
1036f8cc9b09SAdrian Chadd 	sc->sc_rx.recv_rxbuf_init = ath_edma_rxbuf_init;
103799e8d8c3SAdrian Chadd 
103899e8d8c3SAdrian Chadd 	sc->sc_rx.recv_setup = ath_edma_dma_rxsetup;
103999e8d8c3SAdrian Chadd 	sc->sc_rx.recv_teardown = ath_edma_dma_rxteardown;
1040f0db652cSAdrian Chadd 
1041f0db652cSAdrian Chadd 	sc->sc_rx.recv_sched = ath_edma_recv_sched;
1042f0db652cSAdrian Chadd 	sc->sc_rx.recv_sched_queue = ath_edma_recv_sched_queue;
1043f8cc9b09SAdrian Chadd }
1044