fix a kmap leak in virtio_console
[linux/fpc-iii.git] / drivers / net / wireless / ath / ath9k / htc_drv_main.c
blob608d739d13782233db4271f9248c3472601b1a5f
1 /*
2 * Copyright (c) 2010-2011 Atheros Communications Inc.
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.
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.
17 #include "htc.h"
19 /*************/
20 /* Utilities */
21 /*************/
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)
27 if (IS_CHAN_5GHZ(ichan))
28 return HTC_MODE_11NA;
30 return HTC_MODE_11NG;
33 bool ath9k_htc_setpower(struct ath9k_htc_priv *priv,
34 enum ath9k_power_mode mode)
36 bool ret;
38 mutex_lock(&priv->htc_pm_lock);
39 ret = ath9k_hw_setpower(priv->ah, mode);
40 mutex_unlock(&priv->htc_pm_lock);
42 return ret;
45 void ath9k_htc_ps_wakeup(struct ath9k_htc_priv *priv)
47 mutex_lock(&priv->htc_pm_lock);
48 if (++priv->ps_usecount != 1)
49 goto unlock;
50 ath9k_hw_setpower(priv->ah, ATH9K_PM_AWAKE);
52 unlock:
53 mutex_unlock(&priv->htc_pm_lock);
56 void ath9k_htc_ps_restore(struct ath9k_htc_priv *priv)
58 bool reset;
60 mutex_lock(&priv->htc_pm_lock);
61 if (--priv->ps_usecount != 0)
62 goto unlock;
64 if (priv->ps_idle) {
65 ath9k_hw_setrxabort(priv->ah, true);
66 ath9k_hw_stopdmarecv(priv->ah, &reset);
67 ath9k_hw_setpower(priv->ah, ATH9K_PM_FULL_SLEEP);
68 } else if (priv->ps_enabled) {
69 ath9k_hw_setpower(priv->ah, ATH9K_PM_NETWORK_SLEEP);
72 unlock:
73 mutex_unlock(&priv->htc_pm_lock);
76 void ath9k_ps_work(struct work_struct *work)
78 struct ath9k_htc_priv *priv =
79 container_of(work, struct ath9k_htc_priv,
80 ps_work);
81 ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
83 /* The chip wakes up after receiving the first beacon
84 while network sleep is enabled. For the driver to
85 be in sync with the hw, set the chip to awake and
86 only then set it to sleep.
88 ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
91 static void ath9k_htc_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
93 struct ath9k_htc_priv *priv = data;
94 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
96 if ((vif->type == NL80211_IFTYPE_AP ||
97 vif->type == NL80211_IFTYPE_MESH_POINT) &&
98 bss_conf->enable_beacon)
99 priv->reconfig_beacon = true;
101 if (bss_conf->assoc) {
102 priv->rearm_ani = true;
103 priv->reconfig_beacon = true;
107 static void ath9k_htc_vif_reconfig(struct ath9k_htc_priv *priv)
109 priv->rearm_ani = false;
110 priv->reconfig_beacon = false;
112 ieee80211_iterate_active_interfaces_atomic(
113 priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
114 ath9k_htc_vif_iter, priv);
115 if (priv->rearm_ani)
116 ath9k_htc_start_ani(priv);
118 if (priv->reconfig_beacon) {
119 ath9k_htc_ps_wakeup(priv);
120 ath9k_htc_beacon_reconfig(priv);
121 ath9k_htc_ps_restore(priv);
125 static void ath9k_htc_bssid_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
127 struct ath9k_vif_iter_data *iter_data = data;
128 int i;
130 if (iter_data->hw_macaddr != NULL) {
131 for (i = 0; i < ETH_ALEN; i++)
132 iter_data->mask[i] &= ~(iter_data->hw_macaddr[i] ^ mac[i]);
133 } else {
134 iter_data->hw_macaddr = mac;
138 static void ath9k_htc_set_mac_bssid_mask(struct ath9k_htc_priv *priv,
139 struct ieee80211_vif *vif)
141 struct ath_common *common = ath9k_hw_common(priv->ah);
142 struct ath9k_vif_iter_data iter_data;
145 * Pick the MAC address of the first interface as the new hardware
146 * MAC address. The hardware will use it together with the BSSID mask
147 * when matching addresses.
149 iter_data.hw_macaddr = NULL;
150 memset(&iter_data.mask, 0xff, ETH_ALEN);
152 if (vif)
153 ath9k_htc_bssid_iter(&iter_data, vif->addr, vif);
155 /* Get list of all active MAC addresses */
156 ieee80211_iterate_active_interfaces_atomic(
157 priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
158 ath9k_htc_bssid_iter, &iter_data);
160 memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
162 if (iter_data.hw_macaddr)
163 memcpy(common->macaddr, iter_data.hw_macaddr, ETH_ALEN);
165 ath_hw_setbssidmask(common);
168 static void ath9k_htc_set_opmode(struct ath9k_htc_priv *priv)
170 if (priv->num_ibss_vif)
171 priv->ah->opmode = NL80211_IFTYPE_ADHOC;
172 else if (priv->num_ap_vif)
173 priv->ah->opmode = NL80211_IFTYPE_AP;
174 else if (priv->num_mbss_vif)
175 priv->ah->opmode = NL80211_IFTYPE_MESH_POINT;
176 else
177 priv->ah->opmode = NL80211_IFTYPE_STATION;
179 ath9k_hw_setopmode(priv->ah);
182 void ath9k_htc_reset(struct ath9k_htc_priv *priv)
184 struct ath_hw *ah = priv->ah;
185 struct ath_common *common = ath9k_hw_common(ah);
186 struct ieee80211_channel *channel = priv->hw->conf.chandef.chan;
187 struct ath9k_hw_cal_data *caldata = NULL;
188 enum htc_phymode mode;
189 __be16 htc_mode;
190 u8 cmd_rsp;
191 int ret;
193 mutex_lock(&priv->mutex);
194 ath9k_htc_ps_wakeup(priv);
196 ath9k_htc_stop_ani(priv);
197 ieee80211_stop_queues(priv->hw);
199 del_timer_sync(&priv->tx.cleanup_timer);
200 ath9k_htc_tx_drain(priv);
202 WMI_CMD(WMI_DISABLE_INTR_CMDID);
203 WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
204 WMI_CMD(WMI_STOP_RECV_CMDID);
206 ath9k_wmi_event_drain(priv);
208 caldata = &priv->caldata;
209 ret = ath9k_hw_reset(ah, ah->curchan, caldata, false);
210 if (ret) {
211 ath_err(common,
212 "Unable to reset device (%u Mhz) reset status %d\n",
213 channel->center_freq, ret);
216 ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
217 &priv->curtxpow);
219 WMI_CMD(WMI_START_RECV_CMDID);
220 ath9k_host_rx_init(priv);
222 mode = ath9k_htc_get_curmode(priv, ah->curchan);
223 htc_mode = cpu_to_be16(mode);
224 WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
226 WMI_CMD(WMI_ENABLE_INTR_CMDID);
227 htc_start(priv->htc);
228 ath9k_htc_vif_reconfig(priv);
229 ieee80211_wake_queues(priv->hw);
231 mod_timer(&priv->tx.cleanup_timer,
232 jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
234 ath9k_htc_ps_restore(priv);
235 mutex_unlock(&priv->mutex);
238 static int ath9k_htc_set_channel(struct ath9k_htc_priv *priv,
239 struct ieee80211_hw *hw,
240 struct ath9k_channel *hchan)
242 struct ath_hw *ah = priv->ah;
243 struct ath_common *common = ath9k_hw_common(ah);
244 struct ieee80211_conf *conf = &common->hw->conf;
245 bool fastcc;
246 struct ieee80211_channel *channel = hw->conf.chandef.chan;
247 struct ath9k_hw_cal_data *caldata = NULL;
248 enum htc_phymode mode;
249 __be16 htc_mode;
250 u8 cmd_rsp;
251 int ret;
253 if (test_bit(OP_INVALID, &priv->op_flags))
254 return -EIO;
256 fastcc = !!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL);
258 ath9k_htc_ps_wakeup(priv);
260 del_timer_sync(&priv->tx.cleanup_timer);
261 ath9k_htc_tx_drain(priv);
263 WMI_CMD(WMI_DISABLE_INTR_CMDID);
264 WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
265 WMI_CMD(WMI_STOP_RECV_CMDID);
267 ath9k_wmi_event_drain(priv);
269 ath_dbg(common, CONFIG,
270 "(%u MHz) -> (%u MHz), HT: %d, HT40: %d fastcc: %d\n",
271 priv->ah->curchan->channel,
272 channel->center_freq, conf_is_ht(conf), conf_is_ht40(conf),
273 fastcc);
275 if (!fastcc)
276 caldata = &priv->caldata;
278 ret = ath9k_hw_reset(ah, hchan, caldata, fastcc);
279 if (ret) {
280 ath_err(common,
281 "Unable to reset channel (%u Mhz) reset status %d\n",
282 channel->center_freq, ret);
283 goto err;
286 ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
287 &priv->curtxpow);
289 WMI_CMD(WMI_START_RECV_CMDID);
290 if (ret)
291 goto err;
293 ath9k_host_rx_init(priv);
295 mode = ath9k_htc_get_curmode(priv, hchan);
296 htc_mode = cpu_to_be16(mode);
297 WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
298 if (ret)
299 goto err;
301 WMI_CMD(WMI_ENABLE_INTR_CMDID);
302 if (ret)
303 goto err;
305 htc_start(priv->htc);
307 if (!test_bit(OP_SCANNING, &priv->op_flags) &&
308 !(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL))
309 ath9k_htc_vif_reconfig(priv);
311 mod_timer(&priv->tx.cleanup_timer,
312 jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
314 err:
315 ath9k_htc_ps_restore(priv);
316 return ret;
320 * Monitor mode handling is a tad complicated because the firmware requires
321 * an interface to be created exclusively, while mac80211 doesn't associate
322 * an interface with the mode.
324 * So, for now, only one monitor interface can be configured.
326 static void __ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
328 struct ath_common *common = ath9k_hw_common(priv->ah);
329 struct ath9k_htc_target_vif hvif;
330 int ret = 0;
331 u8 cmd_rsp;
333 memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
334 memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
335 hvif.index = priv->mon_vif_idx;
336 WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
337 if (ret) {
338 ath_err(common, "Unable to remove monitor interface at idx: %d\n",
339 priv->mon_vif_idx);
342 priv->nvifs--;
343 priv->vif_slot &= ~(1 << priv->mon_vif_idx);
346 static int ath9k_htc_add_monitor_interface(struct ath9k_htc_priv *priv)
348 struct ath_common *common = ath9k_hw_common(priv->ah);
349 struct ath9k_htc_target_vif hvif;
350 struct ath9k_htc_target_sta tsta;
351 int ret = 0, sta_idx;
352 u8 cmd_rsp;
354 if ((priv->nvifs >= ATH9K_HTC_MAX_VIF) ||
355 (priv->nstations >= ATH9K_HTC_MAX_STA)) {
356 ret = -ENOBUFS;
357 goto err_vif;
360 sta_idx = ffz(priv->sta_slot);
361 if ((sta_idx < 0) || (sta_idx > ATH9K_HTC_MAX_STA)) {
362 ret = -ENOBUFS;
363 goto err_vif;
367 * Add an interface.
369 memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
370 memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
372 hvif.opmode = HTC_M_MONITOR;
373 hvif.index = ffz(priv->vif_slot);
375 WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
376 if (ret)
377 goto err_vif;
380 * Assign the monitor interface index as a special case here.
381 * This is needed when the interface is brought down.
383 priv->mon_vif_idx = hvif.index;
384 priv->vif_slot |= (1 << hvif.index);
387 * Set the hardware mode to monitor only if there are no
388 * other interfaces.
390 if (!priv->nvifs)
391 priv->ah->opmode = NL80211_IFTYPE_MONITOR;
393 priv->nvifs++;
396 * Associate a station with the interface for packet injection.
398 memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
400 memcpy(&tsta.macaddr, common->macaddr, ETH_ALEN);
402 tsta.is_vif_sta = 1;
403 tsta.sta_index = sta_idx;
404 tsta.vif_index = hvif.index;
405 tsta.maxampdu = cpu_to_be16(0xffff);
407 WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
408 if (ret) {
409 ath_err(common, "Unable to add station entry for monitor mode\n");
410 goto err_sta;
413 priv->sta_slot |= (1 << sta_idx);
414 priv->nstations++;
415 priv->vif_sta_pos[priv->mon_vif_idx] = sta_idx;
416 priv->ah->is_monitoring = true;
418 ath_dbg(common, CONFIG,
419 "Attached a monitor interface at idx: %d, sta idx: %d\n",
420 priv->mon_vif_idx, sta_idx);
422 return 0;
424 err_sta:
426 * Remove the interface from the target.
428 __ath9k_htc_remove_monitor_interface(priv);
429 err_vif:
430 ath_dbg(common, FATAL, "Unable to attach a monitor interface\n");
432 return ret;
435 static int ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
437 struct ath_common *common = ath9k_hw_common(priv->ah);
438 int ret = 0;
439 u8 cmd_rsp, sta_idx;
441 __ath9k_htc_remove_monitor_interface(priv);
443 sta_idx = priv->vif_sta_pos[priv->mon_vif_idx];
445 WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
446 if (ret) {
447 ath_err(common, "Unable to remove station entry for monitor mode\n");
448 return ret;
451 priv->sta_slot &= ~(1 << sta_idx);
452 priv->nstations--;
453 priv->ah->is_monitoring = false;
455 ath_dbg(common, CONFIG,
456 "Removed a monitor interface at idx: %d, sta idx: %d\n",
457 priv->mon_vif_idx, sta_idx);
459 return 0;
462 static int ath9k_htc_add_station(struct ath9k_htc_priv *priv,
463 struct ieee80211_vif *vif,
464 struct ieee80211_sta *sta)
466 struct ath_common *common = ath9k_hw_common(priv->ah);
467 struct ath9k_htc_target_sta tsta;
468 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
469 struct ath9k_htc_sta *ista;
470 int ret, sta_idx;
471 u8 cmd_rsp;
472 u16 maxampdu;
474 if (priv->nstations >= ATH9K_HTC_MAX_STA)
475 return -ENOBUFS;
477 sta_idx = ffz(priv->sta_slot);
478 if ((sta_idx < 0) || (sta_idx > ATH9K_HTC_MAX_STA))
479 return -ENOBUFS;
481 memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
483 if (sta) {
484 ista = (struct ath9k_htc_sta *) sta->drv_priv;
485 memcpy(&tsta.macaddr, sta->addr, ETH_ALEN);
486 memcpy(&tsta.bssid, common->curbssid, ETH_ALEN);
487 ista->index = sta_idx;
488 tsta.is_vif_sta = 0;
489 maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
490 sta->ht_cap.ampdu_factor);
491 tsta.maxampdu = cpu_to_be16(maxampdu);
492 } else {
493 memcpy(&tsta.macaddr, vif->addr, ETH_ALEN);
494 tsta.is_vif_sta = 1;
495 tsta.maxampdu = cpu_to_be16(0xffff);
498 tsta.sta_index = sta_idx;
499 tsta.vif_index = avp->index;
501 WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
502 if (ret) {
503 if (sta)
504 ath_err(common,
505 "Unable to add station entry for: %pM\n",
506 sta->addr);
507 return ret;
510 if (sta) {
511 ath_dbg(common, CONFIG,
512 "Added a station entry for: %pM (idx: %d)\n",
513 sta->addr, tsta.sta_index);
514 } else {
515 ath_dbg(common, CONFIG,
516 "Added a station entry for VIF %d (idx: %d)\n",
517 avp->index, tsta.sta_index);
520 priv->sta_slot |= (1 << sta_idx);
521 priv->nstations++;
522 if (!sta)
523 priv->vif_sta_pos[avp->index] = sta_idx;
525 return 0;
528 static int ath9k_htc_remove_station(struct ath9k_htc_priv *priv,
529 struct ieee80211_vif *vif,
530 struct ieee80211_sta *sta)
532 struct ath_common *common = ath9k_hw_common(priv->ah);
533 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
534 struct ath9k_htc_sta *ista;
535 int ret;
536 u8 cmd_rsp, sta_idx;
538 if (sta) {
539 ista = (struct ath9k_htc_sta *) sta->drv_priv;
540 sta_idx = ista->index;
541 } else {
542 sta_idx = priv->vif_sta_pos[avp->index];
545 WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
546 if (ret) {
547 if (sta)
548 ath_err(common,
549 "Unable to remove station entry for: %pM\n",
550 sta->addr);
551 return ret;
554 if (sta) {
555 ath_dbg(common, CONFIG,
556 "Removed a station entry for: %pM (idx: %d)\n",
557 sta->addr, sta_idx);
558 } else {
559 ath_dbg(common, CONFIG,
560 "Removed a station entry for VIF %d (idx: %d)\n",
561 avp->index, sta_idx);
564 priv->sta_slot &= ~(1 << sta_idx);
565 priv->nstations--;
567 return 0;
570 int ath9k_htc_update_cap_target(struct ath9k_htc_priv *priv,
571 u8 enable_coex)
573 struct ath9k_htc_cap_target tcap;
574 int ret;
575 u8 cmd_rsp;
577 memset(&tcap, 0, sizeof(struct ath9k_htc_cap_target));
579 tcap.ampdu_limit = cpu_to_be32(0xffff);
580 tcap.ampdu_subframes = 0xff;
581 tcap.enable_coex = enable_coex;
582 tcap.tx_chainmask = priv->ah->caps.tx_chainmask;
584 WMI_CMD_BUF(WMI_TARGET_IC_UPDATE_CMDID, &tcap);
586 return ret;
589 static void ath9k_htc_setup_rate(struct ath9k_htc_priv *priv,
590 struct ieee80211_sta *sta,
591 struct ath9k_htc_target_rate *trate)
593 struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
594 struct ieee80211_supported_band *sband;
595 u32 caps = 0;
596 int i, j;
598 sband = priv->hw->wiphy->bands[priv->hw->conf.chandef.chan->band];
600 for (i = 0, j = 0; i < sband->n_bitrates; i++) {
601 if (sta->supp_rates[sband->band] & BIT(i)) {
602 trate->rates.legacy_rates.rs_rates[j]
603 = (sband->bitrates[i].bitrate * 2) / 10;
604 j++;
607 trate->rates.legacy_rates.rs_nrates = j;
609 if (sta->ht_cap.ht_supported) {
610 for (i = 0, j = 0; i < 77; i++) {
611 if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
612 trate->rates.ht_rates.rs_rates[j++] = i;
613 if (j == ATH_HTC_RATE_MAX)
614 break;
616 trate->rates.ht_rates.rs_nrates = j;
618 caps = WLAN_RC_HT_FLAG;
619 if (sta->ht_cap.cap & IEEE80211_HT_CAP_RX_STBC)
620 caps |= ATH_RC_TX_STBC_FLAG;
621 if (sta->ht_cap.mcs.rx_mask[1])
622 caps |= WLAN_RC_DS_FLAG;
623 if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
624 (conf_is_ht40(&priv->hw->conf)))
625 caps |= WLAN_RC_40_FLAG;
626 if (conf_is_ht40(&priv->hw->conf) &&
627 (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40))
628 caps |= WLAN_RC_SGI_FLAG;
629 else if (conf_is_ht20(&priv->hw->conf) &&
630 (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20))
631 caps |= WLAN_RC_SGI_FLAG;
634 trate->sta_index = ista->index;
635 trate->isnew = 1;
636 trate->capflags = cpu_to_be32(caps);
639 static int ath9k_htc_send_rate_cmd(struct ath9k_htc_priv *priv,
640 struct ath9k_htc_target_rate *trate)
642 struct ath_common *common = ath9k_hw_common(priv->ah);
643 int ret;
644 u8 cmd_rsp;
646 WMI_CMD_BUF(WMI_RC_RATE_UPDATE_CMDID, trate);
647 if (ret) {
648 ath_err(common,
649 "Unable to initialize Rate information on target\n");
652 return ret;
655 static void ath9k_htc_init_rate(struct ath9k_htc_priv *priv,
656 struct ieee80211_sta *sta)
658 struct ath_common *common = ath9k_hw_common(priv->ah);
659 struct ath9k_htc_target_rate trate;
660 int ret;
662 memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
663 ath9k_htc_setup_rate(priv, sta, &trate);
664 ret = ath9k_htc_send_rate_cmd(priv, &trate);
665 if (!ret)
666 ath_dbg(common, CONFIG,
667 "Updated target sta: %pM, rate caps: 0x%X\n",
668 sta->addr, be32_to_cpu(trate.capflags));
671 static void ath9k_htc_update_rate(struct ath9k_htc_priv *priv,
672 struct ieee80211_vif *vif,
673 struct ieee80211_bss_conf *bss_conf)
675 struct ath_common *common = ath9k_hw_common(priv->ah);
676 struct ath9k_htc_target_rate trate;
677 struct ieee80211_sta *sta;
678 int ret;
680 memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
682 rcu_read_lock();
683 sta = ieee80211_find_sta(vif, bss_conf->bssid);
684 if (!sta) {
685 rcu_read_unlock();
686 return;
688 ath9k_htc_setup_rate(priv, sta, &trate);
689 rcu_read_unlock();
691 ret = ath9k_htc_send_rate_cmd(priv, &trate);
692 if (!ret)
693 ath_dbg(common, CONFIG,
694 "Updated target sta: %pM, rate caps: 0x%X\n",
695 bss_conf->bssid, be32_to_cpu(trate.capflags));
698 static int ath9k_htc_tx_aggr_oper(struct ath9k_htc_priv *priv,
699 struct ieee80211_vif *vif,
700 struct ieee80211_sta *sta,
701 enum ieee80211_ampdu_mlme_action action,
702 u16 tid)
704 struct ath_common *common = ath9k_hw_common(priv->ah);
705 struct ath9k_htc_target_aggr aggr;
706 struct ath9k_htc_sta *ista;
707 int ret = 0;
708 u8 cmd_rsp;
710 if (tid >= ATH9K_HTC_MAX_TID)
711 return -EINVAL;
713 memset(&aggr, 0, sizeof(struct ath9k_htc_target_aggr));
714 ista = (struct ath9k_htc_sta *) sta->drv_priv;
716 aggr.sta_index = ista->index;
717 aggr.tidno = tid & 0xf;
718 aggr.aggr_enable = (action == IEEE80211_AMPDU_TX_START) ? true : false;
720 WMI_CMD_BUF(WMI_TX_AGGR_ENABLE_CMDID, &aggr);
721 if (ret)
722 ath_dbg(common, CONFIG,
723 "Unable to %s TX aggregation for (%pM, %d)\n",
724 (aggr.aggr_enable) ? "start" : "stop", sta->addr, tid);
725 else
726 ath_dbg(common, CONFIG,
727 "%s TX aggregation for (%pM, %d)\n",
728 (aggr.aggr_enable) ? "Starting" : "Stopping",
729 sta->addr, tid);
731 spin_lock_bh(&priv->tx.tx_lock);
732 ista->tid_state[tid] = (aggr.aggr_enable && !ret) ? AGGR_START : AGGR_STOP;
733 spin_unlock_bh(&priv->tx.tx_lock);
735 return ret;
738 /*******/
739 /* ANI */
740 /*******/
742 void ath9k_htc_start_ani(struct ath9k_htc_priv *priv)
744 struct ath_common *common = ath9k_hw_common(priv->ah);
745 unsigned long timestamp = jiffies_to_msecs(jiffies);
747 common->ani.longcal_timer = timestamp;
748 common->ani.shortcal_timer = timestamp;
749 common->ani.checkani_timer = timestamp;
751 set_bit(OP_ANI_RUNNING, &priv->op_flags);
753 ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
754 msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
757 void ath9k_htc_stop_ani(struct ath9k_htc_priv *priv)
759 cancel_delayed_work_sync(&priv->ani_work);
760 clear_bit(OP_ANI_RUNNING, &priv->op_flags);
763 void ath9k_htc_ani_work(struct work_struct *work)
765 struct ath9k_htc_priv *priv =
766 container_of(work, struct ath9k_htc_priv, ani_work.work);
767 struct ath_hw *ah = priv->ah;
768 struct ath_common *common = ath9k_hw_common(ah);
769 bool longcal = false;
770 bool shortcal = false;
771 bool aniflag = false;
772 unsigned int timestamp = jiffies_to_msecs(jiffies);
773 u32 cal_interval, short_cal_interval;
775 short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
776 ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
778 /* Only calibrate if awake */
779 if (ah->power_mode != ATH9K_PM_AWAKE)
780 goto set_timer;
782 /* Long calibration runs independently of short calibration. */
783 if ((timestamp - common->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
784 longcal = true;
785 ath_dbg(common, ANI, "longcal @%lu\n", jiffies);
786 common->ani.longcal_timer = timestamp;
789 /* Short calibration applies only while caldone is false */
790 if (!common->ani.caldone) {
791 if ((timestamp - common->ani.shortcal_timer) >=
792 short_cal_interval) {
793 shortcal = true;
794 ath_dbg(common, ANI, "shortcal @%lu\n", jiffies);
795 common->ani.shortcal_timer = timestamp;
796 common->ani.resetcal_timer = timestamp;
798 } else {
799 if ((timestamp - common->ani.resetcal_timer) >=
800 ATH_RESTART_CALINTERVAL) {
801 common->ani.caldone = ath9k_hw_reset_calvalid(ah);
802 if (common->ani.caldone)
803 common->ani.resetcal_timer = timestamp;
807 /* Verify whether we must check ANI */
808 if ((timestamp - common->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
809 aniflag = true;
810 common->ani.checkani_timer = timestamp;
813 /* Skip all processing if there's nothing to do. */
814 if (longcal || shortcal || aniflag) {
816 ath9k_htc_ps_wakeup(priv);
818 /* Call ANI routine if necessary */
819 if (aniflag)
820 ath9k_hw_ani_monitor(ah, ah->curchan);
822 /* Perform calibration if necessary */
823 if (longcal || shortcal)
824 common->ani.caldone =
825 ath9k_hw_calibrate(ah, ah->curchan,
826 ah->rxchainmask, longcal);
828 ath9k_htc_ps_restore(priv);
831 set_timer:
833 * Set timer interval based on previous results.
834 * The interval must be the shortest necessary to satisfy ANI,
835 * short calibration and long calibration.
837 cal_interval = ATH_LONG_CALINTERVAL;
838 cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
839 if (!common->ani.caldone)
840 cal_interval = min(cal_interval, (u32)short_cal_interval);
842 ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
843 msecs_to_jiffies(cal_interval));
846 /**********************/
847 /* mac80211 Callbacks */
848 /**********************/
850 static void ath9k_htc_tx(struct ieee80211_hw *hw,
851 struct ieee80211_tx_control *control,
852 struct sk_buff *skb)
854 struct ieee80211_hdr *hdr;
855 struct ath9k_htc_priv *priv = hw->priv;
856 struct ath_common *common = ath9k_hw_common(priv->ah);
857 int padpos, padsize, ret, slot;
859 hdr = (struct ieee80211_hdr *) skb->data;
861 /* Add the padding after the header if this is not already done */
862 padpos = ieee80211_hdrlen(hdr->frame_control);
863 padsize = padpos & 3;
864 if (padsize && skb->len > padpos) {
865 if (skb_headroom(skb) < padsize) {
866 ath_dbg(common, XMIT, "No room for padding\n");
867 goto fail_tx;
869 skb_push(skb, padsize);
870 memmove(skb->data, skb->data + padsize, padpos);
873 slot = ath9k_htc_tx_get_slot(priv);
874 if (slot < 0) {
875 ath_dbg(common, XMIT, "No free TX slot\n");
876 goto fail_tx;
879 ret = ath9k_htc_tx_start(priv, control->sta, skb, slot, false);
880 if (ret != 0) {
881 ath_dbg(common, XMIT, "Tx failed\n");
882 goto clear_slot;
885 ath9k_htc_check_stop_queues(priv);
887 return;
889 clear_slot:
890 ath9k_htc_tx_clear_slot(priv, slot);
891 fail_tx:
892 dev_kfree_skb_any(skb);
895 static int ath9k_htc_start(struct ieee80211_hw *hw)
897 struct ath9k_htc_priv *priv = hw->priv;
898 struct ath_hw *ah = priv->ah;
899 struct ath_common *common = ath9k_hw_common(ah);
900 struct ieee80211_channel *curchan = hw->conf.chandef.chan;
901 struct ath9k_channel *init_channel;
902 int ret = 0;
903 enum htc_phymode mode;
904 __be16 htc_mode;
905 u8 cmd_rsp;
907 mutex_lock(&priv->mutex);
909 ath_dbg(common, CONFIG,
910 "Starting driver with initial channel: %d MHz\n",
911 curchan->center_freq);
913 /* Ensure that HW is awake before flushing RX */
914 ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
915 WMI_CMD(WMI_FLUSH_RECV_CMDID);
917 /* setup initial channel */
918 init_channel = ath9k_cmn_get_channel(hw, ah, &hw->conf.chandef);
920 ret = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
921 if (ret) {
922 ath_err(common,
923 "Unable to reset hardware; reset status %d (freq %u MHz)\n",
924 ret, curchan->center_freq);
925 mutex_unlock(&priv->mutex);
926 return ret;
929 ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
930 &priv->curtxpow);
932 mode = ath9k_htc_get_curmode(priv, init_channel);
933 htc_mode = cpu_to_be16(mode);
934 WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
935 WMI_CMD(WMI_ATH_INIT_CMDID);
936 WMI_CMD(WMI_START_RECV_CMDID);
938 ath9k_host_rx_init(priv);
940 ret = ath9k_htc_update_cap_target(priv, 0);
941 if (ret)
942 ath_dbg(common, CONFIG,
943 "Failed to update capability in target\n");
945 clear_bit(OP_INVALID, &priv->op_flags);
946 htc_start(priv->htc);
948 spin_lock_bh(&priv->tx.tx_lock);
949 priv->tx.flags &= ~ATH9K_HTC_OP_TX_QUEUES_STOP;
950 spin_unlock_bh(&priv->tx.tx_lock);
952 ieee80211_wake_queues(hw);
954 mod_timer(&priv->tx.cleanup_timer,
955 jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
957 ath9k_htc_start_btcoex(priv);
959 mutex_unlock(&priv->mutex);
961 return ret;
964 static void ath9k_htc_stop(struct ieee80211_hw *hw)
966 struct ath9k_htc_priv *priv = hw->priv;
967 struct ath_hw *ah = priv->ah;
968 struct ath_common *common = ath9k_hw_common(ah);
969 int ret __attribute__ ((unused));
970 u8 cmd_rsp;
972 mutex_lock(&priv->mutex);
974 if (test_bit(OP_INVALID, &priv->op_flags)) {
975 ath_dbg(common, ANY, "Device not present\n");
976 mutex_unlock(&priv->mutex);
977 return;
980 ath9k_htc_ps_wakeup(priv);
982 WMI_CMD(WMI_DISABLE_INTR_CMDID);
983 WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
984 WMI_CMD(WMI_STOP_RECV_CMDID);
986 tasklet_kill(&priv->rx_tasklet);
988 del_timer_sync(&priv->tx.cleanup_timer);
989 ath9k_htc_tx_drain(priv);
990 ath9k_wmi_event_drain(priv);
992 mutex_unlock(&priv->mutex);
994 /* Cancel all the running timers/work .. */
995 cancel_work_sync(&priv->fatal_work);
996 cancel_work_sync(&priv->ps_work);
998 #ifdef CONFIG_MAC80211_LEDS
999 cancel_work_sync(&priv->led_work);
1000 #endif
1001 ath9k_htc_stop_ani(priv);
1003 mutex_lock(&priv->mutex);
1005 ath9k_htc_stop_btcoex(priv);
1007 /* Remove a monitor interface if it's present. */
1008 if (priv->ah->is_monitoring)
1009 ath9k_htc_remove_monitor_interface(priv);
1011 ath9k_hw_phy_disable(ah);
1012 ath9k_hw_disable(ah);
1013 ath9k_htc_ps_restore(priv);
1014 ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);
1016 set_bit(OP_INVALID, &priv->op_flags);
1018 ath_dbg(common, CONFIG, "Driver halt\n");
1019 mutex_unlock(&priv->mutex);
1022 static int ath9k_htc_add_interface(struct ieee80211_hw *hw,
1023 struct ieee80211_vif *vif)
1025 struct ath9k_htc_priv *priv = hw->priv;
1026 struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1027 struct ath_common *common = ath9k_hw_common(priv->ah);
1028 struct ath9k_htc_target_vif hvif;
1029 int ret = 0;
1030 u8 cmd_rsp;
1032 mutex_lock(&priv->mutex);
1034 ath9k_htc_ps_wakeup(priv);
1035 memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1036 memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1038 switch (vif->type) {
1039 case NL80211_IFTYPE_STATION:
1040 hvif.opmode = HTC_M_STA;
1041 break;
1042 case NL80211_IFTYPE_ADHOC:
1043 hvif.opmode = HTC_M_IBSS;
1044 break;
1045 case NL80211_IFTYPE_AP:
1046 hvif.opmode = HTC_M_HOSTAP;
1047 break;
1048 case NL80211_IFTYPE_MESH_POINT:
1049 hvif.opmode = HTC_M_WDS; /* close enough */
1050 break;
1051 default:
1052 ath_err(common,
1053 "Interface type %d not yet supported\n", vif->type);
1054 ret = -EOPNOTSUPP;
1055 goto out;
1058 /* Index starts from zero on the target */
1059 avp->index = hvif.index = ffz(priv->vif_slot);
1060 hvif.rtsthreshold = cpu_to_be16(2304);
1061 WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
1062 if (ret)
1063 goto out;
1066 * We need a node in target to tx mgmt frames
1067 * before association.
1069 ret = ath9k_htc_add_station(priv, vif, NULL);
1070 if (ret) {
1071 WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1072 goto out;
1075 ath9k_htc_set_mac_bssid_mask(priv, vif);
1077 priv->vif_slot |= (1 << avp->index);
1078 priv->nvifs++;
1080 INC_VIF(priv, vif->type);
1082 if ((vif->type == NL80211_IFTYPE_AP) ||
1083 (vif->type == NL80211_IFTYPE_MESH_POINT) ||
1084 (vif->type == NL80211_IFTYPE_ADHOC))
1085 ath9k_htc_assign_bslot(priv, vif);
1087 ath9k_htc_set_opmode(priv);
1089 if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
1090 !test_bit(OP_ANI_RUNNING, &priv->op_flags)) {
1091 ath9k_hw_set_tsfadjust(priv->ah, true);
1092 ath9k_htc_start_ani(priv);
1095 ath_dbg(common, CONFIG, "Attach a VIF of type: %d at idx: %d\n",
1096 vif->type, avp->index);
1098 out:
1099 ath9k_htc_ps_restore(priv);
1100 mutex_unlock(&priv->mutex);
1102 return ret;
1105 static void ath9k_htc_remove_interface(struct ieee80211_hw *hw,
1106 struct ieee80211_vif *vif)
1108 struct ath9k_htc_priv *priv = hw->priv;
1109 struct ath_common *common = ath9k_hw_common(priv->ah);
1110 struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1111 struct ath9k_htc_target_vif hvif;
1112 int ret = 0;
1113 u8 cmd_rsp;
1115 mutex_lock(&priv->mutex);
1116 ath9k_htc_ps_wakeup(priv);
1118 memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1119 memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1120 hvif.index = avp->index;
1121 WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1122 if (ret) {
1123 ath_err(common, "Unable to remove interface at idx: %d\n",
1124 avp->index);
1126 priv->nvifs--;
1127 priv->vif_slot &= ~(1 << avp->index);
1129 ath9k_htc_remove_station(priv, vif, NULL);
1131 DEC_VIF(priv, vif->type);
1133 if ((vif->type == NL80211_IFTYPE_AP) ||
1134 vif->type == NL80211_IFTYPE_MESH_POINT ||
1135 (vif->type == NL80211_IFTYPE_ADHOC))
1136 ath9k_htc_remove_bslot(priv, vif);
1138 ath9k_htc_set_opmode(priv);
1140 ath9k_htc_set_mac_bssid_mask(priv, vif);
1143 * Stop ANI only if there are no associated station interfaces.
1145 if ((vif->type == NL80211_IFTYPE_AP) && (priv->num_ap_vif == 0)) {
1146 priv->rearm_ani = false;
1147 ieee80211_iterate_active_interfaces_atomic(
1148 priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1149 ath9k_htc_vif_iter, priv);
1150 if (!priv->rearm_ani)
1151 ath9k_htc_stop_ani(priv);
1154 ath_dbg(common, CONFIG, "Detach Interface at idx: %d\n", avp->index);
1156 ath9k_htc_ps_restore(priv);
1157 mutex_unlock(&priv->mutex);
1160 static int ath9k_htc_config(struct ieee80211_hw *hw, u32 changed)
1162 struct ath9k_htc_priv *priv = hw->priv;
1163 struct ath_common *common = ath9k_hw_common(priv->ah);
1164 struct ieee80211_conf *conf = &hw->conf;
1165 bool chip_reset = false;
1166 int ret = 0;
1168 mutex_lock(&priv->mutex);
1169 ath9k_htc_ps_wakeup(priv);
1171 if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1172 mutex_lock(&priv->htc_pm_lock);
1174 priv->ps_idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1175 if (!priv->ps_idle)
1176 chip_reset = true;
1178 mutex_unlock(&priv->htc_pm_lock);
1182 * Monitor interface should be added before
1183 * IEEE80211_CONF_CHANGE_CHANNEL is handled.
1185 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1186 if ((conf->flags & IEEE80211_CONF_MONITOR) &&
1187 !priv->ah->is_monitoring)
1188 ath9k_htc_add_monitor_interface(priv);
1189 else if (priv->ah->is_monitoring)
1190 ath9k_htc_remove_monitor_interface(priv);
1193 if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) || chip_reset) {
1194 struct ieee80211_channel *curchan = hw->conf.chandef.chan;
1195 int pos = curchan->hw_value;
1197 ath_dbg(common, CONFIG, "Set channel: %d MHz\n",
1198 curchan->center_freq);
1200 ath9k_cmn_get_channel(hw, priv->ah, &hw->conf.chandef);
1201 if (ath9k_htc_set_channel(priv, hw, &priv->ah->channels[pos]) < 0) {
1202 ath_err(common, "Unable to set channel\n");
1203 ret = -EINVAL;
1204 goto out;
1209 if (changed & IEEE80211_CONF_CHANGE_PS) {
1210 if (conf->flags & IEEE80211_CONF_PS) {
1211 ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
1212 priv->ps_enabled = true;
1213 } else {
1214 priv->ps_enabled = false;
1215 cancel_work_sync(&priv->ps_work);
1216 ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1220 if (changed & IEEE80211_CONF_CHANGE_POWER) {
1221 priv->txpowlimit = 2 * conf->power_level;
1222 ath9k_cmn_update_txpow(priv->ah, priv->curtxpow,
1223 priv->txpowlimit, &priv->curtxpow);
1226 out:
1227 ath9k_htc_ps_restore(priv);
1228 mutex_unlock(&priv->mutex);
1229 return ret;
1232 #define SUPPORTED_FILTERS \
1233 (FIF_PROMISC_IN_BSS | \
1234 FIF_ALLMULTI | \
1235 FIF_CONTROL | \
1236 FIF_PSPOLL | \
1237 FIF_OTHER_BSS | \
1238 FIF_BCN_PRBRESP_PROMISC | \
1239 FIF_PROBE_REQ | \
1240 FIF_FCSFAIL)
1242 static void ath9k_htc_configure_filter(struct ieee80211_hw *hw,
1243 unsigned int changed_flags,
1244 unsigned int *total_flags,
1245 u64 multicast)
1247 struct ath9k_htc_priv *priv = hw->priv;
1248 u32 rfilt;
1250 mutex_lock(&priv->mutex);
1251 changed_flags &= SUPPORTED_FILTERS;
1252 *total_flags &= SUPPORTED_FILTERS;
1254 if (test_bit(OP_INVALID, &priv->op_flags)) {
1255 ath_dbg(ath9k_hw_common(priv->ah), ANY,
1256 "Unable to configure filter on invalid state\n");
1257 mutex_unlock(&priv->mutex);
1258 return;
1260 ath9k_htc_ps_wakeup(priv);
1262 priv->rxfilter = *total_flags;
1263 rfilt = ath9k_htc_calcrxfilter(priv);
1264 ath9k_hw_setrxfilter(priv->ah, rfilt);
1266 ath_dbg(ath9k_hw_common(priv->ah), CONFIG, "Set HW RX filter: 0x%x\n",
1267 rfilt);
1269 ath9k_htc_ps_restore(priv);
1270 mutex_unlock(&priv->mutex);
1273 static int ath9k_htc_sta_add(struct ieee80211_hw *hw,
1274 struct ieee80211_vif *vif,
1275 struct ieee80211_sta *sta)
1277 struct ath9k_htc_priv *priv = hw->priv;
1278 int ret;
1280 mutex_lock(&priv->mutex);
1281 ath9k_htc_ps_wakeup(priv);
1282 ret = ath9k_htc_add_station(priv, vif, sta);
1283 if (!ret)
1284 ath9k_htc_init_rate(priv, sta);
1285 ath9k_htc_ps_restore(priv);
1286 mutex_unlock(&priv->mutex);
1288 return ret;
1291 static int ath9k_htc_sta_remove(struct ieee80211_hw *hw,
1292 struct ieee80211_vif *vif,
1293 struct ieee80211_sta *sta)
1295 struct ath9k_htc_priv *priv = hw->priv;
1296 struct ath9k_htc_sta *ista;
1297 int ret;
1299 mutex_lock(&priv->mutex);
1300 ath9k_htc_ps_wakeup(priv);
1301 ista = (struct ath9k_htc_sta *) sta->drv_priv;
1302 htc_sta_drain(priv->htc, ista->index);
1303 ret = ath9k_htc_remove_station(priv, vif, sta);
1304 ath9k_htc_ps_restore(priv);
1305 mutex_unlock(&priv->mutex);
1307 return ret;
1310 static void ath9k_htc_sta_rc_update(struct ieee80211_hw *hw,
1311 struct ieee80211_vif *vif,
1312 struct ieee80211_sta *sta, u32 changed)
1314 struct ath9k_htc_priv *priv = hw->priv;
1315 struct ath_common *common = ath9k_hw_common(priv->ah);
1316 struct ath9k_htc_target_rate trate;
1318 mutex_lock(&priv->mutex);
1319 ath9k_htc_ps_wakeup(priv);
1321 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
1322 memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
1323 ath9k_htc_setup_rate(priv, sta, &trate);
1324 if (!ath9k_htc_send_rate_cmd(priv, &trate))
1325 ath_dbg(common, CONFIG,
1326 "Supported rates for sta: %pM updated, rate caps: 0x%X\n",
1327 sta->addr, be32_to_cpu(trate.capflags));
1328 else
1329 ath_dbg(common, CONFIG,
1330 "Unable to update supported rates for sta: %pM\n",
1331 sta->addr);
1334 ath9k_htc_ps_restore(priv);
1335 mutex_unlock(&priv->mutex);
1338 static int ath9k_htc_conf_tx(struct ieee80211_hw *hw,
1339 struct ieee80211_vif *vif, u16 queue,
1340 const struct ieee80211_tx_queue_params *params)
1342 struct ath9k_htc_priv *priv = hw->priv;
1343 struct ath_common *common = ath9k_hw_common(priv->ah);
1344 struct ath9k_tx_queue_info qi;
1345 int ret = 0, qnum;
1347 if (queue >= IEEE80211_NUM_ACS)
1348 return 0;
1350 mutex_lock(&priv->mutex);
1351 ath9k_htc_ps_wakeup(priv);
1353 memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1355 qi.tqi_aifs = params->aifs;
1356 qi.tqi_cwmin = params->cw_min;
1357 qi.tqi_cwmax = params->cw_max;
1358 qi.tqi_burstTime = params->txop * 32;
1360 qnum = get_hw_qnum(queue, priv->hwq_map);
1362 ath_dbg(common, CONFIG,
1363 "Configure tx [queue/hwq] [%d/%d], aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1364 queue, qnum, params->aifs, params->cw_min,
1365 params->cw_max, params->txop);
1367 ret = ath_htc_txq_update(priv, qnum, &qi);
1368 if (ret) {
1369 ath_err(common, "TXQ Update failed\n");
1370 goto out;
1373 if ((priv->ah->opmode == NL80211_IFTYPE_ADHOC) &&
1374 (qnum == priv->hwq_map[IEEE80211_AC_BE]))
1375 ath9k_htc_beaconq_config(priv);
1376 out:
1377 ath9k_htc_ps_restore(priv);
1378 mutex_unlock(&priv->mutex);
1380 return ret;
1383 static int ath9k_htc_set_key(struct ieee80211_hw *hw,
1384 enum set_key_cmd cmd,
1385 struct ieee80211_vif *vif,
1386 struct ieee80211_sta *sta,
1387 struct ieee80211_key_conf *key)
1389 struct ath9k_htc_priv *priv = hw->priv;
1390 struct ath_common *common = ath9k_hw_common(priv->ah);
1391 int ret = 0;
1393 if (htc_modparam_nohwcrypt)
1394 return -ENOSPC;
1396 if ((vif->type == NL80211_IFTYPE_ADHOC ||
1397 vif->type == NL80211_IFTYPE_MESH_POINT) &&
1398 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
1399 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
1400 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1402 * For now, disable hw crypto for the RSN IBSS group keys. This
1403 * could be optimized in the future to use a modified key cache
1404 * design to support per-STA RX GTK, but until that gets
1405 * implemented, use of software crypto for group addressed
1406 * frames is a acceptable to allow RSN IBSS to be used.
1408 return -EOPNOTSUPP;
1411 mutex_lock(&priv->mutex);
1412 ath_dbg(common, CONFIG, "Set HW Key\n");
1413 ath9k_htc_ps_wakeup(priv);
1415 switch (cmd) {
1416 case SET_KEY:
1417 ret = ath_key_config(common, vif, sta, key);
1418 if (ret >= 0) {
1419 key->hw_key_idx = ret;
1420 /* push IV and Michael MIC generation to stack */
1421 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1422 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1423 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1424 if (priv->ah->sw_mgmt_crypto &&
1425 key->cipher == WLAN_CIPHER_SUITE_CCMP)
1426 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
1427 ret = 0;
1429 break;
1430 case DISABLE_KEY:
1431 ath_key_delete(common, key);
1432 break;
1433 default:
1434 ret = -EINVAL;
1437 ath9k_htc_ps_restore(priv);
1438 mutex_unlock(&priv->mutex);
1440 return ret;
1443 static void ath9k_htc_set_bssid(struct ath9k_htc_priv *priv)
1445 struct ath_common *common = ath9k_hw_common(priv->ah);
1447 ath9k_hw_write_associd(priv->ah);
1448 ath_dbg(common, CONFIG, "BSSID: %pM aid: 0x%x\n",
1449 common->curbssid, common->curaid);
1452 static void ath9k_htc_bss_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
1454 struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
1455 struct ath_common *common = ath9k_hw_common(priv->ah);
1456 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1458 if ((vif->type == NL80211_IFTYPE_STATION) && bss_conf->assoc) {
1459 common->curaid = bss_conf->aid;
1460 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1464 static void ath9k_htc_choose_set_bssid(struct ath9k_htc_priv *priv)
1466 if (priv->num_sta_assoc_vif == 1) {
1467 ieee80211_iterate_active_interfaces_atomic(
1468 priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1469 ath9k_htc_bss_iter, priv);
1470 ath9k_htc_set_bssid(priv);
1474 static void ath9k_htc_bss_info_changed(struct ieee80211_hw *hw,
1475 struct ieee80211_vif *vif,
1476 struct ieee80211_bss_conf *bss_conf,
1477 u32 changed)
1479 struct ath9k_htc_priv *priv = hw->priv;
1480 struct ath_hw *ah = priv->ah;
1481 struct ath_common *common = ath9k_hw_common(ah);
1483 mutex_lock(&priv->mutex);
1484 ath9k_htc_ps_wakeup(priv);
1486 if (changed & BSS_CHANGED_ASSOC) {
1487 ath_dbg(common, CONFIG, "BSS Changed ASSOC %d\n",
1488 bss_conf->assoc);
1490 bss_conf->assoc ?
1491 priv->num_sta_assoc_vif++ : priv->num_sta_assoc_vif--;
1493 if (priv->ah->opmode == NL80211_IFTYPE_STATION) {
1494 ath9k_htc_choose_set_bssid(priv);
1495 if (bss_conf->assoc && (priv->num_sta_assoc_vif == 1))
1496 ath9k_htc_start_ani(priv);
1497 else if (priv->num_sta_assoc_vif == 0)
1498 ath9k_htc_stop_ani(priv);
1502 if (changed & BSS_CHANGED_IBSS) {
1503 if (priv->ah->opmode == NL80211_IFTYPE_ADHOC) {
1504 common->curaid = bss_conf->aid;
1505 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1506 ath9k_htc_set_bssid(priv);
1510 if ((changed & BSS_CHANGED_BEACON_ENABLED) && bss_conf->enable_beacon) {
1511 ath_dbg(common, CONFIG, "Beacon enabled for BSS: %pM\n",
1512 bss_conf->bssid);
1513 ath9k_htc_set_tsfadjust(priv, vif);
1514 set_bit(OP_ENABLE_BEACON, &priv->op_flags);
1515 ath9k_htc_beacon_config(priv, vif);
1518 if ((changed & BSS_CHANGED_BEACON_ENABLED) && !bss_conf->enable_beacon) {
1520 * Disable SWBA interrupt only if there are no
1521 * concurrent AP/mesh or IBSS interfaces.
1523 if ((priv->num_ap_vif + priv->num_mbss_vif <= 1) ||
1524 priv->num_ibss_vif) {
1525 ath_dbg(common, CONFIG,
1526 "Beacon disabled for BSS: %pM\n",
1527 bss_conf->bssid);
1528 clear_bit(OP_ENABLE_BEACON, &priv->op_flags);
1529 ath9k_htc_beacon_config(priv, vif);
1533 if (changed & BSS_CHANGED_BEACON_INT) {
1535 * Reset the HW TSF for the first AP or mesh interface.
1537 if (priv->nvifs == 1 &&
1538 ((priv->ah->opmode == NL80211_IFTYPE_AP &&
1539 vif->type == NL80211_IFTYPE_AP &&
1540 priv->num_ap_vif == 1) ||
1541 (priv->ah->opmode == NL80211_IFTYPE_MESH_POINT &&
1542 vif->type == NL80211_IFTYPE_MESH_POINT &&
1543 priv->num_mbss_vif == 1))) {
1544 set_bit(OP_TSF_RESET, &priv->op_flags);
1546 ath_dbg(common, CONFIG,
1547 "Beacon interval changed for BSS: %pM\n",
1548 bss_conf->bssid);
1549 ath9k_htc_beacon_config(priv, vif);
1552 if (changed & BSS_CHANGED_ERP_SLOT) {
1553 if (bss_conf->use_short_slot)
1554 ah->slottime = 9;
1555 else
1556 ah->slottime = 20;
1558 ath9k_hw_init_global_settings(ah);
1561 if (changed & BSS_CHANGED_HT)
1562 ath9k_htc_update_rate(priv, vif, bss_conf);
1564 ath9k_htc_ps_restore(priv);
1565 mutex_unlock(&priv->mutex);
1568 static u64 ath9k_htc_get_tsf(struct ieee80211_hw *hw,
1569 struct ieee80211_vif *vif)
1571 struct ath9k_htc_priv *priv = hw->priv;
1572 u64 tsf;
1574 mutex_lock(&priv->mutex);
1575 ath9k_htc_ps_wakeup(priv);
1576 tsf = ath9k_hw_gettsf64(priv->ah);
1577 ath9k_htc_ps_restore(priv);
1578 mutex_unlock(&priv->mutex);
1580 return tsf;
1583 static void ath9k_htc_set_tsf(struct ieee80211_hw *hw,
1584 struct ieee80211_vif *vif, u64 tsf)
1586 struct ath9k_htc_priv *priv = hw->priv;
1588 mutex_lock(&priv->mutex);
1589 ath9k_htc_ps_wakeup(priv);
1590 ath9k_hw_settsf64(priv->ah, tsf);
1591 ath9k_htc_ps_restore(priv);
1592 mutex_unlock(&priv->mutex);
1595 static void ath9k_htc_reset_tsf(struct ieee80211_hw *hw,
1596 struct ieee80211_vif *vif)
1598 struct ath9k_htc_priv *priv = hw->priv;
1600 mutex_lock(&priv->mutex);
1601 ath9k_htc_ps_wakeup(priv);
1602 ath9k_hw_reset_tsf(priv->ah);
1603 ath9k_htc_ps_restore(priv);
1604 mutex_unlock(&priv->mutex);
1607 static int ath9k_htc_ampdu_action(struct ieee80211_hw *hw,
1608 struct ieee80211_vif *vif,
1609 enum ieee80211_ampdu_mlme_action action,
1610 struct ieee80211_sta *sta,
1611 u16 tid, u16 *ssn, u8 buf_size)
1613 struct ath9k_htc_priv *priv = hw->priv;
1614 struct ath9k_htc_sta *ista;
1615 int ret = 0;
1617 mutex_lock(&priv->mutex);
1618 ath9k_htc_ps_wakeup(priv);
1620 switch (action) {
1621 case IEEE80211_AMPDU_RX_START:
1622 break;
1623 case IEEE80211_AMPDU_RX_STOP:
1624 break;
1625 case IEEE80211_AMPDU_TX_START:
1626 ret = ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1627 if (!ret)
1628 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1629 break;
1630 case IEEE80211_AMPDU_TX_STOP_CONT:
1631 case IEEE80211_AMPDU_TX_STOP_FLUSH:
1632 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1633 ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1634 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1635 break;
1636 case IEEE80211_AMPDU_TX_OPERATIONAL:
1637 ista = (struct ath9k_htc_sta *) sta->drv_priv;
1638 spin_lock_bh(&priv->tx.tx_lock);
1639 ista->tid_state[tid] = AGGR_OPERATIONAL;
1640 spin_unlock_bh(&priv->tx.tx_lock);
1641 break;
1642 default:
1643 ath_err(ath9k_hw_common(priv->ah), "Unknown AMPDU action\n");
1646 ath9k_htc_ps_restore(priv);
1647 mutex_unlock(&priv->mutex);
1649 return ret;
1652 static void ath9k_htc_sw_scan_start(struct ieee80211_hw *hw)
1654 struct ath9k_htc_priv *priv = hw->priv;
1656 mutex_lock(&priv->mutex);
1657 spin_lock_bh(&priv->beacon_lock);
1658 set_bit(OP_SCANNING, &priv->op_flags);
1659 spin_unlock_bh(&priv->beacon_lock);
1660 cancel_work_sync(&priv->ps_work);
1661 ath9k_htc_stop_ani(priv);
1662 mutex_unlock(&priv->mutex);
1665 static void ath9k_htc_sw_scan_complete(struct ieee80211_hw *hw)
1667 struct ath9k_htc_priv *priv = hw->priv;
1669 mutex_lock(&priv->mutex);
1670 spin_lock_bh(&priv->beacon_lock);
1671 clear_bit(OP_SCANNING, &priv->op_flags);
1672 spin_unlock_bh(&priv->beacon_lock);
1673 ath9k_htc_ps_wakeup(priv);
1674 ath9k_htc_vif_reconfig(priv);
1675 ath9k_htc_ps_restore(priv);
1676 mutex_unlock(&priv->mutex);
1679 static int ath9k_htc_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1681 return 0;
1684 static void ath9k_htc_set_coverage_class(struct ieee80211_hw *hw,
1685 u8 coverage_class)
1687 struct ath9k_htc_priv *priv = hw->priv;
1689 mutex_lock(&priv->mutex);
1690 ath9k_htc_ps_wakeup(priv);
1691 priv->ah->coverage_class = coverage_class;
1692 ath9k_hw_init_global_settings(priv->ah);
1693 ath9k_htc_ps_restore(priv);
1694 mutex_unlock(&priv->mutex);
1698 * Currently, this is used only for selecting the minimum rate
1699 * for management frames, rate selection for data frames remain
1700 * unaffected.
1702 static int ath9k_htc_set_bitrate_mask(struct ieee80211_hw *hw,
1703 struct ieee80211_vif *vif,
1704 const struct cfg80211_bitrate_mask *mask)
1706 struct ath9k_htc_priv *priv = hw->priv;
1707 struct ath_common *common = ath9k_hw_common(priv->ah);
1708 struct ath9k_htc_target_rate_mask tmask;
1709 struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1710 int ret = 0;
1711 u8 cmd_rsp;
1713 memset(&tmask, 0, sizeof(struct ath9k_htc_target_rate_mask));
1715 tmask.vif_index = avp->index;
1716 tmask.band = IEEE80211_BAND_2GHZ;
1717 tmask.mask = cpu_to_be32(mask->control[IEEE80211_BAND_2GHZ].legacy);
1719 WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
1720 if (ret) {
1721 ath_err(common,
1722 "Unable to set 2G rate mask for "
1723 "interface at idx: %d\n", avp->index);
1724 goto out;
1727 tmask.band = IEEE80211_BAND_5GHZ;
1728 tmask.mask = cpu_to_be32(mask->control[IEEE80211_BAND_5GHZ].legacy);
1730 WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
1731 if (ret) {
1732 ath_err(common,
1733 "Unable to set 5G rate mask for "
1734 "interface at idx: %d\n", avp->index);
1735 goto out;
1738 ath_dbg(common, CONFIG, "Set bitrate masks: 0x%x, 0x%x\n",
1739 mask->control[IEEE80211_BAND_2GHZ].legacy,
1740 mask->control[IEEE80211_BAND_5GHZ].legacy);
1741 out:
1742 return ret;
1746 static int ath9k_htc_get_stats(struct ieee80211_hw *hw,
1747 struct ieee80211_low_level_stats *stats)
1749 struct ath9k_htc_priv *priv = hw->priv;
1750 struct ath_hw *ah = priv->ah;
1751 struct ath9k_mib_stats *mib_stats = &ah->ah_mibStats;
1753 stats->dot11ACKFailureCount = mib_stats->ackrcv_bad;
1754 stats->dot11RTSFailureCount = mib_stats->rts_bad;
1755 stats->dot11FCSErrorCount = mib_stats->fcs_bad;
1756 stats->dot11RTSSuccessCount = mib_stats->rts_good;
1758 return 0;
1761 struct base_eep_header *ath9k_htc_get_eeprom_base(struct ath9k_htc_priv *priv)
1763 struct base_eep_header *pBase = NULL;
1765 * This can be done since all the 3 EEPROM families have the
1766 * same base header upto a certain point, and we are interested in
1767 * the data only upto that point.
1770 if (AR_SREV_9271(priv->ah))
1771 pBase = (struct base_eep_header *)
1772 &priv->ah->eeprom.map4k.baseEepHeader;
1773 else if (priv->ah->hw_version.usbdev == AR9280_USB)
1774 pBase = (struct base_eep_header *)
1775 &priv->ah->eeprom.def.baseEepHeader;
1776 else if (priv->ah->hw_version.usbdev == AR9287_USB)
1777 pBase = (struct base_eep_header *)
1778 &priv->ah->eeprom.map9287.baseEepHeader;
1779 return pBase;
1783 static int ath9k_htc_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant,
1784 u32 *rx_ant)
1786 struct ath9k_htc_priv *priv = hw->priv;
1787 struct base_eep_header *pBase = ath9k_htc_get_eeprom_base(priv);
1788 if (pBase) {
1789 *tx_ant = pBase->txMask;
1790 *rx_ant = pBase->rxMask;
1791 } else {
1792 *tx_ant = 0;
1793 *rx_ant = 0;
1795 return 0;
1798 struct ieee80211_ops ath9k_htc_ops = {
1799 .tx = ath9k_htc_tx,
1800 .start = ath9k_htc_start,
1801 .stop = ath9k_htc_stop,
1802 .add_interface = ath9k_htc_add_interface,
1803 .remove_interface = ath9k_htc_remove_interface,
1804 .config = ath9k_htc_config,
1805 .configure_filter = ath9k_htc_configure_filter,
1806 .sta_add = ath9k_htc_sta_add,
1807 .sta_remove = ath9k_htc_sta_remove,
1808 .conf_tx = ath9k_htc_conf_tx,
1809 .sta_rc_update = ath9k_htc_sta_rc_update,
1810 .bss_info_changed = ath9k_htc_bss_info_changed,
1811 .set_key = ath9k_htc_set_key,
1812 .get_tsf = ath9k_htc_get_tsf,
1813 .set_tsf = ath9k_htc_set_tsf,
1814 .reset_tsf = ath9k_htc_reset_tsf,
1815 .ampdu_action = ath9k_htc_ampdu_action,
1816 .sw_scan_start = ath9k_htc_sw_scan_start,
1817 .sw_scan_complete = ath9k_htc_sw_scan_complete,
1818 .set_rts_threshold = ath9k_htc_set_rts_threshold,
1819 .rfkill_poll = ath9k_htc_rfkill_poll_state,
1820 .set_coverage_class = ath9k_htc_set_coverage_class,
1821 .set_bitrate_mask = ath9k_htc_set_bitrate_mask,
1822 .get_stats = ath9k_htc_get_stats,
1823 .get_antenna = ath9k_htc_get_antenna,
1825 #ifdef CONFIG_ATH9K_HTC_DEBUGFS
1826 .get_et_sset_count = ath9k_htc_get_et_sset_count,
1827 .get_et_stats = ath9k_htc_get_et_stats,
1828 .get_et_strings = ath9k_htc_get_et_strings,
1829 #endif