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