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