blob: 4f52e0429f996718ad1630a597541f803f256079 [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
Sujithcee075a2009-03-13 09:07:23 +05302 * Copyright (c) 2008-2009 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
Sujith394cf0a2009-02-09 13:26:54 +053017#include "ath9k.h"
Luis R. Rodriguezb622a722010-04-15 17:39:28 -040018#include "ar9003_mac.h"
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070019
Felix Fietkaub5c804752010-04-15 17:38:48 -040020#define SKB_CB_ATHBUF(__skb) (*((struct ath_buf **)__skb->cb))
21
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -070022static inline bool ath_is_alt_ant_ratio_better(int alt_ratio, int maxdelta,
23 int mindelta, int main_rssi_avg,
24 int alt_rssi_avg, int pkt_count)
25{
26 return (((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
27 (alt_rssi_avg > main_rssi_avg + maxdelta)) ||
28 (alt_rssi_avg > main_rssi_avg + mindelta)) && (pkt_count > 50);
29}
30
Mohammed Shafi Shajakhanb85c5732011-05-13 20:31:09 +053031static inline bool ath_ant_div_comb_alt_check(u8 div_group, int alt_ratio,
32 int curr_main_set, int curr_alt_set,
33 int alt_rssi_avg, int main_rssi_avg)
34{
35 bool result = false;
36 switch (div_group) {
37 case 0:
38 if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
39 result = true;
40 break;
41 case 1:
42 if ((((curr_main_set == ATH_ANT_DIV_COMB_LNA2) &&
43 (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) &&
44 (alt_rssi_avg >= (main_rssi_avg - 5))) ||
45 ((curr_main_set == ATH_ANT_DIV_COMB_LNA1) &&
46 (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) &&
47 (alt_rssi_avg >= (main_rssi_avg - 2)))) &&
48 (alt_rssi_avg >= 4))
49 result = true;
50 else
51 result = false;
52 break;
53 }
54
55 return result;
56}
57
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -070058static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
59{
60 return sc->ps_enabled &&
61 (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
62}
63
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070064/*
65 * Setup and link descriptors.
66 *
67 * 11N: we can no longer afford to self link the last descriptor.
68 * MAC acknowledges BA status as long as it copies frames to host
69 * buffer (or rx fifo). This can incorrectly acknowledge packets
70 * to a sender if last desc is self-linked.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070071 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070072static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
73{
Sujithcbe61d82009-02-09 13:27:12 +053074 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080075 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070076 struct ath_desc *ds;
77 struct sk_buff *skb;
78
79 ATH_RXBUF_RESET(bf);
80
81 ds = bf->bf_desc;
Sujithbe0418a2008-11-18 09:05:55 +053082 ds->ds_link = 0; /* link to null */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070083 ds->ds_data = bf->bf_buf_addr;
84
Sujithbe0418a2008-11-18 09:05:55 +053085 /* virtual addr of the beginning of the buffer. */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070086 skb = bf->bf_mpdu;
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -070087 BUG_ON(skb == NULL);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070088 ds->ds_vdata = skb->data;
89
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080090 /*
91 * setup rx descriptors. The rx_bufsize here tells the hardware
Luis R. Rodriguezb4b6cda2008-11-20 17:15:13 -080092 * how much data it can DMA to us and that we are prepared
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080093 * to process
94 */
Sujithb77f4832008-12-07 21:44:03 +053095 ath9k_hw_setuprxdesc(ah, ds,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080096 common->rx_bufsize,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070097 0);
98
Sujithb77f4832008-12-07 21:44:03 +053099 if (sc->rx.rxlink == NULL)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700100 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
101 else
Sujithb77f4832008-12-07 21:44:03 +0530102 *sc->rx.rxlink = bf->bf_daddr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700103
Sujithb77f4832008-12-07 21:44:03 +0530104 sc->rx.rxlink = &ds->ds_link;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700105}
106
Sujithff37e332008-11-24 12:07:55 +0530107static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
108{
109 /* XXX block beacon interrupts */
110 ath9k_hw_setantenna(sc->sc_ah, antenna);
Sujithb77f4832008-12-07 21:44:03 +0530111 sc->rx.defant = antenna;
112 sc->rx.rxotherant = 0;
Sujithff37e332008-11-24 12:07:55 +0530113}
114
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700115static void ath_opmode_init(struct ath_softc *sc)
116{
Sujithcbe61d82009-02-09 13:27:12 +0530117 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700118 struct ath_common *common = ath9k_hw_common(ah);
119
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700120 u32 rfilt, mfilt[2];
121
122 /* configure rx filter */
123 rfilt = ath_calcrxfilter(sc);
124 ath9k_hw_setrxfilter(ah, rfilt);
125
126 /* configure bssid mask */
Felix Fietkau364734f2010-09-14 20:22:44 +0200127 ath_hw_setbssidmask(common);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700128
129 /* configure operational mode */
130 ath9k_hw_setopmode(ah);
131
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700132 /* calculate and install multicast filter */
133 mfilt[0] = mfilt[1] = ~0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700134 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700135}
136
Felix Fietkaub5c804752010-04-15 17:38:48 -0400137static bool ath_rx_edma_buf_link(struct ath_softc *sc,
138 enum ath9k_rx_qtype qtype)
139{
140 struct ath_hw *ah = sc->sc_ah;
141 struct ath_rx_edma *rx_edma;
142 struct sk_buff *skb;
143 struct ath_buf *bf;
144
145 rx_edma = &sc->rx.rx_edma[qtype];
146 if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
147 return false;
148
149 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
150 list_del_init(&bf->list);
151
152 skb = bf->bf_mpdu;
153
154 ATH_RXBUF_RESET(bf);
155 memset(skb->data, 0, ah->caps.rx_status_len);
156 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
157 ah->caps.rx_status_len, DMA_TO_DEVICE);
158
159 SKB_CB_ATHBUF(skb) = bf;
160 ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
161 skb_queue_tail(&rx_edma->rx_fifo, skb);
162
163 return true;
164}
165
166static void ath_rx_addbuffer_edma(struct ath_softc *sc,
167 enum ath9k_rx_qtype qtype, int size)
168{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400169 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
170 u32 nbuf = 0;
171
Felix Fietkaub5c804752010-04-15 17:38:48 -0400172 if (list_empty(&sc->rx.rxbuf)) {
Joe Perches226afe62010-12-02 19:12:37 -0800173 ath_dbg(common, ATH_DBG_QUEUE, "No free rx buf available\n");
Felix Fietkaub5c804752010-04-15 17:38:48 -0400174 return;
175 }
176
177 while (!list_empty(&sc->rx.rxbuf)) {
178 nbuf++;
179
180 if (!ath_rx_edma_buf_link(sc, qtype))
181 break;
182
183 if (nbuf >= size)
184 break;
185 }
186}
187
188static void ath_rx_remove_buffer(struct ath_softc *sc,
189 enum ath9k_rx_qtype qtype)
190{
191 struct ath_buf *bf;
192 struct ath_rx_edma *rx_edma;
193 struct sk_buff *skb;
194
195 rx_edma = &sc->rx.rx_edma[qtype];
196
197 while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
198 bf = SKB_CB_ATHBUF(skb);
199 BUG_ON(!bf);
200 list_add_tail(&bf->list, &sc->rx.rxbuf);
201 }
202}
203
204static void ath_rx_edma_cleanup(struct ath_softc *sc)
205{
206 struct ath_buf *bf;
207
208 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
209 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
210
211 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
212 if (bf->bf_mpdu)
213 dev_kfree_skb_any(bf->bf_mpdu);
214 }
215
216 INIT_LIST_HEAD(&sc->rx.rxbuf);
217
218 kfree(sc->rx.rx_bufptr);
219 sc->rx.rx_bufptr = NULL;
220}
221
222static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
223{
224 skb_queue_head_init(&rx_edma->rx_fifo);
225 skb_queue_head_init(&rx_edma->rx_buffers);
226 rx_edma->rx_fifo_hwsize = size;
227}
228
229static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
230{
231 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
232 struct ath_hw *ah = sc->sc_ah;
233 struct sk_buff *skb;
234 struct ath_buf *bf;
235 int error = 0, i;
236 u32 size;
237
Felix Fietkaub5c804752010-04-15 17:38:48 -0400238 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
239 ah->caps.rx_status_len);
240
241 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
242 ah->caps.rx_lp_qdepth);
243 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
244 ah->caps.rx_hp_qdepth);
245
246 size = sizeof(struct ath_buf) * nbufs;
247 bf = kzalloc(size, GFP_KERNEL);
248 if (!bf)
249 return -ENOMEM;
250
251 INIT_LIST_HEAD(&sc->rx.rxbuf);
252 sc->rx.rx_bufptr = bf;
253
254 for (i = 0; i < nbufs; i++, bf++) {
255 skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
256 if (!skb) {
257 error = -ENOMEM;
258 goto rx_init_fail;
259 }
260
261 memset(skb->data, 0, common->rx_bufsize);
262 bf->bf_mpdu = skb;
263
264 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
265 common->rx_bufsize,
266 DMA_BIDIRECTIONAL);
267 if (unlikely(dma_mapping_error(sc->dev,
268 bf->bf_buf_addr))) {
269 dev_kfree_skb_any(skb);
270 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -0700271 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -0800272 ath_err(common,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400273 "dma_mapping_error() on RX init\n");
274 error = -ENOMEM;
275 goto rx_init_fail;
276 }
277
278 list_add_tail(&bf->list, &sc->rx.rxbuf);
279 }
280
281 return 0;
282
283rx_init_fail:
284 ath_rx_edma_cleanup(sc);
285 return error;
286}
287
288static void ath_edma_start_recv(struct ath_softc *sc)
289{
290 spin_lock_bh(&sc->rx.rxbuflock);
291
292 ath9k_hw_rxena(sc->sc_ah);
293
294 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
295 sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
296
297 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
298 sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
299
Felix Fietkaub5c804752010-04-15 17:38:48 -0400300 ath_opmode_init(sc);
301
Luis R. Rodriguez48a6a462010-09-16 15:12:28 -0400302 ath9k_hw_startpcureceive(sc->sc_ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
Luis R. Rodriguez7583c5502010-10-20 16:07:04 -0700303
304 spin_unlock_bh(&sc->rx.rxbuflock);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400305}
306
307static void ath_edma_stop_recv(struct ath_softc *sc)
308{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400309 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
310 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400311}
312
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700313int ath_rx_init(struct ath_softc *sc, int nbufs)
314{
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -0700315 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700316 struct sk_buff *skb;
317 struct ath_buf *bf;
318 int error = 0;
319
Luis R. Rodriguez4bdd1e92010-10-26 15:27:24 -0700320 spin_lock_init(&sc->sc_pcu_lock);
Sujith797fe5cb2009-03-30 15:28:45 +0530321 sc->sc_flags &= ~SC_OP_RXFLUSH;
322 spin_lock_init(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700323
Felix Fietkau0d955212011-01-26 18:23:27 +0100324 common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
325 sc->sc_ah->caps.rx_status_len;
326
Felix Fietkaub5c804752010-04-15 17:38:48 -0400327 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
328 return ath_rx_edma_init(sc, nbufs);
329 } else {
Joe Perches226afe62010-12-02 19:12:37 -0800330 ath_dbg(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
331 common->cachelsz, common->rx_bufsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700332
Felix Fietkaub5c804752010-04-15 17:38:48 -0400333 /* Initialize rx descriptors */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700334
Felix Fietkaub5c804752010-04-15 17:38:48 -0400335 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
Vasanthakumar Thiagarajan4adfcde2010-04-15 17:39:33 -0400336 "rx", nbufs, 1, 0);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400337 if (error != 0) {
Joe Perches38002762010-12-02 19:12:36 -0800338 ath_err(common,
339 "failed to allocate rx descriptors: %d\n",
340 error);
Sujith797fe5cb2009-03-30 15:28:45 +0530341 goto err;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700342 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400343
344 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
345 skb = ath_rxbuf_alloc(common, common->rx_bufsize,
346 GFP_KERNEL);
347 if (skb == NULL) {
348 error = -ENOMEM;
349 goto err;
350 }
351
352 bf->bf_mpdu = skb;
353 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
354 common->rx_bufsize,
355 DMA_FROM_DEVICE);
356 if (unlikely(dma_mapping_error(sc->dev,
357 bf->bf_buf_addr))) {
358 dev_kfree_skb_any(skb);
359 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -0700360 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -0800361 ath_err(common,
362 "dma_mapping_error() on RX init\n");
Felix Fietkaub5c804752010-04-15 17:38:48 -0400363 error = -ENOMEM;
364 goto err;
365 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400366 }
367 sc->rx.rxlink = NULL;
Sujith797fe5cb2009-03-30 15:28:45 +0530368 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700369
Sujith797fe5cb2009-03-30 15:28:45 +0530370err:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700371 if (error)
372 ath_rx_cleanup(sc);
373
374 return error;
375}
376
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700377void ath_rx_cleanup(struct ath_softc *sc)
378{
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800379 struct ath_hw *ah = sc->sc_ah;
380 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700381 struct sk_buff *skb;
382 struct ath_buf *bf;
383
Felix Fietkaub5c804752010-04-15 17:38:48 -0400384 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
385 ath_rx_edma_cleanup(sc);
386 return;
387 } else {
388 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
389 skb = bf->bf_mpdu;
390 if (skb) {
391 dma_unmap_single(sc->dev, bf->bf_buf_addr,
392 common->rx_bufsize,
393 DMA_FROM_DEVICE);
394 dev_kfree_skb(skb);
Ben Greear6cf9e992010-10-14 12:45:30 -0700395 bf->bf_buf_addr = 0;
396 bf->bf_mpdu = NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400397 }
Luis R. Rodriguez051b9192009-03-23 18:25:01 -0400398 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700399
Felix Fietkaub5c804752010-04-15 17:38:48 -0400400 if (sc->rx.rxdma.dd_desc_len != 0)
401 ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
402 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700403}
404
405/*
406 * Calculate the receive filter according to the
407 * operating mode and state:
408 *
409 * o always accept unicast, broadcast, and multicast traffic
410 * o maintain current state of phy error reception (the hal
411 * may enable phy error frames for noise immunity work)
412 * o probe request frames are accepted only when operating in
413 * hostap, adhoc, or monitor modes
414 * o enable promiscuous mode according to the interface state
415 * o accept beacons:
416 * - when operating in adhoc mode so the 802.11 layer creates
417 * node table entries for peers,
418 * - when operating in station mode for collecting rssi data when
419 * the station is otherwise quiet, or
420 * - when operating as a repeater so we see repeater-sta beacons
421 * - when scanning
422 */
423
424u32 ath_calcrxfilter(struct ath_softc *sc)
425{
426#define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
Sujith7dcfdcd2008-08-11 14:03:13 +0530427
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700428 u32 rfilt;
429
430 rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
431 | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
432 | ATH9K_RX_FILTER_MCAST;
433
Jouni Malinen9c1d8e42010-10-13 17:29:31 +0300434 if (sc->rx.rxfilter & FIF_PROBE_REQ)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700435 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
436
Jouni Malinen217ba9d2009-03-10 10:55:50 +0200437 /*
438 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
439 * mode interface or when in monitor mode. AP mode does not need this
440 * since it receives all in-BSS frames anyway.
441 */
Felix Fietkau2e286942011-03-09 01:48:12 +0100442 if (sc->sc_ah->is_monitoring)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700443 rfilt |= ATH9K_RX_FILTER_PROM;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700444
Sujithd42c6b72009-02-04 08:10:22 +0530445 if (sc->rx.rxfilter & FIF_CONTROL)
446 rfilt |= ATH9K_RX_FILTER_CONTROL;
447
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530448 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
Ben Greearcfda6692010-09-14 12:00:22 -0700449 (sc->nvifs <= 1) &&
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530450 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
451 rfilt |= ATH9K_RX_FILTER_MYBEACON;
452 else
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700453 rfilt |= ATH9K_RX_FILTER_BEACON;
454
Felix Fietkau264bbec2011-04-07 19:24:23 +0200455 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
Senthil Balasubramanian66afad02009-09-18 15:06:07 +0530456 (sc->rx.rxfilter & FIF_PSPOLL))
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530457 rfilt |= ATH9K_RX_FILTER_PSPOLL;
Sujithbe0418a2008-11-18 09:05:55 +0530458
Sujith7ea310b2009-09-03 12:08:43 +0530459 if (conf_is_ht(&sc->hw->conf))
460 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
461
Felix Fietkau7545daf2011-01-24 19:23:16 +0100462 if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
Javier Cardona5eb6ba82009-08-20 19:12:07 -0700463 /* The following may also be needed for other older chips */
464 if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
465 rfilt |= ATH9K_RX_FILTER_PROM;
Jouni Malinenb93bce22009-03-03 19:23:30 +0200466 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
467 }
468
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700469 return rfilt;
Sujith7dcfdcd2008-08-11 14:03:13 +0530470
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700471#undef RX_FILTER_PRESERVE
472}
473
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700474int ath_startrecv(struct ath_softc *sc)
475{
Sujithcbe61d82009-02-09 13:27:12 +0530476 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700477 struct ath_buf *bf, *tbf;
478
Felix Fietkaub5c804752010-04-15 17:38:48 -0400479 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
480 ath_edma_start_recv(sc);
481 return 0;
482 }
483
Sujithb77f4832008-12-07 21:44:03 +0530484 spin_lock_bh(&sc->rx.rxbuflock);
485 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700486 goto start_recv;
487
Sujithb77f4832008-12-07 21:44:03 +0530488 sc->rx.rxlink = NULL;
489 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700490 ath_rx_buf_link(sc, bf);
491 }
492
493 /* We could have deleted elements so the list may be empty now */
Sujithb77f4832008-12-07 21:44:03 +0530494 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700495 goto start_recv;
496
Sujithb77f4832008-12-07 21:44:03 +0530497 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700498 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
Sujithbe0418a2008-11-18 09:05:55 +0530499 ath9k_hw_rxena(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700500
501start_recv:
Sujithbe0418a2008-11-18 09:05:55 +0530502 ath_opmode_init(sc);
Luis R. Rodriguez48a6a462010-09-16 15:12:28 -0400503 ath9k_hw_startpcureceive(ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
Sujithbe0418a2008-11-18 09:05:55 +0530504
Luis R. Rodriguez7583c5502010-10-20 16:07:04 -0700505 spin_unlock_bh(&sc->rx.rxbuflock);
506
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700507 return 0;
508}
509
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700510bool ath_stoprecv(struct ath_softc *sc)
511{
Sujithcbe61d82009-02-09 13:27:12 +0530512 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau5882da022011-04-08 20:13:18 +0200513 bool stopped, reset = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700514
Luis R. Rodriguez1e450282010-10-20 16:07:03 -0700515 spin_lock_bh(&sc->rx.rxbuflock);
Felix Fietkaud47844a2010-11-20 03:08:47 +0100516 ath9k_hw_abortpcurecv(ah);
Sujithbe0418a2008-11-18 09:05:55 +0530517 ath9k_hw_setrxfilter(ah, 0);
Felix Fietkau5882da022011-04-08 20:13:18 +0200518 stopped = ath9k_hw_stopdmarecv(ah, &reset);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400519
520 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
521 ath_edma_stop_recv(sc);
522 else
523 sc->rx.rxlink = NULL;
Luis R. Rodriguez1e450282010-10-20 16:07:03 -0700524 spin_unlock_bh(&sc->rx.rxbuflock);
Sujithbe0418a2008-11-18 09:05:55 +0530525
Rajkumar Manoharand5847472010-12-20 14:39:51 +0530526 if (!(ah->ah_flags & AH_UNPLUGGED) &&
527 unlikely(!stopped)) {
Ben Greeard7fd1b502010-12-06 13:13:07 -0800528 ath_err(ath9k_hw_common(sc->sc_ah),
529 "Could not stop RX, we could be "
530 "confusing the DMA engine when we start RX up\n");
531 ATH_DBG_WARN_ON_ONCE(!stopped);
532 }
Felix Fietkau2232d312011-04-15 00:41:43 +0200533 return stopped && !reset;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700534}
535
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700536void ath_flushrecv(struct ath_softc *sc)
537{
Sujith98deeea2008-08-11 14:05:46 +0530538 sc->sc_flags |= SC_OP_RXFLUSH;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400539 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
540 ath_rx_tasklet(sc, 1, true);
541 ath_rx_tasklet(sc, 1, false);
Sujith98deeea2008-08-11 14:05:46 +0530542 sc->sc_flags &= ~SC_OP_RXFLUSH;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700543}
544
Jouni Malinencc659652009-05-14 21:28:48 +0300545static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
546{
547 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
548 struct ieee80211_mgmt *mgmt;
549 u8 *pos, *end, id, elen;
550 struct ieee80211_tim_ie *tim;
551
552 mgmt = (struct ieee80211_mgmt *)skb->data;
553 pos = mgmt->u.beacon.variable;
554 end = skb->data + skb->len;
555
556 while (pos + 2 < end) {
557 id = *pos++;
558 elen = *pos++;
559 if (pos + elen > end)
560 break;
561
562 if (id == WLAN_EID_TIM) {
563 if (elen < sizeof(*tim))
564 break;
565 tim = (struct ieee80211_tim_ie *) pos;
566 if (tim->dtim_count != 0)
567 break;
568 return tim->bitmap_ctrl & 0x01;
569 }
570
571 pos += elen;
572 }
573
574 return false;
575}
576
Jouni Malinencc659652009-05-14 21:28:48 +0300577static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
578{
579 struct ieee80211_mgmt *mgmt;
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700580 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300581
582 if (skb->len < 24 + 8 + 2 + 2)
583 return;
584
585 mgmt = (struct ieee80211_mgmt *)skb->data;
Ben Greear48014162011-01-15 19:13:48 +0000586 if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0) {
587 /* TODO: This doesn't work well if you have stations
588 * associated to two different APs because curbssid
589 * is just the last AP that any of the stations associated
590 * with.
591 */
Jouni Malinencc659652009-05-14 21:28:48 +0300592 return; /* not from our current AP */
Ben Greear48014162011-01-15 19:13:48 +0000593 }
Jouni Malinencc659652009-05-14 21:28:48 +0300594
Sujith1b04b932010-01-08 10:36:05 +0530595 sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
Gabor Juhos293dc5d2009-06-19 12:17:48 +0200596
Sujith1b04b932010-01-08 10:36:05 +0530597 if (sc->ps_flags & PS_BEACON_SYNC) {
598 sc->ps_flags &= ~PS_BEACON_SYNC;
Joe Perches226afe62010-12-02 19:12:37 -0800599 ath_dbg(common, ATH_DBG_PS,
600 "Reconfigure Beacon timers based on timestamp from the AP\n");
Rajkumar Manoharan99e4d432011-04-04 22:56:19 +0530601 ath_set_beacon(sc);
Rajkumar Manoharandeb75182011-05-06 18:27:46 +0530602 sc->ps_flags &= ~PS_TSFOOR_SYNC;
Jouni Malinenccdfeab2009-05-20 21:59:08 +0300603 }
604
Jouni Malinencc659652009-05-14 21:28:48 +0300605 if (ath_beacon_dtim_pending_cab(skb)) {
606 /*
607 * Remain awake waiting for buffered broadcast/multicast
Gabor Juhos58f5fff2009-06-17 20:53:20 +0200608 * frames. If the last broadcast/multicast frame is not
609 * received properly, the next beacon frame will work as
610 * a backup trigger for returning into NETWORK SLEEP state,
611 * so we are waiting for it as well.
Jouni Malinencc659652009-05-14 21:28:48 +0300612 */
Joe Perches226afe62010-12-02 19:12:37 -0800613 ath_dbg(common, ATH_DBG_PS,
614 "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
Sujith1b04b932010-01-08 10:36:05 +0530615 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
Jouni Malinencc659652009-05-14 21:28:48 +0300616 return;
617 }
618
Sujith1b04b932010-01-08 10:36:05 +0530619 if (sc->ps_flags & PS_WAIT_FOR_CAB) {
Jouni Malinencc659652009-05-14 21:28:48 +0300620 /*
621 * This can happen if a broadcast frame is dropped or the AP
622 * fails to send a frame indicating that all CAB frames have
623 * been delivered.
624 */
Sujith1b04b932010-01-08 10:36:05 +0530625 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
Joe Perches226afe62010-12-02 19:12:37 -0800626 ath_dbg(common, ATH_DBG_PS,
627 "PS wait for CAB frames timed out\n");
Jouni Malinencc659652009-05-14 21:28:48 +0300628 }
Jouni Malinencc659652009-05-14 21:28:48 +0300629}
630
631static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb)
632{
633 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700634 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300635
636 hdr = (struct ieee80211_hdr *)skb->data;
637
638 /* Process Beacon and CAB receive in PS state */
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -0700639 if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
640 && ieee80211_is_beacon(hdr->frame_control))
Jouni Malinencc659652009-05-14 21:28:48 +0300641 ath_rx_ps_beacon(sc, skb);
Sujith1b04b932010-01-08 10:36:05 +0530642 else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
Jouni Malinencc659652009-05-14 21:28:48 +0300643 (ieee80211_is_data(hdr->frame_control) ||
644 ieee80211_is_action(hdr->frame_control)) &&
645 is_multicast_ether_addr(hdr->addr1) &&
646 !ieee80211_has_moredata(hdr->frame_control)) {
Jouni Malinencc659652009-05-14 21:28:48 +0300647 /*
648 * No more broadcast/multicast frames to be received at this
649 * point.
650 */
Senthil Balasubramanian3fac6df2010-09-16 15:12:35 -0400651 sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
Joe Perches226afe62010-12-02 19:12:37 -0800652 ath_dbg(common, ATH_DBG_PS,
653 "All PS CAB frames received, back to sleep\n");
Sujith1b04b932010-01-08 10:36:05 +0530654 } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300655 !is_multicast_ether_addr(hdr->addr1) &&
656 !ieee80211_has_morefrags(hdr->frame_control)) {
Sujith1b04b932010-01-08 10:36:05 +0530657 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
Joe Perches226afe62010-12-02 19:12:37 -0800658 ath_dbg(common, ATH_DBG_PS,
659 "Going back to sleep after having received PS-Poll data (0x%lx)\n",
Sujith1b04b932010-01-08 10:36:05 +0530660 sc->ps_flags & (PS_WAIT_FOR_BEACON |
661 PS_WAIT_FOR_CAB |
662 PS_WAIT_FOR_PSPOLL_DATA |
663 PS_WAIT_FOR_TX_ACK));
Jouni Malinencc659652009-05-14 21:28:48 +0300664 }
665}
666
Felix Fietkaub5c804752010-04-15 17:38:48 -0400667static bool ath_edma_get_buffers(struct ath_softc *sc,
668 enum ath9k_rx_qtype qtype)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700669{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400670 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
671 struct ath_hw *ah = sc->sc_ah;
672 struct ath_common *common = ath9k_hw_common(ah);
673 struct sk_buff *skb;
Sujithbe0418a2008-11-18 09:05:55 +0530674 struct ath_buf *bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400675 int ret;
676
677 skb = skb_peek(&rx_edma->rx_fifo);
678 if (!skb)
679 return false;
680
681 bf = SKB_CB_ATHBUF(skb);
682 BUG_ON(!bf);
683
Ming Leice9426d2010-05-15 18:25:40 +0800684 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400685 common->rx_bufsize, DMA_FROM_DEVICE);
686
687 ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data);
Ming Leice9426d2010-05-15 18:25:40 +0800688 if (ret == -EINPROGRESS) {
689 /*let device gain the buffer again*/
690 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
691 common->rx_bufsize, DMA_FROM_DEVICE);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400692 return false;
Ming Leice9426d2010-05-15 18:25:40 +0800693 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400694
695 __skb_unlink(skb, &rx_edma->rx_fifo);
696 if (ret == -EINVAL) {
697 /* corrupt descriptor, skip this one and the following one */
698 list_add_tail(&bf->list, &sc->rx.rxbuf);
699 ath_rx_edma_buf_link(sc, qtype);
700 skb = skb_peek(&rx_edma->rx_fifo);
701 if (!skb)
702 return true;
703
704 bf = SKB_CB_ATHBUF(skb);
705 BUG_ON(!bf);
706
707 __skb_unlink(skb, &rx_edma->rx_fifo);
708 list_add_tail(&bf->list, &sc->rx.rxbuf);
709 ath_rx_edma_buf_link(sc, qtype);
Vasanthakumar Thiagarajan083e3e82010-05-10 19:41:34 -0700710 return true;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400711 }
712 skb_queue_tail(&rx_edma->rx_buffers, skb);
713
714 return true;
715}
716
717static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
718 struct ath_rx_status *rs,
719 enum ath9k_rx_qtype qtype)
720{
721 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
722 struct sk_buff *skb;
723 struct ath_buf *bf;
724
725 while (ath_edma_get_buffers(sc, qtype));
726 skb = __skb_dequeue(&rx_edma->rx_buffers);
727 if (!skb)
728 return NULL;
729
730 bf = SKB_CB_ATHBUF(skb);
731 ath9k_hw_process_rxdesc_edma(sc->sc_ah, rs, skb->data);
732 return bf;
733}
734
735static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
736 struct ath_rx_status *rs)
737{
738 struct ath_hw *ah = sc->sc_ah;
739 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700740 struct ath_desc *ds;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400741 struct ath_buf *bf;
742 int ret;
743
744 if (list_empty(&sc->rx.rxbuf)) {
745 sc->rx.rxlink = NULL;
746 return NULL;
747 }
748
749 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
750 ds = bf->bf_desc;
751
752 /*
753 * Must provide the virtual address of the current
754 * descriptor, the physical address, and the virtual
755 * address of the next descriptor in the h/w chain.
756 * This allows the HAL to look ahead to see if the
757 * hardware is done with a descriptor by checking the
758 * done bit in the following descriptor and the address
759 * of the current descriptor the DMA engine is working
760 * on. All this is necessary because of our use of
761 * a self-linked list to avoid rx overruns.
762 */
763 ret = ath9k_hw_rxprocdesc(ah, ds, rs, 0);
764 if (ret == -EINPROGRESS) {
765 struct ath_rx_status trs;
766 struct ath_buf *tbf;
767 struct ath_desc *tds;
768
769 memset(&trs, 0, sizeof(trs));
770 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
771 sc->rx.rxlink = NULL;
772 return NULL;
773 }
774
775 tbf = list_entry(bf->list.next, struct ath_buf, list);
776
777 /*
778 * On some hardware the descriptor status words could
779 * get corrupted, including the done bit. Because of
780 * this, check if the next descriptor's done bit is
781 * set or not.
782 *
783 * If the next descriptor's done bit is set, the current
784 * descriptor has been corrupted. Force s/w to discard
785 * this descriptor and continue...
786 */
787
788 tds = tbf->bf_desc;
789 ret = ath9k_hw_rxprocdesc(ah, tds, &trs, 0);
790 if (ret == -EINPROGRESS)
791 return NULL;
792 }
793
794 if (!bf->bf_mpdu)
795 return bf;
796
797 /*
798 * Synchronize the DMA transfer with CPU before
799 * 1. accessing the frame
800 * 2. requeueing the same buffer to h/w
801 */
Ming Leice9426d2010-05-15 18:25:40 +0800802 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400803 common->rx_bufsize,
804 DMA_FROM_DEVICE);
805
806 return bf;
807}
808
Sujithd4357002010-05-20 15:34:38 +0530809/* Assumes you've already done the endian to CPU conversion */
810static bool ath9k_rx_accept(struct ath_common *common,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700811 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530812 struct ieee80211_rx_status *rxs,
813 struct ath_rx_status *rx_stats,
814 bool *decrypt_error)
815{
Senthil Balasubramanian38852b22010-12-06 19:09:27 +0530816#define is_mc_or_valid_tkip_keyix ((is_mc || \
817 (rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID && \
818 test_bit(rx_stats->rs_keyix, common->tkip_keymap))))
819
Sujithd4357002010-05-20 15:34:38 +0530820 struct ath_hw *ah = common->ah;
Sujithd4357002010-05-20 15:34:38 +0530821 __le16 fc;
Vasanthakumar Thiagarajanb7b1b512010-05-20 14:34:48 -0700822 u8 rx_status_len = ah->caps.rx_status_len;
Sujithd4357002010-05-20 15:34:38 +0530823
Sujithd4357002010-05-20 15:34:38 +0530824 fc = hdr->frame_control;
825
826 if (!rx_stats->rs_datalen)
827 return false;
828 /*
829 * rs_status follows rs_datalen so if rs_datalen is too large
830 * we can take a hint that hardware corrupted it, so ignore
831 * those frames.
832 */
Vasanthakumar Thiagarajanb7b1b512010-05-20 14:34:48 -0700833 if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len))
Sujithd4357002010-05-20 15:34:38 +0530834 return false;
835
Felix Fietkau0d955212011-01-26 18:23:27 +0100836 /* Only use error bits from the last fragment */
Sujithd4357002010-05-20 15:34:38 +0530837 if (rx_stats->rs_more)
Felix Fietkau0d955212011-01-26 18:23:27 +0100838 return true;
Sujithd4357002010-05-20 15:34:38 +0530839
840 /*
841 * The rx_stats->rs_status will not be set until the end of the
842 * chained descriptors so it can be ignored if rs_more is set. The
843 * rs_more will be false at the last element of the chained
844 * descriptors.
845 */
846 if (rx_stats->rs_status != 0) {
847 if (rx_stats->rs_status & ATH9K_RXERR_CRC)
848 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
849 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
850 return false;
851
852 if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
853 *decrypt_error = true;
854 } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
Senthil Balasubramanian38852b22010-12-06 19:09:27 +0530855 bool is_mc;
Felix Fietkau56363dd2010-08-28 18:21:21 +0200856 /*
857 * The MIC error bit is only valid if the frame
858 * is not a control frame or fragment, and it was
859 * decrypted using a valid TKIP key.
860 */
Senthil Balasubramanian38852b22010-12-06 19:09:27 +0530861 is_mc = !!is_multicast_ether_addr(hdr->addr1);
862
Felix Fietkau56363dd2010-08-28 18:21:21 +0200863 if (!ieee80211_is_ctl(fc) &&
864 !ieee80211_has_morefrags(fc) &&
865 !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
Senthil Balasubramanian38852b22010-12-06 19:09:27 +0530866 is_mc_or_valid_tkip_keyix)
Sujithd4357002010-05-20 15:34:38 +0530867 rxs->flag |= RX_FLAG_MMIC_ERROR;
Felix Fietkau56363dd2010-08-28 18:21:21 +0200868 else
869 rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
Sujithd4357002010-05-20 15:34:38 +0530870 }
871 /*
872 * Reject error frames with the exception of
873 * decryption and MIC failures. For monitor mode,
874 * we also ignore the CRC error.
875 */
Rajkumar Manoharan5f841b42010-10-27 18:31:15 +0530876 if (ah->is_monitoring) {
Sujithd4357002010-05-20 15:34:38 +0530877 if (rx_stats->rs_status &
878 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
879 ATH9K_RXERR_CRC))
880 return false;
881 } else {
882 if (rx_stats->rs_status &
883 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
884 return false;
885 }
886 }
887 }
888 return true;
889}
890
891static int ath9k_process_rate(struct ath_common *common,
892 struct ieee80211_hw *hw,
893 struct ath_rx_status *rx_stats,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700894 struct ieee80211_rx_status *rxs)
Sujithd4357002010-05-20 15:34:38 +0530895{
896 struct ieee80211_supported_band *sband;
897 enum ieee80211_band band;
898 unsigned int i = 0;
899
900 band = hw->conf.channel->band;
901 sband = hw->wiphy->bands[band];
902
903 if (rx_stats->rs_rate & 0x80) {
904 /* HT rate */
905 rxs->flag |= RX_FLAG_HT;
906 if (rx_stats->rs_flags & ATH9K_RX_2040)
907 rxs->flag |= RX_FLAG_40MHZ;
908 if (rx_stats->rs_flags & ATH9K_RX_GI)
909 rxs->flag |= RX_FLAG_SHORT_GI;
910 rxs->rate_idx = rx_stats->rs_rate & 0x7f;
911 return 0;
912 }
913
914 for (i = 0; i < sband->n_bitrates; i++) {
915 if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
916 rxs->rate_idx = i;
917 return 0;
918 }
919 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
920 rxs->flag |= RX_FLAG_SHORTPRE;
921 rxs->rate_idx = i;
922 return 0;
923 }
924 }
925
926 /*
927 * No valid hardware bitrate found -- we should not get here
928 * because hardware has already validated this frame as OK.
929 */
Joe Perches226afe62010-12-02 19:12:37 -0800930 ath_dbg(common, ATH_DBG_XMIT,
931 "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
932 rx_stats->rs_rate);
Sujithd4357002010-05-20 15:34:38 +0530933
934 return -EINVAL;
935}
936
937static void ath9k_process_rssi(struct ath_common *common,
938 struct ieee80211_hw *hw,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700939 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530940 struct ath_rx_status *rx_stats)
941{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100942 struct ath_softc *sc = hw->priv;
Sujithd4357002010-05-20 15:34:38 +0530943 struct ath_hw *ah = common->ah;
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200944 int last_rssi;
Sujithd4357002010-05-20 15:34:38 +0530945 __le16 fc;
946
Rajkumar Manoharan2b892a92011-05-09 19:11:28 +0530947 if ((ah->opmode != NL80211_IFTYPE_STATION) &&
948 (ah->opmode != NL80211_IFTYPE_ADHOC))
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200949 return;
950
Sujithd4357002010-05-20 15:34:38 +0530951 fc = hdr->frame_control;
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200952 if (!ieee80211_is_beacon(fc) ||
Ben Greear48014162011-01-15 19:13:48 +0000953 compare_ether_addr(hdr->addr3, common->curbssid)) {
954 /* TODO: This doesn't work well if you have stations
955 * associated to two different APs because curbssid
956 * is just the last AP that any of the stations associated
957 * with.
958 */
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200959 return;
Ben Greear48014162011-01-15 19:13:48 +0000960 }
Sujithd4357002010-05-20 15:34:38 +0530961
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200962 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
Felix Fietkau9ac586152011-01-24 19:23:18 +0100963 ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
Ben Greear686b9cb2010-09-23 09:44:36 -0700964
Felix Fietkau9ac586152011-01-24 19:23:18 +0100965 last_rssi = sc->last_rssi;
Sujithd4357002010-05-20 15:34:38 +0530966 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
967 rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
968 ATH_RSSI_EP_MULTIPLIER);
969 if (rx_stats->rs_rssi < 0)
970 rx_stats->rs_rssi = 0;
971
972 /* Update Beacon RSSI, this is used by ANI. */
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200973 ah->stats.avgbrssi = rx_stats->rs_rssi;
Sujithd4357002010-05-20 15:34:38 +0530974}
975
976/*
977 * For Decrypt or Demic errors, we only mark packet status here and always push
978 * up the frame up to let mac80211 handle the actual error case, be it no
979 * decryption key or real decryption error. This let us keep statistics there.
980 */
981static int ath9k_rx_skb_preprocess(struct ath_common *common,
982 struct ieee80211_hw *hw,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700983 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530984 struct ath_rx_status *rx_stats,
985 struct ieee80211_rx_status *rx_status,
986 bool *decrypt_error)
987{
Sujithd4357002010-05-20 15:34:38 +0530988 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
989
990 /*
991 * everything but the rate is checked here, the rate check is done
992 * separately to avoid doing two lookups for a rate for each frame.
993 */
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700994 if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
Sujithd4357002010-05-20 15:34:38 +0530995 return -EINVAL;
996
Felix Fietkau0d955212011-01-26 18:23:27 +0100997 /* Only use status info from the last fragment */
998 if (rx_stats->rs_more)
999 return 0;
1000
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -07001001 ath9k_process_rssi(common, hw, hdr, rx_stats);
Sujithd4357002010-05-20 15:34:38 +05301002
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -07001003 if (ath9k_process_rate(common, hw, rx_stats, rx_status))
Sujithd4357002010-05-20 15:34:38 +05301004 return -EINVAL;
1005
Sujithd4357002010-05-20 15:34:38 +05301006 rx_status->band = hw->conf.channel->band;
1007 rx_status->freq = hw->conf.channel->center_freq;
1008 rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
1009 rx_status->antenna = rx_stats->rs_antenna;
Johannes Berg6ebacbb2011-02-23 15:06:08 +01001010 rx_status->flag |= RX_FLAG_MACTIME_MPDU;
Sujithd4357002010-05-20 15:34:38 +05301011
1012 return 0;
1013}
1014
1015static void ath9k_rx_skb_postprocess(struct ath_common *common,
1016 struct sk_buff *skb,
1017 struct ath_rx_status *rx_stats,
1018 struct ieee80211_rx_status *rxs,
1019 bool decrypt_error)
1020{
1021 struct ath_hw *ah = common->ah;
1022 struct ieee80211_hdr *hdr;
1023 int hdrlen, padpos, padsize;
1024 u8 keyix;
1025 __le16 fc;
1026
1027 /* see if any padding is done by the hw and remove it */
1028 hdr = (struct ieee80211_hdr *) skb->data;
1029 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1030 fc = hdr->frame_control;
1031 padpos = ath9k_cmn_padpos(hdr->frame_control);
1032
1033 /* The MAC header is padded to have 32-bit boundary if the
1034 * packet payload is non-zero. The general calculation for
1035 * padsize would take into account odd header lengths:
1036 * padsize = (4 - padpos % 4) % 4; However, since only
1037 * even-length headers are used, padding can only be 0 or 2
1038 * bytes and we can optimize this a bit. In addition, we must
1039 * not try to remove padding from short control frames that do
1040 * not have payload. */
1041 padsize = padpos & 3;
1042 if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1043 memmove(skb->data + padsize, skb->data, padpos);
1044 skb_pull(skb, padsize);
1045 }
1046
1047 keyix = rx_stats->rs_keyix;
1048
1049 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1050 ieee80211_has_protected(fc)) {
1051 rxs->flag |= RX_FLAG_DECRYPTED;
1052 } else if (ieee80211_has_protected(fc)
1053 && !decrypt_error && skb->len >= hdrlen + 4) {
1054 keyix = skb->data[hdrlen + 3] >> 6;
1055
1056 if (test_bit(keyix, common->keymap))
1057 rxs->flag |= RX_FLAG_DECRYPTED;
1058 }
1059 if (ah->sw_mgmt_crypto &&
1060 (rxs->flag & RX_FLAG_DECRYPTED) &&
1061 ieee80211_is_mgmt(fc))
1062 /* Use software decrypt for management frames. */
1063 rxs->flag &= ~RX_FLAG_DECRYPTED;
1064}
Felix Fietkaub5c804752010-04-15 17:38:48 -04001065
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001066static void ath_lnaconf_alt_good_scan(struct ath_ant_comb *antcomb,
1067 struct ath_hw_antcomb_conf ant_conf,
1068 int main_rssi_avg)
1069{
1070 antcomb->quick_scan_cnt = 0;
1071
1072 if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
1073 antcomb->rssi_lna2 = main_rssi_avg;
1074 else if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA1)
1075 antcomb->rssi_lna1 = main_rssi_avg;
1076
1077 switch ((ant_conf.main_lna_conf << 4) | ant_conf.alt_lna_conf) {
1078 case (0x10): /* LNA2 A-B */
1079 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1080 antcomb->first_quick_scan_conf =
1081 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1082 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1083 break;
1084 case (0x20): /* LNA1 A-B */
1085 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1086 antcomb->first_quick_scan_conf =
1087 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1088 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1089 break;
1090 case (0x21): /* LNA1 LNA2 */
1091 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA2;
1092 antcomb->first_quick_scan_conf =
1093 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1094 antcomb->second_quick_scan_conf =
1095 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1096 break;
1097 case (0x12): /* LNA2 LNA1 */
1098 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1;
1099 antcomb->first_quick_scan_conf =
1100 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1101 antcomb->second_quick_scan_conf =
1102 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1103 break;
1104 case (0x13): /* LNA2 A+B */
1105 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1106 antcomb->first_quick_scan_conf =
1107 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1108 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1109 break;
1110 case (0x23): /* LNA1 A+B */
1111 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1112 antcomb->first_quick_scan_conf =
1113 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1114 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1115 break;
1116 default:
1117 break;
1118 }
1119}
1120
1121static void ath_select_ant_div_from_quick_scan(struct ath_ant_comb *antcomb,
1122 struct ath_hw_antcomb_conf *div_ant_conf,
1123 int main_rssi_avg, int alt_rssi_avg,
1124 int alt_ratio)
1125{
1126 /* alt_good */
1127 switch (antcomb->quick_scan_cnt) {
1128 case 0:
1129 /* set alt to main, and alt to first conf */
1130 div_ant_conf->main_lna_conf = antcomb->main_conf;
1131 div_ant_conf->alt_lna_conf = antcomb->first_quick_scan_conf;
1132 break;
1133 case 1:
1134 /* set alt to main, and alt to first conf */
1135 div_ant_conf->main_lna_conf = antcomb->main_conf;
1136 div_ant_conf->alt_lna_conf = antcomb->second_quick_scan_conf;
1137 antcomb->rssi_first = main_rssi_avg;
1138 antcomb->rssi_second = alt_rssi_avg;
1139
1140 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1141 /* main is LNA1 */
1142 if (ath_is_alt_ant_ratio_better(alt_ratio,
1143 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1144 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1145 main_rssi_avg, alt_rssi_avg,
1146 antcomb->total_pkt_count))
1147 antcomb->first_ratio = true;
1148 else
1149 antcomb->first_ratio = false;
1150 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1151 if (ath_is_alt_ant_ratio_better(alt_ratio,
1152 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1153 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1154 main_rssi_avg, alt_rssi_avg,
1155 antcomb->total_pkt_count))
1156 antcomb->first_ratio = true;
1157 else
1158 antcomb->first_ratio = false;
1159 } else {
1160 if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1161 (alt_rssi_avg > main_rssi_avg +
1162 ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1163 (alt_rssi_avg > main_rssi_avg)) &&
1164 (antcomb->total_pkt_count > 50))
1165 antcomb->first_ratio = true;
1166 else
1167 antcomb->first_ratio = false;
1168 }
1169 break;
1170 case 2:
1171 antcomb->alt_good = false;
1172 antcomb->scan_not_start = false;
1173 antcomb->scan = false;
1174 antcomb->rssi_first = main_rssi_avg;
1175 antcomb->rssi_third = alt_rssi_avg;
1176
1177 if (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1)
1178 antcomb->rssi_lna1 = alt_rssi_avg;
1179 else if (antcomb->second_quick_scan_conf ==
1180 ATH_ANT_DIV_COMB_LNA2)
1181 antcomb->rssi_lna2 = alt_rssi_avg;
1182 else if (antcomb->second_quick_scan_conf ==
1183 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2) {
1184 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)
1185 antcomb->rssi_lna2 = main_rssi_avg;
1186 else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1)
1187 antcomb->rssi_lna1 = main_rssi_avg;
1188 }
1189
1190 if (antcomb->rssi_lna2 > antcomb->rssi_lna1 +
1191 ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)
1192 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1193 else
1194 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
1195
1196 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1197 if (ath_is_alt_ant_ratio_better(alt_ratio,
1198 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1199 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1200 main_rssi_avg, alt_rssi_avg,
1201 antcomb->total_pkt_count))
1202 antcomb->second_ratio = true;
1203 else
1204 antcomb->second_ratio = false;
1205 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1206 if (ath_is_alt_ant_ratio_better(alt_ratio,
1207 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1208 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1209 main_rssi_avg, alt_rssi_avg,
1210 antcomb->total_pkt_count))
1211 antcomb->second_ratio = true;
1212 else
1213 antcomb->second_ratio = false;
1214 } else {
1215 if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1216 (alt_rssi_avg > main_rssi_avg +
1217 ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1218 (alt_rssi_avg > main_rssi_avg)) &&
1219 (antcomb->total_pkt_count > 50))
1220 antcomb->second_ratio = true;
1221 else
1222 antcomb->second_ratio = false;
1223 }
1224
1225 /* set alt to the conf with maximun ratio */
1226 if (antcomb->first_ratio && antcomb->second_ratio) {
1227 if (antcomb->rssi_second > antcomb->rssi_third) {
1228 /* first alt*/
1229 if ((antcomb->first_quick_scan_conf ==
1230 ATH_ANT_DIV_COMB_LNA1) ||
1231 (antcomb->first_quick_scan_conf ==
1232 ATH_ANT_DIV_COMB_LNA2))
1233 /* Set alt LNA1 or LNA2*/
1234 if (div_ant_conf->main_lna_conf ==
1235 ATH_ANT_DIV_COMB_LNA2)
1236 div_ant_conf->alt_lna_conf =
1237 ATH_ANT_DIV_COMB_LNA1;
1238 else
1239 div_ant_conf->alt_lna_conf =
1240 ATH_ANT_DIV_COMB_LNA2;
1241 else
1242 /* Set alt to A+B or A-B */
1243 div_ant_conf->alt_lna_conf =
1244 antcomb->first_quick_scan_conf;
1245 } else if ((antcomb->second_quick_scan_conf ==
1246 ATH_ANT_DIV_COMB_LNA1) ||
1247 (antcomb->second_quick_scan_conf ==
1248 ATH_ANT_DIV_COMB_LNA2)) {
1249 /* Set alt LNA1 or LNA2 */
1250 if (div_ant_conf->main_lna_conf ==
1251 ATH_ANT_DIV_COMB_LNA2)
1252 div_ant_conf->alt_lna_conf =
1253 ATH_ANT_DIV_COMB_LNA1;
1254 else
1255 div_ant_conf->alt_lna_conf =
1256 ATH_ANT_DIV_COMB_LNA2;
1257 } else {
1258 /* Set alt to A+B or A-B */
1259 div_ant_conf->alt_lna_conf =
1260 antcomb->second_quick_scan_conf;
1261 }
1262 } else if (antcomb->first_ratio) {
1263 /* first alt */
1264 if ((antcomb->first_quick_scan_conf ==
1265 ATH_ANT_DIV_COMB_LNA1) ||
1266 (antcomb->first_quick_scan_conf ==
1267 ATH_ANT_DIV_COMB_LNA2))
1268 /* Set alt LNA1 or LNA2 */
1269 if (div_ant_conf->main_lna_conf ==
1270 ATH_ANT_DIV_COMB_LNA2)
1271 div_ant_conf->alt_lna_conf =
1272 ATH_ANT_DIV_COMB_LNA1;
1273 else
1274 div_ant_conf->alt_lna_conf =
1275 ATH_ANT_DIV_COMB_LNA2;
1276 else
1277 /* Set alt to A+B or A-B */
1278 div_ant_conf->alt_lna_conf =
1279 antcomb->first_quick_scan_conf;
1280 } else if (antcomb->second_ratio) {
1281 /* second alt */
1282 if ((antcomb->second_quick_scan_conf ==
1283 ATH_ANT_DIV_COMB_LNA1) ||
1284 (antcomb->second_quick_scan_conf ==
1285 ATH_ANT_DIV_COMB_LNA2))
1286 /* Set alt LNA1 or LNA2 */
1287 if (div_ant_conf->main_lna_conf ==
1288 ATH_ANT_DIV_COMB_LNA2)
1289 div_ant_conf->alt_lna_conf =
1290 ATH_ANT_DIV_COMB_LNA1;
1291 else
1292 div_ant_conf->alt_lna_conf =
1293 ATH_ANT_DIV_COMB_LNA2;
1294 else
1295 /* Set alt to A+B or A-B */
1296 div_ant_conf->alt_lna_conf =
1297 antcomb->second_quick_scan_conf;
1298 } else {
1299 /* main is largest */
1300 if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
1301 (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
1302 /* Set alt LNA1 or LNA2 */
1303 if (div_ant_conf->main_lna_conf ==
1304 ATH_ANT_DIV_COMB_LNA2)
1305 div_ant_conf->alt_lna_conf =
1306 ATH_ANT_DIV_COMB_LNA1;
1307 else
1308 div_ant_conf->alt_lna_conf =
1309 ATH_ANT_DIV_COMB_LNA2;
1310 else
1311 /* Set alt to A+B or A-B */
1312 div_ant_conf->alt_lna_conf = antcomb->main_conf;
1313 }
1314 break;
1315 default:
1316 break;
1317 }
1318}
1319
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301320static void ath_ant_div_conf_fast_divbias(struct ath_hw_antcomb_conf *ant_conf,
1321 struct ath_ant_comb *antcomb, int alt_ratio)
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001322{
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301323 if (ant_conf->div_group == 0) {
1324 /* Adjust the fast_div_bias based on main and alt lna conf */
1325 switch ((ant_conf->main_lna_conf << 4) |
1326 ant_conf->alt_lna_conf) {
1327 case (0x01): /* A-B LNA2 */
1328 ant_conf->fast_div_bias = 0x3b;
1329 break;
1330 case (0x02): /* A-B LNA1 */
1331 ant_conf->fast_div_bias = 0x3d;
1332 break;
1333 case (0x03): /* A-B A+B */
1334 ant_conf->fast_div_bias = 0x1;
1335 break;
1336 case (0x10): /* LNA2 A-B */
1337 ant_conf->fast_div_bias = 0x7;
1338 break;
1339 case (0x12): /* LNA2 LNA1 */
1340 ant_conf->fast_div_bias = 0x2;
1341 break;
1342 case (0x13): /* LNA2 A+B */
1343 ant_conf->fast_div_bias = 0x7;
1344 break;
1345 case (0x20): /* LNA1 A-B */
1346 ant_conf->fast_div_bias = 0x6;
1347 break;
1348 case (0x21): /* LNA1 LNA2 */
1349 ant_conf->fast_div_bias = 0x0;
1350 break;
1351 case (0x23): /* LNA1 A+B */
1352 ant_conf->fast_div_bias = 0x6;
1353 break;
1354 case (0x30): /* A+B A-B */
1355 ant_conf->fast_div_bias = 0x1;
1356 break;
1357 case (0x31): /* A+B LNA2 */
1358 ant_conf->fast_div_bias = 0x3b;
1359 break;
1360 case (0x32): /* A+B LNA1 */
1361 ant_conf->fast_div_bias = 0x3d;
1362 break;
1363 default:
1364 break;
1365 }
1366 } else if (ant_conf->div_group == 2) {
1367 /* Adjust the fast_div_bias based on main and alt_lna_conf */
1368 switch ((ant_conf->main_lna_conf << 4) |
1369 ant_conf->alt_lna_conf) {
1370 case (0x01): /* A-B LNA2 */
1371 ant_conf->fast_div_bias = 0x1;
1372 ant_conf->main_gaintb = 0;
1373 ant_conf->alt_gaintb = 0;
1374 break;
1375 case (0x02): /* A-B LNA1 */
1376 ant_conf->fast_div_bias = 0x1;
1377 ant_conf->main_gaintb = 0;
1378 ant_conf->alt_gaintb = 0;
1379 break;
1380 case (0x03): /* A-B A+B */
1381 ant_conf->fast_div_bias = 0x1;
1382 ant_conf->main_gaintb = 0;
1383 ant_conf->alt_gaintb = 0;
1384 break;
1385 case (0x10): /* LNA2 A-B */
1386 if (!(antcomb->scan) &&
1387 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1388 ant_conf->fast_div_bias = 0x1;
1389 else
1390 ant_conf->fast_div_bias = 0x2;
1391 ant_conf->main_gaintb = 0;
1392 ant_conf->alt_gaintb = 0;
1393 break;
1394 case (0x12): /* LNA2 LNA1 */
1395 ant_conf->fast_div_bias = 0x1;
1396 ant_conf->main_gaintb = 0;
1397 ant_conf->alt_gaintb = 0;
1398 break;
1399 case (0x13): /* LNA2 A+B */
1400 if (!(antcomb->scan) &&
1401 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1402 ant_conf->fast_div_bias = 0x1;
1403 else
1404 ant_conf->fast_div_bias = 0x2;
1405 ant_conf->main_gaintb = 0;
1406 ant_conf->alt_gaintb = 0;
1407 break;
1408 case (0x20): /* LNA1 A-B */
1409 if (!(antcomb->scan) &&
1410 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1411 ant_conf->fast_div_bias = 0x1;
1412 else
1413 ant_conf->fast_div_bias = 0x2;
1414 ant_conf->main_gaintb = 0;
1415 ant_conf->alt_gaintb = 0;
1416 break;
1417 case (0x21): /* LNA1 LNA2 */
1418 ant_conf->fast_div_bias = 0x1;
1419 ant_conf->main_gaintb = 0;
1420 ant_conf->alt_gaintb = 0;
1421 break;
1422 case (0x23): /* LNA1 A+B */
1423 if (!(antcomb->scan) &&
1424 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1425 ant_conf->fast_div_bias = 0x1;
1426 else
1427 ant_conf->fast_div_bias = 0x2;
1428 ant_conf->main_gaintb = 0;
1429 ant_conf->alt_gaintb = 0;
1430 break;
1431 case (0x30): /* A+B A-B */
1432 ant_conf->fast_div_bias = 0x1;
1433 ant_conf->main_gaintb = 0;
1434 ant_conf->alt_gaintb = 0;
1435 break;
1436 case (0x31): /* A+B LNA2 */
1437 ant_conf->fast_div_bias = 0x1;
1438 ant_conf->main_gaintb = 0;
1439 ant_conf->alt_gaintb = 0;
1440 break;
1441 case (0x32): /* A+B LNA1 */
1442 ant_conf->fast_div_bias = 0x1;
1443 ant_conf->main_gaintb = 0;
1444 ant_conf->alt_gaintb = 0;
1445 break;
1446 default:
1447 break;
1448 }
1449
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001450 }
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301451
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001452}
1453
1454/* Antenna diversity and combining */
1455static void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs)
1456{
1457 struct ath_hw_antcomb_conf div_ant_conf;
1458 struct ath_ant_comb *antcomb = &sc->ant_comb;
1459 int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set;
Sujith Manoharan0ff2b5c2011-04-20 11:00:34 +05301460 int curr_main_set;
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001461 int main_rssi = rs->rs_rssi_ctl0;
1462 int alt_rssi = rs->rs_rssi_ctl1;
1463 int rx_ant_conf, main_ant_conf;
1464 bool short_scan = false;
1465
1466 rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) &
1467 ATH_ANT_RX_MASK;
1468 main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) &
1469 ATH_ANT_RX_MASK;
1470
Mohammed Shafi Shajakhan21e8ee62011-05-13 20:31:40 +05301471 /* Record packet only when both main_rssi and alt_rssi is positive */
1472 if (main_rssi > 0 && alt_rssi > 0) {
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001473 antcomb->total_pkt_count++;
1474 antcomb->main_total_rssi += main_rssi;
1475 antcomb->alt_total_rssi += alt_rssi;
1476 if (main_ant_conf == rx_ant_conf)
1477 antcomb->main_recv_cnt++;
1478 else
1479 antcomb->alt_recv_cnt++;
1480 }
1481
1482 /* Short scan check */
1483 if (antcomb->scan && antcomb->alt_good) {
1484 if (time_after(jiffies, antcomb->scan_start_time +
1485 msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
1486 short_scan = true;
1487 else
1488 if (antcomb->total_pkt_count ==
1489 ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
1490 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1491 antcomb->total_pkt_count);
1492 if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
1493 short_scan = true;
1494 }
1495 }
1496
1497 if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
1498 rs->rs_moreaggr) && !short_scan)
1499 return;
1500
1501 if (antcomb->total_pkt_count) {
1502 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1503 antcomb->total_pkt_count);
1504 main_rssi_avg = (antcomb->main_total_rssi /
1505 antcomb->total_pkt_count);
1506 alt_rssi_avg = (antcomb->alt_total_rssi /
1507 antcomb->total_pkt_count);
1508 }
1509
1510
1511 ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf);
1512 curr_alt_set = div_ant_conf.alt_lna_conf;
1513 curr_main_set = div_ant_conf.main_lna_conf;
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001514
1515 antcomb->count++;
1516
1517 if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
1518 if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1519 ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
1520 main_rssi_avg);
1521 antcomb->alt_good = true;
1522 } else {
1523 antcomb->alt_good = false;
1524 }
1525
1526 antcomb->count = 0;
1527 antcomb->scan = true;
1528 antcomb->scan_not_start = true;
1529 }
1530
1531 if (!antcomb->scan) {
Mohammed Shafi Shajakhanb85c5732011-05-13 20:31:09 +05301532 if (ath_ant_div_comb_alt_check(div_ant_conf.div_group,
1533 alt_ratio, curr_main_set, curr_alt_set,
1534 alt_rssi_avg, main_rssi_avg)) {
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001535 if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
1536 /* Switch main and alt LNA */
1537 div_ant_conf.main_lna_conf =
1538 ATH_ANT_DIV_COMB_LNA2;
1539 div_ant_conf.alt_lna_conf =
1540 ATH_ANT_DIV_COMB_LNA1;
1541 } else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
1542 div_ant_conf.main_lna_conf =
1543 ATH_ANT_DIV_COMB_LNA1;
1544 div_ant_conf.alt_lna_conf =
1545 ATH_ANT_DIV_COMB_LNA2;
1546 }
1547
1548 goto div_comb_done;
1549 } else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
1550 (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
1551 /* Set alt to another LNA */
1552 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
1553 div_ant_conf.alt_lna_conf =
1554 ATH_ANT_DIV_COMB_LNA1;
1555 else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
1556 div_ant_conf.alt_lna_conf =
1557 ATH_ANT_DIV_COMB_LNA2;
1558
1559 goto div_comb_done;
1560 }
1561
1562 if ((alt_rssi_avg < (main_rssi_avg +
Mohammed Shafi Shajakhan8afbcc82011-05-13 20:30:56 +05301563 div_ant_conf.lna1_lna2_delta)))
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001564 goto div_comb_done;
1565 }
1566
1567 if (!antcomb->scan_not_start) {
1568 switch (curr_alt_set) {
1569 case ATH_ANT_DIV_COMB_LNA2:
1570 antcomb->rssi_lna2 = alt_rssi_avg;
1571 antcomb->rssi_lna1 = main_rssi_avg;
1572 antcomb->scan = true;
1573 /* set to A+B */
1574 div_ant_conf.main_lna_conf =
1575 ATH_ANT_DIV_COMB_LNA1;
1576 div_ant_conf.alt_lna_conf =
1577 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1578 break;
1579 case ATH_ANT_DIV_COMB_LNA1:
1580 antcomb->rssi_lna1 = alt_rssi_avg;
1581 antcomb->rssi_lna2 = main_rssi_avg;
1582 antcomb->scan = true;
1583 /* set to A+B */
1584 div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1585 div_ant_conf.alt_lna_conf =
1586 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1587 break;
1588 case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
1589 antcomb->rssi_add = alt_rssi_avg;
1590 antcomb->scan = true;
1591 /* set to A-B */
1592 div_ant_conf.alt_lna_conf =
1593 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1594 break;
1595 case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
1596 antcomb->rssi_sub = alt_rssi_avg;
1597 antcomb->scan = false;
1598 if (antcomb->rssi_lna2 >
1599 (antcomb->rssi_lna1 +
1600 ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
1601 /* use LNA2 as main LNA */
1602 if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
1603 (antcomb->rssi_add > antcomb->rssi_sub)) {
1604 /* set to A+B */
1605 div_ant_conf.main_lna_conf =
1606 ATH_ANT_DIV_COMB_LNA2;
1607 div_ant_conf.alt_lna_conf =
1608 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1609 } else if (antcomb->rssi_sub >
1610 antcomb->rssi_lna1) {
1611 /* set to A-B */
1612 div_ant_conf.main_lna_conf =
1613 ATH_ANT_DIV_COMB_LNA2;
1614 div_ant_conf.alt_lna_conf =
1615 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1616 } else {
1617 /* set to LNA1 */
1618 div_ant_conf.main_lna_conf =
1619 ATH_ANT_DIV_COMB_LNA2;
1620 div_ant_conf.alt_lna_conf =
1621 ATH_ANT_DIV_COMB_LNA1;
1622 }
1623 } else {
1624 /* use LNA1 as main LNA */
1625 if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
1626 (antcomb->rssi_add > antcomb->rssi_sub)) {
1627 /* set to A+B */
1628 div_ant_conf.main_lna_conf =
1629 ATH_ANT_DIV_COMB_LNA1;
1630 div_ant_conf.alt_lna_conf =
1631 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1632 } else if (antcomb->rssi_sub >
1633 antcomb->rssi_lna1) {
1634 /* set to A-B */
1635 div_ant_conf.main_lna_conf =
1636 ATH_ANT_DIV_COMB_LNA1;
1637 div_ant_conf.alt_lna_conf =
1638 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1639 } else {
1640 /* set to LNA2 */
1641 div_ant_conf.main_lna_conf =
1642 ATH_ANT_DIV_COMB_LNA1;
1643 div_ant_conf.alt_lna_conf =
1644 ATH_ANT_DIV_COMB_LNA2;
1645 }
1646 }
1647 break;
1648 default:
1649 break;
1650 }
1651 } else {
1652 if (!antcomb->alt_good) {
1653 antcomb->scan_not_start = false;
1654 /* Set alt to another LNA */
1655 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
1656 div_ant_conf.main_lna_conf =
1657 ATH_ANT_DIV_COMB_LNA2;
1658 div_ant_conf.alt_lna_conf =
1659 ATH_ANT_DIV_COMB_LNA1;
1660 } else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
1661 div_ant_conf.main_lna_conf =
1662 ATH_ANT_DIV_COMB_LNA1;
1663 div_ant_conf.alt_lna_conf =
1664 ATH_ANT_DIV_COMB_LNA2;
1665 }
1666 goto div_comb_done;
1667 }
1668 }
1669
1670 ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
1671 main_rssi_avg, alt_rssi_avg,
1672 alt_ratio);
1673
1674 antcomb->quick_scan_cnt++;
1675
1676div_comb_done:
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301677 ath_ant_div_conf_fast_divbias(&div_ant_conf, antcomb, alt_ratio);
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001678 ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf);
1679
1680 antcomb->scan_start_time = jiffies;
1681 antcomb->total_pkt_count = 0;
1682 antcomb->main_total_rssi = 0;
1683 antcomb->alt_total_rssi = 0;
1684 antcomb->main_recv_cnt = 0;
1685 antcomb->alt_recv_cnt = 0;
1686}
1687
Felix Fietkaub5c804752010-04-15 17:38:48 -04001688int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1689{
1690 struct ath_buf *bf;
Felix Fietkau0d955212011-01-26 18:23:27 +01001691 struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001692 struct ieee80211_rx_status *rxs;
Sujithcbe61d82009-02-09 13:27:12 +05301693 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -07001694 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezb4afffc2009-11-02 11:36:08 -08001695 /*
Mohammed Shafi Shajakhancae6b742010-12-07 21:23:16 +05301696 * The hw can technically differ from common->hw when using ath9k
Luis R. Rodriguezb4afffc2009-11-02 11:36:08 -08001697 * virtual wiphy so to account for that we iterate over the active
1698 * wiphys and find the appropriate wiphy and therefore hw.
1699 */
Felix Fietkau7545daf2011-01-24 19:23:16 +01001700 struct ieee80211_hw *hw = sc->hw;
Sujithbe0418a2008-11-18 09:05:55 +05301701 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc9b14172009-11-04 16:47:22 -08001702 int retval;
Sujithbe0418a2008-11-18 09:05:55 +05301703 bool decrypt_error = false;
Felix Fietkau29bffa92010-03-29 20:14:23 -07001704 struct ath_rx_status rs;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001705 enum ath9k_rx_qtype qtype;
1706 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1707 int dma_type;
Vasanthakumar Thiagarajan5c6dd922010-05-20 14:34:47 -07001708 u8 rx_status_len = ah->caps.rx_status_len;
Felix Fietkaua6d20552010-06-12 00:33:54 -04001709 u64 tsf = 0;
1710 u32 tsf_lower = 0;
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001711 unsigned long flags;
Sujithbe0418a2008-11-18 09:05:55 +05301712
Felix Fietkaub5c804752010-04-15 17:38:48 -04001713 if (edma)
Felix Fietkaub5c804752010-04-15 17:38:48 -04001714 dma_type = DMA_BIDIRECTIONAL;
Ming Lei56824222010-05-14 21:15:38 +08001715 else
1716 dma_type = DMA_FROM_DEVICE;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001717
1718 qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
Sujithb77f4832008-12-07 21:44:03 +05301719 spin_lock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001720
Felix Fietkaua6d20552010-06-12 00:33:54 -04001721 tsf = ath9k_hw_gettsf64(ah);
1722 tsf_lower = tsf & 0xffffffff;
1723
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001724 do {
1725 /* If handling rx interrupt and flush is in progress => exit */
Sujith98deeea2008-08-11 14:05:46 +05301726 if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001727 break;
1728
Felix Fietkau29bffa92010-03-29 20:14:23 -07001729 memset(&rs, 0, sizeof(rs));
Felix Fietkaub5c804752010-04-15 17:38:48 -04001730 if (edma)
1731 bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1732 else
1733 bf = ath_get_next_rx_buf(sc, &rs);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001734
Felix Fietkaub5c804752010-04-15 17:38:48 -04001735 if (!bf)
1736 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001737
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001738 skb = bf->bf_mpdu;
Sujithbe0418a2008-11-18 09:05:55 +05301739 if (!skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001740 continue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001741
Felix Fietkau0d955212011-01-26 18:23:27 +01001742 /*
1743 * Take frame header from the first fragment and RX status from
1744 * the last one.
1745 */
1746 if (sc->rx.frag)
1747 hdr_skb = sc->rx.frag;
1748 else
1749 hdr_skb = skb;
1750
1751 hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
1752 rxs = IEEE80211_SKB_RXCB(hdr_skb);
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001753
Felix Fietkau29bffa92010-03-29 20:14:23 -07001754 ath_debug_stat_rx(sc, &rs);
Sujith1395d3f2010-01-08 10:36:11 +05301755
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301756 /*
Sujithbe0418a2008-11-18 09:05:55 +05301757 * If we're asked to flush receive queue, directly
1758 * chain it back at the queue without processing it.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001759 */
Sujithbe0418a2008-11-18 09:05:55 +05301760 if (flush)
Felix Fietkau0d955212011-01-26 18:23:27 +01001761 goto requeue_drop_frag;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001762
Jan Friedrichc8f3b722010-08-02 23:55:50 +02001763 retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
1764 rxs, &decrypt_error);
1765 if (retval)
Felix Fietkau0d955212011-01-26 18:23:27 +01001766 goto requeue_drop_frag;
Jan Friedrichc8f3b722010-08-02 23:55:50 +02001767
Felix Fietkaua6d20552010-06-12 00:33:54 -04001768 rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1769 if (rs.rs_tstamp > tsf_lower &&
1770 unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1771 rxs->mactime -= 0x100000000ULL;
1772
1773 if (rs.rs_tstamp < tsf_lower &&
1774 unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1775 rxs->mactime += 0x100000000ULL;
1776
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001777 /* Ensure we always have an skb to requeue once we are done
1778 * processing the current buffer's skb */
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001779 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001780
1781 /* If there is no memory we ignore the current RX'd frame,
1782 * tell hardware it can give us a new frame using the old
Sujithb77f4832008-12-07 21:44:03 +05301783 * skb and put it at the tail of the sc->rx.rxbuf list for
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001784 * processing. */
1785 if (!requeue_skb)
Felix Fietkau0d955212011-01-26 18:23:27 +01001786 goto requeue_drop_frag;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001787
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301788 /* Unmap the frame */
Gabor Juhos7da3c552009-01-14 20:17:03 +01001789 dma_unmap_single(sc->dev, bf->bf_buf_addr,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001790 common->rx_bufsize,
Felix Fietkaub5c804752010-04-15 17:38:48 -04001791 dma_type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001792
Felix Fietkaub5c804752010-04-15 17:38:48 -04001793 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1794 if (ah->caps.rx_status_len)
1795 skb_pull(skb, ah->caps.rx_status_len);
Sujithbe0418a2008-11-18 09:05:55 +05301796
Felix Fietkau0d955212011-01-26 18:23:27 +01001797 if (!rs.rs_more)
1798 ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1799 rxs, decrypt_error);
Sujithbe0418a2008-11-18 09:05:55 +05301800
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001801 /* We will now give hardware our shiny new allocated skb */
1802 bf->bf_mpdu = requeue_skb;
Gabor Juhos7da3c552009-01-14 20:17:03 +01001803 bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001804 common->rx_bufsize,
Felix Fietkaub5c804752010-04-15 17:38:48 -04001805 dma_type);
Gabor Juhos7da3c552009-01-14 20:17:03 +01001806 if (unlikely(dma_mapping_error(sc->dev,
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001807 bf->bf_buf_addr))) {
1808 dev_kfree_skb_any(requeue_skb);
1809 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -07001810 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -08001811 ath_err(common, "dma_mapping_error() on RX\n");
Felix Fietkau7545daf2011-01-24 19:23:16 +01001812 ieee80211_rx(hw, skb);
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001813 break;
1814 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001815
Felix Fietkau0d955212011-01-26 18:23:27 +01001816 if (rs.rs_more) {
1817 /*
1818 * rs_more indicates chained descriptors which can be
1819 * used to link buffers together for a sort of
1820 * scatter-gather operation.
1821 */
1822 if (sc->rx.frag) {
1823 /* too many fragments - cannot handle frame */
1824 dev_kfree_skb_any(sc->rx.frag);
1825 dev_kfree_skb_any(skb);
1826 skb = NULL;
1827 }
1828 sc->rx.frag = skb;
1829 goto requeue;
1830 }
1831
1832 if (sc->rx.frag) {
1833 int space = skb->len - skb_tailroom(hdr_skb);
1834
1835 sc->rx.frag = NULL;
1836
1837 if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
1838 dev_kfree_skb(skb);
1839 goto requeue_drop_frag;
1840 }
1841
1842 skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
1843 skb->len);
1844 dev_kfree_skb_any(skb);
1845 skb = hdr_skb;
1846 }
1847
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001848 /*
1849 * change the default rx antenna if rx diversity chooses the
1850 * other antenna 3 times in a row.
1851 */
Felix Fietkau29bffa92010-03-29 20:14:23 -07001852 if (sc->rx.defant != rs.rs_antenna) {
Sujithb77f4832008-12-07 21:44:03 +05301853 if (++sc->rx.rxotherant >= 3)
Felix Fietkau29bffa92010-03-29 20:14:23 -07001854 ath_setdefantenna(sc, rs.rs_antenna);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001855 } else {
Sujithb77f4832008-12-07 21:44:03 +05301856 sc->rx.rxotherant = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001857 }
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301858
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001859 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Mohammed Shafi Shajakhanaaef24b2010-12-07 20:40:58 +05301860
1861 if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -07001862 PS_WAIT_FOR_CAB |
Mohammed Shafi Shajakhanaaef24b2010-12-07 20:40:58 +05301863 PS_WAIT_FOR_PSPOLL_DATA)) ||
Mohammed Shafi Shajakhancedc7e32011-04-22 13:12:23 +05301864 ath9k_check_auto_sleep(sc))
Jouni Malinencc659652009-05-14 21:28:48 +03001865 ath_rx_ps(sc, skb);
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001866 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Jouni Malinencc659652009-05-14 21:28:48 +03001867
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001868 if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)
1869 ath_ant_comb_scan(sc, &rs);
1870
Felix Fietkau7545daf2011-01-24 19:23:16 +01001871 ieee80211_rx(hw, skb);
Jouni Malinencc659652009-05-14 21:28:48 +03001872
Felix Fietkau0d955212011-01-26 18:23:27 +01001873requeue_drop_frag:
1874 if (sc->rx.frag) {
1875 dev_kfree_skb_any(sc->rx.frag);
1876 sc->rx.frag = NULL;
1877 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001878requeue:
Felix Fietkaub5c804752010-04-15 17:38:48 -04001879 if (edma) {
1880 list_add_tail(&bf->list, &sc->rx.rxbuf);
1881 ath_rx_edma_buf_link(sc, qtype);
1882 } else {
1883 list_move_tail(&bf->list, &sc->rx.rxbuf);
1884 ath_rx_buf_link(sc, bf);
Felix Fietkau95294972011-04-07 19:30:32 +02001885 ath9k_hw_rxena(ah);
Felix Fietkaub5c804752010-04-15 17:38:48 -04001886 }
Sujithbe0418a2008-11-18 09:05:55 +05301887 } while (1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001888
Sujithb77f4832008-12-07 21:44:03 +05301889 spin_unlock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001890
1891 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001892}