blob: c4c5d9f455bbca23a0213b6c2795855e13444ef5 [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:
43 if ((((curr_main_set == ATH_ANT_DIV_COMB_LNA2) &&
44 (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) &&
45 (alt_rssi_avg >= (main_rssi_avg - 5))) ||
46 ((curr_main_set == ATH_ANT_DIV_COMB_LNA1) &&
47 (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) &&
48 (alt_rssi_avg >= (main_rssi_avg - 2)))) &&
49 (alt_rssi_avg >= 4))
50 result = true;
51 else
52 result = false;
53 break;
54 }
55
56 return result;
57}
58
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -070059static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
60{
61 return sc->ps_enabled &&
62 (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
63}
64
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070065/*
66 * Setup and link descriptors.
67 *
68 * 11N: we can no longer afford to self link the last descriptor.
69 * MAC acknowledges BA status as long as it copies frames to host
70 * buffer (or rx fifo). This can incorrectly acknowledge packets
71 * to a sender if last desc is self-linked.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070072 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070073static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
74{
Sujithcbe61d82009-02-09 13:27:12 +053075 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080076 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070077 struct ath_desc *ds;
78 struct sk_buff *skb;
79
80 ATH_RXBUF_RESET(bf);
81
82 ds = bf->bf_desc;
Sujithbe0418a2008-11-18 09:05:55 +053083 ds->ds_link = 0; /* link to null */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070084 ds->ds_data = bf->bf_buf_addr;
85
Sujithbe0418a2008-11-18 09:05:55 +053086 /* virtual addr of the beginning of the buffer. */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070087 skb = bf->bf_mpdu;
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -070088 BUG_ON(skb == NULL);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070089 ds->ds_vdata = skb->data;
90
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080091 /*
92 * setup rx descriptors. The rx_bufsize here tells the hardware
Luis R. Rodriguezb4b6cda2008-11-20 17:15:13 -080093 * how much data it can DMA to us and that we are prepared
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080094 * to process
95 */
Sujithb77f4832008-12-07 21:44:03 +053096 ath9k_hw_setuprxdesc(ah, ds,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080097 common->rx_bufsize,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070098 0);
99
Sujithb77f4832008-12-07 21:44:03 +0530100 if (sc->rx.rxlink == NULL)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700101 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
102 else
Sujithb77f4832008-12-07 21:44:03 +0530103 *sc->rx.rxlink = bf->bf_daddr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700104
Sujithb77f4832008-12-07 21:44:03 +0530105 sc->rx.rxlink = &ds->ds_link;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700106}
107
Sujithff37e332008-11-24 12:07:55 +0530108static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
109{
110 /* XXX block beacon interrupts */
111 ath9k_hw_setantenna(sc->sc_ah, antenna);
Sujithb77f4832008-12-07 21:44:03 +0530112 sc->rx.defant = antenna;
113 sc->rx.rxotherant = 0;
Sujithff37e332008-11-24 12:07:55 +0530114}
115
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700116static void ath_opmode_init(struct ath_softc *sc)
117{
Sujithcbe61d82009-02-09 13:27:12 +0530118 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700119 struct ath_common *common = ath9k_hw_common(ah);
120
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700121 u32 rfilt, mfilt[2];
122
123 /* configure rx filter */
124 rfilt = ath_calcrxfilter(sc);
125 ath9k_hw_setrxfilter(ah, rfilt);
126
127 /* configure bssid mask */
Felix Fietkau364734f2010-09-14 20:22:44 +0200128 ath_hw_setbssidmask(common);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700129
130 /* configure operational mode */
131 ath9k_hw_setopmode(ah);
132
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700133 /* calculate and install multicast filter */
134 mfilt[0] = mfilt[1] = ~0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700135 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700136}
137
Felix Fietkaub5c804752010-04-15 17:38:48 -0400138static bool ath_rx_edma_buf_link(struct ath_softc *sc,
139 enum ath9k_rx_qtype qtype)
140{
141 struct ath_hw *ah = sc->sc_ah;
142 struct ath_rx_edma *rx_edma;
143 struct sk_buff *skb;
144 struct ath_buf *bf;
145
146 rx_edma = &sc->rx.rx_edma[qtype];
147 if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
148 return false;
149
150 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
151 list_del_init(&bf->list);
152
153 skb = bf->bf_mpdu;
154
155 ATH_RXBUF_RESET(bf);
156 memset(skb->data, 0, ah->caps.rx_status_len);
157 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
158 ah->caps.rx_status_len, DMA_TO_DEVICE);
159
160 SKB_CB_ATHBUF(skb) = bf;
161 ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
162 skb_queue_tail(&rx_edma->rx_fifo, skb);
163
164 return true;
165}
166
167static void ath_rx_addbuffer_edma(struct ath_softc *sc,
168 enum ath9k_rx_qtype qtype, int size)
169{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400170 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
171 u32 nbuf = 0;
172
Felix Fietkaub5c804752010-04-15 17:38:48 -0400173 if (list_empty(&sc->rx.rxbuf)) {
Joe Perches226afe62010-12-02 19:12:37 -0800174 ath_dbg(common, ATH_DBG_QUEUE, "No free rx buf available\n");
Felix Fietkaub5c804752010-04-15 17:38:48 -0400175 return;
176 }
177
178 while (!list_empty(&sc->rx.rxbuf)) {
179 nbuf++;
180
181 if (!ath_rx_edma_buf_link(sc, qtype))
182 break;
183
184 if (nbuf >= size)
185 break;
186 }
187}
188
189static void ath_rx_remove_buffer(struct ath_softc *sc,
190 enum ath9k_rx_qtype qtype)
191{
192 struct ath_buf *bf;
193 struct ath_rx_edma *rx_edma;
194 struct sk_buff *skb;
195
196 rx_edma = &sc->rx.rx_edma[qtype];
197
198 while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
199 bf = SKB_CB_ATHBUF(skb);
200 BUG_ON(!bf);
201 list_add_tail(&bf->list, &sc->rx.rxbuf);
202 }
203}
204
205static void ath_rx_edma_cleanup(struct ath_softc *sc)
206{
207 struct ath_buf *bf;
208
209 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
210 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
211
212 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
213 if (bf->bf_mpdu)
214 dev_kfree_skb_any(bf->bf_mpdu);
215 }
216
217 INIT_LIST_HEAD(&sc->rx.rxbuf);
218
219 kfree(sc->rx.rx_bufptr);
220 sc->rx.rx_bufptr = NULL;
221}
222
223static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
224{
225 skb_queue_head_init(&rx_edma->rx_fifo);
226 skb_queue_head_init(&rx_edma->rx_buffers);
227 rx_edma->rx_fifo_hwsize = size;
228}
229
230static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
231{
232 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
233 struct ath_hw *ah = sc->sc_ah;
234 struct sk_buff *skb;
235 struct ath_buf *bf;
236 int error = 0, i;
237 u32 size;
238
Felix Fietkaub5c804752010-04-15 17:38:48 -0400239 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
240 ah->caps.rx_status_len);
241
242 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
243 ah->caps.rx_lp_qdepth);
244 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
245 ah->caps.rx_hp_qdepth);
246
247 size = sizeof(struct ath_buf) * nbufs;
248 bf = kzalloc(size, GFP_KERNEL);
249 if (!bf)
250 return -ENOMEM;
251
252 INIT_LIST_HEAD(&sc->rx.rxbuf);
253 sc->rx.rx_bufptr = bf;
254
255 for (i = 0; i < nbufs; i++, bf++) {
256 skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
257 if (!skb) {
258 error = -ENOMEM;
259 goto rx_init_fail;
260 }
261
262 memset(skb->data, 0, common->rx_bufsize);
263 bf->bf_mpdu = skb;
264
265 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
266 common->rx_bufsize,
267 DMA_BIDIRECTIONAL);
268 if (unlikely(dma_mapping_error(sc->dev,
269 bf->bf_buf_addr))) {
270 dev_kfree_skb_any(skb);
271 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -0700272 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -0800273 ath_err(common,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400274 "dma_mapping_error() on RX init\n");
275 error = -ENOMEM;
276 goto rx_init_fail;
277 }
278
279 list_add_tail(&bf->list, &sc->rx.rxbuf);
280 }
281
282 return 0;
283
284rx_init_fail:
285 ath_rx_edma_cleanup(sc);
286 return error;
287}
288
289static void ath_edma_start_recv(struct ath_softc *sc)
290{
291 spin_lock_bh(&sc->rx.rxbuflock);
292
293 ath9k_hw_rxena(sc->sc_ah);
294
295 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
296 sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
297
298 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
299 sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
300
Felix Fietkaub5c804752010-04-15 17:38:48 -0400301 ath_opmode_init(sc);
302
Luis R. Rodriguez48a6a462010-09-16 15:12:28 -0400303 ath9k_hw_startpcureceive(sc->sc_ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
Luis R. Rodriguez7583c5502010-10-20 16:07:04 -0700304
305 spin_unlock_bh(&sc->rx.rxbuflock);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400306}
307
308static void ath_edma_stop_recv(struct ath_softc *sc)
309{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400310 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
311 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400312}
313
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700314int ath_rx_init(struct ath_softc *sc, int nbufs)
315{
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -0700316 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700317 struct sk_buff *skb;
318 struct ath_buf *bf;
319 int error = 0;
320
Luis R. Rodriguez4bdd1e92010-10-26 15:27:24 -0700321 spin_lock_init(&sc->sc_pcu_lock);
Sujith797fe5cb2009-03-30 15:28:45 +0530322 sc->sc_flags &= ~SC_OP_RXFLUSH;
323 spin_lock_init(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700324
Felix Fietkau0d955212011-01-26 18:23:27 +0100325 common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
326 sc->sc_ah->caps.rx_status_len;
327
Felix Fietkaub5c804752010-04-15 17:38:48 -0400328 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
329 return ath_rx_edma_init(sc, nbufs);
330 } else {
Joe Perches226afe62010-12-02 19:12:37 -0800331 ath_dbg(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
332 common->cachelsz, common->rx_bufsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700333
Felix Fietkaub5c804752010-04-15 17:38:48 -0400334 /* Initialize rx descriptors */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700335
Felix Fietkaub5c804752010-04-15 17:38:48 -0400336 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
Vasanthakumar Thiagarajan4adfcde2010-04-15 17:39:33 -0400337 "rx", nbufs, 1, 0);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400338 if (error != 0) {
Joe Perches38002762010-12-02 19:12:36 -0800339 ath_err(common,
340 "failed to allocate rx descriptors: %d\n",
341 error);
Sujith797fe5cb2009-03-30 15:28:45 +0530342 goto err;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700343 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400344
345 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
346 skb = ath_rxbuf_alloc(common, common->rx_bufsize,
347 GFP_KERNEL);
348 if (skb == NULL) {
349 error = -ENOMEM;
350 goto err;
351 }
352
353 bf->bf_mpdu = skb;
354 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
355 common->rx_bufsize,
356 DMA_FROM_DEVICE);
357 if (unlikely(dma_mapping_error(sc->dev,
358 bf->bf_buf_addr))) {
359 dev_kfree_skb_any(skb);
360 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -0700361 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -0800362 ath_err(common,
363 "dma_mapping_error() on RX init\n");
Felix Fietkaub5c804752010-04-15 17:38:48 -0400364 error = -ENOMEM;
365 goto err;
366 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400367 }
368 sc->rx.rxlink = NULL;
Sujith797fe5cb2009-03-30 15:28:45 +0530369 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700370
Sujith797fe5cb2009-03-30 15:28:45 +0530371err:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700372 if (error)
373 ath_rx_cleanup(sc);
374
375 return error;
376}
377
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700378void ath_rx_cleanup(struct ath_softc *sc)
379{
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800380 struct ath_hw *ah = sc->sc_ah;
381 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700382 struct sk_buff *skb;
383 struct ath_buf *bf;
384
Felix Fietkaub5c804752010-04-15 17:38:48 -0400385 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
386 ath_rx_edma_cleanup(sc);
387 return;
388 } else {
389 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
390 skb = bf->bf_mpdu;
391 if (skb) {
392 dma_unmap_single(sc->dev, bf->bf_buf_addr,
393 common->rx_bufsize,
394 DMA_FROM_DEVICE);
395 dev_kfree_skb(skb);
Ben Greear6cf9e992010-10-14 12:45:30 -0700396 bf->bf_buf_addr = 0;
397 bf->bf_mpdu = NULL;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400398 }
Luis R. Rodriguez051b9192009-03-23 18:25:01 -0400399 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700400
Felix Fietkaub5c804752010-04-15 17:38:48 -0400401 if (sc->rx.rxdma.dd_desc_len != 0)
402 ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
403 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700404}
405
406/*
407 * Calculate the receive filter according to the
408 * operating mode and state:
409 *
410 * o always accept unicast, broadcast, and multicast traffic
411 * o maintain current state of phy error reception (the hal
412 * may enable phy error frames for noise immunity work)
413 * o probe request frames are accepted only when operating in
414 * hostap, adhoc, or monitor modes
415 * o enable promiscuous mode according to the interface state
416 * o accept beacons:
417 * - when operating in adhoc mode so the 802.11 layer creates
418 * node table entries for peers,
419 * - when operating in station mode for collecting rssi data when
420 * the station is otherwise quiet, or
421 * - when operating as a repeater so we see repeater-sta beacons
422 * - when scanning
423 */
424
425u32 ath_calcrxfilter(struct ath_softc *sc)
426{
427#define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
Sujith7dcfdcd2008-08-11 14:03:13 +0530428
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700429 u32 rfilt;
430
431 rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
432 | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
433 | ATH9K_RX_FILTER_MCAST;
434
Jouni Malinen9c1d8e42010-10-13 17:29:31 +0300435 if (sc->rx.rxfilter & FIF_PROBE_REQ)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700436 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
437
Jouni Malinen217ba9d2009-03-10 10:55:50 +0200438 /*
439 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
440 * mode interface or when in monitor mode. AP mode does not need this
441 * since it receives all in-BSS frames anyway.
442 */
Felix Fietkau2e286942011-03-09 01:48:12 +0100443 if (sc->sc_ah->is_monitoring)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700444 rfilt |= ATH9K_RX_FILTER_PROM;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700445
Sujithd42c6b72009-02-04 08:10:22 +0530446 if (sc->rx.rxfilter & FIF_CONTROL)
447 rfilt |= ATH9K_RX_FILTER_CONTROL;
448
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530449 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
Ben Greearcfda6692010-09-14 12:00:22 -0700450 (sc->nvifs <= 1) &&
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530451 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
452 rfilt |= ATH9K_RX_FILTER_MYBEACON;
453 else
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700454 rfilt |= ATH9K_RX_FILTER_BEACON;
455
Felix Fietkau264bbec2011-04-07 19:24:23 +0200456 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
Senthil Balasubramanian66afad02009-09-18 15:06:07 +0530457 (sc->rx.rxfilter & FIF_PSPOLL))
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530458 rfilt |= ATH9K_RX_FILTER_PSPOLL;
Sujithbe0418a2008-11-18 09:05:55 +0530459
Sujith7ea310b2009-09-03 12:08:43 +0530460 if (conf_is_ht(&sc->hw->conf))
461 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
462
Felix Fietkau7545daf2011-01-24 19:23:16 +0100463 if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
Javier Cardona5eb6ba82009-08-20 19:12:07 -0700464 /* The following may also be needed for other older chips */
465 if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
466 rfilt |= ATH9K_RX_FILTER_PROM;
Jouni Malinenb93bce22009-03-03 19:23:30 +0200467 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
468 }
469
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700470 return rfilt;
Sujith7dcfdcd2008-08-11 14:03:13 +0530471
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700472#undef RX_FILTER_PRESERVE
473}
474
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700475int ath_startrecv(struct ath_softc *sc)
476{
Sujithcbe61d82009-02-09 13:27:12 +0530477 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700478 struct ath_buf *bf, *tbf;
479
Felix Fietkaub5c804752010-04-15 17:38:48 -0400480 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
481 ath_edma_start_recv(sc);
482 return 0;
483 }
484
Sujithb77f4832008-12-07 21:44:03 +0530485 spin_lock_bh(&sc->rx.rxbuflock);
486 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700487 goto start_recv;
488
Sujithb77f4832008-12-07 21:44:03 +0530489 sc->rx.rxlink = NULL;
490 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700491 ath_rx_buf_link(sc, bf);
492 }
493
494 /* We could have deleted elements so the list may be empty now */
Sujithb77f4832008-12-07 21:44:03 +0530495 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700496 goto start_recv;
497
Sujithb77f4832008-12-07 21:44:03 +0530498 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700499 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
Sujithbe0418a2008-11-18 09:05:55 +0530500 ath9k_hw_rxena(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700501
502start_recv:
Sujithbe0418a2008-11-18 09:05:55 +0530503 ath_opmode_init(sc);
Luis R. Rodriguez48a6a462010-09-16 15:12:28 -0400504 ath9k_hw_startpcureceive(ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
Sujithbe0418a2008-11-18 09:05:55 +0530505
Luis R. Rodriguez7583c5502010-10-20 16:07:04 -0700506 spin_unlock_bh(&sc->rx.rxbuflock);
507
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700508 return 0;
509}
510
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700511bool ath_stoprecv(struct ath_softc *sc)
512{
Sujithcbe61d82009-02-09 13:27:12 +0530513 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau5882da022011-04-08 20:13:18 +0200514 bool stopped, reset = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700515
Luis R. Rodriguez1e450282010-10-20 16:07:03 -0700516 spin_lock_bh(&sc->rx.rxbuflock);
Felix Fietkaud47844a2010-11-20 03:08:47 +0100517 ath9k_hw_abortpcurecv(ah);
Sujithbe0418a2008-11-18 09:05:55 +0530518 ath9k_hw_setrxfilter(ah, 0);
Felix Fietkau5882da022011-04-08 20:13:18 +0200519 stopped = ath9k_hw_stopdmarecv(ah, &reset);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400520
521 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
522 ath_edma_stop_recv(sc);
523 else
524 sc->rx.rxlink = NULL;
Luis R. Rodriguez1e450282010-10-20 16:07:03 -0700525 spin_unlock_bh(&sc->rx.rxbuflock);
Sujithbe0418a2008-11-18 09:05:55 +0530526
Rajkumar Manoharand5847472010-12-20 14:39:51 +0530527 if (!(ah->ah_flags & AH_UNPLUGGED) &&
528 unlikely(!stopped)) {
Ben Greeard7fd1b502010-12-06 13:13:07 -0800529 ath_err(ath9k_hw_common(sc->sc_ah),
530 "Could not stop RX, we could be "
531 "confusing the DMA engine when we start RX up\n");
532 ATH_DBG_WARN_ON_ONCE(!stopped);
533 }
Felix Fietkau2232d312011-04-15 00:41:43 +0200534 return stopped && !reset;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700535}
536
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700537void ath_flushrecv(struct ath_softc *sc)
538{
Sujith98deeea2008-08-11 14:05:46 +0530539 sc->sc_flags |= SC_OP_RXFLUSH;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400540 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
541 ath_rx_tasklet(sc, 1, true);
542 ath_rx_tasklet(sc, 1, false);
Sujith98deeea2008-08-11 14:05:46 +0530543 sc->sc_flags &= ~SC_OP_RXFLUSH;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700544}
545
Jouni Malinencc659652009-05-14 21:28:48 +0300546static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
547{
548 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
549 struct ieee80211_mgmt *mgmt;
550 u8 *pos, *end, id, elen;
551 struct ieee80211_tim_ie *tim;
552
553 mgmt = (struct ieee80211_mgmt *)skb->data;
554 pos = mgmt->u.beacon.variable;
555 end = skb->data + skb->len;
556
557 while (pos + 2 < end) {
558 id = *pos++;
559 elen = *pos++;
560 if (pos + elen > end)
561 break;
562
563 if (id == WLAN_EID_TIM) {
564 if (elen < sizeof(*tim))
565 break;
566 tim = (struct ieee80211_tim_ie *) pos;
567 if (tim->dtim_count != 0)
568 break;
569 return tim->bitmap_ctrl & 0x01;
570 }
571
572 pos += elen;
573 }
574
575 return false;
576}
577
Jouni Malinencc659652009-05-14 21:28:48 +0300578static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
579{
580 struct ieee80211_mgmt *mgmt;
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700581 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300582
583 if (skb->len < 24 + 8 + 2 + 2)
584 return;
585
586 mgmt = (struct ieee80211_mgmt *)skb->data;
Ben Greear48014162011-01-15 19:13:48 +0000587 if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0) {
588 /* TODO: This doesn't work well if you have stations
589 * associated to two different APs because curbssid
590 * is just the last AP that any of the stations associated
591 * with.
592 */
Jouni Malinencc659652009-05-14 21:28:48 +0300593 return; /* not from our current AP */
Ben Greear48014162011-01-15 19:13:48 +0000594 }
Jouni Malinencc659652009-05-14 21:28:48 +0300595
Sujith1b04b932010-01-08 10:36:05 +0530596 sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
Gabor Juhos293dc5d2009-06-19 12:17:48 +0200597
Sujith1b04b932010-01-08 10:36:05 +0530598 if (sc->ps_flags & PS_BEACON_SYNC) {
599 sc->ps_flags &= ~PS_BEACON_SYNC;
Joe Perches226afe62010-12-02 19:12:37 -0800600 ath_dbg(common, ATH_DBG_PS,
601 "Reconfigure Beacon timers based on timestamp from the AP\n");
Rajkumar Manoharan99e4d432011-04-04 22:56:19 +0530602 ath_set_beacon(sc);
Rajkumar Manoharandeb75182011-05-06 18:27:46 +0530603 sc->ps_flags &= ~PS_TSFOOR_SYNC;
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 */
764 ret = ath9k_hw_rxprocdesc(ah, ds, rs, 0);
765 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;
790 ret = ath9k_hw_rxprocdesc(ah, tds, &trs, 0);
791 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{
Senthil Balasubramanian38852b22010-12-06 19:09:27 +0530817#define is_mc_or_valid_tkip_keyix ((is_mc || \
818 (rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID && \
819 test_bit(rx_stats->rs_keyix, common->tkip_keymap))))
820
Sujithd4357002010-05-20 15:34:38 +0530821 struct ath_hw *ah = common->ah;
Sujithd4357002010-05-20 15:34:38 +0530822 __le16 fc;
Vasanthakumar Thiagarajanb7b1b512010-05-20 14:34:48 -0700823 u8 rx_status_len = ah->caps.rx_status_len;
Sujithd4357002010-05-20 15:34:38 +0530824
Sujithd4357002010-05-20 15:34:38 +0530825 fc = hdr->frame_control;
826
827 if (!rx_stats->rs_datalen)
828 return false;
829 /*
830 * rs_status follows rs_datalen so if rs_datalen is too large
831 * we can take a hint that hardware corrupted it, so ignore
832 * those frames.
833 */
Vasanthakumar Thiagarajanb7b1b512010-05-20 14:34:48 -0700834 if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len))
Sujithd4357002010-05-20 15:34:38 +0530835 return false;
836
Felix Fietkau0d955212011-01-26 18:23:27 +0100837 /* Only use error bits from the last fragment */
Sujithd4357002010-05-20 15:34:38 +0530838 if (rx_stats->rs_more)
Felix Fietkau0d955212011-01-26 18:23:27 +0100839 return true;
Sujithd4357002010-05-20 15:34:38 +0530840
841 /*
842 * The rx_stats->rs_status will not be set until the end of the
843 * chained descriptors so it can be ignored if rs_more is set. The
844 * rs_more will be false at the last element of the chained
845 * descriptors.
846 */
847 if (rx_stats->rs_status != 0) {
848 if (rx_stats->rs_status & ATH9K_RXERR_CRC)
849 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
850 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
851 return false;
852
853 if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
854 *decrypt_error = true;
855 } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
Senthil Balasubramanian38852b22010-12-06 19:09:27 +0530856 bool is_mc;
Felix Fietkau56363dd2010-08-28 18:21:21 +0200857 /*
858 * The MIC error bit is only valid if the frame
859 * is not a control frame or fragment, and it was
860 * decrypted using a valid TKIP key.
861 */
Senthil Balasubramanian38852b22010-12-06 19:09:27 +0530862 is_mc = !!is_multicast_ether_addr(hdr->addr1);
863
Felix Fietkau56363dd2010-08-28 18:21:21 +0200864 if (!ieee80211_is_ctl(fc) &&
865 !ieee80211_has_morefrags(fc) &&
866 !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
Senthil Balasubramanian38852b22010-12-06 19:09:27 +0530867 is_mc_or_valid_tkip_keyix)
Sujithd4357002010-05-20 15:34:38 +0530868 rxs->flag |= RX_FLAG_MMIC_ERROR;
Felix Fietkau56363dd2010-08-28 18:21:21 +0200869 else
870 rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
Sujithd4357002010-05-20 15:34:38 +0530871 }
872 /*
873 * Reject error frames with the exception of
874 * decryption and MIC failures. For monitor mode,
875 * we also ignore the CRC error.
876 */
Rajkumar Manoharan5f841b42010-10-27 18:31:15 +0530877 if (ah->is_monitoring) {
Sujithd4357002010-05-20 15:34:38 +0530878 if (rx_stats->rs_status &
879 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
880 ATH9K_RXERR_CRC))
881 return false;
882 } else {
883 if (rx_stats->rs_status &
884 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
885 return false;
886 }
887 }
888 }
889 return true;
890}
891
892static int ath9k_process_rate(struct ath_common *common,
893 struct ieee80211_hw *hw,
894 struct ath_rx_status *rx_stats,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700895 struct ieee80211_rx_status *rxs)
Sujithd4357002010-05-20 15:34:38 +0530896{
897 struct ieee80211_supported_band *sband;
898 enum ieee80211_band band;
899 unsigned int i = 0;
900
901 band = hw->conf.channel->band;
902 sband = hw->wiphy->bands[band];
903
904 if (rx_stats->rs_rate & 0x80) {
905 /* HT rate */
906 rxs->flag |= RX_FLAG_HT;
907 if (rx_stats->rs_flags & ATH9K_RX_2040)
908 rxs->flag |= RX_FLAG_40MHZ;
909 if (rx_stats->rs_flags & ATH9K_RX_GI)
910 rxs->flag |= RX_FLAG_SHORT_GI;
911 rxs->rate_idx = rx_stats->rs_rate & 0x7f;
912 return 0;
913 }
914
915 for (i = 0; i < sband->n_bitrates; i++) {
916 if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
917 rxs->rate_idx = i;
918 return 0;
919 }
920 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
921 rxs->flag |= RX_FLAG_SHORTPRE;
922 rxs->rate_idx = i;
923 return 0;
924 }
925 }
926
927 /*
928 * No valid hardware bitrate found -- we should not get here
929 * because hardware has already validated this frame as OK.
930 */
Joe Perches226afe62010-12-02 19:12:37 -0800931 ath_dbg(common, ATH_DBG_XMIT,
932 "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
933 rx_stats->rs_rate);
Sujithd4357002010-05-20 15:34:38 +0530934
935 return -EINVAL;
936}
937
938static void ath9k_process_rssi(struct ath_common *common,
939 struct ieee80211_hw *hw,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700940 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530941 struct ath_rx_status *rx_stats)
942{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100943 struct ath_softc *sc = hw->priv;
Sujithd4357002010-05-20 15:34:38 +0530944 struct ath_hw *ah = common->ah;
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200945 int last_rssi;
Sujithd4357002010-05-20 15:34:38 +0530946 __le16 fc;
947
Rajkumar Manoharan2b892a92011-05-09 19:11:28 +0530948 if ((ah->opmode != NL80211_IFTYPE_STATION) &&
949 (ah->opmode != NL80211_IFTYPE_ADHOC))
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200950 return;
951
Sujithd4357002010-05-20 15:34:38 +0530952 fc = hdr->frame_control;
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200953 if (!ieee80211_is_beacon(fc) ||
Ben Greear48014162011-01-15 19:13:48 +0000954 compare_ether_addr(hdr->addr3, common->curbssid)) {
955 /* TODO: This doesn't work well if you have stations
956 * associated to two different APs because curbssid
957 * is just the last AP that any of the stations associated
958 * with.
959 */
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200960 return;
Ben Greear48014162011-01-15 19:13:48 +0000961 }
Sujithd4357002010-05-20 15:34:38 +0530962
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200963 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
Felix Fietkau9ac586152011-01-24 19:23:18 +0100964 ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
Ben Greear686b9cb2010-09-23 09:44:36 -0700965
Felix Fietkau9ac586152011-01-24 19:23:18 +0100966 last_rssi = sc->last_rssi;
Sujithd4357002010-05-20 15:34:38 +0530967 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
968 rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
969 ATH_RSSI_EP_MULTIPLIER);
970 if (rx_stats->rs_rssi < 0)
971 rx_stats->rs_rssi = 0;
972
973 /* Update Beacon RSSI, this is used by ANI. */
Felix Fietkau9fa23e12010-10-15 20:03:31 +0200974 ah->stats.avgbrssi = rx_stats->rs_rssi;
Sujithd4357002010-05-20 15:34:38 +0530975}
976
977/*
978 * For Decrypt or Demic errors, we only mark packet status here and always push
979 * up the frame up to let mac80211 handle the actual error case, be it no
980 * decryption key or real decryption error. This let us keep statistics there.
981 */
982static int ath9k_rx_skb_preprocess(struct ath_common *common,
983 struct ieee80211_hw *hw,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700984 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530985 struct ath_rx_status *rx_stats,
986 struct ieee80211_rx_status *rx_status,
987 bool *decrypt_error)
988{
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;
1009 rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
1010 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) {
1079 case (0x10): /* LNA2 A-B */
1080 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;
1085 case (0x20): /* LNA1 A-B */
1086 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;
1091 case (0x21): /* LNA1 LNA2 */
1092 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;
1098 case (0x12): /* LNA2 LNA1 */
1099 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;
1105 case (0x13): /* LNA2 A+B */
1106 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;
1111 case (0x23): /* LNA1 A+B */
1112 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) {
1328 case (0x01): /* A-B LNA2 */
1329 ant_conf->fast_div_bias = 0x3b;
1330 break;
1331 case (0x02): /* A-B LNA1 */
1332 ant_conf->fast_div_bias = 0x3d;
1333 break;
1334 case (0x03): /* A-B A+B */
1335 ant_conf->fast_div_bias = 0x1;
1336 break;
1337 case (0x10): /* LNA2 A-B */
1338 ant_conf->fast_div_bias = 0x7;
1339 break;
1340 case (0x12): /* LNA2 LNA1 */
1341 ant_conf->fast_div_bias = 0x2;
1342 break;
1343 case (0x13): /* LNA2 A+B */
1344 ant_conf->fast_div_bias = 0x7;
1345 break;
1346 case (0x20): /* LNA1 A-B */
1347 ant_conf->fast_div_bias = 0x6;
1348 break;
1349 case (0x21): /* LNA1 LNA2 */
1350 ant_conf->fast_div_bias = 0x0;
1351 break;
1352 case (0x23): /* LNA1 A+B */
1353 ant_conf->fast_div_bias = 0x6;
1354 break;
1355 case (0x30): /* A+B A-B */
1356 ant_conf->fast_div_bias = 0x1;
1357 break;
1358 case (0x31): /* A+B LNA2 */
1359 ant_conf->fast_div_bias = 0x3b;
1360 break;
1361 case (0x32): /* A+B LNA1 */
1362 ant_conf->fast_div_bias = 0x3d;
1363 break;
1364 default:
1365 break;
1366 }
1367 } else if (ant_conf->div_group == 2) {
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 = 0x1;
1390 else
1391 ant_conf->fast_div_bias = 0x2;
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 = 0x1;
1404 else
1405 ant_conf->fast_div_bias = 0x2;
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 = 0x1;
1413 else
1414 ant_conf->fast_div_bias = 0x2;
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 = 0x1;
1427 else
1428 ant_conf->fast_div_bias = 0x2;
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 }
1450
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001451 }
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301452
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001453}
1454
1455/* Antenna diversity and combining */
1456static void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs)
1457{
1458 struct ath_hw_antcomb_conf div_ant_conf;
1459 struct ath_ant_comb *antcomb = &sc->ant_comb;
1460 int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set;
Sujith Manoharan0ff2b5c2011-04-20 11:00:34 +05301461 int curr_main_set;
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001462 int main_rssi = rs->rs_rssi_ctl0;
1463 int alt_rssi = rs->rs_rssi_ctl1;
1464 int rx_ant_conf, main_ant_conf;
1465 bool short_scan = false;
1466
1467 rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) &
1468 ATH_ANT_RX_MASK;
1469 main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) &
1470 ATH_ANT_RX_MASK;
1471
Mohammed Shafi Shajakhan21e8ee62011-05-13 20:31:40 +05301472 /* Record packet only when both main_rssi and alt_rssi is positive */
1473 if (main_rssi > 0 && alt_rssi > 0) {
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001474 antcomb->total_pkt_count++;
1475 antcomb->main_total_rssi += main_rssi;
1476 antcomb->alt_total_rssi += alt_rssi;
1477 if (main_ant_conf == rx_ant_conf)
1478 antcomb->main_recv_cnt++;
1479 else
1480 antcomb->alt_recv_cnt++;
1481 }
1482
1483 /* Short scan check */
1484 if (antcomb->scan && antcomb->alt_good) {
1485 if (time_after(jiffies, antcomb->scan_start_time +
1486 msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
1487 short_scan = true;
1488 else
1489 if (antcomb->total_pkt_count ==
1490 ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
1491 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1492 antcomb->total_pkt_count);
1493 if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
1494 short_scan = true;
1495 }
1496 }
1497
1498 if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
1499 rs->rs_moreaggr) && !short_scan)
1500 return;
1501
1502 if (antcomb->total_pkt_count) {
1503 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1504 antcomb->total_pkt_count);
1505 main_rssi_avg = (antcomb->main_total_rssi /
1506 antcomb->total_pkt_count);
1507 alt_rssi_avg = (antcomb->alt_total_rssi /
1508 antcomb->total_pkt_count);
1509 }
1510
1511
1512 ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf);
1513 curr_alt_set = div_ant_conf.alt_lna_conf;
1514 curr_main_set = div_ant_conf.main_lna_conf;
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001515
1516 antcomb->count++;
1517
1518 if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
1519 if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1520 ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
1521 main_rssi_avg);
1522 antcomb->alt_good = true;
1523 } else {
1524 antcomb->alt_good = false;
1525 }
1526
1527 antcomb->count = 0;
1528 antcomb->scan = true;
1529 antcomb->scan_not_start = true;
1530 }
1531
1532 if (!antcomb->scan) {
Mohammed Shafi Shajakhanb85c5732011-05-13 20:31:09 +05301533 if (ath_ant_div_comb_alt_check(div_ant_conf.div_group,
1534 alt_ratio, curr_main_set, curr_alt_set,
1535 alt_rssi_avg, main_rssi_avg)) {
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001536 if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
1537 /* Switch main and alt LNA */
1538 div_ant_conf.main_lna_conf =
1539 ATH_ANT_DIV_COMB_LNA2;
1540 div_ant_conf.alt_lna_conf =
1541 ATH_ANT_DIV_COMB_LNA1;
1542 } else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
1543 div_ant_conf.main_lna_conf =
1544 ATH_ANT_DIV_COMB_LNA1;
1545 div_ant_conf.alt_lna_conf =
1546 ATH_ANT_DIV_COMB_LNA2;
1547 }
1548
1549 goto div_comb_done;
1550 } else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
1551 (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
1552 /* Set alt to another LNA */
1553 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
1554 div_ant_conf.alt_lna_conf =
1555 ATH_ANT_DIV_COMB_LNA1;
1556 else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
1557 div_ant_conf.alt_lna_conf =
1558 ATH_ANT_DIV_COMB_LNA2;
1559
1560 goto div_comb_done;
1561 }
1562
1563 if ((alt_rssi_avg < (main_rssi_avg +
Mohammed Shafi Shajakhan8afbcc82011-05-13 20:30:56 +05301564 div_ant_conf.lna1_lna2_delta)))
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001565 goto div_comb_done;
1566 }
1567
1568 if (!antcomb->scan_not_start) {
1569 switch (curr_alt_set) {
1570 case ATH_ANT_DIV_COMB_LNA2:
1571 antcomb->rssi_lna2 = alt_rssi_avg;
1572 antcomb->rssi_lna1 = main_rssi_avg;
1573 antcomb->scan = true;
1574 /* set to A+B */
1575 div_ant_conf.main_lna_conf =
1576 ATH_ANT_DIV_COMB_LNA1;
1577 div_ant_conf.alt_lna_conf =
1578 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1579 break;
1580 case ATH_ANT_DIV_COMB_LNA1:
1581 antcomb->rssi_lna1 = alt_rssi_avg;
1582 antcomb->rssi_lna2 = main_rssi_avg;
1583 antcomb->scan = true;
1584 /* set to A+B */
1585 div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1586 div_ant_conf.alt_lna_conf =
1587 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1588 break;
1589 case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
1590 antcomb->rssi_add = alt_rssi_avg;
1591 antcomb->scan = true;
1592 /* set to A-B */
1593 div_ant_conf.alt_lna_conf =
1594 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1595 break;
1596 case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
1597 antcomb->rssi_sub = alt_rssi_avg;
1598 antcomb->scan = false;
1599 if (antcomb->rssi_lna2 >
1600 (antcomb->rssi_lna1 +
1601 ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
1602 /* use LNA2 as main LNA */
1603 if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
1604 (antcomb->rssi_add > antcomb->rssi_sub)) {
1605 /* set to A+B */
1606 div_ant_conf.main_lna_conf =
1607 ATH_ANT_DIV_COMB_LNA2;
1608 div_ant_conf.alt_lna_conf =
1609 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1610 } else if (antcomb->rssi_sub >
1611 antcomb->rssi_lna1) {
1612 /* set to A-B */
1613 div_ant_conf.main_lna_conf =
1614 ATH_ANT_DIV_COMB_LNA2;
1615 div_ant_conf.alt_lna_conf =
1616 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1617 } else {
1618 /* set to LNA1 */
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 }
1624 } else {
1625 /* use LNA1 as main LNA */
1626 if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
1627 (antcomb->rssi_add > antcomb->rssi_sub)) {
1628 /* set to A+B */
1629 div_ant_conf.main_lna_conf =
1630 ATH_ANT_DIV_COMB_LNA1;
1631 div_ant_conf.alt_lna_conf =
1632 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1633 } else if (antcomb->rssi_sub >
1634 antcomb->rssi_lna1) {
1635 /* set to A-B */
1636 div_ant_conf.main_lna_conf =
1637 ATH_ANT_DIV_COMB_LNA1;
1638 div_ant_conf.alt_lna_conf =
1639 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1640 } else {
1641 /* set to LNA2 */
1642 div_ant_conf.main_lna_conf =
1643 ATH_ANT_DIV_COMB_LNA1;
1644 div_ant_conf.alt_lna_conf =
1645 ATH_ANT_DIV_COMB_LNA2;
1646 }
1647 }
1648 break;
1649 default:
1650 break;
1651 }
1652 } else {
1653 if (!antcomb->alt_good) {
1654 antcomb->scan_not_start = false;
1655 /* Set alt to another LNA */
1656 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
1657 div_ant_conf.main_lna_conf =
1658 ATH_ANT_DIV_COMB_LNA2;
1659 div_ant_conf.alt_lna_conf =
1660 ATH_ANT_DIV_COMB_LNA1;
1661 } else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
1662 div_ant_conf.main_lna_conf =
1663 ATH_ANT_DIV_COMB_LNA1;
1664 div_ant_conf.alt_lna_conf =
1665 ATH_ANT_DIV_COMB_LNA2;
1666 }
1667 goto div_comb_done;
1668 }
1669 }
1670
1671 ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
1672 main_rssi_avg, alt_rssi_avg,
1673 alt_ratio);
1674
1675 antcomb->quick_scan_cnt++;
1676
1677div_comb_done:
Mohammed Shafi Shajakhan3e9a2122011-05-13 20:31:23 +05301678 ath_ant_div_conf_fast_divbias(&div_ant_conf, antcomb, alt_ratio);
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001679 ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf);
1680
1681 antcomb->scan_start_time = jiffies;
1682 antcomb->total_pkt_count = 0;
1683 antcomb->main_total_rssi = 0;
1684 antcomb->alt_total_rssi = 0;
1685 antcomb->main_recv_cnt = 0;
1686 antcomb->alt_recv_cnt = 0;
1687}
1688
Felix Fietkaub5c804752010-04-15 17:38:48 -04001689int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1690{
1691 struct ath_buf *bf;
Felix Fietkau0d955212011-01-26 18:23:27 +01001692 struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001693 struct ieee80211_rx_status *rxs;
Sujithcbe61d82009-02-09 13:27:12 +05301694 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -07001695 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezb4afffc2009-11-02 11:36:08 -08001696 /*
Mohammed Shafi Shajakhancae6b742010-12-07 21:23:16 +05301697 * The hw can technically differ from common->hw when using ath9k
Luis R. Rodriguezb4afffc2009-11-02 11:36:08 -08001698 * virtual wiphy so to account for that we iterate over the active
1699 * wiphys and find the appropriate wiphy and therefore hw.
1700 */
Felix Fietkau7545daf2011-01-24 19:23:16 +01001701 struct ieee80211_hw *hw = sc->hw;
Sujithbe0418a2008-11-18 09:05:55 +05301702 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc9b14172009-11-04 16:47:22 -08001703 int retval;
Sujithbe0418a2008-11-18 09:05:55 +05301704 bool decrypt_error = false;
Felix Fietkau29bffa92010-03-29 20:14:23 -07001705 struct ath_rx_status rs;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001706 enum ath9k_rx_qtype qtype;
1707 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1708 int dma_type;
Vasanthakumar Thiagarajan5c6dd922010-05-20 14:34:47 -07001709 u8 rx_status_len = ah->caps.rx_status_len;
Felix Fietkaua6d20552010-06-12 00:33:54 -04001710 u64 tsf = 0;
1711 u32 tsf_lower = 0;
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001712 unsigned long flags;
Sujithbe0418a2008-11-18 09:05:55 +05301713
Felix Fietkaub5c804752010-04-15 17:38:48 -04001714 if (edma)
Felix Fietkaub5c804752010-04-15 17:38:48 -04001715 dma_type = DMA_BIDIRECTIONAL;
Ming Lei56824222010-05-14 21:15:38 +08001716 else
1717 dma_type = DMA_FROM_DEVICE;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001718
1719 qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
Sujithb77f4832008-12-07 21:44:03 +05301720 spin_lock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001721
Felix Fietkaua6d20552010-06-12 00:33:54 -04001722 tsf = ath9k_hw_gettsf64(ah);
1723 tsf_lower = tsf & 0xffffffff;
1724
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001725 do {
1726 /* If handling rx interrupt and flush is in progress => exit */
Sujith98deeea2008-08-11 14:05:46 +05301727 if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001728 break;
1729
Felix Fietkau29bffa92010-03-29 20:14:23 -07001730 memset(&rs, 0, sizeof(rs));
Felix Fietkaub5c804752010-04-15 17:38:48 -04001731 if (edma)
1732 bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1733 else
1734 bf = ath_get_next_rx_buf(sc, &rs);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001735
Felix Fietkaub5c804752010-04-15 17:38:48 -04001736 if (!bf)
1737 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001738
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001739 skb = bf->bf_mpdu;
Sujithbe0418a2008-11-18 09:05:55 +05301740 if (!skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001741 continue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001742
Felix Fietkau0d955212011-01-26 18:23:27 +01001743 /*
1744 * Take frame header from the first fragment and RX status from
1745 * the last one.
1746 */
1747 if (sc->rx.frag)
1748 hdr_skb = sc->rx.frag;
1749 else
1750 hdr_skb = skb;
1751
1752 hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
1753 rxs = IEEE80211_SKB_RXCB(hdr_skb);
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001754
Felix Fietkau29bffa92010-03-29 20:14:23 -07001755 ath_debug_stat_rx(sc, &rs);
Sujith1395d3f2010-01-08 10:36:11 +05301756
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301757 /*
Sujithbe0418a2008-11-18 09:05:55 +05301758 * If we're asked to flush receive queue, directly
1759 * chain it back at the queue without processing it.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001760 */
Sujithbe0418a2008-11-18 09:05:55 +05301761 if (flush)
Felix Fietkau0d955212011-01-26 18:23:27 +01001762 goto requeue_drop_frag;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001763
Jan Friedrichc8f3b722010-08-02 23:55:50 +02001764 retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
1765 rxs, &decrypt_error);
1766 if (retval)
Felix Fietkau0d955212011-01-26 18:23:27 +01001767 goto requeue_drop_frag;
Jan Friedrichc8f3b722010-08-02 23:55:50 +02001768
Felix Fietkaua6d20552010-06-12 00:33:54 -04001769 rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1770 if (rs.rs_tstamp > tsf_lower &&
1771 unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1772 rxs->mactime -= 0x100000000ULL;
1773
1774 if (rs.rs_tstamp < tsf_lower &&
1775 unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1776 rxs->mactime += 0x100000000ULL;
1777
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001778 /* Ensure we always have an skb to requeue once we are done
1779 * processing the current buffer's skb */
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001780 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001781
1782 /* If there is no memory we ignore the current RX'd frame,
1783 * tell hardware it can give us a new frame using the old
Sujithb77f4832008-12-07 21:44:03 +05301784 * skb and put it at the tail of the sc->rx.rxbuf list for
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001785 * processing. */
1786 if (!requeue_skb)
Felix Fietkau0d955212011-01-26 18:23:27 +01001787 goto requeue_drop_frag;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001788
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301789 /* Unmap the frame */
Gabor Juhos7da3c552009-01-14 20:17:03 +01001790 dma_unmap_single(sc->dev, bf->bf_buf_addr,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001791 common->rx_bufsize,
Felix Fietkaub5c804752010-04-15 17:38:48 -04001792 dma_type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001793
Felix Fietkaub5c804752010-04-15 17:38:48 -04001794 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1795 if (ah->caps.rx_status_len)
1796 skb_pull(skb, ah->caps.rx_status_len);
Sujithbe0418a2008-11-18 09:05:55 +05301797
Felix Fietkau0d955212011-01-26 18:23:27 +01001798 if (!rs.rs_more)
1799 ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1800 rxs, decrypt_error);
Sujithbe0418a2008-11-18 09:05:55 +05301801
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001802 /* We will now give hardware our shiny new allocated skb */
1803 bf->bf_mpdu = requeue_skb;
Gabor Juhos7da3c552009-01-14 20:17:03 +01001804 bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001805 common->rx_bufsize,
Felix Fietkaub5c804752010-04-15 17:38:48 -04001806 dma_type);
Gabor Juhos7da3c552009-01-14 20:17:03 +01001807 if (unlikely(dma_mapping_error(sc->dev,
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001808 bf->bf_buf_addr))) {
1809 dev_kfree_skb_any(requeue_skb);
1810 bf->bf_mpdu = NULL;
Ben Greear6cf9e992010-10-14 12:45:30 -07001811 bf->bf_buf_addr = 0;
Joe Perches38002762010-12-02 19:12:36 -08001812 ath_err(common, "dma_mapping_error() on RX\n");
Felix Fietkau7545daf2011-01-24 19:23:16 +01001813 ieee80211_rx(hw, skb);
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001814 break;
1815 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001816
Felix Fietkau0d955212011-01-26 18:23:27 +01001817 if (rs.rs_more) {
1818 /*
1819 * rs_more indicates chained descriptors which can be
1820 * used to link buffers together for a sort of
1821 * scatter-gather operation.
1822 */
1823 if (sc->rx.frag) {
1824 /* too many fragments - cannot handle frame */
1825 dev_kfree_skb_any(sc->rx.frag);
1826 dev_kfree_skb_any(skb);
1827 skb = NULL;
1828 }
1829 sc->rx.frag = skb;
1830 goto requeue;
1831 }
1832
1833 if (sc->rx.frag) {
1834 int space = skb->len - skb_tailroom(hdr_skb);
1835
1836 sc->rx.frag = NULL;
1837
1838 if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
1839 dev_kfree_skb(skb);
1840 goto requeue_drop_frag;
1841 }
1842
1843 skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
1844 skb->len);
1845 dev_kfree_skb_any(skb);
1846 skb = hdr_skb;
1847 }
1848
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001849 /*
1850 * change the default rx antenna if rx diversity chooses the
1851 * other antenna 3 times in a row.
1852 */
Felix Fietkau29bffa92010-03-29 20:14:23 -07001853 if (sc->rx.defant != rs.rs_antenna) {
Sujithb77f4832008-12-07 21:44:03 +05301854 if (++sc->rx.rxotherant >= 3)
Felix Fietkau29bffa92010-03-29 20:14:23 -07001855 ath_setdefantenna(sc, rs.rs_antenna);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001856 } else {
Sujithb77f4832008-12-07 21:44:03 +05301857 sc->rx.rxotherant = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001858 }
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301859
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001860 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Mohammed Shafi Shajakhanaaef24b2010-12-07 20:40:58 +05301861
1862 if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -07001863 PS_WAIT_FOR_CAB |
Mohammed Shafi Shajakhanaaef24b2010-12-07 20:40:58 +05301864 PS_WAIT_FOR_PSPOLL_DATA)) ||
Mohammed Shafi Shajakhancedc7e32011-04-22 13:12:23 +05301865 ath9k_check_auto_sleep(sc))
Jouni Malinencc659652009-05-14 21:28:48 +03001866 ath_rx_ps(sc, skb);
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001867 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Jouni Malinencc659652009-05-14 21:28:48 +03001868
Vasanthakumar Thiagarajan102885a2010-09-02 01:34:43 -07001869 if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)
1870 ath_ant_comb_scan(sc, &rs);
1871
Felix Fietkau7545daf2011-01-24 19:23:16 +01001872 ieee80211_rx(hw, skb);
Jouni Malinencc659652009-05-14 21:28:48 +03001873
Felix Fietkau0d955212011-01-26 18:23:27 +01001874requeue_drop_frag:
1875 if (sc->rx.frag) {
1876 dev_kfree_skb_any(sc->rx.frag);
1877 sc->rx.frag = NULL;
1878 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001879requeue:
Felix Fietkaub5c804752010-04-15 17:38:48 -04001880 if (edma) {
1881 list_add_tail(&bf->list, &sc->rx.rxbuf);
1882 ath_rx_edma_buf_link(sc, qtype);
1883 } else {
1884 list_move_tail(&bf->list, &sc->rx.rxbuf);
1885 ath_rx_buf_link(sc, bf);
Felix Fietkau95294972011-04-07 19:30:32 +02001886 ath9k_hw_rxena(ah);
Felix Fietkaub5c804752010-04-15 17:38:48 -04001887 }
Sujithbe0418a2008-11-18 09:05:55 +05301888 } while (1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001889
Sujithb77f4832008-12-07 21:44:03 +05301890 spin_unlock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001891
1892 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001893}