blob: 28ad1d5af129aec5cc42e1ece1265393db06699e [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
2 * Copyright (c) 2008 Atheros Communications Inc.
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
Sujith394cf0a2009-02-09 13:26:54 +053017#include "ath9k.h"
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070018
19/*
20 * Setup and link descriptors.
21 *
22 * 11N: we can no longer afford to self link the last descriptor.
23 * MAC acknowledges BA status as long as it copies frames to host
24 * buffer (or rx fifo). This can incorrectly acknowledge packets
25 * to a sender if last desc is self-linked.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070026 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070027static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
28{
Sujithcbe61d82009-02-09 13:27:12 +053029 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070030 struct ath_desc *ds;
31 struct sk_buff *skb;
32
33 ATH_RXBUF_RESET(bf);
34
35 ds = bf->bf_desc;
Sujithbe0418a2008-11-18 09:05:55 +053036 ds->ds_link = 0; /* link to null */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070037 ds->ds_data = bf->bf_buf_addr;
38
Sujithbe0418a2008-11-18 09:05:55 +053039 /* virtual addr of the beginning of the buffer. */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070040 skb = bf->bf_mpdu;
41 ASSERT(skb != NULL);
42 ds->ds_vdata = skb->data;
43
Sujithb77f4832008-12-07 21:44:03 +053044 /* setup rx descriptors. The rx.bufsize here tells the harware
Luis R. Rodriguezb4b6cda2008-11-20 17:15:13 -080045 * how much data it can DMA to us and that we are prepared
46 * to process */
Sujithb77f4832008-12-07 21:44:03 +053047 ath9k_hw_setuprxdesc(ah, ds,
48 sc->rx.bufsize,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070049 0);
50
Sujithb77f4832008-12-07 21:44:03 +053051 if (sc->rx.rxlink == NULL)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070052 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
53 else
Sujithb77f4832008-12-07 21:44:03 +053054 *sc->rx.rxlink = bf->bf_daddr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070055
Sujithb77f4832008-12-07 21:44:03 +053056 sc->rx.rxlink = &ds->ds_link;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070057 ath9k_hw_rxena(ah);
58}
59
Sujithff37e332008-11-24 12:07:55 +053060static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
61{
62 /* XXX block beacon interrupts */
63 ath9k_hw_setantenna(sc->sc_ah, antenna);
Sujithb77f4832008-12-07 21:44:03 +053064 sc->rx.defant = antenna;
65 sc->rx.rxotherant = 0;
Sujithff37e332008-11-24 12:07:55 +053066}
67
68/*
69 * Extend 15-bit time stamp from rx descriptor to
70 * a full 64-bit TSF using the current h/w TSF.
71*/
72static u64 ath_extend_tsf(struct ath_softc *sc, u32 rstamp)
73{
74 u64 tsf;
75
76 tsf = ath9k_hw_gettsf64(sc->sc_ah);
77 if ((tsf & 0x7fff) < rstamp)
78 tsf -= 0x8000;
79 return (tsf & ~0x7fff) | rstamp;
80}
81
Sujithbe0418a2008-11-18 09:05:55 +053082static struct sk_buff *ath_rxbuf_alloc(struct ath_softc *sc, u32 len)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070083{
84 struct sk_buff *skb;
85 u32 off;
86
87 /*
88 * Cache-line-align. This is important (for the
89 * 5210 at least) as not doing so causes bogus data
90 * in rx'd frames.
91 */
92
Luis R. Rodriguezb4b6cda2008-11-20 17:15:13 -080093 /* Note: the kernel can allocate a value greater than
94 * what we ask it to give us. We really only need 4 KB as that
95 * is this hardware supports and in fact we need at least 3849
96 * as that is the MAX AMSDU size this hardware supports.
97 * Unfortunately this means we may get 8 KB here from the
98 * kernel... and that is actually what is observed on some
99 * systems :( */
Sujith17d79042009-02-09 13:27:03 +0530100 skb = dev_alloc_skb(len + sc->cachelsz - 1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700101 if (skb != NULL) {
Sujith17d79042009-02-09 13:27:03 +0530102 off = ((unsigned long) skb->data) % sc->cachelsz;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700103 if (off != 0)
Sujith17d79042009-02-09 13:27:03 +0530104 skb_reserve(skb, sc->cachelsz - off);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700105 } else {
106 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +0530107 "skbuff alloc of size %u failed\n", len);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700108 return NULL;
109 }
110
111 return skb;
112}
113
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700114/*
Sujithbe0418a2008-11-18 09:05:55 +0530115 * For Decrypt or Demic errors, we only mark packet status here and always push
116 * up the frame up to let mac80211 handle the actual error case, be it no
117 * decryption key or real decryption error. This let us keep statistics there.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700118 */
Sujithbe0418a2008-11-18 09:05:55 +0530119static int ath_rx_prepare(struct sk_buff *skb, struct ath_desc *ds,
120 struct ieee80211_rx_status *rx_status, bool *decrypt_error,
121 struct ath_softc *sc)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700122{
Sujithbe0418a2008-11-18 09:05:55 +0530123 struct ieee80211_hdr *hdr;
Sujithbe0418a2008-11-18 09:05:55 +0530124 u8 ratecode;
125 __le16 fc;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700126
Sujithbe0418a2008-11-18 09:05:55 +0530127 hdr = (struct ieee80211_hdr *)skb->data;
128 fc = hdr->frame_control;
129 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700130
Sujithbe0418a2008-11-18 09:05:55 +0530131 if (ds->ds_rxstat.rs_more) {
132 /*
133 * Frame spans multiple descriptors; this cannot happen yet
134 * as we don't support jumbograms. If not in monitor mode,
135 * discard the frame. Enable this if you want to see
136 * error frames in Monitor mode.
137 */
Sujith2660b812009-02-09 13:27:26 +0530138 if (sc->sc_ah->opmode != NL80211_IFTYPE_MONITOR)
Sujithbe0418a2008-11-18 09:05:55 +0530139 goto rx_next;
140 } else if (ds->ds_rxstat.rs_status != 0) {
141 if (ds->ds_rxstat.rs_status & ATH9K_RXERR_CRC)
142 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
143 if (ds->ds_rxstat.rs_status & ATH9K_RXERR_PHY)
144 goto rx_next;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700145
Sujithbe0418a2008-11-18 09:05:55 +0530146 if (ds->ds_rxstat.rs_status & ATH9K_RXERR_DECRYPT) {
147 *decrypt_error = true;
148 } else if (ds->ds_rxstat.rs_status & ATH9K_RXERR_MIC) {
149 if (ieee80211_is_ctl(fc))
150 /*
151 * Sometimes, we get invalid
152 * MIC failures on valid control frames.
153 * Remove these mic errors.
154 */
155 ds->ds_rxstat.rs_status &= ~ATH9K_RXERR_MIC;
156 else
157 rx_status->flag |= RX_FLAG_MMIC_ERROR;
158 }
159 /*
160 * Reject error frames with the exception of
161 * decryption and MIC failures. For monitor mode,
162 * we also ignore the CRC error.
163 */
Sujith2660b812009-02-09 13:27:26 +0530164 if (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR) {
Sujithbe0418a2008-11-18 09:05:55 +0530165 if (ds->ds_rxstat.rs_status &
166 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
167 ATH9K_RXERR_CRC))
168 goto rx_next;
169 } else {
170 if (ds->ds_rxstat.rs_status &
171 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
172 goto rx_next;
173 }
174 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700175 }
176
Sujithbe0418a2008-11-18 09:05:55 +0530177 ratecode = ds->ds_rxstat.rs_rate;
Sujithbe0418a2008-11-18 09:05:55 +0530178
Sujithbe0418a2008-11-18 09:05:55 +0530179 if (ratecode & 0x80) {
Jouni Malinenbaad1d92008-12-12 14:38:34 +0200180 /* HT rate */
181 rx_status->flag |= RX_FLAG_HT;
Sujithbe0418a2008-11-18 09:05:55 +0530182 if (ds->ds_rxstat.rs_flags & ATH9K_RX_2040)
Jouni Malinenbaad1d92008-12-12 14:38:34 +0200183 rx_status->flag |= RX_FLAG_40MHZ;
Sujithbe0418a2008-11-18 09:05:55 +0530184 if (ds->ds_rxstat.rs_flags & ATH9K_RX_GI)
Jouni Malinenbaad1d92008-12-12 14:38:34 +0200185 rx_status->flag |= RX_FLAG_SHORT_GI;
186 rx_status->rate_idx = ratecode & 0x7f;
187 } else {
188 int i = 0, cur_band, n_rates;
189 struct ieee80211_hw *hw = sc->hw;
190
191 cur_band = hw->conf.channel->band;
192 n_rates = sc->sbands[cur_band].n_bitrates;
193
194 for (i = 0; i < n_rates; i++) {
195 if (sc->sbands[cur_band].bitrates[i].hw_value ==
196 ratecode) {
197 rx_status->rate_idx = i;
198 break;
199 }
200
201 if (sc->sbands[cur_band].bitrates[i].hw_value_short ==
202 ratecode) {
203 rx_status->rate_idx = i;
204 rx_status->flag |= RX_FLAG_SHORTPRE;
205 break;
206 }
207 }
Sujithbe0418a2008-11-18 09:05:55 +0530208 }
209
210 rx_status->mactime = ath_extend_tsf(sc, ds->ds_rxstat.rs_tstamp);
211 rx_status->band = sc->hw->conf.channel->band;
212 rx_status->freq = sc->hw->conf.channel->center_freq;
Sujith17d79042009-02-09 13:27:03 +0530213 rx_status->noise = sc->ani.noise_floor;
Sujithbe0418a2008-11-18 09:05:55 +0530214 rx_status->signal = rx_status->noise + ds->ds_rxstat.rs_rssi;
Sujithbe0418a2008-11-18 09:05:55 +0530215 rx_status->antenna = ds->ds_rxstat.rs_antenna;
216
217 /* at 45 you will be able to use MCS 15 reliably. A more elaborate
218 * scheme can be used here but it requires tables of SNR/throughput for
219 * each possible mode used. */
220 rx_status->qual = ds->ds_rxstat.rs_rssi * 100 / 45;
221
222 /* rssi can be more than 45 though, anything above that
223 * should be considered at 100% */
224 if (rx_status->qual > 100)
225 rx_status->qual = 100;
226
227 rx_status->flag |= RX_FLAG_TSFT;
228
229 return 1;
230rx_next:
231 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700232}
233
234static void ath_opmode_init(struct ath_softc *sc)
235{
Sujithcbe61d82009-02-09 13:27:12 +0530236 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700237 u32 rfilt, mfilt[2];
238
239 /* configure rx filter */
240 rfilt = ath_calcrxfilter(sc);
241 ath9k_hw_setrxfilter(ah, rfilt);
242
243 /* configure bssid mask */
Sujith2660b812009-02-09 13:27:26 +0530244 if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
Sujithba52da52009-02-09 13:27:10 +0530245 ath9k_hw_setbssidmask(sc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700246
247 /* configure operational mode */
248 ath9k_hw_setopmode(ah);
249
250 /* Handle any link-level address change. */
Sujithba52da52009-02-09 13:27:10 +0530251 ath9k_hw_setmac(ah, sc->sc_ah->macaddr);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700252
253 /* calculate and install multicast filter */
254 mfilt[0] = mfilt[1] = ~0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700255 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700256}
257
258int ath_rx_init(struct ath_softc *sc, int nbufs)
259{
260 struct sk_buff *skb;
261 struct ath_buf *bf;
262 int error = 0;
263
264 do {
Sujithb77f4832008-12-07 21:44:03 +0530265 spin_lock_init(&sc->rx.rxflushlock);
Sujith98deeea2008-08-11 14:05:46 +0530266 sc->sc_flags &= ~SC_OP_RXFLUSH;
Sujithb77f4832008-12-07 21:44:03 +0530267 spin_lock_init(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700268
Sujithb77f4832008-12-07 21:44:03 +0530269 sc->rx.bufsize = roundup(IEEE80211_MAX_MPDU_LEN,
Sujith17d79042009-02-09 13:27:03 +0530270 min(sc->cachelsz,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700271 (u16)64));
272
Sujith04bd46382008-11-28 22:18:05 +0530273 DPRINTF(sc, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
Sujith17d79042009-02-09 13:27:03 +0530274 sc->cachelsz, sc->rx.bufsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700275
276 /* Initialize rx descriptors */
277
Sujithb77f4832008-12-07 21:44:03 +0530278 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700279 "rx", nbufs, 1);
280 if (error != 0) {
281 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +0530282 "failed to allocate rx descriptors: %d\n", error);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700283 break;
284 }
285
Sujithb77f4832008-12-07 21:44:03 +0530286 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
287 skb = ath_rxbuf_alloc(sc, sc->rx.bufsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700288 if (skb == NULL) {
289 error = -ENOMEM;
290 break;
291 }
292
293 bf->bf_mpdu = skb;
Gabor Juhos7da3c552009-01-14 20:17:03 +0100294 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
Sujithb77f4832008-12-07 21:44:03 +0530295 sc->rx.bufsize,
Gabor Juhos7da3c552009-01-14 20:17:03 +0100296 DMA_FROM_DEVICE);
297 if (unlikely(dma_mapping_error(sc->dev,
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -0800298 bf->bf_buf_addr))) {
299 dev_kfree_skb_any(skb);
300 bf->bf_mpdu = NULL;
301 DPRINTF(sc, ATH_DBG_CONFIG,
Gabor Juhos7da3c552009-01-14 20:17:03 +0100302 "dma_mapping_error() on RX init\n");
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -0800303 error = -ENOMEM;
304 break;
305 }
Sujith927e70e2008-08-14 13:26:34 +0530306 bf->bf_dmacontext = bf->bf_buf_addr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700307 }
Sujithb77f4832008-12-07 21:44:03 +0530308 sc->rx.rxlink = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700309
310 } while (0);
311
312 if (error)
313 ath_rx_cleanup(sc);
314
315 return error;
316}
317
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700318void ath_rx_cleanup(struct ath_softc *sc)
319{
320 struct sk_buff *skb;
321 struct ath_buf *bf;
322
Sujithb77f4832008-12-07 21:44:03 +0530323 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700324 skb = bf->bf_mpdu;
325 if (skb)
326 dev_kfree_skb(skb);
327 }
328
Sujithb77f4832008-12-07 21:44:03 +0530329 if (sc->rx.rxdma.dd_desc_len != 0)
330 ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700331}
332
333/*
334 * Calculate the receive filter according to the
335 * operating mode and state:
336 *
337 * o always accept unicast, broadcast, and multicast traffic
338 * o maintain current state of phy error reception (the hal
339 * may enable phy error frames for noise immunity work)
340 * o probe request frames are accepted only when operating in
341 * hostap, adhoc, or monitor modes
342 * o enable promiscuous mode according to the interface state
343 * o accept beacons:
344 * - when operating in adhoc mode so the 802.11 layer creates
345 * node table entries for peers,
346 * - when operating in station mode for collecting rssi data when
347 * the station is otherwise quiet, or
348 * - when operating as a repeater so we see repeater-sta beacons
349 * - when scanning
350 */
351
352u32 ath_calcrxfilter(struct ath_softc *sc)
353{
354#define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
Sujith7dcfdcd2008-08-11 14:03:13 +0530355
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700356 u32 rfilt;
357
358 rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
359 | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
360 | ATH9K_RX_FILTER_MCAST;
361
362 /* If not a STA, enable processing of Probe Requests */
Sujith2660b812009-02-09 13:27:26 +0530363 if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700364 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
365
366 /* Can't set HOSTAP into promiscous mode */
Sujith2660b812009-02-09 13:27:26 +0530367 if (((sc->sc_ah->opmode != NL80211_IFTYPE_AP) &&
Sujithb77f4832008-12-07 21:44:03 +0530368 (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) ||
Sujith2660b812009-02-09 13:27:26 +0530369 (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700370 rfilt |= ATH9K_RX_FILTER_PROM;
371 /* ??? To prevent from sending ACK */
372 rfilt &= ~ATH9K_RX_FILTER_UCAST;
373 }
374
Sujithd42c6b72009-02-04 08:10:22 +0530375 if (sc->rx.rxfilter & FIF_CONTROL)
376 rfilt |= ATH9K_RX_FILTER_CONTROL;
377
Sujith2660b812009-02-09 13:27:26 +0530378 if (sc->sc_ah->opmode == NL80211_IFTYPE_STATION ||
379 sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700380 rfilt |= ATH9K_RX_FILTER_BEACON;
381
382 /* If in HOSTAP mode, want to enable reception of PSPOLL frames
383 & beacon frames */
Sujith2660b812009-02-09 13:27:26 +0530384 if (sc->sc_ah->opmode == NL80211_IFTYPE_AP)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700385 rfilt |= (ATH9K_RX_FILTER_BEACON | ATH9K_RX_FILTER_PSPOLL);
Sujithbe0418a2008-11-18 09:05:55 +0530386
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700387 return rfilt;
Sujith7dcfdcd2008-08-11 14:03:13 +0530388
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700389#undef RX_FILTER_PRESERVE
390}
391
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700392int ath_startrecv(struct ath_softc *sc)
393{
Sujithcbe61d82009-02-09 13:27:12 +0530394 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700395 struct ath_buf *bf, *tbf;
396
Sujithb77f4832008-12-07 21:44:03 +0530397 spin_lock_bh(&sc->rx.rxbuflock);
398 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700399 goto start_recv;
400
Sujithb77f4832008-12-07 21:44:03 +0530401 sc->rx.rxlink = NULL;
402 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700403 ath_rx_buf_link(sc, bf);
404 }
405
406 /* We could have deleted elements so the list may be empty now */
Sujithb77f4832008-12-07 21:44:03 +0530407 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700408 goto start_recv;
409
Sujithb77f4832008-12-07 21:44:03 +0530410 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700411 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
Sujithbe0418a2008-11-18 09:05:55 +0530412 ath9k_hw_rxena(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700413
414start_recv:
Sujithb77f4832008-12-07 21:44:03 +0530415 spin_unlock_bh(&sc->rx.rxbuflock);
Sujithbe0418a2008-11-18 09:05:55 +0530416 ath_opmode_init(sc);
417 ath9k_hw_startpcureceive(ah);
418
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700419 return 0;
420}
421
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700422bool ath_stoprecv(struct ath_softc *sc)
423{
Sujithcbe61d82009-02-09 13:27:12 +0530424 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700425 bool stopped;
426
Sujithbe0418a2008-11-18 09:05:55 +0530427 ath9k_hw_stoppcurecv(ah);
428 ath9k_hw_setrxfilter(ah, 0);
429 stopped = ath9k_hw_stopdmarecv(ah);
Sujithb77f4832008-12-07 21:44:03 +0530430 sc->rx.rxlink = NULL;
Sujithbe0418a2008-11-18 09:05:55 +0530431
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700432 return stopped;
433}
434
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700435void ath_flushrecv(struct ath_softc *sc)
436{
Sujithb77f4832008-12-07 21:44:03 +0530437 spin_lock_bh(&sc->rx.rxflushlock);
Sujith98deeea2008-08-11 14:05:46 +0530438 sc->sc_flags |= SC_OP_RXFLUSH;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700439 ath_rx_tasklet(sc, 1);
Sujith98deeea2008-08-11 14:05:46 +0530440 sc->sc_flags &= ~SC_OP_RXFLUSH;
Sujithb77f4832008-12-07 21:44:03 +0530441 spin_unlock_bh(&sc->rx.rxflushlock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700442}
443
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700444int ath_rx_tasklet(struct ath_softc *sc, int flush)
445{
446#define PA2DESC(_sc, _pa) \
Sujithb77f4832008-12-07 21:44:03 +0530447 ((struct ath_desc *)((caddr_t)(_sc)->rx.rxdma.dd_desc + \
448 ((_pa) - (_sc)->rx.rxdma.dd_desc_paddr)))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700449
Sujithbe0418a2008-11-18 09:05:55 +0530450 struct ath_buf *bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700451 struct ath_desc *ds;
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800452 struct sk_buff *skb = NULL, *requeue_skb;
Sujithbe0418a2008-11-18 09:05:55 +0530453 struct ieee80211_rx_status rx_status;
Sujithcbe61d82009-02-09 13:27:12 +0530454 struct ath_hw *ah = sc->sc_ah;
Sujithbe0418a2008-11-18 09:05:55 +0530455 struct ieee80211_hdr *hdr;
456 int hdrlen, padsize, retval;
457 bool decrypt_error = false;
458 u8 keyix;
459
Sujithb77f4832008-12-07 21:44:03 +0530460 spin_lock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700461
462 do {
463 /* If handling rx interrupt and flush is in progress => exit */
Sujith98deeea2008-08-11 14:05:46 +0530464 if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700465 break;
466
Sujithb77f4832008-12-07 21:44:03 +0530467 if (list_empty(&sc->rx.rxbuf)) {
468 sc->rx.rxlink = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700469 break;
470 }
471
Sujithb77f4832008-12-07 21:44:03 +0530472 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700473 ds = bf->bf_desc;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700474
475 /*
476 * Must provide the virtual address of the current
477 * descriptor, the physical address, and the virtual
478 * address of the next descriptor in the h/w chain.
479 * This allows the HAL to look ahead to see if the
480 * hardware is done with a descriptor by checking the
481 * done bit in the following descriptor and the address
482 * of the current descriptor the DMA engine is working
483 * on. All this is necessary because of our use of
484 * a self-linked list to avoid rx overruns.
485 */
Sujithbe0418a2008-11-18 09:05:55 +0530486 retval = ath9k_hw_rxprocdesc(ah, ds,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700487 bf->bf_daddr,
488 PA2DESC(sc, ds->ds_link),
489 0);
490 if (retval == -EINPROGRESS) {
491 struct ath_buf *tbf;
492 struct ath_desc *tds;
493
Sujithb77f4832008-12-07 21:44:03 +0530494 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
495 sc->rx.rxlink = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700496 break;
497 }
498
499 tbf = list_entry(bf->list.next, struct ath_buf, list);
500
501 /*
502 * On some hardware the descriptor status words could
503 * get corrupted, including the done bit. Because of
504 * this, check if the next descriptor's done bit is
505 * set or not.
506 *
507 * If the next descriptor's done bit is set, the current
508 * descriptor has been corrupted. Force s/w to discard
509 * this descriptor and continue...
510 */
511
512 tds = tbf->bf_desc;
Sujithbe0418a2008-11-18 09:05:55 +0530513 retval = ath9k_hw_rxprocdesc(ah, tds, tbf->bf_daddr,
514 PA2DESC(sc, tds->ds_link), 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700515 if (retval == -EINPROGRESS) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700516 break;
517 }
518 }
519
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700520 skb = bf->bf_mpdu;
Sujithbe0418a2008-11-18 09:05:55 +0530521 if (!skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700522 continue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700523
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700524 /*
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +0530525 * Synchronize the DMA transfer with CPU before
526 * 1. accessing the frame
527 * 2. requeueing the same buffer to h/w
528 */
Gabor Juhos7da3c552009-01-14 20:17:03 +0100529 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +0530530 sc->rx.bufsize,
Gabor Juhos7da3c552009-01-14 20:17:03 +0100531 DMA_FROM_DEVICE);
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +0530532
533 /*
Sujithbe0418a2008-11-18 09:05:55 +0530534 * If we're asked to flush receive queue, directly
535 * chain it back at the queue without processing it.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700536 */
Sujithbe0418a2008-11-18 09:05:55 +0530537 if (flush)
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800538 goto requeue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700539
Sujithbe0418a2008-11-18 09:05:55 +0530540 if (!ds->ds_rxstat.rs_datalen)
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800541 goto requeue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700542
Sujithbe0418a2008-11-18 09:05:55 +0530543 /* The status portion of the descriptor could get corrupted. */
Sujithb77f4832008-12-07 21:44:03 +0530544 if (sc->rx.bufsize < ds->ds_rxstat.rs_datalen)
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800545 goto requeue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700546
Sujithbe0418a2008-11-18 09:05:55 +0530547 if (!ath_rx_prepare(skb, ds, &rx_status, &decrypt_error, sc))
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800548 goto requeue;
549
550 /* Ensure we always have an skb to requeue once we are done
551 * processing the current buffer's skb */
Sujithb77f4832008-12-07 21:44:03 +0530552 requeue_skb = ath_rxbuf_alloc(sc, sc->rx.bufsize);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800553
554 /* If there is no memory we ignore the current RX'd frame,
555 * tell hardware it can give us a new frame using the old
Sujithb77f4832008-12-07 21:44:03 +0530556 * skb and put it at the tail of the sc->rx.rxbuf list for
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800557 * processing. */
558 if (!requeue_skb)
559 goto requeue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700560
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +0530561 /* Unmap the frame */
Gabor Juhos7da3c552009-01-14 20:17:03 +0100562 dma_unmap_single(sc->dev, bf->bf_buf_addr,
Sujithb77f4832008-12-07 21:44:03 +0530563 sc->rx.bufsize,
Gabor Juhos7da3c552009-01-14 20:17:03 +0100564 DMA_FROM_DEVICE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700565
Sujithbe0418a2008-11-18 09:05:55 +0530566 skb_put(skb, ds->ds_rxstat.rs_datalen);
567 skb->protocol = cpu_to_be16(ETH_P_CONTROL);
568
569 /* see if any padding is done by the hw and remove it */
570 hdr = (struct ieee80211_hdr *)skb->data;
571 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
572
Jouni Malinen9c5f89b2008-12-11 18:22:13 +0200573 /* The MAC header is padded to have 32-bit boundary if the
574 * packet payload is non-zero. The general calculation for
575 * padsize would take into account odd header lengths:
576 * padsize = (4 - hdrlen % 4) % 4; However, since only
577 * even-length headers are used, padding can only be 0 or 2
578 * bytes and we can optimize this a bit. In addition, we must
579 * not try to remove padding from short control frames that do
580 * not have payload. */
581 padsize = hdrlen & 3;
582 if (padsize && hdrlen >= 24) {
Sujithbe0418a2008-11-18 09:05:55 +0530583 memmove(skb->data + padsize, skb->data, hdrlen);
584 skb_pull(skb, padsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700585 }
586
Sujithbe0418a2008-11-18 09:05:55 +0530587 keyix = ds->ds_rxstat.rs_keyix;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700588
Sujithbe0418a2008-11-18 09:05:55 +0530589 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error) {
590 rx_status.flag |= RX_FLAG_DECRYPTED;
591 } else if ((le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_PROTECTED)
592 && !decrypt_error && skb->len >= hdrlen + 4) {
593 keyix = skb->data[hdrlen + 3] >> 6;
594
Sujith17d79042009-02-09 13:27:03 +0530595 if (test_bit(keyix, sc->keymap))
Sujithbe0418a2008-11-18 09:05:55 +0530596 rx_status.flag |= RX_FLAG_DECRYPTED;
597 }
Jouni Malinen0ced0e12009-01-08 13:32:13 +0200598 if (ah->sw_mgmt_crypto &&
599 (rx_status.flag & RX_FLAG_DECRYPTED) &&
600 ieee80211_is_mgmt(hdr->frame_control)) {
601 /* Use software decrypt for management frames. */
602 rx_status.flag &= ~RX_FLAG_DECRYPTED;
603 }
Sujithbe0418a2008-11-18 09:05:55 +0530604
605 /* Send the frame to mac80211 */
606 __ieee80211_rx(sc->hw, skb, &rx_status);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800607
608 /* We will now give hardware our shiny new allocated skb */
609 bf->bf_mpdu = requeue_skb;
Gabor Juhos7da3c552009-01-14 20:17:03 +0100610 bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
Sujithb77f4832008-12-07 21:44:03 +0530611 sc->rx.bufsize,
Gabor Juhos7da3c552009-01-14 20:17:03 +0100612 DMA_FROM_DEVICE);
613 if (unlikely(dma_mapping_error(sc->dev,
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -0800614 bf->bf_buf_addr))) {
615 dev_kfree_skb_any(requeue_skb);
616 bf->bf_mpdu = NULL;
617 DPRINTF(sc, ATH_DBG_CONFIG,
Gabor Juhos7da3c552009-01-14 20:17:03 +0100618 "dma_mapping_error() on RX\n");
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -0800619 break;
620 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800621 bf->bf_dmacontext = bf->bf_buf_addr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700622
623 /*
624 * change the default rx antenna if rx diversity chooses the
625 * other antenna 3 times in a row.
626 */
Sujithb77f4832008-12-07 21:44:03 +0530627 if (sc->rx.defant != ds->ds_rxstat.rs_antenna) {
628 if (++sc->rx.rxotherant >= 3)
Sujithbe0418a2008-11-18 09:05:55 +0530629 ath_setdefantenna(sc, ds->ds_rxstat.rs_antenna);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700630 } else {
Sujithb77f4832008-12-07 21:44:03 +0530631 sc->rx.rxotherant = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700632 }
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +0530633
634 if (ieee80211_is_beacon(hdr->frame_control) &&
635 (sc->sc_flags & SC_OP_WAIT_FOR_BEACON)) {
636 sc->sc_flags &= ~SC_OP_WAIT_FOR_BEACON;
637 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
638 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800639requeue:
Sujithb77f4832008-12-07 21:44:03 +0530640 list_move_tail(&bf->list, &sc->rx.rxbuf);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800641 ath_rx_buf_link(sc, bf);
Sujithbe0418a2008-11-18 09:05:55 +0530642 } while (1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700643
Sujithb77f4832008-12-07 21:44:03 +0530644 spin_unlock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700645
646 return 0;
647#undef PA2DESC
648}