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