blob: ab9e3a8410bc2065fff9cb58996645fbf67be4fc [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
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070045 ds = bf->bf_desc;
Sujithbe0418a2008-11-18 09:05:55 +053046 ds->ds_link = 0; /* link to null */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070047 ds->ds_data = bf->bf_buf_addr;
48
Sujithbe0418a2008-11-18 09:05:55 +053049 /* virtual addr of the beginning of the buffer. */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070050 skb = bf->bf_mpdu;
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -070051 BUG_ON(skb == NULL);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070052 ds->ds_vdata = skb->data;
53
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080054 /*
55 * setup rx descriptors. The rx_bufsize here tells the hardware
Luis R. Rodriguezb4b6cda2008-11-20 17:15:13 -080056 * how much data it can DMA to us and that we are prepared
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080057 * to process
58 */
Sujithb77f4832008-12-07 21:44:03 +053059 ath9k_hw_setuprxdesc(ah, ds,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080060 common->rx_bufsize,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070061 0);
62
Sujithb77f4832008-12-07 21:44:03 +053063 if (sc->rx.rxlink == NULL)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070064 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
65 else
Sujithb77f4832008-12-07 21:44:03 +053066 *sc->rx.rxlink = bf->bf_daddr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070067
Sujithb77f4832008-12-07 21:44:03 +053068 sc->rx.rxlink = &ds->ds_link;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070069}
70
Felix Fietkaue96542e2013-08-10 15:59:15 +020071static void ath_rx_buf_relink(struct ath_softc *sc, struct ath_buf *bf)
72{
73 if (sc->rx.buf_hold)
74 ath_rx_buf_link(sc, sc->rx.buf_hold);
75
76 sc->rx.buf_hold = bf;
77}
78
Sujithff37e332008-11-24 12:07:55 +053079static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
80{
81 /* XXX block beacon interrupts */
82 ath9k_hw_setantenna(sc->sc_ah, antenna);
Sujithb77f4832008-12-07 21:44:03 +053083 sc->rx.defant = antenna;
84 sc->rx.rxotherant = 0;
Sujithff37e332008-11-24 12:07:55 +053085}
86
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070087static void ath_opmode_init(struct ath_softc *sc)
88{
Sujithcbe61d82009-02-09 13:27:12 +053089 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez15107182009-09-10 09:22:37 -070090 struct ath_common *common = ath9k_hw_common(ah);
91
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070092 u32 rfilt, mfilt[2];
93
94 /* configure rx filter */
95 rfilt = ath_calcrxfilter(sc);
96 ath9k_hw_setrxfilter(ah, rfilt);
97
98 /* configure bssid mask */
Felix Fietkau364734f2010-09-14 20:22:44 +020099 ath_hw_setbssidmask(common);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700100
101 /* configure operational mode */
102 ath9k_hw_setopmode(ah);
103
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700104 /* calculate and install multicast filter */
105 mfilt[0] = mfilt[1] = ~0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700106 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700107}
108
Felix Fietkaub5c804752010-04-15 17:38:48 -0400109static bool ath_rx_edma_buf_link(struct ath_softc *sc,
110 enum ath9k_rx_qtype qtype)
111{
112 struct ath_hw *ah = sc->sc_ah;
113 struct ath_rx_edma *rx_edma;
114 struct sk_buff *skb;
115 struct ath_buf *bf;
116
117 rx_edma = &sc->rx.rx_edma[qtype];
118 if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
119 return false;
120
121 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
122 list_del_init(&bf->list);
123
124 skb = bf->bf_mpdu;
125
Felix Fietkaub5c804752010-04-15 17:38:48 -0400126 memset(skb->data, 0, ah->caps.rx_status_len);
127 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
128 ah->caps.rx_status_len, DMA_TO_DEVICE);
129
130 SKB_CB_ATHBUF(skb) = bf;
131 ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
Sujith Manoharan07236bf2013-04-23 12:22:18 +0530132 __skb_queue_tail(&rx_edma->rx_fifo, skb);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400133
134 return true;
135}
136
137static void ath_rx_addbuffer_edma(struct ath_softc *sc,
Sujith Manoharan7a8972032013-04-23 12:22:16 +0530138 enum ath9k_rx_qtype qtype)
Felix Fietkaub5c804752010-04-15 17:38:48 -0400139{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400140 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Mohammed Shafi Shajakhan6a01f0c2012-02-28 20:54:44 +0530141 struct ath_buf *bf, *tbf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400142
Felix Fietkaub5c804752010-04-15 17:38:48 -0400143 if (list_empty(&sc->rx.rxbuf)) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800144 ath_dbg(common, QUEUE, "No free rx buf available\n");
Felix Fietkaub5c804752010-04-15 17:38:48 -0400145 return;
146 }
147
Mohammed Shafi Shajakhan6a01f0c2012-02-28 20:54:44 +0530148 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
Felix Fietkaub5c804752010-04-15 17:38:48 -0400149 if (!ath_rx_edma_buf_link(sc, qtype))
150 break;
151
Felix Fietkaub5c804752010-04-15 17:38:48 -0400152}
153
154static void ath_rx_remove_buffer(struct ath_softc *sc,
155 enum ath9k_rx_qtype qtype)
156{
157 struct ath_buf *bf;
158 struct ath_rx_edma *rx_edma;
159 struct sk_buff *skb;
160
161 rx_edma = &sc->rx.rx_edma[qtype];
162
Sujith Manoharan07236bf2013-04-23 12:22:18 +0530163 while ((skb = __skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
Felix Fietkaub5c804752010-04-15 17:38:48 -0400164 bf = SKB_CB_ATHBUF(skb);
165 BUG_ON(!bf);
166 list_add_tail(&bf->list, &sc->rx.rxbuf);
167 }
168}
169
170static void ath_rx_edma_cleanup(struct ath_softc *sc)
171{
Mohammed Shafi Shajakhanba542382011-09-23 14:33:14 +0530172 struct ath_hw *ah = sc->sc_ah;
173 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400174 struct ath_buf *bf;
175
176 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
177 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
178
179 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
Mohammed Shafi Shajakhanba542382011-09-23 14:33:14 +0530180 if (bf->bf_mpdu) {
181 dma_unmap_single(sc->dev, bf->bf_buf_addr,
182 common->rx_bufsize,
183 DMA_BIDIRECTIONAL);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400184 dev_kfree_skb_any(bf->bf_mpdu);
Mohammed Shafi Shajakhanba542382011-09-23 14:33:14 +0530185 bf->bf_buf_addr = 0;
186 bf->bf_mpdu = NULL;
187 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400188 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400189}
190
191static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
192{
Sujith Manoharan5d07cca2013-08-14 21:15:57 +0530193 __skb_queue_head_init(&rx_edma->rx_fifo);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400194 rx_edma->rx_fifo_hwsize = size;
195}
196
197static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
198{
199 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
200 struct ath_hw *ah = sc->sc_ah;
201 struct sk_buff *skb;
202 struct ath_buf *bf;
203 int error = 0, i;
204 u32 size;
205
Felix Fietkaub5c804752010-04-15 17:38:48 -0400206 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
207 ah->caps.rx_status_len);
208
209 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
210 ah->caps.rx_lp_qdepth);
211 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
212 ah->caps.rx_hp_qdepth);
213
214 size = sizeof(struct ath_buf) * nbufs;
Felix Fietkaub81950b12012-12-12 13:14:22 +0100215 bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400216 if (!bf)
217 return -ENOMEM;
218
219 INIT_LIST_HEAD(&sc->rx.rxbuf);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400220
221 for (i = 0; i < nbufs; i++, bf++) {
222 skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
223 if (!skb) {
224 error = -ENOMEM;
225 goto rx_init_fail;
226 }
227
228 memset(skb->data, 0, common->rx_bufsize);
229 bf->bf_mpdu = skb;
230
231 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
232 common->rx_bufsize,
233 DMA_BIDIRECTIONAL);
234 if (unlikely(dma_mapping_error(sc->dev,
235 bf->bf_buf_addr))) {
236 dev_kfree_skb_any(skb);
237 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -0700238 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -0800239 ath_err(common,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400240 "dma_mapping_error() on RX init\n");
241 error = -ENOMEM;
242 goto rx_init_fail;
243 }
244
245 list_add_tail(&bf->list, &sc->rx.rxbuf);
246 }
247
248 return 0;
249
250rx_init_fail:
251 ath_rx_edma_cleanup(sc);
252 return error;
253}
254
255static void ath_edma_start_recv(struct ath_softc *sc)
256{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400257 ath9k_hw_rxena(sc->sc_ah);
Sujith Manoharan7a8972032013-04-23 12:22:16 +0530258 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP);
259 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400260 ath_opmode_init(sc);
Sujith Manoharan4cb54fa2012-06-04 16:27:52 +0530261 ath9k_hw_startpcureceive(sc->sc_ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
Felix Fietkaub5c804752010-04-15 17:38:48 -0400262}
263
264static void ath_edma_stop_recv(struct ath_softc *sc)
265{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400266 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
267 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400268}
269
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700270int ath_rx_init(struct ath_softc *sc, int nbufs)
271{
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -0700272 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700273 struct sk_buff *skb;
274 struct ath_buf *bf;
275 int error = 0;
276
Luis R. Rodriguez4bdd1e92010-10-26 15:27:24 -0700277 spin_lock_init(&sc->sc_pcu_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700278
Felix Fietkau0d955212011-01-26 18:23:27 +0100279 common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
280 sc->sc_ah->caps.rx_status_len;
281
Sujith Manoharane87f3d52013-04-23 12:22:17 +0530282 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
Felix Fietkaub5c804752010-04-15 17:38:48 -0400283 return ath_rx_edma_init(sc, nbufs);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700284
Sujith Manoharane87f3d52013-04-23 12:22:17 +0530285 ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
286 common->cachelsz, common->rx_bufsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700287
Sujith Manoharane87f3d52013-04-23 12:22:17 +0530288 /* Initialize rx descriptors */
289
290 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
291 "rx", nbufs, 1, 0);
292 if (error != 0) {
293 ath_err(common,
294 "failed to allocate rx descriptors: %d\n",
295 error);
296 goto err;
297 }
298
299 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
300 skb = ath_rxbuf_alloc(common, common->rx_bufsize,
301 GFP_KERNEL);
302 if (skb == NULL) {
303 error = -ENOMEM;
Sujith797fe5cb2009-03-30 15:28:45 +0530304 goto err;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700305 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400306
Sujith Manoharane87f3d52013-04-23 12:22:17 +0530307 bf->bf_mpdu = skb;
308 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
309 common->rx_bufsize,
310 DMA_FROM_DEVICE);
311 if (unlikely(dma_mapping_error(sc->dev,
312 bf->bf_buf_addr))) {
313 dev_kfree_skb_any(skb);
314 bf->bf_mpdu = NULL;
315 bf->bf_buf_addr = 0;
316 ath_err(common,
317 "dma_mapping_error() on RX init\n");
318 error = -ENOMEM;
319 goto err;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400320 }
Sujith797fe5cb2009-03-30 15:28:45 +0530321 }
Sujith Manoharane87f3d52013-04-23 12:22:17 +0530322 sc->rx.rxlink = NULL;
Sujith797fe5cb2009-03-30 15:28:45 +0530323err:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700324 if (error)
325 ath_rx_cleanup(sc);
326
327 return error;
328}
329
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700330void ath_rx_cleanup(struct ath_softc *sc)
331{
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800332 struct ath_hw *ah = sc->sc_ah;
333 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700334 struct sk_buff *skb;
335 struct ath_buf *bf;
336
Felix Fietkaub5c804752010-04-15 17:38:48 -0400337 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
338 ath_rx_edma_cleanup(sc);
339 return;
Sujith Manoharane87f3d52013-04-23 12:22:17 +0530340 }
341
342 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
343 skb = bf->bf_mpdu;
344 if (skb) {
345 dma_unmap_single(sc->dev, bf->bf_buf_addr,
346 common->rx_bufsize,
347 DMA_FROM_DEVICE);
348 dev_kfree_skb(skb);
349 bf->bf_buf_addr = 0;
350 bf->bf_mpdu = NULL;
Luis R. Rodriguez051b9192009-03-23 18:25:01 -0400351 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400352 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700353}
354
355/*
356 * Calculate the receive filter according to the
357 * operating mode and state:
358 *
359 * o always accept unicast, broadcast, and multicast traffic
360 * o maintain current state of phy error reception (the hal
361 * may enable phy error frames for noise immunity work)
362 * o probe request frames are accepted only when operating in
363 * hostap, adhoc, or monitor modes
364 * o enable promiscuous mode according to the interface state
365 * o accept beacons:
366 * - when operating in adhoc mode so the 802.11 layer creates
367 * node table entries for peers,
368 * - when operating in station mode for collecting rssi data when
369 * the station is otherwise quiet, or
370 * - when operating as a repeater so we see repeater-sta beacons
371 * - when scanning
372 */
373
374u32 ath_calcrxfilter(struct ath_softc *sc)
375{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700376 u32 rfilt;
377
Felix Fietkauac066972011-10-08 15:49:57 +0200378 rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700379 | ATH9K_RX_FILTER_MCAST;
380
Zefir Kurtisi73e49372013-04-03 18:31:31 +0200381 /* if operating on a DFS channel, enable radar pulse detection */
382 if (sc->hw->conf.radar_enabled)
383 rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR;
384
Jouni Malinen9c1d8e42010-10-13 17:29:31 +0300385 if (sc->rx.rxfilter & FIF_PROBE_REQ)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700386 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
387
Jouni Malinen217ba9d2009-03-10 10:55:50 +0200388 /*
389 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
390 * mode interface or when in monitor mode. AP mode does not need this
391 * since it receives all in-BSS frames anyway.
392 */
Felix Fietkau2e286942011-03-09 01:48:12 +0100393 if (sc->sc_ah->is_monitoring)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700394 rfilt |= ATH9K_RX_FILTER_PROM;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700395
Sujithd42c6b72009-02-04 08:10:22 +0530396 if (sc->rx.rxfilter & FIF_CONTROL)
397 rfilt |= ATH9K_RX_FILTER_CONTROL;
398
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530399 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
Ben Greearcfda6692010-09-14 12:00:22 -0700400 (sc->nvifs <= 1) &&
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530401 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
402 rfilt |= ATH9K_RX_FILTER_MYBEACON;
403 else
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700404 rfilt |= ATH9K_RX_FILTER_BEACON;
405
Felix Fietkau264bbec2011-04-07 19:24:23 +0200406 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
Senthil Balasubramanian66afad02009-09-18 15:06:07 +0530407 (sc->rx.rxfilter & FIF_PSPOLL))
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530408 rfilt |= ATH9K_RX_FILTER_PSPOLL;
Sujithbe0418a2008-11-18 09:05:55 +0530409
Sujith7ea310b2009-09-03 12:08:43 +0530410 if (conf_is_ht(&sc->hw->conf))
411 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
412
Felix Fietkau7545daf2011-01-24 19:23:16 +0100413 if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
Thomas Wagnera5494592012-09-25 21:32:55 +0530414 /* This is needed for older chips */
415 if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
Javier Cardona5eb6ba82009-08-20 19:12:07 -0700416 rfilt |= ATH9K_RX_FILTER_PROM;
Jouni Malinenb93bce22009-03-03 19:23:30 +0200417 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
418 }
419
Gabor Juhosb3d7aa42012-07-03 19:13:33 +0200420 if (AR_SREV_9550(sc->sc_ah))
421 rfilt |= ATH9K_RX_FILTER_4ADDRESS;
422
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700423 return rfilt;
Sujith7dcfdcd2008-08-11 14:03:13 +0530424
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700425}
426
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700427int ath_startrecv(struct ath_softc *sc)
428{
Sujithcbe61d82009-02-09 13:27:12 +0530429 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700430 struct ath_buf *bf, *tbf;
431
Felix Fietkaub5c804752010-04-15 17:38:48 -0400432 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
433 ath_edma_start_recv(sc);
434 return 0;
435 }
436
Sujithb77f4832008-12-07 21:44:03 +0530437 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700438 goto start_recv;
439
Felix Fietkaue96542e2013-08-10 15:59:15 +0200440 sc->rx.buf_hold = NULL;
Sujithb77f4832008-12-07 21:44:03 +0530441 sc->rx.rxlink = NULL;
442 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700443 ath_rx_buf_link(sc, bf);
444 }
445
446 /* We could have deleted elements so the list may be empty now */
Sujithb77f4832008-12-07 21:44:03 +0530447 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700448 goto start_recv;
449
Sujithb77f4832008-12-07 21:44:03 +0530450 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700451 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
Sujithbe0418a2008-11-18 09:05:55 +0530452 ath9k_hw_rxena(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700453
454start_recv:
Sujithbe0418a2008-11-18 09:05:55 +0530455 ath_opmode_init(sc);
Sujith Manoharan4cb54fa2012-06-04 16:27:52 +0530456 ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
Sujithbe0418a2008-11-18 09:05:55 +0530457
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700458 return 0;
459}
460
Felix Fietkau4b883f02013-01-09 16:16:56 +0100461static void ath_flushrecv(struct ath_softc *sc)
462{
463 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
464 ath_rx_tasklet(sc, 1, true);
465 ath_rx_tasklet(sc, 1, false);
466}
467
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700468bool ath_stoprecv(struct ath_softc *sc)
469{
Sujithcbe61d82009-02-09 13:27:12 +0530470 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau5882da022011-04-08 20:13:18 +0200471 bool stopped, reset = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700472
Felix Fietkaud47844a2010-11-20 03:08:47 +0100473 ath9k_hw_abortpcurecv(ah);
Sujithbe0418a2008-11-18 09:05:55 +0530474 ath9k_hw_setrxfilter(ah, 0);
Felix Fietkau5882da022011-04-08 20:13:18 +0200475 stopped = ath9k_hw_stopdmarecv(ah, &reset);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400476
Felix Fietkau4b883f02013-01-09 16:16:56 +0100477 ath_flushrecv(sc);
478
Felix Fietkaub5c804752010-04-15 17:38:48 -0400479 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
480 ath_edma_stop_recv(sc);
481 else
482 sc->rx.rxlink = NULL;
Sujithbe0418a2008-11-18 09:05:55 +0530483
Rajkumar Manoharand5847472010-12-20 14:39:51 +0530484 if (!(ah->ah_flags & AH_UNPLUGGED) &&
485 unlikely(!stopped)) {
Ben Greeard7fd1b502010-12-06 13:13:07 -0800486 ath_err(ath9k_hw_common(sc->sc_ah),
487 "Could not stop RX, we could be "
488 "confusing the DMA engine when we start RX up\n");
489 ATH_DBG_WARN_ON_ONCE(!stopped);
490 }
Felix Fietkau2232d312011-04-15 00:41:43 +0200491 return stopped && !reset;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700492}
493
Jouni Malinencc659652009-05-14 21:28:48 +0300494static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
495{
496 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
497 struct ieee80211_mgmt *mgmt;
498 u8 *pos, *end, id, elen;
499 struct ieee80211_tim_ie *tim;
500
501 mgmt = (struct ieee80211_mgmt *)skb->data;
502 pos = mgmt->u.beacon.variable;
503 end = skb->data + skb->len;
504
505 while (pos + 2 < end) {
506 id = *pos++;
507 elen = *pos++;
508 if (pos + elen > end)
509 break;
510
511 if (id == WLAN_EID_TIM) {
512 if (elen < sizeof(*tim))
513 break;
514 tim = (struct ieee80211_tim_ie *) pos;
515 if (tim->dtim_count != 0)
516 break;
517 return tim->bitmap_ctrl & 0x01;
518 }
519
520 pos += elen;
521 }
522
523 return false;
524}
525
Jouni Malinencc659652009-05-14 21:28:48 +0300526static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
527{
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700528 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300529
530 if (skb->len < 24 + 8 + 2 + 2)
531 return;
532
Sujith1b04b932010-01-08 10:36:05 +0530533 sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
Gabor Juhos293dc5d2009-06-19 12:17:48 +0200534
Sujith1b04b932010-01-08 10:36:05 +0530535 if (sc->ps_flags & PS_BEACON_SYNC) {
536 sc->ps_flags &= ~PS_BEACON_SYNC;
Joe Perchesd2182b62011-12-15 14:55:53 -0800537 ath_dbg(common, PS,
Sujith Manoharan1a6404a2013-02-04 15:38:24 +0530538 "Reconfigure beacon timers based on synchronized timestamp\n");
Sujith Manoharanef4ad632012-07-17 17:15:56 +0530539 ath9k_set_beacon(sc);
Jouni Malinenccdfeab2009-05-20 21:59:08 +0300540 }
541
Jouni Malinencc659652009-05-14 21:28:48 +0300542 if (ath_beacon_dtim_pending_cab(skb)) {
543 /*
544 * Remain awake waiting for buffered broadcast/multicast
Gabor Juhos58f5fff2009-06-17 20:53:20 +0200545 * frames. If the last broadcast/multicast frame is not
546 * received properly, the next beacon frame will work as
547 * a backup trigger for returning into NETWORK SLEEP state,
548 * so we are waiting for it as well.
Jouni Malinencc659652009-05-14 21:28:48 +0300549 */
Joe Perchesd2182b62011-12-15 14:55:53 -0800550 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800551 "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
Sujith1b04b932010-01-08 10:36:05 +0530552 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
Jouni Malinencc659652009-05-14 21:28:48 +0300553 return;
554 }
555
Sujith1b04b932010-01-08 10:36:05 +0530556 if (sc->ps_flags & PS_WAIT_FOR_CAB) {
Jouni Malinencc659652009-05-14 21:28:48 +0300557 /*
558 * This can happen if a broadcast frame is dropped or the AP
559 * fails to send a frame indicating that all CAB frames have
560 * been delivered.
561 */
Sujith1b04b932010-01-08 10:36:05 +0530562 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
Joe Perchesd2182b62011-12-15 14:55:53 -0800563 ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
Jouni Malinencc659652009-05-14 21:28:48 +0300564 }
Jouni Malinencc659652009-05-14 21:28:48 +0300565}
566
Rajkumar Manoharanf73c6042011-09-26 22:16:56 +0530567static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
Jouni Malinencc659652009-05-14 21:28:48 +0300568{
569 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700570 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300571
572 hdr = (struct ieee80211_hdr *)skb->data;
573
574 /* Process Beacon and CAB receive in PS state */
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -0700575 if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530576 && mybeacon) {
Jouni Malinencc659652009-05-14 21:28:48 +0300577 ath_rx_ps_beacon(sc, skb);
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530578 } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
579 (ieee80211_is_data(hdr->frame_control) ||
580 ieee80211_is_action(hdr->frame_control)) &&
581 is_multicast_ether_addr(hdr->addr1) &&
582 !ieee80211_has_moredata(hdr->frame_control)) {
Jouni Malinencc659652009-05-14 21:28:48 +0300583 /*
584 * No more broadcast/multicast frames to be received at this
585 * point.
586 */
Senthil Balasubramanian3fac6df2010-09-16 15:12:35 -0400587 sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
Joe Perchesd2182b62011-12-15 14:55:53 -0800588 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800589 "All PS CAB frames received, back to sleep\n");
Sujith1b04b932010-01-08 10:36:05 +0530590 } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300591 !is_multicast_ether_addr(hdr->addr1) &&
592 !ieee80211_has_morefrags(hdr->frame_control)) {
Sujith1b04b932010-01-08 10:36:05 +0530593 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
Joe Perchesd2182b62011-12-15 14:55:53 -0800594 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800595 "Going back to sleep after having received PS-Poll data (0x%lx)\n",
Sujith1b04b932010-01-08 10:36:05 +0530596 sc->ps_flags & (PS_WAIT_FOR_BEACON |
597 PS_WAIT_FOR_CAB |
598 PS_WAIT_FOR_PSPOLL_DATA |
599 PS_WAIT_FOR_TX_ACK));
Jouni Malinencc659652009-05-14 21:28:48 +0300600 }
601}
602
Felix Fietkaub5c804752010-04-15 17:38:48 -0400603static bool ath_edma_get_buffers(struct ath_softc *sc,
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100604 enum ath9k_rx_qtype qtype,
605 struct ath_rx_status *rs,
606 struct ath_buf **dest)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700607{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400608 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
609 struct ath_hw *ah = sc->sc_ah;
610 struct ath_common *common = ath9k_hw_common(ah);
611 struct sk_buff *skb;
Sujithbe0418a2008-11-18 09:05:55 +0530612 struct ath_buf *bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400613 int ret;
614
615 skb = skb_peek(&rx_edma->rx_fifo);
616 if (!skb)
617 return false;
618
619 bf = SKB_CB_ATHBUF(skb);
620 BUG_ON(!bf);
621
Ming Leice9426d2010-05-15 18:25:40 +0800622 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400623 common->rx_bufsize, DMA_FROM_DEVICE);
624
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100625 ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
Ming Leice9426d2010-05-15 18:25:40 +0800626 if (ret == -EINPROGRESS) {
627 /*let device gain the buffer again*/
628 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
629 common->rx_bufsize, DMA_FROM_DEVICE);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400630 return false;
Ming Leice9426d2010-05-15 18:25:40 +0800631 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400632
633 __skb_unlink(skb, &rx_edma->rx_fifo);
634 if (ret == -EINVAL) {
635 /* corrupt descriptor, skip this one and the following one */
636 list_add_tail(&bf->list, &sc->rx.rxbuf);
637 ath_rx_edma_buf_link(sc, qtype);
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100638
Felix Fietkaub5c804752010-04-15 17:38:48 -0400639 skb = skb_peek(&rx_edma->rx_fifo);
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100640 if (skb) {
641 bf = SKB_CB_ATHBUF(skb);
642 BUG_ON(!bf);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400643
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100644 __skb_unlink(skb, &rx_edma->rx_fifo);
645 list_add_tail(&bf->list, &sc->rx.rxbuf);
646 ath_rx_edma_buf_link(sc, qtype);
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100647 }
Tom Hughes6bb51c72012-06-27 18:21:15 +0100648
649 bf = NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400650 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400651
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100652 *dest = bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400653 return true;
654}
655
656static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
657 struct ath_rx_status *rs,
658 enum ath9k_rx_qtype qtype)
659{
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100660 struct ath_buf *bf = NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400661
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100662 while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
663 if (!bf)
664 continue;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400665
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100666 return bf;
667 }
668 return NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400669}
670
671static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
672 struct ath_rx_status *rs)
673{
674 struct ath_hw *ah = sc->sc_ah;
675 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700676 struct ath_desc *ds;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400677 struct ath_buf *bf;
678 int ret;
679
680 if (list_empty(&sc->rx.rxbuf)) {
681 sc->rx.rxlink = NULL;
682 return NULL;
683 }
684
685 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Felix Fietkaue96542e2013-08-10 15:59:15 +0200686 if (bf == sc->rx.buf_hold)
687 return NULL;
688
Felix Fietkaub5c804752010-04-15 17:38:48 -0400689 ds = bf->bf_desc;
690
691 /*
692 * Must provide the virtual address of the current
693 * descriptor, the physical address, and the virtual
694 * address of the next descriptor in the h/w chain.
695 * This allows the HAL to look ahead to see if the
696 * hardware is done with a descriptor by checking the
697 * done bit in the following descriptor and the address
698 * of the current descriptor the DMA engine is working
699 * on. All this is necessary because of our use of
700 * a self-linked list to avoid rx overruns.
701 */
Rajkumar Manoharan3de21112011-08-13 10:28:11 +0530702 ret = ath9k_hw_rxprocdesc(ah, ds, rs);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400703 if (ret == -EINPROGRESS) {
704 struct ath_rx_status trs;
705 struct ath_buf *tbf;
706 struct ath_desc *tds;
707
708 memset(&trs, 0, sizeof(trs));
709 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
710 sc->rx.rxlink = NULL;
711 return NULL;
712 }
713
714 tbf = list_entry(bf->list.next, struct ath_buf, list);
715
716 /*
717 * On some hardware the descriptor status words could
718 * get corrupted, including the done bit. Because of
719 * this, check if the next descriptor's done bit is
720 * set or not.
721 *
722 * If the next descriptor's done bit is set, the current
723 * descriptor has been corrupted. Force s/w to discard
724 * this descriptor and continue...
725 */
726
727 tds = tbf->bf_desc;
Rajkumar Manoharan3de21112011-08-13 10:28:11 +0530728 ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400729 if (ret == -EINPROGRESS)
730 return NULL;
Felix Fietkau723e7112013-04-08 00:04:11 +0200731
732 /*
733 * mark descriptor as zero-length and set the 'more'
734 * flag to ensure that both buffers get discarded
735 */
736 rs->rs_datalen = 0;
737 rs->rs_more = true;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400738 }
739
Felix Fietkaua3dc48e2013-01-09 16:16:52 +0100740 list_del(&bf->list);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400741 if (!bf->bf_mpdu)
742 return bf;
743
744 /*
745 * Synchronize the DMA transfer with CPU before
746 * 1. accessing the frame
747 * 2. requeueing the same buffer to h/w
748 */
Ming Leice9426d2010-05-15 18:25:40 +0800749 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400750 common->rx_bufsize,
751 DMA_FROM_DEVICE);
752
753 return bf;
754}
755
Sujithd4357002010-05-20 15:34:38 +0530756/* Assumes you've already done the endian to CPU conversion */
757static bool ath9k_rx_accept(struct ath_common *common,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700758 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530759 struct ieee80211_rx_status *rxs,
760 struct ath_rx_status *rx_stats,
761 bool *decrypt_error)
762{
Felix Fietkauec205992011-10-08 22:02:59 +0200763 struct ath_softc *sc = (struct ath_softc *) common->priv;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800764 bool is_mc, is_valid_tkip, strip_mic, mic_error;
Sujithd4357002010-05-20 15:34:38 +0530765 struct ath_hw *ah = common->ah;
Sujithd4357002010-05-20 15:34:38 +0530766 __le16 fc;
767
Sujithd4357002010-05-20 15:34:38 +0530768 fc = hdr->frame_control;
769
Felix Fietkau66760ea2011-07-13 23:35:05 +0800770 is_mc = !!is_multicast_ether_addr(hdr->addr1);
771 is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
772 test_bit(rx_stats->rs_keyix, common->tkip_keymap);
Bill Jordan152e5852011-08-19 11:10:22 -0400773 strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
Michael Liang2a5783b2012-04-20 17:11:57 +0800774 ieee80211_has_protected(fc) &&
Bill Jordan152e5852011-08-19 11:10:22 -0400775 !(rx_stats->rs_status &
Felix Fietkau846d9362011-10-08 22:02:58 +0200776 (ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
777 ATH9K_RXERR_KEYMISS));
Felix Fietkau66760ea2011-07-13 23:35:05 +0800778
Felix Fietkauf88373f2012-02-05 21:15:17 +0100779 /*
780 * Key miss events are only relevant for pairwise keys where the
781 * descriptor does contain a valid key index. This has been observed
782 * mostly with CCMP encryption.
783 */
Felix Fietkaubed3d9c2012-06-23 19:23:31 +0200784 if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID ||
785 !test_bit(rx_stats->rs_keyix, common->ccmp_keymap))
Felix Fietkauf88373f2012-02-05 21:15:17 +0100786 rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
787
Felix Fietkau66760ea2011-07-13 23:35:05 +0800788 mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
789 !ieee80211_has_morefrags(fc) &&
790 !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
791 (rx_stats->rs_status & ATH9K_RXERR_MIC);
792
Sujithd4357002010-05-20 15:34:38 +0530793 /*
794 * The rx_stats->rs_status will not be set until the end of the
795 * chained descriptors so it can be ignored if rs_more is set. The
796 * rs_more will be false at the last element of the chained
797 * descriptors.
798 */
799 if (rx_stats->rs_status != 0) {
Felix Fietkau846d9362011-10-08 22:02:58 +0200800 u8 status_mask;
801
Felix Fietkau66760ea2011-07-13 23:35:05 +0800802 if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
Sujithd4357002010-05-20 15:34:38 +0530803 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800804 mic_error = false;
805 }
Sujithd4357002010-05-20 15:34:38 +0530806
Felix Fietkau846d9362011-10-08 22:02:58 +0200807 if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
808 (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
Sujithd4357002010-05-20 15:34:38 +0530809 *decrypt_error = true;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800810 mic_error = false;
Sujithd4357002010-05-20 15:34:38 +0530811 }
Felix Fietkau66760ea2011-07-13 23:35:05 +0800812
Sujithd4357002010-05-20 15:34:38 +0530813 /*
814 * Reject error frames with the exception of
815 * decryption and MIC failures. For monitor mode,
816 * we also ignore the CRC error.
817 */
Felix Fietkau846d9362011-10-08 22:02:58 +0200818 status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
819 ATH9K_RXERR_KEYMISS;
820
Felix Fietkauec205992011-10-08 22:02:59 +0200821 if (ah->is_monitoring && (sc->rx.rxfilter & FIF_FCSFAIL))
Felix Fietkau846d9362011-10-08 22:02:58 +0200822 status_mask |= ATH9K_RXERR_CRC;
823
824 if (rx_stats->rs_status & ~status_mask)
825 return false;
Sujithd4357002010-05-20 15:34:38 +0530826 }
Felix Fietkau66760ea2011-07-13 23:35:05 +0800827
828 /*
829 * For unicast frames the MIC error bit can have false positives,
830 * so all MIC error reports need to be validated in software.
831 * False negatives are not common, so skip software verification
832 * if the hardware considers the MIC valid.
833 */
834 if (strip_mic)
835 rxs->flag |= RX_FLAG_MMIC_STRIPPED;
836 else if (is_mc && mic_error)
837 rxs->flag |= RX_FLAG_MMIC_ERROR;
838
Sujithd4357002010-05-20 15:34:38 +0530839 return true;
840}
841
842static int ath9k_process_rate(struct ath_common *common,
843 struct ieee80211_hw *hw,
844 struct ath_rx_status *rx_stats,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700845 struct ieee80211_rx_status *rxs)
Sujithd4357002010-05-20 15:34:38 +0530846{
847 struct ieee80211_supported_band *sband;
848 enum ieee80211_band band;
849 unsigned int i = 0;
Ben Greear990e08a2012-04-17 15:19:03 -0700850 struct ath_softc __maybe_unused *sc = common->priv;
Sujithd4357002010-05-20 15:34:38 +0530851
Karl Beldan675a0b02013-03-25 16:26:57 +0100852 band = hw->conf.chandef.chan->band;
Sujithd4357002010-05-20 15:34:38 +0530853 sband = hw->wiphy->bands[band];
854
Simon Wunderlichf819c0e2013-08-14 08:01:32 +0200855 switch (hw->conf.chandef.width) {
856 case NL80211_CHAN_WIDTH_5:
857 rxs->flag |= RX_FLAG_5MHZ;
858 break;
859 case NL80211_CHAN_WIDTH_10:
860 rxs->flag |= RX_FLAG_10MHZ;
861 break;
862 default:
863 break;
864 }
865
Sujithd4357002010-05-20 15:34:38 +0530866 if (rx_stats->rs_rate & 0x80) {
867 /* HT rate */
868 rxs->flag |= RX_FLAG_HT;
Oleksij Rempelab276102013-05-24 12:18:30 +0200869 rxs->flag |= rx_stats->flag;
Sujithd4357002010-05-20 15:34:38 +0530870 rxs->rate_idx = rx_stats->rs_rate & 0x7f;
871 return 0;
872 }
873
874 for (i = 0; i < sband->n_bitrates; i++) {
875 if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
876 rxs->rate_idx = i;
877 return 0;
878 }
879 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
880 rxs->flag |= RX_FLAG_SHORTPRE;
881 rxs->rate_idx = i;
882 return 0;
883 }
884 }
885
886 /*
887 * No valid hardware bitrate found -- we should not get here
888 * because hardware has already validated this frame as OK.
889 */
Joe Perchesd2182b62011-12-15 14:55:53 -0800890 ath_dbg(common, ANY,
Joe Perches226afe62010-12-02 19:12:37 -0800891 "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
892 rx_stats->rs_rate);
Ben Greear15072182012-04-03 09:18:59 -0700893 RX_STAT_INC(rx_rate_err);
Sujithd4357002010-05-20 15:34:38 +0530894 return -EINVAL;
895}
896
897static void ath9k_process_rssi(struct ath_common *common,
898 struct ieee80211_hw *hw,
Sujith Manoharane3acd132013-08-14 21:15:54 +0530899 struct ath_rx_status *rx_stats,
900 struct ieee80211_rx_status *rxs)
Sujithd4357002010-05-20 15:34:38 +0530901{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100902 struct ath_softc *sc = hw->priv;
Sujithd4357002010-05-20 15:34:38 +0530903 struct ath_hw *ah = common->ah;
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200904 int last_rssi;
Felix Fietkau2ef16752012-03-03 15:17:06 +0100905 int rssi = rx_stats->rs_rssi;
Sujithd4357002010-05-20 15:34:38 +0530906
Sujith Manoharane3acd132013-08-14 21:15:54 +0530907 /*
908 * RSSI is not available for subframes in an A-MPDU.
909 */
910 if (rx_stats->rs_moreaggr) {
911 rxs->flag |= RX_FLAG_NO_SIGNAL_VAL;
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200912 return;
Sujith Manoharane3acd132013-08-14 21:15:54 +0530913 }
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200914
Sujith Manoharane3acd132013-08-14 21:15:54 +0530915 /*
916 * Check if the RSSI for the last subframe in an A-MPDU
917 * or an unaggregated frame is valid.
918 */
919 if (rx_stats->rs_rssi == ATH9K_RSSI_BAD) {
920 rxs->flag |= RX_FLAG_NO_SIGNAL_VAL;
921 return;
922 }
923
924 /*
925 * Update Beacon RSSI, this is used by ANI.
926 */
927 if (rx_stats->is_mybeacon &&
928 ((ah->opmode == NL80211_IFTYPE_STATION) ||
929 (ah->opmode == NL80211_IFTYPE_ADHOC))) {
Felix Fietkau9ac586152011-01-24 19:23:18 +0100930 ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
Sujith Manoharane3acd132013-08-14 21:15:54 +0530931 last_rssi = sc->last_rssi;
Ben Greear686b9cb2010-09-23 09:44:36 -0700932
Sujith Manoharane3acd132013-08-14 21:15:54 +0530933 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
934 rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
935 if (rssi < 0)
936 rssi = 0;
Sujithd4357002010-05-20 15:34:38 +0530937
Sujith Manoharane3acd132013-08-14 21:15:54 +0530938 ah->stats.avgbrssi = rssi;
939 }
940
941 rxs->signal = ah->noise + rx_stats->rs_rssi;
Sujithd4357002010-05-20 15:34:38 +0530942}
943
Sujith Manoharane0dd1a92013-08-14 09:11:13 +0530944static void ath9k_process_tsf(struct ath_rx_status *rs,
945 struct ieee80211_rx_status *rxs,
946 u64 tsf)
947{
948 u32 tsf_lower = tsf & 0xffffffff;
949
950 rxs->mactime = (tsf & ~0xffffffffULL) | rs->rs_tstamp;
951 if (rs->rs_tstamp > tsf_lower &&
952 unlikely(rs->rs_tstamp - tsf_lower > 0x10000000))
953 rxs->mactime -= 0x100000000ULL;
954
955 if (rs->rs_tstamp < tsf_lower &&
956 unlikely(tsf_lower - rs->rs_tstamp > 0x10000000))
957 rxs->mactime += 0x100000000ULL;
958}
959
Sujith Manoharan3105b672013-08-14 09:11:14 +0530960#ifdef CONFIG_ATH9K_DEBUGFS
961static s8 fix_rssi_inv_only(u8 rssi_val)
962{
963 if (rssi_val == 128)
964 rssi_val = 0;
965 return (s8) rssi_val;
966}
967#endif
968
969/* returns 1 if this was a spectral frame, even if not handled. */
970static int ath_process_fft(struct ath_softc *sc, struct ieee80211_hdr *hdr,
971 struct ath_rx_status *rs, u64 tsf)
972{
973#ifdef CONFIG_ATH9K_DEBUGFS
974 struct ath_hw *ah = sc->sc_ah;
975 u8 bins[SPECTRAL_HT20_NUM_BINS];
976 u8 *vdata = (u8 *)hdr;
977 struct fft_sample_ht20 fft_sample;
978 struct ath_radar_info *radar_info;
979 struct ath_ht20_mag_info *mag_info;
980 int len = rs->rs_datalen;
981 int dc_pos;
982 u16 length, max_magnitude;
983
984 /* AR9280 and before report via ATH9K_PHYERR_RADAR, AR93xx and newer
985 * via ATH9K_PHYERR_SPECTRAL. Haven't seen ATH9K_PHYERR_FALSE_RADAR_EXT
986 * yet, but this is supposed to be possible as well.
987 */
988 if (rs->rs_phyerr != ATH9K_PHYERR_RADAR &&
989 rs->rs_phyerr != ATH9K_PHYERR_FALSE_RADAR_EXT &&
990 rs->rs_phyerr != ATH9K_PHYERR_SPECTRAL)
991 return 0;
992
993 /* check if spectral scan bit is set. This does not have to be checked
994 * if received through a SPECTRAL phy error, but shouldn't hurt.
995 */
996 radar_info = ((struct ath_radar_info *)&vdata[len]) - 1;
997 if (!(radar_info->pulse_bw_info & SPECTRAL_SCAN_BITMASK))
998 return 0;
999
1000 /* Variation in the data length is possible and will be fixed later.
1001 * Note that we only support HT20 for now.
1002 *
1003 * TODO: add HT20_40 support as well.
1004 */
1005 if ((len > SPECTRAL_HT20_TOTAL_DATA_LEN + 2) ||
1006 (len < SPECTRAL_HT20_TOTAL_DATA_LEN - 1))
1007 return 1;
1008
1009 fft_sample.tlv.type = ATH_FFT_SAMPLE_HT20;
1010 length = sizeof(fft_sample) - sizeof(fft_sample.tlv);
1011 fft_sample.tlv.length = __cpu_to_be16(length);
1012
1013 fft_sample.freq = __cpu_to_be16(ah->curchan->chan->center_freq);
1014 fft_sample.rssi = fix_rssi_inv_only(rs->rs_rssi_ctl0);
1015 fft_sample.noise = ah->noise;
1016
1017 switch (len - SPECTRAL_HT20_TOTAL_DATA_LEN) {
1018 case 0:
1019 /* length correct, nothing to do. */
1020 memcpy(bins, vdata, SPECTRAL_HT20_NUM_BINS);
1021 break;
1022 case -1:
1023 /* first byte missing, duplicate it. */
1024 memcpy(&bins[1], vdata, SPECTRAL_HT20_NUM_BINS - 1);
1025 bins[0] = vdata[0];
1026 break;
1027 case 2:
1028 /* MAC added 2 extra bytes at bin 30 and 32, remove them. */
1029 memcpy(bins, vdata, 30);
1030 bins[30] = vdata[31];
1031 memcpy(&bins[31], &vdata[33], SPECTRAL_HT20_NUM_BINS - 31);
1032 break;
1033 case 1:
1034 /* MAC added 2 extra bytes AND first byte is missing. */
1035 bins[0] = vdata[0];
1036 memcpy(&bins[0], vdata, 30);
1037 bins[31] = vdata[31];
1038 memcpy(&bins[32], &vdata[33], SPECTRAL_HT20_NUM_BINS - 32);
1039 break;
1040 default:
1041 return 1;
1042 }
1043
1044 /* DC value (value in the middle) is the blind spot of the spectral
1045 * sample and invalid, interpolate it.
1046 */
1047 dc_pos = SPECTRAL_HT20_NUM_BINS / 2;
1048 bins[dc_pos] = (bins[dc_pos + 1] + bins[dc_pos - 1]) / 2;
1049
1050 /* mag data is at the end of the frame, in front of radar_info */
1051 mag_info = ((struct ath_ht20_mag_info *)radar_info) - 1;
1052
1053 /* copy raw bins without scaling them */
1054 memcpy(fft_sample.data, bins, SPECTRAL_HT20_NUM_BINS);
1055 fft_sample.max_exp = mag_info->max_exp & 0xf;
1056
1057 max_magnitude = spectral_max_magnitude(mag_info->all_bins);
1058 fft_sample.max_magnitude = __cpu_to_be16(max_magnitude);
1059 fft_sample.max_index = spectral_max_index(mag_info->all_bins);
1060 fft_sample.bitmap_weight = spectral_bitmap_weight(mag_info->all_bins);
1061 fft_sample.tsf = __cpu_to_be64(tsf);
1062
1063 ath_debug_send_fft_sample(sc, &fft_sample.tlv);
1064 return 1;
1065#else
1066 return 0;
1067#endif
1068}
1069
Sujith Manoharan6f384822013-08-14 09:11:18 +05301070static bool ath9k_is_mybeacon(struct ath_softc *sc, struct ieee80211_hdr *hdr)
1071{
1072 struct ath_hw *ah = sc->sc_ah;
1073 struct ath_common *common = ath9k_hw_common(ah);
1074
1075 if (ieee80211_is_beacon(hdr->frame_control)) {
1076 RX_STAT_INC(rx_beacons);
1077 if (!is_zero_ether_addr(common->curbssid) &&
1078 ether_addr_equal(hdr->addr3, common->curbssid))
1079 return true;
1080 }
1081
1082 return false;
1083}
1084
Sujithd4357002010-05-20 15:34:38 +05301085/*
1086 * For Decrypt or Demic errors, we only mark packet status here and always push
1087 * up the frame up to let mac80211 handle the actual error case, be it no
1088 * decryption key or real decryption error. This let us keep statistics there.
1089 */
Felix Fietkau723e7112013-04-08 00:04:11 +02001090static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
Sujith Manoharan6f384822013-08-14 09:11:18 +05301091 struct sk_buff *skb,
Sujithd4357002010-05-20 15:34:38 +05301092 struct ath_rx_status *rx_stats,
1093 struct ieee80211_rx_status *rx_status,
Sujith Manoharane0dd1a92013-08-14 09:11:13 +05301094 bool *decrypt_error, u64 tsf)
Sujithd4357002010-05-20 15:34:38 +05301095{
Felix Fietkau723e7112013-04-08 00:04:11 +02001096 struct ieee80211_hw *hw = sc->hw;
1097 struct ath_hw *ah = sc->sc_ah;
1098 struct ath_common *common = ath9k_hw_common(ah);
Sujith Manoharan6f384822013-08-14 09:11:18 +05301099 struct ieee80211_hdr *hdr;
Felix Fietkau723e7112013-04-08 00:04:11 +02001100 bool discard_current = sc->rx.discard_next;
Sujith Manoharan7c5c73c2013-08-14 09:11:21 +05301101 int ret = 0;
Felix Fietkau723e7112013-04-08 00:04:11 +02001102
Sujith Manoharan5871d2d2013-08-14 09:11:11 +05301103 /*
1104 * Discard corrupt descriptors which are marked in
1105 * ath_get_next_rx_buf().
1106 */
Felix Fietkau723e7112013-04-08 00:04:11 +02001107 sc->rx.discard_next = rx_stats->rs_more;
1108 if (discard_current)
1109 return -EINVAL;
Felix Fietkauf749b942011-07-28 14:08:57 +02001110
Sujithd4357002010-05-20 15:34:38 +05301111 /*
Sujith Manoharan5871d2d2013-08-14 09:11:11 +05301112 * Discard zero-length packets.
1113 */
1114 if (!rx_stats->rs_datalen) {
1115 RX_STAT_INC(rx_len_err);
1116 return -EINVAL;
1117 }
1118
1119 /*
1120 * rs_status follows rs_datalen so if rs_datalen is too large
1121 * we can take a hint that hardware corrupted it, so ignore
1122 * those frames.
1123 */
1124 if (rx_stats->rs_datalen > (common->rx_bufsize - ah->caps.rx_status_len)) {
1125 RX_STAT_INC(rx_len_err);
1126 return -EINVAL;
1127 }
1128
Sujith Manoharan4a470642013-08-14 09:11:12 +05301129 /* Only use status info from the last fragment */
1130 if (rx_stats->rs_more)
1131 return 0;
1132
Sujith Manoharanb0925592013-08-14 09:11:20 +05301133 /*
1134 * Return immediately if the RX descriptor has been marked
1135 * as corrupt based on the various error bits.
1136 *
1137 * This is different from the other corrupt descriptor
1138 * condition handled above.
1139 */
Sujith Manoharan7c5c73c2013-08-14 09:11:21 +05301140 if (rx_stats->rs_status & ATH9K_RXERR_CORRUPT_DESC) {
1141 ret = -EINVAL;
1142 goto exit;
1143 }
Sujith Manoharanb0925592013-08-14 09:11:20 +05301144
Sujith Manoharan6f384822013-08-14 09:11:18 +05301145 hdr = (struct ieee80211_hdr *) (skb->data + ah->caps.rx_status_len);
1146
Sujith Manoharane0dd1a92013-08-14 09:11:13 +05301147 ath9k_process_tsf(rx_stats, rx_status, tsf);
Sujith Manoharan5e85a322013-08-14 09:11:16 +05301148 ath_debug_stat_rx(sc, rx_stats);
Sujith Manoharane0dd1a92013-08-14 09:11:13 +05301149
Sujith Manoharan5871d2d2013-08-14 09:11:11 +05301150 /*
Sujith Manoharan6b87d712013-08-14 09:11:15 +05301151 * Process PHY errors and return so that the packet
1152 * can be dropped.
1153 */
1154 if (rx_stats->rs_status & ATH9K_RXERR_PHY) {
1155 ath9k_dfs_process_phyerr(sc, hdr, rx_stats, rx_status->mactime);
1156 if (ath_process_fft(sc, hdr, rx_stats, rx_status->mactime))
1157 RX_STAT_INC(rx_spectral);
1158
Sujith Manoharan7c5c73c2013-08-14 09:11:21 +05301159 ret = -EINVAL;
1160 goto exit;
Sujith Manoharan6b87d712013-08-14 09:11:15 +05301161 }
1162
1163 /*
Sujithd4357002010-05-20 15:34:38 +05301164 * everything but the rate is checked here, the rate check is done
1165 * separately to avoid doing two lookups for a rate for each frame.
1166 */
Sujith Manoharan7c5c73c2013-08-14 09:11:21 +05301167 if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error)) {
1168 ret = -EINVAL;
1169 goto exit;
1170 }
Sujithd4357002010-05-20 15:34:38 +05301171
Sujith Manoharan6f384822013-08-14 09:11:18 +05301172 rx_stats->is_mybeacon = ath9k_is_mybeacon(sc, hdr);
Sujith Manoharaneb5f9522013-08-14 09:11:19 +05301173 if (rx_stats->is_mybeacon) {
1174 sc->hw_busy_count = 0;
1175 ath_start_rx_poll(sc, 3);
1176 }
Sujith Manoharan6f384822013-08-14 09:11:18 +05301177
Sujith Manoharan7c5c73c2013-08-14 09:11:21 +05301178 if (ath9k_process_rate(common, hw, rx_stats, rx_status)) {
1179 ret =-EINVAL;
1180 goto exit;
1181 }
Sujithd4357002010-05-20 15:34:38 +05301182
Sujith Manoharane3acd132013-08-14 21:15:54 +05301183 ath9k_process_rssi(common, hw, rx_stats, rx_status);
Sujith Manoharan74a97752013-06-03 09:19:23 +05301184
Karl Beldan675a0b02013-03-25 16:26:57 +01001185 rx_status->band = hw->conf.chandef.chan->band;
1186 rx_status->freq = hw->conf.chandef.chan->center_freq;
Sujithd4357002010-05-20 15:34:38 +05301187 rx_status->antenna = rx_stats->rs_antenna;
Thomas Pedersen96d21372012-12-10 14:48:01 -08001188 rx_status->flag |= RX_FLAG_MACTIME_END;
Sujithd4357002010-05-20 15:34:38 +05301189
Sujith Manoharana5525d92013-08-14 09:11:17 +05301190#ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
1191 if (ieee80211_is_data_present(hdr->frame_control) &&
1192 !ieee80211_is_qos_nullfunc(hdr->frame_control))
1193 sc->rx.num_pkts++;
1194#endif
1195
Sujith Manoharan7c5c73c2013-08-14 09:11:21 +05301196exit:
1197 sc->rx.discard_next = false;
1198 return ret;
Sujithd4357002010-05-20 15:34:38 +05301199}
1200
1201static void ath9k_rx_skb_postprocess(struct ath_common *common,
1202 struct sk_buff *skb,
1203 struct ath_rx_status *rx_stats,
1204 struct ieee80211_rx_status *rxs,
1205 bool decrypt_error)
1206{
1207 struct ath_hw *ah = common->ah;
1208 struct ieee80211_hdr *hdr;
1209 int hdrlen, padpos, padsize;
1210 u8 keyix;
1211 __le16 fc;
1212
1213 /* see if any padding is done by the hw and remove it */
1214 hdr = (struct ieee80211_hdr *) skb->data;
1215 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1216 fc = hdr->frame_control;
Felix Fietkauc60c9922013-04-08 00:04:09 +02001217 padpos = ieee80211_hdrlen(fc);
Sujithd4357002010-05-20 15:34:38 +05301218
1219 /* The MAC header is padded to have 32-bit boundary if the
1220 * packet payload is non-zero. The general calculation for
1221 * padsize would take into account odd header lengths:
1222 * padsize = (4 - padpos % 4) % 4; However, since only
1223 * even-length headers are used, padding can only be 0 or 2
1224 * bytes and we can optimize this a bit. In addition, we must
1225 * not try to remove padding from short control frames that do
1226 * not have payload. */
1227 padsize = padpos & 3;
1228 if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1229 memmove(skb->data + padsize, skb->data, padpos);
1230 skb_pull(skb, padsize);
1231 }
1232
1233 keyix = rx_stats->rs_keyix;
1234
1235 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1236 ieee80211_has_protected(fc)) {
1237 rxs->flag |= RX_FLAG_DECRYPTED;
1238 } else if (ieee80211_has_protected(fc)
1239 && !decrypt_error && skb->len >= hdrlen + 4) {
1240 keyix = skb->data[hdrlen + 3] >> 6;
1241
1242 if (test_bit(keyix, common->keymap))
1243 rxs->flag |= RX_FLAG_DECRYPTED;
1244 }
1245 if (ah->sw_mgmt_crypto &&
1246 (rxs->flag & RX_FLAG_DECRYPTED) &&
1247 ieee80211_is_mgmt(fc))
1248 /* Use software decrypt for management frames. */
1249 rxs->flag &= ~RX_FLAG_DECRYPTED;
1250}
Felix Fietkaub5c804752010-04-15 17:38:48 -04001251
Sujith Manoharanc3124df2013-08-14 21:15:56 +05301252/*
1253 * Run the LNA combining algorithm only in these cases:
1254 *
1255 * Standalone WLAN cards with both LNA/Antenna diversity
1256 * enabled in the EEPROM.
1257 *
1258 * WLAN+BT cards which are in the supported card list
1259 * in ath_pci_id_table and the user has loaded the
1260 * driver with "bt_ant_diversity" set to true.
1261 */
1262static void ath9k_antenna_check(struct ath_softc *sc,
1263 struct ath_rx_status *rs)
1264{
1265 struct ath_hw *ah = sc->sc_ah;
1266 struct ath9k_hw_capabilities *pCap = &ah->caps;
1267 struct ath_common *common = ath9k_hw_common(ah);
1268
1269 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB))
1270 return;
1271
1272 /*
Sujith Manoharanc3124df2013-08-14 21:15:56 +05301273 * Change the default rx antenna if rx diversity
1274 * chooses the other antenna 3 times in a row.
1275 */
1276 if (sc->rx.defant != rs->rs_antenna) {
1277 if (++sc->rx.rxotherant >= 3)
1278 ath_setdefantenna(sc, rs->rs_antenna);
1279 } else {
1280 sc->rx.rxotherant = 0;
1281 }
1282
1283 if (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV) {
1284 if (common->bt_ant_diversity)
1285 ath_ant_comb_scan(sc, rs);
1286 } else {
1287 ath_ant_comb_scan(sc, rs);
1288 }
1289}
1290
Christian Lamparter21fbbca2013-01-30 23:37:41 +01001291static void ath9k_apply_ampdu_details(struct ath_softc *sc,
1292 struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
1293{
1294 if (rs->rs_isaggr) {
1295 rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
1296
1297 rxs->ampdu_reference = sc->rx.ampdu_ref;
1298
1299 if (!rs->rs_moreaggr) {
1300 rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
1301 sc->rx.ampdu_ref++;
1302 }
1303
1304 if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
1305 rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
1306 }
1307}
1308
Felix Fietkaub5c804752010-04-15 17:38:48 -04001309int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1310{
1311 struct ath_buf *bf;
Felix Fietkau0d955212011-01-26 18:23:27 +01001312 struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001313 struct ieee80211_rx_status *rxs;
Sujithcbe61d82009-02-09 13:27:12 +05301314 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -07001315 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau7545daf2011-01-24 19:23:16 +01001316 struct ieee80211_hw *hw = sc->hw;
Luis R. Rodriguezc9b14172009-11-04 16:47:22 -08001317 int retval;
Felix Fietkau29bffa92010-03-29 20:14:23 -07001318 struct ath_rx_status rs;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001319 enum ath9k_rx_qtype qtype;
1320 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1321 int dma_type;
Felix Fietkaua6d20552010-06-12 00:33:54 -04001322 u64 tsf = 0;
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001323 unsigned long flags;
Felix Fietkau2e1cd492013-04-08 00:04:10 +02001324 dma_addr_t new_buf_addr;
Sujithbe0418a2008-11-18 09:05:55 +05301325
Felix Fietkaub5c804752010-04-15 17:38:48 -04001326 if (edma)
Felix Fietkaub5c804752010-04-15 17:38:48 -04001327 dma_type = DMA_BIDIRECTIONAL;
Ming Lei56824222010-05-14 21:15:38 +08001328 else
1329 dma_type = DMA_FROM_DEVICE;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001330
1331 qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001332
Felix Fietkaua6d20552010-06-12 00:33:54 -04001333 tsf = ath9k_hw_gettsf64(ah);
Felix Fietkaua6d20552010-06-12 00:33:54 -04001334
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001335 do {
Lorenzo Bianconie1352fd2012-08-10 11:00:24 +02001336 bool decrypt_error = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001337
Felix Fietkau29bffa92010-03-29 20:14:23 -07001338 memset(&rs, 0, sizeof(rs));
Felix Fietkaub5c804752010-04-15 17:38:48 -04001339 if (edma)
1340 bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1341 else
1342 bf = ath_get_next_rx_buf(sc, &rs);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001343
Felix Fietkaub5c804752010-04-15 17:38:48 -04001344 if (!bf)
1345 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001346
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001347 skb = bf->bf_mpdu;
Sujithbe0418a2008-11-18 09:05:55 +05301348 if (!skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001349 continue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001350
Felix Fietkau0d955212011-01-26 18:23:27 +01001351 /*
1352 * Take frame header from the first fragment and RX status from
1353 * the last one.
1354 */
1355 if (sc->rx.frag)
1356 hdr_skb = sc->rx.frag;
1357 else
1358 hdr_skb = skb;
1359
Sujith Manoharanf6307dd2013-08-14 09:11:09 +05301360 rxs = IEEE80211_SKB_RXCB(hdr_skb);
Ashok Nagarajanffb1c562012-03-09 18:57:39 -08001361 memset(rxs, 0, sizeof(struct ieee80211_rx_status));
1362
Sujith Manoharan6f384822013-08-14 09:11:18 +05301363 retval = ath9k_rx_skb_preprocess(sc, hdr_skb, &rs, rxs,
Sujith Manoharane0dd1a92013-08-14 09:11:13 +05301364 &decrypt_error, tsf);
Zefir Kurtisi83c76572011-11-16 11:09:44 +01001365 if (retval)
1366 goto requeue_drop_frag;
1367
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001368 /* Ensure we always have an skb to requeue once we are done
1369 * processing the current buffer's skb */
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001370 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001371
1372 /* If there is no memory we ignore the current RX'd frame,
1373 * tell hardware it can give us a new frame using the old
Sujithb77f4832008-12-07 21:44:03 +05301374 * skb and put it at the tail of the sc->rx.rxbuf list for
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001375 * processing. */
Ben Greear15072182012-04-03 09:18:59 -07001376 if (!requeue_skb) {
1377 RX_STAT_INC(rx_oom_err);
Felix Fietkau0d955212011-01-26 18:23:27 +01001378 goto requeue_drop_frag;
Ben Greear15072182012-04-03 09:18:59 -07001379 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001380
Felix Fietkau2e1cd492013-04-08 00:04:10 +02001381 /* We will now give hardware our shiny new allocated skb */
1382 new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
1383 common->rx_bufsize, dma_type);
1384 if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
1385 dev_kfree_skb_any(requeue_skb);
1386 goto requeue_drop_frag;
1387 }
1388
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301389 /* Unmap the frame */
Gabor Juhos7da3c552009-01-14 20:17:03 +01001390 dma_unmap_single(sc->dev, bf->bf_buf_addr,
Felix Fietkau2e1cd492013-04-08 00:04:10 +02001391 common->rx_bufsize, dma_type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001392
Sujith Manoharan176f0e82013-04-23 12:22:19 +05301393 bf->bf_mpdu = requeue_skb;
1394 bf->bf_buf_addr = new_buf_addr;
1395
Felix Fietkaub5c804752010-04-15 17:38:48 -04001396 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1397 if (ah->caps.rx_status_len)
1398 skb_pull(skb, ah->caps.rx_status_len);
Sujithbe0418a2008-11-18 09:05:55 +05301399
Felix Fietkau0d955212011-01-26 18:23:27 +01001400 if (!rs.rs_more)
1401 ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1402 rxs, decrypt_error);
Sujithbe0418a2008-11-18 09:05:55 +05301403
Felix Fietkau0d955212011-01-26 18:23:27 +01001404 if (rs.rs_more) {
Ben Greear15072182012-04-03 09:18:59 -07001405 RX_STAT_INC(rx_frags);
Felix Fietkau0d955212011-01-26 18:23:27 +01001406 /*
1407 * rs_more indicates chained descriptors which can be
1408 * used to link buffers together for a sort of
1409 * scatter-gather operation.
1410 */
1411 if (sc->rx.frag) {
1412 /* too many fragments - cannot handle frame */
1413 dev_kfree_skb_any(sc->rx.frag);
1414 dev_kfree_skb_any(skb);
Ben Greear15072182012-04-03 09:18:59 -07001415 RX_STAT_INC(rx_too_many_frags_err);
Felix Fietkau0d955212011-01-26 18:23:27 +01001416 skb = NULL;
1417 }
1418 sc->rx.frag = skb;
1419 goto requeue;
1420 }
1421
1422 if (sc->rx.frag) {
1423 int space = skb->len - skb_tailroom(hdr_skb);
1424
Felix Fietkau0d955212011-01-26 18:23:27 +01001425 if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
1426 dev_kfree_skb(skb);
Ben Greear15072182012-04-03 09:18:59 -07001427 RX_STAT_INC(rx_oom_err);
Felix Fietkau0d955212011-01-26 18:23:27 +01001428 goto requeue_drop_frag;
1429 }
1430
Eric Dumazetb5447ff2012-03-15 13:43:29 -07001431 sc->rx.frag = NULL;
1432
Felix Fietkau0d955212011-01-26 18:23:27 +01001433 skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
1434 skb->len);
1435 dev_kfree_skb_any(skb);
1436 skb = hdr_skb;
1437 }
1438
Felix Fietkau66760ea2011-07-13 23:35:05 +08001439 if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
1440 skb_trim(skb, skb->len - 8);
1441
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001442 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Mohammed Shafi Shajakhanaaef24b2010-12-07 20:40:58 +05301443 if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
Rajkumar Manoharanf73c6042011-09-26 22:16:56 +05301444 PS_WAIT_FOR_CAB |
1445 PS_WAIT_FOR_PSPOLL_DATA)) ||
1446 ath9k_check_auto_sleep(sc))
1447 ath_rx_ps(sc, skb, rs.is_mybeacon);
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001448 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Jouni Malinencc659652009-05-14 21:28:48 +03001449
Sujith Manoharanc3124df2013-08-14 21:15:56 +05301450 ath9k_antenna_check(sc, &rs);
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001451
Christian Lamparter21fbbca2013-01-30 23:37:41 +01001452 ath9k_apply_ampdu_details(sc, &rs, rxs);
1453
Felix Fietkau7545daf2011-01-24 19:23:16 +01001454 ieee80211_rx(hw, skb);
Jouni Malinencc659652009-05-14 21:28:48 +03001455
Felix Fietkau0d955212011-01-26 18:23:27 +01001456requeue_drop_frag:
1457 if (sc->rx.frag) {
1458 dev_kfree_skb_any(sc->rx.frag);
1459 sc->rx.frag = NULL;
1460 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001461requeue:
Felix Fietkaua3dc48e2013-01-09 16:16:52 +01001462 list_add_tail(&bf->list, &sc->rx.rxbuf);
1463 if (flush)
1464 continue;
1465
Felix Fietkaub5c804752010-04-15 17:38:48 -04001466 if (edma) {
Felix Fietkaub5c804752010-04-15 17:38:48 -04001467 ath_rx_edma_buf_link(sc, qtype);
1468 } else {
Felix Fietkaue96542e2013-08-10 15:59:15 +02001469 ath_rx_buf_relink(sc, bf);
Felix Fietkaua3dc48e2013-01-09 16:16:52 +01001470 ath9k_hw_rxena(ah);
Felix Fietkaub5c804752010-04-15 17:38:48 -04001471 }
Sujithbe0418a2008-11-18 09:05:55 +05301472 } while (1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001473
Rajkumar Manoharan29ab0b32011-08-13 10:28:10 +05301474 if (!(ah->imask & ATH9K_INT_RXEOL)) {
1475 ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
Felix Fietkau72d874c2011-10-08 20:06:19 +02001476 ath9k_hw_set_interrupts(ah);
Rajkumar Manoharan29ab0b32011-08-13 10:28:10 +05301477 }
1478
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001479 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001480}