Deleted Added
sdiff udiff text old ( 227360 ) new ( 227364 )
full compact
1/*-
2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
15 *
16 * NO WARRANTY
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.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sys/dev/ath/if_ath_tx.c 227360 2011-11-08 21:25:36Z adrian $");
32
33/*
34 * Driver for the Atheros Wireless LAN controller.
35 *
36 * This software is derived from work of Atsushi Onoe; his contribution
37 * is greatly appreciated.
38 */
39
40#include "opt_inet.h"
41#include "opt_ath.h"
42#include "opt_wlan.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/sysctl.h>
47#include <sys/mbuf.h>
48#include <sys/malloc.h>
49#include <sys/lock.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>
56#include <sys/bus.h>
57#include <sys/endian.h>
58#include <sys/kthread.h>
59#include <sys/taskqueue.h>
60#include <sys/priv.h>
61
62#include <machine/bus.h>
63
64#include <net/if.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>
71
72#include <net80211/ieee80211_var.h>
73#include <net80211/ieee80211_regdomain.h>
74#ifdef IEEE80211_SUPPORT_SUPERG
75#include <net80211/ieee80211_superg.h>
76#endif
77#ifdef IEEE80211_SUPPORT_TDMA
78#include <net80211/ieee80211_tdma.h>
79#endif
80
81#include <net/bpf.h>
82
83#ifdef INET
84#include <netinet/in.h>
85#include <netinet/if_ether.h>
86#endif
87
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>
91
92#include <dev/ath/if_ath_debug.h>
93
94#ifdef ATH_TX99_DIAG
95#include <dev/ath/ath_tx99/ath_tx99.h>
96#endif
97
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>
101
102/*
103 * Whether to use the 11n rate scenario functions or not
104 */
105static inline int
106ath_tx_is_11n(struct ath_softc *sc)
107{
108 return (sc->sc_ah->ah_magic == 0x20065416);
109}
110
111void
112ath_txfrag_cleanup(struct ath_softc *sc,
113 ath_bufhead *frags, struct ieee80211_node *ni)
114{
115 struct ath_buf *bf, *next;
116
117 ATH_TXBUF_LOCK_ASSERT(sc);
118
119 TAILQ_FOREACH_SAFE(bf, frags, bf_list, next) {
120 /* NB: bf assumed clean */
121 TAILQ_REMOVE(frags, bf, bf_list);
122 TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
123 ieee80211_node_decref(ni);
124 }
125}
126
127/*
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.
131 */
132int
133ath_txfrag_setup(struct ath_softc *sc, ath_bufhead *frags,
134 struct mbuf *m0, struct ieee80211_node *ni)
135{
136 struct mbuf *m;
137 struct ath_buf *bf;
138
139 ATH_TXBUF_LOCK(sc);
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);
144 break;
145 }
146 ieee80211_node_incref(ni);
147 TAILQ_INSERT_TAIL(frags, bf, bf_list);
148 }
149 ATH_TXBUF_UNLOCK(sc);
150
151 return !TAILQ_EMPTY(frags);
152}
153
154/*
155 * Reclaim mbuf resources. For fragmented frames we
156 * need to claim each frag chained with m_nextpkt.
157 */
158void
159ath_freetx(struct mbuf *m)
160{
161 struct mbuf *next;
162
163 do {
164 next = m->m_nextpkt;
165 m->m_nextpkt = NULL;
166 m_freem(m);
167 } while ((m = next) != NULL);
168}
169
170static int
171ath_tx_dmasetup(struct ath_softc *sc, struct ath_buf *bf, struct mbuf *m0)
172{
173 struct mbuf *m;
174 int error;
175
176 /*
177 * Load the DMA map so any coalescing is done. This
178 * also calculates the number of descriptors we need.
179 */
180 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
181 bf->bf_segs, &bf->bf_nseg,
182 BUS_DMA_NOWAIT);
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++;
188 ath_freetx(m0);
189 return error;
190 }
191 /*
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.
195 */
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_DONTWAIT, ATH_TXDESC);
199 if (m == NULL) {
200 ath_freetx(m0);
201 sc->sc_stats.ast_tx_nombuf++;
202 return ENOMEM;
203 }
204 m0 = m;
205 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m0,
206 bf->bf_segs, &bf->bf_nseg,
207 BUS_DMA_NOWAIT);
208 if (error != 0) {
209 sc->sc_stats.ast_tx_busdma++;
210 ath_freetx(m0);
211 return error;
212 }
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++;
217 ath_freetx(m0);
218 return EIO;
219 }
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);
223 bf->bf_m = m0;
224
225 return 0;
226}
227
228/*
229 * Chain together segments+descriptors for a non-11n frame.
230 */
231static void
232ath_tx_chaindesclist(struct ath_softc *sc, struct ath_txq *txq, struct ath_buf *bf)
233{
234 struct ath_hal *ah = sc->sc_ah;
235 struct ath_desc *ds, *ds0;
236 int i;
237
238 /*
239 * Fillin the remainder of the descriptor info.
240 */
241 ds0 = ds = bf->bf_desc;
242 for (i = 0; i < bf->bf_nseg; i++, ds++) {
243 ds->ds_data = bf->bf_segs[i].ds_addr;
244 if (i == bf->bf_nseg - 1)
245 ds->ds_link = 0;
246 else
247 ds->ds_link = bf->bf_daddr + sizeof(*ds) * (i + 1);
248 ath_hal_filltxdesc(ah, ds
249 , bf->bf_segs[i].ds_len /* segment length */
250 , i == 0 /* first segment */
251 , i == bf->bf_nseg - 1 /* last segment */
252 , ds0 /* first descriptor */
253 );
254 DPRINTF(sc, ATH_DEBUG_XMIT,
255 "%s: %d: %08x %08x %08x %08x %08x %08x\n",
256 __func__, i, ds->ds_link, ds->ds_data,
257 ds->ds_ctl0, ds->ds_ctl1, ds->ds_hw[0], ds->ds_hw[1]);
258 bf->bf_lastds = ds;
259 }
260}
261
262static void
263ath_tx_handoff(struct ath_softc *sc, struct ath_txq *txq, struct ath_buf *bf)
264{
265 struct ath_hal *ah = sc->sc_ah;
266
267 /* Fill in the details in the descriptor list */
268 ath_tx_chaindesclist(sc, txq, bf);
269
270 /*
271 * Insert the frame on the outbound list and pass it on
272 * to the hardware. Multicast frames buffered for power
273 * save stations and transmit from the CAB queue are stored
274 * on a s/w only queue and loaded on to the CAB queue in
275 * the SWBA handler since frames only go out on DTIM and
276 * to avoid possible races.
277 */
278 ATH_TXQ_LOCK(txq);
279 KASSERT((bf->bf_flags & ATH_BUF_BUSY) == 0,
280 ("busy status 0x%x", bf->bf_flags));
281 if (txq->axq_qnum != ATH_TXQ_SWQ) {
282#ifdef IEEE80211_SUPPORT_TDMA
283 int qbusy;
284
285 ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
286 qbusy = ath_hal_txqenabled(ah, txq->axq_qnum);
287 if (txq->axq_link == NULL) {
288 /*
289 * Be careful writing the address to TXDP. If
290 * the tx q is enabled then this write will be
291 * ignored. Normally this is not an issue but
292 * when tdma is in use and the q is beacon gated
293 * this race can occur. If the q is busy then
294 * defer the work to later--either when another
295 * packet comes along or when we prepare a beacon
296 * frame at SWBA.
297 */
298 if (!qbusy) {
299 ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
300 txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
301 DPRINTF(sc, ATH_DEBUG_XMIT,
302 "%s: TXDP[%u] = %p (%p) depth %d\n",
303 __func__, txq->axq_qnum,
304 (caddr_t)bf->bf_daddr, bf->bf_desc,
305 txq->axq_depth);
306 } else {
307 txq->axq_flags |= ATH_TXQ_PUTPENDING;
308 DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
309 "%s: Q%u busy, defer enable\n", __func__,
310 txq->axq_qnum);
311 }
312 } else {
313 *txq->axq_link = bf->bf_daddr;
314 DPRINTF(sc, ATH_DEBUG_XMIT,
315 "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
316 txq->axq_qnum, txq->axq_link,
317 (caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth);
318 if ((txq->axq_flags & ATH_TXQ_PUTPENDING) && !qbusy) {
319 /*
320 * The q was busy when we previously tried
321 * to write the address of the first buffer
322 * in the chain. Since it's not busy now
323 * handle this chore. We are certain the
324 * buffer at the front is the right one since
325 * axq_link is NULL only when the buffer list
326 * is/was empty.
327 */
328 ath_hal_puttxbuf(ah, txq->axq_qnum,
329 TAILQ_FIRST(&txq->axq_q)->bf_daddr);
330 txq->axq_flags &= ~ATH_TXQ_PUTPENDING;
331 DPRINTF(sc, ATH_DEBUG_TDMA | ATH_DEBUG_XMIT,
332 "%s: Q%u restarted\n", __func__,
333 txq->axq_qnum);
334 }
335 }
336#else
337 ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
338 if (txq->axq_link == NULL) {
339 ath_hal_puttxbuf(ah, txq->axq_qnum, bf->bf_daddr);
340 DPRINTF(sc, ATH_DEBUG_XMIT,
341 "%s: TXDP[%u] = %p (%p) depth %d\n",
342 __func__, txq->axq_qnum,
343 (caddr_t)bf->bf_daddr, bf->bf_desc,
344 txq->axq_depth);
345 } else {
346 *txq->axq_link = bf->bf_daddr;
347 DPRINTF(sc, ATH_DEBUG_XMIT,
348 "%s: link[%u](%p)=%p (%p) depth %d\n", __func__,
349 txq->axq_qnum, txq->axq_link,
350 (caddr_t)bf->bf_daddr, bf->bf_desc, txq->axq_depth);
351 }
352#endif /* IEEE80211_SUPPORT_TDMA */
353 if (bf->bf_state.bfs_aggr)
354 txq->axq_aggr_depth++;
355 txq->axq_link = &bf->bf_lastds->ds_link;
356 ath_hal_txstart(ah, txq->axq_qnum);
357 } else {
358 if (txq->axq_link != NULL) {
359 struct ath_buf *last = ATH_TXQ_LAST(txq, axq_q_s);
360 struct ieee80211_frame *wh;
361
362 /* mark previous frame */
363 wh = mtod(last->bf_m, struct ieee80211_frame *);
364 wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
365 bus_dmamap_sync(sc->sc_dmat, last->bf_dmamap,
366 BUS_DMASYNC_PREWRITE);
367
368 /* link descriptor */
369 *txq->axq_link = bf->bf_daddr;
370 }
371 ATH_TXQ_INSERT_TAIL(txq, bf, bf_list);
372 txq->axq_link = &bf->bf_desc[bf->bf_nseg - 1].ds_link;
373 }
374 ATH_TXQ_UNLOCK(txq);
375}
376
377static int
378ath_tx_tag_crypto(struct ath_softc *sc, struct ieee80211_node *ni,
379 struct mbuf *m0, int iswep, int isfrag, int *hdrlen, int *pktlen, int *keyix)
380{
381 if (iswep) {
382 const struct ieee80211_cipher *cip;
383 struct ieee80211_key *k;
384
385 /*
386 * Construct the 802.11 header+trailer for an encrypted
387 * frame. The only reason this can fail is because of an
388 * unknown or unsupported cipher/key type.
389 */
390 k = ieee80211_crypto_encap(ni, m0);
391 if (k == NULL) {
392 /*
393 * This can happen when the key is yanked after the
394 * frame was queued. Just discard the frame; the
395 * 802.11 layer counts failures and provides
396 * debugging/diagnostics.
397 */
398 return 0;
399 }
400 /*
401 * Adjust the packet + header lengths for the crypto
402 * additions and calculate the h/w key index. When
403 * a s/w mic is done the frame will have had any mic
404 * added to it prior to entry so m0->m_pkthdr.len will
405 * account for it. Otherwise we need to add it to the
406 * packet length.
407 */
408 cip = k->wk_cipher;
409 (*hdrlen) += cip->ic_header;
410 (*pktlen) += cip->ic_header + cip->ic_trailer;
411 /* NB: frags always have any TKIP MIC done in s/w */
412 if ((k->wk_flags & IEEE80211_KEY_SWMIC) == 0 && !isfrag)
413 (*pktlen) += cip->ic_miclen;
414 (*keyix) = k->wk_keyix;
415 } else if (ni->ni_ucastkey.wk_cipher == &ieee80211_cipher_none) {
416 /*
417 * Use station key cache slot, if assigned.
418 */
419 (*keyix) = ni->ni_ucastkey.wk_keyix;
420 if ((*keyix) == IEEE80211_KEYIX_NONE)
421 (*keyix) = HAL_TXKEYIX_INVALID;
422 } else
423 (*keyix) = HAL_TXKEYIX_INVALID;
424
425 return 1;
426}
427
428static uint8_t
429ath_tx_get_rtscts_rate(struct ath_hal *ah, const HAL_RATE_TABLE *rt,
430 int rix, int cix, int shortPreamble)
431{
432 uint8_t ctsrate;
433
434 /*
435 * CTS transmit rate is derived from the transmit rate
436 * by looking in the h/w rate table. We must also factor
437 * in whether or not a short preamble is to be used.
438 */
439 /* NB: cix is set above where RTS/CTS is enabled */
440 KASSERT(cix != 0xff, ("cix not setup"));
441 ctsrate = rt->info[cix].rateCode;
442
443 /* XXX this should only matter for legacy rates */
444 if (shortPreamble)
445 ctsrate |= rt->info[cix].shortPreamble;
446
447 return ctsrate;
448}
449
450
451/*
452 * Calculate the RTS/CTS duration for legacy frames.
453 */
454static int
455ath_tx_calc_ctsduration(struct ath_hal *ah, int rix, int cix,
456 int shortPreamble, int pktlen, const HAL_RATE_TABLE *rt,
457 int flags)
458{
459 int ctsduration = 0;
460
461 /* This mustn't be called for HT modes */
462 if (rt->info[cix].phy == IEEE80211_T_HT) {
463 printf("%s: HT rate where it shouldn't be (0x%x)\n",
464 __func__, rt->info[cix].rateCode);
465 return -1;
466 }
467
468 /*
469 * Compute the transmit duration based on the frame
470 * size and the size of an ACK frame. We call into the
471 * HAL to do the computation since it depends on the
472 * characteristics of the actual PHY being used.
473 *
474 * NB: CTS is assumed the same size as an ACK so we can
475 * use the precalculated ACK durations.
476 */
477 if (shortPreamble) {
478 if (flags & HAL_TXDESC_RTSENA) /* SIFS + CTS */
479 ctsduration += rt->info[cix].spAckDuration;
480 ctsduration += ath_hal_computetxtime(ah,
481 rt, pktlen, rix, AH_TRUE);
482 if ((flags & HAL_TXDESC_NOACK) == 0) /* SIFS + ACK */
483 ctsduration += rt->info[rix].spAckDuration;
484 } else {
485 if (flags & HAL_TXDESC_RTSENA) /* SIFS + CTS */
486 ctsduration += rt->info[cix].lpAckDuration;
487 ctsduration += ath_hal_computetxtime(ah,
488 rt, pktlen, rix, AH_FALSE);
489 if ((flags & HAL_TXDESC_NOACK) == 0) /* SIFS + ACK */
490 ctsduration += rt->info[rix].lpAckDuration;
491 }
492
493 return ctsduration;
494}
495
496int
497ath_tx_start(struct ath_softc *sc, struct ieee80211_node *ni, struct ath_buf *bf,
498 struct mbuf *m0)
499{
500 struct ieee80211vap *vap = ni->ni_vap;
501 struct ath_vap *avp = ATH_VAP(vap);
502 struct ath_hal *ah = sc->sc_ah;
503 struct ifnet *ifp = sc->sc_ifp;
504 struct ieee80211com *ic = ifp->if_l2com;
505 const struct chanAccParams *cap = &ic->ic_wme.wme_chanParams;
506 int error, iswep, ismcast, isfrag, ismrr;
507 int keyix, hdrlen, pktlen, try0;
508 u_int8_t rix, txrate, ctsrate;
509 u_int8_t cix = 0xff; /* NB: silence compiler */
510 struct ath_desc *ds;
511 struct ath_txq *txq;
512 struct ieee80211_frame *wh;
513 u_int subtype, flags, ctsduration;
514 HAL_PKT_TYPE atype;
515 const HAL_RATE_TABLE *rt;
516 HAL_BOOL shortPreamble;
517 struct ath_node *an;
518 u_int pri;
519 uint8_t try[4], rate[4];
520
521 bzero(try, sizeof(try));
522 bzero(rate, sizeof(rate));
523
524 wh = mtod(m0, struct ieee80211_frame *);
525 iswep = wh->i_fc[1] & IEEE80211_FC1_WEP;
526 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
527 isfrag = m0->m_flags & M_FRAG;
528 hdrlen = ieee80211_anyhdrsize(wh);
529 /*
530 * Packet length must not include any
531 * pad bytes; deduct them here.
532 */
533 pktlen = m0->m_pkthdr.len - (hdrlen & 3);
534
535 /* Handle encryption twiddling if needed */
536 if (! ath_tx_tag_crypto(sc, ni, m0, iswep, isfrag, &hdrlen, &pktlen, &keyix)) {
537 ath_freetx(m0);
538 return EIO;
539 }
540
541 /* packet header may have moved, reset our local pointer */
542 wh = mtod(m0, struct ieee80211_frame *);
543
544 pktlen += IEEE80211_CRC_LEN;
545
546 /*
547 * Load the DMA map so any coalescing is done. This
548 * also calculates the number of descriptors we need.
549 */
550 error = ath_tx_dmasetup(sc, bf, m0);
551 if (error != 0)
552 return error;
553 bf->bf_node = ni; /* NB: held reference */
554 m0 = bf->bf_m; /* NB: may have changed */
555 wh = mtod(m0, struct ieee80211_frame *);
556
557 /* setup descriptors */
558 ds = bf->bf_desc;
559 rt = sc->sc_currates;
560 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
561
562 /*
563 * NB: the 802.11 layer marks whether or not we should
564 * use short preamble based on the current mode and
565 * negotiated parameters.
566 */
567 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
568 (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE)) {
569 shortPreamble = AH_TRUE;
570 sc->sc_stats.ast_tx_shortpre++;
571 } else {
572 shortPreamble = AH_FALSE;
573 }
574
575 an = ATH_NODE(ni);
576 flags = HAL_TXDESC_CLRDMASK; /* XXX needed for crypto errs */
577 ismrr = 0; /* default no multi-rate retry*/
578 pri = M_WME_GETAC(m0); /* honor classification */
579 /* XXX use txparams instead of fixed values */
580 /*
581 * Calculate Atheros packet type from IEEE80211 packet header,
582 * setup for rate calculations, and select h/w transmit queue.
583 */
584 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
585 case IEEE80211_FC0_TYPE_MGT:
586 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK;
587 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON)
588 atype = HAL_PKT_TYPE_BEACON;
589 else if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP)
590 atype = HAL_PKT_TYPE_PROBE_RESP;
591 else if (subtype == IEEE80211_FC0_SUBTYPE_ATIM)
592 atype = HAL_PKT_TYPE_ATIM;
593 else
594 atype = HAL_PKT_TYPE_NORMAL; /* XXX */
595 rix = an->an_mgmtrix;
596 txrate = rt->info[rix].rateCode;
597 if (shortPreamble)
598 txrate |= rt->info[rix].shortPreamble;
599 try0 = ATH_TXMGTTRY;
600 flags |= HAL_TXDESC_INTREQ; /* force interrupt */
601 break;
602 case IEEE80211_FC0_TYPE_CTL:
603 atype = HAL_PKT_TYPE_PSPOLL; /* stop setting of duration */
604 rix = an->an_mgmtrix;
605 txrate = rt->info[rix].rateCode;
606 if (shortPreamble)
607 txrate |= rt->info[rix].shortPreamble;
608 try0 = ATH_TXMGTTRY;
609 flags |= HAL_TXDESC_INTREQ; /* force interrupt */
610 break;
611 case IEEE80211_FC0_TYPE_DATA:
612 atype = HAL_PKT_TYPE_NORMAL; /* default */
613 /*
614 * Data frames: multicast frames go out at a fixed rate,
615 * EAPOL frames use the mgmt frame rate; otherwise consult
616 * the rate control module for the rate to use.
617 */
618 if (ismcast) {
619 rix = an->an_mcastrix;
620 txrate = rt->info[rix].rateCode;
621 if (shortPreamble)
622 txrate |= rt->info[rix].shortPreamble;
623 try0 = 1;
624 } else if (m0->m_flags & M_EAPOL) {
625 /* XXX? maybe always use long preamble? */
626 rix = an->an_mgmtrix;
627 txrate = rt->info[rix].rateCode;
628 if (shortPreamble)
629 txrate |= rt->info[rix].shortPreamble;
630 try0 = ATH_TXMAXTRY; /* XXX?too many? */
631 } else {
632 ath_rate_findrate(sc, an, shortPreamble, pktlen,
633 &rix, &try0, &txrate);
634 sc->sc_txrix = rix; /* for LED blinking */
635 sc->sc_lastdatarix = rix; /* for fast frames */
636 if (try0 != ATH_TXMAXTRY)
637 ismrr = 1;
638 }
639 if (cap->cap_wmeParams[pri].wmep_noackPolicy)
640 flags |= HAL_TXDESC_NOACK;
641 break;
642 default:
643 if_printf(ifp, "bogus frame type 0x%x (%s)\n",
644 wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__);
645 /* XXX statistic */
646 ath_freetx(m0);
647 return EIO;
648 }
649 txq = sc->sc_ac2q[pri];
650
651 /*
652 * When servicing one or more stations in power-save mode
653 * (or) if there is some mcast data waiting on the mcast
654 * queue (to prevent out of order delivery) multicast
655 * frames must be buffered until after the beacon.
656 */
657 if (ismcast && (vap->iv_ps_sta || avp->av_mcastq.axq_depth))
658 txq = &avp->av_mcastq;
659
660 /*
661 * Calculate miscellaneous flags.
662 */
663 if (ismcast) {
664 flags |= HAL_TXDESC_NOACK; /* no ack on broad/multicast */
665 } else if (pktlen > vap->iv_rtsthreshold &&
666 (ni->ni_ath_flags & IEEE80211_NODE_FF) == 0) {
667 flags |= HAL_TXDESC_RTSENA; /* RTS based on frame length */
668 cix = rt->info[rix].controlRate;
669 sc->sc_stats.ast_tx_rts++;
670 }
671 if (flags & HAL_TXDESC_NOACK) /* NB: avoid double counting */
672 sc->sc_stats.ast_tx_noack++;
673#ifdef IEEE80211_SUPPORT_TDMA
674 if (sc->sc_tdma && (flags & HAL_TXDESC_NOACK) == 0) {
675 DPRINTF(sc, ATH_DEBUG_TDMA,
676 "%s: discard frame, ACK required w/ TDMA\n", __func__);
677 sc->sc_stats.ast_tdma_ack++;
678 ath_freetx(m0);
679 return EIO;
680 }
681#endif
682
683 /*
684 * If 802.11g protection is enabled, determine whether
685 * to use RTS/CTS or just CTS. Note that this is only
686 * done for OFDM unicast frames.
687 */
688 if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
689 rt->info[rix].phy == IEEE80211_T_OFDM &&
690 (flags & HAL_TXDESC_NOACK) == 0) {
691 /* XXX fragments must use CCK rates w/ protection */
692 if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
693 flags |= HAL_TXDESC_RTSENA;
694 else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
695 flags |= HAL_TXDESC_CTSENA;
696 if (isfrag) {
697 /*
698 * For frags it would be desirable to use the
699 * highest CCK rate for RTS/CTS. But stations
700 * farther away may detect it at a lower CCK rate
701 * so use the configured protection rate instead
702 * (for now).
703 */
704 cix = rt->info[sc->sc_protrix].controlRate;
705 } else
706 cix = rt->info[sc->sc_protrix].controlRate;
707 sc->sc_stats.ast_tx_protect++;
708 }
709
710#if 0
711 /*
712 * If 11n protection is enabled and it's a HT frame,
713 * enable RTS.
714 *
715 * XXX ic_htprotmode or ic_curhtprotmode?
716 * XXX should it_htprotmode only matter if ic_curhtprotmode
717 * XXX indicates it's not a HT pure environment?
718 */
719 if ((ic->ic_htprotmode == IEEE80211_PROT_RTSCTS) &&
720 rt->info[rix].phy == IEEE80211_T_HT &&
721 (flags & HAL_TXDESC_NOACK) == 0) {
722 cix = rt->info[sc->sc_protrix].controlRate;
723 flags |= HAL_TXDESC_RTSENA;
724 sc->sc_stats.ast_tx_htprotect++;
725 }
726#endif
727
728 /*
729 * Calculate duration. This logically belongs in the 802.11
730 * layer but it lacks sufficient information to calculate it.
731 */
732 if ((flags & HAL_TXDESC_NOACK) == 0 &&
733 (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_CTL) {
734 u_int16_t dur;
735 if (shortPreamble)
736 dur = rt->info[rix].spAckDuration;
737 else
738 dur = rt->info[rix].lpAckDuration;
739 if (wh->i_fc[1] & IEEE80211_FC1_MORE_FRAG) {
740 dur += dur; /* additional SIFS+ACK */
741 KASSERT(m0->m_nextpkt != NULL, ("no fragment"));
742 /*
743 * Include the size of next fragment so NAV is
744 * updated properly. The last fragment uses only
745 * the ACK duration
746 */
747 dur += ath_hal_computetxtime(ah, rt,
748 m0->m_nextpkt->m_pkthdr.len,
749 rix, shortPreamble);
750 }
751 if (isfrag) {
752 /*
753 * Force hardware to use computed duration for next
754 * fragment by disabling multi-rate retry which updates
755 * duration based on the multi-rate duration table.
756 */
757 ismrr = 0;
758 try0 = ATH_TXMGTTRY; /* XXX? */
759 }
760 *(u_int16_t *)wh->i_dur = htole16(dur);
761 }
762
763 /*
764 * Calculate RTS/CTS rate and duration if needed.
765 */
766 ctsduration = 0;
767 if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
768 ctsrate = ath_tx_get_rtscts_rate(ah, rt, rix, cix, shortPreamble);
769
770 /* The 11n chipsets do ctsduration calculations for you */
771 if (! ath_tx_is_11n(sc))
772 ctsduration = ath_tx_calc_ctsduration(ah, rix, cix, shortPreamble,
773 pktlen, rt, flags);
774 /*
775 * Must disable multi-rate retry when using RTS/CTS.
776 */
777 ismrr = 0;
778 try0 = ATH_TXMGTTRY; /* XXX */
779 } else
780 ctsrate = 0;
781
782 /*
783 * At this point we are committed to sending the frame
784 * and we don't need to look at m_nextpkt; clear it in
785 * case this frame is part of frag chain.
786 */
787 m0->m_nextpkt = NULL;
788
789 if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
790 ieee80211_dump_pkt(ic, mtod(m0, const uint8_t *), m0->m_len,
791 sc->sc_hwmap[rix].ieeerate, -1);
792
793 if (ieee80211_radiotap_active_vap(vap)) {
794 u_int64_t tsf = ath_hal_gettsf64(ah);
795
796 sc->sc_tx_th.wt_tsf = htole64(tsf);
797 sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
798 if (iswep)
799 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
800 if (isfrag)
801 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
802 sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
803 sc->sc_tx_th.wt_txpower = ni->ni_txpower;
804 sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
805
806 ieee80211_radiotap_tx(vap, m0);
807 }
808
809 /*
810 * Determine if a tx interrupt should be generated for
811 * this descriptor. We take a tx interrupt to reap
812 * descriptors when the h/w hits an EOL condition or
813 * when the descriptor is specifically marked to generate
814 * an interrupt. We periodically mark descriptors in this
815 * way to insure timely replenishing of the supply needed
816 * for sending frames. Defering interrupts reduces system
817 * load and potentially allows more concurrent work to be
818 * done but if done to aggressively can cause senders to
819 * backup.
820 *
821 * NB: use >= to deal with sc_txintrperiod changing
822 * dynamically through sysctl.
823 */
824 if (flags & HAL_TXDESC_INTREQ) {
825 txq->axq_intrcnt = 0;
826 } else if (++txq->axq_intrcnt >= sc->sc_txintrperiod) {
827 flags |= HAL_TXDESC_INTREQ;
828 txq->axq_intrcnt = 0;
829 }
830
831 if (ath_tx_is_11n(sc)) {
832 rate[0] = rix;
833 try[0] = try0;
834 }
835
836 /*
837 * Formulate first tx descriptor with tx controls.
838 */
839 /* XXX check return value? */
840 /* XXX is this ok to call for 11n descriptors? */
841 /* XXX or should it go through the first, next, last 11n calls? */
842 ath_hal_setuptxdesc(ah, ds
843 , pktlen /* packet length */
844 , hdrlen /* header length */
845 , atype /* Atheros packet type */
846 , ni->ni_txpower /* txpower */
847 , txrate, try0 /* series 0 rate/tries */
848 , keyix /* key cache index */
849 , sc->sc_txantenna /* antenna mode */
850 , flags /* flags */
851 , ctsrate /* rts/cts rate */
852 , ctsduration /* rts/cts duration */
853 );
854 bf->bf_txflags = flags;
855 /*
856 * Setup the multi-rate retry state only when we're
857 * going to use it. This assumes ath_hal_setuptxdesc
858 * initializes the descriptors (so we don't have to)
859 * when the hardware supports multi-rate retry and
860 * we don't use it.
861 */
862 if (ismrr) {
863 if (ath_tx_is_11n(sc))
864 ath_rate_getxtxrates(sc, an, rix, rate, try);
865 else
866 ath_rate_setupxtxdesc(sc, an, ds, shortPreamble, rix);
867 }
868
869 if (ath_tx_is_11n(sc)) {
870 ath_buf_set_rate(sc, ni, bf, pktlen, flags, ctsrate, (atype == HAL_PKT_TYPE_PSPOLL), rate, try);
871 }
872
873 ath_tx_handoff(sc, txq, bf);
874 return 0;
875}
876
877static int
878ath_tx_raw_start(struct ath_softc *sc, struct ieee80211_node *ni,
879 struct ath_buf *bf, struct mbuf *m0,
880 const struct ieee80211_bpf_params *params)
881{
882 struct ifnet *ifp = sc->sc_ifp;
883 struct ieee80211com *ic = ifp->if_l2com;
884 struct ath_hal *ah = sc->sc_ah;
885 struct ieee80211vap *vap = ni->ni_vap;
886 int error, ismcast, ismrr;
887 int keyix, hdrlen, pktlen, try0, txantenna;
888 u_int8_t rix, cix, txrate, ctsrate, rate1, rate2, rate3;
889 struct ieee80211_frame *wh;
890 u_int flags, ctsduration;
891 HAL_PKT_TYPE atype;
892 const HAL_RATE_TABLE *rt;
893 struct ath_desc *ds;
894 u_int pri;
895 uint8_t try[4], rate[4];
896
897 bzero(try, sizeof(try));
898 bzero(rate, sizeof(rate));
899
900 wh = mtod(m0, struct ieee80211_frame *);
901 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1);
902 hdrlen = ieee80211_anyhdrsize(wh);
903 /*
904 * Packet length must not include any
905 * pad bytes; deduct them here.
906 */
907 /* XXX honor IEEE80211_BPF_DATAPAD */
908 pktlen = m0->m_pkthdr.len - (hdrlen & 3) + IEEE80211_CRC_LEN;
909
910 /* Handle encryption twiddling if needed */
911 if (! ath_tx_tag_crypto(sc, ni, m0, params->ibp_flags & IEEE80211_BPF_CRYPTO, 0, &hdrlen, &pktlen, &keyix)) {
912 ath_freetx(m0);
913 return EIO;
914 }
915 /* packet header may have moved, reset our local pointer */
916 wh = mtod(m0, struct ieee80211_frame *);
917
918 error = ath_tx_dmasetup(sc, bf, m0);
919 if (error != 0)
920 return error;
921 m0 = bf->bf_m; /* NB: may have changed */
922 wh = mtod(m0, struct ieee80211_frame *);
923 bf->bf_node = ni; /* NB: held reference */
924
925 flags = HAL_TXDESC_CLRDMASK; /* XXX needed for crypto errs */
926 flags |= HAL_TXDESC_INTREQ; /* force interrupt */
927 if (params->ibp_flags & IEEE80211_BPF_RTS)
928 flags |= HAL_TXDESC_RTSENA;
929 else if (params->ibp_flags & IEEE80211_BPF_CTS)
930 flags |= HAL_TXDESC_CTSENA;
931 /* XXX leave ismcast to injector? */
932 if ((params->ibp_flags & IEEE80211_BPF_NOACK) || ismcast)
933 flags |= HAL_TXDESC_NOACK;
934
935 rt = sc->sc_currates;
936 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
937 rix = ath_tx_findrix(sc, params->ibp_rate0);
938 txrate = rt->info[rix].rateCode;
939 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
940 txrate |= rt->info[rix].shortPreamble;
941 sc->sc_txrix = rix;
942 try0 = params->ibp_try0;
943 ismrr = (params->ibp_try1 != 0);
944 txantenna = params->ibp_pri >> 2;
945 if (txantenna == 0) /* XXX? */
946 txantenna = sc->sc_txantenna;
947
948 ctsduration = 0;
949 if (flags & (HAL_TXDESC_RTSENA|HAL_TXDESC_CTSENA)) {
950 cix = ath_tx_findrix(sc, params->ibp_ctsrate);
951 ctsrate = ath_tx_get_rtscts_rate(ah, rt, rix, cix, params->ibp_flags & IEEE80211_BPF_SHORTPRE);
952 /* The 11n chipsets do ctsduration calculations for you */
953 if (! ath_tx_is_11n(sc))
954 ctsduration = ath_tx_calc_ctsduration(ah, rix, cix,
955 params->ibp_flags & IEEE80211_BPF_SHORTPRE, pktlen,
956 rt, flags);
957 /*
958 * Must disable multi-rate retry when using RTS/CTS.
959 */
960 ismrr = 0; /* XXX */
961 } else
962 ctsrate = 0;
963
964 pri = params->ibp_pri & 3;
965 /*
966 * NB: we mark all packets as type PSPOLL so the h/w won't
967 * set the sequence number, duration, etc.
968 */
969 atype = HAL_PKT_TYPE_PSPOLL;
970
971 if (IFF_DUMPPKTS(sc, ATH_DEBUG_XMIT))
972 ieee80211_dump_pkt(ic, mtod(m0, caddr_t), m0->m_len,
973 sc->sc_hwmap[rix].ieeerate, -1);
974
975 if (ieee80211_radiotap_active_vap(vap)) {
976 u_int64_t tsf = ath_hal_gettsf64(ah);
977
978 sc->sc_tx_th.wt_tsf = htole64(tsf);
979 sc->sc_tx_th.wt_flags = sc->sc_hwmap[rix].txflags;
980 if (wh->i_fc[1] & IEEE80211_FC1_WEP)
981 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_WEP;
982 if (m0->m_flags & M_FRAG)
983 sc->sc_tx_th.wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
984 sc->sc_tx_th.wt_rate = sc->sc_hwmap[rix].ieeerate;
985 sc->sc_tx_th.wt_txpower = ni->ni_txpower;
986 sc->sc_tx_th.wt_antenna = sc->sc_txantenna;
987
988 ieee80211_radiotap_tx(vap, m0);
989 }
990
991 /*
992 * Formulate first tx descriptor with tx controls.
993 */
994 ds = bf->bf_desc;
995 /* XXX check return value? */
996 ath_hal_setuptxdesc(ah, ds
997 , pktlen /* packet length */
998 , hdrlen /* header length */
999 , atype /* Atheros packet type */
1000 , params->ibp_power /* txpower */
1001 , txrate, try0 /* series 0 rate/tries */
1002 , keyix /* key cache index */
1003 , txantenna /* antenna mode */
1004 , flags /* flags */
1005 , ctsrate /* rts/cts rate */
1006 , ctsduration /* rts/cts duration */
1007 );
1008 bf->bf_txflags = flags;
1009
1010 if (ath_tx_is_11n(sc)) {
1011 rate[0] = ath_tx_findrix(sc, params->ibp_rate0);
1012 try[0] = params->ibp_try0;
1013
1014 if (ismrr) {
1015 /* Remember, rate[] is actually an array of rix's -adrian */
1016 rate[0] = ath_tx_findrix(sc, params->ibp_rate0);
1017 rate[1] = ath_tx_findrix(sc, params->ibp_rate1);
1018 rate[2] = ath_tx_findrix(sc, params->ibp_rate2);
1019 rate[3] = ath_tx_findrix(sc, params->ibp_rate3);
1020
1021 try[0] = params->ibp_try0;
1022 try[1] = params->ibp_try1;
1023 try[2] = params->ibp_try2;
1024 try[3] = params->ibp_try3;
1025 }
1026 } else {
1027 if (ismrr) {
1028 rix = ath_tx_findrix(sc, params->ibp_rate1);
1029 rate1 = rt->info[rix].rateCode;
1030 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
1031 rate1 |= rt->info[rix].shortPreamble;
1032 if (params->ibp_try2) {
1033 rix = ath_tx_findrix(sc, params->ibp_rate2);
1034 rate2 = rt->info[rix].rateCode;
1035 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
1036 rate2 |= rt->info[rix].shortPreamble;
1037 } else
1038 rate2 = 0;
1039 if (params->ibp_try3) {
1040 rix = ath_tx_findrix(sc, params->ibp_rate3);
1041 rate3 = rt->info[rix].rateCode;
1042 if (params->ibp_flags & IEEE80211_BPF_SHORTPRE)
1043 rate3 |= rt->info[rix].shortPreamble;
1044 } else
1045 rate3 = 0;
1046 ath_hal_setupxtxdesc(ah, ds
1047 , rate1, params->ibp_try1 /* series 1 */
1048 , rate2, params->ibp_try2 /* series 2 */
1049 , rate3, params->ibp_try3 /* series 3 */
1050 );
1051 }
1052 }
1053
1054 if (ath_tx_is_11n(sc)) {
1055 /*
1056 * notice that rix doesn't include any of the "magic" flags txrate
1057 * does for communicating "other stuff" to the HAL.
1058 */
1059 ath_buf_set_rate(sc, ni, bf, pktlen, flags, ctsrate, (atype == HAL_PKT_TYPE_PSPOLL), rate, try);
1060 }
1061
1062 /* NB: no buffered multicast in power save support */
1063 ath_tx_handoff(sc, sc->sc_ac2q[pri], bf);
1064 return 0;
1065}
1066
1067int
1068ath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
1069 const struct ieee80211_bpf_params *params)
1070{
1071 struct ieee80211com *ic = ni->ni_ic;
1072 struct ifnet *ifp = ic->ic_ifp;
1073 struct ath_softc *sc = ifp->if_softc;
1074 struct ath_buf *bf;
1075 int error;
1076
1077 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || sc->sc_invalid) {
1078 DPRINTF(sc, ATH_DEBUG_XMIT, "%s: discard frame, %s", __func__,
1079 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ?
1080 "!running" : "invalid");
1081 m_freem(m);
1082 error = ENETDOWN;
1083 goto bad;
1084 }
1085 /*
1086 * Grab a TX buffer and associated resources.
1087 */
1088 bf = ath_getbuf(sc);
1089 if (bf == NULL) {
1090 sc->sc_stats.ast_tx_nobuf++;
1091 m_freem(m);
1092 error = ENOBUFS;
1093 goto bad;
1094 }
1095
1096 if (params == NULL) {
1097 /*
1098 * Legacy path; interpret frame contents to decide
1099 * precisely how to send the frame.
1100 */
1101 if (ath_tx_start(sc, ni, bf, m)) {
1102 error = EIO; /* XXX */
1103 goto bad2;
1104 }
1105 } else {
1106 /*
1107 * Caller supplied explicit parameters to use in
1108 * sending the frame.
1109 */
1110 if (ath_tx_raw_start(sc, ni, bf, m, params)) {
1111 error = EIO; /* XXX */
1112 goto bad2;
1113 }
1114 }
1115 sc->sc_wd_timer = 5;
1116 ifp->if_opackets++;
1117 sc->sc_stats.ast_tx_raw++;
1118
1119 return 0;
1120bad2:
1121 ATH_TXBUF_LOCK(sc);
1122 TAILQ_INSERT_HEAD(&sc->sc_txbuf, bf, bf_list);
1123 ATH_TXBUF_UNLOCK(sc);
1124bad:
1125 ifp->if_oerrors++;
1126 sc->sc_stats.ast_tx_raw_fail++;
1127 ieee80211_free_node(ni);
1128 return error;
1129}