2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
29 * $FreeBSD: head/sys/dev/ath/if_ath.c 203751 2010-02-10 11:12:39Z rpaulo $");
34 * Driver for the Atheros Wireless LAN controller.
36 * This software is derived from work of Atsushi Onoe; his contribution
37 * is greatly appreciated.
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysctl.h>
48 #include <sys/malloc.h>
50 #include <sys/mutex.h>
51 #include <sys/kernel.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/errno.h>
55 #include <sys/callout.h>
57 #include <sys/endian.h>
58 #include <sys/kthread.h>
59 #include <sys/taskqueue.h>
63 #include <net/if_dl.h>
64 #include <net/if_media.h>
65 #include <net/if_types.h>
66 #include <net/if_arp.h>
67 #include <net/ethernet.h>
68 #include <net/if_llc.h>
69 #include <net/ifq_var.h>
71 #include <netproto/802_11/ieee80211_var.h>
72 #include <netproto/802_11/ieee80211_regdomain.h>
73 #ifdef IEEE80211_SUPPORT_SUPERG
74 #include <netproto/802_11/ieee80211_superg.h>
76 #ifdef IEEE80211_SUPPORT_TDMA
77 #include <netproto/802_11/ieee80211_tdma.h>
83 #include <netinet/in.h>
84 #include <netinet/if_ether.h>
87 #include <dev/netif/ath/ath/if_athvar.h>
88 #include <dev/netif/ath/hal/ath_hal/ah_devid.h> /* XXX for softled */
91 #include <dev/netif/ath_tx99/ath_tx99.h>
95 * ATH_BCBUF determines the number of vap's that can transmit
96 * beacons and also (currently) the number of vap's that can
97 * have unique mac addresses/bssid. When staggering beacons
98 * 4 is probably a good max as otherwise the beacons become
99 * very closely spaced and there is limited time for cab q traffic
100 * to go out. You can burst beacons instead but that is not good
101 * for stations in power save and at some point you really want
102 * another radio (and channel).
104 * The limit on the number of mac addresses is tied to our use of
105 * the U/L bit and tracking addresses in a byte; it would be
106 * worthwhile to allow more for applications like proxy sta.
108 CTASSERT(ATH_BCBUF <= 8);
110 /* unaligned little endian access */
111 #define LE_READ_2(p) \
113 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8)))
114 #define LE_READ_4(p) \
116 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8) | \
117 (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24)))
119 static struct ieee80211vap *ath_vap_create(struct ieee80211com *,
120 const char name[IFNAMSIZ], int unit, int opmode,
121 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
122 const uint8_t mac[IEEE80211_ADDR_LEN]);
123 static void ath_vap_delete(struct ieee80211vap *);
124 static void ath_init(void *);
125 static void ath_stop_locked(struct ifnet *);
126 static void ath_stop(struct ifnet *);
127 static void ath_start(struct ifnet *);
128 static int ath_reset(struct ifnet *);
129 static int ath_reset_vap(struct ieee80211vap *, u_long);
130 static int ath_media_change(struct ifnet *);
131 static void ath_watchdog(void *);
132 static int ath_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
133 static void ath_fatal_proc(void *, int);
134 static void ath_bmiss_vap(struct ieee80211vap *);
135 static void ath_bmiss_proc(void *, int);
136 static int ath_keyset(struct ath_softc *, const struct ieee80211_key *,
137 struct ieee80211_node *);
138 static int ath_key_alloc(struct ieee80211vap *,
139 struct ieee80211_key *,
140 ieee80211_keyix *, ieee80211_keyix *);
141 static int ath_key_delete(struct ieee80211vap *,
142 const struct ieee80211_key *);
143 static int ath_key_set(struct ieee80211vap *, const struct ieee80211_key *,
144 const u_int8_t mac[IEEE80211_ADDR_LEN]);
145 static void ath_key_update_begin(struct ieee80211vap *);
146 static void ath_key_update_end(struct ieee80211vap *);
147 static void ath_update_mcast(struct ifnet *);
148 static void ath_update_promisc(struct ifnet *);
149 static void ath_mode_init(struct ath_softc *);
150 static void ath_setslottime(struct ath_softc *);
151 static void ath_updateslot(struct ifnet *);
152 static int ath_beaconq_setup(struct ath_hal *);
153 static int ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *);
154 static void ath_beacon_update(struct ieee80211vap *, int item);
155 static void ath_beacon_setup(struct ath_softc *, struct ath_buf *);
156 static void ath_beacon_proc(void *, int);
157 static struct ath_buf *ath_beacon_generate(struct ath_softc *,
158 struct ieee80211vap *);
159 static void ath_bstuck_proc(void *, int);
160 static void ath_beacon_return(struct ath_softc *, struct ath_buf *);
161 static void ath_beacon_free(struct ath_softc *);
162 static void ath_beacon_config(struct ath_softc *, struct ieee80211vap *);
163 static void ath_descdma_cleanup(struct ath_softc *sc,
164 struct ath_descdma *, ath_bufhead *);
165 static int ath_desc_alloc(struct ath_softc *);
166 static void ath_desc_free(struct ath_softc *);
167 static struct ieee80211_node *ath_node_alloc(struct ieee80211vap *,
168 const uint8_t [IEEE80211_ADDR_LEN]);
169 static void ath_node_free(struct ieee80211_node *);
170 static void ath_node_getsignal(const struct ieee80211_node *,
172 static int ath_rxbuf_init(struct ath_softc *, struct ath_buf *);
173 static void ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
174 int subtype, int rssi, int nf);
175 static void ath_setdefantenna(struct ath_softc *, u_int);
176 static void ath_rx_proc(void *, int);
177 static void ath_txq_init(struct ath_softc *sc, struct ath_txq *, int);
178 static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
179 static int ath_tx_setup(struct ath_softc *, int, int);
180 static int ath_wme_update(struct ieee80211com *);
181 static void ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
182 static void ath_tx_cleanup(struct ath_softc *);
183 static void ath_freetx(struct mbuf *);
184 static int ath_tx_start(struct ath_softc *, struct ieee80211_node *,
185 struct ath_buf *, struct mbuf *);
186 static void ath_tx_proc_q0(void *, int);
187 static void ath_tx_proc_q0123(void *, int);
188 static void ath_tx_proc(void *, int);
189 static void ath_tx_draintxq(struct ath_softc *, struct ath_txq *);
190 static int ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
191 static void ath_draintxq(struct ath_softc *);
192 static void ath_stoprecv(struct ath_softc *);
193 static int ath_startrecv(struct ath_softc *);
194 static void ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
195 static void ath_scan_start(struct ieee80211com *);
196 static void ath_scan_end(struct ieee80211com *);
197 static void ath_set_channel(struct ieee80211com *);
198 static void ath_calibrate(void *);
199 static int ath_newstate(struct ieee80211vap *, enum ieee80211_state, int);
200 static void ath_setup_stationkey(struct ieee80211_node *);
201 static void ath_newassoc(struct ieee80211_node *, int);
202 static int ath_setregdomain(struct ieee80211com *,
203 struct ieee80211_regdomain *, int,
204 struct ieee80211_channel []);
205 static void ath_getradiocaps(struct ieee80211com *, int, int *,
206 struct ieee80211_channel []);
207 static int ath_getchannels(struct ath_softc *);
208 static void ath_led_event(struct ath_softc *, int);
210 static int ath_rate_setup(struct ath_softc *, u_int mode);
211 static void ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
213 static void ath_sysctlattach(struct ath_softc *);
214 static int ath_raw_xmit(struct ieee80211_node *,
215 struct mbuf *, const struct ieee80211_bpf_params *);
216 static void ath_announce(struct ath_softc *);
218 #ifdef IEEE80211_SUPPORT_TDMA
219 static void ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt,
221 static void ath_tdma_bintvalsetup(struct ath_softc *sc,
222 const struct ieee80211_tdma_state *tdma);
223 static void ath_tdma_config(struct ath_softc *sc, struct ieee80211vap *vap);
224 static void ath_tdma_update(struct ieee80211_node *ni,
225 const struct ieee80211_tdma_param *tdma, int);
226 static void ath_tdma_beacon_send(struct ath_softc *sc,
227 struct ieee80211vap *vap);
230 ath_hal_setcca(struct ath_hal *ah, int ena)
233 * NB: fill me in; this is not provided by default because disabling
234 * CCA in most locales violates regulatory.
239 ath_hal_getcca(struct ath_hal *ah)
242 if (ath_hal_getcapability(ah, HAL_CAP_DIAG, 0, &diag) != HAL_OK)
244 return ((diag & 0x500000) == 0);
247 #define TDMA_EP_MULTIPLIER (1<<10) /* pow2 to optimize out * and / */
248 #define TDMA_LPF_LEN 6
249 #define TDMA_DUMMY_MARKER 0x127
250 #define TDMA_EP_MUL(x, mul) ((x) * (mul))
251 #define TDMA_IN(x) (TDMA_EP_MUL((x), TDMA_EP_MULTIPLIER))
252 #define TDMA_LPF(x, y, len) \
253 ((x != TDMA_DUMMY_MARKER) ? (((x) * ((len)-1) + (y)) / (len)) : (y))
254 #define TDMA_SAMPLE(x, y) do { \
255 x = TDMA_LPF((x), TDMA_IN(y), TDMA_LPF_LEN); \
257 #define TDMA_EP_RND(x,mul) \
258 ((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))
259 #define TDMA_AVG(x) TDMA_EP_RND(x, TDMA_EP_MULTIPLIER)
260 #endif /* IEEE80211_SUPPORT_TDMA */
262 SYSCTL_DECL(_hw_ath);
264 /* XXX validate sysctl values */
265 static int ath_longcalinterval = 30; /* long cals every 30 secs */
266 SYSCTL_INT(_hw_ath, OID_AUTO, longcal, CTLFLAG_RW, &ath_longcalinterval,
267 0, "long chip calibration interval (secs)");
268 static int ath_shortcalinterval = 100; /* short cals every 100 ms */
269 SYSCTL_INT(_hw_ath, OID_AUTO, shortcal, CTLFLAG_RW, &ath_shortcalinterval,
270 0, "short chip calibration interval (msecs)");
271 static int ath_resetcalinterval = 20*60; /* reset cal state 20 mins */
272 SYSCTL_INT(_hw_ath, OID_AUTO, resetcal, CTLFLAG_RW, &ath_resetcalinterval,
273 0, "reset chip calibration results (secs)");
275 static int ath_rxbuf = ATH_RXBUF; /* # rx buffers to allocate */
276 SYSCTL_INT(_hw_ath, OID_AUTO, rxbuf, CTLFLAG_RW, &ath_rxbuf,
277 0, "rx buffers allocated");
278 TUNABLE_INT("hw.ath.rxbuf", &ath_rxbuf);
279 static int ath_txbuf = ATH_TXBUF; /* # tx buffers to allocate */
280 SYSCTL_INT(_hw_ath, OID_AUTO, txbuf, CTLFLAG_RW, &ath_txbuf,
281 0, "tx buffers allocated");
282 TUNABLE_INT("hw.ath.txbuf", &ath_txbuf);
284 static int ath_bstuck_threshold = 4; /* max missed beacons */
285 SYSCTL_INT(_hw_ath, OID_AUTO, bstuck, CTLFLAG_RW, &ath_bstuck_threshold,
286 0, "max missed beacon xmits before chip reset");
290 ATH_DEBUG_XMIT = 0x00000001, /* basic xmit operation */
291 ATH_DEBUG_XMIT_DESC = 0x00000002, /* xmit descriptors */
292 ATH_DEBUG_RECV = 0x00000004, /* basic recv operation */
293 ATH_DEBUG_RECV_DESC = 0x00000008, /* recv descriptors */
294 ATH_DEBUG_RATE = 0x00000010, /* rate control */
295 ATH_DEBUG_RESET = 0x00000020, /* reset processing */
296 ATH_DEBUG_MODE = 0x00000040, /* mode init/setup */
297 ATH_DEBUG_BEACON = 0x00000080, /* beacon handling */
298 ATH_DEBUG_WATCHDOG = 0x00000100, /* watchdog timeout */
299 ATH_DEBUG_INTR = 0x00001000, /* ISR */
300 ATH_DEBUG_TX_PROC = 0x00002000, /* tx ISR proc */
301 ATH_DEBUG_RX_PROC = 0x00004000, /* rx ISR proc */
302 ATH_DEBUG_BEACON_PROC = 0x00008000, /* beacon ISR proc */
303 ATH_DEBUG_CALIBRATE = 0x00010000, /* periodic calibration */
304 ATH_DEBUG_KEYCACHE = 0x00020000, /* key cache management */
305 ATH_DEBUG_STATE = 0x00040000, /* 802.11 state transitions */
306 ATH_DEBUG_NODE = 0x00080000, /* node management */
307 ATH_DEBUG_LED = 0x00100000, /* led management */
308 ATH_DEBUG_FF = 0x00200000, /* fast frames */
309 ATH_DEBUG_DFS = 0x00400000, /* DFS processing */
310 ATH_DEBUG_TDMA = 0x00800000, /* TDMA processing */
311 ATH_DEBUG_TDMA_TIMER = 0x01000000, /* TDMA timer processing */
312 ATH_DEBUG_REGDOMAIN = 0x02000000, /* regulatory processing */
313 ATH_DEBUG_FATAL = 0x80000000, /* fatal errors */
314 ATH_DEBUG_ANY = 0xffffffff
316 static int ath_debug = 0;
317 SYSCTL_INT(_hw_ath, OID_AUTO, debug, CTLFLAG_RW, &ath_debug,
318 0, "control debugging printfs");
319 TUNABLE_INT("hw.ath.debug", &ath_debug);
321 #define IFF_DUMPPKTS(sc, m) \
322 ((sc->sc_debug & (m)) || \
323 (sc->sc_ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
324 #define DPRINTF(sc, m, fmt, ...) do { \
325 if (sc->sc_debug & (m)) \
326 kprintf(fmt, __VA_ARGS__); \
328 #define ether_sprintf(x) "<dummy>"
329 #define KEYPRINTF(sc, ix, hk, mac) do { \
330 if (sc->sc_debug & ATH_DEBUG_KEYCACHE) \
331 ath_keyprint(sc, __func__, ix, hk, mac); \
333 static void ath_printrxbuf(struct ath_softc *, const struct ath_buf *bf,
335 static void ath_printtxbuf(struct ath_softc *, const struct ath_buf *bf,
336 u_int qnum, u_int ix, int done);
338 #define IFF_DUMPPKTS(sc, m) \
339 ((sc->sc_ifp->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
340 #define DPRINTF(sc, m, fmt, ...) do { \
343 #define KEYPRINTF(sc, k, ix, mac) do { \
348 MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
351 ath_attach(u_int16_t devid, struct ath_softc *sc)
354 struct ieee80211com *ic;
355 struct ath_hal *ah = NULL;
359 uint8_t macaddr[IEEE80211_ADDR_LEN];
361 DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
363 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211);
365 device_printf(sc->sc_dev, "can not if_alloc()\n");
371 /* set these up early for if_printf use */
372 if_initname(ifp, device_get_name(sc->sc_dev),
373 device_get_unit(sc->sc_dev));
375 /* prepare sysctl tree for use in sub modules */
376 sysctl_ctx_init(&sc->sc_sysctl_ctx);
377 sc->sc_sysctl_tree = SYSCTL_ADD_NODE(&sc->sc_sysctl_ctx,
378 SYSCTL_STATIC_CHILDREN(_hw),
380 device_get_nameunit(sc->sc_dev),
383 ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, &status);
385 if_printf(ifp, "unable to attach hardware; HAL status %u\n",
391 sc->sc_invalid = 0; /* ready to go, enable interrupt handling */
393 sc->sc_debug = ath_debug;
397 * Check if the MAC has multi-rate retry support.
398 * We do this by trying to setup a fake extended
399 * descriptor. MAC's that don't have support will
400 * return false w/o doing anything. MAC's that do
401 * support it will return true w/o doing anything.
403 sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
406 * Check if the device has hardware counters for PHY
407 * errors. If so we need to enable the MIB interrupt
408 * so we can act on stat triggers.
410 if (ath_hal_hwphycounters(ah))
414 * Get the hardware key cache size.
416 sc->sc_keymax = ath_hal_keycachesize(ah);
417 if (sc->sc_keymax > ATH_KEYMAX) {
418 if_printf(ifp, "Warning, using only %u of %u key cache slots\n",
419 ATH_KEYMAX, sc->sc_keymax);
420 sc->sc_keymax = ATH_KEYMAX;
423 * Reset the key cache since some parts do not
424 * reset the contents on initial power up.
426 for (i = 0; i < sc->sc_keymax; i++)
427 ath_hal_keyreset(ah, i);
430 * Collect the default channel list.
432 error = ath_getchannels(sc);
437 * Setup rate tables for all potential media types.
439 ath_rate_setup(sc, IEEE80211_MODE_11A);
440 ath_rate_setup(sc, IEEE80211_MODE_11B);
441 ath_rate_setup(sc, IEEE80211_MODE_11G);
442 ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
443 ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
444 ath_rate_setup(sc, IEEE80211_MODE_STURBO_A);
445 ath_rate_setup(sc, IEEE80211_MODE_11NA);
446 ath_rate_setup(sc, IEEE80211_MODE_11NG);
447 ath_rate_setup(sc, IEEE80211_MODE_HALF);
448 ath_rate_setup(sc, IEEE80211_MODE_QUARTER);
450 /* NB: setup here so ath_rate_update is happy */
451 ath_setcurmode(sc, IEEE80211_MODE_11A);
454 * Allocate tx+rx descriptors and populate the lists.
456 error = ath_desc_alloc(sc);
458 if_printf(ifp, "failed to allocate descriptors: %d\n", error);
461 callout_init(&sc->sc_cal_ch);
462 callout_init(&sc->sc_wd_ch);
464 ATH_TXBUF_LOCK_INIT(sc);
466 sc->sc_tq = taskqueue_create("ath_taskq", M_INTWAIT,
467 taskqueue_thread_enqueue, &sc->sc_tq);
468 taskqueue_start_threads(&sc->sc_tq, 1, TDPRI_KERN_DAEMON, -1,
469 "%s taskq", ifp->if_xname);
471 TASK_INIT(&sc->sc_rxtask, 0, ath_rx_proc, sc);
472 TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
473 TASK_INIT(&sc->sc_bstucktask,0, ath_bstuck_proc, sc);
476 * Allocate hardware transmit queues: one queue for
477 * beacon frames and one data queue for each QoS
478 * priority. Note that the hal handles reseting
479 * these queues at the needed time.
483 sc->sc_bhalq = ath_beaconq_setup(ah);
484 if (sc->sc_bhalq == (u_int) -1) {
485 if_printf(ifp, "unable to setup a beacon xmit queue!\n");
489 sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
490 if (sc->sc_cabq == NULL) {
491 if_printf(ifp, "unable to setup CAB xmit queue!\n");
495 /* NB: insure BK queue is the lowest priority h/w queue */
496 if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
497 if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
498 ieee80211_wme_acnames[WME_AC_BK]);
502 if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
503 !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
504 !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
506 * Not enough hardware tx queues to properly do WME;
507 * just punt and assign them all to the same h/w queue.
508 * We could do a better job of this if, for example,
509 * we allocate queues when we switch from station to
512 if (sc->sc_ac2q[WME_AC_VI] != NULL)
513 ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
514 if (sc->sc_ac2q[WME_AC_BE] != NULL)
515 ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
516 sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
517 sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
518 sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
522 * Special case certain configurations. Note the
523 * CAB queue is handled by these specially so don't
524 * include them when checking the txq setup mask.
526 switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
528 TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
531 TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
534 TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
539 * Setup rate control. Some rate control modules
540 * call back to change the anntena state so expose
541 * the necessary entry points.
542 * XXX maybe belongs in struct ath_ratectrl?
544 sc->sc_setdefantenna = ath_setdefantenna;
545 sc->sc_rc = ath_rate_attach(sc);
546 if (sc->sc_rc == NULL) {
553 sc->sc_ledon = 0; /* low true */
554 sc->sc_ledidle = (2700*hz)/1000; /* 2.7sec */
555 callout_init_mp(&sc->sc_ledtimer);
557 * Auto-enable soft led processing for IBM cards and for
558 * 5211 minipci cards. Users can also manually enable/disable
559 * support with a sysctl.
561 sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
562 if (sc->sc_softled) {
563 ath_hal_gpioCfgOutput(ah, sc->sc_ledpin,
564 HAL_GPIO_MUX_MAC_NETWORK_LED);
565 ath_hal_gpioset(ah, sc->sc_ledpin, !sc->sc_ledon);
569 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
570 ifp->if_start = ath_start;
571 ifp->if_ioctl = ath_ioctl;
572 ifp->if_init = ath_init;
573 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
574 ifq_set_ready(&ifp->if_snd);
577 /* XXX not right but it's not used anywhere important */
578 ic->ic_phytype = IEEE80211_T_OFDM;
579 ic->ic_opmode = IEEE80211_M_STA;
581 IEEE80211_C_STA /* station mode */
582 | IEEE80211_C_IBSS /* ibss, nee adhoc, mode */
583 | IEEE80211_C_HOSTAP /* hostap mode */
584 | IEEE80211_C_MONITOR /* monitor mode */
585 | IEEE80211_C_AHDEMO /* adhoc demo mode */
586 | IEEE80211_C_WDS /* 4-address traffic works */
587 | IEEE80211_C_MBSS /* mesh point link mode */
588 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
589 | IEEE80211_C_SHSLOT /* short slot time supported */
590 | IEEE80211_C_WPA /* capable of WPA1+WPA2 */
591 | IEEE80211_C_BGSCAN /* capable of bg scanning */
592 | IEEE80211_C_TXFRAG /* handle tx frags */
595 * Query the hal to figure out h/w crypto support.
597 if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
598 ic->ic_cryptocaps |= IEEE80211_CRYPTO_WEP;
599 if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
600 ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_OCB;
601 if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
602 ic->ic_cryptocaps |= IEEE80211_CRYPTO_AES_CCM;
603 if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
604 ic->ic_cryptocaps |= IEEE80211_CRYPTO_CKIP;
605 if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
606 ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIP;
608 * Check if h/w does the MIC and/or whether the
609 * separate key cache entries are required to
610 * handle both tx+rx MIC keys.
612 if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
613 ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
615 * If the h/w supports storing tx+rx MIC keys
616 * in one cache slot automatically enable use.
618 if (ath_hal_hastkipsplit(ah) ||
619 !ath_hal_settkipsplit(ah, AH_FALSE))
622 * If the h/w can do TKIP MIC together with WME then
623 * we use it; otherwise we force the MIC to be done
624 * in software by the net80211 layer.
626 if (ath_hal_haswmetkipmic(ah))
627 sc->sc_wmetkipmic = 1;
629 sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
631 * Check for multicast key search support.
633 if (ath_hal_hasmcastkeysearch(sc->sc_ah) &&
634 !ath_hal_getmcastkeysearch(sc->sc_ah)) {
635 ath_hal_setmcastkeysearch(sc->sc_ah, 1);
637 sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
639 * Mark key cache slots associated with global keys
640 * as in use. If we knew TKIP was not to be used we
641 * could leave the +32, +64, and +32+64 slots free.
643 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
644 setbit(sc->sc_keymap, i);
645 setbit(sc->sc_keymap, i+64);
646 if (sc->sc_splitmic) {
647 setbit(sc->sc_keymap, i+32);
648 setbit(sc->sc_keymap, i+32+64);
652 * TPC support can be done either with a global cap or
653 * per-packet support. The latter is not available on
654 * all parts. We're a bit pedantic here as all parts
655 * support a global cap.
657 if (ath_hal_hastpc(ah) || ath_hal_hastxpowlimit(ah))
658 ic->ic_caps |= IEEE80211_C_TXPMGT;
661 * Mark WME capability only if we have sufficient
662 * hardware queues to do proper priority scheduling.
664 if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
665 ic->ic_caps |= IEEE80211_C_WME;
667 * Check for misc other capabilities.
669 if (ath_hal_hasbursting(ah))
670 ic->ic_caps |= IEEE80211_C_BURST;
671 sc->sc_hasbmask = ath_hal_hasbssidmask(ah);
672 sc->sc_hasbmatch = ath_hal_hasbssidmatch(ah);
673 sc->sc_hastsfadd = ath_hal_hastsfadjust(ah);
674 if (ath_hal_hasfastframes(ah))
675 ic->ic_caps |= IEEE80211_C_FF;
676 wmodes = ath_hal_getwirelessmodes(ah);
677 if (wmodes & (HAL_MODE_108G|HAL_MODE_TURBO))
678 ic->ic_caps |= IEEE80211_C_TURBOP;
679 #ifdef IEEE80211_SUPPORT_TDMA
680 if (ath_hal_macversion(ah) > 0x78) {
681 ic->ic_caps |= IEEE80211_C_TDMA; /* capable of TDMA */
682 ic->ic_tdma_update = ath_tdma_update;
686 * Indicate we need the 802.11 header padded to a
687 * 32-bit boundary for 4-address and QoS frames.
689 ic->ic_flags |= IEEE80211_F_DATAPAD;
692 * Query the hal about antenna support.
694 sc->sc_defant = ath_hal_getdefantenna(ah);
697 * Not all chips have the VEOL support we want to
698 * use with IBSS beacons; check here for it.
700 sc->sc_hasveol = ath_hal_hasveol(ah);
702 /* get mac address from hardware */
703 ath_hal_getmac(ah, macaddr);
705 ath_hal_getbssidmask(ah, sc->sc_hwbssidmask);
707 /* NB: used to size node table key mapping array */
708 ic->ic_max_keyix = sc->sc_keymax;
709 /* call MI attach routine. */
710 ieee80211_ifattach(ic, macaddr);
711 ic->ic_setregdomain = ath_setregdomain;
712 ic->ic_getradiocaps = ath_getradiocaps;
713 sc->sc_opmode = HAL_M_STA;
715 /* override default methods */
716 ic->ic_newassoc = ath_newassoc;
717 ic->ic_updateslot = ath_updateslot;
718 ic->ic_wme.wme_update = ath_wme_update;
719 ic->ic_vap_create = ath_vap_create;
720 ic->ic_vap_delete = ath_vap_delete;
721 ic->ic_raw_xmit = ath_raw_xmit;
722 ic->ic_update_mcast = ath_update_mcast;
723 ic->ic_update_promisc = ath_update_promisc;
724 ic->ic_node_alloc = ath_node_alloc;
725 sc->sc_node_free = ic->ic_node_free;
726 ic->ic_node_free = ath_node_free;
727 ic->ic_node_getsignal = ath_node_getsignal;
728 ic->ic_scan_start = ath_scan_start;
729 ic->ic_scan_end = ath_scan_end;
730 ic->ic_set_channel = ath_set_channel;
732 ieee80211_radiotap_attach(ic,
733 &sc->sc_tx_th.wt_ihdr, sizeof(sc->sc_tx_th),
734 ATH_TX_RADIOTAP_PRESENT,
735 &sc->sc_rx_th.wr_ihdr, sizeof(sc->sc_rx_th),
736 ATH_RX_RADIOTAP_PRESENT);
739 * Setup dynamic sysctl's now that country code and
740 * regdomain are available from the hal.
742 ath_sysctlattach(sc);
745 ieee80211_announce(ic);
761 ath_detach(struct ath_softc *sc)
763 struct ifnet *ifp = sc->sc_ifp;
765 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
766 __func__, ifp->if_flags);
769 * NB: the order of these is important:
770 * o stop the chip so no more interrupts will fire
771 * o call the 802.11 layer before detaching the hal to
772 * insure callbacks into the driver to delete global
773 * key cache entries can be handled
774 * o free the taskqueue which drains any pending tasks
775 * o reclaim the tx queue data structures after calling
776 * the 802.11 layer as we'll get called back to reclaim
777 * node state and potentially want to use them
778 * o to cleanup the tx queues the hal is called, so detach
780 * Other than that, it's straightforward...
783 ieee80211_ifdetach(ifp->if_l2com);
784 taskqueue_free(sc->sc_tq);
786 if (sc->sc_tx99 != NULL)
787 sc->sc_tx99->detach(sc->sc_tx99);
789 ath_rate_detach(sc->sc_rc);
792 ath_hal_detach(sc->sc_ah); /* NB: sets chip in full sleep */
793 if (sc->sc_sysctl_tree) {
794 sysctl_ctx_free(&sc->sc_sysctl_ctx);
795 sc->sc_sysctl_tree = NULL;
803 * MAC address handling for multiple BSS on the same radio.
804 * The first vap uses the MAC address from the EEPROM. For
805 * subsequent vap's we set the U/L bit (bit 1) in the MAC
806 * address and use the next six bits as an index.
809 assign_address(struct ath_softc *sc, uint8_t mac[IEEE80211_ADDR_LEN], int clone)
813 if (clone && sc->sc_hasbmask) {
814 /* NB: we only do this if h/w supports multiple bssid */
815 for (i = 0; i < 8; i++)
816 if ((sc->sc_bssidmask & (1<<i)) == 0)
819 mac[0] |= (i << 2)|0x2;
822 sc->sc_bssidmask |= 1<<i;
823 sc->sc_hwbssidmask[0] &= ~mac[0];
829 reclaim_address(struct ath_softc *sc, const uint8_t mac[IEEE80211_ADDR_LEN])
834 if (i != 0 || --sc->sc_nbssid0 == 0) {
835 sc->sc_bssidmask &= ~(1<<i);
836 /* recalculate bssid mask from remaining addresses */
838 for (i = 1; i < 8; i++)
839 if (sc->sc_bssidmask & (1<<i))
840 mask &= ~((i<<2)|0x2);
841 sc->sc_hwbssidmask[0] |= mask;
846 * Assign a beacon xmit slot. We try to space out
847 * assignments so when beacons are staggered the
848 * traffic coming out of the cab q has maximal time
849 * to go out before the next beacon is scheduled.
852 assign_bslot(struct ath_softc *sc)
857 for (slot = 0; slot < ATH_BCBUF; slot++)
858 if (sc->sc_bslot[slot] == NULL) {
859 if (sc->sc_bslot[(slot+1)%ATH_BCBUF] == NULL &&
860 sc->sc_bslot[(slot-1)%ATH_BCBUF] == NULL)
863 /* NB: keep looking for a double slot */
868 static struct ieee80211vap *
869 ath_vap_create(struct ieee80211com *ic,
870 const char name[IFNAMSIZ], int unit, int opmode, int flags,
871 const uint8_t bssid[IEEE80211_ADDR_LEN],
872 const uint8_t mac0[IEEE80211_ADDR_LEN])
874 struct ath_softc *sc = ic->ic_ifp->if_softc;
876 struct ieee80211vap *vap;
877 uint8_t mac[IEEE80211_ADDR_LEN];
878 int ic_opmode, needbeacon, error;
880 avp = (struct ath_vap *) kmalloc(sizeof(struct ath_vap),
881 M_80211_VAP, M_WAITOK | M_ZERO);
883 IEEE80211_ADDR_COPY(mac, mac0);
886 ic_opmode = opmode; /* default to opmode of new vap */
888 case IEEE80211_M_STA:
889 if (sc->sc_nstavaps != 0) { /* XXX only 1 for now */
890 device_printf(sc->sc_dev, "only 1 sta vap supported\n");
895 * With multiple vaps we must fall back
896 * to s/w beacon miss handling.
898 flags |= IEEE80211_CLONE_NOBEACONS;
900 if (flags & IEEE80211_CLONE_NOBEACONS) {
902 * Station mode w/o beacons are implemented w/ AP mode.
904 ic_opmode = IEEE80211_M_HOSTAP;
907 case IEEE80211_M_IBSS:
908 if (sc->sc_nvaps != 0) { /* XXX only 1 for now */
909 device_printf(sc->sc_dev,
910 "only 1 ibss vap supported\n");
915 case IEEE80211_M_AHDEMO:
916 #ifdef IEEE80211_SUPPORT_TDMA
917 if (flags & IEEE80211_CLONE_TDMA) {
918 if (sc->sc_nvaps != 0) {
919 device_printf(sc->sc_dev,
920 "only 1 tdma vap supported\n");
924 flags |= IEEE80211_CLONE_NOBEACONS;
928 case IEEE80211_M_MONITOR:
929 if (sc->sc_nvaps != 0 && ic->ic_opmode != opmode) {
931 * Adopt existing mode. Adding a monitor or ahdemo
932 * vap to an existing configuration is of dubious
933 * value but should be ok.
935 /* XXX not right for monitor mode */
936 ic_opmode = ic->ic_opmode;
939 case IEEE80211_M_HOSTAP:
940 case IEEE80211_M_MBSS:
943 case IEEE80211_M_WDS:
944 if (sc->sc_nvaps != 0 && ic->ic_opmode == IEEE80211_M_STA) {
945 device_printf(sc->sc_dev,
946 "wds not supported in sta mode\n");
950 * Silently remove any request for a unique
951 * bssid; WDS vap's always share the local
954 flags &= ~IEEE80211_CLONE_BSSID;
955 if (sc->sc_nvaps == 0)
956 ic_opmode = IEEE80211_M_HOSTAP;
958 ic_opmode = ic->ic_opmode;
961 device_printf(sc->sc_dev, "unknown opmode %d\n", opmode);
965 * Check that a beacon buffer is available; the code below assumes it.
967 if (needbeacon & STAILQ_EMPTY(&sc->sc_bbuf)) {
968 device_printf(sc->sc_dev, "no beacon buffer available\n");
973 if (opmode == IEEE80211_M_HOSTAP || opmode == IEEE80211_M_MBSS) {
974 assign_address(sc, mac, flags & IEEE80211_CLONE_BSSID);
975 ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
979 /* XXX can't hold mutex across if_alloc */
981 error = ieee80211_vap_setup(ic, vap, name, unit, opmode, flags,
985 device_printf(sc->sc_dev, "%s: error %d creating vap\n",
990 /* h/w crypto support */
991 vap->iv_key_alloc = ath_key_alloc;
992 vap->iv_key_delete = ath_key_delete;
993 vap->iv_key_set = ath_key_set;
994 vap->iv_key_update_begin = ath_key_update_begin;
995 vap->iv_key_update_end = ath_key_update_end;
997 /* override various methods */
998 avp->av_recv_mgmt = vap->iv_recv_mgmt;
999 vap->iv_recv_mgmt = ath_recv_mgmt;
1000 vap->iv_reset = ath_reset_vap;
1001 vap->iv_update_beacon = ath_beacon_update;
1002 avp->av_newstate = vap->iv_newstate;
1003 vap->iv_newstate = ath_newstate;
1004 avp->av_bmiss = vap->iv_bmiss;
1005 vap->iv_bmiss = ath_bmiss_vap;
1010 * Allocate beacon state and setup the q for buffered
1011 * multicast frames. We know a beacon buffer is
1012 * available because we checked above.
1014 avp->av_bcbuf = STAILQ_FIRST(&sc->sc_bbuf);
1015 STAILQ_REMOVE_HEAD(&sc->sc_bbuf, bf_list);
1016 if (opmode != IEEE80211_M_IBSS || !sc->sc_hasveol) {
1018 * Assign the vap to a beacon xmit slot. As above
1019 * this cannot fail to find a free one.
1021 avp->av_bslot = assign_bslot(sc);
1022 KASSERT(sc->sc_bslot[avp->av_bslot] == NULL,
1023 ("beacon slot %u not empty", avp->av_bslot));
1024 sc->sc_bslot[avp->av_bslot] = vap;
1027 if (sc->sc_hastsfadd && sc->sc_nbcnvaps > 0) {
1029 * Multple vaps are to transmit beacons and we
1030 * have h/w support for TSF adjusting; enable
1031 * use of staggered beacons.
1033 sc->sc_stagbeacons = 1;
1035 ath_txq_init(sc, &avp->av_mcastq, ATH_TXQ_SWQ);
1038 ic->ic_opmode = ic_opmode;
1039 if (opmode != IEEE80211_M_WDS) {
1041 if (opmode == IEEE80211_M_STA)
1043 if (opmode == IEEE80211_M_MBSS)
1046 switch (ic_opmode) {
1047 case IEEE80211_M_IBSS:
1048 sc->sc_opmode = HAL_M_IBSS;
1050 case IEEE80211_M_STA:
1051 sc->sc_opmode = HAL_M_STA;
1053 case IEEE80211_M_AHDEMO:
1054 #ifdef IEEE80211_SUPPORT_TDMA
1055 if (vap->iv_caps & IEEE80211_C_TDMA) {
1057 /* NB: disable tsf adjust */
1058 sc->sc_stagbeacons = 0;
1061 * NB: adhoc demo mode is a pseudo mode; to the hal it's
1066 case IEEE80211_M_HOSTAP:
1067 case IEEE80211_M_MBSS:
1068 sc->sc_opmode = HAL_M_HOSTAP;
1070 case IEEE80211_M_MONITOR:
1071 sc->sc_opmode = HAL_M_MONITOR;
1074 /* XXX should not happen */
1077 if (sc->sc_hastsfadd) {
1079 * Configure whether or not TSF adjust should be done.
1081 ath_hal_settsfadjust(sc->sc_ah, sc->sc_stagbeacons);
1083 if (flags & IEEE80211_CLONE_NOBEACONS) {
1085 * Enable s/w beacon miss handling.
1091 /* complete setup */
1092 ieee80211_vap_attach(vap, ath_media_change, ieee80211_media_status);
1095 reclaim_address(sc, mac);
1096 ath_hal_setbssidmask(sc->sc_ah, sc->sc_hwbssidmask);
1098 kfree(avp, M_80211_VAP);
1104 ath_vap_delete(struct ieee80211vap *vap)
1106 struct ieee80211com *ic = vap->iv_ic;
1107 struct ifnet *ifp = ic->ic_ifp;
1108 struct ath_softc *sc = ifp->if_softc;
1109 struct ath_hal *ah = sc->sc_ah;
1110 struct ath_vap *avp = ATH_VAP(vap);
1112 if (ifp->if_flags & IFF_RUNNING) {
1114 * Quiesce the hardware while we remove the vap. In
1115 * particular we need to reclaim all references to
1116 * the vap state by any frames pending on the tx queues.
1118 ath_hal_intrset(ah, 0); /* disable interrupts */
1119 ath_draintxq(sc); /* stop xmit side */
1120 ath_stoprecv(sc); /* stop recv side */
1123 ieee80211_vap_detach(vap);
1126 * Reclaim beacon state. Note this must be done before
1127 * the vap instance is reclaimed as we may have a reference
1128 * to it in the buffer for the beacon frame.
1130 if (avp->av_bcbuf != NULL) {
1131 if (avp->av_bslot != -1) {
1132 sc->sc_bslot[avp->av_bslot] = NULL;
1135 ath_beacon_return(sc, avp->av_bcbuf);
1136 avp->av_bcbuf = NULL;
1137 if (sc->sc_nbcnvaps == 0) {
1138 sc->sc_stagbeacons = 0;
1139 if (sc->sc_hastsfadd)
1140 ath_hal_settsfadjust(sc->sc_ah, 0);
1143 * Reclaim any pending mcast frames for the vap.
1145 ath_tx_draintxq(sc, &avp->av_mcastq);
1146 ATH_TXQ_LOCK_DESTROY(&avp->av_mcastq);
1149 * Update bookkeeping.
1151 if (vap->iv_opmode == IEEE80211_M_STA) {
1153 if (sc->sc_nstavaps == 0 && sc->sc_swbmiss)
1155 } else if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
1156 vap->iv_opmode == IEEE80211_M_MBSS) {
1157 reclaim_address(sc, vap->iv_myaddr);
1158 ath_hal_setbssidmask(ah, sc->sc_hwbssidmask);
1159 if (vap->iv_opmode == IEEE80211_M_MBSS)
1162 if (vap->iv_opmode != IEEE80211_M_WDS)
1164 #ifdef IEEE80211_SUPPORT_TDMA
1165 /* TDMA operation ceases when the last vap is destroyed */
1166 if (sc->sc_tdma && sc->sc_nvaps == 0) {
1172 kfree(avp, M_80211_VAP);
1174 if (ifp->if_flags & IFF_RUNNING) {
1176 * Restart rx+tx machines if still running (RUNNING will
1177 * be reset if we just destroyed the last vap).
1179 if (ath_startrecv(sc) != 0)
1180 if_printf(ifp, "%s: unable to restart recv logic\n",
1182 if (sc->sc_beacons) { /* restart beacons */
1183 #ifdef IEEE80211_SUPPORT_TDMA
1185 ath_tdma_config(sc, NULL);
1188 ath_beacon_config(sc, NULL);
1190 ath_hal_intrset(ah, sc->sc_imask);
1195 ath_suspend(struct ath_softc *sc)
1197 struct ifnet *ifp = sc->sc_ifp;
1198 struct ieee80211com *ic = ifp->if_l2com;
1200 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1201 __func__, ifp->if_flags);
1203 sc->sc_resume_up = (ifp->if_flags & IFF_UP) != 0;
1204 if (ic->ic_opmode == IEEE80211_M_STA)
1207 ieee80211_suspend_all(ic);
1209 * NB: don't worry about putting the chip in low power
1210 * mode; pci will power off our socket on suspend and
1211 * CardBus detaches the device.
1216 * Reset the key cache since some parts do not reset the
1217 * contents on resume. First we clear all entries, then
1218 * re-load keys that the 802.11 layer assumes are setup
1222 ath_reset_keycache(struct ath_softc *sc)
1224 struct ifnet *ifp = sc->sc_ifp;
1225 struct ieee80211com *ic = ifp->if_l2com;
1226 struct ath_hal *ah = sc->sc_ah;
1229 for (i = 0; i < sc->sc_keymax; i++)
1230 ath_hal_keyreset(ah, i);
1231 ieee80211_crypto_reload_keys(ic);
1235 ath_resume(struct ath_softc *sc)
1237 struct ifnet *ifp = sc->sc_ifp;
1238 struct ieee80211com *ic = ifp->if_l2com;
1239 struct ath_hal *ah = sc->sc_ah;
1242 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1243 __func__, ifp->if_flags);
1246 * Must reset the chip before we reload the
1247 * keycache as we were powered down on suspend.
1249 ath_hal_reset(ah, sc->sc_opmode,
1250 sc->sc_curchan != NULL ? sc->sc_curchan : ic->ic_curchan,
1252 ath_reset_keycache(sc);
1253 if (sc->sc_resume_up) {
1254 if (ic->ic_opmode == IEEE80211_M_STA) {
1257 * Program the beacon registers using the last rx'd
1258 * beacon frame and enable sync on the next beacon
1259 * we see. This should handle the case where we
1260 * wakeup and find the same AP and also the case where
1261 * we wakeup and need to roam. For the latter we
1262 * should get bmiss events that trigger a roam.
1264 ath_beacon_config(sc, NULL);
1265 sc->sc_syncbeacon = 1;
1267 ieee80211_resume_all(ic);
1269 if (sc->sc_softled) {
1270 ath_hal_gpioCfgOutput(ah, sc->sc_ledpin,
1271 HAL_GPIO_MUX_MAC_NETWORK_LED);
1272 ath_hal_gpioset(ah, sc->sc_ledpin, !sc->sc_ledon);
1277 ath_shutdown(struct ath_softc *sc)
1279 struct ifnet *ifp = sc->sc_ifp;
1281 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
1282 __func__, ifp->if_flags);
1285 /* NB: no point powering down chip as we're about to reboot */
1289 * Interrupt handler. Most of the actual processing is deferred.
1294 struct ath_softc *sc = arg;
1295 struct ifnet *ifp = sc->sc_ifp;
1296 struct ath_hal *ah = sc->sc_ah;
1299 if (sc->sc_invalid) {
1301 * The hardware is not ready/present, don't touch anything.
1302 * Note this can happen early on if the IRQ is shared.
1304 DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
1307 if (!ath_hal_intrpend(ah)) /* shared irq, not for us */
1309 if ((ifp->if_flags & IFF_UP) == 0 ||
1310 (ifp->if_flags & IFF_RUNNING) == 0) {
1313 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1314 __func__, ifp->if_flags);
1315 ath_hal_getisr(ah, &status); /* clear ISR */
1316 ath_hal_intrset(ah, 0); /* disable further intr's */
1320 * Figure out the reason(s) for the interrupt. Note
1321 * that the hal returns a pseudo-ISR that may include
1322 * bits we haven't explicitly enabled so we mask the
1323 * value to insure we only process bits we requested.
1325 ath_hal_getisr(ah, &status); /* NB: clears ISR too */
1326 DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
1327 status &= sc->sc_imask; /* discard unasked for bits */
1328 if (status & HAL_INT_FATAL) {
1329 sc->sc_stats.ast_hardware++;
1330 ath_hal_intrset(ah, 0); /* disable intr's until reset */
1331 ath_fatal_proc(sc, 0);
1333 if (status & HAL_INT_SWBA) {
1335 * Software beacon alert--time to send a beacon.
1336 * Handle beacon transmission directly; deferring
1337 * this is too slow to meet timing constraints
1340 #ifdef IEEE80211_SUPPORT_TDMA
1342 if (sc->sc_tdmaswba == 0) {
1343 struct ieee80211com *ic = ifp->if_l2com;
1344 struct ieee80211vap *vap =
1345 TAILQ_FIRST(&ic->ic_vaps);
1346 ath_tdma_beacon_send(sc, vap);
1348 vap->iv_tdma->tdma_bintval;
1354 ath_beacon_proc(sc, 0);
1355 #ifdef IEEE80211_SUPPORT_SUPERG
1357 * Schedule the rx taskq in case there's no
1358 * traffic so any frames held on the staging
1359 * queue are aged and potentially flushed.
1361 taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1365 if (status & HAL_INT_RXEOL) {
1367 * NB: the hardware should re-read the link when
1368 * RXE bit is written, but it doesn't work at
1369 * least on older hardware revs.
1371 sc->sc_stats.ast_rxeol++;
1372 sc->sc_rxlink = NULL;
1374 if (status & HAL_INT_TXURN) {
1375 sc->sc_stats.ast_txurn++;
1376 /* bump tx trigger level */
1377 ath_hal_updatetxtriglevel(ah, AH_TRUE);
1379 if (status & HAL_INT_RX)
1380 taskqueue_enqueue(sc->sc_tq, &sc->sc_rxtask);
1381 if (status & HAL_INT_TX)
1382 taskqueue_enqueue(sc->sc_tq, &sc->sc_txtask);
1383 if (status & HAL_INT_BMISS) {
1384 sc->sc_stats.ast_bmiss++;
1385 taskqueue_enqueue(sc->sc_tq, &sc->sc_bmisstask);
1387 if (status & HAL_INT_MIB) {
1388 sc->sc_stats.ast_mib++;
1390 * Disable interrupts until we service the MIB
1391 * interrupt; otherwise it will continue to fire.
1393 ath_hal_intrset(ah, 0);
1395 * Let the hal handle the event. We assume it will
1396 * clear whatever condition caused the interrupt.
1398 ath_hal_mibevent(ah, &sc->sc_halstats);
1399 ath_hal_intrset(ah, sc->sc_imask);
1401 if (status & HAL_INT_RXORN) {
1402 /* NB: hal marks HAL_INT_FATAL when RXORN is fatal */
1403 sc->sc_stats.ast_rxorn++;
1409 ath_fatal_proc(void *arg, int pending)
1411 struct ath_softc *sc = arg;
1412 struct ifnet *ifp = sc->sc_ifp;
1417 if_printf(ifp, "hardware error; resetting\n");
1419 * Fatal errors are unrecoverable. Typically these
1420 * are caused by DMA errors. Collect h/w state from
1421 * the hal so we can diagnose what's going on.
1423 if (ath_hal_getfatalstate(sc->sc_ah, &sp, &len)) {
1424 KASSERT(len >= 6*sizeof(u_int32_t), ("len %u bytes", len));
1426 if_printf(ifp, "0x%08x 0x%08x 0x%08x, 0x%08x 0x%08x 0x%08x\n",
1427 state[0], state[1] , state[2], state[3],
1428 state[4], state[5]);
1434 ath_bmiss_vap(struct ieee80211vap *vap)
1437 * Workaround phantom bmiss interrupts by sanity-checking
1438 * the time of our last rx'd frame. If it is within the
1439 * beacon miss interval then ignore the interrupt. If it's
1440 * truly a bmiss we'll get another interrupt soon and that'll
1441 * be dispatched up for processing. Note this applies only
1442 * for h/w beacon miss events.
1444 if ((vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) == 0) {
1445 struct ifnet *ifp = vap->iv_ic->ic_ifp;
1446 struct ath_softc *sc = ifp->if_softc;
1447 u_int64_t lastrx = sc->sc_lastrx;
1448 u_int64_t tsf = ath_hal_gettsf64(sc->sc_ah);
1449 u_int bmisstimeout =
1450 vap->iv_bmissthreshold * vap->iv_bss->ni_intval * 1024;
1452 DPRINTF(sc, ATH_DEBUG_BEACON,
1453 "%s: tsf %llu lastrx %lld (%llu) bmiss %u\n",
1454 __func__, (unsigned long long) tsf,
1455 (unsigned long long)(tsf - lastrx),
1456 (unsigned long long) lastrx, bmisstimeout);
1458 if (tsf - lastrx <= bmisstimeout) {
1459 sc->sc_stats.ast_bmiss_phantom++;
1463 ATH_VAP(vap)->av_bmiss(vap);
1467 ath_hal_gethangstate(struct ath_hal *ah, uint32_t mask, uint32_t *hangs)
1472 if (!ath_hal_getdiagstate(ah, 32, &mask, sizeof(mask), &sp, &rsize))
1474 KASSERT(rsize == sizeof(uint32_t), ("resultsize %u", rsize));
1475 *hangs = *(uint32_t *)sp;
1480 ath_bmiss_proc(void *arg, int pending)
1482 struct ath_softc *sc = arg;
1483 struct ifnet *ifp = sc->sc_ifp;
1486 DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
1488 if (ath_hal_gethangstate(sc->sc_ah, 0xff, &hangs) && hangs != 0) {
1489 if_printf(ifp, "bb hang detected (0x%x), reseting\n", hangs);
1492 ieee80211_beacon_miss(ifp->if_l2com);
1496 * Handle TKIP MIC setup to deal hardware that doesn't do MIC
1497 * calcs together with WME. If necessary disable the crypto
1498 * hardware and mark the 802.11 state so keys will be setup
1499 * with the MIC work done in software.
1502 ath_settkipmic(struct ath_softc *sc)
1504 struct ifnet *ifp = sc->sc_ifp;
1505 struct ieee80211com *ic = ifp->if_l2com;
1507 if ((ic->ic_cryptocaps & IEEE80211_CRYPTO_TKIP) && !sc->sc_wmetkipmic) {
1508 if (ic->ic_flags & IEEE80211_F_WME) {
1509 ath_hal_settkipmic(sc->sc_ah, AH_FALSE);
1510 ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC;
1512 ath_hal_settkipmic(sc->sc_ah, AH_TRUE);
1513 ic->ic_cryptocaps |= IEEE80211_CRYPTO_TKIPMIC;
1521 struct ath_softc *sc = (struct ath_softc *) arg;
1522 struct ifnet *ifp = sc->sc_ifp;
1523 struct ieee80211com *ic = ifp->if_l2com;
1524 struct ath_hal *ah = sc->sc_ah;
1527 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
1528 __func__, ifp->if_flags);
1532 * Stop anything previously setup. This is safe
1533 * whether this is the first time through or not.
1535 ath_stop_locked(ifp);
1538 * The basic interface to setting the hardware in a good
1539 * state is ``reset''. On return the hardware is known to
1540 * be powered up and with interrupts disabled. This must
1541 * be followed by initialization of the appropriate bits
1542 * and then setup of the interrupt mask.
1545 if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_FALSE, &status)) {
1546 if_printf(ifp, "unable to reset hardware; hal status %u\n",
1551 ath_chan_change(sc, ic->ic_curchan);
1554 * Likewise this is set during reset so update
1555 * state cached in the driver.
1557 sc->sc_diversity = ath_hal_getdiversity(ah);
1558 sc->sc_lastlongcal = 0;
1559 sc->sc_resetcal = 1;
1560 sc->sc_lastcalreset = 0;
1563 * Setup the hardware after reset: the key cache
1564 * is filled as needed and the receive engine is
1565 * set going. Frame transmit is handled entirely
1566 * in the frame output path; there's nothing to do
1567 * here except setup the interrupt mask.
1569 if (ath_startrecv(sc) != 0) {
1570 if_printf(ifp, "unable to start recv logic\n");
1576 * Enable interrupts.
1578 sc->sc_imask = HAL_INT_RX | HAL_INT_TX
1579 | HAL_INT_RXEOL | HAL_INT_RXORN
1580 | HAL_INT_FATAL | HAL_INT_GLOBAL;
1582 * Enable MIB interrupts when there are hardware phy counters.
1583 * Note we only do this (at the moment) for station mode.
1585 if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
1586 sc->sc_imask |= HAL_INT_MIB;
1588 ifp->if_flags |= IFF_RUNNING;
1589 callout_reset(&sc->sc_wd_ch, hz, ath_watchdog, sc);
1590 ath_hal_intrset(ah, sc->sc_imask);
1594 #ifdef ATH_TX99_DIAG
1595 if (sc->sc_tx99 != NULL)
1596 sc->sc_tx99->start(sc->sc_tx99);
1599 ieee80211_start_all(ic); /* start all vap's */
1603 ath_stop_locked(struct ifnet *ifp)
1605 struct ath_softc *sc = ifp->if_softc;
1606 struct ath_hal *ah = sc->sc_ah;
1608 DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
1609 __func__, sc->sc_invalid, ifp->if_flags);
1611 ATH_LOCK_ASSERT(sc);
1612 if (ifp->if_flags & IFF_RUNNING) {
1614 * Shutdown the hardware and driver:
1615 * reset 802.11 state machine
1617 * disable interrupts
1618 * turn off the radio
1619 * clear transmit machinery
1620 * clear receive machinery
1621 * drain and release tx queues
1622 * reclaim beacon resources
1623 * power down hardware
1625 * Note that some of this work is not possible if the
1626 * hardware is gone (invalid).
1628 #ifdef ATH_TX99_DIAG
1629 if (sc->sc_tx99 != NULL)
1630 sc->sc_tx99->stop(sc->sc_tx99);
1632 callout_stop(&sc->sc_wd_ch);
1633 sc->sc_wd_timer = 0;
1634 ifp->if_flags &= ~IFF_RUNNING;
1635 if (!sc->sc_invalid) {
1636 if (sc->sc_softled) {
1637 callout_stop(&sc->sc_ledtimer);
1638 ath_hal_gpioset(ah, sc->sc_ledpin,
1640 sc->sc_blinking = 0;
1642 ath_hal_intrset(ah, 0);
1645 if (!sc->sc_invalid) {
1647 ath_hal_phydisable(ah);
1649 sc->sc_rxlink = NULL;
1650 ath_beacon_free(sc); /* XXX not needed */
1655 ath_stop(struct ifnet *ifp)
1657 struct ath_softc *sc = ifp->if_softc;
1660 ath_stop_locked(ifp);
1665 * Reset the hardware w/o losing operational state. This is
1666 * basically a more efficient way of doing ath_stop, ath_init,
1667 * followed by state transitions to the current 802.11
1668 * operational state. Used to recover from various errors and
1669 * to reset or reload hardware state.
1672 ath_reset(struct ifnet *ifp)
1674 struct ath_softc *sc = ifp->if_softc;
1675 struct ieee80211com *ic = ifp->if_l2com;
1676 struct ath_hal *ah = sc->sc_ah;
1679 ath_hal_intrset(ah, 0); /* disable interrupts */
1680 ath_draintxq(sc); /* stop xmit side */
1681 ath_stoprecv(sc); /* stop recv side */
1682 ath_settkipmic(sc); /* configure TKIP MIC handling */
1683 /* NB: indicate channel change so we do a full reset */
1684 if (!ath_hal_reset(ah, sc->sc_opmode, ic->ic_curchan, AH_TRUE, &status))
1685 if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
1687 sc->sc_diversity = ath_hal_getdiversity(ah);
1688 if (ath_startrecv(sc) != 0) /* restart recv */
1689 if_printf(ifp, "%s: unable to start recv logic\n", __func__);
1691 * We may be doing a reset in response to an ioctl
1692 * that changes the channel so update any state that
1693 * might change as a result.
1695 ath_chan_change(sc, ic->ic_curchan);
1696 if (sc->sc_beacons) { /* restart beacons */
1697 #ifdef IEEE80211_SUPPORT_TDMA
1699 ath_tdma_config(sc, NULL);
1702 ath_beacon_config(sc, NULL);
1704 ath_hal_intrset(ah, sc->sc_imask);
1706 ath_start(ifp); /* restart xmit */
1711 ath_reset_vap(struct ieee80211vap *vap, u_long cmd)
1713 struct ieee80211com *ic = vap->iv_ic;
1714 struct ifnet *ifp = ic->ic_ifp;
1715 struct ath_softc *sc = ifp->if_softc;
1716 struct ath_hal *ah = sc->sc_ah;
1719 case IEEE80211_IOC_TXPOWER:
1721 * If per-packet TPC is enabled, then we have nothing
1722 * to do; otherwise we need to force the global limit.
1723 * All this can happen directly; no need to reset.
1725 if (!ath_hal_gettpc(ah))
1726 ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
1729 return ath_reset(ifp);
1732 static struct ath_buf *
1733 _ath_getbuf_locked(struct ath_softc *sc)
1737 ATH_TXBUF_LOCK_ASSERT(sc);
1739 bf = STAILQ_FIRST(&sc->sc_txbuf);
1740 if (bf != NULL && (bf->bf_flags & ATH_BUF_BUSY) == 0)
1741 STAILQ_REMOVE_HEAD(&sc->sc_txbuf, bf_list);
1745 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: %s\n", __func__,
1746 STAILQ_FIRST(&sc->sc_txbuf) == NULL ?
1747 "out of xmit buffers" : "xmit buffer busy");
1752 static struct ath_buf *
1753 ath_getbuf(struct ath_softc *sc)
1758 bf = _ath_getbuf_locked(sc);
1760 struct ifnet *ifp = sc->sc_ifp;
1762 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: stop queue\n", __func__);
1763 sc->sc_stats.ast_tx_qstop++;
1764 ifp->if_flags |= IFF_OACTIVE;
1766 ATH_TXBUF_UNLOCK(sc);
1771 * Cleanup driver resources when we run out of buffers
1772 * while processing fragments; return the tx buffers
1773 * allocated and drop node references.
1776 ath_txfrag_cleanup(struct ath_softc *sc,
1777 ath_bufhead *frags, struct ieee80211_node *ni)
1779 struct ath_buf *bf, *next;
1781 ATH_TXBUF_LOCK_ASSERT(sc);
1783 STAILQ_FOREACH_MUTABLE(bf, frags, bf_list, next) {
1784 /* NB: bf assumed clean */
1785 STAILQ_REMOVE_HEAD(frags, bf_list);
1786 STAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
1787 ieee80211_node_decref(ni);
1792 * Setup xmit of a fragmented frame. Allocate a buffer
1793 * for each frag and bump the node reference count to
1794 * reflect the held reference to be setup by ath_tx_start.
1797 ath_txfrag_setup(struct ath_softc *sc, ath_bufhead *frags,
1798 struct mbuf *m0, struct ieee80211_node *ni)
1804 for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) {
1805 bf = _ath_getbuf_locked(sc);
1806 if (bf == NULL) { /* out of buffers, cleanup */
1807 ath_txfrag_cleanup(sc, frags, ni);
1810 ieee80211_node_incref(ni);
1811 STAILQ_INSERT_TAIL(frags, bf, bf_list);
1813 ATH_TXBUF_UNLOCK(sc);
1815 return !STAILQ_EMPTY(frags);
1819 ath_start(struct ifnet *ifp)
1821 struct ath_softc *sc = ifp->if_softc;
1822 struct ieee80211_node *ni;
1824 struct mbuf *m, *next;
1827 if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid) {
1828 ifq_purge(&ifp->if_snd);
1833 * Grab a TX buffer and associated resources.
1835 bf = ath_getbuf(sc);
1839 IF_DEQUEUE(&ifp->if_snd, m);
1842 STAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
1843 ATH_TXBUF_UNLOCK(sc);
1846 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1848 * Check for fragmentation. If this frame
1849 * has been broken up verify we have enough
1850 * buffers to send all the fragments so all
1853 STAILQ_INIT(&frags);
1854 if ((m->m_flags & M_FRAG) &&
1855 !ath_txfrag_setup(sc, &frags, m, ni)) {
1856 DPRINTF(sc, ATH_DEBUG_XMIT,
1857 "%s: out of txfrag buffers\n", __func__);
1858 sc->sc_stats.ast_tx_nofrag++;
1866 * Pass the frame to the h/w for transmission.
1867 * Fragmented frames have each frag chained together
1868 * with m_nextpkt. We know there are sufficient ath_buf's
1869 * to send all the frags because of work done by
1870 * ath_txfrag_setup. We leave m_nextpkt set while
1871 * calling ath_tx_start so it can use it to extend the
1872 * the tx duration to cover the subsequent frag and
1873 * so it can reclaim all the mbufs in case of an error;
1874 * ath_tx_start clears m_nextpkt once it commits to
1875 * handing the frame to the hardware.
1877 next = m->m_nextpkt;
1878 if (ath_tx_start(sc, ni, bf, m)) {
1885 STAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
1886 ath_txfrag_cleanup(sc, &frags, ni);
1887 ATH_TXBUF_UNLOCK(sc);
1889 ieee80211_free_node(ni);
1894 * Beware of state changing between frags.
1895 * XXX check sta power-save state?
1897 if (ni->ni_vap->iv_state != IEEE80211_S_RUN) {
1898 DPRINTF(sc, ATH_DEBUG_XMIT,
1899 "%s: flush fragmented packet, state %s\n",
1901 ieee80211_state_name[ni->ni_vap->iv_state]);
1906 bf = STAILQ_FIRST(&frags);
1907 KASSERT(bf != NULL, ("no buf for txfrag"));
1908 STAILQ_REMOVE_HEAD(&frags, bf_list);
1912 sc->sc_wd_timer = 5;
1917 ath_media_change(struct ifnet *ifp)
1919 int error = ieee80211_media_change(ifp);
1920 /* NB: only the fixed rate can change and that doesn't need a reset */
1921 return (error == ENETRESET ? 0 : error);
1926 ath_keyprint(struct ath_softc *sc, const char *tag, u_int ix,
1927 const HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
1929 static const char *ciphers[] = {
1939 kprintf("%s: [%02u] %-7s ", tag, ix, ciphers[hk->kv_type]);
1940 for (i = 0, n = hk->kv_len; i < n; i++)
1941 kprintf("%02x", hk->kv_val[i]);
1942 kprintf(" mac %s", ether_sprintf(mac));
1943 if (hk->kv_type == HAL_CIPHER_TKIP) {
1944 kprintf(" %s ", sc->sc_splitmic ? "mic" : "rxmic");
1945 for (i = 0; i < sizeof(hk->kv_mic); i++)
1946 kprintf("%02x", hk->kv_mic[i]);
1947 if (!sc->sc_splitmic) {
1949 for (i = 0; i < sizeof(hk->kv_txmic); i++)
1950 kprintf("%02x", hk->kv_txmic[i]);
1958 * Set a TKIP key into the hardware. This handles the
1959 * potential distribution of key state to multiple key
1960 * cache slots for TKIP.
1963 ath_keyset_tkip(struct ath_softc *sc, const struct ieee80211_key *k,
1964 HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
1966 #define IEEE80211_KEY_XR (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV)
1967 static const u_int8_t zerobssid[IEEE80211_ADDR_LEN];
1968 struct ath_hal *ah = sc->sc_ah;
1970 KASSERT(k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP,
1971 ("got a non-TKIP key, cipher %u", k->wk_cipher->ic_cipher));
1972 if ((k->wk_flags & IEEE80211_KEY_XR) == IEEE80211_KEY_XR) {
1973 if (sc->sc_splitmic) {
1975 * TX key goes at first index, RX key at the rx index.
1976 * The hal handles the MIC keys at index+64.
1978 memcpy(hk->kv_mic, k->wk_txmic, sizeof(hk->kv_mic));
1979 KEYPRINTF(sc, k->wk_keyix, hk, zerobssid);
1980 if (!ath_hal_keyset(ah, k->wk_keyix, hk, zerobssid))
1983 memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
1984 KEYPRINTF(sc, k->wk_keyix+32, hk, mac);
1985 /* XXX delete tx key on failure? */
1986 return ath_hal_keyset(ah, k->wk_keyix+32, hk, mac);
1989 * Room for both TX+RX MIC keys in one key cache
1990 * slot, just set key at the first index; the hal
1991 * will handle the rest.
1993 memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
1994 memcpy(hk->kv_txmic, k->wk_txmic, sizeof(hk->kv_txmic));
1995 KEYPRINTF(sc, k->wk_keyix, hk, mac);
1996 return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
1998 } else if (k->wk_flags & IEEE80211_KEY_XMIT) {
1999 if (sc->sc_splitmic) {
2001 * NB: must pass MIC key in expected location when
2002 * the keycache only holds one MIC key per entry.
2004 memcpy(hk->kv_mic, k->wk_txmic, sizeof(hk->kv_txmic));
2006 memcpy(hk->kv_txmic, k->wk_txmic, sizeof(hk->kv_txmic));
2007 KEYPRINTF(sc, k->wk_keyix, hk, mac);
2008 return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
2009 } else if (k->wk_flags & IEEE80211_KEY_RECV) {
2010 memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
2011 KEYPRINTF(sc, k->wk_keyix, hk, mac);
2012 return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
2015 #undef IEEE80211_KEY_XR
2019 * Set a net80211 key into the hardware. This handles the
2020 * potential distribution of key state to multiple key
2021 * cache slots for TKIP with hardware MIC support.
2024 ath_keyset(struct ath_softc *sc, const struct ieee80211_key *k,
2025 struct ieee80211_node *bss)
2027 #define N(a) (sizeof(a)/sizeof(a[0]))
2028 static const u_int8_t ciphermap[] = {
2029 HAL_CIPHER_WEP, /* IEEE80211_CIPHER_WEP */
2030 HAL_CIPHER_TKIP, /* IEEE80211_CIPHER_TKIP */
2031 HAL_CIPHER_AES_OCB, /* IEEE80211_CIPHER_AES_OCB */
2032 HAL_CIPHER_AES_CCM, /* IEEE80211_CIPHER_AES_CCM */
2033 (u_int8_t) -1, /* 4 is not allocated */
2034 HAL_CIPHER_CKIP, /* IEEE80211_CIPHER_CKIP */
2035 HAL_CIPHER_CLR, /* IEEE80211_CIPHER_NONE */
2037 struct ath_hal *ah = sc->sc_ah;
2038 const struct ieee80211_cipher *cip = k->wk_cipher;
2039 u_int8_t gmac[IEEE80211_ADDR_LEN];
2040 const u_int8_t *mac;
2043 memset(&hk, 0, sizeof(hk));
2045 * Software crypto uses a "clear key" so non-crypto
2046 * state kept in the key cache are maintained and
2047 * so that rx frames have an entry to match.
2049 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
2050 KASSERT(cip->ic_cipher < N(ciphermap),
2051 ("invalid cipher type %u", cip->ic_cipher));
2052 hk.kv_type = ciphermap[cip->ic_cipher];
2053 hk.kv_len = k->wk_keylen;
2054 memcpy(hk.kv_val, k->wk_key, k->wk_keylen);
2056 hk.kv_type = HAL_CIPHER_CLR;
2058 if ((k->wk_flags & IEEE80211_KEY_GROUP) && sc->sc_mcastkey) {
2060 * Group keys on hardware that supports multicast frame
2061 * key search use a MAC that is the sender's address with
2062 * the high bit set instead of the app-specified address.
2064 IEEE80211_ADDR_COPY(gmac, bss->ni_macaddr);
2068 mac = k->wk_macaddr;
2070 if (hk.kv_type == HAL_CIPHER_TKIP &&
2071 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) {
2072 return ath_keyset_tkip(sc, k, &hk, mac);
2074 KEYPRINTF(sc, k->wk_keyix, &hk, mac);
2075 return ath_hal_keyset(ah, k->wk_keyix, &hk, mac);
2081 * Allocate tx/rx key slots for TKIP. We allocate two slots for
2082 * each key, one for decrypt/encrypt and the other for the MIC.
2085 key_alloc_2pair(struct ath_softc *sc,
2086 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
2088 #define N(a) (sizeof(a)/sizeof(a[0]))
2091 KASSERT(sc->sc_splitmic, ("key cache !split"));
2092 /* XXX could optimize */
2093 for (i = 0; i < N(sc->sc_keymap)/4; i++) {
2094 u_int8_t b = sc->sc_keymap[i];
2097 * One or more slots in this byte are free.
2105 /* XXX IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV */
2106 if (isset(sc->sc_keymap, keyix+32) ||
2107 isset(sc->sc_keymap, keyix+64) ||
2108 isset(sc->sc_keymap, keyix+32+64)) {
2109 /* full pair unavailable */
2111 if (keyix == (i+1)*NBBY) {
2112 /* no slots were appropriate, advance */
2117 setbit(sc->sc_keymap, keyix);
2118 setbit(sc->sc_keymap, keyix+64);
2119 setbit(sc->sc_keymap, keyix+32);
2120 setbit(sc->sc_keymap, keyix+32+64);
2121 DPRINTF(sc, ATH_DEBUG_KEYCACHE,
2122 "%s: key pair %u,%u %u,%u\n",
2123 __func__, keyix, keyix+64,
2124 keyix+32, keyix+32+64);
2126 *rxkeyix = keyix+32;
2130 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__);
2136 * Allocate tx/rx key slots for TKIP. We allocate two slots for
2137 * each key, one for decrypt/encrypt and the other for the MIC.
2140 key_alloc_pair(struct ath_softc *sc,
2141 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
2143 #define N(a) (sizeof(a)/sizeof(a[0]))
2146 KASSERT(!sc->sc_splitmic, ("key cache split"));
2147 /* XXX could optimize */
2148 for (i = 0; i < N(sc->sc_keymap)/4; i++) {
2149 u_int8_t b = sc->sc_keymap[i];
2152 * One or more slots in this byte are free.
2160 if (isset(sc->sc_keymap, keyix+64)) {
2161 /* full pair unavailable */
2163 if (keyix == (i+1)*NBBY) {
2164 /* no slots were appropriate, advance */
2169 setbit(sc->sc_keymap, keyix);
2170 setbit(sc->sc_keymap, keyix+64);
2171 DPRINTF(sc, ATH_DEBUG_KEYCACHE,
2172 "%s: key pair %u,%u\n",
2173 __func__, keyix, keyix+64);
2174 *txkeyix = *rxkeyix = keyix;
2178 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__);
2184 * Allocate a single key cache slot.
2187 key_alloc_single(struct ath_softc *sc,
2188 ieee80211_keyix *txkeyix, ieee80211_keyix *rxkeyix)
2190 #define N(a) (sizeof(a)/sizeof(a[0]))
2193 /* XXX try i,i+32,i+64,i+32+64 to minimize key pair conflicts */
2194 for (i = 0; i < N(sc->sc_keymap); i++) {
2195 u_int8_t b = sc->sc_keymap[i];
2198 * One or more slots are free.
2203 setbit(sc->sc_keymap, keyix);
2204 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: key %u\n",
2206 *txkeyix = *rxkeyix = keyix;
2210 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of space\n", __func__);
2216 * Allocate one or more key cache slots for a uniacst key. The
2217 * key itself is needed only to identify the cipher. For hardware
2218 * TKIP with split cipher+MIC keys we allocate two key cache slot
2219 * pairs so that we can setup separate TX and RX MIC keys. Note
2220 * that the MIC key for a TKIP key at slot i is assumed by the
2221 * hardware to be at slot i+64. This limits TKIP keys to the first
2225 ath_key_alloc(struct ieee80211vap *vap, struct ieee80211_key *k,
2226 ieee80211_keyix *keyix, ieee80211_keyix *rxkeyix)
2228 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
2231 * Group key allocation must be handled specially for
2232 * parts that do not support multicast key cache search
2233 * functionality. For those parts the key id must match
2234 * the h/w key index so lookups find the right key. On
2235 * parts w/ the key search facility we install the sender's
2236 * mac address (with the high bit set) and let the hardware
2237 * find the key w/o using the key id. This is preferred as
2238 * it permits us to support multiple users for adhoc and/or
2239 * multi-station operation.
2241 if (k->wk_keyix != IEEE80211_KEYIX_NONE) {
2243 * Only global keys should have key index assigned.
2245 if (!(&vap->iv_nw_keys[0] <= k &&
2246 k < &vap->iv_nw_keys[IEEE80211_WEP_NKID])) {
2247 /* should not happen */
2248 DPRINTF(sc, ATH_DEBUG_KEYCACHE,
2249 "%s: bogus group key\n", __func__);
2252 if (vap->iv_opmode != IEEE80211_M_HOSTAP ||
2253 !(k->wk_flags & IEEE80211_KEY_GROUP) ||
2256 * XXX we pre-allocate the global keys so
2257 * have no way to check if they've already
2260 *keyix = *rxkeyix = k - vap->iv_nw_keys;
2264 * Group key and device supports multicast key search.
2266 k->wk_keyix = IEEE80211_KEYIX_NONE;
2270 * We allocate two pair for TKIP when using the h/w to do
2271 * the MIC. For everything else, including software crypto,
2272 * we allocate a single entry. Note that s/w crypto requires
2273 * a pass-through slot on the 5211 and 5212. The 5210 does
2274 * not support pass-through cache entries and we map all
2275 * those requests to slot 0.
2277 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
2278 return key_alloc_single(sc, keyix, rxkeyix);
2279 } else if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP &&
2280 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) {
2281 if (sc->sc_splitmic)
2282 return key_alloc_2pair(sc, keyix, rxkeyix);
2284 return key_alloc_pair(sc, keyix, rxkeyix);
2286 return key_alloc_single(sc, keyix, rxkeyix);
2291 * Delete an entry in the key cache allocated by ath_key_alloc.
2294 ath_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
2296 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
2297 struct ath_hal *ah = sc->sc_ah;
2298 const struct ieee80211_cipher *cip = k->wk_cipher;
2299 u_int keyix = k->wk_keyix;
2301 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: delete key %u\n", __func__, keyix);
2303 ath_hal_keyreset(ah, keyix);
2305 * Handle split tx/rx keying required for TKIP with h/w MIC.
2307 if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
2308 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && sc->sc_splitmic)
2309 ath_hal_keyreset(ah, keyix+32); /* RX key */
2310 if (keyix >= IEEE80211_WEP_NKID) {
2312 * Don't touch keymap entries for global keys so
2313 * they are never considered for dynamic allocation.
2315 clrbit(sc->sc_keymap, keyix);
2316 if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
2317 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0) {
2318 clrbit(sc->sc_keymap, keyix+64); /* TX key MIC */
2319 if (sc->sc_splitmic) {
2320 /* +32 for RX key, +32+64 for RX key MIC */
2321 clrbit(sc->sc_keymap, keyix+32);
2322 clrbit(sc->sc_keymap, keyix+32+64);
2330 * Set the key cache contents for the specified key. Key cache
2331 * slot(s) must already have been allocated by ath_key_alloc.
2334 ath_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k,
2335 const u_int8_t mac[IEEE80211_ADDR_LEN])
2337 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
2339 return ath_keyset(sc, k, vap->iv_bss);
2343 * Block/unblock tx+rx processing while a key change is done.
2344 * We assume the caller serializes key management operations
2345 * so we only need to worry about synchronization with other
2346 * uses that originate in the driver.
2349 ath_key_update_begin(struct ieee80211vap *vap)
2351 struct ifnet *ifp = vap->iv_ic->ic_ifp;
2352 struct ath_softc *sc = ifp->if_softc;
2354 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
2355 taskqueue_block(sc->sc_tq);
2356 IF_LOCK(&ifp->if_snd); /* NB: doesn't block mgmt frames */
2360 ath_key_update_end(struct ieee80211vap *vap)
2362 struct ifnet *ifp = vap->iv_ic->ic_ifp;
2363 struct ath_softc *sc = ifp->if_softc;
2365 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
2366 IF_UNLOCK(&ifp->if_snd);
2367 taskqueue_unblock(sc->sc_tq);
2371 * Calculate the receive filter according to the
2372 * operating mode and state:
2374 * o always accept unicast, broadcast, and multicast traffic
2375 * o accept PHY error frames when hardware doesn't have MIB support
2376 * to count and we need them for ANI (sta mode only until recently)
2377 * and we are not scanning (ANI is disabled)
2378 * NB: older hal's add rx filter bits out of sight and we need to
2379 * blindly preserve them
2380 * o probe request frames are accepted only when operating in
2381 * hostap, adhoc, mesh, or monitor modes
2382 * o enable promiscuous mode
2383 * - when in monitor mode
2384 * - if interface marked PROMISC (assumes bridge setting is filtered)
2386 * - when operating in station mode for collecting rssi data when
2387 * the station is otherwise quiet, or
2388 * - when operating in adhoc mode so the 802.11 layer creates
2389 * node table entries for peers,
2391 * - when doing s/w beacon miss (e.g. for ap+sta)
2392 * - when operating in ap mode in 11g to detect overlapping bss that
2393 * require protection
2394 * - when operating in mesh mode to detect neighbors
2395 * o accept control frames:
2396 * - when in monitor mode
2397 * XXX BAR frames for 11n
2398 * XXX HT protection for 11n
2401 ath_calcrxfilter(struct ath_softc *sc)
2403 struct ifnet *ifp = sc->sc_ifp;
2404 struct ieee80211com *ic = ifp->if_l2com;
2407 rfilt = HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
2408 if (!sc->sc_needmib && !sc->sc_scanning)
2409 rfilt |= HAL_RX_FILTER_PHYERR;
2410 if (ic->ic_opmode != IEEE80211_M_STA)
2411 rfilt |= HAL_RX_FILTER_PROBEREQ;
2412 /* XXX ic->ic_monvaps != 0? */
2413 if (ic->ic_opmode == IEEE80211_M_MONITOR || (ifp->if_flags & IFF_PROMISC))
2414 rfilt |= HAL_RX_FILTER_PROM;
2415 if (ic->ic_opmode == IEEE80211_M_STA ||
2416 ic->ic_opmode == IEEE80211_M_IBSS ||
2417 sc->sc_swbmiss || sc->sc_scanning)
2418 rfilt |= HAL_RX_FILTER_BEACON;
2420 * NB: We don't recalculate the rx filter when
2421 * ic_protmode changes; otherwise we could do
2422 * this only when ic_protmode != NONE.
2424 if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
2425 IEEE80211_IS_CHAN_ANYG(ic->ic_curchan))
2426 rfilt |= HAL_RX_FILTER_BEACON;
2427 if (sc->sc_nmeshvaps) {
2428 rfilt |= HAL_RX_FILTER_BEACON;
2429 if (sc->sc_hasbmatch)
2430 rfilt |= HAL_RX_FILTER_BSSID;
2432 rfilt |= HAL_RX_FILTER_PROM;
2434 if (ic->ic_opmode == IEEE80211_M_MONITOR)
2435 rfilt |= HAL_RX_FILTER_CONTROL;
2436 DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, %s if_flags 0x%x\n",
2437 __func__, rfilt, ieee80211_opmode_name[ic->ic_opmode], ifp->if_flags);
2442 ath_update_promisc(struct ifnet *ifp)
2444 struct ath_softc *sc = ifp->if_softc;
2447 /* configure rx filter */
2448 rfilt = ath_calcrxfilter(sc);
2449 ath_hal_setrxfilter(sc->sc_ah, rfilt);
2451 DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x\n", __func__, rfilt);
2455 ath_update_mcast(struct ifnet *ifp)
2457 struct ath_softc *sc = ifp->if_softc;
2460 /* calculate and install multicast filter */
2461 if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
2462 struct ifmultiaddr *ifma;
2464 * Merge multicast addresses to form the hardware filter.
2466 mfilt[0] = mfilt[1] = 0;
2468 if_maddr_rlock(ifp); /* XXX need some fiddling to remove? */
2470 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2475 /* calculate XOR of eight 6bit values */
2476 dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
2477 val = LE_READ_4(dl + 0);
2478 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2479 val = LE_READ_4(dl + 3);
2480 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2482 mfilt[pos / 32] |= (1 << (pos % 32));
2485 if_maddr_runlock(ifp);
2488 mfilt[0] = mfilt[1] = ~0;
2489 ath_hal_setmcastfilter(sc->sc_ah, mfilt[0], mfilt[1]);
2490 DPRINTF(sc, ATH_DEBUG_MODE, "%s: MC filter %08x:%08x\n",
2491 __func__, mfilt[0], mfilt[1]);
2495 ath_mode_init(struct ath_softc *sc)
2497 struct ifnet *ifp = sc->sc_ifp;
2498 struct ath_hal *ah = sc->sc_ah;
2501 /* configure rx filter */
2502 rfilt = ath_calcrxfilter(sc);
2503 ath_hal_setrxfilter(ah, rfilt);
2505 /* configure operational mode */
2506 ath_hal_setopmode(ah);
2508 /* handle any link-level address change */
2509 ath_hal_setmac(ah, IF_LLADDR(ifp));
2511 /* calculate and install multicast filter */
2512 ath_update_mcast(ifp);
2516 * Set the slot time based on the current setting.
2519 ath_setslottime(struct ath_softc *sc)
2521 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2522 struct ath_hal *ah = sc->sc_ah;
2525 if (IEEE80211_IS_CHAN_HALF(ic->ic_curchan))
2527 else if (IEEE80211_IS_CHAN_QUARTER(ic->ic_curchan))
2529 else if (IEEE80211_IS_CHAN_ANYG(ic->ic_curchan)) {
2530 /* honor short/long slot time only in 11g */
2531 /* XXX shouldn't honor on pure g or turbo g channel */
2532 if (ic->ic_flags & IEEE80211_F_SHSLOT)
2533 usec = HAL_SLOT_TIME_9;
2535 usec = HAL_SLOT_TIME_20;
2537 usec = HAL_SLOT_TIME_9;
2539 DPRINTF(sc, ATH_DEBUG_RESET,
2540 "%s: chan %u MHz flags 0x%x %s slot, %u usec\n",
2541 __func__, ic->ic_curchan->ic_freq, ic->ic_curchan->ic_flags,
2542 ic->ic_flags & IEEE80211_F_SHSLOT ? "short" : "long", usec);
2544 ath_hal_setslottime(ah, usec);
2545 sc->sc_updateslot = OK;
2549 * Callback from the 802.11 layer to update the
2550 * slot time based on the current setting.
2553 ath_updateslot(struct ifnet *ifp)
2555 struct ath_softc *sc = ifp->if_softc;
2556 struct ieee80211com *ic = ifp->if_l2com;
2559 * When not coordinating the BSS, change the hardware
2560 * immediately. For other operation we defer the change
2561 * until beacon updates have propagated to the stations.
2563 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
2564 ic->ic_opmode == IEEE80211_M_MBSS)
2565 sc->sc_updateslot = UPDATE;
2567 ath_setslottime(sc);
2571 * Setup a h/w transmit queue for beacons.
2574 ath_beaconq_setup(struct ath_hal *ah)
2578 memset(&qi, 0, sizeof(qi));
2579 qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
2580 qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
2581 qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
2582 /* NB: for dynamic turbo, don't enable any other interrupts */
2583 qi.tqi_qflags = HAL_TXQ_TXDESCINT_ENABLE;
2584 return ath_hal_setuptxqueue(ah, HAL_TX_QUEUE_BEACON, &qi);
2588 * Setup the transmit queue parameters for the beacon queue.
2591 ath_beaconq_config(struct ath_softc *sc)
2593 #define ATH_EXPONENT_TO_VALUE(v) ((1<<(v))-1)
2594 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2595 struct ath_hal *ah = sc->sc_ah;
2598 ath_hal_gettxqueueprops(ah, sc->sc_bhalq, &qi);
2599 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
2600 ic->ic_opmode == IEEE80211_M_MBSS) {
2602 * Always burst out beacon and CAB traffic.
2604 qi.tqi_aifs = ATH_BEACON_AIFS_DEFAULT;
2605 qi.tqi_cwmin = ATH_BEACON_CWMIN_DEFAULT;
2606 qi.tqi_cwmax = ATH_BEACON_CWMAX_DEFAULT;
2608 struct wmeParams *wmep =
2609 &ic->ic_wme.wme_chanParams.cap_wmeParams[WME_AC_BE];
2611 * Adhoc mode; important thing is to use 2x cwmin.
2613 qi.tqi_aifs = wmep->wmep_aifsn;
2614 qi.tqi_cwmin = 2*ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
2615 qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
2618 if (!ath_hal_settxqueueprops(ah, sc->sc_bhalq, &qi)) {
2619 device_printf(sc->sc_dev, "unable to update parameters for "
2620 "beacon hardware queue!\n");
2623 ath_hal_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */
2626 #undef ATH_EXPONENT_TO_VALUE
2630 * Allocate and setup an initial beacon frame.
2633 ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
2635 struct ieee80211vap *vap = ni->ni_vap;
2636 struct ath_vap *avp = ATH_VAP(vap);
2642 if (bf->bf_m != NULL) {
2643 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2647 if (bf->bf_node != NULL) {
2648 ieee80211_free_node(bf->bf_node);
2653 * NB: the beacon data buffer must be 32-bit aligned;
2654 * we assume the mbuf routines will return us something
2655 * with this alignment (perhaps should assert).
2657 m = ieee80211_beacon_alloc(ni, &avp->av_boff);
2659 device_printf(sc->sc_dev, "%s: cannot get mbuf\n", __func__);
2660 sc->sc_stats.ast_be_nombuf++;
2663 error = bus_dmamap_load_mbuf_segment(sc->sc_dmat, bf->bf_dmamap, m,
2664 bf->bf_segs, 1, &bf->bf_nseg,
2667 device_printf(sc->sc_dev,
2668 "%s: cannot map mbuf, bus_dmamap_load_mbuf_segment returns %d\n",
2675 * Calculate a TSF adjustment factor required for staggered
2676 * beacons. Note that we assume the format of the beacon
2677 * frame leaves the tstamp field immediately following the
2680 if (sc->sc_stagbeacons && avp->av_bslot > 0) {
2682 struct ieee80211_frame *wh;
2685 * The beacon interval is in TU's; the TSF is in usecs.
2686 * We figure out how many TU's to add to align the timestamp
2687 * then convert to TSF units and handle byte swapping before
2688 * inserting it in the frame. The hardware will then add this
2689 * each time a beacon frame is sent. Note that we align vap's
2690 * 1..N and leave vap 0 untouched. This means vap 0 has a
2691 * timestamp in one beacon interval while the others get a
2692 * timstamp aligned to the next interval.
2694 tsfadjust = ni->ni_intval *
2695 (ATH_BCBUF - avp->av_bslot) / ATH_BCBUF;
2696 tsfadjust = htole64(tsfadjust << 10); /* TU -> TSF */
2698 DPRINTF(sc, ATH_DEBUG_BEACON,
2699 "%s: %s beacons bslot %d intval %u tsfadjust %llu\n",
2700 __func__, sc->sc_stagbeacons ? "stagger" : "burst",
2701 avp->av_bslot, ni->ni_intval,
2702 (long long unsigned) le64toh(tsfadjust));
2704 wh = mtod(m, struct ieee80211_frame *);
2705 memcpy(&wh[1], &tsfadjust, sizeof(tsfadjust));
2708 bf->bf_node = ieee80211_ref_node(ni);
2714 * Setup the beacon frame for transmit.
2717 ath_beacon_setup(struct ath_softc *sc, struct ath_buf *bf)
2719 #define USE_SHPREAMBLE(_ic) \
2720 (((_ic)->ic_flags & (IEEE80211_F_SHPREAMBLE | IEEE80211_F_USEBARKER))\
2721 == IEEE80211_F_SHPREAMBLE)
2722 struct ieee80211_node *ni = bf->bf_node;
2723 struct ieee80211com *ic = ni->ni_ic;
2724 struct mbuf *m = bf->bf_m;
2725 struct ath_hal *ah = sc->sc_ah;
2726 struct ath_desc *ds;
2728 const HAL_RATE_TABLE *rt;
2731 DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: m %p len %u\n",
2732 __func__, m, m->m_len);
2734 /* setup descriptors */
2737 flags = HAL_TXDESC_NOACK;
2738 if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) {
2739 ds->ds_link = bf->bf_daddr; /* self-linked */
2740 flags |= HAL_TXDESC_VEOL;
2742 * Let hardware handle antenna switching.
2744 antenna = sc->sc_txantenna;
2748 * Switch antenna every 4 beacons.
2749 * XXX assumes two antenna
2751 if (sc->sc_txantenna != 0)
2752 antenna = sc->sc_txantenna;
2753 else if (sc->sc_stagbeacons && sc->sc_nbcnvaps != 0)
2754 antenna = ((sc->sc_stats.ast_be_xmit / sc->sc_nbcnvaps) & 4 ? 2 : 1);
2756 antenna = (sc->sc_stats.ast_be_xmit & 4 ? 2 : 1);
2759 KASSERT(bf->bf_nseg == 1,
2760 ("multi-segment beacon frame; nseg %u", bf->bf_nseg));
2761 ds->ds_data = bf->bf_segs[0].ds_addr;
2763 * Calculate rate code.
2764 * XXX everything at min xmit rate
2767 rt = sc->sc_currates;
2768 rate = rt->info[rix].rateCode;
2769 if (USE_SHPREAMBLE(ic))
2770 rate |= rt->info[rix].shortPreamble;
2771 ath_hal_setuptxdesc(ah, ds
2772 , m->m_len + IEEE80211_CRC_LEN /* frame length */
2773 , sizeof(struct ieee80211_frame)/* header length */
2774 , HAL_PKT_TYPE_BEACON /* Atheros packet type */
2775 , ni->ni_txpower /* txpower XXX */
2776 , rate, 1 /* series 0 rate/tries */
2777 , HAL_TXKEYIX_INVALID /* no encryption */
2778 , antenna /* antenna mode */
2779 , flags /* no ack, veol for beacons */
2780 , 0 /* rts/cts rate */
2781 , 0 /* rts/cts duration */
2783 /* NB: beacon's BufLen must be a multiple of 4 bytes */
2784 ath_hal_filltxdesc(ah, ds
2785 , roundup(m->m_len, 4) /* buffer length */
2786 , AH_TRUE /* first segment */
2787 , AH_TRUE /* last segment */
2788 , ds /* first descriptor */
2793 #undef USE_SHPREAMBLE
2797 ath_beacon_update(struct ieee80211vap *vap, int item)
2799 struct ieee80211_beacon_offsets *bo = &ATH_VAP(vap)->av_boff;
2801 setbit(bo->bo_flags, item);
2805 * Append the contents of src to dst; both queues
2806 * are assumed to be locked.
2809 ath_txqmove(struct ath_txq *dst, struct ath_txq *src)
2811 STAILQ_CONCAT(&dst->axq_q, &src->axq_q);
2812 dst->axq_link = src->axq_link;
2813 src->axq_link = NULL;
2814 dst->axq_depth += src->axq_depth;
2819 * Transmit a beacon frame at SWBA. Dynamic updates to the
2820 * frame contents are done as needed and the slot time is
2821 * also adjusted based on current state.
2824 ath_beacon_proc(void *arg, int pending)
2826 struct ath_softc *sc = arg;
2827 struct ath_hal *ah = sc->sc_ah;
2828 struct ieee80211vap *vap;
2833 DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: pending %u\n",
2836 * Check if the previous beacon has gone out. If
2837 * not don't try to post another, skip this period
2838 * and wait for the next. Missed beacons indicate
2839 * a problem and should not occur. If we miss too
2840 * many consecutive beacons reset the device.
2842 if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
2843 sc->sc_bmisscount++;
2844 DPRINTF(sc, ATH_DEBUG_BEACON,
2845 "%s: missed %u consecutive beacons\n",
2846 __func__, sc->sc_bmisscount);
2847 if (sc->sc_bmisscount >= ath_bstuck_threshold)
2848 taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask);
2851 if (sc->sc_bmisscount != 0) {
2852 DPRINTF(sc, ATH_DEBUG_BEACON,
2853 "%s: resume beacon xmit after %u misses\n",
2854 __func__, sc->sc_bmisscount);
2855 sc->sc_bmisscount = 0;
2858 if (sc->sc_stagbeacons) { /* staggered beacons */
2859 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
2862 tsftu = ath_hal_gettsf32(ah) >> 10;
2864 slot = ((tsftu % ic->ic_lintval) * ATH_BCBUF) / ic->ic_lintval;
2865 vap = sc->sc_bslot[(slot+1) % ATH_BCBUF];
2867 if (vap != NULL && vap->iv_state >= IEEE80211_S_RUN) {
2868 bf = ath_beacon_generate(sc, vap);
2870 bfaddr = bf->bf_daddr;
2872 } else { /* burst'd beacons */
2873 uint32_t *bflink = &bfaddr;
2875 for (slot = 0; slot < ATH_BCBUF; slot++) {
2876 vap = sc->sc_bslot[slot];
2877 if (vap != NULL && vap->iv_state >= IEEE80211_S_RUN) {
2878 bf = ath_beacon_generate(sc, vap);
2880 *bflink = bf->bf_daddr;
2881 bflink = &bf->bf_desc->ds_link;
2885 *bflink = 0; /* terminate list */
2889 * Handle slot time change when a non-ERP station joins/leaves
2890 * an 11g network. The 802.11 layer notifies us via callback,
2891 * we mark updateslot, then wait one beacon before effecting
2892 * the change. This gives associated stations at least one
2893 * beacon interval to note the state change.
2896 if (sc->sc_updateslot == UPDATE) {
2897 sc->sc_updateslot = COMMIT; /* commit next beacon */
2898 sc->sc_slotupdate = slot;
2899 } else if (sc->sc_updateslot == COMMIT && sc->sc_slotupdate == slot)
2900 ath_setslottime(sc); /* commit change to h/w */
2903 * Check recent per-antenna transmit statistics and flip
2904 * the default antenna if noticeably more frames went out
2905 * on the non-default antenna.
2906 * XXX assumes 2 anntenae
2908 if (!sc->sc_diversity && (!sc->sc_stagbeacons || slot == 0)) {
2909 otherant = sc->sc_defant & 1 ? 2 : 1;
2910 if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2)
2911 ath_setdefantenna(sc, otherant);
2912 sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0;
2917 * Stop any current dma and put the new frame on the queue.
2918 * This should never fail since we check above that no frames
2919 * are still pending on the queue.
2921 if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
2922 DPRINTF(sc, ATH_DEBUG_ANY,
2923 "%s: beacon queue %u did not stop?\n",
2924 __func__, sc->sc_bhalq);
2926 /* NB: cabq traffic should already be queued and primed */
2927 ath_hal_puttxbuf(ah, sc->sc_bhalq, bfaddr);
2928 ath_hal_txstart(ah, sc->sc_bhalq);
2930 sc->sc_stats.ast_be_xmit++;
2934 static struct ath_buf *
2935 ath_beacon_generate(struct ath_softc *sc, struct ieee80211vap *vap)
2937 struct ath_vap *avp = ATH_VAP(vap);
2938 struct ath_txq *cabq = sc->sc_cabq;
2943 KASSERT(vap->iv_state >= IEEE80211_S_RUN,
2944 ("not running, state %d", vap->iv_state));
2945 KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer"));
2948 * Update dynamic beacon contents. If this returns
2949 * non-zero then we need to remap the memory because
2950 * the beacon frame changed size (probably because
2951 * of the TIM bitmap).
2955 nmcastq = avp->av_mcastq.axq_depth;
2956 if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, nmcastq)) {
2957 /* XXX too conservative? */
2958 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2959 error = bus_dmamap_load_mbuf_segment(sc->sc_dmat, bf->bf_dmamap, m,
2960 bf->bf_segs, 1, &bf->bf_nseg,
2963 if_printf(vap->iv_ifp,
2964 "%s: bus_dmamap_load_mbuf_segment failed, error %u\n",
2969 if ((avp->av_boff.bo_tim[4] & 1) && cabq->axq_depth) {
2970 DPRINTF(sc, ATH_DEBUG_BEACON,
2971 "%s: cabq did not drain, mcastq %u cabq %u\n",
2972 __func__, nmcastq, cabq->axq_depth);
2973 sc->sc_stats.ast_cabq_busy++;
2974 if (sc->sc_nvaps > 1 && sc->sc_stagbeacons) {
2976 * CABQ traffic from a previous vap is still pending.
2977 * We must drain the q before this beacon frame goes
2978 * out as otherwise this vap's stations will get cab
2979 * frames from a different vap.
2980 * XXX could be slow causing us to miss DBA
2982 ath_tx_draintxq(sc, cabq);
2985 ath_beacon_setup(sc, bf);
2986 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
2989 * Enable the CAB queue before the beacon queue to
2990 * insure cab frames are triggered by this beacon.
2992 if (avp->av_boff.bo_tim[4] & 1) {
2993 struct ath_hal *ah = sc->sc_ah;
2995 /* NB: only at DTIM */
2997 ATH_TXQ_LOCK(&avp->av_mcastq);
2999 struct ath_buf *bfm;
3002 * Move frames from the s/w mcast q to the h/w cab q.
3005 bfm = STAILQ_FIRST(&avp->av_mcastq.axq_q);
3006 if (cabq->axq_link != NULL) {
3007 *cabq->axq_link = bfm->bf_daddr;
3009 ath_hal_puttxbuf(ah, cabq->axq_qnum,
3011 ath_txqmove(cabq, &avp->av_mcastq);
3013 sc->sc_stats.ast_cabq_xmit += nmcastq;
3015 /* NB: gated by beacon so safe to start here */
3016 ath_hal_txstart(ah, cabq->axq_qnum);
3017 ATH_TXQ_UNLOCK(cabq);
3018 ATH_TXQ_UNLOCK(&avp->av_mcastq);
3024 ath_beacon_start_adhoc(struct ath_softc *sc, struct ieee80211vap *vap)
3026 struct ath_vap *avp = ATH_VAP(vap);
3027 struct ath_hal *ah = sc->sc_ah;
3032 KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer"));
3035 * Update dynamic beacon contents. If this returns
3036 * non-zero then we need to remap the memory because
3037 * the beacon frame changed size (probably because
3038 * of the TIM bitmap).
3042 if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, 0)) {
3043 /* XXX too conservative? */
3044 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3045 error = bus_dmamap_load_mbuf_segment(sc->sc_dmat, bf->bf_dmamap, m,
3046 bf->bf_segs, 1, &bf->bf_nseg,
3049 if_printf(vap->iv_ifp,
3050 "%s: bus_dmamap_load_mbuf_segment failed, error %u\n",
3055 ath_beacon_setup(sc, bf);
3056 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
3058 /* NB: caller is known to have already stopped tx dma */
3059 ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
3060 ath_hal_txstart(ah, sc->sc_bhalq);
3064 * Reset the hardware after detecting beacons have stopped.
3067 ath_bstuck_proc(void *arg, int pending)
3069 struct ath_softc *sc = arg;
3070 struct ifnet *ifp = sc->sc_ifp;
3072 if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
3074 sc->sc_stats.ast_bstuck++;
3079 * Reclaim beacon resources and return buffer to the pool.
3082 ath_beacon_return(struct ath_softc *sc, struct ath_buf *bf)
3085 if (bf->bf_m != NULL) {
3086 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3090 if (bf->bf_node != NULL) {
3091 ieee80211_free_node(bf->bf_node);
3094 STAILQ_INSERT_TAIL(&sc->sc_bbuf, bf, bf_list);
3098 * Reclaim beacon resources.
3101 ath_beacon_free(struct ath_softc *sc)
3105 STAILQ_FOREACH(bf, &sc->sc_bbuf, bf_list) {
3106 if (bf->bf_m != NULL) {
3107 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3111 if (bf->bf_node != NULL) {
3112 ieee80211_free_node(bf->bf_node);
3119 * Configure the beacon and sleep timers.
3121 * When operating as an AP this resets the TSF and sets
3122 * up the hardware to notify us when we need to issue beacons.
3124 * When operating in station mode this sets up the beacon
3125 * timers according to the timestamp of the last received
3126 * beacon and the current TSF, configures PCF and DTIM
3127 * handling, programs the sleep registers so the hardware
3128 * will wakeup in time to receive beacons, and configures
3129 * the beacon miss handling so we'll receive a BMISS
3130 * interrupt when we stop seeing beacons from the AP
3131 * we've associated with.
3134 ath_beacon_config(struct ath_softc *sc, struct ieee80211vap *vap)
3136 #define TSF_TO_TU(_h,_l) \
3137 ((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
3139 struct ath_hal *ah = sc->sc_ah;
3140 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
3141 struct ieee80211_node *ni;
3142 u_int32_t nexttbtt, intval, tsftu;
3146 vap = TAILQ_FIRST(&ic->ic_vaps); /* XXX */
3149 /* extract tstamp from last beacon and convert to TU */
3150 nexttbtt = TSF_TO_TU(LE_READ_4(ni->ni_tstamp.data + 4),
3151 LE_READ_4(ni->ni_tstamp.data));
3152 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
3153 ic->ic_opmode == IEEE80211_M_MBSS) {
3155 * For multi-bss ap/mesh support beacons are either staggered
3156 * evenly over N slots or burst together. For the former
3157 * arrange for the SWBA to be delivered for each slot.
3158 * Slots that are not occupied will generate nothing.
3160 /* NB: the beacon interval is kept internally in TU's */
3161 intval = ni->ni_intval & HAL_BEACON_PERIOD;
3162 if (sc->sc_stagbeacons)
3163 intval /= ATH_BCBUF;
3165 /* NB: the beacon interval is kept internally in TU's */
3166 intval = ni->ni_intval & HAL_BEACON_PERIOD;
3168 if (nexttbtt == 0) /* e.g. for ap mode */
3170 else if (intval) /* NB: can be 0 for monitor mode */
3171 nexttbtt = roundup(nexttbtt, intval);
3172 DPRINTF(sc, ATH_DEBUG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
3173 __func__, nexttbtt, intval, ni->ni_intval);
3174 if (ic->ic_opmode == IEEE80211_M_STA && !sc->sc_swbmiss) {
3175 HAL_BEACON_STATE bs;
3176 int dtimperiod, dtimcount;
3177 int cfpperiod, cfpcount;
3180 * Setup dtim and cfp parameters according to
3181 * last beacon we received (which may be none).
3183 dtimperiod = ni->ni_dtim_period;
3184 if (dtimperiod <= 0) /* NB: 0 if not known */
3186 dtimcount = ni->ni_dtim_count;
3187 if (dtimcount >= dtimperiod) /* NB: sanity check */
3188 dtimcount = 0; /* XXX? */
3189 cfpperiod = 1; /* NB: no PCF support yet */
3192 * Pull nexttbtt forward to reflect the current
3193 * TSF and calculate dtim+cfp state for the result.
3195 tsf = ath_hal_gettsf64(ah);
3196 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
3199 if (--dtimcount < 0) {
3200 dtimcount = dtimperiod - 1;
3202 cfpcount = cfpperiod - 1;
3204 } while (nexttbtt < tsftu);
3205 memset(&bs, 0, sizeof(bs));
3206 bs.bs_intval = intval;
3207 bs.bs_nexttbtt = nexttbtt;
3208 bs.bs_dtimperiod = dtimperiod*intval;
3209 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
3210 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
3211 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
3212 bs.bs_cfpmaxduration = 0;
3215 * The 802.11 layer records the offset to the DTIM
3216 * bitmap while receiving beacons; use it here to
3217 * enable h/w detection of our AID being marked in
3218 * the bitmap vector (to indicate frames for us are
3219 * pending at the AP).
3220 * XXX do DTIM handling in s/w to WAR old h/w bugs
3221 * XXX enable based on h/w rev for newer chips
3223 bs.bs_timoffset = ni->ni_timoff;
3226 * Calculate the number of consecutive beacons to miss
3227 * before taking a BMISS interrupt.
3228 * Note that we clamp the result to at most 10 beacons.
3230 bs.bs_bmissthreshold = vap->iv_bmissthreshold;
3231 if (bs.bs_bmissthreshold > 10)
3232 bs.bs_bmissthreshold = 10;
3233 else if (bs.bs_bmissthreshold <= 0)
3234 bs.bs_bmissthreshold = 1;
3237 * Calculate sleep duration. The configuration is
3238 * given in ms. We insure a multiple of the beacon
3239 * period is used. Also, if the sleep duration is
3240 * greater than the DTIM period then it makes senses
3241 * to make it a multiple of that.
3243 * XXX fixed at 100ms
3245 bs.bs_sleepduration =
3246 roundup(IEEE80211_MS_TO_TU(100), bs.bs_intval);
3247 if (bs.bs_sleepduration > bs.bs_dtimperiod)
3248 bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
3250 DPRINTF(sc, ATH_DEBUG_BEACON,
3251 "%s: tsf %ju tsf:tu %u intval %u nexttbtt %u dtim %u nextdtim %u bmiss %u sleep %u cfp:period %u maxdur %u next %u timoffset %u\n"
3258 , bs.bs_bmissthreshold
3259 , bs.bs_sleepduration
3261 , bs.bs_cfpmaxduration
3265 ath_hal_intrset(ah, 0);
3266 ath_hal_beacontimers(ah, &bs);
3267 sc->sc_imask |= HAL_INT_BMISS;
3268 ath_hal_intrset(ah, sc->sc_imask);
3270 ath_hal_intrset(ah, 0);
3271 if (nexttbtt == intval)
3272 intval |= HAL_BEACON_RESET_TSF;
3273 if (ic->ic_opmode == IEEE80211_M_IBSS) {
3275 * In IBSS mode enable the beacon timers but only
3276 * enable SWBA interrupts if we need to manually
3277 * prepare beacon frames. Otherwise we use a
3278 * self-linked tx descriptor and let the hardware
3281 intval |= HAL_BEACON_ENA;
3282 if (!sc->sc_hasveol)
3283 sc->sc_imask |= HAL_INT_SWBA;
3284 if ((intval & HAL_BEACON_RESET_TSF) == 0) {
3286 * Pull nexttbtt forward to reflect
3289 tsf = ath_hal_gettsf64(ah);
3290 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
3293 } while (nexttbtt < tsftu);
3295 ath_beaconq_config(sc);
3296 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
3297 ic->ic_opmode == IEEE80211_M_MBSS) {
3299 * In AP/mesh mode we enable the beacon timers
3300 * and SWBA interrupts to prepare beacon frames.
3302 intval |= HAL_BEACON_ENA;
3303 sc->sc_imask |= HAL_INT_SWBA; /* beacon prepare */
3304 ath_beaconq_config(sc);
3306 ath_hal_beaconinit(ah, nexttbtt, intval);
3307 sc->sc_bmisscount = 0;
3308 ath_hal_intrset(ah, sc->sc_imask);
3310 * When using a self-linked beacon descriptor in
3311 * ibss mode load it once here.
3313 if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol)
3314 ath_beacon_start_adhoc(sc, vap);
3316 sc->sc_syncbeacon = 0;
3322 ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
3324 bus_addr_t *paddr = (bus_addr_t*) arg;
3325 KASSERT(error == 0, ("error %u on bus_dma callback", error));
3326 *paddr = segs->ds_addr;
3330 ath_descdma_setup(struct ath_softc *sc,
3331 struct ath_descdma *dd, ath_bufhead *head,
3332 const char *name, int nbuf, int ndesc)
3334 #define DS2PHYS(_dd, _ds) \
3335 ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
3336 struct ifnet *ifp = sc->sc_ifp;
3337 struct ath_desc *ds;
3339 int i, bsize, error;
3341 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers %u desc/buf\n",
3342 __func__, name, nbuf, ndesc);
3345 dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
3348 * Setup DMA descriptor area.
3350 error = bus_dma_tag_create(dd->dd_dmat, /* parent */
3351 PAGE_SIZE, 0, /* alignment, bounds */
3352 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
3353 BUS_SPACE_MAXADDR, /* highaddr */
3354 NULL, NULL, /* filter, filterarg */
3355 dd->dd_desc_len, /* maxsize */
3357 dd->dd_desc_len, /* maxsegsize */
3358 BUS_DMA_ALLOCNOW, /* flags */
3361 if_printf(ifp, "cannot allocate %s DMA tag\n", dd->dd_name);
3365 /* allocate descriptors */
3366 error = bus_dmamap_create(dd->dd_dmat, BUS_DMA_NOWAIT, &dd->dd_dmamap);
3368 if_printf(ifp, "unable to create dmamap for %s descriptors, "
3369 "error %u\n", dd->dd_name, error);
3373 error = bus_dmamem_alloc(dd->dd_dmat, (void**) &dd->dd_desc,
3374 BUS_DMA_NOWAIT | BUS_DMA_COHERENT,
3377 if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
3378 "error %u\n", nbuf * ndesc, dd->dd_name, error);
3382 error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap,
3383 dd->dd_desc, dd->dd_desc_len,
3384 ath_load_cb, &dd->dd_desc_paddr,
3387 if_printf(ifp, "unable to map %s descriptors, error %u\n",
3388 dd->dd_name, error);
3393 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
3394 __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len,
3395 (caddr_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len);
3397 /* allocate rx buffers */
3398 bsize = sizeof(struct ath_buf) * nbuf;
3399 bf = kmalloc(bsize, M_ATHDEV, M_INTWAIT | M_ZERO);
3401 if_printf(ifp, "malloc of %s buffers failed, size %u\n",
3402 dd->dd_name, bsize);
3408 for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
3410 bf->bf_daddr = DS2PHYS(dd, ds);
3411 error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
3414 if_printf(ifp, "unable to create dmamap for %s "
3415 "buffer %u, error %u\n", dd->dd_name, i, error);
3416 ath_descdma_cleanup(sc, dd, head);
3419 STAILQ_INSERT_TAIL(head, bf, bf_list);
3423 bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3425 bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3427 bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
3429 bus_dma_tag_destroy(dd->dd_dmat);
3430 memset(dd, 0, sizeof(*dd));
3436 ath_descdma_cleanup(struct ath_softc *sc,
3437 struct ath_descdma *dd, ath_bufhead *head)
3440 struct ieee80211_node *ni;
3442 bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
3443 bus_dmamem_free(dd->dd_dmat, dd->dd_desc, dd->dd_dmamap);
3444 bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
3445 bus_dma_tag_destroy(dd->dd_dmat);
3447 STAILQ_FOREACH(bf, head, bf_list) {
3452 if (bf->bf_dmamap != NULL) {
3453 bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
3454 bf->bf_dmamap = NULL;
3460 * Reclaim node reference.
3462 ieee80211_free_node(ni);
3467 kfree(dd->dd_bufptr, M_ATHDEV);
3468 memset(dd, 0, sizeof(*dd));
3472 ath_desc_alloc(struct ath_softc *sc)
3476 error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
3477 "rx", ath_rxbuf, 1);
3481 error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
3482 "tx", ath_txbuf, ATH_TXDESC);
3484 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
3488 error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
3489 "beacon", ATH_BCBUF, 1);
3491 ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3492 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
3499 ath_desc_free(struct ath_softc *sc)
3502 if (sc->sc_bdma.dd_desc_len != 0)
3503 ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
3504 if (sc->sc_txdma.dd_desc_len != 0)
3505 ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
3506 if (sc->sc_rxdma.dd_desc_len != 0)
3507 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
3510 static struct ieee80211_node *
3511 ath_node_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN])
3513 struct ieee80211com *ic = vap->iv_ic;
3514 struct ath_softc *sc = ic->ic_ifp->if_softc;
3515 const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
3516 struct ath_node *an;
3518 an = kmalloc(space, M_80211_NODE, M_INTWAIT|M_ZERO);
3523 ath_rate_node_init(sc, an);
3525 DPRINTF(sc, ATH_DEBUG_NODE, "%s: an %p\n", __func__, an);
3526 return &an->an_node;
3530 ath_node_free(struct ieee80211_node *ni)
3532 struct ieee80211com *ic = ni->ni_ic;
3533 struct ath_softc *sc = ic->ic_ifp->if_softc;
3535 DPRINTF(sc, ATH_DEBUG_NODE, "%s: ni %p\n", __func__, ni);
3537 ath_rate_node_cleanup(sc, ATH_NODE(ni));
3538 sc->sc_node_free(ni);
3542 ath_node_getsignal(const struct ieee80211_node *ni, int8_t *rssi, int8_t *noise)
3544 struct ieee80211com *ic = ni->ni_ic;
3545 struct ath_softc *sc = ic->ic_ifp->if_softc;
3546 struct ath_hal *ah = sc->sc_ah;
3548 *rssi = ic->ic_node_getrssi(ni);
3549 if (ni->ni_chan != IEEE80211_CHAN_ANYC)
3550 *noise = ath_hal_getchannoise(ah, ni->ni_chan);
3552 *noise = -95; /* nominally correct */
3556 ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
3558 struct ath_hal *ah = sc->sc_ah;
3561 struct ath_desc *ds;
3566 * NB: by assigning a page to the rx dma buffer we
3567 * implicitly satisfy the Atheros requirement that
3568 * this buffer be cache-line-aligned and sized to be
3569 * multiple of the cache line size. Not doing this
3570 * causes weird stuff to happen (for the 5210 at least).
3572 m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
3574 DPRINTF(sc, ATH_DEBUG_ANY,
3575 "%s: no mbuf/cluster\n", __func__);
3576 sc->sc_stats.ast_rx_nombuf++;
3579 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
3581 error = bus_dmamap_load_mbuf_segment(sc->sc_dmat,
3583 bf->bf_segs, 1, &bf->bf_nseg,
3586 DPRINTF(sc, ATH_DEBUG_ANY,
3587 "%s: bus_dmamap_load_mbuf_segment failed; error %d\n",
3589 sc->sc_stats.ast_rx_busdma++;
3593 KASSERT(bf->bf_nseg == 1,
3594 ("multi-segment packet; nseg %u", bf->bf_nseg));
3597 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREREAD);
3600 * Setup descriptors. For receive we always terminate
3601 * the descriptor list with a self-linked entry so we'll
3602 * not get overrun under high load (as can happen with a
3603 * 5212 when ANI processing enables PHY error frames).
3605 * To insure the last descriptor is self-linked we create
3606 * each descriptor as self-linked and add it to the end. As
3607 * each additional descriptor is added the previous self-linked
3608 * entry is ``fixed'' naturally. This should be safe even
3609 * if DMA is happening. When processing RX interrupts we
3610 * never remove/process the last, self-linked, entry on the
3611 * descriptor list. This insures the hardware always has
3612 * someplace to write a new frame.
3615 ds->ds_link = bf->bf_daddr; /* link to self */
3616 ds->ds_data = bf->bf_segs[0].ds_addr;
3617 ath_hal_setuprxdesc(ah, ds
3618 , m->m_len /* buffer size */
3622 if (sc->sc_rxlink != NULL)
3623 *sc->sc_rxlink = bf->bf_daddr;
3624 sc->sc_rxlink = &ds->ds_link;
3629 * Extend 15-bit time stamp from rx descriptor to
3630 * a full 64-bit TSF using the specified TSF.
3632 static __inline u_int64_t
3633 ath_extend_tsf(u_int32_t rstamp, u_int64_t tsf)
3635 if ((tsf & 0x7fff) < rstamp)
3637 return ((tsf &~ 0x7fff) | rstamp);
3641 * Intercept management frames to collect beacon rssi data
3642 * and to do ibss merges.
3645 ath_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m,
3646 int subtype, int rssi, int nf)
3648 struct ieee80211vap *vap = ni->ni_vap;
3649 struct ath_softc *sc = vap->iv_ic->ic_ifp->if_softc;
3652 * Call up first so subsequent work can use information
3653 * potentially stored in the node (e.g. for ibss merge).
3655 ATH_VAP(vap)->av_recv_mgmt(ni, m, subtype, rssi, nf);
3657 case IEEE80211_FC0_SUBTYPE_BEACON:
3658 /* update rssi statistics for use by the hal */
3659 ATH_RSSI_LPF(sc->sc_halstats.ns_avgbrssi, rssi);
3660 if (sc->sc_syncbeacon &&
3661 ni == vap->iv_bss && vap->iv_state == IEEE80211_S_RUN) {
3663 * Resync beacon timers using the tsf of the beacon
3664 * frame we just received.
3666 ath_beacon_config(sc, vap);
3669 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
3670 if (vap->iv_opmode == IEEE80211_M_IBSS &&
3671 vap->iv_state == IEEE80211_S_RUN) {
3672 uint32_t rstamp = sc->sc_lastrs->rs_tstamp;
3673 u_int64_t tsf = ath_extend_tsf(rstamp,
3674 ath_hal_gettsf64(sc->sc_ah));
3676 * Handle ibss merge as needed; check the tsf on the
3677 * frame before attempting the merge. The 802.11 spec
3678 * says the station should change it's bssid to match
3679 * the oldest station with the same ssid, where oldest
3680 * is determined by the tsf. Note that hardware
3681 * reconfiguration happens through callback to
3682 * ath_newstate as the state machine will go from
3683 * RUN -> RUN when this happens.
3685 if (le64toh(ni->ni_tstamp.tsf) >= tsf) {
3686 DPRINTF(sc, ATH_DEBUG_STATE,
3687 "ibss merge, rstamp %u tsf %ju "
3688 "tstamp %ju\n", rstamp, (uintmax_t)tsf,
3689 (uintmax_t)ni->ni_tstamp.tsf);
3690 (void) ieee80211_ibss_merge(ni);
3698 * Set the default antenna.
3701 ath_setdefantenna(struct ath_softc *sc, u_int antenna)
3703 struct ath_hal *ah = sc->sc_ah;
3705 /* XXX block beacon interrupts */
3706 ath_hal_setdefantenna(ah, antenna);
3707 if (sc->sc_defant != antenna)
3708 sc->sc_stats.ast_ant_defswitch++;
3709 sc->sc_defant = antenna;
3710 sc->sc_rxotherant = 0;
3714 ath_rx_tap(struct ifnet *ifp, struct mbuf *m,
3715 const struct ath_rx_status *rs, u_int64_t tsf, int16_t nf)
3717 #define CHAN_HT20 htole32(IEEE80211_CHAN_HT20)
3718 #define CHAN_HT40U htole32(IEEE80211_CHAN_HT40U)
3719 #define CHAN_HT40D htole32(IEEE80211_CHAN_HT40D)
3720 #define CHAN_HT (CHAN_HT20|CHAN_HT40U|CHAN_HT40D)
3721 struct ath_softc *sc = ifp->if_softc;
3722 const HAL_RATE_TABLE *rt;
3725 rt = sc->sc_currates;
3726 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
3727 rix = rt->rateCodeToIndex[rs->rs_rate];
3728 sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix].ieeerate;
3729 sc->sc_rx_th.wr_flags = sc->sc_hwmap[rix].rxflags;
3730 #ifdef AH_SUPPORT_AR5416
3731 sc->sc_rx_th.wr_chan_flags &= ~CHAN_HT;
3732 if (sc->sc_rx_th.wr_rate & IEEE80211_RATE_MCS) { /* HT rate */
3733 struct ieee80211com *ic = ifp->if_l2com;
3735 if ((rs->rs_flags & HAL_RX_2040) == 0)
3736 sc->sc_rx_th.wr_chan_flags |= CHAN_HT20;
3737 else if (IEEE80211_IS_CHAN_HT40U(ic->ic_curchan))
3738 sc->sc_rx_th.wr_chan_flags |= CHAN_HT40U;
3740 sc->sc_rx_th.wr_chan_flags |= CHAN_HT40D;
3741 if ((rs->rs_flags & HAL_RX_GI) == 0)
3742 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
3745 sc->sc_rx_th.wr_tsf = htole64(ath_extend_tsf(rs->rs_tstamp, tsf));
3746 if (rs->rs_status & HAL_RXERR_CRC)
3747 sc->sc_rx_th.wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
3748 /* XXX propagate other error flags from descriptor */
3749 sc->sc_rx_th.wr_antnoise = nf;
3750 sc->sc_rx_th.wr_antsignal = nf + rs->rs_rssi;
3751 sc->sc_rx_th.wr_antenna = rs->rs_antenna;
3759 ath_handle_micerror(struct ieee80211com *ic,
3760 struct ieee80211_frame *wh, int keyix)
3762 struct ieee80211_node *ni;
3764 /* XXX recheck MIC to deal w/ chips that lie */
3765 /* XXX discard MIC errors on !data frames */
3766 ni = ieee80211_find_rxnode(ic, (const struct ieee80211_frame_min *) wh);
3768 ieee80211_notify_michael_failure(ni->ni_vap, wh, keyix);
3769 ieee80211_free_node(ni);
3774 ath_rx_proc(void *arg, int npending)
3776 #define PA2DESC(_sc, _pa) \
3777 ((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
3778 ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
3779 struct ath_softc *sc = arg;
3781 struct ifnet *ifp = sc->sc_ifp;
3782 struct ieee80211com *ic = ifp->if_l2com;
3783 struct ath_hal *ah = sc->sc_ah;
3784 struct ath_desc *ds;
3785 struct ath_rx_status *rs;
3787 struct ieee80211_node *ni;
3788 int len, type, ngood;
3794 DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending);
3796 nf = ath_hal_getchannoise(ah, sc->sc_curchan);
3797 sc->sc_stats.ast_rx_noise = nf;
3798 tsf = ath_hal_gettsf64(ah);
3800 bf = STAILQ_FIRST(&sc->sc_rxbuf);
3801 if (bf == NULL) { /* NB: shouldn't happen */
3802 if_printf(ifp, "%s: no buffer!\n", __func__);
3806 if (m == NULL) { /* NB: shouldn't happen */
3808 * If mbuf allocation failed previously there
3809 * will be no mbuf; try again to re-populate it.
3811 /* XXX make debug msg */
3812 if_printf(ifp, "%s: no mbuf!\n", __func__);
3813 STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list);
3817 if (ds->ds_link == bf->bf_daddr) {
3818 /* NB: never process the self-linked entry at the end */
3821 /* XXX sync descriptor memory */
3823 * Must provide the virtual address of the current
3824 * descriptor, the physical address, and the virtual
3825 * address of the next descriptor in the h/w chain.
3826 * This allows the HAL to look ahead to see if the
3827 * hardware is done with a descriptor by checking the
3828 * done bit in the following descriptor and the address
3829 * of the current descriptor the DMA engine is working
3830 * on. All this is necessary because of our use of
3831 * a self-linked list to avoid rx overruns.
3833 rs = &bf->bf_status.ds_rxstat;
3834 status = ath_hal_rxprocdesc(ah, ds,
3835 bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
3837 if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
3838 ath_printrxbuf(sc, bf, 0, status == HAL_OK);
3840 if (status == HAL_EINPROGRESS)
3842 STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list);
3843 if (rs->rs_status != 0) {
3844 if (rs->rs_status & HAL_RXERR_CRC)
3845 sc->sc_stats.ast_rx_crcerr++;
3846 if (rs->rs_status & HAL_RXERR_FIFO)
3847 sc->sc_stats.ast_rx_fifoerr++;
3848 if (rs->rs_status & HAL_RXERR_PHY) {
3849 sc->sc_stats.ast_rx_phyerr++;
3850 phyerr = rs->rs_phyerr & 0x1f;
3851 sc->sc_stats.ast_rx_phy[phyerr]++;
3852 goto rx_error; /* NB: don't count in ierrors */
3854 if (rs->rs_status & HAL_RXERR_DECRYPT) {
3856 * Decrypt error. If the error occurred
3857 * because there was no hardware key, then
3858 * let the frame through so the upper layers
3859 * can process it. This is necessary for 5210
3860 * parts which have no way to setup a ``clear''
3863 * XXX do key cache faulting
3865 if (rs->rs_keyix == HAL_RXKEYIX_INVALID)
3867 sc->sc_stats.ast_rx_badcrypt++;
3869 if (rs->rs_status & HAL_RXERR_MIC) {
3870 sc->sc_stats.ast_rx_badmic++;
3872 * Do minimal work required to hand off
3873 * the 802.11 header for notification.
3875 /* XXX frag's and qos frames */
3876 len = rs->rs_datalen;
3877 if (len >= sizeof (struct ieee80211_frame)) {
3878 bus_dmamap_sync(sc->sc_dmat,
3880 BUS_DMASYNC_POSTREAD);
3881 ath_handle_micerror(ic,
3882 mtod(m, struct ieee80211_frame *),
3884 rs->rs_keyix-32 : rs->rs_keyix);
3890 * Cleanup any pending partial frame.
3892 if (sc->sc_rxpending != NULL) {
3893 m_freem(sc->sc_rxpending);
3894 sc->sc_rxpending = NULL;
3897 * When a tap is present pass error frames
3898 * that have been requested. By default we
3899 * pass decrypt+mic errors but others may be
3900 * interesting (e.g. crc).
3902 if (ieee80211_radiotap_active(ic) &&
3903 (rs->rs_status & sc->sc_monpass)) {
3904 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
3905 BUS_DMASYNC_POSTREAD);
3906 /* NB: bpf needs the mbuf length setup */
3907 len = rs->rs_datalen;
3908 m->m_pkthdr.len = m->m_len = len;
3909 ath_rx_tap(ifp, m, rs, tsf, nf);
3910 ieee80211_radiotap_rx_all(ic, m);
3912 /* XXX pass MIC errors up for s/w reclaculation */
3917 * Sync and unmap the frame. At this point we're
3918 * committed to passing the mbuf somewhere so clear
3919 * bf_m; this means a new mbuf must be allocated
3920 * when the rx descriptor is setup again to receive
3923 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
3924 BUS_DMASYNC_POSTREAD);
3925 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3928 len = rs->rs_datalen;
3933 * Frame spans multiple descriptors; save
3934 * it for the next completed descriptor, it
3935 * will be used to construct a jumbogram.
3937 if (sc->sc_rxpending != NULL) {
3938 /* NB: max frame size is currently 2 clusters */
3939 sc->sc_stats.ast_rx_toobig++;
3940 m_freem(sc->sc_rxpending);
3942 m->m_pkthdr.rcvif = ifp;
3943 m->m_pkthdr.len = len;
3944 sc->sc_rxpending = m;
3946 } else if (sc->sc_rxpending != NULL) {
3948 * This is the second part of a jumbogram,
3949 * chain it to the first mbuf, adjust the
3950 * frame length, and clear the rxpending state.
3952 sc->sc_rxpending->m_next = m;
3953 sc->sc_rxpending->m_pkthdr.len += len;
3954 m = sc->sc_rxpending;
3955 sc->sc_rxpending = NULL;
3958 * Normal single-descriptor receive; setup
3959 * the rcvif and packet length.
3961 m->m_pkthdr.rcvif = ifp;
3962 m->m_pkthdr.len = len;
3966 sc->sc_stats.ast_ant_rx[rs->rs_antenna]++;
3969 * Populate the rx status block. When there are bpf
3970 * listeners we do the additional work to provide
3971 * complete status. Otherwise we fill in only the
3972 * material required by ieee80211_input. Note that
3973 * noise setting is filled in above.
3975 if (ieee80211_radiotap_active(ic))
3976 ath_rx_tap(ifp, m, rs, tsf, nf);
3979 * From this point on we assume the frame is at least
3980 * as large as ieee80211_frame_min; verify that.
3982 if (len < IEEE80211_MIN_LEN) {
3983 if (!ieee80211_radiotap_active(ic)) {
3984 DPRINTF(sc, ATH_DEBUG_RECV,
3985 "%s: short packet %d\n", __func__, len);
3986 sc->sc_stats.ast_rx_tooshort++;
3988 /* NB: in particular this captures ack's */
3989 ieee80211_radiotap_rx_all(ic, m);
3995 if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) {
3996 const HAL_RATE_TABLE *rt = sc->sc_currates;
3997 uint8_t rix = rt->rateCodeToIndex[rs->rs_rate];
3999 ieee80211_dump_pkt(ic, mtod(m, caddr_t), len,
4000 sc->sc_hwmap[rix].ieeerate, rs->rs_rssi);
4003 m_adj(m, -IEEE80211_CRC_LEN);
4006 * Locate the node for sender, track state, and then
4007 * pass the (referenced) node up to the 802.11 layer
4010 ni = ieee80211_find_rxnode_withkey(ic,
4011 mtod(m, const struct ieee80211_frame_min *),
4012 rs->rs_keyix == HAL_RXKEYIX_INVALID ?
4013 IEEE80211_KEYIX_NONE : rs->rs_keyix);
4016 * Sending station is known, dispatch directly.
4019 type = ieee80211_input(ni, m, rs->rs_rssi, nf);
4020 ieee80211_free_node(ni);
4022 * Arrange to update the last rx timestamp only for
4023 * frames from our ap when operating in station mode.
4024 * This assumes the rx key is always setup when
4027 if (ic->ic_opmode == IEEE80211_M_STA &&
4028 rs->rs_keyix != HAL_RXKEYIX_INVALID)
4031 type = ieee80211_input_all(ic, m, rs->rs_rssi, nf);
4034 * Track rx rssi and do any rx antenna management.
4036 ATH_RSSI_LPF(sc->sc_halstats.ns_avgrssi, rs->rs_rssi);
4037 if (sc->sc_diversity) {
4039 * When using fast diversity, change the default rx
4040 * antenna if diversity chooses the other antenna 3
4043 if (sc->sc_defant != rs->rs_antenna) {
4044 if (++sc->sc_rxotherant >= 3)
4045 ath_setdefantenna(sc, rs->rs_antenna);
4047 sc->sc_rxotherant = 0;
4049 if (sc->sc_softled) {
4051 * Blink for any data frame. Otherwise do a
4052 * heartbeat-style blink when idle. The latter
4053 * is mainly for station mode where we depend on
4054 * periodic beacon frames to trigger the poll event.
4056 if (type == IEEE80211_FC0_TYPE_DATA) {
4057 const HAL_RATE_TABLE *rt = sc->sc_currates;
4059 rt->rateCodeToIndex[rs->rs_rate]);
4060 } else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
4061 ath_led_event(sc, 0);
4064 STAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
4065 } while (ath_rxbuf_init(sc, bf) == 0);
4067 /* rx signal state monitoring */
4068 ath_hal_rxmonitor(ah, &sc->sc_halstats, sc->sc_curchan);
4070 sc->sc_lastrx = tsf;
4072 if ((ifp->if_flags & IFF_OACTIVE) == 0) {
4073 #ifdef IEEE80211_SUPPORT_SUPERG
4074 ieee80211_ff_age_all(ic, 100);
4076 if (!ifq_is_empty(&ifp->if_snd))
4083 ath_txq_init(struct ath_softc *sc, struct ath_txq *txq, int qnum)
4085 txq->axq_qnum = qnum;
4088 txq->axq_intrcnt = 0;
4089 txq->axq_link = NULL;
4090 STAILQ_INIT(&txq->axq_q);
4091 ATH_TXQ_LOCK_INIT(sc, txq);
4095 * Setup a h/w transmit queue.
4097 static struct ath_txq *
4098 ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
4100 #define N(a) (sizeof(a)/sizeof(a[0]))
4101 struct ath_hal *ah = sc->sc_ah;
4105 memset(&qi, 0, sizeof(qi));
4106 qi.tqi_subtype = subtype;
4107 qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
4108 qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
4109 qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
4111 * Enable interrupts only for EOL and DESC conditions.
4112 * We mark tx descriptors to receive a DESC interrupt
4113 * when a tx queue gets deep; otherwise waiting for the
4114 * EOL to reap descriptors. Note that this is done to
4115 * reduce interrupt load and this only defers reaping
4116 * descriptors, never transmitting frames. Aside from
4117 * reducing interrupts this also permits more concurrency.
4118 * The only potential downside is if the tx queue backs
4119 * up in which case the top half of the kernel may backup
4120 * due to a lack of tx descriptors.
4122 qi.tqi_qflags = HAL_TXQ_TXEOLINT_ENABLE | HAL_TXQ_TXDESCINT_ENABLE;
4123 qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
4126 * NB: don't print a message, this happens
4127 * normally on parts with too few tx queues
4131 if (qnum >= N(sc->sc_txq)) {
4132 device_printf(sc->sc_dev,
4133 "hal qnum %u out of range, max %zu!\n",
4134 qnum, N(sc->sc_txq));
4135 ath_hal_releasetxqueue(ah, qnum);
4138 if (!ATH_TXQ_SETUP(sc, qnum)) {
4139 ath_txq_init(sc, &sc->sc_txq[qnum], qnum);
4140 sc->sc_txqsetup |= 1<<qnum;
4142 return &sc->sc_txq[qnum];
4147 * Setup a hardware data transmit queue for the specified
4148 * access control. The hal may not support all requested
4149 * queues in which case it will return a reference to a
4150 * previously setup queue. We record the mapping from ac's
4151 * to h/w queues for use by ath_tx_start and also track
4152 * the set of h/w queues being used to optimize work in the
4153 * transmit interrupt handler and related routines.
4156 ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
4158 #define N(a) (sizeof(a)/sizeof(a[0]))
4159 struct ath_txq *txq;
4161 if (ac >= N(sc->sc_ac2q)) {
4162 device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
4163 ac, N(sc->sc_ac2q));
4166 txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
4169 sc->sc_ac2q[ac] = txq;
4177 * Update WME parameters for a transmit queue.
4180 ath_txq_update(struct ath_softc *sc, int ac)
4182 #define ATH_EXPONENT_TO_VALUE(v) ((1<<v)-1)
4183 #define ATH_TXOP_TO_US(v) (v<<5)
4184 struct ifnet *ifp = sc->sc_ifp;
4185 struct ieee80211com *ic = ifp->if_l2com;
4186 struct ath_txq *txq = sc->sc_ac2q[ac];
4187 struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
4188 struct ath_hal *ah = sc->sc_ah;
4191 ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
4192 #ifdef IEEE80211_SUPPORT_TDMA
4195 * AIFS is zero so there's no pre-transmit wait. The
4196 * burst time defines the slot duration and is configured
4197 * through net80211. The QCU is setup to not do post-xmit
4198 * back off, lockout all lower-priority QCU's, and fire
4199 * off the DMA beacon alert timer which is setup based
4200 * on the slot configuration.
4202 qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
4203 | HAL_TXQ_TXERRINT_ENABLE
4204 | HAL_TXQ_TXURNINT_ENABLE
4205 | HAL_TXQ_TXEOLINT_ENABLE
4207 | HAL_TXQ_BACKOFF_DISABLE
4208 | HAL_TXQ_ARB_LOCKOUT_GLOBAL
4212 qi.tqi_readyTime = sc->sc_tdmaslotlen;
4213 qi.tqi_burstTime = qi.tqi_readyTime;
4216 qi.tqi_qflags = HAL_TXQ_TXOKINT_ENABLE
4217 | HAL_TXQ_TXERRINT_ENABLE
4218 | HAL_TXQ_TXDESCINT_ENABLE
4219 | HAL_TXQ_TXURNINT_ENABLE
4221 qi.tqi_aifs = wmep->wmep_aifsn;
4222 qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
4223 qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
4224 qi.tqi_readyTime = 0;
4225 qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
4226 #ifdef IEEE80211_SUPPORT_TDMA
4230 DPRINTF(sc, ATH_DEBUG_RESET,
4231 "%s: Q%u qflags 0x%x aifs %u cwmin %u cwmax %u burstTime %u\n",
4232 __func__, txq->axq_qnum, qi.tqi_qflags,
4233 qi.tqi_aifs, qi.tqi_cwmin, qi.tqi_cwmax, qi.tqi_burstTime);
4235 if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
4236 if_printf(ifp, "unable to update hardware queue "
4237 "parameters for %s traffic!\n",
4238 ieee80211_wme_acnames[ac]);
4241 ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
4244 #undef ATH_TXOP_TO_US
4245 #undef ATH_EXPONENT_TO_VALUE
4249 * Callback from the 802.11 layer to update WME parameters.
4252 ath_wme_update(struct ieee80211com *ic)
4254 struct ath_softc *sc = ic->ic_ifp->if_softc;
4256 return !ath_txq_update(sc, WME_AC_BE) ||
4257 !ath_txq_update(sc, WME_AC_BK) ||
4258 !ath_txq_update(sc, WME_AC_VI) ||
4259 !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
4263 * Reclaim resources for a setup queue.
4266 ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
4269 ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
4270 ATH_TXQ_LOCK_DESTROY(txq);
4271 sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
4275 * Reclaim all tx queue resources.
4278 ath_tx_cleanup(struct ath_softc *sc)
4282 ATH_TXBUF_LOCK_DESTROY(sc);
4283 for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4284 if (ATH_TXQ_SETUP(sc, i))
4285 ath_tx_cleanupq(sc, &sc->sc_txq[i]);
4289 * Return h/w rate index for an IEEE rate (w/o basic rate bit)
4290 * using the current rates in sc_rixmap.
4293 ath_tx_findrix(const struct ath_softc *sc, uint8_t rate)
4295 int rix = sc->sc_rixmap[rate];
4296 /* NB: return lowest rix for invalid rate */
4297 return (rix == 0xff ? 0 : rix);
4301 * Reclaim mbuf resources. For fragmented frames we
4302 * need to claim each frag chained with m_nextpkt.
4305 ath_freetx(struct mbuf *m)
4310 next = m->m_nextpkt;
4311 m->m_nextpkt = NULL;
4313 } while ((m = next) != NULL);
4317 ath_tx_dmasetup(struct ath_softc *sc, struct ath_buf *bf, struct mbuf *m0)
4323 * Load the DMA map so any coalescing is done. This
4324 * also calculates the number of descriptors we need.
4326 error = bus_dmamap_load_mbuf_defrag(sc->sc_dmat, bf->bf_dmamap, &m0,
4327 bf->bf_segs, ATH_TXDESC,
4328 &bf->bf_nseg, BUS_DMA_NOWAIT);
4330 sc->sc_stats.ast_tx_busdma++;
4336 * Discard null packets.
4338 if (bf->bf_nseg == 0) { /* null packet, discard */
4339 sc->sc_stats.ast_tx_nodata++;
4343 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n",
4344 __func__, m0, m0->m_pkthdr.len);
4345 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
4352 ath_tx_handoff(struct ath_softc *sc, struct ath_txq *txq, struct ath_buf *bf)
4354 struct ath_hal *ah = sc->sc_ah;
4355 struct ath_desc *ds, *ds0;
4359 * Fillin the remainder of the descriptor info.
4361 ds0 = ds = bf->bf_desc;
4362 for (i = 0; i < bf->bf_nseg; i++, ds++) {
4363 ds->ds_data = bf->bf_segs[i].ds_addr;
4364 if (i == bf->bf_nseg - 1)
4367 ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
4368 ath_hal_filltxdesc(ah, ds
4369 , bf->bf_segs[i].ds_len /* segment length */
4370 , i == 0 /* first segment */
4371 , i == bf->bf_nseg - 1 /* last segment */
4372 , ds0 /* first descriptor */
4374 DPRINTF(sc, ATH_DEBUG_XMIT,
4375 "%s: %d: %08x %08x %08x %08x %08x %08x\n",
4376 __func__, i, ds->ds_link, ds->ds_data,
4377 ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]);
4380 * Insert the frame on the outbound list and pass it on
4381 * to the hardware. Multicast frames buffered for power
4382 * save stations and transmit from the CAB queue are stored
4383 * on a s/w only queue and loaded on to the CAB queue in
4384 * the SWBA handler since frames only go out on DTIM and
4385 * to avoid possible races.
4388 KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
4389 ("busy status 0x%x", bf->bf_flags));
4390 if (txq->axq_qnum != ATH_TXQ_SWQ) {
4391 #ifdef IEEE80211_SUPPORT_TDMA
4394 ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
4395 qbusy = ath_hal_txqenabled(ah, txq->axq_qnum);
4396 if (txq->axq_link == NULL) {
4398 * Be careful writing the address to TXDP. If
4399 * the tx q is enabled then this write will be
4400 * ignored. Normally this is not an issue but
4401 * when tdma is in use and the q is beacon gated
4402 * this race can occur. If the q is busy then
4403 * defer the work to later--either when another
4404 * packet comes along or when we prepare a beacon
4408 ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
4409 txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
4410 DPRINTF(sc, ATH_DEBUG_XMIT,
4411 "%s: TXDP[%u] = %p (%p) depth %d\n",
4412 __func__, txq->axq_qnum,
4413 (caddr_t)bf->bf_daddr, bf->bf_desc,
4416 txq->axq_flags |= ATH_TXQ_PUTPENDING;
4417 DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
4418 "%s: Q%u busy, defer enable\n", __func__,
4422 *txq->axq_link = bf->bf_daddr;
4423 DPRINTF(sc, ATH_DEBUG_XMIT,
4424 "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
4425 txq->axq_qnum, txq->axq_link,
4426 (caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth);
4427 if ((txq->axq_flags & ATH_TXQ_PUTPENDING) && !qbusy) {
4429 * The q was busy when we previously tried
4430 * to write the address of the first buffer
4431 * in the chain. Since it's not busy now
4432 * handle this chore. We are certain the
4433 * buffer at the front is the right one since
4434 * axq_link is NULL only when the buffer list
4437 ath_hal_puttxbuf(ah, txq->axq_qnum,
4438 STAILQ_FIRST(&txq->axq_q)->bf_daddr);
4439 txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
4440 DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
4441 "%s: Q%u restarted\n", __func__,
4446 ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
4447 if (txq->axq_link == NULL) {
4448 ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
4449 DPRINTF(sc, ATH_DEBUG_XMIT,
4450 "%s: TXDP[%u] = %p (%p) depth %d\n",
4451 __func__, txq->axq_qnum,
4452 (caddr_t)bf->bf_daddr, bf->bf_desc,
4455 *txq->axq_link = bf->bf_daddr;
4456 DPRINTF(sc, ATH_DEBUG_XMIT,
4457 "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
4458 txq->axq_qnum, txq->axq_link,
4459 (caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth);
4461 #endif /* IEEE80211_SUPPORT_TDMA */
4462 txq->axq_link = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
4463 ath_hal_txstart(ah, txq->axq_qnum);
4465 if (txq->axq_link != NULL) {
4466 struct ath_buf *last = ATH_TXQ_LAST(txq);
4467 struct ieee80211_frame *wh;
4469 /* mark previous frame */
4470 wh = mtod(last->bf_m, struct ieee80211_frame *);
4471 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
4472 bus_dmamap_sync(sc->sc_dmat, last->bf_dmamap,
4473 BUS_DMASYNC_PREWRITE);
4475 /* link descriptor */
4476 *txq->axq_link = bf->bf_daddr;
4478 ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
4479 txq->axq_link = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
4481 ATH_TXQ_UNLOCK(txq);
4485 ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf,
4488 struct ieee80211vap *vap = ni->ni_vap;
4489 struct ath_vap *avp = ATH_VAP(vap);
4490 struct ath_hal *ah = sc->sc_ah;
4491 struct ifnet *ifp = sc->sc_ifp;
4492 struct ieee80211com *ic = ifp->if_l2com;
4493 const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
4494 int error, iswep, ismcast, isfrag, ismrr;
4495 int keyix, hdrlen, pktlen, try0;
4496 u_int8_t rix, txrate, ctsrate;
4497 u_int8_t cix = 0xff; /* NB: silence compiler */
4498 struct ath_desc *ds;
4499 struct ath_txq *txq;
4500 struct ieee80211_frame *wh;
4501 u_int subtype, flags, ctsduration;
4503 const HAL_RATE_TABLE *rt;
4504 HAL_BOOL shortPreamble;
4505 struct ath_node *an;
4508 wh = mtod(m0, struct ieee80211_frame *);
4509 iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
4510 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
4511 isfrag = m0->m_flags & M_FRAG;
4512 hdrlen = ieee80211_anyhdrsize(wh);
4514 * Packet length must not include any
4515 * pad bytes; deduct them here.
4517 pktlen = m0->m_pkthdr.len - (hdrlen & 3);
4520 const struct ieee80211_cipher *cip;
4521 struct ieee80211_key *k;
4524 * Construct the 802.11 header+trailer for an encrypted
4525 * frame. The only reason this can fail is because of an
4526 * unknown or unsupported cipher/key type.
4528 k = ieee80211_crypto_encap(ni, m0);
4531 * This can happen when the key is yanked after the
4532 * frame was queued. Just discard the frame; the
4533 * 802.11 layer counts failures and provides
4534 * debugging/diagnostics.
4540 * Adjust the packet + header lengths for the crypto
4541 * additions and calculate the h/w key index. When
4542 * a s/w mic is done the frame will have had any mic
4543 * added to it prior to entry so m0->m_pkthdr.len will
4544 * account for it. Otherwise we need to add it to the
4548 hdrlen += cip->ic_header;
4549 pktlen += cip->ic_header + cip->ic_trailer;
4550 /* NB: frags always have any TKIP MIC done in s/w */
4551 if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag)
4552 pktlen += cip->ic_miclen;
4553 keyix = k->wk_keyix;
4555 /* packet header may have moved, reset our local pointer */
4556 wh = mtod(m0, struct ieee80211_frame *);
4557 } else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
4559 * Use station key cache slot, if assigned.
4561 keyix = ni->ni_ucastkey.wk_keyix;
4562 if (keyix == IEEE80211_KEYIX_NONE)
4563 keyix = HAL_TXKEYIX_INVALID;
4565 keyix = HAL_TXKEYIX_INVALID;
4567 pktlen += IEEE80211_CRC_LEN;
4570 * Load the DMA map so any coalescing is done. This
4571 * also calculates the number of descriptors we need.
4573 error = ath_tx_dmasetup(sc, bf, m0);
4577 bf->bf_node = ni; /* NB: held reference */
4578 m0 = bf->bf_m; /* NB: may have changed */
4579 wh = mtod(m0, struct ieee80211_frame *);
4581 /* setup descriptors */
4583 rt = sc->sc_currates;
4584 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
4587 * NB: the 802.11 layer marks whether or not we should
4588 * use short preamble based on the current mode and
4589 * negotiated parameters.
4591 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
4592 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
4593 shortPreamble = AH_TRUE;
4594 sc->sc_stats.ast_tx_shortpre++;
4596 shortPreamble = AH_FALSE;
4600 flags = HAL_TXDESC_CLRDMASK; /* XXX needed for crypto errs */
4601 ismrr = 0; /* default no multi-rate retry*/
4602 pri = M_WME_GETAC(m0); /* honor classification */
4603 /* XXX use txparams instead of fixed values */
4605 * Calculate Atheros packet type from IEEE80211 packet header,
4606 * setup for rate calculations, and select h/w transmit queue.
4608 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
4609 case IEEE80211_FC0_TYPE_MGT:
4610 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
4611 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
4612 atype = HAL_PKT_TYPE_BEACON;
4613 else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
4614 atype = HAL_PKT_TYPE_PROBE_RESP;
4615 else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
4616 atype = HAL_PKT_TYPE_ATIM;
4618 atype = HAL_PKT_TYPE_NORMAL; /* XXX */
4619 rix = an->an_mgmtrix;
4620 txrate = rt->info[rix].rateCode;
4622 txrate |= rt->info[rix].shortPreamble;
4623 try0 = ATH_TXMGTTRY;
4624 flags |= HAL_TXDESC_INTREQ; /* force interrupt */
4626 case IEEE80211_FC0_TYPE_CTL:
4627 atype = HAL_PKT_TYPE_PSPOLL; /* stop setting of duration */
4628 rix = an->an_mgmtrix;
4629 txrate = rt->info[rix].rateCode;
4631 txrate |= rt->info[rix].shortPreamble;
4632 try0 = ATH_TXMGTTRY;
4633 flags |= HAL_TXDESC_INTREQ; /* force interrupt */
4635 case IEEE80211_FC0_TYPE_DATA:
4636 atype = HAL_PKT_TYPE_NORMAL; /* default */
4638 * Data frames: multicast frames go out at a fixed rate,
4639 * EAPOL frames use the mgmt frame rate; otherwise consult
4640 * the rate control module for the rate to use.
4643 rix = an->an_mcastrix;
4644 txrate = rt->info[rix].rateCode;
4646 txrate |= rt->info[rix].shortPreamble;
4648 } else if (m0->m_flags & M_EAPOL) {
4649 /* XXX? maybe always use long preamble? */
4650 rix = an->an_mgmtrix;
4651 txrate = rt->info[rix].rateCode;
4653 txrate |= rt->info[rix].shortPreamble;
4654 try0 = ATH_TXMAXTRY; /* XXX?too many? */
4656 ath_rate_findrate(sc, an, shortPreamble, pktlen,
4657 &rix, &try0, &txrate);
4658 sc->sc_txrix = rix; /* for LED blinking */
4659 sc->sc_lastdatarix = rix; /* for fast frames */
4660 if (try0 != ATH_TXMAXTRY)
4663 if (cap->cap_wmeParams[pri].wmep_noackPolicy)
4664 flags |= HAL_TXDESC_NOACK;
4667 if_printf(ifp, "bogus frame type 0x%x (%s)\n",
4668 wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
4673 txq = sc->sc_ac2q[pri];
4676 * When servicing one or more stations in power-save mode
4677 * (or) if there is some mcast data waiting on the mcast
4678 * queue (to prevent out of order delivery) multicast
4679 * frames must be buffered until after the beacon.
4681 if (ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth))
4682 txq = &avp->av_mcastq;
4685 * Calculate miscellaneous flags.
4688 flags |= HAL_TXDESC_NOACK; /* no ack on broad/multicast */
4689 } else if (pktlen > vap->iv_rtsthreshold &&
4690 (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
4691 flags |= HAL_TXDESC_RTSENA; /* RTS based on frame length */
4692 cix = rt->info[rix].controlRate;
4693 sc->sc_stats.ast_tx_rts++;
4695 if (flags & HAL_TXDESC_NOACK) /* NB: avoid double counting */
4696 sc->sc_stats.ast_tx_noack++;
4697 #ifdef IEEE80211_SUPPORT_TDMA
4698 if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) {
4699 DPRINTF(sc, ATH_DEBUG_TDMA,
4700 "%s: discard frame, ACK required w/ TDMA\n", __func__);
4701 sc->sc_stats.ast_tdma_ack++;
4708 * If 802.11g protection is enabled, determine whether
4709 * to use RTS/CTS or just CTS. Note that this is only
4710 * done for OFDM unicast frames.
4712 if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
4713 rt->info[rix].phy == IEEE80211_T_OFDM &&
4714 (flags & HAL_TXDESC_NOACK) == 0) {
4715 /* XXX fragments must use CCK rates w/ protection */
4716 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
4717 flags |= HAL_TXDESC_RTSENA;
4718 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
4719 flags |= HAL_TXDESC_CTSENA;
4722 * For frags it would be desirable to use the
4723 * highest CCK rate for RTS/CTS. But stations
4724 * farther away may detect it at a lower CCK rate
4725 * so use the configured protection rate instead
4728 cix = rt->info[sc->sc_protrix].controlRate;
4730 cix = rt->info[sc->sc_protrix].controlRate;
4731 sc->sc_stats.ast_tx_protect++;
4735 * Calculate duration. This logically belongs in the 802.11
4736 * layer but it lacks sufficient information to calculate it.
4738 if ((flags & HAL_TXDESC_NOACK) == 0 &&
4739 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
4742 dur = rt->info[rix].spAckDuration;
4744 dur = rt->info[rix].lpAckDuration;
4745 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
4746 dur += dur; /* additional SIFS+ACK */
4747 KASSERT(m0->m_nextpkt != NULL, ("no fragment"));
4749 * Include the size of next fragment so NAV is
4750 * updated properly. The last fragment uses only
4753 dur += ath_hal_computetxtime(ah, rt,
4754 m0->m_nextpkt->m_pkthdr.len,
4755 rix, shortPreamble);
4759 * Force hardware to use computed duration for next
4760 * fragment by disabling multi-rate retry which updates
4761 * duration based on the multi-rate duration table.
4764 try0 = ATH_TXMGTTRY; /* XXX? */
4766 *(u_int16_t *)wh->i_dur = htole16(dur);
4770 * Calculate RTS/CTS rate and duration if needed.
4773 if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
4775 * CTS transmit rate is derived from the transmit rate
4776 * by looking in the h/w rate table. We must also factor
4777 * in whether or not a short preamble is to be used.
4779 /* NB: cix is set above where RTS/CTS is enabled */
4780 KASSERT(cix != 0xff, ("cix not setup"));
4781 ctsrate = rt->info[cix].rateCode;
4783 * Compute the transmit duration based on the frame
4784 * size and the size of an ACK frame. We call into the
4785 * HAL to do the computation since it depends on the
4786 * characteristics of the actual PHY being used.
4788 * NB: CTS is assumed the same size as an ACK so we can
4789 * use the precalculated ACK durations.
4791 if (shortPreamble) {
4792 ctsrate |= rt->info[cix].shortPreamble;
4793 if (flags & HAL_TXDESC_RTSENA) /* SIFS + CTS */
4794 ctsduration += rt->info[cix].spAckDuration;
4795 ctsduration += ath_hal_computetxtime(ah,
4796 rt, pktlen, rix, AH_TRUE);
4797 if ((flags & HAL_TXDESC_NOACK) == 0) /* SIFS + ACK */
4798 ctsduration += rt->info[rix].spAckDuration;
4800 if (flags & HAL_TXDESC_RTSENA) /* SIFS + CTS */
4801 ctsduration += rt->info[cix].lpAckDuration;
4802 ctsduration += ath_hal_computetxtime(ah,
4803 rt, pktlen, rix, AH_FALSE);
4804 if ((flags & HAL_TXDESC_NOACK) == 0) /* SIFS + ACK */
4805 ctsduration += rt->info[rix].lpAckDuration;
4808 * Must disable multi-rate retry when using RTS/CTS.
4811 try0 = ATH_TXMGTTRY; /* XXX */
4816 * At this point we are committed to sending the frame
4817 * and we don't need to look at m_nextpkt; clear it in
4818 * case this frame is part of frag chain.
4820 m0->m_nextpkt = NULL;
4822 if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
4823 ieee80211_dump_pkt(ic, mtod(m0, const uint8_t *), m0->m_len,
4824 sc->sc_hwmap[rix].ieeerate, -1);
4826 if (ieee80211_radiotap_active_vap(vap)) {
4827 u_int64_t tsf = ath_hal_gettsf64(ah);
4829 sc->sc_tx_th.wt_tsf = htole64(tsf);
4830 sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
4832 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
4834 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
4835 sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
4836 sc->sc_tx_th.wt_txpower = ni->ni_txpower;
4837 sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
4839 ieee80211_radiotap_tx(vap, m0);
4843 * Determine if a tx interrupt should be generated for
4844 * this descriptor. We take a tx interrupt to reap
4845 * descriptors when the h/w hits an EOL condition or
4846 * when the descriptor is specifically marked to generate
4847 * an interrupt. We periodically mark descriptors in this
4848 * way to insure timely replenishing of the supply needed
4849 * for sending frames. Defering interrupts reduces system
4850 * load and potentially allows more concurrent work to be
4851 * done but if done to aggressively can cause senders to
4854 * NB: use >= to deal with sc_txintrperiod changing
4855 * dynamically through sysctl.
4857 if (flags & HAL_TXDESC_INTREQ) {
4858 txq->axq_intrcnt = 0;
4859 } else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
4860 flags |= HAL_TXDESC_INTREQ;
4861 txq->axq_intrcnt = 0;
4865 * Formulate first tx descriptor with tx controls.
4867 /* XXX check return value? */
4868 ath_hal_setuptxdesc(ah, ds
4869 , pktlen /* packet length */
4870 , hdrlen /* header length */
4871 , atype /* Atheros packet type */
4872 , ni->ni_txpower /* txpower */
4873 , txrate, try0 /* series 0 rate/tries */
4874 , keyix /* key cache index */
4875 , sc->sc_txantenna /* antenna mode */
4877 , ctsrate /* rts/cts rate */
4878 , ctsduration /* rts/cts duration */
4880 bf->bf_txflags = flags;
4882 * Setup the multi-rate retry state only when we're
4883 * going to use it. This assumes ath_hal_setuptxdesc
4884 * initializes the descriptors (so we don't have to)
4885 * when the hardware supports multi-rate retry and
4889 ath_rate_setupxtxdesc(sc, an, ds, shortPreamble, rix);
4891 ath_tx_handoff(sc, txq, bf);
4896 * Process completed xmit descriptors from the specified queue.
4899 ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
4901 struct ath_hal *ah = sc->sc_ah;
4902 struct ifnet *ifp = sc->sc_ifp;
4903 struct ieee80211com *ic = ifp->if_l2com;
4904 struct ath_buf *bf, *last;
4905 struct ath_desc *ds, *ds0;
4906 struct ath_tx_status *ts;
4907 struct ieee80211_node *ni;
4908 struct ath_node *an;
4909 int sr, lr, pri, nacked;
4912 DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
4913 __func__, txq->axq_qnum,
4914 (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
4919 txq->axq_intrcnt = 0; /* reset periodic desc intr count */
4920 bf = STAILQ_FIRST(&txq->axq_q);
4922 ATH_TXQ_UNLOCK(txq);
4925 ds0 = &bf->bf_desc[0];
4926 ds = &bf->bf_desc[bf->bf_nseg - 1];
4927 ts = &bf->bf_status.ds_txstat;
4928 status = ath_hal_txprocdesc(ah, ds, ts);
4930 if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
4931 ath_printtxbuf(sc, bf, txq->axq_qnum, 0,
4934 if (status == HAL_EINPROGRESS) {
4935 ATH_TXQ_UNLOCK(txq);
4938 ATH_TXQ_REMOVE_HEAD(txq, bf_list);
4939 #ifdef IEEE80211_SUPPORT_TDMA
4940 if (txq->axq_depth > 0) {
4942 * More frames follow. Mark the buffer busy
4943 * so it's not re-used while the hardware may
4944 * still re-read the link field in the descriptor.
4946 bf->bf_flags |= ATH_BUF_BUSY;
4949 if (txq->axq_depth == 0)
4951 txq->axq_link = NULL;
4952 ATH_TXQ_UNLOCK(txq);
4957 if (ts->ts_status == 0) {
4958 u_int8_t txant = ts->ts_antenna;
4959 sc->sc_stats.ast_ant_tx[txant]++;
4960 sc->sc_ant_tx[txant]++;
4961 if (ts->ts_finaltsi != 0)
4962 sc->sc_stats.ast_tx_altrate++;
4963 pri = M_WME_GETAC(bf->bf_m);
4964 if (pri >= WME_AC_VO)
4965 ic->ic_wme.wme_hipri_traffic++;
4966 if ((bf->bf_txflags & HAL_TXDESC_NOACK) == 0)
4967 ni->ni_inact = ni->ni_inact_reload;
4969 if (ts->ts_status & HAL_TXERR_XRETRY)
4970 sc->sc_stats.ast_tx_xretries++;
4971 if (ts->ts_status & HAL_TXERR_FIFO)
4972 sc->sc_stats.ast_tx_fifoerr++;
4973 if (ts->ts_status & HAL_TXERR_FILT)
4974 sc->sc_stats.ast_tx_filtered++;
4975 if (bf->bf_m->m_flags & M_FF)
4976 sc->sc_stats.ast_ff_txerr++;
4978 sr = ts->ts_shortretry;
4979 lr = ts->ts_longretry;
4980 sc->sc_stats.ast_tx_shortretry += sr;
4981 sc->sc_stats.ast_tx_longretry += lr;
4983 * Hand the descriptor to the rate control algorithm.
4985 if ((ts->ts_status & HAL_TXERR_FILT) == 0 &&
4986 (bf->bf_txflags & HAL_TXDESC_NOACK) == 0) {
4988 * If frame was ack'd update statistics,
4989 * including the last rx time used to
4990 * workaround phantom bmiss interrupts.
4992 if (ts->ts_status == 0) {
4994 sc->sc_stats.ast_tx_rssi = ts->ts_rssi;
4995 ATH_RSSI_LPF(sc->sc_halstats.ns_avgtxrssi,
4998 ath_rate_tx_complete(sc, an, bf);
5001 * Do any tx complete callback. Note this must
5002 * be done before releasing the node reference.
5004 if (bf->bf_m->m_flags & M_TXCB)
5005 ieee80211_process_callback(ni, bf->bf_m,
5006 (bf->bf_txflags & HAL_TXDESC_NOACK) == 0 ?
5007 ts->ts_status : HAL_TXERR_XRETRY);
5008 ieee80211_free_node(ni);
5010 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
5011 BUS_DMASYNC_POSTWRITE);
5012 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
5019 last = STAILQ_LAST(&sc->sc_txbuf, ath_buf, bf_list);
5021 last->bf_flags &= ~ATH_BUF_BUSY;
5022 STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
5023 ATH_TXBUF_UNLOCK(sc);
5025 #ifdef IEEE80211_SUPPORT_SUPERG
5027 * Flush fast-frame staging queue when traffic slows.
5029 if (txq->axq_depth <= 1)
5030 ieee80211_ff_flush(ic, txq->axq_ac);
5036 txqactive(struct ath_hal *ah, int qnum)
5038 u_int32_t txqs = 1<<qnum;
5039 ath_hal_gettxintrtxqs(ah, &txqs);
5040 return (txqs & (1<<qnum));
5044 * Deferred processing of transmit interrupt; special-cased
5045 * for a single hardware transmit queue (e.g. 5210 and 5211).
5048 ath_tx_proc_q0(void *arg, int npending)
5050 struct ath_softc *sc = arg;
5051 struct ifnet *ifp = sc->sc_ifp;
5053 if (txqactive(sc->sc_ah, 0) && ath_tx_processq(sc, &sc->sc_txq[0]))
5054 sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
5055 if (txqactive(sc->sc_ah, sc->sc_cabq->axq_qnum))
5056 ath_tx_processq(sc, sc->sc_cabq);
5057 ifp->if_flags &= ~IFF_OACTIVE;
5058 sc->sc_wd_timer = 0;
5061 ath_led_event(sc, sc->sc_txrix);
5067 * Deferred processing of transmit interrupt; special-cased
5068 * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
5071 ath_tx_proc_q0123(void *arg, int npending)
5073 struct ath_softc *sc = arg;
5074 struct ifnet *ifp = sc->sc_ifp;
5078 * Process each active queue.
5081 if (txqactive(sc->sc_ah, 0))
5082 nacked += ath_tx_processq(sc, &sc->sc_txq[0]);
5083 if (txqactive(sc->sc_ah, 1))
5084 nacked += ath_tx_processq(sc, &sc->sc_txq[1]);
5085 if (txqactive(sc->sc_ah, 2))
5086 nacked += ath_tx_processq(sc, &sc->sc_txq[2]);
5087 if (txqactive(sc->sc_ah, 3))
5088 nacked += ath_tx_processq(sc, &sc->sc_txq[3]);
5089 if (txqactive(sc->sc_ah, sc->sc_cabq->axq_qnum))
5090 ath_tx_processq(sc, sc->sc_cabq);
5092 sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
5094 ifp->if_flags &= ~IFF_OACTIVE;
5095 sc->sc_wd_timer = 0;
5098 ath_led_event(sc, sc->sc_txrix);
5104 * Deferred processing of transmit interrupt.
5107 ath_tx_proc(void *arg, int npending)
5109 struct ath_softc *sc = arg;
5110 struct ifnet *ifp = sc->sc_ifp;
5114 * Process each active queue.
5117 for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
5118 if (ATH_TXQ_SETUP(sc, i) && txqactive(sc->sc_ah, i))
5119 nacked += ath_tx_processq(sc, &sc->sc_txq[i]);
5121 sc->sc_lastrx = ath_hal_gettsf64(sc->sc_ah);
5123 ifp->if_flags &= ~IFF_OACTIVE;
5124 sc->sc_wd_timer = 0;
5127 ath_led_event(sc, sc->sc_txrix);
5133 ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
5136 struct ath_hal *ah = sc->sc_ah;
5138 struct ieee80211_node *ni;
5143 * NB: this assumes output has been stopped and
5144 * we do not need to block ath_tx_proc
5147 bf = STAILQ_LAST(&sc->sc_txbuf, ath_buf, bf_list);
5149 bf->bf_flags &= ~ATH_BUF_BUSY;
5150 ATH_TXBUF_UNLOCK(sc);
5151 for (ix = 0;; ix++) {
5153 bf = STAILQ_FIRST(&txq->axq_q);
5155 txq->axq_link = NULL;
5156 ATH_TXQ_UNLOCK(txq);
5159 ATH_TXQ_REMOVE_HEAD(txq, bf_list);
5160 ATH_TXQ_UNLOCK(txq);
5162 if (sc->sc_debug & ATH_DEBUG_RESET) {
5163 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
5165 ath_printtxbuf(sc, bf, txq->axq_qnum, ix,
5166 ath_hal_txprocdesc(ah, bf->bf_desc,
5167 &bf->bf_status.ds_txstat) == HAL_OK);
5168 ieee80211_dump_pkt(ic, mtod(bf->bf_m, const uint8_t *),
5169 bf->bf_m->m_len, 0, -1);
5171 #endif /* ATH_DEBUG */
5172 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
5177 * Do any callback and reclaim the node reference.
5179 if (bf->bf_m->m_flags & M_TXCB)
5180 ieee80211_process_callback(ni, bf->bf_m, -1);
5181 ieee80211_free_node(ni);
5185 bf->bf_flags &= ~ATH_BUF_BUSY;
5188 STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
5189 ATH_TXBUF_UNLOCK(sc);
5194 ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
5196 struct ath_hal *ah = sc->sc_ah;
5198 DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
5199 __func__, txq->axq_qnum,
5200 (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
5202 (void) ath_hal_stoptxdma(ah, txq->axq_qnum);
5206 * Drain the transmit queues and reclaim resources.
5209 ath_draintxq(struct ath_softc *sc)
5211 struct ath_hal *ah = sc->sc_ah;
5212 struct ifnet *ifp = sc->sc_ifp;
5215 /* XXX return value */
5216 if (!sc->sc_invalid) {
5217 /* don't touch the hardware if marked invalid */
5218 DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
5219 __func__, sc->sc_bhalq,
5220 (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq),
5222 (void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
5223 for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
5224 if (ATH_TXQ_SETUP(sc, i))
5225 ath_tx_stopdma(sc, &sc->sc_txq[i]);
5227 for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
5228 if (ATH_TXQ_SETUP(sc, i))
5229 ath_tx_draintxq(sc, &sc->sc_txq[i]);
5231 if (sc->sc_debug & ATH_DEBUG_RESET) {
5232 struct ath_buf *bf = STAILQ_FIRST(&sc->sc_bbuf);
5233 if (bf != NULL && bf->bf_m != NULL) {
5234 ath_printtxbuf(sc, bf, sc->sc_bhalq, 0,
5235 ath_hal_txprocdesc(ah, bf->bf_desc,
5236 &bf->bf_status.ds_txstat) == HAL_OK);
5237 ieee80211_dump_pkt(ifp->if_l2com,
5238 mtod(bf->bf_m, const uint8_t *), bf->bf_m->m_len,
5242 #endif /* ATH_DEBUG */
5243 ifp->if_flags &= ~IFF_OACTIVE;
5244 sc->sc_wd_timer = 0;
5248 * Disable the receive h/w in preparation for a reset.
5251 ath_stoprecv(struct ath_softc *sc)
5253 #define PA2DESC(_sc, _pa) \
5254 ((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
5255 ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
5256 struct ath_hal *ah = sc->sc_ah;
5258 ath_hal_stoppcurecv(ah); /* disable PCU */
5259 ath_hal_setrxfilter(ah, 0); /* clear recv filter */
5260 ath_hal_stopdmarecv(ah); /* disable DMA engine */
5261 DELAY(3000); /* 3ms is long enough for 1 frame */
5263 if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) {
5267 kprintf("%s: rx queue %p, link %p\n", __func__,
5268 (caddr_t)(uintptr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink);
5270 STAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
5271 struct ath_desc *ds = bf->bf_desc;
5272 struct ath_rx_status *rs = &bf->bf_status.ds_rxstat;
5273 HAL_STATUS status = ath_hal_rxprocdesc(ah, ds,
5274 bf->bf_daddr, PA2DESC(sc, ds->ds_link), rs);
5275 if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL))
5276 ath_printrxbuf(sc, bf, ix, status == HAL_OK);
5281 if (sc->sc_rxpending != NULL) {
5282 m_freem(sc->sc_rxpending);
5283 sc->sc_rxpending = NULL;
5285 sc->sc_rxlink = NULL; /* just in case */
5290 * Enable the receive h/w following a reset.
5293 ath_startrecv(struct ath_softc *sc)
5295 struct ath_hal *ah = sc->sc_ah;
5298 sc->sc_rxlink = NULL;
5299 sc->sc_rxpending = NULL;
5300 STAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
5301 int error = ath_rxbuf_init(sc, bf);
5303 DPRINTF(sc, ATH_DEBUG_RECV,
5304 "%s: ath_rxbuf_init failed %d\n",
5310 bf = STAILQ_FIRST(&sc->sc_rxbuf);
5311 ath_hal_putrxbuf(ah, bf->bf_daddr);
5312 ath_hal_rxena(ah); /* enable recv descriptors */
5313 ath_mode_init(sc); /* set filters, etc. */
5314 ath_hal_startpcurecv(ah); /* re-enable PCU/DMA engine */
5319 * Update internal state after a channel change.
5322 ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
5324 enum ieee80211_phymode mode;
5327 * Change channels and update the h/w rate map
5328 * if we're switching; e.g. 11a to 11b/g.
5330 mode = ieee80211_chan2mode(chan);
5331 if (mode != sc->sc_curmode)
5332 ath_setcurmode(sc, mode);
5333 sc->sc_curchan = chan;
5337 * Set/change channels. If the channel is really being changed,
5338 * it's done by reseting the chip. To accomplish this we must
5339 * first cleanup any pending DMA, then restart stuff after a la
5343 ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
5345 struct ifnet *ifp = sc->sc_ifp;
5346 struct ieee80211com *ic = ifp->if_l2com;
5347 struct ath_hal *ah = sc->sc_ah;
5349 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz, flags 0x%x)\n",
5350 __func__, ieee80211_chan2ieee(ic, chan),
5351 chan->ic_freq, chan->ic_flags);
5352 if (chan != sc->sc_curchan) {
5355 * To switch channels clear any pending DMA operations;
5356 * wait long enough for the RX fifo to drain, reset the
5357 * hardware at the new frequency, and then re-enable
5358 * the relevant bits of the h/w.
5360 ath_hal_intrset(ah, 0); /* disable interrupts */
5361 ath_draintxq(sc); /* clear pending tx frames */
5362 ath_stoprecv(sc); /* turn off frame recv */
5363 if (!ath_hal_reset(ah, sc->sc_opmode, chan, AH_TRUE, &status)) {
5364 if_printf(ifp, "%s: unable to reset "
5365 "channel %u (%u MHz, flags 0x%x), hal status %u\n",
5366 __func__, ieee80211_chan2ieee(ic, chan),
5367 chan->ic_freq, chan->ic_flags, status);
5370 sc->sc_diversity = ath_hal_getdiversity(ah);
5373 * Re-enable rx framework.
5375 if (ath_startrecv(sc) != 0) {
5376 if_printf(ifp, "%s: unable to restart recv logic\n",
5382 * Change channels and update the h/w rate map
5383 * if we're switching; e.g. 11a to 11b/g.
5385 ath_chan_change(sc, chan);
5388 * Re-enable interrupts.
5390 ath_hal_intrset(ah, sc->sc_imask);
5396 * Periodically recalibrate the PHY to account
5397 * for temperature/environment changes.
5400 ath_calibrate(void *arg)
5402 struct ath_softc *sc = arg;
5403 struct ath_hal *ah = sc->sc_ah;
5404 struct ifnet *ifp = sc->sc_ifp;
5405 struct ieee80211com *ic = ifp->if_l2com;
5406 HAL_BOOL longCal, isCalDone;
5411 if (ic->ic_flags & IEEE80211_F_SCAN) /* defer, off channel */
5413 longCal = (ticks - sc->sc_lastlongcal >= ath_longcalinterval*hz);
5415 sc->sc_stats.ast_per_cal++;
5416 if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
5418 * Rfgain is out of bounds, reset the chip
5419 * to load new gain values.
5421 DPRINTF(sc, ATH_DEBUG_CALIBRATE,
5422 "%s: rfgain change\n", __func__);
5423 sc->sc_stats.ast_per_rfgain++;
5427 * If this long cal is after an idle period, then
5428 * reset the data collection state so we start fresh.
5430 if (sc->sc_resetcal) {
5431 (void) ath_hal_calreset(ah, sc->sc_curchan);
5432 sc->sc_lastcalreset = ticks;
5433 sc->sc_resetcal = 0;
5436 if (ath_hal_calibrateN(ah, sc->sc_curchan, longCal, &isCalDone)) {
5439 * Calibrate noise floor data again in case of change.
5441 ath_hal_process_noisefloor(ah);
5444 DPRINTF(sc, ATH_DEBUG_ANY,
5445 "%s: calibration of channel %u failed\n",
5446 __func__, sc->sc_curchan->ic_freq);
5447 sc->sc_stats.ast_per_calfail++;
5452 * Use a shorter interval to potentially collect multiple
5453 * data samples required to complete calibration. Once
5454 * we're told the work is done we drop back to a longer
5455 * interval between requests. We're more aggressive doing
5456 * work when operating as an AP to improve operation right
5459 nextcal = (1000*ath_shortcalinterval)/hz;
5460 if (sc->sc_opmode != HAL_M_HOSTAP)
5463 nextcal = ath_longcalinterval*hz;
5464 sc->sc_lastlongcal = ticks;
5465 if (sc->sc_lastcalreset == 0)