if_ath_tx_ht.c revision 236995
1/*-
2 * Copyright (c) 2011 Adrian Chadd, Xenion Pty Ltd.
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_ht.c 236995 2012-06-13 05:47:24Z adrian $");
32
33#include "opt_inet.h"
34#include "opt_ath.h"
35#include "opt_wlan.h"
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/sysctl.h>
40#include <sys/mbuf.h>
41#include <sys/malloc.h>
42#include <sys/lock.h>
43#include <sys/mutex.h>
44#include <sys/kernel.h>
45#include <sys/socket.h>
46#include <sys/sockio.h>
47#include <sys/errno.h>
48#include <sys/callout.h>
49#include <sys/bus.h>
50#include <sys/endian.h>
51#include <sys/kthread.h>
52#include <sys/taskqueue.h>
53#include <sys/priv.h>
54
55#include <machine/bus.h>
56
57#include <net/if.h>
58#include <net/if_dl.h>
59#include <net/if_media.h>
60#include <net/if_types.h>
61#include <net/if_arp.h>
62#include <net/ethernet.h>
63#include <net/if_llc.h>
64
65#include <net80211/ieee80211_var.h>
66#include <net80211/ieee80211_regdomain.h>
67#ifdef IEEE80211_SUPPORT_SUPERG
68#include <net80211/ieee80211_superg.h>
69#endif
70#ifdef IEEE80211_SUPPORT_TDMA
71#include <net80211/ieee80211_tdma.h>
72#endif
73
74#include <net/bpf.h>
75
76#ifdef INET
77#include <netinet/in.h>
78#include <netinet/if_ether.h>
79#endif
80
81#include <dev/ath/if_athvar.h>
82#include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
83#include <dev/ath/ath_hal/ah_diagcodes.h>
84
85#ifdef ATH_TX99_DIAG
86#include <dev/ath/ath_tx99/ath_tx99.h>
87#endif
88
89#include <dev/ath/if_ath_tx.h>		/* XXX for some support functions */
90#include <dev/ath/if_ath_tx_ht.h>
91#include <dev/ath/if_athrate.h>
92#include <dev/ath/if_ath_debug.h>
93
94/*
95 * XXX net80211?
96 */
97#define	IEEE80211_AMPDU_SUBFRAME_DEFAULT		32
98
99#define	ATH_AGGR_DELIM_SZ	4	/* delimiter size   */
100#define	ATH_AGGR_MINPLEN	256	/* in bytes, minimum packet length */
101#define	ATH_AGGR_ENCRYPTDELIM	10	/* number of delimiters for encryption padding */
102
103/*
104 * returns delimiter padding required given the packet length
105 */
106#define	ATH_AGGR_GET_NDELIM(_len)					\
107	    (((((_len) + ATH_AGGR_DELIM_SZ) < ATH_AGGR_MINPLEN) ?	\
108	    (ATH_AGGR_MINPLEN - (_len) - ATH_AGGR_DELIM_SZ) : 0) >> 2)
109
110#define	PADBYTES(_len)		((4 - ((_len) % 4)) % 4)
111
112int ath_max_4ms_framelen[4][32] = {
113	[MCS_HT20] = {
114		3212,  6432,  9648,  12864,  19300,  25736,  28952,  32172,
115		6424,  12852, 19280, 25708,  38568,  51424,  57852,  64280,
116		9628,  19260, 28896, 38528,  57792,  65532,  65532,  65532,
117		12828, 25656, 38488, 51320,  65532,  65532,  65532,  65532,
118	},
119	[MCS_HT20_SGI] = {
120		3572,  7144,  10720,  14296,  21444,  28596,  32172,  35744,
121		7140,  14284, 21428,  28568,  42856,  57144,  64288,  65532,
122		10700, 21408, 32112,  42816,  64228,  65532,  65532,  65532,
123		14256, 28516, 42780,  57040,  65532,  65532,  65532,  65532,
124	},
125	[MCS_HT40] = {
126		6680,  13360,  20044,  26724,  40092,  53456,  60140,  65532,
127		13348, 26700,  40052,  53400,  65532,  65532,  65532,  65532,
128		20004, 40008,  60016,  65532,  65532,  65532,  65532,  65532,
129		26644, 53292,  65532,  65532,  65532,  65532,  65532,  65532,
130	},
131	[MCS_HT40_SGI] = {
132		7420,  14844,  22272,  29696,  44544,  59396,  65532,  65532,
133		14832, 29668,  44504,  59340,  65532,  65532,  65532,  65532,
134		22232, 44464,  65532,  65532,  65532,  65532,  65532,  65532,
135		29616, 59232,  65532,  65532,  65532,  65532,  65532,  65532,
136	}
137};
138
139/*
140 * XXX should be in net80211
141 */
142static int ieee80211_mpdudensity_map[] = {
143	0,		/* IEEE80211_HTCAP_MPDUDENSITY_NA */
144	25,		/* IEEE80211_HTCAP_MPDUDENSITY_025 */
145	50,		/* IEEE80211_HTCAP_MPDUDENSITY_05 */
146	100,		/* IEEE80211_HTCAP_MPDUDENSITY_1 */
147	200,		/* IEEE80211_HTCAP_MPDUDENSITY_2 */
148	400,		/* IEEE80211_HTCAP_MPDUDENSITY_4 */
149	800,		/* IEEE80211_HTCAP_MPDUDENSITY_8 */
150	1600,		/* IEEE80211_HTCAP_MPDUDENSITY_16 */
151};
152
153/*
154 * XXX should be in the HAL/net80211 ?
155 */
156#define	BITS_PER_BYTE		8
157#define	OFDM_PLCP_BITS		22
158#define	HT_RC_2_MCS(_rc)	((_rc) & 0x7f)
159#define	HT_RC_2_STREAMS(_rc)	((((_rc) & 0x78) >> 3) + 1)
160#define	L_STF			8
161#define	L_LTF			8
162#define	L_SIG			4
163#define	HT_SIG			8
164#define	HT_STF			4
165#define	HT_LTF(_ns)		(4 * (_ns))
166#define	SYMBOL_TIME(_ns)	((_ns) << 2)		// ns * 4 us
167#define	SYMBOL_TIME_HALFGI(_ns)	(((_ns) * 18 + 4) / 5)	// ns * 3.6 us
168#define	NUM_SYMBOLS_PER_USEC(_usec)	(_usec >> 2)
169#define	NUM_SYMBOLS_PER_USEC_HALFGI(_usec)	(((_usec*5)-4)/18)
170#define	IS_HT_RATE(_rate)	((_rate) & 0x80)
171
172const uint32_t bits_per_symbol[][2] = {
173    /* 20MHz 40MHz */
174    {    26,   54 },     //  0: BPSK
175    {    52,  108 },     //  1: QPSK 1/2
176    {    78,  162 },     //  2: QPSK 3/4
177    {   104,  216 },     //  3: 16-QAM 1/2
178    {   156,  324 },     //  4: 16-QAM 3/4
179    {   208,  432 },     //  5: 64-QAM 2/3
180    {   234,  486 },     //  6: 64-QAM 3/4
181    {   260,  540 },     //  7: 64-QAM 5/6
182    {    52,  108 },     //  8: BPSK
183    {   104,  216 },     //  9: QPSK 1/2
184    {   156,  324 },     // 10: QPSK 3/4
185    {   208,  432 },     // 11: 16-QAM 1/2
186    {   312,  648 },     // 12: 16-QAM 3/4
187    {   416,  864 },     // 13: 64-QAM 2/3
188    {   468,  972 },     // 14: 64-QAM 3/4
189    {   520, 1080 },     // 15: 64-QAM 5/6
190    {    78,  162 },     // 16: BPSK
191    {   156,  324 },     // 17: QPSK 1/2
192    {   234,  486 },     // 18: QPSK 3/4
193    {   312,  648 },     // 19: 16-QAM 1/2
194    {   468,  972 },     // 20: 16-QAM 3/4
195    {   624, 1296 },     // 21: 64-QAM 2/3
196    {   702, 1458 },     // 22: 64-QAM 3/4
197    {   780, 1620 },     // 23: 64-QAM 5/6
198    {   104,  216 },     // 24: BPSK
199    {   208,  432 },     // 25: QPSK 1/2
200    {   312,  648 },     // 26: QPSK 3/4
201    {   416,  864 },     // 27: 16-QAM 1/2
202    {   624, 1296 },     // 28: 16-QAM 3/4
203    {   832, 1728 },     // 29: 64-QAM 2/3
204    {   936, 1944 },     // 30: 64-QAM 3/4
205    {  1040, 2160 },     // 31: 64-QAM 5/6
206};
207
208/*
209 * Fill in the rate array information based on the current
210 * node configuration and the choices made by the rate
211 * selection code and ath_buf setup code.
212 *
213 * Later on, this may end up also being made by the
214 * rate control code, but for now it can live here.
215 *
216 * This needs to be called just before the packet is
217 * queued to the software queue or hardware queue,
218 * so all of the needed fields in bf_state are setup.
219 */
220void
221ath_tx_rate_fill_rcflags(struct ath_softc *sc, struct ath_buf *bf)
222{
223	struct ieee80211_node *ni = bf->bf_node;
224	struct ieee80211com *ic = ni->ni_ic;
225	const HAL_RATE_TABLE *rt = sc->sc_currates;
226	struct ath_rc_series *rc = bf->bf_state.bfs_rc;
227	uint8_t rate;
228	int i;
229
230	for (i = 0; i < ATH_RC_NUM; i++) {
231		rc[i].flags = 0;
232		if (rc[i].tries == 0)
233			continue;
234
235		rate = rt->info[rc[i].rix].rateCode;
236
237		/*
238		 * XXX only do this for legacy rates?
239		 */
240		if (bf->bf_state.bfs_shpream)
241			rate |= rt->info[rc[i].rix].shortPreamble;
242
243		/*
244		 * Save this, used by the TX and completion code
245		 */
246		rc[i].ratecode = rate;
247
248		if (bf->bf_state.bfs_txflags &
249		    (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA))
250			rc[i].flags |= ATH_RC_RTSCTS_FLAG;
251
252		/* Only enable shortgi, 2040, dual-stream if HT is set */
253		if (IS_HT_RATE(rate)) {
254			rc[i].flags |= ATH_RC_HT_FLAG;
255
256			if (ni->ni_chw == 40)
257				rc[i].flags |= ATH_RC_CW40_FLAG;
258
259			if (ni->ni_chw == 40 &&
260			    ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40 &&
261			    ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40)
262				rc[i].flags |= ATH_RC_SGI_FLAG;
263
264			if (ni->ni_chw == 20 &&
265			    ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20 &&
266			    ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20)
267				rc[i].flags |= ATH_RC_SGI_FLAG;
268
269			/* XXX dual stream? and 3-stream? */
270		}
271
272		/*
273		 * Calculate the maximum 4ms frame length based
274		 * on the MCS rate, SGI and channel width flags.
275		 */
276		if ((rc[i].flags & ATH_RC_HT_FLAG) &&
277		    (HT_RC_2_MCS(rate) < 32)) {
278			int j;
279			if (rc[i].flags & ATH_RC_CW40_FLAG) {
280				if (rc[i].flags & ATH_RC_SGI_FLAG)
281					j = MCS_HT40_SGI;
282				else
283					j = MCS_HT40;
284			} else {
285				if (rc[i].flags & ATH_RC_SGI_FLAG)
286					j = MCS_HT20_SGI;
287				else
288					j = MCS_HT20;
289			}
290			rc[i].max4msframelen =
291			    ath_max_4ms_framelen[j][HT_RC_2_MCS(rate)];
292		} else
293			rc[i].max4msframelen = 0;
294		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
295		    "%s: i=%d, rate=0x%x, flags=0x%x, max4ms=%d\n",
296		    __func__, i, rate, rc[i].flags, rc[i].max4msframelen);
297	}
298}
299
300/*
301 * Return the number of delimiters to be added to
302 * meet the minimum required mpdudensity.
303 *
304 * Caller should make sure that the rate is HT.
305 *
306 * TODO: is this delimiter calculation supposed to be the
307 * total frame length, the hdr length, the data length (including
308 * delimiters, padding, CRC, etc) or ?
309 *
310 * TODO: this should ensure that the rate control information
311 * HAS been setup for the first rate.
312 *
313 * TODO: ensure this is only called for MCS rates.
314 *
315 * TODO: enforce MCS < 31
316 */
317static int
318ath_compute_num_delims(struct ath_softc *sc, struct ath_buf *first_bf,
319    uint16_t pktlen)
320{
321	const HAL_RATE_TABLE *rt = sc->sc_currates;
322	struct ieee80211_node *ni = first_bf->bf_node;
323	struct ieee80211vap *vap = ni->ni_vap;
324	int ndelim, mindelim = 0;
325	int mpdudensity;	 /* in 1/100'th of a microsecond */
326	uint8_t rc, rix, flags;
327	int width, half_gi;
328	uint32_t nsymbits, nsymbols;
329	uint16_t minlen;
330
331	/*
332	 * vap->iv_ampdu_density is a value, rather than the actual
333	 * density.
334	 */
335	if (vap->iv_ampdu_density > IEEE80211_HTCAP_MPDUDENSITY_16)
336		mpdudensity = 1600;		/* maximum density */
337	else
338		mpdudensity = ieee80211_mpdudensity_map[vap->iv_ampdu_density];
339
340	/* Select standard number of delimiters based on frame length */
341	ndelim = ATH_AGGR_GET_NDELIM(pktlen);
342
343	/*
344	 * If encryption is enabled, add extra delimiters to let the
345	 * crypto hardware catch up. This could be tuned per-MAC and
346	 * per-rate, but for now we'll simply assume encryption is
347	 * always enabled.
348	 */
349	ndelim += ATH_AGGR_ENCRYPTDELIM;
350
351	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
352	    "%s: pktlen=%d, ndelim=%d, mpdudensity=%d\n",
353	    __func__, pktlen, ndelim, mpdudensity);
354
355	/*
356	 * If the MPDU density is 0, we can return here.
357	 * Otherwise, we need to convert the desired mpdudensity
358	 * into a byte length, based on the rate in the subframe.
359	 */
360	if (mpdudensity == 0)
361		return ndelim;
362
363	/*
364	 * Convert desired mpdu density from microeconds to bytes based
365	 * on highest rate in rate series (i.e. first rate) to determine
366	 * required minimum length for subframe. Take into account
367	 * whether high rate is 20 or 40Mhz and half or full GI.
368	 */
369	rix = first_bf->bf_state.bfs_rc[0].rix;
370	rc = rt->info[rix].rateCode;
371	flags = first_bf->bf_state.bfs_rc[0].flags;
372	width = !! (flags & ATH_RC_CW40_FLAG);
373	half_gi = !! (flags & ATH_RC_SGI_FLAG);
374
375	/*
376	 * mpdudensity is in 1/100th of a usec, so divide by 100
377	 */
378	if (half_gi)
379		nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(mpdudensity);
380	else
381		nsymbols = NUM_SYMBOLS_PER_USEC(mpdudensity);
382	nsymbols /= 100;
383
384	if (nsymbols == 0)
385		nsymbols = 1;
386
387	nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
388	minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
389
390	/*
391	 * Min length is the minimum frame length for the
392	 * required MPDU density.
393	 */
394	if (pktlen < minlen) {
395		mindelim = (minlen - pktlen) / ATH_AGGR_DELIM_SZ;
396		ndelim = MAX(mindelim, ndelim);
397	}
398
399	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
400	    "%s: pktlen=%d, minlen=%d, rix=%x, rc=%x, width=%d, hgi=%d, ndelim=%d\n",
401	    __func__, pktlen, minlen, rix, rc, width, half_gi, ndelim);
402
403	return ndelim;
404}
405
406/*
407 * Fetch the aggregation limit.
408 *
409 * It's the lowest of the four rate series 4ms frame length.
410 */
411static int
412ath_get_aggr_limit(struct ath_softc *sc, struct ath_buf *bf)
413{
414	int amin = 65530;
415	int i;
416
417	for (i = 0; i < 4; i++) {
418		if (bf->bf_state.bfs_rc[i].tries == 0)
419			continue;
420		amin = MIN(amin, bf->bf_state.bfs_rc[i].max4msframelen);
421	}
422
423	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: max frame len= %d\n",
424	    __func__, amin);
425
426	return amin;
427}
428
429/*
430 * Setup a 11n rate series structure
431 *
432 * This should be called for both legacy and MCS rates.
433 *
434 * It, along with ath_buf_set_rate, must be called -after- a burst
435 * or aggregate is setup.
436 */
437static void
438ath_rateseries_setup(struct ath_softc *sc, struct ieee80211_node *ni,
439    struct ath_buf *bf, HAL_11N_RATE_SERIES *series)
440{
441	struct ieee80211com *ic = ni->ni_ic;
442	struct ath_hal *ah = sc->sc_ah;
443	HAL_BOOL shortPreamble = AH_FALSE;
444	const HAL_RATE_TABLE *rt = sc->sc_currates;
445	int i;
446	int pktlen;
447	int flags = bf->bf_state.bfs_txflags;
448	struct ath_rc_series *rc = bf->bf_state.bfs_rc;
449
450	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
451	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE))
452		shortPreamble = AH_TRUE;
453
454	/*
455	 * If this is the first frame in an aggregate series,
456	 * use the aggregate length.
457	 */
458	if (bf->bf_state.bfs_aggr)
459		pktlen = bf->bf_state.bfs_al;
460	else
461		pktlen = bf->bf_state.bfs_pktlen;
462
463	/*
464	 * XXX TODO: modify this routine to use the bfs_rc[x].flags
465	 * XXX fields.
466	 */
467	memset(series, 0, sizeof(HAL_11N_RATE_SERIES) * 4);
468	for (i = 0; i < 4;  i++) {
469		/* Only set flags for actual TX attempts */
470		if (rc[i].tries == 0)
471			continue;
472
473		series[i].Tries = rc[i].tries;
474
475		/*
476		 * XXX this isn't strictly correct - sc_txchainmask
477		 * XXX isn't the currently active chainmask;
478		 * XXX it's the interface chainmask at startup.
479		 * XXX It's overridden in the HAL rate scenario function
480		 * XXX for now.
481		 */
482		series[i].ChSel = sc->sc_txchainmask;
483
484		if (flags & (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA))
485			series[i].RateFlags |= HAL_RATESERIES_RTS_CTS;
486
487		/*
488		 * Transmit 40MHz frames only if the node has negotiated
489		 * it rather than whether the node is capable of it or not.
490	 	 * It's subtly different in the hostap case.
491	 	 */
492		if (ni->ni_chw == 40)
493			series[i].RateFlags |= HAL_RATESERIES_2040;
494
495		/*
496		 * Set short-GI only if the node has advertised it
497		 * the channel width is suitable, and we support it.
498		 * We don't currently have a "negotiated" set of bits -
499		 * ni_htcap is what the remote end sends, not what this
500		 * node is capable of.
501		 */
502		if (ni->ni_chw == 40 &&
503		    ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40 &&
504		    ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40)
505			series[i].RateFlags |= HAL_RATESERIES_HALFGI;
506
507		if (ni->ni_chw == 20 &&
508		    ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20 &&
509		    ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20)
510			series[i].RateFlags |= HAL_RATESERIES_HALFGI;
511
512		series[i].Rate = rt->info[rc[i].rix].rateCode;
513
514		/* PktDuration doesn't include slot, ACK, RTS, etc timing - it's just the packet duration */
515		if (series[i].Rate & IEEE80211_RATE_MCS) {
516			series[i].PktDuration =
517			    ath_computedur_ht(pktlen
518				, series[i].Rate
519				, HT_RC_2_STREAMS(series[i].Rate)
520				, series[i].RateFlags & HAL_RATESERIES_2040
521				, series[i].RateFlags & HAL_RATESERIES_HALFGI);
522		} else {
523			if (shortPreamble)
524				series[i].Rate |=
525				    rt->info[rc[i].rix].shortPreamble;
526			series[i].PktDuration = ath_hal_computetxtime(ah,
527			    rt, pktlen, rc[i].rix, shortPreamble);
528		}
529	}
530}
531
532#if 0
533static void
534ath_rateseries_print(HAL_11N_RATE_SERIES *series)
535{
536	int i;
537	for (i = 0; i < 4; i++) {
538		printf("series %d: rate %x; tries %d; pktDuration %d; chSel %d; rateFlags %x\n",
539		    i,
540		    series[i].Rate,
541		    series[i].Tries,
542		    series[i].PktDuration,
543		    series[i].ChSel,
544		    series[i].RateFlags);
545	}
546}
547#endif
548
549/*
550 * Setup the 11n rate scenario and burst duration for the given TX descriptor
551 * list.
552 *
553 * This isn't useful for sending beacon frames, which has different needs
554 * wrt what's passed into the rate scenario function.
555 */
556
557void
558ath_buf_set_rate(struct ath_softc *sc, struct ieee80211_node *ni,
559    struct ath_buf *bf)
560{
561	HAL_11N_RATE_SERIES series[4];
562	struct ath_desc *ds = bf->bf_desc;
563	struct ath_desc *lastds = NULL;
564	struct ath_hal *ah = sc->sc_ah;
565	int is_pspoll = (bf->bf_state.bfs_atype == HAL_PKT_TYPE_PSPOLL);
566	int ctsrate = bf->bf_state.bfs_ctsrate;
567	int flags = bf->bf_state.bfs_txflags;
568
569	/* Setup rate scenario */
570	memset(&series, 0, sizeof(series));
571
572	ath_rateseries_setup(sc, ni, bf, series);
573
574	/* Enforce AR5416 aggregate limit - can't do RTS w/ an agg frame > 8k */
575
576	/* Enforce RTS and CTS are mutually exclusive */
577
578	/* Get a pointer to the last tx descriptor in the list */
579	lastds = bf->bf_lastds;
580
581#if 0
582	printf("pktlen: %d; flags 0x%x\n", pktlen, flags);
583	ath_rateseries_print(series);
584#endif
585
586	/* Set rate scenario */
587	ath_hal_set11nratescenario(ah, ds,
588	    !is_pspoll,	/* whether to override the duration or not */
589			/* don't allow hardware to override the duration on ps-poll packets */
590	    ctsrate,	/* rts/cts rate */
591	    series,	/* 11n rate series */
592	    4,		/* number of series */
593	    flags);
594
595	/* Setup the last descriptor in the chain */
596	ath_hal_setuplasttxdesc(ah, lastds, ds);
597
598	/* Set burst duration */
599	/*
600	 * This is only required when doing 11n burst, not aggregation
601	 * ie, if there's a second frame in a RIFS or A-MPDU burst
602	 * w/ >1 A-MPDU frame bursting back to back.
603	 * Normal A-MPDU doesn't do bursting -between- aggregates.
604	 *
605	 * .. and it's highly likely this won't ever be implemented
606	 */
607	//ath_hal_set11nburstduration(ah, ds, 8192);
608}
609
610/*
611 * Form an aggregate packet list.
612 *
613 * This function enforces the aggregate restrictions/requirements.
614 *
615 * These are:
616 *
617 * + The aggregate size maximum (64k for AR9160 and later, 8K for
618 *   AR5416 when doing RTS frame protection.)
619 * + Maximum number of sub-frames for an aggregate
620 * + The aggregate delimiter size, giving MACs time to do whatever is
621 *   needed before each frame
622 * + Enforce the BAW limit
623 *
624 * Each descriptor queued should have the DMA setup.
625 * The rate series, descriptor setup, linking, etc is all done
626 * externally. This routine simply chains them together.
627 * ath_tx_setds_11n() will take care of configuring the per-
628 * descriptor setup, and ath_buf_set_rate() will configure the
629 * rate control.
630 *
631 * Note that the TID lock is only grabbed when dequeuing packets from
632 * the TID queue. If some code in another thread adds to the head of this
633 * list, very strange behaviour will occur. Since retransmission is the
634 * only reason this will occur, and this routine is designed to be called
635 * from within the scheduler task, it won't ever clash with the completion
636 * task.
637 *
638 * So if you want to call this from an upper layer context (eg, to direct-
639 * dispatch aggregate frames to the hardware), please keep this in mind.
640 */
641ATH_AGGR_STATUS
642ath_tx_form_aggr(struct ath_softc *sc, struct ath_node *an, struct ath_tid *tid,
643    ath_bufhead *bf_q)
644{
645	//struct ieee80211_node *ni = &an->an_node;
646	struct ath_buf *bf, *bf_first = NULL, *bf_prev = NULL;
647	int nframes = 0;
648	uint16_t aggr_limit = 0, al = 0, bpad = 0, al_delta, h_baw;
649	struct ieee80211_tx_ampdu *tap;
650	int status = ATH_AGGR_DONE;
651	int prev_frames = 0;	/* XXX for AR5416 burst, not done here */
652	int prev_al = 0;	/* XXX also for AR5416 burst */
653
654	ATH_TXQ_LOCK_ASSERT(sc->sc_ac2q[tid->ac]);
655
656	tap = ath_tx_get_tx_tid(an, tid->tid);
657	if (tap == NULL) {
658		status = ATH_AGGR_ERROR;
659		goto finish;
660	}
661
662	h_baw = tap->txa_wnd / 2;
663
664	for (;;) {
665		bf = TAILQ_FIRST(&tid->axq_q);
666		if (bf_first == NULL)
667			bf_first = bf;
668		if (bf == NULL) {
669			status = ATH_AGGR_DONE;
670			break;
671		} else {
672			/*
673			 * It's the first frame;
674			 * set the aggregation limit based on the
675			 * rate control decision that has been made.
676			 */
677			aggr_limit = ath_get_aggr_limit(sc, bf_first);
678		}
679
680		/* Set this early just so things don't get confused */
681		bf->bf_next = NULL;
682
683		/*
684		 * Don't unlock the tid lock until we're sure we are going
685		 * to queue this frame.
686		 */
687
688		/*
689		 * If the frame doesn't have a sequence number that we're
690		 * tracking in the BAW (eg NULL QOS data frame), we can't
691		 * aggregate it. Stop the aggregation process; the sender
692		 * can then TX what's in the list thus far and then
693		 * TX the frame individually.
694		 */
695		if (! bf->bf_state.bfs_dobaw) {
696			status = ATH_AGGR_NONAGGR;
697			break;
698		}
699
700		/*
701		 * If any of the rates are non-HT, this packet
702		 * can't be aggregated.
703		 * XXX TODO: add a bf_state flag which gets marked
704		 * if any active rate is non-HT.
705		 */
706
707		/*
708		 * do not exceed aggregation limit
709		 */
710		al_delta = ATH_AGGR_DELIM_SZ + bf->bf_state.bfs_pktlen;
711		if (nframes &&
712		    (aggr_limit < (al + bpad + al_delta + prev_al))) {
713			status = ATH_AGGR_LIMITED;
714			break;
715		}
716
717		/*
718		 * If RTS/CTS is set on the first frame, enforce
719		 * the RTS aggregate limit.
720		 */
721		if (bf_first->bf_state.bfs_txflags &
722		    (HAL_TXDESC_CTSENA | HAL_TXDESC_RTSENA)) {
723			if (nframes &&
724			   (sc->sc_rts_aggr_limit <
725			     (al + bpad + al_delta + prev_al))) {
726				status = ATH_AGGR_8K_LIMITED;
727				break;
728			}
729		}
730
731		/*
732		 * Do not exceed subframe limit.
733		 */
734		if ((nframes + prev_frames) >= MIN((h_baw),
735		    IEEE80211_AMPDU_SUBFRAME_DEFAULT)) {
736			status = ATH_AGGR_LIMITED;
737			break;
738		}
739
740		/*
741		 * If the current frame has an RTS/CTS configuration
742		 * that differs from the first frame, override the
743		 * subsequent frame with this config.
744		 */
745		bf->bf_state.bfs_txflags &=
746		    (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA);
747		bf->bf_state.bfs_txflags |=
748		    bf_first->bf_state.bfs_txflags &
749		    (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA);
750
751		/*
752		 * If the packet has a sequence number, do not
753		 * step outside of the block-ack window.
754		 */
755		if (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
756		    SEQNO(bf->bf_state.bfs_seqno))) {
757			status = ATH_AGGR_BAW_CLOSED;
758			break;
759		}
760
761		/*
762		 * this packet is part of an aggregate.
763		 */
764		ATH_TXQ_REMOVE(tid, bf, bf_list);
765
766		/* The TID lock is required for the BAW update */
767		ath_tx_addto_baw(sc, an, tid, bf);
768		bf->bf_state.bfs_addedbaw = 1;
769
770		/*
771		 * XXX enforce ACK for aggregate frames (this needs to be
772		 * XXX handled more gracefully?
773		 */
774		if (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) {
775			device_printf(sc->sc_dev,
776			    "%s: HAL_TXDESC_NOACK set for an aggregate frame?\n",
777			    __func__);
778			bf->bf_state.bfs_txflags &= (~HAL_TXDESC_NOACK);
779		}
780
781		/*
782		 * Add the now owned buffer (which isn't
783		 * on the software TXQ any longer) to our
784		 * aggregate frame list.
785		 */
786		TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
787		nframes ++;
788
789		/* Completion handler */
790		bf->bf_comp = ath_tx_aggr_comp;
791
792		/*
793		 * add padding for previous frame to aggregation length
794		 */
795		al += bpad + al_delta;
796
797		/*
798		 * Calculate delimiters needed for the current frame
799		 */
800		bf->bf_state.bfs_ndelim =
801		    ath_compute_num_delims(sc, bf_first,
802		    bf->bf_state.bfs_pktlen);
803
804		/*
805		 * Calculate the padding needed from this set of delimiters,
806		 * used when calculating if the next frame will fit in
807		 * the aggregate.
808		 */
809		bpad = PADBYTES(al_delta) + (bf->bf_state.bfs_ndelim << 2);
810
811		/*
812		 * Chain the buffers together
813		 */
814		if (bf_prev)
815			bf_prev->bf_next = bf;
816		bf_prev = bf;
817
818		/*
819		 * XXX TODO: if any sub-frames have RTS/CTS enabled;
820		 * enable it for the entire aggregate.
821		 */
822
823#if 0
824		/*
825		 * terminate aggregation on a small packet boundary
826		 */
827		if (bf->bf_state.bfs_pktlen < ATH_AGGR_MINPLEN) {
828			status = ATH_AGGR_SHORTPKT;
829			break;
830		}
831#endif
832
833	}
834
835finish:
836	/*
837	 * Just in case the list was empty when we tried to
838	 * dequeue a packet ..
839	 */
840	if (bf_first) {
841		bf_first->bf_state.bfs_al = al;
842		bf_first->bf_state.bfs_nframes = nframes;
843	}
844	return status;
845}
846