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