blob: cd2722529c146d40e877a2d73b34fc942f28e130 [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 Perches226afe62010-12-02 19:12:37 -0800175 ath_dbg(common, ATH_DBG_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 Perches226afe62010-12-02 19:12:37 -0800340 ath_dbg(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
341 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#undef RX_FILTER_PRESERVE
479}
480
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700481int ath_startrecv(struct ath_softc *sc)
482{
Sujithcbe61d82009-02-09 13:27:12 +0530483 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700484 struct ath_buf *bf, *tbf;
485
Felix Fietkaub5c804752010-04-15 17:38:48 -0400486 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
487 ath_edma_start_recv(sc);
488 return 0;
489 }
490
Sujithb77f4832008-12-07 21:44:03 +0530491 spin_lock_bh(&sc->rx.rxbuflock);
492 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700493 goto start_recv;
494
Sujithb77f4832008-12-07 21:44:03 +0530495 sc->rx.rxlink = NULL;
496 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700497 ath_rx_buf_link(sc, bf);
498 }
499
500 /* We could have deleted elements so the list may be empty now */
Sujithb77f4832008-12-07 21:44:03 +0530501 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700502 goto start_recv;
503
Sujithb77f4832008-12-07 21:44:03 +0530504 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700505 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
Sujithbe0418a2008-11-18 09:05:55 +0530506 ath9k_hw_rxena(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700507
508start_recv:
Sujithbe0418a2008-11-18 09:05:55 +0530509 ath_opmode_init(sc);
Luis R. Rodriguez48a6a462010-09-16 15:12:28 -0400510 ath9k_hw_startpcureceive(ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
Sujithbe0418a2008-11-18 09:05:55 +0530511
Luis R. Rodriguez7583c5502010-10-20 16:07:04 -0700512 spin_unlock_bh(&sc->rx.rxbuflock);
513
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700514 return 0;
515}
516
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700517bool ath_stoprecv(struct ath_softc *sc)
518{
Sujithcbe61d82009-02-09 13:27:12 +0530519 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau5882da022011-04-08 20:13:18 +0200520 bool stopped, reset = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700521
Luis R. Rodriguez1e450282010-10-20 16:07:03 -0700522 spin_lock_bh(&sc->rx.rxbuflock);
Felix Fietkaud47844a2010-11-20 03:08:47 +0100523 ath9k_hw_abortpcurecv(ah);
Sujithbe0418a2008-11-18 09:05:55 +0530524 ath9k_hw_setrxfilter(ah, 0);
Felix Fietkau5882da022011-04-08 20:13:18 +0200525 stopped = ath9k_hw_stopdmarecv(ah, &reset);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400526
527 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
528 ath_edma_stop_recv(sc);
529 else
530 sc->rx.rxlink = NULL;
Luis R. Rodriguez1e450282010-10-20 16:07:03 -0700531 spin_unlock_bh(&sc->rx.rxbuflock);
Sujithbe0418a2008-11-18 09:05:55 +0530532
Rajkumar Manoharand5847472010-12-20 14:39:51 +0530533 if (!(ah->ah_flags & AH_UNPLUGGED) &&
534 unlikely(!stopped)) {
Ben Greeard7fd1b502010-12-06 13:13:07 -0800535 ath_err(ath9k_hw_common(sc->sc_ah),
536 "Could not stop RX, we could be "
537 "confusing the DMA engine when we start RX up\n");
538 ATH_DBG_WARN_ON_ONCE(!stopped);
539 }
Felix Fietkau2232d312011-04-15 00:41:43 +0200540 return stopped && !reset;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700541}
542
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700543void ath_flushrecv(struct ath_softc *sc)
544{
Sujith98deeea2008-08-11 14:05:46 +0530545 sc->sc_flags |= SC_OP_RXFLUSH;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400546 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
547 ath_rx_tasklet(sc, 1, true);
548 ath_rx_tasklet(sc, 1, false);
Sujith98deeea2008-08-11 14:05:46 +0530549 sc->sc_flags &= ~SC_OP_RXFLUSH;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700550}
551
Jouni Malinencc659652009-05-14 21:28:48 +0300552static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
553{
554 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
555 struct ieee80211_mgmt *mgmt;
556 u8 *pos, *end, id, elen;
557 struct ieee80211_tim_ie *tim;
558
559 mgmt = (struct ieee80211_mgmt *)skb->data;
560 pos = mgmt->u.beacon.variable;
561 end = skb->data + skb->len;
562
563 while (pos + 2 < end) {
564 id = *pos++;
565 elen = *pos++;
566 if (pos + elen > end)
567 break;
568
569 if (id == WLAN_EID_TIM) {
570 if (elen < sizeof(*tim))
571 break;
572 tim = (struct ieee80211_tim_ie *) pos;
573 if (tim->dtim_count != 0)
574 break;
575 return tim->bitmap_ctrl & 0x01;
576 }
577
578 pos += elen;
579 }
580
581 return false;
582}
583
Jouni Malinencc659652009-05-14 21:28:48 +0300584static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
585{
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700586 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300587
588 if (skb->len < 24 + 8 + 2 + 2)
589 return;
590
Sujith1b04b932010-01-08 10:36:05 +0530591 sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
Gabor Juhos293dc5d2009-06-19 12:17:48 +0200592
Sujith1b04b932010-01-08 10:36:05 +0530593 if (sc->ps_flags & PS_BEACON_SYNC) {
594 sc->ps_flags &= ~PS_BEACON_SYNC;
Joe Perches226afe62010-12-02 19:12:37 -0800595 ath_dbg(common, ATH_DBG_PS,
596 "Reconfigure Beacon timers based on timestamp from the AP\n");
Rajkumar Manoharan99e4d432011-04-04 22:56:19 +0530597 ath_set_beacon(sc);
Jouni Malinenccdfeab2009-05-20 21:59:08 +0300598 }
599
Jouni Malinencc659652009-05-14 21:28:48 +0300600 if (ath_beacon_dtim_pending_cab(skb)) {
601 /*
602 * Remain awake waiting for buffered broadcast/multicast
Gabor Juhos58f5fff2009-06-17 20:53:20 +0200603 * frames. If the last broadcast/multicast frame is not
604 * received properly, the next beacon frame will work as
605 * a backup trigger for returning into NETWORK SLEEP state,
606 * so we are waiting for it as well.
Jouni Malinencc659652009-05-14 21:28:48 +0300607 */
Joe Perches226afe62010-12-02 19:12:37 -0800608 ath_dbg(common, ATH_DBG_PS,
609 "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
Sujith1b04b932010-01-08 10:36:05 +0530610 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
Jouni Malinencc659652009-05-14 21:28:48 +0300611 return;
612 }
613
Sujith1b04b932010-01-08 10:36:05 +0530614 if (sc->ps_flags & PS_WAIT_FOR_CAB) {
Jouni Malinencc659652009-05-14 21:28:48 +0300615 /*
616 * This can happen if a broadcast frame is dropped or the AP
617 * fails to send a frame indicating that all CAB frames have
618 * been delivered.
619 */
Sujith1b04b932010-01-08 10:36:05 +0530620 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
Joe Perches226afe62010-12-02 19:12:37 -0800621 ath_dbg(common, ATH_DBG_PS,
622 "PS wait for CAB frames timed out\n");
Jouni Malinencc659652009-05-14 21:28:48 +0300623 }
Jouni Malinencc659652009-05-14 21:28:48 +0300624}
625
Rajkumar Manoharanf73c6042011-09-26 22:16:56 +0530626static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb, bool mybeacon)
Jouni Malinencc659652009-05-14 21:28:48 +0300627{
628 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700629 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300630
631 hdr = (struct ieee80211_hdr *)skb->data;
632
633 /* Process Beacon and CAB receive in PS state */
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -0700634 if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
Rajkumar Manoharanf73c6042011-09-26 22:16:56 +0530635 && mybeacon)
Jouni Malinencc659652009-05-14 21:28:48 +0300636 ath_rx_ps_beacon(sc, skb);
Sujith1b04b932010-01-08 10:36:05 +0530637 else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
Jouni Malinencc659652009-05-14 21:28:48 +0300638 (ieee80211_is_data(hdr->frame_control) ||
639 ieee80211_is_action(hdr->frame_control)) &&
640 is_multicast_ether_addr(hdr->addr1) &&
641 !ieee80211_has_moredata(hdr->frame_control)) {
Jouni Malinencc659652009-05-14 21:28:48 +0300642 /*
643 * No more broadcast/multicast frames to be received at this
644 * point.
645 */
Senthil Balasubramanian3fac6df2010-09-16 15:12:35 -0400646 sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
Joe Perches226afe62010-12-02 19:12:37 -0800647 ath_dbg(common, ATH_DBG_PS,
648 "All PS CAB frames received, back to sleep\n");
Sujith1b04b932010-01-08 10:36:05 +0530649 } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300650 !is_multicast_ether_addr(hdr->addr1) &&
651 !ieee80211_has_morefrags(hdr->frame_control)) {
Sujith1b04b932010-01-08 10:36:05 +0530652 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
Joe Perches226afe62010-12-02 19:12:37 -0800653 ath_dbg(common, ATH_DBG_PS,
654 "Going back to sleep after having received PS-Poll data (0x%lx)\n",
Sujith1b04b932010-01-08 10:36:05 +0530655 sc->ps_flags & (PS_WAIT_FOR_BEACON |
656 PS_WAIT_FOR_CAB |
657 PS_WAIT_FOR_PSPOLL_DATA |
658 PS_WAIT_FOR_TX_ACK));
Jouni Malinencc659652009-05-14 21:28:48 +0300659 }
660}
661
Felix Fietkaub5c804752010-04-15 17:38:48 -0400662static bool ath_edma_get_buffers(struct ath_softc *sc,
663 enum ath9k_rx_qtype qtype)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700664{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400665 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
666 struct ath_hw *ah = sc->sc_ah;
667 struct ath_common *common = ath9k_hw_common(ah);
668 struct sk_buff *skb;
Sujithbe0418a2008-11-18 09:05:55 +0530669 struct ath_buf *bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400670 int ret;
671
672 skb = skb_peek(&rx_edma->rx_fifo);
673 if (!skb)
674 return false;
675
676 bf = SKB_CB_ATHBUF(skb);
677 BUG_ON(!bf);
678
Ming Leice9426d2010-05-15 18:25:40 +0800679 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400680 common->rx_bufsize, DMA_FROM_DEVICE);
681
682 ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data);
Ming Leice9426d2010-05-15 18:25:40 +0800683 if (ret == -EINPROGRESS) {
684 /*let device gain the buffer again*/
685 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
686 common->rx_bufsize, DMA_FROM_DEVICE);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400687 return false;
Ming Leice9426d2010-05-15 18:25:40 +0800688 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400689
690 __skb_unlink(skb, &rx_edma->rx_fifo);
691 if (ret == -EINVAL) {
692 /* corrupt descriptor, skip this one and the following one */
693 list_add_tail(&bf->list, &sc->rx.rxbuf);
694 ath_rx_edma_buf_link(sc, qtype);
695 skb = skb_peek(&rx_edma->rx_fifo);
696 if (!skb)
697 return true;
698
699 bf = SKB_CB_ATHBUF(skb);
700 BUG_ON(!bf);
701
702 __skb_unlink(skb, &rx_edma->rx_fifo);
703 list_add_tail(&bf->list, &sc->rx.rxbuf);
704 ath_rx_edma_buf_link(sc, qtype);
Vasanthakumar Thiagarajan083e3e82010-05-10 19:41:34 -0700705 return true;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400706 }
707 skb_queue_tail(&rx_edma->rx_buffers, skb);
708
709 return true;
710}
711
712static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
713 struct ath_rx_status *rs,
714 enum ath9k_rx_qtype qtype)
715{
716 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
717 struct sk_buff *skb;
718 struct ath_buf *bf;
719
720 while (ath_edma_get_buffers(sc, qtype));
721 skb = __skb_dequeue(&rx_edma->rx_buffers);
722 if (!skb)
723 return NULL;
724
725 bf = SKB_CB_ATHBUF(skb);
726 ath9k_hw_process_rxdesc_edma(sc->sc_ah, rs, skb->data);
727 return bf;
728}
729
730static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
731 struct ath_rx_status *rs)
732{
733 struct ath_hw *ah = sc->sc_ah;
734 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700735 struct ath_desc *ds;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400736 struct ath_buf *bf;
737 int ret;
738
739 if (list_empty(&sc->rx.rxbuf)) {
740 sc->rx.rxlink = NULL;
741 return NULL;
742 }
743
744 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
745 ds = bf->bf_desc;
746
747 /*
748 * Must provide the virtual address of the current
749 * descriptor, the physical address, and the virtual
750 * address of the next descriptor in the h/w chain.
751 * This allows the HAL to look ahead to see if the
752 * hardware is done with a descriptor by checking the
753 * done bit in the following descriptor and the address
754 * of the current descriptor the DMA engine is working
755 * on. All this is necessary because of our use of
756 * a self-linked list to avoid rx overruns.
757 */
Rajkumar Manoharan3de21112011-08-13 10:28:11 +0530758 ret = ath9k_hw_rxprocdesc(ah, ds, rs);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400759 if (ret == -EINPROGRESS) {
760 struct ath_rx_status trs;
761 struct ath_buf *tbf;
762 struct ath_desc *tds;
763
764 memset(&trs, 0, sizeof(trs));
765 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
766 sc->rx.rxlink = NULL;
767 return NULL;
768 }
769
770 tbf = list_entry(bf->list.next, struct ath_buf, list);
771
772 /*
773 * On some hardware the descriptor status words could
774 * get corrupted, including the done bit. Because of
775 * this, check if the next descriptor's done bit is
776 * set or not.
777 *
778 * If the next descriptor's done bit is set, the current
779 * descriptor has been corrupted. Force s/w to discard
780 * this descriptor and continue...
781 */
782
783 tds = tbf->bf_desc;
Rajkumar Manoharan3de21112011-08-13 10:28:11 +0530784 ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400785 if (ret == -EINPROGRESS)
786 return NULL;
787 }
788
789 if (!bf->bf_mpdu)
790 return bf;
791
792 /*
793 * Synchronize the DMA transfer with CPU before
794 * 1. accessing the frame
795 * 2. requeueing the same buffer to h/w
796 */
Ming Leice9426d2010-05-15 18:25:40 +0800797 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400798 common->rx_bufsize,
799 DMA_FROM_DEVICE);
800
801 return bf;
802}
803
Sujithd4357002010-05-20 15:34:38 +0530804/* Assumes you've already done the endian to CPU conversion */
805static bool ath9k_rx_accept(struct ath_common *common,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700806 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530807 struct ieee80211_rx_status *rxs,
808 struct ath_rx_status *rx_stats,
809 bool *decrypt_error)
810{
Felix Fietkau66760ea2011-07-13 23:35:05 +0800811 bool is_mc, is_valid_tkip, strip_mic, mic_error;
Sujithd4357002010-05-20 15:34:38 +0530812 struct ath_hw *ah = common->ah;
Sujithd4357002010-05-20 15:34:38 +0530813 __le16 fc;
Vasanthakumar Thiagarajanb7b1b512010-05-20 14:34:48 -0700814 u8 rx_status_len = ah->caps.rx_status_len;
Sujithd4357002010-05-20 15:34:38 +0530815
Sujithd4357002010-05-20 15:34:38 +0530816 fc = hdr->frame_control;
817
Felix Fietkau66760ea2011-07-13 23:35:05 +0800818 is_mc = !!is_multicast_ether_addr(hdr->addr1);
819 is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
820 test_bit(rx_stats->rs_keyix, common->tkip_keymap);
Bill Jordan152e5852011-08-19 11:10:22 -0400821 strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
822 !(rx_stats->rs_status &
Felix Fietkau846d9362011-10-08 22:02:58 +0200823 (ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
824 ATH9K_RXERR_KEYMISS));
Felix Fietkau66760ea2011-07-13 23:35:05 +0800825
Sujithd4357002010-05-20 15:34:38 +0530826 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
Felix Fietkau66760ea2011-07-13 23:35:05 +0800840 mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
841 !ieee80211_has_morefrags(fc) &&
842 !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
843 (rx_stats->rs_status & ATH9K_RXERR_MIC);
844
Sujithd4357002010-05-20 15:34:38 +0530845 /*
846 * The rx_stats->rs_status will not be set until the end of the
847 * chained descriptors so it can be ignored if rs_more is set. The
848 * rs_more will be false at the last element of the chained
849 * descriptors.
850 */
851 if (rx_stats->rs_status != 0) {
Felix Fietkau846d9362011-10-08 22:02:58 +0200852 u8 status_mask;
853
Felix Fietkau66760ea2011-07-13 23:35:05 +0800854 if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
Sujithd4357002010-05-20 15:34:38 +0530855 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800856 mic_error = false;
857 }
Sujithd4357002010-05-20 15:34:38 +0530858 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
859 return false;
860
Felix Fietkau846d9362011-10-08 22:02:58 +0200861 if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
862 (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
Sujithd4357002010-05-20 15:34:38 +0530863 *decrypt_error = true;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800864 mic_error = false;
Sujithd4357002010-05-20 15:34:38 +0530865 }
Felix Fietkau66760ea2011-07-13 23:35:05 +0800866
Sujithd4357002010-05-20 15:34:38 +0530867 /*
868 * Reject error frames with the exception of
869 * decryption and MIC failures. For monitor mode,
870 * we also ignore the CRC error.
871 */
Felix Fietkau846d9362011-10-08 22:02:58 +0200872 status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
873 ATH9K_RXERR_KEYMISS;
874
875 if (ah->is_monitoring)
876 status_mask |= ATH9K_RXERR_CRC;
877
878 if (rx_stats->rs_status & ~status_mask)
879 return false;
Sujithd4357002010-05-20 15:34:38 +0530880 }
Felix Fietkau66760ea2011-07-13 23:35:05 +0800881
882 /*
883 * For unicast frames the MIC error bit can have false positives,
884 * so all MIC error reports need to be validated in software.
885 * False negatives are not common, so skip software verification
886 * if the hardware considers the MIC valid.
887 */
888 if (strip_mic)
889 rxs->flag |= RX_FLAG_MMIC_STRIPPED;
890 else if (is_mc && mic_error)
891 rxs->flag |= RX_FLAG_MMIC_ERROR;
892
Sujithd4357002010-05-20 15:34:38 +0530893 return true;
894}
895
896static int ath9k_process_rate(struct ath_common *common,
897 struct ieee80211_hw *hw,
898 struct ath_rx_status *rx_stats,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700899 struct ieee80211_rx_status *rxs)
Sujithd4357002010-05-20 15:34:38 +0530900{
901 struct ieee80211_supported_band *sband;
902 enum ieee80211_band band;
903 unsigned int i = 0;
904
905 band = hw->conf.channel->band;
906 sband = hw->wiphy->bands[band];
907
908 if (rx_stats->rs_rate & 0x80) {
909 /* HT rate */
910 rxs->flag |= RX_FLAG_HT;
911 if (rx_stats->rs_flags & ATH9K_RX_2040)
912 rxs->flag |= RX_FLAG_40MHZ;
913 if (rx_stats->rs_flags & ATH9K_RX_GI)
914 rxs->flag |= RX_FLAG_SHORT_GI;
915 rxs->rate_idx = rx_stats->rs_rate & 0x7f;
916 return 0;
917 }
918
919 for (i = 0; i < sband->n_bitrates; i++) {
920 if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
921 rxs->rate_idx = i;
922 return 0;
923 }
924 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
925 rxs->flag |= RX_FLAG_SHORTPRE;
926 rxs->rate_idx = i;
927 return 0;
928 }
929 }
930
931 /*
932 * No valid hardware bitrate found -- we should not get here
933 * because hardware has already validated this frame as OK.
934 */
Mohammed Shafi Shajakhan9976f622011-08-26 11:10:01 +0530935 ath_dbg(common, ATH_DBG_ANY,
Joe Perches226afe62010-12-02 19:12:37 -0800936 "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
937 rx_stats->rs_rate);
Sujithd4357002010-05-20 15:34:38 +0530938
939 return -EINVAL;
940}
941
942static void ath9k_process_rssi(struct ath_common *common,
943 struct ieee80211_hw *hw,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700944 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530945 struct ath_rx_status *rx_stats)
946{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100947 struct ath_softc *sc = hw->priv;
Sujithd4357002010-05-20 15:34:38 +0530948 struct ath_hw *ah = common->ah;
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200949 int last_rssi;
Sujithd4357002010-05-20 15:34:38 +0530950
Rajkumar Manoharancf3af742011-08-27 16:17:47 +0530951 if (!rx_stats->is_mybeacon ||
952 ((ah->opmode != NL80211_IFTYPE_STATION) &&
953 (ah->opmode != NL80211_IFTYPE_ADHOC)))
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200954 return;
955
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200956 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
Felix Fietkau9ac586152011-01-24 19:23:18 +0100957 ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
Ben Greear686b9cb2010-09-23 09:44:36 -0700958
Felix Fietkau9ac586152011-01-24 19:23:18 +0100959 last_rssi = sc->last_rssi;
Sujithd4357002010-05-20 15:34:38 +0530960 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
961 rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
962 ATH_RSSI_EP_MULTIPLIER);
963 if (rx_stats->rs_rssi < 0)
964 rx_stats->rs_rssi = 0;
965
966 /* Update Beacon RSSI, this is used by ANI. */
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200967 ah->stats.avgbrssi = rx_stats->rs_rssi;
Sujithd4357002010-05-20 15:34:38 +0530968}
969
970/*
971 * For Decrypt or Demic errors, we only mark packet status here and always push
972 * up the frame up to let mac80211 handle the actual error case, be it no
973 * decryption key or real decryption error. This let us keep statistics there.
974 */
975static int ath9k_rx_skb_preprocess(struct ath_common *common,
976 struct ieee80211_hw *hw,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700977 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530978 struct ath_rx_status *rx_stats,
979 struct ieee80211_rx_status *rx_status,
980 bool *decrypt_error)
981{
Felix Fietkauf749b942011-07-28 14:08:57 +0200982 struct ath_hw *ah = common->ah;
983
Sujithd4357002010-05-20 15:34:38 +0530984 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
985
986 /*
987 * everything but the rate is checked here, the rate check is done
988 * separately to avoid doing two lookups for a rate for each frame.
989 */
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700990 if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
Sujithd4357002010-05-20 15:34:38 +0530991 return -EINVAL;
992
Felix Fietkau0d955212011-01-26 18:23:27 +0100993 /* Only use status info from the last fragment */
994 if (rx_stats->rs_more)
995 return 0;
996
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700997 ath9k_process_rssi(common, hw, hdr, rx_stats);
Sujithd4357002010-05-20 15:34:38 +0530998
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700999 if (ath9k_process_rate(common, hw, rx_stats, rx_status))
Sujithd4357002010-05-20 15:34:38 +05301000 return -EINVAL;
1001
Sujithd4357002010-05-20 15:34:38 +05301002 rx_status->band = hw->conf.channel->band;
1003 rx_status->freq = hw->conf.channel->center_freq;
Felix Fietkauf749b942011-07-28 14:08:57 +02001004 rx_status->signal = ah->noise + rx_stats->rs_rssi;
Sujithd4357002010-05-20 15:34:38 +05301005 rx_status->antenna = rx_stats->rs_antenna;
Johannes Berg6ebacbb2011-02-23 15:06:08 +01001006 rx_status->flag |= RX_FLAG_MACTIME_MPDU;
Sujithd4357002010-05-20 15:34:38 +05301007
1008 return 0;
1009}
1010
1011static void ath9k_rx_skb_postprocess(struct ath_common *common,
1012 struct sk_buff *skb,
1013 struct ath_rx_status *rx_stats,
1014 struct ieee80211_rx_status *rxs,
1015 bool decrypt_error)
1016{
1017 struct ath_hw *ah = common->ah;
1018 struct ieee80211_hdr *hdr;
1019 int hdrlen, padpos, padsize;
1020 u8 keyix;
1021 __le16 fc;
1022
1023 /* see if any padding is done by the hw and remove it */
1024 hdr = (struct ieee80211_hdr *) skb->data;
1025 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1026 fc = hdr->frame_control;
1027 padpos = ath9k_cmn_padpos(hdr->frame_control);
1028
1029 /* The MAC header is padded to have 32-bit boundary if the
1030 * packet payload is non-zero. The general calculation for
1031 * padsize would take into account odd header lengths:
1032 * padsize = (4 - padpos % 4) % 4; However, since only
1033 * even-length headers are used, padding can only be 0 or 2
1034 * bytes and we can optimize this a bit. In addition, we must
1035 * not try to remove padding from short control frames that do
1036 * not have payload. */
1037 padsize = padpos & 3;
1038 if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1039 memmove(skb->data + padsize, skb->data, padpos);
1040 skb_pull(skb, padsize);
1041 }
1042
1043 keyix = rx_stats->rs_keyix;
1044
1045 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1046 ieee80211_has_protected(fc)) {
1047 rxs->flag |= RX_FLAG_DECRYPTED;
1048 } else if (ieee80211_has_protected(fc)
1049 && !decrypt_error && skb->len >= hdrlen + 4) {
1050 keyix = skb->data[hdrlen + 3] >> 6;
1051
1052 if (test_bit(keyix, common->keymap))
1053 rxs->flag |= RX_FLAG_DECRYPTED;
1054 }
1055 if (ah->sw_mgmt_crypto &&
1056 (rxs->flag & RX_FLAG_DECRYPTED) &&
1057 ieee80211_is_mgmt(fc))
1058 /* Use software decrypt for management frames. */
1059 rxs->flag &= ~RX_FLAG_DECRYPTED;
1060}
Felix Fietkaub5c804752010-04-15 17:38:48 -04001061
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001062static void ath_lnaconf_alt_good_scan(struct ath_ant_comb *antcomb,
1063 struct ath_hw_antcomb_conf ant_conf,
1064 int main_rssi_avg)
1065{
1066 antcomb->quick_scan_cnt = 0;
1067
1068 if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
1069 antcomb->rssi_lna2 = main_rssi_avg;
1070 else if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA1)
1071 antcomb->rssi_lna1 = main_rssi_avg;
1072
1073 switch ((ant_conf.main_lna_conf << 4) | ant_conf.alt_lna_conf) {
Gabor Juhos223c5a82011-06-21 11:23:45 +02001074 case 0x10: /* LNA2 A-B */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001075 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1076 antcomb->first_quick_scan_conf =
1077 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1078 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1079 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001080 case 0x20: /* LNA1 A-B */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001081 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1082 antcomb->first_quick_scan_conf =
1083 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1084 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1085 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001086 case 0x21: /* LNA1 LNA2 */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001087 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA2;
1088 antcomb->first_quick_scan_conf =
1089 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1090 antcomb->second_quick_scan_conf =
1091 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1092 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001093 case 0x12: /* LNA2 LNA1 */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001094 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1;
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 0x13: /* LNA2 A+B */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001101 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1102 antcomb->first_quick_scan_conf =
1103 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1104 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1105 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001106 case 0x23: /* LNA1 A+B */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001107 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1108 antcomb->first_quick_scan_conf =
1109 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1110 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1111 break;
1112 default:
1113 break;
1114 }
1115}
1116
1117static void ath_select_ant_div_from_quick_scan(struct ath_ant_comb *antcomb,
1118 struct ath_hw_antcomb_conf *div_ant_conf,
1119 int main_rssi_avg, int alt_rssi_avg,
1120 int alt_ratio)
1121{
1122 /* alt_good */
1123 switch (antcomb->quick_scan_cnt) {
1124 case 0:
1125 /* set alt to main, and alt to first conf */
1126 div_ant_conf->main_lna_conf = antcomb->main_conf;
1127 div_ant_conf->alt_lna_conf = antcomb->first_quick_scan_conf;
1128 break;
1129 case 1:
1130 /* set alt to main, and alt to first conf */
1131 div_ant_conf->main_lna_conf = antcomb->main_conf;
1132 div_ant_conf->alt_lna_conf = antcomb->second_quick_scan_conf;
1133 antcomb->rssi_first = main_rssi_avg;
1134 antcomb->rssi_second = alt_rssi_avg;
1135
1136 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1137 /* main is LNA1 */
1138 if (ath_is_alt_ant_ratio_better(alt_ratio,
1139 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1140 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1141 main_rssi_avg, alt_rssi_avg,
1142 antcomb->total_pkt_count))
1143 antcomb->first_ratio = true;
1144 else
1145 antcomb->first_ratio = false;
1146 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1147 if (ath_is_alt_ant_ratio_better(alt_ratio,
1148 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1149 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1150 main_rssi_avg, alt_rssi_avg,
1151 antcomb->total_pkt_count))
1152 antcomb->first_ratio = true;
1153 else
1154 antcomb->first_ratio = false;
1155 } else {
1156 if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1157 (alt_rssi_avg > main_rssi_avg +
1158 ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1159 (alt_rssi_avg > main_rssi_avg)) &&
1160 (antcomb->total_pkt_count > 50))
1161 antcomb->first_ratio = true;
1162 else
1163 antcomb->first_ratio = false;
1164 }
1165 break;
1166 case 2:
1167 antcomb->alt_good = false;
1168 antcomb->scan_not_start = false;
1169 antcomb->scan = false;
1170 antcomb->rssi_first = main_rssi_avg;
1171 antcomb->rssi_third = alt_rssi_avg;
1172
1173 if (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1)
1174 antcomb->rssi_lna1 = alt_rssi_avg;
1175 else if (antcomb->second_quick_scan_conf ==
1176 ATH_ANT_DIV_COMB_LNA2)
1177 antcomb->rssi_lna2 = alt_rssi_avg;
1178 else if (antcomb->second_quick_scan_conf ==
1179 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2) {
1180 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)
1181 antcomb->rssi_lna2 = main_rssi_avg;
1182 else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1)
1183 antcomb->rssi_lna1 = main_rssi_avg;
1184 }
1185
1186 if (antcomb->rssi_lna2 > antcomb->rssi_lna1 +
1187 ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)
1188 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1189 else
1190 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
1191
1192 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1193 if (ath_is_alt_ant_ratio_better(alt_ratio,
1194 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1195 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1196 main_rssi_avg, alt_rssi_avg,
1197 antcomb->total_pkt_count))
1198 antcomb->second_ratio = true;
1199 else
1200 antcomb->second_ratio = false;
1201 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1202 if (ath_is_alt_ant_ratio_better(alt_ratio,
1203 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1204 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1205 main_rssi_avg, alt_rssi_avg,
1206 antcomb->total_pkt_count))
1207 antcomb->second_ratio = true;
1208 else
1209 antcomb->second_ratio = false;
1210 } else {
1211 if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1212 (alt_rssi_avg > main_rssi_avg +
1213 ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1214 (alt_rssi_avg > main_rssi_avg)) &&
1215 (antcomb->total_pkt_count > 50))
1216 antcomb->second_ratio = true;
1217 else
1218 antcomb->second_ratio = false;
1219 }
1220
1221 /* set alt to the conf with maximun ratio */
1222 if (antcomb->first_ratio && antcomb->second_ratio) {
1223 if (antcomb->rssi_second > antcomb->rssi_third) {
1224 /* first alt*/
1225 if ((antcomb->first_quick_scan_conf ==
1226 ATH_ANT_DIV_COMB_LNA1) ||
1227 (antcomb->first_quick_scan_conf ==
1228 ATH_ANT_DIV_COMB_LNA2))
1229 /* Set alt LNA1 or LNA2*/
1230 if (div_ant_conf->main_lna_conf ==
1231 ATH_ANT_DIV_COMB_LNA2)
1232 div_ant_conf->alt_lna_conf =
1233 ATH_ANT_DIV_COMB_LNA1;
1234 else
1235 div_ant_conf->alt_lna_conf =
1236 ATH_ANT_DIV_COMB_LNA2;
1237 else
1238 /* Set alt to A+B or A-B */
1239 div_ant_conf->alt_lna_conf =
1240 antcomb->first_quick_scan_conf;
1241 } else if ((antcomb->second_quick_scan_conf ==
1242 ATH_ANT_DIV_COMB_LNA1) ||
1243 (antcomb->second_quick_scan_conf ==
1244 ATH_ANT_DIV_COMB_LNA2)) {
1245 /* Set alt LNA1 or LNA2 */
1246 if (div_ant_conf->main_lna_conf ==
1247 ATH_ANT_DIV_COMB_LNA2)
1248 div_ant_conf->alt_lna_conf =
1249 ATH_ANT_DIV_COMB_LNA1;
1250 else
1251 div_ant_conf->alt_lna_conf =
1252 ATH_ANT_DIV_COMB_LNA2;
1253 } else {
1254 /* Set alt to A+B or A-B */
1255 div_ant_conf->alt_lna_conf =
1256 antcomb->second_quick_scan_conf;
1257 }
1258 } else if (antcomb->first_ratio) {
1259 /* first alt */
1260 if ((antcomb->first_quick_scan_conf ==
1261 ATH_ANT_DIV_COMB_LNA1) ||
1262 (antcomb->first_quick_scan_conf ==
1263 ATH_ANT_DIV_COMB_LNA2))
1264 /* Set alt LNA1 or LNA2 */
1265 if (div_ant_conf->main_lna_conf ==
1266 ATH_ANT_DIV_COMB_LNA2)
1267 div_ant_conf->alt_lna_conf =
1268 ATH_ANT_DIV_COMB_LNA1;
1269 else
1270 div_ant_conf->alt_lna_conf =
1271 ATH_ANT_DIV_COMB_LNA2;
1272 else
1273 /* Set alt to A+B or A-B */
1274 div_ant_conf->alt_lna_conf =
1275 antcomb->first_quick_scan_conf;
1276 } else if (antcomb->second_ratio) {
1277 /* second alt */
1278 if ((antcomb->second_quick_scan_conf ==
1279 ATH_ANT_DIV_COMB_LNA1) ||
1280 (antcomb->second_quick_scan_conf ==
1281 ATH_ANT_DIV_COMB_LNA2))
1282 /* Set alt LNA1 or LNA2 */
1283 if (div_ant_conf->main_lna_conf ==
1284 ATH_ANT_DIV_COMB_LNA2)
1285 div_ant_conf->alt_lna_conf =
1286 ATH_ANT_DIV_COMB_LNA1;
1287 else
1288 div_ant_conf->alt_lna_conf =
1289 ATH_ANT_DIV_COMB_LNA2;
1290 else
1291 /* Set alt to A+B or A-B */
1292 div_ant_conf->alt_lna_conf =
1293 antcomb->second_quick_scan_conf;
1294 } else {
1295 /* main is largest */
1296 if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
1297 (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
1298 /* Set alt LNA1 or LNA2 */
1299 if (div_ant_conf->main_lna_conf ==
1300 ATH_ANT_DIV_COMB_LNA2)
1301 div_ant_conf->alt_lna_conf =
1302 ATH_ANT_DIV_COMB_LNA1;
1303 else
1304 div_ant_conf->alt_lna_conf =
1305 ATH_ANT_DIV_COMB_LNA2;
1306 else
1307 /* Set alt to A+B or A-B */
1308 div_ant_conf->alt_lna_conf = antcomb->main_conf;
1309 }
1310 break;
1311 default:
1312 break;
1313 }
1314}
1315
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301316static void ath_ant_div_conf_fast_divbias(struct ath_hw_antcomb_conf *ant_conf,
1317 struct ath_ant_comb *antcomb, int alt_ratio)
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001318{
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301319 if (ant_conf->div_group == 0) {
1320 /* Adjust the fast_div_bias based on main and alt lna conf */
1321 switch ((ant_conf->main_lna_conf << 4) |
1322 ant_conf->alt_lna_conf) {
Gabor Juhos223c5a82011-06-21 11:23:45 +02001323 case 0x01: /* A-B LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301324 ant_conf->fast_div_bias = 0x3b;
1325 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001326 case 0x02: /* A-B LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301327 ant_conf->fast_div_bias = 0x3d;
1328 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001329 case 0x03: /* A-B A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301330 ant_conf->fast_div_bias = 0x1;
1331 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001332 case 0x10: /* LNA2 A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301333 ant_conf->fast_div_bias = 0x7;
1334 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001335 case 0x12: /* LNA2 LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301336 ant_conf->fast_div_bias = 0x2;
1337 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001338 case 0x13: /* LNA2 A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301339 ant_conf->fast_div_bias = 0x7;
1340 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001341 case 0x20: /* LNA1 A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301342 ant_conf->fast_div_bias = 0x6;
1343 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001344 case 0x21: /* LNA1 LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301345 ant_conf->fast_div_bias = 0x0;
1346 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001347 case 0x23: /* LNA1 A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301348 ant_conf->fast_div_bias = 0x6;
1349 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001350 case 0x30: /* A+B A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301351 ant_conf->fast_div_bias = 0x1;
1352 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001353 case 0x31: /* A+B LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301354 ant_conf->fast_div_bias = 0x3b;
1355 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001356 case 0x32: /* A+B LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301357 ant_conf->fast_div_bias = 0x3d;
1358 break;
1359 default:
1360 break;
1361 }
Gabor Juhose7ef5bc2011-06-21 11:23:46 +02001362 } else if (ant_conf->div_group == 1) {
1363 /* Adjust the fast_div_bias based on main and alt_lna_conf */
1364 switch ((ant_conf->main_lna_conf << 4) |
1365 ant_conf->alt_lna_conf) {
1366 case 0x01: /* A-B LNA2 */
1367 ant_conf->fast_div_bias = 0x1;
1368 ant_conf->main_gaintb = 0;
1369 ant_conf->alt_gaintb = 0;
1370 break;
1371 case 0x02: /* A-B LNA1 */
1372 ant_conf->fast_div_bias = 0x1;
1373 ant_conf->main_gaintb = 0;
1374 ant_conf->alt_gaintb = 0;
1375 break;
1376 case 0x03: /* A-B A+B */
1377 ant_conf->fast_div_bias = 0x1;
1378 ant_conf->main_gaintb = 0;
1379 ant_conf->alt_gaintb = 0;
1380 break;
1381 case 0x10: /* LNA2 A-B */
1382 if (!(antcomb->scan) &&
1383 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1384 ant_conf->fast_div_bias = 0x3f;
1385 else
1386 ant_conf->fast_div_bias = 0x1;
1387 ant_conf->main_gaintb = 0;
1388 ant_conf->alt_gaintb = 0;
1389 break;
1390 case 0x12: /* LNA2 LNA1 */
1391 ant_conf->fast_div_bias = 0x1;
1392 ant_conf->main_gaintb = 0;
1393 ant_conf->alt_gaintb = 0;
1394 break;
1395 case 0x13: /* LNA2 A+B */
1396 if (!(antcomb->scan) &&
1397 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1398 ant_conf->fast_div_bias = 0x3f;
1399 else
1400 ant_conf->fast_div_bias = 0x1;
1401 ant_conf->main_gaintb = 0;
1402 ant_conf->alt_gaintb = 0;
1403 break;
1404 case 0x20: /* LNA1 A-B */
1405 if (!(antcomb->scan) &&
1406 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1407 ant_conf->fast_div_bias = 0x3f;
1408 else
1409 ant_conf->fast_div_bias = 0x1;
1410 ant_conf->main_gaintb = 0;
1411 ant_conf->alt_gaintb = 0;
1412 break;
1413 case 0x21: /* LNA1 LNA2 */
1414 ant_conf->fast_div_bias = 0x1;
1415 ant_conf->main_gaintb = 0;
1416 ant_conf->alt_gaintb = 0;
1417 break;
1418 case 0x23: /* LNA1 A+B */
1419 if (!(antcomb->scan) &&
1420 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1421 ant_conf->fast_div_bias = 0x3f;
1422 else
1423 ant_conf->fast_div_bias = 0x1;
1424 ant_conf->main_gaintb = 0;
1425 ant_conf->alt_gaintb = 0;
1426 break;
1427 case 0x30: /* A+B A-B */
1428 ant_conf->fast_div_bias = 0x1;
1429 ant_conf->main_gaintb = 0;
1430 ant_conf->alt_gaintb = 0;
1431 break;
1432 case 0x31: /* A+B LNA2 */
1433 ant_conf->fast_div_bias = 0x1;
1434 ant_conf->main_gaintb = 0;
1435 ant_conf->alt_gaintb = 0;
1436 break;
1437 case 0x32: /* A+B LNA1 */
1438 ant_conf->fast_div_bias = 0x1;
1439 ant_conf->main_gaintb = 0;
1440 ant_conf->alt_gaintb = 0;
1441 break;
1442 default:
1443 break;
1444 }
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301445 } else if (ant_conf->div_group == 2) {
1446 /* Adjust the fast_div_bias based on main and alt_lna_conf */
1447 switch ((ant_conf->main_lna_conf << 4) |
1448 ant_conf->alt_lna_conf) {
Gabor Juhos223c5a82011-06-21 11:23:45 +02001449 case 0x01: /* A-B LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301450 ant_conf->fast_div_bias = 0x1;
1451 ant_conf->main_gaintb = 0;
1452 ant_conf->alt_gaintb = 0;
1453 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001454 case 0x02: /* A-B LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301455 ant_conf->fast_div_bias = 0x1;
1456 ant_conf->main_gaintb = 0;
1457 ant_conf->alt_gaintb = 0;
1458 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001459 case 0x03: /* A-B A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301460 ant_conf->fast_div_bias = 0x1;
1461 ant_conf->main_gaintb = 0;
1462 ant_conf->alt_gaintb = 0;
1463 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001464 case 0x10: /* LNA2 A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301465 if (!(antcomb->scan) &&
1466 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1467 ant_conf->fast_div_bias = 0x1;
1468 else
1469 ant_conf->fast_div_bias = 0x2;
1470 ant_conf->main_gaintb = 0;
1471 ant_conf->alt_gaintb = 0;
1472 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001473 case 0x12: /* LNA2 LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301474 ant_conf->fast_div_bias = 0x1;
1475 ant_conf->main_gaintb = 0;
1476 ant_conf->alt_gaintb = 0;
1477 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001478 case 0x13: /* LNA2 A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301479 if (!(antcomb->scan) &&
1480 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1481 ant_conf->fast_div_bias = 0x1;
1482 else
1483 ant_conf->fast_div_bias = 0x2;
1484 ant_conf->main_gaintb = 0;
1485 ant_conf->alt_gaintb = 0;
1486 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001487 case 0x20: /* LNA1 A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301488 if (!(antcomb->scan) &&
1489 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1490 ant_conf->fast_div_bias = 0x1;
1491 else
1492 ant_conf->fast_div_bias = 0x2;
1493 ant_conf->main_gaintb = 0;
1494 ant_conf->alt_gaintb = 0;
1495 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001496 case 0x21: /* LNA1 LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301497 ant_conf->fast_div_bias = 0x1;
1498 ant_conf->main_gaintb = 0;
1499 ant_conf->alt_gaintb = 0;
1500 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001501 case 0x23: /* LNA1 A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301502 if (!(antcomb->scan) &&
1503 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1504 ant_conf->fast_div_bias = 0x1;
1505 else
1506 ant_conf->fast_div_bias = 0x2;
1507 ant_conf->main_gaintb = 0;
1508 ant_conf->alt_gaintb = 0;
1509 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001510 case 0x30: /* A+B A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301511 ant_conf->fast_div_bias = 0x1;
1512 ant_conf->main_gaintb = 0;
1513 ant_conf->alt_gaintb = 0;
1514 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001515 case 0x31: /* A+B LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301516 ant_conf->fast_div_bias = 0x1;
1517 ant_conf->main_gaintb = 0;
1518 ant_conf->alt_gaintb = 0;
1519 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001520 case 0x32: /* A+B LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301521 ant_conf->fast_div_bias = 0x1;
1522 ant_conf->main_gaintb = 0;
1523 ant_conf->alt_gaintb = 0;
1524 break;
1525 default:
1526 break;
1527 }
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001528 }
1529}
1530
1531/* Antenna diversity and combining */
1532static void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs)
1533{
1534 struct ath_hw_antcomb_conf div_ant_conf;
1535 struct ath_ant_comb *antcomb = &sc->ant_comb;
1536 int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set;
Sujith Manoharan0ff2b5c2011-04-20 11:00:34 +05301537 int curr_main_set;
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001538 int main_rssi = rs->rs_rssi_ctl0;
1539 int alt_rssi = rs->rs_rssi_ctl1;
1540 int rx_ant_conf, main_ant_conf;
1541 bool short_scan = false;
1542
1543 rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) &
1544 ATH_ANT_RX_MASK;
1545 main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) &
1546 ATH_ANT_RX_MASK;
1547
Mohammed Shafi Shajakhan21e8ee62011-05-13 20:31:40 +05301548 /* Record packet only when both main_rssi and alt_rssi is positive */
1549 if (main_rssi > 0 && alt_rssi > 0) {
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001550 antcomb->total_pkt_count++;
1551 antcomb->main_total_rssi += main_rssi;
1552 antcomb->alt_total_rssi += alt_rssi;
1553 if (main_ant_conf == rx_ant_conf)
1554 antcomb->main_recv_cnt++;
1555 else
1556 antcomb->alt_recv_cnt++;
1557 }
1558
1559 /* Short scan check */
1560 if (antcomb->scan && antcomb->alt_good) {
1561 if (time_after(jiffies, antcomb->scan_start_time +
1562 msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
1563 short_scan = true;
1564 else
1565 if (antcomb->total_pkt_count ==
1566 ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
1567 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1568 antcomb->total_pkt_count);
1569 if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
1570 short_scan = true;
1571 }
1572 }
1573
1574 if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
1575 rs->rs_moreaggr) && !short_scan)
1576 return;
1577
1578 if (antcomb->total_pkt_count) {
1579 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1580 antcomb->total_pkt_count);
1581 main_rssi_avg = (antcomb->main_total_rssi /
1582 antcomb->total_pkt_count);
1583 alt_rssi_avg = (antcomb->alt_total_rssi /
1584 antcomb->total_pkt_count);
1585 }
1586
1587
1588 ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf);
1589 curr_alt_set = div_ant_conf.alt_lna_conf;
1590 curr_main_set = div_ant_conf.main_lna_conf;
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001591
1592 antcomb->count++;
1593
1594 if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
1595 if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1596 ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
1597 main_rssi_avg);
1598 antcomb->alt_good = true;
1599 } else {
1600 antcomb->alt_good = false;
1601 }
1602
1603 antcomb->count = 0;
1604 antcomb->scan = true;
1605 antcomb->scan_not_start = true;
1606 }
1607
1608 if (!antcomb->scan) {
Mohammed Shafi Shajakhanb85c5732011-05-13 20:31:09 +05301609 if (ath_ant_div_comb_alt_check(div_ant_conf.div_group,
1610 alt_ratio, curr_main_set, curr_alt_set,
1611 alt_rssi_avg, main_rssi_avg)) {
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001612 if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
1613 /* Switch main and alt LNA */
1614 div_ant_conf.main_lna_conf =
1615 ATH_ANT_DIV_COMB_LNA2;
1616 div_ant_conf.alt_lna_conf =
1617 ATH_ANT_DIV_COMB_LNA1;
1618 } else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
1619 div_ant_conf.main_lna_conf =
1620 ATH_ANT_DIV_COMB_LNA1;
1621 div_ant_conf.alt_lna_conf =
1622 ATH_ANT_DIV_COMB_LNA2;
1623 }
1624
1625 goto div_comb_done;
1626 } else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
1627 (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
1628 /* Set alt to another LNA */
1629 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
1630 div_ant_conf.alt_lna_conf =
1631 ATH_ANT_DIV_COMB_LNA1;
1632 else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
1633 div_ant_conf.alt_lna_conf =
1634 ATH_ANT_DIV_COMB_LNA2;
1635
1636 goto div_comb_done;
1637 }
1638
1639 if ((alt_rssi_avg < (main_rssi_avg +
Mohammed Shafi Shajakhan8afbcc82011-05-13 20:30:56 +05301640 div_ant_conf.lna1_lna2_delta)))
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001641 goto div_comb_done;
1642 }
1643
1644 if (!antcomb->scan_not_start) {
1645 switch (curr_alt_set) {
1646 case ATH_ANT_DIV_COMB_LNA2:
1647 antcomb->rssi_lna2 = alt_rssi_avg;
1648 antcomb->rssi_lna1 = main_rssi_avg;
1649 antcomb->scan = true;
1650 /* set to A+B */
1651 div_ant_conf.main_lna_conf =
1652 ATH_ANT_DIV_COMB_LNA1;
1653 div_ant_conf.alt_lna_conf =
1654 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1655 break;
1656 case ATH_ANT_DIV_COMB_LNA1:
1657 antcomb->rssi_lna1 = alt_rssi_avg;
1658 antcomb->rssi_lna2 = main_rssi_avg;
1659 antcomb->scan = true;
1660 /* set to A+B */
1661 div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1662 div_ant_conf.alt_lna_conf =
1663 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1664 break;
1665 case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
1666 antcomb->rssi_add = alt_rssi_avg;
1667 antcomb->scan = true;
1668 /* set to A-B */
1669 div_ant_conf.alt_lna_conf =
1670 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1671 break;
1672 case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
1673 antcomb->rssi_sub = alt_rssi_avg;
1674 antcomb->scan = false;
1675 if (antcomb->rssi_lna2 >
1676 (antcomb->rssi_lna1 +
1677 ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
1678 /* use LNA2 as main LNA */
1679 if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
1680 (antcomb->rssi_add > antcomb->rssi_sub)) {
1681 /* set to A+B */
1682 div_ant_conf.main_lna_conf =
1683 ATH_ANT_DIV_COMB_LNA2;
1684 div_ant_conf.alt_lna_conf =
1685 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1686 } else if (antcomb->rssi_sub >
1687 antcomb->rssi_lna1) {
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_MINUS_LNA2;
1693 } else {
1694 /* set to LNA1 */
1695 div_ant_conf.main_lna_conf =
1696 ATH_ANT_DIV_COMB_LNA2;
1697 div_ant_conf.alt_lna_conf =
1698 ATH_ANT_DIV_COMB_LNA1;
1699 }
1700 } else {
1701 /* use LNA1 as main LNA */
1702 if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
1703 (antcomb->rssi_add > antcomb->rssi_sub)) {
1704 /* set to A+B */
1705 div_ant_conf.main_lna_conf =
1706 ATH_ANT_DIV_COMB_LNA1;
1707 div_ant_conf.alt_lna_conf =
1708 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1709 } else if (antcomb->rssi_sub >
1710 antcomb->rssi_lna1) {
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_MINUS_LNA2;
1716 } else {
1717 /* set to LNA2 */
1718 div_ant_conf.main_lna_conf =
1719 ATH_ANT_DIV_COMB_LNA1;
1720 div_ant_conf.alt_lna_conf =
1721 ATH_ANT_DIV_COMB_LNA2;
1722 }
1723 }
1724 break;
1725 default:
1726 break;
1727 }
1728 } else {
1729 if (!antcomb->alt_good) {
1730 antcomb->scan_not_start = false;
1731 /* Set alt to another LNA */
1732 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
1733 div_ant_conf.main_lna_conf =
1734 ATH_ANT_DIV_COMB_LNA2;
1735 div_ant_conf.alt_lna_conf =
1736 ATH_ANT_DIV_COMB_LNA1;
1737 } else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
1738 div_ant_conf.main_lna_conf =
1739 ATH_ANT_DIV_COMB_LNA1;
1740 div_ant_conf.alt_lna_conf =
1741 ATH_ANT_DIV_COMB_LNA2;
1742 }
1743 goto div_comb_done;
1744 }
1745 }
1746
1747 ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
1748 main_rssi_avg, alt_rssi_avg,
1749 alt_ratio);
1750
1751 antcomb->quick_scan_cnt++;
1752
1753div_comb_done:
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301754 ath_ant_div_conf_fast_divbias(&div_ant_conf, antcomb, alt_ratio);
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001755 ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf);
1756
1757 antcomb->scan_start_time = jiffies;
1758 antcomb->total_pkt_count = 0;
1759 antcomb->main_total_rssi = 0;
1760 antcomb->alt_total_rssi = 0;
1761 antcomb->main_recv_cnt = 0;
1762 antcomb->alt_recv_cnt = 0;
1763}
1764
Felix Fietkaub5c804752010-04-15 17:38:48 -04001765int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1766{
1767 struct ath_buf *bf;
Felix Fietkau0d955212011-01-26 18:23:27 +01001768 struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001769 struct ieee80211_rx_status *rxs;
Sujithcbe61d82009-02-09 13:27:12 +05301770 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -07001771 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau7545daf2011-01-24 19:23:16 +01001772 struct ieee80211_hw *hw = sc->hw;
Sujithbe0418a2008-11-18 09:05:55 +05301773 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc9b14172009-11-04 16:47:22 -08001774 int retval;
Sujithbe0418a2008-11-18 09:05:55 +05301775 bool decrypt_error = false;
Felix Fietkau29bffa92010-03-29 20:14:23 -07001776 struct ath_rx_status rs;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001777 enum ath9k_rx_qtype qtype;
1778 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1779 int dma_type;
Vasanthakumar Thiagarajan5c6dd922010-05-20 14:34:47 -07001780 u8 rx_status_len = ah->caps.rx_status_len;
Felix Fietkaua6d20552010-06-12 00:33:54 -04001781 u64 tsf = 0;
1782 u32 tsf_lower = 0;
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001783 unsigned long flags;
Sujithbe0418a2008-11-18 09:05:55 +05301784
Felix Fietkaub5c804752010-04-15 17:38:48 -04001785 if (edma)
Felix Fietkaub5c804752010-04-15 17:38:48 -04001786 dma_type = DMA_BIDIRECTIONAL;
Ming Lei56824222010-05-14 21:15:38 +08001787 else
1788 dma_type = DMA_FROM_DEVICE;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001789
1790 qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
Sujithb77f4832008-12-07 21:44:03 +05301791 spin_lock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001792
Felix Fietkaua6d20552010-06-12 00:33:54 -04001793 tsf = ath9k_hw_gettsf64(ah);
1794 tsf_lower = tsf & 0xffffffff;
1795
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001796 do {
1797 /* If handling rx interrupt and flush is in progress => exit */
Sujith98deeea2008-08-11 14:05:46 +05301798 if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001799 break;
1800
Felix Fietkau29bffa92010-03-29 20:14:23 -07001801 memset(&rs, 0, sizeof(rs));
Felix Fietkaub5c804752010-04-15 17:38:48 -04001802 if (edma)
1803 bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1804 else
1805 bf = ath_get_next_rx_buf(sc, &rs);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001806
Felix Fietkaub5c804752010-04-15 17:38:48 -04001807 if (!bf)
1808 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001809
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001810 skb = bf->bf_mpdu;
Sujithbe0418a2008-11-18 09:05:55 +05301811 if (!skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001812 continue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001813
Felix Fietkau0d955212011-01-26 18:23:27 +01001814 /*
1815 * Take frame header from the first fragment and RX status from
1816 * the last one.
1817 */
1818 if (sc->rx.frag)
1819 hdr_skb = sc->rx.frag;
1820 else
1821 hdr_skb = skb;
1822
1823 hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
1824 rxs = IEEE80211_SKB_RXCB(hdr_skb);
Rajkumar Manoharancf3af742011-08-27 16:17:47 +05301825 if (ieee80211_is_beacon(hdr->frame_control) &&
1826 !compare_ether_addr(hdr->addr3, common->curbssid))
1827 rs.is_mybeacon = true;
1828 else
1829 rs.is_mybeacon = false;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001830
Felix Fietkau29bffa92010-03-29 20:14:23 -07001831 ath_debug_stat_rx(sc, &rs);
Sujith1395d3f2010-01-08 10:36:11 +05301832
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301833 /*
Sujithbe0418a2008-11-18 09:05:55 +05301834 * If we're asked to flush receive queue, directly
1835 * chain it back at the queue without processing it.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001836 */
Felix Fietkau34832882011-09-14 21:23:03 +02001837 if (sc->sc_flags & SC_OP_RXFLUSH)
Felix Fietkau0d955212011-01-26 18:23:27 +01001838 goto requeue_drop_frag;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001839
Jan Friedrichc8f3b722010-08-02 23:55:50 +02001840 retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
1841 rxs, &decrypt_error);
1842 if (retval)
Felix Fietkau0d955212011-01-26 18:23:27 +01001843 goto requeue_drop_frag;
Jan Friedrichc8f3b722010-08-02 23:55:50 +02001844
Felix Fietkaua6d20552010-06-12 00:33:54 -04001845 rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1846 if (rs.rs_tstamp > tsf_lower &&
1847 unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1848 rxs->mactime -= 0x100000000ULL;
1849
1850 if (rs.rs_tstamp < tsf_lower &&
1851 unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1852 rxs->mactime += 0x100000000ULL;
1853
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001854 /* Ensure we always have an skb to requeue once we are done
1855 * processing the current buffer's skb */
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001856 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001857
1858 /* If there is no memory we ignore the current RX'd frame,
1859 * tell hardware it can give us a new frame using the old
Sujithb77f4832008-12-07 21:44:03 +05301860 * skb and put it at the tail of the sc->rx.rxbuf list for
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001861 * processing. */
1862 if (!requeue_skb)
Felix Fietkau0d955212011-01-26 18:23:27 +01001863 goto requeue_drop_frag;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001864
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301865 /* Unmap the frame */
Gabor Juhos7da3c552009-01-14 20:17:03 +01001866 dma_unmap_single(sc->dev, bf->bf_buf_addr,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001867 common->rx_bufsize,
Felix Fietkaub5c804752010-04-15 17:38:48 -04001868 dma_type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001869
Felix Fietkaub5c804752010-04-15 17:38:48 -04001870 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1871 if (ah->caps.rx_status_len)
1872 skb_pull(skb, ah->caps.rx_status_len);
Sujithbe0418a2008-11-18 09:05:55 +05301873
Felix Fietkau0d955212011-01-26 18:23:27 +01001874 if (!rs.rs_more)
1875 ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1876 rxs, decrypt_error);
Sujithbe0418a2008-11-18 09:05:55 +05301877
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001878 /* We will now give hardware our shiny new allocated skb */
1879 bf->bf_mpdu = requeue_skb;
Gabor Juhos7da3c552009-01-14 20:17:03 +01001880 bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001881 common->rx_bufsize,
Felix Fietkaub5c804752010-04-15 17:38:48 -04001882 dma_type);
Gabor Juhos7da3c552009-01-14 20:17:03 +01001883 if (unlikely(dma_mapping_error(sc->dev,
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001884 bf->bf_buf_addr))) {
1885 dev_kfree_skb_any(requeue_skb);
1886 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -07001887 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -08001888 ath_err(common, "dma_mapping_error() on RX\n");
Felix Fietkau7545daf2011-01-24 19:23:16 +01001889 ieee80211_rx(hw, skb);
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001890 break;
1891 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001892
Felix Fietkau0d955212011-01-26 18:23:27 +01001893 if (rs.rs_more) {
1894 /*
1895 * rs_more indicates chained descriptors which can be
1896 * used to link buffers together for a sort of
1897 * scatter-gather operation.
1898 */
1899 if (sc->rx.frag) {
1900 /* too many fragments - cannot handle frame */
1901 dev_kfree_skb_any(sc->rx.frag);
1902 dev_kfree_skb_any(skb);
1903 skb = NULL;
1904 }
1905 sc->rx.frag = skb;
1906 goto requeue;
1907 }
1908
1909 if (sc->rx.frag) {
1910 int space = skb->len - skb_tailroom(hdr_skb);
1911
1912 sc->rx.frag = NULL;
1913
1914 if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
1915 dev_kfree_skb(skb);
1916 goto requeue_drop_frag;
1917 }
1918
1919 skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
1920 skb->len);
1921 dev_kfree_skb_any(skb);
1922 skb = hdr_skb;
1923 }
1924
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001925 /*
1926 * change the default rx antenna if rx diversity chooses the
1927 * other antenna 3 times in a row.
1928 */
Felix Fietkau29bffa92010-03-29 20:14:23 -07001929 if (sc->rx.defant != rs.rs_antenna) {
Sujithb77f4832008-12-07 21:44:03 +05301930 if (++sc->rx.rxotherant >= 3)
Felix Fietkau29bffa92010-03-29 20:14:23 -07001931 ath_setdefantenna(sc, rs.rs_antenna);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001932 } else {
Sujithb77f4832008-12-07 21:44:03 +05301933 sc->rx.rxotherant = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001934 }
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301935
Felix Fietkau66760ea2011-07-13 23:35:05 +08001936 if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
1937 skb_trim(skb, skb->len - 8);
1938
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001939 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Mohammed Shafi Shajakhanaaef24b2010-12-07 20:40:58 +05301940
1941 if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
Rajkumar Manoharanf73c6042011-09-26 22:16:56 +05301942 PS_WAIT_FOR_CAB |
1943 PS_WAIT_FOR_PSPOLL_DATA)) ||
1944 ath9k_check_auto_sleep(sc))
1945 ath_rx_ps(sc, skb, rs.is_mybeacon);
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001946 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Jouni Malinencc659652009-05-14 21:28:48 +03001947
Felix Fietkau43c35282011-09-03 01:40:27 +02001948 if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx == 3)
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001949 ath_ant_comb_scan(sc, &rs);
1950
Felix Fietkau7545daf2011-01-24 19:23:16 +01001951 ieee80211_rx(hw, skb);
Jouni Malinencc659652009-05-14 21:28:48 +03001952
Felix Fietkau0d955212011-01-26 18:23:27 +01001953requeue_drop_frag:
1954 if (sc->rx.frag) {
1955 dev_kfree_skb_any(sc->rx.frag);
1956 sc->rx.frag = NULL;
1957 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001958requeue:
Felix Fietkaub5c804752010-04-15 17:38:48 -04001959 if (edma) {
1960 list_add_tail(&bf->list, &sc->rx.rxbuf);
1961 ath_rx_edma_buf_link(sc, qtype);
1962 } else {
1963 list_move_tail(&bf->list, &sc->rx.rxbuf);
1964 ath_rx_buf_link(sc, bf);
Felix Fietkau34832882011-09-14 21:23:03 +02001965 if (!flush)
1966 ath9k_hw_rxena(ah);
Felix Fietkaub5c804752010-04-15 17:38:48 -04001967 }
Sujithbe0418a2008-11-18 09:05:55 +05301968 } while (1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001969
Sujithb77f4832008-12-07 21:44:03 +05301970 spin_unlock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001971
Rajkumar Manoharan29ab0b32011-08-13 10:28:10 +05301972 if (!(ah->imask & ATH9K_INT_RXEOL)) {
1973 ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
Felix Fietkau72d874c2011-10-08 20:06:19 +02001974 ath9k_hw_set_interrupts(ah);
Rajkumar Manoharan29ab0b32011-08-13 10:28:10 +05301975 }
1976
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001977 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001978}