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