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