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>
51 #include <ieee80211_var.h>
53 #include <if_athvar.h>
57 #include "ratectrl11n.h"
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);
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,
68 static void ath_rate_findrate_11n(struct ath_softc_tgt *sc,
69 struct ath_node_target *an,
75 struct ath_rc_series series[],
79 rcSortValidRates(const RATE_TABLE_11N *pRateTable, TX_RATE_CTRL *pRc)
83 for (i=pRc->maxValidRate-1; i > 0; i--) {
84 for (j=0; j <= i-1; j++) {
86 if (pRateTable->info[pRc->validRateIndex[j]].rateKbps >
87 pRateTable->info[pRc->validRateIndex[j+1]].rateKbps)
90 if (pRateTable->info[pRc->validRateIndex[j]].userRateKbps >
91 pRateTable->info[pRc->validRateIndex[j+1]].userRateKbps)
95 tmp = pRc->validRateIndex[j];
96 pRc->validRateIndex[j] = pRc->validRateIndex[j+1];
97 pRc->validRateIndex[j+1] = tmp;
103 /* Access functions for validTxRateMask */
106 rcInitValidTxMask(TX_RATE_CTRL *pRc)
110 for (i = 0; i < pRc->rateTableSize; i++) {
111 pRc->validRateIndex[i] = FALSE;
116 rcSetValidTxMask(TX_RATE_CTRL *pRc, A_UINT8 index, A_BOOL validTxRate)
118 ASSERT(index < pRc->rateTableSize);
119 pRc->validRateIndex[index] = validTxRate ? TRUE : FALSE;
123 /* Iterators for validTxRateMask */
125 rcGetNextValidTxRate(const RATE_TABLE_11N *pRateTable, TX_RATE_CTRL *pRc,
126 A_UINT8 curValidTxRate, A_UINT8 *pNextIndex)
130 for (i = 0; i < pRc->maxValidRate-1; i++) {
131 if (pRc->validRateIndex[i] == curValidTxRate) {
132 *pNextIndex = pRc->validRateIndex[i+1];
137 /* No more valid rates */
144 rcGetNextLowerValidTxRate(const RATE_TABLE_11N *pRateTable, TX_RATE_CTRL *pRc,
145 A_UINT8 curValidTxRate, A_UINT8 *pNextIndex)
149 for (i = 1; i < pRc->maxValidRate ; i++) {
150 if (pRc->validRateIndex[i] == curValidTxRate) {
151 *pNextIndex = pRc->validRateIndex[i-1];
159 /* Return true only for single stream */
162 rcIsValidPhyRate(A_UINT32 phy, A_UINT32 capflag, A_BOOL ignoreCW)
164 if (WLAN_RC_PHY_HT(phy) && !(capflag & WLAN_RC_HT_FLAG)) {
168 if (WLAN_RC_PHY_DS(phy) && !(capflag & WLAN_RC_DS_FLAG)) {
171 if (WLAN_RC_PHY_SGI(phy) && !(capflag & WLAN_RC_HT40_SGI_FLAG)) {
175 if (!ignoreCW && WLAN_RC_PHY_HT(phy)) {
176 if (WLAN_RC_PHY_40(phy) && !(capflag & WLAN_RC_40_FLAG)) {
180 if (!WLAN_RC_PHY_40(phy) && (capflag & WLAN_RC_40_FLAG)) {
189 * Initialize the Valid Rate Index from valid entries in Rate Table
191 static A_UINT8 rcSibInitValidRates(const RATE_TABLE_11N *pRateTable,
194 PHY_STATE_CTRL *pPhyStateCtrl)
197 A_UINT8 singleStream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
200 for (i = 0; i < pRateTable->rateCount; i++) {
202 valid = pRateTable->info[i].validSingleStream;
204 valid = pRateTable->info[i].valid;
208 A_UINT32 phy = pRateTable->info[i].phy;
210 if (!rcIsValidPhyRate(phy, capflag, FALSE))
213 pPhyStateCtrl->validPhyRateIndex[phy][pPhyStateCtrl->validPhyRateCount[phy]] = i;
214 pPhyStateCtrl->validPhyRateCount[phy] += 1;
216 rcSetValidTxMask(pRc, i, TRUE);
226 * Initialize the Valid Rate Index from Rate Set
229 rcSibSetValidRates(const RATE_TABLE_11N *pRateTable,
231 struct ieee80211_rateset *pRateSet,
233 struct ath_node_target *an,
234 PHY_STATE_CTRL *pPhyStateCtrl)
236 A_UINT8 i, j, hi = 0;
237 A_UINT8 singleStream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
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;
245 struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
248 valid = pRateTable->info[j].validSTBC;
249 } else if (singleStream) {
253 valid = pRateTable->info[j].validSingleStream;
255 valid = pRateTable->info[j].valid;
259 * We allow a rate only if its valid and the capflag matches one of
260 * the validity (TRUE/TRUE_20/TRUE_40) flags
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))
270 pPhyStateCtrl->validPhyRateIndex[phy][pPhyStateCtrl->validPhyRateCount[phy]] = j;
271 pPhyStateCtrl->validPhyRateCount[phy] += 1;
273 rcSetValidTxMask(pRc, j, TRUE);
283 rcSibSetValidHtRates(const RATE_TABLE_11N *pRateTable,
287 struct ath_node_target *an,
288 PHY_STATE_CTRL *pPhyStateCtrl)
290 A_UINT8 i, j, hi = 0;
291 A_UINT8 singleStream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
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;
299 struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
302 valid = pRateTable->info[j].validSTBC;
303 } else if (singleStream) {
307 valid = pRateTable->info[j].validSingleStream;
309 valid = pRateTable->info[j].valid;
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) &&
318 (capflag & WLAN_RC_WEP_TKIP_FLAG)) )
323 if (!rcIsValidPhyRate(phy, capflag, FALSE))
326 pPhyStateCtrl->validPhyRateIndex[phy][pPhyStateCtrl->validPhyRateCount[phy]] = j;
327 pPhyStateCtrl->validPhyRateCount[phy] += 1;
329 rcSetValidTxMask(pRc, j, TRUE);
338 * Update the SIB's rate control information
340 * This should be called when the supported rates change
341 * (e.g. SME operation, wireless mode change)
343 * It will determine which rates are valid for use.
346 rcSibUpdate_ht(struct ath_softc_tgt *sc, struct ath_node_target *an,
347 A_UINT32 capflag, A_BOOL keepState, struct ieee80211_rate *pRateSet)
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;
356 A_UINT8 i, j, k, hi = 0, htHi = 0;
358 pRateTable = (RATE_TABLE_11N*)asc->hwRateTable[sc->sc_curmode];
360 /* Initial rate table size. Will change depending on the working rate set */
361 pRc->rateTableSize = MAX_TX_RATE_TBL;
363 /* Initialize thresholds according to the global rate table */
364 for (i = 0 ; (i < pRc->rateTableSize) && (!keepState); i++) {
365 pRc->state[i].per = 0;
368 /* Determine the valid rates */
369 rcInitValidTxMask(pRc);
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;
375 mPhyCtrlState.validPhyRateCount[i] = 0;
378 pRc->rcPhyMode = (capflag & WLAN_RC_40_FLAG);
380 if (pRateSet == NULL || !pRateSet->rates.rs_nrates) {
381 /* No working rate, just initialize valid rates */
382 hi = rcSibInitValidRates(pRateTable, pRc, capflag, &mPhyCtrlState);
384 /* Use intersection of working rates and valid rates */
385 hi = rcSibSetValidRates(pRateTable, pRc, &(pRateSet->rates),
386 capflag, an, &mPhyCtrlState);
388 if (capflag & WLAN_RC_HT_FLAG) {
389 htHi = rcSibSetValidHtRates(pRateTable, pRc, phtMcs,
390 capflag, an, &mPhyCtrlState);
393 hi = A_MAX(hi, htHi);
396 pRc->rateTableSize = hi + 1;
399 ASSERT(pRc->rateTableSize <= MAX_TX_RATE_TBL);
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];
406 if (!rcIsValidPhyRate(i, pRateTable->initialRateMax, TRUE) ||
407 !mPhyCtrlState.validPhyRateCount[i])
410 pRc->rateMaxPhy = mPhyCtrlState.validPhyRateIndex[i][j-1];
413 ASSERT(pRc->rateTableSize <= MAX_TX_RATE_TBL);
414 ASSERT(k <= MAX_TX_RATE_TBL);
416 pRc->rateMaxPhy = pRc->validRateIndex[k-4];
417 pRc->maxValidRate = k;
419 rcSortValidRates(pRateTable, pRc);
423 rcRateFind_ht(struct ath_softc_tgt *sc, struct atheros_node *pSib,
424 const RATE_TABLE_11N *pRateTable, A_BOOL probeAllowed, A_BOOL *isProbing)
427 A_UINT32 bestThruput, thisThruput;
429 A_UINT8 rate, nextRate, bestRate;
430 A_UINT8 maxIndex, minIndex;
432 TX_RATE_CTRL *pRc = NULL;
434 pRc = (TX_RATE_CTRL *)(pSib ? (pSib) : NULL);
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
447 nowMsec = A_MS_TICKGET();
448 dt = nowMsec - pRc->rssiTime;
451 * Now look up the rate in the rssi table and return it.
452 * If no rates match then we return 0 (lowest rate)
456 maxIndex = pRc->maxValidRate-1;
462 * Try the higher rate first. It will reduce memory moving time
463 * if we have very good channel characteristics.
465 for (index = maxIndex; index >= minIndex ; index--) {
468 rate = pRc->validRateIndex[index];
469 if (rate > pRc->rateMaxPhy) {
473 /* if the best throughput is already larger than the userRateKbps..
474 * then we could skip of rest of calculation..
476 if( bestThruput >= pRateTable->info[rate].userRateKbps)
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.
490 perThres = pRc->state[rate].per;
491 if ( perThres < 12 ) {
495 thisThruput = pRateTable->info[rate].userRateKbps * (100 - perThres);
496 if (bestThruput <= thisThruput) {
497 bestThruput = thisThruput;
505 * Must check the actual rate (rateKbps) to account for non-monoticity of
509 if (rate >= pRc->rateMaxPhy && probeAllowed) {
510 rate = pRc->rateMaxPhy;
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))
519 pRc->probeRate = rate;
520 pRc->probeTime = nowMsec;
521 pRc->hwMaxRetryPktCnt = 0;
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
533 if (rate > (pRc->rateTableSize - 1)) {
534 rate = pRc->rateTableSize - 1;
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;
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)
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);
560 /* For now, only single stream STBC is supported */
561 if (pRateTable->info[rix].rateCode >= 0x80 &&
562 pRateTable->info[rix].rateCode <= 0x87)
564 series->flags |= ATH_RC_TX_STBC_FLAG;
568 series->rix = pRateTable->info[rix].baseIndex;
569 series->max4msframelen = pRateTable->info[rix].max4msframelen;
570 series->txrateKbps = pRateTable->info[rix].rateKbps;
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.
575 #ifndef ATH_ENABLE_WLAN_FOR_K2
577 series->tx_chainmask = pRateTable->info[rix].txChainMask_3ch;
578 else if(chainmask == 1)
579 series->tx_chainmask = 1;
581 series->tx_chainmask = pRateTable->info[rix].txChainMask_2ch; /*Chainmask is 3 or 5*/
583 series->tx_chainmask = 1;
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)
594 struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
595 TX_RATE_CTRL *pRc = (TX_RATE_CTRL *)(pSib);
598 for (j = RATE_TABLE_11N_SIZE; j > 0; j-- ) {
599 if (rcGetNextLowerValidTxRate(pRateTable, pRc, rix, &nextIndex)) {
606 for (j = stepDown; j > 0; j-- ) {
607 if (rcGetNextLowerValidTxRate(pRateTable, pRc, rix, &nextIndex)) {
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)
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);
631 rix = rcRateFind_ht(sc, asn, pRateTable, (rcflag & ATH_RC_PROBE_ALLOWED) ? 1 : 0,
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);
641 * Get the next tried/allowed rate. No RTS for the next series
642 * after the probe rate
644 nrix = rcRateGetIndex( sc, an, pRateTable, nrix, 1, FALSE);
647 tryPerRate = (numTries/numRates);
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);
653 /* Fill in the other rates for multirate retry */
654 for (; i < numRates; i++) {
658 tryNum = ((i + 1) == numRates) ? numTries - (tryPerRate * i) : tryPerRate ;
659 minRate = (((i + 1) == numRates) && (rcflag & ATH_RC_MINRATE_LASTRATE)) ? 1 : 0;
661 nrix = rcRateGetIndex(sc, an, pRateTable, nrix, stepDnInc, minRate);
663 /* All other rates in the series have RTS enabled */
664 rcRateSetseries(pRateTable, &series[i], tryNum,
665 nrix, TRUE, asc->tx_chainmask, asn->stbc);
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.
677 if (sc->sc_curmode == IEEE80211_MODE_11NG) {
678 dot11Rate = pRateTable->info[rix].dot11Rate;
679 phy = pRateTable->info[rix].phy;
681 ((dot11Rate == 2 && phy == WLAN_RC_PHY_HT_40_SS) ||
682 (dot11Rate == 3 && phy == WLAN_RC_PHY_HT_20_SS)))
684 series[3].rix = series[2].rix;
685 series[3].flags = series[2].flags;
686 series[3].max4msframelen = series[2].max4msframelen;
692 * AP91 Kite: NetGear OTA location-4 downlink.
693 * Enable RTS/CTS at MCS 3-0 for downlink throughput.
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;
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)
709 A_UINT32 nowMsec = A_MS_TICKGET();
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];
716 static A_UINT32 nRetry2PerLookup[10] = {
732 pRc = (TX_RATE_CTRL *)(pSib);
734 ASSERT(retries >= 0 && retries < MAX_TX_RETRIES);
741 lastPer = pRc->state[txRate].per;
744 /* Update the PER. */
746 pRc->state[txRate].per += 30;
747 if (pRc->state[txRate].per > 100) {
748 pRc->state[txRate].per = 100;
753 count = sizeof(nRetry2PerLookup) / sizeof(nRetry2PerLookup[0]);
754 if (retries >= count) {
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));
763 /* Xretries == 1 or 2 */
765 if (pRc->probeRate == txRate)
771 * Update the PER. Make sure it doesn't index out of array's bounds.
773 count = sizeof(nRetry2PerLookup) / sizeof(nRetry2PerLookup[0]);
774 if (retries >= count) {
779 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
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.
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));
796 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
798 pRc->state[txRate].per = (A_UINT8)(pRc->state[txRate].per -
799 (pRc->state[txRate].per / 8) + (nRetry2PerLookup[retries] / 8));
803 * If we got at most one retry then increase the max rate if
804 * this was a probe. Otherwise, ignore the probe.
807 if (pRc->probeRate && pRc->probeRate == txRate) {
808 if (retries > 0 || 2 * nBad > nFrames) {
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.
818 pRc->rateMaxPhy = pRc->probeRate;
820 if (pRc->state[pRc->probeRate].per > 30) {
821 pRc->state[pRc->probeRate].per = 20;
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.
831 pRc->probeTime = nowMsec - pRateTable->probeInterval / 2;
837 * Don't update anything. We don't know if this was because
838 * of collisions or poor signal.
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.
844 pRc->hwMaxRetryPktCnt = 0;
847 * It worked with no retries. First ignore bogus (small)
850 if (txRate == pRc->rateMaxPhy && pRc->hwMaxRetryPktCnt < 255) {
851 pRc->hwMaxRetryPktCnt++;
859 ASSERT((pRc->rateMaxPhy >= 0 && pRc->rateMaxPhy <= pRc->rateTableSize &&
860 pRc->rateMaxPhy != INVALID_RATE_MAX));
863 * If this rate looks bad (high PER) then stop using it for
864 * a while (except if we are probing).
866 if (pRc->state[txRate].per >= 55 && txRate > 0 &&
867 pRateTable->info[txRate].rateKbps <=
868 pRateTable->info[pRc->rateMaxPhy].rateKbps)
870 rcGetNextLowerValidTxRate(pRateTable, pRc, (A_UINT8) txRate,
873 /* Don't probe for a little while. */
874 pRc->probeTime = nowMsec;
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) {
885 if (pRc->state[rate].per > pRc->state[rate+1].per) {
886 pRc->state[rate].per = pRc->state[rate+1].per;
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;
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;
904 pRc->perDownTime = nowMsec;
909 * This routine is called by the Tx interrupt service routine to give
910 * the status of previous frames.
912 void rcUpdate_11n(struct ath_softc_tgt *sc, struct ath_node_target *an,
914 int finalTSIdx, int Xretries,
915 struct ath_rc_series rcs[], int nFrames,
916 int nBad, int long_retry)
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);
931 ASSERT (rcs[0].tries != 0);
934 * If the first rate is not the final index, there are intermediate rate failures
937 if (finalTSIdx != 0) {
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))) {
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;
955 rix = pRateTable->info[rcs[series].rix].baseIndex;
958 /* FIXME:XXXX, too many args! */
959 rcUpdate_ht(sc, an, rix, Xretries? 1 : 2, rcs[series].tries,
960 curTxAnt, nFrames, nFrames);
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.
969 if (rcs[0].tries == 1 && Xretries == 1) {
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))) {
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;
987 rix = pRateTable->info[rcs[series].rix].baseIndex;
990 /* FIXME:XXXX, too many args! */
991 rcUpdate_ht(sc, an, rix, Xretries, long_retry, curTxAnt,
995 void ath_tx_status_update_rate(struct ath_softc_tgt *sc,
996 struct ath_rc_series rcs[],
998 WMI_TXSTATUS_EVENT *txs)
1000 struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
1001 RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
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;
1009 if (rcs[series].flags & ATH_RC_CW40_FLAG)
1010 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_CW40;
1012 if (rcs[series].flags & ATH_RC_HT40_SGI_FLAG)
1013 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_SGI;
1016 txs->txstatus[txs->cnt].ts_rate |= SM(rcs[series].rix, ATH9K_HTC_TXSTAT_RATE);
1019 if (rcs[series].flags & ATH_RC_RTSCTS_FLAG)
1020 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_RTC_CTS;
1024 struct ath_ratectrl *
1025 ath_rate_attach(struct ath_softc_tgt *sc)
1027 struct atheros_softc *asc;
1029 asc = adf_os_mem_alloc(sizeof(struct atheros_softc));
1033 adf_os_mem_set(asc, 0, sizeof(struct atheros_softc));
1034 asc->arc.arc_space = sizeof(struct atheros_node);
1036 ar5416AttachRateTables(asc);
1038 asc->tx_chainmask = 1;
1044 ath_rate_findrate(struct ath_softc_tgt *sc,
1045 struct ath_node_target *an,
1051 unsigned int rcflag,
1052 struct ath_rc_series series[],
1057 if (!numRates || !numTries) {
1061 ath_rate_findrate_11n(sc, an, frameLen, numTries, numRates, stepDnInc,
1062 rcflag, series, isProbe);
1065 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
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)
1074 ath_rate_tx_complete_11n(sc, an, ds, rcs, nframes, nbad);
1078 ath_rate_newassoc(struct ath_softc_tgt *sc, struct ath_node_target *an, int isnew,
1079 unsigned int capflag, struct ieee80211_rate *rs)
1081 ath_rate_newassoc_11n(sc, an, isnew, capflag, rs);
1084 void ath_rate_node_update(struct ath_softc_tgt *sc,
1085 struct ath_node_target *an,
1088 struct ieee80211_rate *rs)
1090 struct ieee80211_node_target *ni = &an->ni;
1092 ath_rate_newassoc(sc, ATH_NODE_TARGET(ni), isnew, capflag, rs);
1095 static int init_ath_rate_atheros(void);
1096 static void exit_ath_rate_atheros(void);
1099 ath_rate_newstate(struct ath_softc_tgt *sc,
1100 struct ieee80211vap_target *vap,
1101 enum ieee80211_state state,
1103 struct ieee80211_rate *rs)
1105 struct ieee80211_node_target *ni = vap->iv_bss;
1106 struct atheros_softc *asc = (struct atheros_softc *) sc->sc_rc;
1108 asc->tx_chainmask = sc->sc_ic.ic_tx_chainmask;
1109 ath_rate_newassoc(sc, ATH_NODE_TARGET(ni), 1, capflag, rs);
1113 ath_rate_findrate_11n(struct ath_softc_tgt *sc,
1114 struct ath_node_target *an,
1119 unsigned int rcflag,
1120 struct ath_rc_series series[],
1124 if (!numRates || !numTries) {
1128 rcRateFind_11n(sc, an, numTries, numRates, stepDnInc, rcflag, series, isProbe);
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)
1138 int finalTSIdx = ds->ds_txstat.ts_rate;
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)) {
1148 rcUpdate_11n(sc, an,
1149 ds->ds_txstat.ts_antenna, finalTSIdx,
1150 tx_status, rcs, nframes , nbad,
1151 ds->ds_txstat.ts_longretry);
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)
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;
1164 rcSibUpdate_ht(sc, an, capflag, 0, rs);