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