2 * Copyright (c) 2013 Qualcomm Atheros, Inc.
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:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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.
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.
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>
50 #include <adf_net_types.h>
52 #include <ieee80211_var.h>
54 #include <if_athvar.h>
58 #include "ratectrl11n.h"
60 INLINE A_RSSI median(A_RSSI a, A_RSSI b, A_RSSI c);
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);
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,
71 static void ath_rate_findrate_11n(struct ath_softc_tgt *sc,
72 struct ath_node_target *an,
78 struct ath_rc_series series[],
82 rcSortValidRates(const RATE_TABLE_11N *pRateTable, TX_RATE_CTRL *pRc)
86 for (i=pRc->maxValidRate-1; i > 0; i--) {
87 for (j=0; j <= i-1; j++) {
89 if (pRateTable->info[pRc->validRateIndex[j]].rateKbps >
90 pRateTable->info[pRc->validRateIndex[j+1]].rateKbps)
93 if (pRateTable->info[pRc->validRateIndex[j]].userRateKbps >
94 pRateTable->info[pRc->validRateIndex[j+1]].userRateKbps)
98 tmp = pRc->validRateIndex[j];
99 pRc->validRateIndex[j] = pRc->validRateIndex[j+1];
100 pRc->validRateIndex[j+1] = tmp;
106 /* Access functions for validTxRateMask */
109 rcInitValidTxMask(TX_RATE_CTRL *pRc)
113 for (i = 0; i < pRc->rateTableSize; i++) {
114 pRc->validRateIndex[i] = FALSE;
119 rcSetValidTxMask(TX_RATE_CTRL *pRc, A_UINT8 index, A_BOOL validTxRate)
121 ASSERT(index < pRc->rateTableSize);
122 pRc->validRateIndex[index] = validTxRate ? TRUE : FALSE;
127 rcIsValidTxMask(TX_RATE_CTRL *pRc, A_UINT8 index)
129 ASSERT(index < pRc->rateTableSize);
130 return (pRc->validRateIndex[index]);
133 /* Iterators for validTxRateMask */
135 rcGetNextValidTxRate(const RATE_TABLE_11N *pRateTable, TX_RATE_CTRL *pRc,
136 A_UINT8 curValidTxRate, A_UINT8 *pNextIndex)
140 for (i = 0; i < pRc->maxValidRate-1; i++) {
141 if (pRc->validRateIndex[i] == curValidTxRate) {
142 *pNextIndex = pRc->validRateIndex[i+1];
147 /* No more valid rates */
154 rcGetNextLowerValidTxRate(const RATE_TABLE_11N *pRateTable, TX_RATE_CTRL *pRc,
155 A_UINT8 curValidTxRate, A_UINT8 *pNextIndex)
159 for (i = 1; i < pRc->maxValidRate ; i++) {
160 if (pRc->validRateIndex[i] == curValidTxRate) {
161 *pNextIndex = pRc->validRateIndex[i-1];
169 /* Return true only for single stream */
172 rcIsValidPhyRate(A_UINT32 phy, A_UINT32 capflag, A_BOOL ignoreCW)
174 if (WLAN_RC_PHY_HT(phy) && !(capflag & WLAN_RC_HT_FLAG)) {
178 if (WLAN_RC_PHY_DS(phy) && !(capflag & WLAN_RC_DS_FLAG)) {
181 if (WLAN_RC_PHY_SGI(phy) && !(capflag & WLAN_RC_HT40_SGI_FLAG)) {
185 if (!ignoreCW && WLAN_RC_PHY_HT(phy)) {
186 if (WLAN_RC_PHY_40(phy) && !(capflag & WLAN_RC_40_FLAG)) {
190 if (!WLAN_RC_PHY_40(phy) && (capflag & WLAN_RC_40_FLAG)) {
199 * Initialize the Valid Rate Index from valid entries in Rate Table
201 static A_UINT8 rcSibInitValidRates(const RATE_TABLE_11N *pRateTable,
204 PHY_STATE_CTRL *pPhyStateCtrl)
207 A_UINT8 singleStream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
210 for (i = 0; i < pRateTable->rateCount; i++) {
212 valid = pRateTable->info[i].validSingleStream;
214 valid = pRateTable->info[i].valid;
218 A_UINT32 phy = pRateTable->info[i].phy;
220 if (!rcIsValidPhyRate(phy, capflag, FALSE))
223 pPhyStateCtrl->validPhyRateIndex[phy][pPhyStateCtrl->validPhyRateCount[phy]] = i;
224 pPhyStateCtrl->validPhyRateCount[phy] += 1;
226 rcSetValidTxMask(pRc, i, TRUE);
236 * Initialize the Valid Rate Index from Rate Set
239 rcSibSetValidRates(const RATE_TABLE_11N *pRateTable,
241 struct ieee80211_rateset *pRateSet,
243 struct ath_node_target *an,
244 PHY_STATE_CTRL *pPhyStateCtrl)
246 A_UINT8 i, j, hi = 0;
247 A_UINT8 singleStream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
249 struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
251 /* Use intersection of working rates and valid rates */
252 for (i = 0; i < pRateSet->rs_nrates; i++) {
253 for (j = 0; j < pRateTable->rateCount; j++) {
254 A_UINT32 phy = pRateTable->info[j].phy;
258 valid = pRateTable->info[j].validSTBC;
259 } else if (singleStream) {
263 valid = pRateTable->info[j].validSingleStream;
265 valid = pRateTable->info[j].valid;
269 * We allow a rate only if its valid and the capflag matches one of
270 * the validity (TRUE/TRUE_20/TRUE_40) flags
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))
280 pPhyStateCtrl->validPhyRateIndex[phy][pPhyStateCtrl->validPhyRateCount[phy]] = j;
281 pPhyStateCtrl->validPhyRateCount[phy] += 1;
283 rcSetValidTxMask(pRc, j, TRUE);
293 rcSibSetValidHtRates(const RATE_TABLE_11N *pRateTable,
297 struct ath_node_target *an,
298 PHY_STATE_CTRL *pPhyStateCtrl)
300 A_UINT8 i, j, hi = 0;
301 A_UINT8 singleStream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
303 struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
305 /* Use intersection of working rates and valid rates */
306 for (i = 0; i < ((struct ieee80211_rateset *)pMcsSet)->rs_nrates; i++) {
307 for (j = 0; j < pRateTable->rateCount; j++) {
308 A_UINT32 phy = pRateTable->info[j].phy;
312 valid = pRateTable->info[j].validSTBC;
313 } else if (singleStream) {
317 valid = pRateTable->info[j].validSingleStream;
319 valid = pRateTable->info[j].valid;
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) &&
328 (capflag & WLAN_RC_WEP_TKIP_FLAG)) )
333 if (!rcIsValidPhyRate(phy, capflag, FALSE))
336 pPhyStateCtrl->validPhyRateIndex[phy][pPhyStateCtrl->validPhyRateCount[phy]] = j;
337 pPhyStateCtrl->validPhyRateCount[phy] += 1;
339 rcSetValidTxMask(pRc, j, TRUE);
348 * Update the SIB's rate control information
350 * This should be called when the supported rates change
351 * (e.g. SME operation, wireless mode change)
353 * It will determine which rates are valid for use.
356 rcSibUpdate_ht(struct ath_softc_tgt *sc, struct ath_node_target *an,
357 A_UINT32 capflag, A_BOOL keepState, struct ieee80211_rate *pRateSet)
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;
366 A_UINT8 i, j, k, hi = 0, htHi = 0;
368 pRateTable = (RATE_TABLE_11N*)asc->hwRateTable[sc->sc_curmode];
370 /* Initial rate table size. Will change depending on the working rate set */
371 pRc->rateTableSize = MAX_TX_RATE_TBL;
373 /* Initialize thresholds according to the global rate table */
374 for (i = 0 ; (i < pRc->rateTableSize) && (!keepState); i++) {
375 pRc->state[i].per = 0;
378 /* Determine the valid rates */
379 rcInitValidTxMask(pRc);
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;
385 mPhyCtrlState.validPhyRateCount[i] = 0;
388 pRc->rcPhyMode = (capflag & WLAN_RC_40_FLAG);
390 if (pRateSet == NULL || !pRateSet->rates.rs_nrates) {
391 /* No working rate, just initialize valid rates */
392 hi = rcSibInitValidRates(pRateTable, pRc, capflag, &mPhyCtrlState);
394 /* Use intersection of working rates and valid rates */
395 hi = rcSibSetValidRates(pRateTable, pRc, &(pRateSet->rates),
396 capflag, an, &mPhyCtrlState);
398 if (capflag & WLAN_RC_HT_FLAG) {
399 htHi = rcSibSetValidHtRates(pRateTable, pRc, phtMcs,
400 capflag, an, &mPhyCtrlState);
403 hi = A_MAX(hi, htHi);
406 pRc->rateTableSize = hi + 1;
409 ASSERT(pRc->rateTableSize <= MAX_TX_RATE_TBL);
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];
416 if (!rcIsValidPhyRate(i, pRateTable->initialRateMax, TRUE) ||
417 !mPhyCtrlState.validPhyRateCount[i])
420 pRc->rateMaxPhy = mPhyCtrlState.validPhyRateIndex[i][j-1];
423 ASSERT(pRc->rateTableSize <= MAX_TX_RATE_TBL);
424 ASSERT(k <= MAX_TX_RATE_TBL);
426 pRc->rateMaxPhy = pRc->validRateIndex[k-4];
427 pRc->maxValidRate = k;
429 rcSortValidRates(pRateTable, pRc);
433 rcSibUpdate_11n(struct ath_softc_tgt *sc, struct ath_node_target *pSib,
434 A_UINT32 capflag, A_BOOL keepState, struct ieee80211_rate *pRateSet)
438 ((capflag & ATH_RC_DS_FLAG) ? WLAN_RC_DS_FLAG : 0) |
439 ((capflag & ATH_RC_HT40_SGI_FLAG) ? WLAN_RC_HT40_SGI_FLAG : 0) |
440 ((capflag & ATH_RC_HT_FLAG) ? WLAN_RC_HT_FLAG : 0) |
441 ((capflag & ATH_RC_CW40_FLAG) ? WLAN_RC_40_FLAG : 0) |
442 ((capflag & ATH_RC_TX_STBC_FLAG) ? WLAN_RC_STBC_FLAG : 0),
448 * Return the median of three numbers
450 INLINE A_RSSI median(A_RSSI a, A_RSSI b, A_RSSI c)
472 rcRateFind_ht(struct ath_softc_tgt *sc, struct atheros_node *pSib,
473 const RATE_TABLE_11N *pRateTable, A_BOOL probeAllowed, A_BOOL *isProbing)
476 A_UINT32 bestThruput, thisThruput;
478 A_UINT8 rate, nextRate, bestRate;
479 A_UINT8 maxIndex, minIndex;
481 TX_RATE_CTRL *pRc = NULL;
483 pRc = (TX_RATE_CTRL *)(pSib ? (pSib) : NULL);
488 * Age (reduce) last ack rssi based on how old it is.
489 * The bizarre numbers are so the delta is 160msec,
490 * meaning we divide by 16.
491 * 0msec <= dt <= 25msec: don't derate
492 * 25msec <= dt <= 185msec: derate linearly from 0 to 10dB
493 * 185msec <= dt: derate by 10dB
496 nowMsec = A_MS_TICKGET();
497 dt = nowMsec - pRc->rssiTime;
500 * Now look up the rate in the rssi table and return it.
501 * If no rates match then we return 0 (lowest rate)
505 maxIndex = pRc->maxValidRate-1;
511 * Try the higher rate first. It will reduce memory moving time
512 * if we have very good channel characteristics.
514 for (index = maxIndex; index >= minIndex ; index--) {
517 rate = pRc->validRateIndex[index];
518 if (rate > pRc->rateMaxPhy) {
522 /* if the best throughput is already larger than the userRateKbps..
523 * then we could skip of rest of calculation..
525 if( bestThruput >= pRateTable->info[rate].userRateKbps)
529 * For TCP the average collision rate is around 11%,
530 * so we ignore PERs less than this. This is to
531 * prevent the rate we are currently using (whose
532 * PER might be in the 10-15 range because of TCP
533 * collisions) looking worse than the next lower
534 * rate whose PER has decayed close to 0. If we
535 * used to next lower rate, its PER would grow to
536 * 10-15 and we would be worse off then staying
537 * at the current rate.
539 perThres = pRc->state[rate].per;
540 if ( perThres < 12 ) {
544 thisThruput = pRateTable->info[rate].userRateKbps * (100 - perThres);
545 if (bestThruput <= thisThruput) {
546 bestThruput = thisThruput;
554 * Must check the actual rate (rateKbps) to account for non-monoticity of
558 if (rate >= pRc->rateMaxPhy && probeAllowed) {
559 rate = pRc->rateMaxPhy;
561 /* Probe the next allowed phy state */
562 /* FIXME: Check to make sure ratMax is checked properly */
563 if (rcGetNextValidTxRate( pRateTable, pRc, rate, &nextRate) &&
564 (nowMsec - pRc->probeTime > pRateTable->probeInterval) &&
565 (pRc->hwMaxRetryPktCnt >= 1))
568 pRc->probeRate = rate;
569 pRc->probeTime = nowMsec;
570 pRc->hwMaxRetryPktCnt = 0;
577 * Make sure rate is not higher than the allowed maximum.
578 * We should also enforce the min, but I suspect the min is
579 * normally 1 rather than 0 because of the rate 9 vs 6 issue
582 if (rate > (pRc->rateTableSize - 1)) {
583 rate = pRc->rateTableSize - 1;
586 /* record selected rate, which is used to decide if we want to do fast frame */
587 if (!(*isProbing) && pSib) {
588 pSib->lastRateKbps = pRateTable->info[rate].rateKbps;
589 ((struct atheros_softc*)sc->sc_rc)->currentTxRateKbps = pSib->lastRateKbps;
590 ((struct atheros_softc*)sc->sc_rc)->currentTxRateIndex = rate;
597 rcRateSetseries(const RATE_TABLE_11N *pRateTable ,
598 struct ath_rc_series *series,
599 A_UINT8 tries, A_UINT8 rix,
600 A_BOOL rtsctsenable, A_UINT32 chainmask,int stbc)
602 series->tries = tries;
603 series->flags = (rtsctsenable? ATH_RC_RTSCTS_FLAG : 0) |
604 (WLAN_RC_PHY_DS(pRateTable->info[rix].phy) ? ATH_RC_DS_FLAG : 0) |
605 (WLAN_RC_PHY_40(pRateTable->info[rix].phy) ? ATH_RC_CW40_FLAG : 0) |
606 (WLAN_RC_PHY_SGI(pRateTable->info[rix].phy) ? ATH_RC_HT40_SGI_FLAG : 0);
609 /* For now, only single stream STBC is supported */
610 if (pRateTable->info[rix].rateCode >= 0x80 &&
611 pRateTable->info[rix].rateCode <= 0x87)
613 series->flags |= ATH_RC_TX_STBC_FLAG;
617 series->rix = pRateTable->info[rix].baseIndex;
618 series->max4msframelen = pRateTable->info[rix].max4msframelen;
619 series->txrateKbps = pRateTable->info[rix].rateKbps;
621 /* If the hardware is capable of multiple transmit chains (chainmask is 3, 5 or 7),
622 * then choose the number of transmit chains dynamically based on entries in the rate table.
624 #ifndef ATH_ENABLE_WLAN_FOR_K2
626 series->tx_chainmask = pRateTable->info[rix].txChainMask_3ch;
627 else if(chainmask == 1)
628 series->tx_chainmask = 1;
630 series->tx_chainmask = pRateTable->info[rix].txChainMask_2ch; /*Chainmask is 3 or 5*/
632 series->tx_chainmask = 1;
637 rcRateGetIndex(struct ath_softc_tgt *sc, struct ath_node_target *an,
638 const RATE_TABLE_11N *pRateTable ,
639 A_UINT8 rix, A_UINT16 stepDown, A_UINT16 minRate)
643 struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
644 TX_RATE_CTRL *pRc = (TX_RATE_CTRL *)(pSib);
647 for (j = RATE_TABLE_11N_SIZE; j > 0; j-- ) {
648 if (rcGetNextLowerValidTxRate(pRateTable, pRc, rix, &nextIndex)) {
655 for (j = stepDown; j > 0; j-- ) {
656 if (rcGetNextLowerValidTxRate(pRateTable, pRc, rix, &nextIndex)) {
667 void rcRateFind_11n(struct ath_softc_tgt *sc, struct ath_node_target *an,
668 int numTries, int numRates, int stepDnInc,
669 unsigned int rcflag, struct ath_rc_series series[], int *isProbe)
672 A_UINT8 tryPerRate = 0;
673 struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
674 RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
675 struct atheros_node *asn = ATH_NODE_ATHEROS(an);
680 rix = rcRateFind_ht(sc, asn, pRateTable, (rcflag & ATH_RC_PROBE_ALLOWED) ? 1 : 0,
684 if ((rcflag & ATH_RC_PROBE_ALLOWED) && (*isProbe)) {
685 /* set one try for probe rates. For the probes don't enable rts */
686 rcRateSetseries(pRateTable, &series[i++], 1, nrix,
687 FALSE, asc->tx_chainmask, asn->stbc);
690 * Get the next tried/allowed rate. No RTS for the next series
691 * after the probe rate
693 nrix = rcRateGetIndex( sc, an, pRateTable, nrix, 1, FALSE);
696 tryPerRate = (numTries/numRates);
698 /* Set the choosen rate. No RTS for first series entry. */
699 rcRateSetseries(pRateTable, &series[i++], tryPerRate,
700 nrix, FALSE, asc->tx_chainmask, asn->stbc);
702 /* Fill in the other rates for multirate retry */
703 for (; i < numRates; i++) {
707 tryNum = ((i + 1) == numRates) ? numTries - (tryPerRate * i) : tryPerRate ;
708 minRate = (((i + 1) == numRates) && (rcflag & ATH_RC_MINRATE_LASTRATE)) ? 1 : 0;
710 nrix = rcRateGetIndex(sc, an, pRateTable, nrix, stepDnInc, minRate);
712 /* All other rates in the series have RTS enabled */
713 rcRateSetseries(pRateTable, &series[i], tryNum,
714 nrix, TRUE, asc->tx_chainmask, asn->stbc);
719 * Change rate series to enable aggregation when operating at lower MCS rates.
720 * When first rate in series is MCS2 in HT40 @ 2.4GHz, series should look like:
721 * {MCS2, MCS1, MCS0, MCS0}.
722 * When first rate in series is MCS3 in HT20 @ 2.4GHz, series should look like:
723 * {MCS3, MCS2, MCS1, MCS1}
724 * So, set fourth rate in series to be same as third one for above conditions.
726 if (sc->sc_curmode == IEEE80211_MODE_11NG) {
727 dot11Rate = pRateTable->info[rix].dot11Rate;
728 phy = pRateTable->info[rix].phy;
730 ((dot11Rate == 2 && phy == WLAN_RC_PHY_HT_40_SS) ||
731 (dot11Rate == 3 && phy == WLAN_RC_PHY_HT_20_SS)))
733 series[3].rix = series[2].rix;
734 series[3].flags = series[2].flags;
735 series[3].max4msframelen = series[2].max4msframelen;
741 * AP91 Kite: NetGear OTA location-4 downlink.
742 * Enable RTS/CTS at MCS 3-0 for downlink throughput.
744 if (sc->sc_curmode == IEEE80211_MODE_11NG) {
745 dot11Rate = pRateTable->info[rix].dot11Rate;
746 if (dot11Rate <= 3 ) {
747 series[0].flags |= ATH_RC_RTSCTS_FLAG;
753 rcUpdate_ht(struct ath_softc_tgt *sc, struct ath_node_target *an, int txRate,
754 A_BOOL Xretries, int retries, A_UINT8 curTxAnt,
755 A_UINT16 nFrames, A_UINT16 nBad)
758 A_UINT32 nowMsec = A_MS_TICKGET();
761 struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
762 struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
763 RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
765 static A_UINT32 nRetry2PerLookup[10] = {
781 pRc = (TX_RATE_CTRL *)(pSib);
783 ASSERT(retries >= 0 && retries < MAX_TX_RETRIES);
790 lastPer = pRc->state[txRate].per;
793 /* Update the PER. */
795 pRc->state[txRate].per += 30;
796 if (pRc->state[txRate].per > 100) {
797 pRc->state[txRate].per = 100;
802 count = sizeof(nRetry2PerLookup) / sizeof(nRetry2PerLookup[0]);
803 if (retries >= count) {
807 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
808 pRc->state[txRate].per = (A_UINT8)(pRc->state[txRate].per -
809 (pRc->state[txRate].per / 8) + ((100) / 8));
812 /* Xretries == 1 or 2 */
814 if (pRc->probeRate == txRate)
820 * Update the PER. Make sure it doesn't index out of array's bounds.
822 count = sizeof(nRetry2PerLookup) / sizeof(nRetry2PerLookup[0]);
823 if (retries >= count) {
828 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
830 * Assuming that nFrames is not 0. The current PER
831 * from the retries is 100 * retries / (retries+1),
832 * since the first retries attempts failed, and the
833 * next one worked. For the one that worked, nBad
834 * subframes out of nFrames wored, so the PER for
835 * that part is 100 * nBad / nFrames, and it contributes
836 * 100 * nBad / (nFrames * (retries+1)) to the above
837 * PER. The expression below is a simplified version
838 * of the sum of these two terms.
841 pRc->state[txRate].per = (A_UINT8)(pRc->state[txRate].per -
842 (pRc->state[txRate].per / 8) +
843 ((100*(retries*nFrames + nBad)/(nFrames*(retries+1))) / 8));
845 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
847 pRc->state[txRate].per = (A_UINT8)(pRc->state[txRate].per -
848 (pRc->state[txRate].per / 8) + (nRetry2PerLookup[retries] / 8));
852 * If we got at most one retry then increase the max rate if
853 * this was a probe. Otherwise, ignore the probe.
856 if (pRc->probeRate && pRc->probeRate == txRate) {
857 if (retries > 0 || 2 * nBad > nFrames) {
859 * Since we probed with just a single attempt,
860 * any retries means the probe failed. Also,
861 * if the attempt worked, but more than half
862 * the subframes were bad then also consider
863 * the probe a failure.
867 pRc->rateMaxPhy = pRc->probeRate;
869 if (pRc->state[pRc->probeRate].per > 30) {
870 pRc->state[pRc->probeRate].per = 20;
876 * Since this probe succeeded, we allow the next probe
877 * twice as soon. This allows the maxRate to move up
878 * faster if the probes are succesful.
880 pRc->probeTime = nowMsec - pRateTable->probeInterval / 2;
886 * Don't update anything. We don't know if this was because
887 * of collisions or poor signal.
889 * Later: if rssiAck is close to pRc->state[txRate].rssiThres
890 * and we see lots of retries, then we could increase
891 * pRc->state[txRate].rssiThres.
893 pRc->hwMaxRetryPktCnt = 0;
896 * It worked with no retries. First ignore bogus (small)
899 if (txRate == pRc->rateMaxPhy && pRc->hwMaxRetryPktCnt < 255) {
900 pRc->hwMaxRetryPktCnt++;
908 ASSERT((pRc->rateMaxPhy >= 0 && pRc->rateMaxPhy <= pRc->rateTableSize &&
909 pRc->rateMaxPhy != INVALID_RATE_MAX));
912 * If this rate looks bad (high PER) then stop using it for
913 * a while (except if we are probing).
915 if (pRc->state[txRate].per >= 55 && txRate > 0 &&
916 pRateTable->info[txRate].rateKbps <=
917 pRateTable->info[pRc->rateMaxPhy].rateKbps)
919 rcGetNextLowerValidTxRate(pRateTable, pRc, (A_UINT8) txRate,
922 /* Don't probe for a little while. */
923 pRc->probeTime = nowMsec;
926 /* Make sure the rates below this have lower PER */
927 /* Monotonicity is kept only for rates below the current rate. */
928 if (pRc->state[txRate].per < lastPer) {
929 for (rate = txRate - 1; rate >= 0; rate--) {
930 if (pRateTable->info[rate].phy != pRateTable->info[txRate].phy) {
934 if (pRc->state[rate].per > pRc->state[rate+1].per) {
935 pRc->state[rate].per = pRc->state[rate+1].per;
940 /* Maintain monotonicity for rates above the current rate*/
941 for (rate = txRate; rate < pRc->rateTableSize - 1; rate++) {
942 if (pRc->state[rate+1].per < pRc->state[rate].per) {
943 pRc->state[rate+1].per = pRc->state[rate].per;
947 /* Every so often, we reduce the thresholds and PER (different for CCK and OFDM). */
948 if (nowMsec - pRc->perDownTime >= pRateTable->rssiReduceInterval) {
949 for (rate = 0; rate < pRc->rateTableSize; rate++) {
950 pRc->state[rate].per = 7*pRc->state[rate].per/8;
953 pRc->perDownTime = nowMsec;
958 * This routine is called by the Tx interrupt service routine to give
959 * the status of previous frames.
961 void rcUpdate_11n(struct ath_softc_tgt *sc, struct ath_node_target *an,
963 int finalTSIdx, int Xretries,
964 struct ath_rc_series rcs[], int nFrames,
965 int nBad, int long_retry)
969 struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
970 RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
971 struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
972 TX_RATE_CTRL *pRc = (TX_RATE_CTRL *)(pSib);
980 ASSERT (rcs[0].tries != 0);
983 * If the first rate is not the final index, there are intermediate rate failures
986 if (finalTSIdx != 0) {
988 /* Process intermediate rates that failed.*/
989 for (series = 0; series < finalTSIdx ; series++) {
990 if (rcs[series].tries != 0) {
991 flags = rcs[series].flags;
992 /* If HT40 and we have switched mode from 40 to 20 => don't update */
993 if ((flags & ATH_RC_CW40_FLAG) &&
994 (pRc->rcPhyMode != (flags & ATH_RC_CW40_FLAG))) {
997 if ((flags & ATH_RC_CW40_FLAG) && (flags & ATH_RC_HT40_SGI_FLAG)) {
998 rix = pRateTable->info[rcs[series].rix].htIndex;
999 } else if (flags & ATH_RC_HT40_SGI_FLAG) {
1000 rix = pRateTable->info[rcs[series].rix].sgiIndex;
1001 } else if (flags & ATH_RC_CW40_FLAG) {
1002 rix = pRateTable->info[rcs[series].rix].cw40Index;
1004 rix = pRateTable->info[rcs[series].rix].baseIndex;
1007 /* FIXME:XXXX, too many args! */
1008 rcUpdate_ht(sc, an, rix, Xretries? 1 : 2, rcs[series].tries,
1009 curTxAnt, nFrames, nFrames);
1014 * Handle the special case of MIMO PS burst, where the second aggregate is sent
1015 * out with only one rate and one try. Treating it as an excessive retry penalizes
1016 * the rate inordinately.
1018 if (rcs[0].tries == 1 && Xretries == 1) {
1023 flags = rcs[series].flags;
1024 /* If HT40 and we have switched mode from 40 to 20 => don't update */
1025 if ((flags & ATH_RC_CW40_FLAG) &&
1026 (pRc->rcPhyMode != (flags & ATH_RC_CW40_FLAG))) {
1029 if ((flags & ATH_RC_CW40_FLAG) && (flags & ATH_RC_HT40_SGI_FLAG)) {
1030 rix = pRateTable->info[rcs[series].rix].htIndex;
1031 } else if (flags & ATH_RC_HT40_SGI_FLAG) {
1032 rix = pRateTable->info[rcs[series].rix].sgiIndex;
1033 } else if (flags & ATH_RC_CW40_FLAG) {
1034 rix = pRateTable->info[rcs[series].rix].cw40Index;
1036 rix = pRateTable->info[rcs[series].rix].baseIndex;
1039 /* FIXME:XXXX, too many args! */
1040 rcUpdate_ht(sc, an, rix, Xretries, long_retry, curTxAnt,
1044 void ath_tx_status_update_rate(struct ath_softc_tgt *sc,
1045 struct ath_rc_series rcs[],
1047 WMI_TXSTATUS_EVENT *txs)
1049 struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
1050 RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
1053 if (pRateTable->info[rcs[series].rix].rateCode & 0x80) {
1054 txs->txstatus[txs->cnt].ts_rate |= SM(pRateTable->info[rcs[series].rix].dot11Rate,
1055 ATH9K_HTC_TXSTAT_RATE);
1056 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_MCS;
1058 if (rcs[series].flags & ATH_RC_CW40_FLAG)
1059 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_CW40;
1061 if (rcs[series].flags & ATH_RC_HT40_SGI_FLAG)
1062 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_SGI;
1065 txs->txstatus[txs->cnt].ts_rate |= SM(rcs[series].rix, ATH9K_HTC_TXSTAT_RATE);
1068 if (rcs[series].flags & ATH_RC_RTSCTS_FLAG)
1069 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_RTC_CTS;
1073 struct ath_ratectrl *
1074 ath_rate_attach(struct ath_softc_tgt *sc)
1076 struct atheros_softc *asc;
1078 asc = adf_os_mem_alloc(sizeof(struct atheros_softc));
1082 adf_os_mem_set(asc, 0, sizeof(struct atheros_softc));
1083 asc->arc.arc_space = sizeof(struct atheros_node);
1085 ar5416AttachRateTables(asc);
1087 asc->tx_chainmask = 1;
1093 ath_rate_detach(struct ath_ratectrl *rc)
1095 adf_os_mem_free(rc);
1099 ath_rate_findrate(struct ath_softc_tgt *sc,
1100 struct ath_node_target *an,
1106 unsigned int rcflag,
1107 struct ath_rc_series series[],
1112 if (!numRates || !numTries) {
1116 ath_rate_findrate_11n(sc, an, frameLen, numTries, numRates, stepDnInc,
1117 rcflag, series, isProbe);
1120 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
1123 ath_rate_tx_complete(struct ath_softc_tgt *sc,
1124 struct ath_node_target *an,
1125 struct ath_tx_desc *ds,
1126 struct ath_rc_series rcs[],
1127 int nframes, int nbad)
1129 ath_rate_tx_complete_11n(sc, an, ds, rcs, nframes, nbad);
1133 ath_rate_newassoc(struct ath_softc_tgt *sc, struct ath_node_target *an, int isnew,
1134 unsigned int capflag, struct ieee80211_rate *rs)
1136 ath_rate_newassoc_11n(sc, an, isnew, capflag, rs);
1139 void ath_rate_node_update(struct ath_softc_tgt *sc,
1140 struct ath_node_target *an,
1143 struct ieee80211_rate *rs)
1145 struct ieee80211_node_target *ni = &an->ni;
1147 ath_rate_newassoc(sc, ATH_NODE_TARGET(ni), isnew, capflag, rs);
1150 static int init_ath_rate_atheros(void);
1151 static void exit_ath_rate_atheros(void);
1154 ath_rate_newstate(struct ath_softc_tgt *sc,
1155 struct ieee80211vap_target *vap,
1156 enum ieee80211_state state,
1158 struct ieee80211_rate *rs)
1160 struct ieee80211_node_target *ni = vap->iv_bss;
1161 struct atheros_softc *asc = (struct atheros_softc *) sc->sc_rc;
1163 asc->tx_chainmask = sc->sc_ic.ic_tx_chainmask;
1164 ath_rate_newassoc(sc, ni, 1, capflag, rs);
1168 ath_rate_findrate_11n(struct ath_softc_tgt *sc,
1169 struct ath_node_target *an,
1174 unsigned int rcflag,
1175 struct ath_rc_series series[],
1179 if (!numRates || !numTries) {
1183 rcRateFind_11n(sc, an, numTries, numRates, stepDnInc, rcflag, series, isProbe);
1187 ath_rate_tx_complete_11n(struct ath_softc_tgt *sc,
1188 struct ath_node_target *an,
1189 struct ath_tx_desc *ds,
1190 struct ath_rc_series rcs[],
1191 int nframes, int nbad)
1193 int finalTSIdx = ds->ds_txstat.ts_rate;
1196 if ((ds->ds_txstat.ts_status & HAL_TXERR_XRETRY) ||
1197 (ds->ds_txstat.ts_status & HAL_TXERR_FIFO) ||
1198 (ds->ds_txstat.ts_flags & HAL_TX_DATA_UNDERRUN) ||
1199 (ds->ds_txstat.ts_flags & HAL_TX_DELIM_UNDERRUN)) {
1203 rcUpdate_11n(sc, an,
1204 ds->ds_txstat.ts_antenna, finalTSIdx,
1205 tx_status, rcs, nframes , nbad,
1206 ds->ds_txstat.ts_longretry);
1210 ath_rate_newassoc_11n(struct ath_softc_tgt *sc, struct ath_node_target *an, int isnew,
1211 unsigned int capflag, struct ieee80211_rate *rs)
1214 struct atheros_node *oan = ATH_NODE_ATHEROS(an);
1216 oan->htcap = ((capflag & ATH_RC_DS_FLAG) ? WLAN_RC_DS_FLAG : 0) |
1217 ((capflag & ATH_RC_HT40_SGI_FLAG) ? WLAN_RC_HT40_SGI_FLAG : 0) |
1218 ((capflag & ATH_RC_HT_FLAG) ? WLAN_RC_HT_FLAG : 0) |
1219 ((capflag & ATH_RC_CW40_FLAG) ? WLAN_RC_40_FLAG : 0) |
1220 ((capflag & ATH_RC_WEP_TKIP_FLAG) ? WLAN_RC_WEP_TKIP_FLAG : 0);
1222 #ifdef MAGPIE_MERLIN
1223 /* Rx STBC is a 2-bit mask. Needs to convert from ath definition to wlan definition. */
1225 oan->htcap |= (((capflag & ATH_RC_RX_STBC_FLAG) >> ATH_RC_RX_STBC_FLAG_S)
1226 << WLAN_RC_STBC_FLAG_S);
1228 /* If only one chain is enabled, do not do stbc. */
1229 if (sc->sc_txstbcsupport) {
1230 oan->stbc = (capflag & ATH_RC_RX_STBC_FLAG) >> ATH_RC_RX_STBC_FLAG_S;
1236 rcSibUpdate_11n(sc, an, oan->htcap, 0, rs);
1240 void ath_rate_mcs2rate(struct ath_softc_tgt *sc,a_uint8_t sgi, a_uint8_t ht40,
1241 a_uint8_t rateCode, a_uint32_t *txrate, a_uint32_t *rxrate)
1244 struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
1245 RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
1246 a_uint32_t rateKbps = 0;
1248 *txrate = asc->currentTxRateKbps;
1250 /* look 11NA table for rateKbps*/
1251 for (idx = 0; idx < pRateTable->rateCount && !rateKbps; ++idx) {
1252 if (pRateTable->info[idx].rateCode == rateCode) {
1254 if(pRateTable->info[idx].valid == TRUE_40 &&
1255 pRateTable->info[idx].phy == WLAN_RC_PHY_HT_40_DS_HGI)
1256 rateKbps = pRateTable->info[idx].rateKbps;
1258 if (pRateTable->info[idx].valid == TRUE_40)/* HT40 only*/
1259 rateKbps = pRateTable->info[idx].rateKbps;
1261 if (pRateTable->info[idx].valid != FALSE)
1262 rateKbps = pRateTable->info[idx].rateKbps;