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 /* TX */
0021 /******/
0022 
0023 static const int subtype_txq_to_hwq[] = {
0024     [IEEE80211_AC_BE] = ATH_TXQ_AC_BE,
0025     [IEEE80211_AC_BK] = ATH_TXQ_AC_BK,
0026     [IEEE80211_AC_VI] = ATH_TXQ_AC_VI,
0027     [IEEE80211_AC_VO] = ATH_TXQ_AC_VO,
0028 };
0029 
0030 #define ATH9K_HTC_INIT_TXQ(subtype) do {            \
0031         qi.tqi_subtype = subtype_txq_to_hwq[subtype];   \
0032         qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;     \
0033         qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;        \
0034         qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;        \
0035         qi.tqi_physCompBuf = 0;             \
0036         qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |  \
0037             TXQ_FLAG_TXDESCINT_ENABLE;      \
0038     } while (0)
0039 
0040 int get_hw_qnum(u16 queue, int *hwq_map)
0041 {
0042     switch (queue) {
0043     case 0:
0044         return hwq_map[IEEE80211_AC_VO];
0045     case 1:
0046         return hwq_map[IEEE80211_AC_VI];
0047     case 2:
0048         return hwq_map[IEEE80211_AC_BE];
0049     case 3:
0050         return hwq_map[IEEE80211_AC_BK];
0051     default:
0052         return hwq_map[IEEE80211_AC_BE];
0053     }
0054 }
0055 
0056 void ath9k_htc_check_stop_queues(struct ath9k_htc_priv *priv)
0057 {
0058     spin_lock_bh(&priv->tx.tx_lock);
0059     priv->tx.queued_cnt++;
0060     if ((priv->tx.queued_cnt >= ATH9K_HTC_TX_THRESHOLD) &&
0061         !(priv->tx.flags & ATH9K_HTC_OP_TX_QUEUES_STOP)) {
0062         priv->tx.flags |= ATH9K_HTC_OP_TX_QUEUES_STOP;
0063         ieee80211_stop_queues(priv->hw);
0064     }
0065     spin_unlock_bh(&priv->tx.tx_lock);
0066 }
0067 
0068 void ath9k_htc_check_wake_queues(struct ath9k_htc_priv *priv)
0069 {
0070     spin_lock_bh(&priv->tx.tx_lock);
0071     if ((priv->tx.queued_cnt < ATH9K_HTC_TX_THRESHOLD) &&
0072         (priv->tx.flags & ATH9K_HTC_OP_TX_QUEUES_STOP)) {
0073         priv->tx.flags &= ~ATH9K_HTC_OP_TX_QUEUES_STOP;
0074         ieee80211_wake_queues(priv->hw);
0075     }
0076     spin_unlock_bh(&priv->tx.tx_lock);
0077 }
0078 
0079 int ath9k_htc_tx_get_slot(struct ath9k_htc_priv *priv)
0080 {
0081     int slot;
0082 
0083     spin_lock_bh(&priv->tx.tx_lock);
0084     slot = find_first_zero_bit(priv->tx.tx_slot, MAX_TX_BUF_NUM);
0085     if (slot >= MAX_TX_BUF_NUM) {
0086         spin_unlock_bh(&priv->tx.tx_lock);
0087         return -ENOBUFS;
0088     }
0089     __set_bit(slot, priv->tx.tx_slot);
0090     spin_unlock_bh(&priv->tx.tx_lock);
0091 
0092     return slot;
0093 }
0094 
0095 void ath9k_htc_tx_clear_slot(struct ath9k_htc_priv *priv, int slot)
0096 {
0097     spin_lock_bh(&priv->tx.tx_lock);
0098     __clear_bit(slot, priv->tx.tx_slot);
0099     spin_unlock_bh(&priv->tx.tx_lock);
0100 }
0101 
0102 static inline enum htc_endpoint_id get_htc_epid(struct ath9k_htc_priv *priv,
0103                         u16 qnum)
0104 {
0105     enum htc_endpoint_id epid;
0106 
0107     switch (qnum) {
0108     case 0:
0109         TX_QSTAT_INC(priv, IEEE80211_AC_VO);
0110         epid = priv->data_vo_ep;
0111         break;
0112     case 1:
0113         TX_QSTAT_INC(priv, IEEE80211_AC_VI);
0114         epid = priv->data_vi_ep;
0115         break;
0116     case 2:
0117         TX_QSTAT_INC(priv, IEEE80211_AC_BE);
0118         epid = priv->data_be_ep;
0119         break;
0120     case 3:
0121     default:
0122         TX_QSTAT_INC(priv, IEEE80211_AC_BK);
0123         epid = priv->data_bk_ep;
0124         break;
0125     }
0126 
0127     return epid;
0128 }
0129 
0130 static inline struct sk_buff_head*
0131 get_htc_epid_queue(struct ath9k_htc_priv *priv, u8 epid)
0132 {
0133     struct ath_common *common = ath9k_hw_common(priv->ah);
0134     struct sk_buff_head *epid_queue = NULL;
0135 
0136     if (epid == priv->mgmt_ep)
0137         epid_queue = &priv->tx.mgmt_ep_queue;
0138     else if (epid == priv->cab_ep)
0139         epid_queue = &priv->tx.cab_ep_queue;
0140     else if (epid == priv->data_be_ep)
0141         epid_queue = &priv->tx.data_be_queue;
0142     else if (epid == priv->data_bk_ep)
0143         epid_queue = &priv->tx.data_bk_queue;
0144     else if (epid == priv->data_vi_ep)
0145         epid_queue = &priv->tx.data_vi_queue;
0146     else if (epid == priv->data_vo_ep)
0147         epid_queue = &priv->tx.data_vo_queue;
0148     else
0149         ath_err(common, "Invalid EPID: %d\n", epid);
0150 
0151     return epid_queue;
0152 }
0153 
0154 /*
0155  * Removes the driver header and returns the TX slot number
0156  */
0157 static inline int strip_drv_header(struct ath9k_htc_priv *priv,
0158                    struct sk_buff *skb)
0159 {
0160     struct ath_common *common = ath9k_hw_common(priv->ah);
0161     struct ath9k_htc_tx_ctl *tx_ctl;
0162     int slot;
0163 
0164     tx_ctl = HTC_SKB_CB(skb);
0165 
0166     if (tx_ctl->epid == priv->mgmt_ep) {
0167         struct tx_mgmt_hdr *tx_mhdr =
0168             (struct tx_mgmt_hdr *)skb->data;
0169         slot = tx_mhdr->cookie;
0170         skb_pull(skb, sizeof(struct tx_mgmt_hdr));
0171     } else if ((tx_ctl->epid == priv->data_bk_ep) ||
0172            (tx_ctl->epid == priv->data_be_ep) ||
0173            (tx_ctl->epid == priv->data_vi_ep) ||
0174            (tx_ctl->epid == priv->data_vo_ep) ||
0175            (tx_ctl->epid == priv->cab_ep)) {
0176         struct tx_frame_hdr *tx_fhdr =
0177             (struct tx_frame_hdr *)skb->data;
0178         slot = tx_fhdr->cookie;
0179         skb_pull(skb, sizeof(struct tx_frame_hdr));
0180     } else {
0181         ath_err(common, "Unsupported EPID: %d\n", tx_ctl->epid);
0182         slot = -EINVAL;
0183     }
0184 
0185     return slot;
0186 }
0187 
0188 int ath_htc_txq_update(struct ath9k_htc_priv *priv, int qnum,
0189                struct ath9k_tx_queue_info *qinfo)
0190 {
0191     struct ath_hw *ah = priv->ah;
0192     int error = 0;
0193     struct ath9k_tx_queue_info qi;
0194 
0195     ath9k_hw_get_txq_props(ah, qnum, &qi);
0196 
0197     qi.tqi_aifs = qinfo->tqi_aifs;
0198     qi.tqi_cwmin = qinfo->tqi_cwmin / 2; /* XXX */
0199     qi.tqi_cwmax = qinfo->tqi_cwmax;
0200     qi.tqi_burstTime = qinfo->tqi_burstTime;
0201     qi.tqi_readyTime = qinfo->tqi_readyTime;
0202 
0203     if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
0204         ath_err(ath9k_hw_common(ah),
0205             "Unable to update hardware queue %u!\n", qnum);
0206         error = -EIO;
0207     } else {
0208         ath9k_hw_resettxqueue(ah, qnum);
0209     }
0210 
0211     return error;
0212 }
0213 
0214 static void ath9k_htc_tx_mgmt(struct ath9k_htc_priv *priv,
0215                   struct ath9k_htc_vif *avp,
0216                   struct sk_buff *skb,
0217                   u8 sta_idx, u8 vif_idx, u8 slot)
0218 {
0219     struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
0220     struct ieee80211_mgmt *mgmt;
0221     struct ieee80211_hdr *hdr;
0222     struct tx_mgmt_hdr mgmt_hdr;
0223     struct ath9k_htc_tx_ctl *tx_ctl;
0224     u8 *tx_fhdr;
0225 
0226     tx_ctl = HTC_SKB_CB(skb);
0227     hdr = (struct ieee80211_hdr *) skb->data;
0228 
0229     memset(tx_ctl, 0, sizeof(*tx_ctl));
0230     memset(&mgmt_hdr, 0, sizeof(struct tx_mgmt_hdr));
0231 
0232     /*
0233      * Set the TSF adjust value for probe response
0234      * frame also.
0235      */
0236     if (avp && unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
0237         mgmt = (struct ieee80211_mgmt *)skb->data;
0238         mgmt->u.probe_resp.timestamp = avp->tsfadjust;
0239     }
0240 
0241     tx_ctl->type = ATH9K_HTC_MGMT;
0242 
0243     mgmt_hdr.node_idx = sta_idx;
0244     mgmt_hdr.vif_idx = vif_idx;
0245     mgmt_hdr.tidno = 0;
0246     mgmt_hdr.flags = 0;
0247     mgmt_hdr.cookie = slot;
0248 
0249     mgmt_hdr.key_type = ath9k_cmn_get_hw_crypto_keytype(skb);
0250     if (mgmt_hdr.key_type == ATH9K_KEY_TYPE_CLEAR)
0251         mgmt_hdr.keyix = (u8) ATH9K_TXKEYIX_INVALID;
0252     else
0253         mgmt_hdr.keyix = tx_info->control.hw_key->hw_key_idx;
0254 
0255     tx_fhdr = skb_push(skb, sizeof(mgmt_hdr));
0256     memcpy(tx_fhdr, (u8 *) &mgmt_hdr, sizeof(mgmt_hdr));
0257     tx_ctl->epid = priv->mgmt_ep;
0258 }
0259 
0260 static void ath9k_htc_tx_data(struct ath9k_htc_priv *priv,
0261                   struct ieee80211_vif *vif,
0262                   struct sk_buff *skb,
0263                   u8 sta_idx, u8 vif_idx, u8 slot,
0264                   bool is_cab)
0265 {
0266     struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
0267     struct ieee80211_hdr *hdr;
0268     struct ath9k_htc_tx_ctl *tx_ctl;
0269     struct tx_frame_hdr tx_hdr;
0270     u32 flags = 0;
0271     u8 *qc, *tx_fhdr;
0272     u16 qnum;
0273 
0274     tx_ctl = HTC_SKB_CB(skb);
0275     hdr = (struct ieee80211_hdr *) skb->data;
0276 
0277     memset(tx_ctl, 0, sizeof(*tx_ctl));
0278     memset(&tx_hdr, 0, sizeof(struct tx_frame_hdr));
0279 
0280     tx_hdr.node_idx = sta_idx;
0281     tx_hdr.vif_idx = vif_idx;
0282     tx_hdr.cookie = slot;
0283 
0284     /*
0285      * This is a bit redundant but it helps to get
0286      * the per-packet index quickly when draining the
0287      * TX queue in the HIF layer. Otherwise we would
0288      * have to parse the packet contents ...
0289      */
0290     tx_ctl->sta_idx = sta_idx;
0291 
0292     if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
0293         tx_ctl->type = ATH9K_HTC_AMPDU;
0294         tx_hdr.data_type = ATH9K_HTC_AMPDU;
0295     } else {
0296         tx_ctl->type = ATH9K_HTC_NORMAL;
0297         tx_hdr.data_type = ATH9K_HTC_NORMAL;
0298     }
0299 
0300     /* Transmit all frames that should not be reordered relative
0301      * to each other using the same priority. For other QoS data
0302      * frames extract the priority from the header.
0303      */
0304     if (!(tx_info->control.flags & IEEE80211_TX_CTRL_DONT_REORDER) &&
0305         ieee80211_is_data_qos(hdr->frame_control)) {
0306         qc = ieee80211_get_qos_ctl(hdr);
0307         tx_hdr.tidno = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
0308     }
0309 
0310     /* Check for RTS protection */
0311     if (priv->hw->wiphy->rts_threshold != (u32) -1)
0312         if (skb->len > priv->hw->wiphy->rts_threshold)
0313             flags |= ATH9K_HTC_TX_RTSCTS;
0314 
0315     /* CTS-to-self */
0316     if (!(flags & ATH9K_HTC_TX_RTSCTS) &&
0317         (vif && vif->bss_conf.use_cts_prot))
0318         flags |= ATH9K_HTC_TX_CTSONLY;
0319 
0320     tx_hdr.flags = cpu_to_be32(flags);
0321     tx_hdr.key_type = ath9k_cmn_get_hw_crypto_keytype(skb);
0322     if (tx_hdr.key_type == ATH9K_KEY_TYPE_CLEAR)
0323         tx_hdr.keyix = (u8) ATH9K_TXKEYIX_INVALID;
0324     else
0325         tx_hdr.keyix = tx_info->control.hw_key->hw_key_idx;
0326 
0327     tx_fhdr = skb_push(skb, sizeof(tx_hdr));
0328     memcpy(tx_fhdr, (u8 *) &tx_hdr, sizeof(tx_hdr));
0329 
0330     if (is_cab) {
0331         CAB_STAT_INC(priv);
0332         tx_ctl->epid = priv->cab_ep;
0333         return;
0334     }
0335 
0336     qnum = skb_get_queue_mapping(skb);
0337     tx_ctl->epid = get_htc_epid(priv, qnum);
0338 }
0339 
0340 int ath9k_htc_tx_start(struct ath9k_htc_priv *priv,
0341                struct ieee80211_sta *sta,
0342                struct sk_buff *skb,
0343                u8 slot, bool is_cab)
0344 {
0345     struct ieee80211_hdr *hdr;
0346     struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
0347     struct ieee80211_vif *vif = tx_info->control.vif;
0348     struct ath9k_htc_sta *ista;
0349     struct ath9k_htc_vif *avp = NULL;
0350     u8 sta_idx, vif_idx;
0351 
0352     hdr = (struct ieee80211_hdr *) skb->data;
0353 
0354     /*
0355      * Find out on which interface this packet has to be
0356      * sent out.
0357      */
0358     if (vif) {
0359         avp = (struct ath9k_htc_vif *) vif->drv_priv;
0360         vif_idx = avp->index;
0361     } else {
0362         if (!priv->ah->is_monitoring) {
0363             ath_dbg(ath9k_hw_common(priv->ah), XMIT,
0364                 "VIF is null, but no monitor interface !\n");
0365             return -EINVAL;
0366         }
0367 
0368         vif_idx = priv->mon_vif_idx;
0369     }
0370 
0371     /*
0372      * Find out which station this packet is destined for.
0373      */
0374     if (sta) {
0375         ista = (struct ath9k_htc_sta *) sta->drv_priv;
0376         sta_idx = ista->index;
0377     } else {
0378         sta_idx = priv->vif_sta_pos[vif_idx];
0379     }
0380 
0381     if (ieee80211_is_data(hdr->frame_control))
0382         ath9k_htc_tx_data(priv, vif, skb,
0383                   sta_idx, vif_idx, slot, is_cab);
0384     else
0385         ath9k_htc_tx_mgmt(priv, avp, skb,
0386                   sta_idx, vif_idx, slot);
0387 
0388 
0389     return htc_send(priv->htc, skb);
0390 }
0391 
0392 static inline bool __ath9k_htc_check_tx_aggr(struct ath9k_htc_priv *priv,
0393                          struct ath9k_htc_sta *ista, u8 tid)
0394 {
0395     bool ret = false;
0396 
0397     spin_lock_bh(&priv->tx.tx_lock);
0398     if ((tid < ATH9K_HTC_MAX_TID) && (ista->tid_state[tid] == AGGR_STOP))
0399         ret = true;
0400     spin_unlock_bh(&priv->tx.tx_lock);
0401 
0402     return ret;
0403 }
0404 
0405 static void ath9k_htc_check_tx_aggr(struct ath9k_htc_priv *priv,
0406                     struct ieee80211_vif *vif,
0407                     struct sk_buff *skb)
0408 {
0409     struct ieee80211_sta *sta;
0410     struct ieee80211_hdr *hdr;
0411     __le16 fc;
0412 
0413     hdr = (struct ieee80211_hdr *) skb->data;
0414     fc = hdr->frame_control;
0415 
0416     rcu_read_lock();
0417 
0418     sta = ieee80211_find_sta(vif, hdr->addr1);
0419     if (!sta) {
0420         rcu_read_unlock();
0421         return;
0422     }
0423 
0424     if (sta && conf_is_ht(&priv->hw->conf) &&
0425         !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
0426         if (ieee80211_is_data_qos(fc)) {
0427             u8 *qc, tid;
0428             struct ath9k_htc_sta *ista;
0429 
0430             qc = ieee80211_get_qos_ctl(hdr);
0431             tid = qc[0] & 0xf;
0432             ista = (struct ath9k_htc_sta *)sta->drv_priv;
0433             if (__ath9k_htc_check_tx_aggr(priv, ista, tid)) {
0434                 ieee80211_start_tx_ba_session(sta, tid, 0);
0435                 spin_lock_bh(&priv->tx.tx_lock);
0436                 ista->tid_state[tid] = AGGR_PROGRESS;
0437                 spin_unlock_bh(&priv->tx.tx_lock);
0438             }
0439         }
0440     }
0441 
0442     rcu_read_unlock();
0443 }
0444 
0445 static void ath9k_htc_tx_process(struct ath9k_htc_priv *priv,
0446                  struct sk_buff *skb,
0447                  struct __wmi_event_txstatus *txs)
0448 {
0449     struct ieee80211_vif *vif;
0450     struct ath9k_htc_tx_ctl *tx_ctl;
0451     struct ieee80211_tx_info *tx_info;
0452     struct ieee80211_tx_rate *rate;
0453     struct ieee80211_conf *cur_conf = &priv->hw->conf;
0454     bool txok;
0455     int slot;
0456     int hdrlen, padsize;
0457 
0458     slot = strip_drv_header(priv, skb);
0459     if (slot < 0) {
0460         dev_kfree_skb_any(skb);
0461         return;
0462     }
0463 
0464     tx_ctl = HTC_SKB_CB(skb);
0465     txok = tx_ctl->txok;
0466     tx_info = IEEE80211_SKB_CB(skb);
0467     vif = tx_info->control.vif;
0468     rate = &tx_info->status.rates[0];
0469 
0470     memset(&tx_info->status, 0, sizeof(tx_info->status));
0471 
0472     /*
0473      * URB submission failed for this frame, it never reached
0474      * the target.
0475      */
0476     if (!txok || !vif || !txs)
0477         goto send_mac80211;
0478 
0479     if (txs->ts_flags & ATH9K_HTC_TXSTAT_ACK) {
0480         tx_info->flags |= IEEE80211_TX_STAT_ACK;
0481         if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
0482             tx_info->flags |= IEEE80211_TX_STAT_AMPDU;
0483     }
0484 
0485     if (txs->ts_flags & ATH9K_HTC_TXSTAT_FILT)
0486         tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
0487 
0488     if (txs->ts_flags & ATH9K_HTC_TXSTAT_RTC_CTS)
0489         rate->flags |= IEEE80211_TX_RC_USE_RTS_CTS;
0490 
0491     rate->count = 1;
0492     rate->idx = MS(txs->ts_rate, ATH9K_HTC_TXSTAT_RATE);
0493 
0494     if (txs->ts_flags & ATH9K_HTC_TXSTAT_MCS) {
0495         rate->flags |= IEEE80211_TX_RC_MCS;
0496 
0497         if (txs->ts_flags & ATH9K_HTC_TXSTAT_CW40)
0498             rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
0499         if (txs->ts_flags & ATH9K_HTC_TXSTAT_SGI)
0500             rate->flags |= IEEE80211_TX_RC_SHORT_GI;
0501     } else {
0502         if (cur_conf->chandef.chan->band == NL80211_BAND_5GHZ)
0503             rate->idx += 4; /* No CCK rates */
0504     }
0505 
0506     ath9k_htc_check_tx_aggr(priv, vif, skb);
0507 
0508 send_mac80211:
0509     spin_lock_bh(&priv->tx.tx_lock);
0510     if (WARN_ON(--priv->tx.queued_cnt < 0))
0511         priv->tx.queued_cnt = 0;
0512     spin_unlock_bh(&priv->tx.tx_lock);
0513 
0514     ath9k_htc_tx_clear_slot(priv, slot);
0515 
0516     /* Remove padding before handing frame back to mac80211 */
0517     hdrlen = ieee80211_get_hdrlen_from_skb(skb);
0518 
0519     padsize = hdrlen & 3;
0520     if (padsize && skb->len > hdrlen + padsize) {
0521         memmove(skb->data + padsize, skb->data, hdrlen);
0522         skb_pull(skb, padsize);
0523     }
0524 
0525     /* Send status to mac80211 */
0526     ieee80211_tx_status(priv->hw, skb);
0527 }
0528 
0529 static inline void ath9k_htc_tx_drainq(struct ath9k_htc_priv *priv,
0530                        struct sk_buff_head *queue)
0531 {
0532     struct sk_buff *skb;
0533 
0534     while ((skb = skb_dequeue(queue)) != NULL) {
0535         ath9k_htc_tx_process(priv, skb, NULL);
0536     }
0537 }
0538 
0539 void ath9k_htc_tx_drain(struct ath9k_htc_priv *priv)
0540 {
0541     struct ath9k_htc_tx_event *event, *tmp;
0542 
0543     spin_lock_bh(&priv->tx.tx_lock);
0544     priv->tx.flags |= ATH9K_HTC_OP_TX_DRAIN;
0545     spin_unlock_bh(&priv->tx.tx_lock);
0546 
0547     /*
0548      * Ensure that all pending TX frames are flushed,
0549      * and that the TX completion/failed tasklets is killed.
0550      */
0551     htc_stop(priv->htc);
0552     tasklet_kill(&priv->wmi->wmi_event_tasklet);
0553     tasklet_kill(&priv->tx_failed_tasklet);
0554 
0555     ath9k_htc_tx_drainq(priv, &priv->tx.mgmt_ep_queue);
0556     ath9k_htc_tx_drainq(priv, &priv->tx.cab_ep_queue);
0557     ath9k_htc_tx_drainq(priv, &priv->tx.data_be_queue);
0558     ath9k_htc_tx_drainq(priv, &priv->tx.data_bk_queue);
0559     ath9k_htc_tx_drainq(priv, &priv->tx.data_vi_queue);
0560     ath9k_htc_tx_drainq(priv, &priv->tx.data_vo_queue);
0561     ath9k_htc_tx_drainq(priv, &priv->tx.tx_failed);
0562 
0563     /*
0564      * The TX cleanup timer has already been killed.
0565      */
0566     spin_lock_bh(&priv->wmi->event_lock);
0567     list_for_each_entry_safe(event, tmp, &priv->wmi->pending_tx_events, list) {
0568         list_del(&event->list);
0569         kfree(event);
0570     }
0571     spin_unlock_bh(&priv->wmi->event_lock);
0572 
0573     spin_lock_bh(&priv->tx.tx_lock);
0574     priv->tx.flags &= ~ATH9K_HTC_OP_TX_DRAIN;
0575     spin_unlock_bh(&priv->tx.tx_lock);
0576 }
0577 
0578 void ath9k_tx_failed_tasklet(struct tasklet_struct *t)
0579 {
0580     struct ath9k_htc_priv *priv = from_tasklet(priv, t, tx_failed_tasklet);
0581 
0582     spin_lock(&priv->tx.tx_lock);
0583     if (priv->tx.flags & ATH9K_HTC_OP_TX_DRAIN) {
0584         spin_unlock(&priv->tx.tx_lock);
0585         return;
0586     }
0587     spin_unlock(&priv->tx.tx_lock);
0588 
0589     ath9k_htc_tx_drainq(priv, &priv->tx.tx_failed);
0590 }
0591 
0592 static inline bool check_cookie(struct ath9k_htc_priv *priv,
0593                 struct sk_buff *skb,
0594                 u8 cookie, u8 epid)
0595 {
0596     u8 fcookie = 0;
0597 
0598     if (epid == priv->mgmt_ep) {
0599         struct tx_mgmt_hdr *hdr;
0600         hdr = (struct tx_mgmt_hdr *) skb->data;
0601         fcookie = hdr->cookie;
0602     } else if ((epid == priv->data_bk_ep) ||
0603            (epid == priv->data_be_ep) ||
0604            (epid == priv->data_vi_ep) ||
0605            (epid == priv->data_vo_ep) ||
0606            (epid == priv->cab_ep)) {
0607         struct tx_frame_hdr *hdr;
0608         hdr = (struct tx_frame_hdr *) skb->data;
0609         fcookie = hdr->cookie;
0610     }
0611 
0612     if (fcookie == cookie)
0613         return true;
0614 
0615     return false;
0616 }
0617 
0618 static struct sk_buff* ath9k_htc_tx_get_packet(struct ath9k_htc_priv *priv,
0619                            struct __wmi_event_txstatus *txs)
0620 {
0621     struct ath_common *common = ath9k_hw_common(priv->ah);
0622     struct sk_buff_head *epid_queue;
0623     struct sk_buff *skb, *tmp;
0624     unsigned long flags;
0625     u8 epid = MS(txs->ts_rate, ATH9K_HTC_TXSTAT_EPID);
0626 
0627     epid_queue = get_htc_epid_queue(priv, epid);
0628     if (!epid_queue)
0629         return NULL;
0630 
0631     spin_lock_irqsave(&epid_queue->lock, flags);
0632     skb_queue_walk_safe(epid_queue, skb, tmp) {
0633         if (check_cookie(priv, skb, txs->cookie, epid)) {
0634             __skb_unlink(skb, epid_queue);
0635             spin_unlock_irqrestore(&epid_queue->lock, flags);
0636             return skb;
0637         }
0638     }
0639     spin_unlock_irqrestore(&epid_queue->lock, flags);
0640 
0641     ath_dbg(common, XMIT, "No matching packet for cookie: %d, epid: %d\n",
0642         txs->cookie, epid);
0643 
0644     return NULL;
0645 }
0646 
0647 void ath9k_htc_txstatus(struct ath9k_htc_priv *priv, void *wmi_event)
0648 {
0649     struct wmi_event_txstatus *txs = wmi_event;
0650     struct __wmi_event_txstatus *__txs;
0651     struct sk_buff *skb;
0652     struct ath9k_htc_tx_event *tx_pend;
0653     int i;
0654 
0655     for (i = 0; i < txs->cnt; i++) {
0656         WARN_ON(txs->cnt > HTC_MAX_TX_STATUS);
0657 
0658         __txs = &txs->txstatus[i];
0659 
0660         skb = ath9k_htc_tx_get_packet(priv, __txs);
0661         if (!skb) {
0662             /*
0663              * Store this event, so that the TX cleanup
0664              * routine can check later for the needed packet.
0665              */
0666             tx_pend = kzalloc(sizeof(struct ath9k_htc_tx_event),
0667                       GFP_ATOMIC);
0668             if (!tx_pend)
0669                 continue;
0670 
0671             memcpy(&tx_pend->txs, __txs,
0672                    sizeof(struct __wmi_event_txstatus));
0673 
0674             spin_lock(&priv->wmi->event_lock);
0675             list_add_tail(&tx_pend->list,
0676                       &priv->wmi->pending_tx_events);
0677             spin_unlock(&priv->wmi->event_lock);
0678 
0679             continue;
0680         }
0681 
0682         ath9k_htc_tx_process(priv, skb, __txs);
0683     }
0684 
0685     /* Wake TX queues if needed */
0686     ath9k_htc_check_wake_queues(priv);
0687 }
0688 
0689 void ath9k_htc_txep(void *drv_priv, struct sk_buff *skb,
0690             enum htc_endpoint_id ep_id, bool txok)
0691 {
0692     struct ath9k_htc_priv *priv = drv_priv;
0693     struct ath9k_htc_tx_ctl *tx_ctl;
0694     struct sk_buff_head *epid_queue;
0695 
0696     tx_ctl = HTC_SKB_CB(skb);
0697     tx_ctl->txok = txok;
0698     tx_ctl->timestamp = jiffies;
0699 
0700     if (!txok) {
0701         skb_queue_tail(&priv->tx.tx_failed, skb);
0702         tasklet_schedule(&priv->tx_failed_tasklet);
0703         return;
0704     }
0705 
0706     epid_queue = get_htc_epid_queue(priv, ep_id);
0707     if (!epid_queue) {
0708         dev_kfree_skb_any(skb);
0709         return;
0710     }
0711 
0712     skb_queue_tail(epid_queue, skb);
0713 }
0714 
0715 static inline bool check_packet(struct ath9k_htc_priv *priv, struct sk_buff *skb)
0716 {
0717     struct ath_common *common = ath9k_hw_common(priv->ah);
0718     struct ath9k_htc_tx_ctl *tx_ctl;
0719 
0720     tx_ctl = HTC_SKB_CB(skb);
0721 
0722     if (time_after(jiffies,
0723                tx_ctl->timestamp +
0724                msecs_to_jiffies(ATH9K_HTC_TX_TIMEOUT_INTERVAL))) {
0725         ath_dbg(common, XMIT, "Dropping a packet due to TX timeout\n");
0726         return true;
0727     }
0728 
0729     return false;
0730 }
0731 
0732 static void ath9k_htc_tx_cleanup_queue(struct ath9k_htc_priv *priv,
0733                        struct sk_buff_head *epid_queue)
0734 {
0735     bool process = false;
0736     unsigned long flags;
0737     struct sk_buff *skb, *tmp;
0738     struct sk_buff_head queue;
0739 
0740     skb_queue_head_init(&queue);
0741 
0742     spin_lock_irqsave(&epid_queue->lock, flags);
0743     skb_queue_walk_safe(epid_queue, skb, tmp) {
0744         if (check_packet(priv, skb)) {
0745             __skb_unlink(skb, epid_queue);
0746             __skb_queue_tail(&queue, skb);
0747             process = true;
0748         }
0749     }
0750     spin_unlock_irqrestore(&epid_queue->lock, flags);
0751 
0752     if (process) {
0753         skb_queue_walk_safe(&queue, skb, tmp) {
0754             __skb_unlink(skb, &queue);
0755             ath9k_htc_tx_process(priv, skb, NULL);
0756         }
0757     }
0758 }
0759 
0760 void ath9k_htc_tx_cleanup_timer(struct timer_list *t)
0761 {
0762     struct ath9k_htc_priv *priv = from_timer(priv, t, tx.cleanup_timer);
0763     struct ath_common *common = ath9k_hw_common(priv->ah);
0764     struct ath9k_htc_tx_event *event, *tmp;
0765     struct sk_buff *skb;
0766 
0767     spin_lock(&priv->wmi->event_lock);
0768     list_for_each_entry_safe(event, tmp, &priv->wmi->pending_tx_events, list) {
0769 
0770         skb = ath9k_htc_tx_get_packet(priv, &event->txs);
0771         if (skb) {
0772             ath_dbg(common, XMIT,
0773                 "Found packet for cookie: %d, epid: %d\n",
0774                 event->txs.cookie,
0775                 MS(event->txs.ts_rate, ATH9K_HTC_TXSTAT_EPID));
0776 
0777             ath9k_htc_tx_process(priv, skb, &event->txs);
0778             list_del(&event->list);
0779             kfree(event);
0780             continue;
0781         }
0782 
0783         if (++event->count >= ATH9K_HTC_TX_TIMEOUT_COUNT) {
0784             list_del(&event->list);
0785             kfree(event);
0786         }
0787     }
0788     spin_unlock(&priv->wmi->event_lock);
0789 
0790     /*
0791      * Check if status-pending packets have to be cleaned up.
0792      */
0793     ath9k_htc_tx_cleanup_queue(priv, &priv->tx.mgmt_ep_queue);
0794     ath9k_htc_tx_cleanup_queue(priv, &priv->tx.cab_ep_queue);
0795     ath9k_htc_tx_cleanup_queue(priv, &priv->tx.data_be_queue);
0796     ath9k_htc_tx_cleanup_queue(priv, &priv->tx.data_bk_queue);
0797     ath9k_htc_tx_cleanup_queue(priv, &priv->tx.data_vi_queue);
0798     ath9k_htc_tx_cleanup_queue(priv, &priv->tx.data_vo_queue);
0799 
0800     /* Wake TX queues if needed */
0801     ath9k_htc_check_wake_queues(priv);
0802 
0803     mod_timer(&priv->tx.cleanup_timer,
0804           jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
0805 }
0806 
0807 int ath9k_tx_init(struct ath9k_htc_priv *priv)
0808 {
0809     skb_queue_head_init(&priv->tx.mgmt_ep_queue);
0810     skb_queue_head_init(&priv->tx.cab_ep_queue);
0811     skb_queue_head_init(&priv->tx.data_be_queue);
0812     skb_queue_head_init(&priv->tx.data_bk_queue);
0813     skb_queue_head_init(&priv->tx.data_vi_queue);
0814     skb_queue_head_init(&priv->tx.data_vo_queue);
0815     skb_queue_head_init(&priv->tx.tx_failed);
0816 
0817     /* Allow ath9k_wmi_event_tasklet(WMI_TXSTATUS_EVENTID) to operate. */
0818     smp_wmb();
0819     priv->tx.initialized = true;
0820 
0821     return 0;
0822 }
0823 
0824 void ath9k_tx_cleanup(struct ath9k_htc_priv *priv)
0825 {
0826 
0827 }
0828 
0829 bool ath9k_htc_txq_setup(struct ath9k_htc_priv *priv, int subtype)
0830 {
0831     struct ath_hw *ah = priv->ah;
0832     struct ath_common *common = ath9k_hw_common(ah);
0833     struct ath9k_tx_queue_info qi;
0834     int qnum;
0835 
0836     memset(&qi, 0, sizeof(qi));
0837     ATH9K_HTC_INIT_TXQ(subtype);
0838 
0839     qnum = ath9k_hw_setuptxqueue(priv->ah, ATH9K_TX_QUEUE_DATA, &qi);
0840     if (qnum == -1)
0841         return false;
0842 
0843     if (qnum >= ARRAY_SIZE(priv->hwq_map)) {
0844         ath_err(common, "qnum %u out of range, max %zu!\n",
0845             qnum, ARRAY_SIZE(priv->hwq_map));
0846         ath9k_hw_releasetxqueue(ah, qnum);
0847         return false;
0848     }
0849 
0850     priv->hwq_map[subtype] = qnum;
0851     return true;
0852 }
0853 
0854 int ath9k_htc_cabq_setup(struct ath9k_htc_priv *priv)
0855 {
0856     struct ath9k_tx_queue_info qi;
0857 
0858     memset(&qi, 0, sizeof(qi));
0859     ATH9K_HTC_INIT_TXQ(0);
0860 
0861     return ath9k_hw_setuptxqueue(priv->ah, ATH9K_TX_QUEUE_CAB, &qi);
0862 }
0863 
0864 /******/
0865 /* RX */
0866 /******/
0867 
0868 /*
0869  * Calculate the RX filter to be set in the HW.
0870  */
0871 u32 ath9k_htc_calcrxfilter(struct ath9k_htc_priv *priv)
0872 {
0873 #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
0874 
0875     struct ath_hw *ah = priv->ah;
0876     u32 rfilt;
0877 
0878     rfilt = (ath9k_hw_getrxfilter(ah) & RX_FILTER_PRESERVE)
0879         | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
0880         | ATH9K_RX_FILTER_MCAST;
0881 
0882     if (priv->rxfilter & FIF_PROBE_REQ)
0883         rfilt |= ATH9K_RX_FILTER_PROBEREQ;
0884 
0885     if (ah->is_monitoring)
0886         rfilt |= ATH9K_RX_FILTER_PROM;
0887 
0888     if (priv->rxfilter & FIF_CONTROL)
0889         rfilt |= ATH9K_RX_FILTER_CONTROL;
0890 
0891     if ((ah->opmode == NL80211_IFTYPE_STATION) &&
0892         (priv->nvifs <= 1) &&
0893         !(priv->rxfilter & FIF_BCN_PRBRESP_PROMISC))
0894         rfilt |= ATH9K_RX_FILTER_MYBEACON;
0895     else
0896         rfilt |= ATH9K_RX_FILTER_BEACON;
0897 
0898     if (conf_is_ht(&priv->hw->conf)) {
0899         rfilt |= ATH9K_RX_FILTER_COMP_BAR;
0900         rfilt |= ATH9K_RX_FILTER_UNCOMP_BA_BAR;
0901     }
0902 
0903     if (priv->rxfilter & FIF_PSPOLL)
0904         rfilt |= ATH9K_RX_FILTER_PSPOLL;
0905 
0906     if (priv->nvifs > 1 ||
0907         priv->rxfilter & (FIF_OTHER_BSS | FIF_MCAST_ACTION))
0908         rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
0909 
0910     return rfilt;
0911 
0912 #undef RX_FILTER_PRESERVE
0913 }
0914 
0915 /*
0916  * Recv initialization for opmode change.
0917  */
0918 static void ath9k_htc_opmode_init(struct ath9k_htc_priv *priv)
0919 {
0920     struct ath_hw *ah = priv->ah;
0921     u32 rfilt, mfilt[2];
0922 
0923     /* configure rx filter */
0924     rfilt = ath9k_htc_calcrxfilter(priv);
0925     ath9k_hw_setrxfilter(ah, rfilt);
0926 
0927     /* calculate and install multicast filter */
0928     mfilt[0] = mfilt[1] = ~0;
0929     ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
0930 }
0931 
0932 void ath9k_host_rx_init(struct ath9k_htc_priv *priv)
0933 {
0934     struct ath_common *common = ath9k_hw_common(priv->ah);
0935     ath9k_hw_rxena(priv->ah);
0936     ath9k_htc_opmode_init(priv);
0937     ath9k_hw_startpcureceive(priv->ah, test_bit(ATH_OP_SCANNING, &common->op_flags));
0938 }
0939 
0940 static inline void convert_htc_flag(struct ath_rx_status *rx_stats,
0941                    struct ath_htc_rx_status *rxstatus)
0942 {
0943     rx_stats->enc_flags = 0;
0944     rx_stats->bw = RATE_INFO_BW_20;
0945     if (rxstatus->rs_flags & ATH9K_RX_2040)
0946         rx_stats->bw = RATE_INFO_BW_40;
0947     if (rxstatus->rs_flags & ATH9K_RX_GI)
0948         rx_stats->enc_flags |= RX_ENC_FLAG_SHORT_GI;
0949 }
0950 
0951 static void rx_status_htc_to_ath(struct ath_rx_status *rx_stats,
0952                  struct ath_htc_rx_status *rxstatus)
0953 {
0954     rx_stats->rs_datalen    = be16_to_cpu(rxstatus->rs_datalen);
0955     rx_stats->rs_status = rxstatus->rs_status;
0956     rx_stats->rs_phyerr = rxstatus->rs_phyerr;
0957     rx_stats->rs_rssi   = rxstatus->rs_rssi;
0958     rx_stats->rs_keyix  = rxstatus->rs_keyix;
0959     rx_stats->rs_rate   = rxstatus->rs_rate;
0960     rx_stats->rs_antenna    = rxstatus->rs_antenna;
0961     rx_stats->rs_more   = rxstatus->rs_more;
0962 
0963     memcpy(rx_stats->rs_rssi_ctl, rxstatus->rs_rssi_ctl,
0964         sizeof(rx_stats->rs_rssi_ctl));
0965     memcpy(rx_stats->rs_rssi_ext, rxstatus->rs_rssi_ext,
0966         sizeof(rx_stats->rs_rssi_ext));
0967 
0968     rx_stats->rs_isaggr = rxstatus->rs_isaggr;
0969     rx_stats->rs_moreaggr   = rxstatus->rs_moreaggr;
0970     rx_stats->rs_num_delims = rxstatus->rs_num_delims;
0971     convert_htc_flag(rx_stats, rxstatus);
0972 }
0973 
0974 static bool ath9k_rx_prepare(struct ath9k_htc_priv *priv,
0975                  struct ath9k_htc_rxbuf *rxbuf,
0976                  struct ieee80211_rx_status *rx_status)
0977 
0978 {
0979     struct ieee80211_hdr *hdr;
0980     struct ieee80211_hw *hw = priv->hw;
0981     struct sk_buff *skb = rxbuf->skb;
0982     struct ath_common *common = ath9k_hw_common(priv->ah);
0983     struct ath_hw *ah = common->ah;
0984     struct ath_htc_rx_status *rxstatus;
0985     struct ath_rx_status rx_stats;
0986     bool decrypt_error = false;
0987     u16 rs_datalen;
0988     bool is_phyerr;
0989 
0990     if (skb->len < HTC_RX_FRAME_HEADER_SIZE) {
0991         ath_err(common, "Corrupted RX frame, dropping (len: %d)\n",
0992             skb->len);
0993         goto rx_next;
0994     }
0995 
0996     rxstatus = (struct ath_htc_rx_status *)skb->data;
0997 
0998     rs_datalen = be16_to_cpu(rxstatus->rs_datalen);
0999     if (unlikely(rs_datalen -
1000         (skb->len - HTC_RX_FRAME_HEADER_SIZE) != 0)) {
1001         ath_err(common,
1002             "Corrupted RX data len, dropping (dlen: %d, skblen: %d)\n",
1003             rs_datalen, skb->len);
1004         goto rx_next;
1005     }
1006 
1007     is_phyerr = rxstatus->rs_status & ATH9K_RXERR_PHY;
1008     /*
1009      * Discard zero-length packets and packets smaller than an ACK
1010      * which are not PHY_ERROR (short radar pulses have a length of 3)
1011      */
1012     if (unlikely(!rs_datalen || (rs_datalen < 10 && !is_phyerr))) {
1013         ath_dbg(common, ANY,
1014             "Short RX data len, dropping (dlen: %d)\n",
1015             rs_datalen);
1016         goto rx_next;
1017     }
1018 
1019     if (rxstatus->rs_keyix >= ATH_KEYMAX &&
1020         rxstatus->rs_keyix != ATH9K_RXKEYIX_INVALID) {
1021         ath_dbg(common, ANY,
1022             "Invalid keyix, dropping (keyix: %d)\n",
1023             rxstatus->rs_keyix);
1024         goto rx_next;
1025     }
1026 
1027     /* Get the RX status information */
1028 
1029     memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
1030 
1031     /* Copy everything from ath_htc_rx_status (HTC_RX_FRAME_HEADER).
1032      * After this, we can drop this part of skb. */
1033     rx_status_htc_to_ath(&rx_stats, rxstatus);
1034     ath9k_htc_err_stat_rx(priv, &rx_stats);
1035     rx_status->mactime = be64_to_cpu(rxstatus->rs_tstamp);
1036     skb_pull(skb, HTC_RX_FRAME_HEADER_SIZE);
1037 
1038     /*
1039      * everything but the rate is checked here, the rate check is done
1040      * separately to avoid doing two lookups for a rate for each frame.
1041      */
1042     hdr = (struct ieee80211_hdr *)skb->data;
1043 
1044     /*
1045      * Process PHY errors and return so that the packet
1046      * can be dropped.
1047      */
1048     if (unlikely(is_phyerr)) {
1049         /* TODO: Not using DFS processing now. */
1050         if (ath_cmn_process_fft(&priv->spec_priv, hdr,
1051                     &rx_stats, rx_status->mactime)) {
1052             /* TODO: Code to collect spectral scan statistics */
1053         }
1054         goto rx_next;
1055     }
1056 
1057     if (!ath9k_cmn_rx_accept(common, hdr, rx_status, &rx_stats,
1058             &decrypt_error, priv->rxfilter))
1059         goto rx_next;
1060 
1061     ath9k_cmn_rx_skb_postprocess(common, skb, &rx_stats,
1062                      rx_status, decrypt_error);
1063 
1064     if (ath9k_cmn_process_rate(common, hw, &rx_stats, rx_status))
1065         goto rx_next;
1066 
1067     rx_stats.is_mybeacon = ath_is_mybeacon(common, hdr);
1068     ath9k_cmn_process_rssi(common, hw, &rx_stats, rx_status);
1069 
1070     rx_status->band = ah->curchan->chan->band;
1071     rx_status->freq = ah->curchan->chan->center_freq;
1072     rx_status->antenna = rx_stats.rs_antenna;
1073     rx_status->flag |= RX_FLAG_MACTIME_END;
1074 
1075     return true;
1076 rx_next:
1077     return false;
1078 }
1079 
1080 /*
1081  * FIXME: Handle FLUSH later on.
1082  */
1083 void ath9k_rx_tasklet(struct tasklet_struct *t)
1084 {
1085     struct ath9k_htc_priv *priv = from_tasklet(priv, t, rx_tasklet);
1086     struct ath9k_htc_rxbuf *rxbuf = NULL, *tmp_buf = NULL;
1087     struct ieee80211_rx_status rx_status;
1088     struct sk_buff *skb;
1089     unsigned long flags;
1090     struct ieee80211_hdr *hdr;
1091 
1092     do {
1093         spin_lock_irqsave(&priv->rx.rxbuflock, flags);
1094         list_for_each_entry(tmp_buf, &priv->rx.rxbuf, list) {
1095             if (tmp_buf->in_process) {
1096                 rxbuf = tmp_buf;
1097                 break;
1098             }
1099         }
1100 
1101         if (rxbuf == NULL) {
1102             spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
1103             break;
1104         }
1105 
1106         if (!rxbuf->skb)
1107             goto requeue;
1108 
1109         if (!ath9k_rx_prepare(priv, rxbuf, &rx_status)) {
1110             dev_kfree_skb_any(rxbuf->skb);
1111             goto requeue;
1112         }
1113 
1114         memcpy(IEEE80211_SKB_RXCB(rxbuf->skb), &rx_status,
1115                sizeof(struct ieee80211_rx_status));
1116         skb = rxbuf->skb;
1117         hdr = (struct ieee80211_hdr *) skb->data;
1118 
1119         if (ieee80211_is_beacon(hdr->frame_control) && priv->ps_enabled)
1120                 ieee80211_queue_work(priv->hw, &priv->ps_work);
1121 
1122         spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
1123 
1124         ieee80211_rx(priv->hw, skb);
1125 
1126         spin_lock_irqsave(&priv->rx.rxbuflock, flags);
1127 requeue:
1128         rxbuf->in_process = false;
1129         rxbuf->skb = NULL;
1130         list_move_tail(&rxbuf->list, &priv->rx.rxbuf);
1131         rxbuf = NULL;
1132         spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
1133     } while (1);
1134 
1135 }
1136 
1137 void ath9k_htc_rxep(void *drv_priv, struct sk_buff *skb,
1138             enum htc_endpoint_id ep_id)
1139 {
1140     struct ath9k_htc_priv *priv = drv_priv;
1141     struct ath_hw *ah = priv->ah;
1142     struct ath_common *common = ath9k_hw_common(ah);
1143     struct ath9k_htc_rxbuf *rxbuf = NULL, *tmp_buf = NULL;
1144     unsigned long flags;
1145 
1146     /* Check if ath9k_rx_init() completed. */
1147     if (!data_race(priv->rx.initialized))
1148         goto err;
1149 
1150     spin_lock_irqsave(&priv->rx.rxbuflock, flags);
1151     list_for_each_entry(tmp_buf, &priv->rx.rxbuf, list) {
1152         if (!tmp_buf->in_process) {
1153             rxbuf = tmp_buf;
1154             break;
1155         }
1156     }
1157     spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
1158 
1159     if (rxbuf == NULL) {
1160         ath_dbg(common, ANY, "No free RX buffer\n");
1161         goto err;
1162     }
1163 
1164     spin_lock_irqsave(&priv->rx.rxbuflock, flags);
1165     rxbuf->skb = skb;
1166     rxbuf->in_process = true;
1167     spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
1168 
1169     tasklet_schedule(&priv->rx_tasklet);
1170     return;
1171 err:
1172     dev_kfree_skb_any(skb);
1173 }
1174 
1175 /* FIXME: Locking for cleanup/init */
1176 
1177 void ath9k_rx_cleanup(struct ath9k_htc_priv *priv)
1178 {
1179     struct ath9k_htc_rxbuf *rxbuf, *tbuf;
1180 
1181     list_for_each_entry_safe(rxbuf, tbuf, &priv->rx.rxbuf, list) {
1182         list_del(&rxbuf->list);
1183         if (rxbuf->skb)
1184             dev_kfree_skb_any(rxbuf->skb);
1185         kfree(rxbuf);
1186     }
1187 }
1188 
1189 int ath9k_rx_init(struct ath9k_htc_priv *priv)
1190 {
1191     int i = 0;
1192 
1193     INIT_LIST_HEAD(&priv->rx.rxbuf);
1194     spin_lock_init(&priv->rx.rxbuflock);
1195 
1196     for (i = 0; i < ATH9K_HTC_RXBUF; i++) {
1197         struct ath9k_htc_rxbuf *rxbuf =
1198             kzalloc(sizeof(struct ath9k_htc_rxbuf), GFP_KERNEL);
1199         if (rxbuf == NULL)
1200             goto err;
1201 
1202         list_add_tail(&rxbuf->list, &priv->rx.rxbuf);
1203     }
1204 
1205     /* Allow ath9k_htc_rxep() to operate. */
1206     smp_wmb();
1207     priv->rx.initialized = true;
1208 
1209     return 0;
1210 
1211 err:
1212     ath9k_rx_cleanup(priv);
1213     return -ENOMEM;
1214 }