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