xref: /freebsd/sys/dev/ath/if_ath_tx_ht.c (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
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$");
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 
113 int 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  */
143 static 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 
173 const 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  */
221 void
222 ath_tx_rate_fill_rcflags(struct ath_softc *sc, struct ath_buf *bf)
223 {
224 	struct ieee80211_node *ni = bf->bf_node;
225 	struct ieee80211vap *vap = ni->ni_vap;
226 	struct ieee80211com *ic = ni->ni_ic;
227 	const HAL_RATE_TABLE *rt = sc->sc_currates;
228 	struct ath_rc_series *rc = bf->bf_state.bfs_rc;
229 	uint8_t rate;
230 	int i;
231 	int do_ldpc;
232 	int do_stbc;
233 
234 	/*
235 	 * We only do LDPC if the rate is 11n, both we and the
236 	 * receiver support LDPC and it's enabled.
237 	 *
238 	 * It's a global flag, not a per-try flag, so we clear
239 	 * it if any of the rate entries aren't 11n.
240 	 */
241 	do_ldpc = 0;
242 	if ((ni->ni_vap->iv_htcaps & IEEE80211_HTCAP_LDPC) &&
243 	    (ni->ni_htcap & IEEE80211_HTCAP_LDPC))
244 		do_ldpc = 1;
245 
246 	/*
247 	 * The 11n duration calculation doesn't know about LDPC,
248 	 * so don't enable it for positioning.
249 	 */
250 	if (bf->bf_flags & ATH_BUF_TOA_PROBE)
251 		do_ldpc = 0;
252 
253 	do_stbc = 0;
254 
255 	for (i = 0; i < ATH_RC_NUM; i++) {
256 		rc[i].flags = 0;
257 		if (rc[i].tries == 0)
258 			continue;
259 
260 		rate = rt->info[rc[i].rix].rateCode;
261 
262 		/*
263 		 * Only enable short preamble for legacy rates
264 		 */
265 		if ((! IS_HT_RATE(rate)) && bf->bf_state.bfs_shpream)
266 			rate |= rt->info[rc[i].rix].shortPreamble;
267 
268 		/*
269 		 * Save this, used by the TX and completion code
270 		 */
271 		rc[i].ratecode = rate;
272 
273 		if (bf->bf_state.bfs_txflags &
274 		    (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA))
275 			rc[i].flags |= ATH_RC_RTSCTS_FLAG;
276 
277 		/*
278 		 * If we can't do LDPC, don't.
279 		 */
280 		if (! IS_HT_RATE(rate))
281 			do_ldpc = 0;
282 
283 		/* Only enable shortgi, 2040, dual-stream if HT is set */
284 		if (IS_HT_RATE(rate)) {
285 			rc[i].flags |= ATH_RC_HT_FLAG;
286 
287 			if (ni->ni_chw == 40)
288 				rc[i].flags |= ATH_RC_CW40_FLAG;
289 
290 			/*
291 			 * NOTE: Don't do short-gi for positioning frames.
292 			 *
293 			 * For now, the ath_hal and net80211 HT duration
294 			 * calculation rounds up the 11n data txtime
295 			 * to the nearest multiple of 3.6 microseconds
296 			 * and doesn't return the fractional part, so
297 			 * we are always "out" by some amount.
298 			 */
299 			if (ni->ni_chw == 40 &&
300 			    ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40 &&
301 			    ni->ni_htcap & IEEE80211_HTCAP_SHORTGI40 &&
302 			    vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40 &&
303 			    (bf->bf_flags & ATH_BUF_TOA_PROBE) == 0) {
304 				rc[i].flags |= ATH_RC_SGI_FLAG;
305 			}
306 
307 			if (ni->ni_chw == 20 &&
308 			    ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20 &&
309 			    ni->ni_htcap & IEEE80211_HTCAP_SHORTGI20 &&
310 			    vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20 &&
311 			    (bf->bf_flags & ATH_BUF_TOA_PROBE) == 0) {
312 				rc[i].flags |= ATH_RC_SGI_FLAG;
313 			}
314 
315 			/*
316 			 * If we have STBC TX enabled and the receiver
317 			 * can receive (at least) 1 stream STBC, AND it's
318 			 * MCS 0-7, AND we have at least two chains enabled,
319 			 * and we're not doing positioning, enable STBC.
320 			 */
321 			if (ic->ic_htcaps & IEEE80211_HTCAP_TXSTBC &&
322 			    ni->ni_vap->iv_flags_ht & IEEE80211_FHT_STBC_TX &&
323 			    ni->ni_htcap & IEEE80211_HTCAP_RXSTBC_1STREAM &&
324 			    (sc->sc_cur_txchainmask > 1) &&
325 			    (HT_RC_2_STREAMS(rate) == 1) &&
326 			    (bf->bf_flags & ATH_BUF_TOA_PROBE) == 0) {
327 				rc[i].flags |= ATH_RC_STBC_FLAG;
328 				do_stbc = 1;
329 			}
330 
331 			/*
332 			 * Dual / Triple stream rate?
333 			 */
334 			if (HT_RC_2_STREAMS(rate) == 2)
335 				rc[i].flags |= ATH_RC_DS_FLAG;
336 			else if (HT_RC_2_STREAMS(rate) == 3)
337 				rc[i].flags |= ATH_RC_TS_FLAG;
338 		}
339 
340 		/*
341 		 * Calculate the maximum TX power cap for the current
342 		 * node.
343 		 */
344 		rc[i].tx_power_cap = ieee80211_get_node_txpower(ni);
345 
346 		/*
347 		 * Calculate the maximum 4ms frame length based
348 		 * on the MCS rate, SGI and channel width flags.
349 		 */
350 		if ((rc[i].flags & ATH_RC_HT_FLAG) &&
351 		    (HT_RC_2_MCS(rate) < 32)) {
352 			int j;
353 			if (rc[i].flags & ATH_RC_CW40_FLAG) {
354 				if (rc[i].flags & ATH_RC_SGI_FLAG)
355 					j = MCS_HT40_SGI;
356 				else
357 					j = MCS_HT40;
358 			} else {
359 				if (rc[i].flags & ATH_RC_SGI_FLAG)
360 					j = MCS_HT20_SGI;
361 				else
362 					j = MCS_HT20;
363 			}
364 			rc[i].max4msframelen =
365 			    ath_max_4ms_framelen[j][HT_RC_2_MCS(rate)];
366 		} else
367 			rc[i].max4msframelen = 0;
368 		DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
369 		    "%s: i=%d, rate=0x%x, flags=0x%x, max4ms=%d\n",
370 		    __func__, i, rate, rc[i].flags, rc[i].max4msframelen);
371 	}
372 
373 	/*
374 	 * LDPC is a global flag, so ...
375 	 */
376 	if (do_ldpc) {
377 		bf->bf_state.bfs_txflags |= HAL_TXDESC_LDPC;
378 		sc->sc_stats.ast_tx_ldpc++;
379 	}
380 
381 	if (do_stbc) {
382 		sc->sc_stats.ast_tx_stbc++;
383 	}
384 }
385 
386 /*
387  * Return the number of delimiters to be added to
388  * meet the minimum required mpdudensity.
389  *
390  * Caller should make sure that the rate is HT.
391  *
392  * TODO: is this delimiter calculation supposed to be the
393  * total frame length, the hdr length, the data length (including
394  * delimiters, padding, CRC, etc) or ?
395  *
396  * TODO: this should ensure that the rate control information
397  * HAS been setup for the first rate.
398  *
399  * TODO: ensure this is only called for MCS rates.
400  *
401  * TODO: enforce MCS < 31
402  */
403 static int
404 ath_compute_num_delims(struct ath_softc *sc, struct ath_buf *first_bf,
405     uint16_t pktlen)
406 {
407 	const HAL_RATE_TABLE *rt = sc->sc_currates;
408 	struct ieee80211_node *ni = first_bf->bf_node;
409 	struct ieee80211vap *vap = ni->ni_vap;
410 	int ndelim, mindelim = 0;
411 	int mpdudensity;	 /* in 1/100'th of a microsecond */
412 	uint8_t rc, rix, flags;
413 	int width, half_gi;
414 	uint32_t nsymbits, nsymbols;
415 	uint16_t minlen;
416 
417 	/*
418 	 * vap->iv_ampdu_density is a value, rather than the actual
419 	 * density.
420 	 */
421 	if (vap->iv_ampdu_density > IEEE80211_HTCAP_MPDUDENSITY_16)
422 		mpdudensity = 1600;		/* maximum density */
423 	else
424 		mpdudensity = ieee80211_mpdudensity_map[vap->iv_ampdu_density];
425 
426 	/* Select standard number of delimiters based on frame length */
427 	ndelim = ATH_AGGR_GET_NDELIM(pktlen);
428 
429 	/*
430 	 * If encryption is enabled, add extra delimiters to let the
431 	 * crypto hardware catch up. This could be tuned per-MAC and
432 	 * per-rate, but for now we'll simply assume encryption is
433 	 * always enabled.
434 	 *
435 	 * Also note that the Atheros reference driver inserts two
436 	 * delimiters by default for pre-AR9380 peers.  This will
437 	 * include "that" required delimiter.
438 	 */
439 	ndelim += ATH_AGGR_ENCRYPTDELIM;
440 
441 	/*
442 	 * For AR9380, there's a minimum number of delimeters
443 	 * required when doing RTS.
444 	 *
445 	 * XXX TODO: this is only needed if (a) RTS/CTS is enabled, and
446 	 * XXX (b) this is the first sub-frame in the aggregate.
447 	 */
448 	if (sc->sc_use_ent && (sc->sc_ent_cfg & AH_ENT_RTSCTS_DELIM_WAR)
449 	    && ndelim < AH_FIRST_DESC_NDELIMS)
450 		ndelim = AH_FIRST_DESC_NDELIMS;
451 
452 	/*
453 	 * If sc_delim_min_pad is non-zero, enforce it as the minimum
454 	 * pad delimiter count.
455 	 */
456 	if (sc->sc_delim_min_pad != 0)
457 		ndelim = MAX(ndelim, sc->sc_delim_min_pad);
458 
459 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
460 	    "%s: pktlen=%d, ndelim=%d, mpdudensity=%d\n",
461 	    __func__, pktlen, ndelim, mpdudensity);
462 
463 	/*
464 	 * If the MPDU density is 0, we can return here.
465 	 * Otherwise, we need to convert the desired mpdudensity
466 	 * into a byte length, based on the rate in the subframe.
467 	 */
468 	if (mpdudensity == 0)
469 		return ndelim;
470 
471 	/*
472 	 * Convert desired mpdu density from microeconds to bytes based
473 	 * on highest rate in rate series (i.e. first rate) to determine
474 	 * required minimum length for subframe. Take into account
475 	 * whether high rate is 20 or 40Mhz and half or full GI.
476 	 */
477 	rix = first_bf->bf_state.bfs_rc[0].rix;
478 	rc = rt->info[rix].rateCode;
479 	flags = first_bf->bf_state.bfs_rc[0].flags;
480 	width = !! (flags & ATH_RC_CW40_FLAG);
481 	half_gi = !! (flags & ATH_RC_SGI_FLAG);
482 
483 	/*
484 	 * mpdudensity is in 1/100th of a usec, so divide by 100
485 	 */
486 	if (half_gi)
487 		nsymbols = NUM_SYMBOLS_PER_USEC_HALFGI(mpdudensity);
488 	else
489 		nsymbols = NUM_SYMBOLS_PER_USEC(mpdudensity);
490 	nsymbols /= 100;
491 
492 	if (nsymbols == 0)
493 		nsymbols = 1;
494 
495 	nsymbits = bits_per_symbol[HT_RC_2_MCS(rc)][width];
496 	minlen = (nsymbols * nsymbits) / BITS_PER_BYTE;
497 
498 	/*
499 	 * Min length is the minimum frame length for the
500 	 * required MPDU density.
501 	 */
502 	if (pktlen < minlen) {
503 		mindelim = (minlen - pktlen) / ATH_AGGR_DELIM_SZ;
504 		ndelim = MAX(mindelim, ndelim);
505 	}
506 
507 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR,
508 	    "%s: pktlen=%d, minlen=%d, rix=%x, rc=%x, width=%d, hgi=%d, ndelim=%d\n",
509 	    __func__, pktlen, minlen, rix, rc, width, half_gi, ndelim);
510 
511 	return ndelim;
512 }
513 
514 /*
515  * Fetch the aggregation limit.
516  *
517  * It's the lowest of the four rate series 4ms frame length.
518  */
519 static int
520 ath_get_aggr_limit(struct ath_softc *sc, struct ath_buf *bf)
521 {
522 	int amin = ATH_AGGR_MAXSIZE;
523 	int i;
524 
525 	if (sc->sc_aggr_limit > 0 && sc->sc_aggr_limit < ATH_AGGR_MAXSIZE)
526 		amin = sc->sc_aggr_limit;
527 
528 	for (i = 0; i < ATH_RC_NUM; i++) {
529 		if (bf->bf_state.bfs_rc[i].tries == 0)
530 			continue;
531 		amin = MIN(amin, bf->bf_state.bfs_rc[i].max4msframelen);
532 	}
533 
534 	DPRINTF(sc, ATH_DEBUG_SW_TX_AGGR, "%s: max frame len= %d\n",
535 	    __func__, amin);
536 
537 	return amin;
538 }
539 
540 /*
541  * Setup a 11n rate series structure
542  *
543  * This should be called for both legacy and MCS rates.
544  *
545  * This uses the rate series stuf from ath_tx_rate_fill_rcflags().
546  *
547  * It, along with ath_buf_set_rate, must be called -after- a burst
548  * or aggregate is setup.
549  */
550 static void
551 ath_rateseries_setup(struct ath_softc *sc, struct ieee80211_node *ni,
552     struct ath_buf *bf, HAL_11N_RATE_SERIES *series)
553 {
554 	struct ieee80211com *ic = ni->ni_ic;
555 	struct ath_hal *ah = sc->sc_ah;
556 	HAL_BOOL shortPreamble = AH_FALSE;
557 	const HAL_RATE_TABLE *rt = sc->sc_currates;
558 	int i;
559 	int pktlen;
560 	struct ath_rc_series *rc = bf->bf_state.bfs_rc;
561 
562 	if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) &&
563 	    (ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE))
564 		shortPreamble = AH_TRUE;
565 
566 	/*
567 	 * If this is the first frame in an aggregate series,
568 	 * use the aggregate length.
569 	 */
570 	if (bf->bf_state.bfs_aggr)
571 		pktlen = bf->bf_state.bfs_al;
572 	else
573 		pktlen = bf->bf_state.bfs_pktlen;
574 
575 	/*
576 	 * XXX TODO: modify this routine to use the bfs_rc[x].flags
577 	 * XXX fields.
578 	 */
579 	memset(series, 0, sizeof(HAL_11N_RATE_SERIES) * 4);
580 	for (i = 0; i < ATH_RC_NUM;  i++) {
581 		/* Only set flags for actual TX attempts */
582 		if (rc[i].tries == 0)
583 			continue;
584 
585 		series[i].Tries = rc[i].tries;
586 
587 		/*
588 		 * XXX TODO: When the NIC is capable of three stream TX,
589 		 * transmit 1/2 stream rates on two streams.
590 		 *
591 		 * This reduces the power consumption of the NIC and
592 		 * keeps it within the PCIe slot power limits.
593 		 */
594 		series[i].ChSel = sc->sc_cur_txchainmask;
595 
596 		/*
597 		 * Setup rate and TX power cap for this series.
598 		 */
599 		series[i].Rate = rt->info[rc[i].rix].rateCode;
600 		series[i].RateIndex = rc[i].rix;
601 		series[i].tx_power_cap = rc[i].tx_power_cap;
602 
603 		/*
604 		 * Enable RTS/CTS as appropriate.
605 		 */
606 		if (rc[i].flags & ATH_RC_RTSCTS_FLAG)
607 			series[i].RateFlags |= HAL_RATESERIES_RTS_CTS;
608 
609 		/*
610 		 * 11n rate? Update 11n flags.
611 		 */
612 		if (rc[i].flags & ATH_RC_HT_FLAG) {
613 			if (rc[i].flags & ATH_RC_CW40_FLAG)
614 				series[i].RateFlags |= HAL_RATESERIES_2040;
615 
616 			if (rc[i].flags & ATH_RC_SGI_FLAG)
617 				series[i].RateFlags |= HAL_RATESERIES_HALFGI;
618 
619 			if (rc[i].flags & ATH_RC_STBC_FLAG)
620 				series[i].RateFlags |= HAL_RATESERIES_STBC;
621 		}
622 
623 		/*
624 		 * TODO: If we're all doing 11n rates then we can set LDPC.
625 		 * If we've been asked to /do/ LDPC but we are handed a
626 		 * legacy rate, then we should complain.  Loudly.
627 		 */
628 
629 		/*
630 		 * PktDuration doesn't include slot, ACK, RTS, etc timing -
631 		 * it's just the packet duration
632 		 */
633 		if (rc[i].flags & ATH_RC_HT_FLAG) {
634 			series[i].PktDuration =
635 			    ath_computedur_ht(pktlen
636 				, series[i].Rate
637 				, HT_RC_2_STREAMS(series[i].Rate)
638 				, series[i].RateFlags & HAL_RATESERIES_2040
639 				, series[i].RateFlags & HAL_RATESERIES_HALFGI);
640 		} else {
641 			if (shortPreamble)
642 				series[i].Rate |=
643 				    rt->info[rc[i].rix].shortPreamble;
644 			/* XXX TODO: don't include SIFS */
645 			series[i].PktDuration = ath_hal_computetxtime(ah,
646 			    rt, pktlen, rc[i].rix, shortPreamble, AH_TRUE);
647 		}
648 	}
649 }
650 
651 #ifdef	ATH_DEBUG
652 static void
653 ath_rateseries_print(struct ath_softc *sc, HAL_11N_RATE_SERIES *series)
654 {
655 	int i;
656 	for (i = 0; i < ATH_RC_NUM; i++) {
657 		device_printf(sc->sc_dev ,"series %d: rate %x; tries %d; "
658 		    "pktDuration %d; chSel %d; txpowcap %d, rateFlags %x\n",
659 		    i,
660 		    series[i].Rate,
661 		    series[i].Tries,
662 		    series[i].PktDuration,
663 		    series[i].ChSel,
664 		    series[i].tx_power_cap,
665 		    series[i].RateFlags);
666 	}
667 }
668 #endif
669 
670 /*
671  * Setup the 11n rate scenario and burst duration for the given TX descriptor
672  * list.
673  *
674  * This isn't useful for sending beacon frames, which has different needs
675  * wrt what's passed into the rate scenario function.
676  */
677 void
678 ath_buf_set_rate(struct ath_softc *sc, struct ieee80211_node *ni,
679     struct ath_buf *bf)
680 {
681 	HAL_11N_RATE_SERIES series[4];
682 	struct ath_desc *ds = bf->bf_desc;
683 	struct ath_hal *ah = sc->sc_ah;
684 	int is_pspoll = (bf->bf_state.bfs_atype == HAL_PKT_TYPE_PSPOLL);
685 	int ctsrate = bf->bf_state.bfs_ctsrate;
686 	int flags = bf->bf_state.bfs_txflags;
687 
688 	/* Setup rate scenario */
689 	memset(&series, 0, sizeof(series));
690 
691 	ath_rateseries_setup(sc, ni, bf, series);
692 
693 #ifdef	ATH_DEBUG
694 	if (sc->sc_debug & ATH_DEBUG_XMIT)
695 		ath_rateseries_print(sc, series);
696 #endif
697 
698 	/* Set rate scenario */
699 	/*
700 	 * Note: Don't allow hardware to override the duration on
701 	 * ps-poll packets.
702 	 */
703 	ath_hal_set11nratescenario(ah, ds,
704 	    !is_pspoll,	/* whether to override the duration or not */
705 	    ctsrate,	/* rts/cts rate */
706 	    series,	/* 11n rate series */
707 	    4,		/* number of series */
708 	    flags);
709 
710 	/* Set burst duration */
711 	/*
712 	 * This is only required when doing 11n burst, not aggregation
713 	 * ie, if there's a second frame in a RIFS or A-MPDU burst
714 	 * w/ >1 A-MPDU frame bursting back to back.
715 	 * Normal A-MPDU doesn't do bursting -between- aggregates.
716 	 *
717 	 * .. and it's highly likely this won't ever be implemented
718 	 */
719 	//ath_hal_set11nburstduration(ah, ds, 8192);
720 }
721 
722 /*
723  * Form an aggregate packet list.
724  *
725  * This function enforces the aggregate restrictions/requirements.
726  *
727  * These are:
728  *
729  * + The aggregate size maximum (64k for AR9160 and later, 8K for
730  *   AR5416 when doing RTS frame protection.)
731  * + Maximum number of sub-frames for an aggregate
732  * + The aggregate delimiter size, giving MACs time to do whatever is
733  *   needed before each frame
734  * + Enforce the BAW limit
735  *
736  * Each descriptor queued should have the DMA setup.
737  * The rate series, descriptor setup, linking, etc is all done
738  * externally. This routine simply chains them together.
739  * ath_tx_setds_11n() will take care of configuring the per-
740  * descriptor setup, and ath_buf_set_rate() will configure the
741  * rate control.
742  *
743  * The TID lock is required for the entirety of this function.
744  *
745  * If some code in another thread adds to the head of this
746  * list, very strange behaviour will occur. Since retransmission is the
747  * only reason this will occur, and this routine is designed to be called
748  * from within the scheduler task, it won't ever clash with the completion
749  * task.
750  *
751  * So if you want to call this from an upper layer context (eg, to direct-
752  * dispatch aggregate frames to the hardware), please keep this in mind.
753  */
754 ATH_AGGR_STATUS
755 ath_tx_form_aggr(struct ath_softc *sc, struct ath_node *an,
756     struct ath_tid *tid, ath_bufhead *bf_q)
757 {
758 	//struct ieee80211_node *ni = &an->an_node;
759 	struct ath_buf *bf, *bf_first = NULL, *bf_prev = NULL;
760 	int nframes = 0;
761 	uint16_t aggr_limit = 0, al = 0, bpad = 0, al_delta, h_baw;
762 	struct ieee80211_tx_ampdu *tap;
763 	int status = ATH_AGGR_DONE;
764 	int prev_frames = 0;	/* XXX for AR5416 burst, not done here */
765 	int prev_al = 0;	/* XXX also for AR5416 burst */
766 
767 	ATH_TX_LOCK_ASSERT(sc);
768 
769 	tap = ath_tx_get_tx_tid(an, tid->tid);
770 	if (tap == NULL) {
771 		status = ATH_AGGR_ERROR;
772 		goto finish;
773 	}
774 
775 	h_baw = tap->txa_wnd / 2;
776 
777 	for (;;) {
778 		bf = ATH_TID_FIRST(tid);
779 		if (bf_first == NULL)
780 			bf_first = bf;
781 		if (bf == NULL) {
782 			status = ATH_AGGR_DONE;
783 			break;
784 		} else {
785 			/*
786 			 * It's the first frame;
787 			 * set the aggregation limit based on the
788 			 * rate control decision that has been made.
789 			 */
790 			aggr_limit = ath_get_aggr_limit(sc, bf_first);
791 		}
792 
793 		/* Set this early just so things don't get confused */
794 		bf->bf_next = NULL;
795 
796 		/*
797 		 * If the frame doesn't have a sequence number that we're
798 		 * tracking in the BAW (eg NULL QOS data frame), we can't
799 		 * aggregate it. Stop the aggregation process; the sender
800 		 * can then TX what's in the list thus far and then
801 		 * TX the frame individually.
802 		 */
803 		if (! bf->bf_state.bfs_dobaw) {
804 			status = ATH_AGGR_NONAGGR;
805 			break;
806 		}
807 
808 		/*
809 		 * If any of the rates are non-HT, this packet
810 		 * can't be aggregated.
811 		 * XXX TODO: add a bf_state flag which gets marked
812 		 * if any active rate is non-HT.
813 		 */
814 
815 		/*
816 		 * do not exceed aggregation limit
817 		 */
818 		al_delta = ATH_AGGR_DELIM_SZ + bf->bf_state.bfs_pktlen;
819 		if (nframes &&
820 		    (aggr_limit < (al + bpad + al_delta + prev_al))) {
821 			status = ATH_AGGR_LIMITED;
822 			break;
823 		}
824 
825 		/*
826 		 * If RTS/CTS is set on the first frame, enforce
827 		 * the RTS aggregate limit.
828 		 */
829 		if (bf_first->bf_state.bfs_txflags &
830 		    (HAL_TXDESC_CTSENA | HAL_TXDESC_RTSENA)) {
831 			if (nframes &&
832 			   (sc->sc_rts_aggr_limit <
833 			     (al + bpad + al_delta + prev_al))) {
834 				status = ATH_AGGR_8K_LIMITED;
835 				break;
836 			}
837 		}
838 
839 		/*
840 		 * Do not exceed subframe limit.
841 		 */
842 		if ((nframes + prev_frames) >= MIN((h_baw),
843 		    IEEE80211_AMPDU_SUBFRAME_DEFAULT)) {
844 			status = ATH_AGGR_LIMITED;
845 			break;
846 		}
847 
848 		/*
849 		 * If the current frame has an RTS/CTS configuration
850 		 * that differs from the first frame, override the
851 		 * subsequent frame with this config.
852 		 */
853 		if (bf != bf_first) {
854 			bf->bf_state.bfs_txflags &=
855 			    ~ (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA);
856 			bf->bf_state.bfs_txflags |=
857 			    bf_first->bf_state.bfs_txflags &
858 			    (HAL_TXDESC_RTSENA | HAL_TXDESC_CTSENA);
859 		}
860 
861 		/*
862 		 * If the packet has a sequence number, do not
863 		 * step outside of the block-ack window.
864 		 */
865 		if (! BAW_WITHIN(tap->txa_start, tap->txa_wnd,
866 		    SEQNO(bf->bf_state.bfs_seqno))) {
867 			status = ATH_AGGR_BAW_CLOSED;
868 			break;
869 		}
870 
871 		/*
872 		 * this packet is part of an aggregate.
873 		 */
874 		ATH_TID_REMOVE(tid, bf, bf_list);
875 
876 		/* The TID lock is required for the BAW update */
877 		ath_tx_addto_baw(sc, an, tid, bf);
878 		bf->bf_state.bfs_addedbaw = 1;
879 
880 		/*
881 		 * XXX enforce ACK for aggregate frames (this needs to be
882 		 * XXX handled more gracefully?
883 		 */
884 		if (bf->bf_state.bfs_txflags & HAL_TXDESC_NOACK) {
885 			device_printf(sc->sc_dev,
886 			    "%s: HAL_TXDESC_NOACK set for an aggregate frame?\n",
887 			    __func__);
888 			bf->bf_state.bfs_txflags &= (~HAL_TXDESC_NOACK);
889 		}
890 
891 		/*
892 		 * Add the now owned buffer (which isn't
893 		 * on the software TXQ any longer) to our
894 		 * aggregate frame list.
895 		 */
896 		TAILQ_INSERT_TAIL(bf_q, bf, bf_list);
897 		nframes ++;
898 
899 		/* Completion handler */
900 		bf->bf_comp = ath_tx_aggr_comp;
901 
902 		/*
903 		 * add padding for previous frame to aggregation length
904 		 */
905 		al += bpad + al_delta;
906 
907 		/*
908 		 * Calculate delimiters needed for the current frame
909 		 */
910 		bf->bf_state.bfs_ndelim =
911 		    ath_compute_num_delims(sc, bf_first,
912 		    bf->bf_state.bfs_pktlen);
913 
914 		/*
915 		 * Calculate the padding needed from this set of delimiters,
916 		 * used when calculating if the next frame will fit in
917 		 * the aggregate.
918 		 */
919 		bpad = PADBYTES(al_delta) + (bf->bf_state.bfs_ndelim << 2);
920 
921 		/*
922 		 * Chain the buffers together
923 		 */
924 		if (bf_prev)
925 			bf_prev->bf_next = bf;
926 		bf_prev = bf;
927 
928 		/*
929 		 * If we're leaking frames, just return at this point;
930 		 * we've queued a single frame and we don't want to add
931 		 * any more.
932 		 */
933 		if (tid->an->an_leak_count) {
934 			status = ATH_AGGR_LEAK_CLOSED;
935 			break;
936 		}
937 
938 #if 0
939 		/*
940 		 * terminate aggregation on a small packet boundary
941 		 */
942 		if (bf->bf_state.bfs_pktlen < ATH_AGGR_MINPLEN) {
943 			status = ATH_AGGR_SHORTPKT;
944 			break;
945 		}
946 #endif
947 
948 	}
949 
950 finish:
951 	/*
952 	 * Just in case the list was empty when we tried to
953 	 * dequeue a packet ..
954 	 */
955 	if (bf_first) {
956 		bf_first->bf_state.bfs_al = al;
957 		bf_first->bf_state.bfs_nframes = nframes;
958 	}
959 	return status;
960 }
961