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