2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Adrian Chadd <adrian@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 * redistribution must be conditioned upon including a substantially
16 * similar Disclaimer requirement for further binary redistribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGES.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 * Driver for the Atheros Wireless LAN controller.
38 * This software is derived from work of Atsushi Onoe; his contribution
39 * is greatly appreciated.
45 * This is needed for register operations which are performed
46 * by the driver - eg, calls to ath_hal_gettsf32().
48 * It's also required for any AH_DEBUG checks in here, eg the
49 * module dependencies.
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/sysctl.h>
58 #include <sys/malloc.h>
60 #include <sys/mutex.h>
61 #include <sys/kernel.h>
62 #include <sys/socket.h>
63 #include <sys/sockio.h>
64 #include <sys/errno.h>
65 #include <sys/callout.h>
67 #include <sys/endian.h>
68 #include <sys/kthread.h>
69 #include <sys/taskqueue.h>
71 #include <sys/module.h>
73 #include <sys/smp.h> /* for mp_ncpus */
75 #include <machine/bus.h>
78 #include <net/if_var.h>
79 #include <net/if_dl.h>
80 #include <net/if_media.h>
81 #include <net/if_types.h>
82 #include <net/if_arp.h>
83 #include <net/ethernet.h>
84 #include <net/if_llc.h>
86 #include <net80211/ieee80211_var.h>
87 #include <net80211/ieee80211_regdomain.h>
88 #ifdef IEEE80211_SUPPORT_SUPERG
89 #include <net80211/ieee80211_superg.h>
91 #ifdef IEEE80211_SUPPORT_TDMA
92 #include <net80211/ieee80211_tdma.h>
98 #include <netinet/in.h>
99 #include <netinet/if_ether.h>
102 #include <dev/ath/if_athvar.h>
103 #include <dev/ath/ath_hal/ah_devid.h> /* XXX for softled */
104 #include <dev/ath/ath_hal/ah_diagcodes.h>
106 #include <dev/ath/if_ath_debug.h>
107 #include <dev/ath/if_ath_misc.h>
108 #include <dev/ath/if_ath_tsf.h>
109 #include <dev/ath/if_ath_tx.h>
110 #include <dev/ath/if_ath_sysctl.h>
111 #include <dev/ath/if_ath_led.h>
112 #include <dev/ath/if_ath_keycache.h>
113 #include <dev/ath/if_ath_rx.h>
114 #include <dev/ath/if_ath_beacon.h>
115 #include <dev/ath/if_athdfs.h>
116 #include <dev/ath/if_ath_descdma.h>
119 #include <dev/ath/ath_tx99/ath_tx99.h>
122 #include <dev/ath/if_ath_rx_edma.h>
125 #include <dev/ath/if_ath_alq.h>
129 * some general macros
131 #define INCR(_l, _sz) (_l) ++; (_l) &= ((_sz) - 1)
132 #define DECR(_l, _sz) (_l) --; (_l) &= ((_sz) - 1)
134 MALLOC_DECLARE(M_ATHDEV);
139 * + Make sure the FIFO is correctly flushed and reinitialised
141 * + Verify multi-descriptor frames work!
142 * + There's a "memory use after free" which needs to be tracked down
143 * and fixed ASAP. I've seen this in the legacy path too, so it
144 * may be a generic RX path issue.
148 * XXX shuffle the function orders so these pre-declarations aren't
151 static int ath_edma_rxfifo_alloc(struct ath_softc *sc, HAL_RX_QUEUE qtype,
153 static int ath_edma_rxfifo_flush(struct ath_softc *sc, HAL_RX_QUEUE qtype);
154 static void ath_edma_rxbuf_free(struct ath_softc *sc, struct ath_buf *bf);
155 static void ath_edma_recv_proc_queue(struct ath_softc *sc,
156 HAL_RX_QUEUE qtype, int dosched);
157 static int ath_edma_recv_proc_deferred_queue(struct ath_softc *sc,
158 HAL_RX_QUEUE qtype, int dosched);
161 ath_edma_stoprecv(struct ath_softc *sc, int dodelay)
163 struct ath_hal *ah = sc->sc_ah;
165 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called, dodelay=%d\n",
170 ath_hal_stoppcurecv(ah);
171 ath_hal_setrxfilter(ah, 0);
176 if (ath_hal_stopdmarecv(ah) == AH_TRUE)
177 sc->sc_rx_stopped = 1;
180 * Give the various bus FIFOs (not EDMA descriptor FIFO)
181 * time to finish flushing out data.
185 /* Flush RX pending for each queue */
186 /* XXX should generic-ify this */
187 if (sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending) {
188 m_freem(sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending);
189 sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending = NULL;
192 if (sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending) {
193 m_freem(sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending);
194 sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending = NULL;
198 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: done\n", __func__);
202 * Re-initialise the FIFO given the current buffer contents.
203 * Specifically, walk from head -> tail, pushing the FIFO contents
204 * back into the FIFO.
207 ath_edma_reinit_fifo(struct ath_softc *sc, HAL_RX_QUEUE qtype)
209 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
213 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called\n", __func__);
215 ATH_RX_LOCK_ASSERT(sc);
218 for (j = 0; j < re->m_fifo_depth; j++) {
220 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
221 "%s: Q%d: pos=%i, addr=0x%jx\n",
225 (uintmax_t)bf->bf_daddr);
226 ath_hal_putrxbuf(sc->sc_ah, bf->bf_daddr, qtype);
227 INCR(i, re->m_fifolen);
230 /* Ensure this worked out right */
231 if (i != re->m_fifo_tail) {
232 device_printf(sc->sc_dev, "%s: i (%d) != tail! (%d)\n",
237 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: done\n", __func__);
244 ath_edma_startrecv(struct ath_softc *sc)
246 struct ath_hal *ah = sc->sc_ah;
248 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
249 "%s: called; resetted=%d, stopped=%d\n", __func__,
250 sc->sc_rx_resetted, sc->sc_rx_stopped);
255 * Sanity check - are we being called whilst RX
256 * isn't stopped? If so, we may end up pushing
257 * too many entries into the RX FIFO and
265 * In theory the hardware has been initialised, right?
267 if (sc->sc_rx_resetted == 1 || sc->sc_rx_stopped == 1) {
268 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
269 "%s: Re-initing HP FIFO\n", __func__);
270 ath_edma_reinit_fifo(sc, HAL_RX_QUEUE_HP);
271 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
272 "%s: Re-initing LP FIFO\n", __func__);
273 ath_edma_reinit_fifo(sc, HAL_RX_QUEUE_LP);
274 sc->sc_rx_resetted = 0;
276 device_printf(sc->sc_dev,
277 "%s: called without resetting chip? "
278 "resetted=%d, stopped=%d\n",
284 /* Add up to m_fifolen entries in each queue */
286 * These must occur after the above write so the FIFO buffers
287 * are pushed/tracked in the same order as the hardware will
290 * XXX TODO: is this really necessary? We should've stopped
291 * the hardware already and reinitialised it, so it's a no-op.
293 ath_edma_rxfifo_alloc(sc, HAL_RX_QUEUE_HP,
294 sc->sc_rxedma[HAL_RX_QUEUE_HP].m_fifolen);
296 ath_edma_rxfifo_alloc(sc, HAL_RX_QUEUE_LP,
297 sc->sc_rxedma[HAL_RX_QUEUE_LP].m_fifolen);
300 ath_hal_startpcurecv(ah, (!! sc->sc_scanning));
303 * We're now doing RX DMA!
305 sc->sc_rx_stopped = 0;
308 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: ready\n", __func__);
314 ath_edma_recv_sched_queue(struct ath_softc *sc, HAL_RX_QUEUE qtype,
317 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called; qtype=%d, dosched=%d\n",
318 __func__, qtype, dosched);
321 ath_power_set_power_state(sc, HAL_PM_AWAKE);
324 ath_edma_recv_proc_queue(sc, qtype, dosched);
327 ath_power_restore_power_state(sc);
330 /* XXX TODO: methodize */
331 taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
333 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: done\n", __func__);
337 ath_edma_recv_sched(struct ath_softc *sc, int dosched)
340 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called; dosched=%d\n",
344 ath_power_set_power_state(sc, HAL_PM_AWAKE);
347 ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_HP, dosched);
348 ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_LP, dosched);
351 ath_power_restore_power_state(sc);
354 /* XXX TODO: methodize */
355 taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
357 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: done\n", __func__);
361 ath_edma_recv_flush(struct ath_softc *sc)
364 DPRINTF(sc, ATH_DEBUG_RECV | ATH_DEBUG_EDMA_RX, "%s: called\n", __func__);
370 // XXX TODO: methodize; make it an RX stop/block
371 while (taskqueue_cancel(sc->sc_tq, &sc->sc_rxtask, NULL) != 0) {
372 taskqueue_drain(sc->sc_tq, &sc->sc_rxtask);
376 ath_power_set_power_state(sc, HAL_PM_AWAKE);
380 * Flush any active frames from FIFO -> deferred list
382 ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_HP, 0);
383 ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_LP, 0);
386 * Process what's in the deferred queue
389 * XXX: If we read the tsf/channoise here and then pass it in,
390 * we could restore the power state before processing
391 * the deferred queue.
393 ath_edma_recv_proc_deferred_queue(sc, HAL_RX_QUEUE_HP, 0);
394 ath_edma_recv_proc_deferred_queue(sc, HAL_RX_QUEUE_LP, 0);
397 ath_power_restore_power_state(sc);
404 DPRINTF(sc, ATH_DEBUG_RECV | ATH_DEBUG_EDMA_RX, "%s: done\n", __func__);
408 * Process frames from the current queue into the deferred queue.
411 ath_edma_recv_proc_queue(struct ath_softc *sc, HAL_RX_QUEUE qtype,
414 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
415 struct ath_rx_status *rs;
419 struct ath_hal *ah = sc->sc_ah;
424 tsf = ath_hal_gettsf64(ah);
425 nf = ath_hal_getchannoise(ah, sc->sc_curchan);
426 sc->sc_stats.ast_rx_noise = nf;
428 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called; qtype=%d, dosched=%d\n", __func__, qtype, dosched);
433 if (sc->sc_rx_resetted == 1) {
435 * XXX We shouldn't ever be scheduled if
436 * receive has been stopped - so complain
439 device_printf(sc->sc_dev,
440 "%s: sc_rx_resetted=1! Bad!\n",
448 bf = re->m_fifo[re->m_fifo_head];
449 /* This shouldn't occur! */
451 device_printf(sc->sc_dev, "%s: Q%d: NULL bf?\n",
460 * Sync descriptor memory - this also syncs the buffer for us.
461 * EDMA descriptors are in cached memory.
463 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
464 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
465 rs = &bf->bf_status.ds_rxstat;
466 bf->bf_rxstatus = ath_hal_rxprocdesc(ah, ds, bf->bf_daddr,
468 if (bf->bf_rxstatus == HAL_EINPROGRESS)
471 if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
472 ath_printrxbuf(sc, bf, 0, bf->bf_rxstatus == HAL_OK);
473 #endif /* ATH_DEBUG */
475 if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_EDMA_RXSTATUS))
476 if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_RXSTATUS,
477 sc->sc_rx_statuslen, (char *) ds);
478 #endif /* ATH_DEBUG */
481 * Completed descriptor.
483 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
484 "%s: Q%d: completed!\n", __func__, qtype);
488 * We've been synced already, so unmap.
490 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
493 * Remove the FIFO entry and place it on the completion
496 re->m_fifo[re->m_fifo_head] = NULL;
497 TAILQ_INSERT_TAIL(&sc->sc_rx_rxlist[qtype], bf, bf_list);
499 /* Bump the descriptor FIFO stats */
500 INCR(re->m_fifo_head, re->m_fifolen);
502 /* XXX check it doesn't fall below 0 */
503 } while (re->m_fifo_depth > 0);
505 /* Append some more fresh frames to the FIFO */
507 ath_edma_rxfifo_alloc(sc, qtype, re->m_fifolen);
511 /* rx signal state monitoring */
512 ath_hal_rxmonitor(ah, &sc->sc_halstats, sc->sc_curchan);
514 ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1,
515 "ath edma rx proc: npkts=%d\n",
522 * Flush the deferred queue.
524 * This destructively flushes the deferred queue - it doesn't
525 * call the wireless stack on each mbuf.
528 ath_edma_flush_deferred_queue(struct ath_softc *sc)
532 ATH_RX_LOCK_ASSERT(sc);
534 /* Free in one set, inside the lock */
535 while (! TAILQ_EMPTY(&sc->sc_rx_rxlist[HAL_RX_QUEUE_LP])) {
536 bf = TAILQ_FIRST(&sc->sc_rx_rxlist[HAL_RX_QUEUE_LP]);
537 TAILQ_REMOVE(&sc->sc_rx_rxlist[HAL_RX_QUEUE_LP], bf, bf_list);
538 /* Free the buffer/mbuf */
539 ath_edma_rxbuf_free(sc, bf);
541 while (! TAILQ_EMPTY(&sc->sc_rx_rxlist[HAL_RX_QUEUE_HP])) {
542 bf = TAILQ_FIRST(&sc->sc_rx_rxlist[HAL_RX_QUEUE_HP]);
543 TAILQ_REMOVE(&sc->sc_rx_rxlist[HAL_RX_QUEUE_HP], bf, bf_list);
544 /* Free the buffer/mbuf */
545 ath_edma_rxbuf_free(sc, bf);
550 ath_edma_recv_proc_deferred_queue(struct ath_softc *sc, HAL_RX_QUEUE qtype,
555 struct ath_buf *bf, *next;
556 struct ath_rx_status *rs;
563 nf = ath_hal_getchannoise(sc->sc_ah, sc->sc_curchan);
565 * XXX TODO: the NF/TSF should be stamped on the bufs themselves,
566 * otherwise we may end up adding in the wrong values if this
567 * is delayed too far..
569 tsf = ath_hal_gettsf64(sc->sc_ah);
571 /* Copy the list over */
573 TAILQ_CONCAT(&rxlist, &sc->sc_rx_rxlist[qtype], bf_list);
576 /* Handle the completed descriptors */
578 * XXX is this SAFE call needed? The ath_buf entries
579 * aren't modified by ath_rx_pkt, right?
581 TAILQ_FOREACH_SAFE(bf, &rxlist, bf_list, next) {
583 * Skip the RX descriptor status - start at the data offset
585 m_adj(bf->bf_m, sc->sc_rx_statuslen);
587 /* Handle the frame */
589 rs = &bf->bf_status.ds_rxstat;
592 if (ath_rx_pkt(sc, rs, bf->bf_rxstatus, tsf, nf, qtype, bf, m))
600 ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1,
601 "ath edma rx deferred proc: ngood=%d\n",
604 /* Free in one set, inside the lock */
606 while (! TAILQ_EMPTY(&rxlist)) {
607 bf = TAILQ_FIRST(&rxlist);
608 TAILQ_REMOVE(&rxlist, bf, bf_list);
609 /* Free the buffer/mbuf */
610 ath_edma_rxbuf_free(sc, bf);
618 ath_edma_recv_tasklet(void *arg, int npending)
620 struct ath_softc *sc = (struct ath_softc *) arg;
621 #ifdef IEEE80211_SUPPORT_SUPERG
622 struct ieee80211com *ic = &sc->sc_ic;
625 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called; npending=%d\n",
630 if (sc->sc_inreset_cnt > 0) {
631 device_printf(sc->sc_dev, "%s: sc_inreset_cnt > 0; skipping\n",
640 ath_power_set_power_state(sc, HAL_PM_AWAKE);
643 ath_edma_recv_proc_deferred_queue(sc, HAL_RX_QUEUE_HP, 1);
644 ath_edma_recv_proc_deferred_queue(sc, HAL_RX_QUEUE_LP, 1);
647 * XXX: If we read the tsf/channoise here and then pass it in,
648 * we could restore the power state before processing
649 * the deferred queue.
652 ath_power_restore_power_state(sc);
655 #ifdef IEEE80211_SUPPORT_SUPERG
656 ieee80211_ff_age_all(ic, 100);
658 if (ath_dfs_tasklet_needed(sc, sc->sc_curchan))
659 taskqueue_enqueue(sc->sc_tq, &sc->sc_dfstask);
665 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called; done!\n", __func__);
669 * Allocate an RX mbuf for the given ath_buf and initialise
672 * + Allocate a 4KB mbuf;
673 * + Setup the DMA map for the given buffer;
677 ath_edma_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
684 ATH_RX_LOCK_ASSERT(sc);
686 m = m_getm(NULL, sc->sc_edma_bufsize, M_NOWAIT, MT_DATA);
688 return (ENOBUFS); /* XXX ?*/
690 /* XXX warn/enforce alignment */
692 len = m->m_ext.ext_size;
694 device_printf(sc->sc_dev, "%s: called: m=%p, size=%d, mtod=%p\n",
701 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
704 * Populate ath_buf fields.
706 bf->bf_desc = mtod(m, struct ath_desc *);
707 bf->bf_lastds = bf->bf_desc; /* XXX only really for TX? */
711 * Zero the descriptor and ensure it makes it out to the
712 * bounce buffer if one is required.
714 * XXX PREWRITE will copy the whole buffer; we only needed it
715 * to sync the first 32 DWORDS. Oh well.
717 memset(bf->bf_desc, '\0', sc->sc_rx_statuslen);
720 * Create DMA mapping.
722 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
723 bf->bf_dmamap, m, bf->bf_segs, &bf->bf_nseg, BUS_DMA_NOWAIT);
726 device_printf(sc->sc_dev, "%s: failed; error=%d\n",
734 * Set daddr to the physical mapping page.
736 bf->bf_daddr = bf->bf_segs[0].ds_addr;
739 * Prepare for the upcoming read.
741 * We need to both sync some data into the buffer (the zero'ed
742 * descriptor payload) and also prepare for the read that's going
745 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
746 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
753 * Allocate a RX buffer.
755 static struct ath_buf *
756 ath_edma_rxbuf_alloc(struct ath_softc *sc)
761 ATH_RX_LOCK_ASSERT(sc);
763 /* Allocate buffer */
764 bf = TAILQ_FIRST(&sc->sc_rxbuf);
765 /* XXX shouldn't happen upon startup? */
767 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: nothing on rxbuf?!\n",
772 /* Remove it from the free list */
773 TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
775 /* Assign RX mbuf to it */
776 error = ath_edma_rxbuf_init(sc, bf);
778 device_printf(sc->sc_dev,
779 "%s: bf=%p, rxbuf alloc failed! error=%d\n",
783 TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
791 ath_edma_rxbuf_free(struct ath_softc *sc, struct ath_buf *bf)
794 ATH_RX_LOCK_ASSERT(sc);
797 * Only unload the frame if we haven't consumed
798 * the mbuf via ath_rx_pkt().
801 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
807 TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
811 * Allocate up to 'n' entries and push them onto the hardware FIFO.
813 * Return how many entries were successfully pushed onto the
817 ath_edma_rxfifo_alloc(struct ath_softc *sc, HAL_RX_QUEUE qtype, int nbufs)
819 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
823 ATH_RX_LOCK_ASSERT(sc);
826 * Allocate buffers until the FIFO is full or nbufs is reached.
828 for (i = 0; i < nbufs && re->m_fifo_depth < re->m_fifolen; i++) {
829 /* Ensure the FIFO is already blank, complain loudly! */
830 if (re->m_fifo[re->m_fifo_tail] != NULL) {
831 device_printf(sc->sc_dev,
832 "%s: Q%d: fifo[%d] != NULL (%p)\n",
836 re->m_fifo[re->m_fifo_tail]);
839 ath_edma_rxbuf_free(sc, re->m_fifo[re->m_fifo_tail]);
841 /* XXX check it's not < 0 */
842 re->m_fifo[re->m_fifo_tail] = NULL;
845 bf = ath_edma_rxbuf_alloc(sc);
846 /* XXX should ensure the FIFO is not NULL? */
848 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
849 "%s: Q%d: alloc failed: i=%d, nbufs=%d?\n",
857 re->m_fifo[re->m_fifo_tail] = bf;
859 /* Write to the RX FIFO */
860 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
861 "%s: Q%d: putrxbuf=%p (0x%jx)\n",
865 (uintmax_t) bf->bf_daddr);
866 ath_hal_putrxbuf(sc->sc_ah, bf->bf_daddr, qtype);
869 INCR(re->m_fifo_tail, re->m_fifolen);
873 * Return how many were allocated.
875 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: Q%d: nbufs=%d, nalloced=%d\n",
884 ath_edma_rxfifo_flush(struct ath_softc *sc, HAL_RX_QUEUE qtype)
886 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
889 ATH_RX_LOCK_ASSERT(sc);
891 for (i = 0; i < re->m_fifolen; i++) {
892 if (re->m_fifo[i] != NULL) {
894 struct ath_buf *bf = re->m_fifo[i];
896 if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
897 ath_printrxbuf(sc, bf, 0, HAL_OK);
899 ath_edma_rxbuf_free(sc, re->m_fifo[i]);
900 re->m_fifo[i] = NULL;
905 if (re->m_rxpending != NULL) {
906 m_freem(re->m_rxpending);
907 re->m_rxpending = NULL;
909 re->m_fifo_head = re->m_fifo_tail = re->m_fifo_depth = 0;
915 * Setup the initial RX FIFO structure.
918 ath_edma_setup_rxfifo(struct ath_softc *sc, HAL_RX_QUEUE qtype)
920 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
922 ATH_RX_LOCK_ASSERT(sc);
924 if (! ath_hal_getrxfifodepth(sc->sc_ah, qtype, &re->m_fifolen)) {
925 device_printf(sc->sc_dev, "%s: qtype=%d, failed\n",
932 device_printf(sc->sc_dev,
933 "%s: type=%d, FIFO depth = %d entries\n",
938 /* Allocate ath_buf FIFO array, pre-zero'ed */
939 re->m_fifo = malloc(sizeof(struct ath_buf *) * re->m_fifolen,
942 if (re->m_fifo == NULL) {
943 device_printf(sc->sc_dev, "%s: malloc failed\n",
949 * Set initial "empty" state.
951 re->m_rxpending = NULL;
952 re->m_fifo_head = re->m_fifo_tail = re->m_fifo_depth = 0;
958 ath_edma_rxfifo_free(struct ath_softc *sc, HAL_RX_QUEUE qtype)
960 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
962 device_printf(sc->sc_dev, "%s: called; qtype=%d\n",
966 free(re->m_fifo, M_ATHDEV);
972 ath_edma_dma_rxsetup(struct ath_softc *sc)
977 * Create RX DMA tag and buffers.
979 error = ath_descdma_setup_rx_edma(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
980 "rx", ath_rxbuf, sc->sc_rx_statuslen);
985 (void) ath_edma_setup_rxfifo(sc, HAL_RX_QUEUE_HP);
986 (void) ath_edma_setup_rxfifo(sc, HAL_RX_QUEUE_LP);
993 ath_edma_dma_rxteardown(struct ath_softc *sc)
997 ath_edma_flush_deferred_queue(sc);
998 ath_edma_rxfifo_flush(sc, HAL_RX_QUEUE_HP);
999 ath_edma_rxfifo_free(sc, HAL_RX_QUEUE_HP);
1001 ath_edma_rxfifo_flush(sc, HAL_RX_QUEUE_LP);
1002 ath_edma_rxfifo_free(sc, HAL_RX_QUEUE_LP);
1005 /* Free RX ath_buf */
1006 /* Free RX DMA tag */
1007 if (sc->sc_rxdma.dd_desc_len != 0)
1008 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
1014 ath_recv_setup_edma(struct ath_softc *sc)
1017 /* Set buffer size to 4k */
1018 sc->sc_edma_bufsize = 4096;
1020 /* Fetch EDMA field and buffer sizes */
1021 (void) ath_hal_getrxstatuslen(sc->sc_ah, &sc->sc_rx_statuslen);
1023 /* Configure the hardware with the RX buffer size */
1024 (void) ath_hal_setrxbufsize(sc->sc_ah, sc->sc_edma_bufsize -
1025 sc->sc_rx_statuslen);
1028 device_printf(sc->sc_dev, "RX status length: %d\n",
1029 sc->sc_rx_statuslen);
1030 device_printf(sc->sc_dev, "RX buffer size: %d\n",
1031 sc->sc_edma_bufsize);
1034 sc->sc_rx.recv_stop = ath_edma_stoprecv;
1035 sc->sc_rx.recv_start = ath_edma_startrecv;
1036 sc->sc_rx.recv_flush = ath_edma_recv_flush;
1037 sc->sc_rx.recv_tasklet = ath_edma_recv_tasklet;
1038 sc->sc_rx.recv_rxbuf_init = ath_edma_rxbuf_init;
1040 sc->sc_rx.recv_setup = ath_edma_dma_rxsetup;
1041 sc->sc_rx.recv_teardown = ath_edma_dma_rxteardown;
1043 sc->sc_rx.recv_sched = ath_edma_recv_sched;
1044 sc->sc_rx.recv_sched_queue = ath_edma_recv_sched_queue;