common-beacon.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2008-2011 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 "common.h"
  17. #define FUDGE 2
  18. /* Calculate the modulo of a 64 bit TSF snapshot with a TU divisor */
  19. static u32 ath9k_mod_tsf64_tu(u64 tsf, u32 div_tu)
  20. {
  21. u32 tsf_mod, tsf_hi, tsf_lo, mod_hi, mod_lo;
  22. tsf_mod = tsf & (BIT(10) - 1);
  23. tsf_hi = tsf >> 32;
  24. tsf_lo = ((u32) tsf) >> 10;
  25. mod_hi = tsf_hi % div_tu;
  26. mod_lo = ((mod_hi << 22) + tsf_lo) % div_tu;
  27. return (mod_lo << 10) | tsf_mod;
  28. }
  29. static u32 ath9k_get_next_tbtt(struct ath_hw *ah, u64 tsf,
  30. unsigned int interval)
  31. {
  32. unsigned int offset;
  33. tsf += TU_TO_USEC(FUDGE + ah->config.sw_beacon_response_time);
  34. offset = ath9k_mod_tsf64_tu(tsf, interval);
  35. return (u32) tsf + TU_TO_USEC(interval) - offset;
  36. }
  37. /*
  38. * This sets up the beacon timers according to the timestamp of the last
  39. * received beacon and the current TSF, configures PCF and DTIM
  40. * handling, programs the sleep registers so the hardware will wakeup in
  41. * time to receive beacons, and configures the beacon miss handling so
  42. * we'll receive a BMISS interrupt when we stop seeing beacons from the AP
  43. * we've associated with.
  44. */
  45. int ath9k_cmn_beacon_config_sta(struct ath_hw *ah,
  46. struct ath_beacon_config *conf,
  47. struct ath9k_beacon_state *bs)
  48. {
  49. struct ath_common *common = ath9k_hw_common(ah);
  50. int dtim_intval;
  51. u64 tsf;
  52. /* No need to configure beacon if we are not associated */
  53. if (!test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
  54. ath_dbg(common, BEACON,
  55. "STA is not yet associated..skipping beacon config\n");
  56. return -EPERM;
  57. }
  58. memset(bs, 0, sizeof(*bs));
  59. conf->intval = conf->beacon_interval;
  60. /*
  61. * Setup dtim parameters according to
  62. * last beacon we received (which may be none).
  63. */
  64. dtim_intval = conf->intval * conf->dtim_period;
  65. /*
  66. * Pull nexttbtt forward to reflect the current
  67. * TSF and calculate dtim state for the result.
  68. */
  69. tsf = ath9k_hw_gettsf64(ah);
  70. conf->nexttbtt = ath9k_get_next_tbtt(ah, tsf, conf->intval);
  71. bs->bs_intval = TU_TO_USEC(conf->intval);
  72. bs->bs_dtimperiod = conf->dtim_period * bs->bs_intval;
  73. bs->bs_nexttbtt = conf->nexttbtt;
  74. bs->bs_nextdtim = conf->nexttbtt;
  75. if (conf->dtim_period > 1)
  76. bs->bs_nextdtim = ath9k_get_next_tbtt(ah, tsf, dtim_intval);
  77. /*
  78. * Calculate the number of consecutive beacons to miss* before taking
  79. * a BMISS interrupt. The configuration is specified in TU so we only
  80. * need calculate based on the beacon interval. Note that we clamp the
  81. * result to at most 15 beacons.
  82. */
  83. bs->bs_bmissthreshold = DIV_ROUND_UP(conf->bmiss_timeout, conf->intval);
  84. if (bs->bs_bmissthreshold > 15)
  85. bs->bs_bmissthreshold = 15;
  86. else if (bs->bs_bmissthreshold <= 0)
  87. bs->bs_bmissthreshold = 1;
  88. /*
  89. * Calculate sleep duration. The configuration is given in ms.
  90. * We ensure a multiple of the beacon period is used. Also, if the sleep
  91. * duration is greater than the DTIM period then it makes senses
  92. * to make it a multiple of that.
  93. *
  94. * XXX fixed at 100ms
  95. */
  96. bs->bs_sleepduration = TU_TO_USEC(roundup(IEEE80211_MS_TO_TU(100),
  97. conf->intval));
  98. if (bs->bs_sleepduration > bs->bs_dtimperiod)
  99. bs->bs_sleepduration = bs->bs_dtimperiod;
  100. /* TSF out of range threshold fixed at 1 second */
  101. bs->bs_tsfoor_threshold = ATH9K_TSFOOR_THRESHOLD;
  102. ath_dbg(common, BEACON, "bmiss: %u sleep: %u\n",
  103. bs->bs_bmissthreshold, bs->bs_sleepduration);
  104. return 0;
  105. }
  106. EXPORT_SYMBOL(ath9k_cmn_beacon_config_sta);
  107. void ath9k_cmn_beacon_config_adhoc(struct ath_hw *ah,
  108. struct ath_beacon_config *conf)
  109. {
  110. struct ath_common *common = ath9k_hw_common(ah);
  111. conf->intval = TU_TO_USEC(conf->beacon_interval);
  112. if (conf->ibss_creator)
  113. conf->nexttbtt = conf->intval;
  114. else
  115. conf->nexttbtt = ath9k_get_next_tbtt(ah, ath9k_hw_gettsf64(ah),
  116. conf->beacon_interval);
  117. if (conf->enable_beacon)
  118. ah->imask |= ATH9K_INT_SWBA;
  119. else
  120. ah->imask &= ~ATH9K_INT_SWBA;
  121. ath_dbg(common, BEACON,
  122. "IBSS (%s) nexttbtt: %u intval: %u conf_intval: %u\n",
  123. (conf->enable_beacon) ? "Enable" : "Disable",
  124. conf->nexttbtt, conf->intval, conf->beacon_interval);
  125. }
  126. EXPORT_SYMBOL(ath9k_cmn_beacon_config_adhoc);
  127. /*
  128. * For multi-bss ap support beacons are either staggered evenly over N slots or
  129. * burst together. For the former arrange for the SWBA to be delivered for each
  130. * slot. Slots that are not occupied will generate nothing.
  131. */
  132. void ath9k_cmn_beacon_config_ap(struct ath_hw *ah,
  133. struct ath_beacon_config *conf,
  134. unsigned int bc_buf)
  135. {
  136. struct ath_common *common = ath9k_hw_common(ah);
  137. /* NB: the beacon interval is kept internally in TU's */
  138. conf->intval = TU_TO_USEC(conf->beacon_interval);
  139. conf->intval /= bc_buf;
  140. conf->nexttbtt = ath9k_get_next_tbtt(ah, ath9k_hw_gettsf64(ah),
  141. conf->beacon_interval);
  142. if (conf->enable_beacon)
  143. ah->imask |= ATH9K_INT_SWBA;
  144. else
  145. ah->imask &= ~ATH9K_INT_SWBA;
  146. ath_dbg(common, BEACON,
  147. "AP (%s) nexttbtt: %u intval: %u conf_intval: %u\n",
  148. (conf->enable_beacon) ? "Enable" : "Disable",
  149. conf->nexttbtt, conf->intval, conf->beacon_interval);
  150. }
  151. EXPORT_SYMBOL(ath9k_cmn_beacon_config_ap);