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