1 /* $NetBSD: ath.c,v 1.106 2009/08/02 13:26:33 jmcneill Exp $ */
4 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15 * redistribution must be conditioned upon including a substantially
16 * similar Disclaimer requirement for further binary redistribution.
17 * 3. Neither the names of the above-listed copyright holders nor the names
18 * of any contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL") version 2 as published by the Free
23 * Software Foundation.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
29 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
30 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
31 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
36 * THE POSSIBILITY OF SUCH DAMAGES.
39 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/sys/dev/ath/if_ath.c,v 1.104 2005/09/16 10:09:23 ru Exp $");
44 __KERNEL_RCSID(0, "$NetBSD: ath.c,v 1.106 2009/08/02 13:26:33 jmcneill Exp $");
48 * Driver for the Atheros Wireless LAN controller.
50 * This software is derived from work of Atsushi Onoe; his contribution
51 * is greatly appreciated.
58 #endif /* __NetBSD__ */
60 #include <sys/param.h>
61 #include <sys/reboot.h>
62 #include <sys/systm.h>
63 #include <sys/types.h>
64 #include <sys/sysctl.h>
66 #include <sys/malloc.h>
67 #include <sys/kernel.h>
68 #include <sys/socket.h>
69 #include <sys/sockio.h>
70 #include <sys/errno.h>
71 #include <sys/callout.h>
73 #include <sys/endian.h>
76 #include <net/if_dl.h>
77 #include <net/if_media.h>
78 #include <net/if_types.h>
79 #include <net/if_arp.h>
80 #include <net/if_ether.h>
81 #include <net/if_llc.h>
83 #include <net80211/ieee80211_netbsd.h>
84 #include <net80211/ieee80211_var.h>
91 #include <netinet/in.h>
94 #include <sys/device.h>
95 #include <dev/ic/ath_netbsd.h>
98 #include <dev/ic/athvar.h>
100 #include "ah_devid.h" /* XXX for softled */
104 #include <dev/ath/ath_tx99/ath_tx99.h>
107 /* unaligned little endian access */
108 #define LE_READ_2(p) \
110 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8)))
111 #define LE_READ_4(p) \
113 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8) | \
114 (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24)))
122 #ifdef AH_NEED_DESC_SWAP
123 #define HTOAH32(x) htole32(x)
125 #define HTOAH32(x) (x)
128 static int ath_ifinit(struct ifnet
*);
129 static int ath_init(struct ath_softc
*);
130 static void ath_stop_locked(struct ifnet
*, int);
131 static void ath_stop(struct ifnet
*, int);
132 static void ath_start(struct ifnet
*);
133 static int ath_media_change(struct ifnet
*);
134 static void ath_watchdog(struct ifnet
*);
135 static int ath_ioctl(struct ifnet
*, u_long
, void *);
136 static void ath_fatal_proc(void *, int);
137 static void ath_rxorn_proc(void *, int);
138 static void ath_bmiss_proc(void *, int);
139 static void ath_radar_proc(void *, int);
140 static int ath_key_alloc(struct ieee80211com
*,
141 const struct ieee80211_key
*,
142 ieee80211_keyix
*, ieee80211_keyix
*);
143 static int ath_key_delete(struct ieee80211com
*,
144 const struct ieee80211_key
*);
145 static int ath_key_set(struct ieee80211com
*, const struct ieee80211_key
*,
146 const u_int8_t mac
[IEEE80211_ADDR_LEN
]);
147 static void ath_key_update_begin(struct ieee80211com
*);
148 static void ath_key_update_end(struct ieee80211com
*);
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_setup(struct ath_softc
*, struct ath_buf
*);
155 static void ath_beacon_proc(void *, int);
156 static void ath_bstuck_proc(void *, int);
157 static void ath_beacon_free(struct ath_softc
*);
158 static void ath_beacon_config(struct ath_softc
*);
159 static void ath_descdma_cleanup(struct ath_softc
*sc
,
160 struct ath_descdma
*, ath_bufhead
*);
161 static int ath_desc_alloc(struct ath_softc
*);
162 static void ath_desc_free(struct ath_softc
*);
163 static struct ieee80211_node
*ath_node_alloc(struct ieee80211_node_table
*);
164 static void ath_node_free(struct ieee80211_node
*);
165 static u_int8_t
ath_node_getrssi(const struct ieee80211_node
*);
166 static int ath_rxbuf_init(struct ath_softc
*, struct ath_buf
*);
167 static void ath_recv_mgmt(struct ieee80211com
*ic
, struct mbuf
*m
,
168 struct ieee80211_node
*ni
,
169 int subtype
, int rssi
, u_int32_t rstamp
);
170 static void ath_setdefantenna(struct ath_softc
*, u_int
);
171 static void ath_rx_proc(void *, int);
172 static struct ath_txq
*ath_txq_setup(struct ath_softc
*, int qtype
, int subtype
);
173 static int ath_tx_setup(struct ath_softc
*, int, int);
174 static int ath_wme_update(struct ieee80211com
*);
175 static void ath_tx_cleanupq(struct ath_softc
*, struct ath_txq
*);
176 static void ath_tx_cleanup(struct ath_softc
*);
177 static int ath_tx_start(struct ath_softc
*, struct ieee80211_node
*,
178 struct ath_buf
*, struct mbuf
*);
179 static void ath_tx_proc_q0(void *, int);
180 static void ath_tx_proc_q0123(void *, int);
181 static void ath_tx_proc(void *, int);
182 static int ath_chan_set(struct ath_softc
*, struct ieee80211_channel
*);
183 static void ath_draintxq(struct ath_softc
*);
184 static void ath_stoprecv(struct ath_softc
*);
185 static int ath_startrecv(struct ath_softc
*);
186 static void ath_chan_change(struct ath_softc
*, struct ieee80211_channel
*);
187 static void ath_next_scan(void *);
188 static void ath_calibrate(void *);
189 static int ath_newstate(struct ieee80211com
*, enum ieee80211_state
, int);
190 static void ath_setup_stationkey(struct ieee80211_node
*);
191 static void ath_newassoc(struct ieee80211_node
*, int);
192 static int ath_getchannels(struct ath_softc
*, u_int cc
,
193 HAL_BOOL outdoor
, HAL_BOOL xchanmode
);
194 static void ath_led_event(struct ath_softc
*, int);
195 static void ath_update_txpow(struct ath_softc
*);
196 static void ath_freetx(struct mbuf
*);
197 static void ath_restore_diversity(struct ath_softc
*);
199 static int ath_rate_setup(struct ath_softc
*, u_int mode
);
200 static void ath_setcurmode(struct ath_softc
*, enum ieee80211_phymode
);
203 static void ath_bpfattach(struct ath_softc
*);
205 static void ath_announce(struct ath_softc
*);
207 int ath_dwelltime
= 200; /* 5 channels/second */
208 int ath_calinterval
= 30; /* calibrate every 30 secs */
209 int ath_outdoor
= AH_TRUE
; /* outdoor operation */
210 int ath_xchanmode
= AH_TRUE
; /* enable extended channels */
211 int ath_countrycode
= CTRY_DEFAULT
; /* country code */
212 int ath_regdomain
= 0; /* regulatory domain */
214 int ath_rxbuf
= ATH_RXBUF
; /* # rx buffers to allocate */
215 int ath_txbuf
= ATH_TXBUF
; /* # tx buffers to allocate */
219 ATH_DEBUG_XMIT
= 0x00000001, /* basic xmit operation */
220 ATH_DEBUG_XMIT_DESC
= 0x00000002, /* xmit descriptors */
221 ATH_DEBUG_RECV
= 0x00000004, /* basic recv operation */
222 ATH_DEBUG_RECV_DESC
= 0x00000008, /* recv descriptors */
223 ATH_DEBUG_RATE
= 0x00000010, /* rate control */
224 ATH_DEBUG_RESET
= 0x00000020, /* reset processing */
225 ATH_DEBUG_MODE
= 0x00000040, /* mode init/setup */
226 ATH_DEBUG_BEACON
= 0x00000080, /* beacon handling */
227 ATH_DEBUG_WATCHDOG
= 0x00000100, /* watchdog timeout */
228 ATH_DEBUG_INTR
= 0x00001000, /* ISR */
229 ATH_DEBUG_TX_PROC
= 0x00002000, /* tx ISR proc */
230 ATH_DEBUG_RX_PROC
= 0x00004000, /* rx ISR proc */
231 ATH_DEBUG_BEACON_PROC
= 0x00008000, /* beacon ISR proc */
232 ATH_DEBUG_CALIBRATE
= 0x00010000, /* periodic calibration */
233 ATH_DEBUG_KEYCACHE
= 0x00020000, /* key cache management */
234 ATH_DEBUG_STATE
= 0x00040000, /* 802.11 state transitions */
235 ATH_DEBUG_NODE
= 0x00080000, /* node management */
236 ATH_DEBUG_LED
= 0x00100000, /* led management */
237 ATH_DEBUG_FF
= 0x00200000, /* fast frames */
238 ATH_DEBUG_DFS
= 0x00400000, /* DFS processing */
239 ATH_DEBUG_FATAL
= 0x80000000, /* fatal errors */
240 ATH_DEBUG_ANY
= 0xffffffff
242 #define IFF_DUMPPKTS(sc, m) \
243 ((sc->sc_debug & (m)) || \
244 (sc->sc_if.if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
245 #define DPRINTF(sc, m, fmt, ...) do { \
246 if (sc->sc_debug & (m)) \
247 printf(fmt, __VA_ARGS__); \
249 #define KEYPRINTF(sc, ix, hk, mac) do { \
250 if (sc->sc_debug & ATH_DEBUG_KEYCACHE) \
251 ath_keyprint(__func__, ix, hk, mac); \
253 static void ath_printrxbuf(struct ath_buf
*bf
, int);
254 static void ath_printtxbuf(struct ath_buf
*bf
, int);
256 #define IFF_DUMPPKTS(sc, m) \
257 ((sc->sc_if.if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
258 #define DPRINTF(m, fmt, ...)
259 #define KEYPRINTF(sc, k, ix, mac)
262 MALLOC_DEFINE(M_ATHDEV
, "athdev", "ath driver dma buffers");
265 ath_attach(u_int16_t devid
, struct ath_softc
*sc
)
267 struct ifnet
*ifp
= &sc
->sc_if
;
268 struct ieee80211com
*ic
= &sc
->sc_ic
;
269 struct ath_hal
*ah
= NULL
;
273 DPRINTF(sc
, ATH_DEBUG_ANY
, "%s: devid 0x%x\n", __func__
, devid
);
275 pmf_self_suspensor_init(sc
->sc_dev
, &sc
->sc_suspensor
, &sc
->sc_qual
);
277 memcpy(ifp
->if_xname
, device_xname(sc
->sc_dev
), IFNAMSIZ
);
279 ah
= ath_hal_attach(devid
, sc
, sc
->sc_st
, sc
->sc_sh
, &status
);
281 if_printf(ifp
, "unable to attach hardware; HAL status %u\n",
286 if (ah
->ah_abi
!= HAL_ABI_VERSION
) {
287 if_printf(ifp
, "HAL ABI mismatch detected "
288 "(HAL:0x%x != driver:0x%x)\n",
289 ah
->ah_abi
, HAL_ABI_VERSION
);
295 if (!prop_dictionary_set_bool(device_properties(sc
->sc_dev
),
296 "pmf-powerdown", false))
300 * Check if the MAC has multi-rate retry support.
301 * We do this by trying to setup a fake extended
302 * descriptor. MAC's that don't have support will
303 * return false w/o doing anything. MAC's that do
304 * support it will return true w/o doing anything.
306 sc
->sc_mrretry
= ath_hal_setupxtxdesc(ah
, NULL
, 0,0, 0,0, 0,0);
309 * Check if the device has hardware counters for PHY
310 * errors. If so we need to enable the MIB interrupt
311 * so we can act on stat triggers.
313 if (ath_hal_hwphycounters(ah
))
317 * Get the hardware key cache size.
319 sc
->sc_keymax
= ath_hal_keycachesize(ah
);
320 if (sc
->sc_keymax
> ATH_KEYMAX
) {
321 if_printf(ifp
, "Warning, using only %u of %u key cache slots\n",
322 ATH_KEYMAX
, sc
->sc_keymax
);
323 sc
->sc_keymax
= ATH_KEYMAX
;
326 * Reset the key cache since some parts do not
327 * reset the contents on initial power up.
329 for (i
= 0; i
< sc
->sc_keymax
; i
++)
330 ath_hal_keyreset(ah
, i
);
332 * Mark key cache slots associated with global keys
333 * as in use. If we knew TKIP was not to be used we
334 * could leave the +32, +64, and +32+64 slots free.
335 * XXX only for splitmic.
337 for (i
= 0; i
< IEEE80211_WEP_NKID
; i
++) {
338 setbit(sc
->sc_keymap
, i
);
339 setbit(sc
->sc_keymap
, i
+32);
340 setbit(sc
->sc_keymap
, i
+64);
341 setbit(sc
->sc_keymap
, i
+32+64);
345 * Collect the channel list using the default country
346 * code and including outdoor channels. The 802.11 layer
347 * is resposible for filtering this list based on settings
350 error
= ath_getchannels(sc
, ath_countrycode
,
351 ath_outdoor
, ath_xchanmode
);
356 * Setup rate tables for all potential media types.
358 ath_rate_setup(sc
, IEEE80211_MODE_11A
);
359 ath_rate_setup(sc
, IEEE80211_MODE_11B
);
360 ath_rate_setup(sc
, IEEE80211_MODE_11G
);
361 ath_rate_setup(sc
, IEEE80211_MODE_TURBO_A
);
362 ath_rate_setup(sc
, IEEE80211_MODE_TURBO_G
);
363 /* NB: setup here so ath_rate_update is happy */
364 ath_setcurmode(sc
, IEEE80211_MODE_11A
);
367 * Allocate tx+rx descriptors and populate the lists.
369 error
= ath_desc_alloc(sc
);
371 if_printf(ifp
, "failed to allocate descriptors: %d\n", error
);
374 ATH_CALLOUT_INIT(&sc
->sc_scan_ch
, debug_mpsafenet
? CALLOUT_MPSAFE
: 0);
375 ATH_CALLOUT_INIT(&sc
->sc_cal_ch
, CALLOUT_MPSAFE
);
377 ATH_CALLOUT_INIT(&sc
->sc_dfs_ch
, CALLOUT_MPSAFE
);
380 ATH_TXBUF_LOCK_INIT(sc
);
382 TASK_INIT(&sc
->sc_rxtask
, 0, ath_rx_proc
, sc
);
383 TASK_INIT(&sc
->sc_rxorntask
, 0, ath_rxorn_proc
, sc
);
384 TASK_INIT(&sc
->sc_fataltask
, 0, ath_fatal_proc
, sc
);
385 TASK_INIT(&sc
->sc_bmisstask
, 0, ath_bmiss_proc
, sc
);
386 TASK_INIT(&sc
->sc_bstucktask
,0, ath_bstuck_proc
, sc
);
387 TASK_INIT(&sc
->sc_radartask
, 0, ath_radar_proc
, sc
);
390 * Allocate hardware transmit queues: one queue for
391 * beacon frames and one data queue for each QoS
392 * priority. Note that the hal handles reseting
393 * these queues at the needed time.
397 sc
->sc_bhalq
= ath_beaconq_setup(ah
);
398 if (sc
->sc_bhalq
== (u_int
) -1) {
399 if_printf(ifp
, "unable to setup a beacon xmit queue!\n");
403 sc
->sc_cabq
= ath_txq_setup(sc
, HAL_TX_QUEUE_CAB
, 0);
404 if (sc
->sc_cabq
== NULL
) {
405 if_printf(ifp
, "unable to setup CAB xmit queue!\n");
409 /* NB: insure BK queue is the lowest priority h/w queue */
410 if (!ath_tx_setup(sc
, WME_AC_BK
, HAL_WME_AC_BK
)) {
411 if_printf(ifp
, "unable to setup xmit queue for %s traffic!\n",
412 ieee80211_wme_acnames
[WME_AC_BK
]);
416 if (!ath_tx_setup(sc
, WME_AC_BE
, HAL_WME_AC_BE
) ||
417 !ath_tx_setup(sc
, WME_AC_VI
, HAL_WME_AC_VI
) ||
418 !ath_tx_setup(sc
, WME_AC_VO
, HAL_WME_AC_VO
)) {
420 * Not enough hardware tx queues to properly do WME;
421 * just punt and assign them all to the same h/w queue.
422 * We could do a better job of this if, for example,
423 * we allocate queues when we switch from station to
426 if (sc
->sc_ac2q
[WME_AC_VI
] != NULL
)
427 ath_tx_cleanupq(sc
, sc
->sc_ac2q
[WME_AC_VI
]);
428 if (sc
->sc_ac2q
[WME_AC_BE
] != NULL
)
429 ath_tx_cleanupq(sc
, sc
->sc_ac2q
[WME_AC_BE
]);
430 sc
->sc_ac2q
[WME_AC_BE
] = sc
->sc_ac2q
[WME_AC_BK
];
431 sc
->sc_ac2q
[WME_AC_VI
] = sc
->sc_ac2q
[WME_AC_BK
];
432 sc
->sc_ac2q
[WME_AC_VO
] = sc
->sc_ac2q
[WME_AC_BK
];
436 * Special case certain configurations. Note the
437 * CAB queue is handled by these specially so don't
438 * include them when checking the txq setup mask.
440 switch (sc
->sc_txqsetup
&~ (1<<sc
->sc_cabq
->axq_qnum
)) {
442 TASK_INIT(&sc
->sc_txtask
, 0, ath_tx_proc_q0
, sc
);
445 TASK_INIT(&sc
->sc_txtask
, 0, ath_tx_proc_q0123
, sc
);
448 TASK_INIT(&sc
->sc_txtask
, 0, ath_tx_proc
, sc
);
453 * Setup rate control. Some rate control modules
454 * call back to change the anntena state so expose
455 * the necessary entry points.
456 * XXX maybe belongs in struct ath_ratectrl?
458 sc
->sc_setdefantenna
= ath_setdefantenna
;
459 sc
->sc_rc
= ath_rate_attach(sc
);
460 if (sc
->sc_rc
== NULL
) {
467 sc
->sc_ledon
= 0; /* low true */
468 sc
->sc_ledidle
= (2700*hz
)/1000; /* 2.7sec */
469 ATH_CALLOUT_INIT(&sc
->sc_ledtimer
, CALLOUT_MPSAFE
);
471 * Auto-enable soft led processing for IBM cards and for
472 * 5211 minipci cards. Users can also manually enable/disable
473 * support with a sysctl.
475 sc
->sc_softled
= (devid
== AR5212_DEVID_IBM
|| devid
== AR5211_DEVID
);
476 if (sc
->sc_softled
) {
477 ath_hal_gpioCfgOutput(ah
, sc
->sc_ledpin
);
478 ath_hal_gpioset(ah
, sc
->sc_ledpin
, !sc
->sc_ledon
);
482 ifp
->if_flags
= IFF_SIMPLEX
| IFF_BROADCAST
| IFF_MULTICAST
;
483 ifp
->if_start
= ath_start
;
484 ifp
->if_stop
= ath_stop
;
485 ifp
->if_watchdog
= ath_watchdog
;
486 ifp
->if_ioctl
= ath_ioctl
;
487 ifp
->if_init
= ath_ifinit
;
488 IFQ_SET_READY(&ifp
->if_snd
);
491 ic
->ic_reset
= ath_reset
;
492 ic
->ic_newassoc
= ath_newassoc
;
493 ic
->ic_updateslot
= ath_updateslot
;
494 ic
->ic_wme
.wme_update
= ath_wme_update
;
495 /* XXX not right but it's not used anywhere important */
496 ic
->ic_phytype
= IEEE80211_T_OFDM
;
497 ic
->ic_opmode
= IEEE80211_M_STA
;
499 IEEE80211_C_IBSS
/* ibss, nee adhoc, mode */
500 | IEEE80211_C_HOSTAP
/* hostap mode */
501 | IEEE80211_C_MONITOR
/* monitor mode */
502 | IEEE80211_C_SHPREAMBLE
/* short preamble supported */
503 | IEEE80211_C_SHSLOT
/* short slot time supported */
504 | IEEE80211_C_WPA
/* capable of WPA1+WPA2 */
505 | IEEE80211_C_TXFRAG
/* handle tx frags */
508 * Query the hal to figure out h/w crypto support.
510 if (ath_hal_ciphersupported(ah
, HAL_CIPHER_WEP
))
511 ic
->ic_caps
|= IEEE80211_C_WEP
;
512 if (ath_hal_ciphersupported(ah
, HAL_CIPHER_AES_OCB
))
513 ic
->ic_caps
|= IEEE80211_C_AES
;
514 if (ath_hal_ciphersupported(ah
, HAL_CIPHER_AES_CCM
))
515 ic
->ic_caps
|= IEEE80211_C_AES_CCM
;
516 if (ath_hal_ciphersupported(ah
, HAL_CIPHER_CKIP
))
517 ic
->ic_caps
|= IEEE80211_C_CKIP
;
518 if (ath_hal_ciphersupported(ah
, HAL_CIPHER_TKIP
)) {
519 ic
->ic_caps
|= IEEE80211_C_TKIP
;
521 * Check if h/w does the MIC and/or whether the
522 * separate key cache entries are required to
523 * handle both tx+rx MIC keys.
525 if (ath_hal_ciphersupported(ah
, HAL_CIPHER_MIC
))
526 ic
->ic_caps
|= IEEE80211_C_TKIPMIC
;
529 * If the h/w supports storing tx+rx MIC keys
530 * in one cache slot automatically enable use.
532 if (ath_hal_hastkipsplit(ah
) ||
533 !ath_hal_settkipsplit(ah
, AH_FALSE
))
537 * If the h/w can do TKIP MIC together with WME then
538 * we use it; otherwise we force the MIC to be done
539 * in software by the net80211 layer.
541 if (ath_hal_haswmetkipmic(ah
))
542 ic
->ic_caps
|= IEEE80211_C_WME_TKIPMIC
;
544 sc
->sc_hasclrkey
= ath_hal_ciphersupported(ah
, HAL_CIPHER_CLR
);
545 sc
->sc_mcastkey
= ath_hal_getmcastkeysearch(ah
);
547 * Mark key cache slots associated with global keys
548 * as in use. If we knew TKIP was not to be used we
549 * could leave the +32, +64, and +32+64 slots free.
551 for (i
= 0; i
< IEEE80211_WEP_NKID
; i
++) {
552 setbit(sc
->sc_keymap
, i
);
553 setbit(sc
->sc_keymap
, i
+64);
554 if (sc
->sc_splitmic
) {
555 setbit(sc
->sc_keymap
, i
+32);
556 setbit(sc
->sc_keymap
, i
+32+64);
560 * TPC support can be done either with a global cap or
561 * per-packet support. The latter is not available on
562 * all parts. We're a bit pedantic here as all parts
563 * support a global cap.
565 if (ath_hal_hastpc(ah
) || ath_hal_hastxpowlimit(ah
))
566 ic
->ic_caps
|= IEEE80211_C_TXPMGT
;
569 * Mark WME capability only if we have sufficient
570 * hardware queues to do proper priority scheduling.
572 if (sc
->sc_ac2q
[WME_AC_BE
] != sc
->sc_ac2q
[WME_AC_BK
])
573 ic
->ic_caps
|= IEEE80211_C_WME
;
575 * Check for misc other capabilities.
577 if (ath_hal_hasbursting(ah
))
578 ic
->ic_caps
|= IEEE80211_C_BURST
;
581 * Indicate we need the 802.11 header padded to a
582 * 32-bit boundary for 4-address and QoS frames.
584 ic
->ic_flags
|= IEEE80211_F_DATAPAD
;
587 * Query the hal about antenna support.
589 sc
->sc_defant
= ath_hal_getdefantenna(ah
);
592 * Not all chips have the VEOL support we want to
593 * use with IBSS beacons; check here for it.
595 sc
->sc_hasveol
= ath_hal_hasveol(ah
);
597 /* get mac address from hardware */
598 ath_hal_getmac(ah
, ic
->ic_myaddr
);
601 /* call MI attach routine. */
602 ieee80211_ifattach(ic
);
603 /* override default methods */
604 ic
->ic_node_alloc
= ath_node_alloc
;
605 sc
->sc_node_free
= ic
->ic_node_free
;
606 ic
->ic_node_free
= ath_node_free
;
607 ic
->ic_node_getrssi
= ath_node_getrssi
;
608 sc
->sc_recv_mgmt
= ic
->ic_recv_mgmt
;
609 ic
->ic_recv_mgmt
= ath_recv_mgmt
;
610 sc
->sc_newstate
= ic
->ic_newstate
;
611 ic
->ic_newstate
= ath_newstate
;
612 ic
->ic_crypto
.cs_max_keyix
= sc
->sc_keymax
;
613 ic
->ic_crypto
.cs_key_alloc
= ath_key_alloc
;
614 ic
->ic_crypto
.cs_key_delete
= ath_key_delete
;
615 ic
->ic_crypto
.cs_key_set
= ath_key_set
;
616 ic
->ic_crypto
.cs_key_update_begin
= ath_key_update_begin
;
617 ic
->ic_crypto
.cs_key_update_end
= ath_key_update_end
;
618 /* complete initialization */
619 ieee80211_media_init(ic
, ath_media_change
, ieee80211_media_status
);
625 sc
->sc_flags
|= ATH_ATTACHED
;
628 * Setup dynamic sysctl's now that country code and
629 * regdomain are available from the hal.
631 ath_sysctlattach(sc
);
633 ieee80211_announce(ic
);
642 /* XXX don't get under the abstraction like this */
643 sc
->sc_dev
->dv_flags
&= ~DVF_ACTIVE
;
648 ath_detach(struct ath_softc
*sc
)
650 struct ifnet
*ifp
= &sc
->sc_if
;
653 if ((sc
->sc_flags
& ATH_ATTACHED
) == 0)
656 DPRINTF(sc
, ATH_DEBUG_ANY
, "%s: if_flags %x\n",
657 __func__
, ifp
->if_flags
);
665 * NB: the order of these is important:
666 * o call the 802.11 layer before detaching the hal to
667 * insure callbacks into the driver to delete global
668 * key cache entries can be handled
669 * o reclaim the tx queue data structures after calling
670 * the 802.11 layer as we'll get called back to reclaim
671 * node state and potentially want to use them
672 * o to cleanup the tx queues the hal is called, so detach
674 * Other than that, it's straightforward...
676 ieee80211_ifdetach(&sc
->sc_ic
);
678 if (sc
->sc_tx99
!= NULL
)
679 sc
->sc_tx99
->detach(sc
->sc_tx99
);
681 ath_rate_detach(sc
->sc_rc
);
684 sysctl_teardown(&sc
->sc_sysctllog
);
685 ath_hal_detach(sc
->sc_ah
);
693 ath_suspend(struct ath_softc
*sc
)
697 * Set the chip in full sleep mode. Note that we are
698 * careful to do this only when bringing the interface
699 * completely to a stop. When the chip is in this state
700 * it must be carefully woken up or references to
701 * registers in the PCI clock domain may freeze the bus
702 * (and system). This varies by chip and is mostly an
703 * issue with newer parts that go to sleep more quickly.
705 ath_hal_setpower(sc
->sc_ah
, HAL_PM_FULL_SLEEP
);
710 ath_resume(struct ath_softc
*sc
)
712 struct ath_hal
*ah
= sc
->sc_ah
;
713 struct ieee80211com
*ic
= &sc
->sc_ic
;
718 ath_hal_setpower(ah
, HAL_PM_AWAKE
);
720 ath_hal_reset(ah
, ic
->ic_opmode
, &sc
->sc_curchan
, AH_FALSE
, &status
);
724 * Reset the key cache since some parts do not
725 * reset the contents on initial power up.
727 for (i
= 0; i
< sc
->sc_keymax
; i
++)
728 ath_hal_keyreset(ah
, i
);
730 ath_hal_resettxqueue(ah
, sc
->sc_bhalq
);
731 for (i
= 0; i
< HAL_NUM_TX_QUEUES
; i
++)
732 if (ATH_TXQ_SETUP(sc
, i
))
733 ath_hal_resettxqueue(ah
, i
);
735 if (sc
->sc_softled
) {
736 ath_hal_gpioCfgOutput(sc
->sc_ah
, sc
->sc_ledpin
);
737 ath_hal_gpioset(sc
->sc_ah
, sc
->sc_ledpin
, !sc
->sc_ledon
);
743 * Interrupt handler. Most of the actual processing is deferred.
748 struct ath_softc
*sc
= arg
;
749 struct ifnet
*ifp
= &sc
->sc_if
;
750 struct ath_hal
*ah
= sc
->sc_ah
;
753 if (!device_activation(sc
->sc_dev
, DEVACT_LEVEL_DRIVER
)) {
755 * The hardware is not ready/present, don't touch anything.
756 * Note this can happen early on if the IRQ is shared.
758 DPRINTF(sc
, ATH_DEBUG_ANY
, "%s: invalid; ignored\n", __func__
);
762 if (!ath_hal_intrpend(ah
)) /* shared irq, not for us */
765 if ((ifp
->if_flags
& (IFF_RUNNING
|IFF_UP
)) != (IFF_RUNNING
|IFF_UP
)) {
766 DPRINTF(sc
, ATH_DEBUG_ANY
, "%s: if_flags 0x%x\n",
767 __func__
, ifp
->if_flags
);
768 ath_hal_getisr(ah
, &status
); /* clear ISR */
769 ath_hal_intrset(ah
, 0); /* disable further intr's */
773 * Figure out the reason(s) for the interrupt. Note
774 * that the hal returns a pseudo-ISR that may include
775 * bits we haven't explicitly enabled so we mask the
776 * value to insure we only process bits we requested.
778 ath_hal_getisr(ah
, &status
); /* NB: clears ISR too */
779 DPRINTF(sc
, ATH_DEBUG_INTR
, "%s: status 0x%x\n", __func__
, status
);
780 status
&= sc
->sc_imask
; /* discard unasked for bits */
781 if (status
& HAL_INT_FATAL
) {
783 * Fatal errors are unrecoverable. Typically
784 * these are caused by DMA errors. Unfortunately
785 * the exact reason is not (presently) returned
788 sc
->sc_stats
.ast_hardware
++;
789 ath_hal_intrset(ah
, 0); /* disable intr's until reset */
790 TASK_RUN_OR_ENQUEUE(&sc
->sc_fataltask
);
791 } else if (status
& HAL_INT_RXORN
) {
792 sc
->sc_stats
.ast_rxorn
++;
793 ath_hal_intrset(ah
, 0); /* disable intr's until reset */
794 TASK_RUN_OR_ENQUEUE(&sc
->sc_rxorntask
);
796 if (status
& HAL_INT_SWBA
) {
798 * Software beacon alert--time to send a beacon.
799 * Handle beacon transmission directly; deferring
800 * this is too slow to meet timing constraints
803 ath_beacon_proc(sc
, 0);
805 if (status
& HAL_INT_RXEOL
) {
807 * NB: the hardware should re-read the link when
808 * RXE bit is written, but it doesn't work at
809 * least on older hardware revs.
811 sc
->sc_stats
.ast_rxeol
++;
812 sc
->sc_rxlink
= NULL
;
814 if (status
& HAL_INT_TXURN
) {
815 sc
->sc_stats
.ast_txurn
++;
816 /* bump tx trigger level */
817 ath_hal_updatetxtriglevel(ah
, AH_TRUE
);
819 if (status
& HAL_INT_RX
)
820 TASK_RUN_OR_ENQUEUE(&sc
->sc_rxtask
);
821 if (status
& HAL_INT_TX
)
822 TASK_RUN_OR_ENQUEUE(&sc
->sc_txtask
);
823 if (status
& HAL_INT_BMISS
) {
824 sc
->sc_stats
.ast_bmiss
++;
825 TASK_RUN_OR_ENQUEUE(&sc
->sc_bmisstask
);
827 if (status
& HAL_INT_MIB
) {
828 sc
->sc_stats
.ast_mib
++;
830 * Disable interrupts until we service the MIB
831 * interrupt; otherwise it will continue to fire.
833 ath_hal_intrset(ah
, 0);
835 * Let the hal handle the event. We assume it will
836 * clear whatever condition caused the interrupt.
838 ath_hal_mibevent(ah
, &sc
->sc_halstats
);
839 ath_hal_intrset(ah
, sc
->sc_imask
);
845 /* Swap transmit descriptor.
846 * if AH_NEED_DESC_SWAP flag is not defined this becomes a "null"
850 ath_desc_swap(struct ath_desc
*ds
)
852 #ifdef AH_NEED_DESC_SWAP
853 ds
->ds_link
= htole32(ds
->ds_link
);
854 ds
->ds_data
= htole32(ds
->ds_data
);
855 ds
->ds_ctl0
= htole32(ds
->ds_ctl0
);
856 ds
->ds_ctl1
= htole32(ds
->ds_ctl1
);
857 ds
->ds_hw
[0] = htole32(ds
->ds_hw
[0]);
858 ds
->ds_hw
[1] = htole32(ds
->ds_hw
[1]);
863 ath_fatal_proc(void *arg
, int pending
)
865 struct ath_softc
*sc
= arg
;
866 struct ifnet
*ifp
= &sc
->sc_if
;
868 if_printf(ifp
, "hardware error; resetting\n");
873 ath_rxorn_proc(void *arg
, int pending
)
875 struct ath_softc
*sc
= arg
;
876 struct ifnet
*ifp
= &sc
->sc_if
;
878 if_printf(ifp
, "rx FIFO overrun; resetting\n");
883 ath_bmiss_proc(void *arg
, int pending
)
885 struct ath_softc
*sc
= arg
;
886 struct ieee80211com
*ic
= &sc
->sc_ic
;
888 DPRINTF(sc
, ATH_DEBUG_ANY
, "%s: pending %u\n", __func__
, pending
);
889 KASSERT(ic
->ic_opmode
== IEEE80211_M_STA
,
890 ("unexpect operating mode %u", ic
->ic_opmode
));
891 if (ic
->ic_state
== IEEE80211_S_RUN
) {
892 u_int64_t lastrx
= sc
->sc_lastrx
;
893 u_int64_t tsf
= ath_hal_gettsf64(sc
->sc_ah
);
895 DPRINTF(sc
, ATH_DEBUG_BEACON
,
896 "%s: tsf %" PRIu64
" lastrx %" PRId64
897 " (%" PRIu64
") bmiss %u\n",
898 __func__
, tsf
, tsf
- lastrx
, lastrx
,
899 ic
->ic_bmisstimeout
*1024);
901 * Workaround phantom bmiss interrupts by sanity-checking
902 * the time of our last rx'd frame. If it is within the
903 * beacon miss interval then ignore the interrupt. If it's
904 * truly a bmiss we'll get another interrupt soon and that'll
905 * be dispatched up for processing.
907 if (tsf
- lastrx
> ic
->ic_bmisstimeout
*1024) {
909 ieee80211_beacon_miss(ic
);
912 sc
->sc_stats
.ast_bmiss_phantom
++;
917 ath_radar_proc(void *arg
, int pending
)
920 struct ath_softc
*sc
= arg
;
921 struct ifnet
*ifp
= &sc
->sc_if
;
922 struct ath_hal
*ah
= sc
->sc_ah
;
925 if (ath_hal_procdfs(ah
, &hchan
)) {
926 if_printf(ifp
, "radar detected on channel %u/0x%x/0x%x\n",
927 hchan
.channel
, hchan
.channelFlags
, hchan
.privFlags
);
929 * Initiate channel change.
937 ath_chan2flags(struct ieee80211com
*ic
, struct ieee80211_channel
*chan
)
939 #define N(a) (sizeof(a) / sizeof(a[0]))
940 static const u_int modeflags
[] = {
941 0, /* IEEE80211_MODE_AUTO */
942 CHANNEL_A
, /* IEEE80211_MODE_11A */
943 CHANNEL_B
, /* IEEE80211_MODE_11B */
944 CHANNEL_PUREG
, /* IEEE80211_MODE_11G */
945 0, /* IEEE80211_MODE_FH */
946 CHANNEL_ST
, /* IEEE80211_MODE_TURBO_A */
947 CHANNEL_108G
/* IEEE80211_MODE_TURBO_G */
949 enum ieee80211_phymode mode
= ieee80211_chan2mode(ic
, chan
);
951 KASSERT(mode
< N(modeflags
), ("unexpected phy mode %u", mode
));
952 KASSERT(modeflags
[mode
] != 0, ("mode %u undefined", mode
));
953 return modeflags
[mode
];
958 ath_ifinit(struct ifnet
*ifp
)
960 struct ath_softc
*sc
= (struct ath_softc
*)ifp
->if_softc
;
966 ath_settkipmic(struct ath_softc
*sc
)
968 struct ieee80211com
*ic
= &sc
->sc_ic
;
969 struct ath_hal
*ah
= sc
->sc_ah
;
971 if ((ic
->ic_caps
& IEEE80211_C_TKIP
) &&
972 !(ic
->ic_caps
& IEEE80211_C_WME_TKIPMIC
)) {
973 if (ic
->ic_flags
& IEEE80211_F_WME
) {
974 (void)ath_hal_settkipmic(ah
, AH_FALSE
);
975 ic
->ic_caps
&= ~IEEE80211_C_TKIPMIC
;
977 (void)ath_hal_settkipmic(ah
, AH_TRUE
);
978 ic
->ic_caps
|= IEEE80211_C_TKIPMIC
;
984 ath_init(struct ath_softc
*sc
)
986 struct ifnet
*ifp
= &sc
->sc_if
;
987 struct ieee80211com
*ic
= &sc
->sc_ic
;
988 struct ath_hal
*ah
= sc
->sc_ah
;
992 DPRINTF(sc
, ATH_DEBUG_ANY
, "%s: if_flags 0x%x\n",
993 __func__
, ifp
->if_flags
);
995 if (device_is_active(sc
->sc_dev
)) {
997 } else if (!pmf_device_subtree_resume(sc
->sc_dev
, &sc
->sc_qual
) ||
998 !device_is_active(sc
->sc_dev
))
1004 * Stop anything previously setup. This is safe
1005 * whether this is the first time through or not.
1007 ath_stop_locked(ifp
, 0);
1010 * The basic interface to setting the hardware in a good
1011 * state is ``reset''. On return the hardware is known to
1012 * be powered up and with interrupts disabled. This must
1013 * be followed by initialization of the appropriate bits
1014 * and then setup of the interrupt mask.
1017 sc
->sc_curchan
.channel
= ic
->ic_curchan
->ic_freq
;
1018 sc
->sc_curchan
.channelFlags
= ath_chan2flags(ic
, ic
->ic_curchan
);
1019 if (!ath_hal_reset(ah
, ic
->ic_opmode
, &sc
->sc_curchan
, AH_FALSE
, &status
)) {
1020 if_printf(ifp
, "unable to reset hardware; hal status %u\n",
1027 * This is needed only to setup initial state
1028 * but it's best done after a reset.
1030 ath_update_txpow(sc
);
1032 * Likewise this is set during reset so update
1033 * state cached in the driver.
1035 ath_restore_diversity(sc
);
1036 sc
->sc_calinterval
= 1;
1037 sc
->sc_caltries
= 0;
1040 * Setup the hardware after reset: the key cache
1041 * is filled as needed and the receive engine is
1042 * set going. Frame transmit is handled entirely
1043 * in the frame output path; there's nothing to do
1044 * here except setup the interrupt mask.
1046 if ((error
= ath_startrecv(sc
)) != 0) {
1047 if_printf(ifp
, "unable to start recv logic\n");
1052 * Enable interrupts.
1054 sc
->sc_imask
= HAL_INT_RX
| HAL_INT_TX
1055 | HAL_INT_RXEOL
| HAL_INT_RXORN
1056 | HAL_INT_FATAL
| HAL_INT_GLOBAL
;
1058 * Enable MIB interrupts when there are hardware phy counters.
1059 * Note we only do this (at the moment) for station mode.
1061 if (sc
->sc_needmib
&& ic
->ic_opmode
== IEEE80211_M_STA
)
1062 sc
->sc_imask
|= HAL_INT_MIB
;
1063 ath_hal_intrset(ah
, sc
->sc_imask
);
1065 ifp
->if_flags
|= IFF_RUNNING
;
1066 ic
->ic_state
= IEEE80211_S_INIT
;
1069 * The hardware should be ready to go now so it's safe
1070 * to kick the 802.11 state machine as it's likely to
1071 * immediately call back to us to send mgmt frames.
1073 ath_chan_change(sc
, ic
->ic_curchan
);
1074 #ifdef ATH_TX99_DIAG
1075 if (sc
->sc_tx99
!= NULL
)
1076 sc
->sc_tx99
->start(sc
->sc_tx99
);
1079 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
1080 if (ic
->ic_roaming
!= IEEE80211_ROAMING_MANUAL
)
1081 ieee80211_new_state(ic
, IEEE80211_S_SCAN
, -1);
1083 ieee80211_new_state(ic
, IEEE80211_S_RUN
, -1);
1090 ath_stop_locked(struct ifnet
*ifp
, int disable
)
1092 struct ath_softc
*sc
= ifp
->if_softc
;
1093 struct ieee80211com
*ic
= &sc
->sc_ic
;
1094 struct ath_hal
*ah
= sc
->sc_ah
;
1096 DPRINTF(sc
, ATH_DEBUG_ANY
, "%s: invalid %d if_flags 0x%x\n",
1097 __func__
, !device_is_enabled(sc
->sc_dev
), ifp
->if_flags
);
1099 ATH_LOCK_ASSERT(sc
);
1100 if (ifp
->if_flags
& IFF_RUNNING
) {
1102 * Shutdown the hardware and driver:
1103 * reset 802.11 state machine
1105 * disable interrupts
1106 * turn off the radio
1107 * clear transmit machinery
1108 * clear receive machinery
1109 * drain and release tx queues
1110 * reclaim beacon resources
1111 * power down hardware
1113 * Note that some of this work is not possible if the
1114 * hardware is gone (invalid).
1116 #ifdef ATH_TX99_DIAG
1117 if (sc
->sc_tx99
!= NULL
)
1118 sc
->sc_tx99
->stop(sc
->sc_tx99
);
1120 ieee80211_new_state(ic
, IEEE80211_S_INIT
, -1);
1121 ifp
->if_flags
&= ~IFF_RUNNING
;
1123 if (device_is_enabled(sc
->sc_dev
)) {
1124 if (sc
->sc_softled
) {
1125 callout_stop(&sc
->sc_ledtimer
);
1126 ath_hal_gpioset(ah
, sc
->sc_ledpin
,
1128 sc
->sc_blinking
= 0;
1130 ath_hal_intrset(ah
, 0);
1133 if (device_is_enabled(sc
->sc_dev
)) {
1135 ath_hal_phydisable(ah
);
1137 sc
->sc_rxlink
= NULL
;
1138 IF_PURGE(&ifp
->if_snd
);
1139 ath_beacon_free(sc
);
1142 pmf_device_suspend(sc
->sc_dev
, &sc
->sc_qual
);
1146 ath_stop(struct ifnet
*ifp
, int disable
)
1148 struct ath_softc
*sc
= ifp
->if_softc
;
1151 ath_stop_locked(ifp
, disable
);
1156 ath_restore_diversity(struct ath_softc
*sc
)
1158 struct ifnet
*ifp
= &sc
->sc_if
;
1159 struct ath_hal
*ah
= sc
->sc_ah
;
1161 if (!ath_hal_setdiversity(sc
->sc_ah
, sc
->sc_diversity
) ||
1162 sc
->sc_diversity
!= ath_hal_getdiversity(ah
)) {
1163 if_printf(ifp
, "could not restore diversity setting %d\n",
1165 sc
->sc_diversity
= ath_hal_getdiversity(ah
);
1170 * Reset the hardware w/o losing operational state. This is
1171 * basically a more efficient way of doing ath_stop, ath_init,
1172 * followed by state transitions to the current 802.11
1173 * operational state. Used to recover from various errors and
1174 * to reset or reload hardware state.
1177 ath_reset(struct ifnet
*ifp
)
1179 struct ath_softc
*sc
= ifp
->if_softc
;
1180 struct ieee80211com
*ic
= &sc
->sc_ic
;
1181 struct ath_hal
*ah
= sc
->sc_ah
;
1182 struct ieee80211_channel
*c
;
1186 * Convert to a HAL channel description with the flags
1187 * constrained to reflect the current operating mode.
1190 sc
->sc_curchan
.channel
= c
->ic_freq
;
1191 sc
->sc_curchan
.channelFlags
= ath_chan2flags(ic
, c
);
1193 ath_hal_intrset(ah
, 0); /* disable interrupts */
1194 ath_draintxq(sc
); /* stop xmit side */
1195 ath_stoprecv(sc
); /* stop recv side */
1196 ath_settkipmic(sc
); /* configure TKIP MIC handling */
1197 /* NB: indicate channel change so we do a full reset */
1198 if (!ath_hal_reset(ah
, ic
->ic_opmode
, &sc
->sc_curchan
, AH_TRUE
, &status
))
1199 if_printf(ifp
, "%s: unable to reset hardware; hal status %u\n",
1201 ath_update_txpow(sc
); /* update tx power state */
1202 ath_restore_diversity(sc
);
1203 sc
->sc_calinterval
= 1;
1204 sc
->sc_caltries
= 0;
1205 if (ath_startrecv(sc
) != 0) /* restart recv */
1206 if_printf(ifp
, "%s: unable to start recv logic\n", __func__
);
1208 * We may be doing a reset in response to an ioctl
1209 * that changes the channel so update any state that
1210 * might change as a result.
1212 ath_chan_change(sc
, c
);
1213 if (ic
->ic_state
== IEEE80211_S_RUN
)
1214 ath_beacon_config(sc
); /* restart beacons */
1215 ath_hal_intrset(ah
, sc
->sc_imask
);
1217 ath_start(ifp
); /* restart xmit */
1222 * Cleanup driver resources when we run out of buffers
1223 * while processing fragments; return the tx buffers
1224 * allocated and drop node references.
1227 ath_txfrag_cleanup(struct ath_softc
*sc
,
1228 ath_bufhead
*frags
, struct ieee80211_node
*ni
)
1232 ATH_TXBUF_LOCK_ASSERT(sc
);
1234 while ((bf
= STAILQ_FIRST(frags
)) != NULL
) {
1235 STAILQ_REMOVE_HEAD(frags
, bf_list
);
1236 STAILQ_INSERT_TAIL(&sc
->sc_txbuf
, bf
, bf_list
);
1237 sc
->sc_if
.if_flags
&= ~IFF_OACTIVE
;
1238 ieee80211_node_decref(ni
);
1243 * Setup xmit of a fragmented frame. Allocate a buffer
1244 * for each frag and bump the node reference count to
1245 * reflect the held reference to be setup by ath_tx_start.
1248 ath_txfrag_setup(struct ath_softc
*sc
, ath_bufhead
*frags
,
1249 struct mbuf
*m0
, struct ieee80211_node
*ni
)
1255 for (m
= m0
->m_nextpkt
; m
!= NULL
; m
= m
->m_nextpkt
) {
1256 bf
= STAILQ_FIRST(&sc
->sc_txbuf
);
1257 if (bf
== NULL
) { /* out of buffers, cleanup */
1258 DPRINTF(sc
, ATH_DEBUG_XMIT
, "%s: out of xmit buffers\n",
1260 sc
->sc_if
.if_flags
|= IFF_OACTIVE
;
1261 ath_txfrag_cleanup(sc
, frags
, ni
);
1264 STAILQ_REMOVE_HEAD(&sc
->sc_txbuf
, bf_list
);
1265 ieee80211_node_incref(ni
);
1266 STAILQ_INSERT_TAIL(frags
, bf
, bf_list
);
1268 ATH_TXBUF_UNLOCK(sc
);
1270 return !STAILQ_EMPTY(frags
);
1274 ath_start(struct ifnet
*ifp
)
1276 struct ath_softc
*sc
= ifp
->if_softc
;
1277 struct ath_hal
*ah
= sc
->sc_ah
;
1278 struct ieee80211com
*ic
= &sc
->sc_ic
;
1279 struct ieee80211_node
*ni
;
1281 struct mbuf
*m
, *next
;
1282 struct ieee80211_frame
*wh
;
1283 struct ether_header
*eh
;
1286 if ((ifp
->if_flags
& IFF_RUNNING
) == 0 ||
1287 !device_is_active(sc
->sc_dev
))
1291 * Grab a TX buffer and associated resources.
1294 bf
= STAILQ_FIRST(&sc
->sc_txbuf
);
1296 STAILQ_REMOVE_HEAD(&sc
->sc_txbuf
, bf_list
);
1297 ATH_TXBUF_UNLOCK(sc
);
1299 DPRINTF(sc
, ATH_DEBUG_XMIT
, "%s: out of xmit buffers\n",
1301 sc
->sc_stats
.ast_tx_qstop
++;
1302 ifp
->if_flags
|= IFF_OACTIVE
;
1306 * Poll the management queue for frames; they
1307 * have priority over normal data frames.
1309 IF_DEQUEUE(&ic
->ic_mgtq
, m
);
1312 * No data frames go out unless we're associated.
1314 if (ic
->ic_state
!= IEEE80211_S_RUN
) {
1315 DPRINTF(sc
, ATH_DEBUG_XMIT
,
1316 "%s: discard data packet, state %s\n",
1318 ieee80211_state_name
[ic
->ic_state
]);
1319 sc
->sc_stats
.ast_tx_discard
++;
1321 STAILQ_INSERT_TAIL(&sc
->sc_txbuf
, bf
, bf_list
);
1322 ATH_TXBUF_UNLOCK(sc
);
1325 IFQ_DEQUEUE(&ifp
->if_snd
, m
); /* XXX: LOCK */
1328 STAILQ_INSERT_TAIL(&sc
->sc_txbuf
, bf
, bf_list
);
1329 ATH_TXBUF_UNLOCK(sc
);
1332 STAILQ_INIT(&frags
);
1334 * Find the node for the destination so we can do
1335 * things like power save and fast frames aggregation.
1337 if (m
->m_len
< sizeof(struct ether_header
) &&
1338 (m
= m_pullup(m
, sizeof(struct ether_header
))) == NULL
) {
1339 ic
->ic_stats
.is_tx_nobuf
++; /* XXX */
1343 eh
= mtod(m
, struct ether_header
*);
1344 ni
= ieee80211_find_txnode(ic
, eh
->ether_dhost
);
1346 /* NB: ieee80211_find_txnode does stat+msg */
1350 if ((ni
->ni_flags
& IEEE80211_NODE_PWR_MGT
) &&
1351 (m
->m_flags
& M_PWR_SAV
) == 0) {
1353 * Station in power save mode; pass the frame
1354 * to the 802.11 layer and continue. We'll get
1355 * the frame back when the time is right.
1357 ieee80211_pwrsave(ic
, ni
, m
);
1360 /* calculate priority so we can find the tx queue */
1361 if (ieee80211_classify(ic
, m
, ni
)) {
1362 DPRINTF(sc
, ATH_DEBUG_XMIT
,
1363 "%s: discard, classification failure\n",
1372 bpf_mtap(ifp
->if_bpf
, m
);
1375 * Encapsulate the packet in prep for transmission.
1377 m
= ieee80211_encap(ic
, m
, ni
);
1379 DPRINTF(sc
, ATH_DEBUG_XMIT
,
1380 "%s: encapsulation failure\n",
1382 sc
->sc_stats
.ast_tx_encap
++;
1386 * Check for fragmentation. If this has frame
1387 * has been broken up verify we have enough
1388 * buffers to send all the fragments so all
1391 if ((m
->m_flags
& M_FRAG
) &&
1392 !ath_txfrag_setup(sc
, &frags
, m
, ni
)) {
1393 DPRINTF(sc
, ATH_DEBUG_ANY
,
1394 "%s: out of txfrag buffers\n", __func__
);
1395 ic
->ic_stats
.is_tx_nobuf
++; /* XXX */
1401 * Hack! The referenced node pointer is in the
1402 * rcvif field of the packet header. This is
1403 * placed there by ieee80211_mgmt_output because
1404 * we need to hold the reference with the frame
1405 * and there's no other way (other than packet
1406 * tags which we consider too expensive to use)
1409 ni
= (struct ieee80211_node
*) m
->m_pkthdr
.rcvif
;
1410 m
->m_pkthdr
.rcvif
= NULL
;
1412 wh
= mtod(m
, struct ieee80211_frame
*);
1413 if ((wh
->i_fc
[0] & IEEE80211_FC0_SUBTYPE_MASK
) ==
1414 IEEE80211_FC0_SUBTYPE_PROBE_RESP
) {
1415 /* fill time stamp */
1419 tsf
= ath_hal_gettsf64(ah
);
1420 /* XXX: adjust 100us delay to xmit */
1422 tstamp
= (u_int32_t
*)&wh
[1];
1423 tstamp
[0] = htole32(tsf
& 0xffffffff);
1424 tstamp
[1] = htole32(tsf
>> 32);
1426 sc
->sc_stats
.ast_tx_mgmt
++;
1430 next
= m
->m_nextpkt
;
1431 if (ath_tx_start(sc
, ni
, bf
, m
)) {
1436 STAILQ_INSERT_TAIL(&sc
->sc_txbuf
, bf
, bf_list
);
1437 ath_txfrag_cleanup(sc
, &frags
, ni
);
1438 ATH_TXBUF_UNLOCK(sc
);
1440 ieee80211_free_node(ni
);
1445 bf
= STAILQ_FIRST(&frags
);
1446 KASSERT(bf
!= NULL
, ("no buf for txfrag"));
1447 STAILQ_REMOVE_HEAD(&frags
, bf_list
);
1456 ath_media_change(struct ifnet
*ifp
)
1458 #define IS_UP(ifp) \
1459 ((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
1462 error
= ieee80211_media_change(ifp
);
1463 if (error
== ENETRESET
) {
1465 ath_init(ifp
->if_softc
); /* XXX lose error */
1474 ath_keyprint(const char *tag
, u_int ix
,
1475 const HAL_KEYVAL
*hk
, const u_int8_t mac
[IEEE80211_ADDR_LEN
])
1477 static const char *ciphers
[] = {
1487 printf("%s: [%02u] %-7s ", tag
, ix
, ciphers
[hk
->kv_type
]);
1488 for (i
= 0, n
= hk
->kv_len
; i
< n
; i
++)
1489 printf("%02x", hk
->kv_val
[i
]);
1490 printf(" mac %s", ether_sprintf(mac
));
1491 if (hk
->kv_type
== HAL_CIPHER_TKIP
) {
1493 for (i
= 0; i
< sizeof(hk
->kv_mic
); i
++)
1494 printf("%02x", hk
->kv_mic
[i
]);
1501 * Set a TKIP key into the hardware. This handles the
1502 * potential distribution of key state to multiple key
1503 * cache slots for TKIP.
1506 ath_keyset_tkip(struct ath_softc
*sc
, const struct ieee80211_key
*k
,
1507 HAL_KEYVAL
*hk
, const u_int8_t mac
[IEEE80211_ADDR_LEN
])
1509 #define IEEE80211_KEY_XR (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV)
1510 static const u_int8_t zerobssid
[IEEE80211_ADDR_LEN
];
1511 struct ath_hal
*ah
= sc
->sc_ah
;
1513 KASSERT(k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
,
1514 ("got a non-TKIP key, cipher %u", k
->wk_cipher
->ic_cipher
));
1515 if ((k
->wk_flags
& IEEE80211_KEY_XR
) == IEEE80211_KEY_XR
) {
1516 if (sc
->sc_splitmic
) {
1518 * TX key goes at first index, RX key at the rx index.
1519 * The hal handles the MIC keys at index+64.
1521 memcpy(hk
->kv_mic
, k
->wk_txmic
, sizeof(hk
->kv_mic
));
1522 KEYPRINTF(sc
, k
->wk_keyix
, hk
, zerobssid
);
1523 if (!ath_hal_keyset(ah
, ATH_KEY(k
->wk_keyix
), hk
,
1527 memcpy(hk
->kv_mic
, k
->wk_rxmic
, sizeof(hk
->kv_mic
));
1528 KEYPRINTF(sc
, k
->wk_keyix
+32, hk
, mac
);
1529 /* XXX delete tx key on failure? */
1530 return ath_hal_keyset(ah
, ATH_KEY(k
->wk_keyix
+32),
1534 * Room for both TX+RX MIC keys in one key cache
1535 * slot, just set key at the first index; the HAL
1536 * will handle the reset.
1538 memcpy(hk
->kv_mic
, k
->wk_rxmic
, sizeof(hk
->kv_mic
));
1539 memcpy(hk
->kv_txmic
, k
->wk_txmic
, sizeof(hk
->kv_txmic
));
1540 KEYPRINTF(sc
, k
->wk_keyix
, hk
, mac
);
1541 return ath_hal_keyset(ah
, ATH_KEY(k
->wk_keyix
), hk
, mac
);
1543 } else if (k
->wk_flags
& IEEE80211_KEY_XMIT
) {
1544 if (sc
->sc_splitmic
) {
1546 * NB: must pass MIC key in expected location when
1547 * the keycache only holds one MIC key per entry.
1549 memcpy(hk
->kv_mic
, k
->wk_txmic
, sizeof(hk
->kv_txmic
));
1551 memcpy(hk
->kv_txmic
, k
->wk_txmic
, sizeof(hk
->kv_txmic
));
1552 KEYPRINTF(sc
, k
->wk_keyix
, hk
, mac
);
1553 return ath_hal_keyset(ah
, ATH_KEY(k
->wk_keyix
), hk
, mac
);
1554 } else if (k
->wk_flags
& IEEE80211_KEY_RECV
) {
1555 memcpy(hk
->kv_mic
, k
->wk_rxmic
, sizeof(hk
->kv_mic
));
1556 KEYPRINTF(sc
, k
->wk_keyix
, hk
, mac
);
1557 return ath_hal_keyset(ah
, k
->wk_keyix
, hk
, mac
);
1560 #undef IEEE80211_KEY_XR
1564 * Set a net80211 key into the hardware. This handles the
1565 * potential distribution of key state to multiple key
1566 * cache slots for TKIP with hardware MIC support.
1569 ath_keyset(struct ath_softc
*sc
, const struct ieee80211_key
*k
,
1570 const u_int8_t mac0
[IEEE80211_ADDR_LEN
],
1571 struct ieee80211_node
*bss
)
1573 #define N(a) (sizeof(a)/sizeof(a[0]))
1574 static const u_int8_t ciphermap
[] = {
1575 HAL_CIPHER_WEP
, /* IEEE80211_CIPHER_WEP */
1576 HAL_CIPHER_TKIP
, /* IEEE80211_CIPHER_TKIP */
1577 HAL_CIPHER_AES_OCB
, /* IEEE80211_CIPHER_AES_OCB */
1578 HAL_CIPHER_AES_CCM
, /* IEEE80211_CIPHER_AES_CCM */
1579 (u_int8_t
) -1, /* 4 is not allocated */
1580 HAL_CIPHER_CKIP
, /* IEEE80211_CIPHER_CKIP */
1581 HAL_CIPHER_CLR
, /* IEEE80211_CIPHER_NONE */
1583 struct ath_hal
*ah
= sc
->sc_ah
;
1584 const struct ieee80211_cipher
*cip
= k
->wk_cipher
;
1585 u_int8_t gmac
[IEEE80211_ADDR_LEN
];
1586 const u_int8_t
*mac
;
1589 memset(&hk
, 0, sizeof(hk
));
1591 * Software crypto uses a "clear key" so non-crypto
1592 * state kept in the key cache are maintained and
1593 * so that rx frames have an entry to match.
1595 if ((k
->wk_flags
& IEEE80211_KEY_SWCRYPT
) == 0) {
1596 KASSERT(cip
->ic_cipher
< N(ciphermap
),
1597 ("invalid cipher type %u", cip
->ic_cipher
));
1598 hk
.kv_type
= ciphermap
[cip
->ic_cipher
];
1599 hk
.kv_len
= k
->wk_keylen
;
1600 memcpy(hk
.kv_val
, k
->wk_key
, k
->wk_keylen
);
1602 hk
.kv_type
= HAL_CIPHER_CLR
;
1604 if ((k
->wk_flags
& IEEE80211_KEY_GROUP
) && sc
->sc_mcastkey
) {
1606 * Group keys on hardware that supports multicast frame
1607 * key search use a mac that is the sender's address with
1608 * the high bit set instead of the app-specified address.
1610 IEEE80211_ADDR_COPY(gmac
, bss
->ni_macaddr
);
1616 if ((hk
.kv_type
== HAL_CIPHER_TKIP
&&
1617 (k
->wk_flags
& IEEE80211_KEY_SWMIC
) == 0)) {
1618 return ath_keyset_tkip(sc
, k
, &hk
, mac
);
1620 KEYPRINTF(sc
, k
->wk_keyix
, &hk
, mac
);
1621 return ath_hal_keyset(ah
, ATH_KEY(k
->wk_keyix
), &hk
, mac
);
1627 * Allocate tx/rx key slots for TKIP. We allocate two slots for
1628 * each key, one for decrypt/encrypt and the other for the MIC.
1631 key_alloc_2pair(struct ath_softc
*sc
,
1632 ieee80211_keyix
*txkeyix
, ieee80211_keyix
*rxkeyix
)
1634 #define N(a) (sizeof(a)/sizeof(a[0]))
1637 KASSERT(sc
->sc_splitmic
, ("key cache !split"));
1638 /* XXX could optimize */
1639 for (i
= 0; i
< N(sc
->sc_keymap
)/4; i
++) {
1640 u_int8_t b
= sc
->sc_keymap
[i
];
1643 * One or more slots in this byte are free.
1651 /* XXX IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV */
1652 if (isset(sc
->sc_keymap
, keyix
+32) ||
1653 isset(sc
->sc_keymap
, keyix
+64) ||
1654 isset(sc
->sc_keymap
, keyix
+32+64)) {
1655 /* full pair unavailable */
1657 if (keyix
== (i
+1)*NBBY
) {
1658 /* no slots were appropriate, advance */
1663 setbit(sc
->sc_keymap
, keyix
);
1664 setbit(sc
->sc_keymap
, keyix
+64);
1665 setbit(sc
->sc_keymap
, keyix
+32);
1666 setbit(sc
->sc_keymap
, keyix
+32+64);
1667 DPRINTF(sc
, ATH_DEBUG_KEYCACHE
,
1668 "%s: key pair %u,%u %u,%u\n",
1669 __func__
, keyix
, keyix
+64,
1670 keyix
+32, keyix
+32+64);
1672 *rxkeyix
= keyix
+32;
1676 DPRINTF(sc
, ATH_DEBUG_KEYCACHE
, "%s: out of pair space\n", __func__
);
1677 return IEEE80211_KEYIX_NONE
;
1682 * Allocate tx/rx key slots for TKIP. We allocate two slots for
1683 * each key, one for decrypt/encrypt and the other for the MIC.
1686 key_alloc_pair(struct ath_softc
*sc
, ieee80211_keyix
*txkeyix
,
1687 ieee80211_keyix
*rxkeyix
)
1689 #define N(a) (sizeof(a)/sizeof(a[0]))
1692 KASSERT(!sc
->sc_splitmic
, ("key cache split"));
1693 /* XXX could optimize */
1694 for (i
= 0; i
< N(sc
->sc_keymap
)/4; i
++) {
1695 uint8_t b
= sc
->sc_keymap
[i
];
1698 * One or more slots in this byte are free.
1706 if (isset(sc
->sc_keymap
, keyix
+64)) {
1707 /* full pair unavailable */
1709 if (keyix
== (i
+1)*NBBY
) {
1710 /* no slots were appropriate, advance */
1715 setbit(sc
->sc_keymap
, keyix
);
1716 setbit(sc
->sc_keymap
, keyix
+64);
1717 DPRINTF(sc
, ATH_DEBUG_KEYCACHE
,
1718 "%s: key pair %u,%u\n",
1719 __func__
, keyix
, keyix
+64);
1720 *txkeyix
= *rxkeyix
= keyix
;
1724 DPRINTF(sc
, ATH_DEBUG_KEYCACHE
, "%s: out of pair space\n", __func__
);
1730 * Allocate a single key cache slot.
1733 key_alloc_single(struct ath_softc
*sc
,
1734 ieee80211_keyix
*txkeyix
, ieee80211_keyix
*rxkeyix
)
1736 #define N(a) (sizeof(a)/sizeof(a[0]))
1739 /* XXX try i,i+32,i+64,i+32+64 to minimize key pair conflicts */
1740 for (i
= 0; i
< N(sc
->sc_keymap
); i
++) {
1741 u_int8_t b
= sc
->sc_keymap
[i
];
1744 * One or more slots are free.
1749 setbit(sc
->sc_keymap
, keyix
);
1750 DPRINTF(sc
, ATH_DEBUG_KEYCACHE
, "%s: key %u\n",
1752 *txkeyix
= *rxkeyix
= keyix
;
1756 DPRINTF(sc
, ATH_DEBUG_KEYCACHE
, "%s: out of space\n", __func__
);
1762 * Allocate one or more key cache slots for a uniacst key. The
1763 * key itself is needed only to identify the cipher. For hardware
1764 * TKIP with split cipher+MIC keys we allocate two key cache slot
1765 * pairs so that we can setup separate TX and RX MIC keys. Note
1766 * that the MIC key for a TKIP key at slot i is assumed by the
1767 * hardware to be at slot i+64. This limits TKIP keys to the first
1771 ath_key_alloc(struct ieee80211com
*ic
, const struct ieee80211_key
*k
,
1772 ieee80211_keyix
*keyix
, ieee80211_keyix
*rxkeyix
)
1774 struct ath_softc
*sc
= ic
->ic_ifp
->if_softc
;
1777 * Group key allocation must be handled specially for
1778 * parts that do not support multicast key cache search
1779 * functionality. For those parts the key id must match
1780 * the h/w key index so lookups find the right key. On
1781 * parts w/ the key search facility we install the sender's
1782 * mac address (with the high bit set) and let the hardware
1783 * find the key w/o using the key id. This is preferred as
1784 * it permits us to support multiple users for adhoc and/or
1785 * multi-station operation.
1787 if ((k
->wk_flags
& IEEE80211_KEY_GROUP
) && !sc
->sc_mcastkey
) {
1788 if (!(&ic
->ic_nw_keys
[0] <= k
&&
1789 k
< &ic
->ic_nw_keys
[IEEE80211_WEP_NKID
])) {
1790 /* should not happen */
1791 DPRINTF(sc
, ATH_DEBUG_KEYCACHE
,
1792 "%s: bogus group key\n", __func__
);
1796 * XXX we pre-allocate the global keys so
1797 * have no way to check if they've already been allocated.
1799 *keyix
= *rxkeyix
= k
- ic
->ic_nw_keys
;
1804 * We allocate two pair for TKIP when using the h/w to do
1805 * the MIC. For everything else, including software crypto,
1806 * we allocate a single entry. Note that s/w crypto requires
1807 * a pass-through slot on the 5211 and 5212. The 5210 does
1808 * not support pass-through cache entries and we map all
1809 * those requests to slot 0.
1811 if (k
->wk_flags
& IEEE80211_KEY_SWCRYPT
) {
1812 return key_alloc_single(sc
, keyix
, rxkeyix
);
1813 } else if (k
->wk_cipher
->ic_cipher
== IEEE80211_CIPHER_TKIP
&&
1814 (k
->wk_flags
& IEEE80211_KEY_SWMIC
) == 0) {
1815 if (sc
->sc_splitmic
)
1816 return key_alloc_2pair(sc
, keyix
, rxkeyix
);
1818 return key_alloc_pair(sc
, keyix
, rxkeyix
);
1820 return key_alloc_single(sc
, keyix
, rxkeyix
);
1825 * Delete an entry in the key cache allocated by ath_key_alloc.
1828 ath_key_delete(struct ieee80211com
*ic
, const struct ieee80211_key
*k
)
1830 struct ath_softc
*sc
= ic
->ic_ifp
->if_softc
;
1831 struct ath_hal
*ah
= sc
->sc_ah
;
1832 const struct ieee80211_cipher
*cip
= k
->wk_cipher
;
1833 u_int keyix
= k
->wk_keyix
;
1835 DPRINTF(sc
, ATH_DEBUG_KEYCACHE
, "%s: delete key %u\n", __func__
, keyix
);
1837 if (!device_has_power(sc
->sc_dev
)) {
1838 aprint_error_dev(sc
->sc_dev
, "deleting keyix %d w/o power\n",
1842 ath_hal_keyreset(ah
, keyix
);
1844 * Handle split tx/rx keying required for TKIP with h/w MIC.
1846 if (cip
->ic_cipher
== IEEE80211_CIPHER_TKIP
&&
1847 (k
->wk_flags
& IEEE80211_KEY_SWMIC
) == 0 && sc
->sc_splitmic
)
1848 ath_hal_keyreset(ah
, keyix
+32); /* RX key */
1849 if (keyix
>= IEEE80211_WEP_NKID
) {
1851 * Don't touch keymap entries for global keys so
1852 * they are never considered for dynamic allocation.
1854 clrbit(sc
->sc_keymap
, keyix
);
1855 if (cip
->ic_cipher
== IEEE80211_CIPHER_TKIP
&&
1856 (k
->wk_flags
& IEEE80211_KEY_SWMIC
) == 0) {
1857 clrbit(sc
->sc_keymap
, keyix
+64); /* TX key MIC */
1858 if (sc
->sc_splitmic
) {
1859 /* +32 for RX key, +32+64 for RX key MIC */
1860 clrbit(sc
->sc_keymap
, keyix
+32);
1861 clrbit(sc
->sc_keymap
, keyix
+32+64);
1869 * Set the key cache contents for the specified key. Key cache
1870 * slot(s) must already have been allocated by ath_key_alloc.
1873 ath_key_set(struct ieee80211com
*ic
, const struct ieee80211_key
*k
,
1874 const u_int8_t mac
[IEEE80211_ADDR_LEN
])
1876 struct ath_softc
*sc
= ic
->ic_ifp
->if_softc
;
1878 if (!device_has_power(sc
->sc_dev
)) {
1879 aprint_error_dev(sc
->sc_dev
, "setting keyix %d w/o power\n",
1882 return ath_keyset(sc
, k
, mac
, ic
->ic_bss
);
1886 * Block/unblock tx+rx processing while a key change is done.
1887 * We assume the caller serializes key management operations
1888 * so we only need to worry about synchronization with other
1889 * uses that originate in the driver.
1892 ath_key_update_begin(struct ieee80211com
*ic
)
1894 struct ifnet
*ifp
= ic
->ic_ifp
;
1895 struct ath_softc
*sc
= ifp
->if_softc
;
1897 DPRINTF(sc
, ATH_DEBUG_KEYCACHE
, "%s:\n", __func__
);
1899 tasklet_disable(&sc
->sc_rxtq
);
1901 IF_LOCK(&ifp
->if_snd
); /* NB: doesn't block mgmt frames */
1905 ath_key_update_end(struct ieee80211com
*ic
)
1907 struct ifnet
*ifp
= ic
->ic_ifp
;
1908 struct ath_softc
*sc
= ifp
->if_softc
;
1910 DPRINTF(sc
, ATH_DEBUG_KEYCACHE
, "%s:\n", __func__
);
1911 IF_UNLOCK(&ifp
->if_snd
);
1913 tasklet_enable(&sc
->sc_rxtq
);
1918 * Calculate the receive filter according to the
1919 * operating mode and state:
1921 * o always accept unicast, broadcast, and multicast traffic
1922 * o maintain current state of phy error reception (the hal
1923 * may enable phy error frames for noise immunity work)
1924 * o probe request frames are accepted only when operating in
1925 * hostap, adhoc, or monitor modes
1926 * o enable promiscuous mode according to the interface state
1928 * - when operating in adhoc mode so the 802.11 layer creates
1929 * node table entries for peers,
1930 * - when operating in station mode for collecting rssi data when
1931 * the station is otherwise quiet, or
1935 ath_calcrxfilter(struct ath_softc
*sc
, enum ieee80211_state state
)
1937 struct ieee80211com
*ic
= &sc
->sc_ic
;
1938 struct ath_hal
*ah
= sc
->sc_ah
;
1939 struct ifnet
*ifp
= &sc
->sc_if
;
1942 rfilt
= (ath_hal_getrxfilter(ah
) & HAL_RX_FILTER_PHYERR
)
1943 | HAL_RX_FILTER_UCAST
| HAL_RX_FILTER_BCAST
| HAL_RX_FILTER_MCAST
;
1944 if (ic
->ic_opmode
!= IEEE80211_M_STA
)
1945 rfilt
|= HAL_RX_FILTER_PROBEREQ
;
1946 if (ic
->ic_opmode
!= IEEE80211_M_HOSTAP
&&
1947 (ifp
->if_flags
& IFF_PROMISC
))
1948 rfilt
|= HAL_RX_FILTER_PROM
;
1949 if (ifp
->if_flags
& IFF_PROMISC
)
1950 rfilt
|= HAL_RX_FILTER_CONTROL
| HAL_RX_FILTER_PROBEREQ
;
1951 if (ic
->ic_opmode
== IEEE80211_M_STA
||
1952 ic
->ic_opmode
== IEEE80211_M_IBSS
||
1953 state
== IEEE80211_S_SCAN
)
1954 rfilt
|= HAL_RX_FILTER_BEACON
;
1959 ath_mode_init(struct ath_softc
*sc
)
1961 struct ifnet
*ifp
= &sc
->sc_if
;
1962 struct ieee80211com
*ic
= &sc
->sc_ic
;
1963 struct ath_hal
*ah
= sc
->sc_ah
;
1964 struct ether_multi
*enm
;
1965 struct ether_multistep estep
;
1966 u_int32_t rfilt
, mfilt
[2], val
;
1970 /* configure rx filter */
1971 rfilt
= ath_calcrxfilter(sc
, ic
->ic_state
);
1972 ath_hal_setrxfilter(ah
, rfilt
);
1974 /* configure operational mode */
1975 ath_hal_setopmode(ah
);
1977 /* Write keys to hardware; it may have been powered down. */
1978 ath_key_update_begin(ic
);
1979 for (i
= 0; i
< IEEE80211_WEP_NKID
; i
++) {
1981 &ic
->ic_crypto
.cs_nw_keys
[i
],
1984 ath_key_update_end(ic
);
1987 * Handle any link-level address change. Note that we only
1988 * need to force ic_myaddr; any other addresses are handled
1989 * as a byproduct of the ifnet code marking the interface
1992 * XXX should get from lladdr instead of arpcom but that's more work
1994 IEEE80211_ADDR_COPY(ic
->ic_myaddr
, CLLADDR(sc
->sc_if
.if_sadl
));
1995 ath_hal_setmac(ah
, ic
->ic_myaddr
);
1997 /* calculate and install multicast filter */
1998 ifp
->if_flags
&= ~IFF_ALLMULTI
;
1999 mfilt
[0] = mfilt
[1] = 0;
2000 ETHER_FIRST_MULTI(estep
, &sc
->sc_ec
, enm
);
2001 while (enm
!= NULL
) {
2003 /* XXX Punt on ranges. */
2004 if (!IEEE80211_ADDR_EQ(enm
->enm_addrlo
, enm
->enm_addrhi
)) {
2005 mfilt
[0] = mfilt
[1] = 0xffffffff;
2006 ifp
->if_flags
|= IFF_ALLMULTI
;
2009 dl
= enm
->enm_addrlo
;
2010 val
= LE_READ_4((char *)dl
+ 0);
2011 pos
= (val
>> 18) ^ (val
>> 12) ^ (val
>> 6) ^ val
;
2012 val
= LE_READ_4((char *)dl
+ 3);
2013 pos
^= (val
>> 18) ^ (val
>> 12) ^ (val
>> 6) ^ val
;
2015 mfilt
[pos
/ 32] |= (1 << (pos
% 32));
2017 ETHER_NEXT_MULTI(estep
, enm
);
2020 ath_hal_setmcastfilter(ah
, mfilt
[0], mfilt
[1]);
2021 DPRINTF(sc
, ATH_DEBUG_MODE
, "%s: RX filter 0x%x, MC filter %08x:%08x\n",
2022 __func__
, rfilt
, mfilt
[0], mfilt
[1]);
2026 * Set the slot time based on the current setting.
2029 ath_setslottime(struct ath_softc
*sc
)
2031 struct ieee80211com
*ic
= &sc
->sc_ic
;
2032 struct ath_hal
*ah
= sc
->sc_ah
;
2034 if (ic
->ic_flags
& IEEE80211_F_SHSLOT
)
2035 ath_hal_setslottime(ah
, HAL_SLOT_TIME_9
);
2037 ath_hal_setslottime(ah
, HAL_SLOT_TIME_20
);
2038 sc
->sc_updateslot
= OK
;
2042 * Callback from the 802.11 layer to update the
2043 * slot time based on the current setting.
2046 ath_updateslot(struct ifnet
*ifp
)
2048 struct ath_softc
*sc
= ifp
->if_softc
;
2049 struct ieee80211com
*ic
= &sc
->sc_ic
;
2052 * When not coordinating the BSS, change the hardware
2053 * immediately. For other operation we defer the change
2054 * until beacon updates have propagated to the stations.
2056 if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
)
2057 sc
->sc_updateslot
= UPDATE
;
2059 ath_setslottime(sc
);
2063 * Setup a h/w transmit queue for beacons.
2066 ath_beaconq_setup(struct ath_hal
*ah
)
2070 memset(&qi
, 0, sizeof(qi
));
2071 qi
.tqi_aifs
= HAL_TXQ_USEDEFAULT
;
2072 qi
.tqi_cwmin
= HAL_TXQ_USEDEFAULT
;
2073 qi
.tqi_cwmax
= HAL_TXQ_USEDEFAULT
;
2074 /* NB: for dynamic turbo, don't enable any other interrupts */
2075 qi
.tqi_qflags
= HAL_TXQ_TXDESCINT_ENABLE
;
2076 return ath_hal_setuptxqueue(ah
, HAL_TX_QUEUE_BEACON
, &qi
);
2080 * Setup the transmit queue parameters for the beacon queue.
2083 ath_beaconq_config(struct ath_softc
*sc
)
2085 #define ATH_EXPONENT_TO_VALUE(v) ((1<<(v))-1)
2086 struct ieee80211com
*ic
= &sc
->sc_ic
;
2087 struct ath_hal
*ah
= sc
->sc_ah
;
2090 ath_hal_gettxqueueprops(ah
, sc
->sc_bhalq
, &qi
);
2091 if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
) {
2093 * Always burst out beacon and CAB traffic.
2095 qi
.tqi_aifs
= ATH_BEACON_AIFS_DEFAULT
;
2096 qi
.tqi_cwmin
= ATH_BEACON_CWMIN_DEFAULT
;
2097 qi
.tqi_cwmax
= ATH_BEACON_CWMAX_DEFAULT
;
2099 struct wmeParams
*wmep
=
2100 &ic
->ic_wme
.wme_chanParams
.cap_wmeParams
[WME_AC_BE
];
2102 * Adhoc mode; important thing is to use 2x cwmin.
2104 qi
.tqi_aifs
= wmep
->wmep_aifsn
;
2105 qi
.tqi_cwmin
= 2*ATH_EXPONENT_TO_VALUE(wmep
->wmep_logcwmin
);
2106 qi
.tqi_cwmax
= ATH_EXPONENT_TO_VALUE(wmep
->wmep_logcwmax
);
2109 if (!ath_hal_settxqueueprops(ah
, sc
->sc_bhalq
, &qi
)) {
2110 device_printf(sc
->sc_dev
, "unable to update parameters for "
2111 "beacon hardware queue!\n");
2114 ath_hal_resettxqueue(ah
, sc
->sc_bhalq
); /* push to h/w */
2117 #undef ATH_EXPONENT_TO_VALUE
2121 * Allocate and setup an initial beacon frame.
2124 ath_beacon_alloc(struct ath_softc
*sc
, struct ieee80211_node
*ni
)
2126 struct ieee80211com
*ic
= ni
->ni_ic
;
2131 bf
= STAILQ_FIRST(&sc
->sc_bbuf
);
2133 DPRINTF(sc
, ATH_DEBUG_BEACON
, "%s: no dma buffers\n", __func__
);
2134 sc
->sc_stats
.ast_be_nombuf
++; /* XXX */
2135 return ENOMEM
; /* XXX */
2138 * NB: the beacon data buffer must be 32-bit aligned;
2139 * we assume the mbuf routines will return us something
2140 * with this alignment (perhaps should assert).
2142 m
= ieee80211_beacon_alloc(ic
, ni
, &sc
->sc_boff
);
2144 DPRINTF(sc
, ATH_DEBUG_BEACON
, "%s: cannot get mbuf\n",
2146 sc
->sc_stats
.ast_be_nombuf
++;
2149 error
= bus_dmamap_load_mbuf(sc
->sc_dmat
, bf
->bf_dmamap
, m
,
2153 bf
->bf_node
= ieee80211_ref_node(ni
);
2161 * Setup the beacon frame for transmit.
2164 ath_beacon_setup(struct ath_softc
*sc
, struct ath_buf
*bf
)
2166 #define USE_SHPREAMBLE(_ic) \
2167 (((_ic)->ic_flags & (IEEE80211_F_SHPREAMBLE | IEEE80211_F_USEBARKER))\
2168 == IEEE80211_F_SHPREAMBLE)
2169 struct ieee80211_node
*ni
= bf
->bf_node
;
2170 struct ieee80211com
*ic
= ni
->ni_ic
;
2171 struct mbuf
*m
= bf
->bf_m
;
2172 struct ath_hal
*ah
= sc
->sc_ah
;
2173 struct ath_desc
*ds
;
2175 const HAL_RATE_TABLE
*rt
;
2178 DPRINTF(sc
, ATH_DEBUG_BEACON
, "%s: m %p len %u\n",
2179 __func__
, m
, m
->m_len
);
2181 /* setup descriptors */
2184 flags
= HAL_TXDESC_NOACK
;
2185 if (ic
->ic_opmode
== IEEE80211_M_IBSS
&& sc
->sc_hasveol
) {
2186 ds
->ds_link
= HTOAH32(bf
->bf_daddr
); /* self-linked */
2187 flags
|= HAL_TXDESC_VEOL
;
2189 * Let hardware handle antenna switching unless
2190 * the user has selected a transmit antenna
2191 * (sc_txantenna is not 0).
2193 antenna
= sc
->sc_txantenna
;
2197 * Switch antenna every 4 beacons, unless the user
2198 * has selected a transmit antenna (sc_txantenna
2201 * XXX assumes two antenna
2203 if (sc
->sc_txantenna
== 0)
2204 antenna
= (sc
->sc_stats
.ast_be_xmit
& 4 ? 2 : 1);
2206 antenna
= sc
->sc_txantenna
;
2209 KASSERT(bf
->bf_nseg
== 1,
2210 ("multi-segment beacon frame; nseg %u", bf
->bf_nseg
));
2211 ds
->ds_data
= bf
->bf_segs
[0].ds_addr
;
2213 * Calculate rate code.
2214 * XXX everything at min xmit rate
2216 rix
= sc
->sc_minrateix
;
2217 rt
= sc
->sc_currates
;
2218 rate
= rt
->info
[rix
].rateCode
;
2219 if (USE_SHPREAMBLE(ic
))
2220 rate
|= rt
->info
[rix
].shortPreamble
;
2221 ath_hal_setuptxdesc(ah
, ds
2222 , m
->m_len
+ IEEE80211_CRC_LEN
/* frame length */
2223 , sizeof(struct ieee80211_frame
)/* header length */
2224 , HAL_PKT_TYPE_BEACON
/* Atheros packet type */
2225 , ni
->ni_txpower
/* txpower XXX */
2226 , rate
, 1 /* series 0 rate/tries */
2227 , HAL_TXKEYIX_INVALID
/* no encryption */
2228 , antenna
/* antenna mode */
2229 , flags
/* no ack, veol for beacons */
2230 , 0 /* rts/cts rate */
2231 , 0 /* rts/cts duration */
2233 /* NB: beacon's BufLen must be a multiple of 4 bytes */
2234 ath_hal_filltxdesc(ah
, ds
2235 , roundup(m
->m_len
, 4) /* buffer length */
2236 , AH_TRUE
/* first segment */
2237 , AH_TRUE
/* last segment */
2238 , ds
/* first descriptor */
2241 /* NB: The desc swap function becomes void, if descriptor swapping
2246 #undef USE_SHPREAMBLE
2250 * Transmit a beacon frame at SWBA. Dynamic updates to the
2251 * frame contents are done as needed and the slot time is
2252 * also adjusted based on current state.
2255 ath_beacon_proc(void *arg
, int pending
)
2257 struct ath_softc
*sc
= arg
;
2258 struct ath_buf
*bf
= STAILQ_FIRST(&sc
->sc_bbuf
);
2259 struct ieee80211_node
*ni
= bf
->bf_node
;
2260 struct ieee80211com
*ic
= ni
->ni_ic
;
2261 struct ath_hal
*ah
= sc
->sc_ah
;
2263 int ncabq
, error
, otherant
;
2265 DPRINTF(sc
, ATH_DEBUG_BEACON_PROC
, "%s: pending %u\n",
2268 if (ic
->ic_opmode
== IEEE80211_M_STA
||
2269 ic
->ic_opmode
== IEEE80211_M_MONITOR
||
2270 bf
== NULL
|| bf
->bf_m
== NULL
) {
2271 DPRINTF(sc
, ATH_DEBUG_ANY
, "%s: ic_flags=%x bf=%p bf_m=%p\n",
2272 __func__
, ic
->ic_flags
, bf
, bf
? bf
->bf_m
: NULL
);
2276 * Check if the previous beacon has gone out. If
2277 * not don't try to post another, skip this period
2278 * and wait for the next. Missed beacons indicate
2279 * a problem and should not occur. If we miss too
2280 * many consecutive beacons reset the device.
2282 if (ath_hal_numtxpending(ah
, sc
->sc_bhalq
) != 0) {
2283 sc
->sc_bmisscount
++;
2284 DPRINTF(sc
, ATH_DEBUG_BEACON_PROC
,
2285 "%s: missed %u consecutive beacons\n",
2286 __func__
, sc
->sc_bmisscount
);
2287 if (sc
->sc_bmisscount
> 3) /* NB: 3 is a guess */
2288 TASK_RUN_OR_ENQUEUE(&sc
->sc_bstucktask
);
2291 if (sc
->sc_bmisscount
!= 0) {
2292 DPRINTF(sc
, ATH_DEBUG_BEACON
,
2293 "%s: resume beacon xmit after %u misses\n",
2294 __func__
, sc
->sc_bmisscount
);
2295 sc
->sc_bmisscount
= 0;
2299 * Update dynamic beacon contents. If this returns
2300 * non-zero then we need to remap the memory because
2301 * the beacon frame changed size (probably because
2302 * of the TIM bitmap).
2305 ncabq
= ath_hal_numtxpending(ah
, sc
->sc_cabq
->axq_qnum
);
2306 if (ieee80211_beacon_update(ic
, bf
->bf_node
, &sc
->sc_boff
, m
, ncabq
)) {
2307 /* XXX too conservative? */
2308 bus_dmamap_unload(sc
->sc_dmat
, bf
->bf_dmamap
);
2309 error
= bus_dmamap_load_mbuf(sc
->sc_dmat
, bf
->bf_dmamap
, m
,
2312 if_printf(&sc
->sc_if
,
2313 "%s: bus_dmamap_load_mbuf failed, error %u\n",
2320 * Handle slot time change when a non-ERP station joins/leaves
2321 * an 11g network. The 802.11 layer notifies us via callback,
2322 * we mark updateslot, then wait one beacon before effecting
2323 * the change. This gives associated stations at least one
2324 * beacon interval to note the state change.
2327 if (sc
->sc_updateslot
== UPDATE
)
2328 sc
->sc_updateslot
= COMMIT
; /* commit next beacon */
2329 else if (sc
->sc_updateslot
== COMMIT
)
2330 ath_setslottime(sc
); /* commit change to h/w */
2333 * Check recent per-antenna transmit statistics and flip
2334 * the default antenna if noticeably more frames went out
2335 * on the non-default antenna.
2336 * XXX assumes 2 anntenae
2338 otherant
= sc
->sc_defant
& 1 ? 2 : 1;
2339 if (sc
->sc_ant_tx
[otherant
] > sc
->sc_ant_tx
[sc
->sc_defant
] + 2)
2340 ath_setdefantenna(sc
, otherant
);
2341 sc
->sc_ant_tx
[1] = sc
->sc_ant_tx
[2] = 0;
2344 * Construct tx descriptor.
2346 ath_beacon_setup(sc
, bf
);
2349 * Stop any current dma and put the new frame on the queue.
2350 * This should never fail since we check above that no frames
2351 * are still pending on the queue.
2353 if (!ath_hal_stoptxdma(ah
, sc
->sc_bhalq
)) {
2354 DPRINTF(sc
, ATH_DEBUG_ANY
,
2355 "%s: beacon queue %u did not stop?\n",
2356 __func__
, sc
->sc_bhalq
);
2358 bus_dmamap_sync(sc
->sc_dmat
, bf
->bf_dmamap
, 0,
2359 bf
->bf_dmamap
->dm_mapsize
, BUS_DMASYNC_PREWRITE
);
2362 * Enable the CAB queue before the beacon queue to
2363 * insure cab frames are triggered by this beacon.
2365 if (ncabq
!= 0 && (sc
->sc_boff
.bo_tim
[4] & 1)) /* NB: only at DTIM */
2366 ath_hal_txstart(ah
, sc
->sc_cabq
->axq_qnum
);
2367 ath_hal_puttxbuf(ah
, sc
->sc_bhalq
, bf
->bf_daddr
);
2368 ath_hal_txstart(ah
, sc
->sc_bhalq
);
2369 DPRINTF(sc
, ATH_DEBUG_BEACON_PROC
,
2370 "%s: TXDP[%u] = %" PRIx64
" (%p)\n", __func__
,
2371 sc
->sc_bhalq
, (uint64_t)bf
->bf_daddr
, bf
->bf_desc
);
2373 sc
->sc_stats
.ast_be_xmit
++;
2377 * Reset the hardware after detecting beacons have stopped.
2380 ath_bstuck_proc(void *arg
, int pending
)
2382 struct ath_softc
*sc
= arg
;
2383 struct ifnet
*ifp
= &sc
->sc_if
;
2385 if_printf(ifp
, "stuck beacon; resetting (bmiss count %u)\n",
2391 * Reclaim beacon resources.
2394 ath_beacon_free(struct ath_softc
*sc
)
2398 STAILQ_FOREACH(bf
, &sc
->sc_bbuf
, bf_list
) {
2399 if (bf
->bf_m
!= NULL
) {
2400 bus_dmamap_unload(sc
->sc_dmat
, bf
->bf_dmamap
);
2404 if (bf
->bf_node
!= NULL
) {
2405 ieee80211_free_node(bf
->bf_node
);
2412 * Configure the beacon and sleep timers.
2414 * When operating as an AP this resets the TSF and sets
2415 * up the hardware to notify us when we need to issue beacons.
2417 * When operating in station mode this sets up the beacon
2418 * timers according to the timestamp of the last received
2419 * beacon and the current TSF, configures PCF and DTIM
2420 * handling, programs the sleep registers so the hardware
2421 * will wakeup in time to receive beacons, and configures
2422 * the beacon miss handling so we'll receive a BMISS
2423 * interrupt when we stop seeing beacons from the AP
2424 * we've associated with.
2427 ath_beacon_config(struct ath_softc
*sc
)
2429 #define TSF_TO_TU(_h,_l) \
2430 ((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
2432 struct ath_hal
*ah
= sc
->sc_ah
;
2433 struct ieee80211com
*ic
= &sc
->sc_ic
;
2434 struct ieee80211_node
*ni
= ic
->ic_bss
;
2435 u_int32_t nexttbtt
, intval
, tsftu
;
2438 /* extract tstamp from last beacon and convert to TU */
2439 nexttbtt
= TSF_TO_TU(LE_READ_4(ni
->ni_tstamp
.data
+ 4),
2440 LE_READ_4(ni
->ni_tstamp
.data
));
2441 /* NB: the beacon interval is kept internally in TU's */
2442 intval
= ni
->ni_intval
& HAL_BEACON_PERIOD
;
2443 if (nexttbtt
== 0) /* e.g. for ap mode */
2445 else if (intval
) /* NB: can be 0 for monitor mode */
2446 nexttbtt
= roundup(nexttbtt
, intval
);
2447 DPRINTF(sc
, ATH_DEBUG_BEACON
, "%s: nexttbtt %u intval %u (%u)\n",
2448 __func__
, nexttbtt
, intval
, ni
->ni_intval
);
2449 if (ic
->ic_opmode
== IEEE80211_M_STA
) {
2450 HAL_BEACON_STATE bs
;
2451 int dtimperiod
, dtimcount
;
2452 int cfpperiod
, cfpcount
;
2455 * Setup dtim and cfp parameters according to
2456 * last beacon we received (which may be none).
2458 dtimperiod
= ni
->ni_dtim_period
;
2459 if (dtimperiod
<= 0) /* NB: 0 if not known */
2461 dtimcount
= ni
->ni_dtim_count
;
2462 if (dtimcount
>= dtimperiod
) /* NB: sanity check */
2463 dtimcount
= 0; /* XXX? */
2464 cfpperiod
= 1; /* NB: no PCF support yet */
2467 * Pull nexttbtt forward to reflect the current
2468 * TSF and calculate dtim+cfp state for the result.
2470 tsf
= ath_hal_gettsf64(ah
);
2471 tsftu
= TSF_TO_TU(tsf
>>32, tsf
) + FUDGE
;
2474 if (--dtimcount
< 0) {
2475 dtimcount
= dtimperiod
- 1;
2477 cfpcount
= cfpperiod
- 1;
2479 } while (nexttbtt
< tsftu
);
2480 memset(&bs
, 0, sizeof(bs
));
2481 bs
.bs_intval
= intval
;
2482 bs
.bs_nexttbtt
= nexttbtt
;
2483 bs
.bs_dtimperiod
= dtimperiod
*intval
;
2484 bs
.bs_nextdtim
= bs
.bs_nexttbtt
+ dtimcount
*intval
;
2485 bs
.bs_cfpperiod
= cfpperiod
*bs
.bs_dtimperiod
;
2486 bs
.bs_cfpnext
= bs
.bs_nextdtim
+ cfpcount
*bs
.bs_dtimperiod
;
2487 bs
.bs_cfpmaxduration
= 0;
2490 * The 802.11 layer records the offset to the DTIM
2491 * bitmap while receiving beacons; use it here to
2492 * enable h/w detection of our AID being marked in
2493 * the bitmap vector (to indicate frames for us are
2494 * pending at the AP).
2495 * XXX do DTIM handling in s/w to WAR old h/w bugs
2496 * XXX enable based on h/w rev for newer chips
2498 bs
.bs_timoffset
= ni
->ni_timoff
;
2501 * Calculate the number of consecutive beacons to miss
2502 * before taking a BMISS interrupt. The configuration
2503 * is specified in ms, so we need to convert that to
2504 * TU's and then calculate based on the beacon interval.
2505 * Note that we clamp the result to at most 10 beacons.
2507 bs
.bs_bmissthreshold
= howmany(ic
->ic_bmisstimeout
, intval
);
2508 if (bs
.bs_bmissthreshold
> 10)
2509 bs
.bs_bmissthreshold
= 10;
2510 else if (bs
.bs_bmissthreshold
<= 0)
2511 bs
.bs_bmissthreshold
= 1;
2514 * Calculate sleep duration. The configuration is
2515 * given in ms. We insure a multiple of the beacon
2516 * period is used. Also, if the sleep duration is
2517 * greater than the DTIM period then it makes senses
2518 * to make it a multiple of that.
2520 * XXX fixed at 100ms
2522 bs
.bs_sleepduration
=
2523 roundup(IEEE80211_MS_TO_TU(100), bs
.bs_intval
);
2524 if (bs
.bs_sleepduration
> bs
.bs_dtimperiod
)
2525 bs
.bs_sleepduration
= roundup(bs
.bs_sleepduration
, bs
.bs_dtimperiod
);
2527 DPRINTF(sc
, ATH_DEBUG_BEACON
,
2528 "%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"
2535 , bs
.bs_bmissthreshold
2536 , bs
.bs_sleepduration
2538 , bs
.bs_cfpmaxduration
2542 ath_hal_intrset(ah
, 0);
2543 ath_hal_beacontimers(ah
, &bs
);
2544 sc
->sc_imask
|= HAL_INT_BMISS
;
2545 ath_hal_intrset(ah
, sc
->sc_imask
);
2547 ath_hal_intrset(ah
, 0);
2548 if (nexttbtt
== intval
)
2549 intval
|= HAL_BEACON_RESET_TSF
;
2550 if (ic
->ic_opmode
== IEEE80211_M_IBSS
) {
2552 * In IBSS mode enable the beacon timers but only
2553 * enable SWBA interrupts if we need to manually
2554 * prepare beacon frames. Otherwise we use a
2555 * self-linked tx descriptor and let the hardware
2558 intval
|= HAL_BEACON_ENA
;
2559 if (!sc
->sc_hasveol
)
2560 sc
->sc_imask
|= HAL_INT_SWBA
;
2561 if ((intval
& HAL_BEACON_RESET_TSF
) == 0) {
2563 * Pull nexttbtt forward to reflect
2566 tsf
= ath_hal_gettsf64(ah
);
2567 tsftu
= TSF_TO_TU(tsf
>>32, tsf
) + FUDGE
;
2570 } while (nexttbtt
< tsftu
);
2572 ath_beaconq_config(sc
);
2573 } else if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
) {
2575 * In AP mode we enable the beacon timers and
2576 * SWBA interrupts to prepare beacon frames.
2578 intval
|= HAL_BEACON_ENA
;
2579 sc
->sc_imask
|= HAL_INT_SWBA
; /* beacon prepare */
2580 ath_beaconq_config(sc
);
2582 ath_hal_beaconinit(ah
, nexttbtt
, intval
);
2583 sc
->sc_bmisscount
= 0;
2584 ath_hal_intrset(ah
, sc
->sc_imask
);
2586 * When using a self-linked beacon descriptor in
2587 * ibss mode load it once here.
2589 if (ic
->ic_opmode
== IEEE80211_M_IBSS
&& sc
->sc_hasveol
)
2590 ath_beacon_proc(sc
, 0);
2592 sc
->sc_syncbeacon
= 0;
2598 ath_descdma_setup(struct ath_softc
*sc
,
2599 struct ath_descdma
*dd
, ath_bufhead
*head
,
2600 const char *name
, int nbuf
, int ndesc
)
2602 #define DS2PHYS(_dd, _ds) \
2603 ((_dd)->dd_desc_paddr + ((char *)(_ds) - (char *)(_dd)->dd_desc))
2604 struct ifnet
*ifp
= &sc
->sc_if
;
2605 struct ath_desc
*ds
;
2607 int i
, bsize
, error
;
2609 DPRINTF(sc
, ATH_DEBUG_RESET
, "%s: %s DMA: %u buffers %u desc/buf\n",
2610 __func__
, name
, nbuf
, ndesc
);
2613 dd
->dd_desc_len
= sizeof(struct ath_desc
) * nbuf
* ndesc
;
2616 * Setup DMA descriptor area.
2618 dd
->dd_dmat
= sc
->sc_dmat
;
2620 error
= bus_dmamem_alloc(dd
->dd_dmat
, dd
->dd_desc_len
, PAGE_SIZE
,
2621 0, &dd
->dd_dseg
, 1, &dd
->dd_dnseg
, 0);
2624 if_printf(ifp
, "unable to alloc memory for %u %s descriptors, "
2625 "error %u\n", nbuf
* ndesc
, dd
->dd_name
, error
);
2629 error
= bus_dmamem_map(dd
->dd_dmat
, &dd
->dd_dseg
, dd
->dd_dnseg
,
2630 dd
->dd_desc_len
, (void **)&dd
->dd_desc
, BUS_DMA_COHERENT
);
2632 if_printf(ifp
, "unable to map %u %s descriptors, error = %u\n",
2633 nbuf
* ndesc
, dd
->dd_name
, error
);
2637 /* allocate descriptors */
2638 error
= bus_dmamap_create(dd
->dd_dmat
, dd
->dd_desc_len
, 1,
2639 dd
->dd_desc_len
, 0, BUS_DMA_NOWAIT
, &dd
->dd_dmamap
);
2641 if_printf(ifp
, "unable to create dmamap for %s descriptors, "
2642 "error %u\n", dd
->dd_name
, error
);
2646 error
= bus_dmamap_load(dd
->dd_dmat
, dd
->dd_dmamap
, dd
->dd_desc
,
2647 dd
->dd_desc_len
, NULL
, BUS_DMA_NOWAIT
);
2649 if_printf(ifp
, "unable to map %s descriptors, error %u\n",
2650 dd
->dd_name
, error
);
2655 dd
->dd_desc_paddr
= dd
->dd_dmamap
->dm_segs
[0].ds_addr
;
2656 DPRINTF(sc
, ATH_DEBUG_RESET
,
2657 "%s: %s DMA map: %p (%lu) -> %" PRIx64
" (%lu)\n",
2658 __func__
, dd
->dd_name
, ds
, (u_long
) dd
->dd_desc_len
,
2659 (uint64_t) dd
->dd_desc_paddr
, /*XXX*/ (u_long
) dd
->dd_desc_len
);
2661 /* allocate rx buffers */
2662 bsize
= sizeof(struct ath_buf
) * nbuf
;
2663 bf
= malloc(bsize
, M_ATHDEV
, M_NOWAIT
| M_ZERO
);
2665 if_printf(ifp
, "malloc of %s buffers failed, size %u\n",
2666 dd
->dd_name
, bsize
);
2672 for (i
= 0; i
< nbuf
; i
++, bf
++, ds
+= ndesc
) {
2674 bf
->bf_daddr
= DS2PHYS(dd
, ds
);
2675 error
= bus_dmamap_create(sc
->sc_dmat
, MCLBYTES
, ndesc
,
2676 MCLBYTES
, 0, BUS_DMA_NOWAIT
, &bf
->bf_dmamap
);
2678 if_printf(ifp
, "unable to create dmamap for %s "
2679 "buffer %u, error %u\n", dd
->dd_name
, i
, error
);
2680 ath_descdma_cleanup(sc
, dd
, head
);
2683 STAILQ_INSERT_TAIL(head
, bf
, bf_list
);
2687 bus_dmamap_unload(dd
->dd_dmat
, dd
->dd_dmamap
);
2689 bus_dmamap_destroy(dd
->dd_dmat
, dd
->dd_dmamap
);
2691 bus_dmamem_unmap(dd
->dd_dmat
, (void *)dd
->dd_desc
, dd
->dd_desc_len
);
2693 bus_dmamem_free(dd
->dd_dmat
, &dd
->dd_dseg
, dd
->dd_dnseg
);
2695 memset(dd
, 0, sizeof(*dd
));
2701 ath_descdma_cleanup(struct ath_softc
*sc
,
2702 struct ath_descdma
*dd
, ath_bufhead
*head
)
2705 struct ieee80211_node
*ni
;
2707 bus_dmamap_unload(dd
->dd_dmat
, dd
->dd_dmamap
);
2708 bus_dmamap_destroy(dd
->dd_dmat
, dd
->dd_dmamap
);
2709 bus_dmamem_unmap(dd
->dd_dmat
, (void *)dd
->dd_desc
, dd
->dd_desc_len
);
2710 bus_dmamem_free(dd
->dd_dmat
, &dd
->dd_dseg
, dd
->dd_dnseg
);
2712 STAILQ_FOREACH(bf
, head
, bf_list
) {
2717 if (bf
->bf_dmamap
!= NULL
) {
2718 bus_dmamap_destroy(sc
->sc_dmat
, bf
->bf_dmamap
);
2719 bf
->bf_dmamap
= NULL
;
2725 * Reclaim node reference.
2727 ieee80211_free_node(ni
);
2732 free(dd
->dd_bufptr
, M_ATHDEV
);
2733 memset(dd
, 0, sizeof(*dd
));
2737 ath_desc_alloc(struct ath_softc
*sc
)
2741 error
= ath_descdma_setup(sc
, &sc
->sc_rxdma
, &sc
->sc_rxbuf
,
2742 "rx", ath_rxbuf
, 1);
2746 error
= ath_descdma_setup(sc
, &sc
->sc_txdma
, &sc
->sc_txbuf
,
2747 "tx", ath_txbuf
, ATH_TXDESC
);
2749 ath_descdma_cleanup(sc
, &sc
->sc_rxdma
, &sc
->sc_rxbuf
);
2753 error
= ath_descdma_setup(sc
, &sc
->sc_bdma
, &sc
->sc_bbuf
,
2756 ath_descdma_cleanup(sc
, &sc
->sc_txdma
, &sc
->sc_txbuf
);
2757 ath_descdma_cleanup(sc
, &sc
->sc_rxdma
, &sc
->sc_rxbuf
);
2764 ath_desc_free(struct ath_softc
*sc
)
2767 if (sc
->sc_bdma
.dd_desc_len
!= 0)
2768 ath_descdma_cleanup(sc
, &sc
->sc_bdma
, &sc
->sc_bbuf
);
2769 if (sc
->sc_txdma
.dd_desc_len
!= 0)
2770 ath_descdma_cleanup(sc
, &sc
->sc_txdma
, &sc
->sc_txbuf
);
2771 if (sc
->sc_rxdma
.dd_desc_len
!= 0)
2772 ath_descdma_cleanup(sc
, &sc
->sc_rxdma
, &sc
->sc_rxbuf
);
2775 static struct ieee80211_node
*
2776 ath_node_alloc(struct ieee80211_node_table
*nt
)
2778 struct ieee80211com
*ic
= nt
->nt_ic
;
2779 struct ath_softc
*sc
= ic
->ic_ifp
->if_softc
;
2780 const size_t space
= sizeof(struct ath_node
) + sc
->sc_rc
->arc_space
;
2781 struct ath_node
*an
;
2783 an
= malloc(space
, M_80211_NODE
, M_NOWAIT
|M_ZERO
);
2788 an
->an_avgrssi
= ATH_RSSI_DUMMY_MARKER
;
2789 ath_rate_node_init(sc
, an
);
2791 DPRINTF(sc
, ATH_DEBUG_NODE
, "%s: an %p\n", __func__
, an
);
2792 return &an
->an_node
;
2796 ath_node_free(struct ieee80211_node
*ni
)
2798 struct ieee80211com
*ic
= ni
->ni_ic
;
2799 struct ath_softc
*sc
= ic
->ic_ifp
->if_softc
;
2801 DPRINTF(sc
, ATH_DEBUG_NODE
, "%s: ni %p\n", __func__
, ni
);
2803 ath_rate_node_cleanup(sc
, ATH_NODE(ni
));
2804 sc
->sc_node_free(ni
);
2808 ath_node_getrssi(const struct ieee80211_node
*ni
)
2810 #define HAL_EP_RND(x, mul) \
2811 ((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))
2812 u_int32_t avgrssi
= ATH_NODE_CONST(ni
)->an_avgrssi
;
2816 * When only one frame is received there will be no state in
2817 * avgrssi so fallback on the value recorded by the 802.11 layer.
2819 if (avgrssi
!= ATH_RSSI_DUMMY_MARKER
)
2820 rssi
= HAL_EP_RND(avgrssi
, HAL_RSSI_EP_MULTIPLIER
);
2823 return rssi
< 0 ? 0 : rssi
> 127 ? 127 : rssi
;
2828 ath_rxbuf_init(struct ath_softc
*sc
, struct ath_buf
*bf
)
2830 struct ath_hal
*ah
= sc
->sc_ah
;
2833 struct ath_desc
*ds
;
2838 * NB: by assigning a page to the rx dma buffer we
2839 * implicitly satisfy the Atheros requirement that
2840 * this buffer be cache-line-aligned and sized to be
2841 * multiple of the cache line size. Not doing this
2842 * causes weird stuff to happen (for the 5210 at least).
2844 m
= m_getcl(M_DONTWAIT
, MT_DATA
, M_PKTHDR
);
2846 DPRINTF(sc
, ATH_DEBUG_ANY
,
2847 "%s: no mbuf/cluster\n", __func__
);
2848 sc
->sc_stats
.ast_rx_nombuf
++;
2852 m
->m_pkthdr
.len
= m
->m_len
= m
->m_ext
.ext_size
;
2854 error
= bus_dmamap_load_mbuf(sc
->sc_dmat
,
2858 DPRINTF(sc
, ATH_DEBUG_ANY
,
2859 "%s: bus_dmamap_load_mbuf failed; error %d\n",
2861 sc
->sc_stats
.ast_rx_busdma
++;
2864 KASSERT(bf
->bf_nseg
== 1,
2865 ("multi-segment packet; nseg %u", bf
->bf_nseg
));
2867 bus_dmamap_sync(sc
->sc_dmat
, bf
->bf_dmamap
, 0,
2868 bf
->bf_dmamap
->dm_mapsize
, BUS_DMASYNC_PREREAD
);
2871 * Setup descriptors. For receive we always terminate
2872 * the descriptor list with a self-linked entry so we'll
2873 * not get overrun under high load (as can happen with a
2874 * 5212 when ANI processing enables PHY error frames).
2876 * To insure the last descriptor is self-linked we create
2877 * each descriptor as self-linked and add it to the end. As
2878 * each additional descriptor is added the previous self-linked
2879 * entry is ``fixed'' naturally. This should be safe even
2880 * if DMA is happening. When processing RX interrupts we
2881 * never remove/process the last, self-linked, entry on the
2882 * descriptor list. This insures the hardware always has
2883 * someplace to write a new frame.
2886 ds
->ds_link
= HTOAH32(bf
->bf_daddr
); /* link to self */
2887 ds
->ds_data
= bf
->bf_segs
[0].ds_addr
;
2888 /* ds->ds_vdata = mtod(m, void *); for radar */
2889 ath_hal_setuprxdesc(ah
, ds
2890 , m
->m_len
/* buffer size */
2894 if (sc
->sc_rxlink
!= NULL
)
2895 *sc
->sc_rxlink
= bf
->bf_daddr
;
2896 sc
->sc_rxlink
= &ds
->ds_link
;
2901 * Extend 15-bit time stamp from rx descriptor to
2902 * a full 64-bit TSF using the specified TSF.
2904 static inline u_int64_t
2905 ath_extend_tsf(u_int32_t rstamp
, u_int64_t tsf
)
2907 if ((tsf
& 0x7fff) < rstamp
)
2909 return ((tsf
&~ 0x7fff) | rstamp
);
2913 * Intercept management frames to collect beacon rssi data
2914 * and to do ibss merges.
2917 ath_recv_mgmt(struct ieee80211com
*ic
, struct mbuf
*m
,
2918 struct ieee80211_node
*ni
,
2919 int subtype
, int rssi
, u_int32_t rstamp
)
2921 struct ath_softc
*sc
= ic
->ic_ifp
->if_softc
;
2924 * Call up first so subsequent work can use information
2925 * potentially stored in the node (e.g. for ibss merge).
2927 sc
->sc_recv_mgmt(ic
, m
, ni
, subtype
, rssi
, rstamp
);
2929 case IEEE80211_FC0_SUBTYPE_BEACON
:
2930 /* update rssi statistics for use by the hal */
2931 ATH_RSSI_LPF(sc
->sc_halstats
.ns_avgbrssi
, rssi
);
2932 if (sc
->sc_syncbeacon
&&
2933 ni
== ic
->ic_bss
&& ic
->ic_state
== IEEE80211_S_RUN
) {
2935 * Resync beacon timers using the tsf of the beacon
2936 * frame we just received.
2938 ath_beacon_config(sc
);
2941 case IEEE80211_FC0_SUBTYPE_PROBE_RESP
:
2942 if (ic
->ic_opmode
== IEEE80211_M_IBSS
&&
2943 ic
->ic_state
== IEEE80211_S_RUN
) {
2944 u_int64_t tsf
= ath_extend_tsf(rstamp
,
2945 ath_hal_gettsf64(sc
->sc_ah
));
2948 * Handle ibss merge as needed; check the tsf on the
2949 * frame before attempting the merge. The 802.11 spec
2950 * says the station should change it's bssid to match
2951 * the oldest station with the same ssid, where oldest
2952 * is determined by the tsf. Note that hardware
2953 * reconfiguration happens through callback to
2954 * ath_newstate as the state machine will go from
2955 * RUN -> RUN when this happens.
2957 if (le64toh(ni
->ni_tstamp
.tsf
) >= tsf
) {
2958 DPRINTF(sc
, ATH_DEBUG_STATE
,
2959 "ibss merge, rstamp %u tsf %ju "
2960 "tstamp %ju\n", rstamp
, (uintmax_t)tsf
,
2961 (uintmax_t)ni
->ni_tstamp
.tsf
);
2962 (void) ieee80211_ibss_merge(ni
);
2970 * Set the default antenna.
2973 ath_setdefantenna(struct ath_softc
*sc
, u_int antenna
)
2975 struct ath_hal
*ah
= sc
->sc_ah
;
2977 /* XXX block beacon interrupts */
2978 ath_hal_setdefantenna(ah
, antenna
);
2979 if (sc
->sc_defant
!= antenna
)
2980 sc
->sc_stats
.ast_ant_defswitch
++;
2981 sc
->sc_defant
= antenna
;
2982 sc
->sc_rxotherant
= 0;
2986 ath_handle_micerror(struct ieee80211com
*ic
,
2987 struct ieee80211_frame
*wh
, int keyix
)
2989 struct ieee80211_node
*ni
;
2991 /* XXX recheck MIC to deal w/ chips that lie */
2992 /* XXX discard MIC errors on !data frames */
2993 ni
= ieee80211_find_rxnode_withkey(ic
, (const struct ieee80211_frame_min
*) wh
, keyix
);
2995 ieee80211_notify_michael_failure(ic
, wh
, keyix
);
2996 ieee80211_free_node(ni
);
3001 ath_rx_proc(void *arg
, int npending
)
3003 #define PA2DESC(_sc, _pa) \
3004 ((struct ath_desc *)((char *)(_sc)->sc_rxdma.dd_desc + \
3005 ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
3006 struct ath_softc
*sc
= arg
;
3008 struct ieee80211com
*ic
= &sc
->sc_ic
;
3009 struct ifnet
*ifp
= &sc
->sc_if
;
3010 struct ath_hal
*ah
= sc
->sc_ah
;
3011 struct ath_desc
*ds
;
3013 struct ieee80211_node
*ni
;
3014 struct ath_node
*an
;
3015 int len
, ngood
, type
;
3020 uint8_t rxerr_tap
, rxerr_mon
;
3022 NET_LOCK_GIANT(); /* XXX */
3025 (ifp
->if_flags
& IFF_PROMISC
) ? HAL_RXERR_CRC
|HAL_RXERR_PHY
: 0;
3027 if (sc
->sc_ic
.ic_opmode
== IEEE80211_M_MONITOR
)
3028 rxerr_mon
= HAL_RXERR_DECRYPT
|HAL_RXERR_MIC
;
3029 else if (ifp
->if_flags
& IFF_PROMISC
)
3030 rxerr_tap
|= HAL_RXERR_DECRYPT
|HAL_RXERR_MIC
;
3032 DPRINTF(sc
, ATH_DEBUG_RX_PROC
, "%s: pending %u\n", __func__
, npending
);
3034 nf
= ath_hal_getchannoise(ah
, &sc
->sc_curchan
);
3035 tsf
= ath_hal_gettsf64(ah
);
3037 bf
= STAILQ_FIRST(&sc
->sc_rxbuf
);
3038 if (bf
== NULL
) { /* NB: shouldn't happen */
3039 if_printf(ifp
, "%s: no buffer!\n", __func__
);
3043 if (ds
->ds_link
== bf
->bf_daddr
) {
3044 /* NB: never process the self-linked entry at the end */
3048 if (m
== NULL
) { /* NB: shouldn't happen */
3049 if_printf(ifp
, "%s: no mbuf!\n", __func__
);
3052 /* XXX sync descriptor memory */
3054 * Must provide the virtual address of the current
3055 * descriptor, the physical address, and the virtual
3056 * address of the next descriptor in the h/w chain.
3057 * This allows the HAL to look ahead to see if the
3058 * hardware is done with a descriptor by checking the
3059 * done bit in the following descriptor and the address
3060 * of the current descriptor the DMA engine is working
3061 * on. All this is necessary because of our use of
3062 * a self-linked list to avoid rx overruns.
3064 status
= ath_hal_rxprocdesc(ah
, ds
,
3065 bf
->bf_daddr
, PA2DESC(sc
, ds
->ds_link
),
3068 if (sc
->sc_debug
& ATH_DEBUG_RECV_DESC
)
3069 ath_printrxbuf(bf
, status
== HAL_OK
);
3071 if (status
== HAL_EINPROGRESS
)
3073 STAILQ_REMOVE_HEAD(&sc
->sc_rxbuf
, bf_list
);
3074 if (ds
->ds_rxstat
.rs_more
) {
3076 * Frame spans multiple descriptors; this
3077 * cannot happen yet as we don't support
3078 * jumbograms. If not in monitor mode,
3079 * discard the frame.
3081 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
3082 sc
->sc_stats
.ast_rx_toobig
++;
3085 /* fall thru for monitor mode handling... */
3086 } else if (ds
->ds_rxstat
.rs_status
!= 0) {
3087 if (ds
->ds_rxstat
.rs_status
& HAL_RXERR_CRC
)
3088 sc
->sc_stats
.ast_rx_crcerr
++;
3089 if (ds
->ds_rxstat
.rs_status
& HAL_RXERR_FIFO
)
3090 sc
->sc_stats
.ast_rx_fifoerr
++;
3091 if (ds
->ds_rxstat
.rs_status
& HAL_RXERR_PHY
) {
3092 sc
->sc_stats
.ast_rx_phyerr
++;
3093 phyerr
= ds
->ds_rxstat
.rs_phyerr
& 0x1f;
3094 sc
->sc_stats
.ast_rx_phy
[phyerr
]++;
3097 if (ds
->ds_rxstat
.rs_status
& HAL_RXERR_DECRYPT
) {
3099 * Decrypt error. If the error occurred
3100 * because there was no hardware key, then
3101 * let the frame through so the upper layers
3102 * can process it. This is necessary for 5210
3103 * parts which have no way to setup a ``clear''
3106 * XXX do key cache faulting
3108 if (ds
->ds_rxstat
.rs_keyix
== HAL_RXKEYIX_INVALID
)
3110 sc
->sc_stats
.ast_rx_badcrypt
++;
3112 if (ds
->ds_rxstat
.rs_status
& HAL_RXERR_MIC
) {
3113 sc
->sc_stats
.ast_rx_badmic
++;
3115 * Do minimal work required to hand off
3116 * the 802.11 header for notifcation.
3118 /* XXX frag's and qos frames */
3119 len
= ds
->ds_rxstat
.rs_datalen
;
3120 if (len
>= sizeof (struct ieee80211_frame
)) {
3121 bus_dmamap_sync(sc
->sc_dmat
,
3123 0, bf
->bf_dmamap
->dm_mapsize
,
3124 BUS_DMASYNC_POSTREAD
);
3125 ath_handle_micerror(ic
,
3126 mtod(m
, struct ieee80211_frame
*),
3128 ds
->ds_rxstat
.rs_keyix
-32 : ds
->ds_rxstat
.rs_keyix
);
3133 * Reject error frames, we normally don't want
3134 * to see them in monitor mode (in monitor mode
3135 * allow through packets that have crypto problems).
3138 if (ds
->ds_rxstat
.rs_status
&~ (rxerr_tap
|rxerr_mon
))
3143 * Sync and unmap the frame. At this point we're
3144 * committed to passing the mbuf somewhere so clear
3145 * bf_m; this means a new sk_buff must be allocated
3146 * when the rx descriptor is setup again to receive
3149 bus_dmamap_sync(sc
->sc_dmat
, bf
->bf_dmamap
,
3150 0, bf
->bf_dmamap
->dm_mapsize
,
3151 BUS_DMASYNC_POSTREAD
);
3152 bus_dmamap_unload(sc
->sc_dmat
, bf
->bf_dmamap
);
3155 m
->m_pkthdr
.rcvif
= ifp
;
3156 len
= ds
->ds_rxstat
.rs_datalen
;
3157 m
->m_pkthdr
.len
= m
->m_len
= len
;
3159 sc
->sc_stats
.ast_ant_rx
[ds
->ds_rxstat
.rs_antenna
]++;
3162 if (sc
->sc_drvbpf
) {
3166 * Discard anything shorter than an ack or cts.
3168 if (len
< IEEE80211_ACK_LEN
) {
3169 DPRINTF(sc
, ATH_DEBUG_RECV
,
3170 "%s: runt packet %d\n",
3172 sc
->sc_stats
.ast_rx_tooshort
++;
3176 rix
= ds
->ds_rxstat
.rs_rate
;
3177 sc
->sc_rx_th
.wr_tsf
= htole64(
3178 ath_extend_tsf(ds
->ds_rxstat
.rs_tstamp
, tsf
));
3179 sc
->sc_rx_th
.wr_flags
= sc
->sc_hwmap
[rix
].rxflags
;
3180 if (ds
->ds_rxstat
.rs_status
&
3181 (HAL_RXERR_CRC
|HAL_RXERR_PHY
)) {
3182 sc
->sc_rx_th
.wr_flags
|=
3183 IEEE80211_RADIOTAP_F_BADFCS
;
3185 sc
->sc_rx_th
.wr_rate
= sc
->sc_hwmap
[rix
].ieeerate
;
3186 sc
->sc_rx_th
.wr_antsignal
= ds
->ds_rxstat
.rs_rssi
+ nf
;
3187 sc
->sc_rx_th
.wr_antnoise
= nf
;
3188 sc
->sc_rx_th
.wr_antenna
= ds
->ds_rxstat
.rs_antenna
;
3190 bpf_mtap2(sc
->sc_drvbpf
,
3191 &sc
->sc_rx_th
, sc
->sc_rx_th_len
, m
);
3195 if (ds
->ds_rxstat
.rs_status
& rxerr_tap
) {
3200 * From this point on we assume the frame is at least
3201 * as large as ieee80211_frame_min; verify that.
3203 if (len
< IEEE80211_MIN_LEN
) {
3204 DPRINTF(sc
, ATH_DEBUG_RECV
, "%s: short packet %d\n",
3206 sc
->sc_stats
.ast_rx_tooshort
++;
3211 if (IFF_DUMPPKTS(sc
, ATH_DEBUG_RECV
)) {
3212 ieee80211_dump_pkt(mtod(m
, void *), len
,
3213 sc
->sc_hwmap
[ds
->ds_rxstat
.rs_rate
].ieeerate
,
3214 ds
->ds_rxstat
.rs_rssi
);
3217 m_adj(m
, -IEEE80211_CRC_LEN
);
3220 * Locate the node for sender, track state, and then
3221 * pass the (referenced) node up to the 802.11 layer
3224 ni
= ieee80211_find_rxnode_withkey(ic
,
3225 mtod(m
, const struct ieee80211_frame_min
*),
3226 ds
->ds_rxstat
.rs_keyix
== HAL_RXKEYIX_INVALID
?
3227 IEEE80211_KEYIX_NONE
: ds
->ds_rxstat
.rs_keyix
);
3229 * Track rx rssi and do any rx antenna management.
3232 ATH_RSSI_LPF(an
->an_avgrssi
, ds
->ds_rxstat
.rs_rssi
);
3233 ATH_RSSI_LPF(sc
->sc_halstats
.ns_avgrssi
, ds
->ds_rxstat
.rs_rssi
);
3235 * Send frame up for processing.
3237 type
= ieee80211_input(ic
, m
, ni
,
3238 ds
->ds_rxstat
.rs_rssi
, ds
->ds_rxstat
.rs_tstamp
);
3239 ieee80211_free_node(ni
);
3240 if (sc
->sc_diversity
) {
3242 * When using fast diversity, change the default rx
3243 * antenna if diversity chooses the other antenna 3
3246 if (sc
->sc_defant
!= ds
->ds_rxstat
.rs_antenna
) {
3247 if (++sc
->sc_rxotherant
>= 3)
3248 ath_setdefantenna(sc
,
3249 ds
->ds_rxstat
.rs_antenna
);
3251 sc
->sc_rxotherant
= 0;
3253 if (sc
->sc_softled
) {
3255 * Blink for any data frame. Otherwise do a
3256 * heartbeat-style blink when idle. The latter
3257 * is mainly for station mode where we depend on
3258 * periodic beacon frames to trigger the poll event.
3260 if (type
== IEEE80211_FC0_TYPE_DATA
) {
3261 sc
->sc_rxrate
= ds
->ds_rxstat
.rs_rate
;
3262 ath_led_event(sc
, ATH_LED_RX
);
3263 } else if (ticks
- sc
->sc_ledevent
>= sc
->sc_ledidle
)
3264 ath_led_event(sc
, ATH_LED_POLL
);
3267 * Arrange to update the last rx timestamp only for
3268 * frames from our ap when operating in station mode.
3269 * This assumes the rx key is always setup when associated.
3271 if (ic
->ic_opmode
== IEEE80211_M_STA
&&
3272 ds
->ds_rxstat
.rs_keyix
!= HAL_RXKEYIX_INVALID
)
3275 STAILQ_INSERT_TAIL(&sc
->sc_rxbuf
, bf
, bf_list
);
3276 } while (ath_rxbuf_init(sc
, bf
) == 0);
3278 /* rx signal state monitoring */
3279 ath_hal_rxmonitor(ah
, &sc
->sc_halstats
, &sc
->sc_curchan
);
3281 if (ath_hal_radar_event(ah
))
3282 TASK_RUN_OR_ENQUEUE(&sc
->sc_radartask
);
3285 sc
->sc_lastrx
= tsf
;
3288 /* XXX Why isn't this necessary in FreeBSD? */
3289 if ((ifp
->if_flags
& IFF_OACTIVE
) == 0 && !IFQ_IS_EMPTY(&ifp
->if_snd
))
3291 #endif /* __NetBSD__ */
3293 NET_UNLOCK_GIANT(); /* XXX */
3298 * Setup a h/w transmit queue.
3300 static struct ath_txq
*
3301 ath_txq_setup(struct ath_softc
*sc
, int qtype
, int subtype
)
3303 #define N(a) (sizeof(a)/sizeof(a[0]))
3304 struct ath_hal
*ah
= sc
->sc_ah
;
3308 memset(&qi
, 0, sizeof(qi
));
3309 qi
.tqi_subtype
= subtype
;
3310 qi
.tqi_aifs
= HAL_TXQ_USEDEFAULT
;
3311 qi
.tqi_cwmin
= HAL_TXQ_USEDEFAULT
;
3312 qi
.tqi_cwmax
= HAL_TXQ_USEDEFAULT
;
3314 * Enable interrupts only for EOL and DESC conditions.
3315 * We mark tx descriptors to receive a DESC interrupt
3316 * when a tx queue gets deep; otherwise waiting for the
3317 * EOL to reap descriptors. Note that this is done to
3318 * reduce interrupt load and this only defers reaping
3319 * descriptors, never transmitting frames. Aside from
3320 * reducing interrupts this also permits more concurrency.
3321 * The only potential downside is if the tx queue backs
3322 * up in which case the top half of the kernel may backup
3323 * due to a lack of tx descriptors.
3325 qi
.tqi_qflags
= HAL_TXQ_TXEOLINT_ENABLE
| HAL_TXQ_TXDESCINT_ENABLE
;
3326 qnum
= ath_hal_setuptxqueue(ah
, qtype
, &qi
);
3329 * NB: don't print a message, this happens
3330 * normally on parts with too few tx queues
3334 if (qnum
>= N(sc
->sc_txq
)) {
3335 device_printf(sc
->sc_dev
,
3336 "hal qnum %u out of range, max %zu!\n",
3337 qnum
, N(sc
->sc_txq
));
3338 ath_hal_releasetxqueue(ah
, qnum
);
3341 if (!ATH_TXQ_SETUP(sc
, qnum
)) {
3342 struct ath_txq
*txq
= &sc
->sc_txq
[qnum
];
3344 txq
->axq_qnum
= qnum
;
3346 txq
->axq_intrcnt
= 0;
3347 txq
->axq_link
= NULL
;
3348 STAILQ_INIT(&txq
->axq_q
);
3349 ATH_TXQ_LOCK_INIT(sc
, txq
);
3350 sc
->sc_txqsetup
|= 1<<qnum
;
3352 return &sc
->sc_txq
[qnum
];
3357 * Setup a hardware data transmit queue for the specified
3358 * access control. The hal may not support all requested
3359 * queues in which case it will return a reference to a
3360 * previously setup queue. We record the mapping from ac's
3361 * to h/w queues for use by ath_tx_start and also track
3362 * the set of h/w queues being used to optimize work in the
3363 * transmit interrupt handler and related routines.
3366 ath_tx_setup(struct ath_softc
*sc
, int ac
, int haltype
)
3368 #define N(a) (sizeof(a)/sizeof(a[0]))
3369 struct ath_txq
*txq
;
3371 if (ac
>= N(sc
->sc_ac2q
)) {
3372 device_printf(sc
->sc_dev
, "AC %u out of range, max %zu!\n",
3373 ac
, N(sc
->sc_ac2q
));
3376 txq
= ath_txq_setup(sc
, HAL_TX_QUEUE_DATA
, haltype
);
3378 sc
->sc_ac2q
[ac
] = txq
;
3386 * Update WME parameters for a transmit queue.
3389 ath_txq_update(struct ath_softc
*sc
, int ac
)
3391 #define ATH_EXPONENT_TO_VALUE(v) ((1<<v)-1)
3392 #define ATH_TXOP_TO_US(v) (v<<5)
3393 struct ieee80211com
*ic
= &sc
->sc_ic
;
3394 struct ath_txq
*txq
= sc
->sc_ac2q
[ac
];
3395 struct wmeParams
*wmep
= &ic
->ic_wme
.wme_chanParams
.cap_wmeParams
[ac
];
3396 struct ath_hal
*ah
= sc
->sc_ah
;
3399 ath_hal_gettxqueueprops(ah
, txq
->axq_qnum
, &qi
);
3400 qi
.tqi_aifs
= wmep
->wmep_aifsn
;
3401 qi
.tqi_cwmin
= ATH_EXPONENT_TO_VALUE(wmep
->wmep_logcwmin
);
3402 qi
.tqi_cwmax
= ATH_EXPONENT_TO_VALUE(wmep
->wmep_logcwmax
);
3403 qi
.tqi_burstTime
= ATH_TXOP_TO_US(wmep
->wmep_txopLimit
);
3405 if (!ath_hal_settxqueueprops(ah
, txq
->axq_qnum
, &qi
)) {
3406 device_printf(sc
->sc_dev
, "unable to update hardware queue "
3407 "parameters for %s traffic!\n",
3408 ieee80211_wme_acnames
[ac
]);
3411 ath_hal_resettxqueue(ah
, txq
->axq_qnum
); /* push to h/w */
3414 #undef ATH_TXOP_TO_US
3415 #undef ATH_EXPONENT_TO_VALUE
3419 * Callback from the 802.11 layer to update WME parameters.
3422 ath_wme_update(struct ieee80211com
*ic
)
3424 struct ath_softc
*sc
= ic
->ic_ifp
->if_softc
;
3426 return !ath_txq_update(sc
, WME_AC_BE
) ||
3427 !ath_txq_update(sc
, WME_AC_BK
) ||
3428 !ath_txq_update(sc
, WME_AC_VI
) ||
3429 !ath_txq_update(sc
, WME_AC_VO
) ? EIO
: 0;
3433 * Reclaim resources for a setup queue.
3436 ath_tx_cleanupq(struct ath_softc
*sc
, struct ath_txq
*txq
)
3439 ath_hal_releasetxqueue(sc
->sc_ah
, txq
->axq_qnum
);
3440 ATH_TXQ_LOCK_DESTROY(txq
);
3441 sc
->sc_txqsetup
&= ~(1<<txq
->axq_qnum
);
3445 * Reclaim all tx queue resources.
3448 ath_tx_cleanup(struct ath_softc
*sc
)
3452 ATH_TXBUF_LOCK_DESTROY(sc
);
3453 for (i
= 0; i
< HAL_NUM_TX_QUEUES
; i
++)
3454 if (ATH_TXQ_SETUP(sc
, i
))
3455 ath_tx_cleanupq(sc
, &sc
->sc_txq
[i
]);
3459 * Defragment an mbuf chain, returning at most maxfrags separate
3460 * mbufs+clusters. If this is not possible NULL is returned and
3461 * the original mbuf chain is left in it's present (potentially
3462 * modified) state. We use two techniques: collapsing consecutive
3463 * mbufs and replacing consecutive mbufs by a cluster.
3465 static struct mbuf
*
3466 ath_defrag(struct mbuf
*m0
, int how
, int maxfrags
)
3468 struct mbuf
*m
, *n
, *n2
, **prev
;
3472 * Calculate the current number of frags.
3475 for (m
= m0
; m
!= NULL
; m
= m
->m_next
)
3478 * First, try to collapse mbufs. Note that we always collapse
3479 * towards the front so we don't need to deal with moving the
3480 * pkthdr. This may be suboptimal if the first mbuf has much
3481 * less data than the following.
3489 if (n
->m_len
< M_TRAILINGSPACE(m
)) {
3490 memcpy(mtod(m
, char *) + m
->m_len
, mtod(n
, void *),
3492 m
->m_len
+= n
->m_len
;
3493 m
->m_next
= n
->m_next
;
3495 if (--curfrags
<= maxfrags
)
3500 KASSERT(maxfrags
> 1,
3501 ("maxfrags %u, but normal collapse failed", maxfrags
));
3503 * Collapse consecutive mbufs to a cluster.
3505 prev
= &m0
->m_next
; /* NB: not the first mbuf */
3506 while ((n
= *prev
) != NULL
) {
3507 if ((n2
= n
->m_next
) != NULL
&&
3508 n
->m_len
+ n2
->m_len
< MCLBYTES
) {
3509 m
= m_getcl(how
, MT_DATA
, 0);
3512 bcopy(mtod(n
, void *), mtod(m
, void *), n
->m_len
);
3513 bcopy(mtod(n2
, void *), mtod(m
, char *) + n
->m_len
,
3515 m
->m_len
= n
->m_len
+ n2
->m_len
;
3516 m
->m_next
= n2
->m_next
;
3520 if (--curfrags
<= maxfrags
) /* +1 cl -2 mbufs */
3523 * Still not there, try the normal collapse
3524 * again before we allocate another cluster.
3531 * No place where we can collapse to a cluster; punt.
3532 * This can occur if, for example, you request 2 frags
3533 * but the packet requires that both be clusters (we
3534 * never reallocate the first mbuf to avoid moving the
3542 * Return h/w rate index for an IEEE rate (w/o basic rate bit).
3545 ath_tx_findrix(const HAL_RATE_TABLE
*rt
, int rate
)
3549 for (i
= 0; i
< rt
->rateCount
; i
++)
3550 if ((rt
->info
[i
].dot11Rate
& IEEE80211_RATE_VAL
) == rate
)
3552 return 0; /* NB: lowest rate */
3556 ath_freetx(struct mbuf
*m
)
3561 next
= m
->m_nextpkt
;
3562 m
->m_nextpkt
= NULL
;
3564 } while ((m
= next
) != NULL
);
3568 deduct_pad_bytes(int len
, int hdrlen
)
3570 /* XXX I am suspicious that this code, which I extracted
3571 * XXX from ath_tx_start() for reuse, does the right thing.
3573 return len
- (hdrlen
& 3);
3577 ath_tx_start(struct ath_softc
*sc
, struct ieee80211_node
*ni
, struct ath_buf
*bf
,
3580 struct ieee80211com
*ic
= &sc
->sc_ic
;
3581 struct ath_hal
*ah
= sc
->sc_ah
;
3582 struct ifnet
*ifp
= &sc
->sc_if
;
3583 const struct chanAccParams
*cap
= &ic
->ic_wme
.wme_chanParams
;
3584 int i
, error
, iswep
, ismcast
, isfrag
, ismrr
;
3585 int keyix
, hdrlen
, pktlen
, try0
;
3586 u_int8_t rix
, txrate
, ctsrate
;
3587 u_int8_t cix
= 0xff; /* NB: silence compiler */
3588 struct ath_desc
*ds
, *ds0
;
3589 struct ath_txq
*txq
;
3590 struct ieee80211_frame
*wh
;
3591 u_int subtype
, flags
, ctsduration
;
3593 const HAL_RATE_TABLE
*rt
;
3594 HAL_BOOL shortPreamble
;
3595 struct ath_node
*an
;
3599 wh
= mtod(m0
, struct ieee80211_frame
*);
3600 iswep
= wh
->i_fc
[1] & IEEE80211_FC1_WEP
;
3601 ismcast
= IEEE80211_IS_MULTICAST(wh
->i_addr1
);
3602 isfrag
= m0
->m_flags
& M_FRAG
;
3603 hdrlen
= ieee80211_anyhdrsize(wh
);
3605 * Packet length must not include any
3606 * pad bytes; deduct them here.
3608 pktlen
= deduct_pad_bytes(m0
->m_pkthdr
.len
, hdrlen
);
3611 const struct ieee80211_cipher
*cip
;
3612 struct ieee80211_key
*k
;
3615 * Construct the 802.11 header+trailer for an encrypted
3616 * frame. The only reason this can fail is because of an
3617 * unknown or unsupported cipher/key type.
3619 k
= ieee80211_crypto_encap(ic
, ni
, m0
);
3622 * This can happen when the key is yanked after the
3623 * frame was queued. Just discard the frame; the
3624 * 802.11 layer counts failures and provides
3625 * debugging/diagnostics.
3631 * Adjust the packet + header lengths for the crypto
3632 * additions and calculate the h/w key index. When
3633 * a s/w mic is done the frame will have had any mic
3634 * added to it prior to entry so m0->m_pkthdr.len above will
3635 * account for it. Otherwise we need to add it to the
3639 hdrlen
+= cip
->ic_header
;
3640 pktlen
+= cip
->ic_header
+ cip
->ic_trailer
;
3641 /* NB: frags always have any TKIP MIC done in s/w */
3642 if ((k
->wk_flags
& IEEE80211_KEY_SWMIC
) == 0 && !isfrag
)
3643 pktlen
+= cip
->ic_miclen
;
3644 keyix
= k
->wk_keyix
;
3646 /* packet header may have moved, reset our local pointer */
3647 wh
= mtod(m0
, struct ieee80211_frame
*);
3648 } else if (ni
->ni_ucastkey
.wk_cipher
== &ieee80211_cipher_none
) {
3650 * Use station key cache slot, if assigned.
3652 keyix
= ni
->ni_ucastkey
.wk_keyix
;
3653 if (keyix
== IEEE80211_KEYIX_NONE
)
3654 keyix
= HAL_TXKEYIX_INVALID
;
3656 keyix
= HAL_TXKEYIX_INVALID
;
3658 pktlen
+= IEEE80211_CRC_LEN
;
3661 * Load the DMA map so any coalescing is done. This
3662 * also calculates the number of descriptors we need.
3664 error
= bus_dmamap_load_mbuf(sc
->sc_dmat
, bf
->bf_dmamap
, m0
,
3666 if (error
== EFBIG
) {
3667 /* XXX packet requires too many descriptors */
3668 bf
->bf_nseg
= ATH_TXDESC
+1;
3669 } else if (error
!= 0) {
3670 sc
->sc_stats
.ast_tx_busdma
++;
3675 * Discard null packets and check for packets that
3676 * require too many TX descriptors. We try to convert
3677 * the latter to a cluster.
3679 if (error
== EFBIG
) { /* too many desc's, linearize */
3680 sc
->sc_stats
.ast_tx_linear
++;
3681 m
= ath_defrag(m0
, M_DONTWAIT
, ATH_TXDESC
);
3684 sc
->sc_stats
.ast_tx_nombuf
++;
3688 error
= bus_dmamap_load_mbuf(sc
->sc_dmat
, bf
->bf_dmamap
, m0
,
3691 sc
->sc_stats
.ast_tx_busdma
++;
3695 KASSERT(bf
->bf_nseg
<= ATH_TXDESC
,
3696 ("too many segments after defrag; nseg %u", bf
->bf_nseg
));
3697 } else if (bf
->bf_nseg
== 0) { /* null packet, discard */
3698 sc
->sc_stats
.ast_tx_nodata
++;
3702 DPRINTF(sc
, ATH_DEBUG_XMIT
, "%s: m %p len %u\n", __func__
, m0
, pktlen
);
3703 bus_dmamap_sync(sc
->sc_dmat
, bf
->bf_dmamap
, 0,
3704 bf
->bf_dmamap
->dm_mapsize
, BUS_DMASYNC_PREWRITE
);
3706 bf
->bf_node
= ni
; /* NB: held reference */
3708 /* setup descriptors */
3710 rt
= sc
->sc_currates
;
3711 KASSERT(rt
!= NULL
, ("no rate table, mode %u", sc
->sc_curmode
));
3714 * NB: the 802.11 layer marks whether or not we should
3715 * use short preamble based on the current mode and
3716 * negotiated parameters.
3718 if ((ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
) &&
3719 (ni
->ni_capinfo
& IEEE80211_CAPINFO_SHORT_PREAMBLE
) && !ismcast
) {
3720 shortPreamble
= AH_TRUE
;
3721 sc
->sc_stats
.ast_tx_shortpre
++;
3723 shortPreamble
= AH_FALSE
;
3727 flags
= HAL_TXDESC_CLRDMASK
; /* XXX needed for crypto errs */
3728 ismrr
= 0; /* default no multi-rate retry*/
3730 * Calculate Atheros packet type from IEEE80211 packet header,
3731 * setup for rate calculations, and select h/w transmit queue.
3733 switch (wh
->i_fc
[0] & IEEE80211_FC0_TYPE_MASK
) {
3734 case IEEE80211_FC0_TYPE_MGT
:
3735 subtype
= wh
->i_fc
[0] & IEEE80211_FC0_SUBTYPE_MASK
;
3736 if (subtype
== IEEE80211_FC0_SUBTYPE_BEACON
)
3737 atype
= HAL_PKT_TYPE_BEACON
;
3738 else if (subtype
== IEEE80211_FC0_SUBTYPE_PROBE_RESP
)
3739 atype
= HAL_PKT_TYPE_PROBE_RESP
;
3740 else if (subtype
== IEEE80211_FC0_SUBTYPE_ATIM
)
3741 atype
= HAL_PKT_TYPE_ATIM
;
3743 atype
= HAL_PKT_TYPE_NORMAL
; /* XXX */
3744 rix
= sc
->sc_minrateix
;
3745 txrate
= rt
->info
[rix
].rateCode
;
3747 txrate
|= rt
->info
[rix
].shortPreamble
;
3748 try0
= ATH_TXMGTTRY
;
3749 /* NB: force all management frames to highest queue */
3750 if (ni
->ni_flags
& IEEE80211_NODE_QOS
) {
3751 /* NB: force all management frames to highest queue */
3755 flags
|= HAL_TXDESC_INTREQ
; /* force interrupt */
3757 case IEEE80211_FC0_TYPE_CTL
:
3758 atype
= HAL_PKT_TYPE_PSPOLL
; /* stop setting of duration */
3759 rix
= sc
->sc_minrateix
;
3760 txrate
= rt
->info
[rix
].rateCode
;
3762 txrate
|= rt
->info
[rix
].shortPreamble
;
3763 try0
= ATH_TXMGTTRY
;
3764 /* NB: force all ctl frames to highest queue */
3765 if (ni
->ni_flags
& IEEE80211_NODE_QOS
) {
3766 /* NB: force all ctl frames to highest queue */
3770 flags
|= HAL_TXDESC_INTREQ
; /* force interrupt */
3772 case IEEE80211_FC0_TYPE_DATA
:
3773 atype
= HAL_PKT_TYPE_NORMAL
; /* default */
3775 * Data frames: multicast frames go out at a fixed rate,
3776 * otherwise consult the rate control module for the
3781 * Check mcast rate setting in case it's changed.
3782 * XXX move out of fastpath
3784 if (ic
->ic_mcast_rate
!= sc
->sc_mcastrate
) {
3786 ath_tx_findrix(rt
, ic
->ic_mcast_rate
);
3787 sc
->sc_mcastrate
= ic
->ic_mcast_rate
;
3789 rix
= sc
->sc_mcastrix
;
3790 txrate
= rt
->info
[rix
].rateCode
;
3793 ath_rate_findrate(sc
, an
, shortPreamble
, pktlen
,
3794 &rix
, &try0
, &txrate
);
3795 sc
->sc_txrate
= txrate
; /* for LED blinking */
3796 if (try0
!= ATH_TXMAXTRY
)
3799 pri
= M_WME_GETAC(m0
);
3800 if (cap
->cap_wmeParams
[pri
].wmep_noackPolicy
)
3801 flags
|= HAL_TXDESC_NOACK
;
3804 if_printf(ifp
, "bogus frame type 0x%x (%s)\n",
3805 wh
->i_fc
[0] & IEEE80211_FC0_TYPE_MASK
, __func__
);
3810 txq
= sc
->sc_ac2q
[pri
];
3813 * When servicing one or more stations in power-save mode
3814 * multicast frames must be buffered until after the beacon.
3815 * We use the CAB queue for that.
3817 if (ismcast
&& ic
->ic_ps_sta
) {
3819 /* XXX? more bit in 802.11 frame header */
3823 * Calculate miscellaneous flags.
3826 flags
|= HAL_TXDESC_NOACK
; /* no ack on broad/multicast */
3827 } else if (pktlen
> ic
->ic_rtsthreshold
) {
3828 flags
|= HAL_TXDESC_RTSENA
; /* RTS based on frame length */
3829 cix
= rt
->info
[rix
].controlRate
;
3830 sc
->sc_stats
.ast_tx_rts
++;
3832 if (flags
& HAL_TXDESC_NOACK
) /* NB: avoid double counting */
3833 sc
->sc_stats
.ast_tx_noack
++;
3836 * If 802.11g protection is enabled, determine whether
3837 * to use RTS/CTS or just CTS. Note that this is only
3838 * done for OFDM unicast frames.
3840 if ((ic
->ic_flags
& IEEE80211_F_USEPROT
) &&
3841 rt
->info
[rix
].phy
== IEEE80211_T_OFDM
&&
3842 (flags
& HAL_TXDESC_NOACK
) == 0) {
3843 /* XXX fragments must use CCK rates w/ protection */
3844 if (ic
->ic_protmode
== IEEE80211_PROT_RTSCTS
)
3845 flags
|= HAL_TXDESC_RTSENA
;
3846 else if (ic
->ic_protmode
== IEEE80211_PROT_CTSONLY
)
3847 flags
|= HAL_TXDESC_CTSENA
;
3850 * For frags it would be desirable to use the
3851 * highest CCK rate for RTS/CTS. But stations
3852 * farther away may detect it at a lower CCK rate
3853 * so use the configured protection rate instead
3856 cix
= rt
->info
[sc
->sc_protrix
].controlRate
;
3858 cix
= rt
->info
[sc
->sc_protrix
].controlRate
;
3859 sc
->sc_stats
.ast_tx_protect
++;
3863 * Calculate duration. This logically belongs in the 802.11
3864 * layer but it lacks sufficient information to calculate it.
3866 if ((flags
& HAL_TXDESC_NOACK
) == 0 &&
3867 (wh
->i_fc
[0] & IEEE80211_FC0_TYPE_MASK
) != IEEE80211_FC0_TYPE_CTL
) {
3870 * XXX not right with fragmentation.
3873 dur
= rt
->info
[rix
].spAckDuration
;
3875 dur
= rt
->info
[rix
].lpAckDuration
;
3876 if (wh
->i_fc
[1] & IEEE80211_FC1_MORE_FRAG
) {
3877 dur
+= dur
; /* additional SIFS+ACK */
3878 KASSERT(m0
->m_nextpkt
!= NULL
, ("no fragment"));
3880 * Include the size of next fragment so NAV is
3881 * updated properly. The last fragment uses only
3884 dur
+= ath_hal_computetxtime(ah
, rt
,
3885 deduct_pad_bytes(m0
->m_nextpkt
->m_pkthdr
.len
,
3887 deduct_pad_bytes(m0
->m_pkthdr
.len
, hdrlen
) + pktlen
,
3888 rix
, shortPreamble
);
3892 * Force hardware to use computed duration for next
3893 * fragment by disabling multi-rate retry which updates
3894 * duration based on the multi-rate duration table.
3896 try0
= ATH_TXMAXTRY
;
3898 *(u_int16_t
*)wh
->i_dur
= htole16(dur
);
3902 * Calculate RTS/CTS rate and duration if needed.
3905 if (flags
& (HAL_TXDESC_RTSENA
|HAL_TXDESC_CTSENA
)) {
3907 * CTS transmit rate is derived from the transmit rate
3908 * by looking in the h/w rate table. We must also factor
3909 * in whether or not a short preamble is to be used.
3911 /* NB: cix is set above where RTS/CTS is enabled */
3912 KASSERT(cix
!= 0xff, ("cix not setup"));
3913 ctsrate
= rt
->info
[cix
].rateCode
;
3915 * Compute the transmit duration based on the frame
3916 * size and the size of an ACK frame. We call into the
3917 * HAL to do the computation since it depends on the
3918 * characteristics of the actual PHY being used.
3920 * NB: CTS is assumed the same size as an ACK so we can
3921 * use the precalculated ACK durations.
3923 if (shortPreamble
) {
3924 ctsrate
|= rt
->info
[cix
].shortPreamble
;
3925 if (flags
& HAL_TXDESC_RTSENA
) /* SIFS + CTS */
3926 ctsduration
+= rt
->info
[cix
].spAckDuration
;
3927 ctsduration
+= ath_hal_computetxtime(ah
,
3928 rt
, pktlen
, rix
, AH_TRUE
);
3929 if ((flags
& HAL_TXDESC_NOACK
) == 0) /* SIFS + ACK */
3930 ctsduration
+= rt
->info
[rix
].spAckDuration
;
3932 if (flags
& HAL_TXDESC_RTSENA
) /* SIFS + CTS */
3933 ctsduration
+= rt
->info
[cix
].lpAckDuration
;
3934 ctsduration
+= ath_hal_computetxtime(ah
,
3935 rt
, pktlen
, rix
, AH_FALSE
);
3936 if ((flags
& HAL_TXDESC_NOACK
) == 0) /* SIFS + ACK */
3937 ctsduration
+= rt
->info
[rix
].lpAckDuration
;
3940 * Must disable multi-rate retry when using RTS/CTS.
3943 try0
= ATH_TXMGTTRY
; /* XXX */
3947 if (IFF_DUMPPKTS(sc
, ATH_DEBUG_XMIT
))
3948 ieee80211_dump_pkt(mtod(m0
, void *), m0
->m_len
,
3949 sc
->sc_hwmap
[txrate
].ieeerate
, -1);
3952 bpf_mtap(ic
->ic_rawbpf
, m0
);
3953 if (sc
->sc_drvbpf
) {
3954 u_int64_t tsf
= ath_hal_gettsf64(ah
);
3956 sc
->sc_tx_th
.wt_tsf
= htole64(tsf
);
3957 sc
->sc_tx_th
.wt_flags
= sc
->sc_hwmap
[txrate
].txflags
;
3959 sc
->sc_tx_th
.wt_flags
|= IEEE80211_RADIOTAP_F_WEP
;
3961 sc
->sc_tx_th
.wt_flags
|= IEEE80211_RADIOTAP_F_FRAG
;
3962 sc
->sc_tx_th
.wt_rate
= sc
->sc_hwmap
[txrate
].ieeerate
;
3963 sc
->sc_tx_th
.wt_txpower
= ni
->ni_txpower
;
3964 sc
->sc_tx_th
.wt_antenna
= sc
->sc_txantenna
;
3966 bpf_mtap2(sc
->sc_drvbpf
,
3967 &sc
->sc_tx_th
, sc
->sc_tx_th_len
, m0
);
3972 * Determine if a tx interrupt should be generated for
3973 * this descriptor. We take a tx interrupt to reap
3974 * descriptors when the h/w hits an EOL condition or
3975 * when the descriptor is specifically marked to generate
3976 * an interrupt. We periodically mark descriptors in this
3977 * way to insure timely replenishing of the supply needed
3978 * for sending frames. Defering interrupts reduces system
3979 * load and potentially allows more concurrent work to be
3980 * done but if done to aggressively can cause senders to
3983 * NB: use >= to deal with sc_txintrperiod changing
3984 * dynamically through sysctl.
3986 if (flags
& HAL_TXDESC_INTREQ
) {
3987 txq
->axq_intrcnt
= 0;
3988 } else if (++txq
->axq_intrcnt
>= sc
->sc_txintrperiod
) {
3989 flags
|= HAL_TXDESC_INTREQ
;
3990 txq
->axq_intrcnt
= 0;
3994 * Formulate first tx descriptor with tx controls.
3996 /* XXX check return value? */
3997 ath_hal_setuptxdesc(ah
, ds
3998 , pktlen
/* packet length */
3999 , hdrlen
/* header length */
4000 , atype
/* Atheros packet type */
4001 , ni
->ni_txpower
/* txpower */
4002 , txrate
, try0
/* series 0 rate/tries */
4003 , keyix
/* key cache index */
4004 , sc
->sc_txantenna
/* antenna mode */
4006 , ctsrate
/* rts/cts rate */
4007 , ctsduration
/* rts/cts duration */
4009 bf
->bf_flags
= flags
;
4011 * Setup the multi-rate retry state only when we're
4012 * going to use it. This assumes ath_hal_setuptxdesc
4013 * initializes the descriptors (so we don't have to)
4014 * when the hardware supports multi-rate retry and
4018 ath_rate_setupxtxdesc(sc
, an
, ds
, shortPreamble
, rix
);
4021 * Fillin the remainder of the descriptor info.
4024 for (i
= 0; i
< bf
->bf_nseg
; i
++, ds
++) {
4025 ds
->ds_data
= bf
->bf_segs
[i
].ds_addr
;
4026 if (i
== bf
->bf_nseg
- 1)
4029 ds
->ds_link
= bf
->bf_daddr
+ sizeof(*ds
) * (i
+ 1);
4030 ath_hal_filltxdesc(ah
, ds
4031 , bf
->bf_segs
[i
].ds_len
/* segment length */
4032 , i
== 0 /* first segment */
4033 , i
== bf
->bf_nseg
- 1 /* last segment */
4034 , ds0
/* first descriptor */
4037 /* NB: The desc swap function becomes void,
4038 * if descriptor swapping is not enabled
4042 DPRINTF(sc
, ATH_DEBUG_XMIT
,
4043 "%s: %d: %08x %08x %08x %08x %08x %08x\n",
4044 __func__
, i
, ds
->ds_link
, ds
->ds_data
,
4045 ds
->ds_ctl0
, ds
->ds_ctl1
, ds
->ds_hw
[0], ds
->ds_hw
[1]);
4048 * Insert the frame on the outbound list and
4049 * pass it on to the hardware.
4052 ATH_TXQ_INSERT_TAIL(txq
, bf
, bf_list
);
4053 if (txq
->axq_link
== NULL
) {
4054 ath_hal_puttxbuf(ah
, txq
->axq_qnum
, bf
->bf_daddr
);
4055 DPRINTF(sc
, ATH_DEBUG_XMIT
,
4056 "%s: TXDP[%u] = %" PRIx64
" (%p) depth %d\n", __func__
,
4057 txq
->axq_qnum
, (uint64_t)bf
->bf_daddr
, bf
->bf_desc
,
4060 *txq
->axq_link
= HTOAH32(bf
->bf_daddr
);
4061 DPRINTF(sc
, ATH_DEBUG_XMIT
,
4062 "%s: link[%u](%p)=%" PRIx64
" (%p) depth %d\n",
4063 __func__
, txq
->axq_qnum
, txq
->axq_link
,
4064 (uint64_t)bf
->bf_daddr
, bf
->bf_desc
, txq
->axq_depth
);
4066 txq
->axq_link
= &bf
->bf_desc
[bf
->bf_nseg
- 1].ds_link
;
4068 * The CAB queue is started from the SWBA handler since
4069 * frames only go out on DTIM and to avoid possible races.
4071 if (txq
!= sc
->sc_cabq
)
4072 ath_hal_txstart(ah
, txq
->axq_qnum
);
4073 ATH_TXQ_UNLOCK(txq
);
4079 * Process completed xmit descriptors from the specified queue.
4082 ath_tx_processq(struct ath_softc
*sc
, struct ath_txq
*txq
)
4084 struct ath_hal
*ah
= sc
->sc_ah
;
4085 struct ieee80211com
*ic
= &sc
->sc_ic
;
4087 struct ath_desc
*ds
, *ds0
;
4088 struct ieee80211_node
*ni
;
4089 struct ath_node
*an
;
4090 int sr
, lr
, pri
, nacked
;
4093 DPRINTF(sc
, ATH_DEBUG_TX_PROC
, "%s: tx queue %u head %p link %p\n",
4094 __func__
, txq
->axq_qnum
,
4095 (void *)(uintptr_t) ath_hal_gettxbuf(sc
->sc_ah
, txq
->axq_qnum
),
4100 txq
->axq_intrcnt
= 0; /* reset periodic desc intr count */
4101 bf
= STAILQ_FIRST(&txq
->axq_q
);
4103 txq
->axq_link
= NULL
;
4104 ATH_TXQ_UNLOCK(txq
);
4107 ds0
= &bf
->bf_desc
[0];
4108 ds
= &bf
->bf_desc
[bf
->bf_nseg
- 1];
4109 status
= ath_hal_txprocdesc(ah
, ds
, &ds
->ds_txstat
);
4110 if (sc
->sc_debug
& ATH_DEBUG_XMIT_DESC
)
4111 ath_printtxbuf(bf
, status
== HAL_OK
);
4112 if (status
== HAL_EINPROGRESS
) {
4113 ATH_TXQ_UNLOCK(txq
);
4116 ATH_TXQ_REMOVE_HEAD(txq
, bf_list
);
4117 ATH_TXQ_UNLOCK(txq
);
4122 if (ds
->ds_txstat
.ts_status
== 0) {
4123 u_int8_t txant
= ds
->ds_txstat
.ts_antenna
;
4124 sc
->sc_stats
.ast_ant_tx
[txant
]++;
4125 sc
->sc_ant_tx
[txant
]++;
4126 if (ds
->ds_txstat
.ts_rate
& HAL_TXSTAT_ALTRATE
)
4127 sc
->sc_stats
.ast_tx_altrate
++;
4128 sc
->sc_stats
.ast_tx_rssi
=
4129 ds
->ds_txstat
.ts_rssi
;
4130 ATH_RSSI_LPF(sc
->sc_halstats
.ns_avgtxrssi
,
4131 ds
->ds_txstat
.ts_rssi
);
4132 pri
= M_WME_GETAC(bf
->bf_m
);
4133 if (pri
>= WME_AC_VO
)
4134 ic
->ic_wme
.wme_hipri_traffic
++;
4135 ni
->ni_inact
= ni
->ni_inact_reload
;
4137 if (ds
->ds_txstat
.ts_status
& HAL_TXERR_XRETRY
)
4138 sc
->sc_stats
.ast_tx_xretries
++;
4139 if (ds
->ds_txstat
.ts_status
& HAL_TXERR_FIFO
)
4140 sc
->sc_stats
.ast_tx_fifoerr
++;
4141 if (ds
->ds_txstat
.ts_status
& HAL_TXERR_FILT
)
4142 sc
->sc_stats
.ast_tx_filtered
++;
4144 sr
= ds
->ds_txstat
.ts_shortretry
;
4145 lr
= ds
->ds_txstat
.ts_longretry
;
4146 sc
->sc_stats
.ast_tx_shortretry
+= sr
;
4147 sc
->sc_stats
.ast_tx_longretry
+= lr
;
4149 * Hand the descriptor to the rate control algorithm.
4151 if ((ds
->ds_txstat
.ts_status
& HAL_TXERR_FILT
) == 0 &&
4152 (bf
->bf_flags
& HAL_TXDESC_NOACK
) == 0) {
4154 * If frame was ack'd update the last rx time
4155 * used to workaround phantom bmiss interrupts.
4157 if (ds
->ds_txstat
.ts_status
== 0)
4159 ath_rate_tx_complete(sc
, an
, ds
, ds0
);
4162 * Reclaim reference to node.
4164 * NB: the node may be reclaimed here if, for example
4165 * this is a DEAUTH message that was sent and the
4166 * node was timed out due to inactivity.
4168 ieee80211_free_node(ni
);
4170 bus_dmamap_sync(sc
->sc_dmat
, bf
->bf_dmamap
, 0,
4171 bf
->bf_dmamap
->dm_mapsize
, BUS_DMASYNC_POSTWRITE
);
4172 bus_dmamap_unload(sc
->sc_dmat
, bf
->bf_dmamap
);
4178 STAILQ_INSERT_TAIL(&sc
->sc_txbuf
, bf
, bf_list
);
4179 sc
->sc_if
.if_flags
&= ~IFF_OACTIVE
;
4180 ATH_TXBUF_UNLOCK(sc
);
4186 txqactive(struct ath_hal
*ah
, int qnum
)
4188 u_int32_t txqs
= 1<<qnum
;
4189 ath_hal_gettxintrtxqs(ah
, &txqs
);
4190 return (txqs
& (1<<qnum
));
4194 * Deferred processing of transmit interrupt; special-cased
4195 * for a single hardware transmit queue (e.g. 5210 and 5211).
4198 ath_tx_proc_q0(void *arg
, int npending
)
4200 struct ath_softc
*sc
= arg
;
4201 struct ifnet
*ifp
= &sc
->sc_if
;
4203 if (txqactive(sc
->sc_ah
, 0) && ath_tx_processq(sc
, &sc
->sc_txq
[0]) > 0){
4204 sc
->sc_lastrx
= ath_hal_gettsf64(sc
->sc_ah
);
4206 if (txqactive(sc
->sc_ah
, sc
->sc_cabq
->axq_qnum
))
4207 ath_tx_processq(sc
, sc
->sc_cabq
);
4210 ath_led_event(sc
, ATH_LED_TX
);
4216 * Deferred processing of transmit interrupt; special-cased
4217 * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
4220 ath_tx_proc_q0123(void *arg
, int npending
)
4222 struct ath_softc
*sc
= arg
;
4223 struct ifnet
*ifp
= &sc
->sc_if
;
4227 * Process each active queue.
4230 if (txqactive(sc
->sc_ah
, 0))
4231 nacked
+= ath_tx_processq(sc
, &sc
->sc_txq
[0]);
4232 if (txqactive(sc
->sc_ah
, 1))
4233 nacked
+= ath_tx_processq(sc
, &sc
->sc_txq
[1]);
4234 if (txqactive(sc
->sc_ah
, 2))
4235 nacked
+= ath_tx_processq(sc
, &sc
->sc_txq
[2]);
4236 if (txqactive(sc
->sc_ah
, 3))
4237 nacked
+= ath_tx_processq(sc
, &sc
->sc_txq
[3]);
4238 if (txqactive(sc
->sc_ah
, sc
->sc_cabq
->axq_qnum
))
4239 ath_tx_processq(sc
, sc
->sc_cabq
);
4241 sc
->sc_lastrx
= ath_hal_gettsf64(sc
->sc_ah
);
4245 ath_led_event(sc
, ATH_LED_TX
);
4251 * Deferred processing of transmit interrupt.
4254 ath_tx_proc(void *arg
, int npending
)
4256 struct ath_softc
*sc
= arg
;
4257 struct ifnet
*ifp
= &sc
->sc_if
;
4261 * Process each active queue.
4264 for (i
= 0; i
< HAL_NUM_TX_QUEUES
; i
++)
4265 if (ATH_TXQ_SETUP(sc
, i
) && txqactive(sc
->sc_ah
, i
))
4266 nacked
+= ath_tx_processq(sc
, &sc
->sc_txq
[i
]);
4268 sc
->sc_lastrx
= ath_hal_gettsf64(sc
->sc_ah
);
4272 ath_led_event(sc
, ATH_LED_TX
);
4278 ath_tx_draintxq(struct ath_softc
*sc
, struct ath_txq
*txq
)
4280 struct ath_hal
*ah
= sc
->sc_ah
;
4281 struct ieee80211_node
*ni
;
4283 struct ath_desc
*ds
;
4286 * NB: this assumes output has been stopped and
4287 * we do not need to block ath_tx_tasklet
4291 bf
= STAILQ_FIRST(&txq
->axq_q
);
4293 txq
->axq_link
= NULL
;
4294 ATH_TXQ_UNLOCK(txq
);
4297 ATH_TXQ_REMOVE_HEAD(txq
, bf_list
);
4298 ATH_TXQ_UNLOCK(txq
);
4299 ds
= &bf
->bf_desc
[bf
->bf_nseg
- 1];
4300 if (sc
->sc_debug
& ATH_DEBUG_RESET
)
4302 ath_hal_txprocdesc(ah
, bf
->bf_desc
,
4303 &ds
->ds_txstat
) == HAL_OK
);
4304 bus_dmamap_unload(sc
->sc_dmat
, bf
->bf_dmamap
);
4311 * Reclaim node reference.
4313 ieee80211_free_node(ni
);
4316 STAILQ_INSERT_TAIL(&sc
->sc_txbuf
, bf
, bf_list
);
4317 sc
->sc_if
.if_flags
&= ~IFF_OACTIVE
;
4318 ATH_TXBUF_UNLOCK(sc
);
4323 ath_tx_stopdma(struct ath_softc
*sc
, struct ath_txq
*txq
)
4325 struct ath_hal
*ah
= sc
->sc_ah
;
4327 (void) ath_hal_stoptxdma(ah
, txq
->axq_qnum
);
4328 DPRINTF(sc
, ATH_DEBUG_RESET
, "%s: tx queue [%u] %p, link %p\n",
4329 __func__
, txq
->axq_qnum
,
4330 (void *)(uintptr_t) ath_hal_gettxbuf(ah
, txq
->axq_qnum
),
4335 * Drain the transmit queues and reclaim resources.
4338 ath_draintxq(struct ath_softc
*sc
)
4340 struct ath_hal
*ah
= sc
->sc_ah
;
4343 /* XXX return value */
4344 if (device_is_active(sc
->sc_dev
)) {
4345 /* don't touch the hardware if marked invalid */
4346 (void) ath_hal_stoptxdma(ah
, sc
->sc_bhalq
);
4347 DPRINTF(sc
, ATH_DEBUG_RESET
,
4348 "%s: beacon queue %p\n", __func__
,
4349 (void *)(uintptr_t) ath_hal_gettxbuf(ah
, sc
->sc_bhalq
));
4350 for (i
= 0; i
< HAL_NUM_TX_QUEUES
; i
++)
4351 if (ATH_TXQ_SETUP(sc
, i
))
4352 ath_tx_stopdma(sc
, &sc
->sc_txq
[i
]);
4354 for (i
= 0; i
< HAL_NUM_TX_QUEUES
; i
++)
4355 if (ATH_TXQ_SETUP(sc
, i
))
4356 ath_tx_draintxq(sc
, &sc
->sc_txq
[i
]);
4360 * Disable the receive h/w in preparation for a reset.
4363 ath_stoprecv(struct ath_softc
*sc
)
4365 #define PA2DESC(_sc, _pa) \
4366 ((struct ath_desc *)((char *)(_sc)->sc_rxdma.dd_desc + \
4367 ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
4368 struct ath_hal
*ah
= sc
->sc_ah
;
4371 ath_hal_stoppcurecv(ah
); /* disable PCU */
4372 ath_hal_setrxfilter(ah
, 0); /* clear recv filter */
4373 ath_hal_stopdmarecv(ah
); /* disable DMA engine */
4374 DELAY(3000); /* 3ms is long enough for 1 frame */
4375 if (sc
->sc_debug
& (ATH_DEBUG_RESET
| ATH_DEBUG_FATAL
)) {
4378 printf("%s: rx queue %p, link %p\n", __func__
,
4379 (void *)(uintptr_t) ath_hal_getrxbuf(ah
), sc
->sc_rxlink
);
4380 STAILQ_FOREACH(bf
, &sc
->sc_rxbuf
, bf_list
) {
4381 struct ath_desc
*ds
= bf
->bf_desc
;
4382 tsf
= ath_hal_gettsf64(sc
->sc_ah
);
4383 HAL_STATUS status
= ath_hal_rxprocdesc(ah
, ds
,
4384 bf
->bf_daddr
, PA2DESC(sc
, ds
->ds_link
),
4386 if (status
== HAL_OK
|| (sc
->sc_debug
& ATH_DEBUG_FATAL
))
4387 ath_printrxbuf(bf
, status
== HAL_OK
);
4390 sc
->sc_rxlink
= NULL
; /* just in case */
4395 * Enable the receive h/w following a reset.
4398 ath_startrecv(struct ath_softc
*sc
)
4400 struct ath_hal
*ah
= sc
->sc_ah
;
4403 sc
->sc_rxlink
= NULL
;
4404 STAILQ_FOREACH(bf
, &sc
->sc_rxbuf
, bf_list
) {
4405 int error
= ath_rxbuf_init(sc
, bf
);
4407 DPRINTF(sc
, ATH_DEBUG_RECV
,
4408 "%s: ath_rxbuf_init failed %d\n",
4414 bf
= STAILQ_FIRST(&sc
->sc_rxbuf
);
4415 ath_hal_putrxbuf(ah
, bf
->bf_daddr
);
4416 ath_hal_rxena(ah
); /* enable recv descriptors */
4417 ath_mode_init(sc
); /* set filters, etc. */
4418 ath_hal_startpcurecv(ah
); /* re-enable PCU/DMA engine */
4423 * Update internal state after a channel change.
4426 ath_chan_change(struct ath_softc
*sc
, struct ieee80211_channel
*chan
)
4428 struct ieee80211com
*ic
= &sc
->sc_ic
;
4429 enum ieee80211_phymode mode
;
4433 * Change channels and update the h/w rate map
4434 * if we're switching; e.g. 11a to 11b/g.
4436 mode
= ieee80211_chan2mode(ic
, chan
);
4437 if (mode
!= sc
->sc_curmode
)
4438 ath_setcurmode(sc
, mode
);
4440 * Update BPF state. NB: ethereal et. al. don't handle
4441 * merged flags well so pick a unique mode for their use.
4443 if (IEEE80211_IS_CHAN_A(chan
))
4444 flags
= IEEE80211_CHAN_A
;
4445 /* XXX 11g schizophrenia */
4446 else if (IEEE80211_IS_CHAN_G(chan
) ||
4447 IEEE80211_IS_CHAN_PUREG(chan
))
4448 flags
= IEEE80211_CHAN_G
;
4450 flags
= IEEE80211_CHAN_B
;
4451 if (IEEE80211_IS_CHAN_T(chan
))
4452 flags
|= IEEE80211_CHAN_TURBO
;
4453 sc
->sc_tx_th
.wt_chan_freq
= sc
->sc_rx_th
.wr_chan_freq
=
4454 htole16(chan
->ic_freq
);
4455 sc
->sc_tx_th
.wt_chan_flags
= sc
->sc_rx_th
.wr_chan_flags
=
4461 * Poll for a channel clear indication; this is required
4462 * for channels requiring DFS and not previously visited
4463 * and/or with a recent radar detection.
4466 ath_dfswait(void *arg
)
4468 struct ath_softc
*sc
= arg
;
4469 struct ath_hal
*ah
= sc
->sc_ah
;
4472 ath_hal_radar_wait(ah
, &hchan
);
4473 if (hchan
.privFlags
& CHANNEL_INTERFERENCE
) {
4474 if_printf(&sc
->sc_if
,
4475 "channel %u/0x%x/0x%x has interference\n",
4476 hchan
.channel
, hchan
.channelFlags
, hchan
.privFlags
);
4479 if ((hchan
.privFlags
& CHANNEL_DFS
) == 0) {
4480 /* XXX should not happen */
4483 if (hchan
.privFlags
& CHANNEL_DFS_CLEAR
) {
4484 sc
->sc_curchan
.privFlags
|= CHANNEL_DFS_CLEAR
;
4485 sc
->sc_if
.if_flags
&= ~IFF_OACTIVE
;
4486 if_printf(&sc
->sc_if
,
4487 "channel %u/0x%x/0x%x marked clear\n",
4488 hchan
.channel
, hchan
.channelFlags
, hchan
.privFlags
);
4490 callout_reset(&sc
->sc_dfs_ch
, 2 * hz
, ath_dfswait
, sc
);
4495 * Set/change channels. If the channel is really being changed,
4496 * it's done by reseting the chip. To accomplish this we must
4497 * first cleanup any pending DMA, then restart stuff after a la
4501 ath_chan_set(struct ath_softc
*sc
, struct ieee80211_channel
*chan
)
4503 struct ath_hal
*ah
= sc
->sc_ah
;
4504 struct ieee80211com
*ic
= &sc
->sc_ic
;
4508 * Convert to a HAL channel description with
4509 * the flags constrained to reflect the current
4512 hchan
.channel
= chan
->ic_freq
;
4513 hchan
.channelFlags
= ath_chan2flags(ic
, chan
);
4515 DPRINTF(sc
, ATH_DEBUG_RESET
,
4516 "%s: %u (%u MHz, hal flags 0x%x) -> %u (%u MHz, hal flags 0x%x)\n",
4518 ath_hal_mhz2ieee(ah
, sc
->sc_curchan
.channel
,
4519 sc
->sc_curchan
.channelFlags
),
4520 sc
->sc_curchan
.channel
, sc
->sc_curchan
.channelFlags
,
4521 ath_hal_mhz2ieee(ah
, hchan
.channel
, hchan
.channelFlags
),
4522 hchan
.channel
, hchan
.channelFlags
);
4523 if (hchan
.channel
!= sc
->sc_curchan
.channel
||
4524 hchan
.channelFlags
!= sc
->sc_curchan
.channelFlags
) {
4528 * To switch channels clear any pending DMA operations;
4529 * wait long enough for the RX fifo to drain, reset the
4530 * hardware at the new frequency, and then re-enable
4531 * the relevant bits of the h/w.
4533 ath_hal_intrset(ah
, 0); /* disable interrupts */
4534 ath_draintxq(sc
); /* clear pending tx frames */
4535 ath_stoprecv(sc
); /* turn off frame recv */
4536 if (!ath_hal_reset(ah
, ic
->ic_opmode
, &hchan
, AH_TRUE
, &status
)) {
4537 if_printf(ic
->ic_ifp
, "%s: unable to reset "
4538 "channel %u (%u MHz, flags 0x%x hal flags 0x%x)\n",
4539 __func__
, ieee80211_chan2ieee(ic
, chan
),
4540 chan
->ic_freq
, chan
->ic_flags
, hchan
.channelFlags
);
4543 sc
->sc_curchan
= hchan
;
4544 ath_update_txpow(sc
); /* update tx power state */
4545 ath_restore_diversity(sc
);
4546 sc
->sc_calinterval
= 1;
4547 sc
->sc_caltries
= 0;
4550 * Re-enable rx framework.
4552 if (ath_startrecv(sc
) != 0) {
4553 if_printf(&sc
->sc_if
,
4554 "%s: unable to restart recv logic\n", __func__
);
4559 * Change channels and update the h/w rate map
4560 * if we're switching; e.g. 11a to 11b/g.
4562 ic
->ic_ibss_chan
= chan
;
4563 ath_chan_change(sc
, chan
);
4567 * Handle DFS required waiting period to determine
4568 * if channel is clear of radar traffic.
4570 if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
) {
4571 #define DFS_AND_NOT_CLEAR(_c) \
4572 (((_c)->privFlags & (CHANNEL_DFS | CHANNEL_DFS_CLEAR)) == CHANNEL_DFS)
4573 if (DFS_AND_NOT_CLEAR(&sc
->sc_curchan
)) {
4574 if_printf(&sc
->sc_if
,
4575 "wait for DFS clear channel signal\n");
4577 sc
->sc_if
.if_flags
|= IFF_OACTIVE
;
4578 callout_reset(&sc
->sc_dfs_ch
,
4579 2 * hz
, ath_dfswait
, sc
);
4581 callout_stop(&sc
->sc_dfs_ch
);
4582 #undef DFS_NOT_CLEAR
4587 * Re-enable interrupts.
4589 ath_hal_intrset(ah
, sc
->sc_imask
);
4595 ath_next_scan(void *arg
)
4597 struct ath_softc
*sc
= arg
;
4598 struct ieee80211com
*ic
= &sc
->sc_ic
;
4601 /* don't call ath_start w/o network interrupts blocked */
4604 if (ic
->ic_state
== IEEE80211_S_SCAN
)
4605 ieee80211_next_scan(ic
);
4610 * Periodically recalibrate the PHY to account
4611 * for temperature/environment changes.
4614 ath_calibrate(void *arg
)
4616 struct ath_softc
*sc
= arg
;
4617 struct ath_hal
*ah
= sc
->sc_ah
;
4620 sc
->sc_stats
.ast_per_cal
++;
4624 if (ath_hal_getrfgain(ah
) == HAL_RFGAIN_NEED_CHANGE
) {
4626 * Rfgain is out of bounds, reset the chip
4627 * to load new gain values.
4629 DPRINTF(sc
, ATH_DEBUG_CALIBRATE
,
4630 "%s: rfgain change\n", __func__
);
4631 sc
->sc_stats
.ast_per_rfgain
++;
4632 ath_reset(&sc
->sc_if
);
4634 if (!ath_hal_calibrate(ah
, &sc
->sc_curchan
, &iqCalDone
)) {
4635 DPRINTF(sc
, ATH_DEBUG_ANY
,
4636 "%s: calibration of channel %u failed\n",
4637 __func__
, sc
->sc_curchan
.channel
);
4638 sc
->sc_stats
.ast_per_calfail
++;
4641 * Calibrate noise floor data again in case of change.
4643 ath_hal_process_noisefloor(ah
);
4645 * Poll more frequently when the IQ calibration is in
4646 * progress to speedup loading the final settings.
4647 * We temper this aggressive polling with an exponential
4648 * back off after 4 tries up to ath_calinterval.
4650 if (iqCalDone
|| sc
->sc_calinterval
>= ath_calinterval
) {
4651 sc
->sc_caltries
= 0;
4652 sc
->sc_calinterval
= ath_calinterval
;
4653 } else if (sc
->sc_caltries
> 4) {
4654 sc
->sc_caltries
= 0;
4655 sc
->sc_calinterval
<<= 1;
4656 if (sc
->sc_calinterval
> ath_calinterval
)
4657 sc
->sc_calinterval
= ath_calinterval
;
4659 KASSERT(0 < sc
->sc_calinterval
&& sc
->sc_calinterval
<= ath_calinterval
,
4660 ("bad calibration interval %u", sc
->sc_calinterval
));
4662 DPRINTF(sc
, ATH_DEBUG_CALIBRATE
,
4663 "%s: next +%u (%siqCalDone tries %u)\n", __func__
,
4664 sc
->sc_calinterval
, iqCalDone
? "" : "!", sc
->sc_caltries
);
4666 callout_reset(&sc
->sc_cal_ch
, sc
->sc_calinterval
* hz
,
4672 ath_newstate(struct ieee80211com
*ic
, enum ieee80211_state nstate
, int arg
)
4674 struct ifnet
*ifp
= ic
->ic_ifp
;
4675 struct ath_softc
*sc
= ifp
->if_softc
;
4676 struct ath_hal
*ah
= sc
->sc_ah
;
4677 struct ieee80211_node
*ni
;
4679 const u_int8_t
*bssid
;
4681 static const HAL_LED_STATE leds
[] = {
4682 HAL_LED_INIT
, /* IEEE80211_S_INIT */
4683 HAL_LED_SCAN
, /* IEEE80211_S_SCAN */
4684 HAL_LED_AUTH
, /* IEEE80211_S_AUTH */
4685 HAL_LED_ASSOC
, /* IEEE80211_S_ASSOC */
4686 HAL_LED_RUN
, /* IEEE80211_S_RUN */
4689 DPRINTF(sc
, ATH_DEBUG_STATE
, "%s: %s -> %s\n", __func__
,
4690 ieee80211_state_name
[ic
->ic_state
],
4691 ieee80211_state_name
[nstate
]);
4693 callout_stop(&sc
->sc_scan_ch
);
4694 callout_stop(&sc
->sc_cal_ch
);
4696 callout_stop(&sc
->sc_dfs_ch
);
4698 ath_hal_setledstate(ah
, leds
[nstate
]); /* set LED */
4700 if (nstate
== IEEE80211_S_INIT
) {
4701 sc
->sc_imask
&= ~(HAL_INT_SWBA
| HAL_INT_BMISS
);
4703 * NB: disable interrupts so we don't rx frames.
4705 ath_hal_intrset(ah
, sc
->sc_imask
&~ HAL_INT_GLOBAL
);
4707 * Notify the rate control algorithm.
4709 ath_rate_newstate(sc
, nstate
);
4713 error
= ath_chan_set(sc
, ic
->ic_curchan
);
4716 rfilt
= ath_calcrxfilter(sc
, nstate
);
4717 if (nstate
== IEEE80211_S_SCAN
)
4718 bssid
= ifp
->if_broadcastaddr
;
4720 bssid
= ni
->ni_bssid
;
4721 ath_hal_setrxfilter(ah
, rfilt
);
4722 DPRINTF(sc
, ATH_DEBUG_STATE
, "%s: RX filter 0x%x bssid %s\n",
4723 __func__
, rfilt
, ether_sprintf(bssid
));
4725 if (nstate
== IEEE80211_S_RUN
&& ic
->ic_opmode
== IEEE80211_M_STA
)
4726 ath_hal_setassocid(ah
, bssid
, ni
->ni_associd
);
4728 ath_hal_setassocid(ah
, bssid
, 0);
4729 if (ic
->ic_flags
& IEEE80211_F_PRIVACY
) {
4730 for (i
= 0; i
< IEEE80211_WEP_NKID
; i
++)
4731 if (ath_hal_keyisvalid(ah
, i
))
4732 ath_hal_keysetmac(ah
, i
, bssid
);
4736 * Notify the rate control algorithm so rates
4737 * are setup should ath_beacon_alloc be called.
4739 ath_rate_newstate(sc
, nstate
);
4741 if (ic
->ic_opmode
== IEEE80211_M_MONITOR
) {
4742 /* nothing to do */;
4743 } else if (nstate
== IEEE80211_S_RUN
) {
4744 DPRINTF(sc
, ATH_DEBUG_STATE
,
4745 "%s(RUN): ic_flags=0x%08x iv=%d bssid=%s "
4746 "capinfo=0x%04x chan=%d\n"
4750 , ether_sprintf(ni
->ni_bssid
)
4752 , ieee80211_chan2ieee(ic
, ic
->ic_curchan
));
4754 switch (ic
->ic_opmode
) {
4755 case IEEE80211_M_HOSTAP
:
4756 case IEEE80211_M_IBSS
:
4758 * Allocate and setup the beacon frame.
4760 * Stop any previous beacon DMA. This may be
4761 * necessary, for example, when an ibss merge
4762 * causes reconfiguration; there will be a state
4763 * transition from RUN->RUN that means we may
4764 * be called with beacon transmission active.
4766 ath_hal_stoptxdma(ah
, sc
->sc_bhalq
);
4767 ath_beacon_free(sc
);
4768 error
= ath_beacon_alloc(sc
, ni
);
4772 * If joining an adhoc network defer beacon timer
4773 * configuration to the next beacon frame so we
4774 * have a current TSF to use. Otherwise we're
4775 * starting an ibss/bss so there's no need to delay.
4777 if (ic
->ic_opmode
== IEEE80211_M_IBSS
&&
4778 ic
->ic_bss
->ni_tstamp
.tsf
!= 0)
4779 sc
->sc_syncbeacon
= 1;
4781 ath_beacon_config(sc
);
4783 case IEEE80211_M_STA
:
4785 * Allocate a key cache slot to the station.
4787 if ((ic
->ic_flags
& IEEE80211_F_PRIVACY
) == 0 &&
4789 ni
->ni_ucastkey
.wk_keyix
== IEEE80211_KEYIX_NONE
)
4790 ath_setup_stationkey(ni
);
4792 * Defer beacon timer configuration to the next
4793 * beacon frame so we have a current TSF to use
4794 * (any TSF collected when scanning is likely old).
4796 sc
->sc_syncbeacon
= 1;
4802 * Let the hal process statistics collected during a
4803 * scan so it can provide calibrated noise floor data.
4805 ath_hal_process_noisefloor(ah
);
4807 * Reset rssi stats; maybe not the best place...
4809 sc
->sc_halstats
.ns_avgbrssi
= ATH_RSSI_DUMMY_MARKER
;
4810 sc
->sc_halstats
.ns_avgrssi
= ATH_RSSI_DUMMY_MARKER
;
4811 sc
->sc_halstats
.ns_avgtxrssi
= ATH_RSSI_DUMMY_MARKER
;
4814 sc
->sc_imask
&~ (HAL_INT_SWBA
| HAL_INT_BMISS
));
4815 sc
->sc_imask
&= ~(HAL_INT_SWBA
| HAL_INT_BMISS
);
4819 * Invoke the parent method to complete the work.
4821 error
= sc
->sc_newstate(ic
, nstate
, arg
);
4823 * Finally, start any timers.
4825 if (nstate
== IEEE80211_S_RUN
) {
4826 /* start periodic recalibration timer */
4827 callout_reset(&sc
->sc_cal_ch
, sc
->sc_calinterval
* hz
,
4829 } else if (nstate
== IEEE80211_S_SCAN
) {
4830 /* start ap/neighbor scan timer */
4831 callout_reset(&sc
->sc_scan_ch
, (ath_dwelltime
* hz
) / 1000,
4839 * Allocate a key cache slot to the station so we can
4840 * setup a mapping from key index to node. The key cache
4841 * slot is needed for managing antenna state and for
4842 * compression when stations do not use crypto. We do
4843 * it uniliaterally here; if crypto is employed this slot
4844 * will be reassigned.
4847 ath_setup_stationkey(struct ieee80211_node
*ni
)
4849 struct ieee80211com
*ic
= ni
->ni_ic
;
4850 struct ath_softc
*sc
= ic
->ic_ifp
->if_softc
;
4851 ieee80211_keyix keyix
, rxkeyix
;
4853 if (!ath_key_alloc(ic
, &ni
->ni_ucastkey
, &keyix
, &rxkeyix
)) {
4855 * Key cache is full; we'll fall back to doing
4856 * the more expensive lookup in software. Note
4857 * this also means no h/w compression.
4859 /* XXX msg+statistic */
4862 ni
->ni_ucastkey
.wk_keyix
= keyix
;
4863 ni
->ni_ucastkey
.wk_rxkeyix
= rxkeyix
;
4864 /* NB: this will create a pass-thru key entry */
4865 ath_keyset(sc
, &ni
->ni_ucastkey
, ni
->ni_macaddr
, ic
->ic_bss
);
4870 * Setup driver-specific state for a newly associated node.
4871 * Note that we're called also on a re-associate, the isnew
4872 * param tells us if this is the first time or not.
4875 ath_newassoc(struct ieee80211_node
*ni
, int isnew
)
4877 struct ieee80211com
*ic
= ni
->ni_ic
;
4878 struct ath_softc
*sc
= ic
->ic_ifp
->if_softc
;
4880 ath_rate_newassoc(sc
, ATH_NODE(ni
), isnew
);
4882 (ic
->ic_flags
& IEEE80211_F_PRIVACY
) == 0 && sc
->sc_hasclrkey
) {
4883 KASSERT(ni
->ni_ucastkey
.wk_keyix
== IEEE80211_KEYIX_NONE
,
4884 ("new assoc with a unicast key already setup (keyix %u)",
4885 ni
->ni_ucastkey
.wk_keyix
));
4886 ath_setup_stationkey(ni
);
4891 ath_getchannels(struct ath_softc
*sc
, u_int cc
,
4892 HAL_BOOL outdoor
, HAL_BOOL xchanmode
)
4894 #define COMPAT (CHANNEL_ALL_NOTURBO|CHANNEL_PASSIVE)
4895 struct ieee80211com
*ic
= &sc
->sc_ic
;
4896 struct ifnet
*ifp
= &sc
->sc_if
;
4897 struct ath_hal
*ah
= sc
->sc_ah
;
4901 chans
= malloc(IEEE80211_CHAN_MAX
* sizeof(HAL_CHANNEL
),
4903 if (chans
== NULL
) {
4904 if_printf(ifp
, "unable to allocate channel table\n");
4907 if (!ath_hal_init_channels(ah
, chans
, IEEE80211_CHAN_MAX
, &nchan
,
4909 cc
, HAL_MODE_ALL
, outdoor
, xchanmode
)) {
4912 (void)ath_hal_getregdomain(ah
, &rd
);
4913 if_printf(ifp
, "unable to collect channel list from hal; "
4914 "regdomain likely %u country code %u\n", rd
, cc
);
4915 free(chans
, M_TEMP
);
4920 * Convert HAL channels to ieee80211 ones and insert
4921 * them in the table according to their channel number.
4923 for (i
= 0; i
< nchan
; i
++) {
4924 HAL_CHANNEL
*c
= &chans
[i
];
4927 ix
= ath_hal_mhz2ieee(ah
, c
->channel
, c
->channelFlags
);
4928 if (ix
> IEEE80211_CHAN_MAX
) {
4929 if_printf(ifp
, "bad hal channel %d (%u/%x) ignored\n",
4930 ix
, c
->channel
, c
->channelFlags
);
4934 /* XXX can't handle stuff <2400 right now */
4936 if_printf(ifp
, "hal channel %d (%u/%x) "
4937 "cannot be handled; ignored\n",
4938 ix
, c
->channel
, c
->channelFlags
);
4942 * Calculate net80211 flags; most are compatible
4943 * but some need massaging. Note the static turbo
4944 * conversion can be removed once net80211 is updated
4945 * to understand static vs. dynamic turbo.
4947 flags
= c
->channelFlags
& COMPAT
;
4948 if (c
->channelFlags
& CHANNEL_STURBO
)
4949 flags
|= IEEE80211_CHAN_TURBO
;
4950 if (ic
->ic_channels
[ix
].ic_freq
== 0) {
4951 ic
->ic_channels
[ix
].ic_freq
= c
->channel
;
4952 ic
->ic_channels
[ix
].ic_flags
= flags
;
4954 /* channels overlap; e.g. 11g and 11b */
4955 ic
->ic_channels
[ix
].ic_flags
|= flags
;
4958 free(chans
, M_TEMP
);
4964 ath_led_done(void *arg
)
4966 struct ath_softc
*sc
= arg
;
4968 sc
->sc_blinking
= 0;
4972 * Turn the LED off: flip the pin and then set a timer so no
4973 * update will happen for the specified duration.
4976 ath_led_off(void *arg
)
4978 struct ath_softc
*sc
= arg
;
4980 ath_hal_gpioset(sc
->sc_ah
, sc
->sc_ledpin
, !sc
->sc_ledon
);
4981 callout_reset(&sc
->sc_ledtimer
, sc
->sc_ledoff
, ath_led_done
, sc
);
4985 * Blink the LED according to the specified on/off times.
4988 ath_led_blink(struct ath_softc
*sc
, int on
, int off
)
4990 DPRINTF(sc
, ATH_DEBUG_LED
, "%s: on %u off %u\n", __func__
, on
, off
);
4991 ath_hal_gpioset(sc
->sc_ah
, sc
->sc_ledpin
, sc
->sc_ledon
);
4992 sc
->sc_blinking
= 1;
4993 sc
->sc_ledoff
= off
;
4994 callout_reset(&sc
->sc_ledtimer
, on
, ath_led_off
, sc
);
4998 ath_led_event(struct ath_softc
*sc
, int event
)
5001 sc
->sc_ledevent
= ticks
; /* time of last event */
5002 if (sc
->sc_blinking
) /* don't interrupt active blink */
5006 ath_led_blink(sc
, sc
->sc_hwmap
[0].ledon
,
5007 sc
->sc_hwmap
[0].ledoff
);
5010 ath_led_blink(sc
, sc
->sc_hwmap
[sc
->sc_txrate
].ledon
,
5011 sc
->sc_hwmap
[sc
->sc_txrate
].ledoff
);
5014 ath_led_blink(sc
, sc
->sc_hwmap
[sc
->sc_rxrate
].ledon
,
5015 sc
->sc_hwmap
[sc
->sc_rxrate
].ledoff
);
5021 ath_update_txpow(struct ath_softc
*sc
)
5023 #define COMPAT (CHANNEL_ALL_NOTURBO|CHANNEL_PASSIVE)
5024 struct ieee80211com
*ic
= &sc
->sc_ic
;
5025 struct ath_hal
*ah
= sc
->sc_ah
;
5028 if (sc
->sc_curtxpow
!= ic
->ic_txpowlimit
) {
5029 ath_hal_settxpowlimit(ah
, ic
->ic_txpowlimit
);
5030 /* read back in case value is clamped */
5031 (void)ath_hal_gettxpowlimit(ah
, &txpow
);
5032 ic
->ic_txpowlimit
= sc
->sc_curtxpow
= txpow
;
5035 * Fetch max tx power level for status requests.
5037 (void)ath_hal_getmaxtxpow(sc
->sc_ah
, &txpow
);
5038 ic
->ic_bss
->ni_txpower
= txpow
;
5042 rate_setup(struct ath_softc
*sc
,
5043 const HAL_RATE_TABLE
*rt
, struct ieee80211_rateset
*rs
)
5047 if (rt
->rateCount
> IEEE80211_RATE_MAXSIZE
) {
5048 DPRINTF(sc
, ATH_DEBUG_ANY
,
5049 "%s: rate table too small (%u > %u)\n",
5050 __func__
, rt
->rateCount
, IEEE80211_RATE_MAXSIZE
);
5051 maxrates
= IEEE80211_RATE_MAXSIZE
;
5053 maxrates
= rt
->rateCount
;
5054 for (i
= 0; i
< maxrates
; i
++)
5055 rs
->rs_rates
[i
] = rt
->info
[i
].dot11Rate
;
5056 rs
->rs_nrates
= maxrates
;
5060 ath_rate_setup(struct ath_softc
*sc
, u_int mode
)
5062 struct ath_hal
*ah
= sc
->sc_ah
;
5063 struct ieee80211com
*ic
= &sc
->sc_ic
;
5064 const HAL_RATE_TABLE
*rt
;
5067 case IEEE80211_MODE_11A
:
5068 rt
= ath_hal_getratetable(ah
, HAL_MODE_11A
);
5070 case IEEE80211_MODE_11B
:
5071 rt
= ath_hal_getratetable(ah
, HAL_MODE_11B
);
5073 case IEEE80211_MODE_11G
:
5074 rt
= ath_hal_getratetable(ah
, HAL_MODE_11G
);
5076 case IEEE80211_MODE_TURBO_A
:
5077 /* XXX until static/dynamic turbo is fixed */
5078 rt
= ath_hal_getratetable(ah
, HAL_MODE_TURBO
);
5080 case IEEE80211_MODE_TURBO_G
:
5081 rt
= ath_hal_getratetable(ah
, HAL_MODE_108G
);
5084 DPRINTF(sc
, ATH_DEBUG_ANY
, "%s: invalid mode %u\n",
5088 sc
->sc_rates
[mode
] = rt
;
5090 rate_setup(sc
, rt
, &ic
->ic_sup_rates
[mode
]);
5097 ath_setcurmode(struct ath_softc
*sc
, enum ieee80211_phymode mode
)
5099 #define N(a) (sizeof(a)/sizeof(a[0]))
5100 /* NB: on/off times from the Atheros NDIS driver, w/ permission */
5101 static const struct {
5102 u_int rate
; /* tx/rx 802.11 rate */
5103 u_int16_t timeOn
; /* LED on time (ms) */
5104 u_int16_t timeOff
; /* LED off time (ms) */
5121 const HAL_RATE_TABLE
*rt
;
5124 memset(sc
->sc_rixmap
, 0xff, sizeof(sc
->sc_rixmap
));
5125 rt
= sc
->sc_rates
[mode
];
5126 KASSERT(rt
!= NULL
, ("no h/w rate set for phy mode %u", mode
));
5127 for (i
= 0; i
< rt
->rateCount
; i
++)
5128 sc
->sc_rixmap
[rt
->info
[i
].dot11Rate
& IEEE80211_RATE_VAL
] = i
;
5129 memset(sc
->sc_hwmap
, 0, sizeof(sc
->sc_hwmap
));
5130 for (i
= 0; i
< 32; i
++) {
5131 u_int8_t ix
= rt
->rateCodeToIndex
[i
];
5133 sc
->sc_hwmap
[i
].ledon
= (500 * hz
) / 1000;
5134 sc
->sc_hwmap
[i
].ledoff
= (130 * hz
) / 1000;
5137 sc
->sc_hwmap
[i
].ieeerate
=
5138 rt
->info
[ix
].dot11Rate
& IEEE80211_RATE_VAL
;
5139 sc
->sc_hwmap
[i
].txflags
= IEEE80211_RADIOTAP_F_DATAPAD
;
5140 if (rt
->info
[ix
].shortPreamble
||
5141 rt
->info
[ix
].phy
== IEEE80211_T_OFDM
)
5142 sc
->sc_hwmap
[i
].txflags
|= IEEE80211_RADIOTAP_F_SHORTPRE
;
5143 /* NB: receive frames include FCS */
5144 sc
->sc_hwmap
[i
].rxflags
= sc
->sc_hwmap
[i
].txflags
|
5145 IEEE80211_RADIOTAP_F_FCS
;
5146 /* setup blink rate table to avoid per-packet lookup */
5147 for (j
= 0; j
< N(blinkrates
)-1; j
++)
5148 if (blinkrates
[j
].rate
== sc
->sc_hwmap
[i
].ieeerate
)
5150 /* NB: this uses the last entry if the rate isn't found */
5151 /* XXX beware of overlow */
5152 sc
->sc_hwmap
[i
].ledon
= (blinkrates
[j
].timeOn
* hz
) / 1000;
5153 sc
->sc_hwmap
[i
].ledoff
= (blinkrates
[j
].timeOff
* hz
) / 1000;
5155 sc
->sc_currates
= rt
;
5156 sc
->sc_curmode
= mode
;
5158 * All protection frames are transmited at 2Mb/s for
5159 * 11g, otherwise at 1Mb/s.
5161 if (mode
== IEEE80211_MODE_11G
)
5162 sc
->sc_protrix
= ath_tx_findrix(rt
, 2*2);
5164 sc
->sc_protrix
= ath_tx_findrix(rt
, 2*1);
5165 /* rate index used to send management frames */
5166 sc
->sc_minrateix
= 0;
5168 * Setup multicast rate state.
5170 /* XXX layering violation */
5171 sc
->sc_mcastrix
= ath_tx_findrix(rt
, sc
->sc_ic
.ic_mcast_rate
);
5172 sc
->sc_mcastrate
= sc
->sc_ic
.ic_mcast_rate
;
5173 /* NB: caller is responsible for reseting rate control state */
5179 ath_printrxbuf(struct ath_buf
*bf
, int done
)
5181 struct ath_desc
*ds
;
5184 for (i
= 0, ds
= bf
->bf_desc
; i
< bf
->bf_nseg
; i
++, ds
++) {
5185 printf("R%d (%p %" PRIx64
5186 ") %08x %08x %08x %08x %08x %08x %02x %02x %c\n", i
, ds
,
5187 (uint64_t)bf
->bf_daddr
+ sizeof (struct ath_desc
) * i
,
5188 ds
->ds_link
, ds
->ds_data
,
5189 ds
->ds_ctl0
, ds
->ds_ctl1
,
5190 ds
->ds_hw
[0], ds
->ds_hw
[1],
5191 ds
->ds_rxstat
.rs_status
, ds
->ds_rxstat
.rs_keyix
,
5192 !done
? ' ' : (ds
->ds_rxstat
.rs_status
== 0) ? '*' : '!');
5197 ath_printtxbuf(struct ath_buf
*bf
, int done
)
5199 struct ath_desc
*ds
;
5202 for (i
= 0, ds
= bf
->bf_desc
; i
< bf
->bf_nseg
; i
++, ds
++) {
5203 printf("T%d (%p %" PRIx64
5204 ") %08x %08x %08x %08x %08x %08x %08x %08x %c\n",
5206 (uint64_t)bf
->bf_daddr
+ sizeof (struct ath_desc
) * i
,
5207 ds
->ds_link
, ds
->ds_data
,
5208 ds
->ds_ctl0
, ds
->ds_ctl1
,
5209 ds
->ds_hw
[0], ds
->ds_hw
[1], ds
->ds_hw
[2], ds
->ds_hw
[3],
5210 !done
? ' ' : (ds
->ds_txstat
.ts_status
== 0) ? '*' : '!');
5213 #endif /* AR_DEBUG */
5216 ath_watchdog(struct ifnet
*ifp
)
5218 struct ath_softc
*sc
= ifp
->if_softc
;
5219 struct ieee80211com
*ic
= &sc
->sc_ic
;
5220 struct ath_txq
*axq
;
5224 if ((ifp
->if_flags
& IFF_RUNNING
) == 0 ||
5225 !device_is_active(sc
->sc_dev
))
5227 for (i
= 0; i
< HAL_NUM_TX_QUEUES
; i
++) {
5228 if (!ATH_TXQ_SETUP(sc
, i
))
5230 axq
= &sc
->sc_txq
[i
];
5232 if (axq
->axq_timer
== 0)
5234 else if (--axq
->axq_timer
== 0) {
5235 ATH_TXQ_UNLOCK(axq
);
5236 if_printf(ifp
, "device timeout (txq %d, "
5237 "txintrperiod %d)\n", i
, sc
->sc_txintrperiod
);
5238 if (sc
->sc_txintrperiod
> 1)
5239 sc
->sc_txintrperiod
--;
5242 sc
->sc_stats
.ast_watchdog
++;
5246 ATH_TXQ_UNLOCK(axq
);
5248 ieee80211_watchdog(ic
);
5252 * Diagnostic interface to the HAL. This is used by various
5253 * tools to do things like retrieve register contents for
5254 * debugging. The mechanism is intentionally opaque so that
5255 * it can change frequently w/o concern for compatiblity.
5258 ath_ioctl_diag(struct ath_softc
*sc
, struct ath_diag
*ad
)
5260 struct ath_hal
*ah
= sc
->sc_ah
;
5261 u_int id
= ad
->ad_id
& ATH_DIAG_ID
;
5262 void *indata
= NULL
;
5263 void *outdata
= NULL
;
5264 u_int32_t insize
= ad
->ad_in_size
;
5265 u_int32_t outsize
= ad
->ad_out_size
;
5268 if (ad
->ad_id
& ATH_DIAG_IN
) {
5272 indata
= malloc(insize
, M_TEMP
, M_NOWAIT
);
5273 if (indata
== NULL
) {
5277 error
= copyin(ad
->ad_in_data
, indata
, insize
);
5281 if (ad
->ad_id
& ATH_DIAG_DYN
) {
5283 * Allocate a buffer for the results (otherwise the HAL
5284 * returns a pointer to a buffer where we can read the
5285 * results). Note that we depend on the HAL leaving this
5286 * pointer for us to use below in reclaiming the buffer;
5287 * may want to be more defensive.
5289 outdata
= malloc(outsize
, M_TEMP
, M_NOWAIT
);
5290 if (outdata
== NULL
) {
5295 if (ath_hal_getdiagstate(ah
, id
, indata
, insize
, &outdata
, &outsize
)) {
5296 if (outsize
< ad
->ad_out_size
)
5297 ad
->ad_out_size
= outsize
;
5298 if (outdata
!= NULL
)
5299 error
= copyout(outdata
, ad
->ad_out_data
,
5305 if ((ad
->ad_id
& ATH_DIAG_IN
) && indata
!= NULL
)
5306 free(indata
, M_TEMP
);
5307 if ((ad
->ad_id
& ATH_DIAG_DYN
) && outdata
!= NULL
)
5308 free(outdata
, M_TEMP
);
5313 ath_ioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
5315 #define IS_RUNNING(ifp) \
5316 ((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING))
5317 struct ath_softc
*sc
= ifp
->if_softc
;
5318 struct ieee80211com
*ic
= &sc
->sc_ic
;
5319 struct ifreq
*ifr
= (struct ifreq
*)data
;
5325 if ((error
= ifioctl_common(ifp
, cmd
, data
)) != 0)
5327 switch (ifp
->if_flags
& (IFF_UP
|IFF_RUNNING
)) {
5328 case IFF_UP
|IFF_RUNNING
:
5330 * To avoid rescanning another access point,
5331 * do not call ath_init() here. Instead,
5332 * only reflect promisc mode settings.
5338 * Beware of being called during attach/detach
5339 * to reset promiscuous mode. In that case we
5340 * will still be marked UP but not RUNNING.
5341 * However trying to re-init the interface
5342 * is the wrong thing to do as we've already
5343 * torn down much of our state. There's
5344 * probably a better way to deal with this.
5346 error
= ath_init(sc
);
5349 ath_stop_locked(ifp
, 1);
5357 if ((error
= ether_ioctl(ifp
, cmd
, data
)) == ENETRESET
) {
5358 if (ifp
->if_flags
& IFF_RUNNING
)
5364 /* NB: embed these numbers to get a consistent view */
5365 sc
->sc_stats
.ast_tx_packets
= ifp
->if_opackets
;
5366 sc
->sc_stats
.ast_rx_packets
= ifp
->if_ipackets
;
5367 sc
->sc_stats
.ast_rx_rssi
= ieee80211_getrssi(ic
);
5370 * NB: Drop the softc lock in case of a page fault;
5371 * we'll accept any potential inconsisentcy in the
5372 * statistics. The alternative is to copy the data
5373 * to a local structure.
5375 return copyout(&sc
->sc_stats
,
5376 ifr
->ifr_data
, sizeof (sc
->sc_stats
));
5378 error
= ath_ioctl_diag(sc
, (struct ath_diag
*) ifr
);
5381 error
= ieee80211_ioctl(ic
, cmd
, data
);
5382 if (error
!= ENETRESET
)
5384 else if (IS_RUNNING(ifp
) &&
5385 ic
->ic_roaming
!= IEEE80211_ROAMING_MANUAL
)
5386 error
= ath_init(sc
);
5398 ath_bpfattach(struct ath_softc
*sc
)
5400 struct ifnet
*ifp
= &sc
->sc_if
;
5402 bpfattach2(ifp
, DLT_IEEE802_11_RADIO
,
5403 sizeof(struct ieee80211_frame
) + sizeof(sc
->sc_tx_th
),
5406 * Initialize constant fields.
5407 * XXX make header lengths a multiple of 32-bits so subsequent
5408 * headers are properly aligned; this is a kludge to keep
5409 * certain applications happy.
5411 * NB: the channel is setup each time we transition to the
5412 * RUN state to avoid filling it in for each frame.
5414 sc
->sc_tx_th_len
= roundup(sizeof(sc
->sc_tx_th
), sizeof(u_int32_t
));
5415 sc
->sc_tx_th
.wt_ihdr
.it_len
= htole16(sc
->sc_tx_th_len
);
5416 sc
->sc_tx_th
.wt_ihdr
.it_present
= htole32(ATH_TX_RADIOTAP_PRESENT
);
5418 sc
->sc_rx_th_len
= roundup(sizeof(sc
->sc_rx_th
), sizeof(u_int32_t
));
5419 sc
->sc_rx_th
.wr_ihdr
.it_len
= htole16(sc
->sc_rx_th_len
);
5420 sc
->sc_rx_th
.wr_ihdr
.it_present
= htole32(ATH_RX_RADIOTAP_PRESENT
);
5425 * Announce various information on device/driver attach.
5428 ath_announce(struct ath_softc
*sc
)
5430 #define HAL_MODE_DUALBAND (HAL_MODE_11A|HAL_MODE_11B)
5431 struct ifnet
*ifp
= &sc
->sc_if
;
5432 struct ath_hal
*ah
= sc
->sc_ah
;
5435 if_printf(ifp
, "mac %d.%d phy %d.%d",
5436 ah
->ah_macVersion
, ah
->ah_macRev
,
5437 ah
->ah_phyRev
>> 4, ah
->ah_phyRev
& 0xf);
5439 * Print radio revision(s). We check the wireless modes
5440 * to avoid falsely printing revs for inoperable parts.
5441 * Dual-band radio revs are returned in the 5 GHz rev number.
5443 ath_hal_getcountrycode(ah
, &cc
);
5444 modes
= ath_hal_getwirelessmodes(ah
, cc
);
5445 if ((modes
& HAL_MODE_DUALBAND
) == HAL_MODE_DUALBAND
) {
5446 if (ah
->ah_analog5GhzRev
&& ah
->ah_analog2GhzRev
)
5447 printf(" 5 GHz radio %d.%d 2 GHz radio %d.%d",
5448 ah
->ah_analog5GhzRev
>> 4,
5449 ah
->ah_analog5GhzRev
& 0xf,
5450 ah
->ah_analog2GhzRev
>> 4,
5451 ah
->ah_analog2GhzRev
& 0xf);
5453 printf(" radio %d.%d", ah
->ah_analog5GhzRev
>> 4,
5454 ah
->ah_analog5GhzRev
& 0xf);
5456 printf(" radio %d.%d", ah
->ah_analog5GhzRev
>> 4,
5457 ah
->ah_analog5GhzRev
& 0xf);
5461 for (i
= 0; i
<= WME_AC_VO
; i
++) {
5462 struct ath_txq
*txq
= sc
->sc_ac2q
[i
];
5463 if_printf(ifp
, "Use hw queue %u for %s traffic\n",
5464 txq
->axq_qnum
, ieee80211_wme_acnames
[i
]);
5466 if_printf(ifp
, "Use hw queue %u for CAB traffic\n",
5467 sc
->sc_cabq
->axq_qnum
);
5468 if_printf(ifp
, "Use hw queue %u for beacons\n", sc
->sc_bhalq
);
5470 if (ath_rxbuf
!= ATH_RXBUF
)
5471 if_printf(ifp
, "using %u rx buffers\n", ath_rxbuf
);
5472 if (ath_txbuf
!= ATH_TXBUF
)
5473 if_printf(ifp
, "using %u tx buffers\n", ath_txbuf
);
5474 #undef HAL_MODE_DUALBAND