ath9k_htc: Fix TKIP encryption
[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 ieee80211_tx_info *tx_info;
248
249         if (!skb)
250                 return;
251
252         if (ep_id == priv->mgmt_ep)
253                 skb_pull(skb, sizeof(struct tx_mgmt_hdr));
254         else
255                 /* TODO: Check for cab/uapsd/data */
256                 skb_pull(skb, sizeof(struct tx_frame_hdr));
257
258         tx_info = IEEE80211_SKB_CB(skb);
259
260         if (txok)
261                 tx_info->flags |= IEEE80211_TX_STAT_ACK;
262
263         skb_queue_tail(&priv->tx_queue, skb);
264         tasklet_schedule(&priv->tx_tasklet);
265 }
266
267 int ath9k_tx_init(struct ath9k_htc_priv *priv)
268 {
269         skb_queue_head_init(&priv->tx_queue);
270         return 0;
271 }
272
273 void ath9k_tx_cleanup(struct ath9k_htc_priv *priv)
274 {
275
276 }
277
278 bool ath9k_htc_txq_setup(struct ath9k_htc_priv *priv,
279                          enum ath9k_tx_queue_subtype subtype)
280 {
281         struct ath_hw *ah = priv->ah;
282         struct ath_common *common = ath9k_hw_common(ah);
283         struct ath9k_tx_queue_info qi;
284         int qnum;
285
286         memset(&qi, 0, sizeof(qi));
287
288         qi.tqi_subtype = subtype;
289         qi.tqi_aifs = ATH9K_TXQ_USEDEFAULT;
290         qi.tqi_cwmin = ATH9K_TXQ_USEDEFAULT;
291         qi.tqi_cwmax = ATH9K_TXQ_USEDEFAULT;
292         qi.tqi_physCompBuf = 0;
293         qi.tqi_qflags = TXQ_FLAG_TXEOLINT_ENABLE | TXQ_FLAG_TXDESCINT_ENABLE;
294
295         qnum = ath9k_hw_setuptxqueue(priv->ah, ATH9K_TX_QUEUE_DATA, &qi);
296         if (qnum == -1)
297                 return false;
298
299         if (qnum >= ARRAY_SIZE(priv->hwq_map)) {
300                 ath_print(common, ATH_DBG_FATAL,
301                           "qnum %u out of range, max %u!\n",
302                           qnum, (unsigned int)ARRAY_SIZE(priv->hwq_map));
303                 ath9k_hw_releasetxqueue(ah, qnum);
304                 return false;
305         }
306
307         priv->hwq_map[subtype] = qnum;
308         return true;
309 }
310
311 /******/
312 /* RX */
313 /******/
314
315 /*
316  * Calculate the RX filter to be set in the HW.
317  */
318 u32 ath9k_htc_calcrxfilter(struct ath9k_htc_priv *priv)
319 {
320 #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
321
322         struct ath_hw *ah = priv->ah;
323         u32 rfilt;
324
325         rfilt = (ath9k_hw_getrxfilter(ah) & RX_FILTER_PRESERVE)
326                 | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
327                 | ATH9K_RX_FILTER_MCAST;
328
329         /* If not a STA, enable processing of Probe Requests */
330         if (ah->opmode != NL80211_IFTYPE_STATION)
331                 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
332
333         /*
334          * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
335          * mode interface or when in monitor mode. AP mode does not need this
336          * since it receives all in-BSS frames anyway.
337          */
338         if (((ah->opmode != NL80211_IFTYPE_AP) &&
339              (priv->rxfilter & FIF_PROMISC_IN_BSS)) ||
340             (ah->opmode == NL80211_IFTYPE_MONITOR))
341                 rfilt |= ATH9K_RX_FILTER_PROM;
342
343         if (priv->rxfilter & FIF_CONTROL)
344                 rfilt |= ATH9K_RX_FILTER_CONTROL;
345
346         if ((ah->opmode == NL80211_IFTYPE_STATION) &&
347             !(priv->rxfilter & FIF_BCN_PRBRESP_PROMISC))
348                 rfilt |= ATH9K_RX_FILTER_MYBEACON;
349         else
350                 rfilt |= ATH9K_RX_FILTER_BEACON;
351
352         if (conf_is_ht(&priv->hw->conf))
353                 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
354
355         return rfilt;
356
357 #undef RX_FILTER_PRESERVE
358 }
359
360 /*
361  * Recv initialization for opmode change.
362  */
363 static void ath9k_htc_opmode_init(struct ath9k_htc_priv *priv)
364 {
365         struct ath_hw *ah = priv->ah;
366         struct ath_common *common = ath9k_hw_common(ah);
367
368         u32 rfilt, mfilt[2];
369
370         /* configure rx filter */
371         rfilt = ath9k_htc_calcrxfilter(priv);
372         ath9k_hw_setrxfilter(ah, rfilt);
373
374         /* configure bssid mask */
375         if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
376                 ath_hw_setbssidmask(common);
377
378         /* configure operational mode */
379         ath9k_hw_setopmode(ah);
380
381         /* Handle any link-level address change. */
382         ath9k_hw_setmac(ah, common->macaddr);
383
384         /* calculate and install multicast filter */
385         mfilt[0] = mfilt[1] = ~0;
386         ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
387 }
388
389 void ath9k_host_rx_init(struct ath9k_htc_priv *priv)
390 {
391         ath9k_hw_rxena(priv->ah);
392         ath9k_htc_opmode_init(priv);
393         ath9k_hw_startpcureceive(priv->ah);
394         priv->rx.last_rssi = ATH_RSSI_DUMMY_MARKER;
395 }
396
397 static void ath9k_process_rate(struct ieee80211_hw *hw,
398                                struct ieee80211_rx_status *rxs,
399                                u8 rx_rate, u8 rs_flags)
400 {
401         struct ieee80211_supported_band *sband;
402         enum ieee80211_band band;
403         unsigned int i = 0;
404
405         if (rx_rate & 0x80) {
406                 /* HT rate */
407                 rxs->flag |= RX_FLAG_HT;
408                 if (rs_flags & ATH9K_RX_2040)
409                         rxs->flag |= RX_FLAG_40MHZ;
410                 if (rs_flags & ATH9K_RX_GI)
411                         rxs->flag |= RX_FLAG_SHORT_GI;
412                 rxs->rate_idx = rx_rate & 0x7f;
413                 return;
414         }
415
416         band = hw->conf.channel->band;
417         sband = hw->wiphy->bands[band];
418
419         for (i = 0; i < sband->n_bitrates; i++) {
420                 if (sband->bitrates[i].hw_value == rx_rate) {
421                         rxs->rate_idx = i;
422                         return;
423                 }
424                 if (sband->bitrates[i].hw_value_short == rx_rate) {
425                         rxs->rate_idx = i;
426                         rxs->flag |= RX_FLAG_SHORTPRE;
427                         return;
428                 }
429         }
430
431 }
432
433 static bool ath9k_rx_prepare(struct ath9k_htc_priv *priv,
434                              struct ath9k_htc_rxbuf *rxbuf,
435                              struct ieee80211_rx_status *rx_status)
436
437 {
438         struct ieee80211_hdr *hdr;
439         struct ieee80211_hw *hw = priv->hw;
440         struct sk_buff *skb = rxbuf->skb;
441         struct ath_common *common = ath9k_hw_common(priv->ah);
442         int hdrlen, padpos, padsize;
443         int last_rssi = ATH_RSSI_DUMMY_MARKER;
444         __le16 fc;
445
446         hdr = (struct ieee80211_hdr *)skb->data;
447         fc = hdr->frame_control;
448         hdrlen = ieee80211_get_hdrlen_from_skb(skb);
449
450         padpos = ath9k_cmn_padpos(fc);
451
452         padsize = padpos & 3;
453         if (padsize && skb->len >= padpos+padsize+FCS_LEN) {
454                 memmove(skb->data + padsize, skb->data, padpos);
455                 skb_pull(skb, padsize);
456         }
457
458         memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
459
460         if (rxbuf->rxstatus.rs_status != 0) {
461                 if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_CRC)
462                         rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
463                 if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_PHY)
464                         goto rx_next;
465
466                 if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_DECRYPT) {
467                         /* FIXME */
468                 } else if (rxbuf->rxstatus.rs_status & ATH9K_RXERR_MIC) {
469                         if (ieee80211_is_ctl(fc))
470                                 /*
471                                  * Sometimes, we get invalid
472                                  * MIC failures on valid control frames.
473                                  * Remove these mic errors.
474                                  */
475                                 rxbuf->rxstatus.rs_status &= ~ATH9K_RXERR_MIC;
476                         else
477                                 rx_status->flag |= RX_FLAG_MMIC_ERROR;
478                 }
479
480                 /*
481                  * Reject error frames with the exception of
482                  * decryption and MIC failures. For monitor mode,
483                  * we also ignore the CRC error.
484                  */
485                 if (priv->ah->opmode == NL80211_IFTYPE_MONITOR) {
486                         if (rxbuf->rxstatus.rs_status &
487                             ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
488                               ATH9K_RXERR_CRC))
489                                 goto rx_next;
490                 } else {
491                         if (rxbuf->rxstatus.rs_status &
492                             ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
493                                 goto rx_next;
494                         }
495                 }
496         }
497
498         if (!(rxbuf->rxstatus.rs_status & ATH9K_RXERR_DECRYPT)) {
499                 u8 keyix;
500                 keyix = rxbuf->rxstatus.rs_keyix;
501                 if (keyix != ATH9K_RXKEYIX_INVALID) {
502                         rx_status->flag |= RX_FLAG_DECRYPTED;
503                 } else if (ieee80211_has_protected(fc) &&
504                            skb->len >= hdrlen + 4) {
505                         keyix = skb->data[hdrlen + 3] >> 6;
506                         if (test_bit(keyix, common->keymap))
507                                 rx_status->flag |= RX_FLAG_DECRYPTED;
508                 }
509         }
510
511         ath9k_process_rate(hw, rx_status, rxbuf->rxstatus.rs_rate,
512                            rxbuf->rxstatus.rs_flags);
513
514         if (priv->op_flags & OP_ASSOCIATED) {
515                 if (rxbuf->rxstatus.rs_rssi != ATH9K_RSSI_BAD &&
516                     !rxbuf->rxstatus.rs_moreaggr)
517                         ATH_RSSI_LPF(priv->rx.last_rssi,
518                                      rxbuf->rxstatus.rs_rssi);
519
520                 last_rssi = priv->rx.last_rssi;
521
522                 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
523                         rxbuf->rxstatus.rs_rssi = ATH_EP_RND(last_rssi,
524                                                              ATH_RSSI_EP_MULTIPLIER);
525
526                 if (rxbuf->rxstatus.rs_rssi < 0)
527                         rxbuf->rxstatus.rs_rssi = 0;
528
529                 if (ieee80211_is_beacon(fc))
530                         priv->ah->stats.avgbrssi = rxbuf->rxstatus.rs_rssi;
531         }
532
533         rx_status->mactime = rxbuf->rxstatus.rs_tstamp;
534         rx_status->band = hw->conf.channel->band;
535         rx_status->freq = hw->conf.channel->center_freq;
536         rx_status->signal =  rxbuf->rxstatus.rs_rssi + ATH_DEFAULT_NOISE_FLOOR;
537         rx_status->antenna = rxbuf->rxstatus.rs_antenna;
538         rx_status->flag |= RX_FLAG_TSFT;
539
540         return true;
541
542 rx_next:
543         return false;
544 }
545
546 /*
547  * FIXME: Handle FLUSH later on.
548  */
549 void ath9k_rx_tasklet(unsigned long data)
550 {
551         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
552         struct ath9k_htc_rxbuf *rxbuf = NULL, *tmp_buf = NULL;
553         struct ieee80211_rx_status rx_status;
554         struct sk_buff *skb;
555         unsigned long flags;
556
557
558         do {
559                 spin_lock_irqsave(&priv->rx.rxbuflock, flags);
560                 list_for_each_entry(tmp_buf, &priv->rx.rxbuf, list) {
561                         if (tmp_buf->in_process) {
562                                 rxbuf = tmp_buf;
563                                 break;
564                         }
565                 }
566
567                 if (rxbuf == NULL) {
568                         spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
569                         break;
570                 }
571
572                 if (!rxbuf->skb)
573                         goto requeue;
574
575                 if (!ath9k_rx_prepare(priv, rxbuf, &rx_status)) {
576                         dev_kfree_skb_any(rxbuf->skb);
577                         goto requeue;
578                 }
579
580                 memcpy(IEEE80211_SKB_RXCB(rxbuf->skb), &rx_status,
581                        sizeof(struct ieee80211_rx_status));
582                 skb = rxbuf->skb;
583                 spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
584
585                 ieee80211_rx(priv->hw, skb);
586
587                 spin_lock_irqsave(&priv->rx.rxbuflock, flags);
588 requeue:
589                 rxbuf->in_process = false;
590                 rxbuf->skb = NULL;
591                 list_move_tail(&rxbuf->list, &priv->rx.rxbuf);
592                 rxbuf = NULL;
593                 spin_unlock_irqrestore(&priv->rx.rxbuflock, flags);
594         } while (1);
595
596 }
597
598 void ath9k_htc_rxep(void *drv_priv, struct sk_buff *skb,
599                     enum htc_endpoint_id ep_id)
600 {
601         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)drv_priv;
602         struct ath_hw *ah = priv->ah;
603         struct ath_common *common = ath9k_hw_common(ah);
604         struct ath9k_htc_rxbuf *rxbuf = NULL, *tmp_buf = NULL;
605         struct ath_htc_rx_status *rxstatus;
606         u32 len = 0;
607
608         spin_lock(&priv->rx.rxbuflock);
609         list_for_each_entry(tmp_buf, &priv->rx.rxbuf, list) {
610                 if (!tmp_buf->in_process) {
611                         rxbuf = tmp_buf;
612                         break;
613                 }
614         }
615         spin_unlock(&priv->rx.rxbuflock);
616
617         if (rxbuf == NULL) {
618                 ath_print(common, ATH_DBG_ANY,
619                           "No free RX buffer\n");
620                 goto err;
621         }
622
623         len = skb->len;
624         if (len <= HTC_RX_FRAME_HEADER_SIZE) {
625                 ath_print(common, ATH_DBG_FATAL,
626                           "Corrupted RX frame, dropping\n");
627                 goto err;
628         }
629
630         rxstatus = (struct ath_htc_rx_status *)skb->data;
631
632         rxstatus->rs_tstamp = be64_to_cpu(rxstatus->rs_tstamp);
633         rxstatus->rs_datalen = be16_to_cpu(rxstatus->rs_datalen);
634         rxstatus->evm0 = be32_to_cpu(rxstatus->evm0);
635         rxstatus->evm1 = be32_to_cpu(rxstatus->evm1);
636         rxstatus->evm2 = be32_to_cpu(rxstatus->evm2);
637
638         if (rxstatus->rs_datalen - (len - HTC_RX_FRAME_HEADER_SIZE) != 0) {
639                 ath_print(common, ATH_DBG_FATAL,
640                           "Corrupted RX data len, dropping "
641                           "(epid: %d, dlen: %d, skblen: %d)\n",
642                           ep_id, rxstatus->rs_datalen, len);
643                 goto err;
644         }
645
646         spin_lock(&priv->rx.rxbuflock);
647         memcpy(&rxbuf->rxstatus, rxstatus, HTC_RX_FRAME_HEADER_SIZE);
648         skb_pull(skb, HTC_RX_FRAME_HEADER_SIZE);
649         rxbuf->skb = skb;
650         rxbuf->in_process = true;
651         spin_unlock(&priv->rx.rxbuflock);
652
653         tasklet_schedule(&priv->rx_tasklet);
654         return;
655 err:
656         dev_kfree_skb_any(skb);
657         return;
658 }
659
660 /* FIXME: Locking for cleanup/init */
661
662 void ath9k_rx_cleanup(struct ath9k_htc_priv *priv)
663 {
664         struct ath9k_htc_rxbuf *rxbuf, *tbuf;
665
666         list_for_each_entry_safe(rxbuf, tbuf, &priv->rx.rxbuf, list) {
667                 list_del(&rxbuf->list);
668                 if (rxbuf->skb)
669                         dev_kfree_skb_any(rxbuf->skb);
670                 kfree(rxbuf);
671         }
672 }
673
674 int ath9k_rx_init(struct ath9k_htc_priv *priv)
675 {
676         struct ath_hw *ah = priv->ah;
677         struct ath_common *common = ath9k_hw_common(ah);
678         struct ath9k_htc_rxbuf *rxbuf;
679         int i = 0;
680
681         INIT_LIST_HEAD(&priv->rx.rxbuf);
682         spin_lock_init(&priv->rx.rxbuflock);
683
684         for (i = 0; i < ATH9K_HTC_RXBUF; i++) {
685                 rxbuf = kzalloc(sizeof(struct ath9k_htc_rxbuf), GFP_KERNEL);
686                 if (rxbuf == NULL) {
687                         ath_print(common, ATH_DBG_FATAL,
688                                   "Unable to allocate RX buffers\n");
689                         goto err;
690                 }
691                 list_add_tail(&rxbuf->list, &priv->rx.rxbuf);
692         }
693
694         return 0;
695
696 err:
697         ath9k_rx_cleanup(priv);
698         return -ENOMEM;
699 }