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