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