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