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