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