b23231fea02c2a6c034cf772e0d0fbc270518ac6
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / ath / ath9k / htc_drv_beacon.c
1 /*
2  * Copyright (c) 2010-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
17 #include "htc.h"
18
19 #define FUDGE 2
20
21 void ath9k_htc_beaconq_config(struct ath9k_htc_priv *priv)
22 {
23         struct ath_hw *ah = priv->ah;
24         struct ath9k_tx_queue_info qi, qi_be;
25
26         memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
27         memset(&qi_be, 0, sizeof(struct ath9k_tx_queue_info));
28
29         ath9k_hw_get_txq_props(ah, priv->beacon.beaconq, &qi);
30
31         if (priv->ah->opmode == NL80211_IFTYPE_AP ||
32             priv->ah->opmode == NL80211_IFTYPE_MESH_POINT) {
33                 qi.tqi_aifs = 1;
34                 qi.tqi_cwmin = 0;
35                 qi.tqi_cwmax = 0;
36         } else if (priv->ah->opmode == NL80211_IFTYPE_ADHOC) {
37                 int qnum = priv->hwq_map[IEEE80211_AC_BE];
38
39                 ath9k_hw_get_txq_props(ah, qnum, &qi_be);
40
41                 qi.tqi_aifs = qi_be.tqi_aifs;
42
43                 /*
44                  * For WIFI Beacon Distribution
45                  * Long slot time  : 2x cwmin
46                  * Short slot time : 4x cwmin
47                  */
48                 if (ah->slottime == ATH9K_SLOT_TIME_20)
49                         qi.tqi_cwmin = 2*qi_be.tqi_cwmin;
50                 else
51                         qi.tqi_cwmin = 4*qi_be.tqi_cwmin;
52
53                 qi.tqi_cwmax = qi_be.tqi_cwmax;
54
55         }
56
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);
60         } else {
61                 ath9k_hw_resettxqueue(ah, priv->beacon.beaconq);
62         }
63 }
64
65 /*
66  * Both nexttbtt and intval have to be in usecs.
67  */
68 static void ath9k_htc_beacon_init(struct ath9k_htc_priv *priv,
69                                   struct ath_beacon_config *conf,
70                                   bool reset_tsf)
71 {
72         struct ath_hw *ah = priv->ah;
73         int ret __attribute__ ((unused));
74         __be32 htc_imask = 0;
75         u8 cmd_rsp;
76
77         WMI_CMD(WMI_DISABLE_INTR_CMDID);
78         if (reset_tsf)
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);
85 }
86
87 static void ath9k_htc_beacon_config_sta(struct ath9k_htc_priv *priv,
88                                         struct ath_beacon_config *bss_conf)
89 {
90         struct ath9k_beacon_state bs;
91         enum ath9k_int imask = 0;
92         __be32 htc_imask = 0;
93         int ret __attribute__ ((unused));
94         u8 cmd_rsp;
95
96         if (ath9k_cmn_beacon_config_sta(priv->ah, bss_conf, &bs) == -EPERM)
97                 return;
98
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);
104 }
105
106 static void ath9k_htc_beacon_config_ap(struct ath9k_htc_priv *priv,
107                                        struct ath_beacon_config *bss_conf)
108 {
109         struct ath_common *common = ath9k_hw_common(priv->ah);
110         u32 tsftu;
111         int ret __attribute__ ((unused));
112         u64 tsf;
113
114         bss_conf->intval = bss_conf->beacon_interval;
115         bss_conf->intval /= ATH9K_HTC_MAX_BCN_VIF;
116         bss_conf->nexttbtt = bss_conf->intval;
117
118         /*
119          * To reduce beacon misses under heavy TX load,
120          * set the beacon response time to a larger value.
121          */
122         if (bss_conf->intval > DEFAULT_SWBA_RESPONSE)
123                 priv->ah->config.sw_beacon_response_time = DEFAULT_SWBA_RESPONSE;
124         else
125                 priv->ah->config.sw_beacon_response_time = MIN_SWBA_RESPONSE;
126
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);
130         } else {
131                 /*
132                  * Pull nexttbtt forward to reflect the current TSF.
133                  */
134                 tsf = ath9k_hw_gettsf64(priv->ah);
135                 tsftu = TSF_TO_TU(tsf >> 32, tsf) + FUDGE;
136                 do {
137                         bss_conf->nexttbtt += bss_conf->intval;
138                 } while (bss_conf->nexttbtt < tsftu);
139         }
140
141         if (bss_conf->enable_beacon)
142                 priv->ah->imask = ATH9K_INT_SWBA;
143
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);
148
149         ath9k_htc_beacon_init(priv, bss_conf, false);
150 }
151
152 static void ath9k_htc_beacon_config_adhoc(struct ath9k_htc_priv *priv,
153                                           struct ath_beacon_config *bss_conf)
154 {
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));
160         u8 cmd_rsp;
161         u64 tsf;
162
163         intval = bss_conf->beacon_interval;
164         nexttbtt = intval;
165
166         /*
167          * Pull nexttbtt forward to reflect the current TSF.
168          */
169         tsf = ath9k_hw_gettsf64(priv->ah);
170         tsftu = TSF_TO_TU(tsf >> 32, tsf) + FUDGE;
171         do {
172                 nexttbtt += intval;
173         } while (nexttbtt < tsftu);
174
175         /*
176          * Only one IBSS interfce is allowed.
177          */
178         if (intval > DEFAULT_SWBA_RESPONSE)
179                 priv->ah->config.sw_beacon_response_time = DEFAULT_SWBA_RESPONSE;
180         else
181                 priv->ah->config.sw_beacon_response_time = MIN_SWBA_RESPONSE;
182
183         if (bss_conf->enable_beacon)
184                 imask |= ATH9K_INT_SWBA;
185
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);
190
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);
196 }
197
198 void ath9k_htc_beaconep(void *drv_priv, struct sk_buff *skb,
199                         enum htc_endpoint_id ep_id, bool txok)
200 {
201         dev_kfree_skb_any(skb);
202 }
203
204 static void ath9k_htc_send_buffered(struct ath9k_htc_priv *priv,
205                                     int slot)
206 {
207         struct ath_common *common = ath9k_hw_common(priv->ah);
208         struct ieee80211_vif *vif;
209         struct sk_buff *skb;
210         struct ieee80211_hdr *hdr;
211         int padpos, padsize, ret, tx_slot;
212
213         spin_lock_bh(&priv->beacon_lock);
214
215         vif = priv->beacon.bslot[slot];
216
217         skb = ieee80211_get_buffered_bc(priv->hw, vif);
218
219         while(skb) {
220                 hdr = (struct ieee80211_hdr *) skb->data;
221
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);
227                                 goto next;
228                         }
229                         skb_push(skb, padsize);
230                         memmove(skb->data, skb->data + padsize, padpos);
231                 }
232
233                 tx_slot = ath9k_htc_tx_get_slot(priv);
234                 if (tx_slot < 0) {
235                         ath_dbg(common, XMIT, "No free CAB slot\n");
236                         dev_kfree_skb_any(skb);
237                         goto next;
238                 }
239
240                 ret = ath9k_htc_tx_start(priv, NULL, skb, tx_slot, true);
241                 if (ret != 0) {
242                         ath9k_htc_tx_clear_slot(priv, tx_slot);
243                         dev_kfree_skb_any(skb);
244
245                         ath_dbg(common, XMIT, "Failed to send CAB frame\n");
246                 } else {
247                         spin_lock_bh(&priv->tx.tx_lock);
248                         priv->tx.queued_cnt++;
249                         spin_unlock_bh(&priv->tx.tx_lock);
250                 }
251         next:
252                 skb = ieee80211_get_buffered_bc(priv->hw, vif);
253         }
254
255         spin_unlock_bh(&priv->beacon_lock);
256 }
257
258 static void ath9k_htc_send_beacon(struct ath9k_htc_priv *priv,
259                                   int slot)
260 {
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;
269         u8 *tx_fhdr;
270         int ret;
271
272         memset(&beacon_hdr, 0, sizeof(struct tx_beacon_header));
273
274         spin_lock_bh(&priv->beacon_lock);
275
276         vif = priv->beacon.bslot[slot];
277         avp = (struct ath9k_htc_vif *)vif->drv_priv;
278
279         if (unlikely(test_bit(ATH_OP_SCANNING, &common->op_flags))) {
280                 spin_unlock_bh(&priv->beacon_lock);
281                 return;
282         }
283
284         /* Get a new beacon */
285         beacon = ieee80211_beacon_get(priv->hw, vif);
286         if (!beacon) {
287                 spin_unlock_bh(&priv->beacon_lock);
288                 return;
289         }
290
291         /*
292          * Update the TSF adjust value here, the HW will
293          * add this value for every beacon.
294          */
295         mgmt = (struct ieee80211_mgmt *)beacon->data;
296         mgmt->u.beacon.timestamp = avp->tsfadjust;
297
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;
302                 avp->seq_no += 0x10;
303                 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
304                 hdr->seq_ctrl |= cpu_to_le16(avp->seq_no);
305         }
306
307         tx_ctl = HTC_SKB_CB(beacon);
308         memset(tx_ctl, 0, sizeof(*tx_ctl));
309
310         tx_ctl->type = ATH9K_HTC_BEACON;
311         tx_ctl->epid = priv->beacon_ep;
312
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));
316
317         ret = htc_send(priv->htc, beacon);
318         if (ret != 0) {
319                 if (ret == -ENOMEM) {
320                         ath_dbg(common, BSTUCK,
321                                 "Failed to send beacon, no free TX buffer\n");
322                 }
323                 dev_kfree_skb_any(beacon);
324         }
325
326         spin_unlock_bh(&priv->beacon_lock);
327 }
328
329 static int ath9k_htc_choose_bslot(struct ath9k_htc_priv *priv,
330                                   struct wmi_event_swba *swba)
331 {
332         struct ath_common *common = ath9k_hw_common(priv->ah);
333         u64 tsf;
334         u32 tsftu;
335         u16 intval;
336         int slot;
337
338         intval = priv->cur_beacon_conf.beacon_interval;
339
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;
344
345         ath_dbg(common, BEACON,
346                 "Choose slot: %d, tsf: %llu, tsftu: %u, intval: %u\n",
347                 slot, tsf, tsftu, intval);
348
349         return slot;
350 }
351
352 void ath9k_htc_swba(struct ath9k_htc_priv *priv,
353                     struct wmi_event_swba *swba)
354 {
355         struct ath_common *common = ath9k_hw_common(priv->ah);
356         int slot;
357
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,
363                                              &priv->fatal_work);
364                 }
365                 return;
366         }
367
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;
373         }
374
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);
379                 return;
380         }
381         spin_unlock_bh(&priv->beacon_lock);
382
383         ath9k_htc_send_buffered(priv, slot);
384         ath9k_htc_send_beacon(priv, slot);
385 }
386
387 void ath9k_htc_assign_bslot(struct ath9k_htc_priv *priv,
388                             struct ieee80211_vif *vif)
389 {
390         struct ath_common *common = ath9k_hw_common(priv->ah);
391         struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *)vif->drv_priv;
392         int i = 0;
393
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) {
397                         avp->bslot = i;
398                         break;
399                 }
400         }
401
402         priv->beacon.bslot[avp->bslot] = vif;
403         spin_unlock_bh(&priv->beacon_lock);
404
405         ath_dbg(common, CONFIG, "Added interface at beacon slot: %d\n",
406                 avp->bslot);
407 }
408
409 void ath9k_htc_remove_bslot(struct ath9k_htc_priv *priv,
410                             struct ieee80211_vif *vif)
411 {
412         struct ath_common *common = ath9k_hw_common(priv->ah);
413         struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *)vif->drv_priv;
414
415         spin_lock_bh(&priv->beacon_lock);
416         priv->beacon.bslot[avp->bslot] = NULL;
417         spin_unlock_bh(&priv->beacon_lock);
418
419         ath_dbg(common, CONFIG, "Removed interface at beacon slot: %d\n",
420                 avp->bslot);
421 }
422
423 /*
424  * Calculate the TSF adjustment value for all slots
425  * other than zero.
426  */
427 void ath9k_htc_set_tsfadjust(struct ath9k_htc_priv *priv,
428                              struct ieee80211_vif *vif)
429 {
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;
433         u64 tsfadjust;
434
435         if (avp->bslot == 0)
436                 return;
437
438         /*
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.
442          */
443         tsfadjust = cur_conf->beacon_interval * avp->bslot / ATH9K_HTC_MAX_BCN_VIF;
444         avp->tsfadjust = cpu_to_le64(TU_TO_USEC(tsfadjust));
445
446         ath_dbg(common, CONFIG, "tsfadjust is: %llu for bslot: %d\n",
447                 (unsigned long long)tsfadjust, avp->bslot);
448 }
449
450 static void ath9k_htc_beacon_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
451 {
452         bool *beacon_configured = (bool *)data;
453         struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
454
455         if (vif->type == NL80211_IFTYPE_STATION &&
456             avp->beacon_configured)
457                 *beacon_configured = true;
458 }
459
460 static bool ath9k_htc_check_beacon_config(struct ath9k_htc_priv *priv,
461                                           struct ieee80211_vif *vif)
462 {
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;
467
468         /*
469          * Changing the beacon interval when multiple AP interfaces
470          * are configured will affect beacon transmission of all
471          * of them.
472          */
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");
479                 return false;
480         }
481
482         /*
483          * If the HW is operating in AP mode, any new station interfaces that
484          * are added cannot change the beacon parameters.
485          */
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");
490                 return false;
491         }
492
493         /*
494          * The beacon parameters are configured only for the first
495          * station interface.
496          */
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);
504
505                 if (beacon_configured) {
506                         ath_dbg(common, CONFIG,
507                                 "Beacon already configured for a station interface\n");
508                         return false;
509                 }
510         }
511
512         return true;
513 }
514
515 void ath9k_htc_beacon_config(struct ath9k_htc_priv *priv,
516                              struct ieee80211_vif *vif)
517 {
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;
522
523         if (!ath9k_htc_check_beacon_config(priv, vif))
524                 return;
525
526         cur_conf->beacon_interval = bss_conf->beacon_int;
527         if (cur_conf->beacon_interval == 0)
528                 cur_conf->beacon_interval = 100;
529
530         cur_conf->dtim_period = bss_conf->dtim_period;
531         cur_conf->bmiss_timeout =
532                 ATH_DEFAULT_BMISS_LIMIT * cur_conf->beacon_interval;
533
534         switch (vif->type) {
535         case NL80211_IFTYPE_STATION:
536                 ath9k_htc_beacon_config_sta(priv, cur_conf);
537                 avp->beacon_configured = true;
538                 break;
539         case NL80211_IFTYPE_ADHOC:
540                 ath9k_htc_beacon_config_adhoc(priv, cur_conf);
541                 break;
542         case NL80211_IFTYPE_MESH_POINT:
543         case NL80211_IFTYPE_AP:
544                 ath9k_htc_beacon_config_ap(priv, cur_conf);
545                 break;
546         default:
547                 ath_dbg(common, CONFIG, "Unsupported beaconing mode\n");
548                 return;
549         }
550 }
551
552 void ath9k_htc_beacon_reconfig(struct ath9k_htc_priv *priv)
553 {
554         struct ath_common *common = ath9k_hw_common(priv->ah);
555         struct ath_beacon_config *cur_conf = &priv->cur_beacon_conf;
556
557         switch (priv->ah->opmode) {
558         case NL80211_IFTYPE_STATION:
559                 ath9k_htc_beacon_config_sta(priv, cur_conf);
560                 break;
561         case NL80211_IFTYPE_ADHOC:
562                 ath9k_htc_beacon_config_adhoc(priv, cur_conf);
563                 break;
564         case NL80211_IFTYPE_MESH_POINT:
565         case NL80211_IFTYPE_AP:
566                 ath9k_htc_beacon_config_ap(priv, cur_conf);
567                 break;
568         default:
569                 ath_dbg(common, CONFIG, "Unsupported beaconing mode\n");
570                 return;
571         }
572 }