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