Merge pull request #13 from olerem/buildfixes
[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                        ((capflag & ATH_RC_TX_STBC_FLAG)   ? WLAN_RC_STBC_FLAG  : 0), 
443                        keepState,
444                        pRateSet);
445 }
446
447 /*
448  * Return the median of three numbers
449  */
450 INLINE A_RSSI median(A_RSSI a, A_RSSI b, A_RSSI c)
451 {
452         if (a >= b) {
453                 if (b >= c) {
454                         return b;
455                 } else if (a > c) {
456                         return c;
457                 } else {
458                         return a;
459                 }
460         } else {
461                 if (a >= c) {
462                         return a;
463                 } else if (b >= c) {
464                         return c;
465                 } else {
466                         return b;
467                 }
468         }
469 }
470
471 static A_UINT8
472 rcRateFind_ht(struct ath_softc_tgt *sc, struct atheros_node *pSib,
473               const RATE_TABLE_11N *pRateTable, A_BOOL probeAllowed, A_BOOL *isProbing)
474 {
475         A_UINT32             dt;
476         A_UINT32             bestThruput, thisThruput;
477         A_UINT32             nowMsec;
478         A_UINT8              rate, nextRate, bestRate;
479         A_UINT8              maxIndex, minIndex;
480         A_INT8               index;
481         TX_RATE_CTRL         *pRc = NULL;
482
483         pRc = (TX_RATE_CTRL *)(pSib ? (pSib) : NULL);
484
485         *isProbing = FALSE;
486
487         /*
488          * Age (reduce) last ack rssi based on how old it is.
489          * The bizarre numbers are so the delta is 160msec,
490          * meaning we divide by 16.
491          *   0msec   <= dt <= 25msec:   don't derate
492          *   25msec  <= dt <= 185msec:  derate linearly from 0 to 10dB
493          *   185msec <= dt:             derate by 10dB
494          */
495
496         nowMsec = A_MS_TICKGET();
497         dt = nowMsec - pRc->rssiTime;
498
499         /*
500          * Now look up the rate in the rssi table and return it.
501          * If no rates match then we return 0 (lowest rate)
502          */
503
504         bestThruput = 0;
505         maxIndex = pRc->maxValidRate-1;
506
507         minIndex = 0;
508         bestRate = minIndex;
509     
510         /*
511          * Try the higher rate first. It will reduce memory moving time
512          * if we have very good channel characteristics.
513          */
514         for (index = maxIndex; index >= minIndex ; index--) {
515                 A_UINT8 perThres;
516     
517                 rate = pRc->validRateIndex[index];
518                 if (rate > pRc->rateMaxPhy) {
519                         continue;
520                 }
521
522                 /* if the best throughput is already larger than the userRateKbps..
523                  * then we could skip of rest of calculation.. 
524                  */
525                 if( bestThruput >= pRateTable->info[rate].userRateKbps)
526                         break;
527
528                 /*
529                  * For TCP the average collision rate is around 11%,
530                  * so we ignore PERs less than this.  This is to
531                  * prevent the rate we are currently using (whose
532                  * PER might be in the 10-15 range because of TCP
533                  * collisions) looking worse than the next lower
534                  * rate whose PER has decayed close to 0.  If we
535                  * used to next lower rate, its PER would grow to
536                  * 10-15 and we would be worse off then staying
537                  * at the current rate.
538                  */
539                 perThres = pRc->state[rate].per;
540                 if ( perThres < 12 ) {
541                         perThres = 12;
542                 }
543
544                 thisThruput = pRateTable->info[rate].userRateKbps * (100 - perThres);
545                 if (bestThruput <= thisThruput) {
546                         bestThruput = thisThruput;
547                         bestRate    = rate;
548                 }
549         }
550
551         rate = bestRate;
552
553         /*
554          * Must check the actual rate (rateKbps) to account for non-monoticity of
555          * 11g's rate table
556          */
557
558         if (rate >= pRc->rateMaxPhy && probeAllowed) {
559                 rate = pRc->rateMaxPhy;
560
561                 /* Probe the next allowed phy state */
562                 /* FIXME: Check to make sure ratMax is checked properly */
563                 if (rcGetNextValidTxRate( pRateTable, pRc, rate, &nextRate) && 
564                     (nowMsec - pRc->probeTime > pRateTable->probeInterval) &&
565                     (pRc->hwMaxRetryPktCnt >= 1))
566                 {
567                         rate                  = nextRate;
568                         pRc->probeRate        = rate;
569                         pRc->probeTime        = nowMsec;
570                         pRc->hwMaxRetryPktCnt = 0;
571                         *isProbing            = TRUE;
572
573                 }
574         }
575
576         /*
577          * Make sure rate is not higher than the allowed maximum.
578          * We should also enforce the min, but I suspect the min is
579          * normally 1 rather than 0 because of the rate 9 vs 6 issue
580          * in the old code.
581          */
582         if (rate > (pRc->rateTableSize - 1)) {
583                 rate = pRc->rateTableSize - 1;
584         }
585
586         /* record selected rate, which is used to decide if we want to do fast frame */
587         if (!(*isProbing) && pSib) {
588                 pSib->lastRateKbps = pRateTable->info[rate].rateKbps;
589                 ((struct atheros_softc*)sc->sc_rc)->currentTxRateKbps = pSib->lastRateKbps;
590                 ((struct atheros_softc*)sc->sc_rc)->currentTxRateIndex = rate;
591         }
592
593         return rate;
594 }
595
596 static void
597 rcRateSetseries(const RATE_TABLE_11N *pRateTable ,
598                 struct ath_rc_series *series,
599                 A_UINT8 tries, A_UINT8 rix,
600                 A_BOOL rtsctsenable, A_UINT32 chainmask,int stbc)
601 {
602         series->tries = tries;
603         series->flags = (rtsctsenable? ATH_RC_RTSCTS_FLAG : 0) | 
604                 (WLAN_RC_PHY_DS(pRateTable->info[rix].phy) ? ATH_RC_DS_FLAG : 0) | 
605                 (WLAN_RC_PHY_40(pRateTable->info[rix].phy) ? ATH_RC_CW40_FLAG : 0) | 
606                 (WLAN_RC_PHY_SGI(pRateTable->info[rix].phy) ? ATH_RC_HT40_SGI_FLAG : 0);
607 #ifdef MAGPIE_MERLIN
608         if (stbc) {
609                 /* For now, only single stream STBC is supported */
610                 if (pRateTable->info[rix].rateCode >= 0x80 && 
611                     pRateTable->info[rix].rateCode <= 0x87)
612                 {
613                         series->flags |= ATH_RC_TX_STBC_FLAG;
614                 }
615         }
616 #endif
617         series->rix = pRateTable->info[rix].baseIndex;
618         series->max4msframelen = pRateTable->info[rix].max4msframelen;
619         series->txrateKbps = pRateTable->info[rix].rateKbps;
620
621         /* If the hardware is capable of multiple transmit chains (chainmask is 3, 5 or 7), 
622          * then choose the number of transmit chains dynamically based on entries in the rate table.
623          */
624 #ifndef ATH_ENABLE_WLAN_FOR_K2
625         if(chainmask == 7)
626                 series->tx_chainmask = pRateTable->info[rix].txChainMask_3ch;
627         else if(chainmask == 1) 
628                 series->tx_chainmask = 1;
629         else 
630                 series->tx_chainmask = pRateTable->info[rix].txChainMask_2ch;  /*Chainmask is 3 or 5*/
631 #else
632         series->tx_chainmask = 1;
633 #endif
634 }
635
636 static A_UINT8 
637 rcRateGetIndex(struct ath_softc_tgt *sc, struct ath_node_target *an,        
638                const RATE_TABLE_11N *pRateTable , 
639                A_UINT8 rix, A_UINT16 stepDown, A_UINT16 minRate)
640 {
641         A_UINT32                j;
642         A_UINT8                 nextIndex;
643         struct atheros_node     *pSib = ATH_NODE_ATHEROS(an);
644         TX_RATE_CTRL            *pRc = (TX_RATE_CTRL *)(pSib);
645     
646         if (minRate) {
647                 for (j = RATE_TABLE_11N_SIZE; j > 0; j-- ) {
648                         if (rcGetNextLowerValidTxRate(pRateTable, pRc, rix, &nextIndex)) {
649                                 rix = nextIndex;
650                         } else {
651                                 break;
652                         }
653                 }
654         } else {
655                 for (j = stepDown; j > 0; j-- ) {
656                         if (rcGetNextLowerValidTxRate(pRateTable, pRc, rix, &nextIndex)) {
657                                 rix = nextIndex;
658                         } else {
659                                 break;
660                         }
661                 }
662         }
663
664         return rix;
665 }
666
667 void rcRateFind_11n(struct ath_softc_tgt *sc, struct ath_node_target *an, 
668                     int numTries, int numRates, int stepDnInc,
669                     unsigned int rcflag, struct ath_rc_series series[], int *isProbe)
670 {
671         A_UINT8 i = 0; 
672         A_UINT8 tryPerRate  = 0;
673         struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
674         RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
675         struct atheros_node *asn = ATH_NODE_ATHEROS(an);
676         A_UINT8 rix, nrix;
677         A_UINT8 dot11Rate;
678         WLAN_PHY phy;
679
680         rix = rcRateFind_ht(sc, asn, pRateTable, (rcflag & ATH_RC_PROBE_ALLOWED) ? 1 : 0, 
681                             isProbe);
682         nrix = rix;
683
684         if ((rcflag & ATH_RC_PROBE_ALLOWED) && (*isProbe)) {
685                 /* set one try for probe rates. For the probes don't enable rts */
686                 rcRateSetseries(pRateTable, &series[i++], 1, nrix,
687                                 FALSE, asc->tx_chainmask, asn->stbc);
688           
689                 /*
690                  * Get the next tried/allowed rate. No RTS for the next series
691                  * after the probe rate
692                  */
693                 nrix = rcRateGetIndex( sc, an, pRateTable, nrix, 1, FALSE);
694         }
695
696         tryPerRate = (numTries/numRates);
697
698         /* Set the choosen rate. No RTS for first series entry. */
699         rcRateSetseries(pRateTable, &series[i++], tryPerRate,
700                         nrix, FALSE, asc->tx_chainmask, asn->stbc);
701
702         /* Fill in the other rates for multirate retry */
703         for (; i < numRates; i++) {
704                 A_UINT8 tryNum;
705                 A_UINT8 minRate;
706
707                 tryNum  = ((i + 1) == numRates) ? numTries - (tryPerRate * i) : tryPerRate ;
708                 minRate = (((i + 1) == numRates) && (rcflag & ATH_RC_MINRATE_LASTRATE)) ? 1 : 0;
709
710                 nrix = rcRateGetIndex(sc, an, pRateTable, nrix, stepDnInc, minRate);
711
712                 /* All other rates in the series have RTS enabled */
713                 rcRateSetseries(pRateTable, &series[i], tryNum,
714                                 nrix, TRUE, asc->tx_chainmask, asn->stbc);
715         }
716
717         /*
718          * BUG 26545:
719          * Change rate series to enable aggregation when operating at lower MCS rates. 
720          * When first rate in series is MCS2 in HT40 @ 2.4GHz, series should look like:
721          *    {MCS2, MCS1, MCS0, MCS0}.
722          * When first rate in series is MCS3 in HT20 @ 2.4GHz, series should look like:
723          *    {MCS3, MCS2, MCS1, MCS1}
724          * So, set fourth rate in series to be same as third one for above conditions.
725          */
726         if (sc->sc_curmode == IEEE80211_MODE_11NG) {
727                 dot11Rate = pRateTable->info[rix].dot11Rate;
728                 phy = pRateTable->info[rix].phy;
729                 if (i == 4 &&
730                     ((dot11Rate == 2 && phy == WLAN_RC_PHY_HT_40_SS) || 
731                      (dot11Rate == 3 && phy == WLAN_RC_PHY_HT_20_SS))) 
732                 {
733                         series[3].rix = series[2].rix;
734                         series[3].flags = series[2].flags;
735                         series[3].max4msframelen = series[2].max4msframelen;
736                 }
737         }
738
739         /*
740          * 2009/02/06
741          * AP91 Kite: NetGear OTA location-4 downlink.
742          *            Enable RTS/CTS at MCS 3-0 for downlink throughput.
743          */
744         if (sc->sc_curmode == IEEE80211_MODE_11NG) {
745                 dot11Rate = pRateTable->info[rix].dot11Rate;
746                 if (dot11Rate <= 3 ) {
747                         series[0].flags |= ATH_RC_RTSCTS_FLAG;         
748                 }
749         }
750 }
751
752 static void
753 rcUpdate_ht(struct ath_softc_tgt *sc, struct ath_node_target *an, int txRate, 
754             A_BOOL Xretries, int retries, A_UINT8 curTxAnt, 
755             A_UINT16 nFrames, A_UINT16 nBad)
756 {
757         TX_RATE_CTRL *pRc;
758         A_UINT32 nowMsec = A_MS_TICKGET();
759         A_UINT8 lastPer;
760         int rate,count;
761         struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
762         struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
763         RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
764
765         static A_UINT32 nRetry2PerLookup[10] = {
766                 100 * 0 / 1,    // 0
767                 100 * 1 / 4,    // 25
768                 100 * 1 / 2,    // 50
769                 100 * 3 / 4,    // 75
770                 100 * 4 / 5,    // 80
771                 100 * 5 / 6,    // 83.3
772                 100 * 6 / 7,    // 85.7
773                 100 * 7 / 8,    // 87.5
774                 100 * 8 / 9,    // 88.8
775                 100 * 9 / 10    // 90
776         };
777
778         if (!pSib)
779                 return;
780
781         pRc = (TX_RATE_CTRL *)(pSib);
782
783         ASSERT(retries >= 0 && retries < MAX_TX_RETRIES);
784         ASSERT(txRate >= 0);
785     
786         if (txRate < 0) {
787                 return;
788         }
789
790         lastPer = pRc->state[txRate].per;
791
792         if (Xretries) {
793                 /* Update the PER. */
794                 if (Xretries == 1) {
795                         pRc->state[txRate].per += 30;
796                         if (pRc->state[txRate].per > 100) {
797                                 pRc->state[txRate].per = 100;
798                         }
799                 } else {
800                         /* Xretries == 2 */
801
802                         count = sizeof(nRetry2PerLookup) / sizeof(nRetry2PerLookup[0]);
803                         if (retries >= count) {
804                                 retries = count - 1;
805                         }
806
807                         /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
808                         pRc->state[txRate].per = (A_UINT8)(pRc->state[txRate].per - 
809                                                    (pRc->state[txRate].per / 8) + ((100) / 8));
810                 }
811
812                 /* Xretries == 1 or 2 */
813
814                 if (pRc->probeRate == txRate)
815                         pRc->probeRate = 0;
816         } else {
817                 /* Xretries == 0 */
818
819                 /*
820                  * Update the PER.  Make sure it doesn't index out of array's bounds.
821                  */
822                 count = sizeof(nRetry2PerLookup) / sizeof(nRetry2PerLookup[0]);
823                 if (retries >= count) {
824                         retries = count - 1;
825                 }
826
827                 if (nBad) {
828                         /* new_PER = 7/8*old_PER + 1/8*(currentPER)  */
829                         /*
830                          * Assuming that nFrames is not 0.  The current PER
831                          * from the retries is 100 * retries / (retries+1),
832                          * since the first retries attempts failed, and the
833                          * next one worked.  For the one that worked, nBad
834                          * subframes out of nFrames wored, so the PER for
835                          * that part is 100 * nBad / nFrames, and it contributes
836                          * 100 * nBad / (nFrames * (retries+1)) to the above
837                          * PER.  The expression below is a simplified version
838                          * of the sum of these two terms.
839                          */
840                         if (nFrames > 0)
841                                 pRc->state[txRate].per = (A_UINT8)(pRc->state[txRate].per - 
842                                            (pRc->state[txRate].per / 8) + 
843                                            ((100*(retries*nFrames + nBad)/(nFrames*(retries+1))) / 8));
844                 } else {
845                         /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
846
847                         pRc->state[txRate].per = (A_UINT8)(pRc->state[txRate].per - 
848                                    (pRc->state[txRate].per / 8) + (nRetry2PerLookup[retries] / 8));
849                 }
850
851                 /*
852                  * If we got at most one retry then increase the max rate if
853                  * this was a probe.  Otherwise, ignore the probe.
854                  */
855
856                 if (pRc->probeRate && pRc->probeRate == txRate) {
857                         if (retries > 0 || 2 * nBad > nFrames) {
858                                 /*
859                                  * Since we probed with just a single attempt,
860                                  * any retries means the probe failed.  Also,
861                                  * if the attempt worked, but more than half
862                                  * the subframes were bad then also consider
863                                  * the probe a failure.
864                                  */
865                                 pRc->probeRate = 0;
866                         } else {
867                                 pRc->rateMaxPhy = pRc->probeRate;
868
869                                 if (pRc->state[pRc->probeRate].per > 30) {
870                                         pRc->state[pRc->probeRate].per = 20;
871                                 }
872
873                                 pRc->probeRate = 0;
874
875                                 /*
876                                  * Since this probe succeeded, we allow the next probe
877                                  * twice as soon.  This allows the maxRate to move up
878                                  * faster if the probes are succesful.
879                                  */
880                                 pRc->probeTime = nowMsec - pRateTable->probeInterval / 2;
881                         }
882                 }
883
884                 if (retries > 0) {
885                         /*
886                          * Don't update anything.  We don't know if this was because
887                          * of collisions or poor signal.
888                          *
889                          * Later: if rssiAck is close to pRc->state[txRate].rssiThres
890                          * and we see lots of retries, then we could increase
891                          * pRc->state[txRate].rssiThres.
892                          */
893                         pRc->hwMaxRetryPktCnt = 0;
894                 } else {
895                         /*
896                          * It worked with no retries.  First ignore bogus (small)
897                          * rssiAck values.
898                          */
899                         if (txRate == pRc->rateMaxPhy && pRc->hwMaxRetryPktCnt < 255) {
900                                 pRc->hwMaxRetryPktCnt++;
901                         }
902
903                 }
904         }
905
906         /* For all cases */
907
908         ASSERT((pRc->rateMaxPhy >= 0 && pRc->rateMaxPhy <= pRc->rateTableSize && 
909                 pRc->rateMaxPhy != INVALID_RATE_MAX));
910     
911         /*
912          * If this rate looks bad (high PER) then stop using it for
913          * a while (except if we are probing).
914          */
915         if (pRc->state[txRate].per >= 55 && txRate > 0 &&
916             pRateTable->info[txRate].rateKbps <= 
917             pRateTable->info[pRc->rateMaxPhy].rateKbps)
918         {
919                 rcGetNextLowerValidTxRate(pRateTable, pRc, (A_UINT8) txRate, 
920                                           &pRc->rateMaxPhy);
921
922                 /* Don't probe for a little while. */
923                 pRc->probeTime = nowMsec;
924         }
925
926         /* Make sure the rates below this have lower PER */
927         /* Monotonicity is kept only for rates below the current rate. */
928         if (pRc->state[txRate].per < lastPer) {
929                 for (rate = txRate - 1; rate >= 0; rate--) {
930                         if (pRateTable->info[rate].phy != pRateTable->info[txRate].phy) {
931                                 break;
932                         }
933
934                         if (pRc->state[rate].per > pRc->state[rate+1].per) {
935                                 pRc->state[rate].per = pRc->state[rate+1].per;
936                         }
937                 }
938         }
939
940         /* Maintain monotonicity for rates above the current rate*/
941         for (rate = txRate; rate < pRc->rateTableSize - 1; rate++) {
942                 if (pRc->state[rate+1].per < pRc->state[rate].per) {
943                         pRc->state[rate+1].per = pRc->state[rate].per;
944                 }
945         }
946
947         /* Every so often, we reduce the thresholds and PER (different for CCK and OFDM). */
948         if (nowMsec - pRc->perDownTime >= pRateTable->rssiReduceInterval) {
949                 for (rate = 0; rate < pRc->rateTableSize; rate++) {
950                         pRc->state[rate].per = 7*pRc->state[rate].per/8;
951                 }
952
953                 pRc->perDownTime = nowMsec;
954         }
955 }
956
957 /*
958  * This routine is called by the Tx interrupt service routine to give
959  * the status of previous frames.
960  */
961 void rcUpdate_11n(struct ath_softc_tgt *sc, struct ath_node_target *an,
962                   A_UINT8 curTxAnt, 
963                   int finalTSIdx, int Xretries,
964                   struct ath_rc_series rcs[], int nFrames, 
965                   int nBad, int long_retry)
966 {
967         A_UINT32 series = 0;
968         A_UINT32 rix;
969         struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
970         RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
971         struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
972         TX_RATE_CTRL *pRc = (TX_RATE_CTRL *)(pSib);
973         A_UINT8 flags;
974
975         if (!an) {
976                 adf_os_assert(0);
977                 return;
978         }
979
980         ASSERT (rcs[0].tries != 0);
981
982         /*
983          * If the first rate is not the final index, there are intermediate rate failures
984          * to be processed.
985          */
986         if (finalTSIdx != 0) {
987
988                 /* Process intermediate rates that failed.*/
989                 for (series = 0; series < finalTSIdx ; series++) {
990                         if (rcs[series].tries != 0) {
991                                 flags = rcs[series].flags;
992                                 /* If HT40 and we have switched mode from 40 to 20 => don't update */
993                                 if ((flags & ATH_RC_CW40_FLAG) && 
994                                     (pRc->rcPhyMode != (flags & ATH_RC_CW40_FLAG))) {
995                                         return;
996                                 }
997                                 if ((flags & ATH_RC_CW40_FLAG) && (flags & ATH_RC_HT40_SGI_FLAG)) {
998                                         rix = pRateTable->info[rcs[series].rix].htIndex;
999                                 } else if (flags & ATH_RC_HT40_SGI_FLAG) {
1000                                         rix = pRateTable->info[rcs[series].rix].sgiIndex;
1001                                 } else if (flags & ATH_RC_CW40_FLAG) {
1002                                         rix = pRateTable->info[rcs[series].rix].cw40Index;
1003                                 } else {
1004                                         rix = pRateTable->info[rcs[series].rix].baseIndex;
1005                                 }
1006
1007                                 /* FIXME:XXXX, too many args! */
1008                                 rcUpdate_ht(sc, an, rix, Xretries? 1 : 2, rcs[series].tries, 
1009                                             curTxAnt, nFrames, nFrames);
1010                         }
1011                 }
1012         } else {
1013                 /*
1014                  * Handle the special case of MIMO PS burst, where the second aggregate is sent
1015                  *  out with only one rate and one try. Treating it as an excessive retry penalizes
1016                  * the rate inordinately.
1017                  */
1018                 if (rcs[0].tries == 1 && Xretries == 1) {
1019                         Xretries = 2;
1020                 }
1021         }
1022
1023         flags = rcs[series].flags;
1024         /* If HT40 and we have switched mode from 40 to 20 => don't update */
1025         if ((flags & ATH_RC_CW40_FLAG) && 
1026             (pRc->rcPhyMode != (flags & ATH_RC_CW40_FLAG))) {
1027                 return;
1028         }
1029         if ((flags & ATH_RC_CW40_FLAG) && (flags & ATH_RC_HT40_SGI_FLAG)) {
1030                 rix = pRateTable->info[rcs[series].rix].htIndex;
1031         } else if (flags & ATH_RC_HT40_SGI_FLAG) {
1032                 rix = pRateTable->info[rcs[series].rix].sgiIndex;
1033         } else if (flags & ATH_RC_CW40_FLAG) {
1034                 rix = pRateTable->info[rcs[series].rix].cw40Index;
1035         } else {
1036                 rix = pRateTable->info[rcs[series].rix].baseIndex;
1037         }
1038
1039         /* FIXME:XXXX, too many args! */
1040         rcUpdate_ht(sc, an, rix, Xretries, long_retry, curTxAnt, 
1041                     nFrames, nBad);
1042 }
1043
1044 void ath_tx_status_update_rate(struct ath_softc_tgt *sc,
1045                                struct ath_rc_series rcs[],
1046                                int series,
1047                                WMI_TXSTATUS_EVENT *txs)
1048 {
1049         struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
1050         RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
1051
1052         /* HT Rate */
1053         if (pRateTable->info[rcs[series].rix].rateCode & 0x80) {
1054                 txs->txstatus[txs->cnt].ts_rate |= SM(pRateTable->info[rcs[series].rix].dot11Rate,
1055                                                                        ATH9K_HTC_TXSTAT_RATE);
1056                 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_MCS;
1057
1058                 if (rcs[series].flags & ATH_RC_CW40_FLAG)
1059                         txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_CW40;
1060
1061                 if (rcs[series].flags & ATH_RC_HT40_SGI_FLAG)
1062                         txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_SGI;
1063
1064         } else {
1065                 txs->txstatus[txs->cnt].ts_rate |= SM(rcs[series].rix, ATH9K_HTC_TXSTAT_RATE);
1066         }
1067
1068         if (rcs[series].flags & ATH_RC_RTSCTS_FLAG)
1069                 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_RTC_CTS;
1070
1071 }
1072
1073 struct ath_ratectrl *
1074 ath_rate_attach(struct ath_softc_tgt *sc)
1075 {
1076         struct atheros_softc *asc;
1077
1078         asc = adf_os_mem_alloc(sizeof(struct atheros_softc));
1079         if (asc == NULL)
1080                 return NULL;
1081
1082         adf_os_mem_set(asc, 0, sizeof(struct atheros_softc));
1083         asc->arc.arc_space = sizeof(struct atheros_node);
1084
1085         ar5416AttachRateTables(asc);
1086
1087         asc->tx_chainmask = 1;
1088     
1089         return &asc->arc;
1090 }
1091
1092 void
1093 ath_rate_detach(struct ath_ratectrl *rc)
1094 {
1095         adf_os_mem_free(rc);
1096 }
1097
1098 void
1099 ath_rate_findrate(struct ath_softc_tgt *sc,
1100                   struct ath_node_target *an,
1101                   int shortPreamble,
1102                   size_t frameLen,
1103                   int numTries,
1104                   int numRates,
1105                   int stepDnInc,
1106                   unsigned int rcflag,
1107                   struct ath_rc_series series[],
1108                   int *isProbe)
1109 {
1110         *isProbe = 0;
1111
1112         if (!numRates || !numTries) {
1113                 return;
1114         }
1115
1116         ath_rate_findrate_11n(sc, an, frameLen, numTries, numRates, stepDnInc,
1117                               rcflag, series, isProbe);
1118 }
1119
1120 #define MS(_v, _f)  (((_v) & _f) >> _f##_S)
1121
1122 void
1123 ath_rate_tx_complete(struct ath_softc_tgt *sc,
1124                      struct ath_node_target *an,
1125                      struct ath_tx_desc *ds,
1126                      struct ath_rc_series rcs[], 
1127                      int nframes, int nbad)
1128 {
1129         ath_rate_tx_complete_11n(sc, an, ds, rcs, nframes, nbad);
1130 }
1131
1132 void
1133 ath_rate_newassoc(struct ath_softc_tgt *sc, struct ath_node_target *an, int isnew, 
1134                   unsigned int capflag, struct ieee80211_rate *rs)
1135 {
1136         ath_rate_newassoc_11n(sc, an, isnew, capflag, rs);
1137 }
1138
1139 void ath_rate_node_update(struct ath_softc_tgt *sc,
1140                           struct ath_node_target *an,
1141                           a_int32_t isnew,
1142                           a_uint32_t capflag,
1143                           struct ieee80211_rate *rs)
1144 {
1145         struct ieee80211_node_target *ni = &an->ni;
1146
1147         ath_rate_newassoc(sc, ATH_NODE_TARGET(ni), isnew, capflag, rs); 
1148 }
1149
1150 static int init_ath_rate_atheros(void);
1151 static void exit_ath_rate_atheros(void);
1152
1153 void
1154 ath_rate_newstate(struct ath_softc_tgt *sc,
1155                   struct ieee80211vap_target *vap,
1156                   enum ieee80211_state state,
1157                   a_uint32_t capflag,
1158                   struct ieee80211_rate *rs)
1159 {
1160         struct ieee80211_node_target *ni = vap->iv_bss;
1161         struct atheros_softc *asc = (struct atheros_softc *) sc->sc_rc;
1162
1163         asc->tx_chainmask = sc->sc_ic.ic_tx_chainmask;
1164         ath_rate_newassoc(sc, ATH_NODE_TARGET(ni), 1, capflag, rs);
1165 }
1166
1167 static void
1168 ath_rate_findrate_11n(struct ath_softc_tgt *sc,
1169                       struct ath_node_target *an,
1170                       size_t frameLen,
1171                       int numTries,
1172                       int numRates,
1173                       int stepDnInc,
1174                       unsigned int rcflag,
1175                       struct ath_rc_series series[],
1176                       int *isProbe)
1177 {
1178         *isProbe = 0;
1179         if (!numRates || !numTries) {
1180                 return;
1181         }
1182
1183         rcRateFind_11n(sc, an, numTries, numRates, stepDnInc, rcflag, series, isProbe);
1184 }
1185
1186 static void
1187 ath_rate_tx_complete_11n(struct ath_softc_tgt *sc,
1188                          struct ath_node_target *an,
1189                          struct ath_tx_desc *ds,
1190                          struct ath_rc_series rcs[], 
1191                          int nframes, int nbad)
1192 {
1193         int finalTSIdx = ds->ds_txstat.ts_rate;
1194         int tx_status = 0;
1195
1196         if ((ds->ds_txstat.ts_status & HAL_TXERR_XRETRY) ||
1197             (ds->ds_txstat.ts_status & HAL_TXERR_FIFO) || 
1198             (ds->ds_txstat.ts_flags & HAL_TX_DATA_UNDERRUN) ||
1199             (ds->ds_txstat.ts_flags & HAL_TX_DELIM_UNDERRUN)) {
1200                 tx_status = 1;
1201         }
1202
1203         rcUpdate_11n(sc, an,
1204                      ds->ds_txstat.ts_antenna, finalTSIdx,
1205                      tx_status, rcs, nframes , nbad,
1206                      ds->ds_txstat.ts_longretry);
1207 }
1208
1209 static void
1210 ath_rate_newassoc_11n(struct ath_softc_tgt *sc, struct ath_node_target *an, int isnew, 
1211                       unsigned int capflag, struct ieee80211_rate *rs)
1212 {
1213         if (isnew) {
1214                 struct atheros_node *oan = ATH_NODE_ATHEROS(an);
1215
1216                 oan->htcap = ((capflag & ATH_RC_DS_FLAG) ? WLAN_RC_DS_FLAG : 0) |
1217                         ((capflag & ATH_RC_HT40_SGI_FLAG) ? WLAN_RC_HT40_SGI_FLAG : 0) | 
1218                         ((capflag & ATH_RC_HT_FLAG)  ? WLAN_RC_HT_FLAG : 0) |
1219                         ((capflag & ATH_RC_CW40_FLAG) ? WLAN_RC_40_FLAG : 0) |
1220                         ((capflag & ATH_RC_WEP_TKIP_FLAG) ? WLAN_RC_WEP_TKIP_FLAG : 0);    
1221     
1222 #ifdef MAGPIE_MERLIN
1223                 /* Rx STBC is a 2-bit mask. Needs to convert from ath definition to wlan definition. */
1224         
1225                 oan->htcap |= (((capflag & ATH_RC_RX_STBC_FLAG) >> ATH_RC_RX_STBC_FLAG_S)
1226                                << WLAN_RC_STBC_FLAG_S);
1227                        
1228                 /* If only one chain is enabled, do not do stbc. */
1229                 if (sc->sc_txstbcsupport) {
1230                         oan->stbc = (capflag & ATH_RC_RX_STBC_FLAG) >> ATH_RC_RX_STBC_FLAG_S;
1231                 } else {
1232                         oan->stbc = 0;
1233                 }
1234         
1235 #endif
1236                 rcSibUpdate_11n(sc, an, oan->htcap, 0, rs);
1237         }
1238 }
1239
1240 void ath_rate_mcs2rate(struct ath_softc_tgt *sc,a_uint8_t sgi, a_uint8_t ht40, 
1241                        a_uint8_t rateCode, a_uint32_t *txrate, a_uint32_t *rxrate)
1242 {
1243         int idx;
1244         struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
1245         RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
1246         a_uint32_t rateKbps = 0;
1247    
1248         *txrate = asc->currentTxRateKbps;
1249
1250         /* look  11NA table for rateKbps*/
1251         for (idx = 0; idx < pRateTable->rateCount && !rateKbps; ++idx) {   
1252                 if (pRateTable->info[idx].rateCode == rateCode) {
1253                         if(ht40 && sgi) {
1254                                 if(pRateTable->info[idx].valid == TRUE_40 &&
1255                                    pRateTable->info[idx].phy == WLAN_RC_PHY_HT_40_DS_HGI)
1256                                         rateKbps = pRateTable->info[idx].rateKbps;
1257                         } else if (ht40) {
1258                                 if (pRateTable->info[idx].valid == TRUE_40)/* HT40 only*/
1259                                         rateKbps = pRateTable->info[idx].rateKbps;
1260                         } else { 
1261                                 if (pRateTable->info[idx].valid != FALSE)
1262                                         rateKbps = pRateTable->info[idx].rateKbps;
1263                         }
1264                 }
1265         }
1266     
1267         *rxrate = rateKbps;
1268 }