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