]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/net/wireless/ath/ath9k/htc_drv_txrx.c
Input: wacom - fix pressure in Cintiq 21UX2
[net-next-2.6.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[WME_AC_VO];
38         case 1:
39                 return hwq_map[WME_AC_VI];
40         case 2:
41                 return hwq_map[WME_AC_BE];
42         case 3:
43                 return hwq_map[WME_AC_BK];
44         default:
45                 return hwq_map[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 static bool ath9k_htc_check_tx_aggr(struct ath9k_htc_priv *priv,
191                                     struct ath9k_htc_sta *ista, u8 tid)
192 {
193         bool ret = false;
194
195         spin_lock_bh(&priv->tx_lock);
196         if ((tid < ATH9K_HTC_MAX_TID) && (ista->tid_state[tid] == AGGR_STOP))
197                 ret = true;
198         spin_unlock_bh(&priv->tx_lock);
199
200         return ret;
201 }
202
203 void ath9k_tx_tasklet(unsigned long data)
204 {
205         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
206         struct ieee80211_sta *sta;
207         struct ieee80211_hdr *hdr;
208         struct ieee80211_tx_info *tx_info;
209         struct sk_buff *skb = NULL;
210         __le16 fc;
211
212         while ((skb = skb_dequeue(&priv->tx_queue)) != NULL) {
213
214                 hdr = (struct ieee80211_hdr *) skb->data;
215                 fc = hdr->frame_control;
216                 tx_info = IEEE80211_SKB_CB(skb);
217
218                 memset(&tx_info->status, 0, sizeof(tx_info->status));
219
220                 rcu_read_lock();
221
222                 sta = ieee80211_find_sta(priv->vif, hdr->addr1);
223                 if (!sta) {
224                         rcu_read_unlock();
225                         ieee80211_tx_status(priv->hw, skb);
226                         continue;
227                 }
228
229                 /* Check if we need to start aggregation */
230
231                 if (sta && conf_is_ht(&priv->hw->conf) &&
232                     !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
233                         if (ieee80211_is_data_qos(fc)) {
234                                 u8 *qc, tid;
235                                 struct ath9k_htc_sta *ista;
236
237                                 qc = ieee80211_get_qos_ctl(hdr);
238                                 tid = qc[0] & 0xf;
239                                 ista = (struct ath9k_htc_sta *)sta->drv_priv;
240
241                                 if (ath9k_htc_check_tx_aggr(priv, ista, tid)) {
242                                         ieee80211_start_tx_ba_session(sta, tid);
243                                         spin_lock_bh(&priv->tx_lock);
244                                         ista->tid_state[tid] = AGGR_PROGRESS;
245                                         spin_unlock_bh(&priv->tx_lock);
246                                 }
247                         }
248                 }
249
250                 rcu_read_unlock();
251
252                 /* Send status to mac80211 */
253                 ieee80211_tx_status(priv->hw, skb);
254         }
255
256         /* Wake TX queues if needed */
257         spin_lock_bh(&priv->tx_lock);
258         if (priv->tx_queues_stop) {
259                 priv->tx_queues_stop = false;
260                 spin_unlock_bh(&priv->tx_lock);
261                 ath_print(ath9k_hw_common(priv->ah), ATH_DBG_XMIT,
262                           "Waking up TX queues\n");
263                 ieee80211_wake_queues(priv->hw);
264                 return;
265         }
266         spin_unlock_bh(&priv->tx_lock);
267 }
268
269 void ath9k_htc_txep(void *drv_priv, struct sk_buff *skb,
270                     enum htc_endpoint_id ep_id, bool txok)
271 {
272         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) drv_priv;
273         struct ath_common *common = ath9k_hw_common(priv->ah);
274         struct ieee80211_tx_info *tx_info;
275
276         if (!skb)
277                 return;
278
279         if (ep_id == priv->mgmt_ep) {
280                 skb_pull(skb, sizeof(struct tx_mgmt_hdr));
281         } else if ((ep_id == priv->data_bk_ep) ||
282                    (ep_id == priv->data_be_ep) ||
283                    (ep_id == priv->data_vi_ep) ||
284                    (ep_id == priv->data_vo_ep)) {
285                 skb_pull(skb, sizeof(struct tx_frame_hdr));
286         } else {
287                 ath_print(common, ATH_DBG_FATAL,
288                           "Unsupported TX EPID: %d\n", ep_id);
289                 dev_kfree_skb_any(skb);
290                 return;
291         }
292
293         tx_info = IEEE80211_SKB_CB(skb);
294
295         if (txok)
296                 tx_info->flags |= IEEE80211_TX_STAT_ACK;
297
298         skb_queue_tail(&priv->tx_queue, skb);
299         tasklet_schedule(&priv->tx_tasklet);
300 }
301
302 int ath9k_tx_init(struct ath9k_htc_priv *priv)
303 {
304         skb_queue_head_init(&priv->tx_queue);
305         return 0;
306 }
307
308 void ath9k_tx_cleanup(struct ath9k_htc_priv *priv)
309 {
310
311 }
312
313 bool ath9k_htc_txq_setup(struct ath9k_htc_priv *priv, int subtype)
314 {
315         struct ath_hw *ah = priv->ah;
316         struct ath_common *common = ath9k_hw_common(ah);
317         struct ath9k_tx_queue_info qi;
318         int qnum;
319
320         memset(&qi, 0, sizeof(qi));
321         ATH9K_HTC_INIT_TXQ(subtype);
322
323         qnum = ath9k_hw_setuptxqueue(priv->ah, ATH9K_TX_QUEUE_DATA, &qi);
324         if (qnum == -1)
325                 return false;
326
327         if (qnum >= ARRAY_SIZE(priv->hwq_map)) {
328                 ath_print(common, ATH_DBG_FATAL,
329                           "qnum %u out of range, max %u!\n",
330                           qnum, (unsigned int)ARRAY_SIZE(priv->hwq_map));
331                 ath9k_hw_releasetxqueue(ah, qnum);
332                 return false;
333         }
334
335         priv->hwq_map[subtype] = qnum;
336         return true;
337 }
338
339 int ath9k_htc_cabq_setup(struct ath9k_htc_priv *priv)
340 {
341         struct ath9k_tx_queue_info qi;
342
343         memset(&qi, 0, sizeof(qi));
344         ATH9K_HTC_INIT_TXQ(0);
345
346         return ath9k_hw_setuptxqueue(priv->ah, ATH9K_TX_QUEUE_CAB, &qi);
347 }
348
349 /******/
350 /* RX */
351 /******/
352
353 /*
354  * Calculate the RX filter to be set in the HW.
355  */
356 u32 ath9k_htc_calcrxfilter(struct ath9k_htc_priv *priv)
357 {
358 #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
359
360         struct ath_hw *ah = priv->ah;
361         u32 rfilt;
362
363         rfilt = (ath9k_hw_getrxfilter(ah) & RX_FILTER_PRESERVE)
364                 | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
365                 | ATH9K_RX_FILTER_MCAST;
366
367         /* If not a STA, enable processing of Probe Requests */
368         if (ah->opmode != NL80211_IFTYPE_STATION)
369                 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
370
371         /*
372          * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
373          * mode interface or when in monitor mode. AP mode does not need this
374          * since it receives all in-BSS frames anyway.
375          */
376         if (((ah->opmode != NL80211_IFTYPE_AP) &&
377              (priv->rxfilter & FIF_PROMISC_IN_BSS)) ||
378             (ah->opmode == NL80211_IFTYPE_MONITOR))
379                 rfilt |= ATH9K_RX_FILTER_PROM;
380
381         if (priv->rxfilter & FIF_CONTROL)
382                 rfilt |= ATH9K_RX_FILTER_CONTROL;
383
384         if ((ah->opmode == NL80211_IFTYPE_STATION) &&
385             !(priv->rxfilter & FIF_BCN_PRBRESP_PROMISC))
386                 rfilt |= ATH9K_RX_FILTER_MYBEACON;
387         else
388                 rfilt |= ATH9K_RX_FILTER_BEACON;
389
390         if (conf_is_ht(&priv->hw->conf))
391                 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
392
393         return rfilt;
394
395 #undef RX_FILTER_PRESERVE
396 }
397
398 /*
399  * Recv initialization for opmode change.
400  */
401 static void ath9k_htc_opmode_init(struct ath9k_htc_priv *priv)
402 {
403         struct ath_hw *ah = priv->ah;
404         struct ath_common *common = ath9k_hw_common(ah);
405
406         u32 rfilt, mfilt[2];
407
408         /* configure rx filter */
409         rfilt = ath9k_htc_calcrxfilter(priv);
410         ath9k_hw_setrxfilter(ah, rfilt);
411
412         /* configure bssid mask */
413         if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
414                 ath_hw_setbssidmask(common);
415
416         /* configure operational mode */
417         ath9k_hw_setopmode(ah);
418
419         /* calculate and install multicast filter */
420         mfilt[0] = mfilt[1] = ~0;
421         ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
422 }
423
424 void ath9k_host_rx_init(struct ath9k_htc_priv *priv)
425 {
426         ath9k_hw_rxena(priv->ah);
427         ath9k_htc_opmode_init(priv);
428         ath9k_hw_startpcureceive(priv->ah, (priv->op_flags & OP_SCANNING));
429         priv->rx.last_rssi = ATH_RSSI_DUMMY_MARKER;
430 }
431
432 static void ath9k_process_rate(struct ieee80211_hw *hw,
433                                struct ieee80211_rx_status *rxs,
434                                u8 rx_rate, u8 rs_flags)
435 {
436         struct ieee80211_supported_band *sband;
437         enum ieee80211_band band;
438         unsigned int i = 0;
439
440         if (rx_rate & 0x80) {
441                 /* HT rate */
442                 rxs->flag |= RX_FLAG_HT;
443                 if (rs_flags & ATH9K_RX_2040)
444                         rxs->flag |= RX_FLAG_40MHZ;
445                 if (rs_flags & ATH9K_RX_GI)
446                         rxs->flag |= RX_FLAG_SHORT_GI;
447                 rxs->rate_idx = rx_rate & 0x7f;
448                 return;
449         }
450
451         band = hw->conf.channel->band;
452         sband = hw->wiphy->bands[band];
453
454         for (i = 0; i < sband->n_bitrates; i++) {
455                 if (sband->bitrates[i].hw_value == rx_rate) {
456                         rxs->rate_idx = i;
457                         return;
458                 }
459                 if (sband->bitrates[i].hw_value_short == rx_rate) {
460                         rxs->rate_idx = i;
461                         rxs->flag |= RX_FLAG_SHORTPRE;
462                         return;
463                 }
464         }
465
466 }
467
468 static bool ath9k_rx_prepare(struct ath9k_htc_priv *priv,
469                              struct ath9k_htc_rxbuf *rxbuf,
470                              struct ieee80211_rx_status *rx_status)
471
472 {
473         struct ieee80211_hdr *hdr;
474         struct ieee80211_hw *hw = priv->hw;
475         struct sk_buff *skb = rxbuf->skb;
476         struct ath_common *common = ath9k_hw_common(priv->ah);
477         struct ath_htc_rx_status *rxstatus;
478         int hdrlen, padpos, padsize;
479         int last_rssi = ATH_RSSI_DUMMY_MARKER;
480         __le16 fc;
481
482         if (skb->len <= HTC_RX_FRAME_HEADER_SIZE) {
483                 ath_print(common, ATH_DBG_FATAL,
484                           "Corrupted RX frame, dropping\n");
485                 goto rx_next;
486         }
487
488         rxstatus = (struct ath_htc_rx_status *)skb->data;
489
490         if (be16_to_cpu(rxstatus->rs_datalen) -
491             (skb->len - HTC_RX_FRAME_HEADER_SIZE) != 0) {
492                 ath_print(common, ATH_DBG_FATAL,
493                           "Corrupted RX data len, dropping "
494                           "(dlen: %d, skblen: %d)\n",
495                           rxstatus->rs_datalen, skb->len);
496                 goto rx_next;
497         }
498
499         /* Get the RX status information */
500         memcpy(&rxbuf->rxstatus, rxstatus, HTC_RX_FRAME_HEADER_SIZE);
501         skb_pull(skb, HTC_RX_FRAME_HEADER_SIZE);
502
503         hdr = (struct ieee80211_hdr *)skb->data;
504         fc = hdr->frame_control;
505         hdrlen = ieee80211_get_hdrlen_from_skb(skb);
506
507         padpos = ath9k_cmn_padpos(fc);
508
509         padsize = padpos & 3;
510         if (padsize && skb->len >= padpos+padsize+FCS_LEN) {
511                 memmove(skb->data + padsize, skb->data, padpos);
512                 skb_pull(skb, padsize);
513         }
514
515         memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
516
517         if (rxbuf->rxstatus.rs_status != 0) {
518                 if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_CRC)
519                         rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
520                 if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_PHY)
521                         goto rx_next;
522
523                 if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_DECRYPT) {
524                         /* FIXME */
525                 } else if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_MIC) {
526                         if (ieee80211_is_ctl(fc))
527                                 /*
528                                  * Sometimes, we get invalid
529                                  * MIC failures on valid control frames.
530                                  * Remove these mic errors.
531                                  */
532                                 rxbuf->rxstatus.rs_status &= ~ATH9K_RXERR_MIC;
533                         else
534                                 rx_status->flag |= RX_FLAG_MMIC_ERROR;
535                 }
536
537                 /*
538                  * Reject error frames with the exception of
539                  * decryption and MIC failures. For monitor mode,
540                  * we also ignore the CRC error.
541                  */
542                 if (priv->ah->opmode == NL80211_IFTYPE_MONITOR) {
543                         if (rxbuf->rxstatus.rs_status &
544                             ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
545                               ATH9K_RXERR_CRC))
546                                 goto rx_next;
547                 } else {
548                         if (rxbuf->rxstatus.rs_status &
549                             ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
550                                 goto rx_next;
551                         }
552                 }
553         }
554
555         if (!(rxbuf->rxstatus.rs_status & ATH9K_RXERR_DECRYPT)) {
556                 u8 keyix;
557                 keyix = rxbuf->rxstatus.rs_keyix;
558                 if (keyix != ATH9K_RXKEYIX_INVALID) {
559                         rx_status->flag |= RX_FLAG_DECRYPTED;
560                 } else if (ieee80211_has_protected(fc) &&
561                            skb->len >= hdrlen + 4) {
562                         keyix = skb->data[hdrlen + 3] >> 6;
563                         if (test_bit(keyix, common->keymap))
564                                 rx_status->flag |= RX_FLAG_DECRYPTED;
565                 }
566         }
567
568         ath9k_process_rate(hw, rx_status, rxbuf->rxstatus.rs_rate,
569                            rxbuf->rxstatus.rs_flags);
570
571         if (priv->op_flags & OP_ASSOCIATED) {
572                 if (rxbuf->rxstatus.rs_rssi != ATH9K_RSSI_BAD &&
573                     !rxbuf->rxstatus.rs_moreaggr)
574                         ATH_RSSI_LPF(priv->rx.last_rssi,
575                                      rxbuf->rxstatus.rs_rssi);
576
577                 last_rssi = priv->rx.last_rssi;
578
579                 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
580                         rxbuf->rxstatus.rs_rssi = ATH_EP_RND(last_rssi,
581                                                              ATH_RSSI_EP_MULTIPLIER);
582
583                 if (rxbuf->rxstatus.rs_rssi < 0)
584                         rxbuf->rxstatus.rs_rssi = 0;
585
586                 if (ieee80211_is_beacon(fc))
587                         priv->ah->stats.avgbrssi = rxbuf->rxstatus.rs_rssi;
588         }
589
590         rx_status->mactime = be64_to_cpu(rxbuf->rxstatus.rs_tstamp);
591         rx_status->band = hw->conf.channel->band;
592         rx_status->freq = hw->conf.channel->center_freq;
593         rx_status->signal =  rxbuf->rxstatus.rs_rssi + ATH_DEFAULT_NOISE_FLOOR;
594         rx_status->antenna = rxbuf->rxstatus.rs_antenna;
595         rx_status->flag |= RX_FLAG_TSFT;
596
597         return true;
598
599 rx_next:
600         return false;
601 }
602
603 /*
604  * FIXME: Handle FLUSH later on.
605  */
606 void ath9k_rx_tasklet(unsigned long data)
607 {
608         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
609         struct ath9k_htc_rxbuf *rxbuf = NULL, *tmp_buf = NULL;
610         struct ieee80211_rx_status rx_status;
611         struct sk_buff *skb;
612         unsigned long flags;
613         struct ieee80211_hdr *hdr;
614
615         do {
616                 spin_lock_irqsave(&priv->rx.rxbuflock, flags);
617                 list_for_each_entry(tmp_buf, &priv->rx.rxbuf, list) {
618                         if (tmp_buf->in_process) {
619                                 rxbuf = tmp_buf;
620                                 break;
621                         }
622                 }
623
624                 if (rxbuf == NULL) {
625                         spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
626                         break;
627                 }
628
629                 if (!rxbuf->skb)
630                         goto requeue;
631
632                 if (!ath9k_rx_prepare(priv, rxbuf, &rx_status)) {
633                         dev_kfree_skb_any(rxbuf->skb);
634                         goto requeue;
635                 }
636
637                 memcpy(IEEE80211_SKB_RXCB(rxbuf->skb), &rx_status,
638                        sizeof(struct ieee80211_rx_status));
639                 skb = rxbuf->skb;
640                 hdr = (struct ieee80211_hdr *) skb->data;
641
642                 if (ieee80211_is_beacon(hdr->frame_control) && priv->ps_enabled)
643                                 ieee80211_queue_work(priv->hw, &priv->ps_work);
644
645                 spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
646
647                 ieee80211_rx(priv->hw, skb);
648
649                 spin_lock_irqsave(&priv->rx.rxbuflock, flags);
650 requeue:
651                 rxbuf->in_process = false;
652                 rxbuf->skb = NULL;
653                 list_move_tail(&rxbuf->list, &priv->rx.rxbuf);
654                 rxbuf = NULL;
655                 spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
656         } while (1);
657
658 }
659
660 void ath9k_htc_rxep(void *drv_priv, struct sk_buff *skb,
661                     enum htc_endpoint_id ep_id)
662 {
663         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)drv_priv;
664         struct ath_hw *ah = priv->ah;
665         struct ath_common *common = ath9k_hw_common(ah);
666         struct ath9k_htc_rxbuf *rxbuf = NULL, *tmp_buf = NULL;
667
668         spin_lock(&priv->rx.rxbuflock);
669         list_for_each_entry(tmp_buf, &priv->rx.rxbuf, list) {
670                 if (!tmp_buf->in_process) {
671                         rxbuf = tmp_buf;
672                         break;
673                 }
674         }
675         spin_unlock(&priv->rx.rxbuflock);
676
677         if (rxbuf == NULL) {
678                 ath_print(common, ATH_DBG_ANY,
679                           "No free RX buffer\n");
680                 goto err;
681         }
682
683         spin_lock(&priv->rx.rxbuflock);
684         rxbuf->skb = skb;
685         rxbuf->in_process = true;
686         spin_unlock(&priv->rx.rxbuflock);
687
688         tasklet_schedule(&priv->rx_tasklet);
689         return;
690 err:
691         dev_kfree_skb_any(skb);
692 }
693
694 /* FIXME: Locking for cleanup/init */
695
696 void ath9k_rx_cleanup(struct ath9k_htc_priv *priv)
697 {
698         struct ath9k_htc_rxbuf *rxbuf, *tbuf;
699
700         list_for_each_entry_safe(rxbuf, tbuf, &priv->rx.rxbuf, list) {
701                 list_del(&rxbuf->list);
702                 if (rxbuf->skb)
703                         dev_kfree_skb_any(rxbuf->skb);
704                 kfree(rxbuf);
705         }
706 }
707
708 int ath9k_rx_init(struct ath9k_htc_priv *priv)
709 {
710         struct ath_hw *ah = priv->ah;
711         struct ath_common *common = ath9k_hw_common(ah);
712         struct ath9k_htc_rxbuf *rxbuf;
713         int i = 0;
714
715         INIT_LIST_HEAD(&priv->rx.rxbuf);
716         spin_lock_init(&priv->rx.rxbuflock);
717
718         for (i = 0; i < ATH9K_HTC_RXBUF; i++) {
719                 rxbuf = kzalloc(sizeof(struct ath9k_htc_rxbuf), GFP_KERNEL);
720                 if (rxbuf == NULL) {
721                         ath_print(common, ATH_DBG_FATAL,
722                                   "Unable to allocate RX buffers\n");
723                         goto err;
724                 }
725                 list_add_tail(&rxbuf->list, &priv->rx.rxbuf);
726         }
727
728         return 0;
729
730 err:
731         ath9k_rx_cleanup(priv);
732         return -ENOMEM;
733 }