blob: a3fc987ebab003bd5fda0545fa581863b71ba806 [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
Sujithcee075a2009-03-13 09:07:23 +05302 * Copyright (c) 2008-2009 Atheros Communications Inc.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07003 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
Sujith394cf0a2009-02-09 13:26:54 +053017#include "ath9k.h"
Luis R. Rodriguezb622a722010-04-15 17:39:28 -040018#include "ar9003_mac.h"
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070019
Felix Fietkaub5c804752010-04-15 17:38:48 -040020#define SKB_CB_ATHBUF(__skb) (*((struct ath_buf **)__skb->cb))
21
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -070022static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
23{
24 return sc->ps_enabled &&
25 (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
26}
27
Jouni Malinenbce048d2009-03-03 19:23:28 +020028static struct ieee80211_hw * ath_get_virt_hw(struct ath_softc *sc,
29 struct ieee80211_hdr *hdr)
30{
Jouni Malinenc52f33d2009-03-03 19:23:29 +020031 struct ieee80211_hw *hw = sc->pri_wiphy->hw;
32 int i;
33
34 spin_lock_bh(&sc->wiphy_lock);
35 for (i = 0; i < sc->num_sec_wiphy; i++) {
36 struct ath_wiphy *aphy = sc->sec_wiphy[i];
37 if (aphy == NULL)
38 continue;
39 if (compare_ether_addr(hdr->addr1, aphy->hw->wiphy->perm_addr)
40 == 0) {
41 hw = aphy->hw;
42 break;
43 }
44 }
45 spin_unlock_bh(&sc->wiphy_lock);
46 return hw;
Jouni Malinenbce048d2009-03-03 19:23:28 +020047}
48
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070049/*
50 * Setup and link descriptors.
51 *
52 * 11N: we can no longer afford to self link the last descriptor.
53 * MAC acknowledges BA status as long as it copies frames to host
54 * buffer (or rx fifo). This can incorrectly acknowledge packets
55 * to a sender if last desc is self-linked.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070056 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070057static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
58{
Sujithcbe61d82009-02-09 13:27:12 +053059 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080060 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070061 struct ath_desc *ds;
62 struct sk_buff *skb;
63
64 ATH_RXBUF_RESET(bf);
65
66 ds = bf->bf_desc;
Sujithbe0418a2008-11-18 09:05:55 +053067 ds->ds_link = 0; /* link to null */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070068 ds->ds_data = bf->bf_buf_addr;
69
Sujithbe0418a2008-11-18 09:05:55 +053070 /* virtual addr of the beginning of the buffer. */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070071 skb = bf->bf_mpdu;
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -070072 BUG_ON(skb == NULL);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070073 ds->ds_vdata = skb->data;
74
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080075 /*
76 * setup rx descriptors. The rx_bufsize here tells the hardware
Luis R. Rodriguezb4b6cda2008-11-20 17:15:13 -080077 * how much data it can DMA to us and that we are prepared
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080078 * to process
79 */
Sujithb77f4832008-12-07 21:44:03 +053080 ath9k_hw_setuprxdesc(ah, ds,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080081 common->rx_bufsize,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070082 0);
83
Sujithb77f4832008-12-07 21:44:03 +053084 if (sc->rx.rxlink == NULL)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070085 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
86 else
Sujithb77f4832008-12-07 21:44:03 +053087 *sc->rx.rxlink = bf->bf_daddr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070088
Sujithb77f4832008-12-07 21:44:03 +053089 sc->rx.rxlink = &ds->ds_link;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070090 ath9k_hw_rxena(ah);
91}
92
Sujithff37e332008-11-24 12:07:55 +053093static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
94{
95 /* XXX block beacon interrupts */
96 ath9k_hw_setantenna(sc->sc_ah, antenna);
Sujithb77f4832008-12-07 21:44:03 +053097 sc->rx.defant = antenna;
98 sc->rx.rxotherant = 0;
Sujithff37e332008-11-24 12:07:55 +053099}
100
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700101static void ath_opmode_init(struct ath_softc *sc)
102{
Sujithcbe61d82009-02-09 13:27:12 +0530103 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700104 struct ath_common *common = ath9k_hw_common(ah);
105
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700106 u32 rfilt, mfilt[2];
107
108 /* configure rx filter */
109 rfilt = ath_calcrxfilter(sc);
110 ath9k_hw_setrxfilter(ah, rfilt);
111
112 /* configure bssid mask */
Sujith2660b812009-02-09 13:27:26 +0530113 if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
Luis R. Rodriguez13b81552009-09-10 17:52:45 -0700114 ath_hw_setbssidmask(common);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700115
116 /* configure operational mode */
117 ath9k_hw_setopmode(ah);
118
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700119 /* calculate and install multicast filter */
120 mfilt[0] = mfilt[1] = ~0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700121 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700122}
123
Felix Fietkaub5c804752010-04-15 17:38:48 -0400124static bool ath_rx_edma_buf_link(struct ath_softc *sc,
125 enum ath9k_rx_qtype qtype)
126{
127 struct ath_hw *ah = sc->sc_ah;
128 struct ath_rx_edma *rx_edma;
129 struct sk_buff *skb;
130 struct ath_buf *bf;
131
132 rx_edma = &sc->rx.rx_edma[qtype];
133 if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
134 return false;
135
136 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
137 list_del_init(&bf->list);
138
139 skb = bf->bf_mpdu;
140
141 ATH_RXBUF_RESET(bf);
142 memset(skb->data, 0, ah->caps.rx_status_len);
143 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
144 ah->caps.rx_status_len, DMA_TO_DEVICE);
145
146 SKB_CB_ATHBUF(skb) = bf;
147 ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
148 skb_queue_tail(&rx_edma->rx_fifo, skb);
149
150 return true;
151}
152
153static void ath_rx_addbuffer_edma(struct ath_softc *sc,
154 enum ath9k_rx_qtype qtype, int size)
155{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400156 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
157 u32 nbuf = 0;
158
Felix Fietkaub5c804752010-04-15 17:38:48 -0400159 if (list_empty(&sc->rx.rxbuf)) {
160 ath_print(common, ATH_DBG_QUEUE, "No free rx buf available\n");
161 return;
162 }
163
164 while (!list_empty(&sc->rx.rxbuf)) {
165 nbuf++;
166
167 if (!ath_rx_edma_buf_link(sc, qtype))
168 break;
169
170 if (nbuf >= size)
171 break;
172 }
173}
174
175static void ath_rx_remove_buffer(struct ath_softc *sc,
176 enum ath9k_rx_qtype qtype)
177{
178 struct ath_buf *bf;
179 struct ath_rx_edma *rx_edma;
180 struct sk_buff *skb;
181
182 rx_edma = &sc->rx.rx_edma[qtype];
183
184 while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
185 bf = SKB_CB_ATHBUF(skb);
186 BUG_ON(!bf);
187 list_add_tail(&bf->list, &sc->rx.rxbuf);
188 }
189}
190
191static void ath_rx_edma_cleanup(struct ath_softc *sc)
192{
193 struct ath_buf *bf;
194
195 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
196 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
197
198 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
199 if (bf->bf_mpdu)
200 dev_kfree_skb_any(bf->bf_mpdu);
201 }
202
203 INIT_LIST_HEAD(&sc->rx.rxbuf);
204
205 kfree(sc->rx.rx_bufptr);
206 sc->rx.rx_bufptr = NULL;
207}
208
209static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
210{
211 skb_queue_head_init(&rx_edma->rx_fifo);
212 skb_queue_head_init(&rx_edma->rx_buffers);
213 rx_edma->rx_fifo_hwsize = size;
214}
215
216static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
217{
218 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
219 struct ath_hw *ah = sc->sc_ah;
220 struct sk_buff *skb;
221 struct ath_buf *bf;
222 int error = 0, i;
223 u32 size;
224
225
226 common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN +
227 ah->caps.rx_status_len,
228 min(common->cachelsz, (u16)64));
229
230 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
231 ah->caps.rx_status_len);
232
233 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
234 ah->caps.rx_lp_qdepth);
235 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
236 ah->caps.rx_hp_qdepth);
237
238 size = sizeof(struct ath_buf) * nbufs;
239 bf = kzalloc(size, GFP_KERNEL);
240 if (!bf)
241 return -ENOMEM;
242
243 INIT_LIST_HEAD(&sc->rx.rxbuf);
244 sc->rx.rx_bufptr = bf;
245
246 for (i = 0; i < nbufs; i++, bf++) {
247 skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
248 if (!skb) {
249 error = -ENOMEM;
250 goto rx_init_fail;
251 }
252
253 memset(skb->data, 0, common->rx_bufsize);
254 bf->bf_mpdu = skb;
255
256 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
257 common->rx_bufsize,
258 DMA_BIDIRECTIONAL);
259 if (unlikely(dma_mapping_error(sc->dev,
260 bf->bf_buf_addr))) {
261 dev_kfree_skb_any(skb);
262 bf->bf_mpdu = NULL;
263 ath_print(common, ATH_DBG_FATAL,
264 "dma_mapping_error() on RX init\n");
265 error = -ENOMEM;
266 goto rx_init_fail;
267 }
268
269 list_add_tail(&bf->list, &sc->rx.rxbuf);
270 }
271
272 return 0;
273
274rx_init_fail:
275 ath_rx_edma_cleanup(sc);
276 return error;
277}
278
279static void ath_edma_start_recv(struct ath_softc *sc)
280{
281 spin_lock_bh(&sc->rx.rxbuflock);
282
283 ath9k_hw_rxena(sc->sc_ah);
284
285 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
286 sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
287
288 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
289 sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
290
291 spin_unlock_bh(&sc->rx.rxbuflock);
292
293 ath_opmode_init(sc);
294
Luis R. Rodriguez40346b62010-06-12 00:33:44 -0400295 ath9k_hw_startpcureceive(sc->sc_ah, (sc->sc_flags & SC_OP_SCANNING));
Felix Fietkaub5c804752010-04-15 17:38:48 -0400296}
297
298static void ath_edma_stop_recv(struct ath_softc *sc)
299{
300 spin_lock_bh(&sc->rx.rxbuflock);
301 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
302 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
303 spin_unlock_bh(&sc->rx.rxbuflock);
304}
305
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700306int ath_rx_init(struct ath_softc *sc, int nbufs)
307{
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -0700308 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700309 struct sk_buff *skb;
310 struct ath_buf *bf;
311 int error = 0;
312
Sujith797fe5c2009-03-30 15:28:45 +0530313 spin_lock_init(&sc->rx.rxflushlock);
314 sc->sc_flags &= ~SC_OP_RXFLUSH;
315 spin_lock_init(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700316
Felix Fietkaub5c804752010-04-15 17:38:48 -0400317 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
318 return ath_rx_edma_init(sc, nbufs);
319 } else {
320 common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN,
321 min(common->cachelsz, (u16)64));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700322
Felix Fietkaub5c804752010-04-15 17:38:48 -0400323 ath_print(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
324 common->cachelsz, common->rx_bufsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700325
Felix Fietkaub5c804752010-04-15 17:38:48 -0400326 /* Initialize rx descriptors */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700327
Felix Fietkaub5c804752010-04-15 17:38:48 -0400328 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
Vasanthakumar Thiagarajan4adfcde2010-04-15 17:39:33 -0400329 "rx", nbufs, 1, 0);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400330 if (error != 0) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700331 ath_print(common, ATH_DBG_FATAL,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400332 "failed to allocate rx descriptors: %d\n",
333 error);
Sujith797fe5c2009-03-30 15:28:45 +0530334 goto err;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700335 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400336
337 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
338 skb = ath_rxbuf_alloc(common, common->rx_bufsize,
339 GFP_KERNEL);
340 if (skb == NULL) {
341 error = -ENOMEM;
342 goto err;
343 }
344
345 bf->bf_mpdu = skb;
346 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
347 common->rx_bufsize,
348 DMA_FROM_DEVICE);
349 if (unlikely(dma_mapping_error(sc->dev,
350 bf->bf_buf_addr))) {
351 dev_kfree_skb_any(skb);
352 bf->bf_mpdu = NULL;
353 ath_print(common, ATH_DBG_FATAL,
354 "dma_mapping_error() on RX init\n");
355 error = -ENOMEM;
356 goto err;
357 }
358 bf->bf_dmacontext = bf->bf_buf_addr;
359 }
360 sc->rx.rxlink = NULL;
Sujith797fe5c2009-03-30 15:28:45 +0530361 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700362
Sujith797fe5c2009-03-30 15:28:45 +0530363err:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700364 if (error)
365 ath_rx_cleanup(sc);
366
367 return error;
368}
369
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700370void ath_rx_cleanup(struct ath_softc *sc)
371{
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800372 struct ath_hw *ah = sc->sc_ah;
373 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700374 struct sk_buff *skb;
375 struct ath_buf *bf;
376
Felix Fietkaub5c804752010-04-15 17:38:48 -0400377 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
378 ath_rx_edma_cleanup(sc);
379 return;
380 } else {
381 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
382 skb = bf->bf_mpdu;
383 if (skb) {
384 dma_unmap_single(sc->dev, bf->bf_buf_addr,
385 common->rx_bufsize,
386 DMA_FROM_DEVICE);
387 dev_kfree_skb(skb);
388 }
Luis R. Rodriguez051b9192009-03-23 18:25:01 -0400389 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700390
Felix Fietkaub5c804752010-04-15 17:38:48 -0400391 if (sc->rx.rxdma.dd_desc_len != 0)
392 ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
393 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700394}
395
396/*
397 * Calculate the receive filter according to the
398 * operating mode and state:
399 *
400 * o always accept unicast, broadcast, and multicast traffic
401 * o maintain current state of phy error reception (the hal
402 * may enable phy error frames for noise immunity work)
403 * o probe request frames are accepted only when operating in
404 * hostap, adhoc, or monitor modes
405 * o enable promiscuous mode according to the interface state
406 * o accept beacons:
407 * - when operating in adhoc mode so the 802.11 layer creates
408 * node table entries for peers,
409 * - when operating in station mode for collecting rssi data when
410 * the station is otherwise quiet, or
411 * - when operating as a repeater so we see repeater-sta beacons
412 * - when scanning
413 */
414
415u32 ath_calcrxfilter(struct ath_softc *sc)
416{
417#define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
Sujith7dcfdcd2008-08-11 14:03:13 +0530418
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700419 u32 rfilt;
420
421 rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
422 | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
423 | ATH9K_RX_FILTER_MCAST;
424
425 /* If not a STA, enable processing of Probe Requests */
Sujith2660b812009-02-09 13:27:26 +0530426 if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700427 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
428
Jouni Malinen217ba9d2009-03-10 10:55:50 +0200429 /*
430 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
431 * mode interface or when in monitor mode. AP mode does not need this
432 * since it receives all in-BSS frames anyway.
433 */
Sujith2660b812009-02-09 13:27:26 +0530434 if (((sc->sc_ah->opmode != NL80211_IFTYPE_AP) &&
Sujithb77f4832008-12-07 21:44:03 +0530435 (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) ||
Jouni Malinen217ba9d2009-03-10 10:55:50 +0200436 (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700437 rfilt |= ATH9K_RX_FILTER_PROM;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700438
Sujithd42c6b72009-02-04 08:10:22 +0530439 if (sc->rx.rxfilter & FIF_CONTROL)
440 rfilt |= ATH9K_RX_FILTER_CONTROL;
441
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530442 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
443 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
444 rfilt |= ATH9K_RX_FILTER_MYBEACON;
445 else
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700446 rfilt |= ATH9K_RX_FILTER_BEACON;
447
Senthil Balasubramanian66afad02009-09-18 15:06:07 +0530448 if ((AR_SREV_9280_10_OR_LATER(sc->sc_ah) ||
449 AR_SREV_9285_10_OR_LATER(sc->sc_ah)) &&
450 (sc->sc_ah->opmode == NL80211_IFTYPE_AP) &&
451 (sc->rx.rxfilter & FIF_PSPOLL))
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530452 rfilt |= ATH9K_RX_FILTER_PSPOLL;
Sujithbe0418a2008-11-18 09:05:55 +0530453
Sujith7ea310b2009-09-03 12:08:43 +0530454 if (conf_is_ht(&sc->hw->conf))
455 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
456
Javier Cardona5eb6ba82009-08-20 19:12:07 -0700457 if (sc->sec_wiphy || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
Jouni Malinenb93bce22009-03-03 19:23:30 +0200458 /* TODO: only needed if more than one BSSID is in use in
459 * station/adhoc mode */
Javier Cardona5eb6ba82009-08-20 19:12:07 -0700460 /* The following may also be needed for other older chips */
461 if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
462 rfilt |= ATH9K_RX_FILTER_PROM;
Jouni Malinenb93bce22009-03-03 19:23:30 +0200463 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
464 }
465
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700466 return rfilt;
Sujith7dcfdcd2008-08-11 14:03:13 +0530467
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700468#undef RX_FILTER_PRESERVE
469}
470
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700471int ath_startrecv(struct ath_softc *sc)
472{
Sujithcbe61d82009-02-09 13:27:12 +0530473 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700474 struct ath_buf *bf, *tbf;
475
Felix Fietkaub5c804752010-04-15 17:38:48 -0400476 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
477 ath_edma_start_recv(sc);
478 return 0;
479 }
480
Sujithb77f4832008-12-07 21:44:03 +0530481 spin_lock_bh(&sc->rx.rxbuflock);
482 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700483 goto start_recv;
484
Sujithb77f4832008-12-07 21:44:03 +0530485 sc->rx.rxlink = NULL;
486 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700487 ath_rx_buf_link(sc, bf);
488 }
489
490 /* We could have deleted elements so the list may be empty now */
Sujithb77f4832008-12-07 21:44:03 +0530491 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700492 goto start_recv;
493
Sujithb77f4832008-12-07 21:44:03 +0530494 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700495 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
Sujithbe0418a2008-11-18 09:05:55 +0530496 ath9k_hw_rxena(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700497
498start_recv:
Sujithb77f4832008-12-07 21:44:03 +0530499 spin_unlock_bh(&sc->rx.rxbuflock);
Sujithbe0418a2008-11-18 09:05:55 +0530500 ath_opmode_init(sc);
Luis R. Rodriguez40346b62010-06-12 00:33:44 -0400501 ath9k_hw_startpcureceive(ah, (sc->sc_flags & SC_OP_SCANNING));
Sujithbe0418a2008-11-18 09:05:55 +0530502
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700503 return 0;
504}
505
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700506bool ath_stoprecv(struct ath_softc *sc)
507{
Sujithcbe61d82009-02-09 13:27:12 +0530508 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700509 bool stopped;
510
Sujithbe0418a2008-11-18 09:05:55 +0530511 ath9k_hw_stoppcurecv(ah);
512 ath9k_hw_setrxfilter(ah, 0);
513 stopped = ath9k_hw_stopdmarecv(ah);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400514
515 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
516 ath_edma_stop_recv(sc);
517 else
518 sc->rx.rxlink = NULL;
Sujithbe0418a2008-11-18 09:05:55 +0530519
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700520 return stopped;
521}
522
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700523void ath_flushrecv(struct ath_softc *sc)
524{
Sujithb77f4832008-12-07 21:44:03 +0530525 spin_lock_bh(&sc->rx.rxflushlock);
Sujith98deeea2008-08-11 14:05:46 +0530526 sc->sc_flags |= SC_OP_RXFLUSH;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400527 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
528 ath_rx_tasklet(sc, 1, true);
529 ath_rx_tasklet(sc, 1, false);
Sujith98deeea2008-08-11 14:05:46 +0530530 sc->sc_flags &= ~SC_OP_RXFLUSH;
Sujithb77f4832008-12-07 21:44:03 +0530531 spin_unlock_bh(&sc->rx.rxflushlock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700532}
533
Jouni Malinencc659652009-05-14 21:28:48 +0300534static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
535{
536 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
537 struct ieee80211_mgmt *mgmt;
538 u8 *pos, *end, id, elen;
539 struct ieee80211_tim_ie *tim;
540
541 mgmt = (struct ieee80211_mgmt *)skb->data;
542 pos = mgmt->u.beacon.variable;
543 end = skb->data + skb->len;
544
545 while (pos + 2 < end) {
546 id = *pos++;
547 elen = *pos++;
548 if (pos + elen > end)
549 break;
550
551 if (id == WLAN_EID_TIM) {
552 if (elen < sizeof(*tim))
553 break;
554 tim = (struct ieee80211_tim_ie *) pos;
555 if (tim->dtim_count != 0)
556 break;
557 return tim->bitmap_ctrl & 0x01;
558 }
559
560 pos += elen;
561 }
562
563 return false;
564}
565
Jouni Malinencc659652009-05-14 21:28:48 +0300566static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
567{
568 struct ieee80211_mgmt *mgmt;
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700569 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300570
571 if (skb->len < 24 + 8 + 2 + 2)
572 return;
573
574 mgmt = (struct ieee80211_mgmt *)skb->data;
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700575 if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0)
Jouni Malinencc659652009-05-14 21:28:48 +0300576 return; /* not from our current AP */
577
Sujith1b04b932010-01-08 10:36:05 +0530578 sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
Gabor Juhos293dc5d2009-06-19 12:17:48 +0200579
Sujith1b04b932010-01-08 10:36:05 +0530580 if (sc->ps_flags & PS_BEACON_SYNC) {
581 sc->ps_flags &= ~PS_BEACON_SYNC;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700582 ath_print(common, ATH_DBG_PS,
583 "Reconfigure Beacon timers based on "
584 "timestamp from the AP\n");
Jouni Malinenccdfeab2009-05-20 21:59:08 +0300585 ath_beacon_config(sc, NULL);
586 }
587
Jouni Malinencc659652009-05-14 21:28:48 +0300588 if (ath_beacon_dtim_pending_cab(skb)) {
589 /*
590 * Remain awake waiting for buffered broadcast/multicast
Gabor Juhos58f5fff2009-06-17 20:53:20 +0200591 * frames. If the last broadcast/multicast frame is not
592 * received properly, the next beacon frame will work as
593 * a backup trigger for returning into NETWORK SLEEP state,
594 * so we are waiting for it as well.
Jouni Malinencc659652009-05-14 21:28:48 +0300595 */
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700596 ath_print(common, ATH_DBG_PS, "Received DTIM beacon indicating "
597 "buffered broadcast/multicast frame(s)\n");
Sujith1b04b932010-01-08 10:36:05 +0530598 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
Jouni Malinencc659652009-05-14 21:28:48 +0300599 return;
600 }
601
Sujith1b04b932010-01-08 10:36:05 +0530602 if (sc->ps_flags & PS_WAIT_FOR_CAB) {
Jouni Malinencc659652009-05-14 21:28:48 +0300603 /*
604 * This can happen if a broadcast frame is dropped or the AP
605 * fails to send a frame indicating that all CAB frames have
606 * been delivered.
607 */
Sujith1b04b932010-01-08 10:36:05 +0530608 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700609 ath_print(common, ATH_DBG_PS,
610 "PS wait for CAB frames timed out\n");
Jouni Malinencc659652009-05-14 21:28:48 +0300611 }
Jouni Malinencc659652009-05-14 21:28:48 +0300612}
613
614static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb)
615{
616 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700617 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300618
619 hdr = (struct ieee80211_hdr *)skb->data;
620
621 /* Process Beacon and CAB receive in PS state */
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -0700622 if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
623 && ieee80211_is_beacon(hdr->frame_control))
Jouni Malinencc659652009-05-14 21:28:48 +0300624 ath_rx_ps_beacon(sc, skb);
Sujith1b04b932010-01-08 10:36:05 +0530625 else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
Jouni Malinencc659652009-05-14 21:28:48 +0300626 (ieee80211_is_data(hdr->frame_control) ||
627 ieee80211_is_action(hdr->frame_control)) &&
628 is_multicast_ether_addr(hdr->addr1) &&
629 !ieee80211_has_moredata(hdr->frame_control)) {
Jouni Malinencc659652009-05-14 21:28:48 +0300630 /*
631 * No more broadcast/multicast frames to be received at this
632 * point.
633 */
Sujith1b04b932010-01-08 10:36:05 +0530634 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700635 ath_print(common, ATH_DBG_PS,
636 "All PS CAB frames received, back to sleep\n");
Sujith1b04b932010-01-08 10:36:05 +0530637 } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300638 !is_multicast_ether_addr(hdr->addr1) &&
639 !ieee80211_has_morefrags(hdr->frame_control)) {
Sujith1b04b932010-01-08 10:36:05 +0530640 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700641 ath_print(common, ATH_DBG_PS,
642 "Going back to sleep after having received "
Pavel Roskinf643e512010-01-29 17:22:12 -0500643 "PS-Poll data (0x%lx)\n",
Sujith1b04b932010-01-08 10:36:05 +0530644 sc->ps_flags & (PS_WAIT_FOR_BEACON |
645 PS_WAIT_FOR_CAB |
646 PS_WAIT_FOR_PSPOLL_DATA |
647 PS_WAIT_FOR_TX_ACK));
Jouni Malinencc659652009-05-14 21:28:48 +0300648 }
649}
650
Luis R. Rodriguezb4afffc2009-11-02 11:36:08 -0800651static void ath_rx_send_to_mac80211(struct ieee80211_hw *hw,
652 struct ath_softc *sc, struct sk_buff *skb,
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -0800653 struct ieee80211_rx_status *rxs)
Jouni Malinen9d64a3c2009-05-14 21:28:47 +0300654{
655 struct ieee80211_hdr *hdr;
656
657 hdr = (struct ieee80211_hdr *)skb->data;
658
659 /* Send the frame to mac80211 */
660 if (is_multicast_ether_addr(hdr->addr1)) {
661 int i;
662 /*
663 * Deliver broadcast/multicast frames to all suitable
664 * virtual wiphys.
665 */
666 /* TODO: filter based on channel configuration */
667 for (i = 0; i < sc->num_sec_wiphy; i++) {
668 struct ath_wiphy *aphy = sc->sec_wiphy[i];
669 struct sk_buff *nskb;
670 if (aphy == NULL)
671 continue;
672 nskb = skb_copy(skb, GFP_ATOMIC);
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -0800673 if (!nskb)
674 continue;
675 ieee80211_rx(aphy->hw, nskb);
Jouni Malinen9d64a3c2009-05-14 21:28:47 +0300676 }
Johannes Bergf1d58c22009-06-17 13:13:00 +0200677 ieee80211_rx(sc->hw, skb);
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -0800678 } else
Jouni Malinen9d64a3c2009-05-14 21:28:47 +0300679 /* Deliver unicast frames based on receiver address */
Luis R. Rodriguezb4afffc2009-11-02 11:36:08 -0800680 ieee80211_rx(hw, skb);
Jouni Malinen9d64a3c2009-05-14 21:28:47 +0300681}
682
Felix Fietkaub5c804752010-04-15 17:38:48 -0400683static bool ath_edma_get_buffers(struct ath_softc *sc,
684 enum ath9k_rx_qtype qtype)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700685{
Felix Fietkaub5c804752010-04-15 17:38:48 -0400686 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
687 struct ath_hw *ah = sc->sc_ah;
688 struct ath_common *common = ath9k_hw_common(ah);
689 struct sk_buff *skb;
Sujithbe0418a2008-11-18 09:05:55 +0530690 struct ath_buf *bf;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400691 int ret;
692
693 skb = skb_peek(&rx_edma->rx_fifo);
694 if (!skb)
695 return false;
696
697 bf = SKB_CB_ATHBUF(skb);
698 BUG_ON(!bf);
699
Ming Leice9426d2010-05-15 18:25:40 +0800700 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400701 common->rx_bufsize, DMA_FROM_DEVICE);
702
703 ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data);
Ming Leice9426d2010-05-15 18:25:40 +0800704 if (ret == -EINPROGRESS) {
705 /*let device gain the buffer again*/
706 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
707 common->rx_bufsize, DMA_FROM_DEVICE);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400708 return false;
Ming Leice9426d2010-05-15 18:25:40 +0800709 }
Felix Fietkaub5c804752010-04-15 17:38:48 -0400710
711 __skb_unlink(skb, &rx_edma->rx_fifo);
712 if (ret == -EINVAL) {
713 /* corrupt descriptor, skip this one and the following one */
714 list_add_tail(&bf->list, &sc->rx.rxbuf);
715 ath_rx_edma_buf_link(sc, qtype);
716 skb = skb_peek(&rx_edma->rx_fifo);
717 if (!skb)
718 return true;
719
720 bf = SKB_CB_ATHBUF(skb);
721 BUG_ON(!bf);
722
723 __skb_unlink(skb, &rx_edma->rx_fifo);
724 list_add_tail(&bf->list, &sc->rx.rxbuf);
725 ath_rx_edma_buf_link(sc, qtype);
Vasanthakumar Thiagarajan083e3e82010-05-10 19:41:34 -0700726 return true;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400727 }
728 skb_queue_tail(&rx_edma->rx_buffers, skb);
729
730 return true;
731}
732
733static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
734 struct ath_rx_status *rs,
735 enum ath9k_rx_qtype qtype)
736{
737 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
738 struct sk_buff *skb;
739 struct ath_buf *bf;
740
741 while (ath_edma_get_buffers(sc, qtype));
742 skb = __skb_dequeue(&rx_edma->rx_buffers);
743 if (!skb)
744 return NULL;
745
746 bf = SKB_CB_ATHBUF(skb);
747 ath9k_hw_process_rxdesc_edma(sc->sc_ah, rs, skb->data);
748 return bf;
749}
750
751static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
752 struct ath_rx_status *rs)
753{
754 struct ath_hw *ah = sc->sc_ah;
755 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700756 struct ath_desc *ds;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400757 struct ath_buf *bf;
758 int ret;
759
760 if (list_empty(&sc->rx.rxbuf)) {
761 sc->rx.rxlink = NULL;
762 return NULL;
763 }
764
765 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
766 ds = bf->bf_desc;
767
768 /*
769 * Must provide the virtual address of the current
770 * descriptor, the physical address, and the virtual
771 * address of the next descriptor in the h/w chain.
772 * This allows the HAL to look ahead to see if the
773 * hardware is done with a descriptor by checking the
774 * done bit in the following descriptor and the address
775 * of the current descriptor the DMA engine is working
776 * on. All this is necessary because of our use of
777 * a self-linked list to avoid rx overruns.
778 */
779 ret = ath9k_hw_rxprocdesc(ah, ds, rs, 0);
780 if (ret == -EINPROGRESS) {
781 struct ath_rx_status trs;
782 struct ath_buf *tbf;
783 struct ath_desc *tds;
784
785 memset(&trs, 0, sizeof(trs));
786 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
787 sc->rx.rxlink = NULL;
788 return NULL;
789 }
790
791 tbf = list_entry(bf->list.next, struct ath_buf, list);
792
793 /*
794 * On some hardware the descriptor status words could
795 * get corrupted, including the done bit. Because of
796 * this, check if the next descriptor's done bit is
797 * set or not.
798 *
799 * If the next descriptor's done bit is set, the current
800 * descriptor has been corrupted. Force s/w to discard
801 * this descriptor and continue...
802 */
803
804 tds = tbf->bf_desc;
805 ret = ath9k_hw_rxprocdesc(ah, tds, &trs, 0);
806 if (ret == -EINPROGRESS)
807 return NULL;
808 }
809
810 if (!bf->bf_mpdu)
811 return bf;
812
813 /*
814 * Synchronize the DMA transfer with CPU before
815 * 1. accessing the frame
816 * 2. requeueing the same buffer to h/w
817 */
Ming Leice9426d2010-05-15 18:25:40 +0800818 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Felix Fietkaub5c804752010-04-15 17:38:48 -0400819 common->rx_bufsize,
820 DMA_FROM_DEVICE);
821
822 return bf;
823}
824
Sujithd4357002010-05-20 15:34:38 +0530825/* Assumes you've already done the endian to CPU conversion */
826static bool ath9k_rx_accept(struct ath_common *common,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700827 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530828 struct ieee80211_rx_status *rxs,
829 struct ath_rx_status *rx_stats,
830 bool *decrypt_error)
831{
832 struct ath_hw *ah = common->ah;
Sujithd4357002010-05-20 15:34:38 +0530833 __le16 fc;
Vasanthakumar Thiagarajanb7b1b512010-05-20 14:34:48 -0700834 u8 rx_status_len = ah->caps.rx_status_len;
Sujithd4357002010-05-20 15:34:38 +0530835
Sujithd4357002010-05-20 15:34:38 +0530836 fc = hdr->frame_control;
837
838 if (!rx_stats->rs_datalen)
839 return false;
840 /*
841 * rs_status follows rs_datalen so if rs_datalen is too large
842 * we can take a hint that hardware corrupted it, so ignore
843 * those frames.
844 */
Vasanthakumar Thiagarajanb7b1b512010-05-20 14:34:48 -0700845 if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len))
Sujithd4357002010-05-20 15:34:38 +0530846 return false;
847
848 /*
849 * rs_more indicates chained descriptors which can be used
850 * to link buffers together for a sort of scatter-gather
851 * operation.
852 * reject the frame, we don't support scatter-gather yet and
853 * the frame is probably corrupt anyway
854 */
855 if (rx_stats->rs_more)
856 return false;
857
858 /*
859 * The rx_stats->rs_status will not be set until the end of the
860 * chained descriptors so it can be ignored if rs_more is set. The
861 * rs_more will be false at the last element of the chained
862 * descriptors.
863 */
864 if (rx_stats->rs_status != 0) {
865 if (rx_stats->rs_status & ATH9K_RXERR_CRC)
866 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
867 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
868 return false;
869
870 if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
871 *decrypt_error = true;
872 } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
873 if (ieee80211_is_ctl(fc))
874 /*
875 * Sometimes, we get invalid
876 * MIC failures on valid control frames.
877 * Remove these mic errors.
878 */
879 rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
880 else
881 rxs->flag |= RX_FLAG_MMIC_ERROR;
882 }
883 /*
884 * Reject error frames with the exception of
885 * decryption and MIC failures. For monitor mode,
886 * we also ignore the CRC error.
887 */
888 if (ah->opmode == NL80211_IFTYPE_MONITOR) {
889 if (rx_stats->rs_status &
890 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
891 ATH9K_RXERR_CRC))
892 return false;
893 } else {
894 if (rx_stats->rs_status &
895 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
896 return false;
897 }
898 }
899 }
900 return true;
901}
902
903static int ath9k_process_rate(struct ath_common *common,
904 struct ieee80211_hw *hw,
905 struct ath_rx_status *rx_stats,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700906 struct ieee80211_rx_status *rxs)
Sujithd4357002010-05-20 15:34:38 +0530907{
908 struct ieee80211_supported_band *sband;
909 enum ieee80211_band band;
910 unsigned int i = 0;
911
912 band = hw->conf.channel->band;
913 sband = hw->wiphy->bands[band];
914
915 if (rx_stats->rs_rate & 0x80) {
916 /* HT rate */
917 rxs->flag |= RX_FLAG_HT;
918 if (rx_stats->rs_flags & ATH9K_RX_2040)
919 rxs->flag |= RX_FLAG_40MHZ;
920 if (rx_stats->rs_flags & ATH9K_RX_GI)
921 rxs->flag |= RX_FLAG_SHORT_GI;
922 rxs->rate_idx = rx_stats->rs_rate & 0x7f;
923 return 0;
924 }
925
926 for (i = 0; i < sband->n_bitrates; i++) {
927 if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
928 rxs->rate_idx = i;
929 return 0;
930 }
931 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
932 rxs->flag |= RX_FLAG_SHORTPRE;
933 rxs->rate_idx = i;
934 return 0;
935 }
936 }
937
938 /*
939 * No valid hardware bitrate found -- we should not get here
940 * because hardware has already validated this frame as OK.
941 */
942 ath_print(common, ATH_DBG_XMIT, "unsupported hw bitrate detected "
943 "0x%02x using 1 Mbit\n", rx_stats->rs_rate);
Sujithd4357002010-05-20 15:34:38 +0530944
945 return -EINVAL;
946}
947
948static void ath9k_process_rssi(struct ath_common *common,
949 struct ieee80211_hw *hw,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700950 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530951 struct ath_rx_status *rx_stats)
952{
953 struct ath_hw *ah = common->ah;
954 struct ieee80211_sta *sta;
Sujithd4357002010-05-20 15:34:38 +0530955 struct ath_node *an;
956 int last_rssi = ATH_RSSI_DUMMY_MARKER;
957 __le16 fc;
958
Sujithd4357002010-05-20 15:34:38 +0530959 fc = hdr->frame_control;
960
961 rcu_read_lock();
962 /*
963 * XXX: use ieee80211_find_sta! This requires quite a bit of work
964 * under the current ath9k virtual wiphy implementation as we have
965 * no way of tying a vif to wiphy. Typically vifs are attached to
966 * at least one sdata of a wiphy on mac80211 but with ath9k virtual
967 * wiphy you'd have to iterate over every wiphy and each sdata.
968 */
969 sta = ieee80211_find_sta_by_hw(hw, hdr->addr2);
970 if (sta) {
971 an = (struct ath_node *) sta->drv_priv;
972 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD &&
973 !rx_stats->rs_moreaggr)
974 ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi);
975 last_rssi = an->last_rssi;
976 }
977 rcu_read_unlock();
978
979 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
980 rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
981 ATH_RSSI_EP_MULTIPLIER);
982 if (rx_stats->rs_rssi < 0)
983 rx_stats->rs_rssi = 0;
984
985 /* Update Beacon RSSI, this is used by ANI. */
986 if (ieee80211_is_beacon(fc))
987 ah->stats.avgbrssi = rx_stats->rs_rssi;
988}
989
990/*
991 * For Decrypt or Demic errors, we only mark packet status here and always push
992 * up the frame up to let mac80211 handle the actual error case, be it no
993 * decryption key or real decryption error. This let us keep statistics there.
994 */
995static int ath9k_rx_skb_preprocess(struct ath_common *common,
996 struct ieee80211_hw *hw,
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -0700997 struct ieee80211_hdr *hdr,
Sujithd4357002010-05-20 15:34:38 +0530998 struct ath_rx_status *rx_stats,
999 struct ieee80211_rx_status *rx_status,
1000 bool *decrypt_error)
1001{
Sujithd4357002010-05-20 15:34:38 +05301002 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
1003
1004 /*
1005 * everything but the rate is checked here, the rate check is done
1006 * separately to avoid doing two lookups for a rate for each frame.
1007 */
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -07001008 if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
Sujithd4357002010-05-20 15:34:38 +05301009 return -EINVAL;
1010
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -07001011 ath9k_process_rssi(common, hw, hdr, rx_stats);
Sujithd4357002010-05-20 15:34:38 +05301012
Vasanthakumar Thiagarajan9f167f62010-05-20 14:34:46 -07001013 if (ath9k_process_rate(common, hw, rx_stats, rx_status))
Sujithd4357002010-05-20 15:34:38 +05301014 return -EINVAL;
1015
Sujithd4357002010-05-20 15:34:38 +05301016 rx_status->band = hw->conf.channel->band;
1017 rx_status->freq = hw->conf.channel->center_freq;
1018 rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
1019 rx_status->antenna = rx_stats->rs_antenna;
1020 rx_status->flag |= RX_FLAG_TSFT;
1021
1022 return 0;
1023}
1024
1025static void ath9k_rx_skb_postprocess(struct ath_common *common,
1026 struct sk_buff *skb,
1027 struct ath_rx_status *rx_stats,
1028 struct ieee80211_rx_status *rxs,
1029 bool decrypt_error)
1030{
1031 struct ath_hw *ah = common->ah;
1032 struct ieee80211_hdr *hdr;
1033 int hdrlen, padpos, padsize;
1034 u8 keyix;
1035 __le16 fc;
1036
1037 /* see if any padding is done by the hw and remove it */
1038 hdr = (struct ieee80211_hdr *) skb->data;
1039 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1040 fc = hdr->frame_control;
1041 padpos = ath9k_cmn_padpos(hdr->frame_control);
1042
1043 /* The MAC header is padded to have 32-bit boundary if the
1044 * packet payload is non-zero. The general calculation for
1045 * padsize would take into account odd header lengths:
1046 * padsize = (4 - padpos % 4) % 4; However, since only
1047 * even-length headers are used, padding can only be 0 or 2
1048 * bytes and we can optimize this a bit. In addition, we must
1049 * not try to remove padding from short control frames that do
1050 * not have payload. */
1051 padsize = padpos & 3;
1052 if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1053 memmove(skb->data + padsize, skb->data, padpos);
1054 skb_pull(skb, padsize);
1055 }
1056
1057 keyix = rx_stats->rs_keyix;
1058
1059 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1060 ieee80211_has_protected(fc)) {
1061 rxs->flag |= RX_FLAG_DECRYPTED;
1062 } else if (ieee80211_has_protected(fc)
1063 && !decrypt_error && skb->len >= hdrlen + 4) {
1064 keyix = skb->data[hdrlen + 3] >> 6;
1065
1066 if (test_bit(keyix, common->keymap))
1067 rxs->flag |= RX_FLAG_DECRYPTED;
1068 }
1069 if (ah->sw_mgmt_crypto &&
1070 (rxs->flag & RX_FLAG_DECRYPTED) &&
1071 ieee80211_is_mgmt(fc))
1072 /* Use software decrypt for management frames. */
1073 rxs->flag &= ~RX_FLAG_DECRYPTED;
1074}
Felix Fietkaub5c804752010-04-15 17:38:48 -04001075
1076int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1077{
1078 struct ath_buf *bf;
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001079 struct sk_buff *skb = NULL, *requeue_skb;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001080 struct ieee80211_rx_status *rxs;
Sujithcbe61d82009-02-09 13:27:12 +05301081 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -07001082 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezb4afffc2009-11-02 11:36:08 -08001083 /*
1084 * The hw can techncically differ from common->hw when using ath9k
1085 * virtual wiphy so to account for that we iterate over the active
1086 * wiphys and find the appropriate wiphy and therefore hw.
1087 */
1088 struct ieee80211_hw *hw = NULL;
Sujithbe0418a2008-11-18 09:05:55 +05301089 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc9b14172009-11-04 16:47:22 -08001090 int retval;
Sujithbe0418a2008-11-18 09:05:55 +05301091 bool decrypt_error = false;
Felix Fietkau29bffa92010-03-29 20:14:23 -07001092 struct ath_rx_status rs;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001093 enum ath9k_rx_qtype qtype;
1094 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1095 int dma_type;
Vasanthakumar Thiagarajan5c6dd922010-05-20 14:34:47 -07001096 u8 rx_status_len = ah->caps.rx_status_len;
Felix Fietkaua6d20552010-06-12 00:33:54 -04001097 u64 tsf = 0;
1098 u32 tsf_lower = 0;
Sujithbe0418a2008-11-18 09:05:55 +05301099
Felix Fietkaub5c804752010-04-15 17:38:48 -04001100 if (edma)
Felix Fietkaub5c804752010-04-15 17:38:48 -04001101 dma_type = DMA_BIDIRECTIONAL;
Ming Lei56824222010-05-14 21:15:38 +08001102 else
1103 dma_type = DMA_FROM_DEVICE;
Felix Fietkaub5c804752010-04-15 17:38:48 -04001104
1105 qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
Sujithb77f4832008-12-07 21:44:03 +05301106 spin_lock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001107
Felix Fietkaua6d20552010-06-12 00:33:54 -04001108 tsf = ath9k_hw_gettsf64(ah);
1109 tsf_lower = tsf & 0xffffffff;
1110
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001111 do {
1112 /* If handling rx interrupt and flush is in progress => exit */
Sujith98deeea2008-08-11 14:05:46 +05301113 if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001114 break;
1115
Felix Fietkau29bffa92010-03-29 20:14:23 -07001116 memset(&rs, 0, sizeof(rs));
Felix Fietkaub5c804752010-04-15 17:38:48 -04001117 if (edma)
1118 bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1119 else
1120 bf = ath_get_next_rx_buf(sc, &rs);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001121
Felix Fietkaub5c804752010-04-15 17:38:48 -04001122 if (!bf)
1123 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001124
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001125 skb = bf->bf_mpdu;
Sujithbe0418a2008-11-18 09:05:55 +05301126 if (!skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001127 continue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001128
Vasanthakumar Thiagarajan5c6dd922010-05-20 14:34:47 -07001129 hdr = (struct ieee80211_hdr *) (skb->data + rx_status_len);
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001130 rxs = IEEE80211_SKB_RXCB(skb);
1131
Luis R. Rodriguezb4afffc2009-11-02 11:36:08 -08001132 hw = ath_get_virt_hw(sc, hdr);
1133
Felix Fietkau29bffa92010-03-29 20:14:23 -07001134 ath_debug_stat_rx(sc, &rs);
Sujith1395d3f2010-01-08 10:36:11 +05301135
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301136 /*
Sujithbe0418a2008-11-18 09:05:55 +05301137 * If we're asked to flush receive queue, directly
1138 * chain it back at the queue without processing it.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001139 */
Sujithbe0418a2008-11-18 09:05:55 +05301140 if (flush)
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001141 goto requeue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001142
Jan Friedrichc8f3b722010-08-02 23:55:50 +02001143 retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
1144 rxs, &decrypt_error);
1145 if (retval)
1146 goto requeue;
1147
Felix Fietkaua6d20552010-06-12 00:33:54 -04001148 rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1149 if (rs.rs_tstamp > tsf_lower &&
1150 unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1151 rxs->mactime -= 0x100000000ULL;
1152
1153 if (rs.rs_tstamp < tsf_lower &&
1154 unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1155 rxs->mactime += 0x100000000ULL;
1156
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001157 /* Ensure we always have an skb to requeue once we are done
1158 * processing the current buffer's skb */
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001159 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001160
1161 /* If there is no memory we ignore the current RX'd frame,
1162 * tell hardware it can give us a new frame using the old
Sujithb77f4832008-12-07 21:44:03 +05301163 * skb and put it at the tail of the sc->rx.rxbuf list for
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001164 * processing. */
1165 if (!requeue_skb)
1166 goto requeue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001167
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +05301168 /* Unmap the frame */
Gabor Juhos7da3c552009-01-14 20:17:03 +01001169 dma_unmap_single(sc->dev, bf->bf_buf_addr,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001170 common->rx_bufsize,
Felix Fietkaub5c804752010-04-15 17:38:48 -04001171 dma_type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001172
Felix Fietkaub5c804752010-04-15 17:38:48 -04001173 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1174 if (ah->caps.rx_status_len)
1175 skb_pull(skb, ah->caps.rx_status_len);
Sujithbe0418a2008-11-18 09:05:55 +05301176
Sujithd4357002010-05-20 15:34:38 +05301177 ath9k_rx_skb_postprocess(common, skb, &rs,
1178 rxs, decrypt_error);
Sujithbe0418a2008-11-18 09:05:55 +05301179
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001180 /* We will now give hardware our shiny new allocated skb */
1181 bf->bf_mpdu = requeue_skb;
Gabor Juhos7da3c552009-01-14 20:17:03 +01001182 bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001183 common->rx_bufsize,
Felix Fietkaub5c804752010-04-15 17:38:48 -04001184 dma_type);
Gabor Juhos7da3c552009-01-14 20:17:03 +01001185 if (unlikely(dma_mapping_error(sc->dev,
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001186 bf->bf_buf_addr))) {
1187 dev_kfree_skb_any(requeue_skb);
1188 bf->bf_mpdu = NULL;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001189 ath_print(common, ATH_DBG_FATAL,
1190 "dma_mapping_error() on RX\n");
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001191 ath_rx_send_to_mac80211(hw, sc, skb, rxs);
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -08001192 break;
1193 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001194 bf->bf_dmacontext = bf->bf_buf_addr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001195
1196 /*
1197 * change the default rx antenna if rx diversity chooses the
1198 * other antenna 3 times in a row.
1199 */
Felix Fietkau29bffa92010-03-29 20:14:23 -07001200 if (sc->rx.defant != rs.rs_antenna) {
Sujithb77f4832008-12-07 21:44:03 +05301201 if (++sc->rx.rxotherant >= 3)
Felix Fietkau29bffa92010-03-29 20:14:23 -07001202 ath_setdefantenna(sc, rs.rs_antenna);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001203 } else {
Sujithb77f4832008-12-07 21:44:03 +05301204 sc->rx.rxotherant = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001205 }
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301206
Vasanthakumar Thiagarajanededf1f2010-05-22 23:58:13 -07001207 if (unlikely(ath9k_check_auto_sleep(sc) ||
1208 (sc->ps_flags & (PS_WAIT_FOR_BEACON |
1209 PS_WAIT_FOR_CAB |
1210 PS_WAIT_FOR_PSPOLL_DATA))))
Jouni Malinencc659652009-05-14 21:28:48 +03001211 ath_rx_ps(sc, skb);
1212
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -08001213 ath_rx_send_to_mac80211(hw, sc, skb, rxs);
Jouni Malinencc659652009-05-14 21:28:48 +03001214
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -08001215requeue:
Felix Fietkaub5c804752010-04-15 17:38:48 -04001216 if (edma) {
1217 list_add_tail(&bf->list, &sc->rx.rxbuf);
1218 ath_rx_edma_buf_link(sc, qtype);
1219 } else {
1220 list_move_tail(&bf->list, &sc->rx.rxbuf);
1221 ath_rx_buf_link(sc, bf);
1222 }
Sujithbe0418a2008-11-18 09:05:55 +05301223 } while (1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001224
Sujithb77f4832008-12-07 21:44:03 +05301225 spin_unlock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001226
1227 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001228}