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