ath9k_htc: Update to upstream's commit d19607454d656cb14d8c16dfbf161eebb542e8fe dated...
[linux-libre-firmware.git] / ath9k_htc / target_firmware / wlan / ratectrl.h
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 #ifndef _RATECTRL_H_
37 #define _RATECTRL_H_
38
39 #include <adf_os_types.h>
40 #include <adf_os_dma.h>
41 #include <adf_os_timer.h>
42 #include <adf_os_lock.h>
43 #include <adf_os_io.h>
44 #include <adf_os_mem.h>
45 #include <adf_os_util.h>
46 #include <adf_os_stdtypes.h>
47 #include <adf_os_defer.h>
48 #include <adf_os_atomic.h>
49 #include <adf_nbuf.h>
50 #include <adf_net.h>
51 #include <adf_net_types.h>
52 #include <adf_net_wcmd.h>
53
54 #include <ieee80211_var.h>
55
56 #include "if_athrate.h"
57 #include "if_athvar.h"
58
59 #define FALSE   0
60 #define TRUE    1
61
62 typedef int8_t          A_RSSI;
63 typedef int32_t         A_RSSI32;
64 typedef u_int8_t        WLAN_PHY;
65
66 #ifndef INLINE
67 #define INLINE          __inline
68 #endif
69
70 #ifndef A_MIN
71 #define A_MIN(a,b)      ((a)<(b)?(a):(b))
72 #endif
73
74 #ifndef A_MAX
75 #define A_MAX(a,b)      ((a)>(b)?(a):(b))
76 #endif
77
78 /*
79  * Use the hal os glue code to get ms time; we supply
80  * a null arg because we know it's not needed.
81  */
82 #define A_MS_TICKGET()  OS_GETUPTIME(NULL)
83
84 #define WLAN_PHY_OFDM   IEEE80211_T_OFDM
85 #define WLAN_PHY_TURBO  IEEE80211_T_TURBO
86 #define WLAN_PHY_CCK    IEEE80211_T_CCK
87 #define WLAN_PHY_XR     (IEEE80211_T_TURBO+1)
88
89 enum {
90         WLAN_RC_PHY_CCK,
91         WLAN_RC_PHY_OFDM,
92         WLAN_RC_PHY_TURBO,
93         WLAN_RC_PHY_XR,
94         WLAN_RC_PHY_HT_20_SS,
95         WLAN_RC_PHY_HT_20_DS,
96         WLAN_RC_PHY_HT_40_SS,
97         WLAN_RC_PHY_HT_40_DS,
98         WLAN_RC_PHY_HT_20_SS_HGI,
99         WLAN_RC_PHY_HT_20_DS_HGI,
100         WLAN_RC_PHY_HT_40_SS_HGI,
101         WLAN_RC_PHY_HT_40_DS_HGI,
102         WLAN_RC_PHY_MAX
103 };
104
105 #define IS_CHAN_TURBO(_c)   (((_c)->channelFlags & CHANNEL_TURBO) != 0)
106 #define IS_CHAN_2GHZ(_c)    (((_c)->channelFlags & CHANNEL_2GHZ) != 0)
107
108 #define PKTLOG_RATE_CTL_FIND(_sc, log_data, flags)   ath_log_rcfind(_sc, log_data, flags);
109 #define PKTLOG_RATE_CTL_UPDATE(_sc, log_data, flags) ath_log_rcupdate(_sc, log_data, flags);
110 #define ASSERT(condition)
111
112 #define WIRELESS_MODE_11NA      IEEE80211_MODE_11NA
113 #define WIRELESS_MODE_11NG      IEEE80211_MODE_11NG
114 #define WIRELESS_MODE_MAX       IEEE80211_MODE_MAX
115
116 #define RX_FLIP_THRESHOLD       3       /* XXX */
117
118 #ifdef MAGPIE_MERLIN
119 #define MAX_TX_RATE_TBL         46
120 #else
121 #define MAX_TX_RATE_TBL         54//46
122 #endif
123
124 /*
125  * State structures for new rate adaptation code
126  *
127  * NOTE: Modifying these structures will impact
128  * the Perl script that parses packet logging data.
129  * See the packet logging module for more information.
130  */
131 typedef struct TxRateCrtlState_s {
132         A_UINT8 per;                /* recent estimate of packet error rate (%) */
133 } TxRateCtrlState;
134
135 typedef struct TxRateCtrl_s {
136         TxRateCtrlState state[MAX_TX_RATE_TBL];                         /* state for each rate */
137         A_UINT8  rateTableSize;       /* rate table size */
138         A_UINT8  probeRate;           /* rate we are probing at */
139         A_UINT32 rssiTime;            /* msec timestamp for last ack rssi */
140         A_UINT32 probeTime;           /* msec timestamp for last probe */
141         A_UINT8  hwMaxRetryPktCnt;    /* num packets since we got HW max retry error */
142         A_UINT8  maxValidRate;       /* maximum number of valid rate */
143         A_UINT8  validRateIndex[MAX_TX_RATE_TBL];    /* rc Index is valid for this Sib */
144         A_UINT32 perDownTime;         /* msec timstamp for last PER down step */
145         A_UINT8  rcPhyMode;
146         A_UINT8  rateMaxPhy;          /* Phy index for the max rate */
147 } TX_RATE_CTRL;
148
149 typedef struct phy_rate_ctrl {
150         /* 11n state */
151         A_UINT8  validPhyRateCount[WLAN_RC_PHY_MAX]; /* valid rate count */
152         A_UINT8  validPhyRateIndex[WLAN_RC_PHY_MAX][MAX_TX_RATE_TBL]; /* index */
153 }PHY_STATE_CTRL;
154
155 /* per-node state */
156 struct atheros_node {
157         TX_RATE_CTRL txRateCtrl;    /* rate control state proper */
158         A_UINT32 lastRateKbps;      /* last rate in Kb/s */
159         A_UINT8 singleStream    :1,   /* When TRUE, only single stream Tx possible */
160                 stbc            :2;   /* Rx stbc capability */
161
162 };
163
164 #define ATH_NODE_ATHEROS(an)    (an->an_rcnode)
165
166 /*
167  * Rate Table structure for various modes - 'b', 'a', 'g', 'xr';
168  * order of fields in info structure is important because hardcoded
169  * structures are initialized within the hal for these
170  */
171
172 typedef struct {
173         int         rateCount;
174         A_UINT8     rateCodeToIndex[RATE_TABLE_SIZE]; /* backward mapping */
175         struct {
176                 int    valid;            /* Valid for use in rate control */
177                 WLAN_PHY  phy;              /* CCK/OFDM/TURBO/XR */
178                 A_UINT16  rateKbps;         /* Rate in Kbits per second */
179                 A_UINT16  userRateKbps;     /* User rate in KBits per second */
180                 A_UINT8   rateCode;         /* rate that goes into hw descriptors */
181                 A_UINT8   shortPreamble;    /* Mask for enabling short preamble in rate code for CCK */
182                 A_UINT8   dot11Rate;        /* Value that goes into supported rates info element of MLME */
183                 A_UINT8   controlRate;      /* Index of next lower basic rate, used for duration computation */
184                 A_RSSI    rssiAckValidMin;  /* Rate control related information */
185                 A_RSSI    rssiAckDeltaMin;  /* Rate control related information */
186                 A_UINT16  lpAckDuration;    /* long preamble ACK duration */
187                 A_UINT16  spAckDuration;    /* short preamble ACK duration*/
188                 A_UINT32  max4msFrameLen;   /* Maximum frame length(bytes) for 4ms tx duration */
189                 struct {
190                         A_UINT32  word4Retries;
191                         A_UINT32  word5Rates;
192                 } normalSched;
193                 struct {
194                         A_UINT32  word4Retries;
195                         A_UINT32  word5Rates;
196                 } shortSched;
197                 struct {
198                         A_UINT32  word4Retries;
199                         A_UINT32  word5Rates;
200                 } probeSched;
201                 struct {
202                         A_UINT32  word4Retries;
203                         A_UINT32  word5Rates;
204                 } probeShortSched;
205                 struct {
206                         A_UINT32  word4Retries;
207                         A_UINT32  word5Rates;
208                 } uapsd_normalSched;
209                 struct {
210                         A_UINT32  word4Retries;
211                         A_UINT32  word5Rates;
212                 } uapsd_shortSched;
213 #ifdef ATH_REMOVE_5G_RATE_TABLE
214 #ifdef ATH_REMOVE_TURBO_RATE_TABLE
215 #ifdef ATH_REMOVE_XR_RATE_TABLE
216         } info[12];
217 #else
218 } info[32];
219 #endif
220 #else
221 } info[32];
222 #endif
223 #else
224 } info[32];
225 #endif
226 A_UINT32    probeInterval;        /* interval for ratectrl to probe for
227                                      other rates */
228 A_UINT32    rssiReduceInterval;   /* interval for ratectrl to reduce RSSI */
229 A_UINT8     regularToTurboThresh; /* upperbound on regular (11a or 11g)
230                                      mode's rate before switching to turbo*/
231 A_UINT8     turboToRegularThresh; /* lowerbound on turbo mode's rate before
232                                      switching to regular */
233 A_UINT8     pktCountThresh;       /* mode switch recommendation criterion:
234                                      number of consecutive packets sent at
235                                      rate beyond the rate threshold */
236 A_UINT8     initialRateMax;       /* the initial rateMax value used in
237                                      rcSibUpdate() */
238 A_UINT8     numTurboRates;        /* number of Turbo rates in the rateTable */
239 A_UINT8     xrToRegularThresh;    /* threshold to switch to Normal mode */
240 } RATE_TABLE;
241
242 /* per-device state */
243 struct atheros_softc {
244         struct ath_ratectrl     arc;
245         /* phy tables that contain rate control data */
246         void                    *hwRateTable[WIRELESS_MODE_MAX];
247         A_UINT32                tx_chainmask;
248         A_UINT32                currentTxRateKbps;
249         A_UINT32                currentTxRateIndex;
250 };
251
252 /*
253  *  Update the SIB's rate control information
254  *
255  *  This should be called when the supported rates change
256  *  (e.g. SME operation, wireless mode change)
257  *
258  *  It will determine which rates are valid for use.
259  */
260 void
261 rcSibUpdate(struct ath_softc_tgt *sc,
262             struct ath_node_target *an,
263             A_BOOL keepState,
264             struct ieee80211_rateset *pRateSet);
265
266 /*
267  *  This routine is called to initialize the rate control parameters
268  *  in the SIB. It is called initially during system initialization
269  *  or when a station is associated with the AP.
270  */
271 void rcSibInit(struct ath_softc_tgt *, struct ath_node_target *);
272
273 /*
274  * Determines and returns the new Tx rate index.
275  */
276 A_UINT16 rcRateFind(struct ath_softc_tgt *, struct atheros_node *,
277                     A_UINT32 frameLen,const  RATE_TABLE *pRateTable);
278
279 struct fusion_rate_info {
280         A_UINT32 txrate;
281         A_UINT8 rssi;
282         A_UINT8 per;
283 };
284
285 void ar5416AttachRateTables(struct atheros_softc *sc);
286
287 void ath_rate_node_update(struct ath_softc_tgt *sc,
288                           struct ath_node_target *an,
289                           a_int32_t isnew,
290                           a_uint32_t capflag,
291                           struct ieee80211_rate *rs);
292
293
294 void ath_rate_newstate(struct ath_softc_tgt *sc,
295                        struct ieee80211vap_target *vap,
296                        enum ieee80211_state state,
297                        a_uint32_t capflag,
298                        struct ieee80211_rate *rs);
299
300 #endif /* _RATECTRL_H_ */