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;
560 struct epoch_tracker et;
564 nf = ath_hal_getchannoise(sc->sc_ah, sc->sc_curchan);
566 * XXX TODO: the NF/TSF should be stamped on the bufs themselves,
567 * otherwise we may end up adding in the wrong values if this
568 * is delayed too far..
570 tsf = ath_hal_gettsf64(sc->sc_ah);
572 /* Copy the list over */
574 TAILQ_CONCAT(&rxlist, &sc->sc_rx_rxlist[qtype], bf_list);
579 /* Handle the completed descriptors */
581 * XXX is this SAFE call needed? The ath_buf entries
582 * aren't modified by ath_rx_pkt, right?
584 TAILQ_FOREACH_SAFE(bf, &rxlist, bf_list, next) {
586 * Skip the RX descriptor status - start at the data offset
588 m_adj(bf->bf_m, sc->sc_rx_statuslen);
590 /* Handle the frame */
592 rs = &bf->bf_status.ds_rxstat;
595 if (ath_rx_pkt(sc, rs, bf->bf_rxstatus, tsf, nf, qtype, bf, m))
604 ATH_KTR(sc, ATH_KTR_INTERRUPTS, 1,
605 "ath edma rx deferred proc: ngood=%d\n",
608 /* Free in one set, inside the lock */
610 while (! TAILQ_EMPTY(&rxlist)) {
611 bf = TAILQ_FIRST(&rxlist);
612 TAILQ_REMOVE(&rxlist, bf, bf_list);
613 /* Free the buffer/mbuf */
614 ath_edma_rxbuf_free(sc, bf);
622 ath_edma_recv_tasklet(void *arg, int npending)
624 struct ath_softc *sc = (struct ath_softc *) arg;
625 #ifdef IEEE80211_SUPPORT_SUPERG
626 struct ieee80211com *ic = &sc->sc_ic;
629 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called; npending=%d\n",
634 if (sc->sc_inreset_cnt > 0) {
635 device_printf(sc->sc_dev, "%s: sc_inreset_cnt > 0; skipping\n",
644 ath_power_set_power_state(sc, HAL_PM_AWAKE);
647 ath_edma_recv_proc_deferred_queue(sc, HAL_RX_QUEUE_HP, 1);
648 ath_edma_recv_proc_deferred_queue(sc, HAL_RX_QUEUE_LP, 1);
651 * XXX: If we read the tsf/channoise here and then pass it in,
652 * we could restore the power state before processing
653 * the deferred queue.
656 ath_power_restore_power_state(sc);
659 #ifdef IEEE80211_SUPPORT_SUPERG
660 ieee80211_ff_age_all(ic, 100);
662 if (ath_dfs_tasklet_needed(sc, sc->sc_curchan))
663 taskqueue_enqueue(sc->sc_tq, &sc->sc_dfstask);
669 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called; done!\n", __func__);
673 * Allocate an RX mbuf for the given ath_buf and initialise
676 * + Allocate a 4KB mbuf;
677 * + Setup the DMA map for the given buffer;
681 ath_edma_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
688 ATH_RX_LOCK_ASSERT(sc);
690 m = m_getm(NULL, sc->sc_edma_bufsize, M_NOWAIT, MT_DATA);
692 return (ENOBUFS); /* XXX ?*/
694 /* XXX warn/enforce alignment */
696 len = m->m_ext.ext_size;
698 device_printf(sc->sc_dev, "%s: called: m=%p, size=%d, mtod=%p\n",
705 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
708 * Populate ath_buf fields.
710 bf->bf_desc = mtod(m, struct ath_desc *);
711 bf->bf_lastds = bf->bf_desc; /* XXX only really for TX? */
715 * Zero the descriptor and ensure it makes it out to the
716 * bounce buffer if one is required.
718 * XXX PREWRITE will copy the whole buffer; we only needed it
719 * to sync the first 32 DWORDS. Oh well.
721 memset(bf->bf_desc, '\0', sc->sc_rx_statuslen);
724 * Create DMA mapping.
726 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
727 bf->bf_dmamap, m, bf->bf_segs, &bf->bf_nseg, BUS_DMA_NOWAIT);
730 device_printf(sc->sc_dev, "%s: failed; error=%d\n",
738 * Set daddr to the physical mapping page.
740 bf->bf_daddr = bf->bf_segs[0].ds_addr;
743 * Prepare for the upcoming read.
745 * We need to both sync some data into the buffer (the zero'ed
746 * descriptor payload) and also prepare for the read that's going
749 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
750 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
757 * Allocate a RX buffer.
759 static struct ath_buf *
760 ath_edma_rxbuf_alloc(struct ath_softc *sc)
765 ATH_RX_LOCK_ASSERT(sc);
767 /* Allocate buffer */
768 bf = TAILQ_FIRST(&sc->sc_rxbuf);
769 /* XXX shouldn't happen upon startup? */
771 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: nothing on rxbuf?!\n",
776 /* Remove it from the free list */
777 TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
779 /* Assign RX mbuf to it */
780 error = ath_edma_rxbuf_init(sc, bf);
782 device_printf(sc->sc_dev,
783 "%s: bf=%p, rxbuf alloc failed! error=%d\n",
787 TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
795 ath_edma_rxbuf_free(struct ath_softc *sc, struct ath_buf *bf)
798 ATH_RX_LOCK_ASSERT(sc);
801 * Only unload the frame if we haven't consumed
802 * the mbuf via ath_rx_pkt().
805 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
811 TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
815 * Allocate up to 'n' entries and push them onto the hardware FIFO.
817 * Return how many entries were successfully pushed onto the
821 ath_edma_rxfifo_alloc(struct ath_softc *sc, HAL_RX_QUEUE qtype, int nbufs)
823 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
827 ATH_RX_LOCK_ASSERT(sc);
830 * Allocate buffers until the FIFO is full or nbufs is reached.
832 for (i = 0; i < nbufs && re->m_fifo_depth < re->m_fifolen; i++) {
833 /* Ensure the FIFO is already blank, complain loudly! */
834 if (re->m_fifo[re->m_fifo_tail] != NULL) {
835 device_printf(sc->sc_dev,
836 "%s: Q%d: fifo[%d] != NULL (%p)\n",
840 re->m_fifo[re->m_fifo_tail]);
843 ath_edma_rxbuf_free(sc, re->m_fifo[re->m_fifo_tail]);
845 /* XXX check it's not < 0 */
846 re->m_fifo[re->m_fifo_tail] = NULL;
849 bf = ath_edma_rxbuf_alloc(sc);
850 /* XXX should ensure the FIFO is not NULL? */
852 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
853 "%s: Q%d: alloc failed: i=%d, nbufs=%d?\n",
861 re->m_fifo[re->m_fifo_tail] = bf;
863 /* Write to the RX FIFO */
864 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
865 "%s: Q%d: putrxbuf=%p (0x%jx)\n",
869 (uintmax_t) bf->bf_daddr);
870 ath_hal_putrxbuf(sc->sc_ah, bf->bf_daddr, qtype);
873 INCR(re->m_fifo_tail, re->m_fifolen);
877 * Return how many were allocated.
879 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: Q%d: nbufs=%d, nalloced=%d\n",
888 ath_edma_rxfifo_flush(struct ath_softc *sc, HAL_RX_QUEUE qtype)
890 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
893 ATH_RX_LOCK_ASSERT(sc);
895 for (i = 0; i < re->m_fifolen; i++) {
896 if (re->m_fifo[i] != NULL) {
898 struct ath_buf *bf = re->m_fifo[i];
900 if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
901 ath_printrxbuf(sc, bf, 0, HAL_OK);
903 ath_edma_rxbuf_free(sc, re->m_fifo[i]);
904 re->m_fifo[i] = NULL;
909 if (re->m_rxpending != NULL) {
910 m_freem(re->m_rxpending);
911 re->m_rxpending = NULL;
913 re->m_fifo_head = re->m_fifo_tail = re->m_fifo_depth = 0;
919 * Setup the initial RX FIFO structure.
922 ath_edma_setup_rxfifo(struct ath_softc *sc, HAL_RX_QUEUE qtype)
924 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
926 ATH_RX_LOCK_ASSERT(sc);
928 if (! ath_hal_getrxfifodepth(sc->sc_ah, qtype, &re->m_fifolen)) {
929 device_printf(sc->sc_dev, "%s: qtype=%d, failed\n",
936 device_printf(sc->sc_dev,
937 "%s: type=%d, FIFO depth = %d entries\n",
942 /* Allocate ath_buf FIFO array, pre-zero'ed */
943 re->m_fifo = malloc(sizeof(struct ath_buf *) * re->m_fifolen,
946 if (re->m_fifo == NULL) {
947 device_printf(sc->sc_dev, "%s: malloc failed\n",
953 * Set initial "empty" state.
955 re->m_rxpending = NULL;
956 re->m_fifo_head = re->m_fifo_tail = re->m_fifo_depth = 0;
962 ath_edma_rxfifo_free(struct ath_softc *sc, HAL_RX_QUEUE qtype)
964 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
966 device_printf(sc->sc_dev, "%s: called; qtype=%d\n",
970 free(re->m_fifo, M_ATHDEV);
976 ath_edma_dma_rxsetup(struct ath_softc *sc)
981 * Create RX DMA tag and buffers.
983 error = ath_descdma_setup_rx_edma(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
984 "rx", ath_rxbuf, sc->sc_rx_statuslen);
989 (void) ath_edma_setup_rxfifo(sc, HAL_RX_QUEUE_HP);
990 (void) ath_edma_setup_rxfifo(sc, HAL_RX_QUEUE_LP);
997 ath_edma_dma_rxteardown(struct ath_softc *sc)
1001 ath_edma_flush_deferred_queue(sc);
1002 ath_edma_rxfifo_flush(sc, HAL_RX_QUEUE_HP);
1003 ath_edma_rxfifo_free(sc, HAL_RX_QUEUE_HP);
1005 ath_edma_rxfifo_flush(sc, HAL_RX_QUEUE_LP);
1006 ath_edma_rxfifo_free(sc, HAL_RX_QUEUE_LP);
1009 /* Free RX ath_buf */
1010 /* Free RX DMA tag */
1011 if (sc->sc_rxdma.dd_desc_len != 0)
1012 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
1018 ath_recv_setup_edma(struct ath_softc *sc)
1021 /* Set buffer size to 4k */
1022 sc->sc_edma_bufsize = 4096;
1024 /* Fetch EDMA field and buffer sizes */
1025 (void) ath_hal_getrxstatuslen(sc->sc_ah, &sc->sc_rx_statuslen);
1027 /* Configure the hardware with the RX buffer size */
1028 (void) ath_hal_setrxbufsize(sc->sc_ah, sc->sc_edma_bufsize -
1029 sc->sc_rx_statuslen);
1032 device_printf(sc->sc_dev, "RX status length: %d\n",
1033 sc->sc_rx_statuslen);
1034 device_printf(sc->sc_dev, "RX buffer size: %d\n",
1035 sc->sc_edma_bufsize);
1038 sc->sc_rx.recv_stop = ath_edma_stoprecv;
1039 sc->sc_rx.recv_start = ath_edma_startrecv;
1040 sc->sc_rx.recv_flush = ath_edma_recv_flush;
1041 sc->sc_rx.recv_tasklet = ath_edma_recv_tasklet;
1042 sc->sc_rx.recv_rxbuf_init = ath_edma_rxbuf_init;
1044 sc->sc_rx.recv_setup = ath_edma_dma_rxsetup;
1045 sc->sc_rx.recv_teardown = ath_edma_dma_rxteardown;
1047 sc->sc_rx.recv_sched = ath_edma_recv_sched;
1048 sc->sc_rx.recv_sched_queue = ath_edma_recv_sched_queue;