ath9k_htc: Start/stop btcoex using a helper
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / ath / ath9k / htc_drv_main.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 /*************/
20 /* Utilities */
21 /*************/
22
23 /* HACK Alert: Use 11NG for 2.4, use 11NA for 5 */
24 static enum htc_phymode ath9k_htc_get_curmode(struct ath9k_htc_priv *priv,
25                                               struct ath9k_channel *ichan)
26 {
27         enum htc_phymode mode;
28
29         mode = -EINVAL;
30
31         switch (ichan->chanmode) {
32         case CHANNEL_G:
33         case CHANNEL_G_HT20:
34         case CHANNEL_G_HT40PLUS:
35         case CHANNEL_G_HT40MINUS:
36                 mode = HTC_MODE_11NG;
37                 break;
38         case CHANNEL_A:
39         case CHANNEL_A_HT20:
40         case CHANNEL_A_HT40PLUS:
41         case CHANNEL_A_HT40MINUS:
42                 mode = HTC_MODE_11NA;
43                 break;
44         default:
45                 break;
46         }
47
48         WARN_ON(mode < 0);
49
50         return mode;
51 }
52
53 bool ath9k_htc_setpower(struct ath9k_htc_priv *priv,
54                         enum ath9k_power_mode mode)
55 {
56         bool ret;
57
58         mutex_lock(&priv->htc_pm_lock);
59         ret = ath9k_hw_setpower(priv->ah, mode);
60         mutex_unlock(&priv->htc_pm_lock);
61
62         return ret;
63 }
64
65 void ath9k_htc_ps_wakeup(struct ath9k_htc_priv *priv)
66 {
67         mutex_lock(&priv->htc_pm_lock);
68         if (++priv->ps_usecount != 1)
69                 goto unlock;
70         ath9k_hw_setpower(priv->ah, ATH9K_PM_AWAKE);
71
72 unlock:
73         mutex_unlock(&priv->htc_pm_lock);
74 }
75
76 void ath9k_htc_ps_restore(struct ath9k_htc_priv *priv)
77 {
78         mutex_lock(&priv->htc_pm_lock);
79         if (--priv->ps_usecount != 0)
80                 goto unlock;
81
82         if (priv->ps_idle)
83                 ath9k_hw_setpower(priv->ah, ATH9K_PM_FULL_SLEEP);
84         else if (priv->ps_enabled)
85                 ath9k_hw_setpower(priv->ah, ATH9K_PM_NETWORK_SLEEP);
86
87 unlock:
88         mutex_unlock(&priv->htc_pm_lock);
89 }
90
91 void ath9k_ps_work(struct work_struct *work)
92 {
93         struct ath9k_htc_priv *priv =
94                 container_of(work, struct ath9k_htc_priv,
95                              ps_work);
96         ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
97
98         /* The chip wakes up after receiving the first beacon
99            while network sleep is enabled. For the driver to
100            be in sync with the hw, set the chip to awake and
101            only then set it to sleep.
102          */
103         ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
104 }
105
106 static void ath9k_htc_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
107 {
108         struct ath9k_htc_priv *priv = data;
109         struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
110
111         if ((vif->type == NL80211_IFTYPE_AP) && bss_conf->enable_beacon)
112                 priv->reconfig_beacon = true;
113
114         if (bss_conf->assoc) {
115                 priv->rearm_ani = true;
116                 priv->reconfig_beacon = true;
117         }
118 }
119
120 static void ath9k_htc_vif_reconfig(struct ath9k_htc_priv *priv)
121 {
122         priv->rearm_ani = false;
123         priv->reconfig_beacon = false;
124
125         ieee80211_iterate_active_interfaces_atomic(priv->hw,
126                                                    ath9k_htc_vif_iter, priv);
127         if (priv->rearm_ani)
128                 ath9k_htc_start_ani(priv);
129
130         if (priv->reconfig_beacon) {
131                 ath9k_htc_ps_wakeup(priv);
132                 ath9k_htc_beacon_reconfig(priv);
133                 ath9k_htc_ps_restore(priv);
134         }
135 }
136
137 static void ath9k_htc_bssid_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
138 {
139         struct ath9k_vif_iter_data *iter_data = data;
140         int i;
141
142         for (i = 0; i < ETH_ALEN; i++)
143                 iter_data->mask[i] &= ~(iter_data->hw_macaddr[i] ^ mac[i]);
144 }
145
146 static void ath9k_htc_set_bssid_mask(struct ath9k_htc_priv *priv,
147                                      struct ieee80211_vif *vif)
148 {
149         struct ath_common *common = ath9k_hw_common(priv->ah);
150         struct ath9k_vif_iter_data iter_data;
151
152         /*
153          * Use the hardware MAC address as reference, the hardware uses it
154          * together with the BSSID mask when matching addresses.
155          */
156         iter_data.hw_macaddr = common->macaddr;
157         memset(&iter_data.mask, 0xff, ETH_ALEN);
158
159         if (vif)
160                 ath9k_htc_bssid_iter(&iter_data, vif->addr, vif);
161
162         /* Get list of all active MAC addresses */
163         ieee80211_iterate_active_interfaces_atomic(priv->hw, ath9k_htc_bssid_iter,
164                                                    &iter_data);
165
166         memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
167         ath_hw_setbssidmask(common);
168 }
169
170 static void ath9k_htc_set_opmode(struct ath9k_htc_priv *priv)
171 {
172         if (priv->num_ibss_vif)
173                 priv->ah->opmode = NL80211_IFTYPE_ADHOC;
174         else if (priv->num_ap_vif)
175                 priv->ah->opmode = NL80211_IFTYPE_AP;
176         else
177                 priv->ah->opmode = NL80211_IFTYPE_STATION;
178
179         ath9k_hw_setopmode(priv->ah);
180 }
181
182 void ath9k_htc_reset(struct ath9k_htc_priv *priv)
183 {
184         struct ath_hw *ah = priv->ah;
185         struct ath_common *common = ath9k_hw_common(ah);
186         struct ieee80211_channel *channel = priv->hw->conf.channel;
187         struct ath9k_hw_cal_data *caldata = NULL;
188         enum htc_phymode mode;
189         __be16 htc_mode;
190         u8 cmd_rsp;
191         int ret;
192
193         mutex_lock(&priv->mutex);
194         ath9k_htc_ps_wakeup(priv);
195
196         ath9k_htc_stop_ani(priv);
197         ieee80211_stop_queues(priv->hw);
198
199         del_timer_sync(&priv->tx.cleanup_timer);
200         ath9k_htc_tx_drain(priv);
201
202         WMI_CMD(WMI_DISABLE_INTR_CMDID);
203         WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
204         WMI_CMD(WMI_STOP_RECV_CMDID);
205
206         ath9k_wmi_event_drain(priv);
207
208         caldata = &priv->caldata;
209         ret = ath9k_hw_reset(ah, ah->curchan, caldata, false);
210         if (ret) {
211                 ath_err(common,
212                         "Unable to reset device (%u Mhz) reset status %d\n",
213                         channel->center_freq, ret);
214         }
215
216         ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
217                                &priv->curtxpow);
218
219         WMI_CMD(WMI_START_RECV_CMDID);
220         ath9k_host_rx_init(priv);
221
222         mode = ath9k_htc_get_curmode(priv, ah->curchan);
223         htc_mode = cpu_to_be16(mode);
224         WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
225
226         WMI_CMD(WMI_ENABLE_INTR_CMDID);
227         htc_start(priv->htc);
228         ath9k_htc_vif_reconfig(priv);
229         ieee80211_wake_queues(priv->hw);
230
231         mod_timer(&priv->tx.cleanup_timer,
232                   jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
233
234         ath9k_htc_ps_restore(priv);
235         mutex_unlock(&priv->mutex);
236 }
237
238 static int ath9k_htc_set_channel(struct ath9k_htc_priv *priv,
239                                  struct ieee80211_hw *hw,
240                                  struct ath9k_channel *hchan)
241 {
242         struct ath_hw *ah = priv->ah;
243         struct ath_common *common = ath9k_hw_common(ah);
244         struct ieee80211_conf *conf = &common->hw->conf;
245         bool fastcc;
246         struct ieee80211_channel *channel = hw->conf.channel;
247         struct ath9k_hw_cal_data *caldata = NULL;
248         enum htc_phymode mode;
249         __be16 htc_mode;
250         u8 cmd_rsp;
251         int ret;
252
253         if (priv->op_flags & OP_INVALID)
254                 return -EIO;
255
256         fastcc = !!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL);
257
258         ath9k_htc_ps_wakeup(priv);
259
260         del_timer_sync(&priv->tx.cleanup_timer);
261         ath9k_htc_tx_drain(priv);
262
263         WMI_CMD(WMI_DISABLE_INTR_CMDID);
264         WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
265         WMI_CMD(WMI_STOP_RECV_CMDID);
266
267         ath9k_wmi_event_drain(priv);
268
269         ath_dbg(common, CONFIG,
270                 "(%u MHz) -> (%u MHz), HT: %d, HT40: %d fastcc: %d\n",
271                 priv->ah->curchan->channel,
272                 channel->center_freq, conf_is_ht(conf), conf_is_ht40(conf),
273                 fastcc);
274
275         if (!fastcc)
276                 caldata = &priv->caldata;
277
278         ret = ath9k_hw_reset(ah, hchan, caldata, fastcc);
279         if (ret) {
280                 ath_err(common,
281                         "Unable to reset channel (%u Mhz) reset status %d\n",
282                         channel->center_freq, ret);
283                 goto err;
284         }
285
286         ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
287                                &priv->curtxpow);
288
289         WMI_CMD(WMI_START_RECV_CMDID);
290         if (ret)
291                 goto err;
292
293         ath9k_host_rx_init(priv);
294
295         mode = ath9k_htc_get_curmode(priv, hchan);
296         htc_mode = cpu_to_be16(mode);
297         WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
298         if (ret)
299                 goto err;
300
301         WMI_CMD(WMI_ENABLE_INTR_CMDID);
302         if (ret)
303                 goto err;
304
305         htc_start(priv->htc);
306
307         if (!(priv->op_flags & OP_SCANNING) &&
308             !(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL))
309                 ath9k_htc_vif_reconfig(priv);
310
311         mod_timer(&priv->tx.cleanup_timer,
312                   jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
313
314 err:
315         ath9k_htc_ps_restore(priv);
316         return ret;
317 }
318
319 /*
320  * Monitor mode handling is a tad complicated because the firmware requires
321  * an interface to be created exclusively, while mac80211 doesn't associate
322  * an interface with the mode.
323  *
324  * So, for now, only one monitor interface can be configured.
325  */
326 static void __ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
327 {
328         struct ath_common *common = ath9k_hw_common(priv->ah);
329         struct ath9k_htc_target_vif hvif;
330         int ret = 0;
331         u8 cmd_rsp;
332
333         memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
334         memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
335         hvif.index = priv->mon_vif_idx;
336         WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
337         if (ret) {
338                 ath_err(common, "Unable to remove monitor interface at idx: %d\n",
339                         priv->mon_vif_idx);
340         }
341
342         priv->nvifs--;
343         priv->vif_slot &= ~(1 << priv->mon_vif_idx);
344 }
345
346 static int ath9k_htc_add_monitor_interface(struct ath9k_htc_priv *priv)
347 {
348         struct ath_common *common = ath9k_hw_common(priv->ah);
349         struct ath9k_htc_target_vif hvif;
350         struct ath9k_htc_target_sta tsta;
351         int ret = 0, sta_idx;
352         u8 cmd_rsp;
353
354         if ((priv->nvifs >= ATH9K_HTC_MAX_VIF) ||
355             (priv->nstations >= ATH9K_HTC_MAX_STA)) {
356                 ret = -ENOBUFS;
357                 goto err_vif;
358         }
359
360         sta_idx = ffz(priv->sta_slot);
361         if ((sta_idx < 0) || (sta_idx > ATH9K_HTC_MAX_STA)) {
362                 ret = -ENOBUFS;
363                 goto err_vif;
364         }
365
366         /*
367          * Add an interface.
368          */
369         memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
370         memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
371
372         hvif.opmode = HTC_M_MONITOR;
373         hvif.index = ffz(priv->vif_slot);
374
375         WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
376         if (ret)
377                 goto err_vif;
378
379         /*
380          * Assign the monitor interface index as a special case here.
381          * This is needed when the interface is brought down.
382          */
383         priv->mon_vif_idx = hvif.index;
384         priv->vif_slot |= (1 << hvif.index);
385
386         /*
387          * Set the hardware mode to monitor only if there are no
388          * other interfaces.
389          */
390         if (!priv->nvifs)
391                 priv->ah->opmode = NL80211_IFTYPE_MONITOR;
392
393         priv->nvifs++;
394
395         /*
396          * Associate a station with the interface for packet injection.
397          */
398         memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
399
400         memcpy(&tsta.macaddr, common->macaddr, ETH_ALEN);
401
402         tsta.is_vif_sta = 1;
403         tsta.sta_index = sta_idx;
404         tsta.vif_index = hvif.index;
405         tsta.maxampdu = cpu_to_be16(0xffff);
406
407         WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
408         if (ret) {
409                 ath_err(common, "Unable to add station entry for monitor mode\n");
410                 goto err_sta;
411         }
412
413         priv->sta_slot |= (1 << sta_idx);
414         priv->nstations++;
415         priv->vif_sta_pos[priv->mon_vif_idx] = sta_idx;
416         priv->ah->is_monitoring = true;
417
418         ath_dbg(common, CONFIG,
419                 "Attached a monitor interface at idx: %d, sta idx: %d\n",
420                 priv->mon_vif_idx, sta_idx);
421
422         return 0;
423
424 err_sta:
425         /*
426          * Remove the interface from the target.
427          */
428         __ath9k_htc_remove_monitor_interface(priv);
429 err_vif:
430         ath_dbg(common, FATAL, "Unable to attach a monitor interface\n");
431
432         return ret;
433 }
434
435 static int ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
436 {
437         struct ath_common *common = ath9k_hw_common(priv->ah);
438         int ret = 0;
439         u8 cmd_rsp, sta_idx;
440
441         __ath9k_htc_remove_monitor_interface(priv);
442
443         sta_idx = priv->vif_sta_pos[priv->mon_vif_idx];
444
445         WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
446         if (ret) {
447                 ath_err(common, "Unable to remove station entry for monitor mode\n");
448                 return ret;
449         }
450
451         priv->sta_slot &= ~(1 << sta_idx);
452         priv->nstations--;
453         priv->ah->is_monitoring = false;
454
455         ath_dbg(common, CONFIG,
456                 "Removed a monitor interface at idx: %d, sta idx: %d\n",
457                 priv->mon_vif_idx, sta_idx);
458
459         return 0;
460 }
461
462 static int ath9k_htc_add_station(struct ath9k_htc_priv *priv,
463                                  struct ieee80211_vif *vif,
464                                  struct ieee80211_sta *sta)
465 {
466         struct ath_common *common = ath9k_hw_common(priv->ah);
467         struct ath9k_htc_target_sta tsta;
468         struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
469         struct ath9k_htc_sta *ista;
470         int ret, sta_idx;
471         u8 cmd_rsp;
472         u16 maxampdu;
473
474         if (priv->nstations >= ATH9K_HTC_MAX_STA)
475                 return -ENOBUFS;
476
477         sta_idx = ffz(priv->sta_slot);
478         if ((sta_idx < 0) || (sta_idx > ATH9K_HTC_MAX_STA))
479                 return -ENOBUFS;
480
481         memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
482
483         if (sta) {
484                 ista = (struct ath9k_htc_sta *) sta->drv_priv;
485                 memcpy(&tsta.macaddr, sta->addr, ETH_ALEN);
486                 memcpy(&tsta.bssid, common->curbssid, ETH_ALEN);
487                 tsta.is_vif_sta = 0;
488                 ista->index = sta_idx;
489         } else {
490                 memcpy(&tsta.macaddr, vif->addr, ETH_ALEN);
491                 tsta.is_vif_sta = 1;
492         }
493
494         tsta.sta_index = sta_idx;
495         tsta.vif_index = avp->index;
496
497         if (!sta) {
498                 tsta.maxampdu = cpu_to_be16(0xffff);
499         } else {
500                 maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
501                                  sta->ht_cap.ampdu_factor);
502                 tsta.maxampdu = cpu_to_be16(maxampdu);
503         }
504
505         WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
506         if (ret) {
507                 if (sta)
508                         ath_err(common,
509                                 "Unable to add station entry for: %pM\n",
510                                 sta->addr);
511                 return ret;
512         }
513
514         if (sta) {
515                 ath_dbg(common, CONFIG,
516                         "Added a station entry for: %pM (idx: %d)\n",
517                         sta->addr, tsta.sta_index);
518         } else {
519                 ath_dbg(common, CONFIG,
520                         "Added a station entry for VIF %d (idx: %d)\n",
521                         avp->index, tsta.sta_index);
522         }
523
524         priv->sta_slot |= (1 << sta_idx);
525         priv->nstations++;
526         if (!sta)
527                 priv->vif_sta_pos[avp->index] = sta_idx;
528
529         return 0;
530 }
531
532 static int ath9k_htc_remove_station(struct ath9k_htc_priv *priv,
533                                     struct ieee80211_vif *vif,
534                                     struct ieee80211_sta *sta)
535 {
536         struct ath_common *common = ath9k_hw_common(priv->ah);
537         struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
538         struct ath9k_htc_sta *ista;
539         int ret;
540         u8 cmd_rsp, sta_idx;
541
542         if (sta) {
543                 ista = (struct ath9k_htc_sta *) sta->drv_priv;
544                 sta_idx = ista->index;
545         } else {
546                 sta_idx = priv->vif_sta_pos[avp->index];
547         }
548
549         WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
550         if (ret) {
551                 if (sta)
552                         ath_err(common,
553                                 "Unable to remove station entry for: %pM\n",
554                                 sta->addr);
555                 return ret;
556         }
557
558         if (sta) {
559                 ath_dbg(common, CONFIG,
560                         "Removed a station entry for: %pM (idx: %d)\n",
561                         sta->addr, sta_idx);
562         } else {
563                 ath_dbg(common, CONFIG,
564                         "Removed a station entry for VIF %d (idx: %d)\n",
565                         avp->index, sta_idx);
566         }
567
568         priv->sta_slot &= ~(1 << sta_idx);
569         priv->nstations--;
570
571         return 0;
572 }
573
574 int ath9k_htc_update_cap_target(struct ath9k_htc_priv *priv,
575                                 u8 enable_coex)
576 {
577         struct ath9k_htc_cap_target tcap;
578         int ret;
579         u8 cmd_rsp;
580
581         memset(&tcap, 0, sizeof(struct ath9k_htc_cap_target));
582
583         tcap.ampdu_limit = cpu_to_be32(0xffff);
584         tcap.ampdu_subframes = 0xff;
585         tcap.enable_coex = enable_coex;
586         tcap.tx_chainmask = priv->ah->caps.tx_chainmask;
587
588         WMI_CMD_BUF(WMI_TARGET_IC_UPDATE_CMDID, &tcap);
589
590         return ret;
591 }
592
593 static void ath9k_htc_setup_rate(struct ath9k_htc_priv *priv,
594                                  struct ieee80211_sta *sta,
595                                  struct ath9k_htc_target_rate *trate)
596 {
597         struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
598         struct ieee80211_supported_band *sband;
599         u32 caps = 0;
600         int i, j;
601
602         sband = priv->hw->wiphy->bands[priv->hw->conf.channel->band];
603
604         for (i = 0, j = 0; i < sband->n_bitrates; i++) {
605                 if (sta->supp_rates[sband->band] & BIT(i)) {
606                         trate->rates.legacy_rates.rs_rates[j]
607                                 = (sband->bitrates[i].bitrate * 2) / 10;
608                         j++;
609                 }
610         }
611         trate->rates.legacy_rates.rs_nrates = j;
612
613         if (sta->ht_cap.ht_supported) {
614                 for (i = 0, j = 0; i < 77; i++) {
615                         if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
616                                 trate->rates.ht_rates.rs_rates[j++] = i;
617                         if (j == ATH_HTC_RATE_MAX)
618                                 break;
619                 }
620                 trate->rates.ht_rates.rs_nrates = j;
621
622                 caps = WLAN_RC_HT_FLAG;
623                 if (sta->ht_cap.mcs.rx_mask[1])
624                         caps |= WLAN_RC_DS_FLAG;
625                 if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
626                      (conf_is_ht40(&priv->hw->conf)))
627                         caps |= WLAN_RC_40_FLAG;
628                 if (conf_is_ht40(&priv->hw->conf) &&
629                     (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40))
630                         caps |= WLAN_RC_SGI_FLAG;
631                 else if (conf_is_ht20(&priv->hw->conf) &&
632                          (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20))
633                         caps |= WLAN_RC_SGI_FLAG;
634         }
635
636         trate->sta_index = ista->index;
637         trate->isnew = 1;
638         trate->capflags = cpu_to_be32(caps);
639 }
640
641 static int ath9k_htc_send_rate_cmd(struct ath9k_htc_priv *priv,
642                                     struct ath9k_htc_target_rate *trate)
643 {
644         struct ath_common *common = ath9k_hw_common(priv->ah);
645         int ret;
646         u8 cmd_rsp;
647
648         WMI_CMD_BUF(WMI_RC_RATE_UPDATE_CMDID, trate);
649         if (ret) {
650                 ath_err(common,
651                         "Unable to initialize Rate information on target\n");
652         }
653
654         return ret;
655 }
656
657 static void ath9k_htc_init_rate(struct ath9k_htc_priv *priv,
658                                 struct ieee80211_sta *sta)
659 {
660         struct ath_common *common = ath9k_hw_common(priv->ah);
661         struct ath9k_htc_target_rate trate;
662         int ret;
663
664         memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
665         ath9k_htc_setup_rate(priv, sta, &trate);
666         ret = ath9k_htc_send_rate_cmd(priv, &trate);
667         if (!ret)
668                 ath_dbg(common, CONFIG,
669                         "Updated target sta: %pM, rate caps: 0x%X\n",
670                         sta->addr, be32_to_cpu(trate.capflags));
671 }
672
673 static void ath9k_htc_update_rate(struct ath9k_htc_priv *priv,
674                                   struct ieee80211_vif *vif,
675                                   struct ieee80211_bss_conf *bss_conf)
676 {
677         struct ath_common *common = ath9k_hw_common(priv->ah);
678         struct ath9k_htc_target_rate trate;
679         struct ieee80211_sta *sta;
680         int ret;
681
682         memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
683
684         rcu_read_lock();
685         sta = ieee80211_find_sta(vif, bss_conf->bssid);
686         if (!sta) {
687                 rcu_read_unlock();
688                 return;
689         }
690         ath9k_htc_setup_rate(priv, sta, &trate);
691         rcu_read_unlock();
692
693         ret = ath9k_htc_send_rate_cmd(priv, &trate);
694         if (!ret)
695                 ath_dbg(common, CONFIG,
696                         "Updated target sta: %pM, rate caps: 0x%X\n",
697                         bss_conf->bssid, be32_to_cpu(trate.capflags));
698 }
699
700 static int ath9k_htc_tx_aggr_oper(struct ath9k_htc_priv *priv,
701                                   struct ieee80211_vif *vif,
702                                   struct ieee80211_sta *sta,
703                                   enum ieee80211_ampdu_mlme_action action,
704                                   u16 tid)
705 {
706         struct ath_common *common = ath9k_hw_common(priv->ah);
707         struct ath9k_htc_target_aggr aggr;
708         struct ath9k_htc_sta *ista;
709         int ret = 0;
710         u8 cmd_rsp;
711
712         if (tid >= ATH9K_HTC_MAX_TID)
713                 return -EINVAL;
714
715         memset(&aggr, 0, sizeof(struct ath9k_htc_target_aggr));
716         ista = (struct ath9k_htc_sta *) sta->drv_priv;
717
718         aggr.sta_index = ista->index;
719         aggr.tidno = tid & 0xf;
720         aggr.aggr_enable = (action == IEEE80211_AMPDU_TX_START) ? true : false;
721
722         WMI_CMD_BUF(WMI_TX_AGGR_ENABLE_CMDID, &aggr);
723         if (ret)
724                 ath_dbg(common, CONFIG,
725                         "Unable to %s TX aggregation for (%pM, %d)\n",
726                         (aggr.aggr_enable) ? "start" : "stop", sta->addr, tid);
727         else
728                 ath_dbg(common, CONFIG,
729                         "%s TX aggregation for (%pM, %d)\n",
730                         (aggr.aggr_enable) ? "Starting" : "Stopping",
731                         sta->addr, tid);
732
733         spin_lock_bh(&priv->tx.tx_lock);
734         ista->tid_state[tid] = (aggr.aggr_enable && !ret) ? AGGR_START : AGGR_STOP;
735         spin_unlock_bh(&priv->tx.tx_lock);
736
737         return ret;
738 }
739
740 /*******/
741 /* ANI */
742 /*******/
743
744 void ath9k_htc_start_ani(struct ath9k_htc_priv *priv)
745 {
746         struct ath_common *common = ath9k_hw_common(priv->ah);
747         unsigned long timestamp = jiffies_to_msecs(jiffies);
748
749         common->ani.longcal_timer = timestamp;
750         common->ani.shortcal_timer = timestamp;
751         common->ani.checkani_timer = timestamp;
752
753         priv->op_flags |= OP_ANI_RUNNING;
754
755         ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
756                                      msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
757 }
758
759 void ath9k_htc_stop_ani(struct ath9k_htc_priv *priv)
760 {
761         cancel_delayed_work_sync(&priv->ani_work);
762         priv->op_flags &= ~OP_ANI_RUNNING;
763 }
764
765 void ath9k_htc_ani_work(struct work_struct *work)
766 {
767         struct ath9k_htc_priv *priv =
768                 container_of(work, struct ath9k_htc_priv, ani_work.work);
769         struct ath_hw *ah = priv->ah;
770         struct ath_common *common = ath9k_hw_common(ah);
771         bool longcal = false;
772         bool shortcal = false;
773         bool aniflag = false;
774         unsigned int timestamp = jiffies_to_msecs(jiffies);
775         u32 cal_interval, short_cal_interval;
776
777         short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
778                 ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
779
780         /* Only calibrate if awake */
781         if (ah->power_mode != ATH9K_PM_AWAKE)
782                 goto set_timer;
783
784         /* Long calibration runs independently of short calibration. */
785         if ((timestamp - common->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
786                 longcal = true;
787                 ath_dbg(common, ANI, "longcal @%lu\n", jiffies);
788                 common->ani.longcal_timer = timestamp;
789         }
790
791         /* Short calibration applies only while caldone is false */
792         if (!common->ani.caldone) {
793                 if ((timestamp - common->ani.shortcal_timer) >=
794                     short_cal_interval) {
795                         shortcal = true;
796                         ath_dbg(common, ANI, "shortcal @%lu\n", jiffies);
797                         common->ani.shortcal_timer = timestamp;
798                         common->ani.resetcal_timer = timestamp;
799                 }
800         } else {
801                 if ((timestamp - common->ani.resetcal_timer) >=
802                     ATH_RESTART_CALINTERVAL) {
803                         common->ani.caldone = ath9k_hw_reset_calvalid(ah);
804                         if (common->ani.caldone)
805                                 common->ani.resetcal_timer = timestamp;
806                 }
807         }
808
809         /* Verify whether we must check ANI */
810         if (ah->config.enable_ani &&
811             (timestamp - common->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
812                 aniflag = true;
813                 common->ani.checkani_timer = timestamp;
814         }
815
816         /* Skip all processing if there's nothing to do. */
817         if (longcal || shortcal || aniflag) {
818
819                 ath9k_htc_ps_wakeup(priv);
820
821                 /* Call ANI routine if necessary */
822                 if (aniflag)
823                         ath9k_hw_ani_monitor(ah, ah->curchan);
824
825                 /* Perform calibration if necessary */
826                 if (longcal || shortcal)
827                         common->ani.caldone =
828                                 ath9k_hw_calibrate(ah, ah->curchan,
829                                                    ah->rxchainmask, longcal);
830
831                 ath9k_htc_ps_restore(priv);
832         }
833
834 set_timer:
835         /*
836         * Set timer interval based on previous results.
837         * The interval must be the shortest necessary to satisfy ANI,
838         * short calibration and long calibration.
839         */
840         cal_interval = ATH_LONG_CALINTERVAL;
841         if (ah->config.enable_ani)
842                 cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
843         if (!common->ani.caldone)
844                 cal_interval = min(cal_interval, (u32)short_cal_interval);
845
846         ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
847                                      msecs_to_jiffies(cal_interval));
848 }
849
850 /**********************/
851 /* mac80211 Callbacks */
852 /**********************/
853
854 static void ath9k_htc_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
855 {
856         struct ieee80211_hdr *hdr;
857         struct ath9k_htc_priv *priv = hw->priv;
858         struct ath_common *common = ath9k_hw_common(priv->ah);
859         int padpos, padsize, ret, slot;
860
861         hdr = (struct ieee80211_hdr *) skb->data;
862
863         /* Add the padding after the header if this is not already done */
864         padpos = ath9k_cmn_padpos(hdr->frame_control);
865         padsize = padpos & 3;
866         if (padsize && skb->len > padpos) {
867                 if (skb_headroom(skb) < padsize) {
868                         ath_dbg(common, XMIT, "No room for padding\n");
869                         goto fail_tx;
870                 }
871                 skb_push(skb, padsize);
872                 memmove(skb->data, skb->data + padsize, padpos);
873         }
874
875         slot = ath9k_htc_tx_get_slot(priv);
876         if (slot < 0) {
877                 ath_dbg(common, XMIT, "No free TX slot\n");
878                 goto fail_tx;
879         }
880
881         ret = ath9k_htc_tx_start(priv, skb, slot, false);
882         if (ret != 0) {
883                 ath_dbg(common, XMIT, "Tx failed\n");
884                 goto clear_slot;
885         }
886
887         ath9k_htc_check_stop_queues(priv);
888
889         return;
890
891 clear_slot:
892         ath9k_htc_tx_clear_slot(priv, slot);
893 fail_tx:
894         dev_kfree_skb_any(skb);
895 }
896
897 static int ath9k_htc_start(struct ieee80211_hw *hw)
898 {
899         struct ath9k_htc_priv *priv = hw->priv;
900         struct ath_hw *ah = priv->ah;
901         struct ath_common *common = ath9k_hw_common(ah);
902         struct ieee80211_channel *curchan = hw->conf.channel;
903         struct ath9k_channel *init_channel;
904         int ret = 0;
905         enum htc_phymode mode;
906         __be16 htc_mode;
907         u8 cmd_rsp;
908
909         mutex_lock(&priv->mutex);
910
911         ath_dbg(common, CONFIG,
912                 "Starting driver with initial channel: %d MHz\n",
913                 curchan->center_freq);
914
915         /* Ensure that HW is awake before flushing RX */
916         ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
917         WMI_CMD(WMI_FLUSH_RECV_CMDID);
918
919         /* setup initial channel */
920         init_channel = ath9k_cmn_get_curchannel(hw, ah);
921
922         ath9k_hw_htc_resetinit(ah);
923         ret = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
924         if (ret) {
925                 ath_err(common,
926                         "Unable to reset hardware; reset status %d (freq %u MHz)\n",
927                         ret, curchan->center_freq);
928                 mutex_unlock(&priv->mutex);
929                 return ret;
930         }
931
932         ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
933                                &priv->curtxpow);
934
935         mode = ath9k_htc_get_curmode(priv, init_channel);
936         htc_mode = cpu_to_be16(mode);
937         WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
938         WMI_CMD(WMI_ATH_INIT_CMDID);
939         WMI_CMD(WMI_START_RECV_CMDID);
940
941         ath9k_host_rx_init(priv);
942
943         ret = ath9k_htc_update_cap_target(priv, 0);
944         if (ret)
945                 ath_dbg(common, CONFIG,
946                         "Failed to update capability in target\n");
947
948         priv->op_flags &= ~OP_INVALID;
949         htc_start(priv->htc);
950
951         spin_lock_bh(&priv->tx.tx_lock);
952         priv->tx.flags &= ~ATH9K_HTC_OP_TX_QUEUES_STOP;
953         spin_unlock_bh(&priv->tx.tx_lock);
954
955         ieee80211_wake_queues(hw);
956
957         mod_timer(&priv->tx.cleanup_timer,
958                   jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
959
960         ath9k_htc_start_btcoex(priv);
961
962         mutex_unlock(&priv->mutex);
963
964         return ret;
965 }
966
967 static void ath9k_htc_stop(struct ieee80211_hw *hw)
968 {
969         struct ath9k_htc_priv *priv = hw->priv;
970         struct ath_hw *ah = priv->ah;
971         struct ath_common *common = ath9k_hw_common(ah);
972         int ret __attribute__ ((unused));
973         u8 cmd_rsp;
974
975         mutex_lock(&priv->mutex);
976
977         if (priv->op_flags & OP_INVALID) {
978                 ath_dbg(common, ANY, "Device not present\n");
979                 mutex_unlock(&priv->mutex);
980                 return;
981         }
982
983         ath9k_htc_ps_wakeup(priv);
984
985         WMI_CMD(WMI_DISABLE_INTR_CMDID);
986         WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
987         WMI_CMD(WMI_STOP_RECV_CMDID);
988
989         tasklet_kill(&priv->rx_tasklet);
990
991         del_timer_sync(&priv->tx.cleanup_timer);
992         ath9k_htc_tx_drain(priv);
993         ath9k_wmi_event_drain(priv);
994
995         mutex_unlock(&priv->mutex);
996
997         /* Cancel all the running timers/work .. */
998         cancel_work_sync(&priv->fatal_work);
999         cancel_work_sync(&priv->ps_work);
1000
1001 #ifdef CONFIG_MAC80211_LEDS
1002         cancel_work_sync(&priv->led_work);
1003 #endif
1004         ath9k_htc_stop_ani(priv);
1005
1006         mutex_lock(&priv->mutex);
1007
1008         ath9k_htc_stop_btcoex(priv);
1009
1010         /* Remove a monitor interface if it's present. */
1011         if (priv->ah->is_monitoring)
1012                 ath9k_htc_remove_monitor_interface(priv);
1013
1014         ath9k_hw_phy_disable(ah);
1015         ath9k_hw_disable(ah);
1016         ath9k_htc_ps_restore(priv);
1017         ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);
1018
1019         priv->op_flags |= OP_INVALID;
1020
1021         ath_dbg(common, CONFIG, "Driver halt\n");
1022         mutex_unlock(&priv->mutex);
1023 }
1024
1025 static int ath9k_htc_add_interface(struct ieee80211_hw *hw,
1026                                    struct ieee80211_vif *vif)
1027 {
1028         struct ath9k_htc_priv *priv = hw->priv;
1029         struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1030         struct ath_common *common = ath9k_hw_common(priv->ah);
1031         struct ath9k_htc_target_vif hvif;
1032         int ret = 0;
1033         u8 cmd_rsp;
1034
1035         mutex_lock(&priv->mutex);
1036
1037         if (priv->nvifs >= ATH9K_HTC_MAX_VIF) {
1038                 mutex_unlock(&priv->mutex);
1039                 return -ENOBUFS;
1040         }
1041
1042         if (priv->num_ibss_vif ||
1043             (priv->nvifs && vif->type == NL80211_IFTYPE_ADHOC)) {
1044                 ath_err(common, "IBSS coexistence with other modes is not allowed\n");
1045                 mutex_unlock(&priv->mutex);
1046                 return -ENOBUFS;
1047         }
1048
1049         if (((vif->type == NL80211_IFTYPE_AP) ||
1050              (vif->type == NL80211_IFTYPE_ADHOC)) &&
1051             ((priv->num_ap_vif + priv->num_ibss_vif) >= ATH9K_HTC_MAX_BCN_VIF)) {
1052                 ath_err(common, "Max. number of beaconing interfaces reached\n");
1053                 mutex_unlock(&priv->mutex);
1054                 return -ENOBUFS;
1055         }
1056
1057         ath9k_htc_ps_wakeup(priv);
1058         memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1059         memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1060
1061         switch (vif->type) {
1062         case NL80211_IFTYPE_STATION:
1063                 hvif.opmode = HTC_M_STA;
1064                 break;
1065         case NL80211_IFTYPE_ADHOC:
1066                 hvif.opmode = HTC_M_IBSS;
1067                 break;
1068         case NL80211_IFTYPE_AP:
1069                 hvif.opmode = HTC_M_HOSTAP;
1070                 break;
1071         default:
1072                 ath_err(common,
1073                         "Interface type %d not yet supported\n", vif->type);
1074                 ret = -EOPNOTSUPP;
1075                 goto out;
1076         }
1077
1078         /* Index starts from zero on the target */
1079         avp->index = hvif.index = ffz(priv->vif_slot);
1080         hvif.rtsthreshold = cpu_to_be16(2304);
1081         WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
1082         if (ret)
1083                 goto out;
1084
1085         /*
1086          * We need a node in target to tx mgmt frames
1087          * before association.
1088          */
1089         ret = ath9k_htc_add_station(priv, vif, NULL);
1090         if (ret) {
1091                 WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1092                 goto out;
1093         }
1094
1095         ath9k_htc_set_bssid_mask(priv, vif);
1096
1097         priv->vif_slot |= (1 << avp->index);
1098         priv->nvifs++;
1099
1100         INC_VIF(priv, vif->type);
1101
1102         if ((vif->type == NL80211_IFTYPE_AP) ||
1103             (vif->type == NL80211_IFTYPE_ADHOC))
1104                 ath9k_htc_assign_bslot(priv, vif);
1105
1106         ath9k_htc_set_opmode(priv);
1107
1108         if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
1109             !(priv->op_flags & OP_ANI_RUNNING)) {
1110                 ath9k_hw_set_tsfadjust(priv->ah, 1);
1111                 ath9k_htc_start_ani(priv);
1112         }
1113
1114         ath_dbg(common, CONFIG, "Attach a VIF of type: %d at idx: %d\n",
1115                 vif->type, avp->index);
1116
1117 out:
1118         ath9k_htc_ps_restore(priv);
1119         mutex_unlock(&priv->mutex);
1120
1121         return ret;
1122 }
1123
1124 static void ath9k_htc_remove_interface(struct ieee80211_hw *hw,
1125                                        struct ieee80211_vif *vif)
1126 {
1127         struct ath9k_htc_priv *priv = hw->priv;
1128         struct ath_common *common = ath9k_hw_common(priv->ah);
1129         struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1130         struct ath9k_htc_target_vif hvif;
1131         int ret = 0;
1132         u8 cmd_rsp;
1133
1134         mutex_lock(&priv->mutex);
1135         ath9k_htc_ps_wakeup(priv);
1136
1137         memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1138         memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1139         hvif.index = avp->index;
1140         WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1141         if (ret) {
1142                 ath_err(common, "Unable to remove interface at idx: %d\n",
1143                         avp->index);
1144         }
1145         priv->nvifs--;
1146         priv->vif_slot &= ~(1 << avp->index);
1147
1148         ath9k_htc_remove_station(priv, vif, NULL);
1149
1150         DEC_VIF(priv, vif->type);
1151
1152         if ((vif->type == NL80211_IFTYPE_AP) ||
1153             (vif->type == NL80211_IFTYPE_ADHOC))
1154                 ath9k_htc_remove_bslot(priv, vif);
1155
1156         ath9k_htc_set_opmode(priv);
1157
1158         ath9k_htc_set_bssid_mask(priv, vif);
1159
1160         /*
1161          * Stop ANI only if there are no associated station interfaces.
1162          */
1163         if ((vif->type == NL80211_IFTYPE_AP) && (priv->num_ap_vif == 0)) {
1164                 priv->rearm_ani = false;
1165                 ieee80211_iterate_active_interfaces_atomic(priv->hw,
1166                                                    ath9k_htc_vif_iter, priv);
1167                 if (!priv->rearm_ani)
1168                         ath9k_htc_stop_ani(priv);
1169         }
1170
1171         ath_dbg(common, CONFIG, "Detach Interface at idx: %d\n", avp->index);
1172
1173         ath9k_htc_ps_restore(priv);
1174         mutex_unlock(&priv->mutex);
1175 }
1176
1177 static int ath9k_htc_config(struct ieee80211_hw *hw, u32 changed)
1178 {
1179         struct ath9k_htc_priv *priv = hw->priv;
1180         struct ath_common *common = ath9k_hw_common(priv->ah);
1181         struct ieee80211_conf *conf = &hw->conf;
1182
1183         mutex_lock(&priv->mutex);
1184
1185         if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1186                 bool enable_radio = false;
1187                 bool idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1188
1189                 mutex_lock(&priv->htc_pm_lock);
1190                 if (!idle && priv->ps_idle)
1191                         enable_radio = true;
1192                 priv->ps_idle = idle;
1193                 mutex_unlock(&priv->htc_pm_lock);
1194
1195                 if (enable_radio) {
1196                         ath_dbg(common, CONFIG, "not-idle: enabling radio\n");
1197                         ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1198                         ath9k_htc_radio_enable(hw);
1199                 }
1200         }
1201
1202         /*
1203          * Monitor interface should be added before
1204          * IEEE80211_CONF_CHANGE_CHANNEL is handled.
1205          */
1206         if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1207                 if ((conf->flags & IEEE80211_CONF_MONITOR) &&
1208                     !priv->ah->is_monitoring)
1209                         ath9k_htc_add_monitor_interface(priv);
1210                 else if (priv->ah->is_monitoring)
1211                         ath9k_htc_remove_monitor_interface(priv);
1212         }
1213
1214         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1215                 struct ieee80211_channel *curchan = hw->conf.channel;
1216                 int pos = curchan->hw_value;
1217
1218                 ath_dbg(common, CONFIG, "Set channel: %d MHz\n",
1219                         curchan->center_freq);
1220
1221                 ath9k_cmn_update_ichannel(&priv->ah->channels[pos],
1222                                           hw->conf.channel,
1223                                           hw->conf.channel_type);
1224
1225                 if (ath9k_htc_set_channel(priv, hw, &priv->ah->channels[pos]) < 0) {
1226                         ath_err(common, "Unable to set channel\n");
1227                         mutex_unlock(&priv->mutex);
1228                         return -EINVAL;
1229                 }
1230
1231         }
1232
1233         if (changed & IEEE80211_CONF_CHANGE_PS) {
1234                 if (conf->flags & IEEE80211_CONF_PS) {
1235                         ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
1236                         priv->ps_enabled = true;
1237                 } else {
1238                         priv->ps_enabled = false;
1239                         cancel_work_sync(&priv->ps_work);
1240                         ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1241                 }
1242         }
1243
1244         if (changed & IEEE80211_CONF_CHANGE_POWER) {
1245                 priv->txpowlimit = 2 * conf->power_level;
1246                 ath9k_cmn_update_txpow(priv->ah, priv->curtxpow,
1247                                        priv->txpowlimit, &priv->curtxpow);
1248         }
1249
1250         if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1251                 mutex_lock(&priv->htc_pm_lock);
1252                 if (!priv->ps_idle) {
1253                         mutex_unlock(&priv->htc_pm_lock);
1254                         goto out;
1255                 }
1256                 mutex_unlock(&priv->htc_pm_lock);
1257
1258                 ath_dbg(common, CONFIG, "idle: disabling radio\n");
1259                 ath9k_htc_radio_disable(hw);
1260         }
1261
1262 out:
1263         mutex_unlock(&priv->mutex);
1264         return 0;
1265 }
1266
1267 #define SUPPORTED_FILTERS                       \
1268         (FIF_PROMISC_IN_BSS |                   \
1269         FIF_ALLMULTI |                          \
1270         FIF_CONTROL |                           \
1271         FIF_PSPOLL |                            \
1272         FIF_OTHER_BSS |                         \
1273         FIF_BCN_PRBRESP_PROMISC |               \
1274         FIF_PROBE_REQ |                         \
1275         FIF_FCSFAIL)
1276
1277 static void ath9k_htc_configure_filter(struct ieee80211_hw *hw,
1278                                        unsigned int changed_flags,
1279                                        unsigned int *total_flags,
1280                                        u64 multicast)
1281 {
1282         struct ath9k_htc_priv *priv = hw->priv;
1283         u32 rfilt;
1284
1285         mutex_lock(&priv->mutex);
1286         changed_flags &= SUPPORTED_FILTERS;
1287         *total_flags &= SUPPORTED_FILTERS;
1288
1289         if (priv->op_flags & OP_INVALID) {
1290                 ath_dbg(ath9k_hw_common(priv->ah), ANY,
1291                         "Unable to configure filter on invalid state\n");
1292                 mutex_unlock(&priv->mutex);
1293                 return;
1294         }
1295         ath9k_htc_ps_wakeup(priv);
1296
1297         priv->rxfilter = *total_flags;
1298         rfilt = ath9k_htc_calcrxfilter(priv);
1299         ath9k_hw_setrxfilter(priv->ah, rfilt);
1300
1301         ath_dbg(ath9k_hw_common(priv->ah), CONFIG, "Set HW RX filter: 0x%x\n",
1302                 rfilt);
1303
1304         ath9k_htc_ps_restore(priv);
1305         mutex_unlock(&priv->mutex);
1306 }
1307
1308 static int ath9k_htc_sta_add(struct ieee80211_hw *hw,
1309                              struct ieee80211_vif *vif,
1310                              struct ieee80211_sta *sta)
1311 {
1312         struct ath9k_htc_priv *priv = hw->priv;
1313         int ret;
1314
1315         mutex_lock(&priv->mutex);
1316         ath9k_htc_ps_wakeup(priv);
1317         ret = ath9k_htc_add_station(priv, vif, sta);
1318         if (!ret)
1319                 ath9k_htc_init_rate(priv, sta);
1320         ath9k_htc_ps_restore(priv);
1321         mutex_unlock(&priv->mutex);
1322
1323         return ret;
1324 }
1325
1326 static int ath9k_htc_sta_remove(struct ieee80211_hw *hw,
1327                                 struct ieee80211_vif *vif,
1328                                 struct ieee80211_sta *sta)
1329 {
1330         struct ath9k_htc_priv *priv = hw->priv;
1331         struct ath9k_htc_sta *ista;
1332         int ret;
1333
1334         mutex_lock(&priv->mutex);
1335         ath9k_htc_ps_wakeup(priv);
1336         ista = (struct ath9k_htc_sta *) sta->drv_priv;
1337         htc_sta_drain(priv->htc, ista->index);
1338         ret = ath9k_htc_remove_station(priv, vif, sta);
1339         ath9k_htc_ps_restore(priv);
1340         mutex_unlock(&priv->mutex);
1341
1342         return ret;
1343 }
1344
1345 static int ath9k_htc_conf_tx(struct ieee80211_hw *hw,
1346                              struct ieee80211_vif *vif, u16 queue,
1347                              const struct ieee80211_tx_queue_params *params)
1348 {
1349         struct ath9k_htc_priv *priv = hw->priv;
1350         struct ath_common *common = ath9k_hw_common(priv->ah);
1351         struct ath9k_tx_queue_info qi;
1352         int ret = 0, qnum;
1353
1354         if (queue >= WME_NUM_AC)
1355                 return 0;
1356
1357         mutex_lock(&priv->mutex);
1358         ath9k_htc_ps_wakeup(priv);
1359
1360         memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1361
1362         qi.tqi_aifs = params->aifs;
1363         qi.tqi_cwmin = params->cw_min;
1364         qi.tqi_cwmax = params->cw_max;
1365         qi.tqi_burstTime = params->txop;
1366
1367         qnum = get_hw_qnum(queue, priv->hwq_map);
1368
1369         ath_dbg(common, CONFIG,
1370                 "Configure tx [queue/hwq] [%d/%d],  aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1371                 queue, qnum, params->aifs, params->cw_min,
1372                 params->cw_max, params->txop);
1373
1374         ret = ath_htc_txq_update(priv, qnum, &qi);
1375         if (ret) {
1376                 ath_err(common, "TXQ Update failed\n");
1377                 goto out;
1378         }
1379
1380         if ((priv->ah->opmode == NL80211_IFTYPE_ADHOC) &&
1381             (qnum == priv->hwq_map[WME_AC_BE]))
1382                     ath9k_htc_beaconq_config(priv);
1383 out:
1384         ath9k_htc_ps_restore(priv);
1385         mutex_unlock(&priv->mutex);
1386
1387         return ret;
1388 }
1389
1390 static int ath9k_htc_set_key(struct ieee80211_hw *hw,
1391                              enum set_key_cmd cmd,
1392                              struct ieee80211_vif *vif,
1393                              struct ieee80211_sta *sta,
1394                              struct ieee80211_key_conf *key)
1395 {
1396         struct ath9k_htc_priv *priv = hw->priv;
1397         struct ath_common *common = ath9k_hw_common(priv->ah);
1398         int ret = 0;
1399
1400         if (htc_modparam_nohwcrypt)
1401                 return -ENOSPC;
1402
1403         if ((vif->type == NL80211_IFTYPE_ADHOC ||
1404              vif->type == NL80211_IFTYPE_MESH_POINT) &&
1405             (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
1406              key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
1407             !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1408                 /*
1409                  * For now, disable hw crypto for the RSN IBSS group keys. This
1410                  * could be optimized in the future to use a modified key cache
1411                  * design to support per-STA RX GTK, but until that gets
1412                  * implemented, use of software crypto for group addressed
1413                  * frames is a acceptable to allow RSN IBSS to be used.
1414                  */
1415                 return -EOPNOTSUPP;
1416         }
1417
1418         mutex_lock(&priv->mutex);
1419         ath_dbg(common, CONFIG, "Set HW Key\n");
1420         ath9k_htc_ps_wakeup(priv);
1421
1422         switch (cmd) {
1423         case SET_KEY:
1424                 ret = ath_key_config(common, vif, sta, key);
1425                 if (ret >= 0) {
1426                         key->hw_key_idx = ret;
1427                         /* push IV and Michael MIC generation to stack */
1428                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1429                         if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1430                                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1431                         if (priv->ah->sw_mgmt_crypto &&
1432                             key->cipher == WLAN_CIPHER_SUITE_CCMP)
1433                                 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
1434                         ret = 0;
1435                 }
1436                 break;
1437         case DISABLE_KEY:
1438                 ath_key_delete(common, key);
1439                 break;
1440         default:
1441                 ret = -EINVAL;
1442         }
1443
1444         ath9k_htc_ps_restore(priv);
1445         mutex_unlock(&priv->mutex);
1446
1447         return ret;
1448 }
1449
1450 static void ath9k_htc_set_bssid(struct ath9k_htc_priv *priv)
1451 {
1452         struct ath_common *common = ath9k_hw_common(priv->ah);
1453
1454         ath9k_hw_write_associd(priv->ah);
1455         ath_dbg(common, CONFIG, "BSSID: %pM aid: 0x%x\n",
1456                 common->curbssid, common->curaid);
1457 }
1458
1459 static void ath9k_htc_bss_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
1460 {
1461         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
1462         struct ath_common *common = ath9k_hw_common(priv->ah);
1463         struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1464
1465         if ((vif->type == NL80211_IFTYPE_STATION) && bss_conf->assoc) {
1466                 common->curaid = bss_conf->aid;
1467                 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1468         }
1469 }
1470
1471 static void ath9k_htc_choose_set_bssid(struct ath9k_htc_priv *priv)
1472 {
1473         if (priv->num_sta_assoc_vif == 1) {
1474                 ieee80211_iterate_active_interfaces_atomic(priv->hw,
1475                                                            ath9k_htc_bss_iter, priv);
1476                 ath9k_htc_set_bssid(priv);
1477         }
1478 }
1479
1480 static void ath9k_htc_bss_info_changed(struct ieee80211_hw *hw,
1481                                        struct ieee80211_vif *vif,
1482                                        struct ieee80211_bss_conf *bss_conf,
1483                                        u32 changed)
1484 {
1485         struct ath9k_htc_priv *priv = hw->priv;
1486         struct ath_hw *ah = priv->ah;
1487         struct ath_common *common = ath9k_hw_common(ah);
1488
1489         mutex_lock(&priv->mutex);
1490         ath9k_htc_ps_wakeup(priv);
1491
1492         if (changed & BSS_CHANGED_ASSOC) {
1493                 ath_dbg(common, CONFIG, "BSS Changed ASSOC %d\n",
1494                         bss_conf->assoc);
1495
1496                 bss_conf->assoc ?
1497                         priv->num_sta_assoc_vif++ : priv->num_sta_assoc_vif--;
1498
1499                 if (priv->ah->opmode == NL80211_IFTYPE_STATION) {
1500                         if (bss_conf->assoc && (priv->num_sta_assoc_vif == 1))
1501                                 ath9k_htc_start_ani(priv);
1502                         else if (priv->num_sta_assoc_vif == 0)
1503                                 ath9k_htc_stop_ani(priv);
1504                 }
1505         }
1506
1507         if (changed & BSS_CHANGED_BSSID) {
1508                 if (priv->ah->opmode == NL80211_IFTYPE_ADHOC) {
1509                         common->curaid = bss_conf->aid;
1510                         memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1511                         ath9k_htc_set_bssid(priv);
1512                 } else if (priv->ah->opmode == NL80211_IFTYPE_STATION) {
1513                         ath9k_htc_choose_set_bssid(priv);
1514                 }
1515         }
1516
1517         if ((changed & BSS_CHANGED_BEACON_ENABLED) && bss_conf->enable_beacon) {
1518                 ath_dbg(common, CONFIG, "Beacon enabled for BSS: %pM\n",
1519                         bss_conf->bssid);
1520                 ath9k_htc_set_tsfadjust(priv, vif);
1521                 priv->op_flags |= OP_ENABLE_BEACON;
1522                 ath9k_htc_beacon_config(priv, vif);
1523         }
1524
1525         if ((changed & BSS_CHANGED_BEACON_ENABLED) && !bss_conf->enable_beacon) {
1526                 /*
1527                  * Disable SWBA interrupt only if there are no
1528                  * AP/IBSS interfaces.
1529                  */
1530                 if ((priv->num_ap_vif <= 1) || priv->num_ibss_vif) {
1531                         ath_dbg(common, CONFIG,
1532                                 "Beacon disabled for BSS: %pM\n",
1533                                 bss_conf->bssid);
1534                         priv->op_flags &= ~OP_ENABLE_BEACON;
1535                         ath9k_htc_beacon_config(priv, vif);
1536                 }
1537         }
1538
1539         if (changed & BSS_CHANGED_BEACON_INT) {
1540                 /*
1541                  * Reset the HW TSF for the first AP interface.
1542                  */
1543                 if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
1544                     (priv->nvifs == 1) &&
1545                     (priv->num_ap_vif == 1) &&
1546                     (vif->type == NL80211_IFTYPE_AP)) {
1547                         priv->op_flags |= OP_TSF_RESET;
1548                 }
1549                 ath_dbg(common, CONFIG,
1550                         "Beacon interval changed for BSS: %pM\n",
1551                         bss_conf->bssid);
1552                 ath9k_htc_beacon_config(priv, vif);
1553         }
1554
1555         if (changed & BSS_CHANGED_ERP_SLOT) {
1556                 if (bss_conf->use_short_slot)
1557                         ah->slottime = 9;
1558                 else
1559                         ah->slottime = 20;
1560
1561                 ath9k_hw_init_global_settings(ah);
1562         }
1563
1564         if (changed & BSS_CHANGED_HT)
1565                 ath9k_htc_update_rate(priv, vif, bss_conf);
1566
1567         ath9k_htc_ps_restore(priv);
1568         mutex_unlock(&priv->mutex);
1569 }
1570
1571 static u64 ath9k_htc_get_tsf(struct ieee80211_hw *hw,
1572                              struct ieee80211_vif *vif)
1573 {
1574         struct ath9k_htc_priv *priv = hw->priv;
1575         u64 tsf;
1576
1577         mutex_lock(&priv->mutex);
1578         ath9k_htc_ps_wakeup(priv);
1579         tsf = ath9k_hw_gettsf64(priv->ah);
1580         ath9k_htc_ps_restore(priv);
1581         mutex_unlock(&priv->mutex);
1582
1583         return tsf;
1584 }
1585
1586 static void ath9k_htc_set_tsf(struct ieee80211_hw *hw,
1587                               struct ieee80211_vif *vif, u64 tsf)
1588 {
1589         struct ath9k_htc_priv *priv = hw->priv;
1590
1591         mutex_lock(&priv->mutex);
1592         ath9k_htc_ps_wakeup(priv);
1593         ath9k_hw_settsf64(priv->ah, tsf);
1594         ath9k_htc_ps_restore(priv);
1595         mutex_unlock(&priv->mutex);
1596 }
1597
1598 static void ath9k_htc_reset_tsf(struct ieee80211_hw *hw,
1599                                 struct ieee80211_vif *vif)
1600 {
1601         struct ath9k_htc_priv *priv = hw->priv;
1602
1603         mutex_lock(&priv->mutex);
1604         ath9k_htc_ps_wakeup(priv);
1605         ath9k_hw_reset_tsf(priv->ah);
1606         ath9k_htc_ps_restore(priv);
1607         mutex_unlock(&priv->mutex);
1608 }
1609
1610 static int ath9k_htc_ampdu_action(struct ieee80211_hw *hw,
1611                                   struct ieee80211_vif *vif,
1612                                   enum ieee80211_ampdu_mlme_action action,
1613                                   struct ieee80211_sta *sta,
1614                                   u16 tid, u16 *ssn, u8 buf_size)
1615 {
1616         struct ath9k_htc_priv *priv = hw->priv;
1617         struct ath9k_htc_sta *ista;
1618         int ret = 0;
1619
1620         mutex_lock(&priv->mutex);
1621         ath9k_htc_ps_wakeup(priv);
1622
1623         switch (action) {
1624         case IEEE80211_AMPDU_RX_START:
1625                 break;
1626         case IEEE80211_AMPDU_RX_STOP:
1627                 break;
1628         case IEEE80211_AMPDU_TX_START:
1629                 ret = ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1630                 if (!ret)
1631                         ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1632                 break;
1633         case IEEE80211_AMPDU_TX_STOP:
1634                 ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1635                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1636                 break;
1637         case IEEE80211_AMPDU_TX_OPERATIONAL:
1638                 ista = (struct ath9k_htc_sta *) sta->drv_priv;
1639                 spin_lock_bh(&priv->tx.tx_lock);
1640                 ista->tid_state[tid] = AGGR_OPERATIONAL;
1641                 spin_unlock_bh(&priv->tx.tx_lock);
1642                 break;
1643         default:
1644                 ath_err(ath9k_hw_common(priv->ah), "Unknown AMPDU action\n");
1645         }
1646
1647         ath9k_htc_ps_restore(priv);
1648         mutex_unlock(&priv->mutex);
1649
1650         return ret;
1651 }
1652
1653 static void ath9k_htc_sw_scan_start(struct ieee80211_hw *hw)
1654 {
1655         struct ath9k_htc_priv *priv = hw->priv;
1656
1657         mutex_lock(&priv->mutex);
1658         spin_lock_bh(&priv->beacon_lock);
1659         priv->op_flags |= OP_SCANNING;
1660         spin_unlock_bh(&priv->beacon_lock);
1661         cancel_work_sync(&priv->ps_work);
1662         ath9k_htc_stop_ani(priv);
1663         mutex_unlock(&priv->mutex);
1664 }
1665
1666 static void ath9k_htc_sw_scan_complete(struct ieee80211_hw *hw)
1667 {
1668         struct ath9k_htc_priv *priv = hw->priv;
1669
1670         mutex_lock(&priv->mutex);
1671         spin_lock_bh(&priv->beacon_lock);
1672         priv->op_flags &= ~OP_SCANNING;
1673         spin_unlock_bh(&priv->beacon_lock);
1674         ath9k_htc_ps_wakeup(priv);
1675         ath9k_htc_vif_reconfig(priv);
1676         ath9k_htc_ps_restore(priv);
1677         mutex_unlock(&priv->mutex);
1678 }
1679
1680 static int ath9k_htc_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1681 {
1682         return 0;
1683 }
1684
1685 static void ath9k_htc_set_coverage_class(struct ieee80211_hw *hw,
1686                                          u8 coverage_class)
1687 {
1688         struct ath9k_htc_priv *priv = hw->priv;
1689
1690         mutex_lock(&priv->mutex);
1691         ath9k_htc_ps_wakeup(priv);
1692         priv->ah->coverage_class = coverage_class;
1693         ath9k_hw_init_global_settings(priv->ah);
1694         ath9k_htc_ps_restore(priv);
1695         mutex_unlock(&priv->mutex);
1696 }
1697
1698 /*
1699  * Currently, this is used only for selecting the minimum rate
1700  * for management frames, rate selection for data frames remain
1701  * unaffected.
1702  */
1703 static int ath9k_htc_set_bitrate_mask(struct ieee80211_hw *hw,
1704                                       struct ieee80211_vif *vif,
1705                                       const struct cfg80211_bitrate_mask *mask)
1706 {
1707         struct ath9k_htc_priv *priv = hw->priv;
1708         struct ath_common *common = ath9k_hw_common(priv->ah);
1709         struct ath9k_htc_target_rate_mask tmask;
1710         struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1711         int ret = 0;
1712         u8 cmd_rsp;
1713
1714         memset(&tmask, 0, sizeof(struct ath9k_htc_target_rate_mask));
1715
1716         tmask.vif_index = avp->index;
1717         tmask.band = IEEE80211_BAND_2GHZ;
1718         tmask.mask = cpu_to_be32(mask->control[IEEE80211_BAND_2GHZ].legacy);
1719
1720         WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
1721         if (ret) {
1722                 ath_err(common,
1723                         "Unable to set 2G rate mask for "
1724                         "interface at idx: %d\n", avp->index);
1725                 goto out;
1726         }
1727
1728         tmask.band = IEEE80211_BAND_5GHZ;
1729         tmask.mask = cpu_to_be32(mask->control[IEEE80211_BAND_5GHZ].legacy);
1730
1731         WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
1732         if (ret) {
1733                 ath_err(common,
1734                         "Unable to set 5G rate mask for "
1735                         "interface at idx: %d\n", avp->index);
1736                 goto out;
1737         }
1738
1739         ath_dbg(common, CONFIG, "Set bitrate masks: 0x%x, 0x%x\n",
1740                 mask->control[IEEE80211_BAND_2GHZ].legacy,
1741                 mask->control[IEEE80211_BAND_5GHZ].legacy);
1742 out:
1743         return ret;
1744 }
1745
1746
1747 static int ath9k_htc_get_stats(struct ieee80211_hw *hw,
1748                                struct ieee80211_low_level_stats *stats)
1749 {
1750         struct ath9k_htc_priv *priv = hw->priv;
1751         struct ath_hw *ah = priv->ah;
1752         struct ath9k_mib_stats *mib_stats = &ah->ah_mibStats;
1753
1754         stats->dot11ACKFailureCount = mib_stats->ackrcv_bad;
1755         stats->dot11RTSFailureCount = mib_stats->rts_bad;
1756         stats->dot11FCSErrorCount = mib_stats->fcs_bad;
1757         stats->dot11RTSSuccessCount = mib_stats->rts_good;
1758
1759         return 0;
1760 }
1761
1762 struct ieee80211_ops ath9k_htc_ops = {
1763         .tx                 = ath9k_htc_tx,
1764         .start              = ath9k_htc_start,
1765         .stop               = ath9k_htc_stop,
1766         .add_interface      = ath9k_htc_add_interface,
1767         .remove_interface   = ath9k_htc_remove_interface,
1768         .config             = ath9k_htc_config,
1769         .configure_filter   = ath9k_htc_configure_filter,
1770         .sta_add            = ath9k_htc_sta_add,
1771         .sta_remove         = ath9k_htc_sta_remove,
1772         .conf_tx            = ath9k_htc_conf_tx,
1773         .bss_info_changed   = ath9k_htc_bss_info_changed,
1774         .set_key            = ath9k_htc_set_key,
1775         .get_tsf            = ath9k_htc_get_tsf,
1776         .set_tsf            = ath9k_htc_set_tsf,
1777         .reset_tsf          = ath9k_htc_reset_tsf,
1778         .ampdu_action       = ath9k_htc_ampdu_action,
1779         .sw_scan_start      = ath9k_htc_sw_scan_start,
1780         .sw_scan_complete   = ath9k_htc_sw_scan_complete,
1781         .set_rts_threshold  = ath9k_htc_set_rts_threshold,
1782         .rfkill_poll        = ath9k_htc_rfkill_poll_state,
1783         .set_coverage_class = ath9k_htc_set_coverage_class,
1784         .set_bitrate_mask   = ath9k_htc_set_bitrate_mask,
1785         .get_stats          = ath9k_htc_get_stats,
1786 };