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