ath.c revision 1.60 1 /* $NetBSD: ath.c,v 1.60 2005/10/14 00:26:45 gdt Exp $ */
2
3 /*-
4 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
20 *
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.
24 *
25 * NO WARRANTY
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.
37 */
38
39 #include <sys/cdefs.h>
40 #ifdef __FreeBSD__
41 __FBSDID("$FreeBSD: src/sys/dev/ath/if_ath.c,v 1.94 2005/07/07 00:04:50 sam Exp $");
42 #endif
43 #ifdef __NetBSD__
44 __KERNEL_RCSID(0, "$NetBSD: ath.c,v 1.60 2005/10/14 00:26:45 gdt Exp $");
45 #endif
46
47 /*
48 * Driver for the Atheros Wireless LAN controller.
49 *
50 * This software is derived from work of Atsushi Onoe; his contribution
51 * is greatly appreciated.
52 */
53
54 #include "opt_inet.h"
55
56 #ifdef __NetBSD__
57 #include "bpfilter.h"
58 #endif /* __NetBSD__ */
59
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>
65 #include <sys/mbuf.h>
66 #include <sys/malloc.h>
67 #include <sys/lock.h>
68 #include <sys/kernel.h>
69 #include <sys/socket.h>
70 #include <sys/sockio.h>
71 #include <sys/errno.h>
72 #include <sys/callout.h>
73 #include <machine/bus.h>
74 #include <sys/endian.h>
75
76 #include <machine/bus.h>
77
78 #include <net/if.h>
79 #include <net/if_dl.h>
80 #include <net/if_media.h>
81 #include <net/if_types.h>
82 #include <net/if_arp.h>
83 #include <net/if_ether.h>
84 #include <net/if_llc.h>
85
86 #include <net80211/ieee80211_netbsd.h>
87 #include <net80211/ieee80211_var.h>
88
89 #if NBPFILTER > 0
90 #include <net/bpf.h>
91 #endif
92
93 #ifdef INET
94 #include <netinet/in.h>
95 #endif
96
97 #include <sys/device.h>
98 #include <dev/ic/ath_netbsd.h>
99
100 #define AR_DEBUG
101 #include <dev/ic/athvar.h>
102 #include <contrib/dev/ic/athhal_desc.h>
103 #include <contrib/dev/ic/athhal_devid.h> /* XXX for softled */
104
105 /* unaligned little endian access */
106 #define LE_READ_2(p) \
107 ((u_int16_t) \
108 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8)))
109 #define LE_READ_4(p) \
110 ((u_int32_t) \
111 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8) | \
112 (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24)))
113
114 enum {
115 ATH_LED_TX,
116 ATH_LED_RX,
117 ATH_LED_POLL,
118 };
119
120 static int ath_ifinit(struct ifnet *);
121 static int ath_init(struct ath_softc *);
122 static void ath_stop_locked(struct ifnet *, int);
123 static void ath_stop(struct ifnet *, int);
124 static void ath_start(struct ifnet *);
125 static int ath_media_change(struct ifnet *);
126 static void ath_watchdog(struct ifnet *);
127 static int ath_ioctl(struct ifnet *, u_long, caddr_t);
128 static void ath_fatal_proc(void *, int);
129 static void ath_rxorn_proc(void *, int);
130 static void ath_bmiss_proc(void *, int);
131 static int ath_key_alloc(struct ieee80211com *,
132 const struct ieee80211_key *);
133 static int ath_key_delete(struct ieee80211com *,
134 const struct ieee80211_key *);
135 static int ath_key_set(struct ieee80211com *, const struct ieee80211_key *,
136 const u_int8_t mac[IEEE80211_ADDR_LEN]);
137 static void ath_key_update_begin(struct ieee80211com *);
138 static void ath_key_update_end(struct ieee80211com *);
139 static void ath_mode_init(struct ath_softc *);
140 static void ath_setslottime(struct ath_softc *);
141 static void ath_updateslot(struct ifnet *);
142 static int ath_beaconq_setup(struct ath_hal *);
143 static int ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *);
144 static void ath_beacon_setup(struct ath_softc *, struct ath_buf *);
145 static void ath_beacon_proc(void *, int);
146 static void ath_bstuck_proc(void *, int);
147 static void ath_beacon_free(struct ath_softc *);
148 static void ath_beacon_config(struct ath_softc *);
149 static void ath_descdma_cleanup(struct ath_softc *sc,
150 struct ath_descdma *, ath_bufhead *);
151 static int ath_desc_alloc(struct ath_softc *);
152 static void ath_desc_free(struct ath_softc *);
153 static struct ieee80211_node *ath_node_alloc(struct ieee80211_node_table *);
154 static void ath_node_free(struct ieee80211_node *);
155 static u_int8_t ath_node_getrssi(const struct ieee80211_node *);
156 static int ath_rxbuf_init(struct ath_softc *, struct ath_buf *);
157 static void ath_recv_mgmt(struct ieee80211com *ic, struct mbuf *m,
158 struct ieee80211_node *ni,
159 int subtype, int rssi, u_int32_t rstamp);
160 static void ath_setdefantenna(struct ath_softc *, u_int);
161 static void ath_rx_proc(void *, int);
162 static struct ath_txq *ath_txq_setup(struct ath_softc*, int qtype, int subtype);
163 static int ath_tx_setup(struct ath_softc *, int, int);
164 static int ath_wme_update(struct ieee80211com *);
165 static void ath_tx_cleanupq(struct ath_softc *, struct ath_txq *);
166 static void ath_tx_cleanup(struct ath_softc *);
167 static int ath_tx_start(struct ath_softc *, struct ieee80211_node *,
168 struct ath_buf *, struct mbuf *);
169 static void ath_tx_proc_q0(void *, int);
170 static void ath_tx_proc_q0123(void *, int);
171 static void ath_tx_proc(void *, int);
172 static int ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
173 static void ath_draintxq(struct ath_softc *);
174 static void ath_stoprecv(struct ath_softc *);
175 static int ath_startrecv(struct ath_softc *);
176 static void ath_chan_change(struct ath_softc *, struct ieee80211_channel *);
177 static void ath_next_scan(void *);
178 static void ath_calibrate(void *);
179 static int ath_newstate(struct ieee80211com *, enum ieee80211_state, int);
180 static void ath_setup_stationkey(struct ieee80211_node *);
181 static void ath_newassoc(struct ieee80211com *,
182 struct ieee80211_node *, int);
183 static int ath_getchannels(struct ath_softc *, u_int cc,
184 HAL_BOOL outdoor, HAL_BOOL xchanmode);
185 static void ath_led_event(struct ath_softc *, int);
186 static void ath_update_txpow(struct ath_softc *);
187
188 static int ath_rate_setup(struct ath_softc *, u_int mode);
189 static void ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
190
191 #ifdef __NetBSD__
192 int ath_enable(struct ath_softc *);
193 void ath_disable(struct ath_softc *);
194 void ath_power(int, void *);
195 #endif
196
197 static void ath_bpfattach(struct ath_softc *);
198 static void ath_announce(struct ath_softc *);
199
200 int ath_dwelltime = 200; /* 5 channels/second */
201 int ath_calinterval = 30; /* calibrate every 30 secs */
202 int ath_outdoor = AH_TRUE; /* outdoor operation */
203 int ath_xchanmode = AH_TRUE; /* enable extended channels */
204 int ath_countrycode = CTRY_DEFAULT; /* country code */
205 int ath_regdomain = 0; /* regulatory domain */
206 int ath_debug = 0;
207
208 #ifdef AR_DEBUG
209 enum {
210 ATH_DEBUG_XMIT = 0x00000001, /* basic xmit operation */
211 ATH_DEBUG_XMIT_DESC = 0x00000002, /* xmit descriptors */
212 ATH_DEBUG_RECV = 0x00000004, /* basic recv operation */
213 ATH_DEBUG_RECV_DESC = 0x00000008, /* recv descriptors */
214 ATH_DEBUG_RATE = 0x00000010, /* rate control */
215 ATH_DEBUG_RESET = 0x00000020, /* reset processing */
216 ATH_DEBUG_MODE = 0x00000040, /* mode init/setup */
217 ATH_DEBUG_BEACON = 0x00000080, /* beacon handling */
218 ATH_DEBUG_WATCHDOG = 0x00000100, /* watchdog timeout */
219 ATH_DEBUG_INTR = 0x00001000, /* ISR */
220 ATH_DEBUG_TX_PROC = 0x00002000, /* tx ISR proc */
221 ATH_DEBUG_RX_PROC = 0x00004000, /* rx ISR proc */
222 ATH_DEBUG_BEACON_PROC = 0x00008000, /* beacon ISR proc */
223 ATH_DEBUG_CALIBRATE = 0x00010000, /* periodic calibration */
224 ATH_DEBUG_KEYCACHE = 0x00020000, /* key cache management */
225 ATH_DEBUG_STATE = 0x00040000, /* 802.11 state transitions */
226 ATH_DEBUG_NODE = 0x00080000, /* node management */
227 ATH_DEBUG_LED = 0x00100000, /* led management */
228 ATH_DEBUG_FATAL = 0x80000000, /* fatal errors */
229 ATH_DEBUG_ANY = 0xffffffff
230 };
231 #define IFF_DUMPPKTS(sc, m) \
232 ((sc->sc_debug & (m)) || \
233 (sc->sc_if.if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
234 #define DPRINTF(sc, m, fmt, ...) do { \
235 if (sc->sc_debug & (m)) \
236 printf(fmt, __VA_ARGS__); \
237 } while (0)
238 #define KEYPRINTF(sc, ix, hk, mac) do { \
239 if (sc->sc_debug & ATH_DEBUG_KEYCACHE) \
240 ath_keyprint(__func__, ix, hk, mac); \
241 } while (0)
242 static void ath_printrxbuf(struct ath_buf *bf, int);
243 static void ath_printtxbuf(struct ath_buf *bf, int);
244 #else
245 #define IFF_DUMPPKTS(sc, m) \
246 ((sc->sc_if.if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
247 #define DPRINTF(m, fmt, ...)
248 #define KEYPRINTF(sc, k, ix, mac)
249 #endif
250
251 #ifdef __NetBSD__
252 int
253 ath_activate(struct device *self, enum devact act)
254 {
255 struct ath_softc *sc = (struct ath_softc *)self;
256 int rv = 0, s;
257
258 s = splnet();
259 switch (act) {
260 case DVACT_ACTIVATE:
261 rv = EOPNOTSUPP;
262 break;
263 case DVACT_DEACTIVATE:
264 if_deactivate(&sc->sc_if);
265 break;
266 }
267 splx(s);
268 return rv;
269 }
270
271 int
272 ath_enable(struct ath_softc *sc)
273 {
274 if (ATH_IS_ENABLED(sc) == 0) {
275 if (sc->sc_enable != NULL && (*sc->sc_enable)(sc) != 0) {
276 printf("%s: device enable failed\n",
277 sc->sc_dev.dv_xname);
278 return (EIO);
279 }
280 sc->sc_flags |= ATH_ENABLED;
281 }
282 return (0);
283 }
284
285 void
286 ath_disable(struct ath_softc *sc)
287 {
288 if (!ATH_IS_ENABLED(sc))
289 return;
290 if (sc->sc_disable != NULL)
291 (*sc->sc_disable)(sc);
292 sc->sc_flags &= ~ATH_ENABLED;
293 }
294 #endif /* __NetBSD__ */
295
296 MALLOC_DEFINE(M_ATHDEV, "athdev", "ath driver dma buffers");
297
298 int
299 ath_attach(u_int16_t devid, struct ath_softc *sc)
300 {
301 struct ifnet *ifp = &sc->sc_if;
302 struct ieee80211com *ic = &sc->sc_ic;
303 struct ath_hal *ah = NULL;
304 HAL_STATUS status;
305 int error = 0, i;
306
307 DPRINTF(sc, ATH_DEBUG_ANY, "%s: devid 0x%x\n", __func__, devid);
308
309 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
310
311 ah = ath_hal_attach(devid, sc, sc->sc_st, ATH_BUSHANDLE2HAL(sc->sc_sh),
312 &status);
313 if (ah == NULL) {
314 if_printf(ifp, "unable to attach hardware; HAL status %u\n",
315 status);
316 error = ENXIO;
317 goto bad;
318 }
319 if (ah->ah_abi != HAL_ABI_VERSION) {
320 if_printf(ifp, "HAL ABI mismatch detected "
321 "(HAL:0x%x != driver:0x%x)\n",
322 ah->ah_abi, HAL_ABI_VERSION);
323 error = ENXIO;
324 goto bad;
325 }
326 sc->sc_ah = ah;
327 sc->sc_invalid = 0; /* ready to go, enable interrupt handling */
328
329 /*
330 * Check if the MAC has multi-rate retry support.
331 * We do this by trying to setup a fake extended
332 * descriptor. MAC's that don't have support will
333 * return false w/o doing anything. MAC's that do
334 * support it will return true w/o doing anything.
335 */
336 sc->sc_mrretry = ath_hal_setupxtxdesc(ah, NULL, 0,0, 0,0, 0,0);
337
338 /*
339 * Check if the device has hardware counters for PHY
340 * errors. If so we need to enable the MIB interrupt
341 * so we can act on stat triggers.
342 */
343 if (ath_hal_hwphycounters(ah))
344 sc->sc_needmib = 1;
345
346 /*
347 * Get the hardware key cache size.
348 */
349 sc->sc_keymax = ath_hal_keycachesize(ah);
350 if (sc->sc_keymax > ATH_KEYMAX) {
351 if_printf(ifp, "Warning, using only %u of %u key cache slots\n",
352 ATH_KEYMAX, sc->sc_keymax);
353 sc->sc_keymax = ATH_KEYMAX;
354 }
355 /*
356 * Reset the key cache since some parts do not
357 * reset the contents on initial power up.
358 */
359 for (i = 0; i < sc->sc_keymax; i++)
360 ath_hal_keyreset(ah, i);
361 /*
362 * Mark key cache slots associated with global keys
363 * as in use. If we knew TKIP was not to be used we
364 * could leave the +32, +64, and +32+64 slots free.
365 * XXX only for splitmic.
366 */
367 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
368 setbit(sc->sc_keymap, i);
369 setbit(sc->sc_keymap, i+32);
370 setbit(sc->sc_keymap, i+64);
371 setbit(sc->sc_keymap, i+32+64);
372 }
373
374 /*
375 * Collect the channel list using the default country
376 * code and including outdoor channels. The 802.11 layer
377 * is resposible for filtering this list based on settings
378 * like the phy mode.
379 */
380 error = ath_getchannels(sc, ath_countrycode,
381 ath_outdoor, ath_xchanmode);
382 if (error != 0)
383 goto bad;
384 /*
385 * TPC support can be done either with a global cap or
386 * per-packet support. The latter is not available on
387 * all parts. We're a bit pedantic here as all parts
388 * support a global cap.
389 */
390 sc->sc_hastpc = ath_hal_hastpc(ah);
391 if (sc->sc_hastpc || ath_hal_hastxpowlimit(ah))
392 ic->ic_caps |= IEEE80211_C_TXPMGT;
393 /*
394 * Query the hal about antenna support.
395 */
396 if (ath_hal_hasdiversity(ah)) {
397 sc->sc_hasdiversity = 1;
398 sc->sc_diversity = ath_hal_getdiversity(ah);
399 }
400 sc->sc_defant = ath_hal_getdefantenna(ah);
401
402 /*
403 * Setup dynamic sysctl's now that country code and regdomain
404 * are available from the hal, and both TPC and diversity
405 * capabilities are known.
406 */
407 ath_sysctlattach(sc);
408
409 /*
410 * Setup rate tables for all potential media types.
411 */
412 ath_rate_setup(sc, IEEE80211_MODE_11A);
413 ath_rate_setup(sc, IEEE80211_MODE_11B);
414 ath_rate_setup(sc, IEEE80211_MODE_11G);
415 ath_rate_setup(sc, IEEE80211_MODE_TURBO_A);
416 ath_rate_setup(sc, IEEE80211_MODE_TURBO_G);
417 /* NB: setup here so ath_rate_update is happy */
418 ath_setcurmode(sc, IEEE80211_MODE_11A);
419
420 /*
421 * Allocate tx+rx descriptors and populate the lists.
422 */
423 error = ath_desc_alloc(sc);
424 if (error != 0) {
425 if_printf(ifp, "failed to allocate descriptors: %d\n", error);
426 goto bad;
427 }
428 ATH_CALLOUT_INIT(&sc->sc_scan_ch, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
429 ATH_CALLOUT_INIT(&sc->sc_cal_ch, CALLOUT_MPSAFE);
430
431 ATH_TXBUF_LOCK_INIT(sc);
432
433 TASK_INIT(&sc->sc_rxtask, 0, ath_rx_proc, sc);
434 TASK_INIT(&sc->sc_rxorntask, 0, ath_rxorn_proc, sc);
435 TASK_INIT(&sc->sc_fataltask, 0, ath_fatal_proc, sc);
436 TASK_INIT(&sc->sc_bmisstask, 0, ath_bmiss_proc, sc);
437 TASK_INIT(&sc->sc_bstucktask, 0, ath_bstuck_proc, sc);
438
439 /*
440 * Allocate hardware transmit queues: one queue for
441 * beacon frames and one data queue for each QoS
442 * priority. Note that the hal handles reseting
443 * these queues at the needed time.
444 *
445 * XXX PS-Poll
446 */
447 sc->sc_bhalq = ath_beaconq_setup(ah);
448 if (sc->sc_bhalq == (u_int) -1) {
449 if_printf(ifp, "unable to setup a beacon xmit queue!\n");
450 error = EIO;
451 goto bad2;
452 }
453 sc->sc_cabq = ath_txq_setup(sc, HAL_TX_QUEUE_CAB, 0);
454 if (sc->sc_cabq == NULL) {
455 if_printf(ifp, "unable to setup CAB xmit queue!\n");
456 error = EIO;
457 goto bad2;
458 }
459 /* NB: insure BK queue is the lowest priority h/w queue */
460 if (!ath_tx_setup(sc, WME_AC_BK, HAL_WME_AC_BK)) {
461 if_printf(ifp, "unable to setup xmit queue for %s traffic!\n",
462 ieee80211_wme_acnames[WME_AC_BK]);
463 error = EIO;
464 goto bad2;
465 }
466 if (!ath_tx_setup(sc, WME_AC_BE, HAL_WME_AC_BE) ||
467 !ath_tx_setup(sc, WME_AC_VI, HAL_WME_AC_VI) ||
468 !ath_tx_setup(sc, WME_AC_VO, HAL_WME_AC_VO)) {
469 /*
470 * Not enough hardware tx queues to properly do WME;
471 * just punt and assign them all to the same h/w queue.
472 * We could do a better job of this if, for example,
473 * we allocate queues when we switch from station to
474 * AP mode.
475 */
476 if (sc->sc_ac2q[WME_AC_VI] != NULL)
477 ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_VI]);
478 if (sc->sc_ac2q[WME_AC_BE] != NULL)
479 ath_tx_cleanupq(sc, sc->sc_ac2q[WME_AC_BE]);
480 sc->sc_ac2q[WME_AC_BE] = sc->sc_ac2q[WME_AC_BK];
481 sc->sc_ac2q[WME_AC_VI] = sc->sc_ac2q[WME_AC_BK];
482 sc->sc_ac2q[WME_AC_VO] = sc->sc_ac2q[WME_AC_BK];
483 }
484
485 /*
486 * Special case certain configurations. Note the
487 * CAB queue is handled by these specially so don't
488 * include them when checking the txq setup mask.
489 */
490 switch (sc->sc_txqsetup &~ (1<<sc->sc_cabq->axq_qnum)) {
491 case 0x01:
492 TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0, sc);
493 break;
494 case 0x0f:
495 TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc_q0123, sc);
496 break;
497 default:
498 TASK_INIT(&sc->sc_txtask, 0, ath_tx_proc, sc);
499 break;
500 }
501
502 /*
503 * Setup rate control. Some rate control modules
504 * call back to change the anntena state so expose
505 * the necessary entry points.
506 * XXX maybe belongs in struct ath_ratectrl?
507 */
508 sc->sc_setdefantenna = ath_setdefantenna;
509 sc->sc_rc = ath_rate_attach(sc);
510 if (sc->sc_rc == NULL) {
511 error = EIO;
512 goto bad2;
513 }
514
515 sc->sc_blinking = 0;
516 sc->sc_ledstate = 1;
517 sc->sc_ledon = 0; /* low true */
518 sc->sc_ledidle = (2700*hz)/1000; /* 2.7sec */
519 ATH_CALLOUT_INIT(&sc->sc_ledtimer, CALLOUT_MPSAFE);
520 /*
521 * Auto-enable soft led processing for IBM cards and for
522 * 5211 minipci cards. Users can also manually enable/disable
523 * support with a sysctl.
524 */
525 sc->sc_softled = (devid == AR5212_DEVID_IBM || devid == AR5211_DEVID);
526 if (sc->sc_softled) {
527 ath_hal_gpioCfgOutput(ah, sc->sc_ledpin);
528 ath_hal_gpioset(ah, sc->sc_ledpin, !sc->sc_ledon);
529 }
530
531 ifp->if_softc = sc;
532 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
533 ifp->if_start = ath_start;
534 ifp->if_watchdog = ath_watchdog;
535 ifp->if_ioctl = ath_ioctl;
536 ifp->if_init = ath_ifinit;
537 IFQ_SET_READY(&ifp->if_snd);
538
539 ic->ic_ifp = ifp;
540 ic->ic_reset = ath_reset;
541 ic->ic_newassoc = ath_newassoc;
542 ic->ic_updateslot = ath_updateslot;
543 ic->ic_wme.wme_update = ath_wme_update;
544 /* XXX not right but it's not used anywhere important */
545 ic->ic_phytype = IEEE80211_T_OFDM;
546 ic->ic_opmode = IEEE80211_M_STA;
547 ic->ic_caps =
548 IEEE80211_C_IBSS /* ibss, nee adhoc, mode */
549 | IEEE80211_C_HOSTAP /* hostap mode */
550 | IEEE80211_C_MONITOR /* monitor mode */
551 | IEEE80211_C_SHPREAMBLE /* short preamble supported */
552 | IEEE80211_C_SHSLOT /* short slot time supported */
553 | IEEE80211_C_WPA /* capable of WPA1+WPA2 */
554 ;
555 /*
556 * Query the hal to figure out h/w crypto support.
557 */
558 if (ath_hal_ciphersupported(ah, HAL_CIPHER_WEP))
559 ic->ic_caps |= IEEE80211_C_WEP;
560 if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_OCB))
561 ic->ic_caps |= IEEE80211_C_AES;
562 if (ath_hal_ciphersupported(ah, HAL_CIPHER_AES_CCM))
563 ic->ic_caps |= IEEE80211_C_AES_CCM;
564 if (ath_hal_ciphersupported(ah, HAL_CIPHER_CKIP))
565 ic->ic_caps |= IEEE80211_C_CKIP;
566 if (ath_hal_ciphersupported(ah, HAL_CIPHER_TKIP)) {
567 ic->ic_caps |= IEEE80211_C_TKIP;
568 /*
569 * Check if h/w does the MIC and/or whether the
570 * separate key cache entries are required to
571 * handle both tx+rx MIC keys.
572 */
573 if (ath_hal_ciphersupported(ah, HAL_CIPHER_MIC))
574 ic->ic_caps |= IEEE80211_C_TKIPMIC;
575 if (ath_hal_tkipsplit(ah))
576 sc->sc_splitmic = 1;
577 }
578 sc->sc_hasclrkey = ath_hal_ciphersupported(ah, HAL_CIPHER_CLR);
579 sc->sc_mcastkey = ath_hal_getmcastkeysearch(ah);
580
581 /*
582 * Mark WME capability only if we have sufficient
583 * hardware queues to do proper priority scheduling.
584 */
585 if (sc->sc_ac2q[WME_AC_BE] != sc->sc_ac2q[WME_AC_BK])
586 ic->ic_caps |= IEEE80211_C_WME;
587 /*
588 * Check for misc other capabilities.
589 */
590 if (ath_hal_hasbursting(ah))
591 ic->ic_caps |= IEEE80211_C_BURST;
592
593 /*
594 * Indicate we need the 802.11 header padded to a
595 * 32-bit boundary for 4-address and QoS frames.
596 */
597 ic->ic_flags |= IEEE80211_F_DATAPAD;
598
599 /*
600 * Not all chips have the VEOL support we want to
601 * use with IBSS beacons; check here for it.
602 */
603 sc->sc_hasveol = ath_hal_hasveol(ah);
604
605 /* get mac address from hardware */
606 ath_hal_getmac(ah, ic->ic_myaddr);
607
608 if_attach(ifp);
609 /* call MI attach routine. */
610 ieee80211_ifattach(ic);
611 /* override default methods */
612 ic->ic_node_alloc = ath_node_alloc;
613 sc->sc_node_free = ic->ic_node_free;
614 ic->ic_node_free = ath_node_free;
615 ic->ic_node_getrssi = ath_node_getrssi;
616 sc->sc_recv_mgmt = ic->ic_recv_mgmt;
617 ic->ic_recv_mgmt = ath_recv_mgmt;
618 sc->sc_newstate = ic->ic_newstate;
619 ic->ic_newstate = ath_newstate;
620 ic->ic_crypto.cs_key_alloc = ath_key_alloc;
621 ic->ic_crypto.cs_key_delete = ath_key_delete;
622 ic->ic_crypto.cs_key_set = ath_key_set;
623 ic->ic_crypto.cs_key_update_begin = ath_key_update_begin;
624 ic->ic_crypto.cs_key_update_end = ath_key_update_end;
625 /* complete initialization */
626 ieee80211_media_init(ic, ath_media_change, ieee80211_media_status);
627
628 ath_bpfattach(sc);
629
630 #ifdef __NetBSD__
631 sc->sc_flags |= ATH_ATTACHED;
632 /*
633 * Make sure the interface is shutdown during reboot.
634 */
635 sc->sc_sdhook = shutdownhook_establish(ath_shutdown, sc);
636 if (sc->sc_sdhook == NULL)
637 printf("%s: WARNING: unable to establish shutdown hook\n",
638 sc->sc_dev.dv_xname);
639 sc->sc_powerhook = powerhook_establish(ath_power, sc);
640 if (sc->sc_powerhook == NULL)
641 printf("%s: WARNING: unable to establish power hook\n",
642 sc->sc_dev.dv_xname);
643 #endif
644 ieee80211_announce(ic);
645 ath_announce(sc);
646 return 0;
647 bad2:
648 ath_tx_cleanup(sc);
649 ath_desc_free(sc);
650 bad:
651 if (ah)
652 ath_hal_detach(ah);
653 sc->sc_invalid = 1;
654 return error;
655 }
656
657 int
658 ath_detach(struct ath_softc *sc)
659 {
660 struct ifnet *ifp = &sc->sc_if;
661 int s;
662
663 if ((sc->sc_flags & ATH_ATTACHED) == 0)
664 return (0);
665
666 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
667 __func__, ifp->if_flags);
668
669 s = splnet();
670 ath_stop(ifp, 1);
671 #if NBPFILTER > 0
672 bpfdetach(ifp);
673 #endif
674 /*
675 * NB: the order of these is important:
676 * o call the 802.11 layer before detaching the hal to
677 * insure callbacks into the driver to delete global
678 * key cache entries can be handled
679 * o reclaim the tx queue data structures after calling
680 * the 802.11 layer as we'll get called back to reclaim
681 * node state and potentially want to use them
682 * o to cleanup the tx queues the hal is called, so detach
683 * it last
684 * Other than that, it's straightforward...
685 */
686 ieee80211_ifdetach(&sc->sc_ic);
687 ath_rate_detach(sc->sc_rc);
688 ath_desc_free(sc);
689 ath_tx_cleanup(sc);
690 sysctl_teardown(&sc->sc_sysctllog);
691 ath_hal_detach(sc->sc_ah);
692 if_detach(ifp);
693 splx(s);
694 powerhook_disestablish(sc->sc_powerhook);
695 shutdownhook_disestablish(sc->sc_sdhook);
696
697 return 0;
698 }
699
700 #ifdef __NetBSD__
701 void
702 ath_power(int why, void *arg)
703 {
704 struct ath_softc *sc = arg;
705 int s;
706
707 DPRINTF(sc, ATH_DEBUG_ANY, "ath_power(%d)\n", why);
708
709 s = splnet();
710 switch (why) {
711 case PWR_SUSPEND:
712 case PWR_STANDBY:
713 ath_suspend(sc, why);
714 break;
715 case PWR_RESUME:
716 ath_resume(sc, why);
717 break;
718 case PWR_SOFTSUSPEND:
719 case PWR_SOFTSTANDBY:
720 case PWR_SOFTRESUME:
721 break;
722 }
723 splx(s);
724 }
725 #endif
726
727 void
728 ath_suspend(struct ath_softc *sc, int why)
729 {
730 struct ifnet *ifp = &sc->sc_if;
731
732 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
733 __func__, ifp->if_flags);
734
735 ath_stop(ifp, 1);
736 if (sc->sc_power != NULL)
737 (*sc->sc_power)(sc, why);
738 }
739
740 void
741 ath_resume(struct ath_softc *sc, int why)
742 {
743 struct ifnet *ifp = &sc->sc_if;
744
745 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags %x\n",
746 __func__, ifp->if_flags);
747
748 if (ifp->if_flags & IFF_UP) {
749 ath_init(sc);
750 #if 0
751 (void)ath_intr(sc);
752 #endif
753 if (sc->sc_power != NULL)
754 (*sc->sc_power)(sc, why);
755 if (ifp->if_flags & IFF_RUNNING)
756 ath_start(ifp);
757 }
758 if (sc->sc_softled) {
759 ath_hal_gpioCfgOutput(sc->sc_ah, sc->sc_ledpin);
760 ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon);
761 }
762 }
763
764 void
765 ath_shutdown(void *arg)
766 {
767 struct ath_softc *sc = arg;
768
769 ath_stop(&sc->sc_if, 1);
770 }
771
772 /*
773 * Interrupt handler. Most of the actual processing is deferred.
774 */
775 int
776 ath_intr(void *arg)
777 {
778 struct ath_softc *sc = arg;
779 struct ifnet *ifp = &sc->sc_if;
780 struct ath_hal *ah = sc->sc_ah;
781 HAL_INT status;
782
783 if (sc->sc_invalid) {
784 /*
785 * The hardware is not ready/present, don't touch anything.
786 * Note this can happen early on if the IRQ is shared.
787 */
788 DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid; ignored\n", __func__);
789 return 0;
790 }
791 if (!ath_hal_intrpend(ah)) /* shared irq, not for us */
792 return 0;
793 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP)) {
794 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
795 __func__, ifp->if_flags);
796 ath_hal_getisr(ah, &status); /* clear ISR */
797 ath_hal_intrset(ah, 0); /* disable further intr's */
798 return 1; /* XXX */
799 }
800 /*
801 * Figure out the reason(s) for the interrupt. Note
802 * that the hal returns a pseudo-ISR that may include
803 * bits we haven't explicitly enabled so we mask the
804 * value to insure we only process bits we requested.
805 */
806 ath_hal_getisr(ah, &status); /* NB: clears ISR too */
807 DPRINTF(sc, ATH_DEBUG_INTR, "%s: status 0x%x\n", __func__, status);
808 status &= sc->sc_imask; /* discard unasked for bits */
809 if (status & HAL_INT_FATAL) {
810 /*
811 * Fatal errors are unrecoverable. Typically
812 * these are caused by DMA errors. Unfortunately
813 * the exact reason is not (presently) returned
814 * by the hal.
815 */
816 sc->sc_stats.ast_hardware++;
817 ath_hal_intrset(ah, 0); /* disable intr's until reset */
818 TASK_RUN_OR_ENQUEUE(&sc->sc_fataltask);
819 } else if (status & HAL_INT_RXORN) {
820 sc->sc_stats.ast_rxorn++;
821 ath_hal_intrset(ah, 0); /* disable intr's until reset */
822 TASK_RUN_OR_ENQUEUE(&sc->sc_rxorntask);
823 } else {
824 if (status & HAL_INT_SWBA) {
825 /*
826 * Software beacon alert--time to send a beacon.
827 * Handle beacon transmission directly; deferring
828 * this is too slow to meet timing constraints
829 * under load.
830 */
831 ath_beacon_proc(sc, 0);
832 }
833 if (status & HAL_INT_RXEOL) {
834 /*
835 * NB: the hardware should re-read the link when
836 * RXE bit is written, but it doesn't work at
837 * least on older hardware revs.
838 */
839 sc->sc_stats.ast_rxeol++;
840 sc->sc_rxlink = NULL;
841 }
842 if (status & HAL_INT_TXURN) {
843 sc->sc_stats.ast_txurn++;
844 /* bump tx trigger level */
845 ath_hal_updatetxtriglevel(ah, AH_TRUE);
846 }
847 if (status & HAL_INT_RX)
848 TASK_RUN_OR_ENQUEUE(&sc->sc_rxtask);
849 if (status & HAL_INT_TX)
850 TASK_RUN_OR_ENQUEUE(&sc->sc_txtask);
851 if (status & HAL_INT_BMISS) {
852 sc->sc_stats.ast_bmiss++;
853 TASK_RUN_OR_ENQUEUE(&sc->sc_bmisstask);
854 }
855 if (status & HAL_INT_MIB) {
856 sc->sc_stats.ast_mib++;
857 /*
858 * Disable interrupts until we service the MIB
859 * interrupt; otherwise it will continue to fire.
860 */
861 ath_hal_intrset(ah, 0);
862 /*
863 * Let the hal handle the event. We assume it will
864 * clear whatever condition caused the interrupt.
865 */
866 ath_hal_mibevent(ah,
867 &ATH_NODE(sc->sc_ic.ic_bss)->an_halstats);
868 ath_hal_intrset(ah, sc->sc_imask);
869 }
870 }
871 return 1;
872 }
873
874 static void
875 ath_fatal_proc(void *arg, int pending)
876 {
877 struct ath_softc *sc = arg;
878 struct ifnet *ifp = &sc->sc_if;
879
880 if_printf(ifp, "hardware error; resetting\n");
881 ath_reset(ifp);
882 }
883
884 static void
885 ath_rxorn_proc(void *arg, int pending)
886 {
887 struct ath_softc *sc = arg;
888 struct ifnet *ifp = &sc->sc_if;
889
890 if_printf(ifp, "rx FIFO overrun; resetting\n");
891 ath_reset(ifp);
892 }
893
894 static void
895 ath_bmiss_proc(void *arg, int pending)
896 {
897 struct ath_softc *sc = arg;
898 struct ieee80211com *ic = &sc->sc_ic;
899
900 DPRINTF(sc, ATH_DEBUG_ANY, "%s: pending %u\n", __func__, pending);
901 KASSERT(ic->ic_opmode == IEEE80211_M_STA,
902 ("unexpect operating mode %u", ic->ic_opmode));
903 if (ic->ic_state == IEEE80211_S_RUN) {
904 /*
905 * Rather than go directly to scan state, try to
906 * reassociate first. If that fails then the state
907 * machine will drop us into scanning after timing
908 * out waiting for a probe response.
909 */
910 NET_LOCK_GIANT();
911 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1);
912 NET_UNLOCK_GIANT();
913 }
914 }
915
916 static u_int
917 ath_chan2flags(struct ieee80211com *ic, struct ieee80211_channel *chan)
918 {
919 #define N(a) (sizeof(a) / sizeof(a[0]))
920 static const u_int modeflags[] = {
921 0, /* IEEE80211_MODE_AUTO */
922 CHANNEL_A, /* IEEE80211_MODE_11A */
923 CHANNEL_B, /* IEEE80211_MODE_11B */
924 CHANNEL_PUREG, /* IEEE80211_MODE_11G */
925 0, /* IEEE80211_MODE_FH */
926 CHANNEL_T, /* IEEE80211_MODE_TURBO_A */
927 CHANNEL_108G /* IEEE80211_MODE_TURBO_G */
928 };
929 enum ieee80211_phymode mode = ieee80211_chan2mode(ic, chan);
930
931 KASSERT(mode < N(modeflags), ("unexpected phy mode %u", mode));
932 KASSERT(modeflags[mode] != 0, ("mode %u undefined", mode));
933 return modeflags[mode];
934 #undef N
935 }
936
937 static int
938 ath_ifinit(struct ifnet *ifp)
939 {
940 struct ath_softc *sc = (struct ath_softc *)ifp->if_softc;
941
942 return ath_init(sc);
943 }
944
945 static int
946 ath_init(struct ath_softc *sc)
947 {
948 struct ifnet *ifp = &sc->sc_if;
949 struct ieee80211com *ic = &sc->sc_ic;
950 struct ieee80211_node *ni;
951 struct ath_hal *ah = sc->sc_ah;
952 HAL_STATUS status;
953 int error = 0;
954
955 DPRINTF(sc, ATH_DEBUG_ANY, "%s: if_flags 0x%x\n",
956 __func__, ifp->if_flags);
957
958 ATH_LOCK(sc);
959
960 if ((error = ath_enable(sc)) != 0)
961 return error;
962
963 /*
964 * Stop anything previously setup. This is safe
965 * whether this is the first time through or not.
966 */
967 ath_stop_locked(ifp, 0);
968
969 /*
970 * The basic interface to setting the hardware in a good
971 * state is ``reset''. On return the hardware is known to
972 * be powered up and with interrupts disabled. This must
973 * be followed by initialization of the appropriate bits
974 * and then setup of the interrupt mask.
975 */
976 sc->sc_curchan.channel = ic->ic_ibss_chan->ic_freq;
977 sc->sc_curchan.channelFlags = ath_chan2flags(ic, ic->ic_ibss_chan);
978 if (!ath_hal_reset(ah, ic->ic_opmode, &sc->sc_curchan, AH_FALSE, &status)) {
979 if_printf(ifp, "unable to reset hardware; hal status %u\n",
980 status);
981 error = EIO;
982 goto done;
983 }
984
985 /*
986 * This is needed only to setup initial state
987 * but it's best done after a reset.
988 */
989 ath_update_txpow(sc);
990
991 /*
992 * Setup the hardware after reset: the key cache
993 * is filled as needed and the receive engine is
994 * set going. Frame transmit is handled entirely
995 * in the frame output path; there's nothing to do
996 * here except setup the interrupt mask.
997 */
998 if ((error = ath_startrecv(sc)) != 0) {
999 if_printf(ifp, "unable to start recv logic\n");
1000 goto done;
1001 }
1002
1003 /*
1004 * Enable interrupts.
1005 */
1006 sc->sc_imask = HAL_INT_RX | HAL_INT_TX
1007 | HAL_INT_RXEOL | HAL_INT_RXORN
1008 | HAL_INT_FATAL | HAL_INT_GLOBAL;
1009 /*
1010 * Enable MIB interrupts when there are hardware phy counters.
1011 * Note we only do this (at the moment) for station mode.
1012 */
1013 if (sc->sc_needmib && ic->ic_opmode == IEEE80211_M_STA)
1014 sc->sc_imask |= HAL_INT_MIB;
1015 ath_hal_intrset(ah, sc->sc_imask);
1016
1017 ifp->if_flags |= IFF_RUNNING;
1018 ic->ic_state = IEEE80211_S_INIT;
1019
1020 /*
1021 * The hardware should be ready to go now so it's safe
1022 * to kick the 802.11 state machine as it's likely to
1023 * immediately call back to us to send mgmt frames.
1024 */
1025 ni = ic->ic_bss;
1026 ni->ni_chan = ic->ic_ibss_chan;
1027 ath_chan_change(sc, ni->ni_chan);
1028 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
1029 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
1030 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
1031 } else
1032 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
1033 done:
1034 ATH_UNLOCK(sc);
1035 return error;
1036 }
1037
1038 static void
1039 ath_stop_locked(struct ifnet *ifp, int disable)
1040 {
1041 struct ath_softc *sc = ifp->if_softc;
1042 struct ieee80211com *ic = &sc->sc_ic;
1043 struct ath_hal *ah = sc->sc_ah;
1044
1045 DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid %u if_flags 0x%x\n",
1046 __func__, sc->sc_invalid, ifp->if_flags);
1047
1048 ATH_LOCK_ASSERT(sc);
1049 if (ifp->if_flags & IFF_RUNNING) {
1050 /*
1051 * Shutdown the hardware and driver:
1052 * reset 802.11 state machine
1053 * turn off timers
1054 * disable interrupts
1055 * turn off the radio
1056 * clear transmit machinery
1057 * clear receive machinery
1058 * drain and release tx queues
1059 * reclaim beacon resources
1060 * power down hardware
1061 *
1062 * Note that some of this work is not possible if the
1063 * hardware is gone (invalid).
1064 */
1065 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
1066 ifp->if_flags &= ~IFF_RUNNING;
1067 ifp->if_timer = 0;
1068 if (!sc->sc_invalid) {
1069 if (sc->sc_softled) {
1070 callout_stop(&sc->sc_ledtimer);
1071 ath_hal_gpioset(ah, sc->sc_ledpin,
1072 !sc->sc_ledon);
1073 sc->sc_blinking = 0;
1074 }
1075 ath_hal_intrset(ah, 0);
1076 }
1077 ath_draintxq(sc);
1078 if (!sc->sc_invalid) {
1079 ath_stoprecv(sc);
1080 ath_hal_phydisable(ah);
1081 } else
1082 sc->sc_rxlink = NULL;
1083 IF_PURGE(&ifp->if_snd);
1084 ath_beacon_free(sc);
1085 if (disable)
1086 ath_disable(sc);
1087 }
1088 }
1089
1090 static void
1091 ath_stop(struct ifnet *ifp, int disable)
1092 {
1093 struct ath_softc *sc = ifp->if_softc;
1094
1095 ATH_LOCK(sc);
1096 ath_stop_locked(ifp, disable);
1097 if (!sc->sc_invalid) {
1098 /*
1099 * Set the chip in full sleep mode. Note that we are
1100 * careful to do this only when bringing the interface
1101 * completely to a stop. When the chip is in this state
1102 * it must be carefully woken up or references to
1103 * registers in the PCI clock domain may freeze the bus
1104 * (and system). This varies by chip and is mostly an
1105 * issue with newer parts that go to sleep more quickly.
1106 */
1107 ath_hal_setpower(sc->sc_ah, HAL_PM_FULL_SLEEP, 0);
1108 }
1109 ATH_UNLOCK(sc);
1110 }
1111
1112 /*
1113 * Reset the hardware w/o losing operational state. This is
1114 * basically a more efficient way of doing ath_stop, ath_init,
1115 * followed by state transitions to the current 802.11
1116 * operational state. Used to recover from various errors and
1117 * to reset or reload hardware state.
1118 */
1119 int
1120 ath_reset(struct ifnet *ifp)
1121 {
1122 struct ath_softc *sc = ifp->if_softc;
1123 struct ieee80211com *ic = &sc->sc_ic;
1124 struct ath_hal *ah = sc->sc_ah;
1125 struct ieee80211_channel *c;
1126 HAL_STATUS status;
1127
1128 /*
1129 * Convert to a HAL channel description with the flags
1130 * constrained to reflect the current operating mode.
1131 */
1132 c = ic->ic_ibss_chan;
1133 sc->sc_curchan.channel = c->ic_freq;
1134 sc->sc_curchan.channelFlags = ath_chan2flags(ic, c);
1135
1136 ath_hal_intrset(ah, 0); /* disable interrupts */
1137 ath_draintxq(sc); /* stop xmit side */
1138 ath_stoprecv(sc); /* stop recv side */
1139 /* NB: indicate channel change so we do a full reset */
1140 if (!ath_hal_reset(ah, ic->ic_opmode, &sc->sc_curchan, AH_TRUE, &status))
1141 if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
1142 __func__, status);
1143 ath_update_txpow(sc); /* update tx power state */
1144 if (ath_startrecv(sc) != 0) /* restart recv */
1145 if_printf(ifp, "%s: unable to start recv logic\n", __func__);
1146 /*
1147 * We may be doing a reset in response to an ioctl
1148 * that changes the channel so update any state that
1149 * might change as a result.
1150 */
1151 ath_chan_change(sc, c);
1152 if (ic->ic_state == IEEE80211_S_RUN)
1153 ath_beacon_config(sc); /* restart beacons */
1154 ath_hal_intrset(ah, sc->sc_imask);
1155
1156 ath_start(ifp); /* restart xmit */
1157 return 0;
1158 }
1159
1160 static void
1161 ath_start(struct ifnet *ifp)
1162 {
1163 struct ath_softc *sc = ifp->if_softc;
1164 struct ath_hal *ah = sc->sc_ah;
1165 struct ieee80211com *ic = &sc->sc_ic;
1166 struct ieee80211_node *ni;
1167 struct ath_buf *bf;
1168 struct mbuf *m;
1169 struct ieee80211_frame *wh;
1170 struct ether_header *eh;
1171
1172 if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
1173 return;
1174 for (;;) {
1175 /*
1176 * Grab a TX buffer and associated resources.
1177 */
1178 ATH_TXBUF_LOCK(sc);
1179 bf = STAILQ_FIRST(&sc->sc_txbuf);
1180 if (bf != NULL)
1181 STAILQ_REMOVE_HEAD(&sc->sc_txbuf, bf_list);
1182 ATH_TXBUF_UNLOCK(sc);
1183 if (bf == NULL) {
1184 DPRINTF(sc, ATH_DEBUG_ANY, "%s: out of xmit buffers\n",
1185 __func__);
1186 sc->sc_stats.ast_tx_qstop++;
1187 ifp->if_flags |= IFF_OACTIVE;
1188 break;
1189 }
1190 /*
1191 * Poll the management queue for frames; they
1192 * have priority over normal data frames.
1193 */
1194 IF_DEQUEUE(&ic->ic_mgtq, m);
1195 if (m == NULL) {
1196 /*
1197 * No data frames go out unless we're associated.
1198 */
1199 if (ic->ic_state != IEEE80211_S_RUN) {
1200 DPRINTF(sc, ATH_DEBUG_ANY,
1201 "%s: ignore data packet, state %u\n",
1202 __func__, ic->ic_state);
1203 sc->sc_stats.ast_tx_discard++;
1204 ATH_TXBUF_LOCK(sc);
1205 STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
1206 ATH_TXBUF_UNLOCK(sc);
1207 break;
1208 }
1209 IFQ_DEQUEUE(&ifp->if_snd, m); /* XXX: LOCK */
1210 if (m == NULL) {
1211 ATH_TXBUF_LOCK(sc);
1212 STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
1213 ATH_TXBUF_UNLOCK(sc);
1214 break;
1215 }
1216 /*
1217 * Find the node for the destination so we can do
1218 * things like power save and fast frames aggregation.
1219 */
1220 if (m->m_len < sizeof(struct ether_header) &&
1221 (m = m_pullup(m, sizeof(struct ether_header))) == NULL) {
1222 ic->ic_stats.is_tx_nobuf++; /* XXX */
1223 ni = NULL;
1224 goto bad;
1225 }
1226 eh = mtod(m, struct ether_header *);
1227 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1228 if (ni == NULL) {
1229 /* NB: ieee80211_find_txnode does stat+msg */
1230 m_freem(m);
1231 goto bad;
1232 }
1233 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) &&
1234 (m->m_flags & M_PWR_SAV) == 0) {
1235 /*
1236 * Station in power save mode; pass the frame
1237 * to the 802.11 layer and continue. We'll get
1238 * the frame back when the time is right.
1239 */
1240 ieee80211_pwrsave(ic, ni, m);
1241 goto reclaim;
1242 }
1243 /* calculate priority so we can find the tx queue */
1244 if (ieee80211_classify(ic, m, ni)) {
1245 DPRINTF(sc, ATH_DEBUG_XMIT,
1246 "%s: discard, classification failure\n",
1247 __func__);
1248 m_freem(m);
1249 goto bad;
1250 }
1251 ifp->if_opackets++;
1252
1253 #if NBPFILTER > 0
1254 if (ifp->if_bpf)
1255 bpf_mtap(ifp->if_bpf, m);
1256 #endif
1257 /*
1258 * Encapsulate the packet in prep for transmission.
1259 */
1260 m = ieee80211_encap(ic, m, ni);
1261 if (m == NULL) {
1262 DPRINTF(sc, ATH_DEBUG_ANY,
1263 "%s: encapsulation failure\n",
1264 __func__);
1265 sc->sc_stats.ast_tx_encap++;
1266 goto bad;
1267 }
1268 } else {
1269 /*
1270 * Hack! The referenced node pointer is in the
1271 * rcvif field of the packet header. This is
1272 * placed there by ieee80211_mgmt_output because
1273 * we need to hold the reference with the frame
1274 * and there's no other way (other than packet
1275 * tags which we consider too expensive to use)
1276 * to pass it along.
1277 */
1278 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
1279 m->m_pkthdr.rcvif = NULL;
1280
1281 wh = mtod(m, struct ieee80211_frame *);
1282 if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1283 IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
1284 /* fill time stamp */
1285 u_int64_t tsf;
1286 u_int32_t *tstamp;
1287
1288 tsf = ath_hal_gettsf64(ah);
1289 /* XXX: adjust 100us delay to xmit */
1290 tsf += 100;
1291 tstamp = (u_int32_t *)&wh[1];
1292 tstamp[0] = htole32(tsf & 0xffffffff);
1293 tstamp[1] = htole32(tsf >> 32);
1294 }
1295 sc->sc_stats.ast_tx_mgmt++;
1296 }
1297
1298 if (ath_tx_start(sc, ni, bf, m)) {
1299 bad:
1300 ifp->if_oerrors++;
1301 reclaim:
1302 ATH_TXBUF_LOCK(sc);
1303 STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
1304 ATH_TXBUF_UNLOCK(sc);
1305 if (ni != NULL)
1306 ieee80211_free_node(ni);
1307 continue;
1308 }
1309
1310 sc->sc_tx_timer = 5;
1311 ifp->if_timer = 1;
1312 }
1313 }
1314
1315 static int
1316 ath_media_change(struct ifnet *ifp)
1317 {
1318 #define IS_UP(ifp) \
1319 ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == (IFF_RUNNING|IFF_UP))
1320 int error;
1321
1322 error = ieee80211_media_change(ifp);
1323 if (error == ENETRESET) {
1324 if (IS_UP(ifp))
1325 ath_init(ifp->if_softc); /* XXX lose error */
1326 error = 0;
1327 }
1328 return error;
1329 #undef IS_UP
1330 }
1331
1332 #ifdef AR_DEBUG
1333 static void
1334 ath_keyprint(const char *tag, u_int ix,
1335 const HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
1336 {
1337 static const char *ciphers[] = {
1338 "WEP",
1339 "AES-OCB",
1340 "AES-CCM",
1341 "CKIP",
1342 "TKIP",
1343 "CLR",
1344 };
1345 int i, n;
1346
1347 printf("%s: [%02u] %-7s ", tag, ix, ciphers[hk->kv_type]);
1348 for (i = 0, n = hk->kv_len; i < n; i++)
1349 printf("%02x", hk->kv_val[i]);
1350 printf(" mac %s", ether_sprintf(mac));
1351 if (hk->kv_type == HAL_CIPHER_TKIP) {
1352 printf(" mic ");
1353 for (i = 0; i < sizeof(hk->kv_mic); i++)
1354 printf("%02x", hk->kv_mic[i]);
1355 }
1356 printf("\n");
1357 }
1358 #endif
1359
1360 /*
1361 * Set a TKIP key into the hardware. This handles the
1362 * potential distribution of key state to multiple key
1363 * cache slots for TKIP.
1364 */
1365 static int
1366 ath_keyset_tkip(struct ath_softc *sc, const struct ieee80211_key *k,
1367 HAL_KEYVAL *hk, const u_int8_t mac[IEEE80211_ADDR_LEN])
1368 {
1369 #define IEEE80211_KEY_XR (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV)
1370 static const u_int8_t zerobssid[IEEE80211_ADDR_LEN];
1371 struct ath_hal *ah = sc->sc_ah;
1372
1373 KASSERT(k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP,
1374 ("got a non-TKIP key, cipher %u", k->wk_cipher->ic_cipher));
1375 KASSERT(sc->sc_splitmic, ("key cache !split"));
1376 if ((k->wk_flags & IEEE80211_KEY_XR) == IEEE80211_KEY_XR) {
1377 /*
1378 * TX key goes at first index, RX key at +32.
1379 * The hal handles the MIC keys at index+64.
1380 */
1381 memcpy(hk->kv_mic, k->wk_txmic, sizeof(hk->kv_mic));
1382 KEYPRINTF(sc, k->wk_keyix, hk, zerobssid);
1383 if (!ath_hal_keyset(ah, k->wk_keyix, hk, zerobssid))
1384 return 0;
1385
1386 memcpy(hk->kv_mic, k->wk_rxmic, sizeof(hk->kv_mic));
1387 KEYPRINTF(sc, k->wk_keyix+32, hk, mac);
1388 /* XXX delete tx key on failure? */
1389 return ath_hal_keyset(ah, k->wk_keyix+32, hk, mac);
1390 } else if (k->wk_flags & IEEE80211_KEY_XR) {
1391 /*
1392 * TX/RX key goes at first index.
1393 * The hal handles the MIC keys are index+64.
1394 */
1395 memcpy(hk->kv_mic, k->wk_flags & IEEE80211_KEY_XMIT ?
1396 k->wk_txmic : k->wk_rxmic, sizeof(hk->kv_mic));
1397 KEYPRINTF(sc, k->wk_keyix, hk, mac);
1398 return ath_hal_keyset(ah, k->wk_keyix, hk, mac);
1399 }
1400 return 0;
1401 #undef IEEE80211_KEY_XR
1402 }
1403
1404 /*
1405 * Set a net80211 key into the hardware. This handles the
1406 * potential distribution of key state to multiple key
1407 * cache slots for TKIP with hardware MIC support.
1408 */
1409 static int
1410 ath_keyset(struct ath_softc *sc, const struct ieee80211_key *k,
1411 const u_int8_t mac0[IEEE80211_ADDR_LEN],
1412 struct ieee80211_node *bss)
1413 {
1414 #define N(a) (sizeof(a)/sizeof(a[0]))
1415 static const u_int8_t ciphermap[] = {
1416 HAL_CIPHER_WEP, /* IEEE80211_CIPHER_WEP */
1417 HAL_CIPHER_TKIP, /* IEEE80211_CIPHER_TKIP */
1418 HAL_CIPHER_AES_OCB, /* IEEE80211_CIPHER_AES_OCB */
1419 HAL_CIPHER_AES_CCM, /* IEEE80211_CIPHER_AES_CCM */
1420 (u_int8_t) -1, /* 4 is not allocated */
1421 HAL_CIPHER_CKIP, /* IEEE80211_CIPHER_CKIP */
1422 HAL_CIPHER_CLR, /* IEEE80211_CIPHER_NONE */
1423 };
1424 struct ath_hal *ah = sc->sc_ah;
1425 const struct ieee80211_cipher *cip = k->wk_cipher;
1426 u_int8_t gmac[IEEE80211_ADDR_LEN];
1427 const u_int8_t *mac;
1428 HAL_KEYVAL hk;
1429
1430 memset(&hk, 0, sizeof(hk));
1431 /*
1432 * Software crypto uses a "clear key" so non-crypto
1433 * state kept in the key cache are maintained and
1434 * so that rx frames have an entry to match.
1435 */
1436 if ((k->wk_flags & IEEE80211_KEY_SWCRYPT) == 0) {
1437 KASSERT(cip->ic_cipher < N(ciphermap),
1438 ("invalid cipher type %u", cip->ic_cipher));
1439 hk.kv_type = ciphermap[cip->ic_cipher];
1440 hk.kv_len = k->wk_keylen;
1441 memcpy(hk.kv_val, k->wk_key, k->wk_keylen);
1442 } else
1443 hk.kv_type = HAL_CIPHER_CLR;
1444
1445 if ((k->wk_flags & IEEE80211_KEY_GROUP) && sc->sc_mcastkey) {
1446 /*
1447 * Group keys on hardware that supports multicast frame
1448 * key search use a mac that is the sender's address with
1449 * the high bit set instead of the app-specified address.
1450 */
1451 IEEE80211_ADDR_COPY(gmac, bss->ni_macaddr);
1452 gmac[0] |= 0x80;
1453 mac = gmac;
1454 } else
1455 mac = mac0;
1456
1457 if (hk.kv_type == HAL_CIPHER_TKIP &&
1458 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 &&
1459 sc->sc_splitmic) {
1460 return ath_keyset_tkip(sc, k, &hk, mac);
1461 } else {
1462 KEYPRINTF(sc, k->wk_keyix, &hk, mac);
1463 return ath_hal_keyset(ah, k->wk_keyix, &hk, mac);
1464 }
1465 #undef N
1466 }
1467
1468 /*
1469 * Allocate tx/rx key slots for TKIP. We allocate two slots for
1470 * each key, one for decrypt/encrypt and the other for the MIC.
1471 */
1472 static u_int16_t
1473 key_alloc_2pair(struct ath_softc *sc)
1474 {
1475 #define N(a) (sizeof(a)/sizeof(a[0]))
1476 u_int i, keyix;
1477
1478 KASSERT(sc->sc_splitmic, ("key cache !split"));
1479 /* XXX could optimize */
1480 for (i = 0; i < N(sc->sc_keymap)/4; i++) {
1481 u_int8_t b = sc->sc_keymap[i];
1482 if (b != 0xff) {
1483 /*
1484 * One or more slots in this byte are free.
1485 */
1486 keyix = i*NBBY;
1487 while (b & 1) {
1488 again:
1489 keyix++;
1490 b >>= 1;
1491 }
1492 /* XXX IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV */
1493 if (isset(sc->sc_keymap, keyix+32) ||
1494 isset(sc->sc_keymap, keyix+64) ||
1495 isset(sc->sc_keymap, keyix+32+64)) {
1496 /* full pair unavailable */
1497 /* XXX statistic */
1498 if (keyix == (i+1)*NBBY) {
1499 /* no slots were appropriate, advance */
1500 continue;
1501 }
1502 goto again;
1503 }
1504 setbit(sc->sc_keymap, keyix);
1505 setbit(sc->sc_keymap, keyix+64);
1506 setbit(sc->sc_keymap, keyix+32);
1507 setbit(sc->sc_keymap, keyix+32+64);
1508 DPRINTF(sc, ATH_DEBUG_KEYCACHE,
1509 "%s: key pair %u,%u %u,%u\n",
1510 __func__, keyix, keyix+64,
1511 keyix+32, keyix+32+64);
1512 return keyix;
1513 }
1514 }
1515 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of pair space\n", __func__);
1516 return IEEE80211_KEYIX_NONE;
1517 #undef N
1518 }
1519
1520 /*
1521 * Allocate a single key cache slot.
1522 */
1523 static u_int16_t
1524 key_alloc_single(struct ath_softc *sc)
1525 {
1526 #define N(a) (sizeof(a)/sizeof(a[0]))
1527 u_int i, keyix;
1528
1529 /* XXX try i,i+32,i+64,i+32+64 to minimize key pair conflicts */
1530 for (i = 0; i < N(sc->sc_keymap); i++) {
1531 u_int8_t b = sc->sc_keymap[i];
1532 if (b != 0xff) {
1533 /*
1534 * One or more slots are free.
1535 */
1536 keyix = i*NBBY;
1537 while (b & 1)
1538 keyix++, b >>= 1;
1539 setbit(sc->sc_keymap, keyix);
1540 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: key %u\n",
1541 __func__, keyix);
1542 return keyix;
1543 }
1544 }
1545 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: out of space\n", __func__);
1546 return IEEE80211_KEYIX_NONE;
1547 #undef N
1548 }
1549
1550 /*
1551 * Allocate one or more key cache slots for a uniacst key. The
1552 * key itself is needed only to identify the cipher. For hardware
1553 * TKIP with split cipher+MIC keys we allocate two key cache slot
1554 * pairs so that we can setup separate TX and RX MIC keys. Note
1555 * that the MIC key for a TKIP key at slot i is assumed by the
1556 * hardware to be at slot i+64. This limits TKIP keys to the first
1557 * 64 entries.
1558 */
1559 static int
1560 ath_key_alloc(struct ieee80211com *ic, const struct ieee80211_key *k)
1561 {
1562 struct ath_softc *sc = ic->ic_ifp->if_softc;
1563
1564 /*
1565 * Group key allocation must be handled specially for
1566 * parts that do not support multicast key cache search
1567 * functionality. For those parts the key id must match
1568 * the h/w key index so lookups find the right key. On
1569 * parts w/ the key search facility we install the sender's
1570 * mac address (with the high bit set) and let the hardware
1571 * find the key w/o using the key id. This is preferred as
1572 * it permits us to support multiple users for adhoc and/or
1573 * multi-station operation.
1574 */
1575 if ((k->wk_flags & IEEE80211_KEY_GROUP) && !sc->sc_mcastkey) {
1576 u_int keyix;
1577
1578 if (!(&ic->ic_nw_keys[0] <= k &&
1579 k < &ic->ic_nw_keys[IEEE80211_WEP_NKID])) {
1580 /* should not happen */
1581 DPRINTF(sc, ATH_DEBUG_KEYCACHE,
1582 "%s: bogus group key\n", __func__);
1583 return IEEE80211_KEYIX_NONE;
1584 }
1585 keyix = k - ic->ic_nw_keys;
1586 /*
1587 * XXX we pre-allocate the global keys so
1588 * have no way to check if they've already been allocated.
1589 */
1590 return keyix;
1591 }
1592
1593 /*
1594 * We allocate two pair for TKIP when using the h/w to do
1595 * the MIC. For everything else, including software crypto,
1596 * we allocate a single entry. Note that s/w crypto requires
1597 * a pass-through slot on the 5211 and 5212. The 5210 does
1598 * not support pass-through cache entries and we map all
1599 * those requests to slot 0.
1600 */
1601 if (k->wk_flags & IEEE80211_KEY_SWCRYPT) {
1602 return key_alloc_single(sc);
1603 } else if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_TKIP &&
1604 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && sc->sc_splitmic) {
1605 return key_alloc_2pair(sc);
1606 } else {
1607 return key_alloc_single(sc);
1608 }
1609 }
1610
1611 /*
1612 * Delete an entry in the key cache allocated by ath_key_alloc.
1613 */
1614 static int
1615 ath_key_delete(struct ieee80211com *ic, const struct ieee80211_key *k)
1616 {
1617 struct ath_softc *sc = ic->ic_ifp->if_softc;
1618 struct ath_hal *ah = sc->sc_ah;
1619 const struct ieee80211_cipher *cip = k->wk_cipher;
1620 struct ieee80211_node *ni;
1621 u_int keyix = k->wk_keyix;
1622
1623 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s: delete key %u\n", __func__, keyix);
1624
1625 ath_hal_keyreset(ah, keyix);
1626 /*
1627 * Check the key->node map and flush any ref.
1628 */
1629 ni = sc->sc_keyixmap[keyix];
1630 if (ni != NULL) {
1631 ieee80211_free_node(ni);
1632 sc->sc_keyixmap[keyix] = NULL;
1633 }
1634 /*
1635 * Handle split tx/rx keying required for TKIP with h/w MIC.
1636 */
1637 if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
1638 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && sc->sc_splitmic) {
1639 ath_hal_keyreset(ah, keyix+32); /* RX key */
1640 ni = sc->sc_keyixmap[keyix+32];
1641 if (ni != NULL) { /* as above... */
1642 ieee80211_free_node(ni);
1643 sc->sc_keyixmap[keyix+32] = NULL;
1644 }
1645 }
1646 if (keyix >= IEEE80211_WEP_NKID) {
1647 /*
1648 * Don't touch keymap entries for global keys so
1649 * they are never considered for dynamic allocation.
1650 */
1651 clrbit(sc->sc_keymap, keyix);
1652 if (cip->ic_cipher == IEEE80211_CIPHER_TKIP &&
1653 (k->wk_flags & IEEE80211_KEY_SWMIC) == 0 &&
1654 sc->sc_splitmic) {
1655 clrbit(sc->sc_keymap, keyix+64); /* TX key MIC */
1656 clrbit(sc->sc_keymap, keyix+32); /* RX key */
1657 clrbit(sc->sc_keymap, keyix+32+64); /* RX key MIC */
1658 }
1659 }
1660 return 1;
1661 }
1662
1663 /*
1664 * Set the key cache contents for the specified key. Key cache
1665 * slot(s) must already have been allocated by ath_key_alloc.
1666 */
1667 static int
1668 ath_key_set(struct ieee80211com *ic, const struct ieee80211_key *k,
1669 const u_int8_t mac[IEEE80211_ADDR_LEN])
1670 {
1671 struct ath_softc *sc = ic->ic_ifp->if_softc;
1672
1673 return ath_keyset(sc, k, mac, ic->ic_bss);
1674 }
1675
1676 /*
1677 * Block/unblock tx+rx processing while a key change is done.
1678 * We assume the caller serializes key management operations
1679 * so we only need to worry about synchronization with other
1680 * uses that originate in the driver.
1681 */
1682 static void
1683 ath_key_update_begin(struct ieee80211com *ic)
1684 {
1685 struct ifnet *ifp = ic->ic_ifp;
1686 struct ath_softc *sc = ifp->if_softc;
1687
1688 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
1689 #if 0
1690 tasklet_disable(&sc->sc_rxtq);
1691 #endif
1692 IF_LOCK(&ifp->if_snd); /* NB: doesn't block mgmt frames */
1693 }
1694
1695 static void
1696 ath_key_update_end(struct ieee80211com *ic)
1697 {
1698 struct ifnet *ifp = ic->ic_ifp;
1699 struct ath_softc *sc = ifp->if_softc;
1700
1701 DPRINTF(sc, ATH_DEBUG_KEYCACHE, "%s:\n", __func__);
1702 IF_UNLOCK(&ifp->if_snd);
1703 #if 0
1704 tasklet_enable(&sc->sc_rxtq);
1705 #endif
1706 }
1707
1708 /*
1709 * Calculate the receive filter according to the
1710 * operating mode and state:
1711 *
1712 * o always accept unicast, broadcast, and multicast traffic
1713 * o maintain current state of phy error reception (the hal
1714 * may enable phy error frames for noise immunity work)
1715 * o probe request frames are accepted only when operating in
1716 * hostap, adhoc, or monitor modes
1717 * o enable promiscuous mode according to the interface state
1718 * o accept beacons:
1719 * - when operating in adhoc mode so the 802.11 layer creates
1720 * node table entries for peers,
1721 * - when operating in station mode for collecting rssi data when
1722 * the station is otherwise quiet, or
1723 * - when scanning
1724 */
1725 static u_int32_t
1726 ath_calcrxfilter(struct ath_softc *sc, enum ieee80211_state state)
1727 {
1728 struct ieee80211com *ic = &sc->sc_ic;
1729 struct ath_hal *ah = sc->sc_ah;
1730 struct ifnet *ifp = &sc->sc_if;
1731 u_int32_t rfilt;
1732
1733 rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR)
1734 | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
1735 if (ic->ic_opmode != IEEE80211_M_STA)
1736 rfilt |= HAL_RX_FILTER_PROBEREQ;
1737 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1738 (ifp->if_flags & IFF_PROMISC))
1739 rfilt |= HAL_RX_FILTER_PROM;
1740 if (ic->ic_opmode == IEEE80211_M_STA ||
1741 ic->ic_opmode == IEEE80211_M_IBSS ||
1742 state == IEEE80211_S_SCAN)
1743 rfilt |= HAL_RX_FILTER_BEACON;
1744 return rfilt;
1745 }
1746
1747 static void
1748 ath_mcastfilter_accum(caddr_t dl, u_int32_t *mfilt)
1749 {
1750 u_int32_t val;
1751 u_int8_t pos;
1752
1753 /* calculate XOR of eight 6bit values */
1754 val = LE_READ_4(dl + 0);
1755 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
1756 val = LE_READ_4(dl + 3);
1757 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
1758 pos &= 0x3f;
1759 mfilt[pos / 32] |= (1 << (pos % 32));
1760 }
1761
1762 static void
1763 ath_mcastfilter_compute(struct ath_softc *sc, u_int32_t *mfilt)
1764 {
1765 struct ifnet *ifp = &sc->sc_if;
1766 struct ether_multi *enm;
1767 struct ether_multistep estep;
1768
1769 mfilt[0] = mfilt[1] = 0;
1770 ETHER_FIRST_MULTI(estep, &sc->sc_ec, enm);
1771 while (enm != NULL) {
1772 /* XXX Punt on ranges. */
1773 if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi)) {
1774 mfilt[0] = mfilt[1] = ~((u_int32_t)0);
1775 ifp->if_flags |= IFF_ALLMULTI;
1776 return;
1777 }
1778 ath_mcastfilter_accum(enm->enm_addrlo, mfilt);
1779 ETHER_NEXT_MULTI(estep, enm);
1780 }
1781 ifp->if_flags &= ~IFF_ALLMULTI;
1782 }
1783
1784 static void
1785 ath_mode_init(struct ath_softc *sc)
1786 {
1787 struct ieee80211com *ic = &sc->sc_ic;
1788 struct ath_hal *ah = sc->sc_ah;
1789 u_int32_t rfilt, mfilt[2];
1790 int i;
1791
1792 /* configure rx filter */
1793 rfilt = ath_calcrxfilter(sc, ic->ic_state);
1794 ath_hal_setrxfilter(ah, rfilt);
1795
1796 /* configure operational mode */
1797 ath_hal_setopmode(ah);
1798
1799 /* Write keys to hardware; it may have been powered down. */
1800 ath_key_update_begin(ic);
1801 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1802 ath_key_set(ic,
1803 &ic->ic_crypto.cs_nw_keys[i],
1804 ic->ic_myaddr);
1805 }
1806 ath_key_update_end(ic);
1807
1808 /*
1809 * Handle any link-level address change. Note that we only
1810 * need to force ic_myaddr; any other addresses are handled
1811 * as a byproduct of the ifnet code marking the interface
1812 * down then up.
1813 *
1814 * XXX should get from lladdr instead of arpcom but that's more work
1815 */
1816 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(sc->sc_if.if_sadl));
1817 ath_hal_setmac(ah, ic->ic_myaddr);
1818
1819 /* calculate and install multicast filter */
1820 #ifdef __FreeBSD__
1821 if ((sc->sc_if.if_flags & IFF_ALLMULTI) == 0)
1822 ath_mcastfilter_compute(sc, mfilt);
1823 else
1824 mfilt[0] = mfilt[1] = ~0;
1825 #endif
1826 #ifdef __NetBSD__
1827 ath_mcastfilter_compute(sc, mfilt);
1828 #endif
1829 ath_hal_setmcastfilter(ah, mfilt[0], mfilt[1]);
1830 DPRINTF(sc, ATH_DEBUG_MODE, "%s: RX filter 0x%x, MC filter %08x:%08x\n",
1831 __func__, rfilt, mfilt[0], mfilt[1]);
1832 }
1833
1834 /*
1835 * Set the slot time based on the current setting.
1836 */
1837 static void
1838 ath_setslottime(struct ath_softc *sc)
1839 {
1840 struct ieee80211com *ic = &sc->sc_ic;
1841 struct ath_hal *ah = sc->sc_ah;
1842
1843 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1844 ath_hal_setslottime(ah, HAL_SLOT_TIME_9);
1845 else
1846 ath_hal_setslottime(ah, HAL_SLOT_TIME_20);
1847 sc->sc_updateslot = OK;
1848 }
1849
1850 /*
1851 * Callback from the 802.11 layer to update the
1852 * slot time based on the current setting.
1853 */
1854 static void
1855 ath_updateslot(struct ifnet *ifp)
1856 {
1857 struct ath_softc *sc = ifp->if_softc;
1858 struct ieee80211com *ic = &sc->sc_ic;
1859
1860 /*
1861 * When not coordinating the BSS, change the hardware
1862 * immediately. For other operation we defer the change
1863 * until beacon updates have propagated to the stations.
1864 */
1865 if (ic->ic_opmode == IEEE80211_M_HOSTAP)
1866 sc->sc_updateslot = UPDATE;
1867 else
1868 ath_setslottime(sc);
1869 }
1870
1871 /*
1872 * Setup a h/w transmit queue for beacons.
1873 */
1874 static int
1875 ath_beaconq_setup(struct ath_hal *ah)
1876 {
1877 HAL_TXQ_INFO qi;
1878
1879 memset(&qi, 0, sizeof(qi));
1880 qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
1881 qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
1882 qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
1883 /* NB: for dynamic turbo, don't enable any other interrupts */
1884 qi.tqi_qflags = TXQ_FLAG_TXDESCINT_ENABLE;
1885 return ath_hal_setuptxqueue(ah, HAL_TX_QUEUE_BEACON, &qi);
1886 }
1887
1888 /*
1889 * Setup the transmit queue parameters for the beacon queue.
1890 */
1891 static int
1892 ath_beaconq_config(struct ath_softc *sc)
1893 {
1894 #define ATH_EXPONENT_TO_VALUE(v) ((1<<(v))-1)
1895 struct ieee80211com *ic = &sc->sc_ic;
1896 struct ath_hal *ah = sc->sc_ah;
1897 HAL_TXQ_INFO qi;
1898
1899 ath_hal_gettxqueueprops(ah, sc->sc_bhalq, &qi);
1900 if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1901 /*
1902 * Always burst out beacon and CAB traffic.
1903 */
1904 qi.tqi_aifs = ATH_BEACON_AIFS_DEFAULT;
1905 qi.tqi_cwmin = ATH_BEACON_CWMIN_DEFAULT;
1906 qi.tqi_cwmax = ATH_BEACON_CWMAX_DEFAULT;
1907 } else {
1908 struct wmeParams *wmep =
1909 &ic->ic_wme.wme_chanParams.cap_wmeParams[WME_AC_BE];
1910 /*
1911 * Adhoc mode; important thing is to use 2x cwmin.
1912 */
1913 qi.tqi_aifs = wmep->wmep_aifsn;
1914 qi.tqi_cwmin = 2*ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
1915 qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
1916 }
1917
1918 if (!ath_hal_settxqueueprops(ah, sc->sc_bhalq, &qi)) {
1919 device_printf(sc->sc_dev, "unable to update parameters for "
1920 "beacon hardware queue!\n");
1921 return 0;
1922 } else {
1923 ath_hal_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */
1924 return 1;
1925 }
1926 #undef ATH_EXPONENT_TO_VALUE
1927 }
1928
1929 /*
1930 * Allocate and setup an initial beacon frame.
1931 */
1932 static int
1933 ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
1934 {
1935 struct ieee80211com *ic = ni->ni_ic;
1936 struct ath_buf *bf;
1937 struct mbuf *m;
1938 int error;
1939
1940 bf = STAILQ_FIRST(&sc->sc_bbuf);
1941 if (bf == NULL) {
1942 DPRINTF(sc, ATH_DEBUG_BEACON, "%s: no dma buffers\n", __func__);
1943 sc->sc_stats.ast_be_nombuf++; /* XXX */
1944 return ENOMEM; /* XXX */
1945 }
1946 /*
1947 * NB: the beacon data buffer must be 32-bit aligned;
1948 * we assume the mbuf routines will return us something
1949 * with this alignment (perhaps should assert).
1950 */
1951 m = ieee80211_beacon_alloc(ic, ni, &sc->sc_boff);
1952 if (m == NULL) {
1953 DPRINTF(sc, ATH_DEBUG_BEACON, "%s: cannot get mbuf\n",
1954 __func__);
1955 sc->sc_stats.ast_be_nombuf++;
1956 return ENOMEM;
1957 }
1958 error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m,
1959 BUS_DMA_NOWAIT);
1960 if (error == 0) {
1961 bf->bf_m = m;
1962 bf->bf_node = ieee80211_ref_node(ni);
1963 } else {
1964 m_freem(m);
1965 }
1966 return error;
1967 }
1968
1969 /*
1970 * Setup the beacon frame for transmit.
1971 */
1972 static void
1973 ath_beacon_setup(struct ath_softc *sc, struct ath_buf *bf)
1974 {
1975 #define USE_SHPREAMBLE(_ic) \
1976 (((_ic)->ic_flags & (IEEE80211_F_SHPREAMBLE | IEEE80211_F_USEBARKER))\
1977 == IEEE80211_F_SHPREAMBLE)
1978 struct ieee80211_node *ni = bf->bf_node;
1979 struct ieee80211com *ic = ni->ni_ic;
1980 struct mbuf *m = bf->bf_m;
1981 struct ath_hal *ah = sc->sc_ah;
1982 struct ath_node *an = ATH_NODE(ni);
1983 struct ath_desc *ds;
1984 int flags, antenna;
1985 u_int8_t rate;
1986
1987 DPRINTF(sc, ATH_DEBUG_BEACON, "%s: m %p len %u\n",
1988 __func__, m, m->m_len);
1989
1990 /* setup descriptors */
1991 ds = bf->bf_desc;
1992
1993 flags = HAL_TXDESC_NOACK;
1994 if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) {
1995 ds->ds_link = bf->bf_daddr; /* self-linked */
1996 flags |= HAL_TXDESC_VEOL;
1997 /*
1998 * Let hardware handle antenna switching unless
1999 * the user has selected a transmit antenna
2000 * (sc_txantenna is not 0).
2001 */
2002 antenna = sc->sc_txantenna;
2003 } else {
2004 ds->ds_link = 0;
2005 /*
2006 * Switch antenna every 4 beacons, unless the user
2007 * has selected a transmit antenna (sc_txantenna
2008 * is not 0).
2009 *
2010 * XXX assumes two antenna
2011 */
2012 if (sc->sc_txantenna == 0)
2013 antenna = (sc->sc_stats.ast_be_xmit & 4 ? 2 : 1);
2014 else
2015 antenna = sc->sc_txantenna;
2016 }
2017
2018 KASSERT(bf->bf_nseg == 1,
2019 ("multi-segment beacon frame; nseg %u", bf->bf_nseg));
2020 ds->ds_data = bf->bf_segs[0].ds_addr;
2021 /*
2022 * Calculate rate code.
2023 * XXX everything at min xmit rate
2024 */
2025 if (USE_SHPREAMBLE(ic))
2026 rate = an->an_tx_mgtratesp;
2027 else
2028 rate = an->an_tx_mgtrate;
2029 ath_hal_setuptxdesc(ah, ds
2030 , m->m_len + IEEE80211_CRC_LEN /* frame length */
2031 , sizeof(struct ieee80211_frame)/* header length */
2032 , HAL_PKT_TYPE_BEACON /* Atheros packet type */
2033 , ni->ni_txpower /* txpower XXX */
2034 , rate, 1 /* series 0 rate/tries */
2035 , HAL_TXKEYIX_INVALID /* no encryption */
2036 , antenna /* antenna mode */
2037 , flags /* no ack, veol for beacons */
2038 , 0 /* rts/cts rate */
2039 , 0 /* rts/cts duration */
2040 );
2041 /* NB: beacon's BufLen must be a multiple of 4 bytes */
2042 ath_hal_filltxdesc(ah, ds
2043 , roundup(m->m_len, 4) /* buffer length */
2044 , AH_TRUE /* first segment */
2045 , AH_TRUE /* last segment */
2046 , ds /* first descriptor */
2047 );
2048 #undef USE_SHPREAMBLE
2049 }
2050
2051 /*
2052 * Transmit a beacon frame at SWBA. Dynamic updates to the
2053 * frame contents are done as needed and the slot time is
2054 * also adjusted based on current state.
2055 */
2056 static void
2057 ath_beacon_proc(void *arg, int pending)
2058 {
2059 struct ath_softc *sc = arg;
2060 struct ath_buf *bf = STAILQ_FIRST(&sc->sc_bbuf);
2061 struct ieee80211_node *ni = bf->bf_node;
2062 struct ieee80211com *ic = ni->ni_ic;
2063 struct ath_hal *ah = sc->sc_ah;
2064 struct mbuf *m;
2065 int ncabq, error, otherant;
2066
2067 DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: pending %u\n",
2068 __func__, pending);
2069
2070 if (ic->ic_opmode == IEEE80211_M_STA ||
2071 ic->ic_opmode == IEEE80211_M_MONITOR ||
2072 bf == NULL || bf->bf_m == NULL) {
2073 DPRINTF(sc, ATH_DEBUG_ANY, "%s: ic_flags=%x bf=%p bf_m=%p\n",
2074 __func__, ic->ic_flags, bf, bf ? bf->bf_m : NULL);
2075 return;
2076 }
2077 /*
2078 * Check if the previous beacon has gone out. If
2079 * not don't don't try to post another, skip this
2080 * period and wait for the next. Missed beacons
2081 * indicate a problem and should not occur. If we
2082 * miss too many consecutive beacons reset the device.
2083 */
2084 if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
2085 sc->sc_bmisscount++;
2086 DPRINTF(sc, ATH_DEBUG_BEACON_PROC,
2087 "%s: missed %u consecutive beacons\n",
2088 __func__, sc->sc_bmisscount);
2089 if (sc->sc_bmisscount > 3) /* NB: 3 is a guess */
2090 TASK_RUN_OR_ENQUEUE(&sc->sc_bstucktask);
2091 return;
2092 }
2093 if (sc->sc_bmisscount != 0) {
2094 DPRINTF(sc, ATH_DEBUG_BEACON,
2095 "%s: resume beacon xmit after %u misses\n",
2096 __func__, sc->sc_bmisscount);
2097 sc->sc_bmisscount = 0;
2098 }
2099
2100 /*
2101 * Update dynamic beacon contents. If this returns
2102 * non-zero then we need to remap the memory because
2103 * the beacon frame changed size (probably because
2104 * of the TIM bitmap).
2105 */
2106 m = bf->bf_m;
2107 ncabq = ath_hal_numtxpending(ah, sc->sc_cabq->axq_qnum);
2108 if (ieee80211_beacon_update(ic, bf->bf_node, &sc->sc_boff, m, ncabq)) {
2109 /* XXX too conservative? */
2110 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2111 error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m,
2112 BUS_DMA_NOWAIT);
2113 if (error != 0) {
2114 if_printf(&sc->sc_if,
2115 "%s: bus_dmamap_load_mbuf failed, error %u\n",
2116 __func__, error);
2117 return;
2118 }
2119 }
2120
2121 /*
2122 * Handle slot time change when a non-ERP station joins/leaves
2123 * an 11g network. The 802.11 layer notifies us via callback,
2124 * we mark updateslot, then wait one beacon before effecting
2125 * the change. This gives associated stations at least one
2126 * beacon interval to note the state change.
2127 */
2128 /* XXX locking */
2129 if (sc->sc_updateslot == UPDATE)
2130 sc->sc_updateslot = COMMIT; /* commit next beacon */
2131 else if (sc->sc_updateslot == COMMIT)
2132 ath_setslottime(sc); /* commit change to h/w */
2133
2134 /*
2135 * Check recent per-antenna transmit statistics and flip
2136 * the default antenna if noticeably more frames went out
2137 * on the non-default antenna.
2138 * XXX assumes 2 anntenae
2139 */
2140 otherant = sc->sc_defant & 1 ? 2 : 1;
2141 if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2)
2142 ath_setdefantenna(sc, otherant);
2143 sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0;
2144
2145 /*
2146 * Construct tx descriptor.
2147 */
2148 ath_beacon_setup(sc, bf);
2149
2150 /*
2151 * Stop any current dma and put the new frame on the queue.
2152 * This should never fail since we check above that no frames
2153 * are still pending on the queue.
2154 */
2155 if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
2156 DPRINTF(sc, ATH_DEBUG_ANY,
2157 "%s: beacon queue %u did not stop?\n",
2158 __func__, sc->sc_bhalq);
2159 }
2160 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 0,
2161 bf->bf_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
2162
2163 /*
2164 * Enable the CAB queue before the beacon queue to
2165 * insure cab frames are triggered by this beacon.
2166 */
2167 if (sc->sc_boff.bo_tim[4] & 1) /* NB: only at DTIM */
2168 ath_hal_txstart(ah, sc->sc_cabq->axq_qnum);
2169 ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
2170 ath_hal_txstart(ah, sc->sc_bhalq);
2171 DPRINTF(sc, ATH_DEBUG_BEACON_PROC,
2172 "%s: TXDP[%u] = %p (%p)\n", __func__,
2173 sc->sc_bhalq, (caddr_t)bf->bf_daddr, bf->bf_desc);
2174
2175 sc->sc_stats.ast_be_xmit++;
2176 }
2177
2178 /*
2179 * Reset the hardware after detecting beacons have stopped.
2180 */
2181 static void
2182 ath_bstuck_proc(void *arg, int pending)
2183 {
2184 struct ath_softc *sc = arg;
2185 struct ifnet *ifp = &sc->sc_if;
2186
2187 if_printf(ifp, "stuck beacon; resetting (bmiss count %u)\n",
2188 sc->sc_bmisscount);
2189 ath_reset(ifp);
2190 }
2191
2192 /*
2193 * Reclaim beacon resources.
2194 */
2195 static void
2196 ath_beacon_free(struct ath_softc *sc)
2197 {
2198 struct ath_buf *bf;
2199
2200 STAILQ_FOREACH(bf, &sc->sc_bbuf, bf_list) {
2201 if (bf->bf_m != NULL) {
2202 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2203 m_freem(bf->bf_m);
2204 bf->bf_m = NULL;
2205 }
2206 if (bf->bf_node != NULL) {
2207 ieee80211_free_node(bf->bf_node);
2208 bf->bf_node = NULL;
2209 }
2210 }
2211 }
2212
2213 /*
2214 * Configure the beacon and sleep timers.
2215 *
2216 * When operating as an AP this resets the TSF and sets
2217 * up the hardware to notify us when we need to issue beacons.
2218 *
2219 * When operating in station mode this sets up the beacon
2220 * timers according to the timestamp of the last received
2221 * beacon and the current TSF, configures PCF and DTIM
2222 * handling, programs the sleep registers so the hardware
2223 * will wakeup in time to receive beacons, and configures
2224 * the beacon miss handling so we'll receive a BMISS
2225 * interrupt when we stop seeing beacons from the AP
2226 * we've associated with.
2227 */
2228 static void
2229 ath_beacon_config(struct ath_softc *sc)
2230 {
2231 #define TSF_TO_TU(_h,_l) (((_h) << 22) | ((_l) >> 10))
2232 struct ath_hal *ah = sc->sc_ah;
2233 struct ieee80211com *ic = &sc->sc_ic;
2234 struct ieee80211_node *ni = ic->ic_bss;
2235 u_int32_t nexttbtt, intval;
2236
2237 /* extract tstamp from last beacon and convert to TU */
2238 nexttbtt = TSF_TO_TU(LE_READ_4(ni->ni_tstamp.data + 4),
2239 LE_READ_4(ni->ni_tstamp.data));
2240 /* NB: the beacon interval is kept internally in TU's */
2241 intval = ni->ni_intval & HAL_BEACON_PERIOD;
2242 if (nexttbtt == 0) /* e.g. for ap mode */
2243 nexttbtt = intval;
2244 else if (intval) /* NB: can be 0 for monitor mode */
2245 nexttbtt = roundup(nexttbtt, intval);
2246 DPRINTF(sc, ATH_DEBUG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
2247 __func__, nexttbtt, intval, ni->ni_intval);
2248 if (ic->ic_opmode == IEEE80211_M_STA) {
2249 HAL_BEACON_STATE bs;
2250 u_int64_t tsf;
2251 u_int32_t tsftu;
2252 int dtimperiod, dtimcount;
2253 int cfpperiod, cfpcount;
2254
2255 /*
2256 * Setup dtim and cfp parameters according to
2257 * last beacon we received (which may be none).
2258 */
2259 dtimperiod = ni->ni_dtim_period;
2260 if (dtimperiod <= 0) /* NB: 0 if not known */
2261 dtimperiod = 1;
2262 dtimcount = ni->ni_dtim_count;
2263 if (dtimcount >= dtimperiod) /* NB: sanity check */
2264 dtimcount = 0; /* XXX? */
2265 cfpperiod = 1; /* NB: no PCF support yet */
2266 cfpcount = 0;
2267 #define FUDGE 2
2268 /*
2269 * Pull nexttbtt forward to reflect the current
2270 * TSF and calculate dtim+cfp state for the result.
2271 */
2272 tsf = ath_hal_gettsf64(ah);
2273 tsftu = TSF_TO_TU((u_int32_t)(tsf>>32), (u_int32_t)tsf) + FUDGE;
2274 do {
2275 nexttbtt += intval;
2276 if (--dtimcount < 0) {
2277 dtimcount = dtimperiod - 1;
2278 if (--cfpcount < 0)
2279 cfpcount = cfpperiod - 1;
2280 }
2281 } while (nexttbtt < tsftu);
2282 #undef FUDGE
2283 memset(&bs, 0, sizeof(bs));
2284 bs.bs_intval = intval;
2285 bs.bs_nexttbtt = nexttbtt;
2286 bs.bs_dtimperiod = dtimperiod*intval;
2287 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
2288 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
2289 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
2290 bs.bs_cfpmaxduration = 0;
2291 #if 0
2292 /*
2293 * The 802.11 layer records the offset to the DTIM
2294 * bitmap while receiving beacons; use it here to
2295 * enable h/w detection of our AID being marked in
2296 * the bitmap vector (to indicate frames for us are
2297 * pending at the AP).
2298 * XXX do DTIM handling in s/w to WAR old h/w bugs
2299 * XXX enable based on h/w rev for newer chips
2300 */
2301 bs.bs_timoffset = ni->ni_timoff;
2302 #endif
2303 /*
2304 * Calculate the number of consecutive beacons to miss
2305 * before taking a BMISS interrupt. The configuration
2306 * is specified in ms, so we need to convert that to
2307 * TU's and then calculate based on the beacon interval.
2308 * Note that we clamp the result to at most 10 beacons.
2309 */
2310 bs.bs_bmissthreshold = howmany(ic->ic_bmisstimeout, intval);
2311 if (bs.bs_bmissthreshold > 10)
2312 bs.bs_bmissthreshold = 10;
2313 else if (bs.bs_bmissthreshold <= 0)
2314 bs.bs_bmissthreshold = 1;
2315
2316 /*
2317 * Calculate sleep duration. The configuration is
2318 * given in ms. We insure a multiple of the beacon
2319 * period is used. Also, if the sleep duration is
2320 * greater than the DTIM period then it makes senses
2321 * to make it a multiple of that.
2322 *
2323 * XXX fixed at 100ms
2324 */
2325 bs.bs_sleepduration =
2326 roundup(IEEE80211_MS_TO_TU(100), bs.bs_intval);
2327 if (bs.bs_sleepduration > bs.bs_dtimperiod)
2328 bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
2329
2330 DPRINTF(sc, ATH_DEBUG_BEACON,
2331 "%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"
2332 , __func__
2333 , tsf, tsftu
2334 , bs.bs_intval
2335 , bs.bs_nexttbtt
2336 , bs.bs_dtimperiod
2337 , bs.bs_nextdtim
2338 , bs.bs_bmissthreshold
2339 , bs.bs_sleepduration
2340 , bs.bs_cfpperiod
2341 , bs.bs_cfpmaxduration
2342 , bs.bs_cfpnext
2343 , bs.bs_timoffset
2344 );
2345 ath_hal_intrset(ah, 0);
2346 ath_hal_beacontimers(ah, &bs);
2347 sc->sc_imask |= HAL_INT_BMISS;
2348 ath_hal_intrset(ah, sc->sc_imask);
2349 } else {
2350 ath_hal_intrset(ah, 0);
2351 if (nexttbtt == intval)
2352 intval |= HAL_BEACON_RESET_TSF;
2353 if (ic->ic_opmode == IEEE80211_M_IBSS) {
2354 /*
2355 * In IBSS mode enable the beacon timers but only
2356 * enable SWBA interrupts if we need to manually
2357 * prepare beacon frames. Otherwise we use a
2358 * self-linked tx descriptor and let the hardware
2359 * deal with things.
2360 */
2361 intval |= HAL_BEACON_ENA;
2362 if (!sc->sc_hasveol)
2363 sc->sc_imask |= HAL_INT_SWBA;
2364 ath_beaconq_config(sc);
2365 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
2366 /*
2367 * In AP mode we enable the beacon timers and
2368 * SWBA interrupts to prepare beacon frames.
2369 */
2370 intval |= HAL_BEACON_ENA;
2371 sc->sc_imask |= HAL_INT_SWBA; /* beacon prepare */
2372 ath_beaconq_config(sc);
2373 }
2374 ath_hal_beaconinit(ah, nexttbtt, intval);
2375 sc->sc_bmisscount = 0;
2376 ath_hal_intrset(ah, sc->sc_imask);
2377 /*
2378 * When using a self-linked beacon descriptor in
2379 * ibss mode load it once here.
2380 */
2381 if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol)
2382 ath_beacon_proc(sc, 0);
2383 }
2384 #undef TSF_TO_TU
2385 }
2386
2387 static int
2388 ath_descdma_setup(struct ath_softc *sc,
2389 struct ath_descdma *dd, ath_bufhead *head,
2390 const char *name, int nbuf, int ndesc)
2391 {
2392 #define DS2PHYS(_dd, _ds) \
2393 ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
2394 struct ifnet *ifp = &sc->sc_if;
2395 struct ath_desc *ds;
2396 struct ath_buf *bf;
2397 int i, bsize, error;
2398
2399 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA: %u buffers %u desc/buf\n",
2400 __func__, name, nbuf, ndesc);
2401
2402 dd->dd_name = name;
2403 dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
2404
2405 /*
2406 * Setup DMA descriptor area.
2407 */
2408 dd->dd_dmat = sc->sc_dmat;
2409
2410 error = bus_dmamem_alloc(dd->dd_dmat, dd->dd_desc_len, PAGE_SIZE,
2411 0, &dd->dd_dseg, 1, &dd->dd_dnseg, 0);
2412
2413 if (error != 0) {
2414 if_printf(ifp, "unable to alloc memory for %u %s descriptors, "
2415 "error %u\n", nbuf * ndesc, dd->dd_name, error);
2416 goto fail0;
2417 }
2418
2419 error = bus_dmamem_map(dd->dd_dmat, &dd->dd_dseg, dd->dd_dnseg,
2420 dd->dd_desc_len, (caddr_t *)&dd->dd_desc, BUS_DMA_COHERENT);
2421 if (error != 0) {
2422 if_printf(ifp, "unable to map %u %s descriptors, error = %u\n",
2423 nbuf * ndesc, dd->dd_name, error);
2424 goto fail1;
2425 }
2426
2427 /* allocate descriptors */
2428 error = bus_dmamap_create(dd->dd_dmat, dd->dd_desc_len, 1,
2429 dd->dd_desc_len, 0, BUS_DMA_NOWAIT, &dd->dd_dmamap);
2430 if (error != 0) {
2431 if_printf(ifp, "unable to create dmamap for %s descriptors, "
2432 "error %u\n", dd->dd_name, error);
2433 goto fail2;
2434 }
2435
2436 error = bus_dmamap_load(dd->dd_dmat, dd->dd_dmamap, dd->dd_desc,
2437 dd->dd_desc_len, NULL, BUS_DMA_NOWAIT);
2438 if (error != 0) {
2439 if_printf(ifp, "unable to map %s descriptors, error %u\n",
2440 dd->dd_name, error);
2441 goto fail3;
2442 }
2443
2444 ds = dd->dd_desc;
2445 dd->dd_desc_paddr = dd->dd_dmamap->dm_segs[0].ds_addr;
2446 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %s DMA map: %p (%lu) -> %p (%lu)\n",
2447 __func__, dd->dd_name, ds, (u_long) dd->dd_desc_len,
2448 (caddr_t) dd->dd_desc_paddr, /*XXX*/ (u_long) dd->dd_desc_len);
2449
2450 /* allocate rx buffers */
2451 bsize = sizeof(struct ath_buf) * nbuf;
2452 bf = malloc(bsize, M_ATHDEV, M_NOWAIT | M_ZERO);
2453 if (bf == NULL) {
2454 if_printf(ifp, "malloc of %s buffers failed, size %u\n",
2455 dd->dd_name, bsize);
2456 goto fail4;
2457 }
2458 dd->dd_bufptr = bf;
2459
2460 STAILQ_INIT(head);
2461 for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
2462 bf->bf_desc = ds;
2463 bf->bf_daddr = DS2PHYS(dd, ds);
2464 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, ndesc,
2465 MCLBYTES, 0, BUS_DMA_NOWAIT, &bf->bf_dmamap);
2466 if (error != 0) {
2467 if_printf(ifp, "unable to create dmamap for %s "
2468 "buffer %u, error %u\n", dd->dd_name, i, error);
2469 ath_descdma_cleanup(sc, dd, head);
2470 return error;
2471 }
2472 STAILQ_INSERT_TAIL(head, bf, bf_list);
2473 }
2474 return 0;
2475 fail4:
2476 bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
2477 fail3:
2478 bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
2479 fail2:
2480 bus_dmamem_unmap(dd->dd_dmat, (caddr_t)dd->dd_desc, dd->dd_desc_len);
2481 fail1:
2482 bus_dmamem_free(dd->dd_dmat, &dd->dd_dseg, dd->dd_dnseg);
2483 fail0:
2484 memset(dd, 0, sizeof(*dd));
2485 return error;
2486 #undef DS2PHYS
2487 }
2488
2489 static void
2490 ath_descdma_cleanup(struct ath_softc *sc,
2491 struct ath_descdma *dd, ath_bufhead *head)
2492 {
2493 struct ath_buf *bf;
2494 struct ieee80211_node *ni;
2495
2496 bus_dmamap_unload(dd->dd_dmat, dd->dd_dmamap);
2497 bus_dmamap_destroy(dd->dd_dmat, dd->dd_dmamap);
2498 bus_dmamem_unmap(dd->dd_dmat, (caddr_t)dd->dd_desc, dd->dd_desc_len);
2499 bus_dmamem_free(dd->dd_dmat, &dd->dd_dseg, dd->dd_dnseg);
2500
2501 STAILQ_FOREACH(bf, head, bf_list) {
2502 if (bf->bf_m) {
2503 m_freem(bf->bf_m);
2504 bf->bf_m = NULL;
2505 }
2506 if (bf->bf_dmamap != NULL) {
2507 bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
2508 bf->bf_dmamap = NULL;
2509 }
2510 ni = bf->bf_node;
2511 bf->bf_node = NULL;
2512 if (ni != NULL) {
2513 /*
2514 * Reclaim node reference.
2515 */
2516 ieee80211_free_node(ni);
2517 }
2518 }
2519
2520 STAILQ_INIT(head);
2521 free(dd->dd_bufptr, M_ATHDEV);
2522 memset(dd, 0, sizeof(*dd));
2523 }
2524
2525 static int
2526 ath_desc_alloc(struct ath_softc *sc)
2527 {
2528 int error;
2529
2530 error = ath_descdma_setup(sc, &sc->sc_rxdma, &sc->sc_rxbuf,
2531 "rx", ATH_RXBUF, 1);
2532 if (error != 0)
2533 return error;
2534
2535 error = ath_descdma_setup(sc, &sc->sc_txdma, &sc->sc_txbuf,
2536 "tx", ATH_TXBUF, ATH_TXDESC);
2537 if (error != 0) {
2538 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
2539 return error;
2540 }
2541
2542 error = ath_descdma_setup(sc, &sc->sc_bdma, &sc->sc_bbuf,
2543 "beacon", 1, 1);
2544 if (error != 0) {
2545 ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
2546 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
2547 return error;
2548 }
2549 return 0;
2550 }
2551
2552 static void
2553 ath_desc_free(struct ath_softc *sc)
2554 {
2555
2556 if (sc->sc_bdma.dd_desc_len != 0)
2557 ath_descdma_cleanup(sc, &sc->sc_bdma, &sc->sc_bbuf);
2558 if (sc->sc_txdma.dd_desc_len != 0)
2559 ath_descdma_cleanup(sc, &sc->sc_txdma, &sc->sc_txbuf);
2560 if (sc->sc_rxdma.dd_desc_len != 0)
2561 ath_descdma_cleanup(sc, &sc->sc_rxdma, &sc->sc_rxbuf);
2562 }
2563
2564 static struct ieee80211_node *
2565 ath_node_alloc(struct ieee80211_node_table *nt)
2566 {
2567 struct ieee80211com *ic = nt->nt_ic;
2568 struct ath_softc *sc = ic->ic_ifp->if_softc;
2569 const size_t space = sizeof(struct ath_node) + sc->sc_rc->arc_space;
2570 struct ath_node *an;
2571
2572 an = malloc(space, M_80211_NODE, M_NOWAIT|M_ZERO);
2573 if (an == NULL) {
2574 /* XXX stat+msg */
2575 return NULL;
2576 }
2577 an->an_avgrssi = ATH_RSSI_DUMMY_MARKER;
2578 an->an_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
2579 an->an_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
2580 an->an_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
2581 ath_rate_node_init(sc, an);
2582
2583 DPRINTF(sc, ATH_DEBUG_NODE, "%s: an %p\n", __func__, an);
2584 return &an->an_node;
2585 }
2586
2587 static void
2588 ath_node_free(struct ieee80211_node *ni)
2589 {
2590 struct ieee80211com *ic = ni->ni_ic;
2591 struct ath_softc *sc = ic->ic_ifp->if_softc;
2592
2593 DPRINTF(sc, ATH_DEBUG_NODE, "%s: ni %p\n", __func__, ni);
2594
2595 ath_rate_node_cleanup(sc, ATH_NODE(ni));
2596 sc->sc_node_free(ni);
2597 }
2598
2599 static u_int8_t
2600 ath_node_getrssi(const struct ieee80211_node *ni)
2601 {
2602 #define HAL_EP_RND(x, mul) \
2603 ((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))
2604 u_int32_t avgrssi = ATH_NODE_CONST(ni)->an_avgrssi;
2605 int32_t rssi;
2606
2607 /*
2608 * When only one frame is received there will be no state in
2609 * avgrssi so fallback on the value recorded by the 802.11 layer.
2610 */
2611 if (avgrssi != ATH_RSSI_DUMMY_MARKER)
2612 rssi = HAL_EP_RND(avgrssi, HAL_RSSI_EP_MULTIPLIER);
2613 else
2614 rssi = ni->ni_rssi;
2615 /* NB: theoretically we shouldn't need this, but be paranoid */
2616 return rssi < 0 ? 0 : rssi > 127 ? 127 : rssi;
2617 #undef HAL_EP_RND
2618 }
2619
2620 static int
2621 ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
2622 {
2623 struct ath_hal *ah = sc->sc_ah;
2624 int error;
2625 struct mbuf *m;
2626 struct ath_desc *ds;
2627
2628 m = bf->bf_m;
2629 if (m == NULL) {
2630 /*
2631 * NB: by assigning a page to the rx dma buffer we
2632 * implicitly satisfy the Atheros requirement that
2633 * this buffer be cache-line-aligned and sized to be
2634 * multiple of the cache line size. Not doing this
2635 * causes weird stuff to happen (for the 5210 at least).
2636 */
2637 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2638 if (m == NULL) {
2639 DPRINTF(sc, ATH_DEBUG_ANY,
2640 "%s: no mbuf/cluster\n", __func__);
2641 sc->sc_stats.ast_rx_nombuf++;
2642 return ENOMEM;
2643 }
2644 bf->bf_m = m;
2645 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
2646
2647 error = bus_dmamap_load_mbuf(sc->sc_dmat,
2648 bf->bf_dmamap, m,
2649 BUS_DMA_NOWAIT);
2650 if (error != 0) {
2651 DPRINTF(sc, ATH_DEBUG_ANY,
2652 "%s: bus_dmamap_load_mbuf failed; error %d\n",
2653 __func__, error);
2654 sc->sc_stats.ast_rx_busdma++;
2655 return error;
2656 }
2657 KASSERT(bf->bf_nseg == 1,
2658 ("multi-segment packet; nseg %u", bf->bf_nseg));
2659 }
2660 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 0,
2661 bf->bf_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
2662
2663 /*
2664 * Setup descriptors. For receive we always terminate
2665 * the descriptor list with a self-linked entry so we'll
2666 * not get overrun under high load (as can happen with a
2667 * 5212 when ANI processing enables PHY error frames).
2668 *
2669 * To insure the last descriptor is self-linked we create
2670 * each descriptor as self-linked and add it to the end. As
2671 * each additional descriptor is added the previous self-linked
2672 * entry is ``fixed'' naturally. This should be safe even
2673 * if DMA is happening. When processing RX interrupts we
2674 * never remove/process the last, self-linked, entry on the
2675 * descriptor list. This insures the hardware always has
2676 * someplace to write a new frame.
2677 */
2678 ds = bf->bf_desc;
2679 ds->ds_link = bf->bf_daddr; /* link to self */
2680 ds->ds_data = bf->bf_segs[0].ds_addr;
2681 ath_hal_setuprxdesc(ah, ds
2682 , m->m_len /* buffer size */
2683 , 0
2684 );
2685
2686 if (sc->sc_rxlink != NULL)
2687 *sc->sc_rxlink = bf->bf_daddr;
2688 sc->sc_rxlink = &ds->ds_link;
2689 return 0;
2690 }
2691
2692 /*
2693 * Extend 15-bit time stamp from rx descriptor to
2694 * a full 64-bit TSF using the current h/w TSF.
2695 */
2696 static __inline u_int64_t
2697 ath_extend_tsf(struct ath_hal *ah, u_int32_t rstamp)
2698 {
2699 u_int64_t tsf;
2700
2701 tsf = ath_hal_gettsf64(ah);
2702 if ((tsf & 0x7fff) < rstamp)
2703 tsf -= 0x8000;
2704 return ((tsf &~ 0x7fff) | rstamp);
2705 }
2706
2707 /*
2708 * Intercept management frames to collect beacon rssi data
2709 * and to do ibss merges.
2710 */
2711 static void
2712 ath_recv_mgmt(struct ieee80211com *ic, struct mbuf *m,
2713 struct ieee80211_node *ni,
2714 int subtype, int rssi, u_int32_t rstamp)
2715 {
2716 struct ath_softc *sc = ic->ic_ifp->if_softc;
2717
2718 /*
2719 * Call up first so subsequent work can use information
2720 * potentially stored in the node (e.g. for ibss merge).
2721 */
2722 sc->sc_recv_mgmt(ic, m, ni, subtype, rssi, rstamp);
2723 switch (subtype) {
2724 case IEEE80211_FC0_SUBTYPE_BEACON:
2725 /* update rssi statistics for use by the hal */
2726 ATH_RSSI_LPF(ATH_NODE(ni)->an_halstats.ns_avgbrssi, rssi);
2727 /* fall thru... */
2728 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
2729 if (ic->ic_opmode == IEEE80211_M_IBSS &&
2730 ic->ic_state == IEEE80211_S_RUN) {
2731 u_int64_t tsf = ath_extend_tsf(sc->sc_ah, rstamp);
2732
2733 /*
2734 * Handle ibss merge as needed; check the tsf on the
2735 * frame before attempting the merge. The 802.11 spec
2736 * says the station should change it's bssid to match
2737 * the oldest station with the same ssid, where oldest
2738 * is determined by the tsf. Note that hardware
2739 * reconfiguration happens through callback to
2740 * ath_newstate as the state machine will go from
2741 * RUN -> RUN when this happens.
2742 */
2743 if (le64toh(ni->ni_tstamp.tsf) >= tsf) {
2744 DPRINTF(sc, ATH_DEBUG_STATE,
2745 "ibss merge, rstamp %u tsf %ju "
2746 "tstamp %ju\n", rstamp, (uintmax_t)tsf,
2747 (uintmax_t)ni->ni_tstamp.tsf);
2748 (void) ieee80211_ibss_merge(ic, ni);
2749 }
2750 }
2751 break;
2752 }
2753 }
2754
2755 /*
2756 * Set the default antenna.
2757 */
2758 static void
2759 ath_setdefantenna(struct ath_softc *sc, u_int antenna)
2760 {
2761 struct ath_hal *ah = sc->sc_ah;
2762
2763 /* XXX block beacon interrupts */
2764 ath_hal_setdefantenna(ah, antenna);
2765 if (sc->sc_defant != antenna)
2766 sc->sc_stats.ast_ant_defswitch++;
2767 sc->sc_defant = antenna;
2768 sc->sc_rxotherant = 0;
2769 }
2770
2771 static void
2772 ath_rx_proc(void *arg, int npending)
2773 {
2774 #define PA2DESC(_sc, _pa) \
2775 ((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
2776 ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
2777 struct ath_softc *sc = arg;
2778 struct ath_buf *bf;
2779 struct ieee80211com *ic = &sc->sc_ic;
2780 struct ifnet *ifp = &sc->sc_if;
2781 struct ath_hal *ah = sc->sc_ah;
2782 struct ath_desc *ds;
2783 struct mbuf *m;
2784 struct ieee80211_node *ni;
2785 struct ath_node *an;
2786 int len, type;
2787 u_int phyerr;
2788 HAL_STATUS status;
2789
2790 NET_LOCK_GIANT(); /* XXX */
2791
2792 DPRINTF(sc, ATH_DEBUG_RX_PROC, "%s: pending %u\n", __func__, npending);
2793 do {
2794 bf = STAILQ_FIRST(&sc->sc_rxbuf);
2795 if (bf == NULL) { /* NB: shouldn't happen */
2796 if_printf(ifp, "%s: no buffer!\n", __func__);
2797 break;
2798 }
2799 ds = bf->bf_desc;
2800 if (ds->ds_link == bf->bf_daddr) {
2801 /* NB: never process the self-linked entry at the end */
2802 break;
2803 }
2804 m = bf->bf_m;
2805 if (m == NULL) { /* NB: shouldn't happen */
2806 if_printf(ifp, "%s: no mbuf!\n", __func__);
2807 continue;
2808 }
2809 /* XXX sync descriptor memory */
2810 /*
2811 * Must provide the virtual address of the current
2812 * descriptor, the physical address, and the virtual
2813 * address of the next descriptor in the h/w chain.
2814 * This allows the HAL to look ahead to see if the
2815 * hardware is done with a descriptor by checking the
2816 * done bit in the following descriptor and the address
2817 * of the current descriptor the DMA engine is working
2818 * on. All this is necessary because of our use of
2819 * a self-linked list to avoid rx overruns.
2820 */
2821 status = ath_hal_rxprocdesc(ah, ds,
2822 bf->bf_daddr, PA2DESC(sc, ds->ds_link));
2823 #ifdef AR_DEBUG
2824 if (sc->sc_debug & ATH_DEBUG_RECV_DESC)
2825 ath_printrxbuf(bf, status == HAL_OK);
2826 #endif
2827 if (status == HAL_EINPROGRESS)
2828 break;
2829 STAILQ_REMOVE_HEAD(&sc->sc_rxbuf, bf_list);
2830 if (ds->ds_rxstat.rs_more) {
2831 /*
2832 * Frame spans multiple descriptors; this
2833 * cannot happen yet as we don't support
2834 * jumbograms. If not in monitor mode,
2835 * discard the frame.
2836 */
2837 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2838 sc->sc_stats.ast_rx_toobig++;
2839 goto rx_next;
2840 }
2841 /* fall thru for monitor mode handling... */
2842 } else if (ds->ds_rxstat.rs_status != 0) {
2843 if (ds->ds_rxstat.rs_status & HAL_RXERR_CRC)
2844 sc->sc_stats.ast_rx_crcerr++;
2845 if (ds->ds_rxstat.rs_status & HAL_RXERR_FIFO)
2846 sc->sc_stats.ast_rx_fifoerr++;
2847 if (ds->ds_rxstat.rs_status & HAL_RXERR_PHY) {
2848 sc->sc_stats.ast_rx_phyerr++;
2849 phyerr = ds->ds_rxstat.rs_phyerr & 0x1f;
2850 sc->sc_stats.ast_rx_phy[phyerr]++;
2851 goto rx_next;
2852 }
2853 if (ds->ds_rxstat.rs_status & HAL_RXERR_DECRYPT) {
2854 /*
2855 * Decrypt error. If the error occurred
2856 * because there was no hardware key, then
2857 * let the frame through so the upper layers
2858 * can process it. This is necessary for 5210
2859 * parts which have no way to setup a ``clear''
2860 * key cache entry.
2861 *
2862 * XXX do key cache faulting
2863 */
2864 if (ds->ds_rxstat.rs_keyix == HAL_RXKEYIX_INVALID)
2865 goto rx_accept;
2866 sc->sc_stats.ast_rx_badcrypt++;
2867 }
2868 if (ds->ds_rxstat.rs_status & HAL_RXERR_MIC) {
2869 sc->sc_stats.ast_rx_badmic++;
2870 /*
2871 * Do minimal work required to hand off
2872 * the 802.11 header for notifcation.
2873 */
2874 /* XXX frag's and qos frames */
2875 len = ds->ds_rxstat.rs_datalen;
2876 if (len >= sizeof (struct ieee80211_frame)) {
2877 bus_dmamap_sync(sc->sc_dmat,
2878 bf->bf_dmamap,
2879 0, bf->bf_dmamap->dm_mapsize,
2880 BUS_DMASYNC_POSTREAD);
2881 ieee80211_notify_michael_failure(ic,
2882 mtod(m, struct ieee80211_frame *),
2883 sc->sc_splitmic ?
2884 ds->ds_rxstat.rs_keyix-32 :
2885 ds->ds_rxstat.rs_keyix
2886 );
2887 }
2888 }
2889 ifp->if_ierrors++;
2890 /*
2891 * Reject error frames, we normally don't want
2892 * to see them in monitor mode (in monitor mode
2893 * allow through packets that have crypto problems).
2894 */
2895 if ((ds->ds_rxstat.rs_status &~
2896 (HAL_RXERR_DECRYPT|HAL_RXERR_MIC)) ||
2897 sc->sc_ic.ic_opmode != IEEE80211_M_MONITOR)
2898 goto rx_next;
2899 }
2900 rx_accept:
2901 /*
2902 * Sync and unmap the frame. At this point we're
2903 * committed to passing the mbuf somewhere so clear
2904 * bf_m; this means a new sk_buff must be allocated
2905 * when the rx descriptor is setup again to receive
2906 * another frame.
2907 */
2908 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap,
2909 0, bf->bf_dmamap->dm_mapsize,
2910 BUS_DMASYNC_POSTREAD);
2911 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2912 bf->bf_m = NULL;
2913
2914 m->m_pkthdr.rcvif = ifp;
2915 len = ds->ds_rxstat.rs_datalen;
2916 m->m_pkthdr.len = m->m_len = len;
2917
2918 sc->sc_stats.ast_ant_rx[ds->ds_rxstat.rs_antenna]++;
2919
2920 #if NBPFILTER > 0
2921 if (sc->sc_drvbpf) {
2922 u_int8_t rix;
2923
2924 /*
2925 * Discard anything shorter than an ack or cts.
2926 */
2927 if (len < IEEE80211_ACK_LEN) {
2928 DPRINTF(sc, ATH_DEBUG_RECV,
2929 "%s: runt packet %d\n",
2930 __func__, len);
2931 sc->sc_stats.ast_rx_tooshort++;
2932 m_freem(m);
2933 goto rx_next;
2934 }
2935 rix = ds->ds_rxstat.rs_rate;
2936 sc->sc_rx_th.wr_flags = sc->sc_hwmap[rix].rxflags;
2937 sc->sc_rx_th.wr_rate = sc->sc_hwmap[rix].ieeerate;
2938 sc->sc_rx_th.wr_antsignal = ds->ds_rxstat.rs_rssi;
2939 sc->sc_rx_th.wr_antenna = ds->ds_rxstat.rs_antenna;
2940 /* XXX TSF */
2941
2942 bpf_mtap2(sc->sc_drvbpf,
2943 &sc->sc_rx_th, sc->sc_rx_th_len, m);
2944 }
2945 #endif
2946
2947 /*
2948 * From this point on we assume the frame is at least
2949 * as large as ieee80211_frame_min; verify that.
2950 */
2951 if (len < IEEE80211_MIN_LEN) {
2952 DPRINTF(sc, ATH_DEBUG_RECV, "%s: short packet %d\n",
2953 __func__, len);
2954 sc->sc_stats.ast_rx_tooshort++;
2955 m_freem(m);
2956 goto rx_next;
2957 }
2958
2959 if (IFF_DUMPPKTS(sc, ATH_DEBUG_RECV)) {
2960 ieee80211_dump_pkt(mtod(m, caddr_t), len,
2961 sc->sc_hwmap[ds->ds_rxstat.rs_rate].ieeerate,
2962 ds->ds_rxstat.rs_rssi);
2963 }
2964
2965 m_adj(m, -IEEE80211_CRC_LEN);
2966
2967 /*
2968 * Locate the node for sender, track state, and then
2969 * pass the (referenced) node up to the 802.11 layer
2970 * for its use. If the sender is unknown spam the
2971 * frame; it'll be dropped where it's not wanted.
2972 */
2973 if (ds->ds_rxstat.rs_keyix != HAL_RXKEYIX_INVALID &&
2974 (ni = sc->sc_keyixmap[ds->ds_rxstat.rs_keyix]) != NULL) {
2975 /*
2976 * Fast path: node is present in the key map;
2977 * grab a reference for processing the frame.
2978 */
2979 an = ATH_NODE(ieee80211_ref_node(ni));
2980 ATH_RSSI_LPF(an->an_avgrssi, ds->ds_rxstat.rs_rssi);
2981 type = ieee80211_input(ic, m, ni,
2982 ds->ds_rxstat.rs_rssi, ds->ds_rxstat.rs_tstamp);
2983 } else {
2984 /*
2985 * Locate the node for sender, track state, and then
2986 * pass the (referenced) node up to the 802.11 layer
2987 * for its use.
2988 */
2989 ni = ieee80211_find_rxnode(ic,
2990 mtod(m, const struct ieee80211_frame_min *));
2991 /*
2992 * Track rx rssi and do any rx antenna management.
2993 */
2994 an = ATH_NODE(ni);
2995 ATH_RSSI_LPF(an->an_avgrssi, ds->ds_rxstat.rs_rssi);
2996 /*
2997 * Send frame up for processing.
2998 */
2999 type = ieee80211_input(ic, m, ni,
3000 ds->ds_rxstat.rs_rssi, ds->ds_rxstat.rs_tstamp);
3001 if (ni != ic->ic_bss) {
3002 u_int16_t keyix;
3003 /*
3004 * If the station has a key cache slot assigned
3005 * update the key->node mapping table.
3006 */
3007 keyix = ni->ni_ucastkey.wk_keyix;
3008 if (keyix != IEEE80211_KEYIX_NONE &&
3009 sc->sc_keyixmap[keyix] == NULL)
3010 sc->sc_keyixmap[keyix] =
3011 ieee80211_ref_node(ni);
3012 }
3013 }
3014 ieee80211_free_node(ni);
3015 if (sc->sc_diversity) {
3016 /*
3017 * When using fast diversity, change the default rx
3018 * antenna if diversity chooses the other antenna 3
3019 * times in a row.
3020 */
3021 if (sc->sc_defant != ds->ds_rxstat.rs_antenna) {
3022 if (++sc->sc_rxotherant >= 3)
3023 ath_setdefantenna(sc,
3024 ds->ds_rxstat.rs_antenna);
3025 } else
3026 sc->sc_rxotherant = 0;
3027 }
3028 if (sc->sc_softled) {
3029 /*
3030 * Blink for any data frame. Otherwise do a
3031 * heartbeat-style blink when idle. The latter
3032 * is mainly for station mode where we depend on
3033 * periodic beacon frames to trigger the poll event.
3034 */
3035 if (type == IEEE80211_FC0_TYPE_DATA) {
3036 sc->sc_rxrate = ds->ds_rxstat.rs_rate;
3037 ath_led_event(sc, ATH_LED_RX);
3038 } else if (ticks - sc->sc_ledevent >= sc->sc_ledidle)
3039 ath_led_event(sc, ATH_LED_POLL);
3040 }
3041 rx_next:
3042 STAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
3043 } while (ath_rxbuf_init(sc, bf) == 0);
3044
3045 /* rx signal state monitoring */
3046 ath_hal_rxmonitor(ah, &ATH_NODE(ic->ic_bss)->an_halstats);
3047
3048 #ifdef __NetBSD__
3049 /* XXX Why isn't this necessary in FreeBSD? */
3050 if ((ifp->if_flags & IFF_OACTIVE) == 0 && !IFQ_IS_EMPTY(&ifp->if_snd))
3051 ath_start(ifp);
3052 #endif /* __NetBSD__ */
3053
3054 NET_UNLOCK_GIANT(); /* XXX */
3055 #undef PA2DESC
3056 }
3057
3058 /*
3059 * Setup a h/w transmit queue.
3060 */
3061 static struct ath_txq *
3062 ath_txq_setup(struct ath_softc *sc, int qtype, int subtype)
3063 {
3064 #define N(a) (sizeof(a)/sizeof(a[0]))
3065 struct ath_hal *ah = sc->sc_ah;
3066 HAL_TXQ_INFO qi;
3067 int qnum;
3068
3069 memset(&qi, 0, sizeof(qi));
3070 qi.tqi_subtype = subtype;
3071 qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
3072 qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
3073 qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
3074 /*
3075 * Enable interrupts only for EOL and DESC conditions.
3076 * We mark tx descriptors to receive a DESC interrupt
3077 * when a tx queue gets deep; otherwise waiting for the
3078 * EOL to reap descriptors. Note that this is done to
3079 * reduce interrupt load and this only defers reaping
3080 * descriptors, never transmitting frames. Aside from
3081 * reducing interrupts this also permits more concurrency.
3082 * The only potential downside is if the tx queue backs
3083 * up in which case the top half of the kernel may backup
3084 * due to a lack of tx descriptors.
3085 */
3086 qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE | TXQ_FLAG_TXDESCINT_ENABLE;
3087 qnum = ath_hal_setuptxqueue(ah, qtype, &qi);
3088 if (qnum == -1) {
3089 /*
3090 * NB: don't print a message, this happens
3091 * normally on parts with too few tx queues
3092 */
3093 return NULL;
3094 }
3095 if (qnum >= N(sc->sc_txq)) {
3096 device_printf(sc->sc_dev,
3097 "hal qnum %u out of range, max %zu!\n",
3098 qnum, N(sc->sc_txq));
3099 ath_hal_releasetxqueue(ah, qnum);
3100 return NULL;
3101 }
3102 if (!ATH_TXQ_SETUP(sc, qnum)) {
3103 struct ath_txq *txq = &sc->sc_txq[qnum];
3104
3105 txq->axq_qnum = qnum;
3106 txq->axq_depth = 0;
3107 txq->axq_intrcnt = 0;
3108 txq->axq_link = NULL;
3109 STAILQ_INIT(&txq->axq_q);
3110 ATH_TXQ_LOCK_INIT(sc, txq);
3111 sc->sc_txqsetup |= 1<<qnum;
3112 }
3113 return &sc->sc_txq[qnum];
3114 #undef N
3115 }
3116
3117 /*
3118 * Setup a hardware data transmit queue for the specified
3119 * access control. The hal may not support all requested
3120 * queues in which case it will return a reference to a
3121 * previously setup queue. We record the mapping from ac's
3122 * to h/w queues for use by ath_tx_start and also track
3123 * the set of h/w queues being used to optimize work in the
3124 * transmit interrupt handler and related routines.
3125 */
3126 static int
3127 ath_tx_setup(struct ath_softc *sc, int ac, int haltype)
3128 {
3129 #define N(a) (sizeof(a)/sizeof(a[0]))
3130 struct ath_txq *txq;
3131
3132 if (ac >= N(sc->sc_ac2q)) {
3133 device_printf(sc->sc_dev, "AC %u out of range, max %zu!\n",
3134 ac, N(sc->sc_ac2q));
3135 return 0;
3136 }
3137 txq = ath_txq_setup(sc, HAL_TX_QUEUE_DATA, haltype);
3138 if (txq != NULL) {
3139 sc->sc_ac2q[ac] = txq;
3140 return 1;
3141 } else
3142 return 0;
3143 #undef N
3144 }
3145
3146 /*
3147 * Update WME parameters for a transmit queue.
3148 */
3149 static int
3150 ath_txq_update(struct ath_softc *sc, int ac)
3151 {
3152 #define ATH_EXPONENT_TO_VALUE(v) ((1<<v)-1)
3153 #define ATH_TXOP_TO_US(v) (v<<5)
3154 struct ieee80211com *ic = &sc->sc_ic;
3155 struct ath_txq *txq = sc->sc_ac2q[ac];
3156 struct wmeParams *wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac];
3157 struct ath_hal *ah = sc->sc_ah;
3158 HAL_TXQ_INFO qi;
3159
3160 ath_hal_gettxqueueprops(ah, txq->axq_qnum, &qi);
3161 qi.tqi_aifs = wmep->wmep_aifsn;
3162 qi.tqi_cwmin = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
3163 qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
3164 qi.tqi_burstTime = ATH_TXOP_TO_US(wmep->wmep_txopLimit);
3165
3166 if (!ath_hal_settxqueueprops(ah, txq->axq_qnum, &qi)) {
3167 device_printf(sc->sc_dev, "unable to update hardware queue "
3168 "parameters for %s traffic!\n",
3169 ieee80211_wme_acnames[ac]);
3170 return 0;
3171 } else {
3172 ath_hal_resettxqueue(ah, txq->axq_qnum); /* push to h/w */
3173 return 1;
3174 }
3175 #undef ATH_TXOP_TO_US
3176 #undef ATH_EXPONENT_TO_VALUE
3177 }
3178
3179 /*
3180 * Callback from the 802.11 layer to update WME parameters.
3181 */
3182 static int
3183 ath_wme_update(struct ieee80211com *ic)
3184 {
3185 struct ath_softc *sc = ic->ic_ifp->if_softc;
3186
3187 return !ath_txq_update(sc, WME_AC_BE) ||
3188 !ath_txq_update(sc, WME_AC_BK) ||
3189 !ath_txq_update(sc, WME_AC_VI) ||
3190 !ath_txq_update(sc, WME_AC_VO) ? EIO : 0;
3191 }
3192
3193 /*
3194 * Reclaim resources for a setup queue.
3195 */
3196 static void
3197 ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq)
3198 {
3199
3200 ath_hal_releasetxqueue(sc->sc_ah, txq->axq_qnum);
3201 ATH_TXQ_LOCK_DESTROY(txq);
3202 sc->sc_txqsetup &= ~(1<<txq->axq_qnum);
3203 }
3204
3205 /*
3206 * Reclaim all tx queue resources.
3207 */
3208 static void
3209 ath_tx_cleanup(struct ath_softc *sc)
3210 {
3211 int i;
3212
3213 ATH_TXBUF_LOCK_DESTROY(sc);
3214 for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3215 if (ATH_TXQ_SETUP(sc, i))
3216 ath_tx_cleanupq(sc, &sc->sc_txq[i]);
3217 }
3218
3219 /*
3220 * Defragment an mbuf chain, returning at most maxfrags separate
3221 * mbufs+clusters. If this is not possible NULL is returned and
3222 * the original mbuf chain is left in it's present (potentially
3223 * modified) state. We use two techniques: collapsing consecutive
3224 * mbufs and replacing consecutive mbufs by a cluster.
3225 */
3226 static struct mbuf *
3227 ath_defrag(struct mbuf *m0, int how, int maxfrags)
3228 {
3229 struct mbuf *m, *n, *n2, **prev;
3230 u_int curfrags;
3231
3232 /*
3233 * Calculate the current number of frags.
3234 */
3235 curfrags = 0;
3236 for (m = m0; m != NULL; m = m->m_next)
3237 curfrags++;
3238 /*
3239 * First, try to collapse mbufs. Note that we always collapse
3240 * towards the front so we don't need to deal with moving the
3241 * pkthdr. This may be suboptimal if the first mbuf has much
3242 * less data than the following.
3243 */
3244 m = m0;
3245 again:
3246 for (;;) {
3247 n = m->m_next;
3248 if (n == NULL)
3249 break;
3250 if ((m->m_flags & M_RDONLY) == 0 &&
3251 n->m_len < M_TRAILINGSPACE(m)) {
3252 bcopy(mtod(n, void *), mtod(m, char *) + m->m_len,
3253 n->m_len);
3254 m->m_len += n->m_len;
3255 m->m_next = n->m_next;
3256 m_free(n);
3257 if (--curfrags <= maxfrags)
3258 return m0;
3259 } else
3260 m = n;
3261 }
3262 KASSERT(maxfrags > 1,
3263 ("maxfrags %u, but normal collapse failed", maxfrags));
3264 /*
3265 * Collapse consecutive mbufs to a cluster.
3266 */
3267 prev = &m0->m_next; /* NB: not the first mbuf */
3268 while ((n = *prev) != NULL) {
3269 if ((n2 = n->m_next) != NULL &&
3270 n->m_len + n2->m_len < MCLBYTES) {
3271 m = m_getcl(how, MT_DATA, 0);
3272 if (m == NULL)
3273 goto bad;
3274 bcopy(mtod(n, void *), mtod(m, void *), n->m_len);
3275 bcopy(mtod(n2, void *), mtod(m, char *) + n->m_len,
3276 n2->m_len);
3277 m->m_len = n->m_len + n2->m_len;
3278 m->m_next = n2->m_next;
3279 *prev = m;
3280 m_free(n);
3281 m_free(n2);
3282 if (--curfrags <= maxfrags) /* +1 cl -2 mbufs */
3283 return m0;
3284 /*
3285 * Still not there, try the normal collapse
3286 * again before we allocate another cluster.
3287 */
3288 goto again;
3289 }
3290 prev = &n->m_next;
3291 }
3292 /*
3293 * No place where we can collapse to a cluster; punt.
3294 * This can occur if, for example, you request 2 frags
3295 * but the packet requires that both be clusters (we
3296 * never reallocate the first mbuf to avoid moving the
3297 * packet header).
3298 */
3299 bad:
3300 return NULL;
3301 }
3302
3303 static int
3304 ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf,
3305 struct mbuf *m0)
3306 {
3307 #define CTS_DURATION \
3308 ath_hal_computetxtime(ah, rt, IEEE80211_ACK_LEN, cix, AH_TRUE)
3309 #define updateCTSForBursting(_ah, _ds, _txq) \
3310 ath_hal_updateCTSForBursting(_ah, _ds, \
3311 _txq->axq_linkbuf != NULL ? _txq->axq_linkbuf->bf_desc : NULL, \
3312 _txq->axq_lastdsWithCTS, _txq->axq_gatingds, \
3313 txopLimit, CTS_DURATION)
3314 struct ieee80211com *ic = &sc->sc_ic;
3315 struct ath_hal *ah = sc->sc_ah;
3316 struct ifnet *ifp = &sc->sc_if;
3317 const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
3318 int i, error, iswep, ismcast, keyix, hdrlen, pktlen, try0;
3319 u_int8_t rix, txrate, ctsrate;
3320 u_int8_t cix = 0xff; /* NB: silence compiler */
3321 struct ath_desc *ds, *ds0;
3322 struct ath_txq *txq;
3323 struct ieee80211_frame *wh;
3324 u_int subtype, flags, ctsduration;
3325 HAL_PKT_TYPE atype;
3326 const HAL_RATE_TABLE *rt;
3327 HAL_BOOL shortPreamble;
3328 struct ath_node *an;
3329 struct mbuf *m;
3330 u_int pri;
3331
3332 wh = mtod(m0, struct ieee80211_frame *);
3333 iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
3334 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
3335 hdrlen = ieee80211_anyhdrsize(wh);
3336 /*
3337 * Packet length must not include any
3338 * pad bytes; deduct them here.
3339 */
3340 pktlen = m0->m_pkthdr.len - (hdrlen & 3);
3341
3342 if (iswep) {
3343 const struct ieee80211_cipher *cip;
3344 struct ieee80211_key *k;
3345
3346 /*
3347 * Construct the 802.11 header+trailer for an encrypted
3348 * frame. The only reason this can fail is because of an
3349 * unknown or unsupported cipher/key type.
3350 */
3351 k = ieee80211_crypto_encap(ic, ni, m0);
3352 if (k == NULL) {
3353 /*
3354 * This can happen when the key is yanked after the
3355 * frame was queued. Just discard the frame; the
3356 * 802.11 layer counts failures and provides
3357 * debugging/diagnostics.
3358 */
3359 m_freem(m0);
3360 return EIO;
3361 }
3362 /*
3363 * Adjust the packet + header lengths for the crypto
3364 * additions and calculate the h/w key index. When
3365 * a s/w mic is done the frame will have had any mic
3366 * added to it prior to entry so skb->len above will
3367 * account for it. Otherwise we need to add it to the
3368 * packet length.
3369 */
3370 cip = k->wk_cipher;
3371 hdrlen += cip->ic_header;
3372 pktlen += cip->ic_header + cip->ic_trailer;
3373 if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0)
3374 pktlen += cip->ic_miclen;
3375 keyix = k->wk_keyix;
3376
3377 /* packet header may have moved, reset our local pointer */
3378 wh = mtod(m0, struct ieee80211_frame *);
3379 } else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
3380 /*
3381 * Use station key cache slot, if assigned.
3382 */
3383 keyix = ni->ni_ucastkey.wk_keyix;
3384 if (keyix == IEEE80211_KEYIX_NONE)
3385 keyix = HAL_TXKEYIX_INVALID;
3386 } else
3387 keyix = HAL_TXKEYIX_INVALID;
3388
3389 pktlen += IEEE80211_CRC_LEN;
3390
3391 /*
3392 * Load the DMA map so any coalescing is done. This
3393 * also calculates the number of descriptors we need.
3394 */
3395 error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0,
3396 BUS_DMA_NOWAIT);
3397 if (error == EFBIG) {
3398 /* XXX packet requires too many descriptors */
3399 bf->bf_nseg = ATH_TXDESC+1;
3400 } else if (error != 0) {
3401 sc->sc_stats.ast_tx_busdma++;
3402 m_freem(m0);
3403 return error;
3404 }
3405 /*
3406 * Discard null packets and check for packets that
3407 * require too many TX descriptors. We try to convert
3408 * the latter to a cluster.
3409 */
3410 if (error == EFBIG) { /* too many desc's, linearize */
3411 sc->sc_stats.ast_tx_linear++;
3412 m = ath_defrag(m0, M_DONTWAIT, ATH_TXDESC);
3413 if (m == NULL) {
3414 m_freem(m0);
3415 sc->sc_stats.ast_tx_nombuf++;
3416 return ENOMEM;
3417 }
3418 m0 = m;
3419 error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_dmamap, m0,
3420 BUS_DMA_NOWAIT);
3421 if (error != 0) {
3422 sc->sc_stats.ast_tx_busdma++;
3423 m_freem(m0);
3424 return error;
3425 }
3426 KASSERT(bf->bf_nseg <= ATH_TXDESC,
3427 ("too many segments after defrag; nseg %u", bf->bf_nseg));
3428 } else if (bf->bf_nseg == 0) { /* null packet, discard */
3429 sc->sc_stats.ast_tx_nodata++;
3430 m_freem(m0);
3431 return EIO;
3432 }
3433 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n", __func__, m0, pktlen);
3434 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 0,
3435 bf->bf_dmamap->dm_mapsize, BUS_DMASYNC_PREWRITE);
3436 bf->bf_m = m0;
3437 bf->bf_node = ni; /* NB: held reference */
3438
3439 /* setup descriptors */
3440 ds = bf->bf_desc;
3441 rt = sc->sc_currates;
3442 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
3443
3444 /*
3445 * NB: the 802.11 layer marks whether or not we should
3446 * use short preamble based on the current mode and
3447 * negotiated parameters.
3448 */
3449 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
3450 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE) && !ismcast) {
3451 shortPreamble = AH_TRUE;
3452 sc->sc_stats.ast_tx_shortpre++;
3453 } else {
3454 shortPreamble = AH_FALSE;
3455 }
3456
3457 an = ATH_NODE(ni);
3458 flags = HAL_TXDESC_CLRDMASK; /* XXX needed for crypto errs */
3459 /*
3460 * Calculate Atheros packet type from IEEE80211 packet header,
3461 * setup for rate calculations, and select h/w transmit queue.
3462 */
3463 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
3464 case IEEE80211_FC0_TYPE_MGT:
3465 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
3466 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
3467 atype = HAL_PKT_TYPE_BEACON;
3468 else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
3469 atype = HAL_PKT_TYPE_PROBE_RESP;
3470 else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
3471 atype = HAL_PKT_TYPE_ATIM;
3472 else
3473 atype = HAL_PKT_TYPE_NORMAL; /* XXX */
3474 rix = 0; /* XXX lowest rate */
3475 try0 = ATH_TXMAXTRY;
3476 if (shortPreamble)
3477 txrate = an->an_tx_mgtratesp;
3478 else
3479 txrate = an->an_tx_mgtrate;
3480 /* NB: force all management frames to highest queue */
3481 if (ni->ni_flags & IEEE80211_NODE_QOS) {
3482 /* NB: force all management frames to highest queue */
3483 pri = WME_AC_VO;
3484 } else
3485 pri = WME_AC_BE;
3486 flags |= HAL_TXDESC_INTREQ; /* force interrupt */
3487 break;
3488 case IEEE80211_FC0_TYPE_CTL:
3489 atype = HAL_PKT_TYPE_PSPOLL; /* stop setting of duration */
3490 rix = 0; /* XXX lowest rate */
3491 try0 = ATH_TXMAXTRY;
3492 if (shortPreamble)
3493 txrate = an->an_tx_mgtratesp;
3494 else
3495 txrate = an->an_tx_mgtrate;
3496 /* NB: force all ctl frames to highest queue */
3497 if (ni->ni_flags & IEEE80211_NODE_QOS) {
3498 /* NB: force all ctl frames to highest queue */
3499 pri = WME_AC_VO;
3500 } else
3501 pri = WME_AC_BE;
3502 flags |= HAL_TXDESC_INTREQ; /* force interrupt */
3503 break;
3504 case IEEE80211_FC0_TYPE_DATA:
3505 atype = HAL_PKT_TYPE_NORMAL; /* default */
3506 /*
3507 * Data frames; consult the rate control module for
3508 * unicast frames. Send multicast frames at the
3509 * lowest rate.
3510 */
3511 if (!ismcast) {
3512 ath_rate_findrate(sc, an, shortPreamble, pktlen,
3513 &rix, &try0, &txrate);
3514 } else {
3515 rix = 0;
3516 try0 = ATH_TXMAXTRY;
3517 txrate = an->an_tx_mgtrate;
3518 }
3519 sc->sc_txrate = txrate; /* for LED blinking */
3520 /*
3521 * Default all non-QoS traffic to the background queue.
3522 */
3523 if (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) {
3524 pri = M_WME_GETAC(m0);
3525 if (cap->cap_wmeParams[pri].wmep_noackPolicy) {
3526 flags |= HAL_TXDESC_NOACK;
3527 sc->sc_stats.ast_tx_noack++;
3528 }
3529 } else
3530 pri = WME_AC_BE;
3531 break;
3532 default:
3533 if_printf(ifp, "bogus frame type 0x%x (%s)\n",
3534 wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
3535 /* XXX statistic */
3536 m_freem(m0);
3537 return EIO;
3538 }
3539 txq = sc->sc_ac2q[pri];
3540
3541 /*
3542 * When servicing one or more stations in power-save mode
3543 * multicast frames must be buffered until after the beacon.
3544 * We use the CAB queue for that.
3545 */
3546 if (ismcast && ic->ic_ps_sta) {
3547 txq = sc->sc_cabq;
3548 /* XXX? more bit in 802.11 frame header */
3549 }
3550
3551 /*
3552 * Calculate miscellaneous flags.
3553 */
3554 if (ismcast) {
3555 flags |= HAL_TXDESC_NOACK; /* no ack on broad/multicast */
3556 sc->sc_stats.ast_tx_noack++;
3557 } else if (pktlen > ic->ic_rtsthreshold) {
3558 flags |= HAL_TXDESC_RTSENA; /* RTS based on frame length */
3559 cix = rt->info[rix].controlRate;
3560 sc->sc_stats.ast_tx_rts++;
3561 }
3562
3563 /*
3564 * If 802.11g protection is enabled, determine whether
3565 * to use RTS/CTS or just CTS. Note that this is only
3566 * done for OFDM unicast frames.
3567 */
3568 if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
3569 rt->info[rix].phy == IEEE80211_T_OFDM &&
3570 (flags & HAL_TXDESC_NOACK) == 0) {
3571 /* XXX fragments must use CCK rates w/ protection */
3572 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
3573 flags |= HAL_TXDESC_RTSENA;
3574 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
3575 flags |= HAL_TXDESC_CTSENA;
3576 cix = rt->info[sc->sc_protrix].controlRate;
3577 sc->sc_stats.ast_tx_protect++;
3578 }
3579
3580 /*
3581 * Calculate duration. This logically belongs in the 802.11
3582 * layer but it lacks sufficient information to calculate it.
3583 */
3584 if ((flags & HAL_TXDESC_NOACK) == 0 &&
3585 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
3586 u_int16_t dur;
3587 /*
3588 * XXX not right with fragmentation.
3589 */
3590 if (shortPreamble)
3591 dur = rt->info[rix].spAckDuration;
3592 else
3593 dur = rt->info[rix].lpAckDuration;
3594 *(u_int16_t *)wh->i_dur = htole16(dur);
3595 }
3596
3597 /*
3598 * Calculate RTS/CTS rate and duration if needed.
3599 */
3600 ctsduration = 0;
3601 if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
3602 /*
3603 * CTS transmit rate is derived from the transmit rate
3604 * by looking in the h/w rate table. We must also factor
3605 * in whether or not a short preamble is to be used.
3606 */
3607 /* NB: cix is set above where RTS/CTS is enabled */
3608 KASSERT(cix != 0xff, ("cix not setup"));
3609 ctsrate = rt->info[cix].rateCode;
3610 /*
3611 * Compute the transmit duration based on the frame
3612 * size and the size of an ACK frame. We call into the
3613 * HAL to do the computation since it depends on the
3614 * characteristics of the actual PHY being used.
3615 *
3616 * NB: CTS is assumed the same size as an ACK so we can
3617 * use the precalculated ACK durations.
3618 */
3619 if (shortPreamble) {
3620 ctsrate |= rt->info[cix].shortPreamble;
3621 if (flags & HAL_TXDESC_RTSENA) /* SIFS + CTS */
3622 ctsduration += rt->info[cix].spAckDuration;
3623 ctsduration += ath_hal_computetxtime(ah,
3624 rt, pktlen, rix, AH_TRUE);
3625 if ((flags & HAL_TXDESC_NOACK) == 0) /* SIFS + ACK */
3626 ctsduration += rt->info[cix].spAckDuration;
3627 } else {
3628 if (flags & HAL_TXDESC_RTSENA) /* SIFS + CTS */
3629 ctsduration += rt->info[cix].lpAckDuration;
3630 ctsduration += ath_hal_computetxtime(ah,
3631 rt, pktlen, rix, AH_FALSE);
3632 if ((flags & HAL_TXDESC_NOACK) == 0) /* SIFS + ACK */
3633 ctsduration += rt->info[cix].lpAckDuration;
3634 }
3635 /*
3636 * Must disable multi-rate retry when using RTS/CTS.
3637 */
3638 try0 = ATH_TXMAXTRY;
3639 } else
3640 ctsrate = 0;
3641
3642 if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
3643 ieee80211_dump_pkt(mtod(m0, caddr_t), m0->m_len,
3644 sc->sc_hwmap[txrate].ieeerate, -1);
3645
3646 if (ic->ic_rawbpf)
3647 bpf_mtap(ic->ic_rawbpf, m0);
3648 if (sc->sc_drvbpf) {
3649 sc->sc_tx_th.wt_flags = sc->sc_hwmap[txrate].txflags;
3650 if (iswep)
3651 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
3652 sc->sc_tx_th.wt_rate = sc->sc_hwmap[txrate].ieeerate;
3653 sc->sc_tx_th.wt_txpower = ni->ni_txpower;
3654 sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
3655
3656 bpf_mtap2(sc->sc_drvbpf,
3657 &sc->sc_tx_th, sc->sc_tx_th_len, m0);
3658 }
3659
3660 /*
3661 * Determine if a tx interrupt should be generated for
3662 * this descriptor. We take a tx interrupt to reap
3663 * descriptors when the h/w hits an EOL condition or
3664 * when the descriptor is specifically marked to generate
3665 * an interrupt. We periodically mark descriptors in this
3666 * way to insure timely replenishing of the supply needed
3667 * for sending frames. Defering interrupts reduces system
3668 * load and potentially allows more concurrent work to be
3669 * done but if done to aggressively can cause senders to
3670 * backup.
3671 *
3672 * NB: use >= to deal with sc_txintrperiod changing
3673 * dynamically through sysctl.
3674 */
3675 if (flags & HAL_TXDESC_INTREQ) {
3676 txq->axq_intrcnt = 0;
3677 } else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
3678 flags |= HAL_TXDESC_INTREQ;
3679 txq->axq_intrcnt = 0;
3680 }
3681
3682 /*
3683 * Formulate first tx descriptor with tx controls.
3684 */
3685 /* XXX check return value? */
3686 ath_hal_setuptxdesc(ah, ds
3687 , pktlen /* packet length */
3688 , hdrlen /* header length */
3689 , atype /* Atheros packet type */
3690 , ni->ni_txpower /* txpower */
3691 , txrate, try0 /* series 0 rate/tries */
3692 , keyix /* key cache index */
3693 , sc->sc_txantenna /* antenna mode */
3694 , flags /* flags */
3695 , ctsrate /* rts/cts rate */
3696 , ctsduration /* rts/cts duration */
3697 );
3698 bf->bf_flags = flags;
3699 /*
3700 * Setup the multi-rate retry state only when we're
3701 * going to use it. This assumes ath_hal_setuptxdesc
3702 * initializes the descriptors (so we don't have to)
3703 * when the hardware supports multi-rate retry and
3704 * we don't use it.
3705 */
3706 if (try0 != ATH_TXMAXTRY)
3707 ath_rate_setupxtxdesc(sc, an, ds, shortPreamble, rix);
3708
3709 /*
3710 * Fillin the remainder of the descriptor info.
3711 */
3712 ds0 = ds;
3713 for (i = 0; i < bf->bf_nseg; i++, ds++) {
3714 ds->ds_data = bf->bf_segs[i].ds_addr;
3715 if (i == bf->bf_nseg - 1)
3716 ds->ds_link = 0;
3717 else
3718 ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
3719 ath_hal_filltxdesc(ah, ds
3720 , bf->bf_segs[i].ds_len /* segment length */
3721 , i == 0 /* first segment */
3722 , i == bf->bf_nseg - 1 /* last segment */
3723 , ds0 /* first descriptor */
3724 );
3725 DPRINTF(sc, ATH_DEBUG_XMIT,
3726 "%s: %d: %08x %08x %08x %08x %08x %08x\n",
3727 __func__, i, ds->ds_link, ds->ds_data,
3728 ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]);
3729 }
3730 /*
3731 * Insert the frame on the outbound list and
3732 * pass it on to the hardware.
3733 */
3734 ATH_TXQ_LOCK(txq);
3735 if (flags & (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA)) {
3736 u_int32_t txopLimit = IEEE80211_TXOP_TO_US(
3737 cap->cap_wmeParams[pri].wmep_txopLimit);
3738 /*
3739 * When bursting, potentially extend the CTS duration
3740 * of a previously queued frame to cover this frame
3741 * and not exceed the txopLimit. If that can be done
3742 * then disable RTS/CTS on this frame since it's now
3743 * covered (burst extension). Otherwise we must terminate
3744 * the burst before this frame goes out so as not to
3745 * violate the WME parameters. All this is complicated
3746 * as we need to update the state of packets on the
3747 * (live) hardware queue. The logic is buried in the hal
3748 * because it's highly chip-specific.
3749 */
3750 if (txopLimit != 0) {
3751 sc->sc_stats.ast_tx_ctsburst++;
3752 if (updateCTSForBursting(ah, ds0, txq) == 0) {
3753 /*
3754 * This frame was not covered by RTS/CTS from
3755 * the previous frame in the burst; update the
3756 * descriptor pointers so this frame is now
3757 * treated as the last frame for extending a
3758 * burst.
3759 */
3760 txq->axq_lastdsWithCTS = ds0;
3761 /* set gating Desc to final desc */
3762 txq->axq_gatingds =
3763 (struct ath_desc *)txq->axq_link;
3764 } else
3765 sc->sc_stats.ast_tx_ctsext++;
3766 }
3767 }
3768 ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
3769 if (txq->axq_link == NULL) {
3770 ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
3771 DPRINTF(sc, ATH_DEBUG_XMIT,
3772 "%s: TXDP[%u] = %p (%p) depth %d\n", __func__,
3773 txq->axq_qnum, (caddr_t)bf->bf_daddr, bf->bf_desc,
3774 txq->axq_depth);
3775 } else {
3776 *txq->axq_link = bf->bf_daddr;
3777 DPRINTF(sc, ATH_DEBUG_XMIT,
3778 "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
3779 txq->axq_qnum, txq->axq_link,
3780 (caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth);
3781 }
3782 txq->axq_link = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
3783 /*
3784 * The CAB queue is started from the SWBA handler since
3785 * frames only go out on DTIM and to avoid possible races.
3786 */
3787 if (txq != sc->sc_cabq)
3788 ath_hal_txstart(ah, txq->axq_qnum);
3789 ATH_TXQ_UNLOCK(txq);
3790
3791 return 0;
3792 #undef updateCTSForBursting
3793 #undef CTS_DURATION
3794 }
3795
3796 /*
3797 * Process completed xmit descriptors from the specified queue.
3798 */
3799 static void
3800 ath_tx_processq(struct ath_softc *sc, struct ath_txq *txq)
3801 {
3802 struct ath_hal *ah = sc->sc_ah;
3803 struct ieee80211com *ic = &sc->sc_ic;
3804 struct ath_buf *bf;
3805 struct ath_desc *ds, *ds0;
3806 struct ieee80211_node *ni;
3807 struct ath_node *an;
3808 int sr, lr, pri;
3809 HAL_STATUS status;
3810
3811 DPRINTF(sc, ATH_DEBUG_TX_PROC, "%s: tx queue %u head %p link %p\n",
3812 __func__, txq->axq_qnum,
3813 (caddr_t)(uintptr_t) ath_hal_gettxbuf(sc->sc_ah, txq->axq_qnum),
3814 txq->axq_link);
3815 for (;;) {
3816 ATH_TXQ_LOCK(txq);
3817 txq->axq_intrcnt = 0; /* reset periodic desc intr count */
3818 bf = STAILQ_FIRST(&txq->axq_q);
3819 if (bf == NULL) {
3820 txq->axq_link = NULL;
3821 ATH_TXQ_UNLOCK(txq);
3822 break;
3823 }
3824 ds0 = &bf->bf_desc[0];
3825 ds = &bf->bf_desc[bf->bf_nseg - 1];
3826 status = ath_hal_txprocdesc(ah, ds);
3827 #ifdef AR_DEBUG
3828 if (sc->sc_debug & ATH_DEBUG_XMIT_DESC)
3829 ath_printtxbuf(bf, status == HAL_OK);
3830 #endif
3831 if (status == HAL_EINPROGRESS) {
3832 ATH_TXQ_UNLOCK(txq);
3833 break;
3834 }
3835 if (ds0 == txq->axq_lastdsWithCTS)
3836 txq->axq_lastdsWithCTS = NULL;
3837 if (ds == txq->axq_gatingds)
3838 txq->axq_gatingds = NULL;
3839 ATH_TXQ_REMOVE_HEAD(txq, bf_list);
3840 ATH_TXQ_UNLOCK(txq);
3841
3842 ni = bf->bf_node;
3843 if (ni != NULL) {
3844 an = ATH_NODE(ni);
3845 if (ds->ds_txstat.ts_status == 0) {
3846 u_int8_t txant = ds->ds_txstat.ts_antenna;
3847 sc->sc_stats.ast_ant_tx[txant]++;
3848 sc->sc_ant_tx[txant]++;
3849 if (ds->ds_txstat.ts_rate & HAL_TXSTAT_ALTRATE)
3850 sc->sc_stats.ast_tx_altrate++;
3851 sc->sc_stats.ast_tx_rssi =
3852 ds->ds_txstat.ts_rssi;
3853 ATH_RSSI_LPF(an->an_halstats.ns_avgtxrssi,
3854 ds->ds_txstat.ts_rssi);
3855 pri = M_WME_GETAC(bf->bf_m);
3856 if (pri >= WME_AC_VO)
3857 ic->ic_wme.wme_hipri_traffic++;
3858 ni->ni_inact = ni->ni_inact_reload;
3859 } else {
3860 if (ds->ds_txstat.ts_status & HAL_TXERR_XRETRY)
3861 sc->sc_stats.ast_tx_xretries++;
3862 if (ds->ds_txstat.ts_status & HAL_TXERR_FIFO)
3863 sc->sc_stats.ast_tx_fifoerr++;
3864 if (ds->ds_txstat.ts_status & HAL_TXERR_FILT)
3865 sc->sc_stats.ast_tx_filtered++;
3866 }
3867 sr = ds->ds_txstat.ts_shortretry;
3868 lr = ds->ds_txstat.ts_longretry;
3869 sc->sc_stats.ast_tx_shortretry += sr;
3870 sc->sc_stats.ast_tx_longretry += lr;
3871 /*
3872 * Hand the descriptor to the rate control algorithm.
3873 */
3874 if ((ds->ds_txstat.ts_status & HAL_TXERR_FILT) == 0 &&
3875 (bf->bf_flags & HAL_TXDESC_NOACK) == 0)
3876 ath_rate_tx_complete(sc, an, ds, ds0);
3877 /*
3878 * Reclaim reference to node.
3879 *
3880 * NB: the node may be reclaimed here if, for example
3881 * this is a DEAUTH message that was sent and the
3882 * node was timed out due to inactivity.
3883 */
3884 ieee80211_free_node(ni);
3885 }
3886 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, 0,
3887 bf->bf_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
3888 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
3889 m_freem(bf->bf_m);
3890 bf->bf_m = NULL;
3891 bf->bf_node = NULL;
3892
3893 ATH_TXBUF_LOCK(sc);
3894 STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
3895 ATH_TXBUF_UNLOCK(sc);
3896 }
3897 }
3898
3899 /*
3900 * Deferred processing of transmit interrupt; special-cased
3901 * for a single hardware transmit queue (e.g. 5210 and 5211).
3902 */
3903 static void
3904 ath_tx_proc_q0(void *arg, int npending)
3905 {
3906 struct ath_softc *sc = arg;
3907 struct ifnet *ifp = &sc->sc_if;
3908
3909 ath_tx_processq(sc, &sc->sc_txq[0]);
3910 ath_tx_processq(sc, sc->sc_cabq);
3911 ifp->if_flags &= ~IFF_OACTIVE;
3912 sc->sc_tx_timer = 0;
3913
3914 if (sc->sc_softled)
3915 ath_led_event(sc, ATH_LED_TX);
3916
3917 ath_start(ifp);
3918 }
3919
3920 /*
3921 * Deferred processing of transmit interrupt; special-cased
3922 * for four hardware queues, 0-3 (e.g. 5212 w/ WME support).
3923 */
3924 static void
3925 ath_tx_proc_q0123(void *arg, int npending)
3926 {
3927 struct ath_softc *sc = arg;
3928 struct ifnet *ifp = &sc->sc_if;
3929
3930 /*
3931 * Process each active queue.
3932 */
3933 ath_tx_processq(sc, &sc->sc_txq[0]);
3934 ath_tx_processq(sc, &sc->sc_txq[1]);
3935 ath_tx_processq(sc, &sc->sc_txq[2]);
3936 ath_tx_processq(sc, &sc->sc_txq[3]);
3937 ath_tx_processq(sc, sc->sc_cabq);
3938
3939 ifp->if_flags &= ~IFF_OACTIVE;
3940 sc->sc_tx_timer = 0;
3941
3942 if (sc->sc_softled)
3943 ath_led_event(sc, ATH_LED_TX);
3944
3945 ath_start(ifp);
3946 }
3947
3948 /*
3949 * Deferred processing of transmit interrupt.
3950 */
3951 static void
3952 ath_tx_proc(void *arg, int npending)
3953 {
3954 struct ath_softc *sc = arg;
3955 struct ifnet *ifp = &sc->sc_if;
3956 int i;
3957
3958 /*
3959 * Process each active queue.
3960 */
3961 /* XXX faster to read ISR_S0_S and ISR_S1_S to determine q's? */
3962 for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
3963 if (ATH_TXQ_SETUP(sc, i))
3964 ath_tx_processq(sc, &sc->sc_txq[i]);
3965
3966 ifp->if_flags &= ~IFF_OACTIVE;
3967 sc->sc_tx_timer = 0;
3968
3969 if (sc->sc_softled)
3970 ath_led_event(sc, ATH_LED_TX);
3971
3972 ath_start(ifp);
3973 }
3974
3975 static void
3976 ath_tx_draintxq(struct ath_softc *sc, struct ath_txq *txq)
3977 {
3978 struct ath_hal *ah = sc->sc_ah;
3979 struct ieee80211_node *ni;
3980 struct ath_buf *bf;
3981
3982 /*
3983 * NB: this assumes output has been stopped and
3984 * we do not need to block ath_tx_tasklet
3985 */
3986 for (;;) {
3987 ATH_TXQ_LOCK(txq);
3988 bf = STAILQ_FIRST(&txq->axq_q);
3989 if (bf == NULL) {
3990 txq->axq_link = NULL;
3991 ATH_TXQ_UNLOCK(txq);
3992 break;
3993 }
3994 ATH_TXQ_REMOVE_HEAD(txq, bf_list);
3995 ATH_TXQ_UNLOCK(txq);
3996 #ifdef AR_DEBUG
3997 if (sc->sc_debug & ATH_DEBUG_RESET)
3998 ath_printtxbuf(bf,
3999 ath_hal_txprocdesc(ah, bf->bf_desc) == HAL_OK);
4000 #endif /* AR_DEBUG */
4001 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
4002 m_freem(bf->bf_m);
4003 bf->bf_m = NULL;
4004 ni = bf->bf_node;
4005 bf->bf_node = NULL;
4006 if (ni != NULL) {
4007 /*
4008 * Reclaim node reference.
4009 */
4010 ieee80211_free_node(ni);
4011 }
4012 ATH_TXBUF_LOCK(sc);
4013 STAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
4014 ATH_TXBUF_UNLOCK(sc);
4015 }
4016 }
4017
4018 static void
4019 ath_tx_stopdma(struct ath_softc *sc, struct ath_txq *txq)
4020 {
4021 struct ath_hal *ah = sc->sc_ah;
4022
4023 (void) ath_hal_stoptxdma(ah, txq->axq_qnum);
4024 DPRINTF(sc, ATH_DEBUG_RESET, "%s: tx queue [%u] %p, link %p\n",
4025 __func__, txq->axq_qnum,
4026 (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, txq->axq_qnum),
4027 txq->axq_link);
4028 }
4029
4030 /*
4031 * Drain the transmit queues and reclaim resources.
4032 */
4033 static void
4034 ath_draintxq(struct ath_softc *sc)
4035 {
4036 struct ath_hal *ah = sc->sc_ah;
4037 struct ifnet *ifp = &sc->sc_if;
4038 int i;
4039
4040 /* XXX return value */
4041 if (!sc->sc_invalid) {
4042 /* don't touch the hardware if marked invalid */
4043 (void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
4044 DPRINTF(sc, ATH_DEBUG_RESET,
4045 "%s: beacon queue %p\n", __func__,
4046 (caddr_t)(uintptr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq));
4047 for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4048 if (ATH_TXQ_SETUP(sc, i))
4049 ath_tx_stopdma(sc, &sc->sc_txq[i]);
4050 }
4051 for (i = 0; i < HAL_NUM_TX_QUEUES; i++)
4052 if (ATH_TXQ_SETUP(sc, i))
4053 ath_tx_draintxq(sc, &sc->sc_txq[i]);
4054 ifp->if_flags &= ~IFF_OACTIVE;
4055 sc->sc_tx_timer = 0;
4056 }
4057
4058 /*
4059 * Disable the receive h/w in preparation for a reset.
4060 */
4061 static void
4062 ath_stoprecv(struct ath_softc *sc)
4063 {
4064 #define PA2DESC(_sc, _pa) \
4065 ((struct ath_desc *)((caddr_t)(_sc)->sc_rxdma.dd_desc + \
4066 ((_pa) - (_sc)->sc_rxdma.dd_desc_paddr)))
4067 struct ath_hal *ah = sc->sc_ah;
4068
4069 ath_hal_stoppcurecv(ah); /* disable PCU */
4070 ath_hal_setrxfilter(ah, 0); /* clear recv filter */
4071 ath_hal_stopdmarecv(ah); /* disable DMA engine */
4072 DELAY(3000); /* 3ms is long enough for 1 frame */
4073 #ifdef AR_DEBUG
4074 if (sc->sc_debug & (ATH_DEBUG_RESET | ATH_DEBUG_FATAL)) {
4075 struct ath_buf *bf;
4076
4077 printf("%s: rx queue %p, link %p\n", __func__,
4078 (caddr_t)(uintptr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink);
4079 STAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
4080 struct ath_desc *ds = bf->bf_desc;
4081 HAL_STATUS status = ath_hal_rxprocdesc(ah, ds,
4082 bf->bf_daddr, PA2DESC(sc, ds->ds_link));
4083 if (status == HAL_OK || (sc->sc_debug & ATH_DEBUG_FATAL))
4084 ath_printrxbuf(bf, status == HAL_OK);
4085 }
4086 }
4087 #endif
4088 sc->sc_rxlink = NULL; /* just in case */
4089 #undef PA2DESC
4090 }
4091
4092 /*
4093 * Enable the receive h/w following a reset.
4094 */
4095 static int
4096 ath_startrecv(struct ath_softc *sc)
4097 {
4098 struct ath_hal *ah = sc->sc_ah;
4099 struct ath_buf *bf;
4100
4101 sc->sc_rxlink = NULL;
4102 STAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
4103 int error = ath_rxbuf_init(sc, bf);
4104 if (error != 0) {
4105 DPRINTF(sc, ATH_DEBUG_RECV,
4106 "%s: ath_rxbuf_init failed %d\n",
4107 __func__, error);
4108 return error;
4109 }
4110 }
4111
4112 bf = STAILQ_FIRST(&sc->sc_rxbuf);
4113 ath_hal_putrxbuf(ah, bf->bf_daddr);
4114 ath_hal_rxena(ah); /* enable recv descriptors */
4115 ath_mode_init(sc); /* set filters, etc. */
4116 ath_hal_startpcurecv(ah); /* re-enable PCU/DMA engine */
4117 return 0;
4118 }
4119
4120 /*
4121 * Update internal state after a channel change.
4122 */
4123 static void
4124 ath_chan_change(struct ath_softc *sc, struct ieee80211_channel *chan)
4125 {
4126 struct ieee80211com *ic = &sc->sc_ic;
4127 enum ieee80211_phymode mode;
4128 u_int16_t flags;
4129
4130 /*
4131 * Change channels and update the h/w rate map
4132 * if we're switching; e.g. 11a to 11b/g.
4133 */
4134 mode = ieee80211_chan2mode(ic, chan);
4135 if (mode != sc->sc_curmode)
4136 ath_setcurmode(sc, mode);
4137 /*
4138 * Update BPF state. NB: ethereal et. al. don't handle
4139 * merged flags well so pick a unique mode for their use.
4140 */
4141 if (IEEE80211_IS_CHAN_A(chan))
4142 flags = IEEE80211_CHAN_A;
4143 /* XXX 11g schizophrenia */
4144 else if (IEEE80211_IS_CHAN_G(chan) ||
4145 IEEE80211_IS_CHAN_PUREG(chan))
4146 flags = IEEE80211_CHAN_G;
4147 else
4148 flags = IEEE80211_CHAN_B;
4149 if (IEEE80211_IS_CHAN_T(chan))
4150 flags |= IEEE80211_CHAN_TURBO;
4151 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
4152 htole16(chan->ic_freq);
4153 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
4154 htole16(flags);
4155 }
4156
4157 /*
4158 * Set/change channels. If the channel is really being changed,
4159 * it's done by reseting the chip. To accomplish this we must
4160 * first cleanup any pending DMA, then restart stuff after a la
4161 * ath_init.
4162 */
4163 static int
4164 ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
4165 {
4166 struct ath_hal *ah = sc->sc_ah;
4167 struct ieee80211com *ic = &sc->sc_ic;
4168 HAL_CHANNEL hchan;
4169
4170 /*
4171 * Convert to a HAL channel description with
4172 * the flags constrained to reflect the current
4173 * operating mode.
4174 */
4175 hchan.channel = chan->ic_freq;
4176 hchan.channelFlags = ath_chan2flags(ic, chan);
4177
4178 DPRINTF(sc, ATH_DEBUG_RESET, "%s: %u (%u MHz) -> %u (%u MHz)\n",
4179 __func__,
4180 ath_hal_mhz2ieee(sc->sc_curchan.channel,
4181 sc->sc_curchan.channelFlags),
4182 sc->sc_curchan.channel,
4183 ath_hal_mhz2ieee(hchan.channel, hchan.channelFlags), hchan.channel);
4184 if (hchan.channel != sc->sc_curchan.channel ||
4185 hchan.channelFlags != sc->sc_curchan.channelFlags) {
4186 HAL_STATUS status;
4187
4188 /*
4189 * To switch channels clear any pending DMA operations;
4190 * wait long enough for the RX fifo to drain, reset the
4191 * hardware at the new frequency, and then re-enable
4192 * the relevant bits of the h/w.
4193 */
4194 ath_hal_intrset(ah, 0); /* disable interrupts */
4195 ath_draintxq(sc); /* clear pending tx frames */
4196 ath_stoprecv(sc); /* turn off frame recv */
4197 if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status)) {
4198 if_printf(&sc->sc_if, "ath_chan_set: unable to reset "
4199 "channel %u (%u Mhz)\n",
4200 ieee80211_chan2ieee(ic, chan), chan->ic_freq);
4201 return EIO;
4202 }
4203 sc->sc_curchan = hchan;
4204 ath_update_txpow(sc); /* update tx power state */
4205
4206 /*
4207 * Re-enable rx framework.
4208 */
4209 if (ath_startrecv(sc) != 0) {
4210 if_printf(&sc->sc_if,
4211 "ath_chan_set: unable to restart recv logic\n");
4212 return EIO;
4213 }
4214
4215 /*
4216 * Change channels and update the h/w rate map
4217 * if we're switching; e.g. 11a to 11b/g.
4218 */
4219 ic->ic_ibss_chan = chan;
4220 ath_chan_change(sc, chan);
4221
4222 /*
4223 * Re-enable interrupts.
4224 */
4225 ath_hal_intrset(ah, sc->sc_imask);
4226 }
4227 return 0;
4228 }
4229
4230 static void
4231 ath_next_scan(void *arg)
4232 {
4233 struct ath_softc *sc = arg;
4234 struct ieee80211com *ic = &sc->sc_ic;
4235 int s;
4236
4237 /* don't call ath_start w/o network interrupts blocked */
4238 s = splnet();
4239
4240 if (ic->ic_state == IEEE80211_S_SCAN)
4241 ieee80211_next_scan(ic);
4242 splx(s);
4243 }
4244
4245 /*
4246 * Periodically recalibrate the PHY to account
4247 * for temperature/environment changes.
4248 */
4249 static void
4250 ath_calibrate(void *arg)
4251 {
4252 struct ath_softc *sc = arg;
4253 struct ath_hal *ah = sc->sc_ah;
4254
4255 sc->sc_stats.ast_per_cal++;
4256
4257 ATH_LOCK(sc);
4258 DPRINTF(sc, ATH_DEBUG_CALIBRATE, "%s: channel %u/%x\n",
4259 __func__, sc->sc_curchan.channel, sc->sc_curchan.channelFlags);
4260
4261 if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
4262 /*
4263 * Rfgain is out of bounds, reset the chip
4264 * to load new gain values.
4265 */
4266 sc->sc_stats.ast_per_rfgain++;
4267 ath_reset(&sc->sc_if);
4268 }
4269 if (!ath_hal_calibrate(ah, &sc->sc_curchan)) {
4270 DPRINTF(sc, ATH_DEBUG_ANY,
4271 "%s: calibration of channel %u failed\n",
4272 __func__, sc->sc_curchan.channel);
4273 sc->sc_stats.ast_per_calfail++;
4274 }
4275 callout_reset(&sc->sc_cal_ch, ath_calinterval * hz, ath_calibrate, sc);
4276 ATH_UNLOCK(sc);
4277 }
4278
4279 static int
4280 ath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
4281 {
4282 struct ifnet *ifp = ic->ic_ifp;
4283 struct ath_softc *sc = ifp->if_softc;
4284 struct ath_hal *ah = sc->sc_ah;
4285 struct ieee80211_node *ni;
4286 int i, error;
4287 const u_int8_t *bssid;
4288 u_int32_t rfilt;
4289 static const HAL_LED_STATE leds[] = {
4290 HAL_LED_INIT, /* IEEE80211_S_INIT */
4291 HAL_LED_SCAN, /* IEEE80211_S_SCAN */
4292 HAL_LED_AUTH, /* IEEE80211_S_AUTH */
4293 HAL_LED_ASSOC, /* IEEE80211_S_ASSOC */
4294 HAL_LED_RUN, /* IEEE80211_S_RUN */
4295 };
4296
4297 DPRINTF(sc, ATH_DEBUG_STATE, "%s: %s -> %s\n", __func__,
4298 ieee80211_state_name[ic->ic_state],
4299 ieee80211_state_name[nstate]);
4300
4301 callout_stop(&sc->sc_scan_ch);
4302 callout_stop(&sc->sc_cal_ch);
4303 ath_hal_setledstate(ah, leds[nstate]); /* set LED */
4304
4305 if (nstate == IEEE80211_S_INIT) {
4306 sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
4307 /*
4308 * NB: disable interrupts so we don't rx frames.
4309 */
4310 ath_hal_intrset(ah, sc->sc_imask &~ HAL_INT_GLOBAL);
4311 /*
4312 * Notify the rate control algorithm.
4313 */
4314 ath_rate_newstate(sc, nstate);
4315 goto done;
4316 }
4317 ni = ic->ic_bss;
4318 error = ath_chan_set(sc, ni->ni_chan);
4319 if (error != 0)
4320 goto bad;
4321 rfilt = ath_calcrxfilter(sc, nstate);
4322 if (nstate == IEEE80211_S_SCAN)
4323 bssid = ifp->if_broadcastaddr;
4324 else
4325 bssid = ni->ni_bssid;
4326 ath_hal_setrxfilter(ah, rfilt);
4327 DPRINTF(sc, ATH_DEBUG_STATE, "%s: RX filter 0x%x bssid %s\n",
4328 __func__, rfilt, ether_sprintf(bssid));
4329
4330 if (nstate == IEEE80211_S_RUN && ic->ic_opmode == IEEE80211_M_STA)
4331 ath_hal_setassocid(ah, bssid, ni->ni_associd);
4332 else
4333 ath_hal_setassocid(ah, bssid, 0);
4334 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
4335 for (i = 0; i < IEEE80211_WEP_NKID; i++)
4336 if (ath_hal_keyisvalid(ah, i))
4337 ath_hal_keysetmac(ah, i, bssid);
4338 }
4339
4340 /*
4341 * Notify the rate control algorithm so rates
4342 * are setup should ath_beacon_alloc be called.
4343 */
4344 ath_rate_newstate(sc, nstate);
4345
4346 if (ic->ic_opmode == IEEE80211_M_MONITOR) {
4347 /* nothing to do */;
4348 } else if (nstate == IEEE80211_S_RUN) {
4349 DPRINTF(sc, ATH_DEBUG_STATE,
4350 "%s(RUN): ic_flags=0x%08x iv=%d bssid=%s "
4351 "capinfo=0x%04x chan=%d\n"
4352 , __func__
4353 , ic->ic_flags
4354 , ni->ni_intval
4355 , ether_sprintf(ni->ni_bssid)
4356 , ni->ni_capinfo
4357 , ieee80211_chan2ieee(ic, ni->ni_chan));
4358
4359 switch (ic->ic_opmode) {
4360 case IEEE80211_M_HOSTAP:
4361 case IEEE80211_M_IBSS:
4362 /*
4363 * Allocate and setup the beacon frame.
4364 *
4365 * Stop any previous beacon DMA. This may be
4366 * necessary, for example, when an ibss merge
4367 * causes reconfiguration; there will be a state
4368 * transition from RUN->RUN that means we may
4369 * be called with beacon transmission active.
4370 */
4371 ath_hal_stoptxdma(ah, sc->sc_bhalq);
4372 ath_beacon_free(sc);
4373 error = ath_beacon_alloc(sc, ni);
4374 if (error != 0)
4375 goto bad;
4376 break;
4377 case IEEE80211_M_STA:
4378 /*
4379 * Allocate a key cache slot to the station.
4380 */
4381 if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0 &&
4382 sc->sc_hasclrkey &&
4383 ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE)
4384 ath_setup_stationkey(ni);
4385 break;
4386 default:
4387 break;
4388 }
4389
4390 /*
4391 * Configure the beacon and sleep timers.
4392 */
4393 ath_beacon_config(sc);
4394 } else {
4395 ath_hal_intrset(ah,
4396 sc->sc_imask &~ (HAL_INT_SWBA | HAL_INT_BMISS));
4397 sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
4398 }
4399 done:
4400 /*
4401 * Invoke the parent method to complete the work.
4402 */
4403 error = sc->sc_newstate(ic, nstate, arg);
4404 /*
4405 * Finally, start any timers.
4406 */
4407 if (nstate == IEEE80211_S_RUN) {
4408 /* start periodic recalibration timer */
4409 callout_reset(&sc->sc_cal_ch, ath_calinterval * hz,
4410 ath_calibrate, sc);
4411 } else if (nstate == IEEE80211_S_SCAN) {
4412 /* start ap/neighbor scan timer */
4413 callout_reset(&sc->sc_scan_ch, (ath_dwelltime * hz) / 1000,
4414 ath_next_scan, sc);
4415 }
4416 bad:
4417 return error;
4418 }
4419
4420 /*
4421 * Allocate a key cache slot to the station so we can
4422 * setup a mapping from key index to node. The key cache
4423 * slot is needed for managing antenna state and for
4424 * compression when stations do not use crypto. We do
4425 * it uniliaterally here; if crypto is employed this slot
4426 * will be reassigned.
4427 */
4428 static void
4429 ath_setup_stationkey(struct ieee80211_node *ni)
4430 {
4431 struct ieee80211com *ic = ni->ni_ic;
4432 struct ath_softc *sc = ic->ic_ifp->if_softc;
4433 u_int16_t keyix;
4434
4435 keyix = ath_key_alloc(ic, &ni->ni_ucastkey);
4436 if (keyix == IEEE80211_KEYIX_NONE) {
4437 /*
4438 * Key cache is full; we'll fall back to doing
4439 * the more expensive lookup in software. Note
4440 * this also means no h/w compression.
4441 */
4442 /* XXX msg+statistic */
4443 } else {
4444 ni->ni_ucastkey.wk_keyix = keyix;
4445 /* NB: this will create a pass-thru key entry */
4446 ath_keyset(sc, &ni->ni_ucastkey, ni->ni_macaddr, ic->ic_bss);
4447 }
4448 }
4449
4450 /*
4451 * Setup driver-specific state for a newly associated node.
4452 * Note that we're called also on a re-associate, the isnew
4453 * param tells us if this is the first time or not.
4454 */
4455 static void
4456 ath_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
4457 {
4458 struct ath_softc *sc = ic->ic_ifp->if_softc;
4459
4460 ath_rate_newassoc(sc, ATH_NODE(ni), isnew);
4461 if (isnew &&
4462 (ic->ic_flags & IEEE80211_F_PRIVACY) == 0 && sc->sc_hasclrkey) {
4463 KASSERT(ni->ni_ucastkey.wk_keyix == IEEE80211_KEYIX_NONE,
4464 ("new assoc with a unicast key already setup (keyix %u)",
4465 ni->ni_ucastkey.wk_keyix));
4466 ath_setup_stationkey(ni);
4467 }
4468 }
4469
4470 static int
4471 ath_getchannels(struct ath_softc *sc, u_int cc,
4472 HAL_BOOL outdoor, HAL_BOOL xchanmode)
4473 {
4474 struct ieee80211com *ic = &sc->sc_ic;
4475 struct ifnet *ifp = &sc->sc_if;
4476 struct ath_hal *ah = sc->sc_ah;
4477 HAL_CHANNEL *chans;
4478 int i, ix, nchan;
4479
4480 chans = malloc(IEEE80211_CHAN_MAX * sizeof(HAL_CHANNEL),
4481 M_TEMP, M_NOWAIT);
4482 if (chans == NULL) {
4483 if_printf(ifp, "unable to allocate channel table\n");
4484 return ENOMEM;
4485 }
4486 if (!ath_hal_init_channels(ah, chans, IEEE80211_CHAN_MAX, &nchan,
4487 cc, HAL_MODE_ALL, outdoor, xchanmode)) {
4488 u_int32_t rd;
4489
4490 ath_hal_getregdomain(ah, &rd);
4491 if_printf(ifp, "unable to collect channel list from hal; "
4492 "regdomain likely %u country code %u\n", rd, cc);
4493 free(chans, M_TEMP);
4494 return EINVAL;
4495 }
4496
4497 /*
4498 * Convert HAL channels to ieee80211 ones and insert
4499 * them in the table according to their channel number.
4500 */
4501 for (i = 0; i < nchan; i++) {
4502 HAL_CHANNEL *c = &chans[i];
4503 ix = ath_hal_mhz2ieee(c->channel, c->channelFlags);
4504 if (ix > IEEE80211_CHAN_MAX) {
4505 if_printf(ifp, "bad hal channel %u (%u/%x) ignored\n",
4506 ix, c->channel, c->channelFlags);
4507 continue;
4508 }
4509 DPRINTF(sc, ATH_DEBUG_ANY,
4510 "%s: HAL channel %d/%d freq %d flags %#04x idx %d\n",
4511 sc->sc_dev.dv_xname, i, nchan, c->channel, c->channelFlags,
4512 ix);
4513 /* NB: flags are known to be compatible */
4514 if (ic->ic_channels[ix].ic_freq == 0) {
4515 ic->ic_channels[ix].ic_freq = c->channel;
4516 ic->ic_channels[ix].ic_flags = c->channelFlags;
4517 } else {
4518 /* channels overlap; e.g. 11g and 11b */
4519 ic->ic_channels[ix].ic_flags |= c->channelFlags;
4520 }
4521 }
4522 free(chans, M_TEMP);
4523 return 0;
4524 }
4525
4526 static void
4527 ath_led_done(void *arg)
4528 {
4529 struct ath_softc *sc = arg;
4530
4531 sc->sc_blinking = 0;
4532 }
4533
4534 /*
4535 * Turn the LED off: flip the pin and then set a timer so no
4536 * update will happen for the specified duration.
4537 */
4538 static void
4539 ath_led_off(void *arg)
4540 {
4541 struct ath_softc *sc = arg;
4542
4543 ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, !sc->sc_ledon);
4544 callout_reset(&sc->sc_ledtimer, sc->sc_ledoff, ath_led_done, sc);
4545 }
4546
4547 /*
4548 * Blink the LED according to the specified on/off times.
4549 */
4550 static void
4551 ath_led_blink(struct ath_softc *sc, int on, int off)
4552 {
4553 DPRINTF(sc, ATH_DEBUG_LED, "%s: on %u off %u\n", __func__, on, off);
4554 ath_hal_gpioset(sc->sc_ah, sc->sc_ledpin, sc->sc_ledon);
4555 sc->sc_blinking = 1;
4556 sc->sc_ledoff = off;
4557 callout_reset(&sc->sc_ledtimer, on, ath_led_off, sc);
4558 }
4559
4560 static void
4561 ath_led_event(struct ath_softc *sc, int event)
4562 {
4563
4564 sc->sc_ledevent = ticks; /* time of last event */
4565 if (sc->sc_blinking) /* don't interrupt active blink */
4566 return;
4567 switch (event) {
4568 case ATH_LED_POLL:
4569 ath_led_blink(sc, sc->sc_hwmap[0].ledon,
4570 sc->sc_hwmap[0].ledoff);
4571 break;
4572 case ATH_LED_TX:
4573 ath_led_blink(sc, sc->sc_hwmap[sc->sc_txrate].ledon,
4574 sc->sc_hwmap[sc->sc_txrate].ledoff);
4575 break;
4576 case ATH_LED_RX:
4577 ath_led_blink(sc, sc->sc_hwmap[sc->sc_rxrate].ledon,
4578 sc->sc_hwmap[sc->sc_rxrate].ledoff);
4579 break;
4580 }
4581 }
4582
4583 static void
4584 ath_update_txpow(struct ath_softc *sc)
4585 {
4586 struct ieee80211com *ic = &sc->sc_ic;
4587 struct ath_hal *ah = sc->sc_ah;
4588 u_int32_t txpow;
4589
4590 if (sc->sc_curtxpow != ic->ic_txpowlimit) {
4591 ath_hal_settxpowlimit(ah, ic->ic_txpowlimit);
4592 /* read back in case value is clamped */
4593 ath_hal_gettxpowlimit(ah, &txpow);
4594 ic->ic_txpowlimit = sc->sc_curtxpow = txpow;
4595 }
4596 /*
4597 * Fetch max tx power level for status requests.
4598 */
4599 ath_hal_getmaxtxpow(sc->sc_ah, &txpow);
4600 ic->ic_bss->ni_txpower = txpow;
4601 }
4602
4603 static int
4604 ath_rate_setup(struct ath_softc *sc, u_int mode)
4605 {
4606 struct ath_hal *ah = sc->sc_ah;
4607 struct ieee80211com *ic = &sc->sc_ic;
4608 const HAL_RATE_TABLE *rt;
4609 struct ieee80211_rateset *rs;
4610 int i, maxrates;
4611
4612 switch (mode) {
4613 case IEEE80211_MODE_11A:
4614 sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11A);
4615 break;
4616 case IEEE80211_MODE_11B:
4617 sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11B);
4618 break;
4619 case IEEE80211_MODE_11G:
4620 sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11G);
4621 break;
4622 case IEEE80211_MODE_TURBO_A:
4623 sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_TURBO);
4624 break;
4625 case IEEE80211_MODE_TURBO_G:
4626 sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_108G);
4627 break;
4628 default:
4629 DPRINTF(sc, ATH_DEBUG_ANY, "%s: invalid mode %u\n",
4630 __func__, mode);
4631 return 0;
4632 }
4633 rt = sc->sc_rates[mode];
4634 if (rt == NULL)
4635 return 0;
4636 if (rt->rateCount > IEEE80211_RATE_MAXSIZE) {
4637 DPRINTF(sc, ATH_DEBUG_ANY,
4638 "%s: rate table too small (%u > %u)\n",
4639 __func__, rt->rateCount, IEEE80211_RATE_MAXSIZE);
4640 maxrates = IEEE80211_RATE_MAXSIZE;
4641 } else
4642 maxrates = rt->rateCount;
4643 rs = &ic->ic_sup_rates[mode];
4644 for (i = 0; i < maxrates; i++)
4645 rs->rs_rates[i] = rt->info[i].dot11Rate;
4646 rs->rs_nrates = maxrates;
4647 return 1;
4648 }
4649
4650 static void
4651 ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
4652 {
4653 #define N(a) (sizeof(a)/sizeof(a[0]))
4654 /* NB: on/off times from the Atheros NDIS driver, w/ permission */
4655 static const struct {
4656 u_int rate; /* tx/rx 802.11 rate */
4657 u_int16_t timeOn; /* LED on time (ms) */
4658 u_int16_t timeOff; /* LED off time (ms) */
4659 } blinkrates[] = {
4660 { 108, 40, 10 },
4661 { 96, 44, 11 },
4662 { 72, 50, 13 },
4663 { 48, 57, 14 },
4664 { 36, 67, 16 },
4665 { 24, 80, 20 },
4666 { 22, 100, 25 },
4667 { 18, 133, 34 },
4668 { 12, 160, 40 },
4669 { 10, 200, 50 },
4670 { 6, 240, 58 },
4671 { 4, 267, 66 },
4672 { 2, 400, 100 },
4673 { 0, 500, 130 },
4674 };
4675 const HAL_RATE_TABLE *rt;
4676 int i, j;
4677
4678 memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
4679 rt = sc->sc_rates[mode];
4680 KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
4681 for (i = 0; i < rt->rateCount; i++)
4682 sc->sc_rixmap[rt->info[i].dot11Rate & IEEE80211_RATE_VAL] = i;
4683 memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
4684 for (i = 0; i < 32; i++) {
4685 u_int8_t ix = rt->rateCodeToIndex[i];
4686 if (ix == 0xff) {
4687 sc->sc_hwmap[i].ledon = (500 * hz) / 1000;
4688 sc->sc_hwmap[i].ledoff = (130 * hz) / 1000;
4689 continue;
4690 }
4691 sc->sc_hwmap[i].ieeerate =
4692 rt->info[ix].dot11Rate & IEEE80211_RATE_VAL;
4693 sc->sc_hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
4694 if (rt->info[ix].shortPreamble ||
4695 rt->info[ix].phy == IEEE80211_T_OFDM)
4696 sc->sc_hwmap[i].txflags |= IEEE80211_RADIOTAP_F_SHORTPRE;
4697 /* NB: receive frames include FCS */
4698 sc->sc_hwmap[i].rxflags = sc->sc_hwmap[i].txflags |
4699 IEEE80211_RADIOTAP_F_FCS;
4700 /* setup blink rate table to avoid per-packet lookup */
4701 for (j = 0; j < N(blinkrates)-1; j++)
4702 if (blinkrates[j].rate == sc->sc_hwmap[i].ieeerate)
4703 break;
4704 /* NB: this uses the last entry if the rate isn't found */
4705 /* XXX beware of overlow */
4706 sc->sc_hwmap[i].ledon = (blinkrates[j].timeOn * hz) / 1000;
4707 sc->sc_hwmap[i].ledoff = (blinkrates[j].timeOff * hz) / 1000;
4708 }
4709 sc->sc_currates = rt;
4710 sc->sc_curmode = mode;
4711 /*
4712 * All protection frames are transmited at 2Mb/s for
4713 * 11g, otherwise at 1Mb/s.
4714 * XXX select protection rate index from rate table.
4715 */
4716 sc->sc_protrix = (mode == IEEE80211_MODE_11G ? 1 : 0);
4717 /* NB: caller is responsible for reseting rate control state */
4718 #undef N
4719 }
4720
4721 #ifdef AR_DEBUG
4722 static void
4723 ath_printrxbuf(struct ath_buf *bf, int done)
4724 {
4725 struct ath_desc *ds;
4726 int i;
4727
4728 for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
4729 printf("R%d (%p %p) %08x %08x %08x %08x %08x %08x %c\n",
4730 i, ds, (struct ath_desc *)bf->bf_daddr + i,
4731 ds->ds_link, ds->ds_data,
4732 ds->ds_ctl0, ds->ds_ctl1,
4733 ds->ds_hw[0], ds->ds_hw[1],
4734 !done ? ' ' : (ds->ds_rxstat.rs_status == 0) ? '*' : '!');
4735 }
4736 }
4737
4738 static void
4739 ath_printtxbuf(struct ath_buf *bf, int done)
4740 {
4741 struct ath_desc *ds;
4742 int i;
4743
4744 for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
4745 printf("T%d (%p %p) %08x %08x %08x %08x %08x %08x %08x %08x %c\n",
4746 i, ds, (struct ath_desc *)bf->bf_daddr + i,
4747 ds->ds_link, ds->ds_data,
4748 ds->ds_ctl0, ds->ds_ctl1,
4749 ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3],
4750 !done ? ' ' : (ds->ds_txstat.ts_status == 0) ? '*' : '!');
4751 }
4752 }
4753 #endif /* AR_DEBUG */
4754
4755 static void
4756 ath_watchdog(struct ifnet *ifp)
4757 {
4758 struct ath_softc *sc = ifp->if_softc;
4759 struct ieee80211com *ic = &sc->sc_ic;
4760
4761 ifp->if_timer = 0;
4762 if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
4763 return;
4764 if (sc->sc_tx_timer) {
4765 if (--sc->sc_tx_timer == 0) {
4766 if_printf(ifp, "device timeout\n");
4767 ath_reset(ifp);
4768 ifp->if_oerrors++;
4769 sc->sc_stats.ast_watchdog++;
4770 } else
4771 ifp->if_timer = 1;
4772 }
4773 ieee80211_watchdog(ic);
4774 }
4775
4776 /*
4777 * Diagnostic interface to the HAL. This is used by various
4778 * tools to do things like retrieve register contents for
4779 * debugging. The mechanism is intentionally opaque so that
4780 * it can change frequently w/o concern for compatiblity.
4781 */
4782 static int
4783 ath_ioctl_diag(struct ath_softc *sc, struct ath_diag *ad)
4784 {
4785 struct ath_hal *ah = sc->sc_ah;
4786 u_int id = ad->ad_id & ATH_DIAG_ID;
4787 void *indata = NULL;
4788 void *outdata = NULL;
4789 u_int32_t insize = ad->ad_in_size;
4790 u_int32_t outsize = ad->ad_out_size;
4791 int error = 0;
4792
4793 if (ad->ad_id & ATH_DIAG_IN) {
4794 /*
4795 * Copy in data.
4796 */
4797 indata = malloc(insize, M_TEMP, M_NOWAIT);
4798 if (indata == NULL) {
4799 error = ENOMEM;
4800 goto bad;
4801 }
4802 error = copyin(ad->ad_in_data, indata, insize);
4803 if (error)
4804 goto bad;
4805 }
4806 if (ad->ad_id & ATH_DIAG_DYN) {
4807 /*
4808 * Allocate a buffer for the results (otherwise the HAL
4809 * returns a pointer to a buffer where we can read the
4810 * results). Note that we depend on the HAL leaving this
4811 * pointer for us to use below in reclaiming the buffer;
4812 * may want to be more defensive.
4813 */
4814 outdata = malloc(outsize, M_TEMP, M_NOWAIT);
4815 if (outdata == NULL) {
4816 error = ENOMEM;
4817 goto bad;
4818 }
4819 }
4820 if (ath_hal_getdiagstate(ah, id, indata, insize, &outdata, &outsize)) {
4821 if (outsize < ad->ad_out_size)
4822 ad->ad_out_size = outsize;
4823 if (outdata != NULL)
4824 error = copyout(outdata, ad->ad_out_data,
4825 ad->ad_out_size);
4826 } else {
4827 error = EINVAL;
4828 }
4829 bad:
4830 if ((ad->ad_id & ATH_DIAG_IN) && indata != NULL)
4831 free(indata, M_TEMP);
4832 if ((ad->ad_id & ATH_DIAG_DYN) && outdata != NULL)
4833 free(outdata, M_TEMP);
4834 return error;
4835 }
4836
4837 static int
4838 ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
4839 {
4840 #define IS_RUNNING(ifp) \
4841 ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) == (IFF_RUNNING|IFF_UP))
4842 struct ath_softc *sc = ifp->if_softc;
4843 struct ieee80211com *ic = &sc->sc_ic;
4844 struct ifreq *ifr = (struct ifreq *)data;
4845 int error = 0;
4846
4847 ATH_LOCK(sc);
4848 switch (cmd) {
4849 case SIOCSIFFLAGS:
4850 if (IS_RUNNING(ifp)) {
4851 /*
4852 * To avoid rescanning another access point,
4853 * do not call ath_init() here. Instead,
4854 * only reflect promisc mode settings.
4855 */
4856 ath_mode_init(sc);
4857 } else if (ifp->if_flags & IFF_UP) {
4858 /*
4859 * Beware of being called during attach/detach
4860 * to reset promiscuous mode. In that case we
4861 * will still be marked UP but not RUNNING.
4862 * However trying to re-init the interface
4863 * is the wrong thing to do as we've already
4864 * torn down much of our state. There's
4865 * probably a better way to deal with this.
4866 */
4867 if (!sc->sc_invalid && ic->ic_bss != NULL)
4868 ath_init(sc); /* XXX lose error */
4869 } else
4870 ath_stop_locked(ifp, 1);
4871 break;
4872 case SIOCADDMULTI:
4873 case SIOCDELMULTI:
4874 error = (cmd == SIOCADDMULTI) ?
4875 ether_addmulti(ifr, &sc->sc_ec) :
4876 ether_delmulti(ifr, &sc->sc_ec);
4877 if (error == ENETRESET) {
4878 if (ifp->if_flags & IFF_RUNNING)
4879 ath_mode_init(sc);
4880 error = 0;
4881 }
4882 break;
4883 case SIOCGATHSTATS:
4884 /* NB: embed these numbers to get a consistent view */
4885 sc->sc_stats.ast_tx_packets = ifp->if_opackets;
4886 sc->sc_stats.ast_rx_packets = ifp->if_ipackets;
4887 sc->sc_stats.ast_rx_rssi = ieee80211_getrssi(ic);
4888 ATH_UNLOCK(sc);
4889 /*
4890 * NB: Drop the softc lock in case of a page fault;
4891 * we'll accept any potential inconsisentcy in the
4892 * statistics. The alternative is to copy the data
4893 * to a local structure.
4894 */
4895 return copyout(&sc->sc_stats,
4896 ifr->ifr_data, sizeof (sc->sc_stats));
4897 case SIOCGATHDIAG:
4898 error = ath_ioctl_diag(sc, (struct ath_diag *) ifr);
4899 break;
4900 default:
4901 error = ieee80211_ioctl(ic, cmd, data);
4902 if (error == ENETRESET) {
4903 if (IS_RUNNING(ifp) &&
4904 ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
4905 ath_init(sc); /* XXX lose error */
4906 error = 0;
4907 }
4908 if (error == ERESTART)
4909 error = IS_RUNNING(ifp) ? ath_reset(ifp) : 0;
4910 break;
4911 }
4912 ATH_UNLOCK(sc);
4913 return error;
4914 #undef IS_RUNNING
4915 }
4916
4917 static void
4918 ath_bpfattach(struct ath_softc *sc)
4919 {
4920 struct ifnet *ifp = &sc->sc_if;
4921
4922 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
4923 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
4924 &sc->sc_drvbpf);
4925 /*
4926 * Initialize constant fields.
4927 * XXX make header lengths a multiple of 32-bits so subsequent
4928 * headers are properly aligned; this is a kludge to keep
4929 * certain applications happy.
4930 *
4931 * NB: the channel is setup each time we transition to the
4932 * RUN state to avoid filling it in for each frame.
4933 */
4934 sc->sc_tx_th_len = roundup(sizeof(sc->sc_tx_th), sizeof(u_int32_t));
4935 sc->sc_tx_th.wt_ihdr.it_len = htole16(sc->sc_tx_th_len);
4936 sc->sc_tx_th.wt_ihdr.it_present = htole32(ATH_TX_RADIOTAP_PRESENT);
4937
4938 sc->sc_rx_th_len = roundup(sizeof(sc->sc_rx_th), sizeof(u_int32_t));
4939 sc->sc_rx_th.wr_ihdr.it_len = htole16(sc->sc_rx_th_len);
4940 sc->sc_rx_th.wr_ihdr.it_present = htole32(ATH_RX_RADIOTAP_PRESENT);
4941 }
4942
4943 /*
4944 * Announce various information on device/driver attach.
4945 */
4946 static void
4947 ath_announce(struct ath_softc *sc)
4948 {
4949 #define HAL_MODE_DUALBAND (HAL_MODE_11A|HAL_MODE_11B)
4950 struct ifnet *ifp = &sc->sc_if;
4951 struct ath_hal *ah = sc->sc_ah;
4952 u_int modes, cc;
4953
4954 if_printf(ifp, "mac %d.%d phy %d.%d",
4955 ah->ah_macVersion, ah->ah_macRev,
4956 ah->ah_phyRev >> 4, ah->ah_phyRev & 0xf);
4957 /*
4958 * Print radio revision(s). We check the wireless modes
4959 * to avoid falsely printing revs for inoperable parts.
4960 * Dual-band radio revs are returned in the 5Ghz rev number.
4961 */
4962 ath_hal_getcountrycode(ah, &cc);
4963 modes = ath_hal_getwirelessmodes(ah, cc);
4964 if ((modes & HAL_MODE_DUALBAND) == HAL_MODE_DUALBAND) {
4965 if (ah->ah_analog5GhzRev && ah->ah_analog2GhzRev)
4966 printf(" 5ghz radio %d.%d 2ghz radio %d.%d",
4967 ah->ah_analog5GhzRev >> 4,
4968 ah->ah_analog5GhzRev & 0xf,
4969 ah->ah_analog2GhzRev >> 4,
4970 ah->ah_analog2GhzRev & 0xf);
4971 else
4972 printf(" radio %d.%d", ah->ah_analog5GhzRev >> 4,
4973 ah->ah_analog5GhzRev & 0xf);
4974 } else
4975 printf(" radio %d.%d", ah->ah_analog5GhzRev >> 4,
4976 ah->ah_analog5GhzRev & 0xf);
4977 printf("\n");
4978 if (bootverbose) {
4979 int i;
4980 for (i = 0; i <= WME_AC_VO; i++) {
4981 struct ath_txq *txq = sc->sc_ac2q[i];
4982 if_printf(ifp, "Use hw queue %u for %s traffic\n",
4983 txq->axq_qnum, ieee80211_wme_acnames[i]);
4984 }
4985 if_printf(ifp, "Use hw queue %u for CAB traffic\n",
4986 sc->sc_cabq->axq_qnum);
4987 if_printf(ifp, "Use hw queue %u for beacons\n", sc->sc_bhalq);
4988 }
4989 #undef HAL_MODE_DUALBAND
4990 }
4991