]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wireless/ath/ath9k/recv.c
ath5k: initialize eeprom struct early on attach
[net-next-2.6.git] / drivers / net / wireless / ath / ath9k / recv.c
CommitLineData
f078f209 1/*
cee075a2 2 * Copyright (c) 2008-2009 Atheros Communications Inc.
f078f209
LR
3 *
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
394cf0a1 17#include "ath9k.h"
f078f209 18
bce048d7
JM
19static struct ieee80211_hw * ath_get_virt_hw(struct ath_softc *sc,
20 struct ieee80211_hdr *hdr)
21{
c52f33d0
JM
22 struct ieee80211_hw *hw = sc->pri_wiphy->hw;
23 int i;
24
25 spin_lock_bh(&sc->wiphy_lock);
26 for (i = 0; i < sc->num_sec_wiphy; i++) {
27 struct ath_wiphy *aphy = sc->sec_wiphy[i];
28 if (aphy == NULL)
29 continue;
30 if (compare_ether_addr(hdr->addr1, aphy->hw->wiphy->perm_addr)
31 == 0) {
32 hw = aphy->hw;
33 break;
34 }
35 }
36 spin_unlock_bh(&sc->wiphy_lock);
37 return hw;
bce048d7
JM
38}
39
f078f209
LR
40/*
41 * Setup and link descriptors.
42 *
43 * 11N: we can no longer afford to self link the last descriptor.
44 * MAC acknowledges BA status as long as it copies frames to host
45 * buffer (or rx fifo). This can incorrectly acknowledge packets
46 * to a sender if last desc is self-linked.
f078f209 47 */
f078f209
LR
48static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
49{
cbe61d8a 50 struct ath_hw *ah = sc->sc_ah;
f078f209
LR
51 struct ath_desc *ds;
52 struct sk_buff *skb;
53
54 ATH_RXBUF_RESET(bf);
55
56 ds = bf->bf_desc;
be0418ad 57 ds->ds_link = 0; /* link to null */
f078f209
LR
58 ds->ds_data = bf->bf_buf_addr;
59
be0418ad 60 /* virtual addr of the beginning of the buffer. */
f078f209
LR
61 skb = bf->bf_mpdu;
62 ASSERT(skb != NULL);
63 ds->ds_vdata = skb->data;
64
b77f483f 65 /* setup rx descriptors. The rx.bufsize here tells the harware
b4b6cda2
LR
66 * how much data it can DMA to us and that we are prepared
67 * to process */
b77f483f
S
68 ath9k_hw_setuprxdesc(ah, ds,
69 sc->rx.bufsize,
f078f209
LR
70 0);
71
b77f483f 72 if (sc->rx.rxlink == NULL)
f078f209
LR
73 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
74 else
b77f483f 75 *sc->rx.rxlink = bf->bf_daddr;
f078f209 76
b77f483f 77 sc->rx.rxlink = &ds->ds_link;
f078f209
LR
78 ath9k_hw_rxena(ah);
79}
80
ff37e337
S
81static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
82{
83 /* XXX block beacon interrupts */
84 ath9k_hw_setantenna(sc->sc_ah, antenna);
b77f483f
S
85 sc->rx.defant = antenna;
86 sc->rx.rxotherant = 0;
ff37e337
S
87}
88
89/*
90 * Extend 15-bit time stamp from rx descriptor to
91 * a full 64-bit TSF using the current h/w TSF.
92*/
93static u64 ath_extend_tsf(struct ath_softc *sc, u32 rstamp)
94{
95 u64 tsf;
96
97 tsf = ath9k_hw_gettsf64(sc->sc_ah);
98 if ((tsf & 0x7fff) < rstamp)
99 tsf -= 0x8000;
100 return (tsf & ~0x7fff) | rstamp;
101}
102
f078f209 103/*
be0418ad
S
104 * For Decrypt or Demic errors, we only mark packet status here and always push
105 * up the frame up to let mac80211 handle the actual error case, be it no
106 * decryption key or real decryption error. This let us keep statistics there.
f078f209 107 */
be0418ad
S
108static int ath_rx_prepare(struct sk_buff *skb, struct ath_desc *ds,
109 struct ieee80211_rx_status *rx_status, bool *decrypt_error,
110 struct ath_softc *sc)
f078f209 111{
be0418ad 112 struct ieee80211_hdr *hdr;
be0418ad
S
113 u8 ratecode;
114 __le16 fc;
bce048d7 115 struct ieee80211_hw *hw;
a59b5a5e
SB
116 struct ieee80211_sta *sta;
117 struct ath_node *an;
118 int last_rssi = ATH_RSSI_DUMMY_MARKER;
119
be0418ad
S
120
121 hdr = (struct ieee80211_hdr *)skb->data;
122 fc = hdr->frame_control;
123 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
bce048d7 124 hw = ath_get_virt_hw(sc, hdr);
be0418ad
S
125
126 if (ds->ds_rxstat.rs_more) {
127 /*
128 * Frame spans multiple descriptors; this cannot happen yet
129 * as we don't support jumbograms. If not in monitor mode,
130 * discard the frame. Enable this if you want to see
131 * error frames in Monitor mode.
132 */
2660b81a 133 if (sc->sc_ah->opmode != NL80211_IFTYPE_MONITOR)
be0418ad
S
134 goto rx_next;
135 } else if (ds->ds_rxstat.rs_status != 0) {
136 if (ds->ds_rxstat.rs_status & ATH9K_RXERR_CRC)
137 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
138 if (ds->ds_rxstat.rs_status & ATH9K_RXERR_PHY)
139 goto rx_next;
f078f209 140
be0418ad
S
141 if (ds->ds_rxstat.rs_status & ATH9K_RXERR_DECRYPT) {
142 *decrypt_error = true;
143 } else if (ds->ds_rxstat.rs_status & ATH9K_RXERR_MIC) {
144 if (ieee80211_is_ctl(fc))
145 /*
146 * Sometimes, we get invalid
147 * MIC failures on valid control frames.
148 * Remove these mic errors.
149 */
150 ds->ds_rxstat.rs_status &= ~ATH9K_RXERR_MIC;
151 else
152 rx_status->flag |= RX_FLAG_MMIC_ERROR;
153 }
154 /*
155 * Reject error frames with the exception of
156 * decryption and MIC failures. For monitor mode,
157 * we also ignore the CRC error.
158 */
2660b81a 159 if (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR) {
be0418ad
S
160 if (ds->ds_rxstat.rs_status &
161 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
162 ATH9K_RXERR_CRC))
163 goto rx_next;
164 } else {
165 if (ds->ds_rxstat.rs_status &
166 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
167 goto rx_next;
168 }
169 }
f078f209
LR
170 }
171
be0418ad 172 ratecode = ds->ds_rxstat.rs_rate;
be0418ad 173
be0418ad 174 if (ratecode & 0x80) {
baad1d92
JM
175 /* HT rate */
176 rx_status->flag |= RX_FLAG_HT;
be0418ad 177 if (ds->ds_rxstat.rs_flags & ATH9K_RX_2040)
baad1d92 178 rx_status->flag |= RX_FLAG_40MHZ;
be0418ad 179 if (ds->ds_rxstat.rs_flags & ATH9K_RX_GI)
baad1d92
JM
180 rx_status->flag |= RX_FLAG_SHORT_GI;
181 rx_status->rate_idx = ratecode & 0x7f;
182 } else {
183 int i = 0, cur_band, n_rates;
baad1d92
JM
184
185 cur_band = hw->conf.channel->band;
186 n_rates = sc->sbands[cur_band].n_bitrates;
187
188 for (i = 0; i < n_rates; i++) {
189 if (sc->sbands[cur_band].bitrates[i].hw_value ==
190 ratecode) {
191 rx_status->rate_idx = i;
192 break;
193 }
194
195 if (sc->sbands[cur_band].bitrates[i].hw_value_short ==
196 ratecode) {
197 rx_status->rate_idx = i;
198 rx_status->flag |= RX_FLAG_SHORTPRE;
199 break;
200 }
201 }
be0418ad
S
202 }
203
a59b5a5e
SB
204 rcu_read_lock();
205 sta = ieee80211_find_sta(sc->hw, hdr->addr2);
206 if (sta) {
207 an = (struct ath_node *) sta->drv_priv;
208 if (ds->ds_rxstat.rs_rssi != ATH9K_RSSI_BAD &&
209 !ds->ds_rxstat.rs_moreaggr)
210 ATH_RSSI_LPF(an->last_rssi, ds->ds_rxstat.rs_rssi);
211 last_rssi = an->last_rssi;
212 }
213 rcu_read_unlock();
214
215 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
216 ds->ds_rxstat.rs_rssi = ATH_EP_RND(last_rssi,
217 ATH_RSSI_EP_MULTIPLIER);
218 if (ds->ds_rxstat.rs_rssi < 0)
219 ds->ds_rxstat.rs_rssi = 0;
220 else if (ds->ds_rxstat.rs_rssi > 127)
221 ds->ds_rxstat.rs_rssi = 127;
222
5e32b1ed
S
223 /* Update Beacon RSSI, this is used by ANI. */
224 if (ieee80211_is_beacon(fc))
22e66a4c 225 sc->sc_ah->stats.avgbrssi = ds->ds_rxstat.rs_rssi;
5e32b1ed 226
be0418ad 227 rx_status->mactime = ath_extend_tsf(sc, ds->ds_rxstat.rs_tstamp);
bce048d7
JM
228 rx_status->band = hw->conf.channel->band;
229 rx_status->freq = hw->conf.channel->center_freq;
17d7904d 230 rx_status->noise = sc->ani.noise_floor;
a59b5a5e 231 rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + ds->ds_rxstat.rs_rssi;
be0418ad
S
232 rx_status->antenna = ds->ds_rxstat.rs_antenna;
233
7d5ca3b8
LR
234 /*
235 * Theory for reporting quality:
236 *
237 * At a hardware RSSI of 45 you will be able to use MCS 7 reliably.
238 * At a hardware RSSI of 45 you will be able to use MCS 15 reliably.
239 * At a hardware RSSI of 35 you should be able use 54 Mbps reliably.
240 *
241 * MCS 7 is the highets MCS index usable by a 1-stream device.
242 * MCS 15 is the highest MCS index usable by a 2-stream device.
243 *
244 * All ath9k devices are either 1-stream or 2-stream.
245 *
246 * How many bars you see is derived from the qual reporting.
247 *
248 * A more elaborate scheme can be used here but it requires tables
249 * of SNR/throughput for each possible mode used. For the MCS table
250 * you can refer to the wireless wiki:
251 *
252 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
253 *
254 */
255 if (conf_is_ht(&hw->conf))
256 rx_status->qual = ds->ds_rxstat.rs_rssi * 100 / 45;
257 else
258 rx_status->qual = ds->ds_rxstat.rs_rssi * 100 / 35;
be0418ad
S
259
260 /* rssi can be more than 45 though, anything above that
261 * should be considered at 100% */
262 if (rx_status->qual > 100)
263 rx_status->qual = 100;
264
265 rx_status->flag |= RX_FLAG_TSFT;
266
267 return 1;
268rx_next:
269 return 0;
f078f209
LR
270}
271
272static void ath_opmode_init(struct ath_softc *sc)
273{
cbe61d8a 274 struct ath_hw *ah = sc->sc_ah;
1510718d
LR
275 struct ath_common *common = ath9k_hw_common(ah);
276
f078f209
LR
277 u32 rfilt, mfilt[2];
278
279 /* configure rx filter */
280 rfilt = ath_calcrxfilter(sc);
281 ath9k_hw_setrxfilter(ah, rfilt);
282
283 /* configure bssid mask */
2660b81a 284 if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
f2b2143e 285 ath9k_hw_setbssidmask(ah);
f078f209
LR
286
287 /* configure operational mode */
288 ath9k_hw_setopmode(ah);
289
290 /* Handle any link-level address change. */
1510718d 291 ath9k_hw_setmac(ah, common->macaddr);
f078f209
LR
292
293 /* calculate and install multicast filter */
294 mfilt[0] = mfilt[1] = ~0;
f078f209 295 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
f078f209
LR
296}
297
298int ath_rx_init(struct ath_softc *sc, int nbufs)
299{
300 struct sk_buff *skb;
301 struct ath_buf *bf;
302 int error = 0;
303
797fe5cb
S
304 spin_lock_init(&sc->rx.rxflushlock);
305 sc->sc_flags &= ~SC_OP_RXFLUSH;
306 spin_lock_init(&sc->rx.rxbuflock);
f078f209 307
797fe5cb 308 sc->rx.bufsize = roundup(IEEE80211_MAX_MPDU_LEN,
d15dd3e5 309 min(sc->common.cachelsz, (u16)64));
f078f209 310
4d6b228d 311 DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
d15dd3e5 312 sc->common.cachelsz, sc->rx.bufsize);
f078f209 313
797fe5cb 314 /* Initialize rx descriptors */
f078f209 315
797fe5cb
S
316 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
317 "rx", nbufs, 1);
318 if (error != 0) {
4d6b228d 319 DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
797fe5cb
S
320 "failed to allocate rx descriptors: %d\n", error);
321 goto err;
322 }
f078f209 323
797fe5cb 324 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
d15dd3e5 325 skb = ath_rxbuf_alloc(&sc->common, sc->rx.bufsize, GFP_KERNEL);
797fe5cb
S
326 if (skb == NULL) {
327 error = -ENOMEM;
328 goto err;
f078f209 329 }
f078f209 330
797fe5cb
S
331 bf->bf_mpdu = skb;
332 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
333 sc->rx.bufsize,
334 DMA_FROM_DEVICE);
335 if (unlikely(dma_mapping_error(sc->dev,
336 bf->bf_buf_addr))) {
337 dev_kfree_skb_any(skb);
338 bf->bf_mpdu = NULL;
4d6b228d 339 DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
797fe5cb
S
340 "dma_mapping_error() on RX init\n");
341 error = -ENOMEM;
342 goto err;
343 }
344 bf->bf_dmacontext = bf->bf_buf_addr;
345 }
346 sc->rx.rxlink = NULL;
f078f209 347
797fe5cb 348err:
f078f209
LR
349 if (error)
350 ath_rx_cleanup(sc);
351
352 return error;
353}
354
f078f209
LR
355void ath_rx_cleanup(struct ath_softc *sc)
356{
357 struct sk_buff *skb;
358 struct ath_buf *bf;
359
b77f483f 360 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
f078f209 361 skb = bf->bf_mpdu;
051b9191 362 if (skb) {
797fe5cb
S
363 dma_unmap_single(sc->dev, bf->bf_buf_addr,
364 sc->rx.bufsize, DMA_FROM_DEVICE);
f078f209 365 dev_kfree_skb(skb);
051b9191 366 }
f078f209
LR
367 }
368
b77f483f
S
369 if (sc->rx.rxdma.dd_desc_len != 0)
370 ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
f078f209
LR
371}
372
373/*
374 * Calculate the receive filter according to the
375 * operating mode and state:
376 *
377 * o always accept unicast, broadcast, and multicast traffic
378 * o maintain current state of phy error reception (the hal
379 * may enable phy error frames for noise immunity work)
380 * o probe request frames are accepted only when operating in
381 * hostap, adhoc, or monitor modes
382 * o enable promiscuous mode according to the interface state
383 * o accept beacons:
384 * - when operating in adhoc mode so the 802.11 layer creates
385 * node table entries for peers,
386 * - when operating in station mode for collecting rssi data when
387 * the station is otherwise quiet, or
388 * - when operating as a repeater so we see repeater-sta beacons
389 * - when scanning
390 */
391
392u32 ath_calcrxfilter(struct ath_softc *sc)
393{
394#define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
7dcfdcd9 395
f078f209
LR
396 u32 rfilt;
397
398 rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
399 | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
400 | ATH9K_RX_FILTER_MCAST;
401
402 /* If not a STA, enable processing of Probe Requests */
2660b81a 403 if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
f078f209
LR
404 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
405
217ba9da
JM
406 /*
407 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
408 * mode interface or when in monitor mode. AP mode does not need this
409 * since it receives all in-BSS frames anyway.
410 */
2660b81a 411 if (((sc->sc_ah->opmode != NL80211_IFTYPE_AP) &&
b77f483f 412 (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) ||
217ba9da 413 (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR))
f078f209 414 rfilt |= ATH9K_RX_FILTER_PROM;
f078f209 415
d42c6b71
S
416 if (sc->rx.rxfilter & FIF_CONTROL)
417 rfilt |= ATH9K_RX_FILTER_CONTROL;
418
dbaaa147
VT
419 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
420 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
421 rfilt |= ATH9K_RX_FILTER_MYBEACON;
422 else
f078f209
LR
423 rfilt |= ATH9K_RX_FILTER_BEACON;
424
63b08b8d 425 if (sc->rx.rxfilter & FIF_PSPOLL)
dbaaa147 426 rfilt |= ATH9K_RX_FILTER_PSPOLL;
be0418ad 427
7ea310be
S
428 if (conf_is_ht(&sc->hw->conf))
429 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
430
5eb6ba83 431 if (sc->sec_wiphy || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
b93bce2a
JM
432 /* TODO: only needed if more than one BSSID is in use in
433 * station/adhoc mode */
5eb6ba83
JC
434 /* The following may also be needed for other older chips */
435 if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
436 rfilt |= ATH9K_RX_FILTER_PROM;
b93bce2a
JM
437 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
438 }
439
f078f209 440 return rfilt;
7dcfdcd9 441
f078f209
LR
442#undef RX_FILTER_PRESERVE
443}
444
f078f209
LR
445int ath_startrecv(struct ath_softc *sc)
446{
cbe61d8a 447 struct ath_hw *ah = sc->sc_ah;
f078f209
LR
448 struct ath_buf *bf, *tbf;
449
b77f483f
S
450 spin_lock_bh(&sc->rx.rxbuflock);
451 if (list_empty(&sc->rx.rxbuf))
f078f209
LR
452 goto start_recv;
453
b77f483f
S
454 sc->rx.rxlink = NULL;
455 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
f078f209
LR
456 ath_rx_buf_link(sc, bf);
457 }
458
459 /* We could have deleted elements so the list may be empty now */
b77f483f 460 if (list_empty(&sc->rx.rxbuf))
f078f209
LR
461 goto start_recv;
462
b77f483f 463 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
f078f209 464 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
be0418ad 465 ath9k_hw_rxena(ah);
f078f209
LR
466
467start_recv:
b77f483f 468 spin_unlock_bh(&sc->rx.rxbuflock);
be0418ad
S
469 ath_opmode_init(sc);
470 ath9k_hw_startpcureceive(ah);
471
f078f209
LR
472 return 0;
473}
474
f078f209
LR
475bool ath_stoprecv(struct ath_softc *sc)
476{
cbe61d8a 477 struct ath_hw *ah = sc->sc_ah;
f078f209
LR
478 bool stopped;
479
be0418ad
S
480 ath9k_hw_stoppcurecv(ah);
481 ath9k_hw_setrxfilter(ah, 0);
482 stopped = ath9k_hw_stopdmarecv(ah);
b77f483f 483 sc->rx.rxlink = NULL;
be0418ad 484
f078f209
LR
485 return stopped;
486}
487
f078f209
LR
488void ath_flushrecv(struct ath_softc *sc)
489{
b77f483f 490 spin_lock_bh(&sc->rx.rxflushlock);
98deeea0 491 sc->sc_flags |= SC_OP_RXFLUSH;
f078f209 492 ath_rx_tasklet(sc, 1);
98deeea0 493 sc->sc_flags &= ~SC_OP_RXFLUSH;
b77f483f 494 spin_unlock_bh(&sc->rx.rxflushlock);
f078f209
LR
495}
496
cc65965c
JM
497static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
498{
499 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
500 struct ieee80211_mgmt *mgmt;
501 u8 *pos, *end, id, elen;
502 struct ieee80211_tim_ie *tim;
503
504 mgmt = (struct ieee80211_mgmt *)skb->data;
505 pos = mgmt->u.beacon.variable;
506 end = skb->data + skb->len;
507
508 while (pos + 2 < end) {
509 id = *pos++;
510 elen = *pos++;
511 if (pos + elen > end)
512 break;
513
514 if (id == WLAN_EID_TIM) {
515 if (elen < sizeof(*tim))
516 break;
517 tim = (struct ieee80211_tim_ie *) pos;
518 if (tim->dtim_count != 0)
519 break;
520 return tim->bitmap_ctrl & 0x01;
521 }
522
523 pos += elen;
524 }
525
526 return false;
527}
528
cc65965c
JM
529static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
530{
531 struct ieee80211_mgmt *mgmt;
1510718d 532 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
cc65965c
JM
533
534 if (skb->len < 24 + 8 + 2 + 2)
535 return;
536
537 mgmt = (struct ieee80211_mgmt *)skb->data;
1510718d 538 if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0)
cc65965c
JM
539 return; /* not from our current AP */
540
293dc5df
GJ
541 sc->sc_flags &= ~SC_OP_WAIT_FOR_BEACON;
542
ccdfeab6
JM
543 if (sc->sc_flags & SC_OP_BEACON_SYNC) {
544 sc->sc_flags &= ~SC_OP_BEACON_SYNC;
4d6b228d 545 DPRINTF(sc->sc_ah, ATH_DBG_PS, "Reconfigure Beacon timers based on "
ccdfeab6
JM
546 "timestamp from the AP\n");
547 ath_beacon_config(sc, NULL);
548 }
549
cc65965c
JM
550 if (ath_beacon_dtim_pending_cab(skb)) {
551 /*
552 * Remain awake waiting for buffered broadcast/multicast
58f5fffd
GJ
553 * frames. If the last broadcast/multicast frame is not
554 * received properly, the next beacon frame will work as
555 * a backup trigger for returning into NETWORK SLEEP state,
556 * so we are waiting for it as well.
cc65965c 557 */
4d6b228d 558 DPRINTF(sc->sc_ah, ATH_DBG_PS, "Received DTIM beacon indicating "
cc65965c 559 "buffered broadcast/multicast frame(s)\n");
58f5fffd 560 sc->sc_flags |= SC_OP_WAIT_FOR_CAB | SC_OP_WAIT_FOR_BEACON;
cc65965c
JM
561 return;
562 }
563
564 if (sc->sc_flags & SC_OP_WAIT_FOR_CAB) {
565 /*
566 * This can happen if a broadcast frame is dropped or the AP
567 * fails to send a frame indicating that all CAB frames have
568 * been delivered.
569 */
293dc5df 570 sc->sc_flags &= ~SC_OP_WAIT_FOR_CAB;
4d6b228d 571 DPRINTF(sc->sc_ah, ATH_DBG_PS, "PS wait for CAB frames timed out\n");
cc65965c 572 }
cc65965c
JM
573}
574
575static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb)
576{
577 struct ieee80211_hdr *hdr;
578
579 hdr = (struct ieee80211_hdr *)skb->data;
580
581 /* Process Beacon and CAB receive in PS state */
9a23f9ca
JM
582 if ((sc->sc_flags & SC_OP_WAIT_FOR_BEACON) &&
583 ieee80211_is_beacon(hdr->frame_control))
cc65965c
JM
584 ath_rx_ps_beacon(sc, skb);
585 else if ((sc->sc_flags & SC_OP_WAIT_FOR_CAB) &&
586 (ieee80211_is_data(hdr->frame_control) ||
587 ieee80211_is_action(hdr->frame_control)) &&
588 is_multicast_ether_addr(hdr->addr1) &&
589 !ieee80211_has_moredata(hdr->frame_control)) {
cc65965c
JM
590 /*
591 * No more broadcast/multicast frames to be received at this
592 * point.
593 */
293dc5df 594 sc->sc_flags &= ~SC_OP_WAIT_FOR_CAB;
4d6b228d 595 DPRINTF(sc->sc_ah, ATH_DBG_PS, "All PS CAB frames received, back to "
293dc5df 596 "sleep\n");
9a23f9ca
JM
597 } else if ((sc->sc_flags & SC_OP_WAIT_FOR_PSPOLL_DATA) &&
598 !is_multicast_ether_addr(hdr->addr1) &&
599 !ieee80211_has_morefrags(hdr->frame_control)) {
600 sc->sc_flags &= ~SC_OP_WAIT_FOR_PSPOLL_DATA;
4d6b228d 601 DPRINTF(sc->sc_ah, ATH_DBG_PS, "Going back to sleep after having "
9a23f9ca
JM
602 "received PS-Poll data (0x%x)\n",
603 sc->sc_flags & (SC_OP_WAIT_FOR_BEACON |
604 SC_OP_WAIT_FOR_CAB |
605 SC_OP_WAIT_FOR_PSPOLL_DATA |
606 SC_OP_WAIT_FOR_TX_ACK));
cc65965c
JM
607 }
608}
609
9d64a3cf
JM
610static void ath_rx_send_to_mac80211(struct ath_softc *sc, struct sk_buff *skb,
611 struct ieee80211_rx_status *rx_status)
612{
613 struct ieee80211_hdr *hdr;
614
615 hdr = (struct ieee80211_hdr *)skb->data;
616
617 /* Send the frame to mac80211 */
618 if (is_multicast_ether_addr(hdr->addr1)) {
619 int i;
620 /*
621 * Deliver broadcast/multicast frames to all suitable
622 * virtual wiphys.
623 */
624 /* TODO: filter based on channel configuration */
625 for (i = 0; i < sc->num_sec_wiphy; i++) {
626 struct ath_wiphy *aphy = sc->sec_wiphy[i];
627 struct sk_buff *nskb;
628 if (aphy == NULL)
629 continue;
630 nskb = skb_copy(skb, GFP_ATOMIC);
f1d58c25
JB
631 if (nskb) {
632 memcpy(IEEE80211_SKB_RXCB(nskb), rx_status,
633 sizeof(*rx_status));
634 ieee80211_rx(aphy->hw, nskb);
635 }
9d64a3cf 636 }
f1d58c25
JB
637 memcpy(IEEE80211_SKB_RXCB(skb), rx_status, sizeof(*rx_status));
638 ieee80211_rx(sc->hw, skb);
9d64a3cf
JM
639 } else {
640 /* Deliver unicast frames based on receiver address */
f1d58c25
JB
641 memcpy(IEEE80211_SKB_RXCB(skb), rx_status, sizeof(*rx_status));
642 ieee80211_rx(ath_get_virt_hw(sc, hdr), skb);
9d64a3cf
JM
643 }
644}
645
f078f209
LR
646int ath_rx_tasklet(struct ath_softc *sc, int flush)
647{
648#define PA2DESC(_sc, _pa) \
b77f483f
S
649 ((struct ath_desc *)((caddr_t)(_sc)->rx.rxdma.dd_desc + \
650 ((_pa) - (_sc)->rx.rxdma.dd_desc_paddr)))
f078f209 651
be0418ad 652 struct ath_buf *bf;
f078f209 653 struct ath_desc *ds;
cb71d9ba 654 struct sk_buff *skb = NULL, *requeue_skb;
be0418ad 655 struct ieee80211_rx_status rx_status;
cbe61d8a 656 struct ath_hw *ah = sc->sc_ah;
be0418ad
S
657 struct ieee80211_hdr *hdr;
658 int hdrlen, padsize, retval;
659 bool decrypt_error = false;
660 u8 keyix;
853da11b 661 __le16 fc;
be0418ad 662
b77f483f 663 spin_lock_bh(&sc->rx.rxbuflock);
f078f209
LR
664
665 do {
666 /* If handling rx interrupt and flush is in progress => exit */
98deeea0 667 if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
f078f209
LR
668 break;
669
b77f483f
S
670 if (list_empty(&sc->rx.rxbuf)) {
671 sc->rx.rxlink = NULL;
f078f209
LR
672 break;
673 }
674
b77f483f 675 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
f078f209 676 ds = bf->bf_desc;
f078f209
LR
677
678 /*
679 * Must provide the virtual address of the current
680 * descriptor, the physical address, and the virtual
681 * address of the next descriptor in the h/w chain.
682 * This allows the HAL to look ahead to see if the
683 * hardware is done with a descriptor by checking the
684 * done bit in the following descriptor and the address
685 * of the current descriptor the DMA engine is working
686 * on. All this is necessary because of our use of
687 * a self-linked list to avoid rx overruns.
688 */
be0418ad 689 retval = ath9k_hw_rxprocdesc(ah, ds,
f078f209
LR
690 bf->bf_daddr,
691 PA2DESC(sc, ds->ds_link),
692 0);
693 if (retval == -EINPROGRESS) {
694 struct ath_buf *tbf;
695 struct ath_desc *tds;
696
b77f483f
S
697 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
698 sc->rx.rxlink = NULL;
f078f209
LR
699 break;
700 }
701
702 tbf = list_entry(bf->list.next, struct ath_buf, list);
703
704 /*
705 * On some hardware the descriptor status words could
706 * get corrupted, including the done bit. Because of
707 * this, check if the next descriptor's done bit is
708 * set or not.
709 *
710 * If the next descriptor's done bit is set, the current
711 * descriptor has been corrupted. Force s/w to discard
712 * this descriptor and continue...
713 */
714
715 tds = tbf->bf_desc;
be0418ad
S
716 retval = ath9k_hw_rxprocdesc(ah, tds, tbf->bf_daddr,
717 PA2DESC(sc, tds->ds_link), 0);
f078f209 718 if (retval == -EINPROGRESS) {
f078f209
LR
719 break;
720 }
721 }
722
f078f209 723 skb = bf->bf_mpdu;
be0418ad 724 if (!skb)
f078f209 725 continue;
f078f209 726
9bf9fca8
VT
727 /*
728 * Synchronize the DMA transfer with CPU before
729 * 1. accessing the frame
730 * 2. requeueing the same buffer to h/w
731 */
7da3c55c 732 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
9bf9fca8 733 sc->rx.bufsize,
7da3c55c 734 DMA_FROM_DEVICE);
9bf9fca8 735
f078f209 736 /*
be0418ad
S
737 * If we're asked to flush receive queue, directly
738 * chain it back at the queue without processing it.
f078f209 739 */
be0418ad 740 if (flush)
cb71d9ba 741 goto requeue;
f078f209 742
be0418ad 743 if (!ds->ds_rxstat.rs_datalen)
cb71d9ba 744 goto requeue;
f078f209 745
be0418ad 746 /* The status portion of the descriptor could get corrupted. */
b77f483f 747 if (sc->rx.bufsize < ds->ds_rxstat.rs_datalen)
cb71d9ba 748 goto requeue;
f078f209 749
be0418ad 750 if (!ath_rx_prepare(skb, ds, &rx_status, &decrypt_error, sc))
cb71d9ba
LR
751 goto requeue;
752
753 /* Ensure we always have an skb to requeue once we are done
754 * processing the current buffer's skb */
d15dd3e5 755 requeue_skb = ath_rxbuf_alloc(&sc->common, sc->rx.bufsize, GFP_ATOMIC);
cb71d9ba
LR
756
757 /* If there is no memory we ignore the current RX'd frame,
758 * tell hardware it can give us a new frame using the old
b77f483f 759 * skb and put it at the tail of the sc->rx.rxbuf list for
cb71d9ba
LR
760 * processing. */
761 if (!requeue_skb)
762 goto requeue;
f078f209 763
9bf9fca8 764 /* Unmap the frame */
7da3c55c 765 dma_unmap_single(sc->dev, bf->bf_buf_addr,
b77f483f 766 sc->rx.bufsize,
7da3c55c 767 DMA_FROM_DEVICE);
f078f209 768
be0418ad 769 skb_put(skb, ds->ds_rxstat.rs_datalen);
be0418ad
S
770
771 /* see if any padding is done by the hw and remove it */
772 hdr = (struct ieee80211_hdr *)skb->data;
773 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
853da11b 774 fc = hdr->frame_control;
be0418ad 775
9c5f89b3
JM
776 /* The MAC header is padded to have 32-bit boundary if the
777 * packet payload is non-zero. The general calculation for
778 * padsize would take into account odd header lengths:
779 * padsize = (4 - hdrlen % 4) % 4; However, since only
780 * even-length headers are used, padding can only be 0 or 2
781 * bytes and we can optimize this a bit. In addition, we must
782 * not try to remove padding from short control frames that do
783 * not have payload. */
784 padsize = hdrlen & 3;
785 if (padsize && hdrlen >= 24) {
be0418ad
S
786 memmove(skb->data + padsize, skb->data, hdrlen);
787 skb_pull(skb, padsize);
f078f209
LR
788 }
789
be0418ad 790 keyix = ds->ds_rxstat.rs_keyix;
f078f209 791
be0418ad
S
792 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error) {
793 rx_status.flag |= RX_FLAG_DECRYPTED;
9d64a3cf 794 } else if (ieee80211_has_protected(fc)
be0418ad
S
795 && !decrypt_error && skb->len >= hdrlen + 4) {
796 keyix = skb->data[hdrlen + 3] >> 6;
797
17d7904d 798 if (test_bit(keyix, sc->keymap))
be0418ad
S
799 rx_status.flag |= RX_FLAG_DECRYPTED;
800 }
0ced0e17
JM
801 if (ah->sw_mgmt_crypto &&
802 (rx_status.flag & RX_FLAG_DECRYPTED) &&
9d64a3cf 803 ieee80211_is_mgmt(fc)) {
0ced0e17
JM
804 /* Use software decrypt for management frames. */
805 rx_status.flag &= ~RX_FLAG_DECRYPTED;
806 }
be0418ad 807
cb71d9ba
LR
808 /* We will now give hardware our shiny new allocated skb */
809 bf->bf_mpdu = requeue_skb;
7da3c55c 810 bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
b77f483f 811 sc->rx.bufsize,
7da3c55c
GJ
812 DMA_FROM_DEVICE);
813 if (unlikely(dma_mapping_error(sc->dev,
f8316df1
LR
814 bf->bf_buf_addr))) {
815 dev_kfree_skb_any(requeue_skb);
816 bf->bf_mpdu = NULL;
4d6b228d 817 DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
7da3c55c 818 "dma_mapping_error() on RX\n");
cc65965c 819 ath_rx_send_to_mac80211(sc, skb, &rx_status);
f8316df1
LR
820 break;
821 }
cb71d9ba 822 bf->bf_dmacontext = bf->bf_buf_addr;
f078f209
LR
823
824 /*
825 * change the default rx antenna if rx diversity chooses the
826 * other antenna 3 times in a row.
827 */
b77f483f
S
828 if (sc->rx.defant != ds->ds_rxstat.rs_antenna) {
829 if (++sc->rx.rxotherant >= 3)
be0418ad 830 ath_setdefantenna(sc, ds->ds_rxstat.rs_antenna);
f078f209 831 } else {
b77f483f 832 sc->rx.rxotherant = 0;
f078f209 833 }
3cbb5dd7 834
9a23f9ca 835 if (unlikely(sc->sc_flags & (SC_OP_WAIT_FOR_BEACON |
f0e9a860 836 SC_OP_WAIT_FOR_CAB |
9a23f9ca 837 SC_OP_WAIT_FOR_PSPOLL_DATA)))
cc65965c
JM
838 ath_rx_ps(sc, skb);
839
840 ath_rx_send_to_mac80211(sc, skb, &rx_status);
841
cb71d9ba 842requeue:
b77f483f 843 list_move_tail(&bf->list, &sc->rx.rxbuf);
cb71d9ba 844 ath_rx_buf_link(sc, bf);
be0418ad
S
845 } while (1);
846
b77f483f 847 spin_unlock_bh(&sc->rx.rxbuflock);
f078f209
LR
848
849 return 0;
850#undef PA2DESC
851}