42bfe0757cc61a38e6351c0aed0e1858f676e696
[dragonfly.git] / sys / dev / netif / ath / ath / if_ath_rx.c
1 /*-
2  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
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
32 /*
33  * Driver for the Atheros Wireless LAN controller.
34  *
35  * This software is derived from work of Atsushi Onoe; his contribution
36  * is greatly appreciated.
37  */
38
39 #include "opt_inet.h"
40 #include "opt_ath.h"
41 /*
42  * This is needed for register operations which are performed
43  * by the driver - eg, calls to ath_hal_gettsf32().
44  *
45  * It's also required for any AH_DEBUG checks in here, eg the
46  * module dependencies.
47  */
48 #include "opt_ah.h"
49 #include "opt_wlan.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/sysctl.h>
54 #include <sys/mbuf.h>
55 #include <sys/malloc.h>
56 #include <sys/lock.h>
57 #include <sys/mutex.h>
58 #include <sys/kernel.h>
59 #include <sys/socket.h>
60 #include <sys/sockio.h>
61 #include <sys/errno.h>
62 #include <sys/callout.h>
63 #include <sys/bus.h>
64 #include <sys/endian.h>
65 #include <sys/kthread.h>
66 #include <sys/taskqueue.h>
67 #include <sys/priv.h>
68 #include <sys/module.h>
69 #include <sys/ktr.h>
70
71 #include <net/if.h>
72 #include <net/if_var.h>
73 #include <net/if_dl.h>
74 #include <net/if_media.h>
75 #include <net/if_types.h>
76 #include <net/if_arp.h>
77 #include <net/ethernet.h>
78 #include <net/if_llc.h>
79 #include <net/ifq_var.h>
80
81 #include <netproto/802_11/ieee80211_var.h>
82 #include <netproto/802_11/ieee80211_regdomain.h>
83 #ifdef IEEE80211_SUPPORT_SUPERG
84 #include <netproto/802_11/ieee80211_superg.h>
85 #endif
86 #ifdef IEEE80211_SUPPORT_TDMA
87 #include <netproto/802_11/ieee80211_tdma.h>
88 #endif
89
90 #include <net/bpf.h>
91
92 #ifdef INET
93 #include <netinet/in.h>
94 #include <netinet/if_ether.h>
95 #endif
96
97 #include <dev/netif/ath/ath/if_athvar.h>
98 #include <dev/netif/ath/ath_hal/ah_devid.h>             /* XXX for softled */
99 #include <dev/netif/ath/ath_hal/ah_diagcodes.h>
100
101 #include <dev/netif/ath/ath/if_ath_debug.h>
102 #include <dev/netif/ath/ath/if_ath_misc.h>
103 #include <dev/netif/ath/ath/if_ath_tsf.h>
104 #include <dev/netif/ath/ath/if_ath_tx.h>
105 #include <dev/netif/ath/ath/if_ath_sysctl.h>
106 #include <dev/netif/ath/ath/if_ath_led.h>
107 #include <dev/netif/ath/ath/if_ath_keycache.h>
108 #include <dev/netif/ath/ath/if_ath_rx.h>
109 #include <dev/netif/ath/ath/if_ath_beacon.h>
110 #include <dev/netif/ath/ath/if_athdfs.h>
111
112 #ifdef ATH_TX99_DIAG
113 #include <dev/netif/ath/ath_tx99/ath_tx99.h>
114 #endif
115
116 #ifdef  ATH_DEBUG_ALQ
117 #include <dev/netif/ath/ath/if_ath_alq.h>
118 #endif
119
120 #include <dev/netif/ath/ath/if_ath_lna_div.h>
121
122 /*
123  * Calculate the receive filter according to the
124  * operating mode and state:
125  *
126  * o always accept unicast, broadcast, and multicast traffic
127  * o accept PHY error frames when hardware doesn't have MIB support
128  *   to count and we need them for ANI (sta mode only until recently)
129  *   and we are not scanning (ANI is disabled)
130  *   NB: older hal's add rx filter bits out of sight and we need to
131  *       blindly preserve them
132  * o probe request frames are accepted only when operating in
133  *   hostap, adhoc, mesh, or monitor modes
134  * o enable promiscuous mode
135  *   - when in monitor mode
136  *   - if interface marked PROMISC (assumes bridge setting is filtered)
137  * o accept beacons:
138  *   - when operating in station mode for collecting rssi data when
139  *     the station is otherwise quiet, or
140  *   - when operating in adhoc mode so the 802.11 layer creates
141  *     node table entries for peers,
142  *   - when scanning
143  *   - when doing s/w beacon miss (e.g. for ap+sta)
144  *   - when operating in ap mode in 11g to detect overlapping bss that
145  *     require protection
146  *   - when operating in mesh mode to detect neighbors
147  * o accept control frames:
148  *   - when in monitor mode
149  * XXX HT protection for 11n
150  */
151 u_int32_t
152 ath_calcrxfilter(struct ath_softc *sc)
153 {
154         struct ifnet *ifp = sc->sc_ifp;
155         struct ieee80211com *ic = ifp->if_l2com;
156         u_int32_t rfilt;
157
158         rfilt = HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
159         if (!sc->sc_needmib && !sc->sc_scanning)
160                 rfilt |= HAL_RX_FILTER_PHYERR;
161         if (ic->ic_opmode != IEEE80211_M_STA)
162                 rfilt |= HAL_RX_FILTER_PROBEREQ;
163         /* XXX ic->ic_monvaps != 0? */
164         if (ic->ic_opmode == IEEE80211_M_MONITOR || (ifp->if_flags & IFF_PROMISC))
165                 rfilt |= HAL_RX_FILTER_PROM;
166
167         /*
168          * Only listen to all beacons if we're scanning.
169          *
170          * Otherwise we only really need to hear beacons from
171          * our own BSSID.
172          */
173         if (ic->ic_opmode == IEEE80211_M_STA ||
174             ic->ic_opmode == IEEE80211_M_IBSS || sc->sc_swbmiss) {
175                 if (sc->sc_do_mybeacon && ! sc->sc_scanning) {
176                         rfilt |= HAL_RX_FILTER_MYBEACON;
177                 } else { /* scanning, non-mybeacon chips */
178                         rfilt |= HAL_RX_FILTER_BEACON;
179                 }
180         }
181
182         /*
183          * NB: We don't recalculate the rx filter when
184          * ic_protmode changes; otherwise we could do
185          * this only when ic_protmode != NONE.
186          */
187         if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
188             IEEE80211_IS_CHAN_ANYG(ic->ic_curchan))
189                 rfilt |= HAL_RX_FILTER_BEACON;
190
191         /*
192          * Enable hardware PS-POLL RX only for hostap mode;
193          * STA mode sends PS-POLL frames but never
194          * receives them.
195          */
196         if (ath_hal_getcapability(sc->sc_ah, HAL_CAP_PSPOLL,
197             0, NULL) == HAL_OK &&
198             ic->ic_opmode == IEEE80211_M_HOSTAP)
199                 rfilt |= HAL_RX_FILTER_PSPOLL;
200
201         if (sc->sc_nmeshvaps) {
202                 rfilt |= HAL_RX_FILTER_BEACON;
203                 if (sc->sc_hasbmatch)
204                         rfilt |= HAL_RX_FILTER_BSSID;
205                 else
206                         rfilt |= HAL_RX_FILTER_PROM;
207         }
208         if (ic->ic_opmode == IEEE80211_M_MONITOR)
209                 rfilt |= HAL_RX_FILTER_CONTROL;
210
211         /*
212          * Enable RX of compressed BAR frames only when doing
213          * 802.11n. Required for A-MPDU.
214          */
215         if (IEEE80211_IS_CHAN_HT(ic->ic_curchan))
216                 rfilt |= HAL_RX_FILTER_COMPBAR;
217
218         /*
219          * Enable radar PHY errors if requested by the
220          * DFS module.
221          */
222         if (sc->sc_dodfs)
223                 rfilt |= HAL_RX_FILTER_PHYRADAR;
224
225         /*
226          * Enable spectral PHY errors if requested by the
227          * spectral module.
228          */
229         if (sc->sc_dospectral)
230                 rfilt |= HAL_RX_FILTER_PHYRADAR;
231
232         DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, %s if_flags 0x%x\n",
233             __func__, rfilt, ieee80211_opmode_name[ic->ic_opmode], ifp->if_flags);
234         return rfilt;
235 }
236
237 static int
238 ath_legacy_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
239 {
240         struct ath_hal *ah = sc->sc_ah;
241         int error;
242         struct mbuf *m;
243         struct ath_desc *ds;
244
245         /* XXX TODO: ATH_RX_LOCK_ASSERT(sc); */
246
247         m = bf->bf_m;
248         if (m == NULL) {
249                 /*
250                  * NB: by assigning a page to the rx dma buffer we
251                  * implicitly satisfy the Atheros requirement that
252                  * this buffer be cache-line-aligned and sized to be
253                  * multiple of the cache line size.  Not doing this
254                  * causes weird stuff to happen (for the 5210 at least).
255                  */
256                 m = m_getcl(MB_WAIT, MT_DATA, M_PKTHDR);
257                 if (m == NULL) {
258                         DPRINTF(sc, ATH_DEBUG_ANY,
259                                 "%s: no mbuf/cluster\n", __func__);
260                         sc->sc_stats.ast_rx_nombuf++;
261                         return ENOMEM;
262                 }
263                 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
264
265                 error = bus_dmamap_load_mbuf_segment(sc->sc_dmat,
266                                              bf->bf_dmamap, m,
267                                              bf->bf_segs, 1, &bf->bf_nseg,
268                                              BUS_DMA_NOWAIT);
269                 if (error != 0) {
270                         DPRINTF(sc, ATH_DEBUG_ANY,
271                             "%s: bus_dmamap_load_mbuf_sg failed; error %d\n",
272                             __func__, error);
273                         sc->sc_stats.ast_rx_busdma++;
274                         m_freem(m);
275                         return error;
276                 }
277                 KASSERT(bf->bf_nseg == 1,
278                         ("multi-segment packet; nseg %u", bf->bf_nseg));
279                 bf->bf_m = m;
280         }
281         bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
282
283         /*
284          * Setup descriptors.  For receive we always terminate
285          * the descriptor list with a self-linked entry so we'll
286          * not get overrun under high load (as can happen with a
287          * 5212 when ANI processing enables PHY error frames).
288          *
289          * To insure the last descriptor is self-linked we create
290          * each descriptor as self-linked and add it to the end.  As
291          * each additional descriptor is added the previous self-linked
292          * entry is ``fixed'' naturally.  This should be safe even
293          * if DMA is happening.  When processing RX interrupts we
294          * never remove/process the last, self-linked, entry on the
295          * descriptor list.  This insures the hardware always has
296          * someplace to write a new frame.
297          */
298         /*
299          * 11N: we can no longer afford to self link the last descriptor.
300          * MAC acknowledges BA status as long as it copies frames to host
301          * buffer (or rx fifo). This can incorrectly acknowledge packets
302          * to a sender if last desc is self-linked.
303          */
304         ds = bf->bf_desc;
305         if (sc->sc_rxslink)
306                 ds->ds_link = bf->bf_daddr;     /* link to self */
307         else
308                 ds->ds_link = 0;                /* terminate the list */
309         ds->ds_data = bf->bf_segs[0].ds_addr;
310         ath_hal_setuprxdesc(ah, ds
311                 , m->m_len              /* buffer size */
312                 , 0
313         );
314
315         if (sc->sc_rxlink != NULL)
316                 *sc->sc_rxlink = bf->bf_daddr;
317         sc->sc_rxlink = &ds->ds_link;
318         return 0;
319 }
320
321 /*
322  * Intercept management frames to collect beacon rssi data
323  * and to do ibss merges.
324  */
325 void
326 ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
327         int subtype, int rssi, int nf)
328 {
329         struct ieee80211vap *vap = ni->ni_vap;
330         struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
331         uint64_t tsf_beacon_old, tsf_beacon;
332         uint64_t nexttbtt;
333         int64_t tsf_delta;
334         int32_t tsf_delta_bmiss;
335         int32_t tsf_remainder;
336         uint64_t tsf_beacon_target;
337         int tsf_intval;
338
339         tsf_beacon_old = ((uint64_t) LE_READ_4(ni->ni_tstamp.data + 4)) << 32;
340         tsf_beacon_old |= LE_READ_4(ni->ni_tstamp.data);
341
342 #define TU_TO_TSF(_tu)  (((u_int64_t)(_tu)) << 10)
343         tsf_intval = 1;
344         if (ni != NULL && ni->ni_intval > 0) {
345                 tsf_intval = TU_TO_TSF(ni->ni_intval);
346         }
347 #undef  TU_TO_TSF
348
349         /*
350          * Call up first so subsequent work can use information
351          * potentially stored in the node (e.g. for ibss merge).
352          */
353         ATH_VAP(vap)->av_recv_mgmt(ni, m, subtype, rssi, nf);
354         switch (subtype) {
355         case IEEE80211_FC0_SUBTYPE_BEACON:
356                 /* update rssi statistics for use by the hal */
357                 /* XXX unlocked check against vap->iv_bss? */
358                 ATH_RSSI_LPF(sc->sc_halstats.ns_avgbrssi, rssi);
359
360                 tsf_beacon = ((uint64_t) LE_READ_4(ni->ni_tstamp.data + 4)) << 32;
361                 tsf_beacon |= LE_READ_4(ni->ni_tstamp.data);
362
363                 nexttbtt = ath_hal_getnexttbtt(sc->sc_ah);
364
365                 /*
366                  * Let's calculate the delta and remainder, so we can see
367                  * if the beacon timer from the AP is varying by more than
368                  * a few TU.  (Which would be a huge, huge problem.)
369                  */
370                 tsf_delta = (long long) tsf_beacon - (long long) tsf_beacon_old;
371
372                 tsf_delta_bmiss = tsf_delta / tsf_intval;
373
374                 /*
375                  * If our delta is greater than half the beacon interval,
376                  * let's round the bmiss value up to the next beacon
377                  * interval.  Ie, we're running really, really early
378                  * on the next beacon.
379                  */
380                 if (tsf_delta % tsf_intval > (tsf_intval / 2))
381                         tsf_delta_bmiss ++;
382
383                 tsf_beacon_target = tsf_beacon_old +
384                     (((unsigned long long) tsf_delta_bmiss) * (long long) tsf_intval);
385
386                 /*
387                  * The remainder using '%' is between 0 .. intval-1.
388                  * If we're actually running too fast, then the remainder
389                  * will be some large number just under intval-1.
390                  * So we need to look at whether we're running
391                  * before or after the target beacon interval
392                  * and if we are, modify how we do the remainder
393                  * calculation.
394                  */
395                 if (tsf_beacon < tsf_beacon_target) {
396                         tsf_remainder =
397                             -(tsf_intval - ((tsf_beacon - tsf_beacon_old) % tsf_intval));
398                 } else {
399                         tsf_remainder = (tsf_beacon - tsf_beacon_old) % tsf_intval;
400                 }
401
402                 DPRINTF(sc, ATH_DEBUG_BEACON, "%s: old_tsf=%llu, new_tsf=%llu, target_tsf=%llu, delta=%lld, bmiss=%d, remainder=%d\n",
403                     __func__,
404                     (unsigned long long) tsf_beacon_old,
405                     (unsigned long long) tsf_beacon,
406                     (unsigned long long) tsf_beacon_target,
407                     (long long) tsf_delta,
408                     tsf_delta_bmiss,
409                     tsf_remainder);
410
411                 DPRINTF(sc, ATH_DEBUG_BEACON, "%s: tsf=%llu, nexttbtt=%llu, delta=%d\n",
412                     __func__,
413                     (unsigned long long) tsf_beacon,
414                     (unsigned long long) nexttbtt,
415                     (int32_t) tsf_beacon - (int32_t) nexttbtt + tsf_intval);
416
417                 if (sc->sc_syncbeacon &&
418                     ni == vap->iv_bss &&
419                     (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)) {
420                         DPRINTF(sc, ATH_DEBUG_BEACON,
421                             "%s: syncbeacon=1; syncing\n",
422                             __func__);
423                         /*
424                          * Resync beacon timers using the tsf of the beacon
425                          * frame we just received.
426                          */
427                         ath_beacon_config(sc, vap);
428                         sc->sc_syncbeacon = 0;
429                 }
430
431
432                 /* fall thru... */
433         case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
434                 if (vap->iv_opmode == IEEE80211_M_IBSS &&
435                     vap->iv_state == IEEE80211_S_RUN) {
436                         uint32_t rstamp = sc->sc_lastrs->rs_tstamp;
437                         uint64_t tsf = ath_extend_tsf(sc, rstamp,
438                                 ath_hal_gettsf64(sc->sc_ah));
439                         /*
440                          * Handle ibss merge as needed; check the tsf on the
441                          * frame before attempting the merge.  The 802.11 spec
442                          * says the station should change it's bssid to match
443                          * the oldest station with the same ssid, where oldest
444                          * is determined by the tsf.  Note that hardware
445                          * reconfiguration happens through callback to
446                          * ath_newstate as the state machine will go from
447                          * RUN -> RUN when this happens.
448                          */
449                         if (le64toh(ni->ni_tstamp.tsf) >= tsf) {
450                                 DPRINTF(sc, ATH_DEBUG_STATE,
451                                     "ibss merge, rstamp %u tsf %ju "
452                                     "tstamp %ju\n", rstamp, (uintmax_t)tsf,
453                                     (uintmax_t)ni->ni_tstamp.tsf);
454                                 (void) ieee80211_ibss_merge(ni);
455                         }
456                 }
457                 break;
458         }
459 }
460
461 #ifdef  ATH_ENABLE_RADIOTAP_VENDOR_EXT
462 static void
463 ath_rx_tap_vendor(struct ifnet *ifp, struct mbuf *m,
464     const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf)
465 {
466         struct ath_softc *sc = ifp->if_softc;
467
468         /* Fill in the extension bitmap */
469         sc->sc_rx_th.wr_ext_bitmap = htole32(1 << ATH_RADIOTAP_VENDOR_HEADER);
470
471         /* Fill in the vendor header */
472         sc->sc_rx_th.wr_vh.vh_oui[0] = 0x7f;
473         sc->sc_rx_th.wr_vh.vh_oui[1] = 0x03;
474         sc->sc_rx_th.wr_vh.vh_oui[2] = 0x00;
475
476         /* XXX what should this be? */
477         sc->sc_rx_th.wr_vh.vh_sub_ns = 0;
478         sc->sc_rx_th.wr_vh.vh_skip_len =
479             htole16(sizeof(struct ath_radiotap_vendor_hdr));
480
481         /* General version info */
482         sc->sc_rx_th.wr_v.vh_version = 1;
483
484         sc->sc_rx_th.wr_v.vh_rx_chainmask = sc->sc_rxchainmask;
485
486         /* rssi */
487         sc->sc_rx_th.wr_v.rssi_ctl[0] = rs->rs_rssi_ctl[0];
488         sc->sc_rx_th.wr_v.rssi_ctl[1] = rs->rs_rssi_ctl[1];
489         sc->sc_rx_th.wr_v.rssi_ctl[2] = rs->rs_rssi_ctl[2];
490         sc->sc_rx_th.wr_v.rssi_ext[0] = rs->rs_rssi_ext[0];
491         sc->sc_rx_th.wr_v.rssi_ext[1] = rs->rs_rssi_ext[1];
492         sc->sc_rx_th.wr_v.rssi_ext[2] = rs->rs_rssi_ext[2];
493
494         /* evm */
495         sc->sc_rx_th.wr_v.evm[0] = rs->rs_evm0;
496         sc->sc_rx_th.wr_v.evm[1] = rs->rs_evm1;
497         sc->sc_rx_th.wr_v.evm[2] = rs->rs_evm2;
498         /* These are only populated from the AR9300 or later */
499         sc->sc_rx_th.wr_v.evm[3] = rs->rs_evm3;
500         sc->sc_rx_th.wr_v.evm[4] = rs->rs_evm4;
501
502         /* direction */
503         sc->sc_rx_th.wr_v.vh_flags = ATH_VENDOR_PKT_RX;
504
505         /* RX rate */
506         sc->sc_rx_th.wr_v.vh_rx_hwrate = rs->rs_rate;
507
508         /* RX flags */
509         sc->sc_rx_th.wr_v.vh_rs_flags = rs->rs_flags;
510
511         if (rs->rs_isaggr)
512                 sc->sc_rx_th.wr_v.vh_flags |= ATH_VENDOR_PKT_ISAGGR;
513         if (rs->rs_moreaggr)
514                 sc->sc_rx_th.wr_v.vh_flags |= ATH_VENDOR_PKT_MOREAGGR;
515
516         /* phyerr info */
517         if (rs->rs_status & HAL_RXERR_PHY) {
518                 sc->sc_rx_th.wr_v.vh_phyerr_code = rs->rs_phyerr;
519                 sc->sc_rx_th.wr_v.vh_flags |= ATH_VENDOR_PKT_RXPHYERR;
520         } else {
521                 sc->sc_rx_th.wr_v.vh_phyerr_code = 0xff;
522         }
523         sc->sc_rx_th.wr_v.vh_rs_status = rs->rs_status;
524         sc->sc_rx_th.wr_v.vh_rssi = rs->rs_rssi;
525 }
526 #endif  /* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
527
528 static void
529 ath_rx_tap(struct ifnet *ifp, struct mbuf *m,
530         const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf)
531 {
532 #define CHAN_HT20       htole32(IEEE80211_CHAN_HT20)
533 #define CHAN_HT40U      htole32(IEEE80211_CHAN_HT40U)
534 #define CHAN_HT40D      htole32(IEEE80211_CHAN_HT40D)
535 #define CHAN_HT         (CHAN_HT20|CHAN_HT40U|CHAN_HT40D)
536         struct ath_softc *sc = ifp->if_softc;
537         const HAL_RATE_TABLE *rt;
538         uint8_t rix;
539
540         rt = sc->sc_currates;
541         KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
542         rix = rt->rateCodeToIndex[rs->rs_rate];
543         sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix].ieeerate;
544         sc->sc_rx_th.wr_flags = sc->sc_hwmap[rix].rxflags;
545 #ifdef AH_SUPPORT_AR5416
546         sc->sc_rx_th.wr_chan_flags &= ~CHAN_HT;
547         if (rs->rs_status & HAL_RXERR_PHY) {
548                 /*
549                  * PHY error - make sure the channel flags
550                  * reflect the actual channel configuration,
551                  * not the received frame.
552                  */
553                 if (IEEE80211_IS_CHAN_HT40U(sc->sc_curchan))
554                         sc->sc_rx_th.wr_chan_flags |= CHAN_HT40U;
555                 else if (IEEE80211_IS_CHAN_HT40D(sc->sc_curchan))
556                         sc->sc_rx_th.wr_chan_flags |= CHAN_HT40D;
557                 else if (IEEE80211_IS_CHAN_HT20(sc->sc_curchan))
558                         sc->sc_rx_th.wr_chan_flags |= CHAN_HT20;
559         } else if (sc->sc_rx_th.wr_rate & IEEE80211_RATE_MCS) { /* HT rate */
560                 struct ieee80211com *ic = ifp->if_l2com;
561
562                 if ((rs->rs_flags & HAL_RX_2040) == 0)
563                         sc->sc_rx_th.wr_chan_flags |= CHAN_HT20;
564                 else if (IEEE80211_IS_CHAN_HT40U(ic->ic_curchan))
565                         sc->sc_rx_th.wr_chan_flags |= CHAN_HT40U;
566                 else
567                         sc->sc_rx_th.wr_chan_flags |= CHAN_HT40D;
568                 if ((rs->rs_flags & HAL_RX_GI) == 0)
569                         sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
570         }
571
572 #endif
573         sc->sc_rx_th.wr_tsf = htole64(ath_extend_tsf(sc, rs->rs_tstamp, tsf));
574         if (rs->rs_status & HAL_RXERR_CRC)
575                 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
576         /* XXX propagate other error flags from descriptor */
577         sc->sc_rx_th.wr_antnoise = nf;
578         sc->sc_rx_th.wr_antsignal = nf + rs->rs_rssi;
579         sc->sc_rx_th.wr_antenna = rs->rs_antenna;
580 #undef CHAN_HT
581 #undef CHAN_HT20
582 #undef CHAN_HT40U
583 #undef CHAN_HT40D
584 }
585
586 static void
587 ath_handle_micerror(struct ieee80211com *ic,
588         struct ieee80211_frame *wh, int keyix)
589 {
590         struct ieee80211_node *ni;
591
592         /* XXX recheck MIC to deal w/ chips that lie */
593         /* XXX discard MIC errors on !data frames */
594         ni = ieee80211_find_rxnode(ic, (const struct ieee80211_frame_min *) wh);
595         if (ni != NULL) {
596                 ieee80211_notify_michael_failure(ni->ni_vap, wh, keyix);
597                 ieee80211_free_node(ni);
598         }
599 }
600
601 /*
602  * Process a single packet.
603  *
604  * The mbuf must already be synced, unmapped and removed from bf->bf_m
605  * by this stage.
606  *
607  * The mbuf must be consumed by this routine - either passed up the
608  * net80211 stack, put on the holding queue, or freed.
609  */
610 int
611 ath_rx_pkt(struct ath_softc *sc, struct ath_rx_status *rs, HAL_STATUS status,
612     uint64_t tsf, int nf, HAL_RX_QUEUE qtype, struct ath_buf *bf,
613     struct mbuf *m)
614 {
615         uint64_t rstamp;
616         int len, type;
617         struct ifnet *ifp = sc->sc_ifp;
618         struct ieee80211com *ic = ifp->if_l2com;
619         struct ieee80211_node *ni;
620         int is_good = 0;
621         struct ath_rx_edma *re = &sc->sc_rxedma[qtype];
622
623         /*
624          * Calculate the correct 64 bit TSF given
625          * the TSF64 register value and rs_tstamp.
626          */
627         rstamp = ath_extend_tsf(sc, rs->rs_tstamp, tsf);
628
629         /* These aren't specifically errors */
630 #ifdef  AH_SUPPORT_AR5416
631         if (rs->rs_flags & HAL_RX_GI)
632                 sc->sc_stats.ast_rx_halfgi++;
633         if (rs->rs_flags & HAL_RX_2040)
634                 sc->sc_stats.ast_rx_2040++;
635         if (rs->rs_flags & HAL_RX_DELIM_CRC_PRE)
636                 sc->sc_stats.ast_rx_pre_crc_err++;
637         if (rs->rs_flags & HAL_RX_DELIM_CRC_POST)
638                 sc->sc_stats.ast_rx_post_crc_err++;
639         if (rs->rs_flags & HAL_RX_DECRYPT_BUSY)
640                 sc->sc_stats.ast_rx_decrypt_busy_err++;
641         if (rs->rs_flags & HAL_RX_HI_RX_CHAIN)
642                 sc->sc_stats.ast_rx_hi_rx_chain++;
643         if (rs->rs_flags & HAL_RX_STBC)
644                 sc->sc_stats.ast_rx_stbc++;
645 #endif /* AH_SUPPORT_AR5416 */
646
647         if (rs->rs_status != 0) {
648                 if (rs->rs_status & HAL_RXERR_CRC)
649                         sc->sc_stats.ast_rx_crcerr++;
650                 if (rs->rs_status & HAL_RXERR_FIFO)
651                         sc->sc_stats.ast_rx_fifoerr++;
652                 if (rs->rs_status & HAL_RXERR_PHY) {
653                         sc->sc_stats.ast_rx_phyerr++;
654                         /* Process DFS radar events */
655                         if ((rs->rs_phyerr == HAL_PHYERR_RADAR) ||
656                             (rs->rs_phyerr == HAL_PHYERR_FALSE_RADAR_EXT)) {
657                                 /* Now pass it to the radar processing code */
658                                 ath_dfs_process_phy_err(sc, m, rstamp, rs);
659                         }
660
661                         /* Be suitably paranoid about receiving phy errors out of the stats array bounds */
662                         if (rs->rs_phyerr < 64)
663                                 sc->sc_stats.ast_rx_phy[rs->rs_phyerr]++;
664                         goto rx_error;  /* NB: don't count in ierrors */
665                 }
666                 if (rs->rs_status & HAL_RXERR_DECRYPT) {
667                         /*
668                          * Decrypt error.  If the error occurred
669                          * because there was no hardware key, then
670                          * let the frame through so the upper layers
671                          * can process it.  This is necessary for 5210
672                          * parts which have no way to setup a ``clear''
673                          * key cache entry.
674                          *
675                          * XXX do key cache faulting
676                          */
677                         if (rs->rs_keyix == HAL_RXKEYIX_INVALID)
678                                 goto rx_accept;
679                         sc->sc_stats.ast_rx_badcrypt++;
680                 }
681                 /*
682                  * Similar as above - if the failure was a keymiss
683                  * just punt it up to the upper layers for now.
684                  */
685                 if (rs->rs_status & HAL_RXERR_KEYMISS) {
686                         sc->sc_stats.ast_rx_keymiss++;
687                         goto rx_accept;
688                 }
689                 if (rs->rs_status & HAL_RXERR_MIC) {
690                         sc->sc_stats.ast_rx_badmic++;
691                         /*
692                          * Do minimal work required to hand off
693                          * the 802.11 header for notification.
694                          */
695                         /* XXX frag's and qos frames */
696                         len = rs->rs_datalen;
697                         if (len >= sizeof (struct ieee80211_frame)) {
698                                 ath_handle_micerror(ic,
699                                     mtod(m, struct ieee80211_frame *),
700                                     sc->sc_splitmic ?
701                                         rs->rs_keyix-32 : rs->rs_keyix);
702                         }
703                 }
704                 ifp->if_ierrors++;
705 rx_error:
706                 /*
707                  * Cleanup any pending partial frame.
708                  */
709                 if (re->m_rxpending != NULL) {
710                         m_freem(re->m_rxpending);
711                         re->m_rxpending = NULL;
712                 }
713                 /*
714                  * When a tap is present pass error frames
715                  * that have been requested.  By default we
716                  * pass decrypt+mic errors but others may be
717                  * interesting (e.g. crc).
718                  */
719                 if (ieee80211_radiotap_active(ic) &&
720                     (rs->rs_status & sc->sc_monpass)) {
721                         /* NB: bpf needs the mbuf length setup */
722                         len = rs->rs_datalen;
723                         m->m_pkthdr.len = m->m_len = len;
724                         ath_rx_tap(ifp, m, rs, rstamp, nf);
725 #ifdef  ATH_ENABLE_RADIOTAP_VENDOR_EXT
726                         ath_rx_tap_vendor(ifp, m, rs, rstamp, nf);
727 #endif  /* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
728                         ieee80211_radiotap_rx_all(ic, m);
729                 }
730                 /* XXX pass MIC errors up for s/w reclaculation */
731                 m_freem(m); m = NULL;
732                 goto rx_next;
733         }
734 rx_accept:
735         len = rs->rs_datalen;
736         m->m_len = len;
737
738         if (rs->rs_more) {
739                 /*
740                  * Frame spans multiple descriptors; save
741                  * it for the next completed descriptor, it
742                  * will be used to construct a jumbogram.
743                  */
744                 if (re->m_rxpending != NULL) {
745                         /* NB: max frame size is currently 2 clusters */
746                         sc->sc_stats.ast_rx_toobig++;
747                         m_freem(re->m_rxpending);
748                 }
749                 m->m_pkthdr.rcvif = ifp;
750                 m->m_pkthdr.len = len;
751                 re->m_rxpending = m;
752                 m = NULL;
753                 goto rx_next;
754         } else if (re->m_rxpending != NULL) {
755                 /*
756                  * This is the second part of a jumbogram,
757                  * chain it to the first mbuf, adjust the
758                  * frame length, and clear the rxpending state.
759                  */
760                 re->m_rxpending->m_next = m;
761                 re->m_rxpending->m_pkthdr.len += len;
762                 m = re->m_rxpending;
763                 re->m_rxpending = NULL;
764         } else {
765                 /*
766                  * Normal single-descriptor receive; setup
767                  * the rcvif and packet length.
768                  */
769                 m->m_pkthdr.rcvif = ifp;
770                 m->m_pkthdr.len = len;
771         }
772
773         /*
774          * Validate rs->rs_antenna.
775          *
776          * Some users w/ AR9285 NICs have reported crashes
777          * here because rs_antenna field is bogusly large.
778          * Let's enforce the maximum antenna limit of 8
779          * (and it shouldn't be hard coded, but that's a
780          * separate problem) and if there's an issue, print
781          * out an error and adjust rs_antenna to something
782          * sensible.
783          *
784          * This code should be removed once the actual
785          * root cause of the issue has been identified.
786          * For example, it may be that the rs_antenna
787          * field is only valid for the lsat frame of
788          * an aggregate and it just happens that it is
789          * "mostly" right. (This is a general statement -
790          * the majority of the statistics are only valid
791          * for the last frame in an aggregate.
792          */
793         if (rs->rs_antenna > 7) {
794                 device_printf(sc->sc_dev, "%s: rs_antenna > 7 (%d)\n",
795                     __func__, rs->rs_antenna);
796 #ifdef  ATH_DEBUG
797                 ath_printrxbuf(sc, bf, 0, status == HAL_OK);
798 #endif /* ATH_DEBUG */
799                 rs->rs_antenna = 0;     /* XXX better than nothing */
800         }
801
802         /*
803          * If this is an AR9285/AR9485, then the receive and LNA
804          * configuration is stored in RSSI[2] / EXTRSSI[2].
805          * We can extract this out to build a much better
806          * receive antenna profile.
807          *
808          * Yes, this just blurts over the above RX antenna field
809          * for now.  It's fine, the AR9285 doesn't really use
810          * that.
811          *
812          * Later on we should store away the fine grained LNA
813          * information and keep separate counters just for
814          * that.  It'll help when debugging the AR9285/AR9485
815          * combined diversity code.
816          */
817         if (sc->sc_rx_lnamixer) {
818                 rs->rs_antenna = 0;
819
820                 /* Bits 0:1 - the LNA configuration used */
821                 rs->rs_antenna |=
822                     ((rs->rs_rssi_ctl[2] & HAL_RX_LNA_CFG_USED)
823                       >> HAL_RX_LNA_CFG_USED_S);
824
825                 /* Bit 2 - the external RX antenna switch */
826                 if (rs->rs_rssi_ctl[2] & HAL_RX_LNA_EXTCFG)
827                         rs->rs_antenna |= 0x4;
828         }
829
830         ifp->if_ipackets++;
831         sc->sc_stats.ast_ant_rx[rs->rs_antenna]++;
832
833         /*
834          * Populate the rx status block.  When there are bpf
835          * listeners we do the additional work to provide
836          * complete status.  Otherwise we fill in only the
837          * material required by ieee80211_input.  Note that
838          * noise setting is filled in above.
839          */
840         if (ieee80211_radiotap_active(ic)) {
841                 ath_rx_tap(ifp, m, rs, rstamp, nf);
842 #ifdef  ATH_ENABLE_RADIOTAP_VENDOR_EXT
843                 ath_rx_tap_vendor(ifp, m, rs, rstamp, nf);
844 #endif  /* ATH_ENABLE_RADIOTAP_VENDOR_EXT */
845         }
846
847         /*
848          * From this point on we assume the frame is at least
849          * as large as ieee80211_frame_min; verify that.
850          */
851         if (len < IEEE80211_MIN_LEN) {
852                 if (!ieee80211_radiotap_active(ic)) {
853                         DPRINTF(sc, ATH_DEBUG_RECV,
854                             "%s: short packet %d\n", __func__, len);
855                         sc->sc_stats.ast_rx_tooshort++;
856                 } else {
857                         /* NB: in particular this captures ack's */
858                         ieee80211_radiotap_rx_all(ic, m);
859                 }
860                 m_freem(m); m = NULL;
861                 goto rx_next;
862         }
863
864         if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) {
865                 const HAL_RATE_TABLE *rt = sc->sc_currates;
866                 uint8_t rix = rt->rateCodeToIndex[rs->rs_rate];
867
868                 ieee80211_dump_pkt(ic, mtod(m, caddr_t), len,
869                     sc->sc_hwmap[rix].ieeerate, rs->rs_rssi);
870         }
871
872         m_adj(m, -IEEE80211_CRC_LEN);
873
874         /*
875          * Locate the node for sender, track state, and then
876          * pass the (referenced) node up to the 802.11 layer
877          * for its use.
878          */
879         ni = ieee80211_find_rxnode_withkey(ic,
880                 mtod(m, const struct ieee80211_frame_min *),
881                 rs->rs_keyix == HAL_RXKEYIX_INVALID ?
882                         IEEE80211_KEYIX_NONE : rs->rs_keyix);
883         sc->sc_lastrs = rs;
884
885 #ifdef  AH_SUPPORT_AR5416
886         if (rs->rs_isaggr)
887                 sc->sc_stats.ast_rx_agg++;
888 #endif /* AH_SUPPORT_AR5416 */
889
890         if (ni != NULL) {
891                 /*
892                  * Only punt packets for ampdu reorder processing for
893                  * 11n nodes; net80211 enforces that M_AMPDU is only
894                  * set for 11n nodes.
895                  */
896                 if (ni->ni_flags & IEEE80211_NODE_HT)
897                         m->m_flags |= M_AMPDU;
898
899                 /*
900                  * Sending station is known, dispatch directly.
901                  */
902                 type = ieee80211_input(ni, m, rs->rs_rssi, nf);
903                 ieee80211_free_node(ni);
904                 m = NULL;
905                 /*
906                  * Arrange to update the last rx timestamp only for
907                  * frames from our ap when operating in station mode.
908                  * This assumes the rx key is always setup when
909                  * associated.
910                  */
911                 if (ic->ic_opmode == IEEE80211_M_STA &&
912                     rs->rs_keyix != HAL_RXKEYIX_INVALID)
913                         is_good = 1;
914         } else {
915                 type = ieee80211_input_all(ic, m, rs->rs_rssi, nf);
916                 m = NULL;
917         }
918
919         /*
920          * At this point we have passed the frame up the stack; thus
921          * the mbuf is no longer ours.
922          */
923
924         /*
925          * Track rx rssi and do any rx antenna management.
926          */
927         ATH_RSSI_LPF(sc->sc_halstats.ns_avgrssi, rs->rs_rssi);
928         if (sc->sc_diversity) {
929                 /*
930                  * When using fast diversity, change the default rx
931                  * antenna if diversity chooses the other antenna 3
932                  * times in a row.
933                  */
934                 if (sc->sc_defant != rs->rs_antenna) {
935                         if (++sc->sc_rxotherant >= 3)
936                                 ath_setdefantenna(sc, rs->rs_antenna);
937                 } else
938                         sc->sc_rxotherant = 0;
939         }
940
941         /* Handle slow diversity if enabled */
942         if (sc->sc_dolnadiv) {
943                 ath_lna_rx_comb_scan(sc, rs, ticks, hz);
944         }
945
946         if (sc->sc_softled) {
947                 /*
948                  * Blink for any data frame.  Otherwise do a
949                  * heartbeat-style blink when idle.  The latter
950                  * is mainly for station mode where we depend on
951                  * periodic beacon frames to trigger the poll event.
952                  */
953                 if (type == IEEE80211_FC0_TYPE_DATA) {
954                         const HAL_RATE_TABLE *rt = sc->sc_currates;
955                         ath_led_event(sc,
956                             rt->rateCodeToIndex[rs->rs_rate]);
957                 } else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
958                         ath_led_event(sc, 0);
959                 }
960 rx_next:
961         /*
962          * Debugging - complain if we didn't NULL the mbuf pointer
963          * here.
964          */
965         if (m != NULL) {
966                 device_printf(sc->sc_dev,
967                     "%s: mbuf %p should've been freed!\n",
968                     __func__,
969                     m);
970         }
971         return (is_good);
972 }
973
974 #define ATH_RX_MAX              128
975
976 /*
977  * XXX TODO: break out the "get buffers" from "call ath_rx_pkt()" like
978  * the EDMA code does.
979  *
980  * XXX TODO: then, do all of the RX list management stuff inside
981  * ATH_RX_LOCK() so we don't end up potentially racing.  The EDMA
982  * code is doing it right.
983  */
984 static void
985 ath_rx_proc(struct ath_softc *sc, int resched)
986 {
987 #define PA2DESC(_sc, _pa) \
988         ((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
989                 ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
990         struct ath_buf *bf;
991         struct ifnet *ifp = sc->sc_ifp;
992         struct ath_hal *ah = sc->sc_ah;
993 #ifdef IEEE80211_SUPPORT_SUPERG
994         struct ieee80211com *ic = ifp->if_l2com;
995 #endif
996         struct ath_desc *ds;
997         struct ath_rx_status *rs;
998         struct mbuf *m;
999         int ngood;
1000         HAL_STATUS status;
1001         int16_t nf;
1002         u_int64_t tsf;
1003         int npkts = 0;
1004         int kickpcu = 0;
1005         int ret;
1006
1007         /* XXX we must not hold the ATH_LOCK here */
1008         ATH_UNLOCK_ASSERT(sc);
1009         ATH_PCU_UNLOCK_ASSERT(sc);
1010
1011         ATH_PCU_LOCK(sc);
1012         sc->sc_rxproc_cnt++;
1013         kickpcu = sc->sc_kickpcu;
1014         ATH_PCU_UNLOCK(sc);
1015
1016         ath_power_set_power_state(sc, HAL_PM_AWAKE);
1017
1018         DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: called\n", __func__);
1019         ngood = 0;
1020         nf = ath_hal_getchannoise(ah, sc->sc_curchan);
1021         sc->sc_stats.ast_rx_noise = nf;
1022         tsf = ath_hal_gettsf64(ah);
1023         do {
1024                 /*
1025                  * Don't process too many packets at a time; give the
1026                  * TX thread time to also run - otherwise the TX
1027                  * latency can jump by quite a bit, causing throughput
1028                  * degredation.
1029                  */
1030                 if (!kickpcu && npkts >= ATH_RX_MAX)
1031                         break;
1032
1033                 bf = TAILQ_FIRST(&sc->sc_rxbuf);
1034                 if (sc->sc_rxslink && bf == NULL) {     /* NB: shouldn't happen */
1035                         if_printf(ifp, "%s: no buffer!\n", __func__);
1036                         break;
1037                 } else if (bf == NULL) {
1038                         /*
1039                          * End of List:
1040                          * this can happen for non-self-linked RX chains
1041                          */
1042                         sc->sc_stats.ast_rx_hitqueueend++;
1043                         break;
1044                 }
1045                 m = bf->bf_m;
1046                 if (m == NULL) {                /* NB: shouldn't happen */
1047                         /*
1048                          * If mbuf allocation failed previously there
1049                          * will be no mbuf; try again to re-populate it.
1050                          */
1051                         /* XXX make debug msg */
1052                         if_printf(ifp, "%s: no mbuf!\n", __func__);
1053                         TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
1054                         goto rx_proc_next;
1055                 }
1056                 ds = bf->bf_desc;
1057                 if (ds->ds_link == bf->bf_daddr) {
1058                         /* NB: never process the self-linked entry at the end */
1059                         sc->sc_stats.ast_rx_hitqueueend++;
1060                         break;
1061                 }
1062                 /* XXX sync descriptor memory */
1063                 /*
1064                  * Must provide the virtual address of the current
1065                  * descriptor, the physical address, and the virtual
1066                  * address of the next descriptor in the h/w chain.
1067                  * This allows the HAL to look ahead to see if the
1068                  * hardware is done with a descriptor by checking the
1069                  * done bit in the following descriptor and the address
1070                  * of the current descriptor the DMA engine is working
1071                  * on.  All this is necessary because of our use of
1072                  * a self-linked list to avoid rx overruns.
1073                  */
1074                 rs = &bf->bf_status.ds_rxstat;
1075                 status = ath_hal_rxprocdesc(ah, ds,
1076                                 bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
1077 #ifdef ATH_DEBUG
1078                 if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
1079                         ath_printrxbuf(sc, bf, 0, status == HAL_OK);
1080 #endif
1081
1082 #ifdef  ATH_DEBUG_ALQ
1083                 if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_EDMA_RXSTATUS))
1084                     if_ath_alq_post(&sc->sc_alq, ATH_ALQ_EDMA_RXSTATUS,
1085                     sc->sc_rx_statuslen, (char *) ds);
1086 #endif  /* ATH_DEBUG_ALQ */
1087
1088                 if (status == HAL_EINPROGRESS)
1089                         break;
1090
1091                 TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
1092                 npkts++;
1093
1094                 /*
1095                  * Process a single frame.
1096                  */
1097                 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_POSTREAD);
1098                 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
1099                 bf->bf_m = NULL;
1100                 if (ath_rx_pkt(sc, rs, status, tsf, nf, HAL_RX_QUEUE_HP, bf, m))
1101                         ngood++;
1102 rx_proc_next:
1103                 /*
1104                  * If there's a holding buffer, insert that onto
1105                  * the RX list; the hardware is now definitely not pointing
1106                  * to it now.
1107                  */
1108                 ret = 0;
1109                 if (sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf != NULL) {
1110                         TAILQ_INSERT_TAIL(&sc->sc_rxbuf,
1111                             sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf,
1112                             bf_list);
1113                         ret = ath_rxbuf_init(sc,
1114                             sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf);
1115                 }
1116                 /*
1117                  * Next, throw our buffer into the holding entry.  The hardware
1118                  * may use the descriptor to read the link pointer before
1119                  * DMAing the next descriptor in to write out a packet.
1120                  */
1121                 sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf = bf;
1122         } while (ret == 0);
1123
1124
1125         /* rx signal state monitoring */
1126         ath_hal_rxmonitor(ah, &sc->sc_halstats, sc->sc_curchan);
1127         if (ngood)
1128                 sc->sc_lastrx = tsf;
1129
1130         ATH_KTR(sc, ATH_KTR_RXPROC, 2, "ath_rx_proc: npkts=%d, ngood=%d", npkts, ngood);
1131         /* Queue DFS tasklet if needed */
1132         if (resched && ath_dfs_tasklet_needed(sc, sc->sc_curchan))
1133                 taskqueue_enqueue(sc->sc_tq, &sc->sc_dfstask);
1134
1135         /*
1136          * Now that all the RX frames were handled that
1137          * need to be handled, kick the PCU if there's
1138          * been an RXEOL condition.
1139          */
1140         if (resched && kickpcu) {
1141                 ATH_PCU_LOCK(sc);
1142                 ATH_KTR(sc, ATH_KTR_ERROR, 0, "ath_rx_proc: kickpcu");
1143                 device_printf(sc->sc_dev, "%s: kickpcu; handled %d packets\n",
1144                     __func__, npkts);
1145
1146                 /*
1147                  * Go through the process of fully tearing down
1148                  * the RX buffers and reinitialising them.
1149                  *
1150                  * There's a hardware bug that causes the RX FIFO
1151                  * to get confused under certain conditions and
1152                  * constantly write over the same frame, leading
1153                  * the RX driver code here to get heavily confused.
1154                  */
1155                 /*
1156                  * XXX Has RX DMA stopped enough here to just call
1157                  *     ath_startrecv()?
1158                  * XXX Do we need to use the holding buffer to restart
1159                  *     RX DMA by appending entries to the final
1160                  *     descriptor?  Quite likely.
1161                  */
1162 #if 1
1163                 ath_startrecv(sc);
1164 #else
1165                 /*
1166                  * Disabled for now - it'd be nice to be able to do
1167                  * this in order to limit the amount of CPU time spent
1168                  * reinitialising the RX side (and thus minimise RX
1169                  * drops) however there's a hardware issue that
1170                  * causes things to get too far out of whack.
1171                  */
1172                 /*
1173                  * XXX can we hold the PCU lock here?
1174                  * Are there any net80211 buffer calls involved?
1175                  */
1176                 bf = TAILQ_FIRST(&sc->sc_rxbuf);
1177                 ath_hal_putrxbuf(ah, bf->bf_daddr, HAL_RX_QUEUE_HP);
1178                 ath_hal_rxena(ah);              /* enable recv descriptors */
1179                 ath_mode_init(sc);              /* set filters, etc. */
1180                 ath_hal_startpcurecv(ah);       /* re-enable PCU/DMA engine */
1181 #endif
1182
1183                 ath_hal_intrset(ah, sc->sc_imask);
1184                 sc->sc_kickpcu = 0;
1185                 ATH_PCU_UNLOCK(sc);
1186         }
1187
1188         /* XXX check this inside of IF_LOCK? */
1189         if (resched && !ifq_is_oactive(&ifp->if_snd)) {
1190 #ifdef IEEE80211_SUPPORT_SUPERG
1191                 ieee80211_ff_age_all(ic, 100);
1192 #endif
1193                 if (!ifq_is_empty(&ifp->if_snd))
1194                         ath_tx_kick(sc);
1195         }
1196 #undef PA2DESC
1197
1198         /*
1199          * Put the hardware to sleep again if we're done with it.
1200          */
1201         ath_power_restore_power_state(sc);
1202
1203         /*
1204          * If we hit the maximum number of frames in this round,
1205          * reschedule for another immediate pass.  This gives
1206          * the TX and TX completion routines time to run, which
1207          * will reduce latency.
1208          */
1209         if (npkts >= ATH_RX_MAX)
1210                 sc->sc_rx.recv_sched(sc, resched);
1211
1212         ATH_PCU_LOCK(sc);
1213         sc->sc_rxproc_cnt--;
1214         ATH_PCU_UNLOCK(sc);
1215 }
1216
1217 #undef  ATH_RX_MAX
1218
1219 /*
1220  * Only run the RX proc if it's not already running.
1221  * Since this may get run as part of the reset/flush path,
1222  * the task can't clash with an existing, running tasklet.
1223  */
1224 static void
1225 ath_legacy_rx_tasklet(void *arg, int npending)
1226 {
1227         struct ath_softc *sc = arg;
1228
1229         wlan_serialize_enter();
1230         ATH_KTR(sc, ATH_KTR_RXPROC, 1, "ath_rx_proc: pending=%d", npending);
1231         DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending);
1232         ATH_PCU_LOCK(sc);
1233         if (sc->sc_inreset_cnt > 0) {
1234                 device_printf(sc->sc_dev,
1235                     "%s: sc_inreset_cnt > 0; skipping\n", __func__);
1236                 ATH_PCU_UNLOCK(sc);
1237                 wlan_serialize_exit();
1238                 return;
1239         }
1240         ATH_PCU_UNLOCK(sc);
1241
1242         ath_rx_proc(sc, 1);
1243         wlan_serialize_exit();
1244 }
1245
1246 static void
1247 ath_legacy_flushrecv(struct ath_softc *sc)
1248 {
1249
1250         ath_rx_proc(sc, 0);
1251 }
1252
1253 static void
1254 ath_legacy_flush_rxpending(struct ath_softc *sc)
1255 {
1256
1257        /* XXX ATH_RX_LOCK_ASSERT(sc); */
1258
1259        if (sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending != NULL) {
1260                m_freem(sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending);
1261                sc->sc_rxedma[HAL_RX_QUEUE_LP].m_rxpending = NULL;
1262        }
1263        if (sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending != NULL) {
1264                m_freem(sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending);
1265                sc->sc_rxedma[HAL_RX_QUEUE_HP].m_rxpending = NULL;
1266        }
1267 }
1268
1269 static int
1270 ath_legacy_flush_rxholdbf(struct ath_softc *sc)
1271 {
1272        struct ath_buf *bf;
1273
1274        /* XXX ATH_RX_LOCK_ASSERT(sc); */
1275        /*
1276         * If there are RX holding buffers, free them here and return
1277         * them to the list.
1278         *
1279         * XXX should just verify that bf->bf_m is NULL, as it must
1280         * be at this point!
1281         */
1282        bf = sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf;
1283        if (bf != NULL) {
1284                if (bf->bf_m != NULL)
1285                        m_freem(bf->bf_m);
1286                bf->bf_m = NULL;
1287                TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
1288                (void) ath_rxbuf_init(sc, bf);
1289        }
1290        sc->sc_rxedma[HAL_RX_QUEUE_HP].m_holdbf = NULL;
1291
1292        bf = sc->sc_rxedma[HAL_RX_QUEUE_LP].m_holdbf;
1293        if (bf != NULL) {
1294                if (bf->bf_m != NULL)
1295                        m_freem(bf->bf_m);
1296                bf->bf_m = NULL;
1297                TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
1298                (void) ath_rxbuf_init(sc, bf);
1299        }
1300        sc->sc_rxedma[HAL_RX_QUEUE_LP].m_holdbf = NULL;
1301
1302        return (0);
1303 }
1304
1305 /*
1306  * Disable the receive h/w in preparation for a reset.
1307  */
1308 static void
1309 ath_legacy_stoprecv(struct ath_softc *sc, int dodelay)
1310 {
1311 #define PA2DESC(_sc, _pa) \
1312         ((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
1313                 ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
1314         struct ath_hal *ah = sc->sc_ah;
1315
1316         ath_hal_stoppcurecv(ah);        /* disable PCU */
1317         ath_hal_setrxfilter(ah, 0);     /* clear recv filter */
1318         ath_hal_stopdmarecv(ah);        /* disable DMA engine */
1319         /*
1320          * TODO: see if this particular DELAY() is required; it may be
1321          * masking some missing FIFO flush or DMA sync.
1322          */
1323 #if 0
1324         if (dodelay)
1325 #endif
1326                 DELAY(3000);            /* 3ms is long enough for 1 frame */
1327 #ifdef ATH_DEBUG
1328         if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) {
1329                 struct ath_buf *bf;
1330                 u_int ix;
1331
1332                 device_printf(sc->sc_dev,
1333                     "%s: rx queue %p, link %p\n",
1334                     __func__,
1335                     (caddr_t)(uintptr_t) ath_hal_getrxbuf(ah, HAL_RX_QUEUE_HP),
1336                     sc->sc_rxlink);
1337                 ix = 0;
1338                 TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
1339                         struct ath_desc *ds = bf->bf_desc;
1340                         struct ath_rx_status *rs = &bf->bf_status.ds_rxstat;
1341                         HAL_STATUS status = ath_hal_rxprocdesc(ah, ds,
1342                                 bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
1343                         if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL))
1344                                 ath_printrxbuf(sc, bf, ix, status == HAL_OK);
1345                         ix++;
1346                 }
1347         }
1348 #endif
1349
1350         (void) ath_legacy_flush_rxpending(sc);
1351         (void) ath_legacy_flush_rxholdbf(sc);
1352
1353         sc->sc_rxlink = NULL;           /* just in case */
1354 #undef PA2DESC
1355 }
1356
1357 /*
1358  * Enable the receive h/w following a reset.
1359  */
1360 static int
1361 ath_legacy_startrecv(struct ath_softc *sc)
1362 {
1363         struct ath_hal *ah = sc->sc_ah;
1364         struct ath_buf *bf;
1365
1366         sc->sc_rxlink = NULL;
1367         (void) ath_legacy_flush_rxpending(sc);
1368         (void) ath_legacy_flush_rxholdbf(sc);
1369
1370         /*
1371          * Re-chain all of the buffers in the RX buffer list.
1372          */
1373         TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
1374                 int error = ath_rxbuf_init(sc, bf);
1375                 if (error != 0) {
1376                         DPRINTF(sc, ATH_DEBUG_RECV,
1377                                 "%s: ath_rxbuf_init failed %d\n",
1378                                 __func__, error);
1379                         return error;
1380                 }
1381         }
1382
1383         bf = TAILQ_FIRST(&sc->sc_rxbuf);
1384         ath_hal_putrxbuf(ah, bf->bf_daddr, HAL_RX_QUEUE_HP);
1385         ath_hal_rxena(ah);              /* enable recv descriptors */
1386         ath_mode_init(sc);              /* set filters, etc. */
1387         ath_hal_startpcurecv(ah);       /* re-enable PCU/DMA engine */
1388         return 0;
1389 }
1390
1391 static int
1392 ath_legacy_dma_rxsetup(struct ath_softc *sc)
1393 {
1394         int error;
1395
1396         error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
1397             "rx", sizeof(struct ath_desc), ath_rxbuf, 1);
1398         if (error != 0)
1399                 return (error);
1400
1401         return (0);
1402 }
1403
1404 static int
1405 ath_legacy_dma_rxteardown(struct ath_softc *sc)
1406 {
1407
1408         if (sc->sc_rxdma.dd_desc_len != 0)
1409                 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
1410         return (0);
1411 }
1412
1413 static void
1414 ath_legacy_recv_sched(struct ath_softc *sc, int dosched)
1415 {
1416
1417         taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1418 }
1419
1420 static void
1421 ath_legacy_recv_sched_queue(struct ath_softc *sc, HAL_RX_QUEUE q,
1422     int dosched)
1423 {
1424
1425         taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1426 }
1427
1428 void
1429 ath_recv_setup_legacy(struct ath_softc *sc)
1430 {
1431
1432         /* Sensible legacy defaults */
1433         /*
1434          * XXX this should be changed to properly support the
1435          * exact RX descriptor size for each HAL.
1436          */
1437         sc->sc_rx_statuslen = sizeof(struct ath_desc);
1438
1439         sc->sc_rx.recv_start = ath_legacy_startrecv;
1440         sc->sc_rx.recv_stop = ath_legacy_stoprecv;
1441         sc->sc_rx.recv_flush = ath_legacy_flushrecv;
1442         sc->sc_rx.recv_tasklet = ath_legacy_rx_tasklet;
1443         sc->sc_rx.recv_rxbuf_init = ath_legacy_rxbuf_init;
1444
1445         sc->sc_rx.recv_setup = ath_legacy_dma_rxsetup;
1446         sc->sc_rx.recv_teardown = ath_legacy_dma_rxteardown;
1447         sc->sc_rx.recv_sched = ath_legacy_recv_sched;
1448         sc->sc_rx.recv_sched_queue = ath_legacy_recv_sched_queue;
1449 }