blob: 82e340d3ec60a81cc83d2427bd9280e55ca3af3a [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>
Sujith394cf0a2009-02-09 13:26:54 +053018#include "ath9k.h"
Luis R. Rodriguezb622a722010-04-15 17:39:28 -040019#include "ar9003_mac.h"
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070020
Felix Fietkau1a04d592013-10-11 23:30:52 +020021#define SKB_CB_ATHBUF(__skb) (*((struct ath_rxbuf **)__skb->cb))
Felix Fietkaub5c804752010-04-15 17:38:48 -040022
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -070023static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
24{
25 return sc->ps_enabled &&
26 (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
27}
28
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070029/*
30 * Setup and link descriptors.
31 *
32 * 11N: we can no longer afford to self link the last descriptor.
33 * MAC acknowledges BA status as long as it copies frames to host
34 * buffer (or rx fifo). This can incorrectly acknowledge packets
35 * to a sender if last desc is self-linked.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070036 */
Felix Fietkau1a04d592013-10-11 23:30:52 +020037static void ath_rx_buf_link(struct ath_softc *sc, struct ath_rxbuf *bf)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070038{
Sujithcbe61d82009-02-09 13:27:12 +053039 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080040 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070041 struct ath_desc *ds;
42 struct sk_buff *skb;
43
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070044 ds = bf->bf_desc;
Sujithbe0418a2008-11-18 09:05:55 +053045 ds->ds_link = 0; /* link to null */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070046 ds->ds_data = bf->bf_buf_addr;
47
Sujithbe0418a2008-11-18 09:05:55 +053048 /* virtual addr of the beginning of the buffer. */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070049 skb = bf->bf_mpdu;
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -070050 BUG_ON(skb == NULL);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070051 ds->ds_vdata = skb->data;
52
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080053 /*
54 * setup rx descriptors. The rx_bufsize here tells the hardware
Luis R. Rodriguezb4b6cda2008-11-20 17:15:13 -080055 * how much data it can DMA to us and that we are prepared
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080056 * to process
57 */
Sujithb77f4832008-12-07 21:44:03 +053058 ath9k_hw_setuprxdesc(ah, ds,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080059 common->rx_bufsize,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070060 0);
61
Sujithb77f4832008-12-07 21:44:03 +053062 if (sc->rx.rxlink == NULL)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070063 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
64 else
Sujithb77f4832008-12-07 21:44:03 +053065 *sc->rx.rxlink = bf->bf_daddr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070066
Sujithb77f4832008-12-07 21:44:03 +053067 sc->rx.rxlink = &ds->ds_link;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070068}
69
Felix Fietkau1a04d592013-10-11 23:30:52 +020070static void ath_rx_buf_relink(struct ath_softc *sc, struct ath_rxbuf *bf)
Felix Fietkaue96542e2013-08-10 15:59:15 +020071{
72 if (sc->rx.buf_hold)
73 ath_rx_buf_link(sc, sc->rx.buf_hold);
74
75 sc->rx.buf_hold = bf;
76}
77
Sujithff37e332008-11-24 12:07:55 +053078static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
79{
80 /* XXX block beacon interrupts */
81 ath9k_hw_setantenna(sc->sc_ah, antenna);
Sujithb77f4832008-12-07 21:44:03 +053082 sc->rx.defant = antenna;
83 sc->rx.rxotherant = 0;
Sujithff37e332008-11-24 12:07:55 +053084}
85
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070086static void ath_opmode_init(struct ath_softc *sc)
87{
Sujithcbe61d82009-02-09 13:27:12 +053088 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez15107182009-09-10 09:22:37 -070089 struct ath_common *common = ath9k_hw_common(ah);
90
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070091 u32 rfilt, mfilt[2];
92
93 /* configure rx filter */
94 rfilt = ath_calcrxfilter(sc);
95 ath9k_hw_setrxfilter(ah, rfilt);
96
97 /* configure bssid mask */
Felix Fietkau364734f2010-09-14 20:22:44 +020098 ath_hw_setbssidmask(common);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070099
100 /* configure operational mode */
101 ath9k_hw_setopmode(ah);
102
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700103 /* calculate and install multicast filter */
104 mfilt[0] = mfilt[1] = ~0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700105 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700106}
107
Felix Fietkaub5c804752010-04-15 17:38:48 -0400108static bool ath_rx_edma_buf_link(struct ath_softc *sc,
109 enum ath9k_rx_qtype qtype)
110{
111 struct ath_hw *ah = sc->sc_ah;
112 struct ath_rx_edma *rx_edma;
113 struct sk_buff *skb;
Felix Fietkau1a04d592013-10-11 23:30:52 +0200114 struct ath_rxbuf *bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400115
116 rx_edma = &sc->rx.rx_edma[qtype];
117 if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
118 return false;
119
Felix Fietkau1a04d592013-10-11 23:30:52 +0200120 bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400121 list_del_init(&bf->list);
122
123 skb = bf->bf_mpdu;
124
Felix Fietkaub5c804752010-04-15 17:38:48 -0400125 memset(skb->data, 0, ah->caps.rx_status_len);
126 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
127 ah->caps.rx_status_len, DMA_TO_DEVICE);
128
129 SKB_CB_ATHBUF(skb) = bf;
130 ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
Sujith Manoharan07236bf2013-04-23 12:22:18 +0530131 __skb_queue_tail(&rx_edma->rx_fifo, skb);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400132
133 return true;
134}
135
136static void ath_rx_addbuffer_edma(struct ath_softc *sc,
Sujith Manoharan7a8972032013-04-23 12:22:16 +0530137 enum ath9k_rx_qtype qtype)
Felix Fietkaub5c804752010-04-15 17:38:48 -0400138{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400139 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkau1a04d592013-10-11 23:30:52 +0200140 struct ath_rxbuf *bf, *tbf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400141
Felix Fietkaub5c804752010-04-15 17:38:48 -0400142 if (list_empty(&sc->rx.rxbuf)) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800143 ath_dbg(common, QUEUE, "No free rx buf available\n");
Felix Fietkaub5c804752010-04-15 17:38:48 -0400144 return;
145 }
146
Mohammed Shafi Shajakhan6a01f0c2012-02-28 20:54:44 +0530147 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list)
Felix Fietkaub5c804752010-04-15 17:38:48 -0400148 if (!ath_rx_edma_buf_link(sc, qtype))
149 break;
150
Felix Fietkaub5c804752010-04-15 17:38:48 -0400151}
152
153static void ath_rx_remove_buffer(struct ath_softc *sc,
154 enum ath9k_rx_qtype qtype)
155{
Felix Fietkau1a04d592013-10-11 23:30:52 +0200156 struct ath_rxbuf *bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400157 struct ath_rx_edma *rx_edma;
158 struct sk_buff *skb;
159
160 rx_edma = &sc->rx.rx_edma[qtype];
161
Sujith Manoharan07236bf2013-04-23 12:22:18 +0530162 while ((skb = __skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
Felix Fietkaub5c804752010-04-15 17:38:48 -0400163 bf = SKB_CB_ATHBUF(skb);
164 BUG_ON(!bf);
165 list_add_tail(&bf->list, &sc->rx.rxbuf);
166 }
167}
168
169static void ath_rx_edma_cleanup(struct ath_softc *sc)
170{
Mohammed Shafi Shajakhanba542382011-09-23 14:33:14 +0530171 struct ath_hw *ah = sc->sc_ah;
172 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau1a04d592013-10-11 23:30:52 +0200173 struct ath_rxbuf *bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400174
175 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
176 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
177
178 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
Mohammed Shafi Shajakhanba542382011-09-23 14:33:14 +0530179 if (bf->bf_mpdu) {
180 dma_unmap_single(sc->dev, bf->bf_buf_addr,
181 common->rx_bufsize,
182 DMA_BIDIRECTIONAL);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400183 dev_kfree_skb_any(bf->bf_mpdu);
Mohammed Shafi Shajakhanba542382011-09-23 14:33:14 +0530184 bf->bf_buf_addr = 0;
185 bf->bf_mpdu = NULL;
186 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400187 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400188}
189
190static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
191{
Sujith Manoharan5d07cca2013-08-14 21:15:57 +0530192 __skb_queue_head_init(&rx_edma->rx_fifo);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400193 rx_edma->rx_fifo_hwsize = size;
194}
195
196static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
197{
198 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
199 struct ath_hw *ah = sc->sc_ah;
200 struct sk_buff *skb;
Felix Fietkau1a04d592013-10-11 23:30:52 +0200201 struct ath_rxbuf *bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400202 int error = 0, i;
203 u32 size;
204
Felix Fietkaub5c804752010-04-15 17:38:48 -0400205 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
206 ah->caps.rx_status_len);
207
208 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
209 ah->caps.rx_lp_qdepth);
210 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
211 ah->caps.rx_hp_qdepth);
212
Felix Fietkau1a04d592013-10-11 23:30:52 +0200213 size = sizeof(struct ath_rxbuf) * nbufs;
Felix Fietkaub81950b12012-12-12 13:14:22 +0100214 bf = devm_kzalloc(sc->dev, size, GFP_KERNEL);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400215 if (!bf)
216 return -ENOMEM;
217
218 INIT_LIST_HEAD(&sc->rx.rxbuf);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400219
220 for (i = 0; i < nbufs; i++, bf++) {
221 skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
222 if (!skb) {
223 error = -ENOMEM;
224 goto rx_init_fail;
225 }
226
227 memset(skb->data, 0, common->rx_bufsize);
228 bf->bf_mpdu = skb;
229
230 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
231 common->rx_bufsize,
232 DMA_BIDIRECTIONAL);
233 if (unlikely(dma_mapping_error(sc->dev,
234 bf->bf_buf_addr))) {
235 dev_kfree_skb_any(skb);
236 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -0700237 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -0800238 ath_err(common,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400239 "dma_mapping_error() on RX init\n");
240 error = -ENOMEM;
241 goto rx_init_fail;
242 }
243
244 list_add_tail(&bf->list, &sc->rx.rxbuf);
245 }
246
247 return 0;
248
249rx_init_fail:
250 ath_rx_edma_cleanup(sc);
251 return error;
252}
253
254static void ath_edma_start_recv(struct ath_softc *sc)
255{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400256 ath9k_hw_rxena(sc->sc_ah);
Sujith Manoharan7a8972032013-04-23 12:22:16 +0530257 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP);
258 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400259 ath_opmode_init(sc);
Sujith Manoharan4cb54fa2012-06-04 16:27:52 +0530260 ath9k_hw_startpcureceive(sc->sc_ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
Felix Fietkaub5c804752010-04-15 17:38:48 -0400261}
262
263static void ath_edma_stop_recv(struct ath_softc *sc)
264{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400265 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
266 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400267}
268
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700269int ath_rx_init(struct ath_softc *sc, int nbufs)
270{
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -0700271 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700272 struct sk_buff *skb;
Felix Fietkau1a04d592013-10-11 23:30:52 +0200273 struct ath_rxbuf *bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700274 int error = 0;
275
Luis R. Rodriguez4bdd1e92010-10-26 15:27:24 -0700276 spin_lock_init(&sc->sc_pcu_lock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700277
Felix Fietkau0d955212011-01-26 18:23:27 +0100278 common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
279 sc->sc_ah->caps.rx_status_len;
280
Sujith Manoharane87f3d52013-04-23 12:22:17 +0530281 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
Felix Fietkaub5c804752010-04-15 17:38:48 -0400282 return ath_rx_edma_init(sc, nbufs);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700283
Sujith Manoharane87f3d52013-04-23 12:22:17 +0530284 ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
285 common->cachelsz, common->rx_bufsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700286
Sujith Manoharane87f3d52013-04-23 12:22:17 +0530287 /* Initialize rx descriptors */
288
289 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
290 "rx", nbufs, 1, 0);
291 if (error != 0) {
292 ath_err(common,
293 "failed to allocate rx descriptors: %d\n",
294 error);
295 goto err;
296 }
297
298 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
299 skb = ath_rxbuf_alloc(common, common->rx_bufsize,
300 GFP_KERNEL);
301 if (skb == NULL) {
302 error = -ENOMEM;
Sujith797fe5cb2009-03-30 15:28:45 +0530303 goto err;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700304 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400305
Sujith Manoharane87f3d52013-04-23 12:22:17 +0530306 bf->bf_mpdu = skb;
307 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
308 common->rx_bufsize,
309 DMA_FROM_DEVICE);
310 if (unlikely(dma_mapping_error(sc->dev,
311 bf->bf_buf_addr))) {
312 dev_kfree_skb_any(skb);
313 bf->bf_mpdu = NULL;
314 bf->bf_buf_addr = 0;
315 ath_err(common,
316 "dma_mapping_error() on RX init\n");
317 error = -ENOMEM;
318 goto err;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400319 }
Sujith797fe5cb2009-03-30 15:28:45 +0530320 }
Sujith Manoharane87f3d52013-04-23 12:22:17 +0530321 sc->rx.rxlink = NULL;
Sujith797fe5cb2009-03-30 15:28:45 +0530322err:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700323 if (error)
324 ath_rx_cleanup(sc);
325
326 return error;
327}
328
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700329void ath_rx_cleanup(struct ath_softc *sc)
330{
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800331 struct ath_hw *ah = sc->sc_ah;
332 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700333 struct sk_buff *skb;
Felix Fietkau1a04d592013-10-11 23:30:52 +0200334 struct ath_rxbuf *bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700335
Felix Fietkaub5c804752010-04-15 17:38:48 -0400336 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
337 ath_rx_edma_cleanup(sc);
338 return;
Sujith Manoharane87f3d52013-04-23 12:22:17 +0530339 }
340
341 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
342 skb = bf->bf_mpdu;
343 if (skb) {
344 dma_unmap_single(sc->dev, bf->bf_buf_addr,
345 common->rx_bufsize,
346 DMA_FROM_DEVICE);
347 dev_kfree_skb(skb);
348 bf->bf_buf_addr = 0;
349 bf->bf_mpdu = NULL;
Luis R. Rodriguez051b9192009-03-23 18:25:01 -0400350 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400351 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700352}
353
354/*
355 * Calculate the receive filter according to the
356 * operating mode and state:
357 *
358 * o always accept unicast, broadcast, and multicast traffic
359 * o maintain current state of phy error reception (the hal
360 * may enable phy error frames for noise immunity work)
361 * o probe request frames are accepted only when operating in
362 * hostap, adhoc, or monitor modes
363 * o enable promiscuous mode according to the interface state
364 * o accept beacons:
365 * - when operating in adhoc mode so the 802.11 layer creates
366 * node table entries for peers,
367 * - when operating in station mode for collecting rssi data when
368 * the station is otherwise quiet, or
369 * - when operating as a repeater so we see repeater-sta beacons
370 * - when scanning
371 */
372
373u32 ath_calcrxfilter(struct ath_softc *sc)
374{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700375 u32 rfilt;
376
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -0700377 if (config_enabled(CONFIG_ATH9K_TX99))
378 return 0;
379
Felix Fietkauac066972011-10-08 15:49:57 +0200380 rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700381 | ATH9K_RX_FILTER_MCAST;
382
Zefir Kurtisi73e49372013-04-03 18:31:31 +0200383 /* if operating on a DFS channel, enable radar pulse detection */
384 if (sc->hw->conf.radar_enabled)
385 rfilt |= ATH9K_RX_FILTER_PHYRADAR | ATH9K_RX_FILTER_PHYERR;
386
Jouni Malinen9c1d8e42010-10-13 17:29:31 +0300387 if (sc->rx.rxfilter & FIF_PROBE_REQ)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700388 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
389
Jouni Malinen217ba9d2009-03-10 10:55:50 +0200390 /*
391 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
392 * mode interface or when in monitor mode. AP mode does not need this
393 * since it receives all in-BSS frames anyway.
394 */
Felix Fietkau2e286942011-03-09 01:48:12 +0100395 if (sc->sc_ah->is_monitoring)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700396 rfilt |= ATH9K_RX_FILTER_PROM;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700397
Sujithd42c6b72009-02-04 08:10:22 +0530398 if (sc->rx.rxfilter & FIF_CONTROL)
399 rfilt |= ATH9K_RX_FILTER_CONTROL;
400
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530401 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
Ben Greearcfda6692010-09-14 12:00:22 -0700402 (sc->nvifs <= 1) &&
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530403 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
404 rfilt |= ATH9K_RX_FILTER_MYBEACON;
405 else
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700406 rfilt |= ATH9K_RX_FILTER_BEACON;
407
Felix Fietkau264bbec2011-04-07 19:24:23 +0200408 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
Senthil Balasubramanian66afad02009-09-18 15:06:07 +0530409 (sc->rx.rxfilter & FIF_PSPOLL))
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530410 rfilt |= ATH9K_RX_FILTER_PSPOLL;
Sujithbe0418a2008-11-18 09:05:55 +0530411
Sujith7ea310b2009-09-03 12:08:43 +0530412 if (conf_is_ht(&sc->hw->conf))
413 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
414
Felix Fietkau7545daf2011-01-24 19:23:16 +0100415 if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
Thomas Wagnera5494592012-09-25 21:32:55 +0530416 /* This is needed for older chips */
417 if (sc->sc_ah->hw_version.macVersion <= AR_SREV_VERSION_9160)
Javier Cardona5eb6ba82009-08-20 19:12:07 -0700418 rfilt |= ATH9K_RX_FILTER_PROM;
Jouni Malinenb93bce22009-03-03 19:23:30 +0200419 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
420 }
421
Sujith Manoharan2c323052013-12-31 08:12:02 +0530422 if (AR_SREV_9550(sc->sc_ah) || AR_SREV_9531(sc->sc_ah))
Gabor Juhosb3d7aa42012-07-03 19:13:33 +0200423 rfilt |= ATH9K_RX_FILTER_4ADDRESS;
424
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700425 return rfilt;
Sujith7dcfdcd2008-08-11 14:03:13 +0530426
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700427}
428
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700429int ath_startrecv(struct ath_softc *sc)
430{
Sujithcbe61d82009-02-09 13:27:12 +0530431 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau1a04d592013-10-11 23:30:52 +0200432 struct ath_rxbuf *bf, *tbf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700433
Felix Fietkaub5c804752010-04-15 17:38:48 -0400434 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
435 ath_edma_start_recv(sc);
436 return 0;
437 }
438
Sujithb77f4832008-12-07 21:44:03 +0530439 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700440 goto start_recv;
441
Felix Fietkaue96542e2013-08-10 15:59:15 +0200442 sc->rx.buf_hold = NULL;
Sujithb77f4832008-12-07 21:44:03 +0530443 sc->rx.rxlink = NULL;
444 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700445 ath_rx_buf_link(sc, bf);
446 }
447
448 /* We could have deleted elements so the list may be empty now */
Sujithb77f4832008-12-07 21:44:03 +0530449 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700450 goto start_recv;
451
Felix Fietkau1a04d592013-10-11 23:30:52 +0200452 bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700453 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
Sujithbe0418a2008-11-18 09:05:55 +0530454 ath9k_hw_rxena(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700455
456start_recv:
Sujithbe0418a2008-11-18 09:05:55 +0530457 ath_opmode_init(sc);
Sujith Manoharan4cb54fa2012-06-04 16:27:52 +0530458 ath9k_hw_startpcureceive(ah, !!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL));
Sujithbe0418a2008-11-18 09:05:55 +0530459
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700460 return 0;
461}
462
Felix Fietkau4b883f02013-01-09 16:16:56 +0100463static void ath_flushrecv(struct ath_softc *sc)
464{
465 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
466 ath_rx_tasklet(sc, 1, true);
467 ath_rx_tasklet(sc, 1, false);
468}
469
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700470bool ath_stoprecv(struct ath_softc *sc)
471{
Sujithcbe61d82009-02-09 13:27:12 +0530472 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau5882da022011-04-08 20:13:18 +0200473 bool stopped, reset = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700474
Felix Fietkaud47844a2010-11-20 03:08:47 +0100475 ath9k_hw_abortpcurecv(ah);
Sujithbe0418a2008-11-18 09:05:55 +0530476 ath9k_hw_setrxfilter(ah, 0);
Felix Fietkau5882da022011-04-08 20:13:18 +0200477 stopped = ath9k_hw_stopdmarecv(ah, &reset);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400478
Felix Fietkau4b883f02013-01-09 16:16:56 +0100479 ath_flushrecv(sc);
480
Felix Fietkaub5c804752010-04-15 17:38:48 -0400481 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
482 ath_edma_stop_recv(sc);
483 else
484 sc->rx.rxlink = NULL;
Sujithbe0418a2008-11-18 09:05:55 +0530485
Rajkumar Manoharand5847472010-12-20 14:39:51 +0530486 if (!(ah->ah_flags & AH_UNPLUGGED) &&
487 unlikely(!stopped)) {
Ben Greeard7fd1b502010-12-06 13:13:07 -0800488 ath_err(ath9k_hw_common(sc->sc_ah),
489 "Could not stop RX, we could be "
490 "confusing the DMA engine when we start RX up\n");
491 ATH_DBG_WARN_ON_ONCE(!stopped);
492 }
Felix Fietkau2232d312011-04-15 00:41:43 +0200493 return stopped && !reset;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700494}
495
Jouni Malinencc659652009-05-14 21:28:48 +0300496static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
497{
498 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
499 struct ieee80211_mgmt *mgmt;
500 u8 *pos, *end, id, elen;
501 struct ieee80211_tim_ie *tim;
502
503 mgmt = (struct ieee80211_mgmt *)skb->data;
504 pos = mgmt->u.beacon.variable;
505 end = skb->data + skb->len;
506
507 while (pos + 2 < end) {
508 id = *pos++;
509 elen = *pos++;
510 if (pos + elen > end)
511 break;
512
513 if (id == WLAN_EID_TIM) {
514 if (elen < sizeof(*tim))
515 break;
516 tim = (struct ieee80211_tim_ie *) pos;
517 if (tim->dtim_count != 0)
518 break;
519 return tim->bitmap_ctrl & 0x01;
520 }
521
522 pos += elen;
523 }
524
525 return false;
526}
527
Jouni Malinencc659652009-05-14 21:28:48 +0300528static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
529{
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700530 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300531
532 if (skb->len < 24 + 8 + 2 + 2)
533 return;
534
Sujith1b04b932010-01-08 10:36:05 +0530535 sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
Gabor Juhos293dc5d2009-06-19 12:17:48 +0200536
Sujith1b04b932010-01-08 10:36:05 +0530537 if (sc->ps_flags & PS_BEACON_SYNC) {
538 sc->ps_flags &= ~PS_BEACON_SYNC;
Joe Perchesd2182b62011-12-15 14:55:53 -0800539 ath_dbg(common, PS,
Sujith Manoharan1a6404a2013-02-04 15:38:24 +0530540 "Reconfigure beacon timers based on synchronized timestamp\n");
Sujith Manoharanef4ad632012-07-17 17:15:56 +0530541 ath9k_set_beacon(sc);
Jouni Malinenccdfeab2009-05-20 21:59:08 +0300542 }
543
Jouni Malinencc659652009-05-14 21:28:48 +0300544 if (ath_beacon_dtim_pending_cab(skb)) {
545 /*
546 * Remain awake waiting for buffered broadcast/multicast
Gabor Juhos58f5fff2009-06-17 20:53:20 +0200547 * frames. If the last broadcast/multicast frame is not
548 * received properly, the next beacon frame will work as
549 * a backup trigger for returning into NETWORK SLEEP state,
550 * so we are waiting for it as well.
Jouni Malinencc659652009-05-14 21:28:48 +0300551 */
Joe Perchesd2182b62011-12-15 14:55:53 -0800552 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800553 "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
Sujith1b04b932010-01-08 10:36:05 +0530554 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
Jouni Malinencc659652009-05-14 21:28:48 +0300555 return;
556 }
557
Sujith1b04b932010-01-08 10:36:05 +0530558 if (sc->ps_flags & PS_WAIT_FOR_CAB) {
Jouni Malinencc659652009-05-14 21:28:48 +0300559 /*
560 * This can happen if a broadcast frame is dropped or the AP
561 * fails to send a frame indicating that all CAB frames have
562 * been delivered.
563 */
Sujith1b04b932010-01-08 10:36:05 +0530564 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
Joe Perchesd2182b62011-12-15 14:55:53 -0800565 ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
Jouni Malinencc659652009-05-14 21:28:48 +0300566 }
Jouni Malinencc659652009-05-14 21:28:48 +0300567}
568
Rajkumar Manoharanf73c6042011-09-26 22:16:56 +0530569static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
Jouni Malinencc659652009-05-14 21:28:48 +0300570{
571 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700572 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300573
574 hdr = (struct ieee80211_hdr *)skb->data;
575
576 /* Process Beacon and CAB receive in PS state */
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -0700577 if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530578 && mybeacon) {
Jouni Malinencc659652009-05-14 21:28:48 +0300579 ath_rx_ps_beacon(sc, skb);
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530580 } else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
581 (ieee80211_is_data(hdr->frame_control) ||
582 ieee80211_is_action(hdr->frame_control)) &&
583 is_multicast_ether_addr(hdr->addr1) &&
584 !ieee80211_has_moredata(hdr->frame_control)) {
Jouni Malinencc659652009-05-14 21:28:48 +0300585 /*
586 * No more broadcast/multicast frames to be received at this
587 * point.
588 */
Senthil Balasubramanian3fac6df2010-09-16 15:12:35 -0400589 sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
Joe Perchesd2182b62011-12-15 14:55:53 -0800590 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800591 "All PS CAB frames received, back to sleep\n");
Sujith1b04b932010-01-08 10:36:05 +0530592 } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300593 !is_multicast_ether_addr(hdr->addr1) &&
594 !ieee80211_has_morefrags(hdr->frame_control)) {
Sujith1b04b932010-01-08 10:36:05 +0530595 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
Joe Perchesd2182b62011-12-15 14:55:53 -0800596 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800597 "Going back to sleep after having received PS-Poll data (0x%lx)\n",
Sujith1b04b932010-01-08 10:36:05 +0530598 sc->ps_flags & (PS_WAIT_FOR_BEACON |
599 PS_WAIT_FOR_CAB |
600 PS_WAIT_FOR_PSPOLL_DATA |
601 PS_WAIT_FOR_TX_ACK));
Jouni Malinencc659652009-05-14 21:28:48 +0300602 }
603}
604
Felix Fietkaub5c804752010-04-15 17:38:48 -0400605static bool ath_edma_get_buffers(struct ath_softc *sc,
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100606 enum ath9k_rx_qtype qtype,
607 struct ath_rx_status *rs,
Felix Fietkau1a04d592013-10-11 23:30:52 +0200608 struct ath_rxbuf **dest)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700609{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400610 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
611 struct ath_hw *ah = sc->sc_ah;
612 struct ath_common *common = ath9k_hw_common(ah);
613 struct sk_buff *skb;
Felix Fietkau1a04d592013-10-11 23:30:52 +0200614 struct ath_rxbuf *bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400615 int ret;
616
617 skb = skb_peek(&rx_edma->rx_fifo);
618 if (!skb)
619 return false;
620
621 bf = SKB_CB_ATHBUF(skb);
622 BUG_ON(!bf);
623
Ming Leice9426d2010-05-15 18:25:40 +0800624 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400625 common->rx_bufsize, DMA_FROM_DEVICE);
626
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100627 ret = ath9k_hw_process_rxdesc_edma(ah, rs, skb->data);
Ming Leice9426d2010-05-15 18:25:40 +0800628 if (ret == -EINPROGRESS) {
629 /*let device gain the buffer again*/
630 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
631 common->rx_bufsize, DMA_FROM_DEVICE);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400632 return false;
Ming Leice9426d2010-05-15 18:25:40 +0800633 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400634
635 __skb_unlink(skb, &rx_edma->rx_fifo);
636 if (ret == -EINVAL) {
637 /* corrupt descriptor, skip this one and the following one */
638 list_add_tail(&bf->list, &sc->rx.rxbuf);
639 ath_rx_edma_buf_link(sc, qtype);
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100640
Felix Fietkaub5c804752010-04-15 17:38:48 -0400641 skb = skb_peek(&rx_edma->rx_fifo);
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100642 if (skb) {
643 bf = SKB_CB_ATHBUF(skb);
644 BUG_ON(!bf);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400645
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100646 __skb_unlink(skb, &rx_edma->rx_fifo);
647 list_add_tail(&bf->list, &sc->rx.rxbuf);
648 ath_rx_edma_buf_link(sc, qtype);
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100649 }
Tom Hughes6bb51c72012-06-27 18:21:15 +0100650
651 bf = NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400652 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400653
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100654 *dest = bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400655 return true;
656}
657
Felix Fietkau1a04d592013-10-11 23:30:52 +0200658static struct ath_rxbuf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400659 struct ath_rx_status *rs,
660 enum ath9k_rx_qtype qtype)
661{
Felix Fietkau1a04d592013-10-11 23:30:52 +0200662 struct ath_rxbuf *bf = NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400663
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100664 while (ath_edma_get_buffers(sc, qtype, rs, &bf)) {
665 if (!bf)
666 continue;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400667
Felix Fietkau3a2923e2012-03-03 15:17:05 +0100668 return bf;
669 }
670 return NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400671}
672
Felix Fietkau1a04d592013-10-11 23:30:52 +0200673static struct ath_rxbuf *ath_get_next_rx_buf(struct ath_softc *sc,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400674 struct ath_rx_status *rs)
675{
676 struct ath_hw *ah = sc->sc_ah;
677 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700678 struct ath_desc *ds;
Felix Fietkau1a04d592013-10-11 23:30:52 +0200679 struct ath_rxbuf *bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400680 int ret;
681
682 if (list_empty(&sc->rx.rxbuf)) {
683 sc->rx.rxlink = NULL;
684 return NULL;
685 }
686
Felix Fietkau1a04d592013-10-11 23:30:52 +0200687 bf = list_first_entry(&sc->rx.rxbuf, struct ath_rxbuf, list);
Felix Fietkaue96542e2013-08-10 15:59:15 +0200688 if (bf == sc->rx.buf_hold)
689 return NULL;
690
Felix Fietkaub5c804752010-04-15 17:38:48 -0400691 ds = bf->bf_desc;
692
693 /*
694 * Must provide the virtual address of the current
695 * descriptor, the physical address, and the virtual
696 * address of the next descriptor in the h/w chain.
697 * This allows the HAL to look ahead to see if the
698 * hardware is done with a descriptor by checking the
699 * done bit in the following descriptor and the address
700 * of the current descriptor the DMA engine is working
701 * on. All this is necessary because of our use of
702 * a self-linked list to avoid rx overruns.
703 */
Rajkumar Manoharan3de21112011-08-13 10:28:11 +0530704 ret = ath9k_hw_rxprocdesc(ah, ds, rs);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400705 if (ret == -EINPROGRESS) {
706 struct ath_rx_status trs;
Felix Fietkau1a04d592013-10-11 23:30:52 +0200707 struct ath_rxbuf *tbf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400708 struct ath_desc *tds;
709
710 memset(&trs, 0, sizeof(trs));
711 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
712 sc->rx.rxlink = NULL;
713 return NULL;
714 }
715
Felix Fietkau1a04d592013-10-11 23:30:52 +0200716 tbf = list_entry(bf->list.next, struct ath_rxbuf, list);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400717
718 /*
719 * On some hardware the descriptor status words could
720 * get corrupted, including the done bit. Because of
721 * this, check if the next descriptor's done bit is
722 * set or not.
723 *
724 * If the next descriptor's done bit is set, the current
725 * descriptor has been corrupted. Force s/w to discard
726 * this descriptor and continue...
727 */
728
729 tds = tbf->bf_desc;
Rajkumar Manoharan3de21112011-08-13 10:28:11 +0530730 ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400731 if (ret == -EINPROGRESS)
732 return NULL;
Felix Fietkau723e7112013-04-08 00:04:11 +0200733
734 /*
Felix Fietkaub7b146c2014-02-24 22:26:06 +0100735 * Re-check previous descriptor, in case it has been filled
736 * in the mean time.
Felix Fietkau723e7112013-04-08 00:04:11 +0200737 */
Felix Fietkaub7b146c2014-02-24 22:26:06 +0100738 ret = ath9k_hw_rxprocdesc(ah, ds, rs);
739 if (ret == -EINPROGRESS) {
740 /*
741 * mark descriptor as zero-length and set the 'more'
742 * flag to ensure that both buffers get discarded
743 */
744 rs->rs_datalen = 0;
745 rs->rs_more = true;
746 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400747 }
748
Felix Fietkaua3dc48e2013-01-09 16:16:52 +0100749 list_del(&bf->list);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400750 if (!bf->bf_mpdu)
751 return bf;
752
753 /*
754 * Synchronize the DMA transfer with CPU before
755 * 1. accessing the frame
756 * 2. requeueing the same buffer to h/w
757 */
Ming Leice9426d2010-05-15 18:25:40 +0800758 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400759 common->rx_bufsize,
760 DMA_FROM_DEVICE);
761
762 return bf;
763}
764
Sujithd4357002010-05-20 15:34:38 +0530765/* Assumes you've already done the endian to CPU conversion */
766static bool ath9k_rx_accept(struct ath_common *common,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700767 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530768 struct ieee80211_rx_status *rxs,
769 struct ath_rx_status *rx_stats,
770 bool *decrypt_error)
771{
Felix Fietkauec205992011-10-08 22:02:59 +0200772 struct ath_softc *sc = (struct ath_softc *) common->priv;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800773 bool is_mc, is_valid_tkip, strip_mic, mic_error;
Sujithd4357002010-05-20 15:34:38 +0530774 struct ath_hw *ah = common->ah;
Sujithd4357002010-05-20 15:34:38 +0530775 __le16 fc;
776
Sujithd4357002010-05-20 15:34:38 +0530777 fc = hdr->frame_control;
778
Felix Fietkau66760ea2011-07-13 23:35:05 +0800779 is_mc = !!is_multicast_ether_addr(hdr->addr1);
780 is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
781 test_bit(rx_stats->rs_keyix, common->tkip_keymap);
Bill Jordan152e5852011-08-19 11:10:22 -0400782 strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
Michael Liang2a5783b2012-04-20 17:11:57 +0800783 ieee80211_has_protected(fc) &&
Bill Jordan152e5852011-08-19 11:10:22 -0400784 !(rx_stats->rs_status &
Felix Fietkau846d9362011-10-08 22:02:58 +0200785 (ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
786 ATH9K_RXERR_KEYMISS));
Felix Fietkau66760ea2011-07-13 23:35:05 +0800787
Felix Fietkauf88373f2012-02-05 21:15:17 +0100788 /*
789 * Key miss events are only relevant for pairwise keys where the
790 * descriptor does contain a valid key index. This has been observed
791 * mostly with CCMP encryption.
792 */
Felix Fietkaubed3d9c2012-06-23 19:23:31 +0200793 if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID ||
794 !test_bit(rx_stats->rs_keyix, common->ccmp_keymap))
Felix Fietkauf88373f2012-02-05 21:15:17 +0100795 rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
796
Felix Fietkau66760ea2011-07-13 23:35:05 +0800797 mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
798 !ieee80211_has_morefrags(fc) &&
799 !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
800 (rx_stats->rs_status & ATH9K_RXERR_MIC);
801
Sujithd4357002010-05-20 15:34:38 +0530802 /*
803 * The rx_stats->rs_status will not be set until the end of the
804 * chained descriptors so it can be ignored if rs_more is set. The
805 * rs_more will be false at the last element of the chained
806 * descriptors.
807 */
808 if (rx_stats->rs_status != 0) {
Felix Fietkau846d9362011-10-08 22:02:58 +0200809 u8 status_mask;
810
Felix Fietkau66760ea2011-07-13 23:35:05 +0800811 if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
Sujithd4357002010-05-20 15:34:38 +0530812 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800813 mic_error = false;
814 }
Sujithd4357002010-05-20 15:34:38 +0530815
Felix Fietkau846d9362011-10-08 22:02:58 +0200816 if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
817 (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
Sujithd4357002010-05-20 15:34:38 +0530818 *decrypt_error = true;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800819 mic_error = false;
Sujithd4357002010-05-20 15:34:38 +0530820 }
Felix Fietkau66760ea2011-07-13 23:35:05 +0800821
Sujithd4357002010-05-20 15:34:38 +0530822 /*
823 * Reject error frames with the exception of
824 * decryption and MIC failures. For monitor mode,
825 * we also ignore the CRC error.
826 */
Felix Fietkau846d9362011-10-08 22:02:58 +0200827 status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
828 ATH9K_RXERR_KEYMISS;
829
Felix Fietkauec205992011-10-08 22:02:59 +0200830 if (ah->is_monitoring && (sc->rx.rxfilter & FIF_FCSFAIL))
Felix Fietkau846d9362011-10-08 22:02:58 +0200831 status_mask |= ATH9K_RXERR_CRC;
832
833 if (rx_stats->rs_status & ~status_mask)
834 return false;
Sujithd4357002010-05-20 15:34:38 +0530835 }
Felix Fietkau66760ea2011-07-13 23:35:05 +0800836
837 /*
838 * For unicast frames the MIC error bit can have false positives,
839 * so all MIC error reports need to be validated in software.
840 * False negatives are not common, so skip software verification
841 * if the hardware considers the MIC valid.
842 */
843 if (strip_mic)
844 rxs->flag |= RX_FLAG_MMIC_STRIPPED;
845 else if (is_mc && mic_error)
846 rxs->flag |= RX_FLAG_MMIC_ERROR;
847
Sujithd4357002010-05-20 15:34:38 +0530848 return true;
849}
850
851static int ath9k_process_rate(struct ath_common *common,
852 struct ieee80211_hw *hw,
853 struct ath_rx_status *rx_stats,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700854 struct ieee80211_rx_status *rxs)
Sujithd4357002010-05-20 15:34:38 +0530855{
856 struct ieee80211_supported_band *sband;
857 enum ieee80211_band band;
858 unsigned int i = 0;
Ben Greear990e08a2012-04-17 15:19:03 -0700859 struct ath_softc __maybe_unused *sc = common->priv;
Sujith Manoharanff9a93f2014-01-09 08:51:14 +0530860 struct ath_hw *ah = sc->sc_ah;
Sujithd4357002010-05-20 15:34:38 +0530861
Sujith Manoharanff9a93f2014-01-09 08:51:14 +0530862 band = ah->curchan->chan->band;
Sujithd4357002010-05-20 15:34:38 +0530863 sband = hw->wiphy->bands[band];
864
Sujith Manoharanff9a93f2014-01-09 08:51:14 +0530865 if (IS_CHAN_QUARTER_RATE(ah->curchan))
Simon Wunderlichf819c0e2013-08-14 08:01:32 +0200866 rxs->flag |= RX_FLAG_5MHZ;
Sujith Manoharanff9a93f2014-01-09 08:51:14 +0530867 else if (IS_CHAN_HALF_RATE(ah->curchan))
Simon Wunderlichf819c0e2013-08-14 08:01:32 +0200868 rxs->flag |= RX_FLAG_10MHZ;
Simon Wunderlichf819c0e2013-08-14 08:01:32 +0200869
Sujithd4357002010-05-20 15:34:38 +0530870 if (rx_stats->rs_rate & 0x80) {
871 /* HT rate */
872 rxs->flag |= RX_FLAG_HT;
Oleksij Rempelab276102013-05-24 12:18:30 +0200873 rxs->flag |= rx_stats->flag;
Sujithd4357002010-05-20 15:34:38 +0530874 rxs->rate_idx = rx_stats->rs_rate & 0x7f;
875 return 0;
876 }
877
878 for (i = 0; i < sband->n_bitrates; i++) {
879 if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
880 rxs->rate_idx = i;
881 return 0;
882 }
883 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
884 rxs->flag |= RX_FLAG_SHORTPRE;
885 rxs->rate_idx = i;
886 return 0;
887 }
888 }
889
890 /*
891 * No valid hardware bitrate found -- we should not get here
892 * because hardware has already validated this frame as OK.
893 */
Joe Perchesd2182b62011-12-15 14:55:53 -0800894 ath_dbg(common, ANY,
Joe Perches226afe62010-12-02 19:12:37 -0800895 "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
896 rx_stats->rs_rate);
Ben Greear15072182012-04-03 09:18:59 -0700897 RX_STAT_INC(rx_rate_err);
Sujithd4357002010-05-20 15:34:38 +0530898 return -EINVAL;
899}
900
901static void ath9k_process_rssi(struct ath_common *common,
902 struct ieee80211_hw *hw,
Sujith Manoharane3acd132013-08-14 21:15:54 +0530903 struct ath_rx_status *rx_stats,
904 struct ieee80211_rx_status *rxs)
Sujithd4357002010-05-20 15:34:38 +0530905{
Felix Fietkau9ac58612011-01-24 19:23:18 +0100906 struct ath_softc *sc = hw->priv;
Sujithd4357002010-05-20 15:34:38 +0530907 struct ath_hw *ah = common->ah;
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200908 int last_rssi;
Felix Fietkau2ef16752012-03-03 15:17:06 +0100909 int rssi = rx_stats->rs_rssi;
Felix Fietkaue45e91d2013-12-14 18:03:36 +0100910 int i, j;
Sujithd4357002010-05-20 15:34:38 +0530911
Sujith Manoharane3acd132013-08-14 21:15:54 +0530912 /*
913 * RSSI is not available for subframes in an A-MPDU.
914 */
915 if (rx_stats->rs_moreaggr) {
916 rxs->flag |= RX_FLAG_NO_SIGNAL_VAL;
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200917 return;
Sujith Manoharane3acd132013-08-14 21:15:54 +0530918 }
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200919
Sujith Manoharane3acd132013-08-14 21:15:54 +0530920 /*
921 * Check if the RSSI for the last subframe in an A-MPDU
922 * or an unaggregated frame is valid.
923 */
924 if (rx_stats->rs_rssi == ATH9K_RSSI_BAD) {
925 rxs->flag |= RX_FLAG_NO_SIGNAL_VAL;
926 return;
927 }
928
Felix Fietkaue45e91d2013-12-14 18:03:36 +0100929 for (i = 0, j = 0; i < ARRAY_SIZE(rx_stats->rs_rssi_ctl); i++) {
930 s8 rssi;
931
932 if (!(ah->rxchainmask & BIT(i)))
933 continue;
934
935 rssi = rx_stats->rs_rssi_ctl[i];
936 if (rssi != ATH9K_RSSI_BAD) {
937 rxs->chains |= BIT(j);
938 rxs->chain_signal[j] = ah->noise + rssi;
939 }
940 j++;
941 }
942
Sujith Manoharane3acd132013-08-14 21:15:54 +0530943 /*
944 * Update Beacon RSSI, this is used by ANI.
945 */
946 if (rx_stats->is_mybeacon &&
947 ((ah->opmode == NL80211_IFTYPE_STATION) ||
948 (ah->opmode == NL80211_IFTYPE_ADHOC))) {
Felix Fietkau9ac58612011-01-24 19:23:18 +0100949 ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
Sujith Manoharane3acd132013-08-14 21:15:54 +0530950 last_rssi = sc->last_rssi;
Ben Greear686b9cb2010-09-23 09:44:36 -0700951
Sujith Manoharane3acd132013-08-14 21:15:54 +0530952 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
953 rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
954 if (rssi < 0)
955 rssi = 0;
Sujithd4357002010-05-20 15:34:38 +0530956
Sujith Manoharane3acd132013-08-14 21:15:54 +0530957 ah->stats.avgbrssi = rssi;
958 }
959
960 rxs->signal = ah->noise + rx_stats->rs_rssi;
Sujithd4357002010-05-20 15:34:38 +0530961}
962
Sujith Manoharane0dd1a92013-08-14 09:11:13 +0530963static void ath9k_process_tsf(struct ath_rx_status *rs,
964 struct ieee80211_rx_status *rxs,
965 u64 tsf)
966{
967 u32 tsf_lower = tsf & 0xffffffff;
968
969 rxs->mactime = (tsf & ~0xffffffffULL) | rs->rs_tstamp;
970 if (rs->rs_tstamp > tsf_lower &&
971 unlikely(rs->rs_tstamp - tsf_lower > 0x10000000))
972 rxs->mactime -= 0x100000000ULL;
973
974 if (rs->rs_tstamp < tsf_lower &&
975 unlikely(tsf_lower - rs->rs_tstamp > 0x10000000))
976 rxs->mactime += 0x100000000ULL;
977}
978
Sujithd4357002010-05-20 15:34:38 +0530979/*
980 * For Decrypt or Demic errors, we only mark packet status here and always push
981 * up the frame up to let mac80211 handle the actual error case, be it no
982 * decryption key or real decryption error. This let us keep statistics there.
983 */
Felix Fietkau723e7112013-04-08 00:04:11 +0200984static int ath9k_rx_skb_preprocess(struct ath_softc *sc,
Sujith Manoharan6f384822013-08-14 09:11:18 +0530985 struct sk_buff *skb,
Sujithd4357002010-05-20 15:34:38 +0530986 struct ath_rx_status *rx_stats,
987 struct ieee80211_rx_status *rx_status,
Sujith Manoharane0dd1a92013-08-14 09:11:13 +0530988 bool *decrypt_error, u64 tsf)
Sujithd4357002010-05-20 15:34:38 +0530989{
Felix Fietkau723e7112013-04-08 00:04:11 +0200990 struct ieee80211_hw *hw = sc->hw;
991 struct ath_hw *ah = sc->sc_ah;
992 struct ath_common *common = ath9k_hw_common(ah);
Sujith Manoharan6f384822013-08-14 09:11:18 +0530993 struct ieee80211_hdr *hdr;
Felix Fietkau723e7112013-04-08 00:04:11 +0200994 bool discard_current = sc->rx.discard_next;
995
Sujith Manoharan5871d2d2013-08-14 09:11:11 +0530996 /*
997 * Discard corrupt descriptors which are marked in
998 * ath_get_next_rx_buf().
999 */
Felix Fietkau723e7112013-04-08 00:04:11 +02001000 if (discard_current)
Felix Fietkaub7b146c2014-02-24 22:26:06 +01001001 goto corrupt;
1002
1003 sc->rx.discard_next = false;
Felix Fietkauf749b942011-07-28 14:08:57 +02001004
Sujithd4357002010-05-20 15:34:38 +05301005 /*
Sujith Manoharan5871d2d2013-08-14 09:11:11 +05301006 * Discard zero-length packets.
1007 */
1008 if (!rx_stats->rs_datalen) {
1009 RX_STAT_INC(rx_len_err);
Felix Fietkaub7b146c2014-02-24 22:26:06 +01001010 goto corrupt;
Sujith Manoharan5871d2d2013-08-14 09:11:11 +05301011 }
1012
Felix Fietkaub7b146c2014-02-24 22:26:06 +01001013 /*
1014 * rs_status follows rs_datalen so if rs_datalen is too large
1015 * we can take a hint that hardware corrupted it, so ignore
1016 * those frames.
1017 */
Sujith Manoharan5871d2d2013-08-14 09:11:11 +05301018 if (rx_stats->rs_datalen > (common->rx_bufsize - ah->caps.rx_status_len)) {
1019 RX_STAT_INC(rx_len_err);
Felix Fietkaub7b146c2014-02-24 22:26:06 +01001020 goto corrupt;
Sujith Manoharan5871d2d2013-08-14 09:11:11 +05301021 }
1022
Sujith Manoharan4a470642013-08-14 09:11:12 +05301023 /* Only use status info from the last fragment */
1024 if (rx_stats->rs_more)
1025 return 0;
1026
Sujith Manoharanb0925592013-08-14 09:11:20 +05301027 /*
1028 * Return immediately if the RX descriptor has been marked
1029 * as corrupt based on the various error bits.
1030 *
1031 * This is different from the other corrupt descriptor
1032 * condition handled above.
1033 */
Felix Fietkaub7b146c2014-02-24 22:26:06 +01001034 if (rx_stats->rs_status & ATH9K_RXERR_CORRUPT_DESC)
1035 goto corrupt;
Sujith Manoharanb0925592013-08-14 09:11:20 +05301036
Sujith Manoharan6f384822013-08-14 09:11:18 +05301037 hdr = (struct ieee80211_hdr *) (skb->data + ah->caps.rx_status_len);
1038
Sujith Manoharane0dd1a92013-08-14 09:11:13 +05301039 ath9k_process_tsf(rx_stats, rx_status, tsf);
Sujith Manoharan5e85a322013-08-14 09:11:16 +05301040 ath_debug_stat_rx(sc, rx_stats);
Sujith Manoharane0dd1a92013-08-14 09:11:13 +05301041
Sujith Manoharan5871d2d2013-08-14 09:11:11 +05301042 /*
Sujith Manoharan6b87d712013-08-14 09:11:15 +05301043 * Process PHY errors and return so that the packet
1044 * can be dropped.
1045 */
1046 if (rx_stats->rs_status & ATH9K_RXERR_PHY) {
1047 ath9k_dfs_process_phyerr(sc, hdr, rx_stats, rx_status->mactime);
1048 if (ath_process_fft(sc, hdr, rx_stats, rx_status->mactime))
1049 RX_STAT_INC(rx_spectral);
1050
Felix Fietkaub7b146c2014-02-24 22:26:06 +01001051 return -EINVAL;
Sujith Manoharan6b87d712013-08-14 09:11:15 +05301052 }
1053
1054 /*
Sujithd4357002010-05-20 15:34:38 +05301055 * everything but the rate is checked here, the rate check is done
1056 * separately to avoid doing two lookups for a rate for each frame.
1057 */
Felix Fietkaub7b146c2014-02-24 22:26:06 +01001058 if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
1059 return -EINVAL;
Sujithd4357002010-05-20 15:34:38 +05301060
Oleksij Rempel1cc47a52014-01-15 17:07:15 +01001061 if (ath_is_mybeacon(common, hdr)) {
1062 RX_STAT_INC(rx_beacons);
1063 rx_stats->is_mybeacon = true;
1064 }
Sujith Manoharan6f384822013-08-14 09:11:18 +05301065
Sujith Manoharanff9a93f2014-01-09 08:51:14 +05301066 /*
1067 * This shouldn't happen, but have a safety check anyway.
1068 */
Felix Fietkaub7b146c2014-02-24 22:26:06 +01001069 if (WARN_ON(!ah->curchan))
1070 return -EINVAL;
Sujith Manoharanff9a93f2014-01-09 08:51:14 +05301071
Felix Fietkaub7b146c2014-02-24 22:26:06 +01001072 if (ath9k_process_rate(common, hw, rx_stats, rx_status))
1073 return -EINVAL;
Sujithd4357002010-05-20 15:34:38 +05301074
Sujith Manoharane3acd132013-08-14 21:15:54 +05301075 ath9k_process_rssi(common, hw, rx_stats, rx_status);
Sujith Manoharan74a97752013-06-03 09:19:23 +05301076
Sujith Manoharanff9a93f2014-01-09 08:51:14 +05301077 rx_status->band = ah->curchan->chan->band;
1078 rx_status->freq = ah->curchan->chan->center_freq;
Sujithd4357002010-05-20 15:34:38 +05301079 rx_status->antenna = rx_stats->rs_antenna;
Thomas Pedersen96d21372012-12-10 14:48:01 -08001080 rx_status->flag |= RX_FLAG_MACTIME_END;
Sujithd4357002010-05-20 15:34:38 +05301081
Sujith Manoharana5525d92013-08-14 09:11:17 +05301082#ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
1083 if (ieee80211_is_data_present(hdr->frame_control) &&
1084 !ieee80211_is_qos_nullfunc(hdr->frame_control))
1085 sc->rx.num_pkts++;
1086#endif
1087
Felix Fietkaub7b146c2014-02-24 22:26:06 +01001088 return 0;
1089
1090corrupt:
1091 sc->rx.discard_next = rx_stats->rs_more;
1092 return -EINVAL;
Sujithd4357002010-05-20 15:34:38 +05301093}
1094
1095static void ath9k_rx_skb_postprocess(struct ath_common *common,
1096 struct sk_buff *skb,
1097 struct ath_rx_status *rx_stats,
1098 struct ieee80211_rx_status *rxs,
1099 bool decrypt_error)
1100{
1101 struct ath_hw *ah = common->ah;
1102 struct ieee80211_hdr *hdr;
1103 int hdrlen, padpos, padsize;
1104 u8 keyix;
1105 __le16 fc;
1106
1107 /* see if any padding is done by the hw and remove it */
1108 hdr = (struct ieee80211_hdr *) skb->data;
1109 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1110 fc = hdr->frame_control;
Felix Fietkauc60c9922013-04-08 00:04:09 +02001111 padpos = ieee80211_hdrlen(fc);
Sujithd4357002010-05-20 15:34:38 +05301112
1113 /* The MAC header is padded to have 32-bit boundary if the
1114 * packet payload is non-zero. The general calculation for
1115 * padsize would take into account odd header lengths:
1116 * padsize = (4 - padpos % 4) % 4; However, since only
1117 * even-length headers are used, padding can only be 0 or 2
1118 * bytes and we can optimize this a bit. In addition, we must
1119 * not try to remove padding from short control frames that do
1120 * not have payload. */
1121 padsize = padpos & 3;
1122 if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1123 memmove(skb->data + padsize, skb->data, padpos);
1124 skb_pull(skb, padsize);
1125 }
1126
1127 keyix = rx_stats->rs_keyix;
1128
1129 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1130 ieee80211_has_protected(fc)) {
1131 rxs->flag |= RX_FLAG_DECRYPTED;
1132 } else if (ieee80211_has_protected(fc)
1133 && !decrypt_error && skb->len >= hdrlen + 4) {
1134 keyix = skb->data[hdrlen + 3] >> 6;
1135
1136 if (test_bit(keyix, common->keymap))
1137 rxs->flag |= RX_FLAG_DECRYPTED;
1138 }
1139 if (ah->sw_mgmt_crypto &&
1140 (rxs->flag & RX_FLAG_DECRYPTED) &&
1141 ieee80211_is_mgmt(fc))
1142 /* Use software decrypt for management frames. */
1143 rxs->flag &= ~RX_FLAG_DECRYPTED;
1144}
Felix Fietkaub5c804752010-04-15 17:38:48 -04001145
Sujith Manoharanc3124df2013-08-14 21:15:56 +05301146/*
1147 * Run the LNA combining algorithm only in these cases:
1148 *
1149 * Standalone WLAN cards with both LNA/Antenna diversity
1150 * enabled in the EEPROM.
1151 *
1152 * WLAN+BT cards which are in the supported card list
1153 * in ath_pci_id_table and the user has loaded the
1154 * driver with "bt_ant_diversity" set to true.
1155 */
1156static void ath9k_antenna_check(struct ath_softc *sc,
1157 struct ath_rx_status *rs)
1158{
1159 struct ath_hw *ah = sc->sc_ah;
1160 struct ath9k_hw_capabilities *pCap = &ah->caps;
1161 struct ath_common *common = ath9k_hw_common(ah);
1162
1163 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB))
1164 return;
1165
1166 /*
Sujith Manoharanc3124df2013-08-14 21:15:56 +05301167 * Change the default rx antenna if rx diversity
1168 * chooses the other antenna 3 times in a row.
1169 */
1170 if (sc->rx.defant != rs->rs_antenna) {
1171 if (++sc->rx.rxotherant >= 3)
1172 ath_setdefantenna(sc, rs->rs_antenna);
1173 } else {
1174 sc->rx.rxotherant = 0;
1175 }
1176
1177 if (pCap->hw_caps & ATH9K_HW_CAP_BT_ANT_DIV) {
1178 if (common->bt_ant_diversity)
1179 ath_ant_comb_scan(sc, rs);
1180 } else {
1181 ath_ant_comb_scan(sc, rs);
1182 }
1183}
1184
Christian Lamparter21fbbca2013-01-30 23:37:41 +01001185static void ath9k_apply_ampdu_details(struct ath_softc *sc,
1186 struct ath_rx_status *rs, struct ieee80211_rx_status *rxs)
1187{
1188 if (rs->rs_isaggr) {
1189 rxs->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
1190
1191 rxs->ampdu_reference = sc->rx.ampdu_ref;
1192
1193 if (!rs->rs_moreaggr) {
1194 rxs->flag |= RX_FLAG_AMPDU_IS_LAST;
1195 sc->rx.ampdu_ref++;
1196 }
1197
1198 if (rs->rs_flags & ATH9K_RX_DELIM_CRC_PRE)
1199 rxs->flag |= RX_FLAG_AMPDU_DELIM_CRC_ERROR;
1200 }
1201}
1202
Felix Fietkaub5c804752010-04-15 17:38:48 -04001203int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1204{
Felix Fietkau1a04d592013-10-11 23:30:52 +02001205 struct ath_rxbuf *bf;
Felix Fietkau0d955212011-01-26 18:23:27 +01001206 struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001207 struct ieee80211_rx_status *rxs;
Sujithcbe61d82009-02-09 13:27:12 +05301208 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -07001209 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau7545daf2011-01-24 19:23:16 +01001210 struct ieee80211_hw *hw = sc->hw;
Luis R. Rodriguezc9b14172009-11-04 16:47:22 -08001211 int retval;
Felix Fietkau29bffa92010-03-29 20:14:23 -07001212 struct ath_rx_status rs;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001213 enum ath9k_rx_qtype qtype;
1214 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1215 int dma_type;
Felix Fietkaua6d20552010-06-12 00:33:54 -04001216 u64 tsf = 0;
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001217 unsigned long flags;
Felix Fietkau2e1cd492013-04-08 00:04:10 +02001218 dma_addr_t new_buf_addr;
Sujithbe0418a2008-11-18 09:05:55 +05301219
Felix Fietkaub5c804752010-04-15 17:38:48 -04001220 if (edma)
Felix Fietkaub5c804752010-04-15 17:38:48 -04001221 dma_type = DMA_BIDIRECTIONAL;
Ming Lei56824222010-05-14 21:15:38 +08001222 else
1223 dma_type = DMA_FROM_DEVICE;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001224
1225 qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001226
Felix Fietkaua6d20552010-06-12 00:33:54 -04001227 tsf = ath9k_hw_gettsf64(ah);
Felix Fietkaua6d20552010-06-12 00:33:54 -04001228
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001229 do {
Lorenzo Bianconie1352fd2012-08-10 11:00:24 +02001230 bool decrypt_error = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001231
Felix Fietkau29bffa92010-03-29 20:14:23 -07001232 memset(&rs, 0, sizeof(rs));
Felix Fietkaub5c804752010-04-15 17:38:48 -04001233 if (edma)
1234 bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1235 else
1236 bf = ath_get_next_rx_buf(sc, &rs);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001237
Felix Fietkaub5c804752010-04-15 17:38:48 -04001238 if (!bf)
1239 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001240
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001241 skb = bf->bf_mpdu;
Sujithbe0418a2008-11-18 09:05:55 +05301242 if (!skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001243 continue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001244
Felix Fietkau0d955212011-01-26 18:23:27 +01001245 /*
1246 * Take frame header from the first fragment and RX status from
1247 * the last one.
1248 */
1249 if (sc->rx.frag)
1250 hdr_skb = sc->rx.frag;
1251 else
1252 hdr_skb = skb;
1253
Sujith Manoharanf6307dd2013-08-14 09:11:09 +05301254 rxs = IEEE80211_SKB_RXCB(hdr_skb);
Ashok Nagarajanffb1c562012-03-09 18:57:39 -08001255 memset(rxs, 0, sizeof(struct ieee80211_rx_status));
1256
Sujith Manoharan6f384822013-08-14 09:11:18 +05301257 retval = ath9k_rx_skb_preprocess(sc, hdr_skb, &rs, rxs,
Sujith Manoharane0dd1a92013-08-14 09:11:13 +05301258 &decrypt_error, tsf);
Zefir Kurtisi83c76572011-11-16 11:09:44 +01001259 if (retval)
1260 goto requeue_drop_frag;
1261
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001262 /* Ensure we always have an skb to requeue once we are done
1263 * processing the current buffer's skb */
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001264 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001265
1266 /* If there is no memory we ignore the current RX'd frame,
1267 * tell hardware it can give us a new frame using the old
Sujithb77f4832008-12-07 21:44:03 +05301268 * skb and put it at the tail of the sc->rx.rxbuf list for
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001269 * processing. */
Ben Greear15072182012-04-03 09:18:59 -07001270 if (!requeue_skb) {
1271 RX_STAT_INC(rx_oom_err);
Felix Fietkau0d955212011-01-26 18:23:27 +01001272 goto requeue_drop_frag;
Ben Greear15072182012-04-03 09:18:59 -07001273 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001274
Felix Fietkau2e1cd492013-04-08 00:04:10 +02001275 /* We will now give hardware our shiny new allocated skb */
1276 new_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
1277 common->rx_bufsize, dma_type);
1278 if (unlikely(dma_mapping_error(sc->dev, new_buf_addr))) {
1279 dev_kfree_skb_any(requeue_skb);
1280 goto requeue_drop_frag;
1281 }
1282
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301283 /* Unmap the frame */
Gabor Juhos7da3c552009-01-14 20:17:03 +01001284 dma_unmap_single(sc->dev, bf->bf_buf_addr,
Felix Fietkau2e1cd492013-04-08 00:04:10 +02001285 common->rx_bufsize, dma_type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001286
Sujith Manoharan176f0e82013-04-23 12:22:19 +05301287 bf->bf_mpdu = requeue_skb;
1288 bf->bf_buf_addr = new_buf_addr;
1289
Felix Fietkaub5c804752010-04-15 17:38:48 -04001290 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1291 if (ah->caps.rx_status_len)
1292 skb_pull(skb, ah->caps.rx_status_len);
Sujithbe0418a2008-11-18 09:05:55 +05301293
Felix Fietkau0d955212011-01-26 18:23:27 +01001294 if (!rs.rs_more)
1295 ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1296 rxs, decrypt_error);
Sujithbe0418a2008-11-18 09:05:55 +05301297
Felix Fietkau0d955212011-01-26 18:23:27 +01001298 if (rs.rs_more) {
Ben Greear15072182012-04-03 09:18:59 -07001299 RX_STAT_INC(rx_frags);
Felix Fietkau0d955212011-01-26 18:23:27 +01001300 /*
1301 * rs_more indicates chained descriptors which can be
1302 * used to link buffers together for a sort of
1303 * scatter-gather operation.
1304 */
1305 if (sc->rx.frag) {
1306 /* too many fragments - cannot handle frame */
1307 dev_kfree_skb_any(sc->rx.frag);
1308 dev_kfree_skb_any(skb);
Ben Greear15072182012-04-03 09:18:59 -07001309 RX_STAT_INC(rx_too_many_frags_err);
Felix Fietkau0d955212011-01-26 18:23:27 +01001310 skb = NULL;
1311 }
1312 sc->rx.frag = skb;
1313 goto requeue;
1314 }
1315
1316 if (sc->rx.frag) {
1317 int space = skb->len - skb_tailroom(hdr_skb);
1318
Felix Fietkau0d955212011-01-26 18:23:27 +01001319 if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
1320 dev_kfree_skb(skb);
Ben Greear15072182012-04-03 09:18:59 -07001321 RX_STAT_INC(rx_oom_err);
Felix Fietkau0d955212011-01-26 18:23:27 +01001322 goto requeue_drop_frag;
1323 }
1324
Eric Dumazetb5447ff2012-03-15 13:43:29 -07001325 sc->rx.frag = NULL;
1326
Felix Fietkau0d955212011-01-26 18:23:27 +01001327 skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
1328 skb->len);
1329 dev_kfree_skb_any(skb);
1330 skb = hdr_skb;
1331 }
1332
Felix Fietkau66760ea2011-07-13 23:35:05 +08001333 if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
1334 skb_trim(skb, skb->len - 8);
1335
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001336 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Mohammed Shafi Shajakhanaaef24b2010-12-07 20:40:58 +05301337 if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
Rajkumar Manoharanf73c6042011-09-26 22:16:56 +05301338 PS_WAIT_FOR_CAB |
1339 PS_WAIT_FOR_PSPOLL_DATA)) ||
1340 ath9k_check_auto_sleep(sc))
1341 ath_rx_ps(sc, skb, rs.is_mybeacon);
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001342 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Jouni Malinencc659652009-05-14 21:28:48 +03001343
Sujith Manoharanc3124df2013-08-14 21:15:56 +05301344 ath9k_antenna_check(sc, &rs);
Christian Lamparter21fbbca2013-01-30 23:37:41 +01001345 ath9k_apply_ampdu_details(sc, &rs, rxs);
Sujith Manoharan350e2dc2014-01-13 07:29:30 +05301346 ath_debug_rate_stats(sc, &rs, skb);
Christian Lamparter21fbbca2013-01-30 23:37:41 +01001347
Felix Fietkau7545daf2011-01-24 19:23:16 +01001348 ieee80211_rx(hw, skb);
Jouni Malinencc659652009-05-14 21:28:48 +03001349
Felix Fietkau0d955212011-01-26 18:23:27 +01001350requeue_drop_frag:
1351 if (sc->rx.frag) {
1352 dev_kfree_skb_any(sc->rx.frag);
1353 sc->rx.frag = NULL;
1354 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001355requeue:
Felix Fietkaua3dc48e2013-01-09 16:16:52 +01001356 list_add_tail(&bf->list, &sc->rx.rxbuf);
1357 if (flush)
1358 continue;
1359
Felix Fietkaub5c804752010-04-15 17:38:48 -04001360 if (edma) {
Felix Fietkaub5c804752010-04-15 17:38:48 -04001361 ath_rx_edma_buf_link(sc, qtype);
1362 } else {
Felix Fietkaue96542e2013-08-10 15:59:15 +02001363 ath_rx_buf_relink(sc, bf);
Felix Fietkaua3dc48e2013-01-09 16:16:52 +01001364 ath9k_hw_rxena(ah);
Felix Fietkaub5c804752010-04-15 17:38:48 -04001365 }
Sujithbe0418a2008-11-18 09:05:55 +05301366 } while (1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001367
Rajkumar Manoharan29ab0b32011-08-13 10:28:10 +05301368 if (!(ah->imask & ATH9K_INT_RXEOL)) {
1369 ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
Felix Fietkau72d874c2011-10-08 20:06:19 +02001370 ath9k_hw_set_interrupts(ah);
Rajkumar Manoharan29ab0b32011-08-13 10:28:10 +05301371 }
1372
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001373 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001374}