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