]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ath/if_ath_rx_edma.c
Merge OpenSSL 1.0.1c.
[FreeBSD/FreeBSD.git] / sys / dev / ath / if_ath_rx_edma.c
1 /*-
2  * Copyright (c) 2012 Adrian Chadd <adrian@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
15  *
16  * NO WARRANTY
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.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 /*
34  * Driver for the Atheros Wireless LAN controller.
35  *
36  * This software is derived from work of Atsushi Onoe; his contribution
37  * is greatly appreciated.
38  */
39
40 #include "opt_inet.h"
41 #include "opt_ath.h"
42 /*
43  * This is needed for register operations which are performed
44  * by the driver - eg, calls to ath_hal_gettsf32().
45  *
46  * It's also required for any AH_DEBUG checks in here, eg the
47  * module dependencies.
48  */
49 #include "opt_ah.h"
50 #include "opt_wlan.h"
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/sysctl.h>
55 #include <sys/mbuf.h>
56 #include <sys/malloc.h>
57 #include <sys/lock.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>
64 #include <sys/bus.h>
65 #include <sys/endian.h>
66 #include <sys/kthread.h>
67 #include <sys/taskqueue.h>
68 #include <sys/priv.h>
69 #include <sys/module.h>
70 #include <sys/ktr.h>
71 #include <sys/smp.h>    /* for mp_ncpus */
72
73 #include <machine/bus.h>
74
75 #include <net/if.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>
82
83 #include <net80211/ieee80211_var.h>
84 #include <net80211/ieee80211_regdomain.h>
85 #ifdef IEEE80211_SUPPORT_SUPERG
86 #include <net80211/ieee80211_superg.h>
87 #endif
88 #ifdef IEEE80211_SUPPORT_TDMA
89 #include <net80211/ieee80211_tdma.h>
90 #endif
91
92 #include <net/bpf.h>
93
94 #ifdef INET
95 #include <netinet/in.h>
96 #include <netinet/if_ether.h>
97 #endif
98
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>
102
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>
113
114 #ifdef ATH_TX99_DIAG
115 #include <dev/ath/ath_tx99/ath_tx99.h>
116 #endif
117
118 #include <dev/ath/if_ath_rx_edma.h>
119
120 /*
121  * some general macros
122   */
123 #define INCR(_l, _sz)           (_l) ++; (_l) &= ((_sz) - 1)
124 #define DECR(_l, _sz)           (_l) --; (_l) &= ((_sz) - 1)
125
126 MALLOC_DECLARE(M_ATHDEV);
127
128 /*
129  * XXX TODO:
130  *
131  * + Add an RX lock, just to ensure we don't have things clash;
132  * + Make sure the FIFO is correctly flushed and reinitialised
133  *   through a reset;
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.
141  */
142
143 /*
144  * XXX shuffle the function orders so these pre-declarations aren't
145  * required!
146  */
147 static  int ath_edma_rxfifo_alloc(struct ath_softc *sc, HAL_RX_QUEUE qtype,
148             int nbufs);
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);
153
154 static void
155 ath_edma_stoprecv(struct ath_softc *sc, int dodelay)
156 {
157         struct ath_hal *ah = sc->sc_ah;
158
159         ath_hal_stoppcurecv(ah);
160         ath_hal_setrxfilter(ah, 0);
161         ath_hal_stopdmarecv(ah);
162
163         DELAY(3000);
164
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;
170         }
171
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;
175         }
176 }
177
178 /*
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.
182  */
183 static void
184 ath_edma_reinit_fifo(struct ath_softc *sc, HAL_RX_QUEUE qtype)
185 {
186         struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
187         struct ath_buf *bf;
188         int i, j;
189
190         i = re->m_fifo_head;
191         for (j = 0; j < re->m_fifo_depth; j++) {
192                 bf = re->m_fifo[i];
193                 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
194                     "%s: Q%d: pos=%i, addr=0x%jx\n",
195                     __func__,
196                     qtype,
197                     i,
198                     (uintmax_t)bf->bf_daddr);
199                 ath_hal_putrxbuf(sc->sc_ah, bf->bf_daddr, qtype);
200                 INCR(i, re->m_fifolen);
201         }
202
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",
206                     __func__,
207                     i,
208                     re->m_fifo_tail);
209         }
210 }
211
212 /*
213  * Start receive.
214  *
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.
218  */
219 static int
220 ath_edma_startrecv(struct ath_softc *sc)
221 {
222         struct ath_hal *ah = sc->sc_ah;
223
224         /* Enable RX FIFO */
225         ath_hal_rxena(ah);
226
227         /*
228          * Entries should only be written out if the
229          * FIFO is empty.
230          *
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.
234          *
235          * A warm reset will clear the registers but not the FIFO.
236          *
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
240          * FIFO.
241          *
242          * I'll chase that up at some point.
243          */
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);
248         }
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);
253         }
254
255         /* Add up to m_fifolen entries in each queue */
256         /*
257          * These must occur after the above write so the FIFO buffers
258          * are pushed/tracked in the same order as the hardware will
259          * process them.
260          */
261         ath_edma_rxfifo_alloc(sc, HAL_RX_QUEUE_HP,
262             sc->sc_rxedma[HAL_RX_QUEUE_HP].m_fifolen);
263
264         ath_edma_rxfifo_alloc(sc, HAL_RX_QUEUE_LP,
265             sc->sc_rxedma[HAL_RX_QUEUE_LP].m_fifolen);
266
267         ath_mode_init(sc);
268         ath_hal_startpcurecv(ah);
269         return (0);
270 }
271
272 static void
273 ath_edma_recv_flush(struct ath_softc *sc)
274 {
275
276         device_printf(sc->sc_dev, "%s: called\n", __func__);
277
278         ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_HP, 0);
279         ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_LP, 0);
280 }
281
282 /*
283  * Process frames from the current queue.
284  *
285  * TODO:
286  *
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.
289  *
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.
293  */
294 static int
295 ath_edma_recv_proc_queue(struct ath_softc *sc, HAL_RX_QUEUE qtype,
296     int dosched)
297 {
298         struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
299         struct ath_rx_status *rs;
300         struct ath_desc *ds;
301         struct ath_buf *bf;
302         struct mbuf *m;
303         HAL_STATUS status;
304         struct ath_hal *ah = sc->sc_ah;
305         uint64_t tsf;
306         int16_t nf;
307         int ngood = 0;
308
309         tsf = ath_hal_gettsf64(ah);
310         nf = ath_hal_getchannoise(ah, sc->sc_curchan);
311         sc->sc_stats.ast_rx_noise = nf;
312
313         do {
314                 bf = re->m_fifo[re->m_fifo_head];
315                 /* This shouldn't occur! */
316                 if (bf == NULL) {
317                         device_printf(sc->sc_dev, "%s: Q%d: NULL bf?\n",
318                             __func__,
319                             qtype);
320                         break;
321                 }
322                 m = bf->bf_m;
323                 ds = bf->bf_desc;
324
325                 /*
326                  * Sync descriptor memory - this also syncs the buffer for us.
327                  *
328                  * EDMA descriptors are in cached memory.
329                  */
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);
334 #ifdef  ATH_DEBUG
335                 if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
336                         ath_printrxbuf(sc, bf, 0, status == HAL_OK);
337 #endif
338                 if (status == HAL_EINPROGRESS)
339                         break;
340
341                 /*
342                  * Completed descriptor.
343                  *
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.)
347                  */
348                 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
349                     "%s: Q%d: completed!\n", __func__, qtype);
350
351                 /*
352                  * Remove the FIFO entry!
353                  */
354                 re->m_fifo[re->m_fifo_head] = NULL;
355
356                 /*
357                  * Skip the RX descriptor status - start at the data offset
358                  */
359                 m_adj(m, sc->sc_rx_statuslen);
360
361                 /* Handle the frame */
362                 if (ath_rx_pkt(sc, rs, status, tsf, nf, qtype, bf))
363                         ngood++;
364
365                 /* Free the buffer/mbuf */
366                 ath_edma_rxbuf_free(sc, bf);
367
368                 /* Bump the descriptor FIFO stats */
369                 INCR(re->m_fifo_head, re->m_fifolen);
370                 re->m_fifo_depth--;
371                 /* XXX check it doesn't fall below 0 */
372         } while (re->m_fifo_depth > 0);
373
374         /* Handle resched and kickpcu appropriately */
375         ATH_PCU_LOCK(sc);
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",
379                     __func__, ngood);
380
381                 /*
382                  * XXX TODO: what should occur here? Just re-poke and
383                  * re-enable the RX FIFO?
384                  */
385                 sc->sc_kickpcu = 0;
386         }
387         ATH_PCU_UNLOCK(sc);
388
389         /* Append some more fresh frames to the FIFO */
390         if (dosched)
391                 ath_edma_rxfifo_alloc(sc, qtype, re->m_fifolen);
392
393         return (ngood);
394 }
395
396 static void
397 ath_edma_recv_tasklet(void *arg, int npending)
398 {
399         struct ath_softc *sc = (struct ath_softc *) arg;
400
401         DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called; npending=%d\n",
402             __func__,
403             npending);
404
405         ATH_PCU_LOCK(sc);
406         if (sc->sc_inreset_cnt > 0) {
407                 device_printf(sc->sc_dev, "%s: sc_inreset_cnt > 0; skipping\n",
408                     __func__);
409                 ATH_PCU_UNLOCK(sc);
410                 return;
411         }
412         ATH_PCU_UNLOCK(sc);
413
414         ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_HP, 1);
415         ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_LP, 1);
416 }
417
418 /*
419  * Allocate an RX mbuf for the given ath_buf and initialise
420  * it for EDMA.
421  *
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
425  *   descriptor lies;
426  * + Take a pointer to the start of the RX buffer, set the
427  *   mbuf "start" to be there;
428  * + Return that.
429  */
430 static int
431 ath_edma_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
432 {
433
434         struct mbuf *m;
435         int error;
436         int len;
437
438 //      device_printf(sc->sc_dev, "%s: called; bf=%p\n", __func__, bf);
439
440         m = m_getm(NULL, sc->sc_edma_bufsize, M_DONTWAIT, MT_DATA);
441         if (! m)
442                 return (ENOBUFS);               /* XXX ?*/
443
444         /* XXX warn/enforce alignment */
445
446         len = m->m_ext.ext_size;
447 #if 0
448         device_printf(sc->sc_dev, "%s: called: m=%p, size=%d, mtod=%p\n",
449             __func__,
450             m,
451             len,
452             mtod(m, char *));
453 #endif
454
455         m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
456
457         /*
458          * Create DMA mapping.
459          */
460         error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
461             bf->bf_dmamap, m, bf->bf_segs, &bf->bf_nseg, BUS_DMA_NOWAIT);
462         if (error != 0) {
463                 device_printf(sc->sc_dev, "%s: failed; error=%d\n",
464                     __func__,
465                     error);
466                 m_freem(m);
467                 return (error);
468         }
469
470         /*
471          * Populate ath_buf fields.
472          */
473
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? */
477         bf->bf_m = m;
478
479         /* Zero the descriptor */
480         memset(bf->bf_desc, '\0', sc->sc_rx_statuslen);
481
482 #if 0
483         /*
484          * Adjust mbuf header and length/size to compensate for the
485          * descriptor size.
486          */
487         m_adj(m, sc->sc_rx_statuslen);
488 #endif
489
490         /* Finish! */
491
492         return (0);
493 }
494
495 static struct ath_buf *
496 ath_edma_rxbuf_alloc(struct ath_softc *sc)
497 {
498         struct ath_buf *bf;
499         int error;
500
501         /* Allocate buffer */
502         bf = TAILQ_FIRST(&sc->sc_rxbuf);
503         /* XXX shouldn't happen upon startup? */
504         if (bf == NULL)
505                 return (NULL);
506
507         /* Remove it from the free list */
508         TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
509
510         /* Assign RX mbuf to it */
511         error = ath_edma_rxbuf_init(sc, bf);
512         if (error != 0) {
513                 device_printf(sc->sc_dev,
514                     "%s: bf=%p, rxbuf alloc failed! error=%d\n",
515                     __func__,
516                     bf,
517                     error);
518                 TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
519                 return (NULL);
520         }
521
522         return (bf);
523 }
524
525 static void
526 ath_edma_rxbuf_free(struct ath_softc *sc, struct ath_buf *bf)
527 {
528
529         bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
530
531         if (bf->bf_m) {
532                 m_freem(bf->bf_m);
533                 bf->bf_m = NULL;
534         }
535
536         /* XXX lock? */
537         TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
538 }
539
540 /*
541  * Allocate up to 'n' entries and push them onto the hardware FIFO.
542  *
543  * Return how many entries were successfully pushed onto the
544  * FIFO.
545  */
546 static int
547 ath_edma_rxfifo_alloc(struct ath_softc *sc, HAL_RX_QUEUE qtype, int nbufs)
548 {
549         struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
550         struct ath_buf *bf;
551         int i;
552
553         /*
554          * Allocate buffers until the FIFO is full or nbufs is reached.
555          */
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",
561                             __func__,
562                             qtype,
563                             re->m_fifo_tail,
564                             re->m_fifo[re->m_fifo_tail]);
565
566                         /* Free the slot */
567                         ath_edma_rxbuf_free(sc, re->m_fifo[re->m_fifo_tail]);
568                         re->m_fifo_depth--;
569                         /* XXX check it's not < 0 */
570                         re->m_fifo[re->m_fifo_tail] = NULL;
571                 }
572
573                 bf = ath_edma_rxbuf_alloc(sc);
574                 /* XXX should ensure the FIFO is not NULL? */
575                 if (bf == NULL) {
576                         device_printf(sc->sc_dev, "%s: Q%d: alloc failed?\n",
577                             __func__,
578                             qtype);
579                         break;
580                 }
581
582                 re->m_fifo[re->m_fifo_tail] = bf;
583
584                 /*
585                  * Flush the descriptor contents before it's handed to the
586                  * hardware.
587                  */
588                 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
589                     BUS_DMASYNC_PREREAD);
590
591                 /* Write to the RX FIFO */
592                 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: Q%d: putrxbuf=%p\n",
593                     __func__,
594                     qtype,
595                     bf->bf_desc);
596                 ath_hal_putrxbuf(sc->sc_ah, bf->bf_daddr, qtype);
597
598                 re->m_fifo_depth++;
599                 INCR(re->m_fifo_tail, re->m_fifolen);
600         }
601
602         /*
603          * Return how many were allocated.
604          */
605         DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: Q%d: nbufs=%d, nalloced=%d\n",
606             __func__,
607             qtype,
608             nbufs,
609             i);
610         return (i);
611 }
612
613 static int
614 ath_edma_rxfifo_flush(struct ath_softc *sc, HAL_RX_QUEUE qtype)
615 {
616         struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
617         int i;
618
619         for (i = 0; i < re->m_fifolen; i++) {
620                 if (re->m_fifo[i] != NULL) {
621 #ifdef  ATH_DEBUG
622                         struct ath_buf *bf = re->m_fifo[i];
623
624                         if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
625                                 ath_printrxbuf(sc, bf, 0, HAL_OK);
626 #endif
627                         ath_edma_rxbuf_free(sc, re->m_fifo[i]);
628                         re->m_fifo[i] = NULL;
629                         re->m_fifo_depth--;
630                 }
631         }
632
633         if (re->m_rxpending != NULL) {
634                 m_freem(re->m_rxpending);
635                 re->m_rxpending = NULL;
636         }
637         re->m_fifo_head = re->m_fifo_tail = re->m_fifo_depth = 0;
638
639         return (0);
640 }
641
642 /*
643  * Setup the initial RX FIFO structure.
644  */
645 static int
646 ath_edma_setup_rxfifo(struct ath_softc *sc, HAL_RX_QUEUE qtype)
647 {
648         struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
649
650         if (! ath_hal_getrxfifodepth(sc->sc_ah, qtype, &re->m_fifolen)) {
651                 device_printf(sc->sc_dev, "%s: qtype=%d, failed\n",
652                     __func__,
653                     qtype);
654                 return (-EINVAL);
655         }
656         device_printf(sc->sc_dev, "%s: type=%d, FIFO depth = %d entries\n",
657             __func__,
658             qtype,
659             re->m_fifolen);
660
661         /* Allocate ath_buf FIFO array, pre-zero'ed */
662         re->m_fifo = malloc(sizeof(struct ath_buf *) * re->m_fifolen,
663             M_ATHDEV,
664             M_NOWAIT | M_ZERO);
665         if (re->m_fifo == NULL) {
666                 device_printf(sc->sc_dev, "%s: malloc failed\n",
667                     __func__);
668                 return (-ENOMEM);
669         }
670
671         /*
672          * Set initial "empty" state.
673          */
674         re->m_rxpending = NULL;
675         re->m_fifo_head = re->m_fifo_tail = re->m_fifo_depth = 0;
676
677         return (0);
678 }
679
680 static int
681 ath_edma_rxfifo_free(struct ath_softc *sc, HAL_RX_QUEUE qtype)
682 {
683         struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
684
685         device_printf(sc->sc_dev, "%s: called; qtype=%d\n",
686             __func__,
687             qtype);
688         
689         free(re->m_fifo, M_ATHDEV);
690
691         return (0);
692 }
693
694 static int
695 ath_edma_dma_rxsetup(struct ath_softc *sc)
696 {
697         int error;
698
699         /* Create RX DMA tag */
700         /* Create RX ath_buf array */
701
702         error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
703             "rx", ath_rxbuf, 1);
704         if (error != 0)
705                 return error;
706
707         (void) ath_edma_setup_rxfifo(sc, HAL_RX_QUEUE_HP);
708         (void) ath_edma_setup_rxfifo(sc, HAL_RX_QUEUE_LP);
709
710         return (0);
711 }
712
713 static int
714 ath_edma_dma_rxteardown(struct ath_softc *sc)
715 {
716
717         device_printf(sc->sc_dev, "%s: called\n", __func__);
718
719         ath_edma_rxfifo_flush(sc, HAL_RX_QUEUE_HP);
720         ath_edma_rxfifo_free(sc, HAL_RX_QUEUE_HP);
721
722         ath_edma_rxfifo_flush(sc, HAL_RX_QUEUE_LP);
723         ath_edma_rxfifo_free(sc, HAL_RX_QUEUE_LP);
724
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);
729
730         return (0);
731 }
732
733 void
734 ath_recv_setup_edma(struct ath_softc *sc)
735 {
736
737         device_printf(sc->sc_dev, "DMA setup: EDMA\n");
738
739         /* Set buffer size to 4k */
740         sc->sc_edma_bufsize = 4096;
741
742         /* Configure the hardware with this */
743         (void) ath_hal_setrxbufsize(sc->sc_ah, sc->sc_edma_bufsize);
744
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);
750
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",
754             sc->sc_tx_desclen);
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",
760             sc->sc_tx_nmaps);
761
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;
767
768         sc->sc_rx.recv_setup = ath_edma_dma_rxsetup;
769         sc->sc_rx.recv_teardown = ath_edma_dma_rxteardown;
770 }