htc_drv_beacon.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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, htc_imask = 0;
  27. u64 tsf;
  28. int num_beacons, offset, dtim_dec_count, cfp_dec_count;
  29. int ret;
  30. u8 cmd_rsp;
  31. memset(&bs, 0, sizeof(bs));
  32. intval = bss_conf->beacon_interval & ATH9K_BEACON_PERIOD;
  33. bmiss_timeout = (ATH_DEFAULT_BMISS_LIMIT * bss_conf->beacon_interval);
  34. /*
  35. * Setup dtim and cfp parameters according to
  36. * last beacon we received (which may be none).
  37. */
  38. dtimperiod = bss_conf->dtim_period;
  39. if (dtimperiod <= 0) /* NB: 0 if not known */
  40. dtimperiod = 1;
  41. dtimcount = 1;
  42. if (dtimcount >= dtimperiod) /* NB: sanity check */
  43. dtimcount = 0;
  44. cfpperiod = 1; /* NB: no PCF support yet */
  45. cfpcount = 0;
  46. sleepduration = intval;
  47. if (sleepduration <= 0)
  48. sleepduration = intval;
  49. /*
  50. * Pull nexttbtt forward to reflect the current
  51. * TSF and calculate dtim+cfp state for the result.
  52. */
  53. tsf = ath9k_hw_gettsf64(priv->ah);
  54. tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
  55. num_beacons = tsftu / intval + 1;
  56. offset = tsftu % intval;
  57. nexttbtt = tsftu - offset;
  58. if (offset)
  59. nexttbtt += intval;
  60. /* DTIM Beacon every dtimperiod Beacon */
  61. dtim_dec_count = num_beacons % dtimperiod;
  62. /* CFP every cfpperiod DTIM Beacon */
  63. cfp_dec_count = (num_beacons / dtimperiod) % cfpperiod;
  64. if (dtim_dec_count)
  65. cfp_dec_count++;
  66. dtimcount -= dtim_dec_count;
  67. if (dtimcount < 0)
  68. dtimcount += dtimperiod;
  69. cfpcount -= cfp_dec_count;
  70. if (cfpcount < 0)
  71. cfpcount += cfpperiod;
  72. bs.bs_intval = intval;
  73. bs.bs_nexttbtt = nexttbtt;
  74. bs.bs_dtimperiod = dtimperiod*intval;
  75. bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
  76. bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
  77. bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
  78. bs.bs_cfpmaxduration = 0;
  79. /*
  80. * Calculate the number of consecutive beacons to miss* before taking
  81. * a BMISS interrupt. The configuration is specified in TU so we only
  82. * need calculate based on the beacon interval. Note that we clamp the
  83. * result to at most 15 beacons.
  84. */
  85. if (sleepduration > intval) {
  86. bs.bs_bmissthreshold = ATH_DEFAULT_BMISS_LIMIT / 2;
  87. } else {
  88. bs.bs_bmissthreshold = DIV_ROUND_UP(bmiss_timeout, intval);
  89. if (bs.bs_bmissthreshold > 15)
  90. bs.bs_bmissthreshold = 15;
  91. else if (bs.bs_bmissthreshold <= 0)
  92. bs.bs_bmissthreshold = 1;
  93. }
  94. /*
  95. * Calculate sleep duration. The configuration is given in ms.
  96. * We ensure a multiple of the beacon period is used. Also, if the sleep
  97. * duration is greater than the DTIM period then it makes senses
  98. * to make it a multiple of that.
  99. *
  100. * XXX fixed at 100ms
  101. */
  102. bs.bs_sleepduration = roundup(IEEE80211_MS_TO_TU(100), sleepduration);
  103. if (bs.bs_sleepduration > bs.bs_dtimperiod)
  104. bs.bs_sleepduration = bs.bs_dtimperiod;
  105. /* TSF out of range threshold fixed at 1 second */
  106. bs.bs_tsfoor_threshold = ATH9K_TSFOOR_THRESHOLD;
  107. ath_print(common, ATH_DBG_BEACON, "tsf: %llu tsftu: %u\n", tsf, tsftu);
  108. ath_print(common, ATH_DBG_BEACON,
  109. "bmiss: %u sleep: %u cfp-period: %u maxdur: %u next: %u\n",
  110. bs.bs_bmissthreshold, bs.bs_sleepduration,
  111. bs.bs_cfpperiod, bs.bs_cfpmaxduration, bs.bs_cfpnext);
  112. /* Set the computed STA beacon timers */
  113. WMI_CMD(WMI_DISABLE_INTR_CMDID);
  114. ath9k_hw_set_sta_beacon_timers(priv->ah, &bs);
  115. imask |= ATH9K_INT_BMISS;
  116. htc_imask = cpu_to_be32(imask);
  117. WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
  118. }
  119. static void ath9k_htc_beacon_config_adhoc(struct ath9k_htc_priv *priv,
  120. struct htc_beacon_config *bss_conf)
  121. {
  122. struct ath_common *common = ath9k_hw_common(priv->ah);
  123. enum ath9k_int imask = 0;
  124. u32 nexttbtt, intval, htc_imask = 0;
  125. int ret;
  126. u8 cmd_rsp;
  127. intval = bss_conf->beacon_interval & ATH9K_BEACON_PERIOD;
  128. nexttbtt = intval;
  129. intval |= ATH9K_BEACON_ENA;
  130. if (priv->op_flags & OP_ENABLE_BEACON)
  131. imask |= ATH9K_INT_SWBA;
  132. ath_print(common, ATH_DBG_BEACON,
  133. "IBSS Beacon config, intval: %d, imask: 0x%x\n",
  134. bss_conf->beacon_interval, imask);
  135. WMI_CMD(WMI_DISABLE_INTR_CMDID);
  136. ath9k_hw_beaconinit(priv->ah, nexttbtt, intval);
  137. priv->bmiss_cnt = 0;
  138. htc_imask = cpu_to_be32(imask);
  139. WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
  140. }
  141. void ath9k_htc_beacon_update(struct ath9k_htc_priv *priv,
  142. struct ieee80211_vif *vif)
  143. {
  144. struct ath_common *common = ath9k_hw_common(priv->ah);
  145. spin_lock_bh(&priv->beacon_lock);
  146. if (priv->beacon)
  147. dev_kfree_skb_any(priv->beacon);
  148. priv->beacon = ieee80211_beacon_get(priv->hw, vif);
  149. if (!priv->beacon)
  150. ath_print(common, ATH_DBG_BEACON,
  151. "Unable to allocate beacon\n");
  152. spin_unlock_bh(&priv->beacon_lock);
  153. }
  154. void ath9k_htc_swba(struct ath9k_htc_priv *priv, u8 beacon_pending)
  155. {
  156. struct ath9k_htc_vif *avp = (void *)priv->vif->drv_priv;
  157. struct tx_beacon_header beacon_hdr;
  158. struct ath9k_htc_tx_ctl tx_ctl;
  159. struct ieee80211_tx_info *info;
  160. u8 *tx_fhdr;
  161. memset(&beacon_hdr, 0, sizeof(struct tx_beacon_header));
  162. memset(&tx_ctl, 0, sizeof(struct ath9k_htc_tx_ctl));
  163. /* FIXME: Handle BMISS */
  164. if (beacon_pending != 0) {
  165. priv->bmiss_cnt++;
  166. return;
  167. }
  168. spin_lock_bh(&priv->beacon_lock);
  169. if (unlikely(priv->op_flags & OP_SCANNING)) {
  170. spin_unlock_bh(&priv->beacon_lock);
  171. return;
  172. }
  173. if (unlikely(priv->beacon == NULL)) {
  174. spin_unlock_bh(&priv->beacon_lock);
  175. return;
  176. }
  177. /* Free the old SKB first */
  178. dev_kfree_skb_any(priv->beacon);
  179. /* Get a new beacon */
  180. priv->beacon = ieee80211_beacon_get(priv->hw, priv->vif);
  181. if (!priv->beacon) {
  182. spin_unlock_bh(&priv->beacon_lock);
  183. return;
  184. }
  185. info = IEEE80211_SKB_CB(priv->beacon);
  186. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  187. struct ieee80211_hdr *hdr =
  188. (struct ieee80211_hdr *) priv->beacon->data;
  189. priv->seq_no += 0x10;
  190. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  191. hdr->seq_ctrl |= cpu_to_le16(priv->seq_no);
  192. }
  193. tx_ctl.type = ATH9K_HTC_NORMAL;
  194. beacon_hdr.vif_index = avp->index;
  195. tx_fhdr = skb_push(priv->beacon, sizeof(beacon_hdr));
  196. memcpy(tx_fhdr, (u8 *) &beacon_hdr, sizeof(beacon_hdr));
  197. htc_send(priv->htc, priv->beacon, priv->beacon_ep, &tx_ctl);
  198. spin_unlock_bh(&priv->beacon_lock);
  199. }
  200. void ath9k_htc_beacon_config(struct ath9k_htc_priv *priv,
  201. struct ieee80211_vif *vif)
  202. {
  203. struct ath_common *common = ath9k_hw_common(priv->ah);
  204. enum nl80211_iftype iftype;
  205. struct htc_beacon_config *cur_conf = &priv->cur_beacon_conf;
  206. if (vif) {
  207. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  208. iftype = vif->type;
  209. cur_conf->beacon_interval = bss_conf->beacon_int;
  210. cur_conf->dtim_period = bss_conf->dtim_period;
  211. cur_conf->listen_interval = 1;
  212. cur_conf->dtim_count = 1;
  213. cur_conf->bmiss_timeout =
  214. ATH_DEFAULT_BMISS_LIMIT * cur_conf->beacon_interval;
  215. } else
  216. iftype = priv->ah->opmode;
  217. if (cur_conf->beacon_interval == 0)
  218. cur_conf->beacon_interval = 100;
  219. switch (iftype) {
  220. case NL80211_IFTYPE_STATION:
  221. ath9k_htc_beacon_config_sta(priv, cur_conf);
  222. break;
  223. case NL80211_IFTYPE_ADHOC:
  224. ath9k_htc_beacon_config_adhoc(priv, cur_conf);
  225. break;
  226. default:
  227. ath_print(common, ATH_DBG_CONFIG,
  228. "Unsupported beaconing mode\n");
  229. return;
  230. }
  231. }