blob: bcc0b222ec18b7a0091fc913b6269510e3f3d14c [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{
208 struct ath_buf *bf;
209
210 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
211 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
212
213 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
214 if (bf->bf_mpdu)
215 dev_kfree_skb_any(bf->bf_mpdu);
216 }
217
218 INIT_LIST_HEAD(&sc->rx.rxbuf);
219
220 kfree(sc->rx.rx_bufptr);
221 sc->rx.rx_bufptr = NULL;
222}
223
224static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
225{
226 skb_queue_head_init(&rx_edma->rx_fifo);
227 skb_queue_head_init(&rx_edma->rx_buffers);
228 rx_edma->rx_fifo_hwsize = size;
229}
230
231static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
232{
233 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
234 struct ath_hw *ah = sc->sc_ah;
235 struct sk_buff *skb;
236 struct ath_buf *bf;
237 int error = 0, i;
238 u32 size;
239
Felix Fietkaub5c804752010-04-15 17:38:48 -0400240 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
241 ah->caps.rx_status_len);
242
243 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
244 ah->caps.rx_lp_qdepth);
245 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
246 ah->caps.rx_hp_qdepth);
247
248 size = sizeof(struct ath_buf) * nbufs;
249 bf = kzalloc(size, GFP_KERNEL);
250 if (!bf)
251 return -ENOMEM;
252
253 INIT_LIST_HEAD(&sc->rx.rxbuf);
254 sc->rx.rx_bufptr = bf;
255
256 for (i = 0; i < nbufs; i++, bf++) {
257 skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
258 if (!skb) {
259 error = -ENOMEM;
260 goto rx_init_fail;
261 }
262
263 memset(skb->data, 0, common->rx_bufsize);
264 bf->bf_mpdu = skb;
265
266 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
267 common->rx_bufsize,
268 DMA_BIDIRECTIONAL);
269 if (unlikely(dma_mapping_error(sc->dev,
270 bf->bf_buf_addr))) {
271 dev_kfree_skb_any(skb);
272 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -0700273 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -0800274 ath_err(common,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400275 "dma_mapping_error() on RX init\n");
276 error = -ENOMEM;
277 goto rx_init_fail;
278 }
279
280 list_add_tail(&bf->list, &sc->rx.rxbuf);
281 }
282
283 return 0;
284
285rx_init_fail:
286 ath_rx_edma_cleanup(sc);
287 return error;
288}
289
290static void ath_edma_start_recv(struct ath_softc *sc)
291{
292 spin_lock_bh(&sc->rx.rxbuflock);
293
294 ath9k_hw_rxena(sc->sc_ah);
295
296 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
297 sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
298
299 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
300 sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
301
Felix Fietkaub5c804752010-04-15 17:38:48 -0400302 ath_opmode_init(sc);
303
Luis R. Rodriguez48a6a462010-09-16 15:12:28 -0400304 ath9k_hw_startpcureceive(sc->sc_ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
Luis R. Rodriguez7583c5502010-10-20 16:07:04 -0700305
306 spin_unlock_bh(&sc->rx.rxbuflock);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400307}
308
309static void ath_edma_stop_recv(struct ath_softc *sc)
310{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400311 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
312 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400313}
314
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700315int ath_rx_init(struct ath_softc *sc, int nbufs)
316{
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -0700317 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700318 struct sk_buff *skb;
319 struct ath_buf *bf;
320 int error = 0;
321
Luis R. Rodriguez4bdd1e92010-10-26 15:27:24 -0700322 spin_lock_init(&sc->sc_pcu_lock);
Sujith797fe5cb2009-03-30 15:28:45 +0530323 sc->sc_flags &= ~SC_OP_RXFLUSH;
324 spin_lock_init(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700325
Felix Fietkau0d955212011-01-26 18:23:27 +0100326 common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
327 sc->sc_ah->caps.rx_status_len;
328
Felix Fietkaub5c804752010-04-15 17:38:48 -0400329 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
330 return ath_rx_edma_init(sc, nbufs);
331 } else {
Joe Perches226afe62010-12-02 19:12:37 -0800332 ath_dbg(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
333 common->cachelsz, common->rx_bufsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700334
Felix Fietkaub5c804752010-04-15 17:38:48 -0400335 /* Initialize rx descriptors */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700336
Felix Fietkaub5c804752010-04-15 17:38:48 -0400337 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
Vasanthakumar Thiagarajan4adfcde2010-04-15 17:39:33 -0400338 "rx", nbufs, 1, 0);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400339 if (error != 0) {
Joe Perches38002762010-12-02 19:12:36 -0800340 ath_err(common,
341 "failed to allocate rx descriptors: %d\n",
342 error);
Sujith797fe5cb2009-03-30 15:28:45 +0530343 goto err;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700344 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400345
346 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
347 skb = ath_rxbuf_alloc(common, common->rx_bufsize,
348 GFP_KERNEL);
349 if (skb == NULL) {
350 error = -ENOMEM;
351 goto err;
352 }
353
354 bf->bf_mpdu = skb;
355 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
356 common->rx_bufsize,
357 DMA_FROM_DEVICE);
358 if (unlikely(dma_mapping_error(sc->dev,
359 bf->bf_buf_addr))) {
360 dev_kfree_skb_any(skb);
361 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -0700362 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -0800363 ath_err(common,
364 "dma_mapping_error() on RX init\n");
Felix Fietkaub5c804752010-04-15 17:38:48 -0400365 error = -ENOMEM;
366 goto err;
367 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400368 }
369 sc->rx.rxlink = NULL;
Sujith797fe5cb2009-03-30 15:28:45 +0530370 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700371
Sujith797fe5cb2009-03-30 15:28:45 +0530372err:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700373 if (error)
374 ath_rx_cleanup(sc);
375
376 return error;
377}
378
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700379void ath_rx_cleanup(struct ath_softc *sc)
380{
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800381 struct ath_hw *ah = sc->sc_ah;
382 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700383 struct sk_buff *skb;
384 struct ath_buf *bf;
385
Felix Fietkaub5c804752010-04-15 17:38:48 -0400386 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
387 ath_rx_edma_cleanup(sc);
388 return;
389 } else {
390 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
391 skb = bf->bf_mpdu;
392 if (skb) {
393 dma_unmap_single(sc->dev, bf->bf_buf_addr,
394 common->rx_bufsize,
395 DMA_FROM_DEVICE);
396 dev_kfree_skb(skb);
Ben Greear6cf9e992010-10-14 12:45:30 -0700397 bf->bf_buf_addr = 0;
398 bf->bf_mpdu = NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400399 }
Luis R. Rodriguez051b9192009-03-23 18:25:01 -0400400 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700401
Felix Fietkaub5c804752010-04-15 17:38:48 -0400402 if (sc->rx.rxdma.dd_desc_len != 0)
403 ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
404 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700405}
406
407/*
408 * Calculate the receive filter according to the
409 * operating mode and state:
410 *
411 * o always accept unicast, broadcast, and multicast traffic
412 * o maintain current state of phy error reception (the hal
413 * may enable phy error frames for noise immunity work)
414 * o probe request frames are accepted only when operating in
415 * hostap, adhoc, or monitor modes
416 * o enable promiscuous mode according to the interface state
417 * o accept beacons:
418 * - when operating in adhoc mode so the 802.11 layer creates
419 * node table entries for peers,
420 * - when operating in station mode for collecting rssi data when
421 * the station is otherwise quiet, or
422 * - when operating as a repeater so we see repeater-sta beacons
423 * - when scanning
424 */
425
426u32 ath_calcrxfilter(struct ath_softc *sc)
427{
428#define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
Sujith7dcfdcd2008-08-11 14:03:13 +0530429
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700430 u32 rfilt;
431
432 rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
433 | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
434 | ATH9K_RX_FILTER_MCAST;
435
Jouni Malinen9c1d8e42010-10-13 17:29:31 +0300436 if (sc->rx.rxfilter & FIF_PROBE_REQ)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700437 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
438
Jouni Malinen217ba9d2009-03-10 10:55:50 +0200439 /*
440 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
441 * mode interface or when in monitor mode. AP mode does not need this
442 * since it receives all in-BSS frames anyway.
443 */
Felix Fietkau2e286942011-03-09 01:48:12 +0100444 if (sc->sc_ah->is_monitoring)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700445 rfilt |= ATH9K_RX_FILTER_PROM;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700446
Sujithd42c6b72009-02-04 08:10:22 +0530447 if (sc->rx.rxfilter & FIF_CONTROL)
448 rfilt |= ATH9K_RX_FILTER_CONTROL;
449
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530450 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
Ben Greearcfda6692010-09-14 12:00:22 -0700451 (sc->nvifs <= 1) &&
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530452 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
453 rfilt |= ATH9K_RX_FILTER_MYBEACON;
454 else
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700455 rfilt |= ATH9K_RX_FILTER_BEACON;
456
Felix Fietkau264bbec2011-04-07 19:24:23 +0200457 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
Senthil Balasubramanian66afad02009-09-18 15:06:07 +0530458 (sc->rx.rxfilter & FIF_PSPOLL))
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530459 rfilt |= ATH9K_RX_FILTER_PSPOLL;
Sujithbe0418a2008-11-18 09:05:55 +0530460
Sujith7ea310b2009-09-03 12:08:43 +0530461 if (conf_is_ht(&sc->hw->conf))
462 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
463
Felix Fietkau7545daf2011-01-24 19:23:16 +0100464 if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
Javier Cardona5eb6ba82009-08-20 19:12:07 -0700465 /* The following may also be needed for other older chips */
466 if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
467 rfilt |= ATH9K_RX_FILTER_PROM;
Jouni Malinenb93bce22009-03-03 19:23:30 +0200468 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
469 }
470
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700471 return rfilt;
Sujith7dcfdcd2008-08-11 14:03:13 +0530472
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700473#undef RX_FILTER_PRESERVE
474}
475
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700476int ath_startrecv(struct ath_softc *sc)
477{
Sujithcbe61d82009-02-09 13:27:12 +0530478 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700479 struct ath_buf *bf, *tbf;
480
Felix Fietkaub5c804752010-04-15 17:38:48 -0400481 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
482 ath_edma_start_recv(sc);
483 return 0;
484 }
485
Sujithb77f4832008-12-07 21:44:03 +0530486 spin_lock_bh(&sc->rx.rxbuflock);
487 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700488 goto start_recv;
489
Sujithb77f4832008-12-07 21:44:03 +0530490 sc->rx.rxlink = NULL;
491 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700492 ath_rx_buf_link(sc, bf);
493 }
494
495 /* We could have deleted elements so the list may be empty now */
Sujithb77f4832008-12-07 21:44:03 +0530496 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700497 goto start_recv;
498
Sujithb77f4832008-12-07 21:44:03 +0530499 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700500 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
Sujithbe0418a2008-11-18 09:05:55 +0530501 ath9k_hw_rxena(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700502
503start_recv:
Sujithbe0418a2008-11-18 09:05:55 +0530504 ath_opmode_init(sc);
Luis R. Rodriguez48a6a462010-09-16 15:12:28 -0400505 ath9k_hw_startpcureceive(ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
Sujithbe0418a2008-11-18 09:05:55 +0530506
Luis R. Rodriguez7583c5502010-10-20 16:07:04 -0700507 spin_unlock_bh(&sc->rx.rxbuflock);
508
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700509 return 0;
510}
511
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700512bool ath_stoprecv(struct ath_softc *sc)
513{
Sujithcbe61d82009-02-09 13:27:12 +0530514 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau5882da022011-04-08 20:13:18 +0200515 bool stopped, reset = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700516
Luis R. Rodriguez1e450282010-10-20 16:07:03 -0700517 spin_lock_bh(&sc->rx.rxbuflock);
Felix Fietkaud47844a2010-11-20 03:08:47 +0100518 ath9k_hw_abortpcurecv(ah);
Sujithbe0418a2008-11-18 09:05:55 +0530519 ath9k_hw_setrxfilter(ah, 0);
Felix Fietkau5882da022011-04-08 20:13:18 +0200520 stopped = ath9k_hw_stopdmarecv(ah, &reset);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400521
522 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
523 ath_edma_stop_recv(sc);
524 else
525 sc->rx.rxlink = NULL;
Luis R. Rodriguez1e450282010-10-20 16:07:03 -0700526 spin_unlock_bh(&sc->rx.rxbuflock);
Sujithbe0418a2008-11-18 09:05:55 +0530527
Rajkumar Manoharand5847472010-12-20 14:39:51 +0530528 if (!(ah->ah_flags & AH_UNPLUGGED) &&
529 unlikely(!stopped)) {
Ben Greeard7fd1b502010-12-06 13:13:07 -0800530 ath_err(ath9k_hw_common(sc->sc_ah),
531 "Could not stop RX, we could be "
532 "confusing the DMA engine when we start RX up\n");
533 ATH_DBG_WARN_ON_ONCE(!stopped);
534 }
Felix Fietkau2232d312011-04-15 00:41:43 +0200535 return stopped && !reset;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700536}
537
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700538void ath_flushrecv(struct ath_softc *sc)
539{
Sujith98deeea2008-08-11 14:05:46 +0530540 sc->sc_flags |= SC_OP_RXFLUSH;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400541 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
542 ath_rx_tasklet(sc, 1, true);
543 ath_rx_tasklet(sc, 1, false);
Sujith98deeea2008-08-11 14:05:46 +0530544 sc->sc_flags &= ~SC_OP_RXFLUSH;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700545}
546
Jouni Malinencc659652009-05-14 21:28:48 +0300547static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
548{
549 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
550 struct ieee80211_mgmt *mgmt;
551 u8 *pos, *end, id, elen;
552 struct ieee80211_tim_ie *tim;
553
554 mgmt = (struct ieee80211_mgmt *)skb->data;
555 pos = mgmt->u.beacon.variable;
556 end = skb->data + skb->len;
557
558 while (pos + 2 < end) {
559 id = *pos++;
560 elen = *pos++;
561 if (pos + elen > end)
562 break;
563
564 if (id == WLAN_EID_TIM) {
565 if (elen < sizeof(*tim))
566 break;
567 tim = (struct ieee80211_tim_ie *) pos;
568 if (tim->dtim_count != 0)
569 break;
570 return tim->bitmap_ctrl & 0x01;
571 }
572
573 pos += elen;
574 }
575
576 return false;
577}
578
Jouni Malinencc659652009-05-14 21:28:48 +0300579static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
580{
581 struct ieee80211_mgmt *mgmt;
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700582 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300583
584 if (skb->len < 24 + 8 + 2 + 2)
585 return;
586
587 mgmt = (struct ieee80211_mgmt *)skb->data;
Ben Greear48014162011-01-15 19:13:48 +0000588 if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0) {
589 /* TODO: This doesn't work well if you have stations
590 * associated to two different APs because curbssid
591 * is just the last AP that any of the stations associated
592 * with.
593 */
Jouni Malinencc659652009-05-14 21:28:48 +0300594 return; /* not from our current AP */
Ben Greear48014162011-01-15 19:13:48 +0000595 }
Jouni Malinencc659652009-05-14 21:28:48 +0300596
Sujith1b04b932010-01-08 10:36:05 +0530597 sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
Gabor Juhos293dc5d2009-06-19 12:17:48 +0200598
Sujith1b04b932010-01-08 10:36:05 +0530599 if (sc->ps_flags & PS_BEACON_SYNC) {
600 sc->ps_flags &= ~PS_BEACON_SYNC;
Joe Perches226afe62010-12-02 19:12:37 -0800601 ath_dbg(common, ATH_DBG_PS,
602 "Reconfigure Beacon timers based on timestamp from the AP\n");
Rajkumar Manoharan99e4d432011-04-04 22:56:19 +0530603 ath_set_beacon(sc);
Jouni Malinenccdfeab2009-05-20 21:59:08 +0300604 }
605
Jouni Malinencc659652009-05-14 21:28:48 +0300606 if (ath_beacon_dtim_pending_cab(skb)) {
607 /*
608 * Remain awake waiting for buffered broadcast/multicast
Gabor Juhos58f5fff2009-06-17 20:53:20 +0200609 * frames. If the last broadcast/multicast frame is not
610 * received properly, the next beacon frame will work as
611 * a backup trigger for returning into NETWORK SLEEP state,
612 * so we are waiting for it as well.
Jouni Malinencc659652009-05-14 21:28:48 +0300613 */
Joe Perches226afe62010-12-02 19:12:37 -0800614 ath_dbg(common, ATH_DBG_PS,
615 "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
Sujith1b04b932010-01-08 10:36:05 +0530616 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
Jouni Malinencc659652009-05-14 21:28:48 +0300617 return;
618 }
619
Sujith1b04b932010-01-08 10:36:05 +0530620 if (sc->ps_flags & PS_WAIT_FOR_CAB) {
Jouni Malinencc659652009-05-14 21:28:48 +0300621 /*
622 * This can happen if a broadcast frame is dropped or the AP
623 * fails to send a frame indicating that all CAB frames have
624 * been delivered.
625 */
Sujith1b04b932010-01-08 10:36:05 +0530626 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
Joe Perches226afe62010-12-02 19:12:37 -0800627 ath_dbg(common, ATH_DBG_PS,
628 "PS wait for CAB frames timed out\n");
Jouni Malinencc659652009-05-14 21:28:48 +0300629 }
Jouni Malinencc659652009-05-14 21:28:48 +0300630}
631
632static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb)
633{
634 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700635 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300636
637 hdr = (struct ieee80211_hdr *)skb->data;
638
639 /* Process Beacon and CAB receive in PS state */
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -0700640 if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
641 && ieee80211_is_beacon(hdr->frame_control))
Jouni Malinencc659652009-05-14 21:28:48 +0300642 ath_rx_ps_beacon(sc, skb);
Sujith1b04b932010-01-08 10:36:05 +0530643 else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
Jouni Malinencc659652009-05-14 21:28:48 +0300644 (ieee80211_is_data(hdr->frame_control) ||
645 ieee80211_is_action(hdr->frame_control)) &&
646 is_multicast_ether_addr(hdr->addr1) &&
647 !ieee80211_has_moredata(hdr->frame_control)) {
Jouni Malinencc659652009-05-14 21:28:48 +0300648 /*
649 * No more broadcast/multicast frames to be received at this
650 * point.
651 */
Senthil Balasubramanian3fac6df2010-09-16 15:12:35 -0400652 sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
Joe Perches226afe62010-12-02 19:12:37 -0800653 ath_dbg(common, ATH_DBG_PS,
654 "All PS CAB frames received, back to sleep\n");
Sujith1b04b932010-01-08 10:36:05 +0530655 } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300656 !is_multicast_ether_addr(hdr->addr1) &&
657 !ieee80211_has_morefrags(hdr->frame_control)) {
Sujith1b04b932010-01-08 10:36:05 +0530658 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
Joe Perches226afe62010-12-02 19:12:37 -0800659 ath_dbg(common, ATH_DBG_PS,
660 "Going back to sleep after having received PS-Poll data (0x%lx)\n",
Sujith1b04b932010-01-08 10:36:05 +0530661 sc->ps_flags & (PS_WAIT_FOR_BEACON |
662 PS_WAIT_FOR_CAB |
663 PS_WAIT_FOR_PSPOLL_DATA |
664 PS_WAIT_FOR_TX_ACK));
Jouni Malinencc659652009-05-14 21:28:48 +0300665 }
666}
667
Felix Fietkaub5c804752010-04-15 17:38:48 -0400668static bool ath_edma_get_buffers(struct ath_softc *sc,
669 enum ath9k_rx_qtype qtype)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700670{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400671 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
672 struct ath_hw *ah = sc->sc_ah;
673 struct ath_common *common = ath9k_hw_common(ah);
674 struct sk_buff *skb;
Sujithbe0418a2008-11-18 09:05:55 +0530675 struct ath_buf *bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400676 int ret;
677
678 skb = skb_peek(&rx_edma->rx_fifo);
679 if (!skb)
680 return false;
681
682 bf = SKB_CB_ATHBUF(skb);
683 BUG_ON(!bf);
684
Ming Leice9426d2010-05-15 18:25:40 +0800685 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400686 common->rx_bufsize, DMA_FROM_DEVICE);
687
688 ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data);
Ming Leice9426d2010-05-15 18:25:40 +0800689 if (ret == -EINPROGRESS) {
690 /*let device gain the buffer again*/
691 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
692 common->rx_bufsize, DMA_FROM_DEVICE);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400693 return false;
Ming Leice9426d2010-05-15 18:25:40 +0800694 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400695
696 __skb_unlink(skb, &rx_edma->rx_fifo);
697 if (ret == -EINVAL) {
698 /* corrupt descriptor, skip this one and the following one */
699 list_add_tail(&bf->list, &sc->rx.rxbuf);
700 ath_rx_edma_buf_link(sc, qtype);
701 skb = skb_peek(&rx_edma->rx_fifo);
702 if (!skb)
703 return true;
704
705 bf = SKB_CB_ATHBUF(skb);
706 BUG_ON(!bf);
707
708 __skb_unlink(skb, &rx_edma->rx_fifo);
709 list_add_tail(&bf->list, &sc->rx.rxbuf);
710 ath_rx_edma_buf_link(sc, qtype);
Vasanthakumar Thiagarajan083e3e82010-05-10 19:41:34 -0700711 return true;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400712 }
713 skb_queue_tail(&rx_edma->rx_buffers, skb);
714
715 return true;
716}
717
718static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
719 struct ath_rx_status *rs,
720 enum ath9k_rx_qtype qtype)
721{
722 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
723 struct sk_buff *skb;
724 struct ath_buf *bf;
725
726 while (ath_edma_get_buffers(sc, qtype));
727 skb = __skb_dequeue(&rx_edma->rx_buffers);
728 if (!skb)
729 return NULL;
730
731 bf = SKB_CB_ATHBUF(skb);
732 ath9k_hw_process_rxdesc_edma(sc->sc_ah, rs, skb->data);
733 return bf;
734}
735
736static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
737 struct ath_rx_status *rs)
738{
739 struct ath_hw *ah = sc->sc_ah;
740 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700741 struct ath_desc *ds;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400742 struct ath_buf *bf;
743 int ret;
744
745 if (list_empty(&sc->rx.rxbuf)) {
746 sc->rx.rxlink = NULL;
747 return NULL;
748 }
749
750 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
751 ds = bf->bf_desc;
752
753 /*
754 * Must provide the virtual address of the current
755 * descriptor, the physical address, and the virtual
756 * address of the next descriptor in the h/w chain.
757 * This allows the HAL to look ahead to see if the
758 * hardware is done with a descriptor by checking the
759 * done bit in the following descriptor and the address
760 * of the current descriptor the DMA engine is working
761 * on. All this is necessary because of our use of
762 * a self-linked list to avoid rx overruns.
763 */
Rajkumar Manoharan3de21112011-08-13 10:28:11 +0530764 ret = ath9k_hw_rxprocdesc(ah, ds, rs);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400765 if (ret == -EINPROGRESS) {
766 struct ath_rx_status trs;
767 struct ath_buf *tbf;
768 struct ath_desc *tds;
769
770 memset(&trs, 0, sizeof(trs));
771 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
772 sc->rx.rxlink = NULL;
773 return NULL;
774 }
775
776 tbf = list_entry(bf->list.next, struct ath_buf, list);
777
778 /*
779 * On some hardware the descriptor status words could
780 * get corrupted, including the done bit. Because of
781 * this, check if the next descriptor's done bit is
782 * set or not.
783 *
784 * If the next descriptor's done bit is set, the current
785 * descriptor has been corrupted. Force s/w to discard
786 * this descriptor and continue...
787 */
788
789 tds = tbf->bf_desc;
Rajkumar Manoharan3de21112011-08-13 10:28:11 +0530790 ret = ath9k_hw_rxprocdesc(ah, tds, &trs);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400791 if (ret == -EINPROGRESS)
792 return NULL;
793 }
794
795 if (!bf->bf_mpdu)
796 return bf;
797
798 /*
799 * Synchronize the DMA transfer with CPU before
800 * 1. accessing the frame
801 * 2. requeueing the same buffer to h/w
802 */
Ming Leice9426d2010-05-15 18:25:40 +0800803 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400804 common->rx_bufsize,
805 DMA_FROM_DEVICE);
806
807 return bf;
808}
809
Sujithd4357002010-05-20 15:34:38 +0530810/* Assumes you've already done the endian to CPU conversion */
811static bool ath9k_rx_accept(struct ath_common *common,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700812 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530813 struct ieee80211_rx_status *rxs,
814 struct ath_rx_status *rx_stats,
815 bool *decrypt_error)
816{
Felix Fietkau66760ea2011-07-13 23:35:05 +0800817 bool is_mc, is_valid_tkip, strip_mic, mic_error;
Sujithd4357002010-05-20 15:34:38 +0530818 struct ath_hw *ah = common->ah;
Sujithd4357002010-05-20 15:34:38 +0530819 __le16 fc;
Vasanthakumar Thiagarajanb7b1b512010-05-20 14:34:48 -0700820 u8 rx_status_len = ah->caps.rx_status_len;
Sujithd4357002010-05-20 15:34:38 +0530821
Sujithd4357002010-05-20 15:34:38 +0530822 fc = hdr->frame_control;
823
Felix Fietkau66760ea2011-07-13 23:35:05 +0800824 is_mc = !!is_multicast_ether_addr(hdr->addr1);
825 is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
826 test_bit(rx_stats->rs_keyix, common->tkip_keymap);
Bill Jordan152e5852011-08-19 11:10:22 -0400827 strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
828 !(rx_stats->rs_status &
Felix Fietkau66760ea2011-07-13 23:35:05 +0800829 (ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC));
830
Sujithd4357002010-05-20 15:34:38 +0530831 if (!rx_stats->rs_datalen)
832 return false;
833 /*
834 * rs_status follows rs_datalen so if rs_datalen is too large
835 * we can take a hint that hardware corrupted it, so ignore
836 * those frames.
837 */
Vasanthakumar Thiagarajanb7b1b512010-05-20 14:34:48 -0700838 if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len))
Sujithd4357002010-05-20 15:34:38 +0530839 return false;
840
Felix Fietkau0d955212011-01-26 18:23:27 +0100841 /* Only use error bits from the last fragment */
Sujithd4357002010-05-20 15:34:38 +0530842 if (rx_stats->rs_more)
Felix Fietkau0d955212011-01-26 18:23:27 +0100843 return true;
Sujithd4357002010-05-20 15:34:38 +0530844
Felix Fietkau66760ea2011-07-13 23:35:05 +0800845 mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
846 !ieee80211_has_morefrags(fc) &&
847 !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
848 (rx_stats->rs_status & ATH9K_RXERR_MIC);
849
Sujithd4357002010-05-20 15:34:38 +0530850 /*
851 * The rx_stats->rs_status will not be set until the end of the
852 * chained descriptors so it can be ignored if rs_more is set. The
853 * rs_more will be false at the last element of the chained
854 * descriptors.
855 */
856 if (rx_stats->rs_status != 0) {
Felix Fietkau66760ea2011-07-13 23:35:05 +0800857 if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
Sujithd4357002010-05-20 15:34:38 +0530858 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800859 mic_error = false;
860 }
Sujithd4357002010-05-20 15:34:38 +0530861 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
862 return false;
863
864 if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
865 *decrypt_error = true;
Felix Fietkau66760ea2011-07-13 23:35:05 +0800866 mic_error = false;
Sujithd4357002010-05-20 15:34:38 +0530867 }
Felix Fietkau66760ea2011-07-13 23:35:05 +0800868
Sujithd4357002010-05-20 15:34:38 +0530869 /*
870 * Reject error frames with the exception of
871 * decryption and MIC failures. For monitor mode,
872 * we also ignore the CRC error.
873 */
Rajkumar Manoharan5f841b42010-10-27 18:31:15 +0530874 if (ah->is_monitoring) {
Sujithd4357002010-05-20 15:34:38 +0530875 if (rx_stats->rs_status &
876 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
877 ATH9K_RXERR_CRC))
878 return false;
879 } else {
880 if (rx_stats->rs_status &
881 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
882 return false;
883 }
884 }
885 }
Felix Fietkau66760ea2011-07-13 23:35:05 +0800886
887 /*
888 * For unicast frames the MIC error bit can have false positives,
889 * so all MIC error reports need to be validated in software.
890 * False negatives are not common, so skip software verification
891 * if the hardware considers the MIC valid.
892 */
893 if (strip_mic)
894 rxs->flag |= RX_FLAG_MMIC_STRIPPED;
895 else if (is_mc && mic_error)
896 rxs->flag |= RX_FLAG_MMIC_ERROR;
897
Sujithd4357002010-05-20 15:34:38 +0530898 return true;
899}
900
901static int ath9k_process_rate(struct ath_common *common,
902 struct ieee80211_hw *hw,
903 struct ath_rx_status *rx_stats,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700904 struct ieee80211_rx_status *rxs)
Sujithd4357002010-05-20 15:34:38 +0530905{
906 struct ieee80211_supported_band *sband;
907 enum ieee80211_band band;
908 unsigned int i = 0;
909
910 band = hw->conf.channel->band;
911 sband = hw->wiphy->bands[band];
912
913 if (rx_stats->rs_rate & 0x80) {
914 /* HT rate */
915 rxs->flag |= RX_FLAG_HT;
916 if (rx_stats->rs_flags & ATH9K_RX_2040)
917 rxs->flag |= RX_FLAG_40MHZ;
918 if (rx_stats->rs_flags & ATH9K_RX_GI)
919 rxs->flag |= RX_FLAG_SHORT_GI;
920 rxs->rate_idx = rx_stats->rs_rate & 0x7f;
921 return 0;
922 }
923
924 for (i = 0; i < sband->n_bitrates; i++) {
925 if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
926 rxs->rate_idx = i;
927 return 0;
928 }
929 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
930 rxs->flag |= RX_FLAG_SHORTPRE;
931 rxs->rate_idx = i;
932 return 0;
933 }
934 }
935
936 /*
937 * No valid hardware bitrate found -- we should not get here
938 * because hardware has already validated this frame as OK.
939 */
Mohammed Shafi Shajakhan9976f622011-08-26 11:10:01 +0530940 ath_dbg(common, ATH_DBG_ANY,
Joe Perches226afe62010-12-02 19:12:37 -0800941 "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
942 rx_stats->rs_rate);
Sujithd4357002010-05-20 15:34:38 +0530943
944 return -EINVAL;
945}
946
947static void ath9k_process_rssi(struct ath_common *common,
948 struct ieee80211_hw *hw,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700949 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530950 struct ath_rx_status *rx_stats)
951{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100952 struct ath_softc *sc = hw->priv;
Sujithd4357002010-05-20 15:34:38 +0530953 struct ath_hw *ah = common->ah;
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200954 int last_rssi;
Sujithd4357002010-05-20 15:34:38 +0530955
Rajkumar Manoharancf3af742011-08-27 16:17:47 +0530956 if (!rx_stats->is_mybeacon ||
957 ((ah->opmode != NL80211_IFTYPE_STATION) &&
958 (ah->opmode != NL80211_IFTYPE_ADHOC)))
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200959 return;
960
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200961 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
Felix Fietkau9ac586152011-01-24 19:23:18 +0100962 ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
Ben Greear686b9cb2010-09-23 09:44:36 -0700963
Felix Fietkau9ac586152011-01-24 19:23:18 +0100964 last_rssi = sc->last_rssi;
Sujithd4357002010-05-20 15:34:38 +0530965 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
966 rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
967 ATH_RSSI_EP_MULTIPLIER);
968 if (rx_stats->rs_rssi < 0)
969 rx_stats->rs_rssi = 0;
970
971 /* Update Beacon RSSI, this is used by ANI. */
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200972 ah->stats.avgbrssi = rx_stats->rs_rssi;
Sujithd4357002010-05-20 15:34:38 +0530973}
974
975/*
976 * For Decrypt or Demic errors, we only mark packet status here and always push
977 * up the frame up to let mac80211 handle the actual error case, be it no
978 * decryption key or real decryption error. This let us keep statistics there.
979 */
980static int ath9k_rx_skb_preprocess(struct ath_common *common,
981 struct ieee80211_hw *hw,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700982 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530983 struct ath_rx_status *rx_stats,
984 struct ieee80211_rx_status *rx_status,
985 bool *decrypt_error)
986{
Felix Fietkauf749b942011-07-28 14:08:57 +0200987 struct ath_hw *ah = common->ah;
988
Sujithd4357002010-05-20 15:34:38 +0530989 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
990
991 /*
992 * everything but the rate is checked here, the rate check is done
993 * separately to avoid doing two lookups for a rate for each frame.
994 */
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700995 if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
Sujithd4357002010-05-20 15:34:38 +0530996 return -EINVAL;
997
Felix Fietkau0d955212011-01-26 18:23:27 +0100998 /* Only use status info from the last fragment */
999 if (rx_stats->rs_more)
1000 return 0;
1001
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -07001002 ath9k_process_rssi(common, hw, hdr, rx_stats);
Sujithd4357002010-05-20 15:34:38 +05301003
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -07001004 if (ath9k_process_rate(common, hw, rx_stats, rx_status))
Sujithd4357002010-05-20 15:34:38 +05301005 return -EINVAL;
1006
Sujithd4357002010-05-20 15:34:38 +05301007 rx_status->band = hw->conf.channel->band;
1008 rx_status->freq = hw->conf.channel->center_freq;
Felix Fietkauf749b942011-07-28 14:08:57 +02001009 rx_status->signal = ah->noise + rx_stats->rs_rssi;
Sujithd4357002010-05-20 15:34:38 +05301010 rx_status->antenna = rx_stats->rs_antenna;
Johannes Berg6ebacbb2011-02-23 15:06:08 +01001011 rx_status->flag |= RX_FLAG_MACTIME_MPDU;
Sujithd4357002010-05-20 15:34:38 +05301012
1013 return 0;
1014}
1015
1016static void ath9k_rx_skb_postprocess(struct ath_common *common,
1017 struct sk_buff *skb,
1018 struct ath_rx_status *rx_stats,
1019 struct ieee80211_rx_status *rxs,
1020 bool decrypt_error)
1021{
1022 struct ath_hw *ah = common->ah;
1023 struct ieee80211_hdr *hdr;
1024 int hdrlen, padpos, padsize;
1025 u8 keyix;
1026 __le16 fc;
1027
1028 /* see if any padding is done by the hw and remove it */
1029 hdr = (struct ieee80211_hdr *) skb->data;
1030 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1031 fc = hdr->frame_control;
1032 padpos = ath9k_cmn_padpos(hdr->frame_control);
1033
1034 /* The MAC header is padded to have 32-bit boundary if the
1035 * packet payload is non-zero. The general calculation for
1036 * padsize would take into account odd header lengths:
1037 * padsize = (4 - padpos % 4) % 4; However, since only
1038 * even-length headers are used, padding can only be 0 or 2
1039 * bytes and we can optimize this a bit. In addition, we must
1040 * not try to remove padding from short control frames that do
1041 * not have payload. */
1042 padsize = padpos & 3;
1043 if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1044 memmove(skb->data + padsize, skb->data, padpos);
1045 skb_pull(skb, padsize);
1046 }
1047
1048 keyix = rx_stats->rs_keyix;
1049
1050 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1051 ieee80211_has_protected(fc)) {
1052 rxs->flag |= RX_FLAG_DECRYPTED;
1053 } else if (ieee80211_has_protected(fc)
1054 && !decrypt_error && skb->len >= hdrlen + 4) {
1055 keyix = skb->data[hdrlen + 3] >> 6;
1056
1057 if (test_bit(keyix, common->keymap))
1058 rxs->flag |= RX_FLAG_DECRYPTED;
1059 }
1060 if (ah->sw_mgmt_crypto &&
1061 (rxs->flag & RX_FLAG_DECRYPTED) &&
1062 ieee80211_is_mgmt(fc))
1063 /* Use software decrypt for management frames. */
1064 rxs->flag &= ~RX_FLAG_DECRYPTED;
1065}
Felix Fietkaub5c804752010-04-15 17:38:48 -04001066
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001067static void ath_lnaconf_alt_good_scan(struct ath_ant_comb *antcomb,
1068 struct ath_hw_antcomb_conf ant_conf,
1069 int main_rssi_avg)
1070{
1071 antcomb->quick_scan_cnt = 0;
1072
1073 if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
1074 antcomb->rssi_lna2 = main_rssi_avg;
1075 else if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA1)
1076 antcomb->rssi_lna1 = main_rssi_avg;
1077
1078 switch ((ant_conf.main_lna_conf << 4) | ant_conf.alt_lna_conf) {
Gabor Juhos223c5a82011-06-21 11:23:45 +02001079 case 0x10: /* LNA2 A-B */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001080 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1081 antcomb->first_quick_scan_conf =
1082 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1083 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1084 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001085 case 0x20: /* LNA1 A-B */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001086 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1087 antcomb->first_quick_scan_conf =
1088 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1089 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1090 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001091 case 0x21: /* LNA1 LNA2 */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001092 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA2;
1093 antcomb->first_quick_scan_conf =
1094 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1095 antcomb->second_quick_scan_conf =
1096 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1097 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001098 case 0x12: /* LNA2 LNA1 */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001099 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1;
1100 antcomb->first_quick_scan_conf =
1101 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1102 antcomb->second_quick_scan_conf =
1103 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1104 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001105 case 0x13: /* LNA2 A+B */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001106 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1107 antcomb->first_quick_scan_conf =
1108 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1109 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1110 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001111 case 0x23: /* LNA1 A+B */
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001112 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1113 antcomb->first_quick_scan_conf =
1114 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1115 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1116 break;
1117 default:
1118 break;
1119 }
1120}
1121
1122static void ath_select_ant_div_from_quick_scan(struct ath_ant_comb *antcomb,
1123 struct ath_hw_antcomb_conf *div_ant_conf,
1124 int main_rssi_avg, int alt_rssi_avg,
1125 int alt_ratio)
1126{
1127 /* alt_good */
1128 switch (antcomb->quick_scan_cnt) {
1129 case 0:
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->first_quick_scan_conf;
1133 break;
1134 case 1:
1135 /* set alt to main, and alt to first conf */
1136 div_ant_conf->main_lna_conf = antcomb->main_conf;
1137 div_ant_conf->alt_lna_conf = antcomb->second_quick_scan_conf;
1138 antcomb->rssi_first = main_rssi_avg;
1139 antcomb->rssi_second = alt_rssi_avg;
1140
1141 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1142 /* main is LNA1 */
1143 if (ath_is_alt_ant_ratio_better(alt_ratio,
1144 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1145 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1146 main_rssi_avg, alt_rssi_avg,
1147 antcomb->total_pkt_count))
1148 antcomb->first_ratio = true;
1149 else
1150 antcomb->first_ratio = false;
1151 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1152 if (ath_is_alt_ant_ratio_better(alt_ratio,
1153 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1154 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1155 main_rssi_avg, alt_rssi_avg,
1156 antcomb->total_pkt_count))
1157 antcomb->first_ratio = true;
1158 else
1159 antcomb->first_ratio = false;
1160 } else {
1161 if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1162 (alt_rssi_avg > main_rssi_avg +
1163 ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1164 (alt_rssi_avg > main_rssi_avg)) &&
1165 (antcomb->total_pkt_count > 50))
1166 antcomb->first_ratio = true;
1167 else
1168 antcomb->first_ratio = false;
1169 }
1170 break;
1171 case 2:
1172 antcomb->alt_good = false;
1173 antcomb->scan_not_start = false;
1174 antcomb->scan = false;
1175 antcomb->rssi_first = main_rssi_avg;
1176 antcomb->rssi_third = alt_rssi_avg;
1177
1178 if (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1)
1179 antcomb->rssi_lna1 = alt_rssi_avg;
1180 else if (antcomb->second_quick_scan_conf ==
1181 ATH_ANT_DIV_COMB_LNA2)
1182 antcomb->rssi_lna2 = alt_rssi_avg;
1183 else if (antcomb->second_quick_scan_conf ==
1184 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2) {
1185 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)
1186 antcomb->rssi_lna2 = main_rssi_avg;
1187 else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1)
1188 antcomb->rssi_lna1 = main_rssi_avg;
1189 }
1190
1191 if (antcomb->rssi_lna2 > antcomb->rssi_lna1 +
1192 ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)
1193 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1194 else
1195 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
1196
1197 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1198 if (ath_is_alt_ant_ratio_better(alt_ratio,
1199 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1200 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1201 main_rssi_avg, alt_rssi_avg,
1202 antcomb->total_pkt_count))
1203 antcomb->second_ratio = true;
1204 else
1205 antcomb->second_ratio = false;
1206 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1207 if (ath_is_alt_ant_ratio_better(alt_ratio,
1208 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1209 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1210 main_rssi_avg, alt_rssi_avg,
1211 antcomb->total_pkt_count))
1212 antcomb->second_ratio = true;
1213 else
1214 antcomb->second_ratio = false;
1215 } else {
1216 if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1217 (alt_rssi_avg > main_rssi_avg +
1218 ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1219 (alt_rssi_avg > main_rssi_avg)) &&
1220 (antcomb->total_pkt_count > 50))
1221 antcomb->second_ratio = true;
1222 else
1223 antcomb->second_ratio = false;
1224 }
1225
1226 /* set alt to the conf with maximun ratio */
1227 if (antcomb->first_ratio && antcomb->second_ratio) {
1228 if (antcomb->rssi_second > antcomb->rssi_third) {
1229 /* first alt*/
1230 if ((antcomb->first_quick_scan_conf ==
1231 ATH_ANT_DIV_COMB_LNA1) ||
1232 (antcomb->first_quick_scan_conf ==
1233 ATH_ANT_DIV_COMB_LNA2))
1234 /* Set alt LNA1 or LNA2*/
1235 if (div_ant_conf->main_lna_conf ==
1236 ATH_ANT_DIV_COMB_LNA2)
1237 div_ant_conf->alt_lna_conf =
1238 ATH_ANT_DIV_COMB_LNA1;
1239 else
1240 div_ant_conf->alt_lna_conf =
1241 ATH_ANT_DIV_COMB_LNA2;
1242 else
1243 /* Set alt to A+B or A-B */
1244 div_ant_conf->alt_lna_conf =
1245 antcomb->first_quick_scan_conf;
1246 } else if ((antcomb->second_quick_scan_conf ==
1247 ATH_ANT_DIV_COMB_LNA1) ||
1248 (antcomb->second_quick_scan_conf ==
1249 ATH_ANT_DIV_COMB_LNA2)) {
1250 /* Set alt LNA1 or LNA2 */
1251 if (div_ant_conf->main_lna_conf ==
1252 ATH_ANT_DIV_COMB_LNA2)
1253 div_ant_conf->alt_lna_conf =
1254 ATH_ANT_DIV_COMB_LNA1;
1255 else
1256 div_ant_conf->alt_lna_conf =
1257 ATH_ANT_DIV_COMB_LNA2;
1258 } else {
1259 /* Set alt to A+B or A-B */
1260 div_ant_conf->alt_lna_conf =
1261 antcomb->second_quick_scan_conf;
1262 }
1263 } else if (antcomb->first_ratio) {
1264 /* first alt */
1265 if ((antcomb->first_quick_scan_conf ==
1266 ATH_ANT_DIV_COMB_LNA1) ||
1267 (antcomb->first_quick_scan_conf ==
1268 ATH_ANT_DIV_COMB_LNA2))
1269 /* Set alt LNA1 or LNA2 */
1270 if (div_ant_conf->main_lna_conf ==
1271 ATH_ANT_DIV_COMB_LNA2)
1272 div_ant_conf->alt_lna_conf =
1273 ATH_ANT_DIV_COMB_LNA1;
1274 else
1275 div_ant_conf->alt_lna_conf =
1276 ATH_ANT_DIV_COMB_LNA2;
1277 else
1278 /* Set alt to A+B or A-B */
1279 div_ant_conf->alt_lna_conf =
1280 antcomb->first_quick_scan_conf;
1281 } else if (antcomb->second_ratio) {
1282 /* second alt */
1283 if ((antcomb->second_quick_scan_conf ==
1284 ATH_ANT_DIV_COMB_LNA1) ||
1285 (antcomb->second_quick_scan_conf ==
1286 ATH_ANT_DIV_COMB_LNA2))
1287 /* Set alt LNA1 or LNA2 */
1288 if (div_ant_conf->main_lna_conf ==
1289 ATH_ANT_DIV_COMB_LNA2)
1290 div_ant_conf->alt_lna_conf =
1291 ATH_ANT_DIV_COMB_LNA1;
1292 else
1293 div_ant_conf->alt_lna_conf =
1294 ATH_ANT_DIV_COMB_LNA2;
1295 else
1296 /* Set alt to A+B or A-B */
1297 div_ant_conf->alt_lna_conf =
1298 antcomb->second_quick_scan_conf;
1299 } else {
1300 /* main is largest */
1301 if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
1302 (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
1303 /* Set alt LNA1 or LNA2 */
1304 if (div_ant_conf->main_lna_conf ==
1305 ATH_ANT_DIV_COMB_LNA2)
1306 div_ant_conf->alt_lna_conf =
1307 ATH_ANT_DIV_COMB_LNA1;
1308 else
1309 div_ant_conf->alt_lna_conf =
1310 ATH_ANT_DIV_COMB_LNA2;
1311 else
1312 /* Set alt to A+B or A-B */
1313 div_ant_conf->alt_lna_conf = antcomb->main_conf;
1314 }
1315 break;
1316 default:
1317 break;
1318 }
1319}
1320
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301321static void ath_ant_div_conf_fast_divbias(struct ath_hw_antcomb_conf *ant_conf,
1322 struct ath_ant_comb *antcomb, int alt_ratio)
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001323{
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301324 if (ant_conf->div_group == 0) {
1325 /* Adjust the fast_div_bias based on main and alt lna conf */
1326 switch ((ant_conf->main_lna_conf << 4) |
1327 ant_conf->alt_lna_conf) {
Gabor Juhos223c5a82011-06-21 11:23:45 +02001328 case 0x01: /* A-B LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301329 ant_conf->fast_div_bias = 0x3b;
1330 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001331 case 0x02: /* A-B LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301332 ant_conf->fast_div_bias = 0x3d;
1333 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001334 case 0x03: /* A-B A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301335 ant_conf->fast_div_bias = 0x1;
1336 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001337 case 0x10: /* LNA2 A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301338 ant_conf->fast_div_bias = 0x7;
1339 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001340 case 0x12: /* LNA2 LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301341 ant_conf->fast_div_bias = 0x2;
1342 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001343 case 0x13: /* LNA2 A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301344 ant_conf->fast_div_bias = 0x7;
1345 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001346 case 0x20: /* LNA1 A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301347 ant_conf->fast_div_bias = 0x6;
1348 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001349 case 0x21: /* LNA1 LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301350 ant_conf->fast_div_bias = 0x0;
1351 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001352 case 0x23: /* LNA1 A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301353 ant_conf->fast_div_bias = 0x6;
1354 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001355 case 0x30: /* A+B A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301356 ant_conf->fast_div_bias = 0x1;
1357 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001358 case 0x31: /* A+B LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301359 ant_conf->fast_div_bias = 0x3b;
1360 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001361 case 0x32: /* A+B LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301362 ant_conf->fast_div_bias = 0x3d;
1363 break;
1364 default:
1365 break;
1366 }
Gabor Juhose7ef5bc2011-06-21 11:23:46 +02001367 } else if (ant_conf->div_group == 1) {
1368 /* Adjust the fast_div_bias based on main and alt_lna_conf */
1369 switch ((ant_conf->main_lna_conf << 4) |
1370 ant_conf->alt_lna_conf) {
1371 case 0x01: /* A-B LNA2 */
1372 ant_conf->fast_div_bias = 0x1;
1373 ant_conf->main_gaintb = 0;
1374 ant_conf->alt_gaintb = 0;
1375 break;
1376 case 0x02: /* A-B LNA1 */
1377 ant_conf->fast_div_bias = 0x1;
1378 ant_conf->main_gaintb = 0;
1379 ant_conf->alt_gaintb = 0;
1380 break;
1381 case 0x03: /* A-B A+B */
1382 ant_conf->fast_div_bias = 0x1;
1383 ant_conf->main_gaintb = 0;
1384 ant_conf->alt_gaintb = 0;
1385 break;
1386 case 0x10: /* LNA2 A-B */
1387 if (!(antcomb->scan) &&
1388 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1389 ant_conf->fast_div_bias = 0x3f;
1390 else
1391 ant_conf->fast_div_bias = 0x1;
1392 ant_conf->main_gaintb = 0;
1393 ant_conf->alt_gaintb = 0;
1394 break;
1395 case 0x12: /* LNA2 LNA1 */
1396 ant_conf->fast_div_bias = 0x1;
1397 ant_conf->main_gaintb = 0;
1398 ant_conf->alt_gaintb = 0;
1399 break;
1400 case 0x13: /* LNA2 A+B */
1401 if (!(antcomb->scan) &&
1402 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1403 ant_conf->fast_div_bias = 0x3f;
1404 else
1405 ant_conf->fast_div_bias = 0x1;
1406 ant_conf->main_gaintb = 0;
1407 ant_conf->alt_gaintb = 0;
1408 break;
1409 case 0x20: /* LNA1 A-B */
1410 if (!(antcomb->scan) &&
1411 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1412 ant_conf->fast_div_bias = 0x3f;
1413 else
1414 ant_conf->fast_div_bias = 0x1;
1415 ant_conf->main_gaintb = 0;
1416 ant_conf->alt_gaintb = 0;
1417 break;
1418 case 0x21: /* LNA1 LNA2 */
1419 ant_conf->fast_div_bias = 0x1;
1420 ant_conf->main_gaintb = 0;
1421 ant_conf->alt_gaintb = 0;
1422 break;
1423 case 0x23: /* LNA1 A+B */
1424 if (!(antcomb->scan) &&
1425 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1426 ant_conf->fast_div_bias = 0x3f;
1427 else
1428 ant_conf->fast_div_bias = 0x1;
1429 ant_conf->main_gaintb = 0;
1430 ant_conf->alt_gaintb = 0;
1431 break;
1432 case 0x30: /* A+B A-B */
1433 ant_conf->fast_div_bias = 0x1;
1434 ant_conf->main_gaintb = 0;
1435 ant_conf->alt_gaintb = 0;
1436 break;
1437 case 0x31: /* A+B LNA2 */
1438 ant_conf->fast_div_bias = 0x1;
1439 ant_conf->main_gaintb = 0;
1440 ant_conf->alt_gaintb = 0;
1441 break;
1442 case 0x32: /* A+B LNA1 */
1443 ant_conf->fast_div_bias = 0x1;
1444 ant_conf->main_gaintb = 0;
1445 ant_conf->alt_gaintb = 0;
1446 break;
1447 default:
1448 break;
1449 }
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301450 } else if (ant_conf->div_group == 2) {
1451 /* Adjust the fast_div_bias based on main and alt_lna_conf */
1452 switch ((ant_conf->main_lna_conf << 4) |
1453 ant_conf->alt_lna_conf) {
Gabor Juhos223c5a82011-06-21 11:23:45 +02001454 case 0x01: /* A-B LNA2 */
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 0x02: /* A-B LNA1 */
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 0x03: /* A-B A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301465 ant_conf->fast_div_bias = 0x1;
1466 ant_conf->main_gaintb = 0;
1467 ant_conf->alt_gaintb = 0;
1468 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001469 case 0x10: /* LNA2 A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301470 if (!(antcomb->scan) &&
1471 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1472 ant_conf->fast_div_bias = 0x1;
1473 else
1474 ant_conf->fast_div_bias = 0x2;
1475 ant_conf->main_gaintb = 0;
1476 ant_conf->alt_gaintb = 0;
1477 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001478 case 0x12: /* LNA2 LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301479 ant_conf->fast_div_bias = 0x1;
1480 ant_conf->main_gaintb = 0;
1481 ant_conf->alt_gaintb = 0;
1482 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001483 case 0x13: /* LNA2 A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301484 if (!(antcomb->scan) &&
1485 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1486 ant_conf->fast_div_bias = 0x1;
1487 else
1488 ant_conf->fast_div_bias = 0x2;
1489 ant_conf->main_gaintb = 0;
1490 ant_conf->alt_gaintb = 0;
1491 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001492 case 0x20: /* LNA1 A-B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301493 if (!(antcomb->scan) &&
1494 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1495 ant_conf->fast_div_bias = 0x1;
1496 else
1497 ant_conf->fast_div_bias = 0x2;
1498 ant_conf->main_gaintb = 0;
1499 ant_conf->alt_gaintb = 0;
1500 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001501 case 0x21: /* LNA1 LNA2 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301502 ant_conf->fast_div_bias = 0x1;
1503 ant_conf->main_gaintb = 0;
1504 ant_conf->alt_gaintb = 0;
1505 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001506 case 0x23: /* LNA1 A+B */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301507 if (!(antcomb->scan) &&
1508 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1509 ant_conf->fast_div_bias = 0x1;
1510 else
1511 ant_conf->fast_div_bias = 0x2;
1512 ant_conf->main_gaintb = 0;
1513 ant_conf->alt_gaintb = 0;
1514 break;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001515 case 0x30: /* A+B A-B */
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 0x31: /* A+B LNA2 */
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;
Gabor Juhos223c5a82011-06-21 11:23:45 +02001525 case 0x32: /* A+B LNA1 */
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301526 ant_conf->fast_div_bias = 0x1;
1527 ant_conf->main_gaintb = 0;
1528 ant_conf->alt_gaintb = 0;
1529 break;
1530 default:
1531 break;
1532 }
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001533 }
1534}
1535
1536/* Antenna diversity and combining */
1537static void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs)
1538{
1539 struct ath_hw_antcomb_conf div_ant_conf;
1540 struct ath_ant_comb *antcomb = &sc->ant_comb;
1541 int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set;
Sujith Manoharan0ff2b5c2011-04-20 11:00:34 +05301542 int curr_main_set;
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001543 int main_rssi = rs->rs_rssi_ctl0;
1544 int alt_rssi = rs->rs_rssi_ctl1;
1545 int rx_ant_conf, main_ant_conf;
1546 bool short_scan = false;
1547
1548 rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) &
1549 ATH_ANT_RX_MASK;
1550 main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) &
1551 ATH_ANT_RX_MASK;
1552
Mohammed Shafi Shajakhan21e8ee62011-05-13 20:31:40 +05301553 /* Record packet only when both main_rssi and alt_rssi is positive */
1554 if (main_rssi > 0 && alt_rssi > 0) {
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001555 antcomb->total_pkt_count++;
1556 antcomb->main_total_rssi += main_rssi;
1557 antcomb->alt_total_rssi += alt_rssi;
1558 if (main_ant_conf == rx_ant_conf)
1559 antcomb->main_recv_cnt++;
1560 else
1561 antcomb->alt_recv_cnt++;
1562 }
1563
1564 /* Short scan check */
1565 if (antcomb->scan && antcomb->alt_good) {
1566 if (time_after(jiffies, antcomb->scan_start_time +
1567 msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
1568 short_scan = true;
1569 else
1570 if (antcomb->total_pkt_count ==
1571 ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
1572 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1573 antcomb->total_pkt_count);
1574 if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
1575 short_scan = true;
1576 }
1577 }
1578
1579 if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
1580 rs->rs_moreaggr) && !short_scan)
1581 return;
1582
1583 if (antcomb->total_pkt_count) {
1584 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1585 antcomb->total_pkt_count);
1586 main_rssi_avg = (antcomb->main_total_rssi /
1587 antcomb->total_pkt_count);
1588 alt_rssi_avg = (antcomb->alt_total_rssi /
1589 antcomb->total_pkt_count);
1590 }
1591
1592
1593 ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf);
1594 curr_alt_set = div_ant_conf.alt_lna_conf;
1595 curr_main_set = div_ant_conf.main_lna_conf;
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001596
1597 antcomb->count++;
1598
1599 if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
1600 if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1601 ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
1602 main_rssi_avg);
1603 antcomb->alt_good = true;
1604 } else {
1605 antcomb->alt_good = false;
1606 }
1607
1608 antcomb->count = 0;
1609 antcomb->scan = true;
1610 antcomb->scan_not_start = true;
1611 }
1612
1613 if (!antcomb->scan) {
Mohammed Shafi Shajakhanb85c5732011-05-13 20:31:09 +05301614 if (ath_ant_div_comb_alt_check(div_ant_conf.div_group,
1615 alt_ratio, curr_main_set, curr_alt_set,
1616 alt_rssi_avg, main_rssi_avg)) {
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001617 if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
1618 /* Switch main and alt LNA */
1619 div_ant_conf.main_lna_conf =
1620 ATH_ANT_DIV_COMB_LNA2;
1621 div_ant_conf.alt_lna_conf =
1622 ATH_ANT_DIV_COMB_LNA1;
1623 } else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
1624 div_ant_conf.main_lna_conf =
1625 ATH_ANT_DIV_COMB_LNA1;
1626 div_ant_conf.alt_lna_conf =
1627 ATH_ANT_DIV_COMB_LNA2;
1628 }
1629
1630 goto div_comb_done;
1631 } else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
1632 (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
1633 /* Set alt to another LNA */
1634 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
1635 div_ant_conf.alt_lna_conf =
1636 ATH_ANT_DIV_COMB_LNA1;
1637 else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
1638 div_ant_conf.alt_lna_conf =
1639 ATH_ANT_DIV_COMB_LNA2;
1640
1641 goto div_comb_done;
1642 }
1643
1644 if ((alt_rssi_avg < (main_rssi_avg +
Mohammed Shafi Shajakhan8afbcc82011-05-13 20:30:56 +05301645 div_ant_conf.lna1_lna2_delta)))
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001646 goto div_comb_done;
1647 }
1648
1649 if (!antcomb->scan_not_start) {
1650 switch (curr_alt_set) {
1651 case ATH_ANT_DIV_COMB_LNA2:
1652 antcomb->rssi_lna2 = alt_rssi_avg;
1653 antcomb->rssi_lna1 = main_rssi_avg;
1654 antcomb->scan = true;
1655 /* set to A+B */
1656 div_ant_conf.main_lna_conf =
1657 ATH_ANT_DIV_COMB_LNA1;
1658 div_ant_conf.alt_lna_conf =
1659 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1660 break;
1661 case ATH_ANT_DIV_COMB_LNA1:
1662 antcomb->rssi_lna1 = alt_rssi_avg;
1663 antcomb->rssi_lna2 = main_rssi_avg;
1664 antcomb->scan = true;
1665 /* set to A+B */
1666 div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1667 div_ant_conf.alt_lna_conf =
1668 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1669 break;
1670 case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
1671 antcomb->rssi_add = alt_rssi_avg;
1672 antcomb->scan = true;
1673 /* set to A-B */
1674 div_ant_conf.alt_lna_conf =
1675 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1676 break;
1677 case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
1678 antcomb->rssi_sub = alt_rssi_avg;
1679 antcomb->scan = false;
1680 if (antcomb->rssi_lna2 >
1681 (antcomb->rssi_lna1 +
1682 ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
1683 /* use LNA2 as main LNA */
1684 if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
1685 (antcomb->rssi_add > antcomb->rssi_sub)) {
1686 /* set to A+B */
1687 div_ant_conf.main_lna_conf =
1688 ATH_ANT_DIV_COMB_LNA2;
1689 div_ant_conf.alt_lna_conf =
1690 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1691 } else if (antcomb->rssi_sub >
1692 antcomb->rssi_lna1) {
1693 /* set to A-B */
1694 div_ant_conf.main_lna_conf =
1695 ATH_ANT_DIV_COMB_LNA2;
1696 div_ant_conf.alt_lna_conf =
1697 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1698 } else {
1699 /* set to LNA1 */
1700 div_ant_conf.main_lna_conf =
1701 ATH_ANT_DIV_COMB_LNA2;
1702 div_ant_conf.alt_lna_conf =
1703 ATH_ANT_DIV_COMB_LNA1;
1704 }
1705 } else {
1706 /* use LNA1 as main LNA */
1707 if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
1708 (antcomb->rssi_add > antcomb->rssi_sub)) {
1709 /* set to A+B */
1710 div_ant_conf.main_lna_conf =
1711 ATH_ANT_DIV_COMB_LNA1;
1712 div_ant_conf.alt_lna_conf =
1713 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1714 } else if (antcomb->rssi_sub >
1715 antcomb->rssi_lna1) {
1716 /* set to A-B */
1717 div_ant_conf.main_lna_conf =
1718 ATH_ANT_DIV_COMB_LNA1;
1719 div_ant_conf.alt_lna_conf =
1720 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1721 } else {
1722 /* set to LNA2 */
1723 div_ant_conf.main_lna_conf =
1724 ATH_ANT_DIV_COMB_LNA1;
1725 div_ant_conf.alt_lna_conf =
1726 ATH_ANT_DIV_COMB_LNA2;
1727 }
1728 }
1729 break;
1730 default:
1731 break;
1732 }
1733 } else {
1734 if (!antcomb->alt_good) {
1735 antcomb->scan_not_start = false;
1736 /* Set alt to another LNA */
1737 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
1738 div_ant_conf.main_lna_conf =
1739 ATH_ANT_DIV_COMB_LNA2;
1740 div_ant_conf.alt_lna_conf =
1741 ATH_ANT_DIV_COMB_LNA1;
1742 } else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
1743 div_ant_conf.main_lna_conf =
1744 ATH_ANT_DIV_COMB_LNA1;
1745 div_ant_conf.alt_lna_conf =
1746 ATH_ANT_DIV_COMB_LNA2;
1747 }
1748 goto div_comb_done;
1749 }
1750 }
1751
1752 ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
1753 main_rssi_avg, alt_rssi_avg,
1754 alt_ratio);
1755
1756 antcomb->quick_scan_cnt++;
1757
1758div_comb_done:
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301759 ath_ant_div_conf_fast_divbias(&div_ant_conf, antcomb, alt_ratio);
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001760 ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf);
1761
1762 antcomb->scan_start_time = jiffies;
1763 antcomb->total_pkt_count = 0;
1764 antcomb->main_total_rssi = 0;
1765 antcomb->alt_total_rssi = 0;
1766 antcomb->main_recv_cnt = 0;
1767 antcomb->alt_recv_cnt = 0;
1768}
1769
Felix Fietkaub5c804752010-04-15 17:38:48 -04001770int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1771{
1772 struct ath_buf *bf;
Felix Fietkau0d955212011-01-26 18:23:27 +01001773 struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001774 struct ieee80211_rx_status *rxs;
Sujithcbe61d82009-02-09 13:27:12 +05301775 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -07001776 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau7545daf2011-01-24 19:23:16 +01001777 struct ieee80211_hw *hw = sc->hw;
Sujithbe0418a2008-11-18 09:05:55 +05301778 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc9b14172009-11-04 16:47:22 -08001779 int retval;
Sujithbe0418a2008-11-18 09:05:55 +05301780 bool decrypt_error = false;
Felix Fietkau29bffa92010-03-29 20:14:23 -07001781 struct ath_rx_status rs;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001782 enum ath9k_rx_qtype qtype;
1783 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1784 int dma_type;
Vasanthakumar Thiagarajan5c6dd922010-05-20 14:34:47 -07001785 u8 rx_status_len = ah->caps.rx_status_len;
Felix Fietkaua6d20552010-06-12 00:33:54 -04001786 u64 tsf = 0;
1787 u32 tsf_lower = 0;
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001788 unsigned long flags;
Sujithbe0418a2008-11-18 09:05:55 +05301789
Felix Fietkaub5c804752010-04-15 17:38:48 -04001790 if (edma)
Felix Fietkaub5c804752010-04-15 17:38:48 -04001791 dma_type = DMA_BIDIRECTIONAL;
Ming Lei56824222010-05-14 21:15:38 +08001792 else
1793 dma_type = DMA_FROM_DEVICE;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001794
1795 qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
Sujithb77f4832008-12-07 21:44:03 +05301796 spin_lock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001797
Felix Fietkaua6d20552010-06-12 00:33:54 -04001798 tsf = ath9k_hw_gettsf64(ah);
1799 tsf_lower = tsf & 0xffffffff;
1800
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001801 do {
1802 /* If handling rx interrupt and flush is in progress => exit */
Sujith98deeea2008-08-11 14:05:46 +05301803 if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001804 break;
1805
Felix Fietkau29bffa92010-03-29 20:14:23 -07001806 memset(&rs, 0, sizeof(rs));
Felix Fietkaub5c804752010-04-15 17:38:48 -04001807 if (edma)
1808 bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1809 else
1810 bf = ath_get_next_rx_buf(sc, &rs);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001811
Felix Fietkaub5c804752010-04-15 17:38:48 -04001812 if (!bf)
1813 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001814
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001815 skb = bf->bf_mpdu;
Sujithbe0418a2008-11-18 09:05:55 +05301816 if (!skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001817 continue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001818
Felix Fietkau0d955212011-01-26 18:23:27 +01001819 /*
1820 * Take frame header from the first fragment and RX status from
1821 * the last one.
1822 */
1823 if (sc->rx.frag)
1824 hdr_skb = sc->rx.frag;
1825 else
1826 hdr_skb = skb;
1827
1828 hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
1829 rxs = IEEE80211_SKB_RXCB(hdr_skb);
Rajkumar Manoharancf3af742011-08-27 16:17:47 +05301830 if (ieee80211_is_beacon(hdr->frame_control) &&
1831 !compare_ether_addr(hdr->addr3, common->curbssid))
1832 rs.is_mybeacon = true;
1833 else
1834 rs.is_mybeacon = false;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001835
Felix Fietkau29bffa92010-03-29 20:14:23 -07001836 ath_debug_stat_rx(sc, &rs);
Sujith1395d3f2010-01-08 10:36:11 +05301837
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301838 /*
Sujithbe0418a2008-11-18 09:05:55 +05301839 * If we're asked to flush receive queue, directly
1840 * chain it back at the queue without processing it.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001841 */
Felix Fietkau34832882011-09-14 21:23:03 +02001842 if (sc->sc_flags & SC_OP_RXFLUSH)
Felix Fietkau0d955212011-01-26 18:23:27 +01001843 goto requeue_drop_frag;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001844
Jan Friedrichc8f3b722010-08-02 23:55:50 +02001845 retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
1846 rxs, &decrypt_error);
1847 if (retval)
Felix Fietkau0d955212011-01-26 18:23:27 +01001848 goto requeue_drop_frag;
Jan Friedrichc8f3b722010-08-02 23:55:50 +02001849
Felix Fietkaua6d20552010-06-12 00:33:54 -04001850 rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1851 if (rs.rs_tstamp > tsf_lower &&
1852 unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1853 rxs->mactime -= 0x100000000ULL;
1854
1855 if (rs.rs_tstamp < tsf_lower &&
1856 unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1857 rxs->mactime += 0x100000000ULL;
1858
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001859 /* Ensure we always have an skb to requeue once we are done
1860 * processing the current buffer's skb */
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001861 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001862
1863 /* If there is no memory we ignore the current RX'd frame,
1864 * tell hardware it can give us a new frame using the old
Sujithb77f4832008-12-07 21:44:03 +05301865 * skb and put it at the tail of the sc->rx.rxbuf list for
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001866 * processing. */
1867 if (!requeue_skb)
Felix Fietkau0d955212011-01-26 18:23:27 +01001868 goto requeue_drop_frag;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001869
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301870 /* Unmap the frame */
Gabor Juhos7da3c552009-01-14 20:17:03 +01001871 dma_unmap_single(sc->dev, bf->bf_buf_addr,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001872 common->rx_bufsize,
Felix Fietkaub5c804752010-04-15 17:38:48 -04001873 dma_type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001874
Felix Fietkaub5c804752010-04-15 17:38:48 -04001875 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1876 if (ah->caps.rx_status_len)
1877 skb_pull(skb, ah->caps.rx_status_len);
Sujithbe0418a2008-11-18 09:05:55 +05301878
Felix Fietkau0d955212011-01-26 18:23:27 +01001879 if (!rs.rs_more)
1880 ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1881 rxs, decrypt_error);
Sujithbe0418a2008-11-18 09:05:55 +05301882
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001883 /* We will now give hardware our shiny new allocated skb */
1884 bf->bf_mpdu = requeue_skb;
Gabor Juhos7da3c552009-01-14 20:17:03 +01001885 bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001886 common->rx_bufsize,
Felix Fietkaub5c804752010-04-15 17:38:48 -04001887 dma_type);
Gabor Juhos7da3c552009-01-14 20:17:03 +01001888 if (unlikely(dma_mapping_error(sc->dev,
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001889 bf->bf_buf_addr))) {
1890 dev_kfree_skb_any(requeue_skb);
1891 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -07001892 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -08001893 ath_err(common, "dma_mapping_error() on RX\n");
Felix Fietkau7545daf2011-01-24 19:23:16 +01001894 ieee80211_rx(hw, skb);
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001895 break;
1896 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001897
Felix Fietkau0d955212011-01-26 18:23:27 +01001898 if (rs.rs_more) {
1899 /*
1900 * rs_more indicates chained descriptors which can be
1901 * used to link buffers together for a sort of
1902 * scatter-gather operation.
1903 */
1904 if (sc->rx.frag) {
1905 /* too many fragments - cannot handle frame */
1906 dev_kfree_skb_any(sc->rx.frag);
1907 dev_kfree_skb_any(skb);
1908 skb = NULL;
1909 }
1910 sc->rx.frag = skb;
1911 goto requeue;
1912 }
1913
1914 if (sc->rx.frag) {
1915 int space = skb->len - skb_tailroom(hdr_skb);
1916
1917 sc->rx.frag = NULL;
1918
1919 if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
1920 dev_kfree_skb(skb);
1921 goto requeue_drop_frag;
1922 }
1923
1924 skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
1925 skb->len);
1926 dev_kfree_skb_any(skb);
1927 skb = hdr_skb;
1928 }
1929
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001930 /*
1931 * change the default rx antenna if rx diversity chooses the
1932 * other antenna 3 times in a row.
1933 */
Felix Fietkau29bffa92010-03-29 20:14:23 -07001934 if (sc->rx.defant != rs.rs_antenna) {
Sujithb77f4832008-12-07 21:44:03 +05301935 if (++sc->rx.rxotherant >= 3)
Felix Fietkau29bffa92010-03-29 20:14:23 -07001936 ath_setdefantenna(sc, rs.rs_antenna);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001937 } else {
Sujithb77f4832008-12-07 21:44:03 +05301938 sc->rx.rxotherant = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001939 }
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301940
Felix Fietkau66760ea2011-07-13 23:35:05 +08001941 if (rxs->flag & RX_FLAG_MMIC_STRIPPED)
1942 skb_trim(skb, skb->len - 8);
1943
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001944 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Mohammed Shafi Shajakhanaaef24b2010-12-07 20:40:58 +05301945
1946 if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -07001947 PS_WAIT_FOR_CAB |
Mohammed Shafi Shajakhanaaef24b2010-12-07 20:40:58 +05301948 PS_WAIT_FOR_PSPOLL_DATA)) ||
Mohammed Shafi Shajakhancedc7e32011-04-22 13:12:23 +05301949 ath9k_check_auto_sleep(sc))
Jouni Malinencc659652009-05-14 21:28:48 +03001950 ath_rx_ps(sc, skb);
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001951 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Jouni Malinencc659652009-05-14 21:28:48 +03001952
Felix Fietkau43c35282011-09-03 01:40:27 +02001953 if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx == 3)
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001954 ath_ant_comb_scan(sc, &rs);
1955
Felix Fietkau7545daf2011-01-24 19:23:16 +01001956 ieee80211_rx(hw, skb);
Jouni Malinencc659652009-05-14 21:28:48 +03001957
Felix Fietkau0d955212011-01-26 18:23:27 +01001958requeue_drop_frag:
1959 if (sc->rx.frag) {
1960 dev_kfree_skb_any(sc->rx.frag);
1961 sc->rx.frag = NULL;
1962 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001963requeue:
Felix Fietkaub5c804752010-04-15 17:38:48 -04001964 if (edma) {
1965 list_add_tail(&bf->list, &sc->rx.rxbuf);
1966 ath_rx_edma_buf_link(sc, qtype);
1967 } else {
1968 list_move_tail(&bf->list, &sc->rx.rxbuf);
1969 ath_rx_buf_link(sc, bf);
Felix Fietkau34832882011-09-14 21:23:03 +02001970 if (!flush)
1971 ath9k_hw_rxena(ah);
Felix Fietkaub5c804752010-04-15 17:38:48 -04001972 }
Sujithbe0418a2008-11-18 09:05:55 +05301973 } while (1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001974
Sujithb77f4832008-12-07 21:44:03 +05301975 spin_unlock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001976
Rajkumar Manoharan29ab0b32011-08-13 10:28:10 +05301977 if (!(ah->imask & ATH9K_INT_RXEOL)) {
1978 ah->imask |= (ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
1979 ath9k_hw_set_interrupts(ah, ah->imask);
1980 }
1981
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001982 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001983}