blob: d13faa44005e0af8ba16721531f7dd1ebdc40d41 [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
Sujith Manoharan5b681382011-05-17 13:36:18 +05302 * Copyright (c) 2008-2011 Atheros Communications Inc.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07003 *
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
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000017#include <linux/dma-mapping.h>
Simon Wunderliche93d0832013-01-08 14:48:58 +010018#include <linux/relay.h>
Sujith394cf0a2009-02-09 13:26:54 +053019#include "ath9k.h"
Luis R. Rodriguezb622a722010-04-15 17:39:28 -040020#include "ar9003_mac.h"
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070021
Felix Fietkaub5c804752010-04-15 17:38:48 -040022#define SKB_CB_ATHBUF(__skb) (*((struct ath_buf **)__skb->cb))
23
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -070024static 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
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070030/*
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.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070037 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070038static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
39{
Sujithcbe61d82009-02-09 13:27:12 +053040 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080041 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070042 struct ath_desc *ds;
43 struct sk_buff *skb;
44
45 ATH_RXBUF_RESET(bf);
46
47 ds = bf->bf_desc;
Sujithbe0418a2008-11-18 09:05:55 +053048 ds->ds_link = 0; /* link to null */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070049 ds->ds_data = bf->bf_buf_addr;
50
Sujithbe0418a2008-11-18 09:05:55 +053051 /* virtual addr of the beginning of the buffer. */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070052 skb = bf->bf_mpdu;
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -070053 BUG_ON(skb == NULL);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070054 ds->ds_vdata = skb->data;
55
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080056 /*
57 * setup rx descriptors. The rx_bufsize here tells the hardware
Luis R. Rodriguezb4b6cda2008-11-20 17:15:13 -080058 * how much data it can DMA to us and that we are prepared
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080059 * to process
60 */
Sujithb77f4832008-12-07 21:44:03 +053061 ath9k_hw_setuprxdesc(ah, ds,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080062 common->rx_bufsize,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070063 0);
64
Sujithb77f4832008-12-07 21:44:03 +053065 if (sc->rx.rxlink == NULL)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070066 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
67 else
Sujithb77f4832008-12-07 21:44:03 +053068 *sc->rx.rxlink = bf->bf_daddr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070069
Sujithb77f4832008-12-07 21:44:03 +053070 sc->rx.rxlink = &ds->ds_link;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070071}
72
Sujithff37e332008-11-24 12:07:55 +053073static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
74{
75 /* XXX block beacon interrupts */
76 ath9k_hw_setantenna(sc->sc_ah, antenna);
Sujithb77f4832008-12-07 21:44:03 +053077 sc->rx.defant = antenna;
78 sc->rx.rxotherant = 0;
Sujithff37e332008-11-24 12:07:55 +053079}
80
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070081static void ath_opmode_init(struct ath_softc *sc)
82{
Sujithcbe61d82009-02-09 13:27:12 +053083 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez15107182009-09-10 09:22:37 -070084 struct ath_common *common = ath9k_hw_common(ah);
85
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070086 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 */
Felix Fietkau364734f2010-09-14 20:22:44 +020093 ath_hw_setbssidmask(common);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070094
95 /* configure operational mode */
96 ath9k_hw_setopmode(ah);
97
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070098 /* calculate and install multicast filter */
99 mfilt[0] = mfilt[1] = ~0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700100 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700101}
102
Felix Fietkaub5c804752010-04-15 17:38:48 -0400103static 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
132static void ath_rx_addbuffer_edma(struct ath_softc *sc,
Sujith Manoharan7a8972032013-04-23 12:22:16 +0530133 enum ath9k_rx_qtype qtype)
Felix Fietkaub5c804752010-04-15 17:38:48 -0400134{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400135 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Mohammed Shafi Shajakhan6a01f0c2012-02-28 20:54:44 +0530136 struct ath_buf *bf, *tbf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400137
Felix Fietkaub5c804752010-04-15 17:38:48 -0400138 if (list_empty(&sc->rx.rxbuf)) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800139 ath_dbg(common, QUEUE, "No free rx buf available\n");
Felix Fietkaub5c804752010-04-15 17:38:48 -0400140 return;
141 }
142
Mohammed Shafi Shajakhan6a01f0c2012-02-28 20:54:44 +0530143 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
Felix Fietkaub5c804752010-04-15 17:38:48 -0400144 if (!ath_rx_edma_buf_link(sc, qtype))
145 break;
146
Felix Fietkaub5c804752010-04-15 17:38:48 -0400147}
148
149static 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
165static void ath_rx_edma_cleanup(struct ath_softc *sc)
166{
Mohammed Shafi Shajakhanba542382011-09-23 14:33:14 +0530167 struct ath_hw *ah = sc->sc_ah;
168 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400169 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) {
Mohammed Shafi Shajakhanba542382011-09-23 14:33:14 +0530175 if (bf->bf_mpdu) {
176 dma_unmap_single(sc->dev, bf->bf_buf_addr,
177 common->rx_bufsize,
178 DMA_BIDIRECTIONAL);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400179 dev_kfree_skb_any(bf->bf_mpdu);
Mohammed Shafi Shajakhanba542382011-09-23 14:33:14 +0530180 bf->bf_buf_addr = 0;
181 bf->bf_mpdu = NULL;
182 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400183 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400184}
185
186static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
187{
188 skb_queue_head_init(&rx_edma->rx_fifo);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400189 rx_edma->rx_fifo_hwsize = size;
190}
191
192static 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
Felix Fietkaub5c804752010-04-15 17:38:48 -0400201 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;
Felix Fietkaub81950b12012-12-12 13:14:22 +0100210 bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400211 if (!bf)
212 return -ENOMEM;
213
214 INIT_LIST_HEAD(&sc->rx.rxbuf);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400215
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;
Ben Greear6cf9e992010-10-14 12:45:30 -0700233 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -0800234 ath_err(common,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400235 "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
245rx_init_fail:
246 ath_rx_edma_cleanup(sc);
247 return error;
248}
249
250static void ath_edma_start_recv(struct ath_softc *sc)
251{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400252 ath9k_hw_rxena(sc->sc_ah);
Sujith Manoharan7a8972032013-04-23 12:22:16 +0530253 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP);
254 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400255 ath_opmode_init(sc);
Sujith Manoharan4cb54fa2012-06-04 16:27:52 +0530256 ath9k_hw_startpcureceive(sc->sc_ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
Felix Fietkaub5c804752010-04-15 17:38:48 -0400257}
258
259static void ath_edma_stop_recv(struct ath_softc *sc)
260{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400261 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
262 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400263}
264
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700265int ath_rx_init(struct ath_softc *sc, int nbufs)
266{
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -0700267 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700268 struct sk_buff *skb;
269 struct ath_buf *bf;
270 int error = 0;
271
Luis R. Rodriguez4bdd1e92010-10-26 15:27:24 -0700272 spin_lock_init(&sc->sc_pcu_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700273
Felix Fietkau0d955212011-01-26 18:23:27 +0100274 common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
275 sc->sc_ah->caps.rx_status_len;
276
Felix Fietkaub5c804752010-04-15 17:38:48 -0400277 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
278 return ath_rx_edma_init(sc, nbufs);
279 } else {
Joe Perchesd2182b62011-12-15 14:55:53 -0800280 ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
Joe Perches226afe62010-12-02 19:12:37 -0800281 common->cachelsz, common->rx_bufsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700282
Felix Fietkaub5c804752010-04-15 17:38:48 -0400283 /* Initialize rx descriptors */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700284
Felix Fietkaub5c804752010-04-15 17:38:48 -0400285 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
Vasanthakumar Thiagarajan4adfcde2010-04-15 17:39:33 -0400286 "rx", nbufs, 1, 0);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400287 if (error != 0) {
Joe Perches38002762010-12-02 19:12:36 -0800288 ath_err(common,
289 "failed to allocate rx descriptors: %d\n",
290 error);
Sujith797fe5cb2009-03-30 15:28:45 +0530291 goto err;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700292 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400293
294 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
295 skb = ath_rxbuf_alloc(common, common->rx_bufsize,
296 GFP_KERNEL);
297 if (skb == NULL) {
298 error = -ENOMEM;
299 goto err;
300 }
301
302 bf->bf_mpdu = skb;
303 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
304 common->rx_bufsize,
305 DMA_FROM_DEVICE);
306 if (unlikely(dma_mapping_error(sc->dev,
307 bf->bf_buf_addr))) {
308 dev_kfree_skb_any(skb);
309 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -0700310 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -0800311 ath_err(common,
312 "dma_mapping_error() on RX init\n");
Felix Fietkaub5c804752010-04-15 17:38:48 -0400313 error = -ENOMEM;
314 goto err;
315 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400316 }
317 sc->rx.rxlink = NULL;
Sujith797fe5cb2009-03-30 15:28:45 +0530318 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700319
Sujith797fe5cb2009-03-30 15:28:45 +0530320err:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700321 if (error)
322 ath_rx_cleanup(sc);
323
324 return error;
325}
326
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700327void ath_rx_cleanup(struct ath_softc *sc)
328{
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800329 struct ath_hw *ah = sc->sc_ah;
330 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700331 struct sk_buff *skb;
332 struct ath_buf *bf;
333
Felix Fietkaub5c804752010-04-15 17:38:48 -0400334 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
335 ath_rx_edma_cleanup(sc);
336 return;
337 } else {
338 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
339 skb = bf->bf_mpdu;
340 if (skb) {
341 dma_unmap_single(sc->dev, bf->bf_buf_addr,
342 common->rx_bufsize,
343 DMA_FROM_DEVICE);
344 dev_kfree_skb(skb);
Ben Greear6cf9e992010-10-14 12:45:30 -0700345 bf->bf_buf_addr = 0;
346 bf->bf_mpdu = NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400347 }
Luis R. Rodriguez051b9192009-03-23 18:25:01 -0400348 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400349 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700350}
351
352/*
353 * Calculate the receive filter according to the
354 * operating mode and state:
355 *
356 * o always accept unicast, broadcast, and multicast traffic
357 * o maintain current state of phy error reception (the hal
358 * may enable phy error frames for noise immunity work)
359 * o probe request frames are accepted only when operating in
360 * hostap, adhoc, or monitor modes
361 * o enable promiscuous mode according to the interface state
362 * o accept beacons:
363 * - when operating in adhoc mode so the 802.11 layer creates
364 * node table entries for peers,
365 * - when operating in station mode for collecting rssi data when
366 * the station is otherwise quiet, or
367 * - when operating as a repeater so we see repeater-sta beacons
368 * - when scanning
369 */
370
371u32 ath_calcrxfilter(struct ath_softc *sc)
372{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700373 u32 rfilt;
374
Felix Fietkauac066972011-10-08 15:49:57 +0200375 rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700376 | ATH9K_RX_FILTER_MCAST;
377
Zefir Kurtisi73e49372013-04-03 18:31:31 +0200378 /* if operating on a DFS channel, enable radar pulse detection */
379 if (sc->hw->conf.radar_enabled)
380 rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR;
381
Jouni Malinen9c1d8e42010-10-13 17:29:31 +0300382 if (sc->rx.rxfilter & FIF_PROBE_REQ)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700383 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
384
Jouni Malinen217ba9d2009-03-10 10:55:50 +0200385 /*
386 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
387 * mode interface or when in monitor mode. AP mode does not need this
388 * since it receives all in-BSS frames anyway.
389 */
Felix Fietkau2e286942011-03-09 01:48:12 +0100390 if (sc->sc_ah->is_monitoring)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700391 rfilt |= ATH9K_RX_FILTER_PROM;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700392
Sujithd42c6b72009-02-04 08:10:22 +0530393 if (sc->rx.rxfilter & FIF_CONTROL)
394 rfilt |= ATH9K_RX_FILTER_CONTROL;
395
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530396 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
Ben Greearcfda6692010-09-14 12:00:22 -0700397 (sc->nvifs <= 1) &&
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530398 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
399 rfilt |= ATH9K_RX_FILTER_MYBEACON;
400 else
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700401 rfilt |= ATH9K_RX_FILTER_BEACON;
402
Felix Fietkau264bbec2011-04-07 19:24:23 +0200403 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
Senthil Balasubramanian66afad02009-09-18 15:06:07 +0530404 (sc->rx.rxfilter & FIF_PSPOLL))
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530405 rfilt |= ATH9K_RX_FILTER_PSPOLL;
Sujithbe0418a2008-11-18 09:05:55 +0530406
Sujith7ea310b2009-09-03 12:08:43 +0530407 if (conf_is_ht(&sc->hw->conf))
408 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
409
Felix Fietkau7545daf2011-01-24 19:23:16 +0100410 if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
Thomas Wagnera5494592012-09-25 21:32:55 +0530411 /* This is needed for older chips */
412 if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
Javier Cardona5eb6ba82009-08-20 19:12:07 -0700413 rfilt |= ATH9K_RX_FILTER_PROM;
Jouni Malinenb93bce22009-03-03 19:23:30 +0200414 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
415 }
416
Gabor Juhosb3d7aa42012-07-03 19:13:33 +0200417 if (AR_SREV_9550(sc->sc_ah))
418 rfilt |= ATH9K_RX_FILTER_4ADDRESS;
419
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700420 return rfilt;
Sujith7dcfdcd2008-08-11 14:03:13 +0530421
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700422}
423
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700424int ath_startrecv(struct ath_softc *sc)
425{
Sujithcbe61d82009-02-09 13:27:12 +0530426 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700427 struct ath_buf *bf, *tbf;
428
Felix Fietkaub5c804752010-04-15 17:38:48 -0400429 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
430 ath_edma_start_recv(sc);
431 return 0;
432 }
433
Sujithb77f4832008-12-07 21:44:03 +0530434 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700435 goto start_recv;
436
Sujithb77f4832008-12-07 21:44:03 +0530437 sc->rx.rxlink = NULL;
438 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700439 ath_rx_buf_link(sc, bf);
440 }
441
442 /* We could have deleted elements so the list may be empty now */
Sujithb77f4832008-12-07 21:44:03 +0530443 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700444 goto start_recv;
445
Sujithb77f4832008-12-07 21:44:03 +0530446 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700447 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
Sujithbe0418a2008-11-18 09:05:55 +0530448 ath9k_hw_rxena(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700449
450start_recv:
Sujithbe0418a2008-11-18 09:05:55 +0530451 ath_opmode_init(sc);
Sujith Manoharan4cb54fa2012-06-04 16:27:52 +0530452 ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
Sujithbe0418a2008-11-18 09:05:55 +0530453
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700454 return 0;
455}
456
Felix Fietkau4b883f02013-01-09 16:16:56 +0100457static void ath_flushrecv(struct ath_softc *sc)
458{
459 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
460 ath_rx_tasklet(sc, 1, true);
461 ath_rx_tasklet(sc, 1, false);
462}
463
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700464bool ath_stoprecv(struct ath_softc *sc)
465{
Sujithcbe61d82009-02-09 13:27:12 +0530466 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau5882da022011-04-08 20:13:18 +0200467 bool stopped, reset = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700468
Felix Fietkaud47844a2010-11-20 03:08:47 +0100469 ath9k_hw_abortpcurecv(ah);
Sujithbe0418a2008-11-18 09:05:55 +0530470 ath9k_hw_setrxfilter(ah, 0);
Felix Fietkau5882da022011-04-08 20:13:18 +0200471 stopped = ath9k_hw_stopdmarecv(ah, &reset);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400472
Felix Fietkau4b883f02013-01-09 16:16:56 +0100473 ath_flushrecv(sc);
474
Felix Fietkaub5c804752010-04-15 17:38:48 -0400475 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
476 ath_edma_stop_recv(sc);
477 else
478 sc->rx.rxlink = NULL;
Sujithbe0418a2008-11-18 09:05:55 +0530479
Rajkumar Manoharand5847472010-12-20 14:39:51 +0530480 if (!(ah->ah_flags & AH_UNPLUGGED) &&
481 unlikely(!stopped)) {
Ben Greeard7fd1b502010-12-06 13:13:07 -0800482 ath_err(ath9k_hw_common(sc->sc_ah),
483 "Could not stop RX, we could be "
484 "confusing the DMA engine when we start RX up\n");
485 ATH_DBG_WARN_ON_ONCE(!stopped);
486 }
Felix Fietkau2232d312011-04-15 00:41:43 +0200487 return stopped && !reset;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700488}
489
Jouni Malinencc659652009-05-14 21:28:48 +0300490static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
491{
492 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
493 struct ieee80211_mgmt *mgmt;
494 u8 *pos, *end, id, elen;
495 struct ieee80211_tim_ie *tim;
496
497 mgmt = (struct ieee80211_mgmt *)skb->data;
498 pos = mgmt->u.beacon.variable;
499 end = skb->data + skb->len;
500
501 while (pos + 2 < end) {
502 id = *pos++;
503 elen = *pos++;
504 if (pos + elen > end)
505 break;
506
507 if (id == WLAN_EID_TIM) {
508 if (elen < sizeof(*tim))
509 break;
510 tim = (struct ieee80211_tim_ie *) pos;
511 if (tim->dtim_count != 0)
512 break;
513 return tim->bitmap_ctrl & 0x01;
514 }
515
516 pos += elen;
517 }
518
519 return false;
520}
521
Jouni Malinencc659652009-05-14 21:28:48 +0300522static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
523{
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700524 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300525
526 if (skb->len < 24 + 8 + 2 + 2)
527 return;
528
Sujith1b04b932010-01-08 10:36:05 +0530529 sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
Gabor Juhos293dc5d2009-06-19 12:17:48 +0200530
Sujith1b04b932010-01-08 10:36:05 +0530531 if (sc->ps_flags & PS_BEACON_SYNC) {
532 sc->ps_flags &= ~PS_BEACON_SYNC;
Joe Perchesd2182b62011-12-15 14:55:53 -0800533 ath_dbg(common, PS,
Sujith Manoharan1a6404a2013-02-04 15:38:24 +0530534 "Reconfigure beacon timers based on synchronized timestamp\n");
Sujith Manoharanef4ad632012-07-17 17:15:56 +0530535 ath9k_set_beacon(sc);
Jouni Malinenccdfeab2009-05-20 21:59:08 +0300536 }
537
Jouni Malinencc659652009-05-14 21:28:48 +0300538 if (ath_beacon_dtim_pending_cab(skb)) {
539 /*
540 * Remain awake waiting for buffered broadcast/multicast
Gabor Juhos58f5fff2009-06-17 20:53:20 +0200541 * frames. If the last broadcast/multicast frame is not
542 * received properly, the next beacon frame will work as
543 * a backup trigger for returning into NETWORK SLEEP state,
544 * so we are waiting for it as well.
Jouni Malinencc659652009-05-14 21:28:48 +0300545 */
Joe Perchesd2182b62011-12-15 14:55:53 -0800546 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800547 "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
Sujith1b04b932010-01-08 10:36:05 +0530548 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
Jouni Malinencc659652009-05-14 21:28:48 +0300549 return;
550 }
551
Sujith1b04b932010-01-08 10:36:05 +0530552 if (sc->ps_flags & PS_WAIT_FOR_CAB) {
Jouni Malinencc659652009-05-14 21:28:48 +0300553 /*
554 * This can happen if a broadcast frame is dropped or the AP
555 * fails to send a frame indicating that all CAB frames have
556 * been delivered.
557 */
Sujith1b04b932010-01-08 10:36:05 +0530558 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
Joe Perchesd2182b62011-12-15 14:55:53 -0800559 ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
Jouni Malinencc659652009-05-14 21:28:48 +0300560 }
Jouni Malinencc659652009-05-14 21:28:48 +0300561}
562
Rajkumar Manoharanf73c6042011-09-26 22:16:56 +0530563static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
Jouni Malinencc659652009-05-14 21:28:48 +0300564{
565 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700566 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300567
568 hdr = (struct ieee80211_hdr *)skb->data;
569
570 /* Process Beacon and CAB receive in PS state */
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -0700571 if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530572 && mybeacon) {
Jouni Malinencc659652009-05-14 21:28:48 +0300573 ath_rx_ps_beacon(sc, skb);
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530574 } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
575 (ieee80211_is_data(hdr->frame_control) ||
576 ieee80211_is_action(hdr->frame_control)) &&
577 is_multicast_ether_addr(hdr->addr1) &&
578 !ieee80211_has_moredata(hdr->frame_control)) {
Jouni Malinencc659652009-05-14 21:28:48 +0300579 /*
580 * No more broadcast/multicast frames to be received at this
581 * point.
582 */
Senthil Balasubramanian3fac6df2010-09-16 15:12:35 -0400583 sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
Joe Perchesd2182b62011-12-15 14:55:53 -0800584 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800585 "All PS CAB frames received, back to sleep\n");
Sujith1b04b932010-01-08 10:36:05 +0530586 } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300587 !is_multicast_ether_addr(hdr->addr1) &&
588 !ieee80211_has_morefrags(hdr->frame_control)) {
Sujith1b04b932010-01-08 10:36:05 +0530589 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
Joe Perchesd2182b62011-12-15 14:55:53 -0800590 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800591 "Going back to sleep after having received PS-Poll data (0x%lx)\n",
Sujith1b04b932010-01-08 10:36:05 +0530592 sc->ps_flags & (PS_WAIT_FOR_BEACON |
593 PS_WAIT_FOR_CAB |
594 PS_WAIT_FOR_PSPOLL_DATA |
595 PS_WAIT_FOR_TX_ACK));
Jouni Malinencc659652009-05-14 21:28:48 +0300596 }
597}
598
Felix Fietkaub5c804752010-04-15 17:38:48 -0400599static bool ath_edma_get_buffers(struct ath_softc *sc,
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100600 enum ath9k_rx_qtype qtype,
601 struct ath_rx_status *rs,
602 struct ath_buf **dest)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700603{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400604 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
605 struct ath_hw *ah = sc->sc_ah;
606 struct ath_common *common = ath9k_hw_common(ah);
607 struct sk_buff *skb;
Sujithbe0418a2008-11-18 09:05:55 +0530608 struct ath_buf *bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400609 int ret;
610
611 skb = skb_peek(&rx_edma->rx_fifo);
612 if (!skb)
613 return false;
614
615 bf = SKB_CB_ATHBUF(skb);
616 BUG_ON(!bf);
617
Ming Leice9426d2010-05-15 18:25:40 +0800618 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400619 common->rx_bufsize, DMA_FROM_DEVICE);
620
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100621 ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
Ming Leice9426d2010-05-15 18:25:40 +0800622 if (ret == -EINPROGRESS) {
623 /*let device gain the buffer again*/
624 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
625 common->rx_bufsize, DMA_FROM_DEVICE);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400626 return false;
Ming Leice9426d2010-05-15 18:25:40 +0800627 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400628
629 __skb_unlink(skb, &rx_edma->rx_fifo);
630 if (ret == -EINVAL) {
631 /* corrupt descriptor, skip this one and the following one */
632 list_add_tail(&bf->list, &sc->rx.rxbuf);
633 ath_rx_edma_buf_link(sc, qtype);
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100634
Felix Fietkaub5c804752010-04-15 17:38:48 -0400635 skb = skb_peek(&rx_edma->rx_fifo);
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100636 if (skb) {
637 bf = SKB_CB_ATHBUF(skb);
638 BUG_ON(!bf);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400639
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100640 __skb_unlink(skb, &rx_edma->rx_fifo);
641 list_add_tail(&bf->list, &sc->rx.rxbuf);
642 ath_rx_edma_buf_link(sc, qtype);
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100643 }
Tom Hughes6bb51c72012-06-27 18:21:15 +0100644
645 bf = NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400646 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400647
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100648 *dest = bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400649 return true;
650}
651
652static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
653 struct ath_rx_status *rs,
654 enum ath9k_rx_qtype qtype)
655{
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100656 struct ath_buf *bf = NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400657
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100658 while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
659 if (!bf)
660 continue;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400661
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100662 return bf;
663 }
664 return NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400665}
666
667static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
668 struct ath_rx_status *rs)
669{
670 struct ath_hw *ah = sc->sc_ah;
671 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700672 struct ath_desc *ds;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400673 struct ath_buf *bf;
674 int ret;
675
676 if (list_empty(&sc->rx.rxbuf)) {
677 sc->rx.rxlink = NULL;
678 return NULL;
679 }
680
681 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
682 ds = bf->bf_desc;
683
684 /*
685 * Must provide the virtual address of the current
686 * descriptor, the physical address, and the virtual
687 * address of the next descriptor in the h/w chain.
688 * This allows the HAL to look ahead to see if the
689 * hardware is done with a descriptor by checking the
690 * done bit in the following descriptor and the address
691 * of the current descriptor the DMA engine is working
692 * on. All this is necessary because of our use of
693 * a self-linked list to avoid rx overruns.
694 */
Rajkumar Manoharan3de21112011-08-13 10:28:11 +0530695 ret = ath9k_hw_rxprocdesc(ah, ds, rs);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400696 if (ret == -EINPROGRESS) {
697 struct ath_rx_status trs;
698 struct ath_buf *tbf;
699 struct ath_desc *tds;
700
701 memset(&trs, 0, sizeof(trs));
702 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
703 sc->rx.rxlink = NULL;
704 return NULL;
705 }
706
707 tbf = list_entry(bf->list.next, struct ath_buf, list);
708
709 /*
710 * On some hardware the descriptor status words could
711 * get corrupted, including the done bit. Because of
712 * this, check if the next descriptor's done bit is
713 * set or not.
714 *
715 * If the next descriptor's done bit is set, the current
716 * descriptor has been corrupted. Force s/w to discard
717 * this descriptor and continue...
718 */
719
720 tds = tbf->bf_desc;
Rajkumar Manoharan3de21112011-08-13 10:28:11 +0530721 ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400722 if (ret == -EINPROGRESS)
723 return NULL;
Felix Fietkau723e7112013-04-08 00:04:11 +0200724
725 /*
726 * mark descriptor as zero-length and set the 'more'
727 * flag to ensure that both buffers get discarded
728 */
729 rs->rs_datalen = 0;
730 rs->rs_more = true;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400731 }
732
Felix Fietkaua3dc48e2013-01-09 16:16:52 +0100733 list_del(&bf->list);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400734 if (!bf->bf_mpdu)
735 return bf;
736
737 /*
738 * Synchronize the DMA transfer with CPU before
739 * 1. accessing the frame
740 * 2. requeueing the same buffer to h/w
741 */
Ming Leice9426d2010-05-15 18:25:40 +0800742 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400743 common->rx_bufsize,
744 DMA_FROM_DEVICE);
745
746 return bf;
747}
748
Sujithd4357002010-05-20 15:34:38 +0530749/* Assumes you've already done the endian to CPU conversion */
750static bool ath9k_rx_accept(struct ath_common *common,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700751 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530752 struct ieee80211_rx_status *rxs,
753 struct ath_rx_status *rx_stats,
754 bool *decrypt_error)
755{
Felix Fietkauec205992011-10-08 22:02:59 +0200756 struct ath_softc *sc = (struct ath_softc *) common->priv;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800757 bool is_mc, is_valid_tkip, strip_mic, mic_error;
Sujithd4357002010-05-20 15:34:38 +0530758 struct ath_hw *ah = common->ah;
Sujithd4357002010-05-20 15:34:38 +0530759 __le16 fc;
Vasanthakumar Thiagarajanb7b1b512010-05-20 14:34:48 -0700760 u8 rx_status_len = ah->caps.rx_status_len;
Sujithd4357002010-05-20 15:34:38 +0530761
Sujithd4357002010-05-20 15:34:38 +0530762 fc = hdr->frame_control;
763
Felix Fietkau66760ea2011-07-13 23:35:05 +0800764 is_mc = !!is_multicast_ether_addr(hdr->addr1);
765 is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
766 test_bit(rx_stats->rs_keyix, common->tkip_keymap);
Bill Jordan152e5852011-08-19 11:10:22 -0400767 strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
Michael Liang2a5783b2012-04-20 17:11:57 +0800768 ieee80211_has_protected(fc) &&
Bill Jordan152e5852011-08-19 11:10:22 -0400769 !(rx_stats->rs_status &
Felix Fietkau846d9362011-10-08 22:02:58 +0200770 (ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
771 ATH9K_RXERR_KEYMISS));
Felix Fietkau66760ea2011-07-13 23:35:05 +0800772
Felix Fietkauf88373f2012-02-05 21:15:17 +0100773 /*
774 * Key miss events are only relevant for pairwise keys where the
775 * descriptor does contain a valid key index. This has been observed
776 * mostly with CCMP encryption.
777 */
Felix Fietkaubed3d9c2012-06-23 19:23:31 +0200778 if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID ||
779 !test_bit(rx_stats->rs_keyix, common->ccmp_keymap))
Felix Fietkauf88373f2012-02-05 21:15:17 +0100780 rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
781
Ben Greear15072182012-04-03 09:18:59 -0700782 if (!rx_stats->rs_datalen) {
783 RX_STAT_INC(rx_len_err);
Sujithd4357002010-05-20 15:34:38 +0530784 return false;
Ben Greear15072182012-04-03 09:18:59 -0700785 }
786
Sujithd4357002010-05-20 15:34:38 +0530787 /*
788 * rs_status follows rs_datalen so if rs_datalen is too large
789 * we can take a hint that hardware corrupted it, so ignore
790 * those frames.
791 */
Ben Greear15072182012-04-03 09:18:59 -0700792 if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len)) {
793 RX_STAT_INC(rx_len_err);
Sujithd4357002010-05-20 15:34:38 +0530794 return false;
Ben Greear15072182012-04-03 09:18:59 -0700795 }
Sujithd4357002010-05-20 15:34:38 +0530796
Felix Fietkau0d955212011-01-26 18:23:27 +0100797 /* Only use error bits from the last fragment */
Sujithd4357002010-05-20 15:34:38 +0530798 if (rx_stats->rs_more)
Felix Fietkau0d955212011-01-26 18:23:27 +0100799 return true;
Sujithd4357002010-05-20 15:34:38 +0530800
Felix Fietkau66760ea2011-07-13 23:35:05 +0800801 mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
802 !ieee80211_has_morefrags(fc) &&
803 !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
804 (rx_stats->rs_status & ATH9K_RXERR_MIC);
805
Sujithd4357002010-05-20 15:34:38 +0530806 /*
807 * The rx_stats->rs_status will not be set until the end of the
808 * chained descriptors so it can be ignored if rs_more is set. The
809 * rs_more will be false at the last element of the chained
810 * descriptors.
811 */
812 if (rx_stats->rs_status != 0) {
Felix Fietkau846d9362011-10-08 22:02:58 +0200813 u8 status_mask;
814
Felix Fietkau66760ea2011-07-13 23:35:05 +0800815 if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
Sujithd4357002010-05-20 15:34:38 +0530816 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800817 mic_error = false;
818 }
Sujithd4357002010-05-20 15:34:38 +0530819 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
820 return false;
821
Felix Fietkau846d9362011-10-08 22:02:58 +0200822 if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
823 (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
Sujithd4357002010-05-20 15:34:38 +0530824 *decrypt_error = true;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800825 mic_error = false;
Sujithd4357002010-05-20 15:34:38 +0530826 }
Felix Fietkau66760ea2011-07-13 23:35:05 +0800827
Sujithd4357002010-05-20 15:34:38 +0530828 /*
829 * Reject error frames with the exception of
830 * decryption and MIC failures. For monitor mode,
831 * we also ignore the CRC error.
832 */
Felix Fietkau846d9362011-10-08 22:02:58 +0200833 status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
834 ATH9K_RXERR_KEYMISS;
835
Felix Fietkauec205992011-10-08 22:02:59 +0200836 if (ah->is_monitoring && (sc->rx.rxfilter & FIF_FCSFAIL))
Felix Fietkau846d9362011-10-08 22:02:58 +0200837 status_mask |= ATH9K_RXERR_CRC;
838
839 if (rx_stats->rs_status & ~status_mask)
840 return false;
Sujithd4357002010-05-20 15:34:38 +0530841 }
Felix Fietkau66760ea2011-07-13 23:35:05 +0800842
843 /*
844 * For unicast frames the MIC error bit can have false positives,
845 * so all MIC error reports need to be validated in software.
846 * False negatives are not common, so skip software verification
847 * if the hardware considers the MIC valid.
848 */
849 if (strip_mic)
850 rxs->flag |= RX_FLAG_MMIC_STRIPPED;
851 else if (is_mc && mic_error)
852 rxs->flag |= RX_FLAG_MMIC_ERROR;
853
Sujithd4357002010-05-20 15:34:38 +0530854 return true;
855}
856
857static int ath9k_process_rate(struct ath_common *common,
858 struct ieee80211_hw *hw,
859 struct ath_rx_status *rx_stats,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700860 struct ieee80211_rx_status *rxs)
Sujithd4357002010-05-20 15:34:38 +0530861{
862 struct ieee80211_supported_band *sband;
863 enum ieee80211_band band;
864 unsigned int i = 0;
Ben Greear990e08a2012-04-17 15:19:03 -0700865 struct ath_softc __maybe_unused *sc = common->priv;
Sujithd4357002010-05-20 15:34:38 +0530866
Karl Beldan675a0b02013-03-25 16:26:57 +0100867 band = hw->conf.chandef.chan->band;
Sujithd4357002010-05-20 15:34:38 +0530868 sband = hw->wiphy->bands[band];
869
870 if (rx_stats->rs_rate & 0x80) {
871 /* HT rate */
872 rxs->flag |= RX_FLAG_HT;
873 if (rx_stats->rs_flags & ATH9K_RX_2040)
874 rxs->flag |= RX_FLAG_40MHZ;
875 if (rx_stats->rs_flags & ATH9K_RX_GI)
876 rxs->flag |= RX_FLAG_SHORT_GI;
877 rxs->rate_idx = rx_stats->rs_rate & 0x7f;
878 return 0;
879 }
880
881 for (i = 0; i < sband->n_bitrates; i++) {
882 if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
883 rxs->rate_idx = i;
884 return 0;
885 }
886 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
887 rxs->flag |= RX_FLAG_SHORTPRE;
888 rxs->rate_idx = i;
889 return 0;
890 }
891 }
892
893 /*
894 * No valid hardware bitrate found -- we should not get here
895 * because hardware has already validated this frame as OK.
896 */
Joe Perchesd2182b62011-12-15 14:55:53 -0800897 ath_dbg(common, ANY,
Joe Perches226afe62010-12-02 19:12:37 -0800898 "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
899 rx_stats->rs_rate);
Ben Greear15072182012-04-03 09:18:59 -0700900 RX_STAT_INC(rx_rate_err);
Sujithd4357002010-05-20 15:34:38 +0530901 return -EINVAL;
902}
903
904static void ath9k_process_rssi(struct ath_common *common,
905 struct ieee80211_hw *hw,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700906 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530907 struct ath_rx_status *rx_stats)
908{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100909 struct ath_softc *sc = hw->priv;
Sujithd4357002010-05-20 15:34:38 +0530910 struct ath_hw *ah = common->ah;
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200911 int last_rssi;
Felix Fietkau2ef16752012-03-03 15:17:06 +0100912 int rssi = rx_stats->rs_rssi;
Sujithd4357002010-05-20 15:34:38 +0530913
Rajkumar Manoharancf3af742011-08-27 16:17:47 +0530914 if (!rx_stats->is_mybeacon ||
915 ((ah->opmode != NL80211_IFTYPE_STATION) &&
916 (ah->opmode != NL80211_IFTYPE_ADHOC)))
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200917 return;
918
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200919 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
Felix Fietkau9ac586152011-01-24 19:23:18 +0100920 ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
Ben Greear686b9cb2010-09-23 09:44:36 -0700921
Felix Fietkau9ac586152011-01-24 19:23:18 +0100922 last_rssi = sc->last_rssi;
Sujithd4357002010-05-20 15:34:38 +0530923 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
Felix Fietkau2ef16752012-03-03 15:17:06 +0100924 rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
925 if (rssi < 0)
926 rssi = 0;
Sujithd4357002010-05-20 15:34:38 +0530927
928 /* Update Beacon RSSI, this is used by ANI. */
Felix Fietkau2ef16752012-03-03 15:17:06 +0100929 ah->stats.avgbrssi = rssi;
Sujithd4357002010-05-20 15:34:38 +0530930}
931
932/*
933 * For Decrypt or Demic errors, we only mark packet status here and always push
934 * up the frame up to let mac80211 handle the actual error case, be it no
935 * decryption key or real decryption error. This let us keep statistics there.
936 */
Felix Fietkau723e7112013-04-08 00:04:11 +0200937static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700938 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530939 struct ath_rx_status *rx_stats,
940 struct ieee80211_rx_status *rx_status,
941 bool *decrypt_error)
942{
Felix Fietkau723e7112013-04-08 00:04:11 +0200943 struct ieee80211_hw *hw = sc->hw;
944 struct ath_hw *ah = sc->sc_ah;
945 struct ath_common *common = ath9k_hw_common(ah);
946 bool discard_current = sc->rx.discard_next;
947
948 sc->rx.discard_next = rx_stats->rs_more;
949 if (discard_current)
950 return -EINVAL;
Felix Fietkauf749b942011-07-28 14:08:57 +0200951
Sujithd4357002010-05-20 15:34:38 +0530952 /*
953 * everything but the rate is checked here, the rate check is done
954 * separately to avoid doing two lookups for a rate for each frame.
955 */
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700956 if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
Sujithd4357002010-05-20 15:34:38 +0530957 return -EINVAL;
958
Felix Fietkau0d955212011-01-26 18:23:27 +0100959 /* Only use status info from the last fragment */
960 if (rx_stats->rs_more)
961 return 0;
962
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700963 ath9k_process_rssi(common, hw, hdr, rx_stats);
Sujithd4357002010-05-20 15:34:38 +0530964
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700965 if (ath9k_process_rate(common, hw, rx_stats, rx_status))
Sujithd4357002010-05-20 15:34:38 +0530966 return -EINVAL;
967
Karl Beldan675a0b02013-03-25 16:26:57 +0100968 rx_status->band = hw->conf.chandef.chan->band;
969 rx_status->freq = hw->conf.chandef.chan->center_freq;
Felix Fietkauf749b942011-07-28 14:08:57 +0200970 rx_status->signal = ah->noise + rx_stats->rs_rssi;
Sujithd4357002010-05-20 15:34:38 +0530971 rx_status->antenna = rx_stats->rs_antenna;
Thomas Pedersen96d21372012-12-10 14:48:01 -0800972 rx_status->flag |= RX_FLAG_MACTIME_END;
Felix Fietkau2ef16752012-03-03 15:17:06 +0100973 if (rx_stats->rs_moreaggr)
974 rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
Sujithd4357002010-05-20 15:34:38 +0530975
Felix Fietkau723e7112013-04-08 00:04:11 +0200976 sc->rx.discard_next = false;
Sujithd4357002010-05-20 15:34:38 +0530977 return 0;
978}
979
980static void ath9k_rx_skb_postprocess(struct ath_common *common,
981 struct sk_buff *skb,
982 struct ath_rx_status *rx_stats,
983 struct ieee80211_rx_status *rxs,
984 bool decrypt_error)
985{
986 struct ath_hw *ah = common->ah;
987 struct ieee80211_hdr *hdr;
988 int hdrlen, padpos, padsize;
989 u8 keyix;
990 __le16 fc;
991
992 /* see if any padding is done by the hw and remove it */
993 hdr = (struct ieee80211_hdr *) skb->data;
994 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
995 fc = hdr->frame_control;
Felix Fietkauc60c9922013-04-08 00:04:09 +0200996 padpos = ieee80211_hdrlen(fc);
Sujithd4357002010-05-20 15:34:38 +0530997
998 /* The MAC header is padded to have 32-bit boundary if the
999 * packet payload is non-zero. The general calculation for
1000 * padsize would take into account odd header lengths:
1001 * padsize = (4 - padpos % 4) % 4; However, since only
1002 * even-length headers are used, padding can only be 0 or 2
1003 * bytes and we can optimize this a bit. In addition, we must
1004 * not try to remove padding from short control frames that do
1005 * not have payload. */
1006 padsize = padpos & 3;
1007 if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1008 memmove(skb->data + padsize, skb->data, padpos);
1009 skb_pull(skb, padsize);
1010 }
1011
1012 keyix = rx_stats->rs_keyix;
1013
1014 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1015 ieee80211_has_protected(fc)) {
1016 rxs->flag |= RX_FLAG_DECRYPTED;
1017 } else if (ieee80211_has_protected(fc)
1018 && !decrypt_error && skb->len >= hdrlen + 4) {
1019 keyix = skb->data[hdrlen + 3] >> 6;
1020
1021 if (test_bit(keyix, common->keymap))
1022 rxs->flag |= RX_FLAG_DECRYPTED;
1023 }
1024 if (ah->sw_mgmt_crypto &&
1025 (rxs->flag & RX_FLAG_DECRYPTED) &&
1026 ieee80211_is_mgmt(fc))
1027 /* Use software decrypt for management frames. */
1028 rxs->flag &= ~RX_FLAG_DECRYPTED;
1029}
Felix Fietkaub5c804752010-04-15 17:38:48 -04001030
Sven Eckelmannab2e2fc2013-01-31 10:26:46 +01001031#ifdef CONFIG_ATH9K_DEBUGFS
Simon Wunderliche93d0832013-01-08 14:48:58 +01001032static s8 fix_rssi_inv_only(u8 rssi_val)
1033{
1034 if (rssi_val == 128)
1035 rssi_val = 0;
1036 return (s8) rssi_val;
1037}
Sven Eckelmannab2e2fc2013-01-31 10:26:46 +01001038#endif
Simon Wunderliche93d0832013-01-08 14:48:58 +01001039
Simon Wunderlich9b99e662013-01-23 17:38:05 +01001040/* returns 1 if this was a spectral frame, even if not handled. */
1041static int ath_process_fft(struct ath_softc *sc, struct ieee80211_hdr *hdr,
1042 struct ath_rx_status *rs, u64 tsf)
Simon Wunderliche93d0832013-01-08 14:48:58 +01001043{
Sven Eckelmannbd2ffe12013-01-31 10:26:45 +01001044#ifdef CONFIG_ATH9K_DEBUGFS
Simon Wunderliche93d0832013-01-08 14:48:58 +01001045 struct ath_hw *ah = sc->sc_ah;
1046 u8 bins[SPECTRAL_HT20_NUM_BINS];
1047 u8 *vdata = (u8 *)hdr;
1048 struct fft_sample_ht20 fft_sample;
1049 struct ath_radar_info *radar_info;
1050 struct ath_ht20_mag_info *mag_info;
1051 int len = rs->rs_datalen;
Sven Eckelmann4ab0b0a2013-01-23 20:12:39 +01001052 int dc_pos;
Sven Eckelmann12824372013-01-31 10:26:48 +01001053 u16 length, max_magnitude;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001054
1055 /* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
1056 * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
1057 * yet, but this is supposed to be possible as well.
1058 */
1059 if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
1060 rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
1061 rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
Simon Wunderlich9b99e662013-01-23 17:38:05 +01001062 return 0;
1063
1064 /* check if spectral scan bit is set. This does not have to be checked
1065 * if received through a SPECTRAL phy error, but shouldn't hurt.
1066 */
1067 radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
1068 if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
1069 return 0;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001070
1071 /* Variation in the data length is possible and will be fixed later.
1072 * Note that we only support HT20 for now.
1073 *
1074 * TODO: add HT20_40 support as well.
1075 */
1076 if ((len > SPECTRAL_HT20_TOTAL_DATA_LEN + 2) ||
1077 (len < SPECTRAL_HT20_TOTAL_DATA_LEN - 1))
Simon Wunderlich9b99e662013-01-23 17:38:05 +01001078 return 1;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001079
1080 fft_sample.tlv.type = ATH_FFT_SAMPLE_HT20;
Sven Eckelmann12824372013-01-31 10:26:48 +01001081 length = sizeof(fft_sample) - sizeof(fft_sample.tlv);
1082 fft_sample.tlv.length = __cpu_to_be16(length);
Simon Wunderliche93d0832013-01-08 14:48:58 +01001083
Sven Eckelmann4ab0b0a2013-01-23 20:12:39 +01001084 fft_sample.freq = __cpu_to_be16(ah->curchan->chan->center_freq);
Simon Wunderliche93d0832013-01-08 14:48:58 +01001085 fft_sample.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl0);
1086 fft_sample.noise = ah->noise;
1087
1088 switch (len - SPECTRAL_HT20_TOTAL_DATA_LEN) {
1089 case 0:
1090 /* length correct, nothing to do. */
1091 memcpy(bins, vdata, SPECTRAL_HT20_NUM_BINS);
1092 break;
1093 case -1:
1094 /* first byte missing, duplicate it. */
1095 memcpy(&bins[1], vdata, SPECTRAL_HT20_NUM_BINS - 1);
1096 bins[0] = vdata[0];
1097 break;
1098 case 2:
1099 /* MAC added 2 extra bytes at bin 30 and 32, remove them. */
1100 memcpy(bins, vdata, 30);
1101 bins[30] = vdata[31];
1102 memcpy(&bins[31], &vdata[33], SPECTRAL_HT20_NUM_BINS - 31);
1103 break;
1104 case 1:
1105 /* MAC added 2 extra bytes AND first byte is missing. */
1106 bins[0] = vdata[0];
1107 memcpy(&bins[0], vdata, 30);
1108 bins[31] = vdata[31];
1109 memcpy(&bins[32], &vdata[33], SPECTRAL_HT20_NUM_BINS - 32);
1110 break;
1111 default:
Simon Wunderlich9b99e662013-01-23 17:38:05 +01001112 return 1;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001113 }
1114
1115 /* DC value (value in the middle) is the blind spot of the spectral
1116 * sample and invalid, interpolate it.
1117 */
1118 dc_pos = SPECTRAL_HT20_NUM_BINS / 2;
1119 bins[dc_pos] = (bins[dc_pos + 1] + bins[dc_pos - 1]) / 2;
1120
1121 /* mag data is at the end of the frame, in front of radar_info */
1122 mag_info = ((struct ath_ht20_mag_info *)radar_info) - 1;
1123
Sven Eckelmann4ab0b0a2013-01-23 20:12:39 +01001124 /* copy raw bins without scaling them */
1125 memcpy(fft_sample.data, bins, SPECTRAL_HT20_NUM_BINS);
1126 fft_sample.max_exp = mag_info->max_exp & 0xf;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001127
Sven Eckelmann12824372013-01-31 10:26:48 +01001128 max_magnitude = spectral_max_magnitude(mag_info->all_bins);
1129 fft_sample.max_magnitude = __cpu_to_be16(max_magnitude);
Simon Wunderliche93d0832013-01-08 14:48:58 +01001130 fft_sample.max_index = spectral_max_index(mag_info->all_bins);
1131 fft_sample.bitmap_weight = spectral_bitmap_weight(mag_info->all_bins);
Sven Eckelmann4ab0b0a2013-01-23 20:12:39 +01001132 fft_sample.tsf = __cpu_to_be64(tsf);
Simon Wunderliche93d0832013-01-08 14:48:58 +01001133
1134 ath_debug_send_fft_sample(sc, &fft_sample.tlv);
Simon Wunderlich9b99e662013-01-23 17:38:05 +01001135 return 1;
1136#else
1137 return 0;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001138#endif
1139}
1140
Christian Lamparter21fbbca2013-01-30 23:37:41 +01001141static void ath9k_apply_ampdu_details(struct ath_softc *sc,
1142 struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
1143{
1144 if (rs->rs_isaggr) {
1145 rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
1146
1147 rxs->ampdu_reference = sc->rx.ampdu_ref;
1148
1149 if (!rs->rs_moreaggr) {
1150 rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
1151 sc->rx.ampdu_ref++;
1152 }
1153
1154 if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
1155 rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
1156 }
1157}
1158
Felix Fietkaub5c804752010-04-15 17:38:48 -04001159int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1160{
1161 struct ath_buf *bf;
Felix Fietkau0d955212011-01-26 18:23:27 +01001162 struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001163 struct ieee80211_rx_status *rxs;
Sujithcbe61d82009-02-09 13:27:12 +05301164 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -07001165 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau7545daf2011-01-24 19:23:16 +01001166 struct ieee80211_hw *hw = sc->hw;
Sujithbe0418a2008-11-18 09:05:55 +05301167 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc9b14172009-11-04 16:47:22 -08001168 int retval;
Felix Fietkau29bffa92010-03-29 20:14:23 -07001169 struct ath_rx_status rs;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001170 enum ath9k_rx_qtype qtype;
1171 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1172 int dma_type;
Vasanthakumar Thiagarajan5c6dd922010-05-20 14:34:47 -07001173 u8 rx_status_len = ah->caps.rx_status_len;
Felix Fietkaua6d20552010-06-12 00:33:54 -04001174 u64 tsf = 0;
1175 u32 tsf_lower = 0;
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001176 unsigned long flags;
Felix Fietkau2e1cd492013-04-08 00:04:10 +02001177 dma_addr_t new_buf_addr;
Sujithbe0418a2008-11-18 09:05:55 +05301178
Felix Fietkaub5c804752010-04-15 17:38:48 -04001179 if (edma)
Felix Fietkaub5c804752010-04-15 17:38:48 -04001180 dma_type = DMA_BIDIRECTIONAL;
Ming Lei56824222010-05-14 21:15:38 +08001181 else
1182 dma_type = DMA_FROM_DEVICE;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001183
1184 qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001185
Felix Fietkaua6d20552010-06-12 00:33:54 -04001186 tsf = ath9k_hw_gettsf64(ah);
1187 tsf_lower = tsf & 0xffffffff;
1188
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001189 do {
Lorenzo Bianconie1352fd2012-08-10 11:00:24 +02001190 bool decrypt_error = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001191
Felix Fietkau29bffa92010-03-29 20:14:23 -07001192 memset(&rs, 0, sizeof(rs));
Felix Fietkaub5c804752010-04-15 17:38:48 -04001193 if (edma)
1194 bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1195 else
1196 bf = ath_get_next_rx_buf(sc, &rs);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001197
Felix Fietkaub5c804752010-04-15 17:38:48 -04001198 if (!bf)
1199 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001200
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001201 skb = bf->bf_mpdu;
Sujithbe0418a2008-11-18 09:05:55 +05301202 if (!skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001203 continue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001204
Felix Fietkau0d955212011-01-26 18:23:27 +01001205 /*
1206 * Take frame header from the first fragment and RX status from
1207 * the last one.
1208 */
1209 if (sc->rx.frag)
1210 hdr_skb = sc->rx.frag;
1211 else
1212 hdr_skb = skb;
1213
1214 hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
1215 rxs = IEEE80211_SKB_RXCB(hdr_skb);
Ben Greear15072182012-04-03 09:18:59 -07001216 if (ieee80211_is_beacon(hdr->frame_control)) {
1217 RX_STAT_INC(rx_beacons);
1218 if (!is_zero_ether_addr(common->curbssid) &&
Joe Perches2e42e472012-05-09 17:17:46 +00001219 ether_addr_equal(hdr->addr3, common->curbssid))
Ben Greear15072182012-04-03 09:18:59 -07001220 rs.is_mybeacon = true;
1221 else
1222 rs.is_mybeacon = false;
1223 }
Rajkumar Manoharancf3af742011-08-27 16:17:47 +05301224 else
1225 rs.is_mybeacon = false;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001226
Mohammed Shafi Shajakhanbe41b052012-10-08 21:30:51 +05301227 if (ieee80211_is_data_present(hdr->frame_control) &&
1228 !ieee80211_is_qos_nullfunc(hdr->frame_control))
1229 sc->rx.num_pkts++;
1230
Felix Fietkau29bffa92010-03-29 20:14:23 -07001231 ath_debug_stat_rx(sc, &rs);
Sujith1395d3f2010-01-08 10:36:11 +05301232
Ashok Nagarajanffb1c562012-03-09 18:57:39 -08001233 memset(rxs, 0, sizeof(struct ieee80211_rx_status));
1234
Felix Fietkaua6d20552010-06-12 00:33:54 -04001235 rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1236 if (rs.rs_tstamp > tsf_lower &&
1237 unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1238 rxs->mactime -= 0x100000000ULL;
1239
1240 if (rs.rs_tstamp < tsf_lower &&
1241 unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1242 rxs->mactime += 0x100000000ULL;
1243
Zefir Kurtisi73e49372013-04-03 18:31:31 +02001244 if (rs.rs_phyerr == ATH9K_PHYERR_RADAR)
1245 ath9k_dfs_process_phyerr(sc, hdr, &rs, rxs->mactime);
1246
Simon Wunderlich9b99e662013-01-23 17:38:05 +01001247 if (rs.rs_status & ATH9K_RXERR_PHY) {
1248 if (ath_process_fft(sc, hdr, &rs, rxs->mactime)) {
1249 RX_STAT_INC(rx_spectral);
1250 goto requeue_drop_frag;
1251 }
1252 }
Simon Wunderliche93d0832013-01-08 14:48:58 +01001253
Felix Fietkau723e7112013-04-08 00:04:11 +02001254 retval = ath9k_rx_skb_preprocess(sc, hdr, &rs, rxs,
1255 &decrypt_error);
Zefir Kurtisi83c76572011-11-16 11:09:44 +01001256 if (retval)
1257 goto requeue_drop_frag;
1258
Rajkumar Manoharan01e18912012-03-15 05:34:27 +05301259 if (rs.is_mybeacon) {
1260 sc->hw_busy_count = 0;
1261 ath_start_rx_poll(sc, 3);
1262 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001263 /* Ensure we always have an skb to requeue once we are done
1264 * processing the current buffer's skb */
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001265 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001266
1267 /* If there is no memory we ignore the current RX'd frame,
1268 * tell hardware it can give us a new frame using the old
Sujithb77f4832008-12-07 21:44:03 +05301269 * skb and put it at the tail of the sc->rx.rxbuf list for
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001270 * processing. */
Ben Greear15072182012-04-03 09:18:59 -07001271 if (!requeue_skb) {
1272 RX_STAT_INC(rx_oom_err);
Felix Fietkau0d955212011-01-26 18:23:27 +01001273 goto requeue_drop_frag;
Ben Greear15072182012-04-03 09:18:59 -07001274 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001275
Felix Fietkau2e1cd492013-04-08 00:04:10 +02001276 /* We will now give hardware our shiny new allocated skb */
1277 new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
1278 common->rx_bufsize, dma_type);
1279 if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
1280 dev_kfree_skb_any(requeue_skb);
1281 goto requeue_drop_frag;
1282 }
1283
1284 bf->bf_mpdu = requeue_skb;
1285 bf->bf_buf_addr = new_buf_addr;
1286
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301287 /* Unmap the frame */
Gabor Juhos7da3c552009-01-14 20:17:03 +01001288 dma_unmap_single(sc->dev, bf->bf_buf_addr,
Felix Fietkau2e1cd492013-04-08 00:04:10 +02001289 common->rx_bufsize, dma_type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001290
Felix Fietkaub5c804752010-04-15 17:38:48 -04001291 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1292 if (ah->caps.rx_status_len)
1293 skb_pull(skb, ah->caps.rx_status_len);
Sujithbe0418a2008-11-18 09:05:55 +05301294
Felix Fietkau0d955212011-01-26 18:23:27 +01001295 if (!rs.rs_more)
1296 ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1297 rxs, decrypt_error);
Sujithbe0418a2008-11-18 09:05:55 +05301298
Felix Fietkau0d955212011-01-26 18:23:27 +01001299 if (rs.rs_more) {
Ben Greear15072182012-04-03 09:18:59 -07001300 RX_STAT_INC(rx_frags);
Felix Fietkau0d955212011-01-26 18:23:27 +01001301 /*
1302 * rs_more indicates chained descriptors which can be
1303 * used to link buffers together for a sort of
1304 * scatter-gather operation.
1305 */
1306 if (sc->rx.frag) {
1307 /* too many fragments - cannot handle frame */
1308 dev_kfree_skb_any(sc->rx.frag);
1309 dev_kfree_skb_any(skb);
Ben Greear15072182012-04-03 09:18:59 -07001310 RX_STAT_INC(rx_too_many_frags_err);
Felix Fietkau0d955212011-01-26 18:23:27 +01001311 skb = NULL;
1312 }
1313 sc->rx.frag = skb;
1314 goto requeue;
1315 }
Felix Fietkau3747c3e2013-04-08 00:04:12 +02001316 if (rs.rs_status & ATH9K_RXERR_CORRUPT_DESC)
1317 goto requeue_drop_frag;
Felix Fietkau0d955212011-01-26 18:23:27 +01001318
1319 if (sc->rx.frag) {
1320 int space = skb->len - skb_tailroom(hdr_skb);
1321
Felix Fietkau0d955212011-01-26 18:23:27 +01001322 if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
1323 dev_kfree_skb(skb);
Ben Greear15072182012-04-03 09:18:59 -07001324 RX_STAT_INC(rx_oom_err);
Felix Fietkau0d955212011-01-26 18:23:27 +01001325 goto requeue_drop_frag;
1326 }
1327
Eric Dumazetb5447ff2012-03-15 13:43:29 -07001328 sc->rx.frag = NULL;
1329
Felix Fietkau0d955212011-01-26 18:23:27 +01001330 skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
1331 skb->len);
1332 dev_kfree_skb_any(skb);
1333 skb = hdr_skb;
1334 }
1335
Mohammed Shafi Shajakhaneb840a82011-11-29 20:30:35 +05301336
1337 if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) {
1338
1339 /*
1340 * change the default rx antenna if rx diversity
1341 * chooses the other antenna 3 times in a row.
1342 */
1343 if (sc->rx.defant != rs.rs_antenna) {
1344 if (++sc->rx.rxotherant >= 3)
1345 ath_setdefantenna(sc, rs.rs_antenna);
1346 } else {
1347 sc->rx.rxotherant = 0;
1348 }
1349
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001350 }
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301351
Felix Fietkau66760ea2011-07-13 23:35:05 +08001352 if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
1353 skb_trim(skb, skb->len - 8);
1354
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001355 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Mohammed Shafi Shajakhanaaef24b2010-12-07 20:40:58 +05301356 if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
Rajkumar Manoharanf73c6042011-09-26 22:16:56 +05301357 PS_WAIT_FOR_CAB |
1358 PS_WAIT_FOR_PSPOLL_DATA)) ||
1359 ath9k_check_auto_sleep(sc))
1360 ath_rx_ps(sc, skb, rs.is_mybeacon);
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001361 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Jouni Malinencc659652009-05-14 21:28:48 +03001362
Felix Fietkau43c35282011-09-03 01:40:27 +02001363 if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx == 3)
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001364 ath_ant_comb_scan(sc, &rs);
1365
Christian Lamparter21fbbca2013-01-30 23:37:41 +01001366 ath9k_apply_ampdu_details(sc, &rs, rxs);
1367
Felix Fietkau7545daf2011-01-24 19:23:16 +01001368 ieee80211_rx(hw, skb);
Jouni Malinencc659652009-05-14 21:28:48 +03001369
Felix Fietkau0d955212011-01-26 18:23:27 +01001370requeue_drop_frag:
1371 if (sc->rx.frag) {
1372 dev_kfree_skb_any(sc->rx.frag);
1373 sc->rx.frag = NULL;
1374 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001375requeue:
Felix Fietkaua3dc48e2013-01-09 16:16:52 +01001376 list_add_tail(&bf->list, &sc->rx.rxbuf);
1377 if (flush)
1378 continue;
1379
Felix Fietkaub5c804752010-04-15 17:38:48 -04001380 if (edma) {
Felix Fietkaub5c804752010-04-15 17:38:48 -04001381 ath_rx_edma_buf_link(sc, qtype);
1382 } else {
Felix Fietkaub5c804752010-04-15 17:38:48 -04001383 ath_rx_buf_link(sc, bf);
Felix Fietkaua3dc48e2013-01-09 16:16:52 +01001384 ath9k_hw_rxena(ah);
Felix Fietkaub5c804752010-04-15 17:38:48 -04001385 }
Sujithbe0418a2008-11-18 09:05:55 +05301386 } while (1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001387
Rajkumar Manoharan29ab0b32011-08-13 10:28:10 +05301388 if (!(ah->imask & ATH9K_INT_RXEOL)) {
1389 ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
Felix Fietkau72d874c2011-10-08 20:06:19 +02001390 ath9k_hw_set_interrupts(ah);
Rajkumar Manoharan29ab0b32011-08-13 10:28:10 +05301391 }
1392
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001393 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001394}