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