blob: 7e1a91af149751b13a53ee9d6dab5c16fcd2f01f [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 Fietkaub5c804752010-04-15 17:38:48 -040021#define SKB_CB_ATHBUF(__skb) (*((struct ath_buf **)__skb->cb))
22
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -070023static inline bool ath_is_alt_ant_ratio_better(int alt_ratio, int maxdelta,
24 int mindelta, int main_rssi_avg,
25 int alt_rssi_avg, int pkt_count)
26{
27 return (((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
28 (alt_rssi_avg > main_rssi_avg + maxdelta)) ||
29 (alt_rssi_avg > main_rssi_avg + mindelta)) && (pkt_count > 50);
30}
31
Mohammed Shafi Shajakhanb85c5732011-05-13 20:31:09 +053032static inline bool ath_ant_div_comb_alt_check(u8 div_group, int alt_ratio,
33 int curr_main_set, int curr_alt_set,
34 int alt_rssi_avg, int main_rssi_avg)
35{
36 bool result = false;
37 switch (div_group) {
38 case 0:
39 if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
40 result = true;
41 break;
42 case 1:
Gabor Juhos66ce2352011-06-21 11:23:43 +020043 case 2:
Mohammed Shafi Shajakhanb85c5732011-05-13 20:31:09 +053044 if ((((curr_main_set == ATH_ANT_DIV_COMB_LNA2) &&
45 (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) &&
46 (alt_rssi_avg >= (main_rssi_avg - 5))) ||
47 ((curr_main_set == ATH_ANT_DIV_COMB_LNA1) &&
48 (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) &&
49 (alt_rssi_avg >= (main_rssi_avg - 2)))) &&
50 (alt_rssi_avg >= 4))
51 result = true;
52 else
53 result = false;
54 break;
55 }
56
57 return result;
58}
59
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -070060static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
61{
62 return sc->ps_enabled &&
63 (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
64}
65
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070066/*
67 * Setup and link descriptors.
68 *
69 * 11N: we can no longer afford to self link the last descriptor.
70 * MAC acknowledges BA status as long as it copies frames to host
71 * buffer (or rx fifo). This can incorrectly acknowledge packets
72 * to a sender if last desc is self-linked.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070073 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070074static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
75{
Sujithcbe61d82009-02-09 13:27:12 +053076 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080077 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070078 struct ath_desc *ds;
79 struct sk_buff *skb;
80
81 ATH_RXBUF_RESET(bf);
82
83 ds = bf->bf_desc;
Sujithbe0418a2008-11-18 09:05:55 +053084 ds->ds_link = 0; /* link to null */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070085 ds->ds_data = bf->bf_buf_addr;
86
Sujithbe0418a2008-11-18 09:05:55 +053087 /* virtual addr of the beginning of the buffer. */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070088 skb = bf->bf_mpdu;
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -070089 BUG_ON(skb == NULL);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070090 ds->ds_vdata = skb->data;
91
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080092 /*
93 * setup rx descriptors. The rx_bufsize here tells the hardware
Luis R. Rodriguezb4b6cda2008-11-20 17:15:13 -080094 * how much data it can DMA to us and that we are prepared
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080095 * to process
96 */
Sujithb77f4832008-12-07 21:44:03 +053097 ath9k_hw_setuprxdesc(ah, ds,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080098 common->rx_bufsize,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070099 0);
100
Sujithb77f4832008-12-07 21:44:03 +0530101 if (sc->rx.rxlink == NULL)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700102 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
103 else
Sujithb77f4832008-12-07 21:44:03 +0530104 *sc->rx.rxlink = bf->bf_daddr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700105
Sujithb77f4832008-12-07 21:44:03 +0530106 sc->rx.rxlink = &ds->ds_link;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700107}
108
Sujithff37e332008-11-24 12:07:55 +0530109static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
110{
111 /* XXX block beacon interrupts */
112 ath9k_hw_setantenna(sc->sc_ah, antenna);
Sujithb77f4832008-12-07 21:44:03 +0530113 sc->rx.defant = antenna;
114 sc->rx.rxotherant = 0;
Sujithff37e332008-11-24 12:07:55 +0530115}
116
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700117static void ath_opmode_init(struct ath_softc *sc)
118{
Sujithcbe61d82009-02-09 13:27:12 +0530119 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700120 struct ath_common *common = ath9k_hw_common(ah);
121
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700122 u32 rfilt, mfilt[2];
123
124 /* configure rx filter */
125 rfilt = ath_calcrxfilter(sc);
126 ath9k_hw_setrxfilter(ah, rfilt);
127
128 /* configure bssid mask */
Felix Fietkau364734f2010-09-14 20:22:44 +0200129 ath_hw_setbssidmask(common);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700130
131 /* configure operational mode */
132 ath9k_hw_setopmode(ah);
133
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700134 /* calculate and install multicast filter */
135 mfilt[0] = mfilt[1] = ~0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700136 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700137}
138
Felix Fietkaub5c804752010-04-15 17:38:48 -0400139static bool ath_rx_edma_buf_link(struct ath_softc *sc,
140 enum ath9k_rx_qtype qtype)
141{
142 struct ath_hw *ah = sc->sc_ah;
143 struct ath_rx_edma *rx_edma;
144 struct sk_buff *skb;
145 struct ath_buf *bf;
146
147 rx_edma = &sc->rx.rx_edma[qtype];
148 if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
149 return false;
150
151 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
152 list_del_init(&bf->list);
153
154 skb = bf->bf_mpdu;
155
156 ATH_RXBUF_RESET(bf);
157 memset(skb->data, 0, ah->caps.rx_status_len);
158 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
159 ah->caps.rx_status_len, DMA_TO_DEVICE);
160
161 SKB_CB_ATHBUF(skb) = bf;
162 ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
163 skb_queue_tail(&rx_edma->rx_fifo, skb);
164
165 return true;
166}
167
168static void ath_rx_addbuffer_edma(struct ath_softc *sc,
169 enum ath9k_rx_qtype qtype, int size)
170{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400171 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
172 u32 nbuf = 0;
173
Felix Fietkaub5c804752010-04-15 17:38:48 -0400174 if (list_empty(&sc->rx.rxbuf)) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800175 ath_dbg(common, QUEUE, "No free rx buf available\n");
Felix Fietkaub5c804752010-04-15 17:38:48 -0400176 return;
177 }
178
179 while (!list_empty(&sc->rx.rxbuf)) {
180 nbuf++;
181
182 if (!ath_rx_edma_buf_link(sc, qtype))
183 break;
184
185 if (nbuf >= size)
186 break;
187 }
188}
189
190static void ath_rx_remove_buffer(struct ath_softc *sc,
191 enum ath9k_rx_qtype qtype)
192{
193 struct ath_buf *bf;
194 struct ath_rx_edma *rx_edma;
195 struct sk_buff *skb;
196
197 rx_edma = &sc->rx.rx_edma[qtype];
198
199 while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
200 bf = SKB_CB_ATHBUF(skb);
201 BUG_ON(!bf);
202 list_add_tail(&bf->list, &sc->rx.rxbuf);
203 }
204}
205
206static void ath_rx_edma_cleanup(struct ath_softc *sc)
207{
Mohammed Shafi Shajakhanba542382011-09-23 14:33:14 +0530208 struct ath_hw *ah = sc->sc_ah;
209 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400210 struct ath_buf *bf;
211
212 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
213 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
214
215 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
Mohammed Shafi Shajakhanba542382011-09-23 14:33:14 +0530216 if (bf->bf_mpdu) {
217 dma_unmap_single(sc->dev, bf->bf_buf_addr,
218 common->rx_bufsize,
219 DMA_BIDIRECTIONAL);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400220 dev_kfree_skb_any(bf->bf_mpdu);
Mohammed Shafi Shajakhanba542382011-09-23 14:33:14 +0530221 bf->bf_buf_addr = 0;
222 bf->bf_mpdu = NULL;
223 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400224 }
225
226 INIT_LIST_HEAD(&sc->rx.rxbuf);
227
228 kfree(sc->rx.rx_bufptr);
229 sc->rx.rx_bufptr = NULL;
230}
231
232static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
233{
234 skb_queue_head_init(&rx_edma->rx_fifo);
235 skb_queue_head_init(&rx_edma->rx_buffers);
236 rx_edma->rx_fifo_hwsize = size;
237}
238
239static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
240{
241 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
242 struct ath_hw *ah = sc->sc_ah;
243 struct sk_buff *skb;
244 struct ath_buf *bf;
245 int error = 0, i;
246 u32 size;
247
Felix Fietkaub5c804752010-04-15 17:38:48 -0400248 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
249 ah->caps.rx_status_len);
250
251 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
252 ah->caps.rx_lp_qdepth);
253 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
254 ah->caps.rx_hp_qdepth);
255
256 size = sizeof(struct ath_buf) * nbufs;
257 bf = kzalloc(size, GFP_KERNEL);
258 if (!bf)
259 return -ENOMEM;
260
261 INIT_LIST_HEAD(&sc->rx.rxbuf);
262 sc->rx.rx_bufptr = bf;
263
264 for (i = 0; i < nbufs; i++, bf++) {
265 skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
266 if (!skb) {
267 error = -ENOMEM;
268 goto rx_init_fail;
269 }
270
271 memset(skb->data, 0, common->rx_bufsize);
272 bf->bf_mpdu = skb;
273
274 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
275 common->rx_bufsize,
276 DMA_BIDIRECTIONAL);
277 if (unlikely(dma_mapping_error(sc->dev,
278 bf->bf_buf_addr))) {
279 dev_kfree_skb_any(skb);
280 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -0700281 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -0800282 ath_err(common,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400283 "dma_mapping_error() on RX init\n");
284 error = -ENOMEM;
285 goto rx_init_fail;
286 }
287
288 list_add_tail(&bf->list, &sc->rx.rxbuf);
289 }
290
291 return 0;
292
293rx_init_fail:
294 ath_rx_edma_cleanup(sc);
295 return error;
296}
297
298static void ath_edma_start_recv(struct ath_softc *sc)
299{
300 spin_lock_bh(&sc->rx.rxbuflock);
301
302 ath9k_hw_rxena(sc->sc_ah);
303
304 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
305 sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
306
307 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
308 sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
309
Felix Fietkaub5c804752010-04-15 17:38:48 -0400310 ath_opmode_init(sc);
311
Luis R. Rodriguez48a6a462010-09-16 15:12:28 -0400312 ath9k_hw_startpcureceive(sc->sc_ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
Luis R. Rodriguez7583c5502010-10-20 16:07:04 -0700313
314 spin_unlock_bh(&sc->rx.rxbuflock);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400315}
316
317static void ath_edma_stop_recv(struct ath_softc *sc)
318{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400319 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
320 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400321}
322
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700323int ath_rx_init(struct ath_softc *sc, int nbufs)
324{
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -0700325 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700326 struct sk_buff *skb;
327 struct ath_buf *bf;
328 int error = 0;
329
Luis R. Rodriguez4bdd1e92010-10-26 15:27:24 -0700330 spin_lock_init(&sc->sc_pcu_lock);
Sujith797fe5cb2009-03-30 15:28:45 +0530331 sc->sc_flags &= ~SC_OP_RXFLUSH;
332 spin_lock_init(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700333
Felix Fietkau0d955212011-01-26 18:23:27 +0100334 common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
335 sc->sc_ah->caps.rx_status_len;
336
Felix Fietkaub5c804752010-04-15 17:38:48 -0400337 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
338 return ath_rx_edma_init(sc, nbufs);
339 } else {
Joe Perchesd2182b62011-12-15 14:55:53 -0800340 ath_dbg(common, CONFIG, "cachelsz %u rxbufsize %u\n",
Joe Perches226afe62010-12-02 19:12:37 -0800341 common->cachelsz, common->rx_bufsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700342
Felix Fietkaub5c804752010-04-15 17:38:48 -0400343 /* Initialize rx descriptors */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700344
Felix Fietkaub5c804752010-04-15 17:38:48 -0400345 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
Vasanthakumar Thiagarajan4adfcde2010-04-15 17:39:33 -0400346 "rx", nbufs, 1, 0);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400347 if (error != 0) {
Joe Perches38002762010-12-02 19:12:36 -0800348 ath_err(common,
349 "failed to allocate rx descriptors: %d\n",
350 error);
Sujith797fe5cb2009-03-30 15:28:45 +0530351 goto err;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700352 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400353
354 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
355 skb = ath_rxbuf_alloc(common, common->rx_bufsize,
356 GFP_KERNEL);
357 if (skb == NULL) {
358 error = -ENOMEM;
359 goto err;
360 }
361
362 bf->bf_mpdu = skb;
363 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
364 common->rx_bufsize,
365 DMA_FROM_DEVICE);
366 if (unlikely(dma_mapping_error(sc->dev,
367 bf->bf_buf_addr))) {
368 dev_kfree_skb_any(skb);
369 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -0700370 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -0800371 ath_err(common,
372 "dma_mapping_error() on RX init\n");
Felix Fietkaub5c804752010-04-15 17:38:48 -0400373 error = -ENOMEM;
374 goto err;
375 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400376 }
377 sc->rx.rxlink = NULL;
Sujith797fe5cb2009-03-30 15:28:45 +0530378 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700379
Sujith797fe5cb2009-03-30 15:28:45 +0530380err:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700381 if (error)
382 ath_rx_cleanup(sc);
383
384 return error;
385}
386
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700387void ath_rx_cleanup(struct ath_softc *sc)
388{
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800389 struct ath_hw *ah = sc->sc_ah;
390 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700391 struct sk_buff *skb;
392 struct ath_buf *bf;
393
Felix Fietkaub5c804752010-04-15 17:38:48 -0400394 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
395 ath_rx_edma_cleanup(sc);
396 return;
397 } else {
398 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
399 skb = bf->bf_mpdu;
400 if (skb) {
401 dma_unmap_single(sc->dev, bf->bf_buf_addr,
402 common->rx_bufsize,
403 DMA_FROM_DEVICE);
404 dev_kfree_skb(skb);
Ben Greear6cf9e992010-10-14 12:45:30 -0700405 bf->bf_buf_addr = 0;
406 bf->bf_mpdu = NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400407 }
Luis R. Rodriguez051b9192009-03-23 18:25:01 -0400408 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700409
Felix Fietkaub5c804752010-04-15 17:38:48 -0400410 if (sc->rx.rxdma.dd_desc_len != 0)
411 ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
412 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700413}
414
415/*
416 * Calculate the receive filter according to the
417 * operating mode and state:
418 *
419 * o always accept unicast, broadcast, and multicast traffic
420 * o maintain current state of phy error reception (the hal
421 * may enable phy error frames for noise immunity work)
422 * o probe request frames are accepted only when operating in
423 * hostap, adhoc, or monitor modes
424 * o enable promiscuous mode according to the interface state
425 * o accept beacons:
426 * - when operating in adhoc mode so the 802.11 layer creates
427 * node table entries for peers,
428 * - when operating in station mode for collecting rssi data when
429 * the station is otherwise quiet, or
430 * - when operating as a repeater so we see repeater-sta beacons
431 * - when scanning
432 */
433
434u32 ath_calcrxfilter(struct ath_softc *sc)
435{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700436 u32 rfilt;
437
Felix Fietkauac066972011-10-08 15:49:57 +0200438 rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700439 | ATH9K_RX_FILTER_MCAST;
440
Jouni Malinen9c1d8e42010-10-13 17:29:31 +0300441 if (sc->rx.rxfilter & FIF_PROBE_REQ)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700442 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
443
Jouni Malinen217ba9d2009-03-10 10:55:50 +0200444 /*
445 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
446 * mode interface or when in monitor mode. AP mode does not need this
447 * since it receives all in-BSS frames anyway.
448 */
Felix Fietkau2e286942011-03-09 01:48:12 +0100449 if (sc->sc_ah->is_monitoring)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700450 rfilt |= ATH9K_RX_FILTER_PROM;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700451
Sujithd42c6b72009-02-04 08:10:22 +0530452 if (sc->rx.rxfilter & FIF_CONTROL)
453 rfilt |= ATH9K_RX_FILTER_CONTROL;
454
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530455 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
Ben Greearcfda6692010-09-14 12:00:22 -0700456 (sc->nvifs <= 1) &&
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530457 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
458 rfilt |= ATH9K_RX_FILTER_MYBEACON;
459 else
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700460 rfilt |= ATH9K_RX_FILTER_BEACON;
461
Felix Fietkau264bbec2011-04-07 19:24:23 +0200462 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
Senthil Balasubramanian66afad02009-09-18 15:06:07 +0530463 (sc->rx.rxfilter & FIF_PSPOLL))
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530464 rfilt |= ATH9K_RX_FILTER_PSPOLL;
Sujithbe0418a2008-11-18 09:05:55 +0530465
Sujith7ea310b2009-09-03 12:08:43 +0530466 if (conf_is_ht(&sc->hw->conf))
467 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
468
Felix Fietkau7545daf2011-01-24 19:23:16 +0100469 if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
Javier Cardona5eb6ba82009-08-20 19:12:07 -0700470 /* The following may also be needed for other older chips */
471 if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
472 rfilt |= ATH9K_RX_FILTER_PROM;
Jouni Malinenb93bce22009-03-03 19:23:30 +0200473 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
474 }
475
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700476 return rfilt;
Sujith7dcfdcd2008-08-11 14:03:13 +0530477
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700478}
479
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700480int ath_startrecv(struct ath_softc *sc)
481{
Sujithcbe61d82009-02-09 13:27:12 +0530482 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700483 struct ath_buf *bf, *tbf;
484
Felix Fietkaub5c804752010-04-15 17:38:48 -0400485 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
486 ath_edma_start_recv(sc);
487 return 0;
488 }
489
Sujithb77f4832008-12-07 21:44:03 +0530490 spin_lock_bh(&sc->rx.rxbuflock);
491 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700492 goto start_recv;
493
Sujithb77f4832008-12-07 21:44:03 +0530494 sc->rx.rxlink = NULL;
495 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700496 ath_rx_buf_link(sc, bf);
497 }
498
499 /* We could have deleted elements so the list may be empty now */
Sujithb77f4832008-12-07 21:44:03 +0530500 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700501 goto start_recv;
502
Sujithb77f4832008-12-07 21:44:03 +0530503 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700504 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
Sujithbe0418a2008-11-18 09:05:55 +0530505 ath9k_hw_rxena(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700506
507start_recv:
Sujithbe0418a2008-11-18 09:05:55 +0530508 ath_opmode_init(sc);
Luis R. Rodriguez48a6a462010-09-16 15:12:28 -0400509 ath9k_hw_startpcureceive(ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
Sujithbe0418a2008-11-18 09:05:55 +0530510
Luis R. Rodriguez7583c5502010-10-20 16:07:04 -0700511 spin_unlock_bh(&sc->rx.rxbuflock);
512
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700513 return 0;
514}
515
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700516bool ath_stoprecv(struct ath_softc *sc)
517{
Sujithcbe61d82009-02-09 13:27:12 +0530518 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau5882da022011-04-08 20:13:18 +0200519 bool stopped, reset = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700520
Luis R. Rodriguez1e450282010-10-20 16:07:03 -0700521 spin_lock_bh(&sc->rx.rxbuflock);
Felix Fietkaud47844a2010-11-20 03:08:47 +0100522 ath9k_hw_abortpcurecv(ah);
Sujithbe0418a2008-11-18 09:05:55 +0530523 ath9k_hw_setrxfilter(ah, 0);
Felix Fietkau5882da022011-04-08 20:13:18 +0200524 stopped = ath9k_hw_stopdmarecv(ah, &reset);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400525
526 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
527 ath_edma_stop_recv(sc);
528 else
529 sc->rx.rxlink = NULL;
Luis R. Rodriguez1e450282010-10-20 16:07:03 -0700530 spin_unlock_bh(&sc->rx.rxbuflock);
Sujithbe0418a2008-11-18 09:05:55 +0530531
Rajkumar Manoharand5847472010-12-20 14:39:51 +0530532 if (!(ah->ah_flags & AH_UNPLUGGED) &&
533 unlikely(!stopped)) {
Ben Greeard7fd1b502010-12-06 13:13:07 -0800534 ath_err(ath9k_hw_common(sc->sc_ah),
535 "Could not stop RX, we could be "
536 "confusing the DMA engine when we start RX up\n");
537 ATH_DBG_WARN_ON_ONCE(!stopped);
538 }
Felix Fietkau2232d312011-04-15 00:41:43 +0200539 return stopped && !reset;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700540}
541
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700542void ath_flushrecv(struct ath_softc *sc)
543{
Sujith98deeea2008-08-11 14:05:46 +0530544 sc->sc_flags |= SC_OP_RXFLUSH;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400545 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
546 ath_rx_tasklet(sc, 1, true);
547 ath_rx_tasklet(sc, 1, false);
Sujith98deeea2008-08-11 14:05:46 +0530548 sc->sc_flags &= ~SC_OP_RXFLUSH;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700549}
550
Jouni Malinencc659652009-05-14 21:28:48 +0300551static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
552{
553 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
554 struct ieee80211_mgmt *mgmt;
555 u8 *pos, *end, id, elen;
556 struct ieee80211_tim_ie *tim;
557
558 mgmt = (struct ieee80211_mgmt *)skb->data;
559 pos = mgmt->u.beacon.variable;
560 end = skb->data + skb->len;
561
562 while (pos + 2 < end) {
563 id = *pos++;
564 elen = *pos++;
565 if (pos + elen > end)
566 break;
567
568 if (id == WLAN_EID_TIM) {
569 if (elen < sizeof(*tim))
570 break;
571 tim = (struct ieee80211_tim_ie *) pos;
572 if (tim->dtim_count != 0)
573 break;
574 return tim->bitmap_ctrl & 0x01;
575 }
576
577 pos += elen;
578 }
579
580 return false;
581}
582
Jouni Malinencc659652009-05-14 21:28:48 +0300583static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
584{
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700585 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300586
587 if (skb->len < 24 + 8 + 2 + 2)
588 return;
589
Sujith1b04b932010-01-08 10:36:05 +0530590 sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
Gabor Juhos293dc5d2009-06-19 12:17:48 +0200591
Sujith1b04b932010-01-08 10:36:05 +0530592 if (sc->ps_flags & PS_BEACON_SYNC) {
593 sc->ps_flags &= ~PS_BEACON_SYNC;
Joe Perchesd2182b62011-12-15 14:55:53 -0800594 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800595 "Reconfigure Beacon timers based on timestamp from the AP\n");
Rajkumar Manoharan99e4d432011-04-04 22:56:19 +0530596 ath_set_beacon(sc);
Jouni Malinenccdfeab2009-05-20 21:59:08 +0300597 }
598
Jouni Malinencc659652009-05-14 21:28:48 +0300599 if (ath_beacon_dtim_pending_cab(skb)) {
600 /*
601 * Remain awake waiting for buffered broadcast/multicast
Gabor Juhos58f5fff2009-06-17 20:53:20 +0200602 * frames. If the last broadcast/multicast frame is not
603 * received properly, the next beacon frame will work as
604 * a backup trigger for returning into NETWORK SLEEP state,
605 * so we are waiting for it as well.
Jouni Malinencc659652009-05-14 21:28:48 +0300606 */
Joe Perchesd2182b62011-12-15 14:55:53 -0800607 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800608 "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
Sujith1b04b932010-01-08 10:36:05 +0530609 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
Jouni Malinencc659652009-05-14 21:28:48 +0300610 return;
611 }
612
Sujith1b04b932010-01-08 10:36:05 +0530613 if (sc->ps_flags & PS_WAIT_FOR_CAB) {
Jouni Malinencc659652009-05-14 21:28:48 +0300614 /*
615 * This can happen if a broadcast frame is dropped or the AP
616 * fails to send a frame indicating that all CAB frames have
617 * been delivered.
618 */
Sujith1b04b932010-01-08 10:36:05 +0530619 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
Joe Perchesd2182b62011-12-15 14:55:53 -0800620 ath_dbg(common, PS, "PS wait for CAB frames timed out\n");
Jouni Malinencc659652009-05-14 21:28:48 +0300621 }
Jouni Malinencc659652009-05-14 21:28:48 +0300622}
623
Rajkumar Manoharanf73c6042011-09-26 22:16:56 +0530624static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
Jouni Malinencc659652009-05-14 21:28:48 +0300625{
626 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700627 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300628
629 hdr = (struct ieee80211_hdr *)skb->data;
630
631 /* Process Beacon and CAB receive in PS state */
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -0700632 if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
Rajkumar Manoharanf73c6042011-09-26 22:16:56 +0530633 && mybeacon)
Jouni Malinencc659652009-05-14 21:28:48 +0300634 ath_rx_ps_beacon(sc, skb);
Sujith1b04b932010-01-08 10:36:05 +0530635 else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
Jouni Malinencc659652009-05-14 21:28:48 +0300636 (ieee80211_is_data(hdr->frame_control) ||
637 ieee80211_is_action(hdr->frame_control)) &&
638 is_multicast_ether_addr(hdr->addr1) &&
639 !ieee80211_has_moredata(hdr->frame_control)) {
Jouni Malinencc659652009-05-14 21:28:48 +0300640 /*
641 * No more broadcast/multicast frames to be received at this
642 * point.
643 */
Senthil Balasubramanian3fac6df2010-09-16 15:12:35 -0400644 sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
Joe Perchesd2182b62011-12-15 14:55:53 -0800645 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800646 "All PS CAB frames received, back to sleep\n");
Sujith1b04b932010-01-08 10:36:05 +0530647 } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300648 !is_multicast_ether_addr(hdr->addr1) &&
649 !ieee80211_has_morefrags(hdr->frame_control)) {
Sujith1b04b932010-01-08 10:36:05 +0530650 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
Joe Perchesd2182b62011-12-15 14:55:53 -0800651 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800652 "Going back to sleep after having received PS-Poll data (0x%lx)\n",
Sujith1b04b932010-01-08 10:36:05 +0530653 sc->ps_flags & (PS_WAIT_FOR_BEACON |
654 PS_WAIT_FOR_CAB |
655 PS_WAIT_FOR_PSPOLL_DATA |
656 PS_WAIT_FOR_TX_ACK));
Jouni Malinencc659652009-05-14 21:28:48 +0300657 }
658}
659
Felix Fietkaub5c804752010-04-15 17:38:48 -0400660static bool ath_edma_get_buffers(struct ath_softc *sc,
661 enum ath9k_rx_qtype qtype)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700662{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400663 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
664 struct ath_hw *ah = sc->sc_ah;
665 struct ath_common *common = ath9k_hw_common(ah);
666 struct sk_buff *skb;
Sujithbe0418a2008-11-18 09:05:55 +0530667 struct ath_buf *bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400668 int ret;
669
670 skb = skb_peek(&rx_edma->rx_fifo);
671 if (!skb)
672 return false;
673
674 bf = SKB_CB_ATHBUF(skb);
675 BUG_ON(!bf);
676
Ming Leice9426d2010-05-15 18:25:40 +0800677 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400678 common->rx_bufsize, DMA_FROM_DEVICE);
679
680 ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data);
Ming Leice9426d2010-05-15 18:25:40 +0800681 if (ret == -EINPROGRESS) {
682 /*let device gain the buffer again*/
683 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
684 common->rx_bufsize, DMA_FROM_DEVICE);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400685 return false;
Ming Leice9426d2010-05-15 18:25:40 +0800686 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400687
688 __skb_unlink(skb, &rx_edma->rx_fifo);
689 if (ret == -EINVAL) {
690 /* corrupt descriptor, skip this one and the following one */
691 list_add_tail(&bf->list, &sc->rx.rxbuf);
692 ath_rx_edma_buf_link(sc, qtype);
693 skb = skb_peek(&rx_edma->rx_fifo);
694 if (!skb)
695 return true;
696
697 bf = SKB_CB_ATHBUF(skb);
698 BUG_ON(!bf);
699
700 __skb_unlink(skb, &rx_edma->rx_fifo);
701 list_add_tail(&bf->list, &sc->rx.rxbuf);
702 ath_rx_edma_buf_link(sc, qtype);
Vasanthakumar Thiagarajan083e3e82010-05-10 19:41:34 -0700703 return true;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400704 }
705 skb_queue_tail(&rx_edma->rx_buffers, skb);
706
707 return true;
708}
709
710static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
711 struct ath_rx_status *rs,
712 enum ath9k_rx_qtype qtype)
713{
714 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
715 struct sk_buff *skb;
716 struct ath_buf *bf;
717
718 while (ath_edma_get_buffers(sc, qtype));
719 skb = __skb_dequeue(&rx_edma->rx_buffers);
720 if (!skb)
721 return NULL;
722
723 bf = SKB_CB_ATHBUF(skb);
724 ath9k_hw_process_rxdesc_edma(sc->sc_ah, rs, skb->data);
725 return bf;
726}
727
728static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
729 struct ath_rx_status *rs)
730{
731 struct ath_hw *ah = sc->sc_ah;
732 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700733 struct ath_desc *ds;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400734 struct ath_buf *bf;
735 int ret;
736
737 if (list_empty(&sc->rx.rxbuf)) {
738 sc->rx.rxlink = NULL;
739 return NULL;
740 }
741
742 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
743 ds = bf->bf_desc;
744
745 /*
746 * Must provide the virtual address of the current
747 * descriptor, the physical address, and the virtual
748 * address of the next descriptor in the h/w chain.
749 * This allows the HAL to look ahead to see if the
750 * hardware is done with a descriptor by checking the
751 * done bit in the following descriptor and the address
752 * of the current descriptor the DMA engine is working
753 * on. All this is necessary because of our use of
754 * a self-linked list to avoid rx overruns.
755 */
Rajkumar Manoharan3de21112011-08-13 10:28:11 +0530756 ret = ath9k_hw_rxprocdesc(ah, ds, rs);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400757 if (ret == -EINPROGRESS) {
758 struct ath_rx_status trs;
759 struct ath_buf *tbf;
760 struct ath_desc *tds;
761
762 memset(&trs, 0, sizeof(trs));
763 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
764 sc->rx.rxlink = NULL;
765 return NULL;
766 }
767
768 tbf = list_entry(bf->list.next, struct ath_buf, list);
769
770 /*
771 * On some hardware the descriptor status words could
772 * get corrupted, including the done bit. Because of
773 * this, check if the next descriptor's done bit is
774 * set or not.
775 *
776 * If the next descriptor's done bit is set, the current
777 * descriptor has been corrupted. Force s/w to discard
778 * this descriptor and continue...
779 */
780
781 tds = tbf->bf_desc;
Rajkumar Manoharan3de21112011-08-13 10:28:11 +0530782 ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400783 if (ret == -EINPROGRESS)
784 return NULL;
785 }
786
787 if (!bf->bf_mpdu)
788 return bf;
789
790 /*
791 * Synchronize the DMA transfer with CPU before
792 * 1. accessing the frame
793 * 2. requeueing the same buffer to h/w
794 */
Ming Leice9426d2010-05-15 18:25:40 +0800795 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400796 common->rx_bufsize,
797 DMA_FROM_DEVICE);
798
799 return bf;
800}
801
Sujithd4357002010-05-20 15:34:38 +0530802/* Assumes you've already done the endian to CPU conversion */
803static bool ath9k_rx_accept(struct ath_common *common,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700804 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530805 struct ieee80211_rx_status *rxs,
806 struct ath_rx_status *rx_stats,
807 bool *decrypt_error)
808{
Felix Fietkauec205992011-10-08 22:02:59 +0200809 struct ath_softc *sc = (struct ath_softc *) common->priv;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800810 bool is_mc, is_valid_tkip, strip_mic, mic_error;
Sujithd4357002010-05-20 15:34:38 +0530811 struct ath_hw *ah = common->ah;
Sujithd4357002010-05-20 15:34:38 +0530812 __le16 fc;
Vasanthakumar Thiagarajanb7b1b512010-05-20 14:34:48 -0700813 u8 rx_status_len = ah->caps.rx_status_len;
Sujithd4357002010-05-20 15:34:38 +0530814
Sujithd4357002010-05-20 15:34:38 +0530815 fc = hdr->frame_control;
816
Felix Fietkau66760ea2011-07-13 23:35:05 +0800817 is_mc = !!is_multicast_ether_addr(hdr->addr1);
818 is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
819 test_bit(rx_stats->rs_keyix, common->tkip_keymap);
Bill Jordan152e5852011-08-19 11:10:22 -0400820 strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
821 !(rx_stats->rs_status &
Felix Fietkau846d9362011-10-08 22:02:58 +0200822 (ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
823 ATH9K_RXERR_KEYMISS));
Felix Fietkau66760ea2011-07-13 23:35:05 +0800824
Felix Fietkauf88373f2012-02-05 21:15:17 +0100825 /*
826 * Key miss events are only relevant for pairwise keys where the
827 * descriptor does contain a valid key index. This has been observed
828 * mostly with CCMP encryption.
829 */
830 if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID)
831 rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
832
Sujithd4357002010-05-20 15:34:38 +0530833 if (!rx_stats->rs_datalen)
834 return false;
835 /*
836 * rs_status follows rs_datalen so if rs_datalen is too large
837 * we can take a hint that hardware corrupted it, so ignore
838 * those frames.
839 */
Vasanthakumar Thiagarajanb7b1b512010-05-20 14:34:48 -0700840 if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len))
Sujithd4357002010-05-20 15:34:38 +0530841 return false;
842
Felix Fietkau0d955212011-01-26 18:23:27 +0100843 /* Only use error bits from the last fragment */
Sujithd4357002010-05-20 15:34:38 +0530844 if (rx_stats->rs_more)
Felix Fietkau0d955212011-01-26 18:23:27 +0100845 return true;
Sujithd4357002010-05-20 15:34:38 +0530846
Felix Fietkau66760ea2011-07-13 23:35:05 +0800847 mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
848 !ieee80211_has_morefrags(fc) &&
849 !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
850 (rx_stats->rs_status & ATH9K_RXERR_MIC);
851
Sujithd4357002010-05-20 15:34:38 +0530852 /*
853 * The rx_stats->rs_status will not be set until the end of the
854 * chained descriptors so it can be ignored if rs_more is set. The
855 * rs_more will be false at the last element of the chained
856 * descriptors.
857 */
858 if (rx_stats->rs_status != 0) {
Felix Fietkau846d9362011-10-08 22:02:58 +0200859 u8 status_mask;
860
Felix Fietkau66760ea2011-07-13 23:35:05 +0800861 if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
Sujithd4357002010-05-20 15:34:38 +0530862 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800863 mic_error = false;
864 }
Sujithd4357002010-05-20 15:34:38 +0530865 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
866 return false;
867
Felix Fietkau846d9362011-10-08 22:02:58 +0200868 if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
869 (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
Sujithd4357002010-05-20 15:34:38 +0530870 *decrypt_error = true;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800871 mic_error = false;
Sujithd4357002010-05-20 15:34:38 +0530872 }
Felix Fietkau66760ea2011-07-13 23:35:05 +0800873
Sujithd4357002010-05-20 15:34:38 +0530874 /*
875 * Reject error frames with the exception of
876 * decryption and MIC failures. For monitor mode,
877 * we also ignore the CRC error.
878 */
Felix Fietkau846d9362011-10-08 22:02:58 +0200879 status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
880 ATH9K_RXERR_KEYMISS;
881
Felix Fietkauec205992011-10-08 22:02:59 +0200882 if (ah->is_monitoring && (sc->rx.rxfilter & FIF_FCSFAIL))
Felix Fietkau846d9362011-10-08 22:02:58 +0200883 status_mask |= ATH9K_RXERR_CRC;
884
885 if (rx_stats->rs_status & ~status_mask)
886 return false;
Sujithd4357002010-05-20 15:34:38 +0530887 }
Felix Fietkau66760ea2011-07-13 23:35:05 +0800888
889 /*
890 * For unicast frames the MIC error bit can have false positives,
891 * so all MIC error reports need to be validated in software.
892 * False negatives are not common, so skip software verification
893 * if the hardware considers the MIC valid.
894 */
895 if (strip_mic)
896 rxs->flag |= RX_FLAG_MMIC_STRIPPED;
897 else if (is_mc && mic_error)
898 rxs->flag |= RX_FLAG_MMIC_ERROR;
899
Sujithd4357002010-05-20 15:34:38 +0530900 return true;
901}
902
903static int ath9k_process_rate(struct ath_common *common,
904 struct ieee80211_hw *hw,
905 struct ath_rx_status *rx_stats,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700906 struct ieee80211_rx_status *rxs)
Sujithd4357002010-05-20 15:34:38 +0530907{
908 struct ieee80211_supported_band *sband;
909 enum ieee80211_band band;
910 unsigned int i = 0;
911
912 band = hw->conf.channel->band;
913 sband = hw->wiphy->bands[band];
914
915 if (rx_stats->rs_rate & 0x80) {
916 /* HT rate */
917 rxs->flag |= RX_FLAG_HT;
918 if (rx_stats->rs_flags & ATH9K_RX_2040)
919 rxs->flag |= RX_FLAG_40MHZ;
920 if (rx_stats->rs_flags & ATH9K_RX_GI)
921 rxs->flag |= RX_FLAG_SHORT_GI;
922 rxs->rate_idx = rx_stats->rs_rate & 0x7f;
923 return 0;
924 }
925
926 for (i = 0; i < sband->n_bitrates; i++) {
927 if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
928 rxs->rate_idx = i;
929 return 0;
930 }
931 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
932 rxs->flag |= RX_FLAG_SHORTPRE;
933 rxs->rate_idx = i;
934 return 0;
935 }
936 }
937
938 /*
939 * No valid hardware bitrate found -- we should not get here
940 * because hardware has already validated this frame as OK.
941 */
Joe Perchesd2182b62011-12-15 14:55:53 -0800942 ath_dbg(common, ANY,
Joe Perches226afe62010-12-02 19:12:37 -0800943 "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
944 rx_stats->rs_rate);
Sujithd4357002010-05-20 15:34:38 +0530945
946 return -EINVAL;
947}
948
949static void ath9k_process_rssi(struct ath_common *common,
950 struct ieee80211_hw *hw,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700951 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530952 struct ath_rx_status *rx_stats)
953{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100954 struct ath_softc *sc = hw->priv;
Sujithd4357002010-05-20 15:34:38 +0530955 struct ath_hw *ah = common->ah;
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200956 int last_rssi;
Sujithd4357002010-05-20 15:34:38 +0530957
Rajkumar Manoharancf3af742011-08-27 16:17:47 +0530958 if (!rx_stats->is_mybeacon ||
959 ((ah->opmode != NL80211_IFTYPE_STATION) &&
960 (ah->opmode != NL80211_IFTYPE_ADHOC)))
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200961 return;
962
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200963 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
Felix Fietkau9ac586152011-01-24 19:23:18 +0100964 ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
Ben Greear686b9cb2010-09-23 09:44:36 -0700965
Felix Fietkau9ac586152011-01-24 19:23:18 +0100966 last_rssi = sc->last_rssi;
Sujithd4357002010-05-20 15:34:38 +0530967 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
968 rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
969 ATH_RSSI_EP_MULTIPLIER);
970 if (rx_stats->rs_rssi < 0)
971 rx_stats->rs_rssi = 0;
972
973 /* Update Beacon RSSI, this is used by ANI. */
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200974 ah->stats.avgbrssi = rx_stats->rs_rssi;
Sujithd4357002010-05-20 15:34:38 +0530975}
976
977/*
978 * For Decrypt or Demic errors, we only mark packet status here and always push
979 * up the frame up to let mac80211 handle the actual error case, be it no
980 * decryption key or real decryption error. This let us keep statistics there.
981 */
982static int ath9k_rx_skb_preprocess(struct ath_common *common,
983 struct ieee80211_hw *hw,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700984 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530985 struct ath_rx_status *rx_stats,
986 struct ieee80211_rx_status *rx_status,
987 bool *decrypt_error)
988{
Felix Fietkauf749b942011-07-28 14:08:57 +0200989 struct ath_hw *ah = common->ah;
990
Sujithd4357002010-05-20 15:34:38 +0530991 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
992
993 /*
994 * everything but the rate is checked here, the rate check is done
995 * separately to avoid doing two lookups for a rate for each frame.
996 */
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700997 if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
Sujithd4357002010-05-20 15:34:38 +0530998 return -EINVAL;
999
Felix Fietkau0d955212011-01-26 18:23:27 +01001000 /* Only use status info from the last fragment */
1001 if (rx_stats->rs_more)
1002 return 0;
1003
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -07001004 ath9k_process_rssi(common, hw, hdr, rx_stats);
Sujithd4357002010-05-20 15:34:38 +05301005
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -07001006 if (ath9k_process_rate(common, hw, rx_stats, rx_status))
Sujithd4357002010-05-20 15:34:38 +05301007 return -EINVAL;
1008
Sujithd4357002010-05-20 15:34:38 +05301009 rx_status->band = hw->conf.channel->band;
1010 rx_status->freq = hw->conf.channel->center_freq;
Felix Fietkauf749b942011-07-28 14:08:57 +02001011 rx_status->signal = ah->noise + rx_stats->rs_rssi;
Sujithd4357002010-05-20 15:34:38 +05301012 rx_status->antenna = rx_stats->rs_antenna;
Johannes Berg6ebacbb2011-02-23 15:06:08 +01001013 rx_status->flag |= RX_FLAG_MACTIME_MPDU;
Sujithd4357002010-05-20 15:34:38 +05301014
1015 return 0;
1016}
1017
1018static void ath9k_rx_skb_postprocess(struct ath_common *common,
1019 struct sk_buff *skb,
1020 struct ath_rx_status *rx_stats,
1021 struct ieee80211_rx_status *rxs,
1022 bool decrypt_error)
1023{
1024 struct ath_hw *ah = common->ah;
1025 struct ieee80211_hdr *hdr;
1026 int hdrlen, padpos, padsize;
1027 u8 keyix;
1028 __le16 fc;
1029
1030 /* see if any padding is done by the hw and remove it */
1031 hdr = (struct ieee80211_hdr *) skb->data;
1032 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1033 fc = hdr->frame_control;
1034 padpos = ath9k_cmn_padpos(hdr->frame_control);
1035
1036 /* The MAC header is padded to have 32-bit boundary if the
1037 * packet payload is non-zero. The general calculation for
1038 * padsize would take into account odd header lengths:
1039 * padsize = (4 - padpos % 4) % 4; However, since only
1040 * even-length headers are used, padding can only be 0 or 2
1041 * bytes and we can optimize this a bit. In addition, we must
1042 * not try to remove padding from short control frames that do
1043 * not have payload. */
1044 padsize = padpos & 3;
1045 if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1046 memmove(skb->data + padsize, skb->data, padpos);
1047 skb_pull(skb, padsize);
1048 }
1049
1050 keyix = rx_stats->rs_keyix;
1051
1052 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1053 ieee80211_has_protected(fc)) {
1054 rxs->flag |= RX_FLAG_DECRYPTED;
1055 } else if (ieee80211_has_protected(fc)
1056 && !decrypt_error && skb->len >= hdrlen + 4) {
1057 keyix = skb->data[hdrlen + 3] >> 6;
1058
1059 if (test_bit(keyix, common->keymap))
1060 rxs->flag |= RX_FLAG_DECRYPTED;
1061 }
1062 if (ah->sw_mgmt_crypto &&
1063 (rxs->flag & RX_FLAG_DECRYPTED) &&
1064 ieee80211_is_mgmt(fc))
1065 /* Use software decrypt for management frames. */
1066 rxs->flag &= ~RX_FLAG_DECRYPTED;
1067}
Felix Fietkaub5c804752010-04-15 17:38:48 -04001068
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001069static void ath_lnaconf_alt_good_scan(struct ath_ant_comb *antcomb,
1070 struct ath_hw_antcomb_conf ant_conf,
1071 int main_rssi_avg)
1072{
1073 antcomb->quick_scan_cnt = 0;
1074
1075 if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
1076 antcomb->rssi_lna2 = main_rssi_avg;
1077 else if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA1)
1078 antcomb->rssi_lna1 = main_rssi_avg;
1079
1080 switch ((ant_conf.main_lna_conf << 4) | ant_conf.alt_lna_conf) {
Gabor Juhos223c5a82011-06-21 11:23:45 +02001081 case 0x10: /* LNA2 A-B */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001082 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1083 antcomb->first_quick_scan_conf =
1084 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1085 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1086 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001087 case 0x20: /* LNA1 A-B */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001088 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1089 antcomb->first_quick_scan_conf =
1090 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1091 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1092 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001093 case 0x21: /* LNA1 LNA2 */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001094 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA2;
1095 antcomb->first_quick_scan_conf =
1096 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1097 antcomb->second_quick_scan_conf =
1098 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1099 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001100 case 0x12: /* LNA2 LNA1 */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001101 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1;
1102 antcomb->first_quick_scan_conf =
1103 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1104 antcomb->second_quick_scan_conf =
1105 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1106 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001107 case 0x13: /* LNA2 A+B */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001108 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1109 antcomb->first_quick_scan_conf =
1110 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1111 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1112 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001113 case 0x23: /* LNA1 A+B */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001114 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1115 antcomb->first_quick_scan_conf =
1116 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1117 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1118 break;
1119 default:
1120 break;
1121 }
1122}
1123
1124static void ath_select_ant_div_from_quick_scan(struct ath_ant_comb *antcomb,
1125 struct ath_hw_antcomb_conf *div_ant_conf,
1126 int main_rssi_avg, int alt_rssi_avg,
1127 int alt_ratio)
1128{
1129 /* alt_good */
1130 switch (antcomb->quick_scan_cnt) {
1131 case 0:
1132 /* set alt to main, and alt to first conf */
1133 div_ant_conf->main_lna_conf = antcomb->main_conf;
1134 div_ant_conf->alt_lna_conf = antcomb->first_quick_scan_conf;
1135 break;
1136 case 1:
1137 /* set alt to main, and alt to first conf */
1138 div_ant_conf->main_lna_conf = antcomb->main_conf;
1139 div_ant_conf->alt_lna_conf = antcomb->second_quick_scan_conf;
1140 antcomb->rssi_first = main_rssi_avg;
1141 antcomb->rssi_second = alt_rssi_avg;
1142
1143 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1144 /* main is LNA1 */
1145 if (ath_is_alt_ant_ratio_better(alt_ratio,
1146 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1147 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1148 main_rssi_avg, alt_rssi_avg,
1149 antcomb->total_pkt_count))
1150 antcomb->first_ratio = true;
1151 else
1152 antcomb->first_ratio = false;
1153 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1154 if (ath_is_alt_ant_ratio_better(alt_ratio,
1155 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1156 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1157 main_rssi_avg, alt_rssi_avg,
1158 antcomb->total_pkt_count))
1159 antcomb->first_ratio = true;
1160 else
1161 antcomb->first_ratio = false;
1162 } else {
1163 if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1164 (alt_rssi_avg > main_rssi_avg +
1165 ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1166 (alt_rssi_avg > main_rssi_avg)) &&
1167 (antcomb->total_pkt_count > 50))
1168 antcomb->first_ratio = true;
1169 else
1170 antcomb->first_ratio = false;
1171 }
1172 break;
1173 case 2:
1174 antcomb->alt_good = false;
1175 antcomb->scan_not_start = false;
1176 antcomb->scan = false;
1177 antcomb->rssi_first = main_rssi_avg;
1178 antcomb->rssi_third = alt_rssi_avg;
1179
1180 if (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1)
1181 antcomb->rssi_lna1 = alt_rssi_avg;
1182 else if (antcomb->second_quick_scan_conf ==
1183 ATH_ANT_DIV_COMB_LNA2)
1184 antcomb->rssi_lna2 = alt_rssi_avg;
1185 else if (antcomb->second_quick_scan_conf ==
1186 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2) {
1187 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)
1188 antcomb->rssi_lna2 = main_rssi_avg;
1189 else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1)
1190 antcomb->rssi_lna1 = main_rssi_avg;
1191 }
1192
1193 if (antcomb->rssi_lna2 > antcomb->rssi_lna1 +
1194 ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)
1195 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1196 else
1197 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
1198
1199 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1200 if (ath_is_alt_ant_ratio_better(alt_ratio,
1201 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1202 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1203 main_rssi_avg, alt_rssi_avg,
1204 antcomb->total_pkt_count))
1205 antcomb->second_ratio = true;
1206 else
1207 antcomb->second_ratio = false;
1208 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1209 if (ath_is_alt_ant_ratio_better(alt_ratio,
1210 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1211 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1212 main_rssi_avg, alt_rssi_avg,
1213 antcomb->total_pkt_count))
1214 antcomb->second_ratio = true;
1215 else
1216 antcomb->second_ratio = false;
1217 } else {
1218 if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1219 (alt_rssi_avg > main_rssi_avg +
1220 ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1221 (alt_rssi_avg > main_rssi_avg)) &&
1222 (antcomb->total_pkt_count > 50))
1223 antcomb->second_ratio = true;
1224 else
1225 antcomb->second_ratio = false;
1226 }
1227
1228 /* set alt to the conf with maximun ratio */
1229 if (antcomb->first_ratio && antcomb->second_ratio) {
1230 if (antcomb->rssi_second > antcomb->rssi_third) {
1231 /* first alt*/
1232 if ((antcomb->first_quick_scan_conf ==
1233 ATH_ANT_DIV_COMB_LNA1) ||
1234 (antcomb->first_quick_scan_conf ==
1235 ATH_ANT_DIV_COMB_LNA2))
1236 /* Set alt LNA1 or LNA2*/
1237 if (div_ant_conf->main_lna_conf ==
1238 ATH_ANT_DIV_COMB_LNA2)
1239 div_ant_conf->alt_lna_conf =
1240 ATH_ANT_DIV_COMB_LNA1;
1241 else
1242 div_ant_conf->alt_lna_conf =
1243 ATH_ANT_DIV_COMB_LNA2;
1244 else
1245 /* Set alt to A+B or A-B */
1246 div_ant_conf->alt_lna_conf =
1247 antcomb->first_quick_scan_conf;
1248 } else if ((antcomb->second_quick_scan_conf ==
1249 ATH_ANT_DIV_COMB_LNA1) ||
1250 (antcomb->second_quick_scan_conf ==
1251 ATH_ANT_DIV_COMB_LNA2)) {
1252 /* Set alt LNA1 or LNA2 */
1253 if (div_ant_conf->main_lna_conf ==
1254 ATH_ANT_DIV_COMB_LNA2)
1255 div_ant_conf->alt_lna_conf =
1256 ATH_ANT_DIV_COMB_LNA1;
1257 else
1258 div_ant_conf->alt_lna_conf =
1259 ATH_ANT_DIV_COMB_LNA2;
1260 } else {
1261 /* Set alt to A+B or A-B */
1262 div_ant_conf->alt_lna_conf =
1263 antcomb->second_quick_scan_conf;
1264 }
1265 } else if (antcomb->first_ratio) {
1266 /* first alt */
1267 if ((antcomb->first_quick_scan_conf ==
1268 ATH_ANT_DIV_COMB_LNA1) ||
1269 (antcomb->first_quick_scan_conf ==
1270 ATH_ANT_DIV_COMB_LNA2))
1271 /* Set alt LNA1 or LNA2 */
1272 if (div_ant_conf->main_lna_conf ==
1273 ATH_ANT_DIV_COMB_LNA2)
1274 div_ant_conf->alt_lna_conf =
1275 ATH_ANT_DIV_COMB_LNA1;
1276 else
1277 div_ant_conf->alt_lna_conf =
1278 ATH_ANT_DIV_COMB_LNA2;
1279 else
1280 /* Set alt to A+B or A-B */
1281 div_ant_conf->alt_lna_conf =
1282 antcomb->first_quick_scan_conf;
1283 } else if (antcomb->second_ratio) {
1284 /* second alt */
1285 if ((antcomb->second_quick_scan_conf ==
1286 ATH_ANT_DIV_COMB_LNA1) ||
1287 (antcomb->second_quick_scan_conf ==
1288 ATH_ANT_DIV_COMB_LNA2))
1289 /* Set alt LNA1 or LNA2 */
1290 if (div_ant_conf->main_lna_conf ==
1291 ATH_ANT_DIV_COMB_LNA2)
1292 div_ant_conf->alt_lna_conf =
1293 ATH_ANT_DIV_COMB_LNA1;
1294 else
1295 div_ant_conf->alt_lna_conf =
1296 ATH_ANT_DIV_COMB_LNA2;
1297 else
1298 /* Set alt to A+B or A-B */
1299 div_ant_conf->alt_lna_conf =
1300 antcomb->second_quick_scan_conf;
1301 } else {
1302 /* main is largest */
1303 if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
1304 (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
1305 /* Set alt LNA1 or LNA2 */
1306 if (div_ant_conf->main_lna_conf ==
1307 ATH_ANT_DIV_COMB_LNA2)
1308 div_ant_conf->alt_lna_conf =
1309 ATH_ANT_DIV_COMB_LNA1;
1310 else
1311 div_ant_conf->alt_lna_conf =
1312 ATH_ANT_DIV_COMB_LNA2;
1313 else
1314 /* Set alt to A+B or A-B */
1315 div_ant_conf->alt_lna_conf = antcomb->main_conf;
1316 }
1317 break;
1318 default:
1319 break;
1320 }
1321}
1322
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301323static void ath_ant_div_conf_fast_divbias(struct ath_hw_antcomb_conf *ant_conf,
1324 struct ath_ant_comb *antcomb, int alt_ratio)
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001325{
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301326 if (ant_conf->div_group == 0) {
1327 /* Adjust the fast_div_bias based on main and alt lna conf */
1328 switch ((ant_conf->main_lna_conf << 4) |
1329 ant_conf->alt_lna_conf) {
Gabor Juhos223c5a82011-06-21 11:23:45 +02001330 case 0x01: /* A-B LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301331 ant_conf->fast_div_bias = 0x3b;
1332 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001333 case 0x02: /* A-B LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301334 ant_conf->fast_div_bias = 0x3d;
1335 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001336 case 0x03: /* A-B A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301337 ant_conf->fast_div_bias = 0x1;
1338 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001339 case 0x10: /* LNA2 A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301340 ant_conf->fast_div_bias = 0x7;
1341 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001342 case 0x12: /* LNA2 LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301343 ant_conf->fast_div_bias = 0x2;
1344 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001345 case 0x13: /* LNA2 A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301346 ant_conf->fast_div_bias = 0x7;
1347 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001348 case 0x20: /* LNA1 A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301349 ant_conf->fast_div_bias = 0x6;
1350 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001351 case 0x21: /* LNA1 LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301352 ant_conf->fast_div_bias = 0x0;
1353 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001354 case 0x23: /* LNA1 A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301355 ant_conf->fast_div_bias = 0x6;
1356 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001357 case 0x30: /* A+B A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301358 ant_conf->fast_div_bias = 0x1;
1359 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001360 case 0x31: /* A+B LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301361 ant_conf->fast_div_bias = 0x3b;
1362 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001363 case 0x32: /* A+B LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301364 ant_conf->fast_div_bias = 0x3d;
1365 break;
1366 default:
1367 break;
1368 }
Gabor Juhose7ef5bc2011-06-21 11:23:46 +02001369 } else if (ant_conf->div_group == 1) {
1370 /* Adjust the fast_div_bias based on main and alt_lna_conf */
1371 switch ((ant_conf->main_lna_conf << 4) |
1372 ant_conf->alt_lna_conf) {
1373 case 0x01: /* A-B LNA2 */
1374 ant_conf->fast_div_bias = 0x1;
1375 ant_conf->main_gaintb = 0;
1376 ant_conf->alt_gaintb = 0;
1377 break;
1378 case 0x02: /* A-B LNA1 */
1379 ant_conf->fast_div_bias = 0x1;
1380 ant_conf->main_gaintb = 0;
1381 ant_conf->alt_gaintb = 0;
1382 break;
1383 case 0x03: /* A-B A+B */
1384 ant_conf->fast_div_bias = 0x1;
1385 ant_conf->main_gaintb = 0;
1386 ant_conf->alt_gaintb = 0;
1387 break;
1388 case 0x10: /* LNA2 A-B */
1389 if (!(antcomb->scan) &&
1390 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1391 ant_conf->fast_div_bias = 0x3f;
1392 else
1393 ant_conf->fast_div_bias = 0x1;
1394 ant_conf->main_gaintb = 0;
1395 ant_conf->alt_gaintb = 0;
1396 break;
1397 case 0x12: /* LNA2 LNA1 */
1398 ant_conf->fast_div_bias = 0x1;
1399 ant_conf->main_gaintb = 0;
1400 ant_conf->alt_gaintb = 0;
1401 break;
1402 case 0x13: /* LNA2 A+B */
1403 if (!(antcomb->scan) &&
1404 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1405 ant_conf->fast_div_bias = 0x3f;
1406 else
1407 ant_conf->fast_div_bias = 0x1;
1408 ant_conf->main_gaintb = 0;
1409 ant_conf->alt_gaintb = 0;
1410 break;
1411 case 0x20: /* LNA1 A-B */
1412 if (!(antcomb->scan) &&
1413 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1414 ant_conf->fast_div_bias = 0x3f;
1415 else
1416 ant_conf->fast_div_bias = 0x1;
1417 ant_conf->main_gaintb = 0;
1418 ant_conf->alt_gaintb = 0;
1419 break;
1420 case 0x21: /* LNA1 LNA2 */
1421 ant_conf->fast_div_bias = 0x1;
1422 ant_conf->main_gaintb = 0;
1423 ant_conf->alt_gaintb = 0;
1424 break;
1425 case 0x23: /* LNA1 A+B */
1426 if (!(antcomb->scan) &&
1427 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1428 ant_conf->fast_div_bias = 0x3f;
1429 else
1430 ant_conf->fast_div_bias = 0x1;
1431 ant_conf->main_gaintb = 0;
1432 ant_conf->alt_gaintb = 0;
1433 break;
1434 case 0x30: /* A+B A-B */
1435 ant_conf->fast_div_bias = 0x1;
1436 ant_conf->main_gaintb = 0;
1437 ant_conf->alt_gaintb = 0;
1438 break;
1439 case 0x31: /* A+B LNA2 */
1440 ant_conf->fast_div_bias = 0x1;
1441 ant_conf->main_gaintb = 0;
1442 ant_conf->alt_gaintb = 0;
1443 break;
1444 case 0x32: /* A+B LNA1 */
1445 ant_conf->fast_div_bias = 0x1;
1446 ant_conf->main_gaintb = 0;
1447 ant_conf->alt_gaintb = 0;
1448 break;
1449 default:
1450 break;
1451 }
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301452 } else if (ant_conf->div_group == 2) {
1453 /* Adjust the fast_div_bias based on main and alt_lna_conf */
1454 switch ((ant_conf->main_lna_conf << 4) |
1455 ant_conf->alt_lna_conf) {
Gabor Juhos223c5a82011-06-21 11:23:45 +02001456 case 0x01: /* A-B LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301457 ant_conf->fast_div_bias = 0x1;
1458 ant_conf->main_gaintb = 0;
1459 ant_conf->alt_gaintb = 0;
1460 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001461 case 0x02: /* A-B LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301462 ant_conf->fast_div_bias = 0x1;
1463 ant_conf->main_gaintb = 0;
1464 ant_conf->alt_gaintb = 0;
1465 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001466 case 0x03: /* A-B A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301467 ant_conf->fast_div_bias = 0x1;
1468 ant_conf->main_gaintb = 0;
1469 ant_conf->alt_gaintb = 0;
1470 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001471 case 0x10: /* LNA2 A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301472 if (!(antcomb->scan) &&
1473 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1474 ant_conf->fast_div_bias = 0x1;
1475 else
1476 ant_conf->fast_div_bias = 0x2;
1477 ant_conf->main_gaintb = 0;
1478 ant_conf->alt_gaintb = 0;
1479 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001480 case 0x12: /* LNA2 LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301481 ant_conf->fast_div_bias = 0x1;
1482 ant_conf->main_gaintb = 0;
1483 ant_conf->alt_gaintb = 0;
1484 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001485 case 0x13: /* LNA2 A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301486 if (!(antcomb->scan) &&
1487 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1488 ant_conf->fast_div_bias = 0x1;
1489 else
1490 ant_conf->fast_div_bias = 0x2;
1491 ant_conf->main_gaintb = 0;
1492 ant_conf->alt_gaintb = 0;
1493 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001494 case 0x20: /* LNA1 A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301495 if (!(antcomb->scan) &&
1496 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1497 ant_conf->fast_div_bias = 0x1;
1498 else
1499 ant_conf->fast_div_bias = 0x2;
1500 ant_conf->main_gaintb = 0;
1501 ant_conf->alt_gaintb = 0;
1502 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001503 case 0x21: /* LNA1 LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301504 ant_conf->fast_div_bias = 0x1;
1505 ant_conf->main_gaintb = 0;
1506 ant_conf->alt_gaintb = 0;
1507 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001508 case 0x23: /* LNA1 A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301509 if (!(antcomb->scan) &&
1510 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1511 ant_conf->fast_div_bias = 0x1;
1512 else
1513 ant_conf->fast_div_bias = 0x2;
1514 ant_conf->main_gaintb = 0;
1515 ant_conf->alt_gaintb = 0;
1516 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001517 case 0x30: /* A+B A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301518 ant_conf->fast_div_bias = 0x1;
1519 ant_conf->main_gaintb = 0;
1520 ant_conf->alt_gaintb = 0;
1521 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001522 case 0x31: /* A+B LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301523 ant_conf->fast_div_bias = 0x1;
1524 ant_conf->main_gaintb = 0;
1525 ant_conf->alt_gaintb = 0;
1526 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001527 case 0x32: /* A+B LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301528 ant_conf->fast_div_bias = 0x1;
1529 ant_conf->main_gaintb = 0;
1530 ant_conf->alt_gaintb = 0;
1531 break;
1532 default:
1533 break;
1534 }
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001535 }
1536}
1537
1538/* Antenna diversity and combining */
1539static void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs)
1540{
1541 struct ath_hw_antcomb_conf div_ant_conf;
1542 struct ath_ant_comb *antcomb = &sc->ant_comb;
1543 int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set;
Sujith Manoharan0ff2b5c2011-04-20 11:00:34 +05301544 int curr_main_set;
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001545 int main_rssi = rs->rs_rssi_ctl0;
1546 int alt_rssi = rs->rs_rssi_ctl1;
1547 int rx_ant_conf, main_ant_conf;
1548 bool short_scan = false;
1549
1550 rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) &
1551 ATH_ANT_RX_MASK;
1552 main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) &
1553 ATH_ANT_RX_MASK;
1554
Mohammed Shafi Shajakhan21e8ee62011-05-13 20:31:40 +05301555 /* Record packet only when both main_rssi and alt_rssi is positive */
1556 if (main_rssi > 0 && alt_rssi > 0) {
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001557 antcomb->total_pkt_count++;
1558 antcomb->main_total_rssi += main_rssi;
1559 antcomb->alt_total_rssi += alt_rssi;
1560 if (main_ant_conf == rx_ant_conf)
1561 antcomb->main_recv_cnt++;
1562 else
1563 antcomb->alt_recv_cnt++;
1564 }
1565
1566 /* Short scan check */
1567 if (antcomb->scan && antcomb->alt_good) {
1568 if (time_after(jiffies, antcomb->scan_start_time +
1569 msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
1570 short_scan = true;
1571 else
1572 if (antcomb->total_pkt_count ==
1573 ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
1574 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1575 antcomb->total_pkt_count);
1576 if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
1577 short_scan = true;
1578 }
1579 }
1580
1581 if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
1582 rs->rs_moreaggr) && !short_scan)
1583 return;
1584
1585 if (antcomb->total_pkt_count) {
1586 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1587 antcomb->total_pkt_count);
1588 main_rssi_avg = (antcomb->main_total_rssi /
1589 antcomb->total_pkt_count);
1590 alt_rssi_avg = (antcomb->alt_total_rssi /
1591 antcomb->total_pkt_count);
1592 }
1593
1594
1595 ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf);
1596 curr_alt_set = div_ant_conf.alt_lna_conf;
1597 curr_main_set = div_ant_conf.main_lna_conf;
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001598
1599 antcomb->count++;
1600
1601 if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
1602 if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1603 ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
1604 main_rssi_avg);
1605 antcomb->alt_good = true;
1606 } else {
1607 antcomb->alt_good = false;
1608 }
1609
1610 antcomb->count = 0;
1611 antcomb->scan = true;
1612 antcomb->scan_not_start = true;
1613 }
1614
1615 if (!antcomb->scan) {
Mohammed Shafi Shajakhanb85c5732011-05-13 20:31:09 +05301616 if (ath_ant_div_comb_alt_check(div_ant_conf.div_group,
1617 alt_ratio, curr_main_set, curr_alt_set,
1618 alt_rssi_avg, main_rssi_avg)) {
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001619 if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
1620 /* Switch main and alt LNA */
1621 div_ant_conf.main_lna_conf =
1622 ATH_ANT_DIV_COMB_LNA2;
1623 div_ant_conf.alt_lna_conf =
1624 ATH_ANT_DIV_COMB_LNA1;
1625 } else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
1626 div_ant_conf.main_lna_conf =
1627 ATH_ANT_DIV_COMB_LNA1;
1628 div_ant_conf.alt_lna_conf =
1629 ATH_ANT_DIV_COMB_LNA2;
1630 }
1631
1632 goto div_comb_done;
1633 } else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
1634 (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
1635 /* Set alt to another LNA */
1636 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
1637 div_ant_conf.alt_lna_conf =
1638 ATH_ANT_DIV_COMB_LNA1;
1639 else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
1640 div_ant_conf.alt_lna_conf =
1641 ATH_ANT_DIV_COMB_LNA2;
1642
1643 goto div_comb_done;
1644 }
1645
1646 if ((alt_rssi_avg < (main_rssi_avg +
Mohammed Shafi Shajakhan8afbcc82011-05-13 20:30:56 +05301647 div_ant_conf.lna1_lna2_delta)))
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001648 goto div_comb_done;
1649 }
1650
1651 if (!antcomb->scan_not_start) {
1652 switch (curr_alt_set) {
1653 case ATH_ANT_DIV_COMB_LNA2:
1654 antcomb->rssi_lna2 = alt_rssi_avg;
1655 antcomb->rssi_lna1 = main_rssi_avg;
1656 antcomb->scan = true;
1657 /* set to A+B */
1658 div_ant_conf.main_lna_conf =
1659 ATH_ANT_DIV_COMB_LNA1;
1660 div_ant_conf.alt_lna_conf =
1661 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1662 break;
1663 case ATH_ANT_DIV_COMB_LNA1:
1664 antcomb->rssi_lna1 = alt_rssi_avg;
1665 antcomb->rssi_lna2 = main_rssi_avg;
1666 antcomb->scan = true;
1667 /* set to A+B */
1668 div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1669 div_ant_conf.alt_lna_conf =
1670 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1671 break;
1672 case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
1673 antcomb->rssi_add = alt_rssi_avg;
1674 antcomb->scan = true;
1675 /* set to A-B */
1676 div_ant_conf.alt_lna_conf =
1677 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1678 break;
1679 case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
1680 antcomb->rssi_sub = alt_rssi_avg;
1681 antcomb->scan = false;
1682 if (antcomb->rssi_lna2 >
1683 (antcomb->rssi_lna1 +
1684 ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
1685 /* use LNA2 as main LNA */
1686 if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
1687 (antcomb->rssi_add > antcomb->rssi_sub)) {
1688 /* set to A+B */
1689 div_ant_conf.main_lna_conf =
1690 ATH_ANT_DIV_COMB_LNA2;
1691 div_ant_conf.alt_lna_conf =
1692 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1693 } else if (antcomb->rssi_sub >
1694 antcomb->rssi_lna1) {
1695 /* set to A-B */
1696 div_ant_conf.main_lna_conf =
1697 ATH_ANT_DIV_COMB_LNA2;
1698 div_ant_conf.alt_lna_conf =
1699 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1700 } else {
1701 /* set to LNA1 */
1702 div_ant_conf.main_lna_conf =
1703 ATH_ANT_DIV_COMB_LNA2;
1704 div_ant_conf.alt_lna_conf =
1705 ATH_ANT_DIV_COMB_LNA1;
1706 }
1707 } else {
1708 /* use LNA1 as main LNA */
1709 if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
1710 (antcomb->rssi_add > antcomb->rssi_sub)) {
1711 /* set to A+B */
1712 div_ant_conf.main_lna_conf =
1713 ATH_ANT_DIV_COMB_LNA1;
1714 div_ant_conf.alt_lna_conf =
1715 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1716 } else if (antcomb->rssi_sub >
1717 antcomb->rssi_lna1) {
1718 /* set to A-B */
1719 div_ant_conf.main_lna_conf =
1720 ATH_ANT_DIV_COMB_LNA1;
1721 div_ant_conf.alt_lna_conf =
1722 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1723 } else {
1724 /* set to LNA2 */
1725 div_ant_conf.main_lna_conf =
1726 ATH_ANT_DIV_COMB_LNA1;
1727 div_ant_conf.alt_lna_conf =
1728 ATH_ANT_DIV_COMB_LNA2;
1729 }
1730 }
1731 break;
1732 default:
1733 break;
1734 }
1735 } else {
1736 if (!antcomb->alt_good) {
1737 antcomb->scan_not_start = false;
1738 /* Set alt to another LNA */
1739 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
1740 div_ant_conf.main_lna_conf =
1741 ATH_ANT_DIV_COMB_LNA2;
1742 div_ant_conf.alt_lna_conf =
1743 ATH_ANT_DIV_COMB_LNA1;
1744 } else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
1745 div_ant_conf.main_lna_conf =
1746 ATH_ANT_DIV_COMB_LNA1;
1747 div_ant_conf.alt_lna_conf =
1748 ATH_ANT_DIV_COMB_LNA2;
1749 }
1750 goto div_comb_done;
1751 }
1752 }
1753
1754 ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
1755 main_rssi_avg, alt_rssi_avg,
1756 alt_ratio);
1757
1758 antcomb->quick_scan_cnt++;
1759
1760div_comb_done:
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301761 ath_ant_div_conf_fast_divbias(&div_ant_conf, antcomb, alt_ratio);
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001762 ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf);
1763
1764 antcomb->scan_start_time = jiffies;
1765 antcomb->total_pkt_count = 0;
1766 antcomb->main_total_rssi = 0;
1767 antcomb->alt_total_rssi = 0;
1768 antcomb->main_recv_cnt = 0;
1769 antcomb->alt_recv_cnt = 0;
1770}
1771
Felix Fietkaub5c804752010-04-15 17:38:48 -04001772int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1773{
1774 struct ath_buf *bf;
Felix Fietkau0d955212011-01-26 18:23:27 +01001775 struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001776 struct ieee80211_rx_status *rxs;
Sujithcbe61d82009-02-09 13:27:12 +05301777 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -07001778 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau7545daf2011-01-24 19:23:16 +01001779 struct ieee80211_hw *hw = sc->hw;
Sujithbe0418a2008-11-18 09:05:55 +05301780 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc9b14172009-11-04 16:47:22 -08001781 int retval;
Sujithbe0418a2008-11-18 09:05:55 +05301782 bool decrypt_error = false;
Felix Fietkau29bffa92010-03-29 20:14:23 -07001783 struct ath_rx_status rs;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001784 enum ath9k_rx_qtype qtype;
1785 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1786 int dma_type;
Vasanthakumar Thiagarajan5c6dd922010-05-20 14:34:47 -07001787 u8 rx_status_len = ah->caps.rx_status_len;
Felix Fietkaua6d20552010-06-12 00:33:54 -04001788 u64 tsf = 0;
1789 u32 tsf_lower = 0;
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001790 unsigned long flags;
Sujithbe0418a2008-11-18 09:05:55 +05301791
Felix Fietkaub5c804752010-04-15 17:38:48 -04001792 if (edma)
Felix Fietkaub5c804752010-04-15 17:38:48 -04001793 dma_type = DMA_BIDIRECTIONAL;
Ming Lei56824222010-05-14 21:15:38 +08001794 else
1795 dma_type = DMA_FROM_DEVICE;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001796
1797 qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
Sujithb77f4832008-12-07 21:44:03 +05301798 spin_lock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001799
Felix Fietkaua6d20552010-06-12 00:33:54 -04001800 tsf = ath9k_hw_gettsf64(ah);
1801 tsf_lower = tsf & 0xffffffff;
1802
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001803 do {
1804 /* If handling rx interrupt and flush is in progress => exit */
Sujith98deeea2008-08-11 14:05:46 +05301805 if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001806 break;
1807
Felix Fietkau29bffa92010-03-29 20:14:23 -07001808 memset(&rs, 0, sizeof(rs));
Felix Fietkaub5c804752010-04-15 17:38:48 -04001809 if (edma)
1810 bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1811 else
1812 bf = ath_get_next_rx_buf(sc, &rs);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001813
Felix Fietkaub5c804752010-04-15 17:38:48 -04001814 if (!bf)
1815 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001816
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001817 skb = bf->bf_mpdu;
Sujithbe0418a2008-11-18 09:05:55 +05301818 if (!skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001819 continue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001820
Felix Fietkau0d955212011-01-26 18:23:27 +01001821 /*
1822 * Take frame header from the first fragment and RX status from
1823 * the last one.
1824 */
1825 if (sc->rx.frag)
1826 hdr_skb = sc->rx.frag;
1827 else
1828 hdr_skb = skb;
1829
1830 hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
1831 rxs = IEEE80211_SKB_RXCB(hdr_skb);
Rajkumar Manoharancf3af742011-08-27 16:17:47 +05301832 if (ieee80211_is_beacon(hdr->frame_control) &&
Mohammed Shafi Shajakhan356cb552011-12-07 16:51:38 +05301833 !is_zero_ether_addr(common->curbssid) &&
Rajkumar Manoharancf3af742011-08-27 16:17:47 +05301834 !compare_ether_addr(hdr->addr3, common->curbssid))
1835 rs.is_mybeacon = true;
1836 else
1837 rs.is_mybeacon = false;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001838
Felix Fietkau29bffa92010-03-29 20:14:23 -07001839 ath_debug_stat_rx(sc, &rs);
Sujith1395d3f2010-01-08 10:36:11 +05301840
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301841 /*
Sujithbe0418a2008-11-18 09:05:55 +05301842 * If we're asked to flush receive queue, directly
1843 * chain it back at the queue without processing it.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001844 */
Felix Fietkau34832882011-09-14 21:23:03 +02001845 if (sc->sc_flags & SC_OP_RXFLUSH)
Felix Fietkau0d955212011-01-26 18:23:27 +01001846 goto requeue_drop_frag;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001847
Felix Fietkaua6d20552010-06-12 00:33:54 -04001848 rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1849 if (rs.rs_tstamp > tsf_lower &&
1850 unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1851 rxs->mactime -= 0x100000000ULL;
1852
1853 if (rs.rs_tstamp < tsf_lower &&
1854 unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1855 rxs->mactime += 0x100000000ULL;
1856
Zefir Kurtisi83c76572011-11-16 11:09:44 +01001857 retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
1858 rxs, &decrypt_error);
1859 if (retval)
1860 goto requeue_drop_frag;
1861
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001862 /* Ensure we always have an skb to requeue once we are done
1863 * processing the current buffer's skb */
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001864 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001865
1866 /* If there is no memory we ignore the current RX'd frame,
1867 * tell hardware it can give us a new frame using the old
Sujithb77f4832008-12-07 21:44:03 +05301868 * skb and put it at the tail of the sc->rx.rxbuf list for
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001869 * processing. */
1870 if (!requeue_skb)
Felix Fietkau0d955212011-01-26 18:23:27 +01001871 goto requeue_drop_frag;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001872
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301873 /* Unmap the frame */
Gabor Juhos7da3c552009-01-14 20:17:03 +01001874 dma_unmap_single(sc->dev, bf->bf_buf_addr,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001875 common->rx_bufsize,
Felix Fietkaub5c804752010-04-15 17:38:48 -04001876 dma_type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001877
Felix Fietkaub5c804752010-04-15 17:38:48 -04001878 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1879 if (ah->caps.rx_status_len)
1880 skb_pull(skb, ah->caps.rx_status_len);
Sujithbe0418a2008-11-18 09:05:55 +05301881
Felix Fietkau0d955212011-01-26 18:23:27 +01001882 if (!rs.rs_more)
1883 ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1884 rxs, decrypt_error);
Sujithbe0418a2008-11-18 09:05:55 +05301885
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001886 /* We will now give hardware our shiny new allocated skb */
1887 bf->bf_mpdu = requeue_skb;
Gabor Juhos7da3c552009-01-14 20:17:03 +01001888 bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001889 common->rx_bufsize,
Felix Fietkaub5c804752010-04-15 17:38:48 -04001890 dma_type);
Gabor Juhos7da3c552009-01-14 20:17:03 +01001891 if (unlikely(dma_mapping_error(sc->dev,
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001892 bf->bf_buf_addr))) {
1893 dev_kfree_skb_any(requeue_skb);
1894 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -07001895 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -08001896 ath_err(common, "dma_mapping_error() on RX\n");
Felix Fietkau7545daf2011-01-24 19:23:16 +01001897 ieee80211_rx(hw, skb);
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001898 break;
1899 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001900
Felix Fietkau0d955212011-01-26 18:23:27 +01001901 if (rs.rs_more) {
1902 /*
1903 * rs_more indicates chained descriptors which can be
1904 * used to link buffers together for a sort of
1905 * scatter-gather operation.
1906 */
1907 if (sc->rx.frag) {
1908 /* too many fragments - cannot handle frame */
1909 dev_kfree_skb_any(sc->rx.frag);
1910 dev_kfree_skb_any(skb);
1911 skb = NULL;
1912 }
1913 sc->rx.frag = skb;
1914 goto requeue;
1915 }
1916
1917 if (sc->rx.frag) {
1918 int space = skb->len - skb_tailroom(hdr_skb);
1919
1920 sc->rx.frag = NULL;
1921
1922 if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
1923 dev_kfree_skb(skb);
1924 goto requeue_drop_frag;
1925 }
1926
1927 skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
1928 skb->len);
1929 dev_kfree_skb_any(skb);
1930 skb = hdr_skb;
1931 }
1932
Mohammed Shafi Shajakhaneb840a82011-11-29 20:30:35 +05301933
1934 if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) {
1935
1936 /*
1937 * change the default rx antenna if rx diversity
1938 * chooses the other antenna 3 times in a row.
1939 */
1940 if (sc->rx.defant != rs.rs_antenna) {
1941 if (++sc->rx.rxotherant >= 3)
1942 ath_setdefantenna(sc, rs.rs_antenna);
1943 } else {
1944 sc->rx.rxotherant = 0;
1945 }
1946
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001947 }
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301948
Felix Fietkau66760ea2011-07-13 23:35:05 +08001949 if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
1950 skb_trim(skb, skb->len - 8);
1951
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001952 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Mohammed Shafi Shajakhanaaef24b2010-12-07 20:40:58 +05301953
1954 if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
Rajkumar Manoharanf73c6042011-09-26 22:16:56 +05301955 PS_WAIT_FOR_CAB |
1956 PS_WAIT_FOR_PSPOLL_DATA)) ||
1957 ath9k_check_auto_sleep(sc))
1958 ath_rx_ps(sc, skb, rs.is_mybeacon);
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001959 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Jouni Malinencc659652009-05-14 21:28:48 +03001960
Felix Fietkau43c35282011-09-03 01:40:27 +02001961 if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx == 3)
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001962 ath_ant_comb_scan(sc, &rs);
1963
Felix Fietkau7545daf2011-01-24 19:23:16 +01001964 ieee80211_rx(hw, skb);
Jouni Malinencc659652009-05-14 21:28:48 +03001965
Felix Fietkau0d955212011-01-26 18:23:27 +01001966requeue_drop_frag:
1967 if (sc->rx.frag) {
1968 dev_kfree_skb_any(sc->rx.frag);
1969 sc->rx.frag = NULL;
1970 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001971requeue:
Felix Fietkaub5c804752010-04-15 17:38:48 -04001972 if (edma) {
1973 list_add_tail(&bf->list, &sc->rx.rxbuf);
1974 ath_rx_edma_buf_link(sc, qtype);
1975 } else {
1976 list_move_tail(&bf->list, &sc->rx.rxbuf);
1977 ath_rx_buf_link(sc, bf);
Felix Fietkau34832882011-09-14 21:23:03 +02001978 if (!flush)
1979 ath9k_hw_rxena(ah);
Felix Fietkaub5c804752010-04-15 17:38:48 -04001980 }
Sujithbe0418a2008-11-18 09:05:55 +05301981 } while (1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001982
Sujithb77f4832008-12-07 21:44:03 +05301983 spin_unlock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001984
Rajkumar Manoharan29ab0b32011-08-13 10:28:10 +05301985 if (!(ah->imask & ATH9K_INT_RXEOL)) {
1986 ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
Felix Fietkau72d874c2011-10-08 20:06:19 +02001987 ath9k_hw_set_interrupts(ah);
Rajkumar Manoharan29ab0b32011-08-13 10:28:10 +05301988 }
1989
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001990 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001991}