2 * Copyright (c) 2012 Adrian Chadd <adrian@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Driver for the Atheros Wireless LAN controller.
36 * This software is derived from work of Atsushi Onoe; his contribution
37 * is greatly appreciated.
43 * This is needed for register operations which are performed
44 * by the driver - eg, calls to ath_hal_gettsf32().
46 * It's also required for any AH_DEBUG checks in here, eg the
47 * module dependencies.
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/sysctl.h>
56 #include <sys/malloc.h>
58 #include <sys/mutex.h>
59 #include <sys/kernel.h>
60 #include <sys/socket.h>
61 #include <sys/sockio.h>
62 #include <sys/errno.h>
63 #include <sys/callout.h>
65 #include <sys/endian.h>
66 #include <sys/kthread.h>
67 #include <sys/taskqueue.h>
69 #include <sys/module.h>
71 #include <sys/smp.h> /* for mp_ncpus */
73 #include <machine/bus.h>
76 #include <net/if_dl.h>
77 #include <net/if_media.h>
78 #include <net/if_types.h>
79 #include <net/if_arp.h>
80 #include <net/ethernet.h>
81 #include <net/if_llc.h>
83 #include <net80211/ieee80211_var.h>
84 #include <net80211/ieee80211_regdomain.h>
85 #ifdef IEEE80211_SUPPORT_SUPERG
86 #include <net80211/ieee80211_superg.h>
88 #ifdef IEEE80211_SUPPORT_TDMA
89 #include <net80211/ieee80211_tdma.h>
95 #include <netinet/in.h>
96 #include <netinet/if_ether.h>
99 #include <dev/ath/if_athvar.h>
100 #include <dev/ath/ath_hal/ah_devid.h> /* XXX for softled */
101 #include <dev/ath/ath_hal/ah_diagcodes.h>
103 #include <dev/ath/if_ath_debug.h>
104 #include <dev/ath/if_ath_misc.h>
105 #include <dev/ath/if_ath_tsf.h>
106 #include <dev/ath/if_ath_tx.h>
107 #include <dev/ath/if_ath_sysctl.h>
108 #include <dev/ath/if_ath_led.h>
109 #include <dev/ath/if_ath_keycache.h>
110 #include <dev/ath/if_ath_rx.h>
111 #include <dev/ath/if_ath_beacon.h>
112 #include <dev/ath/if_athdfs.h>
115 #include <dev/ath/ath_tx99/ath_tx99.h>
118 #include <dev/ath/if_ath_rx_edma.h>
121 * some general macros
123 #define INCR(_l, _sz) (_l) ++; (_l) &= ((_sz) - 1)
124 #define DECR(_l, _sz) (_l) --; (_l) &= ((_sz) - 1)
126 MALLOC_DECLARE(M_ATHDEV);
131 * + Add an RX lock, just to ensure we don't have things clash;
132 * + Make sure the FIFO is correctly flushed and reinitialised
134 * + Handle the "kickpcu" state where the FIFO overflows.
135 * + Implement a "flush" routine, which doesn't push any
136 * new frames into the FIFO.
137 * + Verify multi-descriptor frames work!
138 * + There's a "memory use after free" which needs to be tracked down
139 * and fixed ASAP. I've seen this in the legacy path too, so it
140 * may be a generic RX path issue.
144 * XXX shuffle the function orders so these pre-declarations aren't
147 static int ath_edma_rxfifo_alloc(struct ath_softc *sc, HAL_RX_QUEUE qtype,
149 static int ath_edma_rxfifo_flush(struct ath_softc *sc, HAL_RX_QUEUE qtype);
150 static void ath_edma_rxbuf_free(struct ath_softc *sc, struct ath_buf *bf);
151 static int ath_edma_recv_proc_queue(struct ath_softc *sc,
152 HAL_RX_QUEUE qtype, int dosched);
155 ath_edma_stoprecv(struct ath_softc *sc, int dodelay)
157 struct ath_hal *ah = sc->sc_ah;
159 ath_hal_stoppcurecv(ah);
160 ath_hal_setrxfilter(ah, 0);
161 ath_hal_stopdmarecv(ah);
165 /* Flush RX pending for each queue */
166 /* XXX should generic-ify this */
167 if (sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending) {
168 m_freem(sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending);
169 sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending = NULL;
172 if (sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending) {
173 m_freem(sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending);
174 sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending = NULL;
179 * Re-initialise the FIFO given the current buffer contents.
180 * Specifically, walk from head -> tail, pushing the FIFO contents
181 * back into the FIFO.
184 ath_edma_reinit_fifo(struct ath_softc *sc, HAL_RX_QUEUE qtype)
186 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
191 for (j = 0; j < re->m_fifo_depth; j++) {
193 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
194 "%s: Q%d: pos=%i, addr=0x%jx\n",
198 (uintmax_t)bf->bf_daddr);
199 ath_hal_putrxbuf(sc->sc_ah, bf->bf_daddr, qtype);
200 INCR(i, re->m_fifolen);
203 /* Ensure this worked out right */
204 if (i != re->m_fifo_tail) {
205 device_printf(sc->sc_dev, "%s: i (%d) != tail! (%d)\n",
215 * XXX TODO: this needs to reallocate the FIFO entries when a reset
216 * occurs, in case the FIFO is filled up and no new descriptors get
217 * thrown into the FIFO.
220 ath_edma_startrecv(struct ath_softc *sc)
222 struct ath_hal *ah = sc->sc_ah;
228 * Entries should only be written out if the
231 * XXX This isn't correct. I should be looking
232 * at the value of AR_RXDP_SIZE (0x0070) to determine
233 * how many entries are in here.
235 * A warm reset will clear the registers but not the FIFO.
237 * And I believe this is actually the address of the last
238 * handled buffer rather than the current FIFO pointer.
239 * So if no frames have been (yet) seen, we'll reinit the
242 * I'll chase that up at some point.
244 if (ath_hal_getrxbuf(sc->sc_ah, HAL_RX_QUEUE_HP) == 0) {
245 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
246 "%s: Re-initing HP FIFO\n", __func__);
247 ath_edma_reinit_fifo(sc, HAL_RX_QUEUE_HP);
249 if (ath_hal_getrxbuf(sc->sc_ah, HAL_RX_QUEUE_LP) == 0) {
250 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
251 "%s: Re-initing LP FIFO\n", __func__);
252 ath_edma_reinit_fifo(sc, HAL_RX_QUEUE_LP);
255 /* Add up to m_fifolen entries in each queue */
257 * These must occur after the above write so the FIFO buffers
258 * are pushed/tracked in the same order as the hardware will
261 ath_edma_rxfifo_alloc(sc, HAL_RX_QUEUE_HP,
262 sc->sc_rxedma[HAL_RX_QUEUE_HP].m_fifolen);
264 ath_edma_rxfifo_alloc(sc, HAL_RX_QUEUE_LP,
265 sc->sc_rxedma[HAL_RX_QUEUE_LP].m_fifolen);
268 ath_hal_startpcurecv(ah);
273 ath_edma_recv_flush(struct ath_softc *sc)
276 device_printf(sc->sc_dev, "%s: called\n", __func__);
278 ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_HP, 0);
279 ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_LP, 0);
283 * Process frames from the current queue.
287 * + Add a "dosched" flag, so we don't reschedule any FIFO frames
288 * to the hardware or re-kick the PCU after 'kickpcu' is set.
290 * + Perhaps split "check FIFO contents" and "handle frames", so
291 * we can run the "check FIFO contents" in ath_intr(), but
292 * "handle frames" in the RX tasklet.
295 ath_edma_recv_proc_queue(struct ath_softc *sc, HAL_RX_QUEUE qtype,
298 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
299 struct ath_rx_status *rs;
304 struct ath_hal *ah = sc->sc_ah;
309 tsf = ath_hal_gettsf64(ah);
310 nf = ath_hal_getchannoise(ah, sc->sc_curchan);
311 sc->sc_stats.ast_rx_noise = nf;
314 bf = re->m_fifo[re->m_fifo_head];
315 /* This shouldn't occur! */
317 device_printf(sc->sc_dev, "%s: Q%d: NULL bf?\n",
326 * Sync descriptor memory - this also syncs the buffer for us.
328 * EDMA descriptors are in cached memory.
330 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
331 BUS_DMASYNC_POSTREAD);
332 rs = &bf->bf_status.ds_rxstat;
333 status = ath_hal_rxprocdesc(ah, ds, bf->bf_daddr, NULL, rs);
335 if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
336 ath_printrxbuf(sc, bf, 0, status == HAL_OK);
338 if (status == HAL_EINPROGRESS)
342 * Completed descriptor.
344 * In the future we'll call ath_rx_pkt(), but it first
345 * has to be taught about EDMA RX queues (so it can
346 * access sc_rxpending correctly.)
348 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
349 "%s: Q%d: completed!\n", __func__, qtype);
352 * Remove the FIFO entry!
354 re->m_fifo[re->m_fifo_head] = NULL;
357 * Skip the RX descriptor status - start at the data offset
359 m_adj(m, sc->sc_rx_statuslen);
361 /* Handle the frame */
362 if (ath_rx_pkt(sc, rs, status, tsf, nf, qtype, bf))
365 /* Free the buffer/mbuf */
366 ath_edma_rxbuf_free(sc, bf);
368 /* Bump the descriptor FIFO stats */
369 INCR(re->m_fifo_head, re->m_fifolen);
371 /* XXX check it doesn't fall below 0 */
372 } while (re->m_fifo_depth > 0);
374 /* Handle resched and kickpcu appropriately */
376 if (dosched && sc->sc_kickpcu) {
377 CTR0(ATH_KTR_ERR, "ath_edma_recv_proc_queue(): kickpcu");
378 device_printf(sc->sc_dev, "%s: handled %d descriptors\n",
382 * XXX TODO: what should occur here? Just re-poke and
383 * re-enable the RX FIFO?
389 /* Append some more fresh frames to the FIFO */
391 ath_edma_rxfifo_alloc(sc, qtype, re->m_fifolen);
397 ath_edma_recv_tasklet(void *arg, int npending)
399 struct ath_softc *sc = (struct ath_softc *) arg;
401 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called; npending=%d\n",
406 if (sc->sc_inreset_cnt > 0) {
407 device_printf(sc->sc_dev, "%s: sc_inreset_cnt > 0; skipping\n",
414 ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_HP, 1);
415 ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_LP, 1);
419 * Allocate an RX mbuf for the given ath_buf and initialise
422 * + Allocate a 4KB mbuf;
423 * + Setup the DMA map for the given buffer;
424 * + Keep a pointer to the start of the mbuf - that's where the
426 * + Take a pointer to the start of the RX buffer, set the
427 * mbuf "start" to be there;
431 ath_edma_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
438 // device_printf(sc->sc_dev, "%s: called; bf=%p\n", __func__, bf);
440 m = m_getm(NULL, sc->sc_edma_bufsize, M_DONTWAIT, MT_DATA);
442 return (ENOBUFS); /* XXX ?*/
444 /* XXX warn/enforce alignment */
446 len = m->m_ext.ext_size;
448 device_printf(sc->sc_dev, "%s: called: m=%p, size=%d, mtod=%p\n",
455 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
458 * Create DMA mapping.
460 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
461 bf->bf_dmamap, m, bf->bf_segs, &bf->bf_nseg, BUS_DMA_NOWAIT);
463 device_printf(sc->sc_dev, "%s: failed; error=%d\n",
471 * Populate ath_buf fields.
474 bf->bf_desc = mtod(m, struct ath_desc *);
475 bf->bf_daddr = bf->bf_segs[0].ds_addr;
476 bf->bf_lastds = bf->bf_desc; /* XXX only really for TX? */
479 /* Zero the descriptor */
480 memset(bf->bf_desc, '\0', sc->sc_rx_statuslen);
484 * Adjust mbuf header and length/size to compensate for the
487 m_adj(m, sc->sc_rx_statuslen);
495 static struct ath_buf *
496 ath_edma_rxbuf_alloc(struct ath_softc *sc)
501 /* Allocate buffer */
502 bf = TAILQ_FIRST(&sc->sc_rxbuf);
503 /* XXX shouldn't happen upon startup? */
507 /* Remove it from the free list */
508 TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
510 /* Assign RX mbuf to it */
511 error = ath_edma_rxbuf_init(sc, bf);
513 device_printf(sc->sc_dev,
514 "%s: bf=%p, rxbuf alloc failed! error=%d\n",
518 TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
526 ath_edma_rxbuf_free(struct ath_softc *sc, struct ath_buf *bf)
529 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
537 TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
541 * Allocate up to 'n' entries and push them onto the hardware FIFO.
543 * Return how many entries were successfully pushed onto the
547 ath_edma_rxfifo_alloc(struct ath_softc *sc, HAL_RX_QUEUE qtype, int nbufs)
549 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
554 * Allocate buffers until the FIFO is full or nbufs is reached.
556 for (i = 0; i < nbufs && re->m_fifo_depth < re->m_fifolen; i++) {
557 /* Ensure the FIFO is already blank, complain loudly! */
558 if (re->m_fifo[re->m_fifo_tail] != NULL) {
559 device_printf(sc->sc_dev,
560 "%s: Q%d: fifo[%d] != NULL (%p)\n",
564 re->m_fifo[re->m_fifo_tail]);
567 ath_edma_rxbuf_free(sc, re->m_fifo[re->m_fifo_tail]);
569 /* XXX check it's not < 0 */
570 re->m_fifo[re->m_fifo_tail] = NULL;
573 bf = ath_edma_rxbuf_alloc(sc);
574 /* XXX should ensure the FIFO is not NULL? */
576 device_printf(sc->sc_dev, "%s: Q%d: alloc failed?\n",
582 re->m_fifo[re->m_fifo_tail] = bf;
585 * Flush the descriptor contents before it's handed to the
588 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
589 BUS_DMASYNC_PREREAD);
591 /* Write to the RX FIFO */
592 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: Q%d: putrxbuf=%p\n",
596 ath_hal_putrxbuf(sc->sc_ah, bf->bf_daddr, qtype);
599 INCR(re->m_fifo_tail, re->m_fifolen);
603 * Return how many were allocated.
605 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: Q%d: nbufs=%d, nalloced=%d\n",
614 ath_edma_rxfifo_flush(struct ath_softc *sc, HAL_RX_QUEUE qtype)
616 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
619 for (i = 0; i < re->m_fifolen; i++) {
620 if (re->m_fifo[i] != NULL) {
622 struct ath_buf *bf = re->m_fifo[i];
624 if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
625 ath_printrxbuf(sc, bf, 0, HAL_OK);
627 ath_edma_rxbuf_free(sc, re->m_fifo[i]);
628 re->m_fifo[i] = NULL;
633 if (re->m_rxpending != NULL) {
634 m_freem(re->m_rxpending);
635 re->m_rxpending = NULL;
637 re->m_fifo_head = re->m_fifo_tail = re->m_fifo_depth = 0;
643 * Setup the initial RX FIFO structure.
646 ath_edma_setup_rxfifo(struct ath_softc *sc, HAL_RX_QUEUE qtype)
648 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
650 if (! ath_hal_getrxfifodepth(sc->sc_ah, qtype, &re->m_fifolen)) {
651 device_printf(sc->sc_dev, "%s: qtype=%d, failed\n",
656 device_printf(sc->sc_dev, "%s: type=%d, FIFO depth = %d entries\n",
661 /* Allocate ath_buf FIFO array, pre-zero'ed */
662 re->m_fifo = malloc(sizeof(struct ath_buf *) * re->m_fifolen,
665 if (re->m_fifo == NULL) {
666 device_printf(sc->sc_dev, "%s: malloc failed\n",
672 * Set initial "empty" state.
674 re->m_rxpending = NULL;
675 re->m_fifo_head = re->m_fifo_tail = re->m_fifo_depth = 0;
681 ath_edma_rxfifo_free(struct ath_softc *sc, HAL_RX_QUEUE qtype)
683 struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
685 device_printf(sc->sc_dev, "%s: called; qtype=%d\n",
689 free(re->m_fifo, M_ATHDEV);
695 ath_edma_dma_rxsetup(struct ath_softc *sc)
699 /* Create RX DMA tag */
700 /* Create RX ath_buf array */
702 error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
707 (void) ath_edma_setup_rxfifo(sc, HAL_RX_QUEUE_HP);
708 (void) ath_edma_setup_rxfifo(sc, HAL_RX_QUEUE_LP);
714 ath_edma_dma_rxteardown(struct ath_softc *sc)
717 device_printf(sc->sc_dev, "%s: called\n", __func__);
719 ath_edma_rxfifo_flush(sc, HAL_RX_QUEUE_HP);
720 ath_edma_rxfifo_free(sc, HAL_RX_QUEUE_HP);
722 ath_edma_rxfifo_flush(sc, HAL_RX_QUEUE_LP);
723 ath_edma_rxfifo_free(sc, HAL_RX_QUEUE_LP);
725 /* Free RX ath_buf */
726 /* Free RX DMA tag */
727 if (sc->sc_rxdma.dd_desc_len != 0)
728 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
734 ath_recv_setup_edma(struct ath_softc *sc)
737 device_printf(sc->sc_dev, "DMA setup: EDMA\n");
739 /* Set buffer size to 4k */
740 sc->sc_edma_bufsize = 4096;
742 /* Configure the hardware with this */
743 (void) ath_hal_setrxbufsize(sc->sc_ah, sc->sc_edma_bufsize);
745 /* Fetch EDMA field and buffer sizes */
746 (void) ath_hal_getrxstatuslen(sc->sc_ah, &sc->sc_rx_statuslen);
747 (void) ath_hal_gettxdesclen(sc->sc_ah, &sc->sc_tx_desclen);
748 (void) ath_hal_gettxstatuslen(sc->sc_ah, &sc->sc_tx_statuslen);
749 (void) ath_hal_getntxmaps(sc->sc_ah, &sc->sc_tx_nmaps);
751 device_printf(sc->sc_dev, "RX status length: %d\n",
752 sc->sc_rx_statuslen);
753 device_printf(sc->sc_dev, "TX descriptor length: %d\n",
755 device_printf(sc->sc_dev, "TX status length: %d\n",
756 sc->sc_tx_statuslen);
757 device_printf(sc->sc_dev, "TX/RX buffer size: %d\n",
758 sc->sc_edma_bufsize);
759 device_printf(sc->sc_dev, "TX buffers per descriptor: %d\n",
762 sc->sc_rx.recv_stop = ath_edma_stoprecv;
763 sc->sc_rx.recv_start = ath_edma_startrecv;
764 sc->sc_rx.recv_flush = ath_edma_recv_flush;
765 sc->sc_rx.recv_tasklet = ath_edma_recv_tasklet;
766 sc->sc_rx.recv_rxbuf_init = ath_edma_rxbuf_init;
768 sc->sc_rx.recv_setup = ath_edma_dma_rxsetup;
769 sc->sc_rx.recv_teardown = ath_edma_dma_rxteardown;