blob: ee156e5431472dcf8e1428a78a6bb102bd51c8b4 [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,
133 enum ath9k_rx_qtype qtype, int size)
134{
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);
253
254 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
255 sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
256
257 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
258 sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
259
Felix Fietkaub5c804752010-04-15 17:38:48 -0400260 ath_opmode_init(sc);
261
Sujith Manoharan4cb54fa2012-06-04 16:27:52 +0530262 ath9k_hw_startpcureceive(sc->sc_ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
Felix Fietkaub5c804752010-04-15 17:38:48 -0400263}
264
265static void ath_edma_stop_recv(struct ath_softc *sc)
266{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400267 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
268 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400269}
270
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700271int ath_rx_init(struct ath_softc *sc, int nbufs)
272{
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -0700273 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700274 struct sk_buff *skb;
275 struct ath_buf *bf;
276 int error = 0;
277
Luis R. Rodriguez4bdd1e92010-10-26 15:27:24 -0700278 spin_lock_init(&sc->sc_pcu_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700279
Felix Fietkau0d955212011-01-26 18:23:27 +0100280 common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
281 sc->sc_ah->caps.rx_status_len;
282
Felix Fietkaub5c804752010-04-15 17:38:48 -0400283 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
284 return ath_rx_edma_init(sc, nbufs);
285 } else {
Joe Perchesd2182b62011-12-15 14:55:53 -0800286 ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
Joe Perches226afe62010-12-02 19:12:37 -0800287 common->cachelsz, common->rx_bufsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700288
Felix Fietkaub5c804752010-04-15 17:38:48 -0400289 /* Initialize rx descriptors */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700290
Felix Fietkaub5c804752010-04-15 17:38:48 -0400291 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
Vasanthakumar Thiagarajan4adfcde2010-04-15 17:39:33 -0400292 "rx", nbufs, 1, 0);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400293 if (error != 0) {
Joe Perches38002762010-12-02 19:12:36 -0800294 ath_err(common,
295 "failed to allocate rx descriptors: %d\n",
296 error);
Sujith797fe5cb2009-03-30 15:28:45 +0530297 goto err;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700298 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400299
300 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
301 skb = ath_rxbuf_alloc(common, common->rx_bufsize,
302 GFP_KERNEL);
303 if (skb == NULL) {
304 error = -ENOMEM;
305 goto err;
306 }
307
308 bf->bf_mpdu = skb;
309 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
310 common->rx_bufsize,
311 DMA_FROM_DEVICE);
312 if (unlikely(dma_mapping_error(sc->dev,
313 bf->bf_buf_addr))) {
314 dev_kfree_skb_any(skb);
315 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -0700316 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -0800317 ath_err(common,
318 "dma_mapping_error() on RX init\n");
Felix Fietkaub5c804752010-04-15 17:38:48 -0400319 error = -ENOMEM;
320 goto err;
321 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400322 }
323 sc->rx.rxlink = NULL;
Sujith797fe5cb2009-03-30 15:28:45 +0530324 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700325
Sujith797fe5cb2009-03-30 15:28:45 +0530326err:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700327 if (error)
328 ath_rx_cleanup(sc);
329
330 return error;
331}
332
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700333void ath_rx_cleanup(struct ath_softc *sc)
334{
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800335 struct ath_hw *ah = sc->sc_ah;
336 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700337 struct sk_buff *skb;
338 struct ath_buf *bf;
339
Felix Fietkaub5c804752010-04-15 17:38:48 -0400340 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
341 ath_rx_edma_cleanup(sc);
342 return;
343 } else {
344 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
345 skb = bf->bf_mpdu;
346 if (skb) {
347 dma_unmap_single(sc->dev, bf->bf_buf_addr,
348 common->rx_bufsize,
349 DMA_FROM_DEVICE);
350 dev_kfree_skb(skb);
Ben Greear6cf9e992010-10-14 12:45:30 -0700351 bf->bf_buf_addr = 0;
352 bf->bf_mpdu = NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400353 }
Luis R. Rodriguez051b9192009-03-23 18:25:01 -0400354 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400355 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700356}
357
358/*
359 * Calculate the receive filter according to the
360 * operating mode and state:
361 *
362 * o always accept unicast, broadcast, and multicast traffic
363 * o maintain current state of phy error reception (the hal
364 * may enable phy error frames for noise immunity work)
365 * o probe request frames are accepted only when operating in
366 * hostap, adhoc, or monitor modes
367 * o enable promiscuous mode according to the interface state
368 * o accept beacons:
369 * - when operating in adhoc mode so the 802.11 layer creates
370 * node table entries for peers,
371 * - when operating in station mode for collecting rssi data when
372 * the station is otherwise quiet, or
373 * - when operating as a repeater so we see repeater-sta beacons
374 * - when scanning
375 */
376
377u32 ath_calcrxfilter(struct ath_softc *sc)
378{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700379 u32 rfilt;
380
Felix Fietkauac066972011-10-08 15:49:57 +0200381 rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700382 | ATH9K_RX_FILTER_MCAST;
383
Jouni Malinen9c1d8e42010-10-13 17:29:31 +0300384 if (sc->rx.rxfilter & FIF_PROBE_REQ)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700385 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
386
Jouni Malinen217ba9d2009-03-10 10:55:50 +0200387 /*
388 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
389 * mode interface or when in monitor mode. AP mode does not need this
390 * since it receives all in-BSS frames anyway.
391 */
Felix Fietkau2e286942011-03-09 01:48:12 +0100392 if (sc->sc_ah->is_monitoring)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700393 rfilt |= ATH9K_RX_FILTER_PROM;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700394
Sujithd42c6b72009-02-04 08:10:22 +0530395 if (sc->rx.rxfilter & FIF_CONTROL)
396 rfilt |= ATH9K_RX_FILTER_CONTROL;
397
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530398 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
Ben Greearcfda6692010-09-14 12:00:22 -0700399 (sc->nvifs <= 1) &&
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530400 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
401 rfilt |= ATH9K_RX_FILTER_MYBEACON;
402 else
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700403 rfilt |= ATH9K_RX_FILTER_BEACON;
404
Felix Fietkau264bbec2011-04-07 19:24:23 +0200405 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
Senthil Balasubramanian66afad02009-09-18 15:06:07 +0530406 (sc->rx.rxfilter & FIF_PSPOLL))
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530407 rfilt |= ATH9K_RX_FILTER_PSPOLL;
Sujithbe0418a2008-11-18 09:05:55 +0530408
Sujith7ea310b2009-09-03 12:08:43 +0530409 if (conf_is_ht(&sc->hw->conf))
410 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
411
Felix Fietkau7545daf2011-01-24 19:23:16 +0100412 if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
Thomas Wagnera5494592012-09-25 21:32:55 +0530413 /* This is needed for older chips */
414 if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
Javier Cardona5eb6ba82009-08-20 19:12:07 -0700415 rfilt |= ATH9K_RX_FILTER_PROM;
Jouni Malinenb93bce22009-03-03 19:23:30 +0200416 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
417 }
418
Gabor Juhosb3d7aa42012-07-03 19:13:33 +0200419 if (AR_SREV_9550(sc->sc_ah))
420 rfilt |= ATH9K_RX_FILTER_4ADDRESS;
421
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700422 return rfilt;
Sujith7dcfdcd2008-08-11 14:03:13 +0530423
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700424}
425
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700426int ath_startrecv(struct ath_softc *sc)
427{
Sujithcbe61d82009-02-09 13:27:12 +0530428 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700429 struct ath_buf *bf, *tbf;
430
Felix Fietkaub5c804752010-04-15 17:38:48 -0400431 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
432 ath_edma_start_recv(sc);
433 return 0;
434 }
435
Sujithb77f4832008-12-07 21:44:03 +0530436 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700437 goto start_recv;
438
Sujithb77f4832008-12-07 21:44:03 +0530439 sc->rx.rxlink = NULL;
440 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700441 ath_rx_buf_link(sc, bf);
442 }
443
444 /* We could have deleted elements so the list may be empty now */
Sujithb77f4832008-12-07 21:44:03 +0530445 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700446 goto start_recv;
447
Sujithb77f4832008-12-07 21:44:03 +0530448 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700449 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
Sujithbe0418a2008-11-18 09:05:55 +0530450 ath9k_hw_rxena(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700451
452start_recv:
Sujithbe0418a2008-11-18 09:05:55 +0530453 ath_opmode_init(sc);
Sujith Manoharan4cb54fa2012-06-04 16:27:52 +0530454 ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
Sujithbe0418a2008-11-18 09:05:55 +0530455
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700456 return 0;
457}
458
Felix Fietkau4b883f02013-01-09 16:16:56 +0100459static void ath_flushrecv(struct ath_softc *sc)
460{
461 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
462 ath_rx_tasklet(sc, 1, true);
463 ath_rx_tasklet(sc, 1, false);
464}
465
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700466bool ath_stoprecv(struct ath_softc *sc)
467{
Sujithcbe61d82009-02-09 13:27:12 +0530468 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau5882da022011-04-08 20:13:18 +0200469 bool stopped, reset = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700470
Felix Fietkaud47844a2010-11-20 03:08:47 +0100471 ath9k_hw_abortpcurecv(ah);
Sujithbe0418a2008-11-18 09:05:55 +0530472 ath9k_hw_setrxfilter(ah, 0);
Felix Fietkau5882da022011-04-08 20:13:18 +0200473 stopped = ath9k_hw_stopdmarecv(ah, &reset);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400474
Felix Fietkau4b883f02013-01-09 16:16:56 +0100475 ath_flushrecv(sc);
476
Felix Fietkaub5c804752010-04-15 17:38:48 -0400477 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
478 ath_edma_stop_recv(sc);
479 else
480 sc->rx.rxlink = NULL;
Sujithbe0418a2008-11-18 09:05:55 +0530481
Rajkumar Manoharand5847472010-12-20 14:39:51 +0530482 if (!(ah->ah_flags & AH_UNPLUGGED) &&
483 unlikely(!stopped)) {
Ben Greeard7fd1b502010-12-06 13:13:07 -0800484 ath_err(ath9k_hw_common(sc->sc_ah),
485 "Could not stop RX, we could be "
486 "confusing the DMA engine when we start RX up\n");
487 ATH_DBG_WARN_ON_ONCE(!stopped);
488 }
Felix Fietkau2232d312011-04-15 00:41:43 +0200489 return stopped && !reset;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700490}
491
Jouni Malinencc659652009-05-14 21:28:48 +0300492static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
493{
494 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
495 struct ieee80211_mgmt *mgmt;
496 u8 *pos, *end, id, elen;
497 struct ieee80211_tim_ie *tim;
498
499 mgmt = (struct ieee80211_mgmt *)skb->data;
500 pos = mgmt->u.beacon.variable;
501 end = skb->data + skb->len;
502
503 while (pos + 2 < end) {
504 id = *pos++;
505 elen = *pos++;
506 if (pos + elen > end)
507 break;
508
509 if (id == WLAN_EID_TIM) {
510 if (elen < sizeof(*tim))
511 break;
512 tim = (struct ieee80211_tim_ie *) pos;
513 if (tim->dtim_count != 0)
514 break;
515 return tim->bitmap_ctrl & 0x01;
516 }
517
518 pos += elen;
519 }
520
521 return false;
522}
523
Jouni Malinencc659652009-05-14 21:28:48 +0300524static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
525{
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700526 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300527
528 if (skb->len < 24 + 8 + 2 + 2)
529 return;
530
Sujith1b04b932010-01-08 10:36:05 +0530531 sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
Gabor Juhos293dc5d2009-06-19 12:17:48 +0200532
Sujith1b04b932010-01-08 10:36:05 +0530533 if (sc->ps_flags & PS_BEACON_SYNC) {
534 sc->ps_flags &= ~PS_BEACON_SYNC;
Joe Perchesd2182b62011-12-15 14:55:53 -0800535 ath_dbg(common, PS,
Sujith Manoharan1a6404a2013-02-04 15:38:24 +0530536 "Reconfigure beacon timers based on synchronized timestamp\n");
Sujith Manoharanef4ad632012-07-17 17:15:56 +0530537 ath9k_set_beacon(sc);
Jouni Malinenccdfeab2009-05-20 21:59:08 +0300538 }
539
Jouni Malinencc659652009-05-14 21:28:48 +0300540 if (ath_beacon_dtim_pending_cab(skb)) {
541 /*
542 * Remain awake waiting for buffered broadcast/multicast
Gabor Juhos58f5fff2009-06-17 20:53:20 +0200543 * frames. If the last broadcast/multicast frame is not
544 * received properly, the next beacon frame will work as
545 * a backup trigger for returning into NETWORK SLEEP state,
546 * so we are waiting for it as well.
Jouni Malinencc659652009-05-14 21:28:48 +0300547 */
Joe Perchesd2182b62011-12-15 14:55:53 -0800548 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800549 "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
Sujith1b04b932010-01-08 10:36:05 +0530550 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
Jouni Malinencc659652009-05-14 21:28:48 +0300551 return;
552 }
553
Sujith1b04b932010-01-08 10:36:05 +0530554 if (sc->ps_flags & PS_WAIT_FOR_CAB) {
Jouni Malinencc659652009-05-14 21:28:48 +0300555 /*
556 * This can happen if a broadcast frame is dropped or the AP
557 * fails to send a frame indicating that all CAB frames have
558 * been delivered.
559 */
Sujith1b04b932010-01-08 10:36:05 +0530560 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
Joe Perchesd2182b62011-12-15 14:55:53 -0800561 ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
Jouni Malinencc659652009-05-14 21:28:48 +0300562 }
Jouni Malinencc659652009-05-14 21:28:48 +0300563}
564
Rajkumar Manoharanf73c6042011-09-26 22:16:56 +0530565static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
Jouni Malinencc659652009-05-14 21:28:48 +0300566{
567 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700568 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300569
570 hdr = (struct ieee80211_hdr *)skb->data;
571
572 /* Process Beacon and CAB receive in PS state */
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -0700573 if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530574 && mybeacon) {
Jouni Malinencc659652009-05-14 21:28:48 +0300575 ath_rx_ps_beacon(sc, skb);
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530576 } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
577 (ieee80211_is_data(hdr->frame_control) ||
578 ieee80211_is_action(hdr->frame_control)) &&
579 is_multicast_ether_addr(hdr->addr1) &&
580 !ieee80211_has_moredata(hdr->frame_control)) {
Jouni Malinencc659652009-05-14 21:28:48 +0300581 /*
582 * No more broadcast/multicast frames to be received at this
583 * point.
584 */
Senthil Balasubramanian3fac6df2010-09-16 15:12:35 -0400585 sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
Joe Perchesd2182b62011-12-15 14:55:53 -0800586 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800587 "All PS CAB frames received, back to sleep\n");
Sujith1b04b932010-01-08 10:36:05 +0530588 } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300589 !is_multicast_ether_addr(hdr->addr1) &&
590 !ieee80211_has_morefrags(hdr->frame_control)) {
Sujith1b04b932010-01-08 10:36:05 +0530591 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
Joe Perchesd2182b62011-12-15 14:55:53 -0800592 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800593 "Going back to sleep after having received PS-Poll data (0x%lx)\n",
Sujith1b04b932010-01-08 10:36:05 +0530594 sc->ps_flags & (PS_WAIT_FOR_BEACON |
595 PS_WAIT_FOR_CAB |
596 PS_WAIT_FOR_PSPOLL_DATA |
597 PS_WAIT_FOR_TX_ACK));
Jouni Malinencc659652009-05-14 21:28:48 +0300598 }
599}
600
Felix Fietkaub5c804752010-04-15 17:38:48 -0400601static bool ath_edma_get_buffers(struct ath_softc *sc,
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100602 enum ath9k_rx_qtype qtype,
603 struct ath_rx_status *rs,
604 struct ath_buf **dest)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700605{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400606 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
607 struct ath_hw *ah = sc->sc_ah;
608 struct ath_common *common = ath9k_hw_common(ah);
609 struct sk_buff *skb;
Sujithbe0418a2008-11-18 09:05:55 +0530610 struct ath_buf *bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400611 int ret;
612
613 skb = skb_peek(&rx_edma->rx_fifo);
614 if (!skb)
615 return false;
616
617 bf = SKB_CB_ATHBUF(skb);
618 BUG_ON(!bf);
619
Ming Leice9426d2010-05-15 18:25:40 +0800620 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400621 common->rx_bufsize, DMA_FROM_DEVICE);
622
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100623 ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
Ming Leice9426d2010-05-15 18:25:40 +0800624 if (ret == -EINPROGRESS) {
625 /*let device gain the buffer again*/
626 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
627 common->rx_bufsize, DMA_FROM_DEVICE);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400628 return false;
Ming Leice9426d2010-05-15 18:25:40 +0800629 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400630
631 __skb_unlink(skb, &rx_edma->rx_fifo);
632 if (ret == -EINVAL) {
633 /* corrupt descriptor, skip this one and the following one */
634 list_add_tail(&bf->list, &sc->rx.rxbuf);
635 ath_rx_edma_buf_link(sc, qtype);
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100636
Felix Fietkaub5c804752010-04-15 17:38:48 -0400637 skb = skb_peek(&rx_edma->rx_fifo);
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100638 if (skb) {
639 bf = SKB_CB_ATHBUF(skb);
640 BUG_ON(!bf);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400641
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100642 __skb_unlink(skb, &rx_edma->rx_fifo);
643 list_add_tail(&bf->list, &sc->rx.rxbuf);
644 ath_rx_edma_buf_link(sc, qtype);
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100645 }
Tom Hughes6bb51c72012-06-27 18:21:15 +0100646
647 bf = NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400648 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400649
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100650 *dest = bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400651 return true;
652}
653
654static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
655 struct ath_rx_status *rs,
656 enum ath9k_rx_qtype qtype)
657{
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100658 struct ath_buf *bf = NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400659
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100660 while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
661 if (!bf)
662 continue;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400663
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100664 return bf;
665 }
666 return NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400667}
668
669static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
670 struct ath_rx_status *rs)
671{
672 struct ath_hw *ah = sc->sc_ah;
673 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700674 struct ath_desc *ds;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400675 struct ath_buf *bf;
676 int ret;
677
678 if (list_empty(&sc->rx.rxbuf)) {
679 sc->rx.rxlink = NULL;
680 return NULL;
681 }
682
683 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
684 ds = bf->bf_desc;
685
686 /*
687 * Must provide the virtual address of the current
688 * descriptor, the physical address, and the virtual
689 * address of the next descriptor in the h/w chain.
690 * This allows the HAL to look ahead to see if the
691 * hardware is done with a descriptor by checking the
692 * done bit in the following descriptor and the address
693 * of the current descriptor the DMA engine is working
694 * on. All this is necessary because of our use of
695 * a self-linked list to avoid rx overruns.
696 */
Rajkumar Manoharan3de21112011-08-13 10:28:11 +0530697 ret = ath9k_hw_rxprocdesc(ah, ds, rs);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400698 if (ret == -EINPROGRESS) {
699 struct ath_rx_status trs;
700 struct ath_buf *tbf;
701 struct ath_desc *tds;
702
703 memset(&trs, 0, sizeof(trs));
704 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
705 sc->rx.rxlink = NULL;
706 return NULL;
707 }
708
709 tbf = list_entry(bf->list.next, struct ath_buf, list);
710
711 /*
712 * On some hardware the descriptor status words could
713 * get corrupted, including the done bit. Because of
714 * this, check if the next descriptor's done bit is
715 * set or not.
716 *
717 * If the next descriptor's done bit is set, the current
718 * descriptor has been corrupted. Force s/w to discard
719 * this descriptor and continue...
720 */
721
722 tds = tbf->bf_desc;
Rajkumar Manoharan3de21112011-08-13 10:28:11 +0530723 ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400724 if (ret == -EINPROGRESS)
725 return NULL;
726 }
727
Felix Fietkaua3dc48e2013-01-09 16:16:52 +0100728 list_del(&bf->list);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400729 if (!bf->bf_mpdu)
730 return bf;
731
732 /*
733 * Synchronize the DMA transfer with CPU before
734 * 1. accessing the frame
735 * 2. requeueing the same buffer to h/w
736 */
Ming Leice9426d2010-05-15 18:25:40 +0800737 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400738 common->rx_bufsize,
739 DMA_FROM_DEVICE);
740
741 return bf;
742}
743
Sujithd4357002010-05-20 15:34:38 +0530744/* Assumes you've already done the endian to CPU conversion */
745static bool ath9k_rx_accept(struct ath_common *common,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700746 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530747 struct ieee80211_rx_status *rxs,
748 struct ath_rx_status *rx_stats,
749 bool *decrypt_error)
750{
Felix Fietkauec205992011-10-08 22:02:59 +0200751 struct ath_softc *sc = (struct ath_softc *) common->priv;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800752 bool is_mc, is_valid_tkip, strip_mic, mic_error;
Sujithd4357002010-05-20 15:34:38 +0530753 struct ath_hw *ah = common->ah;
Sujithd4357002010-05-20 15:34:38 +0530754 __le16 fc;
Vasanthakumar Thiagarajanb7b1b512010-05-20 14:34:48 -0700755 u8 rx_status_len = ah->caps.rx_status_len;
Sujithd4357002010-05-20 15:34:38 +0530756
Sujithd4357002010-05-20 15:34:38 +0530757 fc = hdr->frame_control;
758
Felix Fietkau66760ea2011-07-13 23:35:05 +0800759 is_mc = !!is_multicast_ether_addr(hdr->addr1);
760 is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
761 test_bit(rx_stats->rs_keyix, common->tkip_keymap);
Bill Jordan152e5852011-08-19 11:10:22 -0400762 strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
Michael Liang2a5783b2012-04-20 17:11:57 +0800763 ieee80211_has_protected(fc) &&
Bill Jordan152e5852011-08-19 11:10:22 -0400764 !(rx_stats->rs_status &
Felix Fietkau846d9362011-10-08 22:02:58 +0200765 (ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
766 ATH9K_RXERR_KEYMISS));
Felix Fietkau66760ea2011-07-13 23:35:05 +0800767
Felix Fietkauf88373f2012-02-05 21:15:17 +0100768 /*
769 * Key miss events are only relevant for pairwise keys where the
770 * descriptor does contain a valid key index. This has been observed
771 * mostly with CCMP encryption.
772 */
Felix Fietkaubed3d9c2012-06-23 19:23:31 +0200773 if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID ||
774 !test_bit(rx_stats->rs_keyix, common->ccmp_keymap))
Felix Fietkauf88373f2012-02-05 21:15:17 +0100775 rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
776
Ben Greear15072182012-04-03 09:18:59 -0700777 if (!rx_stats->rs_datalen) {
778 RX_STAT_INC(rx_len_err);
Sujithd4357002010-05-20 15:34:38 +0530779 return false;
Ben Greear15072182012-04-03 09:18:59 -0700780 }
781
Sujithd4357002010-05-20 15:34:38 +0530782 /*
783 * rs_status follows rs_datalen so if rs_datalen is too large
784 * we can take a hint that hardware corrupted it, so ignore
785 * those frames.
786 */
Ben Greear15072182012-04-03 09:18:59 -0700787 if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len)) {
788 RX_STAT_INC(rx_len_err);
Sujithd4357002010-05-20 15:34:38 +0530789 return false;
Ben Greear15072182012-04-03 09:18:59 -0700790 }
Sujithd4357002010-05-20 15:34:38 +0530791
Felix Fietkau0d955212011-01-26 18:23:27 +0100792 /* Only use error bits from the last fragment */
Sujithd4357002010-05-20 15:34:38 +0530793 if (rx_stats->rs_more)
Felix Fietkau0d955212011-01-26 18:23:27 +0100794 return true;
Sujithd4357002010-05-20 15:34:38 +0530795
Felix Fietkau66760ea2011-07-13 23:35:05 +0800796 mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
797 !ieee80211_has_morefrags(fc) &&
798 !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
799 (rx_stats->rs_status & ATH9K_RXERR_MIC);
800
Sujithd4357002010-05-20 15:34:38 +0530801 /*
802 * The rx_stats->rs_status will not be set until the end of the
803 * chained descriptors so it can be ignored if rs_more is set. The
804 * rs_more will be false at the last element of the chained
805 * descriptors.
806 */
807 if (rx_stats->rs_status != 0) {
Felix Fietkau846d9362011-10-08 22:02:58 +0200808 u8 status_mask;
809
Felix Fietkau66760ea2011-07-13 23:35:05 +0800810 if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
Sujithd4357002010-05-20 15:34:38 +0530811 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800812 mic_error = false;
813 }
Sujithd4357002010-05-20 15:34:38 +0530814 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
815 return false;
816
Felix Fietkau846d9362011-10-08 22:02:58 +0200817 if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
818 (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
Sujithd4357002010-05-20 15:34:38 +0530819 *decrypt_error = true;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800820 mic_error = false;
Sujithd4357002010-05-20 15:34:38 +0530821 }
Felix Fietkau66760ea2011-07-13 23:35:05 +0800822
Sujithd4357002010-05-20 15:34:38 +0530823 /*
824 * Reject error frames with the exception of
825 * decryption and MIC failures. For monitor mode,
826 * we also ignore the CRC error.
827 */
Felix Fietkau846d9362011-10-08 22:02:58 +0200828 status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
829 ATH9K_RXERR_KEYMISS;
830
Felix Fietkauec205992011-10-08 22:02:59 +0200831 if (ah->is_monitoring && (sc->rx.rxfilter & FIF_FCSFAIL))
Felix Fietkau846d9362011-10-08 22:02:58 +0200832 status_mask |= ATH9K_RXERR_CRC;
833
834 if (rx_stats->rs_status & ~status_mask)
835 return false;
Sujithd4357002010-05-20 15:34:38 +0530836 }
Felix Fietkau66760ea2011-07-13 23:35:05 +0800837
838 /*
839 * For unicast frames the MIC error bit can have false positives,
840 * so all MIC error reports need to be validated in software.
841 * False negatives are not common, so skip software verification
842 * if the hardware considers the MIC valid.
843 */
844 if (strip_mic)
845 rxs->flag |= RX_FLAG_MMIC_STRIPPED;
846 else if (is_mc && mic_error)
847 rxs->flag |= RX_FLAG_MMIC_ERROR;
848
Sujithd4357002010-05-20 15:34:38 +0530849 return true;
850}
851
852static int ath9k_process_rate(struct ath_common *common,
853 struct ieee80211_hw *hw,
854 struct ath_rx_status *rx_stats,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700855 struct ieee80211_rx_status *rxs)
Sujithd4357002010-05-20 15:34:38 +0530856{
857 struct ieee80211_supported_band *sband;
858 enum ieee80211_band band;
859 unsigned int i = 0;
Ben Greear990e08a2012-04-17 15:19:03 -0700860 struct ath_softc __maybe_unused *sc = common->priv;
Sujithd4357002010-05-20 15:34:38 +0530861
862 band = hw->conf.channel->band;
863 sband = hw->wiphy->bands[band];
864
865 if (rx_stats->rs_rate & 0x80) {
866 /* HT rate */
867 rxs->flag |= RX_FLAG_HT;
868 if (rx_stats->rs_flags & ATH9K_RX_2040)
869 rxs->flag |= RX_FLAG_40MHZ;
870 if (rx_stats->rs_flags & ATH9K_RX_GI)
871 rxs->flag |= RX_FLAG_SHORT_GI;
872 rxs->rate_idx = rx_stats->rs_rate & 0x7f;
873 return 0;
874 }
875
876 for (i = 0; i < sband->n_bitrates; i++) {
877 if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
878 rxs->rate_idx = i;
879 return 0;
880 }
881 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
882 rxs->flag |= RX_FLAG_SHORTPRE;
883 rxs->rate_idx = i;
884 return 0;
885 }
886 }
887
888 /*
889 * No valid hardware bitrate found -- we should not get here
890 * because hardware has already validated this frame as OK.
891 */
Joe Perchesd2182b62011-12-15 14:55:53 -0800892 ath_dbg(common, ANY,
Joe Perches226afe62010-12-02 19:12:37 -0800893 "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
894 rx_stats->rs_rate);
Ben Greear15072182012-04-03 09:18:59 -0700895 RX_STAT_INC(rx_rate_err);
Sujithd4357002010-05-20 15:34:38 +0530896 return -EINVAL;
897}
898
899static void ath9k_process_rssi(struct ath_common *common,
900 struct ieee80211_hw *hw,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700901 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530902 struct ath_rx_status *rx_stats)
903{
Felix Fietkau9ac58612011-01-24 19:23:18 +0100904 struct ath_softc *sc = hw->priv;
Sujithd4357002010-05-20 15:34:38 +0530905 struct ath_hw *ah = common->ah;
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200906 int last_rssi;
Felix Fietkau2ef16752012-03-03 15:17:06 +0100907 int rssi = rx_stats->rs_rssi;
Sujithd4357002010-05-20 15:34:38 +0530908
Rajkumar Manoharancf3af742011-08-27 16:17:47 +0530909 if (!rx_stats->is_mybeacon ||
910 ((ah->opmode != NL80211_IFTYPE_STATION) &&
911 (ah->opmode != NL80211_IFTYPE_ADHOC)))
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200912 return;
913
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200914 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
Felix Fietkau9ac58612011-01-24 19:23:18 +0100915 ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
Ben Greear686b9cb2010-09-23 09:44:36 -0700916
Felix Fietkau9ac58612011-01-24 19:23:18 +0100917 last_rssi = sc->last_rssi;
Sujithd4357002010-05-20 15:34:38 +0530918 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
Felix Fietkau2ef16752012-03-03 15:17:06 +0100919 rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
920 if (rssi < 0)
921 rssi = 0;
Sujithd4357002010-05-20 15:34:38 +0530922
923 /* Update Beacon RSSI, this is used by ANI. */
Felix Fietkau2ef16752012-03-03 15:17:06 +0100924 ah->stats.avgbrssi = rssi;
Sujithd4357002010-05-20 15:34:38 +0530925}
926
927/*
928 * For Decrypt or Demic errors, we only mark packet status here and always push
929 * up the frame up to let mac80211 handle the actual error case, be it no
930 * decryption key or real decryption error. This let us keep statistics there.
931 */
932static int ath9k_rx_skb_preprocess(struct ath_common *common,
933 struct ieee80211_hw *hw,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700934 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530935 struct ath_rx_status *rx_stats,
936 struct ieee80211_rx_status *rx_status,
937 bool *decrypt_error)
938{
Felix Fietkauf749b942011-07-28 14:08:57 +0200939 struct ath_hw *ah = common->ah;
940
Sujithd4357002010-05-20 15:34:38 +0530941 /*
942 * everything but the rate is checked here, the rate check is done
943 * separately to avoid doing two lookups for a rate for each frame.
944 */
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700945 if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
Sujithd4357002010-05-20 15:34:38 +0530946 return -EINVAL;
947
Felix Fietkau0d955212011-01-26 18:23:27 +0100948 /* Only use status info from the last fragment */
949 if (rx_stats->rs_more)
950 return 0;
951
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700952 ath9k_process_rssi(common, hw, hdr, rx_stats);
Sujithd4357002010-05-20 15:34:38 +0530953
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700954 if (ath9k_process_rate(common, hw, rx_stats, rx_status))
Sujithd4357002010-05-20 15:34:38 +0530955 return -EINVAL;
956
Sujithd4357002010-05-20 15:34:38 +0530957 rx_status->band = hw->conf.channel->band;
958 rx_status->freq = hw->conf.channel->center_freq;
Felix Fietkauf749b942011-07-28 14:08:57 +0200959 rx_status->signal = ah->noise + rx_stats->rs_rssi;
Sujithd4357002010-05-20 15:34:38 +0530960 rx_status->antenna = rx_stats->rs_antenna;
Thomas Pedersen96d21372012-12-10 14:48:01 -0800961 rx_status->flag |= RX_FLAG_MACTIME_END;
Felix Fietkau2ef16752012-03-03 15:17:06 +0100962 if (rx_stats->rs_moreaggr)
963 rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
Sujithd4357002010-05-20 15:34:38 +0530964
965 return 0;
966}
967
968static void ath9k_rx_skb_postprocess(struct ath_common *common,
969 struct sk_buff *skb,
970 struct ath_rx_status *rx_stats,
971 struct ieee80211_rx_status *rxs,
972 bool decrypt_error)
973{
974 struct ath_hw *ah = common->ah;
975 struct ieee80211_hdr *hdr;
976 int hdrlen, padpos, padsize;
977 u8 keyix;
978 __le16 fc;
979
980 /* see if any padding is done by the hw and remove it */
981 hdr = (struct ieee80211_hdr *) skb->data;
982 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
983 fc = hdr->frame_control;
984 padpos = ath9k_cmn_padpos(hdr->frame_control);
985
986 /* The MAC header is padded to have 32-bit boundary if the
987 * packet payload is non-zero. The general calculation for
988 * padsize would take into account odd header lengths:
989 * padsize = (4 - padpos % 4) % 4; However, since only
990 * even-length headers are used, padding can only be 0 or 2
991 * bytes and we can optimize this a bit. In addition, we must
992 * not try to remove padding from short control frames that do
993 * not have payload. */
994 padsize = padpos & 3;
995 if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
996 memmove(skb->data + padsize, skb->data, padpos);
997 skb_pull(skb, padsize);
998 }
999
1000 keyix = rx_stats->rs_keyix;
1001
1002 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1003 ieee80211_has_protected(fc)) {
1004 rxs->flag |= RX_FLAG_DECRYPTED;
1005 } else if (ieee80211_has_protected(fc)
1006 && !decrypt_error && skb->len >= hdrlen + 4) {
1007 keyix = skb->data[hdrlen + 3] >> 6;
1008
1009 if (test_bit(keyix, common->keymap))
1010 rxs->flag |= RX_FLAG_DECRYPTED;
1011 }
1012 if (ah->sw_mgmt_crypto &&
1013 (rxs->flag & RX_FLAG_DECRYPTED) &&
1014 ieee80211_is_mgmt(fc))
1015 /* Use software decrypt for management frames. */
1016 rxs->flag &= ~RX_FLAG_DECRYPTED;
1017}
Felix Fietkaub5c804752010-04-15 17:38:48 -04001018
Sven Eckelmannab2e2fc2013-01-31 10:26:46 +01001019#ifdef CONFIG_ATH9K_DEBUGFS
Simon Wunderliche93d0832013-01-08 14:48:58 +01001020static s8 fix_rssi_inv_only(u8 rssi_val)
1021{
1022 if (rssi_val == 128)
1023 rssi_val = 0;
1024 return (s8) rssi_val;
1025}
Sven Eckelmannab2e2fc2013-01-31 10:26:46 +01001026#endif
Simon Wunderliche93d0832013-01-08 14:48:58 +01001027
Simon Wunderlich9b99e662013-01-23 17:38:05 +01001028/* returns 1 if this was a spectral frame, even if not handled. */
1029static int ath_process_fft(struct ath_softc *sc, struct ieee80211_hdr *hdr,
1030 struct ath_rx_status *rs, u64 tsf)
Simon Wunderliche93d0832013-01-08 14:48:58 +01001031{
Sven Eckelmannbd2ffe12013-01-31 10:26:45 +01001032#ifdef CONFIG_ATH9K_DEBUGFS
Simon Wunderliche93d0832013-01-08 14:48:58 +01001033 struct ath_hw *ah = sc->sc_ah;
1034 u8 bins[SPECTRAL_HT20_NUM_BINS];
1035 u8 *vdata = (u8 *)hdr;
1036 struct fft_sample_ht20 fft_sample;
1037 struct ath_radar_info *radar_info;
1038 struct ath_ht20_mag_info *mag_info;
1039 int len = rs->rs_datalen;
Sven Eckelmann4ab0b0a2013-01-23 20:12:39 +01001040 int dc_pos;
Sven Eckelmann12824372013-01-31 10:26:48 +01001041 u16 length, max_magnitude;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001042
1043 /* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
1044 * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
1045 * yet, but this is supposed to be possible as well.
1046 */
1047 if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
1048 rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
1049 rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
Simon Wunderlich9b99e662013-01-23 17:38:05 +01001050 return 0;
1051
1052 /* check if spectral scan bit is set. This does not have to be checked
1053 * if received through a SPECTRAL phy error, but shouldn't hurt.
1054 */
1055 radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
1056 if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
1057 return 0;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001058
1059 /* Variation in the data length is possible and will be fixed later.
1060 * Note that we only support HT20 for now.
1061 *
1062 * TODO: add HT20_40 support as well.
1063 */
1064 if ((len > SPECTRAL_HT20_TOTAL_DATA_LEN + 2) ||
1065 (len < SPECTRAL_HT20_TOTAL_DATA_LEN - 1))
Simon Wunderlich9b99e662013-01-23 17:38:05 +01001066 return 1;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001067
1068 fft_sample.tlv.type = ATH_FFT_SAMPLE_HT20;
Sven Eckelmann12824372013-01-31 10:26:48 +01001069 length = sizeof(fft_sample) - sizeof(fft_sample.tlv);
1070 fft_sample.tlv.length = __cpu_to_be16(length);
Simon Wunderliche93d0832013-01-08 14:48:58 +01001071
Sven Eckelmann4ab0b0a2013-01-23 20:12:39 +01001072 fft_sample.freq = __cpu_to_be16(ah->curchan->chan->center_freq);
Simon Wunderliche93d0832013-01-08 14:48:58 +01001073 fft_sample.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl0);
1074 fft_sample.noise = ah->noise;
1075
1076 switch (len - SPECTRAL_HT20_TOTAL_DATA_LEN) {
1077 case 0:
1078 /* length correct, nothing to do. */
1079 memcpy(bins, vdata, SPECTRAL_HT20_NUM_BINS);
1080 break;
1081 case -1:
1082 /* first byte missing, duplicate it. */
1083 memcpy(&bins[1], vdata, SPECTRAL_HT20_NUM_BINS - 1);
1084 bins[0] = vdata[0];
1085 break;
1086 case 2:
1087 /* MAC added 2 extra bytes at bin 30 and 32, remove them. */
1088 memcpy(bins, vdata, 30);
1089 bins[30] = vdata[31];
1090 memcpy(&bins[31], &vdata[33], SPECTRAL_HT20_NUM_BINS - 31);
1091 break;
1092 case 1:
1093 /* MAC added 2 extra bytes AND first byte is missing. */
1094 bins[0] = vdata[0];
1095 memcpy(&bins[0], vdata, 30);
1096 bins[31] = vdata[31];
1097 memcpy(&bins[32], &vdata[33], SPECTRAL_HT20_NUM_BINS - 32);
1098 break;
1099 default:
Simon Wunderlich9b99e662013-01-23 17:38:05 +01001100 return 1;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001101 }
1102
1103 /* DC value (value in the middle) is the blind spot of the spectral
1104 * sample and invalid, interpolate it.
1105 */
1106 dc_pos = SPECTRAL_HT20_NUM_BINS / 2;
1107 bins[dc_pos] = (bins[dc_pos + 1] + bins[dc_pos - 1]) / 2;
1108
1109 /* mag data is at the end of the frame, in front of radar_info */
1110 mag_info = ((struct ath_ht20_mag_info *)radar_info) - 1;
1111
Sven Eckelmann4ab0b0a2013-01-23 20:12:39 +01001112 /* copy raw bins without scaling them */
1113 memcpy(fft_sample.data, bins, SPECTRAL_HT20_NUM_BINS);
1114 fft_sample.max_exp = mag_info->max_exp & 0xf;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001115
Sven Eckelmann12824372013-01-31 10:26:48 +01001116 max_magnitude = spectral_max_magnitude(mag_info->all_bins);
1117 fft_sample.max_magnitude = __cpu_to_be16(max_magnitude);
Simon Wunderliche93d0832013-01-08 14:48:58 +01001118 fft_sample.max_index = spectral_max_index(mag_info->all_bins);
1119 fft_sample.bitmap_weight = spectral_bitmap_weight(mag_info->all_bins);
Sven Eckelmann4ab0b0a2013-01-23 20:12:39 +01001120 fft_sample.tsf = __cpu_to_be64(tsf);
Simon Wunderliche93d0832013-01-08 14:48:58 +01001121
1122 ath_debug_send_fft_sample(sc, &fft_sample.tlv);
Simon Wunderlich9b99e662013-01-23 17:38:05 +01001123 return 1;
1124#else
1125 return 0;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001126#endif
1127}
1128
Christian Lamparter21fbbca2013-01-30 23:37:41 +01001129static void ath9k_apply_ampdu_details(struct ath_softc *sc,
1130 struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
1131{
1132 if (rs->rs_isaggr) {
1133 rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
1134
1135 rxs->ampdu_reference = sc->rx.ampdu_ref;
1136
1137 if (!rs->rs_moreaggr) {
1138 rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
1139 sc->rx.ampdu_ref++;
1140 }
1141
1142 if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
1143 rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
1144 }
1145}
1146
Felix Fietkaub5c804752010-04-15 17:38:48 -04001147int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1148{
1149 struct ath_buf *bf;
Felix Fietkau0d955212011-01-26 18:23:27 +01001150 struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001151 struct ieee80211_rx_status *rxs;
Sujithcbe61d82009-02-09 13:27:12 +05301152 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -07001153 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau7545daf2011-01-24 19:23:16 +01001154 struct ieee80211_hw *hw = sc->hw;
Sujithbe0418a2008-11-18 09:05:55 +05301155 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc9b14172009-11-04 16:47:22 -08001156 int retval;
Felix Fietkau29bffa92010-03-29 20:14:23 -07001157 struct ath_rx_status rs;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001158 enum ath9k_rx_qtype qtype;
1159 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1160 int dma_type;
Vasanthakumar Thiagarajan5c6dd922010-05-20 14:34:47 -07001161 u8 rx_status_len = ah->caps.rx_status_len;
Felix Fietkaua6d20552010-06-12 00:33:54 -04001162 u64 tsf = 0;
1163 u32 tsf_lower = 0;
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001164 unsigned long flags;
Sujithbe0418a2008-11-18 09:05:55 +05301165
Felix Fietkaub5c804752010-04-15 17:38:48 -04001166 if (edma)
Felix Fietkaub5c804752010-04-15 17:38:48 -04001167 dma_type = DMA_BIDIRECTIONAL;
Ming Lei56824222010-05-14 21:15:38 +08001168 else
1169 dma_type = DMA_FROM_DEVICE;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001170
1171 qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001172
Felix Fietkaua6d20552010-06-12 00:33:54 -04001173 tsf = ath9k_hw_gettsf64(ah);
1174 tsf_lower = tsf & 0xffffffff;
1175
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001176 do {
Lorenzo Bianconie1352fd2012-08-10 11:00:24 +02001177 bool decrypt_error = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001178
Felix Fietkau29bffa92010-03-29 20:14:23 -07001179 memset(&rs, 0, sizeof(rs));
Felix Fietkaub5c804752010-04-15 17:38:48 -04001180 if (edma)
1181 bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1182 else
1183 bf = ath_get_next_rx_buf(sc, &rs);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001184
Felix Fietkaub5c804752010-04-15 17:38:48 -04001185 if (!bf)
1186 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001187
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001188 skb = bf->bf_mpdu;
Sujithbe0418a2008-11-18 09:05:55 +05301189 if (!skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001190 continue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001191
Felix Fietkau0d955212011-01-26 18:23:27 +01001192 /*
1193 * Take frame header from the first fragment and RX status from
1194 * the last one.
1195 */
1196 if (sc->rx.frag)
1197 hdr_skb = sc->rx.frag;
1198 else
1199 hdr_skb = skb;
1200
1201 hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
1202 rxs = IEEE80211_SKB_RXCB(hdr_skb);
Ben Greear15072182012-04-03 09:18:59 -07001203 if (ieee80211_is_beacon(hdr->frame_control)) {
1204 RX_STAT_INC(rx_beacons);
1205 if (!is_zero_ether_addr(common->curbssid) &&
Joe Perches2e42e472012-05-09 17:17:46 +00001206 ether_addr_equal(hdr->addr3, common->curbssid))
Ben Greear15072182012-04-03 09:18:59 -07001207 rs.is_mybeacon = true;
1208 else
1209 rs.is_mybeacon = false;
1210 }
Rajkumar Manoharancf3af742011-08-27 16:17:47 +05301211 else
1212 rs.is_mybeacon = false;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001213
Mohammed Shafi Shajakhanbe41b052012-10-08 21:30:51 +05301214 if (ieee80211_is_data_present(hdr->frame_control) &&
1215 !ieee80211_is_qos_nullfunc(hdr->frame_control))
1216 sc->rx.num_pkts++;
1217
Felix Fietkau29bffa92010-03-29 20:14:23 -07001218 ath_debug_stat_rx(sc, &rs);
Sujith1395d3f2010-01-08 10:36:11 +05301219
Ashok Nagarajanffb1c562012-03-09 18:57:39 -08001220 memset(rxs, 0, sizeof(struct ieee80211_rx_status));
1221
Felix Fietkaua6d20552010-06-12 00:33:54 -04001222 rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1223 if (rs.rs_tstamp > tsf_lower &&
1224 unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1225 rxs->mactime -= 0x100000000ULL;
1226
1227 if (rs.rs_tstamp < tsf_lower &&
1228 unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1229 rxs->mactime += 0x100000000ULL;
1230
Simon Wunderlich9b99e662013-01-23 17:38:05 +01001231 if (rs.rs_status & ATH9K_RXERR_PHY) {
1232 if (ath_process_fft(sc, hdr, &rs, rxs->mactime)) {
1233 RX_STAT_INC(rx_spectral);
1234 goto requeue_drop_frag;
1235 }
1236 }
Simon Wunderliche93d0832013-01-08 14:48:58 +01001237
Zefir Kurtisi83c76572011-11-16 11:09:44 +01001238 retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
1239 rxs, &decrypt_error);
1240 if (retval)
1241 goto requeue_drop_frag;
1242
Rajkumar Manoharan01e18912012-03-15 05:34:27 +05301243 if (rs.is_mybeacon) {
1244 sc->hw_busy_count = 0;
1245 ath_start_rx_poll(sc, 3);
1246 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001247 /* Ensure we always have an skb to requeue once we are done
1248 * processing the current buffer's skb */
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001249 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001250
1251 /* If there is no memory we ignore the current RX'd frame,
1252 * tell hardware it can give us a new frame using the old
Sujithb77f4832008-12-07 21:44:03 +05301253 * skb and put it at the tail of the sc->rx.rxbuf list for
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001254 * processing. */
Ben Greear15072182012-04-03 09:18:59 -07001255 if (!requeue_skb) {
1256 RX_STAT_INC(rx_oom_err);
Felix Fietkau0d955212011-01-26 18:23:27 +01001257 goto requeue_drop_frag;
Ben Greear15072182012-04-03 09:18:59 -07001258 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001259
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301260 /* Unmap the frame */
Gabor Juhos7da3c552009-01-14 20:17:03 +01001261 dma_unmap_single(sc->dev, bf->bf_buf_addr,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001262 common->rx_bufsize,
Felix Fietkaub5c804752010-04-15 17:38:48 -04001263 dma_type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001264
Felix Fietkaub5c804752010-04-15 17:38:48 -04001265 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1266 if (ah->caps.rx_status_len)
1267 skb_pull(skb, ah->caps.rx_status_len);
Sujithbe0418a2008-11-18 09:05:55 +05301268
Felix Fietkau0d955212011-01-26 18:23:27 +01001269 if (!rs.rs_more)
1270 ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1271 rxs, decrypt_error);
Sujithbe0418a2008-11-18 09:05:55 +05301272
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001273 /* We will now give hardware our shiny new allocated skb */
1274 bf->bf_mpdu = requeue_skb;
Gabor Juhos7da3c552009-01-14 20:17:03 +01001275 bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001276 common->rx_bufsize,
Felix Fietkaub5c804752010-04-15 17:38:48 -04001277 dma_type);
Gabor Juhos7da3c552009-01-14 20:17:03 +01001278 if (unlikely(dma_mapping_error(sc->dev,
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001279 bf->bf_buf_addr))) {
1280 dev_kfree_skb_any(requeue_skb);
1281 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -07001282 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -08001283 ath_err(common, "dma_mapping_error() on RX\n");
Felix Fietkau7545daf2011-01-24 19:23:16 +01001284 ieee80211_rx(hw, skb);
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001285 break;
1286 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001287
Felix Fietkau0d955212011-01-26 18:23:27 +01001288 if (rs.rs_more) {
Ben Greear15072182012-04-03 09:18:59 -07001289 RX_STAT_INC(rx_frags);
Felix Fietkau0d955212011-01-26 18:23:27 +01001290 /*
1291 * rs_more indicates chained descriptors which can be
1292 * used to link buffers together for a sort of
1293 * scatter-gather operation.
1294 */
1295 if (sc->rx.frag) {
1296 /* too many fragments - cannot handle frame */
1297 dev_kfree_skb_any(sc->rx.frag);
1298 dev_kfree_skb_any(skb);
Ben Greear15072182012-04-03 09:18:59 -07001299 RX_STAT_INC(rx_too_many_frags_err);
Felix Fietkau0d955212011-01-26 18:23:27 +01001300 skb = NULL;
1301 }
1302 sc->rx.frag = skb;
1303 goto requeue;
1304 }
1305
1306 if (sc->rx.frag) {
1307 int space = skb->len - skb_tailroom(hdr_skb);
1308
Felix Fietkau0d955212011-01-26 18:23:27 +01001309 if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
1310 dev_kfree_skb(skb);
Ben Greear15072182012-04-03 09:18:59 -07001311 RX_STAT_INC(rx_oom_err);
Felix Fietkau0d955212011-01-26 18:23:27 +01001312 goto requeue_drop_frag;
1313 }
1314
Eric Dumazetb5447ff2012-03-15 13:43:29 -07001315 sc->rx.frag = NULL;
1316
Felix Fietkau0d955212011-01-26 18:23:27 +01001317 skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
1318 skb->len);
1319 dev_kfree_skb_any(skb);
1320 skb = hdr_skb;
1321 }
1322
Mohammed Shafi Shajakhaneb840a82011-11-29 20:30:35 +05301323
1324 if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) {
1325
1326 /*
1327 * change the default rx antenna if rx diversity
1328 * chooses the other antenna 3 times in a row.
1329 */
1330 if (sc->rx.defant != rs.rs_antenna) {
1331 if (++sc->rx.rxotherant >= 3)
1332 ath_setdefantenna(sc, rs.rs_antenna);
1333 } else {
1334 sc->rx.rxotherant = 0;
1335 }
1336
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001337 }
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301338
Felix Fietkau66760ea2011-07-13 23:35:05 +08001339 if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
1340 skb_trim(skb, skb->len - 8);
1341
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001342 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Mohammed Shafi Shajakhanaaef24b2010-12-07 20:40:58 +05301343 if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
Rajkumar Manoharanf73c6042011-09-26 22:16:56 +05301344 PS_WAIT_FOR_CAB |
1345 PS_WAIT_FOR_PSPOLL_DATA)) ||
1346 ath9k_check_auto_sleep(sc))
1347 ath_rx_ps(sc, skb, rs.is_mybeacon);
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001348 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Jouni Malinencc659652009-05-14 21:28:48 +03001349
Felix Fietkau43c35282011-09-03 01:40:27 +02001350 if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx == 3)
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001351 ath_ant_comb_scan(sc, &rs);
1352
Christian Lamparter21fbbca2013-01-30 23:37:41 +01001353 ath9k_apply_ampdu_details(sc, &rs, rxs);
1354
Felix Fietkau7545daf2011-01-24 19:23:16 +01001355 ieee80211_rx(hw, skb);
Jouni Malinencc659652009-05-14 21:28:48 +03001356
Felix Fietkau0d955212011-01-26 18:23:27 +01001357requeue_drop_frag:
1358 if (sc->rx.frag) {
1359 dev_kfree_skb_any(sc->rx.frag);
1360 sc->rx.frag = NULL;
1361 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001362requeue:
Felix Fietkaua3dc48e2013-01-09 16:16:52 +01001363 list_add_tail(&bf->list, &sc->rx.rxbuf);
1364 if (flush)
1365 continue;
1366
Felix Fietkaub5c804752010-04-15 17:38:48 -04001367 if (edma) {
Felix Fietkaub5c804752010-04-15 17:38:48 -04001368 ath_rx_edma_buf_link(sc, qtype);
1369 } else {
Felix Fietkaub5c804752010-04-15 17:38:48 -04001370 ath_rx_buf_link(sc, bf);
Felix Fietkaua3dc48e2013-01-09 16:16:52 +01001371 ath9k_hw_rxena(ah);
Felix Fietkaub5c804752010-04-15 17:38:48 -04001372 }
Sujithbe0418a2008-11-18 09:05:55 +05301373 } while (1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001374
Rajkumar Manoharan29ab0b32011-08-13 10:28:10 +05301375 if (!(ah->imask & ATH9K_INT_RXEOL)) {
1376 ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
Felix Fietkau72d874c2011-10-08 20:06:19 +02001377 ath9k_hw_set_interrupts(ah);
Rajkumar Manoharan29ab0b32011-08-13 10:28:10 +05301378 }
1379
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001380 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001381}