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