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