2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Driver for the Atheros Wireless LAN controller.
36 * This software is derived from work of Atsushi Onoe; his contribution
37 * is greatly appreciated.
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysctl.h>
48 #include <sys/malloc.h>
50 #include <sys/mutex.h>
51 #include <sys/kernel.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/errno.h>
55 #include <sys/callout.h>
57 #include <sys/endian.h>
58 #include <sys/kthread.h>
59 #include <sys/taskqueue.h>
62 #include <machine/bus.h>
65 #include <net/if_dl.h>
66 #include <net/if_media.h>
67 #include <net/if_types.h>
68 #include <net/if_arp.h>
69 #include <net/ethernet.h>
70 #include <net/if_llc.h>
72 #include <net80211/ieee80211_var.h>
73 #include <net80211/ieee80211_regdomain.h>
74 #ifdef IEEE80211_SUPPORT_SUPERG
75 #include <net80211/ieee80211_superg.h>
77 #ifdef IEEE80211_SUPPORT_TDMA
78 #include <net80211/ieee80211_tdma.h>
84 #include <netinet/in.h>
85 #include <netinet/if_ether.h>
88 #include <dev/ath/if_athvar.h>
89 #include <dev/ath/ath_hal/ah_devid.h> /* XXX for softled */
90 #include <dev/ath/ath_hal/ah_diagcodes.h>
92 #include <dev/ath/if_ath_debug.h>
95 #include <dev/ath/ath_tx99/ath_tx99.h>
98 #include <dev/ath/if_ath_misc.h>
99 #include <dev/ath/if_ath_tx.h>
100 #include <dev/ath/if_ath_tx_ht.h>
103 * Whether to use the 11n rate scenario functions or not
106 ath_tx_is_11n(struct ath_softc *sc)
108 return (sc->sc_ah->ah_magic == 0x20065416);
112 ath_txfrag_cleanup(struct ath_softc *sc,
113 ath_bufhead *frags, struct ieee80211_node *ni)
115 struct ath_buf *bf, *next;
117 ATH_TXBUF_LOCK_ASSERT(sc);
119 STAILQ_FOREACH_SAFE(bf, frags, bf_list, next) {
120 /* NB: bf assumed clean */
121 STAILQ_REMOVE_HEAD(frags, bf_list);
122 STAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
123 ieee80211_node_decref(ni);
128 * Setup xmit of a fragmented frame. Allocate a buffer
129 * for each frag and bump the node reference count to
130 * reflect the held reference to be setup by ath_tx_start.
133 ath_txfrag_setup(struct ath_softc *sc, ath_bufhead *frags,
134 struct mbuf *m0, struct ieee80211_node *ni)
140 for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) {
141 bf = _ath_getbuf_locked(sc);
142 if (bf == NULL) { /* out of buffers, cleanup */
143 ath_txfrag_cleanup(sc, frags, ni);
146 ieee80211_node_incref(ni);
147 STAILQ_INSERT_TAIL(frags, bf, bf_list);
149 ATH_TXBUF_UNLOCK(sc);
151 return !STAILQ_EMPTY(frags);
155 * Reclaim mbuf resources. For fragmented frames we
156 * need to claim each frag chained with m_nextpkt.
159 ath_freetx(struct mbuf *m)
167 } while ((m = next) != NULL);
171 ath_tx_dmasetup(struct ath_softc *sc, struct ath_buf *bf, struct mbuf *m0)
177 * Load the DMA map so any coalescing is done. This
178 * also calculates the number of descriptors we need.
180 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
181 bf->bf_segs, &bf->bf_nseg,
183 if (error == EFBIG) {
184 /* XXX packet requires too many descriptors */
185 bf->bf_nseg = ATH_TXDESC+1;
186 } else if (error != 0) {
187 sc->sc_stats.ast_tx_busdma++;
192 * Discard null packets and check for packets that
193 * require too many TX descriptors. We try to convert
194 * the latter to a cluster.
196 if (bf->bf_nseg > ATH_TXDESC) { /* too many desc's, linearize */
197 sc->sc_stats.ast_tx_linear++;
198 m = m_collapse(m0, M_NOWAIT, ATH_TXDESC);
201 sc->sc_stats.ast_tx_nombuf++;
205 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
206 bf->bf_segs, &bf->bf_nseg,
209 sc->sc_stats.ast_tx_busdma++;
213 KASSERT(bf->bf_nseg <= ATH_TXDESC,
214 ("too many segments after defrag; nseg %u", bf->bf_nseg));
215 } else if (bf->bf_nseg == 0) { /* null packet, discard */
216 sc->sc_stats.ast_tx_nodata++;
220 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: m %p len %u\n",
221 __func__, m0, m0->m_pkthdr.len);
222 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
229 ath_tx_chaindesclist(struct ath_softc *sc, struct ath_txq *txq, struct ath_buf *bf)
231 struct ath_hal *ah = sc->sc_ah;
232 struct ath_desc *ds, *ds0;
236 * Fillin the remainder of the descriptor info.
238 ds0 = ds = bf->bf_desc;
239 for (i = 0; i < bf->bf_nseg; i++, ds++) {
240 ds->ds_data = bf->bf_segs[i].ds_addr;
241 if (i == bf->bf_nseg - 1)
244 ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
245 ath_hal_filltxdesc(ah, ds
246 , bf->bf_segs[i].ds_len /* segment length */
247 , i == 0 /* first segment */
248 , i == bf->bf_nseg - 1 /* last segment */
249 , ds0 /* first descriptor */
251 DPRINTF(sc, ATH_DEBUG_XMIT,
252 "%s: %d: %08x %08x %08x %08x %08x %08x\n",
253 __func__, i, ds->ds_link, ds->ds_data,
254 ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]);
260 ath_tx_handoff(struct ath_softc *sc, struct ath_txq *txq, struct ath_buf *bf)
262 struct ath_hal *ah = sc->sc_ah;
264 /* Fill in the details in the descriptor list */
265 ath_tx_chaindesclist(sc, txq, bf);
268 * Insert the frame on the outbound list and pass it on
269 * to the hardware. Multicast frames buffered for power
270 * save stations and transmit from the CAB queue are stored
271 * on a s/w only queue and loaded on to the CAB queue in
272 * the SWBA handler since frames only go out on DTIM and
273 * to avoid possible races.
276 KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
277 ("busy status 0x%x", bf->bf_flags));
278 if (txq->axq_qnum != ATH_TXQ_SWQ) {
279 #ifdef IEEE80211_SUPPORT_TDMA
282 ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
283 qbusy = ath_hal_txqenabled(ah, txq->axq_qnum);
284 if (txq->axq_link == NULL) {
286 * Be careful writing the address to TXDP. If
287 * the tx q is enabled then this write will be
288 * ignored. Normally this is not an issue but
289 * when tdma is in use and the q is beacon gated
290 * this race can occur. If the q is busy then
291 * defer the work to later--either when another
292 * packet comes along or when we prepare a beacon
296 ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
297 txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
298 DPRINTF(sc, ATH_DEBUG_XMIT,
299 "%s: TXDP[%u] = %p (%p) depth %d\n",
300 __func__, txq->axq_qnum,
301 (caddr_t)bf->bf_daddr, bf->bf_desc,
304 txq->axq_flags |= ATH_TXQ_PUTPENDING;
305 DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
306 "%s: Q%u busy, defer enable\n", __func__,
310 *txq->axq_link = bf->bf_daddr;
311 DPRINTF(sc, ATH_DEBUG_XMIT,
312 "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
313 txq->axq_qnum, txq->axq_link,
314 (caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth);
315 if ((txq->axq_flags & ATH_TXQ_PUTPENDING) && !qbusy) {
317 * The q was busy when we previously tried
318 * to write the address of the first buffer
319 * in the chain. Since it's not busy now
320 * handle this chore. We are certain the
321 * buffer at the front is the right one since
322 * axq_link is NULL only when the buffer list
325 ath_hal_puttxbuf(ah, txq->axq_qnum,
326 STAILQ_FIRST(&txq->axq_q)->bf_daddr);
327 txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
328 DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
329 "%s: Q%u restarted\n", __func__,
334 ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
335 if (txq->axq_link == NULL) {
336 ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
337 DPRINTF(sc, ATH_DEBUG_XMIT,
338 "%s: TXDP[%u] = %p (%p) depth %d\n",
339 __func__, txq->axq_qnum,
340 (caddr_t)bf->bf_daddr, bf->bf_desc,
343 *txq->axq_link = bf->bf_daddr;
344 DPRINTF(sc, ATH_DEBUG_XMIT,
345 "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
346 txq->axq_qnum, txq->axq_link,
347 (caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth);
349 #endif /* IEEE80211_SUPPORT_TDMA */
350 txq->axq_link = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
351 ath_hal_txstart(ah, txq->axq_qnum);
353 if (txq->axq_link != NULL) {
354 struct ath_buf *last = ATH_TXQ_LAST(txq);
355 struct ieee80211_frame *wh;
357 /* mark previous frame */
358 wh = mtod(last->bf_m, struct ieee80211_frame *);
359 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
360 bus_dmamap_sync(sc->sc_dmat, last->bf_dmamap,
361 BUS_DMASYNC_PREWRITE);
363 /* link descriptor */
364 *txq->axq_link = bf->bf_daddr;
366 ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
367 txq->axq_link = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
373 ath_tx_tag_crypto(struct ath_softc *sc, struct ieee80211_node *ni,
374 struct mbuf *m0, int iswep, int isfrag, int *hdrlen, int *pktlen, int *keyix)
377 const struct ieee80211_cipher *cip;
378 struct ieee80211_key *k;
381 * Construct the 802.11 header+trailer for an encrypted
382 * frame. The only reason this can fail is because of an
383 * unknown or unsupported cipher/key type.
385 k = ieee80211_crypto_encap(ni, m0);
388 * This can happen when the key is yanked after the
389 * frame was queued. Just discard the frame; the
390 * 802.11 layer counts failures and provides
391 * debugging/diagnostics.
396 * Adjust the packet + header lengths for the crypto
397 * additions and calculate the h/w key index. When
398 * a s/w mic is done the frame will have had any mic
399 * added to it prior to entry so m0->m_pkthdr.len will
400 * account for it. Otherwise we need to add it to the
404 (*hdrlen) += cip->ic_header;
405 (*pktlen) += cip->ic_header + cip->ic_trailer;
406 /* NB: frags always have any TKIP MIC done in s/w */
407 if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag)
408 (*pktlen) += cip->ic_miclen;
409 (*keyix) = k->wk_keyix;
410 } else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
412 * Use station key cache slot, if assigned.
414 (*keyix) = ni->ni_ucastkey.wk_keyix;
415 if ((*keyix) == IEEE80211_KEYIX_NONE)
416 (*keyix) = HAL_TXKEYIX_INVALID;
418 (*keyix) = HAL_TXKEYIX_INVALID;
424 ath_tx_get_rtscts_rate(struct ath_hal *ah, const HAL_RATE_TABLE *rt,
425 int rix, int cix, int shortPreamble)
430 * CTS transmit rate is derived from the transmit rate
431 * by looking in the h/w rate table. We must also factor
432 * in whether or not a short preamble is to be used.
434 /* NB: cix is set above where RTS/CTS is enabled */
435 KASSERT(cix != 0xff, ("cix not setup"));
436 ctsrate = rt->info[cix].rateCode;
438 /* XXX this should only matter for legacy rates */
440 ctsrate |= rt->info[cix].shortPreamble;
447 * Calculate the RTS/CTS duration for legacy frames.
450 ath_tx_calc_ctsduration(struct ath_hal *ah, int rix, int cix,
451 int shortPreamble, int pktlen, const HAL_RATE_TABLE *rt,
456 /* This mustn't be called for HT modes */
457 if (rt->info[cix].phy == IEEE80211_T_HT) {
458 printf("%s: HT rate where it shouldn't be (0x%x)\n",
459 __func__, rt->info[cix].rateCode);
464 * Compute the transmit duration based on the frame
465 * size and the size of an ACK frame. We call into the
466 * HAL to do the computation since it depends on the
467 * characteristics of the actual PHY being used.
469 * NB: CTS is assumed the same size as an ACK so we can
470 * use the precalculated ACK durations.
473 if (flags & HAL_TXDESC_RTSENA) /* SIFS + CTS */
474 ctsduration += rt->info[cix].spAckDuration;
475 ctsduration += ath_hal_computetxtime(ah,
476 rt, pktlen, rix, AH_TRUE);
477 if ((flags & HAL_TXDESC_NOACK) == 0) /* SIFS + ACK */
478 ctsduration += rt->info[rix].spAckDuration;
480 if (flags & HAL_TXDESC_RTSENA) /* SIFS + CTS */
481 ctsduration += rt->info[cix].lpAckDuration;
482 ctsduration += ath_hal_computetxtime(ah,
483 rt, pktlen, rix, AH_FALSE);
484 if ((flags & HAL_TXDESC_NOACK) == 0) /* SIFS + ACK */
485 ctsduration += rt->info[rix].lpAckDuration;
492 ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf,
495 struct ieee80211vap *vap = ni->ni_vap;
496 struct ath_vap *avp = ATH_VAP(vap);
497 struct ath_hal *ah = sc->sc_ah;
498 struct ifnet *ifp = sc->sc_ifp;
499 struct ieee80211com *ic = ifp->if_l2com;
500 const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
501 int error, iswep, ismcast, isfrag, ismrr;
502 int keyix, hdrlen, pktlen, try0;
503 u_int8_t rix, txrate, ctsrate;
504 u_int8_t cix = 0xff; /* NB: silence compiler */
507 struct ieee80211_frame *wh;
508 u_int subtype, flags, ctsduration;
510 const HAL_RATE_TABLE *rt;
511 HAL_BOOL shortPreamble;
514 uint8_t try[4], rate[4];
516 bzero(try, sizeof(try));
517 bzero(rate, sizeof(rate));
519 wh = mtod(m0, struct ieee80211_frame *);
520 iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
521 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
522 isfrag = m0->m_flags & M_FRAG;
523 hdrlen = ieee80211_anyhdrsize(wh);
525 * Packet length must not include any
526 * pad bytes; deduct them here.
528 pktlen = m0->m_pkthdr.len - (hdrlen & 3);
530 /* Handle encryption twiddling if needed */
531 if (! ath_tx_tag_crypto(sc, ni, m0, iswep, isfrag, &hdrlen, &pktlen, &keyix)) {
536 /* packet header may have moved, reset our local pointer */
537 wh = mtod(m0, struct ieee80211_frame *);
539 pktlen += IEEE80211_CRC_LEN;
542 * Load the DMA map so any coalescing is done. This
543 * also calculates the number of descriptors we need.
545 error = ath_tx_dmasetup(sc, bf, m0);
548 bf->bf_node = ni; /* NB: held reference */
549 m0 = bf->bf_m; /* NB: may have changed */
550 wh = mtod(m0, struct ieee80211_frame *);
552 /* setup descriptors */
554 rt = sc->sc_currates;
555 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
558 * NB: the 802.11 layer marks whether or not we should
559 * use short preamble based on the current mode and
560 * negotiated parameters.
562 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
563 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
564 shortPreamble = AH_TRUE;
565 sc->sc_stats.ast_tx_shortpre++;
567 shortPreamble = AH_FALSE;
571 flags = HAL_TXDESC_CLRDMASK; /* XXX needed for crypto errs */
572 ismrr = 0; /* default no multi-rate retry*/
573 pri = M_WME_GETAC(m0); /* honor classification */
574 /* XXX use txparams instead of fixed values */
576 * Calculate Atheros packet type from IEEE80211 packet header,
577 * setup for rate calculations, and select h/w transmit queue.
579 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
580 case IEEE80211_FC0_TYPE_MGT:
581 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
582 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
583 atype = HAL_PKT_TYPE_BEACON;
584 else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
585 atype = HAL_PKT_TYPE_PROBE_RESP;
586 else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
587 atype = HAL_PKT_TYPE_ATIM;
589 atype = HAL_PKT_TYPE_NORMAL; /* XXX */
590 rix = an->an_mgmtrix;
591 txrate = rt->info[rix].rateCode;
593 txrate |= rt->info[rix].shortPreamble;
595 flags |= HAL_TXDESC_INTREQ; /* force interrupt */
597 case IEEE80211_FC0_TYPE_CTL:
598 atype = HAL_PKT_TYPE_PSPOLL; /* stop setting of duration */
599 rix = an->an_mgmtrix;
600 txrate = rt->info[rix].rateCode;
602 txrate |= rt->info[rix].shortPreamble;
604 flags |= HAL_TXDESC_INTREQ; /* force interrupt */
606 case IEEE80211_FC0_TYPE_DATA:
607 atype = HAL_PKT_TYPE_NORMAL; /* default */
609 * Data frames: multicast frames go out at a fixed rate,
610 * EAPOL frames use the mgmt frame rate; otherwise consult
611 * the rate control module for the rate to use.
614 rix = an->an_mcastrix;
615 txrate = rt->info[rix].rateCode;
617 txrate |= rt->info[rix].shortPreamble;
619 } else if (m0->m_flags & M_EAPOL) {
620 /* XXX? maybe always use long preamble? */
621 rix = an->an_mgmtrix;
622 txrate = rt->info[rix].rateCode;
624 txrate |= rt->info[rix].shortPreamble;
625 try0 = ATH_TXMAXTRY; /* XXX?too many? */
627 ath_rate_findrate(sc, an, shortPreamble, pktlen,
628 &rix, &try0, &txrate);
629 sc->sc_txrix = rix; /* for LED blinking */
630 sc->sc_lastdatarix = rix; /* for fast frames */
631 if (try0 != ATH_TXMAXTRY)
634 if (cap->cap_wmeParams[pri].wmep_noackPolicy)
635 flags |= HAL_TXDESC_NOACK;
638 if_printf(ifp, "bogus frame type 0x%x (%s)\n",
639 wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
644 txq = sc->sc_ac2q[pri];
647 * When servicing one or more stations in power-save mode
648 * (or) if there is some mcast data waiting on the mcast
649 * queue (to prevent out of order delivery) multicast
650 * frames must be buffered until after the beacon.
652 if (ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth))
653 txq = &avp->av_mcastq;
656 * Calculate miscellaneous flags.
659 flags |= HAL_TXDESC_NOACK; /* no ack on broad/multicast */
660 } else if (pktlen > vap->iv_rtsthreshold &&
661 (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
662 flags |= HAL_TXDESC_RTSENA; /* RTS based on frame length */
663 cix = rt->info[rix].controlRate;
664 sc->sc_stats.ast_tx_rts++;
666 if (flags & HAL_TXDESC_NOACK) /* NB: avoid double counting */
667 sc->sc_stats.ast_tx_noack++;
668 #ifdef IEEE80211_SUPPORT_TDMA
669 if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) {
670 DPRINTF(sc, ATH_DEBUG_TDMA,
671 "%s: discard frame, ACK required w/ TDMA\n", __func__);
672 sc->sc_stats.ast_tdma_ack++;
679 * If 802.11g protection is enabled, determine whether
680 * to use RTS/CTS or just CTS. Note that this is only
681 * done for OFDM unicast frames.
683 if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
684 rt->info[rix].phy == IEEE80211_T_OFDM &&
685 (flags & HAL_TXDESC_NOACK) == 0) {
686 /* XXX fragments must use CCK rates w/ protection */
687 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
688 flags |= HAL_TXDESC_RTSENA;
689 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
690 flags |= HAL_TXDESC_CTSENA;
693 * For frags it would be desirable to use the
694 * highest CCK rate for RTS/CTS. But stations
695 * farther away may detect it at a lower CCK rate
696 * so use the configured protection rate instead
699 cix = rt->info[sc->sc_protrix].controlRate;
701 cix = rt->info[sc->sc_protrix].controlRate;
702 sc->sc_stats.ast_tx_protect++;
707 * If 11n protection is enabled and it's a HT frame,
710 * XXX ic_htprotmode or ic_curhtprotmode?
711 * XXX should it_htprotmode only matter if ic_curhtprotmode
712 * XXX indicates it's not a HT pure environment?
714 if ((ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) &&
715 rt->info[rix].phy == IEEE80211_T_HT &&
716 (flags & HAL_TXDESC_NOACK) == 0) {
717 cix = rt->info[sc->sc_protrix].controlRate;
718 flags |= HAL_TXDESC_RTSENA;
719 sc->sc_stats.ast_tx_htprotect++;
724 * Calculate duration. This logically belongs in the 802.11
725 * layer but it lacks sufficient information to calculate it.
727 if ((flags & HAL_TXDESC_NOACK) == 0 &&
728 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
731 dur = rt->info[rix].spAckDuration;
733 dur = rt->info[rix].lpAckDuration;
734 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
735 dur += dur; /* additional SIFS+ACK */
736 KASSERT(m0->m_nextpkt != NULL, ("no fragment"));
738 * Include the size of next fragment so NAV is
739 * updated properly. The last fragment uses only
742 dur += ath_hal_computetxtime(ah, rt,
743 m0->m_nextpkt->m_pkthdr.len,
748 * Force hardware to use computed duration for next
749 * fragment by disabling multi-rate retry which updates
750 * duration based on the multi-rate duration table.
753 try0 = ATH_TXMGTTRY; /* XXX? */
755 *(u_int16_t *)wh->i_dur = htole16(dur);
759 * Calculate RTS/CTS rate and duration if needed.
762 if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
763 ctsrate = ath_tx_get_rtscts_rate(ah, rt, rix, cix, shortPreamble);
765 /* The 11n chipsets do ctsduration calculations for you */
766 if (! ath_tx_is_11n(sc))
767 ctsduration = ath_tx_calc_ctsduration(ah, rix, cix, shortPreamble,
770 * Must disable multi-rate retry when using RTS/CTS.
773 try0 = ATH_TXMGTTRY; /* XXX */
778 * At this point we are committed to sending the frame
779 * and we don't need to look at m_nextpkt; clear it in
780 * case this frame is part of frag chain.
782 m0->m_nextpkt = NULL;
784 if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
785 ieee80211_dump_pkt(ic, mtod(m0, const uint8_t *), m0->m_len,
786 sc->sc_hwmap[rix].ieeerate, -1);
788 if (ieee80211_radiotap_active_vap(vap)) {
789 u_int64_t tsf = ath_hal_gettsf64(ah);
791 sc->sc_tx_th.wt_tsf = htole64(tsf);
792 sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
794 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
796 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
797 sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
798 sc->sc_tx_th.wt_txpower = ni->ni_txpower;
799 sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
801 ieee80211_radiotap_tx(vap, m0);
805 * Determine if a tx interrupt should be generated for
806 * this descriptor. We take a tx interrupt to reap
807 * descriptors when the h/w hits an EOL condition or
808 * when the descriptor is specifically marked to generate
809 * an interrupt. We periodically mark descriptors in this
810 * way to insure timely replenishing of the supply needed
811 * for sending frames. Defering interrupts reduces system
812 * load and potentially allows more concurrent work to be
813 * done but if done to aggressively can cause senders to
816 * NB: use >= to deal with sc_txintrperiod changing
817 * dynamically through sysctl.
819 if (flags & HAL_TXDESC_INTREQ) {
820 txq->axq_intrcnt = 0;
821 } else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
822 flags |= HAL_TXDESC_INTREQ;
823 txq->axq_intrcnt = 0;
826 if (ath_tx_is_11n(sc)) {
832 * Formulate first tx descriptor with tx controls.
834 /* XXX check return value? */
835 /* XXX is this ok to call for 11n descriptors? */
836 /* XXX or should it go through the first, next, last 11n calls? */
837 ath_hal_setuptxdesc(ah, ds
838 , pktlen /* packet length */
839 , hdrlen /* header length */
840 , atype /* Atheros packet type */
841 , ni->ni_txpower /* txpower */
842 , txrate, try0 /* series 0 rate/tries */
843 , keyix /* key cache index */
844 , sc->sc_txantenna /* antenna mode */
846 , ctsrate /* rts/cts rate */
847 , ctsduration /* rts/cts duration */
849 bf->bf_txflags = flags;
851 * Setup the multi-rate retry state only when we're
852 * going to use it. This assumes ath_hal_setuptxdesc
853 * initializes the descriptors (so we don't have to)
854 * when the hardware supports multi-rate retry and
858 if (ath_tx_is_11n(sc))
859 ath_rate_getxtxrates(sc, an, rix, rate, try);
861 ath_rate_setupxtxdesc(sc, an, ds, shortPreamble, rix);
864 if (ath_tx_is_11n(sc)) {
865 ath_buf_set_rate(sc, ni, bf, pktlen, flags, ctsrate, (atype == HAL_PKT_TYPE_PSPOLL), rate, try);
868 ath_tx_handoff(sc, txq, bf);
873 ath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni,
874 struct ath_buf *bf, struct mbuf *m0,
875 const struct ieee80211_bpf_params *params)
877 struct ifnet *ifp = sc->sc_ifp;
878 struct ieee80211com *ic = ifp->if_l2com;
879 struct ath_hal *ah = sc->sc_ah;
880 struct ieee80211vap *vap = ni->ni_vap;
881 int error, ismcast, ismrr;
882 int keyix, hdrlen, pktlen, try0, txantenna;
883 u_int8_t rix, cix, txrate, ctsrate, rate1, rate2, rate3;
884 struct ieee80211_frame *wh;
885 u_int flags, ctsduration;
887 const HAL_RATE_TABLE *rt;
890 uint8_t try[4], rate[4];
892 bzero(try, sizeof(try));
893 bzero(rate, sizeof(rate));
895 wh = mtod(m0, struct ieee80211_frame *);
896 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
897 hdrlen = ieee80211_anyhdrsize(wh);
899 * Packet length must not include any
900 * pad bytes; deduct them here.
902 /* XXX honor IEEE80211_BPF_DATAPAD */
903 pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN;
905 /* Handle encryption twiddling if needed */
906 if (! ath_tx_tag_crypto(sc, ni, m0, params->ibp_flags & IEEE80211_BPF_CRYPTO, 0, &hdrlen, &pktlen, &keyix)) {
910 /* packet header may have moved, reset our local pointer */
911 wh = mtod(m0, struct ieee80211_frame *);
913 error = ath_tx_dmasetup(sc, bf, m0);
916 m0 = bf->bf_m; /* NB: may have changed */
917 wh = mtod(m0, struct ieee80211_frame *);
918 bf->bf_node = ni; /* NB: held reference */
920 flags = HAL_TXDESC_CLRDMASK; /* XXX needed for crypto errs */
921 flags |= HAL_TXDESC_INTREQ; /* force interrupt */
922 if (params->ibp_flags & IEEE80211_BPF_RTS)
923 flags |= HAL_TXDESC_RTSENA;
924 else if (params->ibp_flags & IEEE80211_BPF_CTS)
925 flags |= HAL_TXDESC_CTSENA;
926 /* XXX leave ismcast to injector? */
927 if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast)
928 flags |= HAL_TXDESC_NOACK;
930 rt = sc->sc_currates;
931 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
932 rix = ath_tx_findrix(sc, params->ibp_rate0);
933 txrate = rt->info[rix].rateCode;
934 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
935 txrate |= rt->info[rix].shortPreamble;
937 try0 = params->ibp_try0;
938 ismrr = (params->ibp_try1 != 0);
939 txantenna = params->ibp_pri >> 2;
940 if (txantenna == 0) /* XXX? */
941 txantenna = sc->sc_txantenna;
944 if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
945 cix = ath_tx_findrix(sc, params->ibp_ctsrate);
946 ctsrate = ath_tx_get_rtscts_rate(ah, rt, rix, cix, params->ibp_flags & IEEE80211_BPF_SHORTPRE);
947 /* The 11n chipsets do ctsduration calculations for you */
948 if (! ath_tx_is_11n(sc))
949 ctsduration = ath_tx_calc_ctsduration(ah, rix, cix,
950 params->ibp_flags & IEEE80211_BPF_SHORTPRE, pktlen,
953 * Must disable multi-rate retry when using RTS/CTS.
959 pri = params->ibp_pri & 3;
961 * NB: we mark all packets as type PSPOLL so the h/w won't
962 * set the sequence number, duration, etc.
964 atype = HAL_PKT_TYPE_PSPOLL;
966 if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
967 ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
968 sc->sc_hwmap[rix].ieeerate, -1);
970 if (ieee80211_radiotap_active_vap(vap)) {
971 u_int64_t tsf = ath_hal_gettsf64(ah);
973 sc->sc_tx_th.wt_tsf = htole64(tsf);
974 sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
975 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
976 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
977 if (m0->m_flags & M_FRAG)
978 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
979 sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
980 sc->sc_tx_th.wt_txpower = ni->ni_txpower;
981 sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
983 ieee80211_radiotap_tx(vap, m0);
987 * Formulate first tx descriptor with tx controls.
990 /* XXX check return value? */
991 ath_hal_setuptxdesc(ah, ds
992 , pktlen /* packet length */
993 , hdrlen /* header length */
994 , atype /* Atheros packet type */
995 , params->ibp_power /* txpower */
996 , txrate, try0 /* series 0 rate/tries */
997 , keyix /* key cache index */
998 , txantenna /* antenna mode */
1000 , ctsrate /* rts/cts rate */
1001 , ctsduration /* rts/cts duration */
1003 bf->bf_txflags = flags;
1005 if (ath_tx_is_11n(sc)) {
1006 rate[0] = ath_tx_findrix(sc, params->ibp_rate0);
1007 try[0] = params->ibp_try0;
1010 /* Remember, rate[] is actually an array of rix's -adrian */
1011 rate[0] = ath_tx_findrix(sc, params->ibp_rate0);
1012 rate[1] = ath_tx_findrix(sc, params->ibp_rate1);
1013 rate[2] = ath_tx_findrix(sc, params->ibp_rate2);
1014 rate[3] = ath_tx_findrix(sc, params->ibp_rate3);
1016 try[0] = params->ibp_try0;
1017 try[1] = params->ibp_try1;
1018 try[2] = params->ibp_try2;
1019 try[3] = params->ibp_try3;
1023 rix = ath_tx_findrix(sc, params->ibp_rate1);
1024 rate1 = rt->info[rix].rateCode;
1025 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
1026 rate1 |= rt->info[rix].shortPreamble;
1027 if (params->ibp_try2) {
1028 rix = ath_tx_findrix(sc, params->ibp_rate2);
1029 rate2 = rt->info[rix].rateCode;
1030 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
1031 rate2 |= rt->info[rix].shortPreamble;
1034 if (params->ibp_try3) {
1035 rix = ath_tx_findrix(sc, params->ibp_rate3);
1036 rate3 = rt->info[rix].rateCode;
1037 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
1038 rate3 |= rt->info[rix].shortPreamble;
1041 ath_hal_setupxtxdesc(ah, ds
1042 , rate1, params->ibp_try1 /* series 1 */
1043 , rate2, params->ibp_try2 /* series 2 */
1044 , rate3, params->ibp_try3 /* series 3 */
1049 if (ath_tx_is_11n(sc)) {
1051 * notice that rix doesn't include any of the "magic" flags txrate
1052 * does for communicating "other stuff" to the HAL.
1054 ath_buf_set_rate(sc, ni, bf, pktlen, flags, ctsrate, (atype == HAL_PKT_TYPE_PSPOLL), rate, try);
1057 /* NB: no buffered multicast in power save support */
1058 ath_tx_handoff(sc, sc->sc_ac2q[pri], bf);
1063 ath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1064 const struct ieee80211_bpf_params *params)
1066 struct ieee80211com *ic = ni->ni_ic;
1067 struct ifnet *ifp = ic->ic_ifp;
1068 struct ath_softc *sc = ifp->if_softc;
1072 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) {
1073 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: discard frame, %s", __func__,
1074 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ?
1075 "!running" : "invalid");
1081 * Grab a TX buffer and associated resources.
1083 bf = ath_getbuf(sc);
1085 sc->sc_stats.ast_tx_nobuf++;
1091 if (params == NULL) {
1093 * Legacy path; interpret frame contents to decide
1094 * precisely how to send the frame.
1096 if (ath_tx_start(sc, ni, bf, m)) {
1097 error = EIO; /* XXX */
1102 * Caller supplied explicit parameters to use in
1103 * sending the frame.
1105 if (ath_tx_raw_start(sc, ni, bf, m, params)) {
1106 error = EIO; /* XXX */
1110 sc->sc_wd_timer = 5;
1112 sc->sc_stats.ast_tx_raw++;
1117 STAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
1118 ATH_TXBUF_UNLOCK(sc);
1121 sc->sc_stats.ast_tx_raw_fail++;
1122 ieee80211_free_node(ni);