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