htc_drv_beacon.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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_dbg(common, ATH_DBG_CONFIG, "intval: %u tsf: %llu tsftu: %u\n",
  109. intval, tsf, tsftu);
  110. ath_dbg(common, ATH_DBG_CONFIG,
  111. "bmiss: %u sleep: %u cfp-period: %u maxdur: %u next: %u\n",
  112. bs.bs_bmissthreshold, bs.bs_sleepduration,
  113. bs.bs_cfpperiod, bs.bs_cfpmaxduration, bs.bs_cfpnext);
  114. /* Set the computed STA beacon timers */
  115. WMI_CMD(WMI_DISABLE_INTR_CMDID);
  116. ath9k_hw_set_sta_beacon_timers(priv->ah, &bs);
  117. imask |= ATH9K_INT_BMISS;
  118. htc_imask = cpu_to_be32(imask);
  119. WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
  120. }
  121. static void ath9k_htc_beacon_config_ap(struct ath9k_htc_priv *priv,
  122. struct htc_beacon_config *bss_conf)
  123. {
  124. struct ath_common *common = ath9k_hw_common(priv->ah);
  125. enum ath9k_int imask = 0;
  126. u32 nexttbtt, intval, tsftu;
  127. __be32 htc_imask = 0;
  128. int ret;
  129. u8 cmd_rsp;
  130. u64 tsf;
  131. intval = bss_conf->beacon_interval & ATH9K_BEACON_PERIOD;
  132. intval /= ATH9K_HTC_MAX_BCN_VIF;
  133. nexttbtt = intval;
  134. if (priv->op_flags & OP_TSF_RESET) {
  135. intval |= ATH9K_BEACON_RESET_TSF;
  136. priv->op_flags &= ~OP_TSF_RESET;
  137. } else {
  138. /*
  139. * Pull nexttbtt forward to reflect the current TSF.
  140. */
  141. tsf = ath9k_hw_gettsf64(priv->ah);
  142. tsftu = TSF_TO_TU(tsf >> 32, tsf) + FUDGE;
  143. do {
  144. nexttbtt += intval;
  145. } while (nexttbtt < tsftu);
  146. }
  147. intval |= ATH9K_BEACON_ENA;
  148. if (priv->op_flags & OP_ENABLE_BEACON)
  149. imask |= ATH9K_INT_SWBA;
  150. ath_dbg(common, ATH_DBG_CONFIG,
  151. "AP Beacon config, intval: %d, nexttbtt: %u imask: 0x%x\n",
  152. bss_conf->beacon_interval, nexttbtt, imask);
  153. WMI_CMD(WMI_DISABLE_INTR_CMDID);
  154. ath9k_hw_beaconinit(priv->ah, nexttbtt, intval);
  155. priv->bmiss_cnt = 0;
  156. htc_imask = cpu_to_be32(imask);
  157. WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
  158. }
  159. static void ath9k_htc_beacon_config_adhoc(struct ath9k_htc_priv *priv,
  160. struct htc_beacon_config *bss_conf)
  161. {
  162. struct ath_common *common = ath9k_hw_common(priv->ah);
  163. enum ath9k_int imask = 0;
  164. u32 nexttbtt, intval, tsftu;
  165. __be32 htc_imask = 0;
  166. int ret;
  167. u8 cmd_rsp;
  168. u64 tsf;
  169. intval = bss_conf->beacon_interval & ATH9K_BEACON_PERIOD;
  170. nexttbtt = intval;
  171. /*
  172. * Pull nexttbtt forward to reflect the current TSF.
  173. */
  174. tsf = ath9k_hw_gettsf64(priv->ah);
  175. tsftu = TSF_TO_TU(tsf >> 32, tsf) + FUDGE;
  176. do {
  177. nexttbtt += intval;
  178. } while (nexttbtt < tsftu);
  179. intval |= ATH9K_BEACON_ENA;
  180. if (priv->op_flags & OP_ENABLE_BEACON)
  181. imask |= ATH9K_INT_SWBA;
  182. ath_dbg(common, ATH_DBG_CONFIG,
  183. "IBSS Beacon config, intval: %d, nexttbtt: %u, imask: 0x%x\n",
  184. bss_conf->beacon_interval, nexttbtt, imask);
  185. WMI_CMD(WMI_DISABLE_INTR_CMDID);
  186. ath9k_hw_beaconinit(priv->ah, nexttbtt, intval);
  187. priv->bmiss_cnt = 0;
  188. htc_imask = cpu_to_be32(imask);
  189. WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
  190. }
  191. void ath9k_htc_beaconep(void *drv_priv, struct sk_buff *skb,
  192. enum htc_endpoint_id ep_id, bool txok)
  193. {
  194. dev_kfree_skb_any(skb);
  195. }
  196. void ath9k_htc_swba(struct ath9k_htc_priv *priv, u8 beacon_pending)
  197. {
  198. struct ath9k_htc_vif *avp = (void *)priv->vif->drv_priv;
  199. struct tx_beacon_header beacon_hdr;
  200. struct ath9k_htc_tx_ctl tx_ctl;
  201. struct ieee80211_tx_info *info;
  202. struct sk_buff *beacon;
  203. u8 *tx_fhdr;
  204. memset(&beacon_hdr, 0, sizeof(struct tx_beacon_header));
  205. memset(&tx_ctl, 0, sizeof(struct ath9k_htc_tx_ctl));
  206. /* FIXME: Handle BMISS */
  207. if (beacon_pending != 0) {
  208. priv->bmiss_cnt++;
  209. return;
  210. }
  211. spin_lock_bh(&priv->beacon_lock);
  212. if (unlikely(priv->op_flags & OP_SCANNING)) {
  213. spin_unlock_bh(&priv->beacon_lock);
  214. return;
  215. }
  216. /* Get a new beacon */
  217. beacon = ieee80211_beacon_get(priv->hw, priv->vif);
  218. if (!beacon) {
  219. spin_unlock_bh(&priv->beacon_lock);
  220. return;
  221. }
  222. info = IEEE80211_SKB_CB(beacon);
  223. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  224. struct ieee80211_hdr *hdr =
  225. (struct ieee80211_hdr *) beacon->data;
  226. avp->seq_no += 0x10;
  227. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  228. hdr->seq_ctrl |= cpu_to_le16(avp->seq_no);
  229. }
  230. tx_ctl.type = ATH9K_HTC_NORMAL;
  231. beacon_hdr.vif_index = avp->index;
  232. tx_fhdr = skb_push(beacon, sizeof(beacon_hdr));
  233. memcpy(tx_fhdr, (u8 *) &beacon_hdr, sizeof(beacon_hdr));
  234. htc_send(priv->htc, beacon, priv->beacon_ep, &tx_ctl);
  235. spin_unlock_bh(&priv->beacon_lock);
  236. }
  237. /* Currently, only for IBSS */
  238. void ath9k_htc_beaconq_config(struct ath9k_htc_priv *priv)
  239. {
  240. struct ath_hw *ah = priv->ah;
  241. struct ath9k_tx_queue_info qi, qi_be;
  242. int qnum = priv->hwq_map[WME_AC_BE];
  243. memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
  244. memset(&qi_be, 0, sizeof(struct ath9k_tx_queue_info));
  245. ath9k_hw_get_txq_props(ah, qnum, &qi_be);
  246. qi.tqi_aifs = qi_be.tqi_aifs;
  247. /* For WIFI Beacon Distribution
  248. * Long slot time : 2x cwmin
  249. * Short slot time : 4x cwmin
  250. */
  251. if (ah->slottime == ATH9K_SLOT_TIME_20)
  252. qi.tqi_cwmin = 2*qi_be.tqi_cwmin;
  253. else
  254. qi.tqi_cwmin = 4*qi_be.tqi_cwmin;
  255. qi.tqi_cwmax = qi_be.tqi_cwmax;
  256. if (!ath9k_hw_set_txq_props(ah, priv->beaconq, &qi)) {
  257. ath_err(ath9k_hw_common(ah),
  258. "Unable to update beacon queue %u!\n", qnum);
  259. } else {
  260. ath9k_hw_resettxqueue(ah, priv->beaconq);
  261. }
  262. }
  263. static void ath9k_htc_beacon_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
  264. {
  265. bool *beacon_configured = (bool *)data;
  266. struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
  267. if (vif->type == NL80211_IFTYPE_STATION &&
  268. avp->beacon_configured)
  269. *beacon_configured = true;
  270. }
  271. static bool ath9k_htc_check_beacon_config(struct ath9k_htc_priv *priv,
  272. struct ieee80211_vif *vif)
  273. {
  274. struct ath_common *common = ath9k_hw_common(priv->ah);
  275. struct htc_beacon_config *cur_conf = &priv->cur_beacon_conf;
  276. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  277. bool beacon_configured;
  278. /*
  279. * Changing the beacon interval when multiple AP interfaces
  280. * are configured will affect beacon transmission of all
  281. * of them.
  282. */
  283. if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
  284. (priv->num_ap_vif > 1) &&
  285. (vif->type == NL80211_IFTYPE_AP) &&
  286. (cur_conf->beacon_interval != bss_conf->beacon_int)) {
  287. ath_dbg(common, ATH_DBG_CONFIG,
  288. "Changing beacon interval of multiple AP interfaces !\n");
  289. return false;
  290. }
  291. /*
  292. * If the HW is operating in AP mode, any new station interfaces that
  293. * are added cannot change the beacon parameters.
  294. */
  295. if (priv->num_ap_vif &&
  296. (vif->type != NL80211_IFTYPE_AP)) {
  297. ath_dbg(common, ATH_DBG_CONFIG,
  298. "HW in AP mode, cannot set STA beacon parameters\n");
  299. return false;
  300. }
  301. /*
  302. * The beacon parameters are configured only for the first
  303. * station interface.
  304. */
  305. if ((priv->ah->opmode == NL80211_IFTYPE_STATION) &&
  306. (priv->num_sta_vif > 1) &&
  307. (vif->type == NL80211_IFTYPE_STATION)) {
  308. beacon_configured = false;
  309. ieee80211_iterate_active_interfaces_atomic(priv->hw,
  310. ath9k_htc_beacon_iter,
  311. &beacon_configured);
  312. if (beacon_configured) {
  313. ath_dbg(common, ATH_DBG_CONFIG,
  314. "Beacon already configured for a station interface\n");
  315. return false;
  316. }
  317. }
  318. return true;
  319. }
  320. void ath9k_htc_beacon_config(struct ath9k_htc_priv *priv,
  321. struct ieee80211_vif *vif)
  322. {
  323. struct ath_common *common = ath9k_hw_common(priv->ah);
  324. struct htc_beacon_config *cur_conf = &priv->cur_beacon_conf;
  325. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  326. struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
  327. if (!ath9k_htc_check_beacon_config(priv, vif))
  328. return;
  329. cur_conf->beacon_interval = bss_conf->beacon_int;
  330. if (cur_conf->beacon_interval == 0)
  331. cur_conf->beacon_interval = 100;
  332. cur_conf->dtim_period = bss_conf->dtim_period;
  333. cur_conf->bmiss_timeout =
  334. ATH_DEFAULT_BMISS_LIMIT * cur_conf->beacon_interval;
  335. switch (vif->type) {
  336. case NL80211_IFTYPE_STATION:
  337. ath9k_htc_beacon_config_sta(priv, cur_conf);
  338. avp->beacon_configured = true;
  339. break;
  340. case NL80211_IFTYPE_ADHOC:
  341. ath9k_htc_beacon_config_adhoc(priv, cur_conf);
  342. break;
  343. case NL80211_IFTYPE_AP:
  344. ath9k_htc_beacon_config_ap(priv, cur_conf);
  345. break;
  346. default:
  347. ath_dbg(common, ATH_DBG_CONFIG,
  348. "Unsupported beaconing mode\n");
  349. return;
  350. }
  351. }
  352. void ath9k_htc_beacon_reconfig(struct ath9k_htc_priv *priv)
  353. {
  354. struct ath_common *common = ath9k_hw_common(priv->ah);
  355. struct htc_beacon_config *cur_conf = &priv->cur_beacon_conf;
  356. switch (priv->ah->opmode) {
  357. case NL80211_IFTYPE_STATION:
  358. ath9k_htc_beacon_config_sta(priv, cur_conf);
  359. break;
  360. case NL80211_IFTYPE_ADHOC:
  361. ath9k_htc_beacon_config_adhoc(priv, cur_conf);
  362. break;
  363. case NL80211_IFTYPE_AP:
  364. ath9k_htc_beacon_config_ap(priv, cur_conf);
  365. break;
  366. default:
  367. ath_dbg(common, ATH_DBG_CONFIG,
  368. "Unsupported beaconing mode\n");
  369. return;
  370. }
  371. }