2 * Copyright (c) 2010 Atheros Communications Inc.
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.
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.
21 void ath9k_htc_beaconq_config(struct ath9k_htc_priv *priv)
23 struct ath_hw *ah = priv->ah;
24 struct ath9k_tx_queue_info qi, qi_be;
26 memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
27 memset(&qi_be, 0, sizeof(struct ath9k_tx_queue_info));
29 ath9k_hw_get_txq_props(ah, priv->beaconq, &qi);
31 if (priv->ah->opmode == NL80211_IFTYPE_AP) {
35 } else if (priv->ah->opmode == NL80211_IFTYPE_ADHOC) {
36 int qnum = priv->hwq_map[WME_AC_BE];
38 ath9k_hw_get_txq_props(ah, qnum, &qi_be);
40 qi.tqi_aifs = qi_be.tqi_aifs;
43 * For WIFI Beacon Distribution
44 * Long slot time : 2x cwmin
45 * Short slot time : 4x cwmin
47 if (ah->slottime == ATH9K_SLOT_TIME_20)
48 qi.tqi_cwmin = 2*qi_be.tqi_cwmin;
50 qi.tqi_cwmin = 4*qi_be.tqi_cwmin;
52 qi.tqi_cwmax = qi_be.tqi_cwmax;
56 if (!ath9k_hw_set_txq_props(ah, priv->beaconq, &qi)) {
57 ath_err(ath9k_hw_common(ah),
58 "Unable to update beacon queue %u!\n", priv->beaconq);
60 ath9k_hw_resettxqueue(ah, priv->beaconq);
65 static void ath9k_htc_beacon_config_sta(struct ath9k_htc_priv *priv,
66 struct htc_beacon_config *bss_conf)
68 struct ath_common *common = ath9k_hw_common(priv->ah);
69 struct ath9k_beacon_state bs;
70 enum ath9k_int imask = 0;
71 int dtimperiod, dtimcount, sleepduration;
72 int cfpperiod, cfpcount, bmiss_timeout;
73 u32 nexttbtt = 0, intval, tsftu;
76 int num_beacons, offset, dtim_dec_count, cfp_dec_count;
80 memset(&bs, 0, sizeof(bs));
82 intval = bss_conf->beacon_interval & ATH9K_BEACON_PERIOD;
83 bmiss_timeout = (ATH_DEFAULT_BMISS_LIMIT * bss_conf->beacon_interval);
86 * Setup dtim and cfp parameters according to
87 * last beacon we received (which may be none).
89 dtimperiod = bss_conf->dtim_period;
90 if (dtimperiod <= 0) /* NB: 0 if not known */
93 if (dtimcount >= dtimperiod) /* NB: sanity check */
95 cfpperiod = 1; /* NB: no PCF support yet */
98 sleepduration = intval;
99 if (sleepduration <= 0)
100 sleepduration = intval;
103 * Pull nexttbtt forward to reflect the current
104 * TSF and calculate dtim+cfp state for the result.
106 tsf = ath9k_hw_gettsf64(priv->ah);
107 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
109 num_beacons = tsftu / intval + 1;
110 offset = tsftu % intval;
111 nexttbtt = tsftu - offset;
115 /* DTIM Beacon every dtimperiod Beacon */
116 dtim_dec_count = num_beacons % dtimperiod;
117 /* CFP every cfpperiod DTIM Beacon */
118 cfp_dec_count = (num_beacons / dtimperiod) % cfpperiod;
122 dtimcount -= dtim_dec_count;
124 dtimcount += dtimperiod;
126 cfpcount -= cfp_dec_count;
128 cfpcount += cfpperiod;
130 bs.bs_intval = intval;
131 bs.bs_nexttbtt = nexttbtt;
132 bs.bs_dtimperiod = dtimperiod*intval;
133 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
134 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
135 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
136 bs.bs_cfpmaxduration = 0;
139 * Calculate the number of consecutive beacons to miss* before taking
140 * a BMISS interrupt. The configuration is specified in TU so we only
141 * need calculate based on the beacon interval. Note that we clamp the
142 * result to at most 15 beacons.
144 if (sleepduration > intval) {
145 bs.bs_bmissthreshold = ATH_DEFAULT_BMISS_LIMIT / 2;
147 bs.bs_bmissthreshold = DIV_ROUND_UP(bmiss_timeout, intval);
148 if (bs.bs_bmissthreshold > 15)
149 bs.bs_bmissthreshold = 15;
150 else if (bs.bs_bmissthreshold <= 0)
151 bs.bs_bmissthreshold = 1;
155 * Calculate sleep duration. The configuration is given in ms.
156 * We ensure a multiple of the beacon period is used. Also, if the sleep
157 * duration is greater than the DTIM period then it makes senses
158 * to make it a multiple of that.
163 bs.bs_sleepduration = roundup(IEEE80211_MS_TO_TU(100), sleepduration);
164 if (bs.bs_sleepduration > bs.bs_dtimperiod)
165 bs.bs_sleepduration = bs.bs_dtimperiod;
167 /* TSF out of range threshold fixed at 1 second */
168 bs.bs_tsfoor_threshold = ATH9K_TSFOOR_THRESHOLD;
170 ath_dbg(common, ATH_DBG_CONFIG, "intval: %u tsf: %llu tsftu: %u\n",
172 ath_dbg(common, ATH_DBG_CONFIG,
173 "bmiss: %u sleep: %u cfp-period: %u maxdur: %u next: %u\n",
174 bs.bs_bmissthreshold, bs.bs_sleepduration,
175 bs.bs_cfpperiod, bs.bs_cfpmaxduration, bs.bs_cfpnext);
177 /* Set the computed STA beacon timers */
179 WMI_CMD(WMI_DISABLE_INTR_CMDID);
180 ath9k_hw_set_sta_beacon_timers(priv->ah, &bs);
181 imask |= ATH9K_INT_BMISS;
182 htc_imask = cpu_to_be32(imask);
183 WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
186 static void ath9k_htc_beacon_config_ap(struct ath9k_htc_priv *priv,
187 struct htc_beacon_config *bss_conf)
189 struct ath_common *common = ath9k_hw_common(priv->ah);
190 enum ath9k_int imask = 0;
191 u32 nexttbtt, intval, tsftu;
192 __be32 htc_imask = 0;
197 intval = bss_conf->beacon_interval & ATH9K_BEACON_PERIOD;
198 intval /= ATH9K_HTC_MAX_BCN_VIF;
201 if (priv->op_flags & OP_TSF_RESET) {
202 ath9k_hw_reset_tsf(priv->ah);
203 priv->op_flags &= ~OP_TSF_RESET;
206 * Pull nexttbtt forward to reflect the current TSF.
208 tsf = ath9k_hw_gettsf64(priv->ah);
209 tsftu = TSF_TO_TU(tsf >> 32, tsf) + FUDGE;
212 } while (nexttbtt < tsftu);
215 if (priv->op_flags & OP_ENABLE_BEACON)
216 imask |= ATH9K_INT_SWBA;
218 ath_dbg(common, ATH_DBG_CONFIG,
219 "AP Beacon config, intval: %d, nexttbtt: %u "
221 bss_conf->beacon_interval, nexttbtt, imask);
223 ath9k_htc_beaconq_config(priv);
225 WMI_CMD(WMI_DISABLE_INTR_CMDID);
226 ath9k_hw_beaconinit(priv->ah, TU_TO_USEC(nexttbtt), TU_TO_USEC(intval));
227 priv->cur_beacon_conf.bmiss_cnt = 0;
228 htc_imask = cpu_to_be32(imask);
229 WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
232 static void ath9k_htc_beacon_config_adhoc(struct ath9k_htc_priv *priv,
233 struct htc_beacon_config *bss_conf)
235 struct ath_common *common = ath9k_hw_common(priv->ah);
236 enum ath9k_int imask = 0;
237 u32 nexttbtt, intval, tsftu;
238 __be32 htc_imask = 0;
243 intval = bss_conf->beacon_interval & ATH9K_BEACON_PERIOD;
247 * Pull nexttbtt forward to reflect the current TSF.
249 tsf = ath9k_hw_gettsf64(priv->ah);
250 tsftu = TSF_TO_TU(tsf >> 32, tsf) + FUDGE;
253 } while (nexttbtt < tsftu);
255 if (priv->op_flags & OP_ENABLE_BEACON)
256 imask |= ATH9K_INT_SWBA;
258 ath_dbg(common, ATH_DBG_CONFIG,
259 "IBSS Beacon config, intval: %d, nexttbtt: %u, imask: 0x%x\n",
260 bss_conf->beacon_interval, nexttbtt, imask);
262 WMI_CMD(WMI_DISABLE_INTR_CMDID);
263 ath9k_hw_beaconinit(priv->ah, TU_TO_USEC(nexttbtt), TU_TO_USEC(intval));
264 priv->cur_beacon_conf.bmiss_cnt = 0;
265 htc_imask = cpu_to_be32(imask);
266 WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
269 void ath9k_htc_beaconep(void *drv_priv, struct sk_buff *skb,
270 enum htc_endpoint_id ep_id, bool txok)
272 dev_kfree_skb_any(skb);
275 static void ath9k_htc_send_beacon(struct ath9k_htc_priv *priv,
278 struct ieee80211_vif *vif;
279 struct ath9k_htc_vif *avp;
280 struct tx_beacon_header beacon_hdr;
281 struct ath9k_htc_tx_ctl tx_ctl;
282 struct ieee80211_tx_info *info;
283 struct ieee80211_mgmt *mgmt;
284 struct sk_buff *beacon;
287 memset(&beacon_hdr, 0, sizeof(struct tx_beacon_header));
288 memset(&tx_ctl, 0, sizeof(struct ath9k_htc_tx_ctl));
290 spin_lock_bh(&priv->beacon_lock);
292 vif = priv->cur_beacon_conf.bslot[slot];
293 avp = (struct ath9k_htc_vif *)vif->drv_priv;
295 if (unlikely(priv->op_flags & OP_SCANNING)) {
296 spin_unlock_bh(&priv->beacon_lock);
300 /* Get a new beacon */
301 beacon = ieee80211_beacon_get(priv->hw, vif);
303 spin_unlock_bh(&priv->beacon_lock);
308 * Update the TSF adjust value here, the HW will
309 * add this value for every beacon.
311 mgmt = (struct ieee80211_mgmt *)beacon->data;
312 mgmt->u.beacon.timestamp = avp->tsfadjust;
314 info = IEEE80211_SKB_CB(beacon);
315 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
316 struct ieee80211_hdr *hdr =
317 (struct ieee80211_hdr *) beacon->data;
319 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
320 hdr->seq_ctrl |= cpu_to_le16(avp->seq_no);
323 tx_ctl.type = ATH9K_HTC_NORMAL;
324 beacon_hdr.vif_index = avp->index;
325 tx_fhdr = skb_push(beacon, sizeof(beacon_hdr));
326 memcpy(tx_fhdr, (u8 *) &beacon_hdr, sizeof(beacon_hdr));
328 htc_send(priv->htc, beacon, priv->beacon_ep, &tx_ctl);
330 spin_unlock_bh(&priv->beacon_lock);
333 static int ath9k_htc_choose_bslot(struct ath9k_htc_priv *priv)
335 struct ath_common *common = ath9k_hw_common(priv->ah);
342 intval = priv->cur_beacon_conf.beacon_interval & ATH9K_BEACON_PERIOD;
344 spin_lock_irqsave(&priv->wmi->wmi_lock, flags);
345 tsf = priv->wmi->tsf;
346 spin_unlock_irqrestore(&priv->wmi->wmi_lock, flags);
348 tsftu = TSF_TO_TU(tsf >> 32, tsf);
349 slot = ((tsftu % intval) * ATH9K_HTC_MAX_BCN_VIF) / intval;
350 slot = ATH9K_HTC_MAX_BCN_VIF - slot - 1;
352 ath_dbg(common, ATH_DBG_BEACON,
353 "Choose slot: %d, tsf: %llu, tsftu: %u, intval: %u\n",
354 slot, tsf, tsftu, intval);
359 void ath9k_htc_swba(struct ath9k_htc_priv *priv)
361 struct ath_common *common = ath9k_hw_common(priv->ah);
365 spin_lock_irqsave(&priv->wmi->wmi_lock, flags);
366 if (priv->wmi->beacon_pending != 0) {
367 spin_unlock_irqrestore(&priv->wmi->wmi_lock, flags);
368 priv->cur_beacon_conf.bmiss_cnt++;
369 if (priv->cur_beacon_conf.bmiss_cnt > BSTUCK_THRESHOLD) {
370 ath_dbg(common, ATH_DBG_BEACON,
371 "Beacon stuck, HW reset\n");
372 ath9k_htc_reset(priv);
376 spin_unlock_irqrestore(&priv->wmi->wmi_lock, flags);
378 if (priv->cur_beacon_conf.bmiss_cnt) {
379 ath_dbg(common, ATH_DBG_BEACON,
380 "Resuming beacon xmit after %u misses\n",
381 priv->cur_beacon_conf.bmiss_cnt);
382 priv->cur_beacon_conf.bmiss_cnt = 0;
385 slot = ath9k_htc_choose_bslot(priv);
386 spin_lock_bh(&priv->beacon_lock);
387 if (priv->cur_beacon_conf.bslot[slot] == NULL) {
388 spin_unlock_bh(&priv->beacon_lock);
391 spin_unlock_bh(&priv->beacon_lock);
393 ath9k_htc_send_beacon(priv, slot);
396 void ath9k_htc_assign_bslot(struct ath9k_htc_priv *priv,
397 struct ieee80211_vif *vif)
399 struct ath_common *common = ath9k_hw_common(priv->ah);
400 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *)vif->drv_priv;
403 spin_lock_bh(&priv->beacon_lock);
404 for (i = 0; i < ATH9K_HTC_MAX_BCN_VIF; i++) {
405 if (priv->cur_beacon_conf.bslot[i] == NULL) {
411 priv->cur_beacon_conf.bslot[avp->bslot] = vif;
412 spin_unlock_bh(&priv->beacon_lock);
414 ath_dbg(common, ATH_DBG_CONFIG,
415 "Added interface at beacon slot: %d\n", avp->bslot);
418 void ath9k_htc_remove_bslot(struct ath9k_htc_priv *priv,
419 struct ieee80211_vif *vif)
421 struct ath_common *common = ath9k_hw_common(priv->ah);
422 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *)vif->drv_priv;
424 spin_lock_bh(&priv->beacon_lock);
425 priv->cur_beacon_conf.bslot[avp->bslot] = NULL;
426 spin_unlock_bh(&priv->beacon_lock);
428 ath_dbg(common, ATH_DBG_CONFIG,
429 "Removed interface at beacon slot: %d\n", avp->bslot);
433 * Calculate the TSF adjustment value for all slots
436 void ath9k_htc_set_tsfadjust(struct ath9k_htc_priv *priv,
437 struct ieee80211_vif *vif)
439 struct ath_common *common = ath9k_hw_common(priv->ah);
440 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *)vif->drv_priv;
441 struct htc_beacon_config *cur_conf = &priv->cur_beacon_conf;
448 * The beacon interval cannot be different for multi-AP mode,
449 * and we reach here only for VIF slots greater than zero,
450 * so beacon_interval is guaranteed to be set in cur_conf.
452 tsfadjust = cur_conf->beacon_interval * avp->bslot / ATH9K_HTC_MAX_BCN_VIF;
453 avp->tsfadjust = cpu_to_le64(TU_TO_USEC(tsfadjust));
455 ath_dbg(common, ATH_DBG_CONFIG,
456 "tsfadjust is: %llu for bslot: %d\n",
457 (unsigned long long)tsfadjust, avp->bslot);
460 static void ath9k_htc_beacon_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
462 bool *beacon_configured = (bool *)data;
463 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
465 if (vif->type == NL80211_IFTYPE_STATION &&
466 avp->beacon_configured)
467 *beacon_configured = true;
470 static bool ath9k_htc_check_beacon_config(struct ath9k_htc_priv *priv,
471 struct ieee80211_vif *vif)
473 struct ath_common *common = ath9k_hw_common(priv->ah);
474 struct htc_beacon_config *cur_conf = &priv->cur_beacon_conf;
475 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
476 bool beacon_configured;
479 * Changing the beacon interval when multiple AP interfaces
480 * are configured will affect beacon transmission of all
483 if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
484 (priv->num_ap_vif > 1) &&
485 (vif->type == NL80211_IFTYPE_AP) &&
486 (cur_conf->beacon_interval != bss_conf->beacon_int)) {
487 ath_dbg(common, ATH_DBG_CONFIG,
488 "Changing beacon interval of multiple AP interfaces !\n");
493 * If the HW is operating in AP mode, any new station interfaces that
494 * are added cannot change the beacon parameters.
496 if (priv->num_ap_vif &&
497 (vif->type != NL80211_IFTYPE_AP)) {
498 ath_dbg(common, ATH_DBG_CONFIG,
499 "HW in AP mode, cannot set STA beacon parameters\n");
504 * The beacon parameters are configured only for the first
507 if ((priv->ah->opmode == NL80211_IFTYPE_STATION) &&
508 (priv->num_sta_vif > 1) &&
509 (vif->type == NL80211_IFTYPE_STATION)) {
510 beacon_configured = false;
511 ieee80211_iterate_active_interfaces_atomic(priv->hw,
512 ath9k_htc_beacon_iter,
515 if (beacon_configured) {
516 ath_dbg(common, ATH_DBG_CONFIG,
517 "Beacon already configured for a station interface\n");
525 void ath9k_htc_beacon_config(struct ath9k_htc_priv *priv,
526 struct ieee80211_vif *vif)
528 struct ath_common *common = ath9k_hw_common(priv->ah);
529 struct htc_beacon_config *cur_conf = &priv->cur_beacon_conf;
530 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
531 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
533 if (!ath9k_htc_check_beacon_config(priv, vif))
536 cur_conf->beacon_interval = bss_conf->beacon_int;
537 if (cur_conf->beacon_interval == 0)
538 cur_conf->beacon_interval = 100;
540 cur_conf->dtim_period = bss_conf->dtim_period;
541 cur_conf->bmiss_timeout =
542 ATH_DEFAULT_BMISS_LIMIT * cur_conf->beacon_interval;
545 case NL80211_IFTYPE_STATION:
546 ath9k_htc_beacon_config_sta(priv, cur_conf);
547 avp->beacon_configured = true;
549 case NL80211_IFTYPE_ADHOC:
550 ath9k_htc_beacon_config_adhoc(priv, cur_conf);
552 case NL80211_IFTYPE_AP:
553 ath9k_htc_beacon_config_ap(priv, cur_conf);
556 ath_dbg(common, ATH_DBG_CONFIG,
557 "Unsupported beaconing mode\n");
562 void ath9k_htc_beacon_reconfig(struct ath9k_htc_priv *priv)
564 struct ath_common *common = ath9k_hw_common(priv->ah);
565 struct htc_beacon_config *cur_conf = &priv->cur_beacon_conf;
567 switch (priv->ah->opmode) {
568 case NL80211_IFTYPE_STATION:
569 ath9k_htc_beacon_config_sta(priv, cur_conf);
571 case NL80211_IFTYPE_ADHOC:
572 ath9k_htc_beacon_config_adhoc(priv, cur_conf);
574 case NL80211_IFTYPE_AP:
575 ath9k_htc_beacon_config_ap(priv, cur_conf);
578 ath_dbg(common, ATH_DBG_CONFIG,
579 "Unsupported beaconing mode\n");