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