2 * Copyright (c) 2010-2011 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->beacon.beaconq, &qi);
31 if (priv->ah->opmode == NL80211_IFTYPE_AP ||
32 priv->ah->opmode == NL80211_IFTYPE_MESH_POINT) {
36 } else if (priv->ah->opmode == NL80211_IFTYPE_ADHOC) {
37 int qnum = priv->hwq_map[IEEE80211_AC_BE];
39 ath9k_hw_get_txq_props(ah, qnum, &qi_be);
41 qi.tqi_aifs = qi_be.tqi_aifs;
44 * For WIFI Beacon Distribution
45 * Long slot time : 2x cwmin
46 * Short slot time : 4x cwmin
48 if (ah->slottime == ATH9K_SLOT_TIME_20)
49 qi.tqi_cwmin = 2*qi_be.tqi_cwmin;
51 qi.tqi_cwmin = 4*qi_be.tqi_cwmin;
53 qi.tqi_cwmax = qi_be.tqi_cwmax;
57 if (!ath9k_hw_set_txq_props(ah, priv->beacon.beaconq, &qi)) {
58 ath_err(ath9k_hw_common(ah),
59 "Unable to update beacon queue %u!\n", priv->beacon.beaconq);
61 ath9k_hw_resettxqueue(ah, priv->beacon.beaconq);
66 * Both nexttbtt and intval have to be in usecs.
68 static void ath9k_htc_beacon_init(struct ath9k_htc_priv *priv,
69 struct ath_beacon_config *conf,
72 struct ath_hw *ah = priv->ah;
73 int ret __attribute__ ((unused));
77 WMI_CMD(WMI_DISABLE_INTR_CMDID);
79 ath9k_hw_reset_tsf(ah);
80 ath9k_htc_beaconq_config(priv);
81 ath9k_hw_beaconinit(ah, conf->nexttbtt, conf->intval);
82 priv->beacon.bmisscnt = 0;
83 htc_imask = cpu_to_be32(ah->imask);
84 WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
87 static void ath9k_htc_beacon_config_sta(struct ath9k_htc_priv *priv,
88 struct ath_beacon_config *bss_conf)
90 struct ath9k_beacon_state bs;
91 enum ath9k_int imask = 0;
93 int ret __attribute__ ((unused));
96 if (ath9k_cmn_beacon_config_sta(priv->ah, bss_conf, &bs) == -EPERM)
99 WMI_CMD(WMI_DISABLE_INTR_CMDID);
100 ath9k_hw_set_sta_beacon_timers(priv->ah, &bs);
101 imask |= ATH9K_INT_BMISS;
102 htc_imask = cpu_to_be32(imask);
103 WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
106 static void ath9k_htc_beacon_config_ap(struct ath9k_htc_priv *priv,
107 struct ath_beacon_config *bss_conf)
109 struct ath_common *common = ath9k_hw_common(priv->ah);
111 int ret __attribute__ ((unused));
114 bss_conf->intval = bss_conf->beacon_interval;
115 bss_conf->intval /= ATH9K_HTC_MAX_BCN_VIF;
116 bss_conf->nexttbtt = bss_conf->intval;
119 * To reduce beacon misses under heavy TX load,
120 * set the beacon response time to a larger value.
122 if (bss_conf->intval > DEFAULT_SWBA_RESPONSE)
123 priv->ah->config.sw_beacon_response_time = DEFAULT_SWBA_RESPONSE;
125 priv->ah->config.sw_beacon_response_time = MIN_SWBA_RESPONSE;
127 if (test_bit(OP_TSF_RESET, &priv->op_flags)) {
128 ath9k_hw_reset_tsf(priv->ah);
129 clear_bit(OP_TSF_RESET, &priv->op_flags);
132 * Pull nexttbtt forward to reflect the current TSF.
134 tsf = ath9k_hw_gettsf64(priv->ah);
135 tsftu = TSF_TO_TU(tsf >> 32, tsf) + FUDGE;
137 bss_conf->nexttbtt += bss_conf->intval;
138 } while (bss_conf->nexttbtt < tsftu);
141 if (bss_conf->enable_beacon)
142 priv->ah->imask = ATH9K_INT_SWBA;
144 ath_dbg(common, CONFIG,
145 "AP Beacon config, intval: %d, nexttbtt: %u, resp_time: %d imask: 0x%x\n",
146 bss_conf->beacon_interval, bss_conf->nexttbtt,
147 priv->ah->config.sw_beacon_response_time, priv->ah->imask);
149 ath9k_htc_beacon_init(priv, bss_conf, false);
152 static void ath9k_htc_beacon_config_adhoc(struct ath9k_htc_priv *priv,
153 struct ath_beacon_config *bss_conf)
155 struct ath_common *common = ath9k_hw_common(priv->ah);
156 enum ath9k_int imask = 0;
157 u32 nexttbtt, intval, tsftu;
158 __be32 htc_imask = 0;
159 int ret __attribute__ ((unused));
163 intval = bss_conf->beacon_interval;
167 * Pull nexttbtt forward to reflect the current TSF.
169 tsf = ath9k_hw_gettsf64(priv->ah);
170 tsftu = TSF_TO_TU(tsf >> 32, tsf) + FUDGE;
173 } while (nexttbtt < tsftu);
176 * Only one IBSS interfce is allowed.
178 if (intval > DEFAULT_SWBA_RESPONSE)
179 priv->ah->config.sw_beacon_response_time = DEFAULT_SWBA_RESPONSE;
181 priv->ah->config.sw_beacon_response_time = MIN_SWBA_RESPONSE;
183 if (bss_conf->enable_beacon)
184 imask |= ATH9K_INT_SWBA;
186 ath_dbg(common, CONFIG,
187 "IBSS Beacon config, intval: %d, nexttbtt: %u, resp_time: %d, imask: 0x%x\n",
188 bss_conf->beacon_interval, nexttbtt,
189 priv->ah->config.sw_beacon_response_time, imask);
191 WMI_CMD(WMI_DISABLE_INTR_CMDID);
192 ath9k_hw_beaconinit(priv->ah, TU_TO_USEC(nexttbtt), TU_TO_USEC(intval));
193 priv->beacon.bmisscnt = 0;
194 htc_imask = cpu_to_be32(imask);
195 WMI_CMD_BUF(WMI_ENABLE_INTR_CMDID, &htc_imask);
198 void ath9k_htc_beaconep(void *drv_priv, struct sk_buff *skb,
199 enum htc_endpoint_id ep_id, bool txok)
201 dev_kfree_skb_any(skb);
204 static void ath9k_htc_send_buffered(struct ath9k_htc_priv *priv,
207 struct ath_common *common = ath9k_hw_common(priv->ah);
208 struct ieee80211_vif *vif;
210 struct ieee80211_hdr *hdr;
211 int padpos, padsize, ret, tx_slot;
213 spin_lock_bh(&priv->beacon_lock);
215 vif = priv->beacon.bslot[slot];
217 skb = ieee80211_get_buffered_bc(priv->hw, vif);
220 hdr = (struct ieee80211_hdr *) skb->data;
222 padpos = ieee80211_hdrlen(hdr->frame_control);
223 padsize = padpos & 3;
224 if (padsize && skb->len > padpos) {
225 if (skb_headroom(skb) < padsize) {
226 dev_kfree_skb_any(skb);
229 skb_push(skb, padsize);
230 memmove(skb->data, skb->data + padsize, padpos);
233 tx_slot = ath9k_htc_tx_get_slot(priv);
235 ath_dbg(common, XMIT, "No free CAB slot\n");
236 dev_kfree_skb_any(skb);
240 ret = ath9k_htc_tx_start(priv, NULL, skb, tx_slot, true);
242 ath9k_htc_tx_clear_slot(priv, tx_slot);
243 dev_kfree_skb_any(skb);
245 ath_dbg(common, XMIT, "Failed to send CAB frame\n");
247 spin_lock_bh(&priv->tx.tx_lock);
248 priv->tx.queued_cnt++;
249 spin_unlock_bh(&priv->tx.tx_lock);
252 skb = ieee80211_get_buffered_bc(priv->hw, vif);
255 spin_unlock_bh(&priv->beacon_lock);
258 static void ath9k_htc_send_beacon(struct ath9k_htc_priv *priv,
261 struct ath_common *common = ath9k_hw_common(priv->ah);
262 struct ieee80211_vif *vif;
263 struct ath9k_htc_vif *avp;
264 struct tx_beacon_header beacon_hdr;
265 struct ath9k_htc_tx_ctl *tx_ctl;
266 struct ieee80211_tx_info *info;
267 struct ieee80211_mgmt *mgmt;
268 struct sk_buff *beacon;
272 memset(&beacon_hdr, 0, sizeof(struct tx_beacon_header));
274 spin_lock_bh(&priv->beacon_lock);
276 vif = priv->beacon.bslot[slot];
277 avp = (struct ath9k_htc_vif *)vif->drv_priv;
279 if (unlikely(test_bit(ATH_OP_SCANNING, &common->op_flags))) {
280 spin_unlock_bh(&priv->beacon_lock);
284 /* Get a new beacon */
285 beacon = ieee80211_beacon_get(priv->hw, vif);
287 spin_unlock_bh(&priv->beacon_lock);
292 * Update the TSF adjust value here, the HW will
293 * add this value for every beacon.
295 mgmt = (struct ieee80211_mgmt *)beacon->data;
296 mgmt->u.beacon.timestamp = avp->tsfadjust;
298 info = IEEE80211_SKB_CB(beacon);
299 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
300 struct ieee80211_hdr *hdr =
301 (struct ieee80211_hdr *) beacon->data;
303 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
304 hdr->seq_ctrl |= cpu_to_le16(avp->seq_no);
307 tx_ctl = HTC_SKB_CB(beacon);
308 memset(tx_ctl, 0, sizeof(*tx_ctl));
310 tx_ctl->type = ATH9K_HTC_BEACON;
311 tx_ctl->epid = priv->beacon_ep;
313 beacon_hdr.vif_index = avp->index;
314 tx_fhdr = skb_push(beacon, sizeof(beacon_hdr));
315 memcpy(tx_fhdr, (u8 *) &beacon_hdr, sizeof(beacon_hdr));
317 ret = htc_send(priv->htc, beacon);
319 if (ret == -ENOMEM) {
320 ath_dbg(common, BSTUCK,
321 "Failed to send beacon, no free TX buffer\n");
323 dev_kfree_skb_any(beacon);
326 spin_unlock_bh(&priv->beacon_lock);
329 static int ath9k_htc_choose_bslot(struct ath9k_htc_priv *priv,
330 struct wmi_event_swba *swba)
332 struct ath_common *common = ath9k_hw_common(priv->ah);
338 intval = priv->cur_beacon_conf.beacon_interval;
340 tsf = be64_to_cpu(swba->tsf);
341 tsftu = TSF_TO_TU(tsf >> 32, tsf);
342 slot = ((tsftu % intval) * ATH9K_HTC_MAX_BCN_VIF) / intval;
343 slot = ATH9K_HTC_MAX_BCN_VIF - slot - 1;
345 ath_dbg(common, BEACON,
346 "Choose slot: %d, tsf: %llu, tsftu: %u, intval: %u\n",
347 slot, tsf, tsftu, intval);
352 void ath9k_htc_swba(struct ath9k_htc_priv *priv,
353 struct wmi_event_swba *swba)
355 struct ath_common *common = ath9k_hw_common(priv->ah);
358 if (swba->beacon_pending != 0) {
359 priv->beacon.bmisscnt++;
360 if (priv->beacon.bmisscnt > BSTUCK_THRESHOLD) {
361 ath_dbg(common, BSTUCK, "Beacon stuck, HW reset\n");
362 ieee80211_queue_work(priv->hw,
368 if (priv->beacon.bmisscnt) {
369 ath_dbg(common, BSTUCK,
370 "Resuming beacon xmit after %u misses\n",
371 priv->beacon.bmisscnt);
372 priv->beacon.bmisscnt = 0;
375 slot = ath9k_htc_choose_bslot(priv, swba);
376 spin_lock_bh(&priv->beacon_lock);
377 if (priv->beacon.bslot[slot] == NULL) {
378 spin_unlock_bh(&priv->beacon_lock);
381 spin_unlock_bh(&priv->beacon_lock);
383 ath9k_htc_send_buffered(priv, slot);
384 ath9k_htc_send_beacon(priv, slot);
387 void ath9k_htc_assign_bslot(struct ath9k_htc_priv *priv,
388 struct ieee80211_vif *vif)
390 struct ath_common *common = ath9k_hw_common(priv->ah);
391 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *)vif->drv_priv;
394 spin_lock_bh(&priv->beacon_lock);
395 for (i = 0; i < ATH9K_HTC_MAX_BCN_VIF; i++) {
396 if (priv->beacon.bslot[i] == NULL) {
402 priv->beacon.bslot[avp->bslot] = vif;
403 spin_unlock_bh(&priv->beacon_lock);
405 ath_dbg(common, CONFIG, "Added interface at beacon slot: %d\n",
409 void ath9k_htc_remove_bslot(struct ath9k_htc_priv *priv,
410 struct ieee80211_vif *vif)
412 struct ath_common *common = ath9k_hw_common(priv->ah);
413 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *)vif->drv_priv;
415 spin_lock_bh(&priv->beacon_lock);
416 priv->beacon.bslot[avp->bslot] = NULL;
417 spin_unlock_bh(&priv->beacon_lock);
419 ath_dbg(common, CONFIG, "Removed interface at beacon slot: %d\n",
424 * Calculate the TSF adjustment value for all slots
427 void ath9k_htc_set_tsfadjust(struct ath9k_htc_priv *priv,
428 struct ieee80211_vif *vif)
430 struct ath_common *common = ath9k_hw_common(priv->ah);
431 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *)vif->drv_priv;
432 struct ath_beacon_config *cur_conf = &priv->cur_beacon_conf;
439 * The beacon interval cannot be different for multi-AP mode,
440 * and we reach here only for VIF slots greater than zero,
441 * so beacon_interval is guaranteed to be set in cur_conf.
443 tsfadjust = cur_conf->beacon_interval * avp->bslot / ATH9K_HTC_MAX_BCN_VIF;
444 avp->tsfadjust = cpu_to_le64(TU_TO_USEC(tsfadjust));
446 ath_dbg(common, CONFIG, "tsfadjust is: %llu for bslot: %d\n",
447 (unsigned long long)tsfadjust, avp->bslot);
450 static void ath9k_htc_beacon_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
452 bool *beacon_configured = (bool *)data;
453 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
455 if (vif->type == NL80211_IFTYPE_STATION &&
456 avp->beacon_configured)
457 *beacon_configured = true;
460 static bool ath9k_htc_check_beacon_config(struct ath9k_htc_priv *priv,
461 struct ieee80211_vif *vif)
463 struct ath_common *common = ath9k_hw_common(priv->ah);
464 struct ath_beacon_config *cur_conf = &priv->cur_beacon_conf;
465 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
466 bool beacon_configured;
469 * Changing the beacon interval when multiple AP interfaces
470 * are configured will affect beacon transmission of all
473 if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
474 (priv->num_ap_vif > 1) &&
475 (vif->type == NL80211_IFTYPE_AP) &&
476 (cur_conf->beacon_interval != bss_conf->beacon_int)) {
477 ath_dbg(common, CONFIG,
478 "Changing beacon interval of multiple AP interfaces !\n");
483 * If the HW is operating in AP mode, any new station interfaces that
484 * are added cannot change the beacon parameters.
486 if (priv->num_ap_vif &&
487 (vif->type != NL80211_IFTYPE_AP)) {
488 ath_dbg(common, CONFIG,
489 "HW in AP mode, cannot set STA beacon parameters\n");
494 * The beacon parameters are configured only for the first
497 if ((priv->ah->opmode == NL80211_IFTYPE_STATION) &&
498 (priv->num_sta_vif > 1) &&
499 (vif->type == NL80211_IFTYPE_STATION)) {
500 beacon_configured = false;
501 ieee80211_iterate_active_interfaces_atomic(
502 priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
503 ath9k_htc_beacon_iter, &beacon_configured);
505 if (beacon_configured) {
506 ath_dbg(common, CONFIG,
507 "Beacon already configured for a station interface\n");
515 void ath9k_htc_beacon_config(struct ath9k_htc_priv *priv,
516 struct ieee80211_vif *vif)
518 struct ath_common *common = ath9k_hw_common(priv->ah);
519 struct ath_beacon_config *cur_conf = &priv->cur_beacon_conf;
520 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
521 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
523 if (!ath9k_htc_check_beacon_config(priv, vif))
526 cur_conf->beacon_interval = bss_conf->beacon_int;
527 if (cur_conf->beacon_interval == 0)
528 cur_conf->beacon_interval = 100;
530 cur_conf->dtim_period = bss_conf->dtim_period;
531 cur_conf->bmiss_timeout =
532 ATH_DEFAULT_BMISS_LIMIT * cur_conf->beacon_interval;
535 case NL80211_IFTYPE_STATION:
536 ath9k_htc_beacon_config_sta(priv, cur_conf);
537 avp->beacon_configured = true;
539 case NL80211_IFTYPE_ADHOC:
540 ath9k_htc_beacon_config_adhoc(priv, cur_conf);
542 case NL80211_IFTYPE_MESH_POINT:
543 case NL80211_IFTYPE_AP:
544 ath9k_htc_beacon_config_ap(priv, cur_conf);
547 ath_dbg(common, CONFIG, "Unsupported beaconing mode\n");
552 void ath9k_htc_beacon_reconfig(struct ath9k_htc_priv *priv)
554 struct ath_common *common = ath9k_hw_common(priv->ah);
555 struct ath_beacon_config *cur_conf = &priv->cur_beacon_conf;
557 switch (priv->ah->opmode) {
558 case NL80211_IFTYPE_STATION:
559 ath9k_htc_beacon_config_sta(priv, cur_conf);
561 case NL80211_IFTYPE_ADHOC:
562 ath9k_htc_beacon_config_adhoc(priv, cur_conf);
564 case NL80211_IFTYPE_MESH_POINT:
565 case NL80211_IFTYPE_AP:
566 ath9k_htc_beacon_config_ap(priv, cur_conf);
569 ath_dbg(common, CONFIG, "Unsupported beaconing mode\n");