Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2010-2011 Atheros Communications Inc.
0003  *
0004  * Permission to use, copy, modify, and/or distribute this software for any
0005  * purpose with or without fee is hereby granted, provided that the above
0006  * copyright notice and this permission notice appear in all copies.
0007  *
0008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
0011  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0013  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0014  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0015  */
0016 
0017 #include "htc.h"
0018 
0019 /*************/
0020 /* Utilities */
0021 /*************/
0022 
0023 /* HACK Alert: Use 11NG for 2.4, use 11NA for 5 */
0024 static enum htc_phymode ath9k_htc_get_curmode(struct ath9k_htc_priv *priv,
0025                           struct ath9k_channel *ichan)
0026 {
0027     if (IS_CHAN_5GHZ(ichan))
0028         return HTC_MODE_11NA;
0029 
0030     return HTC_MODE_11NG;
0031 }
0032 
0033 bool ath9k_htc_setpower(struct ath9k_htc_priv *priv,
0034             enum ath9k_power_mode mode)
0035 {
0036     bool ret;
0037 
0038     mutex_lock(&priv->htc_pm_lock);
0039     ret = ath9k_hw_setpower(priv->ah, mode);
0040     mutex_unlock(&priv->htc_pm_lock);
0041 
0042     return ret;
0043 }
0044 
0045 void ath9k_htc_ps_wakeup(struct ath9k_htc_priv *priv)
0046 {
0047     mutex_lock(&priv->htc_pm_lock);
0048     if (++priv->ps_usecount != 1)
0049         goto unlock;
0050     ath9k_hw_setpower(priv->ah, ATH9K_PM_AWAKE);
0051 
0052 unlock:
0053     mutex_unlock(&priv->htc_pm_lock);
0054 }
0055 
0056 void ath9k_htc_ps_restore(struct ath9k_htc_priv *priv)
0057 {
0058     bool reset;
0059 
0060     mutex_lock(&priv->htc_pm_lock);
0061     if (--priv->ps_usecount != 0)
0062         goto unlock;
0063 
0064     if (priv->ps_idle) {
0065         ath9k_hw_setrxabort(priv->ah, true);
0066         ath9k_hw_stopdmarecv(priv->ah, &reset);
0067         ath9k_hw_setpower(priv->ah, ATH9K_PM_FULL_SLEEP);
0068     } else if (priv->ps_enabled) {
0069         ath9k_hw_setpower(priv->ah, ATH9K_PM_NETWORK_SLEEP);
0070     }
0071 
0072 unlock:
0073     mutex_unlock(&priv->htc_pm_lock);
0074 }
0075 
0076 void ath9k_ps_work(struct work_struct *work)
0077 {
0078     struct ath9k_htc_priv *priv =
0079         container_of(work, struct ath9k_htc_priv,
0080                  ps_work);
0081     ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
0082 
0083     /* The chip wakes up after receiving the first beacon
0084        while network sleep is enabled. For the driver to
0085        be in sync with the hw, set the chip to awake and
0086        only then set it to sleep.
0087      */
0088     ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
0089 }
0090 
0091 static void ath9k_htc_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
0092 {
0093     struct ath9k_htc_priv *priv = data;
0094     struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
0095 
0096     if ((vif->type == NL80211_IFTYPE_AP ||
0097          vif->type == NL80211_IFTYPE_MESH_POINT) &&
0098         bss_conf->enable_beacon) {
0099         priv->reconfig_beacon = true;
0100         priv->rearm_ani = true;
0101     }
0102 
0103     if (vif->cfg.assoc) {
0104         priv->rearm_ani = true;
0105         priv->reconfig_beacon = true;
0106     }
0107 }
0108 
0109 static void ath9k_htc_vif_reconfig(struct ath9k_htc_priv *priv)
0110 {
0111     priv->rearm_ani = false;
0112     priv->reconfig_beacon = false;
0113 
0114     ieee80211_iterate_active_interfaces_atomic(
0115         priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
0116         ath9k_htc_vif_iter, priv);
0117     if (priv->rearm_ani)
0118         ath9k_htc_start_ani(priv);
0119 
0120     if (priv->reconfig_beacon) {
0121         ath9k_htc_ps_wakeup(priv);
0122         ath9k_htc_beacon_reconfig(priv);
0123         ath9k_htc_ps_restore(priv);
0124     }
0125 }
0126 
0127 static void ath9k_htc_bssid_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
0128 {
0129     struct ath9k_vif_iter_data *iter_data = data;
0130     int i;
0131 
0132     if (iter_data->hw_macaddr != NULL) {
0133         for (i = 0; i < ETH_ALEN; i++)
0134             iter_data->mask[i] &= ~(iter_data->hw_macaddr[i] ^ mac[i]);
0135     } else {
0136         iter_data->hw_macaddr = mac;
0137     }
0138 }
0139 
0140 static void ath9k_htc_set_mac_bssid_mask(struct ath9k_htc_priv *priv,
0141                      struct ieee80211_vif *vif)
0142 {
0143     struct ath_common *common = ath9k_hw_common(priv->ah);
0144     struct ath9k_vif_iter_data iter_data;
0145 
0146     /*
0147      * Pick the MAC address of the first interface as the new hardware
0148      * MAC address. The hardware will use it together with the BSSID mask
0149      * when matching addresses.
0150      */
0151     iter_data.hw_macaddr = NULL;
0152     eth_broadcast_addr(iter_data.mask);
0153 
0154     if (vif)
0155         ath9k_htc_bssid_iter(&iter_data, vif->addr, vif);
0156 
0157     /* Get list of all active MAC addresses */
0158     ieee80211_iterate_active_interfaces_atomic(
0159         priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
0160         ath9k_htc_bssid_iter, &iter_data);
0161 
0162     memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
0163 
0164     if (iter_data.hw_macaddr)
0165         memcpy(common->macaddr, iter_data.hw_macaddr, ETH_ALEN);
0166 
0167     ath_hw_setbssidmask(common);
0168 }
0169 
0170 static void ath9k_htc_set_opmode(struct ath9k_htc_priv *priv)
0171 {
0172     if (priv->num_ibss_vif)
0173         priv->ah->opmode = NL80211_IFTYPE_ADHOC;
0174     else if (priv->num_ap_vif)
0175         priv->ah->opmode = NL80211_IFTYPE_AP;
0176     else if (priv->num_mbss_vif)
0177         priv->ah->opmode = NL80211_IFTYPE_MESH_POINT;
0178     else
0179         priv->ah->opmode = NL80211_IFTYPE_STATION;
0180 
0181     ath9k_hw_setopmode(priv->ah);
0182 }
0183 
0184 void ath9k_htc_reset(struct ath9k_htc_priv *priv)
0185 {
0186     struct ath_hw *ah = priv->ah;
0187     struct ath_common *common = ath9k_hw_common(ah);
0188     struct ieee80211_channel *channel = priv->hw->conf.chandef.chan;
0189     struct ath9k_hw_cal_data *caldata = NULL;
0190     enum htc_phymode mode;
0191     __be16 htc_mode;
0192     u8 cmd_rsp;
0193     int ret;
0194 
0195     mutex_lock(&priv->mutex);
0196     ath9k_htc_ps_wakeup(priv);
0197 
0198     ath9k_htc_stop_ani(priv);
0199     ieee80211_stop_queues(priv->hw);
0200 
0201     del_timer_sync(&priv->tx.cleanup_timer);
0202     ath9k_htc_tx_drain(priv);
0203 
0204     WMI_CMD(WMI_DISABLE_INTR_CMDID);
0205     WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
0206     WMI_CMD(WMI_STOP_RECV_CMDID);
0207 
0208     ath9k_wmi_event_drain(priv);
0209 
0210     caldata = &priv->caldata;
0211     ret = ath9k_hw_reset(ah, ah->curchan, caldata, false);
0212     if (ret) {
0213         ath_err(common,
0214             "Unable to reset device (%u Mhz) reset status %d\n",
0215             channel->center_freq, ret);
0216     }
0217 
0218     ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
0219                    &priv->curtxpow);
0220 
0221     WMI_CMD(WMI_START_RECV_CMDID);
0222     ath9k_host_rx_init(priv);
0223 
0224     mode = ath9k_htc_get_curmode(priv, ah->curchan);
0225     htc_mode = cpu_to_be16(mode);
0226     WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
0227 
0228     WMI_CMD(WMI_ENABLE_INTR_CMDID);
0229     htc_start(priv->htc);
0230     ath9k_htc_vif_reconfig(priv);
0231     ieee80211_wake_queues(priv->hw);
0232 
0233     mod_timer(&priv->tx.cleanup_timer,
0234           jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
0235 
0236     ath9k_htc_ps_restore(priv);
0237     mutex_unlock(&priv->mutex);
0238 }
0239 
0240 static int ath9k_htc_set_channel(struct ath9k_htc_priv *priv,
0241                  struct ieee80211_hw *hw,
0242                  struct ath9k_channel *hchan)
0243 {
0244     struct ath_hw *ah = priv->ah;
0245     struct ath_common *common = ath9k_hw_common(ah);
0246     struct ieee80211_conf *conf = &common->hw->conf;
0247     bool fastcc;
0248     struct ieee80211_channel *channel = hw->conf.chandef.chan;
0249     struct ath9k_hw_cal_data *caldata;
0250     enum htc_phymode mode;
0251     __be16 htc_mode;
0252     u8 cmd_rsp;
0253     int ret;
0254 
0255     if (test_bit(ATH_OP_INVALID, &common->op_flags))
0256         return -EIO;
0257 
0258     fastcc = !!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL);
0259 
0260     ath9k_htc_ps_wakeup(priv);
0261 
0262     ath9k_htc_stop_ani(priv);
0263     del_timer_sync(&priv->tx.cleanup_timer);
0264     ath9k_htc_tx_drain(priv);
0265 
0266     WMI_CMD(WMI_DISABLE_INTR_CMDID);
0267     WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
0268     WMI_CMD(WMI_STOP_RECV_CMDID);
0269 
0270     ath9k_wmi_event_drain(priv);
0271 
0272     ath_dbg(common, CONFIG,
0273         "(%u MHz) -> (%u MHz), HT: %d, HT40: %d fastcc: %d\n",
0274         priv->ah->curchan->channel,
0275         channel->center_freq, conf_is_ht(conf), conf_is_ht40(conf),
0276         fastcc);
0277     caldata = fastcc ? NULL : &priv->caldata;
0278     ret = ath9k_hw_reset(ah, hchan, caldata, fastcc);
0279     if (ret) {
0280         ath_err(common,
0281             "Unable to reset channel (%u Mhz) reset status %d\n",
0282             channel->center_freq, ret);
0283         goto err;
0284     }
0285 
0286     ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
0287                    &priv->curtxpow);
0288 
0289     WMI_CMD(WMI_START_RECV_CMDID);
0290     if (ret)
0291         goto err;
0292 
0293     ath9k_host_rx_init(priv);
0294 
0295     mode = ath9k_htc_get_curmode(priv, hchan);
0296     htc_mode = cpu_to_be16(mode);
0297     WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
0298     if (ret)
0299         goto err;
0300 
0301     WMI_CMD(WMI_ENABLE_INTR_CMDID);
0302     if (ret)
0303         goto err;
0304 
0305     htc_start(priv->htc);
0306 
0307     if (!test_bit(ATH_OP_SCANNING, &common->op_flags) &&
0308         !(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL))
0309         ath9k_htc_vif_reconfig(priv);
0310 
0311     mod_timer(&priv->tx.cleanup_timer,
0312           jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
0313 
0314     /* perform spectral scan if requested. */
0315     if (test_bit(ATH_OP_SCANNING, &common->op_flags) &&
0316              priv->spec_priv.spectral_mode == SPECTRAL_CHANSCAN)
0317         ath9k_cmn_spectral_scan_trigger(common, &priv->spec_priv);
0318 err:
0319     ath9k_htc_ps_restore(priv);
0320     return ret;
0321 }
0322 
0323 /*
0324  * Monitor mode handling is a tad complicated because the firmware requires
0325  * an interface to be created exclusively, while mac80211 doesn't associate
0326  * an interface with the mode.
0327  *
0328  * So, for now, only one monitor interface can be configured.
0329  */
0330 static void __ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
0331 {
0332     struct ath_common *common = ath9k_hw_common(priv->ah);
0333     struct ath9k_htc_target_vif hvif;
0334     int ret = 0;
0335     u8 cmd_rsp;
0336 
0337     memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
0338     memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
0339     hvif.index = priv->mon_vif_idx;
0340     WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
0341     if (ret) {
0342         ath_err(common, "Unable to remove monitor interface at idx: %d\n",
0343             priv->mon_vif_idx);
0344     }
0345 
0346     priv->nvifs--;
0347     priv->vif_slot &= ~(1 << priv->mon_vif_idx);
0348 }
0349 
0350 static int ath9k_htc_add_monitor_interface(struct ath9k_htc_priv *priv)
0351 {
0352     struct ath_common *common = ath9k_hw_common(priv->ah);
0353     struct ath9k_htc_target_vif hvif;
0354     struct ath9k_htc_target_sta tsta;
0355     int ret = 0, sta_idx;
0356     u8 cmd_rsp;
0357 
0358     if ((priv->nvifs >= ATH9K_HTC_MAX_VIF) ||
0359         (priv->nstations >= ATH9K_HTC_MAX_STA)) {
0360         ret = -ENOBUFS;
0361         goto err_vif;
0362     }
0363 
0364     sta_idx = ffz(priv->sta_slot);
0365     if ((sta_idx < 0) || (sta_idx > ATH9K_HTC_MAX_STA)) {
0366         ret = -ENOBUFS;
0367         goto err_vif;
0368     }
0369 
0370     /*
0371      * Add an interface.
0372      */
0373     memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
0374     memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
0375 
0376     hvif.opmode = HTC_M_MONITOR;
0377     hvif.index = ffz(priv->vif_slot);
0378 
0379     WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
0380     if (ret)
0381         goto err_vif;
0382 
0383     /*
0384      * Assign the monitor interface index as a special case here.
0385      * This is needed when the interface is brought down.
0386      */
0387     priv->mon_vif_idx = hvif.index;
0388     priv->vif_slot |= (1 << hvif.index);
0389 
0390     /*
0391      * Set the hardware mode to monitor only if there are no
0392      * other interfaces.
0393      */
0394     if (!priv->nvifs)
0395         priv->ah->opmode = NL80211_IFTYPE_MONITOR;
0396 
0397     priv->nvifs++;
0398 
0399     /*
0400      * Associate a station with the interface for packet injection.
0401      */
0402     memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
0403 
0404     memcpy(&tsta.macaddr, common->macaddr, ETH_ALEN);
0405 
0406     tsta.is_vif_sta = 1;
0407     tsta.sta_index = sta_idx;
0408     tsta.vif_index = hvif.index;
0409     tsta.maxampdu = cpu_to_be16(0xffff);
0410 
0411     WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
0412     if (ret) {
0413         ath_err(common, "Unable to add station entry for monitor mode\n");
0414         goto err_sta;
0415     }
0416 
0417     priv->sta_slot |= (1 << sta_idx);
0418     priv->nstations++;
0419     priv->vif_sta_pos[priv->mon_vif_idx] = sta_idx;
0420     priv->ah->is_monitoring = true;
0421 
0422     ath_dbg(common, CONFIG,
0423         "Attached a monitor interface at idx: %d, sta idx: %d\n",
0424         priv->mon_vif_idx, sta_idx);
0425 
0426     return 0;
0427 
0428 err_sta:
0429     /*
0430      * Remove the interface from the target.
0431      */
0432     __ath9k_htc_remove_monitor_interface(priv);
0433 err_vif:
0434     ath_dbg(common, FATAL, "Unable to attach a monitor interface\n");
0435 
0436     return ret;
0437 }
0438 
0439 static int ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
0440 {
0441     struct ath_common *common = ath9k_hw_common(priv->ah);
0442     int ret = 0;
0443     u8 cmd_rsp, sta_idx;
0444 
0445     __ath9k_htc_remove_monitor_interface(priv);
0446 
0447     sta_idx = priv->vif_sta_pos[priv->mon_vif_idx];
0448 
0449     WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
0450     if (ret) {
0451         ath_err(common, "Unable to remove station entry for monitor mode\n");
0452         return ret;
0453     }
0454 
0455     priv->sta_slot &= ~(1 << sta_idx);
0456     priv->nstations--;
0457     priv->ah->is_monitoring = false;
0458 
0459     ath_dbg(common, CONFIG,
0460         "Removed a monitor interface at idx: %d, sta idx: %d\n",
0461         priv->mon_vif_idx, sta_idx);
0462 
0463     return 0;
0464 }
0465 
0466 static int ath9k_htc_add_station(struct ath9k_htc_priv *priv,
0467                  struct ieee80211_vif *vif,
0468                  struct ieee80211_sta *sta)
0469 {
0470     struct ath_common *common = ath9k_hw_common(priv->ah);
0471     struct ath9k_htc_target_sta tsta;
0472     struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
0473     struct ath9k_htc_sta *ista;
0474     int ret, sta_idx;
0475     u8 cmd_rsp;
0476     u16 maxampdu;
0477 
0478     if (priv->nstations >= ATH9K_HTC_MAX_STA)
0479         return -ENOBUFS;
0480 
0481     sta_idx = ffz(priv->sta_slot);
0482     if ((sta_idx < 0) || (sta_idx > ATH9K_HTC_MAX_STA))
0483         return -ENOBUFS;
0484 
0485     memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
0486 
0487     if (sta) {
0488         ista = (struct ath9k_htc_sta *) sta->drv_priv;
0489         memcpy(&tsta.macaddr, sta->addr, ETH_ALEN);
0490         memcpy(&tsta.bssid, common->curbssid, ETH_ALEN);
0491         ista->index = sta_idx;
0492         tsta.is_vif_sta = 0;
0493         maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
0494                  sta->deflink.ht_cap.ampdu_factor);
0495         tsta.maxampdu = cpu_to_be16(maxampdu);
0496     } else {
0497         memcpy(&tsta.macaddr, vif->addr, ETH_ALEN);
0498         tsta.is_vif_sta = 1;
0499         tsta.maxampdu = cpu_to_be16(0xffff);
0500     }
0501 
0502     tsta.sta_index = sta_idx;
0503     tsta.vif_index = avp->index;
0504 
0505     WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
0506     if (ret) {
0507         if (sta)
0508             ath_err(common,
0509                 "Unable to add station entry for: %pM\n",
0510                 sta->addr);
0511         return ret;
0512     }
0513 
0514     if (sta) {
0515         ath_dbg(common, CONFIG,
0516             "Added a station entry for: %pM (idx: %d)\n",
0517             sta->addr, tsta.sta_index);
0518     } else {
0519         ath_dbg(common, CONFIG,
0520             "Added a station entry for VIF %d (idx: %d)\n",
0521             avp->index, tsta.sta_index);
0522     }
0523 
0524     priv->sta_slot |= (1 << sta_idx);
0525     priv->nstations++;
0526     if (!sta)
0527         priv->vif_sta_pos[avp->index] = sta_idx;
0528 
0529     return 0;
0530 }
0531 
0532 static int ath9k_htc_remove_station(struct ath9k_htc_priv *priv,
0533                     struct ieee80211_vif *vif,
0534                     struct ieee80211_sta *sta)
0535 {
0536     struct ath_common *common = ath9k_hw_common(priv->ah);
0537     struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
0538     struct ath9k_htc_sta *ista;
0539     int ret;
0540     u8 cmd_rsp, sta_idx;
0541 
0542     if (sta) {
0543         ista = (struct ath9k_htc_sta *) sta->drv_priv;
0544         sta_idx = ista->index;
0545     } else {
0546         sta_idx = priv->vif_sta_pos[avp->index];
0547     }
0548 
0549     WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
0550     if (ret) {
0551         if (sta)
0552             ath_err(common,
0553                 "Unable to remove station entry for: %pM\n",
0554                 sta->addr);
0555         return ret;
0556     }
0557 
0558     if (sta) {
0559         ath_dbg(common, CONFIG,
0560             "Removed a station entry for: %pM (idx: %d)\n",
0561             sta->addr, sta_idx);
0562     } else {
0563         ath_dbg(common, CONFIG,
0564             "Removed a station entry for VIF %d (idx: %d)\n",
0565             avp->index, sta_idx);
0566     }
0567 
0568     priv->sta_slot &= ~(1 << sta_idx);
0569     priv->nstations--;
0570 
0571     return 0;
0572 }
0573 
0574 int ath9k_htc_update_cap_target(struct ath9k_htc_priv *priv,
0575                 u8 enable_coex)
0576 {
0577     struct ath9k_htc_cap_target tcap;
0578     int ret;
0579     u8 cmd_rsp;
0580 
0581     memset(&tcap, 0, sizeof(struct ath9k_htc_cap_target));
0582 
0583     tcap.ampdu_limit = cpu_to_be32(0xffff);
0584     tcap.ampdu_subframes = 0xff;
0585     tcap.enable_coex = enable_coex;
0586     tcap.tx_chainmask = priv->ah->caps.tx_chainmask;
0587 
0588     WMI_CMD_BUF(WMI_TARGET_IC_UPDATE_CMDID, &tcap);
0589 
0590     return ret;
0591 }
0592 
0593 static void ath9k_htc_setup_rate(struct ath9k_htc_priv *priv,
0594                  struct ieee80211_sta *sta,
0595                  struct ath9k_htc_target_rate *trate)
0596 {
0597     struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
0598     struct ieee80211_supported_band *sband;
0599     u32 caps = 0;
0600     int i, j;
0601 
0602     sband = priv->hw->wiphy->bands[priv->hw->conf.chandef.chan->band];
0603 
0604     for (i = 0, j = 0; i < sband->n_bitrates; i++) {
0605         if (sta->deflink.supp_rates[sband->band] & BIT(i)) {
0606             trate->rates.legacy_rates.rs_rates[j]
0607                 = (sband->bitrates[i].bitrate * 2) / 10;
0608             j++;
0609         }
0610     }
0611     trate->rates.legacy_rates.rs_nrates = j;
0612 
0613     if (sta->deflink.ht_cap.ht_supported) {
0614         for (i = 0, j = 0; i < 77; i++) {
0615             if (sta->deflink.ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
0616                 trate->rates.ht_rates.rs_rates[j++] = i;
0617             if (j == ATH_HTC_RATE_MAX)
0618                 break;
0619         }
0620         trate->rates.ht_rates.rs_nrates = j;
0621 
0622         caps = WLAN_RC_HT_FLAG;
0623         if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_RX_STBC)
0624             caps |= ATH_RC_TX_STBC_FLAG;
0625         if (sta->deflink.ht_cap.mcs.rx_mask[1])
0626             caps |= WLAN_RC_DS_FLAG;
0627         if ((sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
0628             (conf_is_ht40(&priv->hw->conf)))
0629             caps |= WLAN_RC_40_FLAG;
0630         if (conf_is_ht40(&priv->hw->conf) &&
0631             (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40))
0632             caps |= WLAN_RC_SGI_FLAG;
0633         else if (conf_is_ht20(&priv->hw->conf) &&
0634              (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20))
0635             caps |= WLAN_RC_SGI_FLAG;
0636     }
0637 
0638     trate->sta_index = ista->index;
0639     trate->isnew = 1;
0640     trate->capflags = cpu_to_be32(caps);
0641 }
0642 
0643 static int ath9k_htc_send_rate_cmd(struct ath9k_htc_priv *priv,
0644                     struct ath9k_htc_target_rate *trate)
0645 {
0646     struct ath_common *common = ath9k_hw_common(priv->ah);
0647     int ret;
0648     u8 cmd_rsp;
0649 
0650     WMI_CMD_BUF(WMI_RC_RATE_UPDATE_CMDID, trate);
0651     if (ret) {
0652         ath_err(common,
0653             "Unable to initialize Rate information on target\n");
0654     }
0655 
0656     return ret;
0657 }
0658 
0659 static void ath9k_htc_init_rate(struct ath9k_htc_priv *priv,
0660                 struct ieee80211_sta *sta)
0661 {
0662     struct ath_common *common = ath9k_hw_common(priv->ah);
0663     struct ath9k_htc_target_rate trate;
0664     int ret;
0665 
0666     memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
0667     ath9k_htc_setup_rate(priv, sta, &trate);
0668     ret = ath9k_htc_send_rate_cmd(priv, &trate);
0669     if (!ret)
0670         ath_dbg(common, CONFIG,
0671             "Updated target sta: %pM, rate caps: 0x%X\n",
0672             sta->addr, be32_to_cpu(trate.capflags));
0673 }
0674 
0675 static void ath9k_htc_update_rate(struct ath9k_htc_priv *priv,
0676                   struct ieee80211_vif *vif,
0677                   struct ieee80211_bss_conf *bss_conf)
0678 {
0679     struct ath_common *common = ath9k_hw_common(priv->ah);
0680     struct ath9k_htc_target_rate trate;
0681     struct ieee80211_sta *sta;
0682     int ret;
0683 
0684     memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
0685 
0686     rcu_read_lock();
0687     sta = ieee80211_find_sta(vif, bss_conf->bssid);
0688     if (!sta) {
0689         rcu_read_unlock();
0690         return;
0691     }
0692     ath9k_htc_setup_rate(priv, sta, &trate);
0693     rcu_read_unlock();
0694 
0695     ret = ath9k_htc_send_rate_cmd(priv, &trate);
0696     if (!ret)
0697         ath_dbg(common, CONFIG,
0698             "Updated target sta: %pM, rate caps: 0x%X\n",
0699             bss_conf->bssid, be32_to_cpu(trate.capflags));
0700 }
0701 
0702 static int ath9k_htc_tx_aggr_oper(struct ath9k_htc_priv *priv,
0703                   struct ieee80211_vif *vif,
0704                   struct ieee80211_sta *sta,
0705                   enum ieee80211_ampdu_mlme_action action,
0706                   u16 tid)
0707 {
0708     struct ath_common *common = ath9k_hw_common(priv->ah);
0709     struct ath9k_htc_target_aggr aggr;
0710     struct ath9k_htc_sta *ista;
0711     int ret = 0;
0712     u8 cmd_rsp;
0713 
0714     if (tid >= ATH9K_HTC_MAX_TID)
0715         return -EINVAL;
0716 
0717     memset(&aggr, 0, sizeof(struct ath9k_htc_target_aggr));
0718     ista = (struct ath9k_htc_sta *) sta->drv_priv;
0719 
0720     aggr.sta_index = ista->index;
0721     aggr.tidno = tid & 0xf;
0722     aggr.aggr_enable = (action == IEEE80211_AMPDU_TX_START) ? true : false;
0723 
0724     WMI_CMD_BUF(WMI_TX_AGGR_ENABLE_CMDID, &aggr);
0725     if (ret)
0726         ath_dbg(common, CONFIG,
0727             "Unable to %s TX aggregation for (%pM, %d)\n",
0728             (aggr.aggr_enable) ? "start" : "stop", sta->addr, tid);
0729     else
0730         ath_dbg(common, CONFIG,
0731             "%s TX aggregation for (%pM, %d)\n",
0732             (aggr.aggr_enable) ? "Starting" : "Stopping",
0733             sta->addr, tid);
0734 
0735     spin_lock_bh(&priv->tx.tx_lock);
0736     ista->tid_state[tid] = (aggr.aggr_enable && !ret) ? AGGR_START : AGGR_STOP;
0737     spin_unlock_bh(&priv->tx.tx_lock);
0738 
0739     return ret;
0740 }
0741 
0742 /*******/
0743 /* ANI */
0744 /*******/
0745 
0746 void ath9k_htc_start_ani(struct ath9k_htc_priv *priv)
0747 {
0748     struct ath_common *common = ath9k_hw_common(priv->ah);
0749     unsigned long timestamp = jiffies_to_msecs(jiffies);
0750 
0751     common->ani.longcal_timer = timestamp;
0752     common->ani.shortcal_timer = timestamp;
0753     common->ani.checkani_timer = timestamp;
0754 
0755     set_bit(ATH_OP_ANI_RUN, &common->op_flags);
0756 
0757     ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
0758                      msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
0759 }
0760 
0761 void ath9k_htc_stop_ani(struct ath9k_htc_priv *priv)
0762 {
0763     struct ath_common *common = ath9k_hw_common(priv->ah);
0764     cancel_delayed_work_sync(&priv->ani_work);
0765     clear_bit(ATH_OP_ANI_RUN, &common->op_flags);
0766 }
0767 
0768 void ath9k_htc_ani_work(struct work_struct *work)
0769 {
0770     struct ath9k_htc_priv *priv =
0771         container_of(work, struct ath9k_htc_priv, ani_work.work);
0772     struct ath_hw *ah = priv->ah;
0773     struct ath_common *common = ath9k_hw_common(ah);
0774     bool longcal = false;
0775     bool shortcal = false;
0776     bool aniflag = false;
0777     unsigned int timestamp = jiffies_to_msecs(jiffies);
0778     u32 cal_interval, short_cal_interval;
0779 
0780     short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
0781         ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
0782 
0783     /* Only calibrate if awake */
0784     if (ah->power_mode != ATH9K_PM_AWAKE)
0785         goto set_timer;
0786 
0787     /* Long calibration runs independently of short calibration. */
0788     if ((timestamp - common->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
0789         longcal = true;
0790         ath_dbg(common, ANI, "longcal @%lu\n", jiffies);
0791         common->ani.longcal_timer = timestamp;
0792     }
0793 
0794     /*
0795      * Short calibration applies only while caldone
0796      * is false or -ETIMEDOUT
0797      */
0798     if (common->ani.caldone <= 0) {
0799         if ((timestamp - common->ani.shortcal_timer) >=
0800             short_cal_interval) {
0801             shortcal = true;
0802             ath_dbg(common, ANI, "shortcal @%lu\n", jiffies);
0803             common->ani.shortcal_timer = timestamp;
0804             common->ani.resetcal_timer = timestamp;
0805         }
0806     } else {
0807         if ((timestamp - common->ani.resetcal_timer) >=
0808             ATH_RESTART_CALINTERVAL) {
0809             common->ani.caldone = ath9k_hw_reset_calvalid(ah);
0810             if (common->ani.caldone)
0811                 common->ani.resetcal_timer = timestamp;
0812         }
0813     }
0814 
0815     /* Verify whether we must check ANI */
0816     if ((timestamp - common->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
0817         aniflag = true;
0818         common->ani.checkani_timer = timestamp;
0819     }
0820 
0821     /* Skip all processing if there's nothing to do. */
0822     if (longcal || shortcal || aniflag) {
0823 
0824         ath9k_htc_ps_wakeup(priv);
0825 
0826         /* Call ANI routine if necessary */
0827         if (aniflag)
0828             ath9k_hw_ani_monitor(ah, ah->curchan);
0829 
0830         /* Perform calibration if necessary */
0831         if (longcal || shortcal)
0832             common->ani.caldone =
0833                 ath9k_hw_calibrate(ah, ah->curchan,
0834                         ah->rxchainmask, longcal) > 0;
0835 
0836         ath9k_htc_ps_restore(priv);
0837     }
0838 
0839 set_timer:
0840     /*
0841     * Set timer interval based on previous results.
0842     * The interval must be the shortest necessary to satisfy ANI,
0843     * short calibration and long calibration.
0844     */
0845     cal_interval = ATH_LONG_CALINTERVAL;
0846     cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
0847     /*
0848      * Short calibration applies only while caldone
0849      * is false or -ETIMEDOUT
0850      */
0851     if (common->ani.caldone <= 0)
0852         cal_interval = min(cal_interval, (u32)short_cal_interval);
0853 
0854     ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
0855                      msecs_to_jiffies(cal_interval));
0856 }
0857 
0858 /**********************/
0859 /* mac80211 Callbacks */
0860 /**********************/
0861 
0862 static void ath9k_htc_tx(struct ieee80211_hw *hw,
0863              struct ieee80211_tx_control *control,
0864              struct sk_buff *skb)
0865 {
0866     struct ieee80211_hdr *hdr;
0867     struct ath9k_htc_priv *priv = hw->priv;
0868     struct ath_common *common = ath9k_hw_common(priv->ah);
0869     int padpos, padsize, ret, slot;
0870 
0871     hdr = (struct ieee80211_hdr *) skb->data;
0872 
0873     /* Add the padding after the header if this is not already done */
0874     padpos = ieee80211_hdrlen(hdr->frame_control);
0875     padsize = padpos & 3;
0876     if (padsize && skb->len > padpos) {
0877         if (skb_headroom(skb) < padsize) {
0878             ath_dbg(common, XMIT, "No room for padding\n");
0879             goto fail_tx;
0880         }
0881         skb_push(skb, padsize);
0882         memmove(skb->data, skb->data + padsize, padpos);
0883     }
0884 
0885     slot = ath9k_htc_tx_get_slot(priv);
0886     if (slot < 0) {
0887         ath_dbg(common, XMIT, "No free TX slot\n");
0888         goto fail_tx;
0889     }
0890 
0891     ret = ath9k_htc_tx_start(priv, control->sta, skb, slot, false);
0892     if (ret != 0) {
0893         ath_dbg(common, XMIT, "Tx failed\n");
0894         goto clear_slot;
0895     }
0896 
0897     ath9k_htc_check_stop_queues(priv);
0898 
0899     return;
0900 
0901 clear_slot:
0902     ath9k_htc_tx_clear_slot(priv, slot);
0903 fail_tx:
0904     dev_kfree_skb_any(skb);
0905 }
0906 
0907 static int ath9k_htc_start(struct ieee80211_hw *hw)
0908 {
0909     struct ath9k_htc_priv *priv = hw->priv;
0910     struct ath_hw *ah = priv->ah;
0911     struct ath_common *common = ath9k_hw_common(ah);
0912     struct ieee80211_channel *curchan = hw->conf.chandef.chan;
0913     struct ath9k_channel *init_channel;
0914     int ret = 0;
0915     enum htc_phymode mode;
0916     __be16 htc_mode;
0917     u8 cmd_rsp;
0918 
0919     mutex_lock(&priv->mutex);
0920 
0921     ath_dbg(common, CONFIG,
0922         "Starting driver with initial channel: %d MHz\n",
0923         curchan->center_freq);
0924 
0925     /* Ensure that HW is awake before flushing RX */
0926     ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
0927     WMI_CMD(WMI_FLUSH_RECV_CMDID);
0928 
0929     /* setup initial channel */
0930     init_channel = ath9k_cmn_get_channel(hw, ah, &hw->conf.chandef);
0931 
0932     ret = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
0933     if (ret) {
0934         ath_err(common,
0935             "Unable to reset hardware; reset status %d (freq %u MHz)\n",
0936             ret, curchan->center_freq);
0937         mutex_unlock(&priv->mutex);
0938         return ret;
0939     }
0940 
0941     ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
0942                    &priv->curtxpow);
0943 
0944     mode = ath9k_htc_get_curmode(priv, init_channel);
0945     htc_mode = cpu_to_be16(mode);
0946     WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
0947     WMI_CMD(WMI_ATH_INIT_CMDID);
0948     WMI_CMD(WMI_START_RECV_CMDID);
0949 
0950     ath9k_host_rx_init(priv);
0951 
0952     ret = ath9k_htc_update_cap_target(priv, 0);
0953     if (ret)
0954         ath_dbg(common, CONFIG,
0955             "Failed to update capability in target\n");
0956 
0957     clear_bit(ATH_OP_INVALID, &common->op_flags);
0958     htc_start(priv->htc);
0959 
0960     spin_lock_bh(&priv->tx.tx_lock);
0961     priv->tx.flags &= ~ATH9K_HTC_OP_TX_QUEUES_STOP;
0962     spin_unlock_bh(&priv->tx.tx_lock);
0963 
0964     ieee80211_wake_queues(hw);
0965 
0966     mod_timer(&priv->tx.cleanup_timer,
0967           jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
0968 
0969     ath9k_htc_start_btcoex(priv);
0970 
0971     mutex_unlock(&priv->mutex);
0972 
0973     return ret;
0974 }
0975 
0976 static void ath9k_htc_stop(struct ieee80211_hw *hw)
0977 {
0978     struct ath9k_htc_priv *priv = hw->priv;
0979     struct ath_hw *ah = priv->ah;
0980     struct ath_common *common = ath9k_hw_common(ah);
0981     int ret __attribute__ ((unused));
0982     u8 cmd_rsp;
0983 
0984     mutex_lock(&priv->mutex);
0985 
0986     if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
0987         ath_dbg(common, ANY, "Device not present\n");
0988         mutex_unlock(&priv->mutex);
0989         return;
0990     }
0991 
0992     ath9k_htc_ps_wakeup(priv);
0993 
0994     WMI_CMD(WMI_DISABLE_INTR_CMDID);
0995     WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
0996     WMI_CMD(WMI_STOP_RECV_CMDID);
0997 
0998     tasklet_kill(&priv->rx_tasklet);
0999 
1000     del_timer_sync(&priv->tx.cleanup_timer);
1001     ath9k_htc_tx_drain(priv);
1002     ath9k_wmi_event_drain(priv);
1003 
1004     mutex_unlock(&priv->mutex);
1005 
1006     /* Cancel all the running timers/work .. */
1007     cancel_work_sync(&priv->fatal_work);
1008     cancel_work_sync(&priv->ps_work);
1009 
1010 #ifdef CONFIG_MAC80211_LEDS
1011     cancel_work_sync(&priv->led_work);
1012 #endif
1013     ath9k_htc_stop_ani(priv);
1014 
1015     mutex_lock(&priv->mutex);
1016 
1017     ath9k_htc_stop_btcoex(priv);
1018 
1019     /* Remove a monitor interface if it's present. */
1020     if (priv->ah->is_monitoring)
1021         ath9k_htc_remove_monitor_interface(priv);
1022 
1023     ath9k_hw_phy_disable(ah);
1024     ath9k_hw_disable(ah);
1025     ath9k_htc_ps_restore(priv);
1026     ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);
1027 
1028     set_bit(ATH_OP_INVALID, &common->op_flags);
1029 
1030     ath_dbg(common, CONFIG, "Driver halt\n");
1031     mutex_unlock(&priv->mutex);
1032 }
1033 
1034 static int ath9k_htc_add_interface(struct ieee80211_hw *hw,
1035                    struct ieee80211_vif *vif)
1036 {
1037     struct ath9k_htc_priv *priv = hw->priv;
1038     struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1039     struct ath_common *common = ath9k_hw_common(priv->ah);
1040     struct ath9k_htc_target_vif hvif;
1041     int ret = 0;
1042     u8 cmd_rsp;
1043 
1044     mutex_lock(&priv->mutex);
1045 
1046     ath9k_htc_ps_wakeup(priv);
1047     memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1048     memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1049 
1050     switch (vif->type) {
1051     case NL80211_IFTYPE_STATION:
1052         hvif.opmode = HTC_M_STA;
1053         break;
1054     case NL80211_IFTYPE_ADHOC:
1055         hvif.opmode = HTC_M_IBSS;
1056         break;
1057     case NL80211_IFTYPE_AP:
1058         hvif.opmode = HTC_M_HOSTAP;
1059         break;
1060     case NL80211_IFTYPE_MESH_POINT:
1061         hvif.opmode = HTC_M_WDS;    /* close enough */
1062         break;
1063     default:
1064         ath_err(common,
1065             "Interface type %d not yet supported\n", vif->type);
1066         ret = -EOPNOTSUPP;
1067         goto out;
1068     }
1069 
1070     /* Index starts from zero on the target */
1071     avp->index = hvif.index = ffz(priv->vif_slot);
1072     hvif.rtsthreshold = cpu_to_be16(2304);
1073     WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
1074     if (ret)
1075         goto out;
1076 
1077     /*
1078      * We need a node in target to tx mgmt frames
1079      * before association.
1080      */
1081     ret = ath9k_htc_add_station(priv, vif, NULL);
1082     if (ret) {
1083         WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1084         goto out;
1085     }
1086 
1087     ath9k_htc_set_mac_bssid_mask(priv, vif);
1088 
1089     priv->vif_slot |= (1 << avp->index);
1090     priv->nvifs++;
1091 
1092     INC_VIF(priv, vif->type);
1093 
1094     if ((vif->type == NL80211_IFTYPE_AP) ||
1095         (vif->type == NL80211_IFTYPE_MESH_POINT) ||
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(ATH_OP_ANI_RUN, &common->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     if (priv->csa_vif == vif)
1142         priv->csa_vif = NULL;
1143 
1144     ath9k_htc_remove_station(priv, vif, NULL);
1145 
1146     DEC_VIF(priv, vif->type);
1147 
1148     if ((vif->type == NL80211_IFTYPE_AP) ||
1149          vif->type == NL80211_IFTYPE_MESH_POINT ||
1150         (vif->type == NL80211_IFTYPE_ADHOC))
1151         ath9k_htc_remove_bslot(priv, vif);
1152 
1153     ath9k_htc_set_opmode(priv);
1154 
1155     ath9k_htc_set_mac_bssid_mask(priv, vif);
1156 
1157     /*
1158      * Stop ANI only if there are no associated station interfaces.
1159      */
1160     if ((vif->type == NL80211_IFTYPE_AP) && (priv->num_ap_vif == 0)) {
1161         priv->rearm_ani = false;
1162         ieee80211_iterate_active_interfaces_atomic(
1163             priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1164             ath9k_htc_vif_iter, priv);
1165         if (!priv->rearm_ani)
1166             ath9k_htc_stop_ani(priv);
1167     }
1168 
1169     ath_dbg(common, CONFIG, "Detach Interface at idx: %d\n", avp->index);
1170 
1171     ath9k_htc_ps_restore(priv);
1172     mutex_unlock(&priv->mutex);
1173 }
1174 
1175 static int ath9k_htc_config(struct ieee80211_hw *hw, u32 changed)
1176 {
1177     struct ath9k_htc_priv *priv = hw->priv;
1178     struct ath_common *common = ath9k_hw_common(priv->ah);
1179     struct ieee80211_conf *conf = &hw->conf;
1180     bool chip_reset = false;
1181     int ret = 0;
1182 
1183     mutex_lock(&priv->mutex);
1184     ath9k_htc_ps_wakeup(priv);
1185 
1186     if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1187         mutex_lock(&priv->htc_pm_lock);
1188 
1189         priv->ps_idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1190         if (!priv->ps_idle)
1191             chip_reset = true;
1192 
1193         mutex_unlock(&priv->htc_pm_lock);
1194     }
1195 
1196     /*
1197      * Monitor interface should be added before
1198      * IEEE80211_CONF_CHANGE_CHANNEL is handled.
1199      */
1200     if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1201         if ((conf->flags & IEEE80211_CONF_MONITOR) &&
1202             !priv->ah->is_monitoring)
1203             ath9k_htc_add_monitor_interface(priv);
1204         else if (priv->ah->is_monitoring)
1205             ath9k_htc_remove_monitor_interface(priv);
1206     }
1207 
1208     if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) || chip_reset) {
1209         struct ieee80211_channel *curchan = hw->conf.chandef.chan;
1210         int pos = curchan->hw_value;
1211 
1212         ath_dbg(common, CONFIG, "Set channel: %d MHz\n",
1213             curchan->center_freq);
1214 
1215         ath9k_cmn_get_channel(hw, priv->ah, &hw->conf.chandef);
1216         if (ath9k_htc_set_channel(priv, hw, &priv->ah->channels[pos]) < 0) {
1217             ath_err(common, "Unable to set channel\n");
1218             ret = -EINVAL;
1219             goto out;
1220         }
1221 
1222     }
1223 
1224     if (changed & IEEE80211_CONF_CHANGE_PS) {
1225         if (conf->flags & IEEE80211_CONF_PS) {
1226             ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
1227             priv->ps_enabled = true;
1228         } else {
1229             priv->ps_enabled = false;
1230             cancel_work_sync(&priv->ps_work);
1231             ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1232         }
1233     }
1234 
1235     if (changed & IEEE80211_CONF_CHANGE_POWER) {
1236         priv->txpowlimit = 2 * conf->power_level;
1237         ath9k_cmn_update_txpow(priv->ah, priv->curtxpow,
1238                        priv->txpowlimit, &priv->curtxpow);
1239     }
1240 
1241 out:
1242     ath9k_htc_ps_restore(priv);
1243     mutex_unlock(&priv->mutex);
1244     return ret;
1245 }
1246 
1247 #define SUPPORTED_FILTERS           \
1248     (FIF_ALLMULTI |             \
1249     FIF_CONTROL |               \
1250     FIF_PSPOLL |                \
1251     FIF_OTHER_BSS |             \
1252     FIF_BCN_PRBRESP_PROMISC |       \
1253     FIF_PROBE_REQ |             \
1254     FIF_MCAST_ACTION |          \
1255     FIF_FCSFAIL)
1256 
1257 static void ath9k_htc_configure_filter(struct ieee80211_hw *hw,
1258                        unsigned int changed_flags,
1259                        unsigned int *total_flags,
1260                        u64 multicast)
1261 {
1262     struct ath9k_htc_priv *priv = hw->priv;
1263     struct ath_common *common = ath9k_hw_common(priv->ah);
1264     u32 rfilt;
1265 
1266     mutex_lock(&priv->mutex);
1267     changed_flags &= SUPPORTED_FILTERS;
1268     *total_flags &= SUPPORTED_FILTERS;
1269 
1270     if (test_bit(ATH_OP_INVALID, &common->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 void ath9k_htc_sta_rc_update_work(struct work_struct *work)
1290 {
1291     struct ath9k_htc_sta *ista =
1292         container_of(work, struct ath9k_htc_sta, rc_update_work);
1293     struct ieee80211_sta *sta =
1294         container_of((void *)ista, struct ieee80211_sta, drv_priv);
1295     struct ath9k_htc_priv *priv = ista->htc_priv;
1296     struct ath_common *common = ath9k_hw_common(priv->ah);
1297     struct ath9k_htc_target_rate trate;
1298 
1299     mutex_lock(&priv->mutex);
1300     ath9k_htc_ps_wakeup(priv);
1301 
1302     memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
1303     ath9k_htc_setup_rate(priv, sta, &trate);
1304     if (!ath9k_htc_send_rate_cmd(priv, &trate))
1305         ath_dbg(common, CONFIG,
1306             "Supported rates for sta: %pM updated, rate caps: 0x%X\n",
1307             sta->addr, be32_to_cpu(trate.capflags));
1308     else
1309         ath_dbg(common, CONFIG,
1310             "Unable to update supported rates for sta: %pM\n",
1311             sta->addr);
1312 
1313     ath9k_htc_ps_restore(priv);
1314     mutex_unlock(&priv->mutex);
1315 }
1316 
1317 static int ath9k_htc_sta_add(struct ieee80211_hw *hw,
1318                  struct ieee80211_vif *vif,
1319                  struct ieee80211_sta *sta)
1320 {
1321     struct ath9k_htc_priv *priv = hw->priv;
1322     struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
1323     int ret;
1324 
1325     mutex_lock(&priv->mutex);
1326     ath9k_htc_ps_wakeup(priv);
1327     ret = ath9k_htc_add_station(priv, vif, sta);
1328     if (!ret) {
1329         INIT_WORK(&ista->rc_update_work, ath9k_htc_sta_rc_update_work);
1330         ista->htc_priv = priv;
1331         ath9k_htc_init_rate(priv, sta);
1332     }
1333     ath9k_htc_ps_restore(priv);
1334     mutex_unlock(&priv->mutex);
1335 
1336     return ret;
1337 }
1338 
1339 static int ath9k_htc_sta_remove(struct ieee80211_hw *hw,
1340                 struct ieee80211_vif *vif,
1341                 struct ieee80211_sta *sta)
1342 {
1343     struct ath9k_htc_priv *priv = hw->priv;
1344     struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
1345     int ret;
1346 
1347     cancel_work_sync(&ista->rc_update_work);
1348 
1349     mutex_lock(&priv->mutex);
1350     ath9k_htc_ps_wakeup(priv);
1351     htc_sta_drain(priv->htc, ista->index);
1352     ret = ath9k_htc_remove_station(priv, vif, sta);
1353     ath9k_htc_ps_restore(priv);
1354     mutex_unlock(&priv->mutex);
1355 
1356     return ret;
1357 }
1358 
1359 static void ath9k_htc_sta_rc_update(struct ieee80211_hw *hw,
1360                     struct ieee80211_vif *vif,
1361                     struct ieee80211_sta *sta, u32 changed)
1362 {
1363     struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
1364 
1365     if (!(changed & IEEE80211_RC_SUPP_RATES_CHANGED))
1366         return;
1367 
1368     schedule_work(&ista->rc_update_work);
1369 }
1370 
1371 static int ath9k_htc_conf_tx(struct ieee80211_hw *hw,
1372                  struct ieee80211_vif *vif,
1373                  unsigned int link_id, u16 queue,
1374                  const struct ieee80211_tx_queue_params *params)
1375 {
1376     struct ath9k_htc_priv *priv = hw->priv;
1377     struct ath_common *common = ath9k_hw_common(priv->ah);
1378     struct ath9k_tx_queue_info qi;
1379     int ret = 0, qnum;
1380 
1381     if (queue >= IEEE80211_NUM_ACS)
1382         return 0;
1383 
1384     mutex_lock(&priv->mutex);
1385     ath9k_htc_ps_wakeup(priv);
1386 
1387     memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1388 
1389     qi.tqi_aifs = params->aifs;
1390     qi.tqi_cwmin = params->cw_min;
1391     qi.tqi_cwmax = params->cw_max;
1392     qi.tqi_burstTime = params->txop * 32;
1393 
1394     qnum = get_hw_qnum(queue, priv->hwq_map);
1395 
1396     ath_dbg(common, CONFIG,
1397         "Configure tx [queue/hwq] [%d/%d],  aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1398         queue, qnum, params->aifs, params->cw_min,
1399         params->cw_max, params->txop);
1400 
1401     ret = ath_htc_txq_update(priv, qnum, &qi);
1402     if (ret) {
1403         ath_err(common, "TXQ Update failed\n");
1404         goto out;
1405     }
1406 
1407     if ((priv->ah->opmode == NL80211_IFTYPE_ADHOC) &&
1408         (qnum == priv->hwq_map[IEEE80211_AC_BE]))
1409             ath9k_htc_beaconq_config(priv);
1410 out:
1411     ath9k_htc_ps_restore(priv);
1412     mutex_unlock(&priv->mutex);
1413 
1414     return ret;
1415 }
1416 
1417 static int ath9k_htc_set_key(struct ieee80211_hw *hw,
1418                  enum set_key_cmd cmd,
1419                  struct ieee80211_vif *vif,
1420                  struct ieee80211_sta *sta,
1421                  struct ieee80211_key_conf *key)
1422 {
1423     struct ath9k_htc_priv *priv = hw->priv;
1424     struct ath_common *common = ath9k_hw_common(priv->ah);
1425     int ret = 0;
1426 
1427     if (htc_modparam_nohwcrypt)
1428         return -ENOSPC;
1429 
1430     if ((vif->type == NL80211_IFTYPE_ADHOC ||
1431          vif->type == NL80211_IFTYPE_MESH_POINT) &&
1432         (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
1433          key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
1434         !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1435         /*
1436          * For now, disable hw crypto for the RSN IBSS group keys. This
1437          * could be optimized in the future to use a modified key cache
1438          * design to support per-STA RX GTK, but until that gets
1439          * implemented, use of software crypto for group addressed
1440          * frames is a acceptable to allow RSN IBSS to be used.
1441          */
1442         return -EOPNOTSUPP;
1443     }
1444 
1445     mutex_lock(&priv->mutex);
1446     ath_dbg(common, CONFIG, "Set HW Key\n");
1447     ath9k_htc_ps_wakeup(priv);
1448 
1449     switch (cmd) {
1450     case SET_KEY:
1451         ret = ath_key_config(common, vif, sta, key);
1452         if (ret >= 0) {
1453             key->hw_key_idx = ret;
1454             /* push IV and Michael MIC generation to stack */
1455             key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1456             if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1457                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1458             if (priv->ah->sw_mgmt_crypto_tx &&
1459                 key->cipher == WLAN_CIPHER_SUITE_CCMP)
1460                 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
1461             ret = 0;
1462         }
1463         break;
1464     case DISABLE_KEY:
1465         ath_key_delete(common, key->hw_key_idx);
1466         break;
1467     default:
1468         ret = -EINVAL;
1469     }
1470 
1471     ath9k_htc_ps_restore(priv);
1472     mutex_unlock(&priv->mutex);
1473 
1474     return ret;
1475 }
1476 
1477 static void ath9k_htc_set_bssid(struct ath9k_htc_priv *priv)
1478 {
1479     struct ath_common *common = ath9k_hw_common(priv->ah);
1480 
1481     ath9k_hw_write_associd(priv->ah);
1482     ath_dbg(common, CONFIG, "BSSID: %pM aid: 0x%x\n",
1483         common->curbssid, common->curaid);
1484 }
1485 
1486 static void ath9k_htc_bss_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
1487 {
1488     struct ath9k_htc_priv *priv = data;
1489     struct ath_common *common = ath9k_hw_common(priv->ah);
1490     struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1491 
1492     if ((vif->type == NL80211_IFTYPE_STATION) && vif->cfg.assoc) {
1493         common->curaid = vif->cfg.aid;
1494         common->last_rssi = ATH_RSSI_DUMMY_MARKER;
1495         memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1496         set_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
1497     }
1498 }
1499 
1500 static void ath9k_htc_choose_set_bssid(struct ath9k_htc_priv *priv)
1501 {
1502     if (priv->num_sta_assoc_vif == 1) {
1503         ieee80211_iterate_active_interfaces_atomic(
1504             priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1505             ath9k_htc_bss_iter, priv);
1506         ath9k_htc_set_bssid(priv);
1507     }
1508 }
1509 
1510 static void ath9k_htc_bss_info_changed(struct ieee80211_hw *hw,
1511                        struct ieee80211_vif *vif,
1512                        struct ieee80211_bss_conf *bss_conf,
1513                        u64 changed)
1514 {
1515     struct ath9k_htc_priv *priv = hw->priv;
1516     struct ath_hw *ah = priv->ah;
1517     struct ath_common *common = ath9k_hw_common(ah);
1518     int slottime;
1519 
1520     mutex_lock(&priv->mutex);
1521     ath9k_htc_ps_wakeup(priv);
1522 
1523     if (changed & BSS_CHANGED_ASSOC) {
1524         ath_dbg(common, CONFIG, "BSS Changed ASSOC %d\n",
1525             vif->cfg.assoc);
1526 
1527         vif->cfg.assoc ?
1528             priv->num_sta_assoc_vif++ : priv->num_sta_assoc_vif--;
1529 
1530         if (!vif->cfg.assoc)
1531             clear_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
1532 
1533         if (priv->ah->opmode == NL80211_IFTYPE_STATION) {
1534             ath9k_htc_choose_set_bssid(priv);
1535             if (vif->cfg.assoc && (priv->num_sta_assoc_vif == 1))
1536                 ath9k_htc_start_ani(priv);
1537             else if (priv->num_sta_assoc_vif == 0)
1538                 ath9k_htc_stop_ani(priv);
1539         }
1540     }
1541 
1542     if (changed & BSS_CHANGED_IBSS) {
1543         if (priv->ah->opmode == NL80211_IFTYPE_ADHOC) {
1544             common->curaid = vif->cfg.aid;
1545             memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1546             ath9k_htc_set_bssid(priv);
1547         }
1548     }
1549 
1550     if ((changed & BSS_CHANGED_BEACON_ENABLED) && bss_conf->enable_beacon) {
1551         ath_dbg(common, CONFIG, "Beacon enabled for BSS: %pM\n",
1552             bss_conf->bssid);
1553         ath9k_htc_set_tsfadjust(priv, vif);
1554         priv->cur_beacon_conf.enable_beacon = 1;
1555         ath9k_htc_beacon_config(priv, vif);
1556     }
1557 
1558     if ((changed & BSS_CHANGED_BEACON_ENABLED) && !bss_conf->enable_beacon) {
1559         /*
1560          * Disable SWBA interrupt only if there are no
1561          * concurrent AP/mesh or IBSS interfaces.
1562          */
1563         if ((priv->num_ap_vif + priv->num_mbss_vif <= 1) ||
1564              priv->num_ibss_vif) {
1565             ath_dbg(common, CONFIG,
1566                 "Beacon disabled for BSS: %pM\n",
1567                 bss_conf->bssid);
1568             priv->cur_beacon_conf.enable_beacon = 0;
1569             ath9k_htc_beacon_config(priv, vif);
1570         }
1571     }
1572 
1573     if (changed & BSS_CHANGED_BEACON_INT) {
1574         /*
1575          * Reset the HW TSF for the first AP or mesh interface.
1576          */
1577         if (priv->nvifs == 1 &&
1578             ((priv->ah->opmode == NL80211_IFTYPE_AP &&
1579               vif->type == NL80211_IFTYPE_AP &&
1580               priv->num_ap_vif == 1) ||
1581             (priv->ah->opmode == NL80211_IFTYPE_MESH_POINT &&
1582               vif->type == NL80211_IFTYPE_MESH_POINT &&
1583               priv->num_mbss_vif == 1))) {
1584             set_bit(OP_TSF_RESET, &priv->op_flags);
1585         }
1586         ath_dbg(common, CONFIG,
1587             "Beacon interval changed for BSS: %pM\n",
1588             bss_conf->bssid);
1589         ath9k_htc_beacon_config(priv, vif);
1590     }
1591 
1592     if (changed & BSS_CHANGED_ERP_SLOT) {
1593         if (bss_conf->use_short_slot)
1594             slottime = 9;
1595         else
1596             slottime = 20;
1597         if (vif->type == NL80211_IFTYPE_AP) {
1598             /*
1599              * Defer update, so that connected stations can adjust
1600              * their settings at the same time.
1601              * See beacon.c for more details
1602              */
1603             priv->beacon.slottime = slottime;
1604             priv->beacon.updateslot = UPDATE;
1605         } else {
1606             ah->slottime = slottime;
1607             ath9k_hw_init_global_settings(ah);
1608         }
1609     }
1610 
1611     if (changed & BSS_CHANGED_HT)
1612         ath9k_htc_update_rate(priv, vif, bss_conf);
1613 
1614     ath9k_htc_ps_restore(priv);
1615     mutex_unlock(&priv->mutex);
1616 }
1617 
1618 static u64 ath9k_htc_get_tsf(struct ieee80211_hw *hw,
1619                  struct ieee80211_vif *vif)
1620 {
1621     struct ath9k_htc_priv *priv = hw->priv;
1622     u64 tsf;
1623 
1624     mutex_lock(&priv->mutex);
1625     ath9k_htc_ps_wakeup(priv);
1626     tsf = ath9k_hw_gettsf64(priv->ah);
1627     ath9k_htc_ps_restore(priv);
1628     mutex_unlock(&priv->mutex);
1629 
1630     return tsf;
1631 }
1632 
1633 static void ath9k_htc_set_tsf(struct ieee80211_hw *hw,
1634                   struct ieee80211_vif *vif, u64 tsf)
1635 {
1636     struct ath9k_htc_priv *priv = hw->priv;
1637 
1638     mutex_lock(&priv->mutex);
1639     ath9k_htc_ps_wakeup(priv);
1640     ath9k_hw_settsf64(priv->ah, tsf);
1641     ath9k_htc_ps_restore(priv);
1642     mutex_unlock(&priv->mutex);
1643 }
1644 
1645 static void ath9k_htc_reset_tsf(struct ieee80211_hw *hw,
1646                 struct ieee80211_vif *vif)
1647 {
1648     struct ath9k_htc_priv *priv = hw->priv;
1649 
1650     mutex_lock(&priv->mutex);
1651     ath9k_htc_ps_wakeup(priv);
1652     ath9k_hw_reset_tsf(priv->ah);
1653     ath9k_htc_ps_restore(priv);
1654     mutex_unlock(&priv->mutex);
1655 }
1656 
1657 static int ath9k_htc_ampdu_action(struct ieee80211_hw *hw,
1658                   struct ieee80211_vif *vif,
1659                   struct ieee80211_ampdu_params *params)
1660 {
1661     struct ath9k_htc_priv *priv = hw->priv;
1662     struct ath9k_htc_sta *ista;
1663     int ret = 0;
1664     struct ieee80211_sta *sta = params->sta;
1665     enum ieee80211_ampdu_mlme_action action = params->action;
1666     u16 tid = params->tid;
1667 
1668     mutex_lock(&priv->mutex);
1669     ath9k_htc_ps_wakeup(priv);
1670 
1671     switch (action) {
1672     case IEEE80211_AMPDU_RX_START:
1673         break;
1674     case IEEE80211_AMPDU_RX_STOP:
1675         break;
1676     case IEEE80211_AMPDU_TX_START:
1677         ret = ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1678         if (!ret)
1679             ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
1680         break;
1681     case IEEE80211_AMPDU_TX_STOP_CONT:
1682     case IEEE80211_AMPDU_TX_STOP_FLUSH:
1683     case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1684         ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1685         ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1686         break;
1687     case IEEE80211_AMPDU_TX_OPERATIONAL:
1688         if (tid >= ATH9K_HTC_MAX_TID) {
1689             ret = -EINVAL;
1690             break;
1691         }
1692         ista = (struct ath9k_htc_sta *) sta->drv_priv;
1693         spin_lock_bh(&priv->tx.tx_lock);
1694         ista->tid_state[tid] = AGGR_OPERATIONAL;
1695         spin_unlock_bh(&priv->tx.tx_lock);
1696         break;
1697     default:
1698         ath_err(ath9k_hw_common(priv->ah), "Unknown AMPDU action\n");
1699     }
1700 
1701     ath9k_htc_ps_restore(priv);
1702     mutex_unlock(&priv->mutex);
1703 
1704     return ret;
1705 }
1706 
1707 static void ath9k_htc_sw_scan_start(struct ieee80211_hw *hw,
1708                     struct ieee80211_vif *vif,
1709                     const u8 *mac_addr)
1710 {
1711     struct ath9k_htc_priv *priv = hw->priv;
1712     struct ath_common *common = ath9k_hw_common(priv->ah);
1713 
1714     mutex_lock(&priv->mutex);
1715     spin_lock_bh(&priv->beacon_lock);
1716     set_bit(ATH_OP_SCANNING, &common->op_flags);
1717     spin_unlock_bh(&priv->beacon_lock);
1718     cancel_work_sync(&priv->ps_work);
1719     ath9k_htc_stop_ani(priv);
1720     mutex_unlock(&priv->mutex);
1721 }
1722 
1723 static void ath9k_htc_sw_scan_complete(struct ieee80211_hw *hw,
1724                        struct ieee80211_vif *vif)
1725 {
1726     struct ath9k_htc_priv *priv = hw->priv;
1727     struct ath_common *common = ath9k_hw_common(priv->ah);
1728 
1729     mutex_lock(&priv->mutex);
1730     spin_lock_bh(&priv->beacon_lock);
1731     clear_bit(ATH_OP_SCANNING, &common->op_flags);
1732     spin_unlock_bh(&priv->beacon_lock);
1733     ath9k_htc_ps_wakeup(priv);
1734     ath9k_htc_vif_reconfig(priv);
1735     ath9k_htc_ps_restore(priv);
1736     mutex_unlock(&priv->mutex);
1737 }
1738 
1739 static int ath9k_htc_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1740 {
1741     return 0;
1742 }
1743 
1744 static void ath9k_htc_set_coverage_class(struct ieee80211_hw *hw,
1745                      s16 coverage_class)
1746 {
1747     struct ath9k_htc_priv *priv = hw->priv;
1748 
1749     mutex_lock(&priv->mutex);
1750     ath9k_htc_ps_wakeup(priv);
1751     priv->ah->coverage_class = coverage_class;
1752     ath9k_hw_init_global_settings(priv->ah);
1753     ath9k_htc_ps_restore(priv);
1754     mutex_unlock(&priv->mutex);
1755 }
1756 
1757 /*
1758  * Currently, this is used only for selecting the minimum rate
1759  * for management frames, rate selection for data frames remain
1760  * unaffected.
1761  */
1762 static int ath9k_htc_set_bitrate_mask(struct ieee80211_hw *hw,
1763                       struct ieee80211_vif *vif,
1764                       const struct cfg80211_bitrate_mask *mask)
1765 {
1766     struct ath9k_htc_priv *priv = hw->priv;
1767     struct ath_common *common = ath9k_hw_common(priv->ah);
1768     struct ath9k_htc_target_rate_mask tmask;
1769     struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1770     int ret = 0;
1771     u8 cmd_rsp;
1772 
1773     memset(&tmask, 0, sizeof(struct ath9k_htc_target_rate_mask));
1774 
1775     tmask.vif_index = avp->index;
1776     tmask.band = NL80211_BAND_2GHZ;
1777     tmask.mask = cpu_to_be32(mask->control[NL80211_BAND_2GHZ].legacy);
1778 
1779     WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
1780     if (ret) {
1781         ath_err(common,
1782             "Unable to set 2G rate mask for "
1783             "interface at idx: %d\n", avp->index);
1784         goto out;
1785     }
1786 
1787     tmask.band = NL80211_BAND_5GHZ;
1788     tmask.mask = cpu_to_be32(mask->control[NL80211_BAND_5GHZ].legacy);
1789 
1790     WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
1791     if (ret) {
1792         ath_err(common,
1793             "Unable to set 5G rate mask for "
1794             "interface at idx: %d\n", avp->index);
1795         goto out;
1796     }
1797 
1798     ath_dbg(common, CONFIG, "Set bitrate masks: 0x%x, 0x%x\n",
1799         mask->control[NL80211_BAND_2GHZ].legacy,
1800         mask->control[NL80211_BAND_5GHZ].legacy);
1801 out:
1802     return ret;
1803 }
1804 
1805 
1806 static int ath9k_htc_get_stats(struct ieee80211_hw *hw,
1807                    struct ieee80211_low_level_stats *stats)
1808 {
1809     struct ath9k_htc_priv *priv = hw->priv;
1810     struct ath_hw *ah = priv->ah;
1811     struct ath9k_mib_stats *mib_stats = &ah->ah_mibStats;
1812 
1813     stats->dot11ACKFailureCount = mib_stats->ackrcv_bad;
1814     stats->dot11RTSFailureCount = mib_stats->rts_bad;
1815     stats->dot11FCSErrorCount = mib_stats->fcs_bad;
1816     stats->dot11RTSSuccessCount = mib_stats->rts_good;
1817 
1818     return 0;
1819 }
1820 
1821 struct base_eep_header *ath9k_htc_get_eeprom_base(struct ath9k_htc_priv *priv)
1822 {
1823     struct base_eep_header *pBase = NULL;
1824     /*
1825      * This can be done since all the 3 EEPROM families have the
1826      * same base header upto a certain point, and we are interested in
1827      * the data only upto that point.
1828      */
1829 
1830     if (AR_SREV_9271(priv->ah))
1831         pBase = (struct base_eep_header *)
1832             &priv->ah->eeprom.map4k.baseEepHeader;
1833     else if (priv->ah->hw_version.usbdev == AR9280_USB)
1834         pBase = (struct base_eep_header *)
1835             &priv->ah->eeprom.def.baseEepHeader;
1836     else if (priv->ah->hw_version.usbdev == AR9287_USB)
1837         pBase = (struct base_eep_header *)
1838             &priv->ah->eeprom.map9287.baseEepHeader;
1839     return pBase;
1840 }
1841 
1842 
1843 static int ath9k_htc_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant,
1844                  u32 *rx_ant)
1845 {
1846     struct ath9k_htc_priv *priv = hw->priv;
1847     struct base_eep_header *pBase = ath9k_htc_get_eeprom_base(priv);
1848     if (pBase) {
1849         *tx_ant = pBase->txMask;
1850         *rx_ant = pBase->rxMask;
1851     } else {
1852         *tx_ant = 0;
1853         *rx_ant = 0;
1854     }
1855     return 0;
1856 }
1857 
1858 static void ath9k_htc_channel_switch_beacon(struct ieee80211_hw *hw,
1859                         struct ieee80211_vif *vif,
1860                         struct cfg80211_chan_def *chandef)
1861 {
1862     struct ath9k_htc_priv *priv = hw->priv;
1863 
1864     /* mac80211 does not support CSA in multi-if cases (yet) */
1865     if (WARN_ON(priv->csa_vif))
1866         return;
1867 
1868     priv->csa_vif = vif;
1869 }
1870 
1871 struct ieee80211_ops ath9k_htc_ops = {
1872     .tx                 = ath9k_htc_tx,
1873     .start              = ath9k_htc_start,
1874     .stop               = ath9k_htc_stop,
1875     .add_interface      = ath9k_htc_add_interface,
1876     .remove_interface   = ath9k_htc_remove_interface,
1877     .config             = ath9k_htc_config,
1878     .configure_filter   = ath9k_htc_configure_filter,
1879     .sta_add            = ath9k_htc_sta_add,
1880     .sta_remove         = ath9k_htc_sta_remove,
1881     .conf_tx            = ath9k_htc_conf_tx,
1882     .sta_rc_update      = ath9k_htc_sta_rc_update,
1883     .bss_info_changed   = ath9k_htc_bss_info_changed,
1884     .set_key            = ath9k_htc_set_key,
1885     .get_tsf            = ath9k_htc_get_tsf,
1886     .set_tsf            = ath9k_htc_set_tsf,
1887     .reset_tsf          = ath9k_htc_reset_tsf,
1888     .ampdu_action       = ath9k_htc_ampdu_action,
1889     .sw_scan_start      = ath9k_htc_sw_scan_start,
1890     .sw_scan_complete   = ath9k_htc_sw_scan_complete,
1891     .set_rts_threshold  = ath9k_htc_set_rts_threshold,
1892     .rfkill_poll        = ath9k_htc_rfkill_poll_state,
1893     .set_coverage_class = ath9k_htc_set_coverage_class,
1894     .set_bitrate_mask   = ath9k_htc_set_bitrate_mask,
1895     .get_stats      = ath9k_htc_get_stats,
1896     .get_antenna        = ath9k_htc_get_antenna,
1897     .channel_switch_beacon  = ath9k_htc_channel_switch_beacon,
1898 
1899 #ifdef CONFIG_ATH9K_HTC_DEBUGFS
1900     .get_et_sset_count  = ath9k_htc_get_et_sset_count,
1901     .get_et_stats       = ath9k_htc_get_et_stats,
1902     .get_et_strings     = ath9k_htc_get_et_strings,
1903 #endif
1904 };