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