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