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