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