rc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright (c) 2004 Sam Leffler, Errno Consulting
  3. * Copyright (c) 2004 Video54 Technologies, Inc.
  4. * Copyright (c) 2008 Atheros Communications Inc.
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #ifndef RC_H
  19. #define RC_H
  20. #include "ath9k.h"
  21. /*
  22. * Interface definitions for transmit rate control modules for the
  23. * Atheros driver.
  24. *
  25. * A rate control module is responsible for choosing the transmit rate
  26. * for each data frame. Management+control frames are always sent at
  27. * a fixed rate.
  28. *
  29. * Only one module may be present at a time; the driver references
  30. * rate control interfaces by symbol name. If multiple modules are
  31. * to be supported we'll need to switch to a registration-based scheme
  32. * as is currently done, for example, for authentication modules.
  33. *
  34. * An instance of the rate control module is attached to each device
  35. * at attach time and detached when the device is destroyed. The module
  36. * may associate data with each device and each node (station). Both
  37. * sets of storage are opaque except for the size of the per-node storage
  38. * which must be provided when the module is attached.
  39. *
  40. * The rate control module is notified for each state transition and
  41. * station association/reassociation. Otherwise it is queried for a
  42. * rate for each outgoing frame and provided status from each transmitted
  43. * frame. Any ancillary processing is the responsibility of the module
  44. * (e.g. if periodic processing is required then the module should setup
  45. * it's own timer).
  46. *
  47. * In addition to the transmit rate for each frame the module must also
  48. * indicate the number of attempts to make at the specified rate. If this
  49. * number is != ATH_TXMAXTRY then an additional callback is made to setup
  50. * additional transmit state. The rate control code is assumed to write
  51. * this additional data directly to the transmit descriptor.
  52. */
  53. struct ath_softc;
  54. #define TRUE 1
  55. #define FALSE 0
  56. #define ATH_RATE_MAX 30
  57. #define MCS_SET_SIZE 128
  58. enum ieee80211_fixed_rate_mode {
  59. IEEE80211_FIXED_RATE_NONE = 0,
  60. IEEE80211_FIXED_RATE_MCS = 1 /* HT rates */
  61. };
  62. /*
  63. * Use the hal os glue code to get ms time
  64. */
  65. #define IEEE80211_RATE_IDX_ENTRY(val, idx) (((val&(0xff<<(idx*8)))>>(idx*8)))
  66. #define SHORT_PRE 1
  67. #define LONG_PRE 0
  68. #define WLAN_PHY_HT_20_SS WLAN_RC_PHY_HT_20_SS
  69. #define WLAN_PHY_HT_20_DS WLAN_RC_PHY_HT_20_DS
  70. #define WLAN_PHY_HT_20_DS_HGI WLAN_RC_PHY_HT_20_DS_HGI
  71. #define WLAN_PHY_HT_40_SS WLAN_RC_PHY_HT_40_SS
  72. #define WLAN_PHY_HT_40_SS_HGI WLAN_RC_PHY_HT_40_SS_HGI
  73. #define WLAN_PHY_HT_40_DS WLAN_RC_PHY_HT_40_DS
  74. #define WLAN_PHY_HT_40_DS_HGI WLAN_RC_PHY_HT_40_DS_HGI
  75. #define WLAN_PHY_OFDM PHY_OFDM
  76. #define WLAN_PHY_CCK PHY_CCK
  77. #define TRUE_20 0x2
  78. #define TRUE_40 0x4
  79. #define TRUE_2040 (TRUE_20|TRUE_40)
  80. #define TRUE_ALL (TRUE_2040|TRUE)
  81. enum {
  82. WLAN_RC_PHY_HT_20_SS = 4,
  83. WLAN_RC_PHY_HT_20_DS,
  84. WLAN_RC_PHY_HT_40_SS,
  85. WLAN_RC_PHY_HT_40_DS,
  86. WLAN_RC_PHY_HT_20_SS_HGI,
  87. WLAN_RC_PHY_HT_20_DS_HGI,
  88. WLAN_RC_PHY_HT_40_SS_HGI,
  89. WLAN_RC_PHY_HT_40_DS_HGI,
  90. WLAN_RC_PHY_MAX
  91. };
  92. #define WLAN_RC_PHY_DS(_phy) ((_phy == WLAN_RC_PHY_HT_20_DS) \
  93. || (_phy == WLAN_RC_PHY_HT_40_DS) \
  94. || (_phy == WLAN_RC_PHY_HT_20_DS_HGI) \
  95. || (_phy == WLAN_RC_PHY_HT_40_DS_HGI))
  96. #define WLAN_RC_PHY_40(_phy) ((_phy == WLAN_RC_PHY_HT_40_SS) \
  97. || (_phy == WLAN_RC_PHY_HT_40_DS) \
  98. || (_phy == WLAN_RC_PHY_HT_40_SS_HGI) \
  99. || (_phy == WLAN_RC_PHY_HT_40_DS_HGI))
  100. #define WLAN_RC_PHY_SGI(_phy) ((_phy == WLAN_RC_PHY_HT_20_SS_HGI) \
  101. || (_phy == WLAN_RC_PHY_HT_20_DS_HGI) \
  102. || (_phy == WLAN_RC_PHY_HT_40_SS_HGI) \
  103. || (_phy == WLAN_RC_PHY_HT_40_DS_HGI))
  104. #define WLAN_RC_PHY_HT(_phy) (_phy >= WLAN_RC_PHY_HT_20_SS)
  105. /* Returns the capflag mode */
  106. #define WLAN_RC_CAP_MODE(capflag) (((capflag & WLAN_RC_HT_FLAG) ? \
  107. (capflag & WLAN_RC_40_FLAG) ? TRUE_40 : TRUE_20 : TRUE))
  108. /* Return TRUE if flag supports HT20 && client supports HT20 or
  109. * return TRUE if flag supports HT40 && client supports HT40.
  110. * This is used becos some rates overlap between HT20/HT40.
  111. */
  112. #define WLAN_RC_PHY_HT_VALID(flag, capflag) (((flag & TRUE_20) && !(capflag \
  113. & WLAN_RC_40_FLAG)) || ((flag & TRUE_40) && \
  114. (capflag & WLAN_RC_40_FLAG)))
  115. #define WLAN_RC_DS_FLAG (0x01)
  116. #define WLAN_RC_40_FLAG (0x02)
  117. #define WLAN_RC_SGI_FLAG (0x04)
  118. #define WLAN_RC_HT_FLAG (0x08)
  119. /* Index into the rate table */
  120. #define INIT_RATE_MAX_20 23
  121. #define INIT_RATE_MAX_40 40
  122. #define RATE_TABLE_SIZE 64
  123. /* XXX: Convert to kdoc */
  124. struct ath_rate_table {
  125. int rate_cnt;
  126. struct {
  127. int valid; /* Valid for use in rate control */
  128. int valid_single_stream;/* Valid for use in rate control
  129. for single stream operation */
  130. u8 phy; /* CCK/OFDM/TURBO/XR */
  131. u32 ratekbps; /* Rate in Kbits per second */
  132. u32 user_ratekbps; /* User rate in KBits per second */
  133. u8 ratecode; /* rate that goes into
  134. hw descriptors */
  135. u8 short_preamble; /* Mask for enabling short preamble
  136. in rate code for CCK */
  137. u8 dot11rate; /* Value that goes into supported
  138. rates info element of MLME */
  139. u8 ctrl_rate; /* Index of next lower basic rate,
  140. used for duration computation */
  141. int8_t rssi_ack_validmin; /* Rate control related */
  142. int8_t rssi_ack_deltamin; /* Rate control related */
  143. u8 base_index; /* base rate index */
  144. u8 cw40index; /* 40cap rate index */
  145. u8 sgi_index; /* shortgi rate index */
  146. u8 ht_index; /* shortgi rate index */
  147. u32 max_4ms_framelen; /* Maximum frame length(bytes)
  148. for 4ms tx duration */
  149. } info[RATE_TABLE_SIZE];
  150. u32 probe_interval; /* interval for ratectrl to
  151. probe for other rates */
  152. u32 rssi_reduce_interval; /* interval for ratectrl
  153. to reduce RSSI */
  154. u8 initial_ratemax; /* the initial ratemax value used
  155. in ath_rc_sib_update() */
  156. };
  157. #define ATH_RC_PROBE_ALLOWED 0x00000001
  158. #define ATH_RC_MINRATE_LASTRATE 0x00000002
  159. #define ATH_RC_SHORT_PREAMBLE 0x00000004
  160. struct ath_rc_series {
  161. u8 rix;
  162. u8 tries;
  163. u8 flags;
  164. u32 max_4ms_framelen;
  165. };
  166. /* rcs_flags definition */
  167. #define ATH_RC_DS_FLAG 0x01
  168. #define ATH_RC_CW40_FLAG 0x02 /* CW 40 */
  169. #define ATH_RC_SGI_FLAG 0x04 /* Short Guard Interval */
  170. #define ATH_RC_HT_FLAG 0x08 /* HT */
  171. #define ATH_RC_RTSCTS_FLAG 0x10 /* RTS-CTS */
  172. /*
  173. * State structures for new rate adaptation code
  174. */
  175. #define MAX_TX_RATE_TBL 64
  176. #define MAX_TX_RATE_PHY 48
  177. struct ath_tx_ratectrl_state {
  178. int8_t rssi_thres; /* required rssi for this rate (dB) */
  179. u8 per; /* recent estimate of packet error rate (%) */
  180. };
  181. struct ath_tx_ratectrl {
  182. struct ath_tx_ratectrl_state state[MAX_TX_RATE_TBL]; /* state */
  183. int8_t rssi_last; /* last ack rssi */
  184. int8_t rssi_last_lookup; /* last ack rssi used for lookup */
  185. int8_t rssi_last_prev; /* previous last ack rssi */
  186. int8_t rssi_last_prev2; /* 2nd previous last ack rssi */
  187. int32_t rssi_sum_cnt; /* count of rssi_sum for averaging */
  188. int32_t rssi_sum_rate; /* rate that we are averaging */
  189. int32_t rssi_sum; /* running sum of rssi for averaging */
  190. u32 valid_txrate_mask; /* mask of valid rates */
  191. u8 rate_table_size; /* rate table size */
  192. u8 rate_max; /* max rate that has recently worked */
  193. u8 probe_rate; /* rate we are probing at */
  194. u32 rssi_time; /* msec timestamp for last ack rssi */
  195. u32 rssi_down_time; /* msec timestamp for last down step */
  196. u32 probe_time; /* msec timestamp for last probe */
  197. u8 hw_maxretry_pktcnt; /* num packets since we got
  198. HW max retry error */
  199. u8 max_valid_rate; /* maximum number of valid rate */
  200. u8 valid_rate_index[MAX_TX_RATE_TBL]; /* valid rate index */
  201. u32 per_down_time; /* msec timstamp for last
  202. PER down step */
  203. /* 11n state */
  204. u8 valid_phy_ratecnt[WLAN_RC_PHY_MAX]; /* valid rate count */
  205. u8 valid_phy_rateidx[WLAN_RC_PHY_MAX][MAX_TX_RATE_TBL];
  206. u8 rc_phy_mode;
  207. u8 rate_max_phy; /* Phy index for the max rate */
  208. u32 rate_max_lastused; /* msec timstamp of when we
  209. last used rateMaxPhy */
  210. u32 probe_interval; /* interval for ratectrl to probe
  211. for other rates */
  212. };
  213. struct ath_rateset {
  214. u8 rs_nrates;
  215. u8 rs_rates[ATH_RATE_MAX];
  216. };
  217. /* per-device state */
  218. struct ath_rate_softc {
  219. /* phy tables that contain rate control data */
  220. const void *hw_rate_table[ATH9K_MODE_MAX];
  221. int fixedrix; /* -1 or index of fixed rate */
  222. };
  223. /* per-node state */
  224. struct ath_rate_node {
  225. struct ath_tx_ratectrl tx_ratectrl; /* rate control state proper */
  226. u32 prev_data_rix; /* rate idx of last data frame */
  227. /* map of rate ix -> negotiated rate set ix */
  228. u8 rixmap[MAX_TX_RATE_TBL];
  229. /* map of ht rate ix -> negotiated rate set ix */
  230. u8 ht_rixmap[MAX_TX_RATE_TBL];
  231. u8 ht_cap; /* ht capabilities */
  232. u8 ant_tx; /* current transmit antenna */
  233. u8 single_stream; /* When TRUE, only single
  234. stream Tx possible */
  235. struct ath_rateset neg_rates; /* Negotiated rates */
  236. struct ath_rateset neg_ht_rates; /* Negotiated HT rates */
  237. struct ath_rate_softc *asc; /* back pointer to atheros softc */
  238. struct ath_vap *avp; /* back pointer to vap */
  239. };
  240. /* Driver data of ieee80211_tx_info */
  241. struct ath_tx_info_priv {
  242. struct ath_rc_series rcs[4];
  243. struct ath_tx_status tx;
  244. int n_frames;
  245. int n_bad_frames;
  246. u8 min_rate;
  247. };
  248. /*
  249. * Attach/detach a rate control module.
  250. */
  251. struct ath_rate_softc *ath_rate_attach(struct ath_hal *ah);
  252. void ath_rate_detach(struct ath_rate_softc *asc);
  253. /*
  254. * Update/reset rate control state for 802.11 state transitions.
  255. * Important mostly as the analog to ath_rate_newassoc when operating
  256. * in station mode.
  257. */
  258. void ath_rc_node_update(struct ieee80211_hw *hw, struct ath_rate_node *rc_priv);
  259. void ath_rate_newstate(struct ath_softc *sc, struct ath_vap *avp);
  260. /*
  261. * Return the tx rate series.
  262. */
  263. void ath_rate_findrate(struct ath_softc *sc, struct ath_rate_node *ath_rc_priv,
  264. int num_tries, int num_rates,
  265. unsigned int rcflag, struct ath_rc_series[],
  266. int *is_probe, int isretry);
  267. /*
  268. * Return rate index for given Dot11 Rate.
  269. */
  270. u8 ath_rate_findrateix(struct ath_softc *sc,
  271. u8 dot11_rate);
  272. /* Routines to register/unregister rate control algorithm */
  273. int ath_rate_control_register(void);
  274. void ath_rate_control_unregister(void);
  275. #endif /* RC_H */