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