blob: ec535834f961f574e432ce4e0325c11b90ffc87d [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
Jouni Malinenbce048d2009-03-03 19:23:28 +020019static struct ieee80211_hw * ath_get_virt_hw(struct ath_softc *sc,
20 struct ieee80211_hdr *hdr)
21{
22 return sc->pri_wiphy->hw;
23}
24
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070025/*
26 * Setup and link descriptors.
27 *
28 * 11N: we can no longer afford to self link the last descriptor.
29 * MAC acknowledges BA status as long as it copies frames to host
30 * buffer (or rx fifo). This can incorrectly acknowledge packets
31 * to a sender if last desc is self-linked.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070032 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070033static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
34{
Sujithcbe61d82009-02-09 13:27:12 +053035 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070036 struct ath_desc *ds;
37 struct sk_buff *skb;
38
39 ATH_RXBUF_RESET(bf);
40
41 ds = bf->bf_desc;
Sujithbe0418a2008-11-18 09:05:55 +053042 ds->ds_link = 0; /* link to null */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070043 ds->ds_data = bf->bf_buf_addr;
44
Sujithbe0418a2008-11-18 09:05:55 +053045 /* virtual addr of the beginning of the buffer. */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070046 skb = bf->bf_mpdu;
47 ASSERT(skb != NULL);
48 ds->ds_vdata = skb->data;
49
Sujithb77f4832008-12-07 21:44:03 +053050 /* setup rx descriptors. The rx.bufsize here tells the harware
Luis R. Rodriguezb4b6cda2008-11-20 17:15:13 -080051 * how much data it can DMA to us and that we are prepared
52 * to process */
Sujithb77f4832008-12-07 21:44:03 +053053 ath9k_hw_setuprxdesc(ah, ds,
54 sc->rx.bufsize,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070055 0);
56
Sujithb77f4832008-12-07 21:44:03 +053057 if (sc->rx.rxlink == NULL)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070058 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
59 else
Sujithb77f4832008-12-07 21:44:03 +053060 *sc->rx.rxlink = bf->bf_daddr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070061
Sujithb77f4832008-12-07 21:44:03 +053062 sc->rx.rxlink = &ds->ds_link;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070063 ath9k_hw_rxena(ah);
64}
65
Sujithff37e332008-11-24 12:07:55 +053066static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
67{
68 /* XXX block beacon interrupts */
69 ath9k_hw_setantenna(sc->sc_ah, antenna);
Sujithb77f4832008-12-07 21:44:03 +053070 sc->rx.defant = antenna;
71 sc->rx.rxotherant = 0;
Sujithff37e332008-11-24 12:07:55 +053072}
73
74/*
75 * Extend 15-bit time stamp from rx descriptor to
76 * a full 64-bit TSF using the current h/w TSF.
77*/
78static u64 ath_extend_tsf(struct ath_softc *sc, u32 rstamp)
79{
80 u64 tsf;
81
82 tsf = ath9k_hw_gettsf64(sc->sc_ah);
83 if ((tsf & 0x7fff) < rstamp)
84 tsf -= 0x8000;
85 return (tsf & ~0x7fff) | rstamp;
86}
87
Sujithbe0418a2008-11-18 09:05:55 +053088static struct sk_buff *ath_rxbuf_alloc(struct ath_softc *sc, u32 len)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070089{
90 struct sk_buff *skb;
91 u32 off;
92
93 /*
94 * Cache-line-align. This is important (for the
95 * 5210 at least) as not doing so causes bogus data
96 * in rx'd frames.
97 */
98
Luis R. Rodriguezb4b6cda2008-11-20 17:15:13 -080099 /* Note: the kernel can allocate a value greater than
100 * what we ask it to give us. We really only need 4 KB as that
101 * is this hardware supports and in fact we need at least 3849
102 * as that is the MAX AMSDU size this hardware supports.
103 * Unfortunately this means we may get 8 KB here from the
104 * kernel... and that is actually what is observed on some
105 * systems :( */
Sujith17d79042009-02-09 13:27:03 +0530106 skb = dev_alloc_skb(len + sc->cachelsz - 1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700107 if (skb != NULL) {
Sujith17d79042009-02-09 13:27:03 +0530108 off = ((unsigned long) skb->data) % sc->cachelsz;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700109 if (off != 0)
Sujith17d79042009-02-09 13:27:03 +0530110 skb_reserve(skb, sc->cachelsz - off);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700111 } else {
112 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +0530113 "skbuff alloc of size %u failed\n", len);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700114 return NULL;
115 }
116
117 return skb;
118}
119
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700120/*
Sujithbe0418a2008-11-18 09:05:55 +0530121 * For Decrypt or Demic errors, we only mark packet status here and always push
122 * up the frame up to let mac80211 handle the actual error case, be it no
123 * decryption key or real decryption error. This let us keep statistics there.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700124 */
Sujithbe0418a2008-11-18 09:05:55 +0530125static int ath_rx_prepare(struct sk_buff *skb, struct ath_desc *ds,
126 struct ieee80211_rx_status *rx_status, bool *decrypt_error,
127 struct ath_softc *sc)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700128{
Sujithbe0418a2008-11-18 09:05:55 +0530129 struct ieee80211_hdr *hdr;
Sujithbe0418a2008-11-18 09:05:55 +0530130 u8 ratecode;
131 __le16 fc;
Jouni Malinenbce048d2009-03-03 19:23:28 +0200132 struct ieee80211_hw *hw;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700133
Sujithbe0418a2008-11-18 09:05:55 +0530134 hdr = (struct ieee80211_hdr *)skb->data;
135 fc = hdr->frame_control;
136 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
Jouni Malinenbce048d2009-03-03 19:23:28 +0200137 hw = ath_get_virt_hw(sc, hdr);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700138
Sujithbe0418a2008-11-18 09:05:55 +0530139 if (ds->ds_rxstat.rs_more) {
140 /*
141 * Frame spans multiple descriptors; this cannot happen yet
142 * as we don't support jumbograms. If not in monitor mode,
143 * discard the frame. Enable this if you want to see
144 * error frames in Monitor mode.
145 */
Sujith2660b812009-02-09 13:27:26 +0530146 if (sc->sc_ah->opmode != NL80211_IFTYPE_MONITOR)
Sujithbe0418a2008-11-18 09:05:55 +0530147 goto rx_next;
148 } else if (ds->ds_rxstat.rs_status != 0) {
149 if (ds->ds_rxstat.rs_status & ATH9K_RXERR_CRC)
150 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
151 if (ds->ds_rxstat.rs_status & ATH9K_RXERR_PHY)
152 goto rx_next;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700153
Sujithbe0418a2008-11-18 09:05:55 +0530154 if (ds->ds_rxstat.rs_status & ATH9K_RXERR_DECRYPT) {
155 *decrypt_error = true;
156 } else if (ds->ds_rxstat.rs_status & ATH9K_RXERR_MIC) {
157 if (ieee80211_is_ctl(fc))
158 /*
159 * Sometimes, we get invalid
160 * MIC failures on valid control frames.
161 * Remove these mic errors.
162 */
163 ds->ds_rxstat.rs_status &= ~ATH9K_RXERR_MIC;
164 else
165 rx_status->flag |= RX_FLAG_MMIC_ERROR;
166 }
167 /*
168 * Reject error frames with the exception of
169 * decryption and MIC failures. For monitor mode,
170 * we also ignore the CRC error.
171 */
Sujith2660b812009-02-09 13:27:26 +0530172 if (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR) {
Sujithbe0418a2008-11-18 09:05:55 +0530173 if (ds->ds_rxstat.rs_status &
174 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
175 ATH9K_RXERR_CRC))
176 goto rx_next;
177 } else {
178 if (ds->ds_rxstat.rs_status &
179 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
180 goto rx_next;
181 }
182 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700183 }
184
Sujithbe0418a2008-11-18 09:05:55 +0530185 ratecode = ds->ds_rxstat.rs_rate;
Sujithbe0418a2008-11-18 09:05:55 +0530186
Sujithbe0418a2008-11-18 09:05:55 +0530187 if (ratecode & 0x80) {
Jouni Malinenbaad1d92008-12-12 14:38:34 +0200188 /* HT rate */
189 rx_status->flag |= RX_FLAG_HT;
Sujithbe0418a2008-11-18 09:05:55 +0530190 if (ds->ds_rxstat.rs_flags & ATH9K_RX_2040)
Jouni Malinenbaad1d92008-12-12 14:38:34 +0200191 rx_status->flag |= RX_FLAG_40MHZ;
Sujithbe0418a2008-11-18 09:05:55 +0530192 if (ds->ds_rxstat.rs_flags & ATH9K_RX_GI)
Jouni Malinenbaad1d92008-12-12 14:38:34 +0200193 rx_status->flag |= RX_FLAG_SHORT_GI;
194 rx_status->rate_idx = ratecode & 0x7f;
195 } else {
196 int i = 0, cur_band, n_rates;
Jouni Malinenbaad1d92008-12-12 14:38:34 +0200197
198 cur_band = hw->conf.channel->band;
199 n_rates = sc->sbands[cur_band].n_bitrates;
200
201 for (i = 0; i < n_rates; i++) {
202 if (sc->sbands[cur_band].bitrates[i].hw_value ==
203 ratecode) {
204 rx_status->rate_idx = i;
205 break;
206 }
207
208 if (sc->sbands[cur_band].bitrates[i].hw_value_short ==
209 ratecode) {
210 rx_status->rate_idx = i;
211 rx_status->flag |= RX_FLAG_SHORTPRE;
212 break;
213 }
214 }
Sujithbe0418a2008-11-18 09:05:55 +0530215 }
216
217 rx_status->mactime = ath_extend_tsf(sc, ds->ds_rxstat.rs_tstamp);
Jouni Malinenbce048d2009-03-03 19:23:28 +0200218 rx_status->band = hw->conf.channel->band;
219 rx_status->freq = hw->conf.channel->center_freq;
Sujith17d79042009-02-09 13:27:03 +0530220 rx_status->noise = sc->ani.noise_floor;
Sujithbe0418a2008-11-18 09:05:55 +0530221 rx_status->signal = rx_status->noise + ds->ds_rxstat.rs_rssi;
Sujithbe0418a2008-11-18 09:05:55 +0530222 rx_status->antenna = ds->ds_rxstat.rs_antenna;
223
224 /* at 45 you will be able to use MCS 15 reliably. A more elaborate
225 * scheme can be used here but it requires tables of SNR/throughput for
226 * each possible mode used. */
227 rx_status->qual = ds->ds_rxstat.rs_rssi * 100 / 45;
228
229 /* rssi can be more than 45 though, anything above that
230 * should be considered at 100% */
231 if (rx_status->qual > 100)
232 rx_status->qual = 100;
233
234 rx_status->flag |= RX_FLAG_TSFT;
235
236 return 1;
237rx_next:
238 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700239}
240
241static void ath_opmode_init(struct ath_softc *sc)
242{
Sujithcbe61d82009-02-09 13:27:12 +0530243 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700244 u32 rfilt, mfilt[2];
245
246 /* configure rx filter */
247 rfilt = ath_calcrxfilter(sc);
248 ath9k_hw_setrxfilter(ah, rfilt);
249
250 /* configure bssid mask */
Sujith2660b812009-02-09 13:27:26 +0530251 if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
Sujithba52da52009-02-09 13:27:10 +0530252 ath9k_hw_setbssidmask(sc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700253
254 /* configure operational mode */
255 ath9k_hw_setopmode(ah);
256
257 /* Handle any link-level address change. */
Sujithba52da52009-02-09 13:27:10 +0530258 ath9k_hw_setmac(ah, sc->sc_ah->macaddr);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700259
260 /* calculate and install multicast filter */
261 mfilt[0] = mfilt[1] = ~0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700262 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700263}
264
265int ath_rx_init(struct ath_softc *sc, int nbufs)
266{
267 struct sk_buff *skb;
268 struct ath_buf *bf;
269 int error = 0;
270
271 do {
Sujithb77f4832008-12-07 21:44:03 +0530272 spin_lock_init(&sc->rx.rxflushlock);
Sujith98deeea2008-08-11 14:05:46 +0530273 sc->sc_flags &= ~SC_OP_RXFLUSH;
Sujithb77f4832008-12-07 21:44:03 +0530274 spin_lock_init(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700275
Sujithb77f4832008-12-07 21:44:03 +0530276 sc->rx.bufsize = roundup(IEEE80211_MAX_MPDU_LEN,
Sujith17d79042009-02-09 13:27:03 +0530277 min(sc->cachelsz,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700278 (u16)64));
279
Sujith04bd46382008-11-28 22:18:05 +0530280 DPRINTF(sc, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
Sujith17d79042009-02-09 13:27:03 +0530281 sc->cachelsz, sc->rx.bufsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700282
283 /* Initialize rx descriptors */
284
Sujithb77f4832008-12-07 21:44:03 +0530285 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700286 "rx", nbufs, 1);
287 if (error != 0) {
288 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +0530289 "failed to allocate rx descriptors: %d\n", error);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700290 break;
291 }
292
Sujithb77f4832008-12-07 21:44:03 +0530293 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
294 skb = ath_rxbuf_alloc(sc, sc->rx.bufsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700295 if (skb == NULL) {
296 error = -ENOMEM;
297 break;
298 }
299
300 bf->bf_mpdu = skb;
Gabor Juhos7da3c552009-01-14 20:17:03 +0100301 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
Sujithb77f4832008-12-07 21:44:03 +0530302 sc->rx.bufsize,
Gabor Juhos7da3c552009-01-14 20:17:03 +0100303 DMA_FROM_DEVICE);
304 if (unlikely(dma_mapping_error(sc->dev,
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -0800305 bf->bf_buf_addr))) {
306 dev_kfree_skb_any(skb);
307 bf->bf_mpdu = NULL;
308 DPRINTF(sc, ATH_DBG_CONFIG,
Gabor Juhos7da3c552009-01-14 20:17:03 +0100309 "dma_mapping_error() on RX init\n");
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -0800310 error = -ENOMEM;
311 break;
312 }
Sujith927e70e2008-08-14 13:26:34 +0530313 bf->bf_dmacontext = bf->bf_buf_addr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700314 }
Sujithb77f4832008-12-07 21:44:03 +0530315 sc->rx.rxlink = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700316
317 } while (0);
318
319 if (error)
320 ath_rx_cleanup(sc);
321
322 return error;
323}
324
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700325void ath_rx_cleanup(struct ath_softc *sc)
326{
327 struct sk_buff *skb;
328 struct ath_buf *bf;
329
Sujithb77f4832008-12-07 21:44:03 +0530330 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700331 skb = bf->bf_mpdu;
332 if (skb)
333 dev_kfree_skb(skb);
334 }
335
Sujithb77f4832008-12-07 21:44:03 +0530336 if (sc->rx.rxdma.dd_desc_len != 0)
337 ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700338}
339
340/*
341 * Calculate the receive filter according to the
342 * operating mode and state:
343 *
344 * o always accept unicast, broadcast, and multicast traffic
345 * o maintain current state of phy error reception (the hal
346 * may enable phy error frames for noise immunity work)
347 * o probe request frames are accepted only when operating in
348 * hostap, adhoc, or monitor modes
349 * o enable promiscuous mode according to the interface state
350 * o accept beacons:
351 * - when operating in adhoc mode so the 802.11 layer creates
352 * node table entries for peers,
353 * - when operating in station mode for collecting rssi data when
354 * the station is otherwise quiet, or
355 * - when operating as a repeater so we see repeater-sta beacons
356 * - when scanning
357 */
358
359u32 ath_calcrxfilter(struct ath_softc *sc)
360{
361#define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
Sujith7dcfdcd2008-08-11 14:03:13 +0530362
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700363 u32 rfilt;
364
365 rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
366 | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
367 | ATH9K_RX_FILTER_MCAST;
368
369 /* If not a STA, enable processing of Probe Requests */
Sujith2660b812009-02-09 13:27:26 +0530370 if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700371 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
372
373 /* Can't set HOSTAP into promiscous mode */
Sujith2660b812009-02-09 13:27:26 +0530374 if (((sc->sc_ah->opmode != NL80211_IFTYPE_AP) &&
Sujithb77f4832008-12-07 21:44:03 +0530375 (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) ||
Sujith2660b812009-02-09 13:27:26 +0530376 (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700377 rfilt |= ATH9K_RX_FILTER_PROM;
378 /* ??? To prevent from sending ACK */
379 rfilt &= ~ATH9K_RX_FILTER_UCAST;
380 }
381
Sujithd42c6b72009-02-04 08:10:22 +0530382 if (sc->rx.rxfilter & FIF_CONTROL)
383 rfilt |= ATH9K_RX_FILTER_CONTROL;
384
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530385 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
386 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
387 rfilt |= ATH9K_RX_FILTER_MYBEACON;
388 else
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700389 rfilt |= ATH9K_RX_FILTER_BEACON;
390
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530391 /* If in HOSTAP mode, want to enable reception of PSPOLL frames */
Sujith2660b812009-02-09 13:27:26 +0530392 if (sc->sc_ah->opmode == NL80211_IFTYPE_AP)
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530393 rfilt |= ATH9K_RX_FILTER_PSPOLL;
Sujithbe0418a2008-11-18 09:05:55 +0530394
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700395 return rfilt;
Sujith7dcfdcd2008-08-11 14:03:13 +0530396
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700397#undef RX_FILTER_PRESERVE
398}
399
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700400int ath_startrecv(struct ath_softc *sc)
401{
Sujithcbe61d82009-02-09 13:27:12 +0530402 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700403 struct ath_buf *bf, *tbf;
404
Sujithb77f4832008-12-07 21:44:03 +0530405 spin_lock_bh(&sc->rx.rxbuflock);
406 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700407 goto start_recv;
408
Sujithb77f4832008-12-07 21:44:03 +0530409 sc->rx.rxlink = NULL;
410 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700411 ath_rx_buf_link(sc, bf);
412 }
413
414 /* We could have deleted elements so the list may be empty now */
Sujithb77f4832008-12-07 21:44:03 +0530415 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700416 goto start_recv;
417
Sujithb77f4832008-12-07 21:44:03 +0530418 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700419 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
Sujithbe0418a2008-11-18 09:05:55 +0530420 ath9k_hw_rxena(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700421
422start_recv:
Sujithb77f4832008-12-07 21:44:03 +0530423 spin_unlock_bh(&sc->rx.rxbuflock);
Sujithbe0418a2008-11-18 09:05:55 +0530424 ath_opmode_init(sc);
425 ath9k_hw_startpcureceive(ah);
426
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700427 return 0;
428}
429
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700430bool ath_stoprecv(struct ath_softc *sc)
431{
Sujithcbe61d82009-02-09 13:27:12 +0530432 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700433 bool stopped;
434
Sujithbe0418a2008-11-18 09:05:55 +0530435 ath9k_hw_stoppcurecv(ah);
436 ath9k_hw_setrxfilter(ah, 0);
437 stopped = ath9k_hw_stopdmarecv(ah);
Sujithb77f4832008-12-07 21:44:03 +0530438 sc->rx.rxlink = NULL;
Sujithbe0418a2008-11-18 09:05:55 +0530439
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700440 return stopped;
441}
442
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700443void ath_flushrecv(struct ath_softc *sc)
444{
Sujithb77f4832008-12-07 21:44:03 +0530445 spin_lock_bh(&sc->rx.rxflushlock);
Sujith98deeea2008-08-11 14:05:46 +0530446 sc->sc_flags |= SC_OP_RXFLUSH;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700447 ath_rx_tasklet(sc, 1);
Sujith98deeea2008-08-11 14:05:46 +0530448 sc->sc_flags &= ~SC_OP_RXFLUSH;
Sujithb77f4832008-12-07 21:44:03 +0530449 spin_unlock_bh(&sc->rx.rxflushlock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700450}
451
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700452int ath_rx_tasklet(struct ath_softc *sc, int flush)
453{
454#define PA2DESC(_sc, _pa) \
Sujithb77f4832008-12-07 21:44:03 +0530455 ((struct ath_desc *)((caddr_t)(_sc)->rx.rxdma.dd_desc + \
456 ((_pa) - (_sc)->rx.rxdma.dd_desc_paddr)))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700457
Sujithbe0418a2008-11-18 09:05:55 +0530458 struct ath_buf *bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700459 struct ath_desc *ds;
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800460 struct sk_buff *skb = NULL, *requeue_skb;
Sujithbe0418a2008-11-18 09:05:55 +0530461 struct ieee80211_rx_status rx_status;
Sujithcbe61d82009-02-09 13:27:12 +0530462 struct ath_hw *ah = sc->sc_ah;
Sujithbe0418a2008-11-18 09:05:55 +0530463 struct ieee80211_hdr *hdr;
464 int hdrlen, padsize, retval;
465 bool decrypt_error = false;
466 u8 keyix;
467
Sujithb77f4832008-12-07 21:44:03 +0530468 spin_lock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700469
470 do {
471 /* If handling rx interrupt and flush is in progress => exit */
Sujith98deeea2008-08-11 14:05:46 +0530472 if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700473 break;
474
Sujithb77f4832008-12-07 21:44:03 +0530475 if (list_empty(&sc->rx.rxbuf)) {
476 sc->rx.rxlink = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700477 break;
478 }
479
Sujithb77f4832008-12-07 21:44:03 +0530480 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700481 ds = bf->bf_desc;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700482
483 /*
484 * Must provide the virtual address of the current
485 * descriptor, the physical address, and the virtual
486 * address of the next descriptor in the h/w chain.
487 * This allows the HAL to look ahead to see if the
488 * hardware is done with a descriptor by checking the
489 * done bit in the following descriptor and the address
490 * of the current descriptor the DMA engine is working
491 * on. All this is necessary because of our use of
492 * a self-linked list to avoid rx overruns.
493 */
Sujithbe0418a2008-11-18 09:05:55 +0530494 retval = ath9k_hw_rxprocdesc(ah, ds,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700495 bf->bf_daddr,
496 PA2DESC(sc, ds->ds_link),
497 0);
498 if (retval == -EINPROGRESS) {
499 struct ath_buf *tbf;
500 struct ath_desc *tds;
501
Sujithb77f4832008-12-07 21:44:03 +0530502 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
503 sc->rx.rxlink = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700504 break;
505 }
506
507 tbf = list_entry(bf->list.next, struct ath_buf, list);
508
509 /*
510 * On some hardware the descriptor status words could
511 * get corrupted, including the done bit. Because of
512 * this, check if the next descriptor's done bit is
513 * set or not.
514 *
515 * If the next descriptor's done bit is set, the current
516 * descriptor has been corrupted. Force s/w to discard
517 * this descriptor and continue...
518 */
519
520 tds = tbf->bf_desc;
Sujithbe0418a2008-11-18 09:05:55 +0530521 retval = ath9k_hw_rxprocdesc(ah, tds, tbf->bf_daddr,
522 PA2DESC(sc, tds->ds_link), 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700523 if (retval == -EINPROGRESS) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700524 break;
525 }
526 }
527
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700528 skb = bf->bf_mpdu;
Sujithbe0418a2008-11-18 09:05:55 +0530529 if (!skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700530 continue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700531
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700532 /*
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +0530533 * Synchronize the DMA transfer with CPU before
534 * 1. accessing the frame
535 * 2. requeueing the same buffer to h/w
536 */
Gabor Juhos7da3c552009-01-14 20:17:03 +0100537 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +0530538 sc->rx.bufsize,
Gabor Juhos7da3c552009-01-14 20:17:03 +0100539 DMA_FROM_DEVICE);
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +0530540
541 /*
Sujithbe0418a2008-11-18 09:05:55 +0530542 * If we're asked to flush receive queue, directly
543 * chain it back at the queue without processing it.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700544 */
Sujithbe0418a2008-11-18 09:05:55 +0530545 if (flush)
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800546 goto requeue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700547
Sujithbe0418a2008-11-18 09:05:55 +0530548 if (!ds->ds_rxstat.rs_datalen)
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800549 goto requeue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700550
Sujithbe0418a2008-11-18 09:05:55 +0530551 /* The status portion of the descriptor could get corrupted. */
Sujithb77f4832008-12-07 21:44:03 +0530552 if (sc->rx.bufsize < ds->ds_rxstat.rs_datalen)
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800553 goto requeue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700554
Sujithbe0418a2008-11-18 09:05:55 +0530555 if (!ath_rx_prepare(skb, ds, &rx_status, &decrypt_error, sc))
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800556 goto requeue;
557
558 /* Ensure we always have an skb to requeue once we are done
559 * processing the current buffer's skb */
Sujithb77f4832008-12-07 21:44:03 +0530560 requeue_skb = ath_rxbuf_alloc(sc, sc->rx.bufsize);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800561
562 /* If there is no memory we ignore the current RX'd frame,
563 * tell hardware it can give us a new frame using the old
Sujithb77f4832008-12-07 21:44:03 +0530564 * skb and put it at the tail of the sc->rx.rxbuf list for
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800565 * processing. */
566 if (!requeue_skb)
567 goto requeue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700568
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +0530569 /* Unmap the frame */
Gabor Juhos7da3c552009-01-14 20:17:03 +0100570 dma_unmap_single(sc->dev, bf->bf_buf_addr,
Sujithb77f4832008-12-07 21:44:03 +0530571 sc->rx.bufsize,
Gabor Juhos7da3c552009-01-14 20:17:03 +0100572 DMA_FROM_DEVICE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700573
Sujithbe0418a2008-11-18 09:05:55 +0530574 skb_put(skb, ds->ds_rxstat.rs_datalen);
575 skb->protocol = cpu_to_be16(ETH_P_CONTROL);
576
577 /* see if any padding is done by the hw and remove it */
578 hdr = (struct ieee80211_hdr *)skb->data;
579 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
580
Jouni Malinen9c5f89b2008-12-11 18:22:13 +0200581 /* The MAC header is padded to have 32-bit boundary if the
582 * packet payload is non-zero. The general calculation for
583 * padsize would take into account odd header lengths:
584 * padsize = (4 - hdrlen % 4) % 4; However, since only
585 * even-length headers are used, padding can only be 0 or 2
586 * bytes and we can optimize this a bit. In addition, we must
587 * not try to remove padding from short control frames that do
588 * not have payload. */
589 padsize = hdrlen & 3;
590 if (padsize && hdrlen >= 24) {
Sujithbe0418a2008-11-18 09:05:55 +0530591 memmove(skb->data + padsize, skb->data, hdrlen);
592 skb_pull(skb, padsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700593 }
594
Sujithbe0418a2008-11-18 09:05:55 +0530595 keyix = ds->ds_rxstat.rs_keyix;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700596
Sujithbe0418a2008-11-18 09:05:55 +0530597 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error) {
598 rx_status.flag |= RX_FLAG_DECRYPTED;
599 } else if ((le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_PROTECTED)
600 && !decrypt_error && skb->len >= hdrlen + 4) {
601 keyix = skb->data[hdrlen + 3] >> 6;
602
Sujith17d79042009-02-09 13:27:03 +0530603 if (test_bit(keyix, sc->keymap))
Sujithbe0418a2008-11-18 09:05:55 +0530604 rx_status.flag |= RX_FLAG_DECRYPTED;
605 }
Jouni Malinen0ced0e12009-01-08 13:32:13 +0200606 if (ah->sw_mgmt_crypto &&
607 (rx_status.flag & RX_FLAG_DECRYPTED) &&
608 ieee80211_is_mgmt(hdr->frame_control)) {
609 /* Use software decrypt for management frames. */
610 rx_status.flag &= ~RX_FLAG_DECRYPTED;
611 }
Sujithbe0418a2008-11-18 09:05:55 +0530612
613 /* Send the frame to mac80211 */
Jouni Malinenbce048d2009-03-03 19:23:28 +0200614 __ieee80211_rx(ath_get_virt_hw(sc, hdr), skb, &rx_status);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800615
616 /* We will now give hardware our shiny new allocated skb */
617 bf->bf_mpdu = requeue_skb;
Gabor Juhos7da3c552009-01-14 20:17:03 +0100618 bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
Sujithb77f4832008-12-07 21:44:03 +0530619 sc->rx.bufsize,
Gabor Juhos7da3c552009-01-14 20:17:03 +0100620 DMA_FROM_DEVICE);
621 if (unlikely(dma_mapping_error(sc->dev,
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -0800622 bf->bf_buf_addr))) {
623 dev_kfree_skb_any(requeue_skb);
624 bf->bf_mpdu = NULL;
625 DPRINTF(sc, ATH_DBG_CONFIG,
Gabor Juhos7da3c552009-01-14 20:17:03 +0100626 "dma_mapping_error() on RX\n");
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -0800627 break;
628 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800629 bf->bf_dmacontext = bf->bf_buf_addr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700630
631 /*
632 * change the default rx antenna if rx diversity chooses the
633 * other antenna 3 times in a row.
634 */
Sujithb77f4832008-12-07 21:44:03 +0530635 if (sc->rx.defant != ds->ds_rxstat.rs_antenna) {
636 if (++sc->rx.rxotherant >= 3)
Sujithbe0418a2008-11-18 09:05:55 +0530637 ath_setdefantenna(sc, ds->ds_rxstat.rs_antenna);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700638 } else {
Sujithb77f4832008-12-07 21:44:03 +0530639 sc->rx.rxotherant = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700640 }
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +0530641
642 if (ieee80211_is_beacon(hdr->frame_control) &&
643 (sc->sc_flags & SC_OP_WAIT_FOR_BEACON)) {
644 sc->sc_flags &= ~SC_OP_WAIT_FOR_BEACON;
645 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
646 }
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800647requeue:
Sujithb77f4832008-12-07 21:44:03 +0530648 list_move_tail(&bf->list, &sc->rx.rxbuf);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800649 ath_rx_buf_link(sc, bf);
Sujithbe0418a2008-11-18 09:05:55 +0530650 } while (1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700651
Sujithb77f4832008-12-07 21:44:03 +0530652 spin_unlock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700653
654 return 0;
655#undef PA2DESC
656}