ath.c revision 1.10 1 /* $NetBSD: ath.c,v 1.10 2003/10/16 07:55:18 ichiro Exp $ */
2
3 /*-
4 * Copyright (c) 2002, 2003 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.14 2003/09/05 22:22:49 sam Exp $");
42 #endif
43 #ifdef __NetBSD__
44 __KERNEL_RCSID(0, "$NetBSD: ath.c,v 1.10 2003/10/16 07:55:18 ichiro Exp $");
45 #endif
46
47 /*
48 * Driver for the Atheros Wireless LAN controller.
49 *
50 * This software is derived from work of Atsushi Onoe; his contribution
51 * is greatly appreciated.
52 */
53
54 #include "opt_inet.h"
55
56 #ifdef __NetBSD__
57 #include "bpfilter.h"
58 #endif /* __NetBSD__ */
59
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/types.h>
63 #include <sys/sysctl.h>
64 #include <sys/mbuf.h>
65 #include <sys/malloc.h>
66 #include <sys/lock.h>
67 #ifdef __FreeBSD__
68 #include <sys/mutex.h>
69 #endif
70 #include <sys/kernel.h>
71 #include <sys/socket.h>
72 #include <sys/sockio.h>
73 #include <sys/errno.h>
74 #include <sys/callout.h>
75 #ifdef __FreeBSD__
76 #include <sys/bus.h>
77 #else
78 #include <machine/bus.h>
79 #endif
80 #include <sys/endian.h>
81
82 #include <machine/bus.h>
83
84 #include <net/if.h>
85 #include <net/if_dl.h>
86 #include <net/if_media.h>
87 #include <net/if_arp.h>
88 #ifdef __FreeBSD__
89 #include <net/ethernet.h>
90 #else
91 #include <net/if_ether.h>
92 #endif
93 #include <net/if_llc.h>
94
95 #include <net80211/ieee80211_var.h>
96 #include <net80211/ieee80211_compat.h>
97
98 #if NBPFILTER > 0
99 #include <net/bpf.h>
100 #endif
101
102 #ifdef INET
103 #include <netinet/in.h>
104 #endif
105
106 #include <dev/ic/athcompat.h>
107
108 #define AR_DEBUG
109 #ifdef __FreeBSD__
110 #include <dev/ath/if_athvar.h>
111 #include <contrib/dev/ath/ah_desc.h>
112 #else
113 #include <dev/ic/athvar.h>
114 #include <../contrib/sys/dev/ic/athhal_desc.h>
115 #endif
116
117 /* unalligned little endian access */
118 #define LE_READ_2(p) \
119 ((u_int16_t) \
120 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8)))
121 #define LE_READ_4(p) \
122 ((u_int32_t) \
123 ((((u_int8_t *)(p))[0] ) | (((u_int8_t *)(p))[1] << 8) | \
124 (((u_int8_t *)(p))[2] << 16) | (((u_int8_t *)(p))[3] << 24)))
125
126 #ifdef __FreeBSD__
127 static void ath_init(void *);
128 #else
129 static int ath_init(struct ifnet *);
130 #endif
131 static int ath_init1(struct ath_softc *);
132 static int ath_intr1(struct ath_softc *);
133 static void ath_stop(struct ifnet *);
134 static void ath_start(struct ifnet *);
135 static void ath_reset(struct ath_softc *);
136 static int ath_media_change(struct ifnet *);
137 static void ath_watchdog(struct ifnet *);
138 static int ath_ioctl(struct ifnet *, u_long, caddr_t);
139 static void ath_fatal_proc(void *, int);
140 static void ath_rxorn_proc(void *, int);
141 static void ath_bmiss_proc(void *, int);
142 static void ath_initkeytable(struct ath_softc *);
143 static void ath_mode_init(struct ath_softc *);
144 static int ath_beacon_alloc(struct ath_softc *, struct ieee80211_node *);
145 static void ath_beacon_proc(void *, int);
146 static void ath_beacon_free(struct ath_softc *);
147 static void ath_beacon_config(struct ath_softc *);
148 static int ath_desc_alloc(struct ath_softc *);
149 static void ath_desc_free(struct ath_softc *);
150 static struct ieee80211_node *ath_node_alloc(struct ieee80211com *);
151 static void ath_node_free(struct ieee80211com *, struct ieee80211_node *);
152 static void ath_node_copy(struct ieee80211com *,
153 struct ieee80211_node *, const struct ieee80211_node *);
154 static int ath_rxbuf_init(struct ath_softc *, struct ath_buf *);
155 static void ath_rx_proc(void *, int);
156 static int ath_tx_start(struct ath_softc *, struct ieee80211_node *,
157 struct ath_buf *, struct mbuf *);
158 static void ath_tx_proc(void *, int);
159 static int ath_chan_set(struct ath_softc *, struct ieee80211_channel *);
160 static void ath_draintxq(struct ath_softc *);
161 static void ath_stoprecv(struct ath_softc *);
162 static int ath_startrecv(struct ath_softc *);
163 static void ath_next_scan(void *);
164 static void ath_calibrate(void *);
165 static int ath_newstate(struct ieee80211com *, enum ieee80211_state, int);
166 static void ath_newassoc(struct ieee80211com *,
167 struct ieee80211_node *, int);
168 static int ath_getchannels(struct ath_softc *, u_int cc, HAL_BOOL outdoor);
169
170 static int ath_rate_setup(struct ath_softc *sc, u_int mode);
171 static void ath_setcurmode(struct ath_softc *, enum ieee80211_phymode);
172 static void ath_rate_ctl_reset(struct ath_softc *, enum ieee80211_state);
173 static void ath_rate_ctl(void *, struct ieee80211_node *);
174
175 #ifdef __NetBSD__
176 int ath_enable(struct ath_softc *);
177 void ath_disable(struct ath_softc *);
178 void ath_power(int, void *);
179 #endif
180
181 #ifdef __FreeBSD__
182 SYSCTL_DECL(_hw_ath);
183 /* XXX validate sysctl values */
184 SYSCTL_INT(_hw_ath, OID_AUTO, dwell, CTLFLAG_RW, &ath_dwelltime,
185 0, "channel dwell time (ms) for AP/station scanning");
186 SYSCTL_INT(_hw_ath, OID_AUTO, calibrate, CTLFLAG_RW, &ath_calinterval,
187 0, "chip calibration interval (secs)");
188 SYSCTL_INT(_hw_ath, OID_AUTO, outdoor, CTLFLAG_RD, &ath_outdoor,
189 0, "enable/disable outdoor operation");
190 SYSCTL_INT(_hw_ath, OID_AUTO, countrycode, CTLFLAG_RD, &ath_countrycode,
191 0, "country code");
192 SYSCTL_INT(_hw_ath, OID_AUTO, regdomain, CTLFLAG_RD, &ath_regdomain,
193 0, "regulatory domain");
194 #endif /* __FreeBSD__ */
195
196 static int ath_dwelltime = 200; /* 5 channels/second */
197 static int ath_calinterval = 30; /* calibrate every 30 secs */
198 static int ath_outdoor = AH_TRUE; /* outdoor operation */
199 static int ath_countrycode = CTRY_DEFAULT; /* country code */
200 static int ath_regdomain = 0; /* regulatory domain */
201
202 #ifdef AR_DEBUG
203 int ath_debug = 0;
204 #ifdef __FreeBSD__
205 SYSCTL_INT(_hw_ath, OID_AUTO, debug, CTLFLAG_RW, &ath_debug,
206 0, "control debugging printfs");
207 #endif /* __FreeBSD__ */
208 #define IFF_DUMPPKTS(_ifp) \
209 (ath_debug || \
210 ((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
211 static void ath_printrxbuf(struct ath_buf *bf, int);
212 static void ath_printtxbuf(struct ath_buf *bf, int);
213 #define DPRINTF(X) if (ath_debug) printf X
214 #define DPRINTF2(X) if (ath_debug > 1) printf X
215 #else
216 #define IFF_DUMPPKTS(_ifp) \
217 (((_ifp)->if_flags & (IFF_DEBUG|IFF_LINK2)) == (IFF_DEBUG|IFF_LINK2))
218 #define DPRINTF(X)
219 #define DPRINTF2(X)
220 #endif
221
222 #ifdef __NetBSD__
223 int
224 ath_activate(struct device *self, enum devact act)
225 {
226 struct ath_softc *sc = (struct ath_softc *)self;
227 int rv = 0, s;
228
229 s = splnet();
230 switch (act) {
231 case DVACT_ACTIVATE:
232 rv = EOPNOTSUPP;
233 break;
234 case DVACT_DEACTIVATE:
235 if_deactivate(&sc->sc_ic.ic_if);
236 break;
237 }
238 splx(s);
239 return rv;
240 }
241
242 int
243 ath_enable(struct ath_softc *sc)
244 {
245 if (ATH_IS_ENABLED(sc) == 0) {
246 if (sc->sc_enable != NULL && (*sc->sc_enable)(sc) != 0) {
247 printf("%s: device enable failed\n",
248 sc->sc_dev.dv_xname);
249 return (EIO);
250 }
251 sc->sc_flags |= ATH_ENABLED;
252 }
253 return (0);
254 }
255
256 void
257 ath_disable(struct ath_softc *sc)
258 {
259 if (!ATH_IS_ENABLED(sc))
260 return;
261 if (sc->sc_disable != NULL)
262 (*sc->sc_disable)(sc);
263 sc->sc_flags &= ~ATH_ENABLED;
264 }
265 #endif /* #ifdef __NetBSD__ */
266
267 int
268 ath_attach(u_int16_t devid, struct ath_softc *sc)
269 {
270 struct ieee80211com *ic = &sc->sc_ic;
271 struct ifnet *ifp = &ic->ic_if;
272 struct ath_hal *ah;
273 HAL_STATUS status;
274 int error = 0;
275
276 DPRINTF(("ath_attach: devid 0x%x\n", devid));
277
278 #ifdef __FreeBSD__
279 /* set these up early for if_printf use */
280 ifp->if_unit = device_get_unit(sc->sc_dev);
281 ifp->if_name = "ath";
282 #else
283 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
284 #endif
285
286 ah = ath_hal_attach(devid, sc, sc->sc_st, sc->sc_sh, &status);
287 if (ah == NULL) {
288 if_printf(ifp, "unable to attach hardware; HAL status %u\n",
289 status);
290 error = ENXIO;
291 goto bad;
292 }
293 sc->sc_ah = ah;
294 sc->sc_invalid = 0; /* ready to go, enable interrupt handling */
295
296 /*
297 * Collect the channel list using the default country
298 * code and including outdoor channels. The 802.11 layer
299 * is resposible for filtering this list based on settings
300 * like the phy mode.
301 */
302 error = ath_getchannels(sc, ath_countrycode, ath_outdoor);
303 if (error != 0)
304 goto bad;
305 /*
306 * Copy these back; they are set as a side effect
307 * of constructing the channel list.
308 */
309 ath_regdomain = ath_hal_getregdomain(ah);
310 ath_countrycode = ath_hal_getcountrycode(ah);
311
312 /*
313 * Setup rate tables for all potential media types.
314 */
315 ath_rate_setup(sc, IEEE80211_MODE_11A);
316 ath_rate_setup(sc, IEEE80211_MODE_11B);
317 ath_rate_setup(sc, IEEE80211_MODE_11G);
318 ath_rate_setup(sc, IEEE80211_MODE_TURBO);
319
320 error = ath_desc_alloc(sc);
321 if (error != 0) {
322 if_printf(ifp, "failed to allocate descriptors: %d\n", error);
323 goto bad;
324 }
325 ATH_CALLOUT_INIT(&sc->sc_scan_ch);
326 ATH_CALLOUT_INIT(&sc->sc_cal_ch);
327
328 #ifdef __FreeBSD__
329 mtx_init(&sc->sc_txbuflock,
330 device_get_nameunit(sc->sc_dev), "xmit buf q", MTX_DEF);
331 mtx_init(&sc->sc_txqlock,
332 device_get_nameunit(sc->sc_dev), "xmit q", MTX_DEF);
333 #endif
334
335 ATH_TASK_INIT(&sc->sc_txtask, ath_tx_proc, sc);
336 ATH_TASK_INIT(&sc->sc_rxtask, ath_rx_proc, sc);
337 ATH_TASK_INIT(&sc->sc_swbatask, ath_beacon_proc, sc);
338 ATH_TASK_INIT(&sc->sc_rxorntask, ath_rxorn_proc, sc);
339 ATH_TASK_INIT(&sc->sc_fataltask, ath_fatal_proc, sc);
340 ATH_TASK_INIT(&sc->sc_bmisstask, ath_bmiss_proc, sc);
341
342 /*
343 * For now just pre-allocate one data queue and one
344 * beacon queue. Note that the HAL handles resetting
345 * them at the needed time. Eventually we'll want to
346 * allocate more tx queues for splitting management
347 * frames and for QOS support.
348 */
349 sc->sc_txhalq = ath_hal_setuptxqueue(ah,
350 HAL_TX_QUEUE_DATA,
351 AH_TRUE /* enable interrupts */
352 );
353 if (sc->sc_txhalq == (u_int) -1) {
354 if_printf(ifp, "unable to setup a data xmit queue!\n");
355 goto bad;
356 }
357 sc->sc_bhalq = ath_hal_setuptxqueue(ah,
358 HAL_TX_QUEUE_BEACON,
359 AH_TRUE /* enable interrupts */
360 );
361 if (sc->sc_bhalq == (u_int) -1) {
362 if_printf(ifp, "unable to setup a beacon xmit queue!\n");
363 goto bad;
364 }
365
366 ifp->if_softc = sc;
367 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
368 ifp->if_start = ath_start;
369 ifp->if_watchdog = ath_watchdog;
370 ifp->if_ioctl = ath_ioctl;
371 ifp->if_init = ath_init;
372 #ifdef __FreeBSD__
373 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
374 #else
375 #if 0
376 ifp->if_stop = ath_stop; /* XXX */
377 #endif
378 IFQ_SET_READY(&ifp->if_snd);
379 #endif
380
381 ic->ic_softc = sc;
382 ic->ic_newassoc = ath_newassoc;
383 /* XXX not right but it's not used anywhere important */
384 ic->ic_phytype = IEEE80211_T_OFDM;
385 ic->ic_opmode = IEEE80211_M_STA;
386 ic->ic_caps = IEEE80211_C_WEP | IEEE80211_C_IBSS | IEEE80211_C_HOSTAP
387 | IEEE80211_C_MONITOR;
388 /* NB: 11g support is identified when we fetch the channel set */
389 if (sc->sc_have11g)
390 ic->ic_caps |= IEEE80211_C_SHPREAMBLE;
391
392 /* get mac address from hardware */
393 ath_hal_getmac(ah, ic->ic_myaddr);
394
395 #ifdef __NetBSD__
396 if_attach(ifp);
397 #endif
398 /* call MI attach routine. */
399 ieee80211_ifattach(ifp);
400 /* override default methods */
401 ic->ic_node_alloc = ath_node_alloc;
402 ic->ic_node_free = ath_node_free;
403 ic->ic_node_copy = ath_node_copy;
404 sc->sc_newstate = ic->ic_newstate;
405 ic->ic_newstate = ath_newstate;
406 /* complete initialization */
407 ieee80211_media_init(ifp, ath_media_change, ieee80211_media_status);
408
409 #if NBPFILTER > 0
410 bpfattach2(ifp, DLT_IEEE802_11_RADIO,
411 sizeof(struct ieee80211_frame) + sizeof(sc->sc_tx_th),
412 &sc->sc_drvbpf);
413 #endif
414 /*
415 * Initialize constant fields.
416 *
417 * NB: the channel is setup each time we transition to the
418 * RUN state to avoid filling it in for each frame.
419 */
420 sc->sc_tx_th.wt_ihdr.it_len = sizeof(sc->sc_tx_th);
421 sc->sc_tx_th.wt_ihdr.it_present = ATH_TX_RADIOTAP_PRESENT;
422
423 sc->sc_rx_th.wr_ihdr.it_len = sizeof(sc->sc_rx_th);
424 sc->sc_rx_th.wr_ihdr.it_present = ATH_RX_RADIOTAP_PRESENT;
425
426 if_printf(ifp, "802.11 address: %s\n", ether_sprintf(ic->ic_myaddr));
427
428 #ifdef __NetBSD__
429 sc->sc_flags |= ATH_ATTACHED;
430 /*
431 * Make sure the interface is shutdown during reboot.
432 */
433 sc->sc_sdhook = shutdownhook_establish(ath_shutdown, sc);
434 if (sc->sc_sdhook == NULL)
435 printf("%s: WARNING: unable to establish shutdown hook\n",
436 sc->sc_dev.dv_xname);
437 sc->sc_powerhook = powerhook_establish(ath_power, sc);
438 if (sc->sc_powerhook == NULL)
439 printf("%s: WARNING: unable to establish power hook\n",
440 sc->sc_dev.dv_xname);
441 #endif
442 return 0;
443 bad:
444 if (ah)
445 ath_hal_detach(ah);
446 sc->sc_invalid = 1;
447 return error;
448 }
449
450 int
451 ath_detach(struct ath_softc *sc)
452 {
453 struct ifnet *ifp = &sc->sc_ic.ic_if;
454 ath_softc_critsect_decl(s);
455
456 DPRINTF(("ath_detach: if_flags %x\n", ifp->if_flags));
457 if ((sc->sc_flags & ATH_ATTACHED) == 0)
458 return (0);
459
460 ath_softc_critsect_begin(sc, s);
461 ath_stop(ifp);
462 #if NBPFILTER > 0
463 bpfdetach(ifp);
464 #endif
465 ath_desc_free(sc);
466 ath_hal_detach(sc->sc_ah);
467 ieee80211_ifdetach(ifp);
468 #ifdef __NetBSD__
469 if_detach(ifp);
470 #endif
471 ath_softc_critsect_end(sc, s);
472 #ifdef __NetBSD__
473 powerhook_disestablish(sc->sc_sdhook);
474 shutdownhook_disestablish(sc->sc_powerhook);
475 #endif
476 return 0;
477 }
478
479 #ifdef __NetBSD__
480 void
481 ath_power(int why, void *arg)
482 {
483 struct ath_softc *sc = arg;
484 int s;
485
486 DPRINTF(("ath_power(%d)\n", why));
487
488 s = splnet();
489 switch (why) {
490 case PWR_SUSPEND:
491 case PWR_STANDBY:
492 ath_suspend(sc, why);
493 break;
494 case PWR_RESUME:
495 ath_resume(sc, why);
496 break;
497 case PWR_SOFTSUSPEND:
498 case PWR_SOFTSTANDBY:
499 case PWR_SOFTRESUME:
500 break;
501 }
502 splx(s);
503 }
504 #endif
505
506 void
507 ath_suspend(struct ath_softc *sc, int why)
508 {
509 struct ifnet *ifp = &sc->sc_ic.ic_if;
510
511 DPRINTF(("ath_suspend: if_flags %x\n", ifp->if_flags));
512
513 ath_stop(ifp);
514 if (sc->sc_power != NULL)
515 (*sc->sc_power)(sc, why);
516 }
517
518 void
519 ath_resume(struct ath_softc *sc, int why)
520 {
521 struct ifnet *ifp = &sc->sc_ic.ic_if;
522
523 DPRINTF(("ath_resume: if_flags %x\n", ifp->if_flags));
524
525 if (ifp->if_flags & IFF_UP) {
526 ath_init(ifp);
527 #if 0
528 (void)ath_intr(sc);
529 #endif
530 if (sc->sc_power != NULL)
531 (*sc->sc_power)(sc, why);
532 if (ifp->if_flags & IFF_RUNNING)
533 ath_start(ifp);
534 }
535 }
536
537 #ifdef __NetBSD__
538 void
539 ath_shutdown(void *arg)
540 {
541 struct ath_softc *sc = arg;
542
543 ath_stop(&sc->sc_ic.ic_if);
544 }
545 #else
546 void
547 ath_shutdown(struct ath_softc *sc)
548 {
549 #if 1
550 return;
551 #else
552 struct ifnet *ifp = &sc->sc_ic.ic_if;
553
554 DPRINTF(("ath_shutdown: if_flags %x\n", ifp->if_flags));
555
556 ath_stop(ifp);
557 #endif
558 }
559 #endif
560
561 #ifdef __NetBSD__
562 int
563 ath_intr(void *arg)
564 {
565 return ath_intr1((struct ath_softc *)arg);
566 }
567 #else
568 void
569 ath_intr(void *arg)
570 {
571 (void)ath_intr1((struct ath_softc *)arg);
572 }
573 #endif
574
575 static int
576 ath_intr1(struct ath_softc *sc)
577 {
578 struct ieee80211com *ic = &sc->sc_ic;
579 struct ifnet *ifp = &ic->ic_if;
580 struct ath_hal *ah = sc->sc_ah;
581 HAL_INT status;
582
583 if (sc->sc_invalid) {
584 /*
585 * The hardware is not ready/present, don't touch anything.
586 * Note this can happen early on if the IRQ is shared.
587 */
588 DPRINTF(("ath_intr: invalid; ignored\n"));
589 return 0;
590 }
591 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) != (IFF_RUNNING|IFF_UP)) {
592 DPRINTF(("ath_intr: if_flags 0x%x\n", ifp->if_flags));
593 ath_hal_getisr(ah, &status); /* clear ISR */
594 ath_hal_intrset(ah, 0); /* disable further intr's */
595 return 1; /* XXX */
596 }
597 ath_hal_getisr(ah, &status); /* NB: clears ISR too */
598 DPRINTF2(("ath_intr: status 0x%x\n", status));
599 #ifdef AR_DEBUG
600 if (ath_debug &&
601 (status & (HAL_INT_FATAL|HAL_INT_RXORN|HAL_INT_BMISS))) {
602 if_printf(ifp, "ath_intr: status 0x%x\n", status);
603 ath_hal_dumpstate(ah);
604 }
605 #endif /* AR_DEBUG */
606 if (status & HAL_INT_FATAL) {
607 sc->sc_stats.ast_hardware++;
608 ath_hal_intrset(ah, 0); /* disable intr's until reset */
609 ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_fataltask);
610 } else if (status & HAL_INT_RXORN) {
611 sc->sc_stats.ast_rxorn++;
612 ath_hal_intrset(ah, 0); /* disable intr's until reset */
613 ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_rxorntask);
614 } else {
615 if (status & HAL_INT_RXEOL) {
616 /*
617 * NB: the hardware should re-read the link when
618 * RXE bit is written, but it doesn't work at
619 * least on older hardware revs.
620 */
621 sc->sc_stats.ast_rxeol++;
622 sc->sc_rxlink = NULL;
623 }
624 if (status & HAL_INT_TXURN) {
625 sc->sc_stats.ast_txurn++;
626 /* bump tx trigger level */
627 ath_hal_updatetxtriglevel(ah, AH_TRUE);
628 }
629 if (status & HAL_INT_RX)
630 ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_rxtask);
631 if (status & HAL_INT_TX)
632 ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_txtask);
633 if (status & HAL_INT_SWBA)
634 ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_swbatask);
635 if (status & HAL_INT_BMISS) {
636 sc->sc_stats.ast_bmiss++;
637 ATH_TASK_RUN_OR_ENQUEUE(&sc->sc_bmisstask);
638 }
639 }
640 return 1;
641 }
642
643 static void
644 ath_fatal_proc(void *arg, int pending)
645 {
646 struct ath_softc *sc = arg;
647
648 device_printf(sc->sc_dev, "hardware error; resetting\n");
649 ath_reset(sc);
650 }
651
652 static void
653 ath_rxorn_proc(void *arg, int pending)
654 {
655 struct ath_softc *sc = arg;
656
657 device_printf(sc->sc_dev, "rx FIFO overrun; resetting\n");
658 ath_reset(sc);
659 }
660
661 static void
662 ath_bmiss_proc(void *arg, int pending)
663 {
664 struct ath_softc *sc = arg;
665 struct ieee80211com *ic = &sc->sc_ic;
666
667 DPRINTF(("ath_bmiss_proc: pending %u\n", pending));
668 KASSERT(ic->ic_opmode == IEEE80211_M_STA,
669 ("unexpect operating mode %u", ic->ic_opmode));
670 if (ic->ic_state == IEEE80211_S_RUN)
671 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
672 }
673
674 static u_int
675 ath_chan2flags(struct ieee80211com *ic, struct ieee80211_channel *chan)
676 {
677 enum ieee80211_phymode mode = ieee80211_chan2mode(ic, chan);
678
679 switch (mode) {
680 case IEEE80211_MODE_AUTO:
681 return 0;
682 case IEEE80211_MODE_11A:
683 return CHANNEL_A;
684 case IEEE80211_MODE_11B:
685 return CHANNEL_B;
686 case IEEE80211_MODE_11G:
687 return CHANNEL_PUREG;
688 case IEEE80211_MODE_TURBO:
689 return CHANNEL_T;
690 default:
691 panic("%s: unsupported mode %d\n", __func__, mode);
692 return 0;
693 }
694 }
695
696 #ifdef __NetBSD__
697 static int
698 ath_init(struct ifnet *ifp)
699 {
700 return ath_init1((struct ath_softc *)ifp->if_softc);
701 }
702 #else
703 static void
704 ath_init(void *arg)
705 {
706 (void)ath_init1((struct ath_softc *)arg);
707 }
708 #endif
709
710 static int
711 ath_init1(struct ath_softc *sc)
712 {
713 struct ieee80211com *ic = &sc->sc_ic;
714 struct ifnet *ifp = &ic->ic_if;
715 struct ieee80211_node *ni;
716 enum ieee80211_phymode mode;
717 struct ath_hal *ah = sc->sc_ah;
718 HAL_STATUS status;
719 HAL_CHANNEL hchan;
720 int error = 0;
721 ath_softc_critsect_decl(s);
722
723 DPRINTF(("ath_init: if_flags 0x%x\n", ifp->if_flags));
724
725 #ifdef __NetBSD__
726 if ((error = ath_enable(sc)) != 0)
727 return error;
728 #endif
729
730 ath_softc_critsect_begin(sc, s);
731 /*
732 * Stop anything previously setup. This is safe
733 * whether this is the first time through or not.
734 */
735 ath_stop(ifp);
736
737 /*
738 * The basic interface to setting the hardware in a good
739 * state is ``reset''. On return the hardware is known to
740 * be powered up and with interrupts disabled. This must
741 * be followed by initialization of the appropriate bits
742 * and then setup of the interrupt mask.
743 */
744 hchan.channel = ic->ic_ibss_chan->ic_freq;
745 hchan.channelFlags = ath_chan2flags(ic, ic->ic_ibss_chan);
746 if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_FALSE, &status)) {
747 if_printf(ifp, "unable to reset hardware; hal status %u\n",
748 status);
749 error = -1;
750 goto done;
751 }
752
753 /*
754 * Setup the hardware after reset: the key cache
755 * is filled as needed and the receive engine is
756 * set going. Frame transmit is handled entirely
757 * in the frame output path; there's nothing to do
758 * here except setup the interrupt mask.
759 */
760 if (ic->ic_flags & IEEE80211_F_WEPON)
761 ath_initkeytable(sc);
762 if ((error = ath_startrecv(sc)) != 0) {
763 if_printf(ifp, "unable to start recv logic\n");
764 goto done;
765 }
766
767 /*
768 * Enable interrupts.
769 */
770 sc->sc_imask = HAL_INT_RX | HAL_INT_TX
771 | HAL_INT_RXEOL | HAL_INT_RXORN
772 | HAL_INT_FATAL | HAL_INT_GLOBAL;
773 ath_hal_intrset(ah, sc->sc_imask);
774
775 ifp->if_flags |= IFF_RUNNING;
776 ic->ic_state = IEEE80211_S_INIT;
777
778 /*
779 * The hardware should be ready to go now so it's safe
780 * to kick the 802.11 state machine as it's likely to
781 * immediately call back to us to send mgmt frames.
782 */
783 ni = ic->ic_bss;
784 ni->ni_chan = ic->ic_ibss_chan;
785 mode = ieee80211_chan2mode(ic, ni->ni_chan);
786 if (mode != sc->sc_curmode)
787 ath_setcurmode(sc, mode);
788 if (ic->ic_opmode != IEEE80211_M_MONITOR)
789 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
790 else
791 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
792 done:
793 ath_softc_critsect_end(sc, s);
794 return error;
795 }
796
797 static void
798 ath_stop(struct ifnet *ifp)
799 {
800 struct ieee80211com *ic = (struct ieee80211com *) ifp;
801 struct ath_softc *sc = ifp->if_softc;
802 struct ath_hal *ah = sc->sc_ah;
803 ath_softc_critsect_decl(s);
804
805 DPRINTF(("ath_stop: invalid %u if_flags 0x%x\n",
806 sc->sc_invalid, ifp->if_flags));
807
808 ath_softc_critsect_begin(sc, s);
809 if (ifp->if_flags & IFF_RUNNING) {
810 /*
811 * Shutdown the hardware and driver:
812 * disable interrupts
813 * turn off timers
814 * clear transmit machinery
815 * clear receive machinery
816 * drain and release tx queues
817 * reclaim beacon resources
818 * reset 802.11 state machine
819 * power down hardware
820 *
821 * Note that some of this work is not possible if the
822 * hardware is gone (invalid).
823 */
824 ifp->if_flags &= ~IFF_RUNNING;
825 ifp->if_timer = 0;
826 if (!sc->sc_invalid)
827 ath_hal_intrset(ah, 0);
828 ath_draintxq(sc);
829 if (!sc->sc_invalid)
830 ath_stoprecv(sc);
831 else
832 sc->sc_rxlink = NULL;
833 #ifdef __FreeBSD__
834 IF_DRAIN(&ifp->if_snd);
835 #else
836 IF_PURGE(&ifp->if_snd);
837 #endif
838 ath_beacon_free(sc);
839 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
840 if (!sc->sc_invalid) {
841 ath_hal_setpower(ah, HAL_PM_FULL_SLEEP, 0);
842 }
843 #ifdef __NetBSD__
844 ath_disable(sc);
845 #endif
846 }
847 ath_softc_critsect_end(sc, s);
848 }
849
850 /*
851 * Reset the hardware w/o losing operational state. This is
852 * basically a more efficient way of doing ath_stop, ath_init,
853 * followed by state transitions to the current 802.11
854 * operational state. Used to recover from errors rx overrun
855 * and to reset the hardware when rf gain settings must be reset.
856 */
857 static void
858 ath_reset(struct ath_softc *sc)
859 {
860 struct ieee80211com *ic = &sc->sc_ic;
861 struct ifnet *ifp = &ic->ic_if;
862 struct ath_hal *ah = sc->sc_ah;
863 struct ieee80211_channel *c;
864 HAL_STATUS status;
865 HAL_CHANNEL hchan;
866
867 /*
868 * Convert to a HAL channel description with the flags
869 * constrained to reflect the current operating mode.
870 */
871 c = ic->ic_ibss_chan;
872 hchan.channel = c->ic_freq;
873 hchan.channelFlags = ath_chan2flags(ic, c);
874
875 ath_hal_intrset(ah, 0); /* disable interrupts */
876 ath_draintxq(sc); /* stop xmit side */
877 ath_stoprecv(sc); /* stop recv side */
878 /* NB: indicate channel change so we do a full reset */
879 if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status))
880 if_printf(ifp, "%s: unable to reset hardware; hal status %u\n",
881 __func__, status);
882 ath_hal_intrset(ah, sc->sc_imask);
883 if (ath_startrecv(sc) != 0) /* restart recv */
884 if_printf(ifp, "%s: unable to start recv logic\n", __func__);
885 ath_start(ifp); /* restart xmit */
886 if (ic->ic_state == IEEE80211_S_RUN)
887 ath_beacon_config(sc); /* restart beacons */
888 }
889
890 static void
891 ath_start(struct ifnet *ifp)
892 {
893 struct ath_softc *sc = ifp->if_softc;
894 struct ath_hal *ah = sc->sc_ah;
895 struct ieee80211com *ic = &sc->sc_ic;
896 struct ieee80211_node *ni;
897 struct ath_buf *bf;
898 struct mbuf *m;
899 struct ieee80211_frame *wh;
900 ath_txbuf_critsect_decl(s);
901
902 if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
903 return;
904 for (;;) {
905 /*
906 * Grab a TX buffer and associated resources.
907 */
908 ath_txbuf_critsect_begin(sc, s);
909 bf = TAILQ_FIRST(&sc->sc_txbuf);
910 if (bf != NULL)
911 TAILQ_REMOVE(&sc->sc_txbuf, bf, bf_list);
912 ath_txbuf_critsect_end(sc, s);
913 if (bf == NULL) {
914 DPRINTF(("ath_start: out of xmit buffers\n"));
915 sc->sc_stats.ast_tx_qstop++;
916 ifp->if_flags |= IFF_OACTIVE;
917 break;
918 }
919 /*
920 * Poll the management queue for frames; they
921 * have priority over normal data frames.
922 */
923 IF_DEQUEUE(&ic->ic_mgtq, m);
924 if (m == NULL) {
925 /*
926 * No data frames go out unless we're associated.
927 */
928 if (ic->ic_state != IEEE80211_S_RUN) {
929 DPRINTF(("ath_start: ignore data packet, "
930 "state %u\n", ic->ic_state));
931 sc->sc_stats.ast_tx_discard++;
932 ath_txbuf_critsect_begin(sc, s);
933 TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
934 ath_txbuf_critsect_end(sc, s);
935 break;
936 }
937 IF_DEQUEUE(&ifp->if_snd, m);
938 if (m == NULL) {
939 ath_txbuf_critsect_begin(sc, s);
940 TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
941 ath_txbuf_critsect_end(sc, s);
942 break;
943 }
944 ifp->if_opackets++;
945
946 #ifdef __NetBSD__
947 #if NBPFILTER > 0
948 if (ifp->if_bpf)
949 bpf_mtap(ifp->if_bpf, m);
950 #endif
951 #endif
952 #ifdef __FreeBSD__
953 BPF_MTAP(ifp, m);
954 #endif
955 /*
956 * Encapsulate the packet in prep for transmission.
957 */
958 m = ieee80211_encap(ifp, m, &ni);
959 if (m == NULL) {
960 DPRINTF(("ath_start: encapsulation failure\n"));
961 sc->sc_stats.ast_tx_encap++;
962 goto bad;
963 }
964 wh = mtod(m, struct ieee80211_frame *);
965 if (ic->ic_flags & IEEE80211_F_WEPON)
966 wh->i_fc[1] |= IEEE80211_FC1_WEP;
967 } else {
968 /*
969 * Hack! The referenced node pointer is in the
970 * rcvif field of the packet header. This is
971 * placed there by ieee80211_mgmt_output because
972 * we need to hold the reference with the frame
973 * and there's no other way (other than packet
974 * tags which we consider too expensive to use)
975 * to pass it along.
976 */
977 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
978 m->m_pkthdr.rcvif = NULL;
979
980 wh = mtod(m, struct ieee80211_frame *);
981 if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
982 IEEE80211_FC0_SUBTYPE_PROBE_RESP) {
983 /* fill time stamp */
984 u_int64_t tsf;
985 u_int32_t *tstamp;
986
987 tsf = ath_hal_gettsf64(ah);
988 /* XXX: adjust 100us delay to xmit */
989 tsf += 100;
990 tstamp = (u_int32_t *)&wh[1];
991 tstamp[0] = htole32(tsf & 0xffffffff);
992 tstamp[1] = htole32(tsf >> 32);
993 }
994 sc->sc_stats.ast_tx_mgmt++;
995 }
996 #if NBPFILTER > 0
997 if (ic->ic_rawbpf)
998 bpf_mtap(ic->ic_rawbpf, m);
999 #endif
1000
1001 #if NBPFILTER > 0
1002 if (sc->sc_drvbpf) {
1003 #ifdef __FreeBSD__
1004 struct mbuf *mb;
1005
1006 MGETHDR(mb, M_DONTWAIT, m->m_type);
1007 if (mb != NULL) {
1008 sc->sc_tx_th.wt_rate =
1009 ni->ni_rates.rs_rates[ni->ni_txrate];
1010
1011 mb->m_next = m;
1012 mb->m_data = (caddr_t)&sc->sc_tx_th;
1013 mb->m_len = sizeof(sc->sc_tx_th);
1014 mb->m_pkthdr.len += mb->m_len;
1015 bpf_mtap(sc->sc_drvbpf, mb);
1016 m_free(mb);
1017 }
1018 #else
1019 struct mbuf mb;
1020
1021 M_COPY_PKTHDR(&mb, m);
1022 sc->sc_tx_th.wt_rate =
1023 ni->ni_rates.rs_rates[ni->ni_txrate];
1024
1025 mb.m_next = m;
1026 mb.m_data = (caddr_t)&sc->sc_tx_th;
1027 mb.m_len = sizeof(sc->sc_tx_th);
1028 mb.m_pkthdr.len += mb.m_len;
1029 bpf_mtap(sc->sc_drvbpf, &mb);
1030 #endif
1031 }
1032 #endif
1033
1034 /*
1035 * TODO:
1036 * The duration field of 802.11 header should be filled.
1037 * XXX This may be done in the ieee80211 layer, but the upper
1038 * doesn't know the detail of parameters such as IFS
1039 * for now..
1040 */
1041 if (ath_tx_start(sc, ni, bf, m)) {
1042 bad:
1043 ath_txbuf_critsect_begin(sc, s);
1044 TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
1045 ath_txbuf_critsect_end(sc, s);
1046 ifp->if_oerrors++;
1047 if (ni && ni != ic->ic_bss)
1048 ieee80211_free_node(ic, ni);
1049 continue;
1050 }
1051
1052 sc->sc_tx_timer = 5;
1053 ifp->if_timer = 1;
1054 }
1055 }
1056
1057 static int
1058 ath_media_change(struct ifnet *ifp)
1059 {
1060 int error;
1061
1062 error = ieee80211_media_change(ifp);
1063 if (error == ENETRESET) {
1064 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
1065 (IFF_RUNNING|IFF_UP))
1066 ath_init(ifp); /* XXX lose error */
1067 error = 0;
1068 }
1069 return error;
1070 }
1071
1072 static void
1073 ath_watchdog(struct ifnet *ifp)
1074 {
1075 struct ath_softc *sc = ifp->if_softc;
1076 struct ieee80211com *ic = &sc->sc_ic;
1077
1078 ifp->if_timer = 0;
1079 if ((ifp->if_flags & IFF_RUNNING) == 0 || sc->sc_invalid)
1080 return;
1081 if (sc->sc_tx_timer) {
1082 if (--sc->sc_tx_timer == 0) {
1083 if_printf(ifp, "device timeout\n");
1084 #ifdef AR_DEBUG
1085 if (ath_debug)
1086 ath_hal_dumpstate(sc->sc_ah);
1087 #endif /* AR_DEBUG */
1088 ath_init(ifp); /* XXX ath_reset??? */
1089 ifp->if_oerrors++;
1090 sc->sc_stats.ast_watchdog++;
1091 return;
1092 }
1093 ifp->if_timer = 1;
1094 }
1095 if (ic->ic_fixed_rate == -1) {
1096 /*
1097 * Run the rate control algorithm if we're not
1098 * locked at a fixed rate.
1099 */
1100 if (ic->ic_opmode == IEEE80211_M_STA)
1101 ath_rate_ctl(sc, ic->ic_bss);
1102 else
1103 ieee80211_iterate_nodes(ic, ath_rate_ctl, sc);
1104 }
1105 ieee80211_watchdog(ifp);
1106 }
1107
1108 static int
1109 ath_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1110 {
1111 struct ath_softc *sc = ifp->if_softc;
1112 struct ifreq *ifr = (struct ifreq *)data;
1113 int error = 0;
1114 ath_softc_critsect_decl(s);
1115
1116 ath_softc_critsect_begin(sc, s);
1117 switch (cmd) {
1118 case SIOCSIFFLAGS:
1119 if (ifp->if_flags & IFF_UP) {
1120 if (ifp->if_flags & IFF_RUNNING) {
1121 /*
1122 * To avoid rescanning another access point,
1123 * do not call ath_init() here. Instead,
1124 * only reflect promisc mode settings.
1125 */
1126 ath_mode_init(sc);
1127 } else
1128 ath_init(ifp); /* XXX lose error */
1129 } else
1130 ath_stop(ifp);
1131 break;
1132 case SIOCADDMULTI:
1133 case SIOCDELMULTI:
1134 #ifdef __FreeBSD__
1135 /*
1136 * The upper layer has already installed/removed
1137 * the multicast address(es), just recalculate the
1138 * multicast filter for the card.
1139 */
1140 if (ifp->if_flags & IFF_RUNNING)
1141 ath_mode_init(sc);
1142 #endif
1143 #ifdef __NetBSD__
1144 error = (cmd == SIOCADDMULTI) ?
1145 ether_addmulti(ifr, &sc->sc_ic.ic_ec) :
1146 ether_delmulti(ifr, &sc->sc_ic.ic_ec);
1147 if (error == ENETRESET) {
1148 if (ifp->if_flags & IFF_RUNNING)
1149 ath_mode_init(sc);
1150 error = 0;
1151 }
1152 #endif
1153 break;
1154 case SIOCGATHSTATS:
1155 copyout(&sc->sc_stats, ifr->ifr_data, sizeof (sc->sc_stats));
1156 break;
1157 default:
1158 error = ieee80211_ioctl(ifp, cmd, data);
1159 if (error == ENETRESET) {
1160 if ((ifp->if_flags & (IFF_RUNNING|IFF_UP)) ==
1161 (IFF_RUNNING|IFF_UP))
1162 ath_init(ifp); /* XXX lose error */
1163 error = 0;
1164 }
1165 break;
1166 }
1167 ath_softc_critsect_end(sc, s);
1168 return error;
1169 }
1170
1171 /*
1172 * Fill the hardware key cache with key entries.
1173 */
1174 static void
1175 ath_initkeytable(struct ath_softc *sc)
1176 {
1177 struct ieee80211com *ic = &sc->sc_ic;
1178 struct ath_hal *ah = sc->sc_ah;
1179 int i;
1180
1181 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1182 struct ieee80211_wepkey *k = &ic->ic_nw_keys[i];
1183 if (k->wk_len == 0)
1184 ath_hal_keyreset(ah, i);
1185 else
1186 /* XXX return value */
1187 /* NB: this uses HAL_KEYVAL == ieee80211_wepkey */
1188 ath_hal_keyset(ah, i, (const HAL_KEYVAL *) k);
1189 }
1190 }
1191
1192 static void
1193 ath_mcastfilter_accum(caddr_t dl, u_int32_t (*mfilt)[2])
1194 {
1195 u_int32_t val;
1196 u_int8_t pos;
1197
1198 val = LE_READ_4(dl + 0);
1199 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
1200 val = LE_READ_4(dl + 3);
1201 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
1202 pos &= 0x3f;
1203 (*mfilt)[pos / 32] |= (1 << (pos % 32));
1204 }
1205
1206 #ifdef __FreeBSD__
1207 static void
1208 ath_mcastfilter_compute(struct ath_softc *sc, u_int32_t (*mfilt)[2])
1209 {
1210 struct ieee80211com *ic = &sc->sc_ic;
1211 struct ifnet *ifp = &ic->ic_if;
1212 struct ifmultiaddr *ifma;
1213
1214 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1215 caddr_t dl;
1216
1217 /* calculate XOR of eight 6bit values */
1218 dl = LLADDR((struct sockaddr_dl *) ifma->ifma_addr);
1219 ath_mcastfilter_accum(dl, &mfilt);
1220 }
1221 }
1222 #else
1223 static void
1224 ath_mcastfilter_compute(struct ath_softc *sc, u_int32_t (*mfilt)[2])
1225 {
1226 struct ifnet *ifp = &sc->sc_ic.ic_if;
1227 struct ether_multi *enm;
1228 struct ether_multistep estep;
1229
1230 ETHER_FIRST_MULTI(estep, &sc->sc_ic.ic_ec, enm);
1231 while (enm != NULL) {
1232 /* XXX Punt on ranges. */
1233 if (!IEEE80211_ADDR_EQ(enm->enm_addrlo, enm->enm_addrhi)) {
1234 (*mfilt)[0] = (*mfilt)[1] = ~((u_int32_t)0);
1235 ifp->if_flags |= IFF_ALLMULTI;
1236 return;
1237 }
1238 ath_mcastfilter_accum(enm->enm_addrlo, mfilt);
1239 ETHER_NEXT_MULTI(estep, enm);
1240 }
1241 ifp->if_flags &= ~IFF_ALLMULTI;
1242 }
1243 #endif
1244
1245 static void
1246 ath_mode_init(struct ath_softc *sc)
1247 {
1248 struct ieee80211com *ic = &sc->sc_ic;
1249 struct ath_hal *ah = sc->sc_ah;
1250 struct ifnet *ifp = &ic->ic_if;
1251 u_int32_t rfilt, mfilt[2];
1252
1253 /* configure operational mode */
1254 ath_hal_setopmode(ah, ic->ic_opmode);
1255
1256 /* receive filter */
1257 rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR)
1258 | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
1259 if (ic->ic_opmode != IEEE80211_M_STA)
1260 rfilt |= HAL_RX_FILTER_PROBEREQ;
1261 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
1262 (ifp->if_flags & IFF_PROMISC))
1263 rfilt |= HAL_RX_FILTER_PROM;
1264 if (ic->ic_state == IEEE80211_S_SCAN)
1265 rfilt |= HAL_RX_FILTER_BEACON;
1266 ath_hal_setrxfilter(ah, rfilt);
1267
1268 /* calculate and install multicast filter */
1269 #ifdef __FreeBSD__
1270 if ((ifp->if_flags & IFF_ALLMULTI) == 0) {
1271 mfilt[0] = mfilt[1] = 0;
1272 ath_mcastfilter_compute(sc, &mfilt);
1273 } else {
1274 mfilt[0] = mfilt[1] = ~0;
1275 }
1276 #endif
1277 #ifdef __NetBSD__
1278 mfilt[0] = mfilt[1] = 0;
1279 ath_mcastfilter_compute(sc, &mfilt);
1280 #endif
1281 ath_hal_setmcastfilter(ah, mfilt[0], mfilt[1]);
1282 DPRINTF(("ath_mode_init: RX filter 0x%x, MC filter %08x:%08x\n",
1283 rfilt, mfilt[0], mfilt[1]));
1284 }
1285
1286 #ifdef __FreeBSD__
1287 static void
1288 ath_mbuf_load_cb(void *arg, bus_dma_segment_t *seg, int nseg, bus_size_t mapsize, int error)
1289 {
1290 struct ath_buf *bf = arg;
1291
1292 KASSERT(nseg <= ATH_MAX_SCATTER,
1293 ("ath_mbuf_load_cb: too many DMA segments %u", nseg));
1294 bf->bf_mapsize = mapsize;
1295 bf->bf_nseg = nseg;
1296 bcopy(seg, bf->bf_segs, nseg * sizeof (seg[0]));
1297 }
1298 #endif /* __FreeBSD__ */
1299
1300 static struct mbuf *
1301 ath_getmbuf(int flags, int type, u_int pktlen)
1302 {
1303 struct mbuf *m;
1304
1305 KASSERT(pktlen <= MCLBYTES, ("802.11 packet too large: %u", pktlen));
1306 #ifdef __FreeBSD__
1307 if (pktlen <= MHLEN)
1308 MGETHDR(m, flags, type);
1309 else
1310 m = m_getcl(flags, type, M_PKTHDR);
1311 #else
1312 MGETHDR(m, flags, type);
1313 if (m != NULL && pktlen > MHLEN)
1314 MCLGET(m, flags);
1315 #endif
1316 return m;
1317 }
1318
1319 static int
1320 ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
1321 {
1322 struct ieee80211com *ic = &sc->sc_ic;
1323 struct ifnet *ifp = &ic->ic_if;
1324 struct ath_hal *ah = sc->sc_ah;
1325 struct ieee80211_frame *wh;
1326 struct ath_buf *bf;
1327 struct ath_desc *ds;
1328 struct mbuf *m;
1329 int error, pktlen;
1330 u_int8_t *frm, rate;
1331 u_int16_t capinfo;
1332 struct ieee80211_rateset *rs;
1333 const HAL_RATE_TABLE *rt;
1334
1335 bf = sc->sc_bcbuf;
1336 if (bf->bf_m != NULL) {
1337 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
1338 m_freem(bf->bf_m);
1339 bf->bf_m = NULL;
1340 bf->bf_node = NULL;
1341 }
1342 /*
1343 * NB: the beacon data buffer must be 32-bit aligned;
1344 * we assume the mbuf routines will return us something
1345 * with this alignment (perhaps should assert).
1346 */
1347 rs = &ni->ni_rates;
1348 pktlen = sizeof (struct ieee80211_frame)
1349 + 8 + 2 + 2 + 2+ni->ni_esslen + 2+rs->rs_nrates + 6;
1350 if (rs->rs_nrates > IEEE80211_RATE_SIZE)
1351 pktlen += 2;
1352 m = ath_getmbuf(M_DONTWAIT, MT_DATA, pktlen);
1353 if (m == NULL) {
1354 DPRINTF(("ath_beacon_alloc: cannot get mbuf/cluster; size %u\n",
1355 pktlen));
1356 sc->sc_stats.ast_be_nombuf++;
1357 return ENOMEM;
1358 }
1359
1360 wh = mtod(m, struct ieee80211_frame *);
1361 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
1362 IEEE80211_FC0_SUBTYPE_BEACON;
1363 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1364 *(u_int16_t *)wh->i_dur = 0;
1365 memcpy(wh->i_addr1, ifp->if_broadcastaddr, IEEE80211_ADDR_LEN);
1366 memcpy(wh->i_addr2, ic->ic_myaddr, IEEE80211_ADDR_LEN);
1367 memcpy(wh->i_addr3, ni->ni_bssid, IEEE80211_ADDR_LEN);
1368 *(u_int16_t *)wh->i_seq = 0;
1369
1370 /*
1371 * beacon frame format
1372 * [8] time stamp
1373 * [2] beacon interval
1374 * [2] cabability information
1375 * [tlv] ssid
1376 * [tlv] supported rates
1377 * [tlv] parameter set (IBSS)
1378 * [tlv] extended supported rates
1379 */
1380 frm = (u_int8_t *)&wh[1];
1381 memset(frm, 0, 8); /* timestamp is set by hardware */
1382 frm += 8;
1383 *(u_int16_t *)frm = htole16(ni->ni_intval);
1384 frm += 2;
1385 if (ic->ic_opmode == IEEE80211_M_IBSS)
1386 capinfo = IEEE80211_CAPINFO_IBSS;
1387 else
1388 capinfo = IEEE80211_CAPINFO_ESS;
1389 if (ic->ic_flags & IEEE80211_F_WEPON)
1390 capinfo |= IEEE80211_CAPINFO_PRIVACY;
1391 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1392 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE;
1393 if (ic->ic_flags & IEEE80211_F_SHSLOT)
1394 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME;
1395 *(u_int16_t *)frm = htole16(capinfo);
1396 frm += 2;
1397 *frm++ = IEEE80211_ELEMID_SSID;
1398 *frm++ = ni->ni_esslen;
1399 memcpy(frm, ni->ni_essid, ni->ni_esslen);
1400 frm += ni->ni_esslen;
1401 frm = ieee80211_add_rates(frm, rs);
1402 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1403 *frm++ = IEEE80211_ELEMID_IBSSPARMS;
1404 *frm++ = 2;
1405 *frm++ = 0; *frm++ = 0; /* TODO: ATIM window */
1406 } else {
1407 /* TODO: TIM */
1408 *frm++ = IEEE80211_ELEMID_TIM;
1409 *frm++ = 4; /* length */
1410 *frm++ = 0; /* DTIM count */
1411 *frm++ = 1; /* DTIM period */
1412 *frm++ = 0; /* bitmap control */
1413 *frm++ = 0; /* Partial Virtual Bitmap (variable length) */
1414 }
1415 frm = ieee80211_add_xrates(frm, rs);
1416 m->m_pkthdr.len = m->m_len = frm - mtod(m, u_int8_t *);
1417 KASSERT(m->m_pkthdr.len <= pktlen,
1418 ("beacon bigger than expected, len %u calculated %u",
1419 m->m_pkthdr.len, pktlen));
1420
1421 DPRINTF2(("ath_beacon_alloc: m %p len %u\n", m, m->m_len));
1422 error = ath_buf_dmamap_load_mbuf(sc->sc_dmat, bf, m, BUS_DMA_NOWAIT);
1423 if (error != 0) {
1424 m_freem(m);
1425 return error;
1426 }
1427 KASSERT(bf->bf_nseg == 1,
1428 ("ath_beacon_alloc: multi-segment packet; nseg %u",
1429 bf->bf_nseg));
1430 bf->bf_m = m;
1431
1432 /* setup descriptors */
1433 ds = bf->bf_desc;
1434
1435 ds->ds_link = 0;
1436 ds->ds_data = bf->bf_segs[0].ds_addr;
1437
1438 DPRINTF2(("%s: segaddr %p seglen %u\n", __func__,
1439 (caddr_t)bf->bf_segs[0].ds_addr, (u_int)bf->bf_segs[0].ds_len));
1440
1441 /*
1442 * Calculate rate code.
1443 * XXX everything at min xmit rate
1444 */
1445 rt = sc->sc_currates;
1446 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
1447 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE)
1448 rate = rt->info[0].rateCode | rt->info[0].shortPreamble;
1449 else
1450 rate = rt->info[0].rateCode;
1451 if (!ath_hal_setuptxdesc(ah, ds
1452 , m->m_pkthdr.len + IEEE80211_CRC_LEN /* packet length */
1453 , sizeof(struct ieee80211_frame) /* header length */
1454 , HAL_PKT_TYPE_BEACON /* Atheros packet type */
1455 , 0x20 /* txpower XXX */
1456 , rate, 1 /* series 0 rate/tries */
1457 , HAL_TXKEYIX_INVALID /* no encryption */
1458 , 0 /* antenna mode */
1459 , HAL_TXDESC_NOACK /* no ack for beacons */
1460 , 0 /* rts/cts rate */
1461 , 0 /* rts/cts duration */
1462 )) {
1463 printf("%s: ath_hal_setuptxdesc failed\n", __func__);
1464 return -1;
1465 }
1466 /* NB: beacon's BufLen must be a multiple of 4 bytes */
1467 /* XXX verify mbuf data area covers this roundup */
1468 if (!ath_hal_filltxdesc(ah, ds
1469 , roundup(bf->bf_segs[0].ds_len, 4) /* buffer length */
1470 , AH_TRUE /* first segment */
1471 , AH_TRUE /* last segment */
1472 )) {
1473 printf("%s: ath_hal_filltxdesc failed\n", __func__);
1474 return -1;
1475 }
1476
1477 /* XXX it is not appropriate to bus_dmamap_sync? -dcy */
1478
1479 return 0;
1480 }
1481
1482 static void
1483 ath_beacon_proc(void *arg, int pending)
1484 {
1485 struct ath_softc *sc = arg;
1486 struct ieee80211com *ic = &sc->sc_ic;
1487 struct ath_buf *bf = sc->sc_bcbuf;
1488 struct ath_hal *ah = sc->sc_ah;
1489
1490 DPRINTF2(("%s: pending %u\n", __func__, pending));
1491 if (ic->ic_opmode == IEEE80211_M_STA ||
1492 bf == NULL || bf->bf_m == NULL) {
1493 DPRINTF(("%s: ic_flags=%x bf=%p bf_m=%p\n",
1494 __func__, ic->ic_flags, bf, bf ? bf->bf_m : NULL));
1495 return;
1496 }
1497 /* TODO: update beacon to reflect PS poll state */
1498 if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
1499 DPRINTF(("%s: beacon queue %u did not stop?",
1500 __func__, sc->sc_bhalq));
1501 return; /* busy, XXX is this right? */
1502 }
1503 ath_buf_dmamap_sync(sc->sc_dmat, bf, BUS_DMASYNC_PREWRITE);
1504
1505 ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
1506 ath_hal_txstart(ah, sc->sc_bhalq);
1507 DPRINTF2(("%s: BCDP%u = %p (%p)\n", __func__,
1508 sc->sc_bhalq, (caddr_t)bf->bf_daddr, bf->bf_desc));
1509 }
1510
1511 static void
1512 ath_beacon_free(struct ath_softc *sc)
1513 {
1514 struct ath_buf *bf = sc->sc_bcbuf;
1515
1516 if (bf->bf_m != NULL) {
1517 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
1518 m_freem(bf->bf_m);
1519 bf->bf_m = NULL;
1520 bf->bf_node = NULL;
1521 }
1522 }
1523
1524 /*
1525 * Configure the beacon and sleep timers.
1526 *
1527 * When operating as an AP this resets the TSF and sets
1528 * up the hardware to notify us when we need to issue beacons.
1529 *
1530 * When operating in station mode this sets up the beacon
1531 * timers according to the timestamp of the last received
1532 * beacon and the current TSF, configures PCF and DTIM
1533 * handling, programs the sleep registers so the hardware
1534 * will wakeup in time to receive beacons, and configures
1535 * the beacon miss handling so we'll receive a BMISS
1536 * interrupt when we stop seeing beacons from the AP
1537 * we've associated with.
1538 */
1539 static void
1540 ath_beacon_config(struct ath_softc *sc)
1541 {
1542 struct ath_hal *ah = sc->sc_ah;
1543 struct ieee80211com *ic = &sc->sc_ic;
1544 struct ieee80211_node *ni = ic->ic_bss;
1545 u_int32_t nexttbtt;
1546
1547 nexttbtt = (LE_READ_4(ni->ni_tstamp + 4) << 22) |
1548 (LE_READ_4(ni->ni_tstamp) >> 10);
1549 DPRINTF(("%s: nexttbtt=%u\n", __func__, nexttbtt));
1550 nexttbtt += ni->ni_intval;
1551 if (ic->ic_opmode == IEEE80211_M_STA) {
1552 HAL_BEACON_STATE bs;
1553 u_int32_t bmisstime;
1554
1555 /* NB: no PCF support right now */
1556 memset(&bs, 0, sizeof(bs));
1557 bs.bs_intval = ni->ni_intval;
1558 bs.bs_nexttbtt = nexttbtt;
1559 bs.bs_dtimperiod = bs.bs_intval;
1560 bs.bs_nextdtim = nexttbtt;
1561 /*
1562 * Calculate the number of consecutive beacons to miss
1563 * before taking a BMISS interrupt. The configuration
1564 * is specified in ms, so we need to convert that to
1565 * TU's and then calculate based on the beacon interval.
1566 * Note that we clamp the result to at most 10 beacons.
1567 */
1568 bmisstime = (ic->ic_bmisstimeout * 1000) / 1024;
1569 bs.bs_bmissthreshold = howmany(bmisstime,ni->ni_intval);
1570 if (bs.bs_bmissthreshold > 10)
1571 bs.bs_bmissthreshold = 10;
1572 else if (bs.bs_bmissthreshold <= 0)
1573 bs.bs_bmissthreshold = 1;
1574
1575 /*
1576 * Calculate sleep duration. The configuration is
1577 * given in ms. We insure a multiple of the beacon
1578 * period is used. Also, if the sleep duration is
1579 * greater than the DTIM period then it makes senses
1580 * to make it a multiple of that.
1581 *
1582 * XXX fixed at 100ms
1583 */
1584 bs.bs_sleepduration =
1585 roundup((100 * 1000) / 1024, bs.bs_intval);
1586 if (bs.bs_sleepduration > bs.bs_dtimperiod)
1587 bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
1588
1589 DPRINTF(("%s: intval %u nexttbtt %u dtim %u nextdtim %u bmiss %u sleep %u\n"
1590 , __func__
1591 , bs.bs_intval
1592 , bs.bs_nexttbtt
1593 , bs.bs_dtimperiod
1594 , bs.bs_nextdtim
1595 , bs.bs_bmissthreshold
1596 , bs.bs_sleepduration
1597 ));
1598 ath_hal_intrset(ah, 0);
1599 /*
1600 * Reset our tsf so the hardware will update the
1601 * tsf register to reflect timestamps found in
1602 * received beacons.
1603 */
1604 ath_hal_resettsf(ah);
1605 ath_hal_beacontimers(ah, &bs, 0/*XXX*/, 0, 0);
1606 sc->sc_imask |= HAL_INT_BMISS;
1607 ath_hal_intrset(ah, sc->sc_imask);
1608 } else {
1609 DPRINTF(("%s: intval %u nexttbtt %u\n",
1610 __func__, ni->ni_intval, nexttbtt));
1611 ath_hal_intrset(ah, 0);
1612 ath_hal_beaconinit(ah, ic->ic_opmode,
1613 nexttbtt, ni->ni_intval);
1614 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1615 sc->sc_imask |= HAL_INT_SWBA; /* beacon prepare */
1616 ath_hal_intrset(ah, sc->sc_imask);
1617 }
1618 }
1619
1620 #ifdef __FreeBSD__
1621 static void
1622 ath_load_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1623 {
1624 bus_addr_t *paddr = (bus_addr_t*) arg;
1625 *paddr = segs->ds_addr;
1626 }
1627 #endif
1628
1629 #ifdef __FreeBSD__
1630 static int
1631 ath_desc_alloc(struct ath_softc *sc)
1632 {
1633 int i, bsize, error;
1634 struct ath_desc *ds;
1635 struct ath_buf *bf;
1636
1637 /* allocate descriptors */
1638 sc->sc_desc_len = sizeof(struct ath_desc) *
1639 (ATH_TXBUF * ATH_TXDESC + ATH_RXBUF + 1);
1640 error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &sc->sc_ddmamap);
1641 if (error != 0)
1642 return error;
1643
1644 error = bus_dmamem_alloc(sc->sc_dmat, (void**) &sc->sc_desc,
1645 BUS_DMA_NOWAIT, &sc->sc_ddmamap);
1646
1647 if (error != 0)
1648 goto fail0;
1649
1650 error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap,
1651 sc->sc_desc, sc->sc_desc_len,
1652 ath_load_cb, &sc->sc_desc_paddr,
1653 BUS_DMA_NOWAIT);
1654 if (error != 0)
1655 goto fail1;
1656
1657 ds = sc->sc_desc;
1658 DPRINTF(("ath_desc_alloc: DMA map: %p (%d) -> %p (%lu)\n",
1659 ds, sc->sc_desc_len,
1660 (caddr_t) sc->sc_desc_paddr, /*XXX*/ (u_long) sc->sc_desc_len));
1661
1662 /* allocate buffers */
1663 bsize = sizeof(struct ath_buf) * (ATH_TXBUF + ATH_RXBUF + 1);
1664 bf = malloc(bsize, M_DEVBUF, M_NOWAIT | M_ZERO);
1665 if (bf == NULL) {
1666 printf("%s: unable to allocate Tx/Rx buffers\n",
1667 sc->sc_dev.dv_xname);
1668 error = -1;
1669 goto fail2;
1670 }
1671 sc->sc_bufptr = bf;
1672
1673 TAILQ_INIT(&sc->sc_rxbuf);
1674 for (i = 0; i < ATH_RXBUF; i++, bf++, ds++) {
1675 bf->bf_desc = ds;
1676 bf->bf_daddr = sc->sc_desc_paddr +
1677 ((caddr_t)ds - (caddr_t)sc->sc_desc);
1678 error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
1679 &bf->bf_dmamap);
1680 if (error != 0)
1681 break;
1682 TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
1683 }
1684
1685 TAILQ_INIT(&sc->sc_txbuf);
1686 for (i = 0; i < ATH_TXBUF; i++, bf++, ds += ATH_TXDESC) {
1687 bf->bf_desc = ds;
1688 bf->bf_daddr = sc->sc_desc_paddr +
1689 ((caddr_t)ds - (caddr_t)sc->sc_desc);
1690 error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT,
1691 &bf->bf_dmamap);
1692 if (error != 0)
1693 break;
1694 TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
1695 }
1696 TAILQ_INIT(&sc->sc_txq);
1697
1698 /* beacon buffer */
1699 bf->bf_desc = ds;
1700 bf->bf_daddr = sc->sc_desc_paddr + ((caddr_t)ds - (caddr_t)sc->sc_desc);
1701 error = bus_dmamap_create(sc->sc_dmat, BUS_DMA_NOWAIT, &bf->bf_dmamap);
1702 if (error != 0)
1703 return error;
1704 sc->sc_bcbuf = bf;
1705 return 0;
1706
1707 fail2:
1708 bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
1709 fail1:
1710 bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap);
1711 fail0:
1712 bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
1713 sc->sc_ddmamap = NULL;
1714 return error;
1715 }
1716 #else
1717 static int
1718 ath_desc_alloc(struct ath_softc *sc)
1719 {
1720 int i, bsize, error = -1;
1721 struct ath_desc *ds;
1722 struct ath_buf *bf;
1723
1724 /* allocate descriptors */
1725 sc->sc_desc_len = sizeof(struct ath_desc) *
1726 (ATH_TXBUF * ATH_TXDESC + ATH_RXBUF + 1);
1727 if ((error = bus_dmamem_alloc(sc->sc_dmat, sc->sc_desc_len, PAGE_SIZE,
1728 0, &sc->sc_dseg, 1, &sc->sc_dnseg, 0)) != 0) {
1729 printf("%s: unable to allocate control data, error = %d\n",
1730 sc->sc_dev.dv_xname, error);
1731 goto fail0;
1732 }
1733
1734 if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg,
1735 sc->sc_desc_len, (caddr_t *)&sc->sc_desc, BUS_DMA_COHERENT)) != 0) {
1736 printf("%s: unable to map control data, error = %d\n",
1737 sc->sc_dev.dv_xname, error);
1738 goto fail1;
1739 }
1740
1741 if ((error = bus_dmamap_create(sc->sc_dmat, sc->sc_desc_len, 1,
1742 sc->sc_desc_len, 0, 0, &sc->sc_ddmamap)) != 0) {
1743 printf("%s: unable to create control data DMA map, "
1744 "error = %d\n", sc->sc_dev.dv_xname, error);
1745 goto fail2;
1746 }
1747
1748 if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_ddmamap, sc->sc_desc,
1749 sc->sc_desc_len, NULL, 0)) != 0) {
1750 printf("%s: unable to load control data DMA map, error = %d\n",
1751 sc->sc_dev.dv_xname, error);
1752 goto fail3;
1753 }
1754
1755 ds = sc->sc_desc;
1756 sc->sc_desc_paddr = sc->sc_ddmamap->dm_segs[0].ds_addr;
1757
1758 DPRINTF(("ath_desc_alloc: DMA map: %p (%lu) -> %p (%lu)\n",
1759 ds, (u_long)sc->sc_desc_len,
1760 (caddr_t) sc->sc_desc_paddr, /*XXX*/ (u_long) sc->sc_desc_len));
1761
1762 /* allocate buffers */
1763 bsize = sizeof(struct ath_buf) * (ATH_TXBUF + ATH_RXBUF + 1);
1764 bf = malloc(bsize, M_DEVBUF, M_NOWAIT | M_ZERO);
1765 if (bf == NULL) {
1766 printf("%s: unable to allocate Tx/Rx buffers\n",
1767 sc->sc_dev.dv_xname);
1768 error = ENOMEM;
1769 goto fail3;
1770 }
1771 sc->sc_bufptr = bf;
1772
1773 TAILQ_INIT(&sc->sc_rxbuf);
1774 for (i = 0; i < ATH_RXBUF; i++, bf++, ds++) {
1775 bf->bf_desc = ds;
1776 bf->bf_daddr = sc->sc_desc_paddr +
1777 ((caddr_t)ds - (caddr_t)sc->sc_desc);
1778 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
1779 MCLBYTES, 0, 0, &bf->bf_dmamap)) != 0) {
1780 printf("%s: unable to create Rx dmamap, error = %d\n",
1781 sc->sc_dev.dv_xname, error);
1782 goto fail4;
1783 }
1784 TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
1785 }
1786
1787 TAILQ_INIT(&sc->sc_txbuf);
1788 for (i = 0; i < ATH_TXBUF; i++, bf++, ds += ATH_TXDESC) {
1789 bf->bf_desc = ds;
1790 bf->bf_daddr = sc->sc_desc_paddr +
1791 ((caddr_t)ds - (caddr_t)sc->sc_desc);
1792 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
1793 ATH_TXDESC, MCLBYTES, 0, 0, &bf->bf_dmamap)) != 0) {
1794 printf("%s: unable to create Tx dmamap, error = %d\n",
1795 sc->sc_dev.dv_xname, error);
1796 goto fail5;
1797 }
1798 TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
1799 }
1800 TAILQ_INIT(&sc->sc_txq);
1801
1802 /* beacon buffer */
1803 bf->bf_desc = ds;
1804 bf->bf_daddr = sc->sc_desc_paddr + ((caddr_t)ds - (caddr_t)sc->sc_desc);
1805 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 0, 0,
1806 &bf->bf_dmamap)) != 0) {
1807 printf("%s: unable to create beacon dmamap, error = %d\n",
1808 sc->sc_dev.dv_xname, error);
1809 goto fail5;
1810 }
1811 sc->sc_bcbuf = bf;
1812 return 0;
1813
1814 fail5:
1815 for (i = ATH_RXBUF; i < ATH_RXBUF + ATH_TXBUF; i++) {
1816 if (sc->sc_bufptr[i].bf_dmamap == NULL)
1817 continue;
1818 bus_dmamap_destroy(sc->sc_dmat, sc->sc_bufptr[i].bf_dmamap);
1819 }
1820 fail4:
1821 for (i = 0; i < ATH_RXBUF; i++) {
1822 if (sc->sc_bufptr[i].bf_dmamap == NULL)
1823 continue;
1824 bus_dmamap_destroy(sc->sc_dmat, sc->sc_bufptr[i].bf_dmamap);
1825 }
1826 fail3:
1827 bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
1828 fail2:
1829 bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
1830 sc->sc_ddmamap = NULL;
1831 fail1:
1832 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_desc, sc->sc_desc_len);
1833 fail0:
1834 bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg);
1835 return error;
1836 }
1837 #endif
1838
1839 static void
1840 ath_desc_free(struct ath_softc *sc)
1841 {
1842 struct ath_buf *bf;
1843
1844 #ifdef __FreeBSD__
1845 bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
1846 bus_dmamem_free(sc->sc_dmat, sc->sc_desc, sc->sc_ddmamap);
1847 bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
1848 #else
1849 bus_dmamap_unload(sc->sc_dmat, sc->sc_ddmamap);
1850 bus_dmamap_destroy(sc->sc_dmat, sc->sc_ddmamap);
1851 bus_dmamem_free(sc->sc_dmat, &sc->sc_dseg, sc->sc_dnseg);
1852 #endif
1853
1854 TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) {
1855 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
1856 bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
1857 m_freem(bf->bf_m);
1858 }
1859 TAILQ_FOREACH(bf, &sc->sc_txbuf, bf_list)
1860 bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
1861 TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
1862 if (bf->bf_m) {
1863 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
1864 bus_dmamap_destroy(sc->sc_dmat, bf->bf_dmamap);
1865 m_freem(bf->bf_m);
1866 bf->bf_m = NULL;
1867 }
1868 }
1869 if (sc->sc_bcbuf != NULL) {
1870 bus_dmamap_unload(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap);
1871 bus_dmamap_destroy(sc->sc_dmat, sc->sc_bcbuf->bf_dmamap);
1872 sc->sc_bcbuf = NULL;
1873 }
1874
1875 TAILQ_INIT(&sc->sc_rxbuf);
1876 TAILQ_INIT(&sc->sc_txbuf);
1877 TAILQ_INIT(&sc->sc_txq);
1878 free(sc->sc_bufptr, M_DEVBUF);
1879 sc->sc_bufptr = NULL;
1880 }
1881
1882 static struct ieee80211_node *
1883 ath_node_alloc(struct ieee80211com *ic)
1884 {
1885 struct ath_node *an =
1886 malloc(sizeof(struct ath_node), M_DEVBUF, M_NOWAIT | M_ZERO);
1887 return an ? &an->an_node : NULL;
1888 }
1889
1890 static void
1891 ath_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
1892 {
1893 struct ath_softc *sc = ic->ic_if.if_softc;
1894 struct ath_buf *bf;
1895
1896 TAILQ_FOREACH(bf, &sc->sc_txq, bf_list) {
1897 if (bf->bf_node == ni)
1898 bf->bf_node = NULL;
1899 }
1900 free(ni, M_DEVBUF);
1901 }
1902
1903 static void
1904 ath_node_copy(struct ieee80211com *ic,
1905 struct ieee80211_node *dst, const struct ieee80211_node *src)
1906 {
1907 *(struct ath_node *)dst = *(const struct ath_node *)src;
1908 }
1909
1910 static int
1911 ath_rxbuf_init(struct ath_softc *sc, struct ath_buf *bf)
1912 {
1913 struct ath_hal *ah = sc->sc_ah;
1914 int error;
1915 struct mbuf *m;
1916 struct ath_desc *ds;
1917
1918 m = bf->bf_m;
1919 if (m == NULL) {
1920 /*
1921 * NB: by assigning a page to the rx dma buffer we
1922 * implicitly satisfy the Atheros requirement that
1923 * this buffer be cache-line-aligned and sized to be
1924 * multiple of the cache line size. Not doing this
1925 * causes weird stuff to happen (for the 5210 at least).
1926 */
1927 m = ath_getmbuf(M_DONTWAIT, MT_DATA, MCLBYTES);
1928 if (m == NULL) {
1929 DPRINTF(("ath_rxbuf_init: no mbuf/cluster\n"));
1930 sc->sc_stats.ast_rx_nombuf++;
1931 return ENOMEM;
1932 }
1933 bf->bf_m = m;
1934 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
1935
1936 error = ath_buf_dmamap_load_mbuf(sc->sc_dmat, bf, m,
1937 BUS_DMA_NOWAIT);
1938 if (error != 0) {
1939 DPRINTF(("ath_rxbuf_init: ath_buf_dmamap_load_mbuf failed;"
1940 " error %d\n", error));
1941 sc->sc_stats.ast_rx_busdma++;
1942 return error;
1943 }
1944 KASSERT(bf->bf_nseg == 1,
1945 ("ath_rxbuf_init: multi-segment packet; nseg %u",
1946 bf->bf_nseg));
1947 }
1948 ath_buf_dmamap_sync(sc->sc_dmat, bf, BUS_DMASYNC_PREREAD);
1949
1950 /* setup descriptors */
1951 ds = bf->bf_desc;
1952 ds->ds_link = 0;
1953 ds->ds_data = bf->bf_segs[0].ds_addr;
1954 ath_hal_setuprxdesc(ah, ds
1955 , m->m_len /* buffer size */
1956 , 0
1957 );
1958
1959 if (sc->sc_rxlink != NULL)
1960 *sc->sc_rxlink = bf->bf_daddr;
1961 sc->sc_rxlink = &ds->ds_link;
1962 return 0;
1963 }
1964
1965 static void
1966 ath_rx_proc(void *arg, int npending)
1967 {
1968 struct ath_softc *sc = arg;
1969 struct ath_buf *bf;
1970 struct ieee80211com *ic = &sc->sc_ic;
1971 struct ifnet *ifp = &ic->ic_if;
1972 struct ath_hal *ah = sc->sc_ah;
1973 struct ath_desc *ds;
1974 struct mbuf *m;
1975 struct ieee80211_frame *wh, whbuf;
1976 struct ieee80211_node *ni;
1977 int len;
1978 u_int phyerr;
1979 HAL_STATUS status;
1980
1981 DPRINTF2(("ath_rx_proc: pending %u\n", npending));
1982 do {
1983 bf = TAILQ_FIRST(&sc->sc_rxbuf);
1984 if (bf == NULL) { /* NB: shouldn't happen */
1985 if_printf(ifp, "ath_rx_proc: no buffer!\n");
1986 break;
1987 }
1988 m = bf->bf_m;
1989 if (m == NULL) { /* NB: shouldn't happen */
1990 if_printf(ifp, "ath_rx_proc: no mbuf!\n");
1991 continue;
1992 }
1993 ds = bf->bf_desc;
1994 status = ath_hal_rxprocdesc(ah, ds);
1995 #ifdef AR_DEBUG
1996 if (ath_debug > 1)
1997 ath_printrxbuf(bf, status == HAL_OK);
1998 #endif
1999 if (status == HAL_EINPROGRESS)
2000 break;
2001 TAILQ_REMOVE(&sc->sc_rxbuf, bf, bf_list);
2002 if (ds->ds_rxstat.rs_status != 0) {
2003 ifp->if_ierrors++;
2004 if (ds->ds_rxstat.rs_status & HAL_RXERR_CRC)
2005 sc->sc_stats.ast_rx_crcerr++;
2006 if (ds->ds_rxstat.rs_status & HAL_RXERR_FIFO)
2007 sc->sc_stats.ast_rx_fifoerr++;
2008 if (ds->ds_rxstat.rs_status & HAL_RXERR_DECRYPT)
2009 sc->sc_stats.ast_rx_badcrypt++;
2010 if (ds->ds_rxstat.rs_status & HAL_RXERR_PHY) {
2011 sc->sc_stats.ast_rx_phyerr++;
2012 phyerr = ds->ds_rxstat.rs_phyerr & 0x1f;
2013 sc->sc_stats.ast_rx_phy[phyerr]++;
2014 }
2015 goto rx_next;
2016 }
2017
2018 len = ds->ds_rxstat.rs_datalen;
2019 if (len < sizeof(struct ieee80211_frame)) {
2020 DPRINTF(("ath_rx_proc: short packet %d\n", len));
2021 sc->sc_stats.ast_rx_tooshort++;
2022 goto rx_next;
2023 }
2024
2025 ath_buf_dmamap_sync(sc->sc_dmat, bf, BUS_DMASYNC_POSTREAD);
2026
2027 wh = mtod(m, struct ieee80211_frame *);
2028 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
2029 IEEE80211_FC0_TYPE_CTL &&
2030 ic->ic_opmode != IEEE80211_M_MONITOR) {
2031 /*
2032 * Discard control frame when not in monitor mode.
2033 */
2034 DPRINTF(("ath_rx_proc: control frame\n"));
2035 sc->sc_stats.ast_rx_ctl++;
2036 goto rx_next;
2037 }
2038
2039 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2040 bf->bf_m = NULL;
2041 m->m_pkthdr.rcvif = ifp;
2042 m->m_pkthdr.len = m->m_len = len;
2043
2044 #if NBPFILTER > 0
2045 if (sc->sc_drvbpf) {
2046 #ifdef __FreeBSD__
2047 struct mbuf *mb;
2048
2049 /* XXX pre-allocate space when setting up recv's */
2050 MGETHDR(mb, M_DONTWAIT, m->m_type);
2051 if (mb != NULL) {
2052 sc->sc_rx_th.wr_rate =
2053 sc->sc_hwmap[ds->ds_rxstat.rs_rate];
2054 sc->sc_rx_th.wr_antsignal =
2055 ds->ds_rxstat.rs_rssi;
2056 sc->sc_rx_th.wr_antenna =
2057 ds->ds_rxstat.rs_antenna;
2058 /* XXX TSF */
2059
2060 (void) m_dup_pkthdr(mb, m, M_DONTWAIT);
2061 mb->m_next = m;
2062 mb->m_data = (caddr_t)&sc->sc_rx_th;
2063 mb->m_len = sizeof(sc->sc_rx_th);
2064 mb->m_pkthdr.len += mb->m_len;
2065 bpf_mtap(sc->sc_drvbpf, mb);
2066 m_free(mb);
2067 }
2068 #else
2069 /* XXX pre-allocate space when setting up recv's */
2070 struct mbuf mb;
2071
2072 sc->sc_rx_th.wr_rate =
2073 sc->sc_hwmap[ds->ds_rxstat.rs_rate];
2074 sc->sc_rx_th.wr_antsignal =
2075 ds->ds_rxstat.rs_rssi;
2076 sc->sc_rx_th.wr_antenna =
2077 ds->ds_rxstat.rs_antenna;
2078 /* XXX TSF */
2079
2080 M_COPY_PKTHDR(&mb, m);
2081 mb.m_next = m;
2082 mb.m_data = (caddr_t)&sc->sc_rx_th;
2083 mb.m_len = sizeof(sc->sc_rx_th);
2084 mb.m_pkthdr.len += mb.m_len;
2085 bpf_mtap(sc->sc_drvbpf, &mb);
2086 #endif
2087 }
2088 #endif
2089
2090 m_adj(m, -IEEE80211_CRC_LEN);
2091 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
2092 /*
2093 * WEP is decrypted by hardware. Clear WEP bit
2094 * and trim WEP header for ieee80211_input().
2095 */
2096 wh->i_fc[1] &= ~IEEE80211_FC1_WEP;
2097 memcpy(&whbuf, wh, sizeof(whbuf));
2098 m_adj(m, IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN);
2099 memcpy(mtod(m, caddr_t), &whbuf, sizeof(whbuf));
2100 /*
2101 * Also trim WEP ICV from the tail.
2102 */
2103 m_adj(m, -IEEE80211_WEP_CRCLEN);
2104 /*
2105 * The header has probably moved.
2106 */
2107 wh = mtod(m, struct ieee80211_frame *);
2108 }
2109
2110 /*
2111 * Locate the node for sender, track state, and
2112 * then pass this node (referenced) up to the 802.11
2113 * layer for its use. We are required to pass
2114 * something so we fall back to ic_bss when this frame
2115 * is from an unknown sender.
2116 */
2117 if (ic->ic_opmode != IEEE80211_M_STA) {
2118 ni = ieee80211_find_node(ic, wh->i_addr2);
2119 if (ni == NULL)
2120 ni = ieee80211_ref_node(ic->ic_bss);
2121 } else
2122 ni = ieee80211_ref_node(ic->ic_bss);
2123 ATH_NODE(ni)->an_rx_antenna = ds->ds_rxstat.rs_antenna;
2124 /*
2125 * Send frame up for processing.
2126 */
2127 ieee80211_input(ifp, m, ni,
2128 ds->ds_rxstat.rs_rssi, ds->ds_rxstat.rs_tstamp);
2129 /*
2130 * The frame may have caused the node to be marked for
2131 * reclamation (e.g. in response to a DEAUTH message)
2132 * so use free_node here instead of unref_node.
2133 */
2134 if (ni == ic->ic_bss)
2135 ieee80211_unref_node(&ni);
2136 else
2137 ieee80211_free_node(ic, ni);
2138 rx_next:
2139 TAILQ_INSERT_TAIL(&sc->sc_rxbuf, bf, bf_list);
2140 } while (ath_rxbuf_init(sc, bf) == 0);
2141
2142 ath_hal_rxmonitor(ah); /* rx signal state monitoring */
2143 ath_hal_rxena(ah); /* in case of RXEOL */
2144 }
2145
2146 /*
2147 * XXX Size of an ACK control frame in bytes.
2148 */
2149 #define IEEE80211_ACK_SIZE (2+2+IEEE80211_ADDR_LEN+4)
2150
2151 static int
2152 ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf,
2153 struct mbuf *m0)
2154 {
2155 struct ieee80211com *ic = &sc->sc_ic;
2156 struct ath_hal *ah = sc->sc_ah;
2157 struct ifnet *ifp = &sc->sc_ic.ic_if;
2158 int i, error, iswep, hdrlen, pktlen;
2159 u_int8_t rix, cix, txrate, ctsrate;
2160 struct ath_desc *ds;
2161 struct mbuf *m;
2162 struct ieee80211_frame *wh;
2163 u_int32_t iv;
2164 u_int8_t *ivp;
2165 u_int8_t hdrbuf[sizeof(struct ieee80211_frame) +
2166 IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN];
2167 u_int subtype, flags, ctsduration, antenna;
2168 HAL_PKT_TYPE atype;
2169 const HAL_RATE_TABLE *rt;
2170 HAL_BOOL shortPreamble;
2171 struct ath_node *an;
2172 ath_txq_critsect_decl(s);
2173
2174 wh = mtod(m0, struct ieee80211_frame *);
2175 iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
2176 hdrlen = sizeof(struct ieee80211_frame);
2177 pktlen = m0->m_pkthdr.len;
2178
2179 if (iswep) {
2180 memcpy(hdrbuf, mtod(m0, caddr_t), hdrlen);
2181 m_adj(m0, hdrlen);
2182 M_PREPEND(m0, sizeof(hdrbuf), M_DONTWAIT);
2183 if (m0 == NULL) {
2184 sc->sc_stats.ast_tx_nombuf++;
2185 return ENOMEM;
2186 }
2187 ivp = hdrbuf + hdrlen;
2188 /*
2189 * XXX
2190 * IV must not duplicate during the lifetime of the key.
2191 * But no mechanism to renew keys is defined in IEEE 802.11
2192 * WEP. And IV may be duplicated between other stations
2193 * because of the session key itself is shared.
2194 * So we use pseudo random IV for now, though it is not the
2195 * right way.
2196 */
2197 iv = arc4random();
2198 for (i = 0; i < IEEE80211_WEP_IVLEN; i++) {
2199 ivp[i] = iv;
2200 iv >>= 8;
2201 }
2202 ivp[i] = sc->sc_ic.ic_wep_txkey << 6; /* Key ID and pad */
2203 memcpy(mtod(m0, caddr_t), hdrbuf, sizeof(hdrbuf));
2204 /*
2205 * The ICV length must be included into hdrlen and pktlen.
2206 */
2207 hdrlen = sizeof(hdrbuf) + IEEE80211_WEP_CRCLEN;
2208 pktlen = m0->m_pkthdr.len + IEEE80211_WEP_CRCLEN;
2209 }
2210 pktlen += IEEE80211_CRC_LEN;
2211
2212 /*
2213 * Load the DMA map so any coalescing is done. This
2214 * also calculates the number of descriptors we need.
2215 */
2216 error = ath_buf_dmamap_load_mbuf(sc->sc_dmat, bf, m0, BUS_DMA_NOWAIT);
2217 if (error != 0) {
2218 sc->sc_stats.ast_tx_busdma++;
2219 m_freem(m0);
2220 return error;
2221 }
2222 /*
2223 * Discard null packets and check for packets that
2224 * require too many TX descriptors. We try to convert
2225 * the latter to a cluster.
2226 */
2227 if (bf->bf_nseg > ATH_TXDESC) { /* too many desc's, linearize */
2228 sc->sc_stats.ast_tx_linear++;
2229 MGETHDR(m, M_DONTWAIT, MT_DATA);
2230 if (m == NULL) {
2231 sc->sc_stats.ast_tx_nombuf++;
2232 m_freem(m0);
2233 return ENOMEM;
2234 }
2235 #ifdef __FreeBSD__
2236 M_MOVE_PKTHDR(m, m0);
2237 #else
2238 M_COPY_PKTHDR(m, m0);
2239 #endif
2240 MCLGET(m, M_DONTWAIT);
2241 if ((m->m_flags & M_EXT) == 0) {
2242 sc->sc_stats.ast_tx_nomcl++;
2243 m_freem(m0);
2244 m_free(m);
2245 return ENOMEM;
2246 }
2247 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
2248 m_freem(m0);
2249 m->m_len = m->m_pkthdr.len;
2250 m0 = m;
2251 error = ath_buf_dmamap_load_mbuf(sc->sc_dmat, bf, m0,
2252 BUS_DMA_NOWAIT);
2253 if (error != 0) {
2254 sc->sc_stats.ast_tx_busdma++;
2255 m_freem(m0);
2256 return error;
2257 }
2258 KASSERT(bf->bf_nseg == 1,
2259 ("ath_tx_start: packet not one segment; nseg %u",
2260 bf->bf_nseg));
2261 } else if (bf->bf_nseg == 0) { /* null packet, discard */
2262 sc->sc_stats.ast_tx_nodata++;
2263 m_freem(m0);
2264 return EIO;
2265 }
2266 DPRINTF2(("ath_tx_start: m %p len %u\n", m0, pktlen));
2267 ath_buf_dmamap_sync(sc->sc_dmat, bf, BUS_DMASYNC_PREWRITE);
2268 bf->bf_m = m0;
2269 bf->bf_node = ni; /* NB: held reference */
2270
2271 /* setup descriptors */
2272 ds = bf->bf_desc;
2273 rt = sc->sc_currates;
2274 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
2275
2276 /*
2277 * Calculate Atheros packet type from IEEE80211 packet header
2278 * and setup for rate calculations.
2279 */
2280 atype = HAL_PKT_TYPE_NORMAL; /* default */
2281 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
2282 case IEEE80211_FC0_TYPE_MGT:
2283 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2284 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
2285 atype = HAL_PKT_TYPE_BEACON;
2286 else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
2287 atype = HAL_PKT_TYPE_PROBE_RESP;
2288 else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
2289 atype = HAL_PKT_TYPE_ATIM;
2290 rix = 0; /* XXX lowest rate */
2291 break;
2292 case IEEE80211_FC0_TYPE_CTL:
2293 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
2294 if (subtype == IEEE80211_FC0_SUBTYPE_PS_POLL)
2295 atype = HAL_PKT_TYPE_PSPOLL;
2296 rix = 0; /* XXX lowest rate */
2297 break;
2298 default:
2299 rix = sc->sc_rixmap[ni->ni_rates.rs_rates[ni->ni_txrate] &
2300 IEEE80211_RATE_VAL];
2301 if (rix == 0xff) {
2302 if_printf(ifp, "bogus xmit rate 0x%x\n",
2303 ni->ni_rates.rs_rates[ni->ni_txrate]);
2304 sc->sc_stats.ast_tx_badrate++;
2305 m_freem(m0);
2306 return EIO;
2307 }
2308 break;
2309 }
2310 /*
2311 * NB: the 802.11 layer marks whether or not we should
2312 * use short preamble based on the current mode and
2313 * negotiated parameters.
2314 */
2315 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) {
2316 txrate = rt->info[rix].rateCode | rt->info[rix].shortPreamble;
2317 shortPreamble = AH_TRUE;
2318 sc->sc_stats.ast_tx_shortpre++;
2319 } else {
2320 txrate = rt->info[rix].rateCode;
2321 shortPreamble = AH_FALSE;
2322 }
2323
2324 /*
2325 * Calculate miscellaneous flags.
2326 */
2327 flags = HAL_TXDESC_CLRDMASK; /* XXX needed for wep errors */
2328 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) {
2329 flags |= HAL_TXDESC_NOACK; /* no ack on broad/multicast */
2330 sc->sc_stats.ast_tx_noack++;
2331 } else if (pktlen > ic->ic_rtsthreshold) {
2332 flags |= HAL_TXDESC_RTSENA; /* RTS based on frame length */
2333 sc->sc_stats.ast_tx_rts++;
2334 }
2335
2336 /*
2337 * Calculate RTS/CTS rate and duration if needed.
2338 */
2339 ctsduration = 0;
2340 if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
2341 /*
2342 * CTS transmit rate is derived from the transmit rate
2343 * by looking in the h/w rate table. We must also factor
2344 * in whether or not a short preamble is to be used.
2345 */
2346 cix = rt->info[rix].controlRate;
2347 ctsrate = rt->info[cix].rateCode;
2348 if (shortPreamble)
2349 ctsrate |= rt->info[cix].shortPreamble;
2350 /*
2351 * Compute the transmit duration based on the size
2352 * of an ACK frame. We call into the HAL to do the
2353 * computation since it depends on the characteristics
2354 * of the actual PHY being used.
2355 */
2356 if (flags & HAL_TXDESC_RTSENA) { /* SIFS + CTS */
2357 ctsduration += ath_hal_computetxtime(ah,
2358 rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
2359 }
2360 /* SIFS + data */
2361 ctsduration += ath_hal_computetxtime(ah,
2362 rt, pktlen, rix, shortPreamble);
2363 if ((flags & HAL_TXDESC_NOACK) == 0) { /* SIFS + ACK */
2364 ctsduration += ath_hal_computetxtime(ah,
2365 rt, IEEE80211_ACK_SIZE, cix, shortPreamble);
2366 }
2367 } else
2368 ctsrate = 0;
2369
2370 /*
2371 * For now use the antenna on which the last good
2372 * frame was received on. We assume this field is
2373 * initialized to 0 which gives us ``auto'' or the
2374 * ``default'' antenna.
2375 */
2376 an = (struct ath_node *) ni;
2377 if (an->an_tx_antenna)
2378 antenna = an->an_tx_antenna;
2379 else
2380 antenna = an->an_rx_antenna;
2381
2382 /*
2383 * Formulate first tx descriptor with tx controls.
2384 */
2385 /* XXX check return value? */
2386 ath_hal_setuptxdesc(ah, ds
2387 , pktlen /* packet length */
2388 , hdrlen /* header length */
2389 , atype /* Atheros packet type */
2390 , 60 /* txpower XXX */
2391 , txrate, 1+10 /* series 0 rate/tries */
2392 , iswep ? sc->sc_ic.ic_wep_txkey : HAL_TXKEYIX_INVALID
2393 , antenna /* antenna mode */
2394 , flags /* flags */
2395 , ctsrate /* rts/cts rate */
2396 , ctsduration /* rts/cts duration */
2397 );
2398 #ifdef notyet
2399 ath_hal_setupxtxdesc(ah, ds
2400 , AH_FALSE /* short preamble */
2401 , 0, 0 /* series 1 rate/tries */
2402 , 0, 0 /* series 2 rate/tries */
2403 , 0, 0 /* series 3 rate/tries */
2404 );
2405 #endif
2406 /*
2407 * Fillin the remainder of the descriptor info.
2408 */
2409 for (i = 0; i < bf->bf_nseg; i++, ds++) {
2410 ds->ds_data = bf->bf_segs[i].ds_addr;
2411 if (i == bf->bf_nseg - 1)
2412 ds->ds_link = 0;
2413 else
2414 ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
2415 ath_hal_filltxdesc(ah, ds
2416 , bf->bf_segs[i].ds_len /* segment length */
2417 , i == 0 /* first segment */
2418 , i == bf->bf_nseg - 1 /* last segment */
2419 );
2420 DPRINTF2(("ath_tx_start: %d: %08x %08x %08x %08x %08x %08x\n",
2421 i, ds->ds_link, ds->ds_data, ds->ds_ctl0, ds->ds_ctl1,
2422 ds->ds_hw[0], ds->ds_hw[1]));
2423 }
2424
2425 /*
2426 * Insert the frame on the outbound list and
2427 * pass it on to the hardware.
2428 */
2429 ath_txq_critsect_begin(sc, s);
2430 TAILQ_INSERT_TAIL(&sc->sc_txq, bf, bf_list);
2431 if (sc->sc_txlink == NULL) {
2432 ath_hal_puttxbuf(ah, sc->sc_txhalq, bf->bf_daddr);
2433 DPRINTF2(("ath_tx_start: TXDP0 = %p (%p)\n",
2434 (caddr_t)bf->bf_daddr, bf->bf_desc));
2435 } else {
2436 *sc->sc_txlink = bf->bf_daddr;
2437 DPRINTF2(("ath_tx_start: link(%p)=%p (%p)\n",
2438 sc->sc_txlink, (caddr_t)bf->bf_daddr, bf->bf_desc));
2439 }
2440 sc->sc_txlink = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
2441 ath_txq_critsect_end(sc, s);
2442
2443 ath_hal_txstart(ah, sc->sc_txhalq);
2444 return 0;
2445 }
2446
2447 static void
2448 ath_tx_proc(void *arg, int npending)
2449 {
2450 struct ath_softc *sc = arg;
2451 struct ath_hal *ah = sc->sc_ah;
2452 struct ath_buf *bf;
2453 struct ieee80211com *ic = &sc->sc_ic;
2454 struct ifnet *ifp = &ic->ic_if;
2455 struct ath_desc *ds;
2456 struct ieee80211_node *ni;
2457 struct ath_node *an;
2458 int sr, lr;
2459 HAL_STATUS status;
2460 ath_txq_critsect_decl(s);
2461 ath_txbuf_critsect_decl(s2);
2462
2463 DPRINTF2(("ath_tx_proc: pending %u tx queue %p, link %p\n",
2464 npending, (caddr_t) ath_hal_gettxbuf(sc->sc_ah, sc->sc_txhalq),
2465 sc->sc_txlink));
2466 for (;;) {
2467 ath_txq_critsect_begin(sc, s);
2468 bf = TAILQ_FIRST(&sc->sc_txq);
2469 if (bf == NULL) {
2470 sc->sc_txlink = NULL;
2471 ath_txq_critsect_end(sc, s);
2472 break;
2473 }
2474 /* only the last descriptor is needed */
2475 ds = &bf->bf_desc[bf->bf_nseg - 1];
2476 status = ath_hal_txprocdesc(ah, ds);
2477 #ifdef AR_DEBUG
2478 if (ath_debug > 1)
2479 ath_printtxbuf(bf, status == HAL_OK);
2480 #endif
2481 if (status == HAL_EINPROGRESS) {
2482 ath_txq_critsect_end(sc, s);
2483 break;
2484 }
2485 TAILQ_REMOVE(&sc->sc_txq, bf, bf_list);
2486 ath_txq_critsect_end(sc, s);
2487
2488 ni = bf->bf_node;
2489 if (ni != NULL) {
2490 an = (struct ath_node *) ni;
2491 if (ds->ds_txstat.ts_status == 0) {
2492 an->an_tx_ok++;
2493 an->an_tx_antenna = ds->ds_txstat.ts_antenna;
2494 } else {
2495 an->an_tx_err++;
2496 ifp->if_oerrors++;
2497 if (ds->ds_txstat.ts_status & HAL_TXERR_XRETRY)
2498 sc->sc_stats.ast_tx_xretries++;
2499 if (ds->ds_txstat.ts_status & HAL_TXERR_FIFO)
2500 sc->sc_stats.ast_tx_fifoerr++;
2501 if (ds->ds_txstat.ts_status & HAL_TXERR_FILT)
2502 sc->sc_stats.ast_tx_filtered++;
2503 an->an_tx_antenna = 0; /* invalidate */
2504 }
2505 sr = ds->ds_txstat.ts_shortretry;
2506 lr = ds->ds_txstat.ts_longretry;
2507 sc->sc_stats.ast_tx_shortretry += sr;
2508 sc->sc_stats.ast_tx_longretry += lr;
2509 if (sr + lr)
2510 an->an_tx_retr++;
2511 /*
2512 * Reclaim reference to node.
2513 *
2514 * NB: the node may be reclaimed here if, for example
2515 * this is a DEAUTH message that was sent and the
2516 * node was timed out due to inactivity.
2517 */
2518 if (ni != ic->ic_bss)
2519 ieee80211_free_node(ic, ni);
2520 }
2521 ath_buf_dmamap_sync(sc->sc_dmat, bf, BUS_DMASYNC_POSTWRITE);
2522 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2523 m_freem(bf->bf_m);
2524 bf->bf_m = NULL;
2525 bf->bf_node = NULL;
2526
2527 ath_txbuf_critsect_begin(sc, s2);
2528 TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
2529 ath_txbuf_critsect_end(sc, s2);
2530 }
2531 ifp->if_flags &= ~IFF_OACTIVE;
2532 sc->sc_tx_timer = 0;
2533
2534 ath_start(ifp);
2535 }
2536
2537 /*
2538 * Drain the transmit queue and reclaim resources.
2539 */
2540 static void
2541 ath_draintxq(struct ath_softc *sc)
2542 {
2543 struct ath_hal *ah = sc->sc_ah;
2544 struct ifnet *ifp = &sc->sc_ic.ic_if;
2545 struct ath_buf *bf;
2546 ath_txq_critsect_decl(s);
2547 ath_txbuf_critsect_decl(s2);
2548
2549 /* XXX return value */
2550 if (!sc->sc_invalid) {
2551 /* don't touch the hardware if marked invalid */
2552 (void) ath_hal_stoptxdma(ah, sc->sc_txhalq);
2553 DPRINTF(("ath_draintxq: tx queue %p, link %p\n",
2554 (caddr_t) ath_hal_gettxbuf(ah, sc->sc_txhalq),
2555 sc->sc_txlink));
2556 (void) ath_hal_stoptxdma(ah, sc->sc_bhalq);
2557 DPRINTF(("ath_draintxq: beacon queue %p\n",
2558 (caddr_t) ath_hal_gettxbuf(ah, sc->sc_bhalq)));
2559 }
2560 for (;;) {
2561 ath_txq_critsect_begin(sc, s);
2562 bf = TAILQ_FIRST(&sc->sc_txq);
2563 if (bf == NULL) {
2564 sc->sc_txlink = NULL;
2565 ath_txq_critsect_end(sc, s);
2566 break;
2567 }
2568 TAILQ_REMOVE(&sc->sc_txq, bf, bf_list);
2569 ath_txq_critsect_end(sc, s);
2570 #ifdef AR_DEBUG
2571 if (ath_debug)
2572 ath_printtxbuf(bf,
2573 ath_hal_txprocdesc(ah, bf->bf_desc) == HAL_OK);
2574 #endif /* AR_DEBUG */
2575 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
2576 m_freem(bf->bf_m);
2577 bf->bf_m = NULL;
2578 bf->bf_node = NULL;
2579 ath_txbuf_critsect_begin(sc, s2);
2580 TAILQ_INSERT_TAIL(&sc->sc_txbuf, bf, bf_list);
2581 ath_txbuf_critsect_end(sc, s2);
2582 }
2583 ifp->if_flags &= ~IFF_OACTIVE;
2584 sc->sc_tx_timer = 0;
2585 }
2586
2587 /*
2588 * Disable the receive h/w in preparation for a reset.
2589 */
2590 static void
2591 ath_stoprecv(struct ath_softc *sc)
2592 {
2593 struct ath_hal *ah = sc->sc_ah;
2594
2595 ath_hal_stoppcurecv(ah); /* disable PCU */
2596 ath_hal_setrxfilter(ah, 0); /* clear recv filter */
2597 ath_hal_stopdmarecv(ah); /* disable DMA engine */
2598 DELAY(3000); /* long enough for 1 frame */
2599 #ifdef AR_DEBUG
2600 if (ath_debug) {
2601 struct ath_buf *bf;
2602
2603 DPRINTF(("ath_stoprecv: rx queue %p, link %p\n",
2604 (caddr_t) ath_hal_getrxbuf(ah), sc->sc_rxlink));
2605 TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
2606 if (ath_hal_rxprocdesc(ah, bf->bf_desc) == HAL_OK)
2607 ath_printrxbuf(bf, 1);
2608 }
2609 }
2610 #endif
2611 sc->sc_rxlink = NULL; /* just in case */
2612 }
2613
2614 /*
2615 * Enable the receive h/w following a reset.
2616 */
2617 static int
2618 ath_startrecv(struct ath_softc *sc)
2619 {
2620 struct ath_hal *ah = sc->sc_ah;
2621 struct ath_buf *bf;
2622
2623 sc->sc_rxlink = NULL;
2624 TAILQ_FOREACH(bf, &sc->sc_rxbuf, bf_list) {
2625 int error = ath_rxbuf_init(sc, bf);
2626 if (error != 0) {
2627 DPRINTF(("ath_startrecv: ath_rxbuf_init failed %d\n",
2628 error));
2629 return error;
2630 }
2631 }
2632
2633 bf = TAILQ_FIRST(&sc->sc_rxbuf);
2634 ath_hal_putrxbuf(ah, bf->bf_daddr);
2635 ath_hal_rxena(ah); /* enable recv descriptors */
2636 ath_mode_init(sc); /* set filters, etc. */
2637 ath_hal_startpcurecv(ah); /* re-enable PCU/DMA engine */
2638 return 0;
2639 }
2640
2641 /*
2642 * Set/change channels. If the channel is really being changed,
2643 * it's done by resetting the chip. To accomplish this we must
2644 * first cleanup any pending DMA, then restart stuff after a la
2645 * ath_init.
2646 */
2647 static int
2648 ath_chan_set(struct ath_softc *sc, struct ieee80211_channel *chan)
2649 {
2650 struct ath_hal *ah = sc->sc_ah;
2651 struct ieee80211com *ic = &sc->sc_ic;
2652
2653 DPRINTF(("ath_chan_set: %u (%u MHz) -> %u (%u MHz)\n",
2654 ieee80211_chan2ieee(ic, ic->ic_ibss_chan),
2655 ic->ic_ibss_chan->ic_freq,
2656 ieee80211_chan2ieee(ic, chan), chan->ic_freq));
2657 if (chan != ic->ic_ibss_chan) {
2658 HAL_STATUS status;
2659 HAL_CHANNEL hchan;
2660 enum ieee80211_phymode mode;
2661
2662 /*
2663 * To switch channels clear any pending DMA operations;
2664 * wait long enough for the RX fifo to drain, reset the
2665 * hardware at the new frequency, and then re-enable
2666 * the relevant bits of the h/w.
2667 */
2668 ath_hal_intrset(ah, 0); /* disable interrupts */
2669 ath_draintxq(sc); /* clear pending tx frames */
2670 ath_stoprecv(sc); /* turn off frame recv */
2671 /*
2672 * Convert to a HAL channel description with
2673 * the flags constrained to reflect the current
2674 * operating mode.
2675 */
2676 hchan.channel = chan->ic_freq;
2677 hchan.channelFlags = ath_chan2flags(ic, chan);
2678 if (!ath_hal_reset(ah, ic->ic_opmode, &hchan, AH_TRUE, &status)) {
2679 if_printf(&ic->ic_if, "ath_chan_set: unable to reset "
2680 "channel %u (%u Mhz)\n",
2681 ieee80211_chan2ieee(ic, chan), chan->ic_freq);
2682 return EIO;
2683 }
2684 /*
2685 * Re-enable rx framework.
2686 */
2687 if (ath_startrecv(sc) != 0) {
2688 if_printf(&ic->ic_if,
2689 "ath_chan_set: unable to restart recv logic\n");
2690 return EIO;
2691 }
2692
2693 /*
2694 * Update BPF state.
2695 */
2696 sc->sc_tx_th.wt_chan_freq = sc->sc_rx_th.wr_chan_freq =
2697 htole16(chan->ic_freq);
2698 sc->sc_tx_th.wt_chan_flags = sc->sc_rx_th.wr_chan_flags =
2699 htole16(chan->ic_flags);
2700
2701 /*
2702 * Change channels and update the h/w rate map
2703 * if we're switching; e.g. 11a to 11b/g.
2704 */
2705 ic->ic_ibss_chan = chan;
2706 mode = ieee80211_chan2mode(ic, chan);
2707 if (mode != sc->sc_curmode)
2708 ath_setcurmode(sc, mode);
2709
2710 /*
2711 * Re-enable interrupts.
2712 */
2713 ath_hal_intrset(ah, sc->sc_imask);
2714 }
2715 return 0;
2716 }
2717
2718 static void
2719 ath_next_scan(void *arg)
2720 {
2721 struct ath_softc *sc = arg;
2722 struct ieee80211com *ic = &sc->sc_ic;
2723 struct ifnet *ifp = &ic->ic_if;
2724 int s;
2725
2726 /* don't call ath_start w/o network interrupts blocked */
2727 s = splnet();
2728
2729 if (ic->ic_state == IEEE80211_S_SCAN)
2730 ieee80211_next_scan(ifp);
2731 splx(s);
2732 }
2733
2734 /*
2735 * Periodically recalibrate the PHY to account
2736 * for temperature/environment changes.
2737 */
2738 static void
2739 ath_calibrate(void *arg)
2740 {
2741 struct ath_softc *sc = arg;
2742 struct ath_hal *ah = sc->sc_ah;
2743 struct ieee80211com *ic = &sc->sc_ic;
2744 struct ieee80211_channel *c;
2745 HAL_CHANNEL hchan;
2746
2747 sc->sc_stats.ast_per_cal++;
2748
2749 /*
2750 * Convert to a HAL channel description with the flags
2751 * constrained to reflect the current operating mode.
2752 */
2753 c = ic->ic_ibss_chan;
2754 hchan.channel = c->ic_freq;
2755 hchan.channelFlags = ath_chan2flags(ic, c);
2756
2757 DPRINTF(("%s: channel %u/%x\n", __func__, c->ic_freq, c->ic_flags));
2758
2759 if (ath_hal_getrfgain(ah) == HAL_RFGAIN_NEED_CHANGE) {
2760 /*
2761 * Rfgain is out of bounds, reset the chip
2762 * to load new gain values.
2763 */
2764 sc->sc_stats.ast_per_rfgain++;
2765 ath_reset(sc);
2766 }
2767 if (!ath_hal_calibrate(ah, &hchan)) {
2768 DPRINTF(("%s: calibration of channel %u failed\n",
2769 __func__, c->ic_freq));
2770 sc->sc_stats.ast_per_calfail++;
2771 }
2772 callout_reset(&sc->sc_cal_ch, hz * ath_calinterval, ath_calibrate, sc);
2773 }
2774
2775 static HAL_LED_STATE
2776 ath_state_to_led(enum ieee80211_state state)
2777 {
2778 switch (state) {
2779 case IEEE80211_S_INIT:
2780 return HAL_LED_INIT;
2781 case IEEE80211_S_SCAN:
2782 return HAL_LED_SCAN;
2783 case IEEE80211_S_AUTH:
2784 return HAL_LED_AUTH;
2785 case IEEE80211_S_ASSOC:
2786 return HAL_LED_ASSOC;
2787 case IEEE80211_S_RUN:
2788 return HAL_LED_RUN;
2789 default:
2790 panic("%s: unknown 802.11 state %d\n", __func__, state);
2791 return HAL_LED_INIT;
2792 }
2793 }
2794
2795 static int
2796 ath_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
2797 {
2798 struct ifnet *ifp = &ic->ic_if;
2799 struct ath_softc *sc = ifp->if_softc;
2800 struct ath_hal *ah = sc->sc_ah;
2801 struct ieee80211_node *ni;
2802 int i, error;
2803 u_int8_t *bssid;
2804 u_int32_t rfilt;
2805
2806 DPRINTF(("%s: %s -> %s\n", __func__,
2807 ieee80211_state_name[ic->ic_state],
2808 ieee80211_state_name[nstate]));
2809
2810 ath_hal_setledstate(ah, ath_state_to_led(nstate)); /* set LED */
2811
2812 if (nstate == IEEE80211_S_INIT) {
2813 sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
2814 ath_hal_intrset(ah, sc->sc_imask);
2815 callout_stop(&sc->sc_scan_ch);
2816 callout_stop(&sc->sc_cal_ch);
2817 return (*sc->sc_newstate)(ic, nstate, arg);
2818 }
2819 ni = ic->ic_bss;
2820 error = ath_chan_set(sc, ni->ni_chan);
2821 if (error != 0)
2822 goto bad;
2823 rfilt = (ath_hal_getrxfilter(ah) & HAL_RX_FILTER_PHYERR)
2824 | HAL_RX_FILTER_UCAST | HAL_RX_FILTER_BCAST | HAL_RX_FILTER_MCAST;
2825 if (ic->ic_opmode != IEEE80211_M_STA)
2826 rfilt |= HAL_RX_FILTER_PROBEREQ;
2827 if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
2828 (ifp->if_flags & IFF_PROMISC))
2829 rfilt |= HAL_RX_FILTER_PROM;
2830 if (nstate == IEEE80211_S_SCAN) {
2831 callout_reset(&sc->sc_scan_ch, (hz * ath_dwelltime) / 1000,
2832 ath_next_scan, sc);
2833 bssid = ifp->if_broadcastaddr;
2834 rfilt |= HAL_RX_FILTER_BEACON;
2835 } else {
2836 callout_stop(&sc->sc_scan_ch);
2837 bssid = ni->ni_bssid;
2838 }
2839 ath_hal_setrxfilter(ah, rfilt);
2840 DPRINTF(("%s: RX filter 0x%x bssid %s\n",
2841 __func__, rfilt, ether_sprintf(bssid)));
2842
2843 if (nstate == IEEE80211_S_RUN && ic->ic_opmode == IEEE80211_M_STA)
2844 ath_hal_setassocid(ah, bssid, ni->ni_associd);
2845 else
2846 ath_hal_setassocid(ah, bssid, 0);
2847 if (ic->ic_flags & IEEE80211_F_WEPON) {
2848 for (i = 0; i < IEEE80211_WEP_NKID; i++)
2849 if (ath_hal_keyisvalid(ah, i))
2850 ath_hal_keysetmac(ah, i, bssid);
2851 }
2852
2853 if (nstate == IEEE80211_S_RUN) {
2854 DPRINTF(("%s(RUN): ic_flags=0x%08x iv=%d bssid=%s "
2855 "capinfo=0x%04x chan=%d\n"
2856 , __func__
2857 , ic->ic_flags
2858 , ni->ni_intval
2859 , ether_sprintf(ni->ni_bssid)
2860 , ni->ni_capinfo
2861 , ieee80211_chan2ieee(ic, ni->ni_chan)));
2862
2863 /*
2864 * Allocate and setup the beacon frame for AP or adhoc mode.
2865 */
2866 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
2867 ic->ic_opmode == IEEE80211_M_IBSS) {
2868 error = ath_beacon_alloc(sc, ni);
2869 if (error != 0)
2870 goto bad;
2871 }
2872
2873 /*
2874 * Configure the beacon and sleep timers.
2875 */
2876 ath_beacon_config(sc);
2877
2878 /* start periodic recalibration timer */
2879 callout_reset(&sc->sc_cal_ch, hz * ath_calinterval,
2880 ath_calibrate, sc);
2881 } else {
2882 sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
2883 ath_hal_intrset(ah, sc->sc_imask);
2884 callout_stop(&sc->sc_cal_ch); /* no calibration */
2885 }
2886 /*
2887 * Reset the rate control state.
2888 */
2889 ath_rate_ctl_reset(sc, nstate);
2890 /*
2891 * Invoke the parent method to complete the work.
2892 */
2893 return (*sc->sc_newstate)(ic, nstate, arg);
2894 bad:
2895 callout_stop(&sc->sc_scan_ch);
2896 callout_stop(&sc->sc_cal_ch);
2897 /* NB: do not invoke the parent */
2898 return error;
2899 }
2900
2901 /*
2902 * Setup driver-specific state for a newly associated node.
2903 * Note that we're called also on a re-associate, the isnew
2904 * param tells us if this is the first time or not.
2905 */
2906 static void
2907 ath_newassoc(struct ieee80211com *ic, struct ieee80211_node *ni, int isnew)
2908 {
2909 if (isnew) {
2910 struct ath_node *an = (struct ath_node *) ni;
2911
2912 an->an_tx_ok = an->an_tx_err =
2913 an->an_tx_retr = an->an_tx_upper = 0;
2914 /* start with highest negotiated rate */
2915 /*
2916 * XXX should do otherwise but only when
2917 * the rate control algorithm is better.
2918 */
2919 KASSERT(ni->ni_rates.rs_nrates > 0,
2920 ("new association w/ no rates!"));
2921 ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
2922 }
2923 }
2924
2925 static int
2926 ath_getchannels(struct ath_softc *sc, u_int cc, HAL_BOOL outdoor)
2927 {
2928 struct ieee80211com *ic = &sc->sc_ic;
2929 struct ifnet *ifp = &ic->ic_if;
2930 struct ath_hal *ah = sc->sc_ah;
2931 HAL_CHANNEL *chans;
2932 int i, ix, nchan;
2933
2934 sc->sc_have11g = 0;
2935 chans = malloc(IEEE80211_CHAN_MAX * sizeof(HAL_CHANNEL),
2936 M_TEMP, M_NOWAIT);
2937 if (chans == NULL) {
2938 if_printf(ifp, "unable to allocate channel table\n");
2939 return ENOMEM;
2940 }
2941 if (!ath_hal_init_channels(ah, chans, IEEE80211_CHAN_MAX, &nchan,
2942 cc, HAL_MODE_ALL, outdoor)) {
2943 if_printf(ifp, "unable to collect channel list from hal\n");
2944 free(chans, M_TEMP);
2945 return EINVAL;
2946 }
2947
2948 /*
2949 * Convert HAL channels to ieee80211 ones and insert
2950 * them in the table according to their channel number.
2951 */
2952 for (i = 0; i < nchan; i++) {
2953 HAL_CHANNEL *c = &chans[i];
2954 ix = ath_hal_mhz2ieee(c->channel, c->channelFlags);
2955 if (ix > IEEE80211_CHAN_MAX) {
2956 if_printf(ifp, "bad hal channel %u (%u/%x) ignored\n",
2957 ix, c->channel, c->channelFlags);
2958 continue;
2959 }
2960 /* NB: flags are known to be compatible */
2961 if (ic->ic_channels[ix].ic_freq == 0) {
2962 ic->ic_channels[ix].ic_freq = c->channel;
2963 ic->ic_channels[ix].ic_flags = c->channelFlags;
2964 } else {
2965 /* channels overlap; e.g. 11g and 11b */
2966 ic->ic_channels[ix].ic_flags |= c->channelFlags;
2967 }
2968 if ((c->channelFlags & CHANNEL_G) == CHANNEL_G)
2969 sc->sc_have11g = 1;
2970 }
2971 free(chans, M_TEMP);
2972 return 0;
2973 }
2974
2975 static int
2976 ath_rate_setup(struct ath_softc *sc, u_int mode)
2977 {
2978 struct ath_hal *ah = sc->sc_ah;
2979 struct ieee80211com *ic = &sc->sc_ic;
2980 const HAL_RATE_TABLE *rt;
2981 struct ieee80211_rateset *rs;
2982 int i, maxrates;
2983
2984 switch (mode) {
2985 case IEEE80211_MODE_11A:
2986 sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11A);
2987 break;
2988 case IEEE80211_MODE_11B:
2989 sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11B);
2990 break;
2991 case IEEE80211_MODE_11G:
2992 sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_11G);
2993 break;
2994 case IEEE80211_MODE_TURBO:
2995 sc->sc_rates[mode] = ath_hal_getratetable(ah, HAL_MODE_TURBO);
2996 break;
2997 default:
2998 DPRINTF(("%s: invalid mode %u\n", __func__, mode));
2999 return 0;
3000 }
3001 rt = sc->sc_rates[mode];
3002 if (rt == NULL)
3003 return 0;
3004 if (rt->rateCount > IEEE80211_RATE_MAXSIZE) {
3005 DPRINTF(("%s: rate table too small (%u > %u)\n",
3006 __func__, rt->rateCount, IEEE80211_RATE_MAXSIZE));
3007 maxrates = IEEE80211_RATE_MAXSIZE;
3008 } else
3009 maxrates = rt->rateCount;
3010 rs = &ic->ic_sup_rates[mode];
3011 for (i = 0; i < maxrates; i++)
3012 rs->rs_rates[i] = rt->info[i].dot11Rate;
3013 rs->rs_nrates = maxrates;
3014 return 1;
3015 }
3016
3017 static void
3018 ath_setcurmode(struct ath_softc *sc, enum ieee80211_phymode mode)
3019 {
3020 const HAL_RATE_TABLE *rt;
3021 int i;
3022
3023 memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
3024 rt = sc->sc_rates[mode];
3025 KASSERT(rt != NULL, ("no h/w rate set for phy mode %u", mode));
3026 for (i = 0; i < rt->rateCount; i++)
3027 sc->sc_rixmap[rt->info[i].dot11Rate & IEEE80211_RATE_VAL] = i;
3028 memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
3029 for (i = 0; i < 32; i++)
3030 sc->sc_hwmap[i] = rt->info[rt->rateCodeToIndex[i]].dot11Rate;
3031 sc->sc_currates = rt;
3032 sc->sc_curmode = mode;
3033 }
3034
3035 /*
3036 * Reset the rate control state for each 802.11 state transition.
3037 */
3038 static void
3039 ath_rate_ctl_reset(struct ath_softc *sc, enum ieee80211_state state)
3040 {
3041 struct ieee80211com *ic = &sc->sc_ic;
3042 struct ieee80211_node *ni;
3043 struct ath_node *an;
3044
3045 an = (struct ath_node *) ic->ic_bss;
3046 an->an_tx_ok = an->an_tx_err = an->an_tx_retr = an->an_tx_upper = 0;
3047 if (ic->ic_opmode == IEEE80211_M_STA) {
3048 ni = ic->ic_bss;
3049 if (state == IEEE80211_S_RUN) {
3050 /* start with highest negotiated rate */
3051 KASSERT(ni->ni_rates.rs_nrates > 0,
3052 ("transition to RUN state w/ no rates!"));
3053 ni->ni_txrate = ni->ni_rates.rs_nrates - 1;
3054 } else {
3055 /* use lowest rate */
3056 ni->ni_txrate = 0;
3057 }
3058 } else {
3059 TAILQ_FOREACH(ni, &ic->ic_node, ni_list) {
3060 ni->ni_txrate = 0; /* use lowest rate */
3061 an = (struct ath_node *) ni;
3062 an->an_tx_ok = an->an_tx_err = an->an_tx_retr =
3063 an->an_tx_upper = 0;
3064 }
3065 }
3066 }
3067
3068 /*
3069 * Examine and potentially adjust the transmit rate.
3070 */
3071 static void
3072 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
3073 {
3074 struct ath_softc *sc = arg;
3075 struct ath_node *an = (struct ath_node *) ni;
3076 struct ieee80211_rateset *rs = &ni->ni_rates;
3077 int mod = 0, orate, enough;
3078
3079 /*
3080 * Rate control
3081 * XXX: very primitive version.
3082 */
3083 sc->sc_stats.ast_rate_calls++;
3084
3085 enough = (an->an_tx_ok + an->an_tx_err >= 10);
3086
3087 /* no packet reached -> down */
3088 if (an->an_tx_err > 0 && an->an_tx_ok == 0)
3089 mod = -1;
3090
3091 /* all packets needs retry in average -> down */
3092 if (enough && an->an_tx_ok < an->an_tx_retr)
3093 mod = -1;
3094
3095 /* no error and less than 10% of packets needs retry -> up */
3096 if (enough && an->an_tx_err == 0 && an->an_tx_ok > an->an_tx_retr * 10)
3097 mod = 1;
3098
3099 orate = ni->ni_txrate;
3100 switch (mod) {
3101 case 0:
3102 if (enough && an->an_tx_upper > 0)
3103 an->an_tx_upper--;
3104 break;
3105 case -1:
3106 if (ni->ni_txrate > 0) {
3107 ni->ni_txrate--;
3108 sc->sc_stats.ast_rate_drop++;
3109 }
3110 an->an_tx_upper = 0;
3111 break;
3112 case 1:
3113 if (++an->an_tx_upper < 2)
3114 break;
3115 an->an_tx_upper = 0;
3116 if (ni->ni_txrate + 1 < rs->rs_nrates) {
3117 ni->ni_txrate++;
3118 sc->sc_stats.ast_rate_raise++;
3119 }
3120 break;
3121 }
3122
3123 if (ni->ni_txrate != orate) {
3124 DPRINTF(("%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
3125 __func__,
3126 (rs->rs_rates[orate] & IEEE80211_RATE_VAL) / 2,
3127 (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
3128 an->an_tx_ok, an->an_tx_err, an->an_tx_retr));
3129 }
3130 if (ni->ni_txrate != orate || enough)
3131 an->an_tx_ok = an->an_tx_err = an->an_tx_retr = 0;
3132 }
3133
3134 #ifdef AR_DEBUG
3135 #ifdef __FreeBSD__
3136 static int
3137 sysctl_hw_ath_dump(SYSCTL_HANDLER_ARGS)
3138 {
3139 char dmode[64];
3140 int error;
3141
3142 strncpy(dmode, "", sizeof(dmode) - 1);
3143 dmode[sizeof(dmode) - 1] = '\0';
3144 error = sysctl_handle_string(oidp, &dmode[0], sizeof(dmode), req);
3145
3146 if (error == 0 && req->newptr != NULL) {
3147 struct ifnet *ifp;
3148 struct ath_softc *sc;
3149
3150 ifp = ifunit("ath0"); /* XXX */
3151 if (!ifp)
3152 return EINVAL;
3153 sc = ifp->if_softc;
3154 if (strcmp(dmode, "hal") == 0)
3155 ath_hal_dumpstate(sc->sc_ah);
3156 else if (strcmp(dmode, "eeprom") == 0)
3157 ath_hal_dumpeeprom(sc->sc_ah);
3158 else if (strcmp(dmode, "rfgain") == 0)
3159 ath_hal_dumprfgain(sc->sc_ah);
3160 else if (strcmp(dmode, "ani") == 0)
3161 ath_hal_dumpani(sc->sc_ah);
3162 else
3163 return EINVAL;
3164 }
3165 return error;
3166 }
3167 SYSCTL_PROC(_hw_ath, OID_AUTO, dump, CTLTYPE_STRING | CTLFLAG_RW,
3168 0, 0, sysctl_hw_ath_dump, "A", "Dump driver state");
3169 #endif /* __FreeBSD__ */
3170
3171 static void
3172 ath_printrxbuf(struct ath_buf *bf, int done)
3173 {
3174 struct ath_desc *ds;
3175 int i;
3176
3177 for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
3178 printf("R%d (%p %p) %08x %08x %08x %08x %08x %08x %c\n",
3179 i, ds, (struct ath_desc *)bf->bf_daddr + i,
3180 ds->ds_link, ds->ds_data,
3181 ds->ds_ctl0, ds->ds_ctl1,
3182 ds->ds_hw[0], ds->ds_hw[1],
3183 !done ? ' ' : (ds->ds_rxstat.rs_status == 0) ? '*' : '!');
3184 }
3185 }
3186
3187 static void
3188 ath_printtxbuf(struct ath_buf *bf, int done)
3189 {
3190 struct ath_desc *ds;
3191 int i;
3192
3193 for (i = 0, ds = bf->bf_desc; i < bf->bf_nseg; i++, ds++) {
3194 printf("T%d (%p %p) %08x %08x %08x %08x %08x %08x %08x %08x %c\n",
3195 i, ds, (struct ath_desc *)bf->bf_daddr + i,
3196 ds->ds_link, ds->ds_data,
3197 ds->ds_ctl0, ds->ds_ctl1,
3198 ds->ds_hw[0], ds->ds_hw[1], ds->ds_hw[2], ds->ds_hw[3],
3199 !done ? ' ' : (ds->ds_txstat.ts_status == 0) ? '*' : '!');
3200 }
3201 }
3202 #endif /* AR_DEBUG */
3203