ath9k_htc: Use SKB's private area for TX parameters
[sfrench/cifs-2.6.git] / drivers / net / wireless / ath / ath9k / htc_drv_txrx.c
1 /*
2  * Copyright (c) 2010 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include "htc.h"
18
19 /******/
20 /* TX */
21 /******/
22
23 static const int subtype_txq_to_hwq[] = {
24         [WME_AC_BE] = ATH_TXQ_AC_BE,
25         [WME_AC_BK] = ATH_TXQ_AC_BK,
26         [WME_AC_VI] = ATH_TXQ_AC_VI,
27         [WME_AC_VO] = ATH_TXQ_AC_VO,
28 };
29
30 #define ATH9K_HTC_INIT_TXQ(subtype) do {                        \
31                 qi.tqi_subtype = subtype_txq_to_hwq[subtype];   \
32                 qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;             \
33                 qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;            \
34                 qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;            \
35                 qi.tqi_physCompBuf = 0;                         \
36                 qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE |      \
37                         TXQ_FLAG_TXDESCINT_ENABLE;              \
38         } while (0)
39
40 int get_hw_qnum(u16 queue, int *hwq_map)
41 {
42         switch (queue) {
43         case 0:
44                 return hwq_map[WME_AC_VO];
45         case 1:
46                 return hwq_map[WME_AC_VI];
47         case 2:
48                 return hwq_map[WME_AC_BE];
49         case 3:
50                 return hwq_map[WME_AC_BK];
51         default:
52                 return hwq_map[WME_AC_BE];
53         }
54 }
55
56 int ath_htc_txq_update(struct ath9k_htc_priv *priv, int qnum,
57                        struct ath9k_tx_queue_info *qinfo)
58 {
59         struct ath_hw *ah = priv->ah;
60         int error = 0;
61         struct ath9k_tx_queue_info qi;
62
63         ath9k_hw_get_txq_props(ah, qnum, &qi);
64
65         qi.tqi_aifs = qinfo->tqi_aifs;
66         qi.tqi_cwmin = qinfo->tqi_cwmin / 2; /* XXX */
67         qi.tqi_cwmax = qinfo->tqi_cwmax;
68         qi.tqi_burstTime = qinfo->tqi_burstTime;
69         qi.tqi_readyTime = qinfo->tqi_readyTime;
70
71         if (!ath9k_hw_set_txq_props(ah, qnum, &qi)) {
72                 ath_err(ath9k_hw_common(ah),
73                         "Unable to update hardware queue %u!\n", qnum);
74                 error = -EIO;
75         } else {
76                 ath9k_hw_resettxqueue(ah, qnum);
77         }
78
79         return error;
80 }
81
82 int ath9k_htc_tx_start(struct ath9k_htc_priv *priv,
83                        struct sk_buff *skb, bool is_cab)
84 {
85         struct ieee80211_hdr *hdr;
86         struct ieee80211_mgmt *mgmt;
87         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
88         struct ieee80211_sta *sta = tx_info->control.sta;
89         struct ieee80211_vif *vif = tx_info->control.vif;
90         struct ath9k_htc_sta *ista;
91         struct ath9k_htc_vif *avp = NULL;
92         struct ath9k_htc_tx_ctl *tx_ctl;
93         enum htc_endpoint_id epid;
94         u16 qnum;
95         __le16 fc;
96         u8 *tx_fhdr;
97         u8 sta_idx, vif_idx;
98
99         tx_ctl = HTC_SKB_CB(skb);
100         memset(tx_ctl, 0, sizeof(*tx_ctl));
101
102         hdr = (struct ieee80211_hdr *) skb->data;
103         fc = hdr->frame_control;
104
105         /*
106          * Find out on which interface this packet has to be
107          * sent out.
108          */
109         if (vif) {
110                 avp = (struct ath9k_htc_vif *) vif->drv_priv;
111                 vif_idx = avp->index;
112         } else {
113                 if (!priv->ah->is_monitoring) {
114                         ath_dbg(ath9k_hw_common(priv->ah), ATH_DBG_XMIT,
115                                 "VIF is null, but no monitor interface !\n");
116                         return -EINVAL;
117                 }
118
119                 vif_idx = priv->mon_vif_idx;
120         }
121
122         /*
123          * Find out which station this packet is destined for.
124          */
125         if (sta) {
126                 ista = (struct ath9k_htc_sta *) sta->drv_priv;
127                 sta_idx = ista->index;
128         } else {
129                 sta_idx = priv->vif_sta_pos[vif_idx];
130         }
131
132         if (ieee80211_is_data(fc)) {
133                 struct tx_frame_hdr tx_hdr;
134                 u32 flags = 0;
135                 u8 *qc;
136
137                 memset(&tx_hdr, 0, sizeof(struct tx_frame_hdr));
138
139                 tx_hdr.node_idx = sta_idx;
140                 tx_hdr.vif_idx = vif_idx;
141
142                 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
143                         tx_ctl->type = ATH9K_HTC_AMPDU;
144                         tx_hdr.data_type = ATH9K_HTC_AMPDU;
145                 } else {
146                         tx_ctl->type = ATH9K_HTC_NORMAL;
147                         tx_hdr.data_type = ATH9K_HTC_NORMAL;
148                 }
149
150                 if (ieee80211_is_data_qos(fc)) {
151                         qc = ieee80211_get_qos_ctl(hdr);
152                         tx_hdr.tidno = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
153                 }
154
155                 /* Check for RTS protection */
156                 if (priv->hw->wiphy->rts_threshold != (u32) -1)
157                         if (skb->len > priv->hw->wiphy->rts_threshold)
158                                 flags |= ATH9K_HTC_TX_RTSCTS;
159
160                 /* CTS-to-self */
161                 if (!(flags & ATH9K_HTC_TX_RTSCTS) &&
162                     (vif && vif->bss_conf.use_cts_prot))
163                         flags |= ATH9K_HTC_TX_CTSONLY;
164
165                 tx_hdr.flags = cpu_to_be32(flags);
166                 tx_hdr.key_type = ath9k_cmn_get_hw_crypto_keytype(skb);
167                 if (tx_hdr.key_type == ATH9K_KEY_TYPE_CLEAR)
168                         tx_hdr.keyix = (u8) ATH9K_TXKEYIX_INVALID;
169                 else
170                         tx_hdr.keyix = tx_info->control.hw_key->hw_key_idx;
171
172                 tx_fhdr = skb_push(skb, sizeof(tx_hdr));
173                 memcpy(tx_fhdr, (u8 *) &tx_hdr, sizeof(tx_hdr));
174
175                 if (is_cab) {
176                         CAB_STAT_INC;
177                         epid = priv->cab_ep;
178                         goto send;
179                 }
180
181                 qnum = skb_get_queue_mapping(skb);
182
183                 switch (qnum) {
184                 case 0:
185                         TX_QSTAT_INC(WME_AC_VO);
186                         epid = priv->data_vo_ep;
187                         break;
188                 case 1:
189                         TX_QSTAT_INC(WME_AC_VI);
190                         epid = priv->data_vi_ep;
191                         break;
192                 case 2:
193                         TX_QSTAT_INC(WME_AC_BE);
194                         epid = priv->data_be_ep;
195                         break;
196                 case 3:
197                 default:
198                         TX_QSTAT_INC(WME_AC_BK);
199                         epid = priv->data_bk_ep;
200                         break;
201                 }
202         } else {
203                 struct tx_mgmt_hdr mgmt_hdr;
204
205                 memset(&mgmt_hdr, 0, sizeof(struct tx_mgmt_hdr));
206
207                 /*
208                  * Set the TSF adjust value for probe response
209                  * frame also.
210                  */
211                 if (avp && unlikely(ieee80211_is_probe_resp(fc))) {
212                         mgmt = (struct ieee80211_mgmt *)skb->data;
213                         mgmt->u.probe_resp.timestamp = avp->tsfadjust;
214                 }
215
216                 tx_ctl->type = ATH9K_HTC_MGMT;
217
218                 mgmt_hdr.node_idx = sta_idx;
219                 mgmt_hdr.vif_idx = vif_idx;
220                 mgmt_hdr.tidno = 0;
221                 mgmt_hdr.flags = 0;
222
223                 mgmt_hdr.key_type = ath9k_cmn_get_hw_crypto_keytype(skb);
224                 if (mgmt_hdr.key_type == ATH9K_KEY_TYPE_CLEAR)
225                         mgmt_hdr.keyix = (u8) ATH9K_TXKEYIX_INVALID;
226                 else
227                         mgmt_hdr.keyix = tx_info->control.hw_key->hw_key_idx;
228
229                 tx_fhdr = skb_push(skb, sizeof(mgmt_hdr));
230                 memcpy(tx_fhdr, (u8 *) &mgmt_hdr, sizeof(mgmt_hdr));
231                 epid = priv->mgmt_ep;
232         }
233 send:
234         return htc_send(priv->htc, skb, epid);
235 }
236
237 static bool ath9k_htc_check_tx_aggr(struct ath9k_htc_priv *priv,
238                                     struct ath9k_htc_sta *ista, u8 tid)
239 {
240         bool ret = false;
241
242         spin_lock_bh(&priv->tx_lock);
243         if ((tid < ATH9K_HTC_MAX_TID) && (ista->tid_state[tid] == AGGR_STOP))
244                 ret = true;
245         spin_unlock_bh(&priv->tx_lock);
246
247         return ret;
248 }
249
250 void ath9k_tx_tasklet(unsigned long data)
251 {
252         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
253         struct ieee80211_vif *vif;
254         struct ieee80211_sta *sta;
255         struct ieee80211_hdr *hdr;
256         struct ieee80211_tx_info *tx_info;
257         struct sk_buff *skb = NULL;
258         __le16 fc;
259
260         while ((skb = skb_dequeue(&priv->tx_queue)) != NULL) {
261
262                 hdr = (struct ieee80211_hdr *) skb->data;
263                 fc = hdr->frame_control;
264                 tx_info = IEEE80211_SKB_CB(skb);
265                 vif = tx_info->control.vif;
266
267                 memset(&tx_info->status, 0, sizeof(tx_info->status));
268
269                 if (!vif)
270                         goto send_mac80211;
271
272                 rcu_read_lock();
273
274                 sta = ieee80211_find_sta(vif, hdr->addr1);
275                 if (!sta) {
276                         rcu_read_unlock();
277                         ieee80211_tx_status(priv->hw, skb);
278                         continue;
279                 }
280
281                 /* Check if we need to start aggregation */
282
283                 if (sta && conf_is_ht(&priv->hw->conf) &&
284                     !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
285                         if (ieee80211_is_data_qos(fc)) {
286                                 u8 *qc, tid;
287                                 struct ath9k_htc_sta *ista;
288
289                                 qc = ieee80211_get_qos_ctl(hdr);
290                                 tid = qc[0] & 0xf;
291                                 ista = (struct ath9k_htc_sta *)sta->drv_priv;
292
293                                 if (ath9k_htc_check_tx_aggr(priv, ista, tid)) {
294                                         ieee80211_start_tx_ba_session(sta, tid, 0);
295                                         spin_lock_bh(&priv->tx_lock);
296                                         ista->tid_state[tid] = AGGR_PROGRESS;
297                                         spin_unlock_bh(&priv->tx_lock);
298                                 }
299                         }
300                 }
301
302                 rcu_read_unlock();
303
304         send_mac80211:
305                 /* Send status to mac80211 */
306                 ieee80211_tx_status(priv->hw, skb);
307         }
308
309         /* Wake TX queues if needed */
310         spin_lock_bh(&priv->tx_lock);
311         if (priv->tx_queues_stop) {
312                 priv->tx_queues_stop = false;
313                 spin_unlock_bh(&priv->tx_lock);
314                 ath_dbg(ath9k_hw_common(priv->ah), ATH_DBG_XMIT,
315                         "Waking up TX queues\n");
316                 ieee80211_wake_queues(priv->hw);
317                 return;
318         }
319         spin_unlock_bh(&priv->tx_lock);
320 }
321
322 void ath9k_htc_txep(void *drv_priv, struct sk_buff *skb,
323                     enum htc_endpoint_id ep_id, bool txok)
324 {
325         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) drv_priv;
326         struct ath_common *common = ath9k_hw_common(priv->ah);
327         struct ieee80211_tx_info *tx_info;
328
329         if (!skb)
330                 return;
331
332         if (ep_id == priv->mgmt_ep) {
333                 skb_pull(skb, sizeof(struct tx_mgmt_hdr));
334         } else if ((ep_id == priv->data_bk_ep) ||
335                    (ep_id == priv->data_be_ep) ||
336                    (ep_id == priv->data_vi_ep) ||
337                    (ep_id == priv->data_vo_ep) ||
338                    (ep_id == priv->cab_ep)) {
339                 skb_pull(skb, sizeof(struct tx_frame_hdr));
340         } else {
341                 ath_err(common, "Unsupported TX EPID: %d\n", ep_id);
342                 dev_kfree_skb_any(skb);
343                 return;
344         }
345
346         tx_info = IEEE80211_SKB_CB(skb);
347
348         if (txok)
349                 tx_info->flags |= IEEE80211_TX_STAT_ACK;
350
351         skb_queue_tail(&priv->tx_queue, skb);
352         tasklet_schedule(&priv->tx_tasklet);
353 }
354
355 int ath9k_tx_init(struct ath9k_htc_priv *priv)
356 {
357         skb_queue_head_init(&priv->tx_queue);
358         return 0;
359 }
360
361 void ath9k_tx_cleanup(struct ath9k_htc_priv *priv)
362 {
363
364 }
365
366 bool ath9k_htc_txq_setup(struct ath9k_htc_priv *priv, int subtype)
367 {
368         struct ath_hw *ah = priv->ah;
369         struct ath_common *common = ath9k_hw_common(ah);
370         struct ath9k_tx_queue_info qi;
371         int qnum;
372
373         memset(&qi, 0, sizeof(qi));
374         ATH9K_HTC_INIT_TXQ(subtype);
375
376         qnum = ath9k_hw_setuptxqueue(priv->ah, ATH9K_TX_QUEUE_DATA, &qi);
377         if (qnum == -1)
378                 return false;
379
380         if (qnum >= ARRAY_SIZE(priv->hwq_map)) {
381                 ath_err(common, "qnum %u out of range, max %zu!\n",
382                         qnum, ARRAY_SIZE(priv->hwq_map));
383                 ath9k_hw_releasetxqueue(ah, qnum);
384                 return false;
385         }
386
387         priv->hwq_map[subtype] = qnum;
388         return true;
389 }
390
391 int ath9k_htc_cabq_setup(struct ath9k_htc_priv *priv)
392 {
393         struct ath9k_tx_queue_info qi;
394
395         memset(&qi, 0, sizeof(qi));
396         ATH9K_HTC_INIT_TXQ(0);
397
398         return ath9k_hw_setuptxqueue(priv->ah, ATH9K_TX_QUEUE_CAB, &qi);
399 }
400
401 /******/
402 /* RX */
403 /******/
404
405 /*
406  * Calculate the RX filter to be set in the HW.
407  */
408 u32 ath9k_htc_calcrxfilter(struct ath9k_htc_priv *priv)
409 {
410 #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
411
412         struct ath_hw *ah = priv->ah;
413         u32 rfilt;
414
415         rfilt = (ath9k_hw_getrxfilter(ah) & RX_FILTER_PRESERVE)
416                 | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
417                 | ATH9K_RX_FILTER_MCAST;
418
419         if (priv->rxfilter & FIF_PROBE_REQ)
420                 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
421
422         /*
423          * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
424          * mode interface or when in monitor mode. AP mode does not need this
425          * since it receives all in-BSS frames anyway.
426          */
427         if (((ah->opmode != NL80211_IFTYPE_AP) &&
428              (priv->rxfilter & FIF_PROMISC_IN_BSS)) ||
429             ah->is_monitoring)
430                 rfilt |= ATH9K_RX_FILTER_PROM;
431
432         if (priv->rxfilter & FIF_CONTROL)
433                 rfilt |= ATH9K_RX_FILTER_CONTROL;
434
435         if ((ah->opmode == NL80211_IFTYPE_STATION) &&
436             !(priv->rxfilter & FIF_BCN_PRBRESP_PROMISC))
437                 rfilt |= ATH9K_RX_FILTER_MYBEACON;
438         else
439                 rfilt |= ATH9K_RX_FILTER_BEACON;
440
441         if (conf_is_ht(&priv->hw->conf)) {
442                 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
443                 rfilt |= ATH9K_RX_FILTER_UNCOMP_BA_BAR;
444         }
445
446         if (priv->rxfilter & FIF_PSPOLL)
447                 rfilt |= ATH9K_RX_FILTER_PSPOLL;
448
449         return rfilt;
450
451 #undef RX_FILTER_PRESERVE
452 }
453
454 /*
455  * Recv initialization for opmode change.
456  */
457 static void ath9k_htc_opmode_init(struct ath9k_htc_priv *priv)
458 {
459         struct ath_hw *ah = priv->ah;
460         u32 rfilt, mfilt[2];
461
462         /* configure rx filter */
463         rfilt = ath9k_htc_calcrxfilter(priv);
464         ath9k_hw_setrxfilter(ah, rfilt);
465
466         /* calculate and install multicast filter */
467         mfilt[0] = mfilt[1] = ~0;
468         ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
469 }
470
471 void ath9k_host_rx_init(struct ath9k_htc_priv *priv)
472 {
473         ath9k_hw_rxena(priv->ah);
474         ath9k_htc_opmode_init(priv);
475         ath9k_hw_startpcureceive(priv->ah, (priv->op_flags & OP_SCANNING));
476         priv->rx.last_rssi = ATH_RSSI_DUMMY_MARKER;
477 }
478
479 static void ath9k_process_rate(struct ieee80211_hw *hw,
480                                struct ieee80211_rx_status *rxs,
481                                u8 rx_rate, u8 rs_flags)
482 {
483         struct ieee80211_supported_band *sband;
484         enum ieee80211_band band;
485         unsigned int i = 0;
486
487         if (rx_rate & 0x80) {
488                 /* HT rate */
489                 rxs->flag |= RX_FLAG_HT;
490                 if (rs_flags & ATH9K_RX_2040)
491                         rxs->flag |= RX_FLAG_40MHZ;
492                 if (rs_flags & ATH9K_RX_GI)
493                         rxs->flag |= RX_FLAG_SHORT_GI;
494                 rxs->rate_idx = rx_rate & 0x7f;
495                 return;
496         }
497
498         band = hw->conf.channel->band;
499         sband = hw->wiphy->bands[band];
500
501         for (i = 0; i < sband->n_bitrates; i++) {
502                 if (sband->bitrates[i].hw_value == rx_rate) {
503                         rxs->rate_idx = i;
504                         return;
505                 }
506                 if (sband->bitrates[i].hw_value_short == rx_rate) {
507                         rxs->rate_idx = i;
508                         rxs->flag |= RX_FLAG_SHORTPRE;
509                         return;
510                 }
511         }
512
513 }
514
515 static bool ath9k_rx_prepare(struct ath9k_htc_priv *priv,
516                              struct ath9k_htc_rxbuf *rxbuf,
517                              struct ieee80211_rx_status *rx_status)
518
519 {
520         struct ieee80211_hdr *hdr;
521         struct ieee80211_hw *hw = priv->hw;
522         struct sk_buff *skb = rxbuf->skb;
523         struct ath_common *common = ath9k_hw_common(priv->ah);
524         struct ath_htc_rx_status *rxstatus;
525         int hdrlen, padpos, padsize;
526         int last_rssi = ATH_RSSI_DUMMY_MARKER;
527         __le16 fc;
528
529         if (skb->len < HTC_RX_FRAME_HEADER_SIZE) {
530                 ath_err(common, "Corrupted RX frame, dropping (len: %d)\n",
531                         skb->len);
532                 goto rx_next;
533         }
534
535         rxstatus = (struct ath_htc_rx_status *)skb->data;
536
537         if (be16_to_cpu(rxstatus->rs_datalen) -
538             (skb->len - HTC_RX_FRAME_HEADER_SIZE) != 0) {
539                 ath_err(common,
540                         "Corrupted RX data len, dropping (dlen: %d, skblen: %d)\n",
541                         rxstatus->rs_datalen, skb->len);
542                 goto rx_next;
543         }
544
545         ath9k_htc_err_stat_rx(priv, rxstatus);
546
547         /* Get the RX status information */
548         memcpy(&rxbuf->rxstatus, rxstatus, HTC_RX_FRAME_HEADER_SIZE);
549         skb_pull(skb, HTC_RX_FRAME_HEADER_SIZE);
550
551         hdr = (struct ieee80211_hdr *)skb->data;
552         fc = hdr->frame_control;
553         hdrlen = ieee80211_get_hdrlen_from_skb(skb);
554
555         padpos = ath9k_cmn_padpos(fc);
556
557         padsize = padpos & 3;
558         if (padsize && skb->len >= padpos+padsize+FCS_LEN) {
559                 memmove(skb->data + padsize, skb->data, padpos);
560                 skb_pull(skb, padsize);
561         }
562
563         memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
564
565         if (rxbuf->rxstatus.rs_status != 0) {
566                 if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_CRC)
567                         rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
568                 if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_PHY)
569                         goto rx_next;
570
571                 if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_DECRYPT) {
572                         /* FIXME */
573                 } else if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_MIC) {
574                         if (ieee80211_is_ctl(fc))
575                                 /*
576                                  * Sometimes, we get invalid
577                                  * MIC failures on valid control frames.
578                                  * Remove these mic errors.
579                                  */
580                                 rxbuf->rxstatus.rs_status &= ~ATH9K_RXERR_MIC;
581                         else
582                                 rx_status->flag |= RX_FLAG_MMIC_ERROR;
583                 }
584
585                 /*
586                  * Reject error frames with the exception of
587                  * decryption and MIC failures. For monitor mode,
588                  * we also ignore the CRC error.
589                  */
590                 if (priv->ah->opmode == NL80211_IFTYPE_MONITOR) {
591                         if (rxbuf->rxstatus.rs_status &
592                             ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
593                               ATH9K_RXERR_CRC))
594                                 goto rx_next;
595                 } else {
596                         if (rxbuf->rxstatus.rs_status &
597                             ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
598                                 goto rx_next;
599                         }
600                 }
601         }
602
603         if (!(rxbuf->rxstatus.rs_status & ATH9K_RXERR_DECRYPT)) {
604                 u8 keyix;
605                 keyix = rxbuf->rxstatus.rs_keyix;
606                 if (keyix != ATH9K_RXKEYIX_INVALID) {
607                         rx_status->flag |= RX_FLAG_DECRYPTED;
608                 } else if (ieee80211_has_protected(fc) &&
609                            skb->len >= hdrlen + 4) {
610                         keyix = skb->data[hdrlen + 3] >> 6;
611                         if (test_bit(keyix, common->keymap))
612                                 rx_status->flag |= RX_FLAG_DECRYPTED;
613                 }
614         }
615
616         ath9k_process_rate(hw, rx_status, rxbuf->rxstatus.rs_rate,
617                            rxbuf->rxstatus.rs_flags);
618
619         if (rxbuf->rxstatus.rs_rssi != ATH9K_RSSI_BAD &&
620             !rxbuf->rxstatus.rs_moreaggr)
621                 ATH_RSSI_LPF(priv->rx.last_rssi,
622                              rxbuf->rxstatus.rs_rssi);
623
624         last_rssi = priv->rx.last_rssi;
625
626         if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
627                 rxbuf->rxstatus.rs_rssi = ATH_EP_RND(last_rssi,
628                                                      ATH_RSSI_EP_MULTIPLIER);
629
630         if (rxbuf->rxstatus.rs_rssi < 0)
631                 rxbuf->rxstatus.rs_rssi = 0;
632
633         if (ieee80211_is_beacon(fc))
634                 priv->ah->stats.avgbrssi = rxbuf->rxstatus.rs_rssi;
635
636         rx_status->mactime = be64_to_cpu(rxbuf->rxstatus.rs_tstamp);
637         rx_status->band = hw->conf.channel->band;
638         rx_status->freq = hw->conf.channel->center_freq;
639         rx_status->signal =  rxbuf->rxstatus.rs_rssi + ATH_DEFAULT_NOISE_FLOOR;
640         rx_status->antenna = rxbuf->rxstatus.rs_antenna;
641         rx_status->flag |= RX_FLAG_MACTIME_MPDU;
642
643         return true;
644
645 rx_next:
646         return false;
647 }
648
649 /*
650  * FIXME: Handle FLUSH later on.
651  */
652 void ath9k_rx_tasklet(unsigned long data)
653 {
654         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
655         struct ath9k_htc_rxbuf *rxbuf = NULL, *tmp_buf = NULL;
656         struct ieee80211_rx_status rx_status;
657         struct sk_buff *skb;
658         unsigned long flags;
659         struct ieee80211_hdr *hdr;
660
661         do {
662                 spin_lock_irqsave(&priv->rx.rxbuflock, flags);
663                 list_for_each_entry(tmp_buf, &priv->rx.rxbuf, list) {
664                         if (tmp_buf->in_process) {
665                                 rxbuf = tmp_buf;
666                                 break;
667                         }
668                 }
669
670                 if (rxbuf == NULL) {
671                         spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
672                         break;
673                 }
674
675                 if (!rxbuf->skb)
676                         goto requeue;
677
678                 if (!ath9k_rx_prepare(priv, rxbuf, &rx_status)) {
679                         dev_kfree_skb_any(rxbuf->skb);
680                         goto requeue;
681                 }
682
683                 memcpy(IEEE80211_SKB_RXCB(rxbuf->skb), &rx_status,
684                        sizeof(struct ieee80211_rx_status));
685                 skb = rxbuf->skb;
686                 hdr = (struct ieee80211_hdr *) skb->data;
687
688                 if (ieee80211_is_beacon(hdr->frame_control) && priv->ps_enabled)
689                                 ieee80211_queue_work(priv->hw, &priv->ps_work);
690
691                 spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
692
693                 ieee80211_rx(priv->hw, skb);
694
695                 spin_lock_irqsave(&priv->rx.rxbuflock, flags);
696 requeue:
697                 rxbuf->in_process = false;
698                 rxbuf->skb = NULL;
699                 list_move_tail(&rxbuf->list, &priv->rx.rxbuf);
700                 rxbuf = NULL;
701                 spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
702         } while (1);
703
704 }
705
706 void ath9k_htc_rxep(void *drv_priv, struct sk_buff *skb,
707                     enum htc_endpoint_id ep_id)
708 {
709         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)drv_priv;
710         struct ath_hw *ah = priv->ah;
711         struct ath_common *common = ath9k_hw_common(ah);
712         struct ath9k_htc_rxbuf *rxbuf = NULL, *tmp_buf = NULL;
713
714         spin_lock(&priv->rx.rxbuflock);
715         list_for_each_entry(tmp_buf, &priv->rx.rxbuf, list) {
716                 if (!tmp_buf->in_process) {
717                         rxbuf = tmp_buf;
718                         break;
719                 }
720         }
721         spin_unlock(&priv->rx.rxbuflock);
722
723         if (rxbuf == NULL) {
724                 ath_dbg(common, ATH_DBG_ANY,
725                         "No free RX buffer\n");
726                 goto err;
727         }
728
729         spin_lock(&priv->rx.rxbuflock);
730         rxbuf->skb = skb;
731         rxbuf->in_process = true;
732         spin_unlock(&priv->rx.rxbuflock);
733
734         tasklet_schedule(&priv->rx_tasklet);
735         return;
736 err:
737         dev_kfree_skb_any(skb);
738 }
739
740 /* FIXME: Locking for cleanup/init */
741
742 void ath9k_rx_cleanup(struct ath9k_htc_priv *priv)
743 {
744         struct ath9k_htc_rxbuf *rxbuf, *tbuf;
745
746         list_for_each_entry_safe(rxbuf, tbuf, &priv->rx.rxbuf, list) {
747                 list_del(&rxbuf->list);
748                 if (rxbuf->skb)
749                         dev_kfree_skb_any(rxbuf->skb);
750                 kfree(rxbuf);
751         }
752 }
753
754 int ath9k_rx_init(struct ath9k_htc_priv *priv)
755 {
756         struct ath_hw *ah = priv->ah;
757         struct ath_common *common = ath9k_hw_common(ah);
758         struct ath9k_htc_rxbuf *rxbuf;
759         int i = 0;
760
761         INIT_LIST_HEAD(&priv->rx.rxbuf);
762         spin_lock_init(&priv->rx.rxbuflock);
763
764         for (i = 0; i < ATH9K_HTC_RXBUF; i++) {
765                 rxbuf = kzalloc(sizeof(struct ath9k_htc_rxbuf), GFP_KERNEL);
766                 if (rxbuf == NULL) {
767                         ath_err(common, "Unable to allocate RX buffers\n");
768                         goto err;
769                 }
770                 list_add_tail(&rxbuf->list, &priv->rx.rxbuf);
771         }
772
773         return 0;
774
775 err:
776         ath9k_rx_cleanup(priv);
777         return -ENOMEM;
778 }