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