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 INLINE A_RSSI median(A_RSSI a, A_RSSI b, A_RSSI c);
61 static void ath_rate_newassoc_11n(struct ath_softc_tgt *sc, struct ath_node_target *an, int isnew,
62 unsigned int capflag, struct ieee80211_rate *rs);
65 static void ath_rate_tx_complete_11n(struct ath_softc_tgt *sc, struct ath_node_target *an,
66 struct ath_tx_desc *ds,
67 struct ath_rc_series rcs[], int nframes,
70 static void ath_rate_findrate_11n(struct ath_softc_tgt *sc,
71 struct ath_node_target *an,
77 struct ath_rc_series series[],
81 rcSortValidRates(const RATE_TABLE_11N *pRateTable, TX_RATE_CTRL *pRc)
85 for (i=pRc->maxValidRate-1; i > 0; i--) {
86 for (j=0; j <= i-1; j++) {
88 if (pRateTable->info[pRc->validRateIndex[j]].rateKbps >
89 pRateTable->info[pRc->validRateIndex[j+1]].rateKbps)
92 if (pRateTable->info[pRc->validRateIndex[j]].userRateKbps >
93 pRateTable->info[pRc->validRateIndex[j+1]].userRateKbps)
97 tmp = pRc->validRateIndex[j];
98 pRc->validRateIndex[j] = pRc->validRateIndex[j+1];
99 pRc->validRateIndex[j+1] = tmp;
105 /* Access functions for validTxRateMask */
108 rcInitValidTxMask(TX_RATE_CTRL *pRc)
112 for (i = 0; i < pRc->rateTableSize; i++) {
113 pRc->validRateIndex[i] = FALSE;
118 rcSetValidTxMask(TX_RATE_CTRL *pRc, A_UINT8 index, A_BOOL validTxRate)
120 ASSERT(index < pRc->rateTableSize);
121 pRc->validRateIndex[index] = validTxRate ? TRUE : FALSE;
125 /* Iterators for validTxRateMask */
127 rcGetNextValidTxRate(const RATE_TABLE_11N *pRateTable, TX_RATE_CTRL *pRc,
128 A_UINT8 curValidTxRate, A_UINT8 *pNextIndex)
132 for (i = 0; i < pRc->maxValidRate-1; i++) {
133 if (pRc->validRateIndex[i] == curValidTxRate) {
134 *pNextIndex = pRc->validRateIndex[i+1];
139 /* No more valid rates */
146 rcGetNextLowerValidTxRate(const RATE_TABLE_11N *pRateTable, TX_RATE_CTRL *pRc,
147 A_UINT8 curValidTxRate, A_UINT8 *pNextIndex)
151 for (i = 1; i < pRc->maxValidRate ; i++) {
152 if (pRc->validRateIndex[i] == curValidTxRate) {
153 *pNextIndex = pRc->validRateIndex[i-1];
161 /* Return true only for single stream */
164 rcIsValidPhyRate(A_UINT32 phy, A_UINT32 capflag, A_BOOL ignoreCW)
166 if (WLAN_RC_PHY_HT(phy) && !(capflag & WLAN_RC_HT_FLAG)) {
170 if (WLAN_RC_PHY_DS(phy) && !(capflag & WLAN_RC_DS_FLAG)) {
173 if (WLAN_RC_PHY_SGI(phy) && !(capflag & WLAN_RC_HT40_SGI_FLAG)) {
177 if (!ignoreCW && WLAN_RC_PHY_HT(phy)) {
178 if (WLAN_RC_PHY_40(phy) && !(capflag & WLAN_RC_40_FLAG)) {
182 if (!WLAN_RC_PHY_40(phy) && (capflag & WLAN_RC_40_FLAG)) {
191 * Initialize the Valid Rate Index from valid entries in Rate Table
193 static A_UINT8 rcSibInitValidRates(const RATE_TABLE_11N *pRateTable,
196 PHY_STATE_CTRL *pPhyStateCtrl)
199 A_UINT8 singleStream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
202 for (i = 0; i < pRateTable->rateCount; i++) {
204 valid = pRateTable->info[i].validSingleStream;
206 valid = pRateTable->info[i].valid;
210 A_UINT32 phy = pRateTable->info[i].phy;
212 if (!rcIsValidPhyRate(phy, capflag, FALSE))
215 pPhyStateCtrl->validPhyRateIndex[phy][pPhyStateCtrl->validPhyRateCount[phy]] = i;
216 pPhyStateCtrl->validPhyRateCount[phy] += 1;
218 rcSetValidTxMask(pRc, i, TRUE);
228 * Initialize the Valid Rate Index from Rate Set
231 rcSibSetValidRates(const RATE_TABLE_11N *pRateTable,
233 struct ieee80211_rateset *pRateSet,
235 struct ath_node_target *an,
236 PHY_STATE_CTRL *pPhyStateCtrl)
238 A_UINT8 i, j, hi = 0;
239 A_UINT8 singleStream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
242 /* Use intersection of working rates and valid rates */
243 for (i = 0; i < pRateSet->rs_nrates; i++) {
244 for (j = 0; j < pRateTable->rateCount; j++) {
245 A_UINT32 phy = pRateTable->info[j].phy;
247 struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
250 valid = pRateTable->info[j].validSTBC;
251 } else if (singleStream) {
255 valid = pRateTable->info[j].validSingleStream;
257 valid = pRateTable->info[j].valid;
261 * We allow a rate only if its valid and the capflag matches one of
262 * the validity (TRUE/TRUE_20/TRUE_40) flags
265 if (((pRateSet->rs_rates[i] & 0x7F) ==
266 (pRateTable->info[j].dot11Rate & 0x7F))
267 && ((valid & WLAN_RC_CAP_MODE(capflag)) ==
268 WLAN_RC_CAP_MODE(capflag)) && !WLAN_RC_PHY_HT(phy)) {
269 if (!rcIsValidPhyRate(phy, capflag, FALSE))
272 pPhyStateCtrl->validPhyRateIndex[phy][pPhyStateCtrl->validPhyRateCount[phy]] = j;
273 pPhyStateCtrl->validPhyRateCount[phy] += 1;
275 rcSetValidTxMask(pRc, j, TRUE);
285 rcSibSetValidHtRates(const RATE_TABLE_11N *pRateTable,
289 struct ath_node_target *an,
290 PHY_STATE_CTRL *pPhyStateCtrl)
292 A_UINT8 i, j, hi = 0;
293 A_UINT8 singleStream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
296 /* Use intersection of working rates and valid rates */
297 for (i = 0; i < ((struct ieee80211_rateset *)pMcsSet)->rs_nrates; i++) {
298 for (j = 0; j < pRateTable->rateCount; j++) {
299 A_UINT32 phy = pRateTable->info[j].phy;
301 struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
304 valid = pRateTable->info[j].validSTBC;
305 } else if (singleStream) {
309 valid = pRateTable->info[j].validSingleStream;
311 valid = pRateTable->info[j].valid;
314 if (((((struct ieee80211_rateset *)pMcsSet)->rs_rates[i] & 0x7F)
315 != (pRateTable->info[j].dot11Rate & 0x7F))
316 || !WLAN_RC_PHY_HT(phy)
317 || !WLAN_RC_PHY_HT_VALID(valid, capflag)
318 || ((pRateTable->info[j].dot11Rate == 15) &&
320 (capflag & WLAN_RC_WEP_TKIP_FLAG)) )
325 if (!rcIsValidPhyRate(phy, capflag, FALSE))
328 pPhyStateCtrl->validPhyRateIndex[phy][pPhyStateCtrl->validPhyRateCount[phy]] = j;
329 pPhyStateCtrl->validPhyRateCount[phy] += 1;
331 rcSetValidTxMask(pRc, j, TRUE);
340 * Update the SIB's rate control information
342 * This should be called when the supported rates change
343 * (e.g. SME operation, wireless mode change)
345 * It will determine which rates are valid for use.
348 rcSibUpdate_ht(struct ath_softc_tgt *sc, struct ath_node_target *an,
349 A_UINT32 capflag, A_BOOL keepState, struct ieee80211_rate *pRateSet)
351 RATE_TABLE_11N *pRateTable = 0;
352 struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
353 struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
354 A_UINT8 *phtMcs = (A_UINT8*)&pRateSet->htrates;
355 TX_RATE_CTRL *pRc = (TX_RATE_CTRL *)(pSib);
356 PHY_STATE_CTRL mPhyCtrlState;
358 A_UINT8 i, j, k, hi = 0, htHi = 0;
360 pRateTable = (RATE_TABLE_11N*)asc->hwRateTable[sc->sc_curmode];
362 /* Initial rate table size. Will change depending on the working rate set */
363 pRc->rateTableSize = MAX_TX_RATE_TBL;
365 /* Initialize thresholds according to the global rate table */
366 for (i = 0 ; (i < pRc->rateTableSize) && (!keepState); i++) {
367 pRc->state[i].per = 0;
370 /* Determine the valid rates */
371 rcInitValidTxMask(pRc);
373 for (i = 0; i < WLAN_RC_PHY_MAX; i++) {
374 for (j = 0; j < MAX_TX_RATE_PHY; j++) {
375 mPhyCtrlState.validPhyRateIndex[i][j] = 0;
377 mPhyCtrlState.validPhyRateCount[i] = 0;
380 pRc->rcPhyMode = (capflag & WLAN_RC_40_FLAG);
382 if (pRateSet == NULL || !pRateSet->rates.rs_nrates) {
383 /* No working rate, just initialize valid rates */
384 hi = rcSibInitValidRates(pRateTable, pRc, capflag, &mPhyCtrlState);
386 /* Use intersection of working rates and valid rates */
387 hi = rcSibSetValidRates(pRateTable, pRc, &(pRateSet->rates),
388 capflag, an, &mPhyCtrlState);
390 if (capflag & WLAN_RC_HT_FLAG) {
391 htHi = rcSibSetValidHtRates(pRateTable, pRc, phtMcs,
392 capflag, an, &mPhyCtrlState);
395 hi = A_MAX(hi, htHi);
398 pRc->rateTableSize = hi + 1;
401 ASSERT(pRc->rateTableSize <= MAX_TX_RATE_TBL);
403 for (i = 0, k = 0; i < WLAN_RC_PHY_MAX; i++) {
404 for (j = 0; j < mPhyCtrlState.validPhyRateCount[i]; j++) {
405 pRc->validRateIndex[k++] = mPhyCtrlState.validPhyRateIndex[i][j];
408 if (!rcIsValidPhyRate(i, pRateTable->initialRateMax, TRUE) ||
409 !mPhyCtrlState.validPhyRateCount[i])
412 pRc->rateMaxPhy = mPhyCtrlState.validPhyRateIndex[i][j-1];
415 ASSERT(pRc->rateTableSize <= MAX_TX_RATE_TBL);
416 ASSERT(k <= MAX_TX_RATE_TBL);
418 pRc->rateMaxPhy = pRc->validRateIndex[k-4];
419 pRc->maxValidRate = k;
421 rcSortValidRates(pRateTable, pRc);
427 * Return the median of three numbers
429 INLINE A_RSSI median(A_RSSI a, A_RSSI b, A_RSSI c)
451 rcRateFind_ht(struct ath_softc_tgt *sc, struct atheros_node *pSib,
452 const RATE_TABLE_11N *pRateTable, A_BOOL probeAllowed, A_BOOL *isProbing)
455 A_UINT32 bestThruput, thisThruput;
457 A_UINT8 rate, nextRate, bestRate;
458 A_UINT8 maxIndex, minIndex;
460 TX_RATE_CTRL *pRc = NULL;
462 pRc = (TX_RATE_CTRL *)(pSib ? (pSib) : NULL);
467 * Age (reduce) last ack rssi based on how old it is.
468 * The bizarre numbers are so the delta is 160msec,
469 * meaning we divide by 16.
470 * 0msec <= dt <= 25msec: don't derate
471 * 25msec <= dt <= 185msec: derate linearly from 0 to 10dB
472 * 185msec <= dt: derate by 10dB
475 nowMsec = A_MS_TICKGET();
476 dt = nowMsec - pRc->rssiTime;
479 * Now look up the rate in the rssi table and return it.
480 * If no rates match then we return 0 (lowest rate)
484 maxIndex = pRc->maxValidRate-1;
490 * Try the higher rate first. It will reduce memory moving time
491 * if we have very good channel characteristics.
493 for (index = maxIndex; index >= minIndex ; index--) {
496 rate = pRc->validRateIndex[index];
497 if (rate > pRc->rateMaxPhy) {
501 /* if the best throughput is already larger than the userRateKbps..
502 * then we could skip of rest of calculation..
504 if( bestThruput >= pRateTable->info[rate].userRateKbps)
508 * For TCP the average collision rate is around 11%,
509 * so we ignore PERs less than this. This is to
510 * prevent the rate we are currently using (whose
511 * PER might be in the 10-15 range because of TCP
512 * collisions) looking worse than the next lower
513 * rate whose PER has decayed close to 0. If we
514 * used to next lower rate, its PER would grow to
515 * 10-15 and we would be worse off then staying
516 * at the current rate.
518 perThres = pRc->state[rate].per;
519 if ( perThres < 12 ) {
523 thisThruput = pRateTable->info[rate].userRateKbps * (100 - perThres);
524 if (bestThruput <= thisThruput) {
525 bestThruput = thisThruput;
533 * Must check the actual rate (rateKbps) to account for non-monoticity of
537 if (rate >= pRc->rateMaxPhy && probeAllowed) {
538 rate = pRc->rateMaxPhy;
540 /* Probe the next allowed phy state */
541 /* FIXME: Check to make sure ratMax is checked properly */
542 if (rcGetNextValidTxRate( pRateTable, pRc, rate, &nextRate) &&
543 (nowMsec - pRc->probeTime > pRateTable->probeInterval) &&
544 (pRc->hwMaxRetryPktCnt >= 1))
547 pRc->probeRate = rate;
548 pRc->probeTime = nowMsec;
549 pRc->hwMaxRetryPktCnt = 0;
556 * Make sure rate is not higher than the allowed maximum.
557 * We should also enforce the min, but I suspect the min is
558 * normally 1 rather than 0 because of the rate 9 vs 6 issue
561 if (rate > (pRc->rateTableSize - 1)) {
562 rate = pRc->rateTableSize - 1;
565 /* record selected rate, which is used to decide if we want to do fast frame */
566 if (!(*isProbing) && pSib) {
567 pSib->lastRateKbps = pRateTable->info[rate].rateKbps;
568 ((struct atheros_softc*)sc->sc_rc)->currentTxRateKbps = pSib->lastRateKbps;
569 ((struct atheros_softc*)sc->sc_rc)->currentTxRateIndex = rate;
576 rcRateSetseries(const RATE_TABLE_11N *pRateTable ,
577 struct ath_rc_series *series,
578 A_UINT8 tries, A_UINT8 rix,
579 A_BOOL rtsctsenable, A_UINT32 chainmask,int stbc)
581 series->tries = tries;
582 series->flags = (rtsctsenable? ATH_RC_RTSCTS_FLAG : 0) |
583 (WLAN_RC_PHY_DS(pRateTable->info[rix].phy) ? ATH_RC_DS_FLAG : 0) |
584 (WLAN_RC_PHY_40(pRateTable->info[rix].phy) ? ATH_RC_CW40_FLAG : 0) |
585 (WLAN_RC_PHY_SGI(pRateTable->info[rix].phy) ? ATH_RC_HT40_SGI_FLAG : 0);
588 /* For now, only single stream STBC is supported */
589 if (pRateTable->info[rix].rateCode >= 0x80 &&
590 pRateTable->info[rix].rateCode <= 0x87)
592 series->flags |= ATH_RC_TX_STBC_FLAG;
596 series->rix = pRateTable->info[rix].baseIndex;
597 series->max4msframelen = pRateTable->info[rix].max4msframelen;
598 series->txrateKbps = pRateTable->info[rix].rateKbps;
600 /* If the hardware is capable of multiple transmit chains (chainmask is 3, 5 or 7),
601 * then choose the number of transmit chains dynamically based on entries in the rate table.
603 #ifndef ATH_ENABLE_WLAN_FOR_K2
605 series->tx_chainmask = pRateTable->info[rix].txChainMask_3ch;
606 else if(chainmask == 1)
607 series->tx_chainmask = 1;
609 series->tx_chainmask = pRateTable->info[rix].txChainMask_2ch; /*Chainmask is 3 or 5*/
611 series->tx_chainmask = 1;
616 rcRateGetIndex(struct ath_softc_tgt *sc, struct ath_node_target *an,
617 const RATE_TABLE_11N *pRateTable ,
618 A_UINT8 rix, A_UINT16 stepDown, A_UINT16 minRate)
622 struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
623 TX_RATE_CTRL *pRc = (TX_RATE_CTRL *)(pSib);
626 for (j = RATE_TABLE_11N_SIZE; j > 0; j-- ) {
627 if (rcGetNextLowerValidTxRate(pRateTable, pRc, rix, &nextIndex)) {
634 for (j = stepDown; j > 0; j-- ) {
635 if (rcGetNextLowerValidTxRate(pRateTable, pRc, rix, &nextIndex)) {
646 void rcRateFind_11n(struct ath_softc_tgt *sc, struct ath_node_target *an,
647 int numTries, int numRates, int stepDnInc,
648 unsigned int rcflag, struct ath_rc_series series[], int *isProbe)
651 A_UINT8 tryPerRate = 0;
652 struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
653 RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
654 struct atheros_node *asn = ATH_NODE_ATHEROS(an);
659 rix = rcRateFind_ht(sc, asn, pRateTable, (rcflag & ATH_RC_PROBE_ALLOWED) ? 1 : 0,
663 if ((rcflag & ATH_RC_PROBE_ALLOWED) && (*isProbe)) {
664 /* set one try for probe rates. For the probes don't enable rts */
665 rcRateSetseries(pRateTable, &series[i++], 1, nrix,
666 FALSE, asc->tx_chainmask, asn->stbc);
669 * Get the next tried/allowed rate. No RTS for the next series
670 * after the probe rate
672 nrix = rcRateGetIndex( sc, an, pRateTable, nrix, 1, FALSE);
675 tryPerRate = (numTries/numRates);
677 /* Set the choosen rate. No RTS for first series entry. */
678 rcRateSetseries(pRateTable, &series[i++], tryPerRate,
679 nrix, FALSE, asc->tx_chainmask, asn->stbc);
681 /* Fill in the other rates for multirate retry */
682 for (; i < numRates; i++) {
686 tryNum = ((i + 1) == numRates) ? numTries - (tryPerRate * i) : tryPerRate ;
687 minRate = (((i + 1) == numRates) && (rcflag & ATH_RC_MINRATE_LASTRATE)) ? 1 : 0;
689 nrix = rcRateGetIndex(sc, an, pRateTable, nrix, stepDnInc, minRate);
691 /* All other rates in the series have RTS enabled */
692 rcRateSetseries(pRateTable, &series[i], tryNum,
693 nrix, TRUE, asc->tx_chainmask, asn->stbc);
698 * Change rate series to enable aggregation when operating at lower MCS rates.
699 * When first rate in series is MCS2 in HT40 @ 2.4GHz, series should look like:
700 * {MCS2, MCS1, MCS0, MCS0}.
701 * When first rate in series is MCS3 in HT20 @ 2.4GHz, series should look like:
702 * {MCS3, MCS2, MCS1, MCS1}
703 * So, set fourth rate in series to be same as third one for above conditions.
705 if (sc->sc_curmode == IEEE80211_MODE_11NG) {
706 dot11Rate = pRateTable->info[rix].dot11Rate;
707 phy = pRateTable->info[rix].phy;
709 ((dot11Rate == 2 && phy == WLAN_RC_PHY_HT_40_SS) ||
710 (dot11Rate == 3 && phy == WLAN_RC_PHY_HT_20_SS)))
712 series[3].rix = series[2].rix;
713 series[3].flags = series[2].flags;
714 series[3].max4msframelen = series[2].max4msframelen;
720 * AP91 Kite: NetGear OTA location-4 downlink.
721 * Enable RTS/CTS at MCS 3-0 for downlink throughput.
723 if (sc->sc_curmode == IEEE80211_MODE_11NG) {
724 dot11Rate = pRateTable->info[rix].dot11Rate;
725 if (dot11Rate <= 3 ) {
726 series[0].flags |= ATH_RC_RTSCTS_FLAG;
732 rcUpdate_ht(struct ath_softc_tgt *sc, struct ath_node_target *an, int txRate,
733 A_BOOL Xretries, int retries, A_UINT8 curTxAnt,
734 A_UINT16 nFrames, A_UINT16 nBad)
737 A_UINT32 nowMsec = A_MS_TICKGET();
740 struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
741 struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
742 RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
744 static A_UINT32 nRetry2PerLookup[10] = {
760 pRc = (TX_RATE_CTRL *)(pSib);
762 ASSERT(retries >= 0 && retries < MAX_TX_RETRIES);
769 lastPer = pRc->state[txRate].per;
772 /* Update the PER. */
774 pRc->state[txRate].per += 30;
775 if (pRc->state[txRate].per > 100) {
776 pRc->state[txRate].per = 100;
781 count = sizeof(nRetry2PerLookup) / sizeof(nRetry2PerLookup[0]);
782 if (retries >= count) {
786 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
787 pRc->state[txRate].per = (A_UINT8)(pRc->state[txRate].per -
788 (pRc->state[txRate].per / 8) + ((100) / 8));
791 /* Xretries == 1 or 2 */
793 if (pRc->probeRate == txRate)
799 * Update the PER. Make sure it doesn't index out of array's bounds.
801 count = sizeof(nRetry2PerLookup) / sizeof(nRetry2PerLookup[0]);
802 if (retries >= count) {
807 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
809 * Assuming that nFrames is not 0. The current PER
810 * from the retries is 100 * retries / (retries+1),
811 * since the first retries attempts failed, and the
812 * next one worked. For the one that worked, nBad
813 * subframes out of nFrames wored, so the PER for
814 * that part is 100 * nBad / nFrames, and it contributes
815 * 100 * nBad / (nFrames * (retries+1)) to the above
816 * PER. The expression below is a simplified version
817 * of the sum of these two terms.
820 pRc->state[txRate].per = (A_UINT8)(pRc->state[txRate].per -
821 (pRc->state[txRate].per / 8) +
822 ((100*(retries*nFrames + nBad)/(nFrames*(retries+1))) / 8));
824 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
826 pRc->state[txRate].per = (A_UINT8)(pRc->state[txRate].per -
827 (pRc->state[txRate].per / 8) + (nRetry2PerLookup[retries] / 8));
831 * If we got at most one retry then increase the max rate if
832 * this was a probe. Otherwise, ignore the probe.
835 if (pRc->probeRate && pRc->probeRate == txRate) {
836 if (retries > 0 || 2 * nBad > nFrames) {
838 * Since we probed with just a single attempt,
839 * any retries means the probe failed. Also,
840 * if the attempt worked, but more than half
841 * the subframes were bad then also consider
842 * the probe a failure.
846 pRc->rateMaxPhy = pRc->probeRate;
848 if (pRc->state[pRc->probeRate].per > 30) {
849 pRc->state[pRc->probeRate].per = 20;
855 * Since this probe succeeded, we allow the next probe
856 * twice as soon. This allows the maxRate to move up
857 * faster if the probes are succesful.
859 pRc->probeTime = nowMsec - pRateTable->probeInterval / 2;
865 * Don't update anything. We don't know if this was because
866 * of collisions or poor signal.
868 * Later: if rssiAck is close to pRc->state[txRate].rssiThres
869 * and we see lots of retries, then we could increase
870 * pRc->state[txRate].rssiThres.
872 pRc->hwMaxRetryPktCnt = 0;
875 * It worked with no retries. First ignore bogus (small)
878 if (txRate == pRc->rateMaxPhy && pRc->hwMaxRetryPktCnt < 255) {
879 pRc->hwMaxRetryPktCnt++;
887 ASSERT((pRc->rateMaxPhy >= 0 && pRc->rateMaxPhy <= pRc->rateTableSize &&
888 pRc->rateMaxPhy != INVALID_RATE_MAX));
891 * If this rate looks bad (high PER) then stop using it for
892 * a while (except if we are probing).
894 if (pRc->state[txRate].per >= 55 && txRate > 0 &&
895 pRateTable->info[txRate].rateKbps <=
896 pRateTable->info[pRc->rateMaxPhy].rateKbps)
898 rcGetNextLowerValidTxRate(pRateTable, pRc, (A_UINT8) txRate,
901 /* Don't probe for a little while. */
902 pRc->probeTime = nowMsec;
905 /* Make sure the rates below this have lower PER */
906 /* Monotonicity is kept only for rates below the current rate. */
907 if (pRc->state[txRate].per < lastPer) {
908 for (rate = txRate - 1; rate >= 0; rate--) {
909 if (pRateTable->info[rate].phy != pRateTable->info[txRate].phy) {
913 if (pRc->state[rate].per > pRc->state[rate+1].per) {
914 pRc->state[rate].per = pRc->state[rate+1].per;
919 /* Maintain monotonicity for rates above the current rate*/
920 for (rate = txRate; rate < pRc->rateTableSize - 1; rate++) {
921 if (pRc->state[rate+1].per < pRc->state[rate].per) {
922 pRc->state[rate+1].per = pRc->state[rate].per;
926 /* Every so often, we reduce the thresholds and PER (different for CCK and OFDM). */
927 if (nowMsec - pRc->perDownTime >= pRateTable->rssiReduceInterval) {
928 for (rate = 0; rate < pRc->rateTableSize; rate++) {
929 pRc->state[rate].per = 7*pRc->state[rate].per/8;
932 pRc->perDownTime = nowMsec;
937 * This routine is called by the Tx interrupt service routine to give
938 * the status of previous frames.
940 void rcUpdate_11n(struct ath_softc_tgt *sc, struct ath_node_target *an,
942 int finalTSIdx, int Xretries,
943 struct ath_rc_series rcs[], int nFrames,
944 int nBad, int long_retry)
948 struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
949 RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
950 struct atheros_node *pSib = ATH_NODE_ATHEROS(an);
951 TX_RATE_CTRL *pRc = (TX_RATE_CTRL *)(pSib);
959 ASSERT (rcs[0].tries != 0);
962 * If the first rate is not the final index, there are intermediate rate failures
965 if (finalTSIdx != 0) {
967 /* Process intermediate rates that failed.*/
968 for (series = 0; series < finalTSIdx ; series++) {
969 if (rcs[series].tries != 0) {
970 flags = rcs[series].flags;
971 /* If HT40 and we have switched mode from 40 to 20 => don't update */
972 if ((flags & ATH_RC_CW40_FLAG) &&
973 (pRc->rcPhyMode != (flags & ATH_RC_CW40_FLAG))) {
976 if ((flags & ATH_RC_CW40_FLAG) && (flags & ATH_RC_HT40_SGI_FLAG)) {
977 rix = pRateTable->info[rcs[series].rix].htIndex;
978 } else if (flags & ATH_RC_HT40_SGI_FLAG) {
979 rix = pRateTable->info[rcs[series].rix].sgiIndex;
980 } else if (flags & ATH_RC_CW40_FLAG) {
981 rix = pRateTable->info[rcs[series].rix].cw40Index;
983 rix = pRateTable->info[rcs[series].rix].baseIndex;
986 /* FIXME:XXXX, too many args! */
987 rcUpdate_ht(sc, an, rix, Xretries? 1 : 2, rcs[series].tries,
988 curTxAnt, nFrames, nFrames);
993 * Handle the special case of MIMO PS burst, where the second aggregate is sent
994 * out with only one rate and one try. Treating it as an excessive retry penalizes
995 * the rate inordinately.
997 if (rcs[0].tries == 1 && Xretries == 1) {
1002 flags = rcs[series].flags;
1003 /* If HT40 and we have switched mode from 40 to 20 => don't update */
1004 if ((flags & ATH_RC_CW40_FLAG) &&
1005 (pRc->rcPhyMode != (flags & ATH_RC_CW40_FLAG))) {
1008 if ((flags & ATH_RC_CW40_FLAG) && (flags & ATH_RC_HT40_SGI_FLAG)) {
1009 rix = pRateTable->info[rcs[series].rix].htIndex;
1010 } else if (flags & ATH_RC_HT40_SGI_FLAG) {
1011 rix = pRateTable->info[rcs[series].rix].sgiIndex;
1012 } else if (flags & ATH_RC_CW40_FLAG) {
1013 rix = pRateTable->info[rcs[series].rix].cw40Index;
1015 rix = pRateTable->info[rcs[series].rix].baseIndex;
1018 /* FIXME:XXXX, too many args! */
1019 rcUpdate_ht(sc, an, rix, Xretries, long_retry, curTxAnt,
1023 void ath_tx_status_update_rate(struct ath_softc_tgt *sc,
1024 struct ath_rc_series rcs[],
1026 WMI_TXSTATUS_EVENT *txs)
1028 struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
1029 RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
1032 if (pRateTable->info[rcs[series].rix].rateCode & 0x80) {
1033 txs->txstatus[txs->cnt].ts_rate |= SM(pRateTable->info[rcs[series].rix].dot11Rate,
1034 ATH9K_HTC_TXSTAT_RATE);
1035 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_MCS;
1037 if (rcs[series].flags & ATH_RC_CW40_FLAG)
1038 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_CW40;
1040 if (rcs[series].flags & ATH_RC_HT40_SGI_FLAG)
1041 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_SGI;
1044 txs->txstatus[txs->cnt].ts_rate |= SM(rcs[series].rix, ATH9K_HTC_TXSTAT_RATE);
1047 if (rcs[series].flags & ATH_RC_RTSCTS_FLAG)
1048 txs->txstatus[txs->cnt].ts_flags |= ATH9K_HTC_TXSTAT_RTC_CTS;
1052 struct ath_ratectrl *
1053 ath_rate_attach(struct ath_softc_tgt *sc)
1055 struct atheros_softc *asc;
1057 asc = adf_os_mem_alloc(sizeof(struct atheros_softc));
1061 adf_os_mem_set(asc, 0, sizeof(struct atheros_softc));
1062 asc->arc.arc_space = sizeof(struct atheros_node);
1064 ar5416AttachRateTables(asc);
1066 asc->tx_chainmask = 1;
1072 ath_rate_detach(struct ath_ratectrl *rc)
1074 adf_os_mem_free(rc);
1078 ath_rate_findrate(struct ath_softc_tgt *sc,
1079 struct ath_node_target *an,
1085 unsigned int rcflag,
1086 struct ath_rc_series series[],
1091 if (!numRates || !numTries) {
1095 ath_rate_findrate_11n(sc, an, frameLen, numTries, numRates, stepDnInc,
1096 rcflag, series, isProbe);
1099 #define MS(_v, _f) (((_v) & _f) >> _f##_S)
1102 ath_rate_tx_complete(struct ath_softc_tgt *sc,
1103 struct ath_node_target *an,
1104 struct ath_tx_desc *ds,
1105 struct ath_rc_series rcs[],
1106 int nframes, int nbad)
1108 ath_rate_tx_complete_11n(sc, an, ds, rcs, nframes, nbad);
1112 ath_rate_newassoc(struct ath_softc_tgt *sc, struct ath_node_target *an, int isnew,
1113 unsigned int capflag, struct ieee80211_rate *rs)
1115 ath_rate_newassoc_11n(sc, an, isnew, capflag, rs);
1118 void ath_rate_node_update(struct ath_softc_tgt *sc,
1119 struct ath_node_target *an,
1122 struct ieee80211_rate *rs)
1124 struct ieee80211_node_target *ni = &an->ni;
1126 ath_rate_newassoc(sc, ATH_NODE_TARGET(ni), isnew, capflag, rs);
1129 static int init_ath_rate_atheros(void);
1130 static void exit_ath_rate_atheros(void);
1133 ath_rate_newstate(struct ath_softc_tgt *sc,
1134 struct ieee80211vap_target *vap,
1135 enum ieee80211_state state,
1137 struct ieee80211_rate *rs)
1139 struct ieee80211_node_target *ni = vap->iv_bss;
1140 struct atheros_softc *asc = (struct atheros_softc *) sc->sc_rc;
1142 asc->tx_chainmask = sc->sc_ic.ic_tx_chainmask;
1143 ath_rate_newassoc(sc, ATH_NODE_TARGET(ni), 1, capflag, rs);
1147 ath_rate_findrate_11n(struct ath_softc_tgt *sc,
1148 struct ath_node_target *an,
1153 unsigned int rcflag,
1154 struct ath_rc_series series[],
1158 if (!numRates || !numTries) {
1162 rcRateFind_11n(sc, an, numTries, numRates, stepDnInc, rcflag, series, isProbe);
1166 ath_rate_tx_complete_11n(struct ath_softc_tgt *sc,
1167 struct ath_node_target *an,
1168 struct ath_tx_desc *ds,
1169 struct ath_rc_series rcs[],
1170 int nframes, int nbad)
1172 int finalTSIdx = ds->ds_txstat.ts_rate;
1175 if ((ds->ds_txstat.ts_status & HAL_TXERR_XRETRY) ||
1176 (ds->ds_txstat.ts_status & HAL_TXERR_FIFO) ||
1177 (ds->ds_txstat.ts_flags & HAL_TX_DATA_UNDERRUN) ||
1178 (ds->ds_txstat.ts_flags & HAL_TX_DELIM_UNDERRUN)) {
1182 rcUpdate_11n(sc, an,
1183 ds->ds_txstat.ts_antenna, finalTSIdx,
1184 tx_status, rcs, nframes , nbad,
1185 ds->ds_txstat.ts_longretry);
1189 ath_rate_newassoc_11n(struct ath_softc_tgt *sc, struct ath_node_target *an, int isnew,
1190 unsigned int capflag, struct ieee80211_rate *rs)
1193 #ifdef MAGPIE_MERLIN
1194 struct atheros_node *oan = ATH_NODE_ATHEROS(an);
1195 /* Only MERLIN can send STBC */
1196 oan->stbc = (capflag & ATH_RC_TX_STBC_FLAG) ? 1 : 0;
1198 rcSibUpdate_ht(sc, an, capflag, 0, rs);
1202 void ath_rate_mcs2rate(struct ath_softc_tgt *sc,a_uint8_t sgi, a_uint8_t ht40,
1203 a_uint8_t rateCode, a_uint32_t *txrate, a_uint32_t *rxrate)
1206 struct atheros_softc *asc = (struct atheros_softc*)sc->sc_rc;
1207 RATE_TABLE_11N *pRateTable = (RATE_TABLE_11N *)asc->hwRateTable[sc->sc_curmode];
1208 a_uint32_t rateKbps = 0;
1210 *txrate = asc->currentTxRateKbps;
1212 /* look 11NA table for rateKbps*/
1213 for (idx = 0; idx < pRateTable->rateCount && !rateKbps; ++idx) {
1214 if (pRateTable->info[idx].rateCode == rateCode) {
1216 if(pRateTable->info[idx].valid == TRUE_40 &&
1217 pRateTable->info[idx].phy == WLAN_RC_PHY_HT_40_DS_HGI)
1218 rateKbps = pRateTable->info[idx].rateKbps;
1220 if (pRateTable->info[idx].valid == TRUE_40)/* HT40 only*/
1221 rateKbps = pRateTable->info[idx].rateKbps;
1223 if (pRateTable->info[idx].valid != FALSE)
1224 rateKbps = pRateTable->info[idx].rateKbps;