Merge branch 'for-john' of git://git.kernel.org/pub/scm/linux/kernel/git/jberg/mac802...
[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         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                 ista->index = sta_idx;
493                 tsta.is_vif_sta = 0;
494                 maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
495                                  sta->ht_cap.ampdu_factor);
496                 tsta.maxampdu = cpu_to_be16(maxampdu);
497         } else {
498                 memcpy(&tsta.macaddr, vif->addr, ETH_ALEN);
499                 tsta.is_vif_sta = 1;
500                 tsta.maxampdu = cpu_to_be16(0xffff);
501         }
502
503         tsta.sta_index = sta_idx;
504         tsta.vif_index = avp->index;
505
506         WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
507         if (ret) {
508                 if (sta)
509                         ath_err(common,
510                                 "Unable to add station entry for: %pM\n",
511                                 sta->addr);
512                 return ret;
513         }
514
515         if (sta) {
516                 ath_dbg(common, CONFIG,
517                         "Added a station entry for: %pM (idx: %d)\n",
518                         sta->addr, tsta.sta_index);
519         } else {
520                 ath_dbg(common, CONFIG,
521                         "Added a station entry for VIF %d (idx: %d)\n",
522                         avp->index, tsta.sta_index);
523         }
524
525         priv->sta_slot |= (1 << sta_idx);
526         priv->nstations++;
527         if (!sta)
528                 priv->vif_sta_pos[avp->index] = sta_idx;
529
530         return 0;
531 }
532
533 static int ath9k_htc_remove_station(struct ath9k_htc_priv *priv,
534                                     struct ieee80211_vif *vif,
535                                     struct ieee80211_sta *sta)
536 {
537         struct ath_common *common = ath9k_hw_common(priv->ah);
538         struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
539         struct ath9k_htc_sta *ista;
540         int ret;
541         u8 cmd_rsp, sta_idx;
542
543         if (sta) {
544                 ista = (struct ath9k_htc_sta *) sta->drv_priv;
545                 sta_idx = ista->index;
546         } else {
547                 sta_idx = priv->vif_sta_pos[avp->index];
548         }
549
550         WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
551         if (ret) {
552                 if (sta)
553                         ath_err(common,
554                                 "Unable to remove station entry for: %pM\n",
555                                 sta->addr);
556                 return ret;
557         }
558
559         if (sta) {
560                 ath_dbg(common, CONFIG,
561                         "Removed a station entry for: %pM (idx: %d)\n",
562                         sta->addr, sta_idx);
563         } else {
564                 ath_dbg(common, CONFIG,
565                         "Removed a station entry for VIF %d (idx: %d)\n",
566                         avp->index, sta_idx);
567         }
568
569         priv->sta_slot &= ~(1 << sta_idx);
570         priv->nstations--;
571
572         return 0;
573 }
574
575 int ath9k_htc_update_cap_target(struct ath9k_htc_priv *priv,
576                                 u8 enable_coex)
577 {
578         struct ath9k_htc_cap_target tcap;
579         int ret;
580         u8 cmd_rsp;
581
582         memset(&tcap, 0, sizeof(struct ath9k_htc_cap_target));
583
584         tcap.ampdu_limit = cpu_to_be32(0xffff);
585         tcap.ampdu_subframes = 0xff;
586         tcap.enable_coex = enable_coex;
587         tcap.tx_chainmask = priv->ah->caps.tx_chainmask;
588
589         WMI_CMD_BUF(WMI_TARGET_IC_UPDATE_CMDID, &tcap);
590
591         return ret;
592 }
593
594 static void ath9k_htc_setup_rate(struct ath9k_htc_priv *priv,
595                                  struct ieee80211_sta *sta,
596                                  struct ath9k_htc_target_rate *trate)
597 {
598         struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
599         struct ieee80211_supported_band *sband;
600         u32 caps = 0;
601         int i, j;
602
603         sband = priv->hw->wiphy->bands[priv->hw->conf.channel->band];
604
605         for (i = 0, j = 0; i < sband->n_bitrates; i++) {
606                 if (sta->supp_rates[sband->band] & BIT(i)) {
607                         trate->rates.legacy_rates.rs_rates[j]
608                                 = (sband->bitrates[i].bitrate * 2) / 10;
609                         j++;
610                 }
611         }
612         trate->rates.legacy_rates.rs_nrates = j;
613
614         if (sta->ht_cap.ht_supported) {
615                 for (i = 0, j = 0; i < 77; i++) {
616                         if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
617                                 trate->rates.ht_rates.rs_rates[j++] = i;
618                         if (j == ATH_HTC_RATE_MAX)
619                                 break;
620                 }
621                 trate->rates.ht_rates.rs_nrates = j;
622
623                 caps = WLAN_RC_HT_FLAG;
624                 if (sta->ht_cap.mcs.rx_mask[1])
625                         caps |= WLAN_RC_DS_FLAG;
626                 if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
627                      (conf_is_ht40(&priv->hw->conf)))
628                         caps |= WLAN_RC_40_FLAG;
629                 if (conf_is_ht40(&priv->hw->conf) &&
630                     (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40))
631                         caps |= WLAN_RC_SGI_FLAG;
632                 else if (conf_is_ht20(&priv->hw->conf) &&
633                          (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20))
634                         caps |= WLAN_RC_SGI_FLAG;
635         }
636
637         trate->sta_index = ista->index;
638         trate->isnew = 1;
639         trate->capflags = cpu_to_be32(caps);
640 }
641
642 static int ath9k_htc_send_rate_cmd(struct ath9k_htc_priv *priv,
643                                     struct ath9k_htc_target_rate *trate)
644 {
645         struct ath_common *common = ath9k_hw_common(priv->ah);
646         int ret;
647         u8 cmd_rsp;
648
649         WMI_CMD_BUF(WMI_RC_RATE_UPDATE_CMDID, trate);
650         if (ret) {
651                 ath_err(common,
652                         "Unable to initialize Rate information on target\n");
653         }
654
655         return ret;
656 }
657
658 static void ath9k_htc_init_rate(struct ath9k_htc_priv *priv,
659                                 struct ieee80211_sta *sta)
660 {
661         struct ath_common *common = ath9k_hw_common(priv->ah);
662         struct ath9k_htc_target_rate trate;
663         int ret;
664
665         memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
666         ath9k_htc_setup_rate(priv, sta, &trate);
667         ret = ath9k_htc_send_rate_cmd(priv, &trate);
668         if (!ret)
669                 ath_dbg(common, CONFIG,
670                         "Updated target sta: %pM, rate caps: 0x%X\n",
671                         sta->addr, be32_to_cpu(trate.capflags));
672 }
673
674 static void ath9k_htc_update_rate(struct ath9k_htc_priv *priv,
675                                   struct ieee80211_vif *vif,
676                                   struct ieee80211_bss_conf *bss_conf)
677 {
678         struct ath_common *common = ath9k_hw_common(priv->ah);
679         struct ath9k_htc_target_rate trate;
680         struct ieee80211_sta *sta;
681         int ret;
682
683         memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
684
685         rcu_read_lock();
686         sta = ieee80211_find_sta(vif, bss_conf->bssid);
687         if (!sta) {
688                 rcu_read_unlock();
689                 return;
690         }
691         ath9k_htc_setup_rate(priv, sta, &trate);
692         rcu_read_unlock();
693
694         ret = ath9k_htc_send_rate_cmd(priv, &trate);
695         if (!ret)
696                 ath_dbg(common, CONFIG,
697                         "Updated target sta: %pM, rate caps: 0x%X\n",
698                         bss_conf->bssid, be32_to_cpu(trate.capflags));
699 }
700
701 static int ath9k_htc_tx_aggr_oper(struct ath9k_htc_priv *priv,
702                                   struct ieee80211_vif *vif,
703                                   struct ieee80211_sta *sta,
704                                   enum ieee80211_ampdu_mlme_action action,
705                                   u16 tid)
706 {
707         struct ath_common *common = ath9k_hw_common(priv->ah);
708         struct ath9k_htc_target_aggr aggr;
709         struct ath9k_htc_sta *ista;
710         int ret = 0;
711         u8 cmd_rsp;
712
713         if (tid >= ATH9K_HTC_MAX_TID)
714                 return -EINVAL;
715
716         memset(&aggr, 0, sizeof(struct ath9k_htc_target_aggr));
717         ista = (struct ath9k_htc_sta *) sta->drv_priv;
718
719         aggr.sta_index = ista->index;
720         aggr.tidno = tid & 0xf;
721         aggr.aggr_enable = (action == IEEE80211_AMPDU_TX_START) ? true : false;
722
723         WMI_CMD_BUF(WMI_TX_AGGR_ENABLE_CMDID, &aggr);
724         if (ret)
725                 ath_dbg(common, CONFIG,
726                         "Unable to %s TX aggregation for (%pM, %d)\n",
727                         (aggr.aggr_enable) ? "start" : "stop", sta->addr, tid);
728         else
729                 ath_dbg(common, CONFIG,
730                         "%s TX aggregation for (%pM, %d)\n",
731                         (aggr.aggr_enable) ? "Starting" : "Stopping",
732                         sta->addr, tid);
733
734         spin_lock_bh(&priv->tx.tx_lock);
735         ista->tid_state[tid] = (aggr.aggr_enable && !ret) ? AGGR_START : AGGR_STOP;
736         spin_unlock_bh(&priv->tx.tx_lock);
737
738         return ret;
739 }
740
741 /*******/
742 /* ANI */
743 /*******/
744
745 void ath9k_htc_start_ani(struct ath9k_htc_priv *priv)
746 {
747         struct ath_common *common = ath9k_hw_common(priv->ah);
748         unsigned long timestamp = jiffies_to_msecs(jiffies);
749
750         common->ani.longcal_timer = timestamp;
751         common->ani.shortcal_timer = timestamp;
752         common->ani.checkani_timer = timestamp;
753
754         set_bit(OP_ANI_RUNNING, &priv->op_flags);
755
756         ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
757                                      msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
758 }
759
760 void ath9k_htc_stop_ani(struct ath9k_htc_priv *priv)
761 {
762         cancel_delayed_work_sync(&priv->ani_work);
763         clear_bit(OP_ANI_RUNNING, &priv->op_flags);
764 }
765
766 void ath9k_htc_ani_work(struct work_struct *work)
767 {
768         struct ath9k_htc_priv *priv =
769                 container_of(work, struct ath9k_htc_priv, ani_work.work);
770         struct ath_hw *ah = priv->ah;
771         struct ath_common *common = ath9k_hw_common(ah);
772         bool longcal = false;
773         bool shortcal = false;
774         bool aniflag = false;
775         unsigned int timestamp = jiffies_to_msecs(jiffies);
776         u32 cal_interval, short_cal_interval;
777
778         short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
779                 ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
780
781         /* Only calibrate if awake */
782         if (ah->power_mode != ATH9K_PM_AWAKE)
783                 goto set_timer;
784
785         /* Long calibration runs independently of short calibration. */
786         if ((timestamp - common->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
787                 longcal = true;
788                 ath_dbg(common, ANI, "longcal @%lu\n", jiffies);
789                 common->ani.longcal_timer = timestamp;
790         }
791
792         /* Short calibration applies only while caldone is false */
793         if (!common->ani.caldone) {
794                 if ((timestamp - common->ani.shortcal_timer) >=
795                     short_cal_interval) {
796                         shortcal = true;
797                         ath_dbg(common, ANI, "shortcal @%lu\n", jiffies);
798                         common->ani.shortcal_timer = timestamp;
799                         common->ani.resetcal_timer = timestamp;
800                 }
801         } else {
802                 if ((timestamp - common->ani.resetcal_timer) >=
803                     ATH_RESTART_CALINTERVAL) {
804                         common->ani.caldone = ath9k_hw_reset_calvalid(ah);
805                         if (common->ani.caldone)
806                                 common->ani.resetcal_timer = timestamp;
807                 }
808         }
809
810         /* Verify whether we must check ANI */
811         if (ah->config.enable_ani &&
812             (timestamp - common->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
813                 aniflag = true;
814                 common->ani.checkani_timer = timestamp;
815         }
816
817         /* Skip all processing if there's nothing to do. */
818         if (longcal || shortcal || aniflag) {
819
820                 ath9k_htc_ps_wakeup(priv);
821
822                 /* Call ANI routine if necessary */
823                 if (aniflag)
824                         ath9k_hw_ani_monitor(ah, ah->curchan);
825
826                 /* Perform calibration if necessary */
827                 if (longcal || shortcal)
828                         common->ani.caldone =
829                                 ath9k_hw_calibrate(ah, ah->curchan,
830                                                    ah->rxchainmask, longcal);
831
832                 ath9k_htc_ps_restore(priv);
833         }
834
835 set_timer:
836         /*
837         * Set timer interval based on previous results.
838         * The interval must be the shortest necessary to satisfy ANI,
839         * short calibration and long calibration.
840         */
841         cal_interval = ATH_LONG_CALINTERVAL;
842         if (ah->config.enable_ani)
843                 cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
844         if (!common->ani.caldone)
845                 cal_interval = min(cal_interval, (u32)short_cal_interval);
846
847         ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
848                                      msecs_to_jiffies(cal_interval));
849 }
850
851 /**********************/
852 /* mac80211 Callbacks */
853 /**********************/
854
855 static void ath9k_htc_tx(struct ieee80211_hw *hw,
856                          struct ieee80211_tx_control *control,
857                          struct sk_buff *skb)
858 {
859         struct ieee80211_hdr *hdr;
860         struct ath9k_htc_priv *priv = hw->priv;
861         struct ath_common *common = ath9k_hw_common(priv->ah);
862         int padpos, padsize, ret, slot;
863
864         hdr = (struct ieee80211_hdr *) skb->data;
865
866         /* Add the padding after the header if this is not already done */
867         padpos = ath9k_cmn_padpos(hdr->frame_control);
868         padsize = padpos & 3;
869         if (padsize && skb->len > padpos) {
870                 if (skb_headroom(skb) < padsize) {
871                         ath_dbg(common, XMIT, "No room for padding\n");
872                         goto fail_tx;
873                 }
874                 skb_push(skb, padsize);
875                 memmove(skb->data, skb->data + padsize, padpos);
876         }
877
878         slot = ath9k_htc_tx_get_slot(priv);
879         if (slot < 0) {
880                 ath_dbg(common, XMIT, "No free TX slot\n");
881                 goto fail_tx;
882         }
883
884         ret = ath9k_htc_tx_start(priv, control->sta, skb, slot, false);
885         if (ret != 0) {
886                 ath_dbg(common, XMIT, "Tx failed\n");
887                 goto clear_slot;
888         }
889
890         ath9k_htc_check_stop_queues(priv);
891
892         return;
893
894 clear_slot:
895         ath9k_htc_tx_clear_slot(priv, slot);
896 fail_tx:
897         dev_kfree_skb_any(skb);
898 }
899
900 static int ath9k_htc_start(struct ieee80211_hw *hw)
901 {
902         struct ath9k_htc_priv *priv = hw->priv;
903         struct ath_hw *ah = priv->ah;
904         struct ath_common *common = ath9k_hw_common(ah);
905         struct ieee80211_channel *curchan = hw->conf.channel;
906         struct ath9k_channel *init_channel;
907         int ret = 0;
908         enum htc_phymode mode;
909         __be16 htc_mode;
910         u8 cmd_rsp;
911
912         mutex_lock(&priv->mutex);
913
914         ath_dbg(common, CONFIG,
915                 "Starting driver with initial channel: %d MHz\n",
916                 curchan->center_freq);
917
918         /* Ensure that HW is awake before flushing RX */
919         ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
920         WMI_CMD(WMI_FLUSH_RECV_CMDID);
921
922         /* setup initial channel */
923         init_channel = ath9k_cmn_get_curchannel(hw, ah);
924
925         ret = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
926         if (ret) {
927                 ath_err(common,
928                         "Unable to reset hardware; reset status %d (freq %u MHz)\n",
929                         ret, curchan->center_freq);
930                 mutex_unlock(&priv->mutex);
931                 return ret;
932         }
933
934         ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
935                                &priv->curtxpow);
936
937         mode = ath9k_htc_get_curmode(priv, init_channel);
938         htc_mode = cpu_to_be16(mode);
939         WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
940         WMI_CMD(WMI_ATH_INIT_CMDID);
941         WMI_CMD(WMI_START_RECV_CMDID);
942
943         ath9k_host_rx_init(priv);
944
945         ret = ath9k_htc_update_cap_target(priv, 0);
946         if (ret)
947                 ath_dbg(common, CONFIG,
948                         "Failed to update capability in target\n");
949
950         clear_bit(OP_INVALID, &priv->op_flags);
951         htc_start(priv->htc);
952
953         spin_lock_bh(&priv->tx.tx_lock);
954         priv->tx.flags &= ~ATH9K_HTC_OP_TX_QUEUES_STOP;
955         spin_unlock_bh(&priv->tx.tx_lock);
956
957         ieee80211_wake_queues(hw);
958
959         mod_timer(&priv->tx.cleanup_timer,
960                   jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
961
962         ath9k_htc_start_btcoex(priv);
963
964         mutex_unlock(&priv->mutex);
965
966         return ret;
967 }
968
969 static void ath9k_htc_stop(struct ieee80211_hw *hw)
970 {
971         struct ath9k_htc_priv *priv = hw->priv;
972         struct ath_hw *ah = priv->ah;
973         struct ath_common *common = ath9k_hw_common(ah);
974         int ret __attribute__ ((unused));
975         u8 cmd_rsp;
976
977         mutex_lock(&priv->mutex);
978
979         if (test_bit(OP_INVALID, &priv->op_flags)) {
980                 ath_dbg(common, ANY, "Device not present\n");
981                 mutex_unlock(&priv->mutex);
982                 return;
983         }
984
985         ath9k_htc_ps_wakeup(priv);
986
987         WMI_CMD(WMI_DISABLE_INTR_CMDID);
988         WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
989         WMI_CMD(WMI_STOP_RECV_CMDID);
990
991         tasklet_kill(&priv->rx_tasklet);
992
993         del_timer_sync(&priv->tx.cleanup_timer);
994         ath9k_htc_tx_drain(priv);
995         ath9k_wmi_event_drain(priv);
996
997         mutex_unlock(&priv->mutex);
998
999         /* Cancel all the running timers/work .. */
1000         cancel_work_sync(&priv->fatal_work);
1001         cancel_work_sync(&priv->ps_work);
1002
1003 #ifdef CONFIG_MAC80211_LEDS
1004         cancel_work_sync(&priv->led_work);
1005 #endif
1006         ath9k_htc_stop_ani(priv);
1007
1008         mutex_lock(&priv->mutex);
1009
1010         ath9k_htc_stop_btcoex(priv);
1011
1012         /* Remove a monitor interface if it's present. */
1013         if (priv->ah->is_monitoring)
1014                 ath9k_htc_remove_monitor_interface(priv);
1015
1016         ath9k_hw_phy_disable(ah);
1017         ath9k_hw_disable(ah);
1018         ath9k_htc_ps_restore(priv);
1019         ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);
1020
1021         set_bit(OP_INVALID, &priv->op_flags);
1022
1023         ath_dbg(common, CONFIG, "Driver halt\n");
1024         mutex_unlock(&priv->mutex);
1025 }
1026
1027 static int ath9k_htc_add_interface(struct ieee80211_hw *hw,
1028                                    struct ieee80211_vif *vif)
1029 {
1030         struct ath9k_htc_priv *priv = hw->priv;
1031         struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1032         struct ath_common *common = ath9k_hw_common(priv->ah);
1033         struct ath9k_htc_target_vif hvif;
1034         int ret = 0;
1035         u8 cmd_rsp;
1036
1037         mutex_lock(&priv->mutex);
1038
1039         if (priv->nvifs >= ATH9K_HTC_MAX_VIF) {
1040                 mutex_unlock(&priv->mutex);
1041                 return -ENOBUFS;
1042         }
1043
1044         if (priv->num_ibss_vif ||
1045             (priv->nvifs && vif->type == NL80211_IFTYPE_ADHOC)) {
1046                 ath_err(common, "IBSS coexistence with other modes is not allowed\n");
1047                 mutex_unlock(&priv->mutex);
1048                 return -ENOBUFS;
1049         }
1050
1051         if (((vif->type == NL80211_IFTYPE_AP) ||
1052              (vif->type == NL80211_IFTYPE_ADHOC)) &&
1053             ((priv->num_ap_vif + priv->num_ibss_vif) >= ATH9K_HTC_MAX_BCN_VIF)) {
1054                 ath_err(common, "Max. number of beaconing interfaces reached\n");
1055                 mutex_unlock(&priv->mutex);
1056                 return -ENOBUFS;
1057         }
1058
1059         ath9k_htc_ps_wakeup(priv);
1060         memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1061         memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1062
1063         switch (vif->type) {
1064         case NL80211_IFTYPE_STATION:
1065                 hvif.opmode = HTC_M_STA;
1066                 break;
1067         case NL80211_IFTYPE_ADHOC:
1068                 hvif.opmode = HTC_M_IBSS;
1069                 break;
1070         case NL80211_IFTYPE_AP:
1071                 hvif.opmode = HTC_M_HOSTAP;
1072                 break;
1073         default:
1074                 ath_err(common,
1075                         "Interface type %d not yet supported\n", vif->type);
1076                 ret = -EOPNOTSUPP;
1077                 goto out;
1078         }
1079
1080         /* Index starts from zero on the target */
1081         avp->index = hvif.index = ffz(priv->vif_slot);
1082         hvif.rtsthreshold = cpu_to_be16(2304);
1083         WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
1084         if (ret)
1085                 goto out;
1086
1087         /*
1088          * We need a node in target to tx mgmt frames
1089          * before association.
1090          */
1091         ret = ath9k_htc_add_station(priv, vif, NULL);
1092         if (ret) {
1093                 WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1094                 goto out;
1095         }
1096
1097         ath9k_htc_set_bssid_mask(priv, vif);
1098
1099         priv->vif_slot |= (1 << avp->index);
1100         priv->nvifs++;
1101
1102         INC_VIF(priv, vif->type);
1103
1104         if ((vif->type == NL80211_IFTYPE_AP) ||
1105             (vif->type == NL80211_IFTYPE_ADHOC))
1106                 ath9k_htc_assign_bslot(priv, vif);
1107
1108         ath9k_htc_set_opmode(priv);
1109
1110         if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
1111             !test_bit(OP_ANI_RUNNING, &priv->op_flags)) {
1112                 ath9k_hw_set_tsfadjust(priv->ah, true);
1113                 ath9k_htc_start_ani(priv);
1114         }
1115
1116         ath_dbg(common, CONFIG, "Attach a VIF of type: %d at idx: %d\n",
1117                 vif->type, avp->index);
1118
1119 out:
1120         ath9k_htc_ps_restore(priv);
1121         mutex_unlock(&priv->mutex);
1122
1123         return ret;
1124 }
1125
1126 static void ath9k_htc_remove_interface(struct ieee80211_hw *hw,
1127                                        struct ieee80211_vif *vif)
1128 {
1129         struct ath9k_htc_priv *priv = hw->priv;
1130         struct ath_common *common = ath9k_hw_common(priv->ah);
1131         struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1132         struct ath9k_htc_target_vif hvif;
1133         int ret = 0;
1134         u8 cmd_rsp;
1135
1136         mutex_lock(&priv->mutex);
1137         ath9k_htc_ps_wakeup(priv);
1138
1139         memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1140         memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1141         hvif.index = avp->index;
1142         WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1143         if (ret) {
1144                 ath_err(common, "Unable to remove interface at idx: %d\n",
1145                         avp->index);
1146         }
1147         priv->nvifs--;
1148         priv->vif_slot &= ~(1 << avp->index);
1149
1150         ath9k_htc_remove_station(priv, vif, NULL);
1151
1152         DEC_VIF(priv, vif->type);
1153
1154         if ((vif->type == NL80211_IFTYPE_AP) ||
1155             (vif->type == NL80211_IFTYPE_ADHOC))
1156                 ath9k_htc_remove_bslot(priv, vif);
1157
1158         ath9k_htc_set_opmode(priv);
1159
1160         ath9k_htc_set_bssid_mask(priv, vif);
1161
1162         /*
1163          * Stop ANI only if there are no associated station interfaces.
1164          */
1165         if ((vif->type == NL80211_IFTYPE_AP) && (priv->num_ap_vif == 0)) {
1166                 priv->rearm_ani = false;
1167                 ieee80211_iterate_active_interfaces_atomic(priv->hw,
1168                                                    ath9k_htc_vif_iter, priv);
1169                 if (!priv->rearm_ani)
1170                         ath9k_htc_stop_ani(priv);
1171         }
1172
1173         ath_dbg(common, CONFIG, "Detach Interface at idx: %d\n", avp->index);
1174
1175         ath9k_htc_ps_restore(priv);
1176         mutex_unlock(&priv->mutex);
1177 }
1178
1179 static int ath9k_htc_config(struct ieee80211_hw *hw, u32 changed)
1180 {
1181         struct ath9k_htc_priv *priv = hw->priv;
1182         struct ath_common *common = ath9k_hw_common(priv->ah);
1183         struct ieee80211_conf *conf = &hw->conf;
1184         bool chip_reset = false;
1185         int ret = 0;
1186
1187         mutex_lock(&priv->mutex);
1188         ath9k_htc_ps_wakeup(priv);
1189
1190         if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1191                 mutex_lock(&priv->htc_pm_lock);
1192
1193                 priv->ps_idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1194                 if (priv->ps_idle)
1195                         chip_reset = true;
1196
1197                 mutex_unlock(&priv->htc_pm_lock);
1198         }
1199
1200         /*
1201          * Monitor interface should be added before
1202          * IEEE80211_CONF_CHANGE_CHANNEL is handled.
1203          */
1204         if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1205                 if ((conf->flags & IEEE80211_CONF_MONITOR) &&
1206                     !priv->ah->is_monitoring)
1207                         ath9k_htc_add_monitor_interface(priv);
1208                 else if (priv->ah->is_monitoring)
1209                         ath9k_htc_remove_monitor_interface(priv);
1210         }
1211
1212         if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) || chip_reset) {
1213                 struct ieee80211_channel *curchan = hw->conf.channel;
1214                 int pos = curchan->hw_value;
1215
1216                 ath_dbg(common, CONFIG, "Set channel: %d MHz\n",
1217                         curchan->center_freq);
1218
1219                 ath9k_cmn_update_ichannel(&priv->ah->channels[pos],
1220                                           hw->conf.channel,
1221                                           hw->conf.channel_type);
1222
1223                 if (ath9k_htc_set_channel(priv, hw, &priv->ah->channels[pos]) < 0) {
1224                         ath_err(common, "Unable to set channel\n");
1225                         ret = -EINVAL;
1226                         goto out;
1227                 }
1228
1229         }
1230
1231         if (changed & IEEE80211_CONF_CHANGE_PS) {
1232                 if (conf->flags & IEEE80211_CONF_PS) {
1233                         ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
1234                         priv->ps_enabled = true;
1235                 } else {
1236                         priv->ps_enabled = false;
1237                         cancel_work_sync(&priv->ps_work);
1238                         ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1239                 }
1240         }
1241
1242         if (changed & IEEE80211_CONF_CHANGE_POWER) {
1243                 priv->txpowlimit = 2 * conf->power_level;
1244                 ath9k_cmn_update_txpow(priv->ah, priv->curtxpow,
1245                                        priv->txpowlimit, &priv->curtxpow);
1246         }
1247
1248 out:
1249         ath9k_htc_ps_restore(priv);
1250         mutex_unlock(&priv->mutex);
1251         return ret;
1252 }
1253
1254 #define SUPPORTED_FILTERS                       \
1255         (FIF_PROMISC_IN_BSS |                   \
1256         FIF_ALLMULTI |                          \
1257         FIF_CONTROL |                           \
1258         FIF_PSPOLL |                            \
1259         FIF_OTHER_BSS |                         \
1260         FIF_BCN_PRBRESP_PROMISC |               \
1261         FIF_PROBE_REQ |                         \
1262         FIF_FCSFAIL)
1263
1264 static void ath9k_htc_configure_filter(struct ieee80211_hw *hw,
1265                                        unsigned int changed_flags,
1266                                        unsigned int *total_flags,
1267                                        u64 multicast)
1268 {
1269         struct ath9k_htc_priv *priv = hw->priv;
1270         u32 rfilt;
1271
1272         mutex_lock(&priv->mutex);
1273         changed_flags &= SUPPORTED_FILTERS;
1274         *total_flags &= SUPPORTED_FILTERS;
1275
1276         if (test_bit(OP_INVALID, &priv->op_flags)) {
1277                 ath_dbg(ath9k_hw_common(priv->ah), ANY,
1278                         "Unable to configure filter on invalid state\n");
1279                 mutex_unlock(&priv->mutex);
1280                 return;
1281         }
1282         ath9k_htc_ps_wakeup(priv);
1283
1284         priv->rxfilter = *total_flags;
1285         rfilt = ath9k_htc_calcrxfilter(priv);
1286         ath9k_hw_setrxfilter(priv->ah, rfilt);
1287
1288         ath_dbg(ath9k_hw_common(priv->ah), CONFIG, "Set HW RX filter: 0x%x\n",
1289                 rfilt);
1290
1291         ath9k_htc_ps_restore(priv);
1292         mutex_unlock(&priv->mutex);
1293 }
1294
1295 static int ath9k_htc_sta_add(struct ieee80211_hw *hw,
1296                              struct ieee80211_vif *vif,
1297                              struct ieee80211_sta *sta)
1298 {
1299         struct ath9k_htc_priv *priv = hw->priv;
1300         int ret;
1301
1302         mutex_lock(&priv->mutex);
1303         ath9k_htc_ps_wakeup(priv);
1304         ret = ath9k_htc_add_station(priv, vif, sta);
1305         if (!ret)
1306                 ath9k_htc_init_rate(priv, sta);
1307         ath9k_htc_ps_restore(priv);
1308         mutex_unlock(&priv->mutex);
1309
1310         return ret;
1311 }
1312
1313 static int ath9k_htc_sta_remove(struct ieee80211_hw *hw,
1314                                 struct ieee80211_vif *vif,
1315                                 struct ieee80211_sta *sta)
1316 {
1317         struct ath9k_htc_priv *priv = hw->priv;
1318         struct ath9k_htc_sta *ista;
1319         int ret;
1320
1321         mutex_lock(&priv->mutex);
1322         ath9k_htc_ps_wakeup(priv);
1323         ista = (struct ath9k_htc_sta *) sta->drv_priv;
1324         htc_sta_drain(priv->htc, ista->index);
1325         ret = ath9k_htc_remove_station(priv, vif, sta);
1326         ath9k_htc_ps_restore(priv);
1327         mutex_unlock(&priv->mutex);
1328
1329         return ret;
1330 }
1331
1332 static void ath9k_htc_sta_rc_update(struct ieee80211_hw *hw,
1333                                     struct ieee80211_vif *vif,
1334                                     struct ieee80211_sta *sta, u32 changed)
1335 {
1336         struct ath9k_htc_priv *priv = hw->priv;
1337         struct ath_common *common = ath9k_hw_common(priv->ah);
1338         struct ath9k_htc_target_rate trate;
1339
1340         mutex_lock(&priv->mutex);
1341         ath9k_htc_ps_wakeup(priv);
1342
1343         if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
1344                 memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
1345                 ath9k_htc_setup_rate(priv, sta, &trate);
1346                 if (!ath9k_htc_send_rate_cmd(priv, &trate))
1347                         ath_dbg(common, CONFIG,
1348                                 "Supported rates for sta: %pM updated, rate caps: 0x%X\n",
1349                                 sta->addr, be32_to_cpu(trate.capflags));
1350                 else
1351                         ath_dbg(common, CONFIG,
1352                                 "Unable to update supported rates for sta: %pM\n",
1353                                 sta->addr);
1354         }
1355
1356         ath9k_htc_ps_restore(priv);
1357         mutex_unlock(&priv->mutex);
1358 }
1359
1360 static int ath9k_htc_conf_tx(struct ieee80211_hw *hw,
1361                              struct ieee80211_vif *vif, u16 queue,
1362                              const struct ieee80211_tx_queue_params *params)
1363 {
1364         struct ath9k_htc_priv *priv = hw->priv;
1365         struct ath_common *common = ath9k_hw_common(priv->ah);
1366         struct ath9k_tx_queue_info qi;
1367         int ret = 0, qnum;
1368
1369         if (queue >= WME_NUM_AC)
1370                 return 0;
1371
1372         mutex_lock(&priv->mutex);
1373         ath9k_htc_ps_wakeup(priv);
1374
1375         memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1376
1377         qi.tqi_aifs = params->aifs;
1378         qi.tqi_cwmin = params->cw_min;
1379         qi.tqi_cwmax = params->cw_max;
1380         qi.tqi_burstTime = params->txop * 32;
1381
1382         qnum = get_hw_qnum(queue, priv->hwq_map);
1383
1384         ath_dbg(common, CONFIG,
1385                 "Configure tx [queue/hwq] [%d/%d],  aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1386                 queue, qnum, params->aifs, params->cw_min,
1387                 params->cw_max, params->txop);
1388
1389         ret = ath_htc_txq_update(priv, qnum, &qi);
1390         if (ret) {
1391                 ath_err(common, "TXQ Update failed\n");
1392                 goto out;
1393         }
1394
1395         if ((priv->ah->opmode == NL80211_IFTYPE_ADHOC) &&
1396             (qnum == priv->hwq_map[WME_AC_BE]))
1397                     ath9k_htc_beaconq_config(priv);
1398 out:
1399         ath9k_htc_ps_restore(priv);
1400         mutex_unlock(&priv->mutex);
1401
1402         return ret;
1403 }
1404
1405 static int ath9k_htc_set_key(struct ieee80211_hw *hw,
1406                              enum set_key_cmd cmd,
1407                              struct ieee80211_vif *vif,
1408                              struct ieee80211_sta *sta,
1409                              struct ieee80211_key_conf *key)
1410 {
1411         struct ath9k_htc_priv *priv = hw->priv;
1412         struct ath_common *common = ath9k_hw_common(priv->ah);
1413         int ret = 0;
1414
1415         if (htc_modparam_nohwcrypt)
1416                 return -ENOSPC;
1417
1418         if ((vif->type == NL80211_IFTYPE_ADHOC ||
1419              vif->type == NL80211_IFTYPE_MESH_POINT) &&
1420             (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
1421              key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
1422             !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1423                 /*
1424                  * For now, disable hw crypto for the RSN IBSS group keys. This
1425                  * could be optimized in the future to use a modified key cache
1426                  * design to support per-STA RX GTK, but until that gets
1427                  * implemented, use of software crypto for group addressed
1428                  * frames is a acceptable to allow RSN IBSS to be used.
1429                  */
1430                 return -EOPNOTSUPP;
1431         }
1432
1433         mutex_lock(&priv->mutex);
1434         ath_dbg(common, CONFIG, "Set HW Key\n");
1435         ath9k_htc_ps_wakeup(priv);
1436
1437         switch (cmd) {
1438         case SET_KEY:
1439                 ret = ath_key_config(common, vif, sta, key);
1440                 if (ret >= 0) {
1441                         key->hw_key_idx = ret;
1442                         /* push IV and Michael MIC generation to stack */
1443                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1444                         if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1445                                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1446                         if (priv->ah->sw_mgmt_crypto &&
1447                             key->cipher == WLAN_CIPHER_SUITE_CCMP)
1448                                 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
1449                         ret = 0;
1450                 }
1451                 break;
1452         case DISABLE_KEY:
1453                 ath_key_delete(common, key);
1454                 break;
1455         default:
1456                 ret = -EINVAL;
1457         }
1458
1459         ath9k_htc_ps_restore(priv);
1460         mutex_unlock(&priv->mutex);
1461
1462         return ret;
1463 }
1464
1465 static void ath9k_htc_set_bssid(struct ath9k_htc_priv *priv)
1466 {
1467         struct ath_common *common = ath9k_hw_common(priv->ah);
1468
1469         ath9k_hw_write_associd(priv->ah);
1470         ath_dbg(common, CONFIG, "BSSID: %pM aid: 0x%x\n",
1471                 common->curbssid, common->curaid);
1472 }
1473
1474 static void ath9k_htc_bss_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
1475 {
1476         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
1477         struct ath_common *common = ath9k_hw_common(priv->ah);
1478         struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1479
1480         if ((vif->type == NL80211_IFTYPE_STATION) && bss_conf->assoc) {
1481                 common->curaid = bss_conf->aid;
1482                 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1483         }
1484 }
1485
1486 static void ath9k_htc_choose_set_bssid(struct ath9k_htc_priv *priv)
1487 {
1488         if (priv->num_sta_assoc_vif == 1) {
1489                 ieee80211_iterate_active_interfaces_atomic(priv->hw,
1490                                                            ath9k_htc_bss_iter, priv);
1491                 ath9k_htc_set_bssid(priv);
1492         }
1493 }
1494
1495 static void ath9k_htc_bss_info_changed(struct ieee80211_hw *hw,
1496                                        struct ieee80211_vif *vif,
1497                                        struct ieee80211_bss_conf *bss_conf,
1498                                        u32 changed)
1499 {
1500         struct ath9k_htc_priv *priv = hw->priv;
1501         struct ath_hw *ah = priv->ah;
1502         struct ath_common *common = ath9k_hw_common(ah);
1503
1504         mutex_lock(&priv->mutex);
1505         ath9k_htc_ps_wakeup(priv);
1506
1507         if (changed & BSS_CHANGED_ASSOC) {
1508                 ath_dbg(common, CONFIG, "BSS Changed ASSOC %d\n",
1509                         bss_conf->assoc);
1510
1511                 bss_conf->assoc ?
1512                         priv->num_sta_assoc_vif++ : priv->num_sta_assoc_vif--;
1513
1514                 if (priv->ah->opmode == NL80211_IFTYPE_STATION) {
1515                         ath9k_htc_choose_set_bssid(priv);
1516                         if (bss_conf->assoc && (priv->num_sta_assoc_vif == 1))
1517                                 ath9k_htc_start_ani(priv);
1518                         else if (priv->num_sta_assoc_vif == 0)
1519                                 ath9k_htc_stop_ani(priv);
1520                 }
1521         }
1522
1523         if (changed & BSS_CHANGED_IBSS) {
1524                 if (priv->ah->opmode == NL80211_IFTYPE_ADHOC) {
1525                         common->curaid = bss_conf->aid;
1526                         memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1527                         ath9k_htc_set_bssid(priv);
1528                 }
1529         }
1530
1531         if ((changed & BSS_CHANGED_BEACON_ENABLED) && bss_conf->enable_beacon) {
1532                 ath_dbg(common, CONFIG, "Beacon enabled for BSS: %pM\n",
1533                         bss_conf->bssid);
1534                 ath9k_htc_set_tsfadjust(priv, vif);
1535                 set_bit(OP_ENABLE_BEACON, &priv->op_flags);
1536                 ath9k_htc_beacon_config(priv, vif);
1537         }
1538
1539         if ((changed & BSS_CHANGED_BEACON_ENABLED) && !bss_conf->enable_beacon) {
1540                 /*
1541                  * Disable SWBA interrupt only if there are no
1542                  * AP/IBSS interfaces.
1543                  */
1544                 if ((priv->num_ap_vif <= 1) || priv->num_ibss_vif) {
1545                         ath_dbg(common, CONFIG,
1546                                 "Beacon disabled for BSS: %pM\n",
1547                                 bss_conf->bssid);
1548                         clear_bit(OP_ENABLE_BEACON, &priv->op_flags);
1549                         ath9k_htc_beacon_config(priv, vif);
1550                 }
1551         }
1552
1553         if (changed & BSS_CHANGED_BEACON_INT) {
1554                 /*
1555                  * Reset the HW TSF for the first AP interface.
1556                  */
1557                 if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
1558                     (priv->nvifs == 1) &&
1559                     (priv->num_ap_vif == 1) &&
1560                     (vif->type == NL80211_IFTYPE_AP)) {
1561                         set_bit(OP_TSF_RESET, &priv->op_flags);
1562                 }
1563                 ath_dbg(common, CONFIG,
1564                         "Beacon interval changed for BSS: %pM\n",
1565                         bss_conf->bssid);
1566                 ath9k_htc_beacon_config(priv, vif);
1567         }
1568
1569         if (changed & BSS_CHANGED_ERP_SLOT) {
1570                 if (bss_conf->use_short_slot)
1571                         ah->slottime = 9;
1572                 else
1573                         ah->slottime = 20;
1574
1575                 ath9k_hw_init_global_settings(ah);
1576         }
1577
1578         if (changed & BSS_CHANGED_HT)
1579                 ath9k_htc_update_rate(priv, vif, bss_conf);
1580
1581         ath9k_htc_ps_restore(priv);
1582         mutex_unlock(&priv->mutex);
1583 }
1584
1585 static u64 ath9k_htc_get_tsf(struct ieee80211_hw *hw,
1586                              struct ieee80211_vif *vif)
1587 {
1588         struct ath9k_htc_priv *priv = hw->priv;
1589         u64 tsf;
1590
1591         mutex_lock(&priv->mutex);
1592         ath9k_htc_ps_wakeup(priv);
1593         tsf = ath9k_hw_gettsf64(priv->ah);
1594         ath9k_htc_ps_restore(priv);
1595         mutex_unlock(&priv->mutex);
1596
1597         return tsf;
1598 }
1599
1600 static void ath9k_htc_set_tsf(struct ieee80211_hw *hw,
1601                               struct ieee80211_vif *vif, u64 tsf)
1602 {
1603         struct ath9k_htc_priv *priv = hw->priv;
1604
1605         mutex_lock(&priv->mutex);
1606         ath9k_htc_ps_wakeup(priv);
1607         ath9k_hw_settsf64(priv->ah, tsf);
1608         ath9k_htc_ps_restore(priv);
1609         mutex_unlock(&priv->mutex);
1610 }
1611
1612 static void ath9k_htc_reset_tsf(struct ieee80211_hw *hw,
1613                                 struct ieee80211_vif *vif)
1614 {
1615         struct ath9k_htc_priv *priv = hw->priv;
1616
1617         mutex_lock(&priv->mutex);
1618         ath9k_htc_ps_wakeup(priv);
1619         ath9k_hw_reset_tsf(priv->ah);
1620         ath9k_htc_ps_restore(priv);
1621         mutex_unlock(&priv->mutex);
1622 }
1623
1624 static int ath9k_htc_ampdu_action(struct ieee80211_hw *hw,
1625                                   struct ieee80211_vif *vif,
1626                                   enum ieee80211_ampdu_mlme_action action,
1627                                   struct ieee80211_sta *sta,
1628                                   u16 tid, u16 *ssn, u8 buf_size)
1629 {
1630         struct ath9k_htc_priv *priv = hw->priv;
1631         struct ath9k_htc_sta *ista;
1632         int ret = 0;
1633
1634         mutex_lock(&priv->mutex);
1635         ath9k_htc_ps_wakeup(priv);
1636
1637         switch (action) {
1638         case IEEE80211_AMPDU_RX_START:
1639                 break;
1640         case IEEE80211_AMPDU_RX_STOP:
1641                 break;
1642         case IEEE80211_AMPDU_TX_START:
1643                 ret = ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1644                 if (!ret)
1645                         ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1646                 break;
1647         case IEEE80211_AMPDU_TX_STOP:
1648                 ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1649                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1650                 break;
1651         case IEEE80211_AMPDU_TX_OPERATIONAL:
1652                 ista = (struct ath9k_htc_sta *) sta->drv_priv;
1653                 spin_lock_bh(&priv->tx.tx_lock);
1654                 ista->tid_state[tid] = AGGR_OPERATIONAL;
1655                 spin_unlock_bh(&priv->tx.tx_lock);
1656                 break;
1657         default:
1658                 ath_err(ath9k_hw_common(priv->ah), "Unknown AMPDU action\n");
1659         }
1660
1661         ath9k_htc_ps_restore(priv);
1662         mutex_unlock(&priv->mutex);
1663
1664         return ret;
1665 }
1666
1667 static void ath9k_htc_sw_scan_start(struct ieee80211_hw *hw)
1668 {
1669         struct ath9k_htc_priv *priv = hw->priv;
1670
1671         mutex_lock(&priv->mutex);
1672         spin_lock_bh(&priv->beacon_lock);
1673         set_bit(OP_SCANNING, &priv->op_flags);
1674         spin_unlock_bh(&priv->beacon_lock);
1675         cancel_work_sync(&priv->ps_work);
1676         ath9k_htc_stop_ani(priv);
1677         mutex_unlock(&priv->mutex);
1678 }
1679
1680 static void ath9k_htc_sw_scan_complete(struct ieee80211_hw *hw)
1681 {
1682         struct ath9k_htc_priv *priv = hw->priv;
1683
1684         mutex_lock(&priv->mutex);
1685         spin_lock_bh(&priv->beacon_lock);
1686         clear_bit(OP_SCANNING, &priv->op_flags);
1687         spin_unlock_bh(&priv->beacon_lock);
1688         ath9k_htc_ps_wakeup(priv);
1689         ath9k_htc_vif_reconfig(priv);
1690         ath9k_htc_ps_restore(priv);
1691         mutex_unlock(&priv->mutex);
1692 }
1693
1694 static int ath9k_htc_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1695 {
1696         return 0;
1697 }
1698
1699 static void ath9k_htc_set_coverage_class(struct ieee80211_hw *hw,
1700                                          u8 coverage_class)
1701 {
1702         struct ath9k_htc_priv *priv = hw->priv;
1703
1704         mutex_lock(&priv->mutex);
1705         ath9k_htc_ps_wakeup(priv);
1706         priv->ah->coverage_class = coverage_class;
1707         ath9k_hw_init_global_settings(priv->ah);
1708         ath9k_htc_ps_restore(priv);
1709         mutex_unlock(&priv->mutex);
1710 }
1711
1712 /*
1713  * Currently, this is used only for selecting the minimum rate
1714  * for management frames, rate selection for data frames remain
1715  * unaffected.
1716  */
1717 static int ath9k_htc_set_bitrate_mask(struct ieee80211_hw *hw,
1718                                       struct ieee80211_vif *vif,
1719                                       const struct cfg80211_bitrate_mask *mask)
1720 {
1721         struct ath9k_htc_priv *priv = hw->priv;
1722         struct ath_common *common = ath9k_hw_common(priv->ah);
1723         struct ath9k_htc_target_rate_mask tmask;
1724         struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1725         int ret = 0;
1726         u8 cmd_rsp;
1727
1728         memset(&tmask, 0, sizeof(struct ath9k_htc_target_rate_mask));
1729
1730         tmask.vif_index = avp->index;
1731         tmask.band = IEEE80211_BAND_2GHZ;
1732         tmask.mask = cpu_to_be32(mask->control[IEEE80211_BAND_2GHZ].legacy);
1733
1734         WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
1735         if (ret) {
1736                 ath_err(common,
1737                         "Unable to set 2G rate mask for "
1738                         "interface at idx: %d\n", avp->index);
1739                 goto out;
1740         }
1741
1742         tmask.band = IEEE80211_BAND_5GHZ;
1743         tmask.mask = cpu_to_be32(mask->control[IEEE80211_BAND_5GHZ].legacy);
1744
1745         WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
1746         if (ret) {
1747                 ath_err(common,
1748                         "Unable to set 5G rate mask for "
1749                         "interface at idx: %d\n", avp->index);
1750                 goto out;
1751         }
1752
1753         ath_dbg(common, CONFIG, "Set bitrate masks: 0x%x, 0x%x\n",
1754                 mask->control[IEEE80211_BAND_2GHZ].legacy,
1755                 mask->control[IEEE80211_BAND_5GHZ].legacy);
1756 out:
1757         return ret;
1758 }
1759
1760
1761 static int ath9k_htc_get_stats(struct ieee80211_hw *hw,
1762                                struct ieee80211_low_level_stats *stats)
1763 {
1764         struct ath9k_htc_priv *priv = hw->priv;
1765         struct ath_hw *ah = priv->ah;
1766         struct ath9k_mib_stats *mib_stats = &ah->ah_mibStats;
1767
1768         stats->dot11ACKFailureCount = mib_stats->ackrcv_bad;
1769         stats->dot11RTSFailureCount = mib_stats->rts_bad;
1770         stats->dot11FCSErrorCount = mib_stats->fcs_bad;
1771         stats->dot11RTSSuccessCount = mib_stats->rts_good;
1772
1773         return 0;
1774 }
1775
1776 struct ieee80211_ops ath9k_htc_ops = {
1777         .tx                 = ath9k_htc_tx,
1778         .start              = ath9k_htc_start,
1779         .stop               = ath9k_htc_stop,
1780         .add_interface      = ath9k_htc_add_interface,
1781         .remove_interface   = ath9k_htc_remove_interface,
1782         .config             = ath9k_htc_config,
1783         .configure_filter   = ath9k_htc_configure_filter,
1784         .sta_add            = ath9k_htc_sta_add,
1785         .sta_remove         = ath9k_htc_sta_remove,
1786         .conf_tx            = ath9k_htc_conf_tx,
1787         .sta_rc_update      = ath9k_htc_sta_rc_update,
1788         .bss_info_changed   = ath9k_htc_bss_info_changed,
1789         .set_key            = ath9k_htc_set_key,
1790         .get_tsf            = ath9k_htc_get_tsf,
1791         .set_tsf            = ath9k_htc_set_tsf,
1792         .reset_tsf          = ath9k_htc_reset_tsf,
1793         .ampdu_action       = ath9k_htc_ampdu_action,
1794         .sw_scan_start      = ath9k_htc_sw_scan_start,
1795         .sw_scan_complete   = ath9k_htc_sw_scan_complete,
1796         .set_rts_threshold  = ath9k_htc_set_rts_threshold,
1797         .rfkill_poll        = ath9k_htc_rfkill_poll_state,
1798         .set_coverage_class = ath9k_htc_set_coverage_class,
1799         .set_bitrate_mask   = ath9k_htc_set_bitrate_mask,
1800         .get_stats          = ath9k_htc_get_stats,
1801 };