788822c487edab5475e504481a7e47fcd9641e6c
[open-ath9k-htc-firmware.git] / target_firmware / wlan / ratectrl_11n_ln.c
1 /*
2  * Copyright (c) 2013 Qualcomm Atheros, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted (subject to the limitations in the
7  * disclaimer below) provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the
15  *    distribution.
16  *
17  *  * Neither the name of Qualcomm Atheros nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific prior written permission.
20  *
21  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
22  * GRANTED BY THIS LICENSE.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
23  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
33  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 #include <adf_os_types.h>
37 #include <adf_os_dma.h>
38 #include <adf_os_timer.h>
39 #include <adf_os_lock.h>
40 #include <adf_os_io.h>
41 #include <adf_os_mem.h>
42 #include <adf_os_module.h>
43 #include <adf_os_pci.h>
44 #include <adf_os_util.h>
45 #include <adf_os_stdtypes.h>
46 #include <adf_os_defer.h>
47 #include <adf_os_atomic.h>
48 #include <adf_nbuf.h>
49 #include <adf_net.h>
50 #include <adf_net_types.h>
51
52 #include <ieee80211_var.h>
53
54 #include <if_athvar.h>
55 #include "ah_desc.h"
56
57 #include "ratectrl.h"
58 #include "ratectrl11n.h"
59
60 INLINE A_RSSI median(A_RSSI a, A_RSSI b, A_RSSI c);
61
62 static void ath_rate_newassoc_11n(struct ath_softc_tgt *sc, struct ath_node_target *an, int isnew, 
63                                   unsigned int capflag, struct ieee80211_rate *rs);
64
65
66 static void ath_rate_tx_complete_11n(struct ath_softc_tgt *sc, struct ath_node_target *an, 
67                                      struct ath_tx_desc *ds,
68                                      struct ath_rc_series rcs[], int nframes, 
69                                      int nbad);
70
71 static void ath_rate_findrate_11n(struct ath_softc_tgt *sc,
72                                   struct ath_node_target *an,
73                                   size_t frameLen,
74                                   int numTries,
75                                   int numRates,
76                                   int stepDnInc,
77                                   unsigned int rcflag,
78                                   struct ath_rc_series series[],
79                                   int *isProbe);
80
81 static void
82 rcSortValidRates(const RATE_TABLE_11N *pRateTable, TX_RATE_CTRL *pRc)
83 {
84         A_UINT8 i,j;
85
86         for (i=pRc->maxValidRate-1; i > 0; i--) {
87                 for (j=0; j <= i-1; j++) {
88 #ifdef MAGPIE_MERLIN      
89                         if (pRateTable->info[pRc->validRateIndex[j]].rateKbps >
90                             pRateTable->info[pRc->validRateIndex[j+1]].rateKbps)
91 #else
92                                 // K2
93                                 if (pRateTable->info[pRc->validRateIndex[j]].userRateKbps >
94                                     pRateTable->info[pRc->validRateIndex[j+1]].userRateKbps)
95 #endif
96                                 {
97                                         A_UINT8 tmp=0;
98                                         tmp = pRc->validRateIndex[j];
99                                         pRc->validRateIndex[j] = pRc->validRateIndex[j+1];
100                                         pRc->validRateIndex[j+1] = tmp;
101                                 }
102                 }
103         }
104 }
105
106 /* Access functions for validTxRateMask */
107
108 static void
109 rcInitValidTxMask(TX_RATE_CTRL *pRc)
110 {
111         A_UINT8 i;
112
113         for (i = 0; i < pRc->rateTableSize; i++) {
114                 pRc->validRateIndex[i] = FALSE;
115         }
116 }
117
118 static INLINE void
119 rcSetValidTxMask(TX_RATE_CTRL *pRc, A_UINT8 index, A_BOOL validTxRate)
120 {
121         ASSERT(index < pRc->rateTableSize);
122         pRc->validRateIndex[index] = validTxRate ? TRUE : FALSE;
123
124 }
125
126 static INLINE A_BOOL
127 rcIsValidTxMask(TX_RATE_CTRL *pRc, A_UINT8 index)
128 {
129         ASSERT(index < pRc->rateTableSize);
130         return (pRc->validRateIndex[index]);
131 }
132
133 /* Iterators for validTxRateMask */
134 static INLINE A_BOOL
135 rcGetNextValidTxRate(const RATE_TABLE_11N *pRateTable, TX_RATE_CTRL *pRc, 
136                      A_UINT8 curValidTxRate, A_UINT8 *pNextIndex)
137 {
138         A_UINT8 i;
139
140         for (i = 0; i < pRc->maxValidRate-1; i++) {
141                 if (pRc->validRateIndex[i] == curValidTxRate) {
142                         *pNextIndex = pRc->validRateIndex[i+1];
143                         return TRUE;
144                 }
145         }
146
147         /* No more valid rates */
148         *pNextIndex = 0;
149     
150         return FALSE;
151 }
152
153 static INLINE A_BOOL
154 rcGetNextLowerValidTxRate(const RATE_TABLE_11N *pRateTable, TX_RATE_CTRL *pRc,  
155                           A_UINT8 curValidTxRate, A_UINT8 *pNextIndex)
156 {
157         A_INT8 i;
158
159         for (i = 1; i < pRc->maxValidRate ; i++) {
160                 if (pRc->validRateIndex[i] == curValidTxRate) {
161                         *pNextIndex = pRc->validRateIndex[i-1];
162                         return TRUE;
163                 }
164         }
165
166         return FALSE;
167 }
168
169 /* Return true only for single stream */
170
171 static A_BOOL
172 rcIsValidPhyRate(A_UINT32 phy, A_UINT32 capflag, A_BOOL ignoreCW)
173 {
174         if (WLAN_RC_PHY_HT(phy) && !(capflag & WLAN_RC_HT_FLAG)) {
175                 return FALSE;
176         }
177
178         if (WLAN_RC_PHY_DS(phy) && !(capflag & WLAN_RC_DS_FLAG))  {
179                 return FALSE;
180         }
181         if (WLAN_RC_PHY_SGI(phy) && !(capflag & WLAN_RC_HT40_SGI_FLAG)) {
182                 return FALSE;
183         }
184
185         if (!ignoreCW && WLAN_RC_PHY_HT(phy)) {
186                 if (WLAN_RC_PHY_40(phy) && !(capflag & WLAN_RC_40_FLAG)) {
187                         return FALSE;
188                 }
189
190                 if (!WLAN_RC_PHY_40(phy) && (capflag & WLAN_RC_40_FLAG)) {
191                         return FALSE;
192                 }
193         }
194     
195         return TRUE;
196 }
197
198 /* 
199  * Initialize the Valid Rate Index from valid entries in Rate Table 
200  */
201 static A_UINT8 rcSibInitValidRates(const RATE_TABLE_11N *pRateTable,
202                                    TX_RATE_CTRL *pRc,
203                                    A_UINT32 capflag,
204                                    PHY_STATE_CTRL *pPhyStateCtrl)
205 {
206         A_UINT8 i, hi = 0;
207         A_UINT8 singleStream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
208         A_UINT8 valid;
209     
210         for (i = 0; i < pRateTable->rateCount; i++) {
211                 if (singleStream) {
212                         valid = pRateTable->info[i].validSingleStream;
213                 } else {
214                         valid = pRateTable->info[i].valid;
215                 }
216             
217                 if (valid == TRUE) {
218                         A_UINT32 phy = pRateTable->info[i].phy;
219
220                         if (!rcIsValidPhyRate(phy, capflag, FALSE)) 
221                                 continue;
222
223                         pPhyStateCtrl->validPhyRateIndex[phy][pPhyStateCtrl->validPhyRateCount[phy]] = i;
224                         pPhyStateCtrl->validPhyRateCount[phy] += 1;
225
226                         rcSetValidTxMask(pRc, i, TRUE);
227
228                         hi = A_MAX(hi, i);
229                 }
230         } 
231     
232         return hi;
233 }
234
235 /* 
236  * Initialize the Valid Rate Index from Rate Set 
237  */
238 static A_UINT8
239 rcSibSetValidRates(const RATE_TABLE_11N *pRateTable,
240                    TX_RATE_CTRL *pRc, 
241                    struct ieee80211_rateset *pRateSet,
242                    A_UINT32 capflag,
243                    struct ath_node_target *an,
244                    PHY_STATE_CTRL *pPhyStateCtrl)
245 {
246         A_UINT8 i, j, hi = 0;
247         A_UINT8 singleStream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
248         A_UINT32 valid;
249        
250         /* Use intersection of working rates and valid rates */
251         for (i = 0; i < pRateSet->rs_nrates; i++) {
252                 for (j = 0; j < pRateTable->rateCount; j++) {
253                         A_UINT32 phy = pRateTable->info[j].phy;
254 #ifdef MAGPIE_MERLIN
255                         struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
256
257                         if (pSib->stbc) {
258                                 valid = pRateTable->info[j].validSTBC;
259                         } else if (singleStream) {
260 #else
261                         if (singleStream) {
262 #endif            
263                                 valid = pRateTable->info[j].validSingleStream;
264                         } else {
265                                 valid = pRateTable->info[j].valid;
266                         }
267         
268                         /*
269                          * We allow a rate only if its valid and the capflag matches one of
270                          * the validity (TRUE/TRUE_20/TRUE_40) flags
271                          */
272
273                         if (((pRateSet->rs_rates[i] & 0x7F) == 
274                              (pRateTable->info[j].dot11Rate & 0x7F))
275                             && ((valid & WLAN_RC_CAP_MODE(capflag)) == 
276                                 WLAN_RC_CAP_MODE(capflag)) && !WLAN_RC_PHY_HT(phy)) {
277                                 if (!rcIsValidPhyRate(phy, capflag, FALSE)) 
278                                         continue;
279
280                                 pPhyStateCtrl->validPhyRateIndex[phy][pPhyStateCtrl->validPhyRateCount[phy]] = j;
281                                 pPhyStateCtrl->validPhyRateCount[phy] += 1;
282
283                                 rcSetValidTxMask(pRc, j, TRUE);
284                                 hi = A_MAX(hi, j);
285                         }
286                 }
287         }
288   
289         return hi;
290 }
291
292 static A_UINT8
293 rcSibSetValidHtRates(const RATE_TABLE_11N *pRateTable,
294                      TX_RATE_CTRL *pRc, 
295                      A_UINT8 *pMcsSet,
296                      A_UINT32 capflag,
297                      struct ath_node_target *an,
298                      PHY_STATE_CTRL *pPhyStateCtrl)
299 {
300         A_UINT8 i, j, hi = 0;
301         A_UINT8 singleStream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
302         A_UINT8 valid;
303     
304         /* Use intersection of working rates and valid rates */
305         for (i = 0; i <  ((struct ieee80211_rateset *)pMcsSet)->rs_nrates; i++) {
306                 for (j = 0; j < pRateTable->rateCount; j++) {
307                         A_UINT32 phy = pRateTable->info[j].phy;
308 #ifdef MAGPIE_MERLIN
309                         struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
310
311                         if (pSib->stbc) {
312                                 valid = pRateTable->info[j].validSTBC;
313                         } else if (singleStream) {
314 #else
315                         if (singleStream) {
316 #endif
317                                 valid = pRateTable->info[j].validSingleStream;
318                         } else {
319                                 valid = pRateTable->info[j].valid;
320                         }
321                            
322                         if (((((struct ieee80211_rateset *)pMcsSet)->rs_rates[i] & 0x7F) 
323                              != (pRateTable->info[j].dot11Rate & 0x7F)) 
324                             || !WLAN_RC_PHY_HT(phy) 
325                             || !WLAN_RC_PHY_HT_VALID(valid, capflag)
326                             || ((pRateTable->info[j].dot11Rate == 15) && 
327                                 (valid & TRUE_20) && 
328                                 (capflag & WLAN_RC_WEP_TKIP_FLAG)) )
329                         {
330                                 continue;
331                         }
332     
333                         if (!rcIsValidPhyRate(phy, capflag, FALSE)) 
334                                 continue;
335     
336                         pPhyStateCtrl->validPhyRateIndex[phy][pPhyStateCtrl->validPhyRateCount[phy]] = j;
337                         pPhyStateCtrl->validPhyRateCount[phy] += 1;
338
339                         rcSetValidTxMask(pRc, j, TRUE);
340                         hi = A_MAX(hi, j);
341                 }
342         }
343
344         return hi;
345 }
346
347 /*
348  *  Update the SIB's rate control information
349  *
350  *  This should be called when the supported rates change
351  *  (e.g. SME operation, wireless mode change)
352  *
353  *  It will determine which rates are valid for use.
354  */
355 static void
356 rcSibUpdate_ht(struct ath_softc_tgt *sc, struct ath_node_target *an,
357                A_UINT32 capflag, A_BOOL keepState, struct ieee80211_rate  *pRateSet)
358 {
359         RATE_TABLE_11N *pRateTable = 0;
360         struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
361         struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
362         A_UINT8 *phtMcs = (A_UINT8*)&pRateSet->htrates;
363         TX_RATE_CTRL *pRc = (TX_RATE_CTRL *)(pSib);
364         PHY_STATE_CTRL mPhyCtrlState;  
365
366         A_UINT8 i, j, k, hi = 0, htHi = 0;
367
368         pRateTable = (RATE_TABLE_11N*)asc->hwRateTable[sc->sc_curmode];
369
370         /* Initial rate table size. Will change depending on the working rate set */
371         pRc->rateTableSize = MAX_TX_RATE_TBL;
372
373         /* Initialize thresholds according to the global rate table */
374         for (i = 0 ; (i < pRc->rateTableSize) && (!keepState); i++) {
375                 pRc->state[i].per       = 0;
376         }
377
378         /* Determine the valid rates */
379         rcInitValidTxMask(pRc);
380
381         for (i = 0; i < WLAN_RC_PHY_MAX; i++) {
382                 for (j = 0; j < MAX_TX_RATE_PHY; j++) {
383                         mPhyCtrlState.validPhyRateIndex[i][j] = 0;
384                 }   
385                 mPhyCtrlState.validPhyRateCount[i] = 0;
386         }
387
388         pRc->rcPhyMode = (capflag & WLAN_RC_40_FLAG);
389
390         if (pRateSet == NULL || !pRateSet->rates.rs_nrates) {
391                 /* No working rate, just initialize valid rates */
392                 hi = rcSibInitValidRates(pRateTable, pRc, capflag, &mPhyCtrlState);
393         } else {
394                 /* Use intersection of working rates and valid rates */
395                 hi = rcSibSetValidRates(pRateTable, pRc, &(pRateSet->rates),
396                                         capflag, an, &mPhyCtrlState);
397
398                 if (capflag & WLAN_RC_HT_FLAG) {
399                         htHi = rcSibSetValidHtRates(pRateTable, pRc, phtMcs,
400                                                     capflag, an, &mPhyCtrlState);
401                 }
402
403                 hi = A_MAX(hi, htHi);
404         }
405
406         pRc->rateTableSize = hi + 1;
407         pRc->rateMaxPhy    = 0;
408     
409         ASSERT(pRc->rateTableSize <= MAX_TX_RATE_TBL);
410
411         for (i = 0, k = 0; i < WLAN_RC_PHY_MAX; i++) {
412                 for (j = 0; j < mPhyCtrlState.validPhyRateCount[i]; j++) {
413                         pRc->validRateIndex[k++] = mPhyCtrlState.validPhyRateIndex[i][j];
414                 }   
415
416                 if (!rcIsValidPhyRate(i, pRateTable->initialRateMax, TRUE) ||
417                     !mPhyCtrlState.validPhyRateCount[i]) 
418                         continue;
419
420                 pRc->rateMaxPhy = mPhyCtrlState.validPhyRateIndex[i][j-1];      
421         }
422     
423         ASSERT(pRc->rateTableSize <= MAX_TX_RATE_TBL);
424         ASSERT(k <= MAX_TX_RATE_TBL);
425
426         pRc->rateMaxPhy = pRc->validRateIndex[k-4];
427         pRc->maxValidRate = k;
428
429         rcSortValidRates(pRateTable, pRc);
430 }
431
432 void 
433 rcSibUpdate_11n(struct ath_softc_tgt *sc, struct ath_node_target *pSib, 
434                 A_UINT32 capflag, A_BOOL keepState, struct ieee80211_rate  *pRateSet)
435 {
436         rcSibUpdate_ht(sc, 
437                        pSib, 
438                        ((capflag & ATH_RC_DS_FLAG)   ? WLAN_RC_DS_FLAG  : 0) |
439                        ((capflag & ATH_RC_HT40_SGI_FLAG)  ? WLAN_RC_HT40_SGI_FLAG : 0) | 
440                        ((capflag & ATH_RC_HT_FLAG)   ? WLAN_RC_HT_FLAG  : 0) |
441                        ((capflag & ATH_RC_CW40_FLAG) ? WLAN_RC_40_FLAG  : 0),
442                        keepState,
443                        pRateSet);
444 }
445
446 /*
447  * Return the median of three numbers
448  */
449 INLINE A_RSSI median(A_RSSI a, A_RSSI b, A_RSSI c)
450 {
451         if (a >= b) {
452                 if (b >= c) {
453                         return b;
454                 } else if (a > c) {
455                         return c;
456                 } else {
457                         return a;
458                 }
459         } else {
460                 if (a >= c) {
461                         return a;
462                 } else if (b >= c) {
463                         return c;
464                 } else {
465                         return b;
466                 }
467         }
468 }
469
470 static A_UINT8
471 rcRateFind_ht(struct ath_softc_tgt *sc, struct atheros_node *pSib,
472               const RATE_TABLE_11N *pRateTable, A_BOOL probeAllowed, A_BOOL *isProbing)
473 {
474         A_UINT32             dt;
475         A_UINT32             bestThruput, thisThruput;
476         A_UINT32             nowMsec;
477         A_UINT8              rate, nextRate, bestRate;
478         A_UINT8              maxIndex, minIndex;
479         A_INT8               index;
480         TX_RATE_CTRL         *pRc = NULL;
481
482         pRc = (TX_RATE_CTRL *)(pSib ? (pSib) : NULL);
483
484         *isProbing = FALSE;
485
486         /*
487          * Age (reduce) last ack rssi based on how old it is.
488          * The bizarre numbers are so the delta is 160msec,
489          * meaning we divide by 16.
490          *   0msec   <= dt <= 25msec:   don't derate
491          *   25msec  <= dt <= 185msec:  derate linearly from 0 to 10dB
492          *   185msec <= dt:             derate by 10dB
493          */
494
495         nowMsec = A_MS_TICKGET();
496         dt = nowMsec - pRc->rssiTime;
497
498         /*
499          * Now look up the rate in the rssi table and return it.
500          * If no rates match then we return 0 (lowest rate)
501          */
502
503         bestThruput = 0;
504         maxIndex = pRc->maxValidRate-1;
505
506         minIndex = 0;
507         bestRate = minIndex;
508     
509         /*
510          * Try the higher rate first. It will reduce memory moving time
511          * if we have very good channel characteristics.
512          */
513         for (index = maxIndex; index >= minIndex ; index--) {
514                 A_UINT8 perThres;
515     
516                 rate = pRc->validRateIndex[index];
517                 if (rate > pRc->rateMaxPhy) {
518                         continue;
519                 }
520
521                 /* if the best throughput is already larger than the userRateKbps..
522                  * then we could skip of rest of calculation.. 
523                  */
524                 if( bestThruput >= pRateTable->info[rate].userRateKbps)
525                         break;
526
527                 /*
528                  * For TCP the average collision rate is around 11%,
529                  * so we ignore PERs less than this.  This is to
530                  * prevent the rate we are currently using (whose
531                  * PER might be in the 10-15 range because of TCP
532                  * collisions) looking worse than the next lower
533                  * rate whose PER has decayed close to 0.  If we
534                  * used to next lower rate, its PER would grow to
535                  * 10-15 and we would be worse off then staying
536                  * at the current rate.
537                  */
538                 perThres = pRc->state[rate].per;
539                 if ( perThres < 12 ) {
540                         perThres = 12;
541                 }
542
543                 thisThruput = pRateTable->info[rate].userRateKbps * (100 - perThres);
544                 if (bestThruput <= thisThruput) {
545                         bestThruput = thisThruput;
546                         bestRate    = rate;
547                 }
548         }
549
550         rate = bestRate;
551
552         /*
553          * Must check the actual rate (rateKbps) to account for non-monoticity of
554          * 11g's rate table
555          */
556
557         if (rate >= pRc->rateMaxPhy && probeAllowed) {
558                 rate = pRc->rateMaxPhy;
559
560                 /* Probe the next allowed phy state */
561                 /* FIXME: Check to make sure ratMax is checked properly */
562                 if (rcGetNextValidTxRate( pRateTable, pRc, rate, &nextRate) && 
563                     (nowMsec - pRc->probeTime > pRateTable->probeInterval) &&
564                     (pRc->hwMaxRetryPktCnt >= 1))
565                 {
566                         rate                  = nextRate;
567                         pRc->probeRate        = rate;
568                         pRc->probeTime        = nowMsec;
569                         pRc->hwMaxRetryPktCnt = 0;
570                         *isProbing            = TRUE;
571
572                 }
573         }
574
575         /*
576          * Make sure rate is not higher than the allowed maximum.
577          * We should also enforce the min, but I suspect the min is
578          * normally 1 rather than 0 because of the rate 9 vs 6 issue
579          * in the old code.
580          */
581         if (rate > (pRc->rateTableSize - 1)) {
582                 rate = pRc->rateTableSize - 1;
583         }
584
585         /* record selected rate, which is used to decide if we want to do fast frame */
586         if (!(*isProbing) && pSib) {
587                 pSib->lastRateKbps = pRateTable->info[rate].rateKbps;
588                 ((struct atheros_softc*)sc->sc_rc)->currentTxRateKbps = pSib->lastRateKbps;
589                 ((struct atheros_softc*)sc->sc_rc)->currentTxRateIndex = rate;
590         }
591
592         return rate;
593 }
594
595 static void
596 rcRateSetseries(const RATE_TABLE_11N *pRateTable ,
597                 struct ath_rc_series *series,
598                 A_UINT8 tries, A_UINT8 rix,
599                 A_BOOL rtsctsenable, A_UINT32 chainmask,int stbc)
600 {
601         series->tries = tries;
602         series->flags = (rtsctsenable? ATH_RC_RTSCTS_FLAG : 0) | 
603                 (WLAN_RC_PHY_DS(pRateTable->info[rix].phy) ? ATH_RC_DS_FLAG : 0) | 
604                 (WLAN_RC_PHY_40(pRateTable->info[rix].phy) ? ATH_RC_CW40_FLAG : 0) | 
605                 (WLAN_RC_PHY_SGI(pRateTable->info[rix].phy) ? ATH_RC_HT40_SGI_FLAG : 0);
606 #ifdef MAGPIE_MERLIN
607         if (stbc) {
608                 /* For now, only single stream STBC is supported */
609                 if (pRateTable->info[rix].rateCode >= 0x80 && 
610                     pRateTable->info[rix].rateCode <= 0x87)
611                 {
612                         series->flags |= ATH_RC_TX_STBC_FLAG;
613                 }
614         }
615 #endif
616         series->rix = pRateTable->info[rix].baseIndex;
617         series->max4msframelen = pRateTable->info[rix].max4msframelen;
618         series->txrateKbps = pRateTable->info[rix].rateKbps;
619
620         /* If the hardware is capable of multiple transmit chains (chainmask is 3, 5 or 7), 
621          * then choose the number of transmit chains dynamically based on entries in the rate table.
622          */
623 #ifndef ATH_ENABLE_WLAN_FOR_K2
624         if(chainmask == 7)
625                 series->tx_chainmask = pRateTable->info[rix].txChainMask_3ch;
626         else if(chainmask == 1) 
627                 series->tx_chainmask = 1;
628         else 
629                 series->tx_chainmask = pRateTable->info[rix].txChainMask_2ch;  /*Chainmask is 3 or 5*/
630 #else
631         series->tx_chainmask = 1;
632 #endif
633 }
634
635 static A_UINT8 
636 rcRateGetIndex(struct ath_softc_tgt *sc, struct ath_node_target *an,        
637                const RATE_TABLE_11N *pRateTable , 
638                A_UINT8 rix, A_UINT16 stepDown, A_UINT16 minRate)
639 {
640         A_UINT32                j;
641         A_UINT8                 nextIndex;
642         struct atheros_node     *pSib = ATH_NODE_ATHEROS(an);
643         TX_RATE_CTRL            *pRc = (TX_RATE_CTRL *)(pSib);
644     
645         if (minRate) {
646                 for (j = RATE_TABLE_11N_SIZE; j > 0; j-- ) {
647                         if (rcGetNextLowerValidTxRate(pRateTable, pRc, rix, &nextIndex)) {
648                                 rix = nextIndex;
649                         } else {
650                                 break;
651                         }
652                 }
653         } else {
654                 for (j = stepDown; j > 0; j-- ) {
655                         if (rcGetNextLowerValidTxRate(pRateTable, pRc, rix, &nextIndex)) {
656                                 rix = nextIndex;
657                         } else {
658                                 break;
659                         }
660                 }
661         }
662
663         return rix;
664 }
665
666 void rcRateFind_11n(struct ath_softc_tgt *sc, struct ath_node_target *an, 
667                     int numTries, int numRates, int stepDnInc,
668                     unsigned int rcflag, struct ath_rc_series series[], int *isProbe)
669 {
670         A_UINT8 i = 0; 
671         A_UINT8 tryPerRate  = 0;
672         struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
673         RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
674         struct atheros_node *asn = ATH_NODE_ATHEROS(an);
675         A_UINT8 rix, nrix;
676         A_UINT8 dot11Rate;
677         WLAN_PHY phy;
678
679         rix = rcRateFind_ht(sc, asn, pRateTable, (rcflag & ATH_RC_PROBE_ALLOWED) ? 1 : 0, 
680                             isProbe);
681         nrix = rix;
682
683         if ((rcflag & ATH_RC_PROBE_ALLOWED) && (*isProbe)) {
684                 /* set one try for probe rates. For the probes don't enable rts */
685                 rcRateSetseries(pRateTable, &series[i++], 1, nrix,
686                                 FALSE, asc->tx_chainmask, asn->stbc);
687           
688                 /*
689                  * Get the next tried/allowed rate. No RTS for the next series
690                  * after the probe rate
691                  */
692                 nrix = rcRateGetIndex( sc, an, pRateTable, nrix, 1, FALSE);
693         }
694
695         tryPerRate = (numTries/numRates);
696
697         /* Set the choosen rate. No RTS for first series entry. */
698         rcRateSetseries(pRateTable, &series[i++], tryPerRate,
699                         nrix, FALSE, asc->tx_chainmask, asn->stbc);
700
701         /* Fill in the other rates for multirate retry */
702         for (; i < numRates; i++) {
703                 A_UINT8 tryNum;
704                 A_UINT8 minRate;
705
706                 tryNum  = ((i + 1) == numRates) ? numTries - (tryPerRate * i) : tryPerRate ;
707                 minRate = (((i + 1) == numRates) && (rcflag & ATH_RC_MINRATE_LASTRATE)) ? 1 : 0;
708
709                 nrix = rcRateGetIndex(sc, an, pRateTable, nrix, stepDnInc, minRate);
710
711                 /* All other rates in the series have RTS enabled */
712                 rcRateSetseries(pRateTable, &series[i], tryNum,
713                                 nrix, TRUE, asc->tx_chainmask, asn->stbc);
714         }
715
716         /*
717          * BUG 26545:
718          * Change rate series to enable aggregation when operating at lower MCS rates. 
719          * When first rate in series is MCS2 in HT40 @ 2.4GHz, series should look like:
720          *    {MCS2, MCS1, MCS0, MCS0}.
721          * When first rate in series is MCS3 in HT20 @ 2.4GHz, series should look like:
722          *    {MCS3, MCS2, MCS1, MCS1}
723          * So, set fourth rate in series to be same as third one for above conditions.
724          */
725         if (sc->sc_curmode == IEEE80211_MODE_11NG) {
726                 dot11Rate = pRateTable->info[rix].dot11Rate;
727                 phy = pRateTable->info[rix].phy;
728                 if (i == 4 &&
729                     ((dot11Rate == 2 && phy == WLAN_RC_PHY_HT_40_SS) || 
730                      (dot11Rate == 3 && phy == WLAN_RC_PHY_HT_20_SS))) 
731                 {
732                         series[3].rix = series[2].rix;
733                         series[3].flags = series[2].flags;
734                         series[3].max4msframelen = series[2].max4msframelen;
735                 }
736         }
737
738         /*
739          * 2009/02/06
740          * AP91 Kite: NetGear OTA location-4 downlink.
741          *            Enable RTS/CTS at MCS 3-0 for downlink throughput.
742          */
743         if (sc->sc_curmode == IEEE80211_MODE_11NG) {
744                 dot11Rate = pRateTable->info[rix].dot11Rate;
745                 if (dot11Rate <= 3 ) {
746                         series[0].flags |= ATH_RC_RTSCTS_FLAG;         
747                 }
748         }
749 }
750
751 static void
752 rcUpdate_ht(struct ath_softc_tgt *sc, struct ath_node_target *an, int txRate, 
753             A_BOOL Xretries, int retries, A_UINT8 curTxAnt, 
754             A_UINT16 nFrames, A_UINT16 nBad)
755 {
756         TX_RATE_CTRL *pRc;
757         A_UINT32 nowMsec = A_MS_TICKGET();
758         A_UINT8 lastPer;
759         int rate,count;
760         struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
761         struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
762         RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
763
764         static A_UINT32 nRetry2PerLookup[10] = {
765                 100 * 0 / 1,    // 0
766                 100 * 1 / 4,    // 25
767                 100 * 1 / 2,    // 50
768                 100 * 3 / 4,    // 75
769                 100 * 4 / 5,    // 80
770                 100 * 5 / 6,    // 83.3
771                 100 * 6 / 7,    // 85.7
772                 100 * 7 / 8,    // 87.5
773                 100 * 8 / 9,    // 88.8
774                 100 * 9 / 10    // 90
775         };
776
777         if (!pSib)
778                 return;
779
780         pRc = (TX_RATE_CTRL *)(pSib);
781
782         ASSERT(retries >= 0 && retries < MAX_TX_RETRIES);
783         ASSERT(txRate >= 0);
784     
785         if (txRate < 0) {
786                 return;
787         }
788
789         lastPer = pRc->state[txRate].per;
790
791         if (Xretries) {
792                 /* Update the PER. */
793                 if (Xretries == 1) {
794                         pRc->state[txRate].per += 30;
795                         if (pRc->state[txRate].per > 100) {
796                                 pRc->state[txRate].per = 100;
797                         }
798                 } else {
799                         /* Xretries == 2 */
800
801                         count = sizeof(nRetry2PerLookup) / sizeof(nRetry2PerLookup[0]);
802                         if (retries >= count) {
803                                 retries = count - 1;
804                         }
805
806                         /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
807                         pRc->state[txRate].per = (A_UINT8)(pRc->state[txRate].per - 
808                                                    (pRc->state[txRate].per / 8) + ((100) / 8));
809                 }
810
811                 /* Xretries == 1 or 2 */
812
813                 if (pRc->probeRate == txRate)
814                         pRc->probeRate = 0;
815         } else {
816                 /* Xretries == 0 */
817
818                 /*
819                  * Update the PER.  Make sure it doesn't index out of array's bounds.
820                  */
821                 count = sizeof(nRetry2PerLookup) / sizeof(nRetry2PerLookup[0]);
822                 if (retries >= count) {
823                         retries = count - 1;
824                 }
825
826                 if (nBad) {
827                         /* new_PER = 7/8*old_PER + 1/8*(currentPER)  */
828                         /*
829                          * Assuming that nFrames is not 0.  The current PER
830                          * from the retries is 100 * retries / (retries+1),
831                          * since the first retries attempts failed, and the
832                          * next one worked.  For the one that worked, nBad
833                          * subframes out of nFrames wored, so the PER for
834                          * that part is 100 * nBad / nFrames, and it contributes
835                          * 100 * nBad / (nFrames * (retries+1)) to the above
836                          * PER.  The expression below is a simplified version
837                          * of the sum of these two terms.
838                          */
839                         if (nFrames > 0)
840                                 pRc->state[txRate].per = (A_UINT8)(pRc->state[txRate].per - 
841                                            (pRc->state[txRate].per / 8) + 
842                                            ((100*(retries*nFrames + nBad)/(nFrames*(retries+1))) / 8));
843                 } else {
844                         /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
845
846                         pRc->state[txRate].per = (A_UINT8)(pRc->state[txRate].per - 
847                                    (pRc->state[txRate].per / 8) + (nRetry2PerLookup[retries] / 8));
848                 }
849
850                 /*
851                  * If we got at most one retry then increase the max rate if
852                  * this was a probe.  Otherwise, ignore the probe.
853                  */
854
855                 if (pRc->probeRate && pRc->probeRate == txRate) {
856                         if (retries > 0 || 2 * nBad > nFrames) {
857                                 /*
858                                  * Since we probed with just a single attempt,
859                                  * any retries means the probe failed.  Also,
860                                  * if the attempt worked, but more than half
861                                  * the subframes were bad then also consider
862                                  * the probe a failure.
863                                  */
864                                 pRc->probeRate = 0;
865                         } else {
866                                 pRc->rateMaxPhy = pRc->probeRate;
867
868                                 if (pRc->state[pRc->probeRate].per > 30) {
869                                         pRc->state[pRc->probeRate].per = 20;
870                                 }
871
872                                 pRc->probeRate = 0;
873
874                                 /*
875                                  * Since this probe succeeded, we allow the next probe
876                                  * twice as soon.  This allows the maxRate to move up
877                                  * faster if the probes are succesful.
878                                  */
879                                 pRc->probeTime = nowMsec - pRateTable->probeInterval / 2;
880                         }
881                 }
882
883                 if (retries > 0) {
884                         /*
885                          * Don't update anything.  We don't know if this was because
886                          * of collisions or poor signal.
887                          *
888                          * Later: if rssiAck is close to pRc->state[txRate].rssiThres
889                          * and we see lots of retries, then we could increase
890                          * pRc->state[txRate].rssiThres.
891                          */
892                         pRc->hwMaxRetryPktCnt = 0;
893                 } else {
894                         /*
895                          * It worked with no retries.  First ignore bogus (small)
896                          * rssiAck values.
897                          */
898                         if (txRate == pRc->rateMaxPhy && pRc->hwMaxRetryPktCnt < 255) {
899                                 pRc->hwMaxRetryPktCnt++;
900                         }
901
902                 }
903         }
904
905         /* For all cases */
906
907         ASSERT((pRc->rateMaxPhy >= 0 && pRc->rateMaxPhy <= pRc->rateTableSize && 
908                 pRc->rateMaxPhy != INVALID_RATE_MAX));
909     
910         /*
911          * If this rate looks bad (high PER) then stop using it for
912          * a while (except if we are probing).
913          */
914         if (pRc->state[txRate].per >= 55 && txRate > 0 &&
915             pRateTable->info[txRate].rateKbps <= 
916             pRateTable->info[pRc->rateMaxPhy].rateKbps)
917         {
918                 rcGetNextLowerValidTxRate(pRateTable, pRc, (A_UINT8) txRate, 
919                                           &pRc->rateMaxPhy);
920
921                 /* Don't probe for a little while. */
922                 pRc->probeTime = nowMsec;
923         }
924
925         /* Make sure the rates below this have lower PER */
926         /* Monotonicity is kept only for rates below the current rate. */
927         if (pRc->state[txRate].per < lastPer) {
928                 for (rate = txRate - 1; rate >= 0; rate--) {
929                         if (pRateTable->info[rate].phy != pRateTable->info[txRate].phy) {
930                                 break;
931                         }
932
933                         if (pRc->state[rate].per > pRc->state[rate+1].per) {
934                                 pRc->state[rate].per = pRc->state[rate+1].per;
935                         }
936                 }
937         }
938
939         /* Maintain monotonicity for rates above the current rate*/
940         for (rate = txRate; rate < pRc->rateTableSize - 1; rate++) {
941                 if (pRc->state[rate+1].per < pRc->state[rate].per) {
942                         pRc->state[rate+1].per = pRc->state[rate].per;
943                 }
944         }
945
946         /* Every so often, we reduce the thresholds and PER (different for CCK and OFDM). */
947         if (nowMsec - pRc->perDownTime >= pRateTable->rssiReduceInterval) {
948                 for (rate = 0; rate < pRc->rateTableSize; rate++) {
949                         pRc->state[rate].per = 7*pRc->state[rate].per/8;
950                 }
951
952                 pRc->perDownTime = nowMsec;
953         }
954 }
955
956 /*
957  * This routine is called by the Tx interrupt service routine to give
958  * the status of previous frames.
959  */
960 void rcUpdate_11n(struct ath_softc_tgt *sc, struct ath_node_target *an,
961                   A_UINT8 curTxAnt, 
962                   int finalTSIdx, int Xretries,
963                   struct ath_rc_series rcs[], int nFrames, 
964                   int nBad, int long_retry)
965 {
966         A_UINT32 series = 0;
967         A_UINT32 rix;
968         struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
969         RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
970         struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
971         TX_RATE_CTRL *pRc = (TX_RATE_CTRL *)(pSib);
972         A_UINT8 flags;
973
974         if (!an) {
975                 adf_os_assert(0);
976                 return;
977         }
978
979         ASSERT (rcs[0].tries != 0);
980
981         /*
982          * If the first rate is not the final index, there are intermediate rate failures
983          * to be processed.
984          */
985         if (finalTSIdx != 0) {
986
987                 /* Process intermediate rates that failed.*/
988                 for (series = 0; series < finalTSIdx ; series++) {
989                         if (rcs[series].tries != 0) {
990                                 flags = rcs[series].flags;
991                                 /* If HT40 and we have switched mode from 40 to 20 => don't update */
992                                 if ((flags & ATH_RC_CW40_FLAG) && 
993                                     (pRc->rcPhyMode != (flags & ATH_RC_CW40_FLAG))) {
994                                         return;
995                                 }
996                                 if ((flags & ATH_RC_CW40_FLAG) && (flags & ATH_RC_HT40_SGI_FLAG)) {
997                                         rix = pRateTable->info[rcs[series].rix].htIndex;
998                                 } else if (flags & ATH_RC_HT40_SGI_FLAG) {
999                                         rix = pRateTable->info[rcs[series].rix].sgiIndex;
1000                                 } else if (flags & ATH_RC_CW40_FLAG) {
1001                                         rix = pRateTable->info[rcs[series].rix].cw40Index;
1002                                 } else {
1003                                         rix = pRateTable->info[rcs[series].rix].baseIndex;
1004                                 }
1005
1006                                 /* FIXME:XXXX, too many args! */
1007                                 rcUpdate_ht(sc, an, rix, Xretries? 1 : 2, rcs[series].tries, 
1008                                             curTxAnt, nFrames, nFrames);
1009                         }
1010                 }
1011         } else {
1012                 /*
1013                  * Handle the special case of MIMO PS burst, where the second aggregate is sent
1014                  *  out with only one rate and one try. Treating it as an excessive retry penalizes
1015                  * the rate inordinately.
1016                  */
1017                 if (rcs[0].tries == 1 && Xretries == 1) {
1018                         Xretries = 2;
1019                 }
1020         }
1021
1022         flags = rcs[series].flags;
1023         /* If HT40 and we have switched mode from 40 to 20 => don't update */
1024         if ((flags & ATH_RC_CW40_FLAG) && 
1025             (pRc->rcPhyMode != (flags & ATH_RC_CW40_FLAG))) {
1026                 return;
1027         }
1028         if ((flags & ATH_RC_CW40_FLAG) && (flags & ATH_RC_HT40_SGI_FLAG)) {
1029                 rix = pRateTable->info[rcs[series].rix].htIndex;
1030         } else if (flags & ATH_RC_HT40_SGI_FLAG) {
1031                 rix = pRateTable->info[rcs[series].rix].sgiIndex;
1032         } else if (flags & ATH_RC_CW40_FLAG) {
1033                 rix = pRateTable->info[rcs[series].rix].cw40Index;
1034         } else {
1035                 rix = pRateTable->info[rcs[series].rix].baseIndex;
1036         }
1037
1038         /* FIXME:XXXX, too many args! */
1039         rcUpdate_ht(sc, an, rix, Xretries, long_retry, curTxAnt, 
1040                     nFrames, nBad);
1041 }
1042
1043 void ath_tx_status_update_rate(struct ath_softc_tgt *sc,
1044                                struct ath_rc_series rcs[],
1045                                int series,
1046                                WMI_TXSTATUS_EVENT *txs)
1047 {
1048         struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
1049         RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
1050
1051         /* HT Rate */
1052         if (pRateTable->info[rcs[series].rix].rateCode & 0x80) {
1053                 txs->txstatus[txs->cnt].ts_rate |= SM(pRateTable->info[rcs[series].rix].dot11Rate,
1054                                                                        ATH9K_HTC_TXSTAT_RATE);
1055                 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_MCS;
1056
1057                 if (rcs[series].flags & ATH_RC_CW40_FLAG)
1058                         txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_CW40;
1059
1060                 if (rcs[series].flags & ATH_RC_HT40_SGI_FLAG)
1061                         txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_SGI;
1062
1063         } else {
1064                 txs->txstatus[txs->cnt].ts_rate |= SM(rcs[series].rix, ATH9K_HTC_TXSTAT_RATE);
1065         }
1066
1067         if (rcs[series].flags & ATH_RC_RTSCTS_FLAG)
1068                 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_RTC_CTS;
1069
1070 }
1071
1072 struct ath_ratectrl *
1073 ath_rate_attach(struct ath_softc_tgt *sc)
1074 {
1075         struct atheros_softc *asc;
1076
1077         asc = adf_os_mem_alloc(sizeof(struct atheros_softc));
1078         if (asc == NULL)
1079                 return NULL;
1080
1081         adf_os_mem_set(asc, 0, sizeof(struct atheros_softc));
1082         asc->arc.arc_space = sizeof(struct atheros_node);
1083
1084         ar5416AttachRateTables(asc);
1085
1086         asc->tx_chainmask = 1;
1087     
1088         return &asc->arc;
1089 }
1090
1091 void
1092 ath_rate_detach(struct ath_ratectrl *rc)
1093 {
1094         adf_os_mem_free(rc);
1095 }
1096
1097 void
1098 ath_rate_findrate(struct ath_softc_tgt *sc,
1099                   struct ath_node_target *an,
1100                   int shortPreamble,
1101                   size_t frameLen,
1102                   int numTries,
1103                   int numRates,
1104                   int stepDnInc,
1105                   unsigned int rcflag,
1106                   struct ath_rc_series series[],
1107                   int *isProbe)
1108 {
1109         *isProbe = 0;
1110
1111         if (!numRates || !numTries) {
1112                 return;
1113         }
1114
1115         ath_rate_findrate_11n(sc, an, frameLen, numTries, numRates, stepDnInc,
1116                               rcflag, series, isProbe);
1117 }
1118
1119 #define MS(_v, _f)  (((_v) & _f) >> _f##_S)
1120
1121 void
1122 ath_rate_tx_complete(struct ath_softc_tgt *sc,
1123                      struct ath_node_target *an,
1124                      struct ath_tx_desc *ds,
1125                      struct ath_rc_series rcs[], 
1126                      int nframes, int nbad)
1127 {
1128         ath_rate_tx_complete_11n(sc, an, ds, rcs, nframes, nbad);
1129 }
1130
1131 void
1132 ath_rate_newassoc(struct ath_softc_tgt *sc, struct ath_node_target *an, int isnew, 
1133                   unsigned int capflag, struct ieee80211_rate *rs)
1134 {
1135         ath_rate_newassoc_11n(sc, an, isnew, capflag, rs);
1136 }
1137
1138 void ath_rate_node_update(struct ath_softc_tgt *sc,
1139                           struct ath_node_target *an,
1140                           a_int32_t isnew,
1141                           a_uint32_t capflag,
1142                           struct ieee80211_rate *rs)
1143 {
1144         struct ieee80211_node_target *ni = &an->ni;
1145
1146         ath_rate_newassoc(sc, ATH_NODE_TARGET(ni), isnew, capflag, rs); 
1147 }
1148
1149 static int init_ath_rate_atheros(void);
1150 static void exit_ath_rate_atheros(void);
1151
1152 void
1153 ath_rate_newstate(struct ath_softc_tgt *sc,
1154                   struct ieee80211vap_target *vap,
1155                   enum ieee80211_state state,
1156                   a_uint32_t capflag,
1157                   struct ieee80211_rate *rs)
1158 {
1159         struct ieee80211_node_target *ni = vap->iv_bss;
1160         struct atheros_softc *asc = (struct atheros_softc *) sc->sc_rc;
1161
1162         asc->tx_chainmask = sc->sc_ic.ic_tx_chainmask;
1163         ath_rate_newassoc(sc, ATH_NODE_TARGET(ni), 1, capflag, rs);
1164 }
1165
1166 static void
1167 ath_rate_findrate_11n(struct ath_softc_tgt *sc,
1168                       struct ath_node_target *an,
1169                       size_t frameLen,
1170                       int numTries,
1171                       int numRates,
1172                       int stepDnInc,
1173                       unsigned int rcflag,
1174                       struct ath_rc_series series[],
1175                       int *isProbe)
1176 {
1177         *isProbe = 0;
1178         if (!numRates || !numTries) {
1179                 return;
1180         }
1181
1182         rcRateFind_11n(sc, an, numTries, numRates, stepDnInc, rcflag, series, isProbe);
1183 }
1184
1185 static void
1186 ath_rate_tx_complete_11n(struct ath_softc_tgt *sc,
1187                          struct ath_node_target *an,
1188                          struct ath_tx_desc *ds,
1189                          struct ath_rc_series rcs[], 
1190                          int nframes, int nbad)
1191 {
1192         int finalTSIdx = ds->ds_txstat.ts_rate;
1193         int tx_status = 0;
1194
1195         if ((ds->ds_txstat.ts_status & HAL_TXERR_XRETRY) ||
1196             (ds->ds_txstat.ts_status & HAL_TXERR_FIFO) || 
1197             (ds->ds_txstat.ts_flags & HAL_TX_DATA_UNDERRUN) ||
1198             (ds->ds_txstat.ts_flags & HAL_TX_DELIM_UNDERRUN)) {
1199                 tx_status = 1;
1200         }
1201
1202         rcUpdate_11n(sc, an,
1203                      ds->ds_txstat.ts_antenna, finalTSIdx,
1204                      tx_status, rcs, nframes , nbad,
1205                      ds->ds_txstat.ts_longretry);
1206 }
1207
1208 static void
1209 ath_rate_newassoc_11n(struct ath_softc_tgt *sc, struct ath_node_target *an, int isnew, 
1210                       unsigned int capflag, struct ieee80211_rate *rs)
1211 {
1212         if (isnew) {
1213                 struct atheros_node *oan = ATH_NODE_ATHEROS(an);
1214
1215                 oan->htcap = ((capflag & ATH_RC_DS_FLAG) ? WLAN_RC_DS_FLAG : 0) |
1216                         ((capflag & ATH_RC_HT40_SGI_FLAG) ? WLAN_RC_HT40_SGI_FLAG : 0) | 
1217                         ((capflag & ATH_RC_HT_FLAG)  ? WLAN_RC_HT_FLAG : 0) |
1218                         ((capflag & ATH_RC_CW40_FLAG) ? WLAN_RC_40_FLAG : 0) |
1219                         ((capflag & ATH_RC_WEP_TKIP_FLAG) ? WLAN_RC_WEP_TKIP_FLAG : 0);    
1220     
1221 #ifdef MAGPIE_MERLIN
1222                 /* Only MERLIN can send STBC */
1223                 oan->stbc = (capflag & ATH_RC_TX_STBC_FLAG) ? 1 : 0;
1224 #endif
1225                 rcSibUpdate_11n(sc, an, oan->htcap, 0, rs);
1226         }
1227 }
1228
1229 void ath_rate_mcs2rate(struct ath_softc_tgt *sc,a_uint8_t sgi, a_uint8_t ht40, 
1230                        a_uint8_t rateCode, a_uint32_t *txrate, a_uint32_t *rxrate)
1231 {
1232         int idx;
1233         struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
1234         RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
1235         a_uint32_t rateKbps = 0;
1236    
1237         *txrate = asc->currentTxRateKbps;
1238
1239         /* look  11NA table for rateKbps*/
1240         for (idx = 0; idx < pRateTable->rateCount && !rateKbps; ++idx) {   
1241                 if (pRateTable->info[idx].rateCode == rateCode) {
1242                         if(ht40 && sgi) {
1243                                 if(pRateTable->info[idx].valid == TRUE_40 &&
1244                                    pRateTable->info[idx].phy == WLAN_RC_PHY_HT_40_DS_HGI)
1245                                         rateKbps = pRateTable->info[idx].rateKbps;
1246                         } else if (ht40) {
1247                                 if (pRateTable->info[idx].valid == TRUE_40)/* HT40 only*/
1248                                         rateKbps = pRateTable->info[idx].rateKbps;
1249                         } else { 
1250                                 if (pRateTable->info[idx].valid != FALSE)
1251                                         rateKbps = pRateTable->info[idx].rateKbps;
1252                         }
1253                 }
1254         }
1255     
1256         *rxrate = rateKbps;
1257 }