]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ath/if_ath_rx_edma.c
Make sure that 'rs' is pointing to the correct RX status.
[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_RX_LOCK(sc);
160         ath_hal_stoppcurecv(ah);
161         ath_hal_setrxfilter(ah, 0);
162         ath_hal_stopdmarecv(ah);
163
164         DELAY(3000);
165
166         /* Flush RX pending for each queue */
167         /* XXX should generic-ify this */
168         if (sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending) {
169                 m_freem(sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending);
170                 sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending = NULL;
171         }
172
173         if (sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending) {
174                 m_freem(sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending);
175                 sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending = NULL;
176         }
177         ATH_RX_UNLOCK(sc);
178 }
179
180 /*
181  * Re-initialise the FIFO given the current buffer contents.
182  * Specifically, walk from head -> tail, pushing the FIFO contents
183  * back into the FIFO.
184  */
185 static void
186 ath_edma_reinit_fifo(struct ath_softc *sc, HAL_RX_QUEUE qtype)
187 {
188         struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
189         struct ath_buf *bf;
190         int i, j;
191
192         ATH_RX_LOCK_ASSERT(sc);
193
194         i = re->m_fifo_head;
195         for (j = 0; j < re->m_fifo_depth; j++) {
196                 bf = re->m_fifo[i];
197                 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
198                     "%s: Q%d: pos=%i, addr=0x%jx\n",
199                     __func__,
200                     qtype,
201                     i,
202                     (uintmax_t)bf->bf_daddr);
203                 ath_hal_putrxbuf(sc->sc_ah, bf->bf_daddr, qtype);
204                 INCR(i, re->m_fifolen);
205         }
206
207         /* Ensure this worked out right */
208         if (i != re->m_fifo_tail) {
209                 device_printf(sc->sc_dev, "%s: i (%d) != tail! (%d)\n",
210                     __func__,
211                     i,
212                     re->m_fifo_tail);
213         }
214 }
215
216 /*
217  * Start receive.
218  *
219  * XXX TODO: this needs to reallocate the FIFO entries when a reset
220  * occurs, in case the FIFO is filled up and no new descriptors get
221  * thrown into the FIFO.
222  */
223 static int
224 ath_edma_startrecv(struct ath_softc *sc)
225 {
226         struct ath_hal *ah = sc->sc_ah;
227
228         ATH_RX_LOCK(sc);
229
230         /* Enable RX FIFO */
231         ath_hal_rxena(ah);
232
233         /*
234          * Entries should only be written out if the
235          * FIFO is empty.
236          *
237          * XXX This isn't correct. I should be looking
238          * at the value of AR_RXDP_SIZE (0x0070) to determine
239          * how many entries are in here.
240          *
241          * A warm reset will clear the registers but not the FIFO.
242          *
243          * And I believe this is actually the address of the last
244          * handled buffer rather than the current FIFO pointer.
245          * So if no frames have been (yet) seen, we'll reinit the
246          * FIFO.
247          *
248          * I'll chase that up at some point.
249          */
250         if (ath_hal_getrxbuf(sc->sc_ah, HAL_RX_QUEUE_HP) == 0) {
251                 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
252                     "%s: Re-initing HP FIFO\n", __func__);
253                 ath_edma_reinit_fifo(sc, HAL_RX_QUEUE_HP);
254         }
255         if (ath_hal_getrxbuf(sc->sc_ah, HAL_RX_QUEUE_LP) == 0) {
256                 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
257                     "%s: Re-initing LP FIFO\n", __func__);
258                 ath_edma_reinit_fifo(sc, HAL_RX_QUEUE_LP);
259         }
260
261         /* Add up to m_fifolen entries in each queue */
262         /*
263          * These must occur after the above write so the FIFO buffers
264          * are pushed/tracked in the same order as the hardware will
265          * process them.
266          */
267         ath_edma_rxfifo_alloc(sc, HAL_RX_QUEUE_HP,
268             sc->sc_rxedma[HAL_RX_QUEUE_HP].m_fifolen);
269
270         ath_edma_rxfifo_alloc(sc, HAL_RX_QUEUE_LP,
271             sc->sc_rxedma[HAL_RX_QUEUE_LP].m_fifolen);
272
273         ath_mode_init(sc);
274         ath_hal_startpcurecv(ah);
275
276         ATH_RX_UNLOCK(sc);
277
278         return (0);
279 }
280
281 static void
282 ath_edma_recv_flush(struct ath_softc *sc)
283 {
284
285         device_printf(sc->sc_dev, "%s: called\n", __func__);
286
287         ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_HP, 0);
288         ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_LP, 0);
289 }
290
291 /*
292  * Process frames from the current queue.
293  *
294  * TODO:
295  *
296  * + Add a "dosched" flag, so we don't reschedule any FIFO frames
297  *   to the hardware or re-kick the PCU after 'kickpcu' is set.
298  *
299  * + Perhaps split "check FIFO contents" and "handle frames", so
300  *   we can run the "check FIFO contents" in ath_intr(), but
301  *   "handle frames" in the RX tasklet.
302  */
303 static int
304 ath_edma_recv_proc_queue(struct ath_softc *sc, HAL_RX_QUEUE qtype,
305     int dosched)
306 {
307         struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
308         struct ath_rx_status *rs;
309         struct ath_desc *ds;
310         struct ath_buf *bf;
311         struct mbuf *m;
312         struct ath_hal *ah = sc->sc_ah;
313         uint64_t tsf;
314         int16_t nf;
315         int ngood = 0;
316         ath_bufhead rxlist;
317         struct ath_buf *next;
318
319         TAILQ_INIT(&rxlist);
320
321         tsf = ath_hal_gettsf64(ah);
322         nf = ath_hal_getchannoise(ah, sc->sc_curchan);
323         sc->sc_stats.ast_rx_noise = nf;
324
325         ATH_RX_LOCK(sc);
326
327         do {
328                 bf = re->m_fifo[re->m_fifo_head];
329                 /* This shouldn't occur! */
330                 if (bf == NULL) {
331                         device_printf(sc->sc_dev, "%s: Q%d: NULL bf?\n",
332                             __func__,
333                             qtype);
334                         break;
335                 }
336                 m = bf->bf_m;
337                 ds = bf->bf_desc;
338
339                 /*
340                  * Sync descriptor memory - this also syncs the buffer for us.
341                  *
342                  * EDMA descriptors are in cached memory.
343                  */
344                 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
345                     BUS_DMASYNC_POSTREAD);
346                 rs = &bf->bf_status.ds_rxstat;
347                 bf->bf_rxstatus = ath_hal_rxprocdesc(ah, ds, bf->bf_daddr,
348                     NULL, rs);
349 #ifdef  ATH_DEBUG
350                 if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
351                         ath_printrxbuf(sc, bf, 0, bf->bf_rxstatus == HAL_OK);
352 #endif
353                 if (bf->bf_rxstatus == HAL_EINPROGRESS)
354                         break;
355
356                 /*
357                  * Completed descriptor.
358                  *
359                  * In the future we'll call ath_rx_pkt(), but it first
360                  * has to be taught about EDMA RX queues (so it can
361                  * access sc_rxpending correctly.)
362                  */
363                 DPRINTF(sc, ATH_DEBUG_EDMA_RX,
364                     "%s: Q%d: completed!\n", __func__, qtype);
365
366                 /*
367                  * Remove the FIFO entry and place it on the completion
368                  * queue.
369                  */
370                 re->m_fifo[re->m_fifo_head] = NULL;
371                 TAILQ_INSERT_TAIL(&rxlist, bf, bf_list);
372
373                 /* Bump the descriptor FIFO stats */
374                 INCR(re->m_fifo_head, re->m_fifolen);
375                 re->m_fifo_depth--;
376                 /* XXX check it doesn't fall below 0 */
377         } while (re->m_fifo_depth > 0);
378
379         /* Append some more fresh frames to the FIFO */
380         if (dosched)
381                 ath_edma_rxfifo_alloc(sc, qtype, re->m_fifolen);
382
383         ATH_RX_UNLOCK(sc);
384
385         /* Handle the completed descriptors */
386         TAILQ_FOREACH_SAFE(bf, &rxlist, bf_list, next) {
387                 /*
388                  * Skip the RX descriptor status - start at the data offset
389                  */
390                 m_adj(bf->bf_m, sc->sc_rx_statuslen);
391
392                 /* Handle the frame */
393                 /*
394                  * Note: this may or may not free bf->bf_m and sync/unmap
395                  * the frame.
396                  */
397                 rs = &bf->bf_status.ds_rxstat;
398                 if (ath_rx_pkt(sc, rs, bf->bf_rxstatus, tsf, nf, qtype, bf))
399                         ngood++;
400         }
401
402         /* Free in one set, inside the lock */
403         ATH_RX_LOCK(sc);
404         TAILQ_FOREACH_SAFE(bf, &rxlist, bf_list, next) {
405                 /* Free the buffer/mbuf */
406                 ath_edma_rxbuf_free(sc, bf);
407         }
408         ATH_RX_UNLOCK(sc);
409
410         /* Handle resched and kickpcu appropriately */
411         ATH_PCU_LOCK(sc);
412         if (dosched && sc->sc_kickpcu) {
413                 CTR0(ATH_KTR_ERR, "ath_edma_recv_proc_queue(): kickpcu");
414                 device_printf(sc->sc_dev, "%s: handled %d descriptors\n",
415                     __func__, ngood);
416
417                 /*
418                  * XXX TODO: what should occur here? Just re-poke and
419                  * re-enable the RX FIFO?
420                  */
421                 sc->sc_kickpcu = 0;
422         }
423         ATH_PCU_UNLOCK(sc);
424
425         return (ngood);
426 }
427
428 static void
429 ath_edma_recv_tasklet(void *arg, int npending)
430 {
431         struct ath_softc *sc = (struct ath_softc *) arg;
432
433         DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: called; npending=%d\n",
434             __func__,
435             npending);
436
437         ATH_PCU_LOCK(sc);
438         if (sc->sc_inreset_cnt > 0) {
439                 device_printf(sc->sc_dev, "%s: sc_inreset_cnt > 0; skipping\n",
440                     __func__);
441                 ATH_PCU_UNLOCK(sc);
442                 return;
443         }
444         ATH_PCU_UNLOCK(sc);
445
446         ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_HP, 1);
447         ath_edma_recv_proc_queue(sc, HAL_RX_QUEUE_LP, 1);
448 }
449
450 /*
451  * Allocate an RX mbuf for the given ath_buf and initialise
452  * it for EDMA.
453  *
454  * + Allocate a 4KB mbuf;
455  * + Setup the DMA map for the given buffer;
456  * + Keep a pointer to the start of the mbuf - that's where the
457  *   descriptor lies;
458  * + Take a pointer to the start of the RX buffer, set the
459  *   mbuf "start" to be there;
460  * + Return that.
461  */
462 static int
463 ath_edma_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
464 {
465
466         struct mbuf *m;
467         int error;
468         int len;
469
470         ATH_RX_LOCK_ASSERT(sc);
471
472 //      device_printf(sc->sc_dev, "%s: called; bf=%p\n", __func__, bf);
473
474         m = m_getm(NULL, sc->sc_edma_bufsize, M_DONTWAIT, MT_DATA);
475         if (! m)
476                 return (ENOBUFS);               /* XXX ?*/
477
478         /* XXX warn/enforce alignment */
479
480         len = m->m_ext.ext_size;
481 #if 0
482         device_printf(sc->sc_dev, "%s: called: m=%p, size=%d, mtod=%p\n",
483             __func__,
484             m,
485             len,
486             mtod(m, char *));
487 #endif
488
489         m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
490
491         /*
492          * Create DMA mapping.
493          */
494         error = bus_dmamap_load_mbuf_sg(sc->sc_dmat,
495             bf->bf_dmamap, m, bf->bf_segs, &bf->bf_nseg, BUS_DMA_NOWAIT);
496         if (error != 0) {
497                 device_printf(sc->sc_dev, "%s: failed; error=%d\n",
498                     __func__,
499                     error);
500                 m_freem(m);
501                 return (error);
502         }
503
504         /*
505          * Populate ath_buf fields.
506          */
507
508         bf->bf_desc = mtod(m, struct ath_desc *);
509         bf->bf_daddr = bf->bf_segs[0].ds_addr;
510         bf->bf_lastds = bf->bf_desc;    /* XXX only really for TX? */
511         bf->bf_m = m;
512
513         /* Zero the descriptor */
514         memset(bf->bf_desc, '\0', sc->sc_rx_statuslen);
515
516 #if 0
517         /*
518          * Adjust mbuf header and length/size to compensate for the
519          * descriptor size.
520          */
521         m_adj(m, sc->sc_rx_statuslen);
522 #endif
523
524         /* Finish! */
525
526         return (0);
527 }
528
529 static struct ath_buf *
530 ath_edma_rxbuf_alloc(struct ath_softc *sc)
531 {
532         struct ath_buf *bf;
533         int error;
534
535         ATH_RX_LOCK_ASSERT(sc);
536
537         /* Allocate buffer */
538         bf = TAILQ_FIRST(&sc->sc_rxbuf);
539         /* XXX shouldn't happen upon startup? */
540         if (bf == NULL)
541                 return (NULL);
542
543         /* Remove it from the free list */
544         TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
545
546         /* Assign RX mbuf to it */
547         error = ath_edma_rxbuf_init(sc, bf);
548         if (error != 0) {
549                 device_printf(sc->sc_dev,
550                     "%s: bf=%p, rxbuf alloc failed! error=%d\n",
551                     __func__,
552                     bf,
553                     error);
554                 TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
555                 return (NULL);
556         }
557
558         return (bf);
559 }
560
561 static void
562 ath_edma_rxbuf_free(struct ath_softc *sc, struct ath_buf *bf)
563 {
564
565         ATH_RX_LOCK_ASSERT(sc);
566
567         /* We're doing this multiple times? */
568         bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
569
570         if (bf->bf_m) {
571                 m_freem(bf->bf_m);
572                 bf->bf_m = NULL;
573         }
574
575         /* XXX lock? */
576         TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
577 }
578
579 /*
580  * Allocate up to 'n' entries and push them onto the hardware FIFO.
581  *
582  * Return how many entries were successfully pushed onto the
583  * FIFO.
584  */
585 static int
586 ath_edma_rxfifo_alloc(struct ath_softc *sc, HAL_RX_QUEUE qtype, int nbufs)
587 {
588         struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
589         struct ath_buf *bf;
590         int i;
591
592         ATH_RX_LOCK_ASSERT(sc);
593
594         /*
595          * Allocate buffers until the FIFO is full or nbufs is reached.
596          */
597         for (i = 0; i < nbufs && re->m_fifo_depth < re->m_fifolen; i++) {
598                 /* Ensure the FIFO is already blank, complain loudly! */
599                 if (re->m_fifo[re->m_fifo_tail] != NULL) {
600                         device_printf(sc->sc_dev,
601                             "%s: Q%d: fifo[%d] != NULL (%p)\n",
602                             __func__,
603                             qtype,
604                             re->m_fifo_tail,
605                             re->m_fifo[re->m_fifo_tail]);
606
607                         /* Free the slot */
608                         ath_edma_rxbuf_free(sc, re->m_fifo[re->m_fifo_tail]);
609                         re->m_fifo_depth--;
610                         /* XXX check it's not < 0 */
611                         re->m_fifo[re->m_fifo_tail] = NULL;
612                 }
613
614                 bf = ath_edma_rxbuf_alloc(sc);
615                 /* XXX should ensure the FIFO is not NULL? */
616                 if (bf == NULL) {
617                         device_printf(sc->sc_dev, "%s: Q%d: alloc failed?\n",
618                             __func__,
619                             qtype);
620                         break;
621                 }
622
623                 re->m_fifo[re->m_fifo_tail] = bf;
624
625                 /*
626                  * Flush the descriptor contents before it's handed to the
627                  * hardware.
628                  */
629                 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
630                     BUS_DMASYNC_PREREAD);
631
632                 /* Write to the RX FIFO */
633                 DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: Q%d: putrxbuf=%p\n",
634                     __func__,
635                     qtype,
636                     bf->bf_desc);
637                 ath_hal_putrxbuf(sc->sc_ah, bf->bf_daddr, qtype);
638
639                 re->m_fifo_depth++;
640                 INCR(re->m_fifo_tail, re->m_fifolen);
641         }
642
643         /*
644          * Return how many were allocated.
645          */
646         DPRINTF(sc, ATH_DEBUG_EDMA_RX, "%s: Q%d: nbufs=%d, nalloced=%d\n",
647             __func__,
648             qtype,
649             nbufs,
650             i);
651         return (i);
652 }
653
654 static int
655 ath_edma_rxfifo_flush(struct ath_softc *sc, HAL_RX_QUEUE qtype)
656 {
657         struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
658         int i;
659
660         ATH_RX_LOCK_ASSERT(sc);
661
662         for (i = 0; i < re->m_fifolen; i++) {
663                 if (re->m_fifo[i] != NULL) {
664 #ifdef  ATH_DEBUG
665                         struct ath_buf *bf = re->m_fifo[i];
666
667                         if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
668                                 ath_printrxbuf(sc, bf, 0, HAL_OK);
669 #endif
670                         ath_edma_rxbuf_free(sc, re->m_fifo[i]);
671                         re->m_fifo[i] = NULL;
672                         re->m_fifo_depth--;
673                 }
674         }
675
676         if (re->m_rxpending != NULL) {
677                 m_freem(re->m_rxpending);
678                 re->m_rxpending = NULL;
679         }
680         re->m_fifo_head = re->m_fifo_tail = re->m_fifo_depth = 0;
681
682         return (0);
683 }
684
685 /*
686  * Setup the initial RX FIFO structure.
687  */
688 static int
689 ath_edma_setup_rxfifo(struct ath_softc *sc, HAL_RX_QUEUE qtype)
690 {
691         struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
692
693         ATH_RX_LOCK_ASSERT(sc);
694
695         if (! ath_hal_getrxfifodepth(sc->sc_ah, qtype, &re->m_fifolen)) {
696                 device_printf(sc->sc_dev, "%s: qtype=%d, failed\n",
697                     __func__,
698                     qtype);
699                 return (-EINVAL);
700         }
701         device_printf(sc->sc_dev, "%s: type=%d, FIFO depth = %d entries\n",
702             __func__,
703             qtype,
704             re->m_fifolen);
705
706         /* Allocate ath_buf FIFO array, pre-zero'ed */
707         re->m_fifo = malloc(sizeof(struct ath_buf *) * re->m_fifolen,
708             M_ATHDEV,
709             M_NOWAIT | M_ZERO);
710         if (re->m_fifo == NULL) {
711                 device_printf(sc->sc_dev, "%s: malloc failed\n",
712                     __func__);
713                 return (-ENOMEM);
714         }
715
716         /*
717          * Set initial "empty" state.
718          */
719         re->m_rxpending = NULL;
720         re->m_fifo_head = re->m_fifo_tail = re->m_fifo_depth = 0;
721
722         return (0);
723 }
724
725 static int
726 ath_edma_rxfifo_free(struct ath_softc *sc, HAL_RX_QUEUE qtype)
727 {
728         struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
729
730         device_printf(sc->sc_dev, "%s: called; qtype=%d\n",
731             __func__,
732             qtype);
733         
734         free(re->m_fifo, M_ATHDEV);
735
736         return (0);
737 }
738
739 static int
740 ath_edma_dma_rxsetup(struct ath_softc *sc)
741 {
742         int error;
743
744         /*
745          * Create RX DMA tag and buffers.
746          */
747         error = ath_descdma_setup_rx_edma(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
748             "rx", ath_rxbuf, sc->sc_rx_statuslen);
749         if (error != 0)
750                 return error;
751
752         ATH_RX_LOCK(sc);
753         (void) ath_edma_setup_rxfifo(sc, HAL_RX_QUEUE_HP);
754         (void) ath_edma_setup_rxfifo(sc, HAL_RX_QUEUE_LP);
755         ATH_RX_UNLOCK(sc);
756
757         return (0);
758 }
759
760 static int
761 ath_edma_dma_rxteardown(struct ath_softc *sc)
762 {
763
764         device_printf(sc->sc_dev, "%s: called\n", __func__);
765
766         ATH_RX_LOCK(sc);
767         ath_edma_rxfifo_flush(sc, HAL_RX_QUEUE_HP);
768         ath_edma_rxfifo_free(sc, HAL_RX_QUEUE_HP);
769
770         ath_edma_rxfifo_flush(sc, HAL_RX_QUEUE_LP);
771         ath_edma_rxfifo_free(sc, HAL_RX_QUEUE_LP);
772         ATH_RX_UNLOCK(sc);
773
774         /* Free RX ath_buf */
775         /* Free RX DMA tag */
776         if (sc->sc_rxdma.dd_desc_len != 0)
777                 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
778
779         return (0);
780 }
781
782 void
783 ath_recv_setup_edma(struct ath_softc *sc)
784 {
785
786         device_printf(sc->sc_dev, "DMA setup: EDMA\n");
787
788         /* Set buffer size to 4k */
789         sc->sc_edma_bufsize = 4096;
790
791         /* Fetch EDMA field and buffer sizes */
792         (void) ath_hal_getrxstatuslen(sc->sc_ah, &sc->sc_rx_statuslen);
793         (void) ath_hal_gettxdesclen(sc->sc_ah, &sc->sc_tx_desclen);
794         (void) ath_hal_gettxstatuslen(sc->sc_ah, &sc->sc_tx_statuslen);
795         (void) ath_hal_getntxmaps(sc->sc_ah, &sc->sc_tx_nmaps);
796
797         /* Configure the hardware with the RX buffer size */
798         (void) ath_hal_setrxbufsize(sc->sc_ah, sc->sc_edma_bufsize -
799             sc->sc_rx_statuslen);
800
801         device_printf(sc->sc_dev, "RX status length: %d\n",
802             sc->sc_rx_statuslen);
803         device_printf(sc->sc_dev, "TX descriptor length: %d\n",
804             sc->sc_tx_desclen);
805         device_printf(sc->sc_dev, "TX status length: %d\n",
806             sc->sc_tx_statuslen);
807         device_printf(sc->sc_dev, "TX/RX buffer size: %d\n",
808             sc->sc_edma_bufsize);
809         device_printf(sc->sc_dev, "TX buffers per descriptor: %d\n",
810             sc->sc_tx_nmaps);
811
812         sc->sc_rx.recv_stop = ath_edma_stoprecv;
813         sc->sc_rx.recv_start = ath_edma_startrecv;
814         sc->sc_rx.recv_flush = ath_edma_recv_flush;
815         sc->sc_rx.recv_tasklet = ath_edma_recv_tasklet;
816         sc->sc_rx.recv_rxbuf_init = ath_edma_rxbuf_init;
817
818         sc->sc_rx.recv_setup = ath_edma_dma_rxsetup;
819         sc->sc_rx.recv_teardown = ath_edma_dma_rxteardown;
820 }