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