Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wireless
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / ath / ath9k / recv.c
1 /*
2  * Copyright (c) 2008-2011 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 <linux/dma-mapping.h>
18 #include "ath9k.h"
19 #include "ar9003_mac.h"
20
21 #define SKB_CB_ATHBUF(__skb)    (*((struct ath_buf **)__skb->cb))
22
23 static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
24 {
25         return sc->ps_enabled &&
26                (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
27 }
28
29 /*
30  * Setup and link descriptors.
31  *
32  * 11N: we can no longer afford to self link the last descriptor.
33  * MAC acknowledges BA status as long as it copies frames to host
34  * buffer (or rx fifo). This can incorrectly acknowledge packets
35  * to a sender if last desc is self-linked.
36  */
37 static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
38 {
39         struct ath_hw *ah = sc->sc_ah;
40         struct ath_common *common = ath9k_hw_common(ah);
41         struct ath_desc *ds;
42         struct sk_buff *skb;
43
44         ATH_RXBUF_RESET(bf);
45
46         ds = bf->bf_desc;
47         ds->ds_link = 0; /* link to null */
48         ds->ds_data = bf->bf_buf_addr;
49
50         /* virtual addr of the beginning of the buffer. */
51         skb = bf->bf_mpdu;
52         BUG_ON(skb == NULL);
53         ds->ds_vdata = skb->data;
54
55         /*
56          * setup rx descriptors. The rx_bufsize here tells the hardware
57          * how much data it can DMA to us and that we are prepared
58          * to process
59          */
60         ath9k_hw_setuprxdesc(ah, ds,
61                              common->rx_bufsize,
62                              0);
63
64         if (sc->rx.rxlink == NULL)
65                 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
66         else
67                 *sc->rx.rxlink = bf->bf_daddr;
68
69         sc->rx.rxlink = &ds->ds_link;
70 }
71
72 static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
73 {
74         /* XXX block beacon interrupts */
75         ath9k_hw_setantenna(sc->sc_ah, antenna);
76         sc->rx.defant = antenna;
77         sc->rx.rxotherant = 0;
78 }
79
80 static void ath_opmode_init(struct ath_softc *sc)
81 {
82         struct ath_hw *ah = sc->sc_ah;
83         struct ath_common *common = ath9k_hw_common(ah);
84
85         u32 rfilt, mfilt[2];
86
87         /* configure rx filter */
88         rfilt = ath_calcrxfilter(sc);
89         ath9k_hw_setrxfilter(ah, rfilt);
90
91         /* configure bssid mask */
92         ath_hw_setbssidmask(common);
93
94         /* configure operational mode */
95         ath9k_hw_setopmode(ah);
96
97         /* calculate and install multicast filter */
98         mfilt[0] = mfilt[1] = ~0;
99         ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
100 }
101
102 static bool ath_rx_edma_buf_link(struct ath_softc *sc,
103                                  enum ath9k_rx_qtype qtype)
104 {
105         struct ath_hw *ah = sc->sc_ah;
106         struct ath_rx_edma *rx_edma;
107         struct sk_buff *skb;
108         struct ath_buf *bf;
109
110         rx_edma = &sc->rx.rx_edma[qtype];
111         if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
112                 return false;
113
114         bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
115         list_del_init(&bf->list);
116
117         skb = bf->bf_mpdu;
118
119         ATH_RXBUF_RESET(bf);
120         memset(skb->data, 0, ah->caps.rx_status_len);
121         dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
122                                 ah->caps.rx_status_len, DMA_TO_DEVICE);
123
124         SKB_CB_ATHBUF(skb) = bf;
125         ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
126         skb_queue_tail(&rx_edma->rx_fifo, skb);
127
128         return true;
129 }
130
131 static void ath_rx_addbuffer_edma(struct ath_softc *sc,
132                                   enum ath9k_rx_qtype qtype, int size)
133 {
134         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
135         struct ath_buf *bf, *tbf;
136
137         if (list_empty(&sc->rx.rxbuf)) {
138                 ath_dbg(common, QUEUE, "No free rx buf available\n");
139                 return;
140         }
141
142         list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
143                 if (!ath_rx_edma_buf_link(sc, qtype))
144                         break;
145
146 }
147
148 static void ath_rx_remove_buffer(struct ath_softc *sc,
149                                  enum ath9k_rx_qtype qtype)
150 {
151         struct ath_buf *bf;
152         struct ath_rx_edma *rx_edma;
153         struct sk_buff *skb;
154
155         rx_edma = &sc->rx.rx_edma[qtype];
156
157         while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
158                 bf = SKB_CB_ATHBUF(skb);
159                 BUG_ON(!bf);
160                 list_add_tail(&bf->list, &sc->rx.rxbuf);
161         }
162 }
163
164 static void ath_rx_edma_cleanup(struct ath_softc *sc)
165 {
166         struct ath_hw *ah = sc->sc_ah;
167         struct ath_common *common = ath9k_hw_common(ah);
168         struct ath_buf *bf;
169
170         ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
171         ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
172
173         list_for_each_entry(bf, &sc->rx.rxbuf, list) {
174                 if (bf->bf_mpdu) {
175                         dma_unmap_single(sc->dev, bf->bf_buf_addr,
176                                         common->rx_bufsize,
177                                         DMA_BIDIRECTIONAL);
178                         dev_kfree_skb_any(bf->bf_mpdu);
179                         bf->bf_buf_addr = 0;
180                         bf->bf_mpdu = NULL;
181                 }
182         }
183
184         INIT_LIST_HEAD(&sc->rx.rxbuf);
185
186         kfree(sc->rx.rx_bufptr);
187         sc->rx.rx_bufptr = NULL;
188 }
189
190 static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
191 {
192         skb_queue_head_init(&rx_edma->rx_fifo);
193         rx_edma->rx_fifo_hwsize = size;
194 }
195
196 static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
197 {
198         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
199         struct ath_hw *ah = sc->sc_ah;
200         struct sk_buff *skb;
201         struct ath_buf *bf;
202         int error = 0, i;
203         u32 size;
204
205         ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
206                                     ah->caps.rx_status_len);
207
208         ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
209                                ah->caps.rx_lp_qdepth);
210         ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
211                                ah->caps.rx_hp_qdepth);
212
213         size = sizeof(struct ath_buf) * nbufs;
214         bf = kzalloc(size, GFP_KERNEL);
215         if (!bf)
216                 return -ENOMEM;
217
218         INIT_LIST_HEAD(&sc->rx.rxbuf);
219         sc->rx.rx_bufptr = bf;
220
221         for (i = 0; i < nbufs; i++, bf++) {
222                 skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
223                 if (!skb) {
224                         error = -ENOMEM;
225                         goto rx_init_fail;
226                 }
227
228                 memset(skb->data, 0, common->rx_bufsize);
229                 bf->bf_mpdu = skb;
230
231                 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
232                                                  common->rx_bufsize,
233                                                  DMA_BIDIRECTIONAL);
234                 if (unlikely(dma_mapping_error(sc->dev,
235                                                 bf->bf_buf_addr))) {
236                                 dev_kfree_skb_any(skb);
237                                 bf->bf_mpdu = NULL;
238                                 bf->bf_buf_addr = 0;
239                                 ath_err(common,
240                                         "dma_mapping_error() on RX init\n");
241                                 error = -ENOMEM;
242                                 goto rx_init_fail;
243                 }
244
245                 list_add_tail(&bf->list, &sc->rx.rxbuf);
246         }
247
248         return 0;
249
250 rx_init_fail:
251         ath_rx_edma_cleanup(sc);
252         return error;
253 }
254
255 static void ath_edma_start_recv(struct ath_softc *sc)
256 {
257         spin_lock_bh(&sc->rx.rxbuflock);
258
259         ath9k_hw_rxena(sc->sc_ah);
260
261         ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
262                               sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
263
264         ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
265                               sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
266
267         ath_opmode_init(sc);
268
269         ath9k_hw_startpcureceive(sc->sc_ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
270
271         spin_unlock_bh(&sc->rx.rxbuflock);
272 }
273
274 static void ath_edma_stop_recv(struct ath_softc *sc)
275 {
276         ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
277         ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
278 }
279
280 int ath_rx_init(struct ath_softc *sc, int nbufs)
281 {
282         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
283         struct sk_buff *skb;
284         struct ath_buf *bf;
285         int error = 0;
286
287         spin_lock_init(&sc->sc_pcu_lock);
288         spin_lock_init(&sc->rx.rxbuflock);
289         clear_bit(SC_OP_RXFLUSH, &sc->sc_flags);
290
291         common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
292                              sc->sc_ah->caps.rx_status_len;
293
294         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
295                 return ath_rx_edma_init(sc, nbufs);
296         } else {
297                 ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
298                         common->cachelsz, common->rx_bufsize);
299
300                 /* Initialize rx descriptors */
301
302                 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
303                                 "rx", nbufs, 1, 0);
304                 if (error != 0) {
305                         ath_err(common,
306                                 "failed to allocate rx descriptors: %d\n",
307                                 error);
308                         goto err;
309                 }
310
311                 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
312                         skb = ath_rxbuf_alloc(common, common->rx_bufsize,
313                                               GFP_KERNEL);
314                         if (skb == NULL) {
315                                 error = -ENOMEM;
316                                 goto err;
317                         }
318
319                         bf->bf_mpdu = skb;
320                         bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
321                                         common->rx_bufsize,
322                                         DMA_FROM_DEVICE);
323                         if (unlikely(dma_mapping_error(sc->dev,
324                                                         bf->bf_buf_addr))) {
325                                 dev_kfree_skb_any(skb);
326                                 bf->bf_mpdu = NULL;
327                                 bf->bf_buf_addr = 0;
328                                 ath_err(common,
329                                         "dma_mapping_error() on RX init\n");
330                                 error = -ENOMEM;
331                                 goto err;
332                         }
333                 }
334                 sc->rx.rxlink = NULL;
335         }
336
337 err:
338         if (error)
339                 ath_rx_cleanup(sc);
340
341         return error;
342 }
343
344 void ath_rx_cleanup(struct ath_softc *sc)
345 {
346         struct ath_hw *ah = sc->sc_ah;
347         struct ath_common *common = ath9k_hw_common(ah);
348         struct sk_buff *skb;
349         struct ath_buf *bf;
350
351         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
352                 ath_rx_edma_cleanup(sc);
353                 return;
354         } else {
355                 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
356                         skb = bf->bf_mpdu;
357                         if (skb) {
358                                 dma_unmap_single(sc->dev, bf->bf_buf_addr,
359                                                 common->rx_bufsize,
360                                                 DMA_FROM_DEVICE);
361                                 dev_kfree_skb(skb);
362                                 bf->bf_buf_addr = 0;
363                                 bf->bf_mpdu = NULL;
364                         }
365                 }
366
367                 if (sc->rx.rxdma.dd_desc_len != 0)
368                         ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
369         }
370 }
371
372 /*
373  * Calculate the receive filter according to the
374  * operating mode and state:
375  *
376  * o always accept unicast, broadcast, and multicast traffic
377  * o maintain current state of phy error reception (the hal
378  *   may enable phy error frames for noise immunity work)
379  * o probe request frames are accepted only when operating in
380  *   hostap, adhoc, or monitor modes
381  * o enable promiscuous mode according to the interface state
382  * o accept beacons:
383  *   - when operating in adhoc mode so the 802.11 layer creates
384  *     node table entries for peers,
385  *   - when operating in station mode for collecting rssi data when
386  *     the station is otherwise quiet, or
387  *   - when operating as a repeater so we see repeater-sta beacons
388  *   - when scanning
389  */
390
391 u32 ath_calcrxfilter(struct ath_softc *sc)
392 {
393         u32 rfilt;
394
395         rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
396                 | ATH9K_RX_FILTER_MCAST;
397
398         if (sc->rx.rxfilter & FIF_PROBE_REQ)
399                 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
400
401         /*
402          * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
403          * mode interface or when in monitor mode. AP mode does not need this
404          * since it receives all in-BSS frames anyway.
405          */
406         if (sc->sc_ah->is_monitoring)
407                 rfilt |= ATH9K_RX_FILTER_PROM;
408
409         if (sc->rx.rxfilter & FIF_CONTROL)
410                 rfilt |= ATH9K_RX_FILTER_CONTROL;
411
412         if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
413             (sc->nvifs <= 1) &&
414             !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
415                 rfilt |= ATH9K_RX_FILTER_MYBEACON;
416         else
417                 rfilt |= ATH9K_RX_FILTER_BEACON;
418
419         if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
420             (sc->rx.rxfilter & FIF_PSPOLL))
421                 rfilt |= ATH9K_RX_FILTER_PSPOLL;
422
423         if (conf_is_ht(&sc->hw->conf))
424                 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
425
426         if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
427                 /* The following may also be needed for other older chips */
428                 if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
429                         rfilt |= ATH9K_RX_FILTER_PROM;
430                 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
431         }
432
433         return rfilt;
434
435 }
436
437 int ath_startrecv(struct ath_softc *sc)
438 {
439         struct ath_hw *ah = sc->sc_ah;
440         struct ath_buf *bf, *tbf;
441
442         if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
443                 ath_edma_start_recv(sc);
444                 return 0;
445         }
446
447         spin_lock_bh(&sc->rx.rxbuflock);
448         if (list_empty(&sc->rx.rxbuf))
449                 goto start_recv;
450
451         sc->rx.rxlink = NULL;
452         list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
453                 ath_rx_buf_link(sc, bf);
454         }
455
456         /* We could have deleted elements so the list may be empty now */
457         if (list_empty(&sc->rx.rxbuf))
458                 goto start_recv;
459
460         bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
461         ath9k_hw_putrxbuf(ah, bf->bf_daddr);
462         ath9k_hw_rxena(ah);
463
464 start_recv:
465         ath_opmode_init(sc);
466         ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
467
468         spin_unlock_bh(&sc->rx.rxbuflock);
469
470         return 0;
471 }
472
473 bool ath_stoprecv(struct ath_softc *sc)
474 {
475         struct ath_hw *ah = sc->sc_ah;
476         bool stopped, reset = false;
477
478         spin_lock_bh(&sc->rx.rxbuflock);
479         ath9k_hw_abortpcurecv(ah);
480         ath9k_hw_setrxfilter(ah, 0);
481         stopped = ath9k_hw_stopdmarecv(ah, &reset);
482
483         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
484                 ath_edma_stop_recv(sc);
485         else
486                 sc->rx.rxlink = NULL;
487         spin_unlock_bh(&sc->rx.rxbuflock);
488
489         if (!(ah->ah_flags & AH_UNPLUGGED) &&
490             unlikely(!stopped)) {
491                 ath_err(ath9k_hw_common(sc->sc_ah),
492                         "Could not stop RX, we could be "
493                         "confusing the DMA engine when we start RX up\n");
494                 ATH_DBG_WARN_ON_ONCE(!stopped);
495         }
496         return stopped && !reset;
497 }
498
499 void ath_flushrecv(struct ath_softc *sc)
500 {
501         set_bit(SC_OP_RXFLUSH, &sc->sc_flags);
502         if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
503                 ath_rx_tasklet(sc, 1, true);
504         ath_rx_tasklet(sc, 1, false);
505         clear_bit(SC_OP_RXFLUSH, &sc->sc_flags);
506 }
507
508 static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
509 {
510         /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
511         struct ieee80211_mgmt *mgmt;
512         u8 *pos, *end, id, elen;
513         struct ieee80211_tim_ie *tim;
514
515         mgmt = (struct ieee80211_mgmt *)skb->data;
516         pos = mgmt->u.beacon.variable;
517         end = skb->data + skb->len;
518
519         while (pos + 2 < end) {
520                 id = *pos++;
521                 elen = *pos++;
522                 if (pos + elen > end)
523                         break;
524
525                 if (id == WLAN_EID_TIM) {
526                         if (elen < sizeof(*tim))
527                                 break;
528                         tim = (struct ieee80211_tim_ie *) pos;
529                         if (tim->dtim_count != 0)
530                                 break;
531                         return tim->bitmap_ctrl & 0x01;
532                 }
533
534                 pos += elen;
535         }
536
537         return false;
538 }
539
540 static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
541 {
542         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
543
544         if (skb->len < 24 + 8 + 2 + 2)
545                 return;
546
547         sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
548
549         if (sc->ps_flags & PS_BEACON_SYNC) {
550                 sc->ps_flags &= ~PS_BEACON_SYNC;
551                 ath_dbg(common, PS,
552                         "Reconfigure Beacon timers based on timestamp from the AP\n");
553                 ath_set_beacon(sc);
554         }
555
556         if (ath_beacon_dtim_pending_cab(skb)) {
557                 /*
558                  * Remain awake waiting for buffered broadcast/multicast
559                  * frames. If the last broadcast/multicast frame is not
560                  * received properly, the next beacon frame will work as
561                  * a backup trigger for returning into NETWORK SLEEP state,
562                  * so we are waiting for it as well.
563                  */
564                 ath_dbg(common, PS,
565                         "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
566                 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
567                 return;
568         }
569
570         if (sc->ps_flags & PS_WAIT_FOR_CAB) {
571                 /*
572                  * This can happen if a broadcast frame is dropped or the AP
573                  * fails to send a frame indicating that all CAB frames have
574                  * been delivered.
575                  */
576                 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
577                 ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
578         }
579 }
580
581 static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
582 {
583         struct ieee80211_hdr *hdr;
584         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
585
586         hdr = (struct ieee80211_hdr *)skb->data;
587
588         /* Process Beacon and CAB receive in PS state */
589         if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
590             && mybeacon) {
591                 ath_rx_ps_beacon(sc, skb);
592         } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
593                    (ieee80211_is_data(hdr->frame_control) ||
594                     ieee80211_is_action(hdr->frame_control)) &&
595                    is_multicast_ether_addr(hdr->addr1) &&
596                    !ieee80211_has_moredata(hdr->frame_control)) {
597                 /*
598                  * No more broadcast/multicast frames to be received at this
599                  * point.
600                  */
601                 sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
602                 ath_dbg(common, PS,
603                         "All PS CAB frames received, back to sleep\n");
604         } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
605                    !is_multicast_ether_addr(hdr->addr1) &&
606                    !ieee80211_has_morefrags(hdr->frame_control)) {
607                 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
608                 ath_dbg(common, PS,
609                         "Going back to sleep after having received PS-Poll data (0x%lx)\n",
610                         sc->ps_flags & (PS_WAIT_FOR_BEACON |
611                                         PS_WAIT_FOR_CAB |
612                                         PS_WAIT_FOR_PSPOLL_DATA |
613                                         PS_WAIT_FOR_TX_ACK));
614         }
615 }
616
617 static bool ath_edma_get_buffers(struct ath_softc *sc,
618                                  enum ath9k_rx_qtype qtype,
619                                  struct ath_rx_status *rs,
620                                  struct ath_buf **dest)
621 {
622         struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
623         struct ath_hw *ah = sc->sc_ah;
624         struct ath_common *common = ath9k_hw_common(ah);
625         struct sk_buff *skb;
626         struct ath_buf *bf;
627         int ret;
628
629         skb = skb_peek(&rx_edma->rx_fifo);
630         if (!skb)
631                 return false;
632
633         bf = SKB_CB_ATHBUF(skb);
634         BUG_ON(!bf);
635
636         dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
637                                 common->rx_bufsize, DMA_FROM_DEVICE);
638
639         ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
640         if (ret == -EINPROGRESS) {
641                 /*let device gain the buffer again*/
642                 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
643                                 common->rx_bufsize, DMA_FROM_DEVICE);
644                 return false;
645         }
646
647         __skb_unlink(skb, &rx_edma->rx_fifo);
648         if (ret == -EINVAL) {
649                 /* corrupt descriptor, skip this one and the following one */
650                 list_add_tail(&bf->list, &sc->rx.rxbuf);
651                 ath_rx_edma_buf_link(sc, qtype);
652
653                 skb = skb_peek(&rx_edma->rx_fifo);
654                 if (skb) {
655                         bf = SKB_CB_ATHBUF(skb);
656                         BUG_ON(!bf);
657
658                         __skb_unlink(skb, &rx_edma->rx_fifo);
659                         list_add_tail(&bf->list, &sc->rx.rxbuf);
660                         ath_rx_edma_buf_link(sc, qtype);
661                 }
662
663                 bf = NULL;
664         }
665
666         *dest = bf;
667         return true;
668 }
669
670 static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
671                                                 struct ath_rx_status *rs,
672                                                 enum ath9k_rx_qtype qtype)
673 {
674         struct ath_buf *bf = NULL;
675
676         while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
677                 if (!bf)
678                         continue;
679
680                 return bf;
681         }
682         return NULL;
683 }
684
685 static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
686                                            struct ath_rx_status *rs)
687 {
688         struct ath_hw *ah = sc->sc_ah;
689         struct ath_common *common = ath9k_hw_common(ah);
690         struct ath_desc *ds;
691         struct ath_buf *bf;
692         int ret;
693
694         if (list_empty(&sc->rx.rxbuf)) {
695                 sc->rx.rxlink = NULL;
696                 return NULL;
697         }
698
699         bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
700         ds = bf->bf_desc;
701
702         /*
703          * Must provide the virtual address of the current
704          * descriptor, the physical address, and the virtual
705          * address of the next descriptor in the h/w chain.
706          * This allows the HAL to look ahead to see if the
707          * hardware is done with a descriptor by checking the
708          * done bit in the following descriptor and the address
709          * of the current descriptor the DMA engine is working
710          * on.  All this is necessary because of our use of
711          * a self-linked list to avoid rx overruns.
712          */
713         ret = ath9k_hw_rxprocdesc(ah, ds, rs);
714         if (ret == -EINPROGRESS) {
715                 struct ath_rx_status trs;
716                 struct ath_buf *tbf;
717                 struct ath_desc *tds;
718
719                 memset(&trs, 0, sizeof(trs));
720                 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
721                         sc->rx.rxlink = NULL;
722                         return NULL;
723                 }
724
725                 tbf = list_entry(bf->list.next, struct ath_buf, list);
726
727                 /*
728                  * On some hardware the descriptor status words could
729                  * get corrupted, including the done bit. Because of
730                  * this, check if the next descriptor's done bit is
731                  * set or not.
732                  *
733                  * If the next descriptor's done bit is set, the current
734                  * descriptor has been corrupted. Force s/w to discard
735                  * this descriptor and continue...
736                  */
737
738                 tds = tbf->bf_desc;
739                 ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
740                 if (ret == -EINPROGRESS)
741                         return NULL;
742         }
743
744         if (!bf->bf_mpdu)
745                 return bf;
746
747         /*
748          * Synchronize the DMA transfer with CPU before
749          * 1. accessing the frame
750          * 2. requeueing the same buffer to h/w
751          */
752         dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
753                         common->rx_bufsize,
754                         DMA_FROM_DEVICE);
755
756         return bf;
757 }
758
759 /* Assumes you've already done the endian to CPU conversion */
760 static bool ath9k_rx_accept(struct ath_common *common,
761                             struct ieee80211_hdr *hdr,
762                             struct ieee80211_rx_status *rxs,
763                             struct ath_rx_status *rx_stats,
764                             bool *decrypt_error)
765 {
766         struct ath_softc *sc = (struct ath_softc *) common->priv;
767         bool is_mc, is_valid_tkip, strip_mic, mic_error;
768         struct ath_hw *ah = common->ah;
769         __le16 fc;
770         u8 rx_status_len = ah->caps.rx_status_len;
771
772         fc = hdr->frame_control;
773
774         is_mc = !!is_multicast_ether_addr(hdr->addr1);
775         is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
776                 test_bit(rx_stats->rs_keyix, common->tkip_keymap);
777         strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
778                 ieee80211_has_protected(fc) &&
779                 !(rx_stats->rs_status &
780                 (ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
781                  ATH9K_RXERR_KEYMISS));
782
783         /*
784          * Key miss events are only relevant for pairwise keys where the
785          * descriptor does contain a valid key index. This has been observed
786          * mostly with CCMP encryption.
787          */
788         if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID ||
789             !test_bit(rx_stats->rs_keyix, common->ccmp_keymap))
790                 rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
791
792         if (!rx_stats->rs_datalen) {
793                 RX_STAT_INC(rx_len_err);
794                 return false;
795         }
796
797         /*
798          * rs_status follows rs_datalen so if rs_datalen is too large
799          * we can take a hint that hardware corrupted it, so ignore
800          * those frames.
801          */
802         if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len)) {
803                 RX_STAT_INC(rx_len_err);
804                 return false;
805         }
806
807         /* Only use error bits from the last fragment */
808         if (rx_stats->rs_more)
809                 return true;
810
811         mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
812                 !ieee80211_has_morefrags(fc) &&
813                 !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
814                 (rx_stats->rs_status & ATH9K_RXERR_MIC);
815
816         /*
817          * The rx_stats->rs_status will not be set until the end of the
818          * chained descriptors so it can be ignored if rs_more is set. The
819          * rs_more will be false at the last element of the chained
820          * descriptors.
821          */
822         if (rx_stats->rs_status != 0) {
823                 u8 status_mask;
824
825                 if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
826                         rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
827                         mic_error = false;
828                 }
829                 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
830                         return false;
831
832                 if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
833                     (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
834                         *decrypt_error = true;
835                         mic_error = false;
836                 }
837
838                 /*
839                  * Reject error frames with the exception of
840                  * decryption and MIC failures. For monitor mode,
841                  * we also ignore the CRC error.
842                  */
843                 status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
844                               ATH9K_RXERR_KEYMISS;
845
846                 if (ah->is_monitoring && (sc->rx.rxfilter & FIF_FCSFAIL))
847                         status_mask |= ATH9K_RXERR_CRC;
848
849                 if (rx_stats->rs_status & ~status_mask)
850                         return false;
851         }
852
853         /*
854          * For unicast frames the MIC error bit can have false positives,
855          * so all MIC error reports need to be validated in software.
856          * False negatives are not common, so skip software verification
857          * if the hardware considers the MIC valid.
858          */
859         if (strip_mic)
860                 rxs->flag |= RX_FLAG_MMIC_STRIPPED;
861         else if (is_mc && mic_error)
862                 rxs->flag |= RX_FLAG_MMIC_ERROR;
863
864         return true;
865 }
866
867 static int ath9k_process_rate(struct ath_common *common,
868                               struct ieee80211_hw *hw,
869                               struct ath_rx_status *rx_stats,
870                               struct ieee80211_rx_status *rxs)
871 {
872         struct ieee80211_supported_band *sband;
873         enum ieee80211_band band;
874         unsigned int i = 0;
875         struct ath_softc __maybe_unused *sc = common->priv;
876
877         band = hw->conf.channel->band;
878         sband = hw->wiphy->bands[band];
879
880         if (rx_stats->rs_rate & 0x80) {
881                 /* HT rate */
882                 rxs->flag |= RX_FLAG_HT;
883                 if (rx_stats->rs_flags & ATH9K_RX_2040)
884                         rxs->flag |= RX_FLAG_40MHZ;
885                 if (rx_stats->rs_flags & ATH9K_RX_GI)
886                         rxs->flag |= RX_FLAG_SHORT_GI;
887                 rxs->rate_idx = rx_stats->rs_rate & 0x7f;
888                 return 0;
889         }
890
891         for (i = 0; i < sband->n_bitrates; i++) {
892                 if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
893                         rxs->rate_idx = i;
894                         return 0;
895                 }
896                 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
897                         rxs->flag |= RX_FLAG_SHORTPRE;
898                         rxs->rate_idx = i;
899                         return 0;
900                 }
901         }
902
903         /*
904          * No valid hardware bitrate found -- we should not get here
905          * because hardware has already validated this frame as OK.
906          */
907         ath_dbg(common, ANY,
908                 "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
909                 rx_stats->rs_rate);
910         RX_STAT_INC(rx_rate_err);
911         return -EINVAL;
912 }
913
914 static void ath9k_process_rssi(struct ath_common *common,
915                                struct ieee80211_hw *hw,
916                                struct ieee80211_hdr *hdr,
917                                struct ath_rx_status *rx_stats)
918 {
919         struct ath_softc *sc = hw->priv;
920         struct ath_hw *ah = common->ah;
921         int last_rssi;
922         int rssi = rx_stats->rs_rssi;
923
924         if (!rx_stats->is_mybeacon ||
925             ((ah->opmode != NL80211_IFTYPE_STATION) &&
926              (ah->opmode != NL80211_IFTYPE_ADHOC)))
927                 return;
928
929         if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
930                 ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
931
932         last_rssi = sc->last_rssi;
933         if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
934                 rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
935         if (rssi < 0)
936                 rssi = 0;
937
938         /* Update Beacon RSSI, this is used by ANI. */
939         ah->stats.avgbrssi = rssi;
940 }
941
942 /*
943  * For Decrypt or Demic errors, we only mark packet status here and always push
944  * up the frame up to let mac80211 handle the actual error case, be it no
945  * decryption key or real decryption error. This let us keep statistics there.
946  */
947 static int ath9k_rx_skb_preprocess(struct ath_common *common,
948                                    struct ieee80211_hw *hw,
949                                    struct ieee80211_hdr *hdr,
950                                    struct ath_rx_status *rx_stats,
951                                    struct ieee80211_rx_status *rx_status,
952                                    bool *decrypt_error)
953 {
954         struct ath_hw *ah = common->ah;
955
956         /*
957          * everything but the rate is checked here, the rate check is done
958          * separately to avoid doing two lookups for a rate for each frame.
959          */
960         if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
961                 return -EINVAL;
962
963         /* Only use status info from the last fragment */
964         if (rx_stats->rs_more)
965                 return 0;
966
967         ath9k_process_rssi(common, hw, hdr, rx_stats);
968
969         if (ath9k_process_rate(common, hw, rx_stats, rx_status))
970                 return -EINVAL;
971
972         rx_status->band = hw->conf.channel->band;
973         rx_status->freq = hw->conf.channel->center_freq;
974         rx_status->signal = ah->noise + rx_stats->rs_rssi;
975         rx_status->antenna = rx_stats->rs_antenna;
976         rx_status->flag |= RX_FLAG_MACTIME_MPDU;
977         if (rx_stats->rs_moreaggr)
978                 rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
979
980         return 0;
981 }
982
983 static void ath9k_rx_skb_postprocess(struct ath_common *common,
984                                      struct sk_buff *skb,
985                                      struct ath_rx_status *rx_stats,
986                                      struct ieee80211_rx_status *rxs,
987                                      bool decrypt_error)
988 {
989         struct ath_hw *ah = common->ah;
990         struct ieee80211_hdr *hdr;
991         int hdrlen, padpos, padsize;
992         u8 keyix;
993         __le16 fc;
994
995         /* see if any padding is done by the hw and remove it */
996         hdr = (struct ieee80211_hdr *) skb->data;
997         hdrlen = ieee80211_get_hdrlen_from_skb(skb);
998         fc = hdr->frame_control;
999         padpos = ath9k_cmn_padpos(hdr->frame_control);
1000
1001         /* The MAC header is padded to have 32-bit boundary if the
1002          * packet payload is non-zero. The general calculation for
1003          * padsize would take into account odd header lengths:
1004          * padsize = (4 - padpos % 4) % 4; However, since only
1005          * even-length headers are used, padding can only be 0 or 2
1006          * bytes and we can optimize this a bit. In addition, we must
1007          * not try to remove padding from short control frames that do
1008          * not have payload. */
1009         padsize = padpos & 3;
1010         if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1011                 memmove(skb->data + padsize, skb->data, padpos);
1012                 skb_pull(skb, padsize);
1013         }
1014
1015         keyix = rx_stats->rs_keyix;
1016
1017         if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1018             ieee80211_has_protected(fc)) {
1019                 rxs->flag |= RX_FLAG_DECRYPTED;
1020         } else if (ieee80211_has_protected(fc)
1021                    && !decrypt_error && skb->len >= hdrlen + 4) {
1022                 keyix = skb->data[hdrlen + 3] >> 6;
1023
1024                 if (test_bit(keyix, common->keymap))
1025                         rxs->flag |= RX_FLAG_DECRYPTED;
1026         }
1027         if (ah->sw_mgmt_crypto &&
1028             (rxs->flag & RX_FLAG_DECRYPTED) &&
1029             ieee80211_is_mgmt(fc))
1030                 /* Use software decrypt for management frames. */
1031                 rxs->flag &= ~RX_FLAG_DECRYPTED;
1032 }
1033
1034 int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1035 {
1036         struct ath_buf *bf;
1037         struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
1038         struct ieee80211_rx_status *rxs;
1039         struct ath_hw *ah = sc->sc_ah;
1040         struct ath_common *common = ath9k_hw_common(ah);
1041         struct ieee80211_hw *hw = sc->hw;
1042         struct ieee80211_hdr *hdr;
1043         int retval;
1044         bool decrypt_error = false;
1045         struct ath_rx_status rs;
1046         enum ath9k_rx_qtype qtype;
1047         bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1048         int dma_type;
1049         u8 rx_status_len = ah->caps.rx_status_len;
1050         u64 tsf = 0;
1051         u32 tsf_lower = 0;
1052         unsigned long flags;
1053
1054         if (edma)
1055                 dma_type = DMA_BIDIRECTIONAL;
1056         else
1057                 dma_type = DMA_FROM_DEVICE;
1058
1059         qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
1060         spin_lock_bh(&sc->rx.rxbuflock);
1061
1062         tsf = ath9k_hw_gettsf64(ah);
1063         tsf_lower = tsf & 0xffffffff;
1064
1065         do {
1066                 /* If handling rx interrupt and flush is in progress => exit */
1067                 if (test_bit(SC_OP_RXFLUSH, &sc->sc_flags) && (flush == 0))
1068                         break;
1069
1070                 memset(&rs, 0, sizeof(rs));
1071                 if (edma)
1072                         bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1073                 else
1074                         bf = ath_get_next_rx_buf(sc, &rs);
1075
1076                 if (!bf)
1077                         break;
1078
1079                 skb = bf->bf_mpdu;
1080                 if (!skb)
1081                         continue;
1082
1083                 /*
1084                  * Take frame header from the first fragment and RX status from
1085                  * the last one.
1086                  */
1087                 if (sc->rx.frag)
1088                         hdr_skb = sc->rx.frag;
1089                 else
1090                         hdr_skb = skb;
1091
1092                 hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
1093                 rxs = IEEE80211_SKB_RXCB(hdr_skb);
1094                 if (ieee80211_is_beacon(hdr->frame_control)) {
1095                         RX_STAT_INC(rx_beacons);
1096                         if (!is_zero_ether_addr(common->curbssid) &&
1097                             ether_addr_equal(hdr->addr3, common->curbssid))
1098                                 rs.is_mybeacon = true;
1099                         else
1100                                 rs.is_mybeacon = false;
1101                 }
1102                 else
1103                         rs.is_mybeacon = false;
1104
1105                 sc->rx.num_pkts++;
1106                 ath_debug_stat_rx(sc, &rs);
1107
1108                 /*
1109                  * If we're asked to flush receive queue, directly
1110                  * chain it back at the queue without processing it.
1111                  */
1112                 if (test_bit(SC_OP_RXFLUSH, &sc->sc_flags)) {
1113                         RX_STAT_INC(rx_drop_rxflush);
1114                         goto requeue_drop_frag;
1115                 }
1116
1117                 memset(rxs, 0, sizeof(struct ieee80211_rx_status));
1118
1119                 rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1120                 if (rs.rs_tstamp > tsf_lower &&
1121                     unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1122                         rxs->mactime -= 0x100000000ULL;
1123
1124                 if (rs.rs_tstamp < tsf_lower &&
1125                     unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1126                         rxs->mactime += 0x100000000ULL;
1127
1128                 retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
1129                                                  rxs, &decrypt_error);
1130                 if (retval)
1131                         goto requeue_drop_frag;
1132
1133                 if (rs.is_mybeacon) {
1134                         sc->hw_busy_count = 0;
1135                         ath_start_rx_poll(sc, 3);
1136                 }
1137                 /* Ensure we always have an skb to requeue once we are done
1138                  * processing the current buffer's skb */
1139                 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
1140
1141                 /* If there is no memory we ignore the current RX'd frame,
1142                  * tell hardware it can give us a new frame using the old
1143                  * skb and put it at the tail of the sc->rx.rxbuf list for
1144                  * processing. */
1145                 if (!requeue_skb) {
1146                         RX_STAT_INC(rx_oom_err);
1147                         goto requeue_drop_frag;
1148                 }
1149
1150                 /* Unmap the frame */
1151                 dma_unmap_single(sc->dev, bf->bf_buf_addr,
1152                                  common->rx_bufsize,
1153                                  dma_type);
1154
1155                 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1156                 if (ah->caps.rx_status_len)
1157                         skb_pull(skb, ah->caps.rx_status_len);
1158
1159                 if (!rs.rs_more)
1160                         ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1161                                                  rxs, decrypt_error);
1162
1163                 /* We will now give hardware our shiny new allocated skb */
1164                 bf->bf_mpdu = requeue_skb;
1165                 bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
1166                                                  common->rx_bufsize,
1167                                                  dma_type);
1168                 if (unlikely(dma_mapping_error(sc->dev,
1169                           bf->bf_buf_addr))) {
1170                         dev_kfree_skb_any(requeue_skb);
1171                         bf->bf_mpdu = NULL;
1172                         bf->bf_buf_addr = 0;
1173                         ath_err(common, "dma_mapping_error() on RX\n");
1174                         ieee80211_rx(hw, skb);
1175                         break;
1176                 }
1177
1178                 if (rs.rs_more) {
1179                         RX_STAT_INC(rx_frags);
1180                         /*
1181                          * rs_more indicates chained descriptors which can be
1182                          * used to link buffers together for a sort of
1183                          * scatter-gather operation.
1184                          */
1185                         if (sc->rx.frag) {
1186                                 /* too many fragments - cannot handle frame */
1187                                 dev_kfree_skb_any(sc->rx.frag);
1188                                 dev_kfree_skb_any(skb);
1189                                 RX_STAT_INC(rx_too_many_frags_err);
1190                                 skb = NULL;
1191                         }
1192                         sc->rx.frag = skb;
1193                         goto requeue;
1194                 }
1195
1196                 if (sc->rx.frag) {
1197                         int space = skb->len - skb_tailroom(hdr_skb);
1198
1199                         if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
1200                                 dev_kfree_skb(skb);
1201                                 RX_STAT_INC(rx_oom_err);
1202                                 goto requeue_drop_frag;
1203                         }
1204
1205                         sc->rx.frag = NULL;
1206
1207                         skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
1208                                                   skb->len);
1209                         dev_kfree_skb_any(skb);
1210                         skb = hdr_skb;
1211                 }
1212
1213
1214                 if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) {
1215
1216                         /*
1217                          * change the default rx antenna if rx diversity
1218                          * chooses the other antenna 3 times in a row.
1219                          */
1220                         if (sc->rx.defant != rs.rs_antenna) {
1221                                 if (++sc->rx.rxotherant >= 3)
1222                                         ath_setdefantenna(sc, rs.rs_antenna);
1223                         } else {
1224                                 sc->rx.rxotherant = 0;
1225                         }
1226
1227                 }
1228
1229                 if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
1230                         skb_trim(skb, skb->len - 8);
1231
1232                 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1233                 if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
1234                                      PS_WAIT_FOR_CAB |
1235                                      PS_WAIT_FOR_PSPOLL_DATA)) ||
1236                     ath9k_check_auto_sleep(sc))
1237                         ath_rx_ps(sc, skb, rs.is_mybeacon);
1238                 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1239
1240                 if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx == 3)
1241                         ath_ant_comb_scan(sc, &rs);
1242
1243                 ieee80211_rx(hw, skb);
1244
1245 requeue_drop_frag:
1246                 if (sc->rx.frag) {
1247                         dev_kfree_skb_any(sc->rx.frag);
1248                         sc->rx.frag = NULL;
1249                 }
1250 requeue:
1251                 if (edma) {
1252                         list_add_tail(&bf->list, &sc->rx.rxbuf);
1253                         ath_rx_edma_buf_link(sc, qtype);
1254                 } else {
1255                         list_move_tail(&bf->list, &sc->rx.rxbuf);
1256                         ath_rx_buf_link(sc, bf);
1257                         if (!flush)
1258                                 ath9k_hw_rxena(ah);
1259                 }
1260         } while (1);
1261
1262         spin_unlock_bh(&sc->rx.rxbuflock);
1263
1264         if (!(ah->imask & ATH9K_INT_RXEOL)) {
1265                 ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
1266                 ath9k_hw_set_interrupts(ah);
1267         }
1268
1269         return 0;
1270 }