htc_drv_beacon.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2010 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "htc.h"
  17. #define FUDGE 2
  18. static void ath9k_htc_beacon_config_sta(struct ath9k_htc_priv *priv,
  19. struct htc_beacon_config *bss_conf)
  20. {
  21. struct ath_common *common = ath9k_hw_common(priv->ah);
  22. struct ath9k_beacon_state bs;
  23. enum ath9k_int imask = 0;
  24. int dtimperiod, dtimcount, sleepduration;
  25. int cfpperiod, cfpcount, bmiss_timeout;
  26. u32 nexttbtt = 0, intval, tsftu;
  27. __be32 htc_imask = 0;
  28. u64 tsf;
  29. int num_beacons, offset, dtim_dec_count, cfp_dec_count;
  30. int ret;
  31. u8 cmd_rsp;
  32. memset(&bs, 0, sizeof(bs));
  33. intval = bss_conf->beacon_interval & ATH9K_BEACON_PERIOD;
  34. bmiss_timeout = (ATH_DEFAULT_BMISS_LIMIT * bss_conf->beacon_interval);
  35. /*
  36. * Setup dtim and cfp parameters according to
  37. * last beacon we received (which may be none).
  38. */
  39. dtimperiod = bss_conf->dtim_period;
  40. if (dtimperiod <= 0) /* NB: 0 if not known */
  41. dtimperiod = 1;
  42. dtimcount = 1;
  43. if (dtimcount >= dtimperiod) /* NB: sanity check */
  44. dtimcount = 0;
  45. cfpperiod = 1; /* NB: no PCF support yet */
  46. cfpcount = 0;
  47. sleepduration = intval;
  48. if (sleepduration <= 0)
  49. sleepduration = intval;
  50. /*
  51. * Pull nexttbtt forward to reflect the current
  52. * TSF and calculate dtim+cfp state for the result.
  53. */
  54. tsf = ath9k_hw_gettsf64(priv->ah);
  55. tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
  56. num_beacons = tsftu / intval + 1;
  57. offset = tsftu % intval;
  58. nexttbtt = tsftu - offset;
  59. if (offset)
  60. nexttbtt += intval;
  61. /* DTIM Beacon every dtimperiod Beacon */
  62. dtim_dec_count = num_beacons % dtimperiod;
  63. /* CFP every cfpperiod DTIM Beacon */
  64. cfp_dec_count = (num_beacons / dtimperiod) % cfpperiod;
  65. if (dtim_dec_count)
  66. cfp_dec_count++;
  67. dtimcount -= dtim_dec_count;
  68. if (dtimcount < 0)
  69. dtimcount += dtimperiod;
  70. cfpcount -= cfp_dec_count;
  71. if (cfpcount < 0)
  72. cfpcount += cfpperiod;
  73. bs.bs_intval = intval;
  74. bs.bs_nexttbtt = nexttbtt;
  75. bs.bs_dtimperiod = dtimperiod*intval;
  76. bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
  77. bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
  78. bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
  79. bs.bs_cfpmaxduration = 0;
  80. /*
  81. * Calculate the number of consecutive beacons to miss* before taking
  82. * a BMISS interrupt. The configuration is specified in TU so we only
  83. * need calculate based on the beacon interval. Note that we clamp the
  84. * result to at most 15 beacons.
  85. */
  86. if (sleepduration > intval) {
  87. bs.bs_bmissthreshold = ATH_DEFAULT_BMISS_LIMIT / 2;
  88. } else {
  89. bs.bs_bmissthreshold = DIV_ROUND_UP(bmiss_timeout, intval);
  90. if (bs.bs_bmissthreshold > 15)
  91. bs.bs_bmissthreshold = 15;
  92. else if (bs.bs_bmissthreshold <= 0)
  93. bs.bs_bmissthreshold = 1;
  94. }
  95. /*
  96. * Calculate sleep duration. The configuration is given in ms.
  97. * We ensure a multiple of the beacon period is used. Also, if the sleep
  98. * duration is greater than the DTIM period then it makes senses
  99. * to make it a multiple of that.
  100. *
  101. * XXX fixed at 100ms
  102. */
  103. bs.bs_sleepduration = roundup(IEEE80211_MS_TO_TU(100), sleepduration);
  104. if (bs.bs_sleepduration > bs.bs_dtimperiod)
  105. bs.bs_sleepduration = bs.bs_dtimperiod;
  106. /* TSF out of range threshold fixed at 1 second */
  107. bs.bs_tsfoor_threshold = ATH9K_TSFOOR_THRESHOLD;
  108. ath_print(common, ATH_DBG_BEACON, "tsf: %llu tsftu: %u\n", tsf, tsftu);
  109. ath_print(common, ATH_DBG_BEACON,
  110. "bmiss: %u sleep: %u cfp-period: %u maxdur: %u next: %u\n",
  111. bs.bs_bmissthreshold, bs.bs_sleepduration,
  112. bs.bs_cfpperiod, bs.bs_cfpmaxduration, bs.bs_cfpnext);
  113. /* Set the computed STA beacon timers */
  114. WMI_CMD(WMI_DISABLE_INTR_CMDID);
  115. ath9k_hw_set_sta_beacon_timers(priv->ah, &bs);
  116. imask |= ATH9K_INT_BMISS;
  117. htc_imask = cpu_to_be32(imask);
  118. WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
  119. }
  120. static void ath9k_htc_beacon_config_adhoc(struct ath9k_htc_priv *priv,
  121. struct htc_beacon_config *bss_conf)
  122. {
  123. struct ath_common *common = ath9k_hw_common(priv->ah);
  124. enum ath9k_int imask = 0;
  125. u32 nexttbtt, intval;
  126. __be32 htc_imask = 0;
  127. int ret;
  128. u8 cmd_rsp;
  129. intval = bss_conf->beacon_interval & ATH9K_BEACON_PERIOD;
  130. nexttbtt = intval;
  131. intval |= ATH9K_BEACON_ENA;
  132. if (priv->op_flags & OP_ENABLE_BEACON)
  133. imask |= ATH9K_INT_SWBA;
  134. ath_print(common, ATH_DBG_BEACON,
  135. "IBSS Beacon config, intval: %d, imask: 0x%x\n",
  136. bss_conf->beacon_interval, imask);
  137. WMI_CMD(WMI_DISABLE_INTR_CMDID);
  138. ath9k_hw_beaconinit(priv->ah, nexttbtt, intval);
  139. priv->bmiss_cnt = 0;
  140. htc_imask = cpu_to_be32(imask);
  141. WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
  142. }
  143. void ath9k_htc_beaconep(void *drv_priv, struct sk_buff *skb,
  144. enum htc_endpoint_id ep_id, bool txok)
  145. {
  146. dev_kfree_skb_any(skb);
  147. }
  148. void ath9k_htc_swba(struct ath9k_htc_priv *priv, u8 beacon_pending)
  149. {
  150. struct ath9k_htc_vif *avp = (void *)priv->vif->drv_priv;
  151. struct tx_beacon_header beacon_hdr;
  152. struct ath9k_htc_tx_ctl tx_ctl;
  153. struct ieee80211_tx_info *info;
  154. struct sk_buff *beacon;
  155. u8 *tx_fhdr;
  156. memset(&beacon_hdr, 0, sizeof(struct tx_beacon_header));
  157. memset(&tx_ctl, 0, sizeof(struct ath9k_htc_tx_ctl));
  158. /* FIXME: Handle BMISS */
  159. if (beacon_pending != 0) {
  160. priv->bmiss_cnt++;
  161. return;
  162. }
  163. spin_lock_bh(&priv->beacon_lock);
  164. if (unlikely(priv->op_flags & OP_SCANNING)) {
  165. spin_unlock_bh(&priv->beacon_lock);
  166. return;
  167. }
  168. /* Get a new beacon */
  169. beacon = ieee80211_beacon_get(priv->hw, priv->vif);
  170. if (!beacon) {
  171. spin_unlock_bh(&priv->beacon_lock);
  172. return;
  173. }
  174. info = IEEE80211_SKB_CB(beacon);
  175. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  176. struct ieee80211_hdr *hdr =
  177. (struct ieee80211_hdr *) beacon->data;
  178. priv->seq_no += 0x10;
  179. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  180. hdr->seq_ctrl |= cpu_to_le16(priv->seq_no);
  181. }
  182. tx_ctl.type = ATH9K_HTC_NORMAL;
  183. beacon_hdr.vif_index = avp->index;
  184. tx_fhdr = skb_push(beacon, sizeof(beacon_hdr));
  185. memcpy(tx_fhdr, (u8 *) &beacon_hdr, sizeof(beacon_hdr));
  186. htc_send(priv->htc, beacon, priv->beacon_ep, &tx_ctl);
  187. spin_unlock_bh(&priv->beacon_lock);
  188. }
  189. /* Currently, only for IBSS */
  190. void ath9k_htc_beaconq_config(struct ath9k_htc_priv *priv)
  191. {
  192. struct ath_hw *ah = priv->ah;
  193. struct ath9k_tx_queue_info qi, qi_be;
  194. int qnum = priv->hwq_map[WME_AC_BE];
  195. memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
  196. memset(&qi_be, 0, sizeof(struct ath9k_tx_queue_info));
  197. ath9k_hw_get_txq_props(ah, qnum, &qi_be);
  198. qi.tqi_aifs = qi_be.tqi_aifs;
  199. /* For WIFI Beacon Distribution
  200. * Long slot time : 2x cwmin
  201. * Short slot time : 4x cwmin
  202. */
  203. if (ah->slottime == ATH9K_SLOT_TIME_20)
  204. qi.tqi_cwmin = 2*qi_be.tqi_cwmin;
  205. else
  206. qi.tqi_cwmin = 4*qi_be.tqi_cwmin;
  207. qi.tqi_cwmax = qi_be.tqi_cwmax;
  208. if (!ath9k_hw_set_txq_props(ah, priv->beaconq, &qi)) {
  209. ath_print(ath9k_hw_common(ah), ATH_DBG_FATAL,
  210. "Unable to update beacon queue %u!\n", qnum);
  211. } else {
  212. ath9k_hw_resettxqueue(ah, priv->beaconq);
  213. }
  214. }
  215. void ath9k_htc_beacon_config(struct ath9k_htc_priv *priv,
  216. struct ieee80211_vif *vif)
  217. {
  218. struct ath_common *common = ath9k_hw_common(priv->ah);
  219. struct htc_beacon_config *cur_conf = &priv->cur_beacon_conf;
  220. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  221. cur_conf->beacon_interval = bss_conf->beacon_int;
  222. if (cur_conf->beacon_interval == 0)
  223. cur_conf->beacon_interval = 100;
  224. cur_conf->dtim_period = bss_conf->dtim_period;
  225. cur_conf->listen_interval = 1;
  226. cur_conf->dtim_count = 1;
  227. cur_conf->bmiss_timeout =
  228. ATH_DEFAULT_BMISS_LIMIT * cur_conf->beacon_interval;
  229. switch (vif->type) {
  230. case NL80211_IFTYPE_STATION:
  231. ath9k_htc_beacon_config_sta(priv, cur_conf);
  232. break;
  233. case NL80211_IFTYPE_ADHOC:
  234. ath9k_htc_beacon_config_adhoc(priv, cur_conf);
  235. break;
  236. default:
  237. ath_print(common, ATH_DBG_CONFIG,
  238. "Unsupported beaconing mode\n");
  239. return;
  240. }
  241. }