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