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