blob: 94560e2fe3765ae11209b7b2e135ff0ac8fd04f3 [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. 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{
Jouni Malinenc52f33d2009-03-03 19:23:29 +020022 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;
Jouni Malinenbce048d2009-03-03 19:23:28 +020038}
39
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070040/*
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.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070047 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070048static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
49{
Sujithcbe61d82009-02-09 13:27:12 +053050 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080051 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070052 struct ath_desc *ds;
53 struct sk_buff *skb;
54
55 ATH_RXBUF_RESET(bf);
56
57 ds = bf->bf_desc;
Sujithbe0418a2008-11-18 09:05:55 +053058 ds->ds_link = 0; /* link to null */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070059 ds->ds_data = bf->bf_buf_addr;
60
Sujithbe0418a2008-11-18 09:05:55 +053061 /* virtual addr of the beginning of the buffer. */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070062 skb = bf->bf_mpdu;
Luis R. Rodriguez9680e8a2009-09-13 23:28:00 -070063 BUG_ON(skb == NULL);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070064 ds->ds_vdata = skb->data;
65
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080066 /*
67 * setup rx descriptors. The rx_bufsize here tells the hardware
Luis R. Rodriguezb4b6cda2008-11-20 17:15:13 -080068 * how much data it can DMA to us and that we are prepared
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080069 * to process
70 */
Sujithb77f4832008-12-07 21:44:03 +053071 ath9k_hw_setuprxdesc(ah, ds,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -080072 common->rx_bufsize,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070073 0);
74
Sujithb77f4832008-12-07 21:44:03 +053075 if (sc->rx.rxlink == NULL)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070076 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
77 else
Sujithb77f4832008-12-07 21:44:03 +053078 *sc->rx.rxlink = bf->bf_daddr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070079
Sujithb77f4832008-12-07 21:44:03 +053080 sc->rx.rxlink = &ds->ds_link;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070081 ath9k_hw_rxena(ah);
82}
83
Sujithff37e332008-11-24 12:07:55 +053084static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
85{
86 /* XXX block beacon interrupts */
87 ath9k_hw_setantenna(sc->sc_ah, antenna);
Sujithb77f4832008-12-07 21:44:03 +053088 sc->rx.defant = antenna;
89 sc->rx.rxotherant = 0;
Sujithff37e332008-11-24 12:07:55 +053090}
91
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070092static void ath_opmode_init(struct ath_softc *sc)
93{
Sujithcbe61d82009-02-09 13:27:12 +053094 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez15107182009-09-10 09:22:37 -070095 struct ath_common *common = ath9k_hw_common(ah);
96
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070097 u32 rfilt, mfilt[2];
98
99 /* configure rx filter */
100 rfilt = ath_calcrxfilter(sc);
101 ath9k_hw_setrxfilter(ah, rfilt);
102
103 /* configure bssid mask */
Sujith2660b812009-02-09 13:27:26 +0530104 if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
Luis R. Rodriguez13b81552009-09-10 17:52:45 -0700105 ath_hw_setbssidmask(common);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700106
107 /* configure operational mode */
108 ath9k_hw_setopmode(ah);
109
110 /* Handle any link-level address change. */
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700111 ath9k_hw_setmac(ah, common->macaddr);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700112
113 /* calculate and install multicast filter */
114 mfilt[0] = mfilt[1] = ~0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700115 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700116}
117
118int ath_rx_init(struct ath_softc *sc, int nbufs)
119{
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -0700120 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700121 struct sk_buff *skb;
122 struct ath_buf *bf;
123 int error = 0;
124
Sujith797fe5c2009-03-30 15:28:45 +0530125 spin_lock_init(&sc->rx.rxflushlock);
126 sc->sc_flags &= ~SC_OP_RXFLUSH;
127 spin_lock_init(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700128
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800129 common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN,
130 min(common->cachelsz, (u16)64));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700131
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700132 ath_print(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800133 common->cachelsz, common->rx_bufsize);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700134
Sujith797fe5c2009-03-30 15:28:45 +0530135 /* Initialize rx descriptors */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700136
Sujith797fe5c2009-03-30 15:28:45 +0530137 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
138 "rx", nbufs, 1);
139 if (error != 0) {
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700140 ath_print(common, ATH_DBG_FATAL,
141 "failed to allocate rx descriptors: %d\n", error);
Sujith797fe5c2009-03-30 15:28:45 +0530142 goto err;
143 }
144
145 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800146 skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
Sujith797fe5c2009-03-30 15:28:45 +0530147 if (skb == NULL) {
148 error = -ENOMEM;
149 goto err;
150 }
151
152 bf->bf_mpdu = skb;
153 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800154 common->rx_bufsize,
Sujith797fe5c2009-03-30 15:28:45 +0530155 DMA_FROM_DEVICE);
156 if (unlikely(dma_mapping_error(sc->dev,
157 bf->bf_buf_addr))) {
158 dev_kfree_skb_any(skb);
159 bf->bf_mpdu = NULL;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700160 ath_print(common, ATH_DBG_FATAL,
161 "dma_mapping_error() on RX init\n");
Sujith797fe5c2009-03-30 15:28:45 +0530162 error = -ENOMEM;
163 goto err;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700164 }
Sujith797fe5c2009-03-30 15:28:45 +0530165 bf->bf_dmacontext = bf->bf_buf_addr;
166 }
167 sc->rx.rxlink = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700168
Sujith797fe5c2009-03-30 15:28:45 +0530169err:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700170 if (error)
171 ath_rx_cleanup(sc);
172
173 return error;
174}
175
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700176void ath_rx_cleanup(struct ath_softc *sc)
177{
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800178 struct ath_hw *ah = sc->sc_ah;
179 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700180 struct sk_buff *skb;
181 struct ath_buf *bf;
182
Sujithb77f4832008-12-07 21:44:03 +0530183 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700184 skb = bf->bf_mpdu;
Luis R. Rodriguez051b9192009-03-23 18:25:01 -0400185 if (skb) {
Sujith797fe5c2009-03-30 15:28:45 +0530186 dma_unmap_single(sc->dev, bf->bf_buf_addr,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800187 common->rx_bufsize, DMA_FROM_DEVICE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700188 dev_kfree_skb(skb);
Luis R. Rodriguez051b9192009-03-23 18:25:01 -0400189 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700190 }
191
Sujithb77f4832008-12-07 21:44:03 +0530192 if (sc->rx.rxdma.dd_desc_len != 0)
193 ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700194}
195
196/*
197 * Calculate the receive filter according to the
198 * operating mode and state:
199 *
200 * o always accept unicast, broadcast, and multicast traffic
201 * o maintain current state of phy error reception (the hal
202 * may enable phy error frames for noise immunity work)
203 * o probe request frames are accepted only when operating in
204 * hostap, adhoc, or monitor modes
205 * o enable promiscuous mode according to the interface state
206 * o accept beacons:
207 * - when operating in adhoc mode so the 802.11 layer creates
208 * node table entries for peers,
209 * - when operating in station mode for collecting rssi data when
210 * the station is otherwise quiet, or
211 * - when operating as a repeater so we see repeater-sta beacons
212 * - when scanning
213 */
214
215u32 ath_calcrxfilter(struct ath_softc *sc)
216{
217#define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
Sujith7dcfdcd2008-08-11 14:03:13 +0530218
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700219 u32 rfilt;
220
221 rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
222 | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
223 | ATH9K_RX_FILTER_MCAST;
224
225 /* If not a STA, enable processing of Probe Requests */
Sujith2660b812009-02-09 13:27:26 +0530226 if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700227 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
228
Jouni Malinen217ba9d2009-03-10 10:55:50 +0200229 /*
230 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
231 * mode interface or when in monitor mode. AP mode does not need this
232 * since it receives all in-BSS frames anyway.
233 */
Sujith2660b812009-02-09 13:27:26 +0530234 if (((sc->sc_ah->opmode != NL80211_IFTYPE_AP) &&
Sujithb77f4832008-12-07 21:44:03 +0530235 (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) ||
Jouni Malinen217ba9d2009-03-10 10:55:50 +0200236 (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700237 rfilt |= ATH9K_RX_FILTER_PROM;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700238
Sujithd42c6b72009-02-04 08:10:22 +0530239 if (sc->rx.rxfilter & FIF_CONTROL)
240 rfilt |= ATH9K_RX_FILTER_CONTROL;
241
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530242 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
243 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
244 rfilt |= ATH9K_RX_FILTER_MYBEACON;
245 else
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700246 rfilt |= ATH9K_RX_FILTER_BEACON;
247
Senthil Balasubramanian66afad02009-09-18 15:06:07 +0530248 if ((AR_SREV_9280_10_OR_LATER(sc->sc_ah) ||
249 AR_SREV_9285_10_OR_LATER(sc->sc_ah)) &&
250 (sc->sc_ah->opmode == NL80211_IFTYPE_AP) &&
251 (sc->rx.rxfilter & FIF_PSPOLL))
Vasanthakumar Thiagarajandbaaa142009-02-19 15:41:52 +0530252 rfilt |= ATH9K_RX_FILTER_PSPOLL;
Sujithbe0418a2008-11-18 09:05:55 +0530253
Sujith7ea310b2009-09-03 12:08:43 +0530254 if (conf_is_ht(&sc->hw->conf))
255 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
256
Javier Cardona5eb6ba82009-08-20 19:12:07 -0700257 if (sc->sec_wiphy || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
Jouni Malinenb93bce22009-03-03 19:23:30 +0200258 /* TODO: only needed if more than one BSSID is in use in
259 * station/adhoc mode */
Javier Cardona5eb6ba82009-08-20 19:12:07 -0700260 /* The following may also be needed for other older chips */
261 if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
262 rfilt |= ATH9K_RX_FILTER_PROM;
Jouni Malinenb93bce22009-03-03 19:23:30 +0200263 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
264 }
265
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700266 return rfilt;
Sujith7dcfdcd2008-08-11 14:03:13 +0530267
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700268#undef RX_FILTER_PRESERVE
269}
270
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700271int ath_startrecv(struct ath_softc *sc)
272{
Sujithcbe61d82009-02-09 13:27:12 +0530273 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700274 struct ath_buf *bf, *tbf;
275
Sujithb77f4832008-12-07 21:44:03 +0530276 spin_lock_bh(&sc->rx.rxbuflock);
277 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700278 goto start_recv;
279
Sujithb77f4832008-12-07 21:44:03 +0530280 sc->rx.rxlink = NULL;
281 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700282 ath_rx_buf_link(sc, bf);
283 }
284
285 /* We could have deleted elements so the list may be empty now */
Sujithb77f4832008-12-07 21:44:03 +0530286 if (list_empty(&sc->rx.rxbuf))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700287 goto start_recv;
288
Sujithb77f4832008-12-07 21:44:03 +0530289 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700290 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
Sujithbe0418a2008-11-18 09:05:55 +0530291 ath9k_hw_rxena(ah);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700292
293start_recv:
Sujithb77f4832008-12-07 21:44:03 +0530294 spin_unlock_bh(&sc->rx.rxbuflock);
Sujithbe0418a2008-11-18 09:05:55 +0530295 ath_opmode_init(sc);
296 ath9k_hw_startpcureceive(ah);
297
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700298 return 0;
299}
300
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700301bool ath_stoprecv(struct ath_softc *sc)
302{
Sujithcbe61d82009-02-09 13:27:12 +0530303 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700304 bool stopped;
305
Sujithbe0418a2008-11-18 09:05:55 +0530306 ath9k_hw_stoppcurecv(ah);
307 ath9k_hw_setrxfilter(ah, 0);
308 stopped = ath9k_hw_stopdmarecv(ah);
Sujithb77f4832008-12-07 21:44:03 +0530309 sc->rx.rxlink = NULL;
Sujithbe0418a2008-11-18 09:05:55 +0530310
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700311 return stopped;
312}
313
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700314void ath_flushrecv(struct ath_softc *sc)
315{
Sujithb77f4832008-12-07 21:44:03 +0530316 spin_lock_bh(&sc->rx.rxflushlock);
Sujith98deeea2008-08-11 14:05:46 +0530317 sc->sc_flags |= SC_OP_RXFLUSH;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700318 ath_rx_tasklet(sc, 1);
Sujith98deeea2008-08-11 14:05:46 +0530319 sc->sc_flags &= ~SC_OP_RXFLUSH;
Sujithb77f4832008-12-07 21:44:03 +0530320 spin_unlock_bh(&sc->rx.rxflushlock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700321}
322
Jouni Malinencc659652009-05-14 21:28:48 +0300323static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
324{
325 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
326 struct ieee80211_mgmt *mgmt;
327 u8 *pos, *end, id, elen;
328 struct ieee80211_tim_ie *tim;
329
330 mgmt = (struct ieee80211_mgmt *)skb->data;
331 pos = mgmt->u.beacon.variable;
332 end = skb->data + skb->len;
333
334 while (pos + 2 < end) {
335 id = *pos++;
336 elen = *pos++;
337 if (pos + elen > end)
338 break;
339
340 if (id == WLAN_EID_TIM) {
341 if (elen < sizeof(*tim))
342 break;
343 tim = (struct ieee80211_tim_ie *) pos;
344 if (tim->dtim_count != 0)
345 break;
346 return tim->bitmap_ctrl & 0x01;
347 }
348
349 pos += elen;
350 }
351
352 return false;
353}
354
Jouni Malinencc659652009-05-14 21:28:48 +0300355static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
356{
357 struct ieee80211_mgmt *mgmt;
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700358 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300359
360 if (skb->len < 24 + 8 + 2 + 2)
361 return;
362
363 mgmt = (struct ieee80211_mgmt *)skb->data;
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700364 if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0)
Jouni Malinencc659652009-05-14 21:28:48 +0300365 return; /* not from our current AP */
366
Sujith1b04b932010-01-08 10:36:05 +0530367 sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
Gabor Juhos293dc5d2009-06-19 12:17:48 +0200368
Sujith1b04b932010-01-08 10:36:05 +0530369 if (sc->ps_flags & PS_BEACON_SYNC) {
370 sc->ps_flags &= ~PS_BEACON_SYNC;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700371 ath_print(common, ATH_DBG_PS,
372 "Reconfigure Beacon timers based on "
373 "timestamp from the AP\n");
Jouni Malinenccdfeab2009-05-20 21:59:08 +0300374 ath_beacon_config(sc, NULL);
375 }
376
Jouni Malinencc659652009-05-14 21:28:48 +0300377 if (ath_beacon_dtim_pending_cab(skb)) {
378 /*
379 * Remain awake waiting for buffered broadcast/multicast
Gabor Juhos58f5fff2009-06-17 20:53:20 +0200380 * frames. If the last broadcast/multicast frame is not
381 * received properly, the next beacon frame will work as
382 * a backup trigger for returning into NETWORK SLEEP state,
383 * so we are waiting for it as well.
Jouni Malinencc659652009-05-14 21:28:48 +0300384 */
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700385 ath_print(common, ATH_DBG_PS, "Received DTIM beacon indicating "
386 "buffered broadcast/multicast frame(s)\n");
Sujith1b04b932010-01-08 10:36:05 +0530387 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
Jouni Malinencc659652009-05-14 21:28:48 +0300388 return;
389 }
390
Sujith1b04b932010-01-08 10:36:05 +0530391 if (sc->ps_flags & PS_WAIT_FOR_CAB) {
Jouni Malinencc659652009-05-14 21:28:48 +0300392 /*
393 * This can happen if a broadcast frame is dropped or the AP
394 * fails to send a frame indicating that all CAB frames have
395 * been delivered.
396 */
Sujith1b04b932010-01-08 10:36:05 +0530397 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700398 ath_print(common, ATH_DBG_PS,
399 "PS wait for CAB frames timed out\n");
Jouni Malinencc659652009-05-14 21:28:48 +0300400 }
Jouni Malinencc659652009-05-14 21:28:48 +0300401}
402
403static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb)
404{
405 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700406 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinencc659652009-05-14 21:28:48 +0300407
408 hdr = (struct ieee80211_hdr *)skb->data;
409
410 /* Process Beacon and CAB receive in PS state */
Sujith1b04b932010-01-08 10:36:05 +0530411 if ((sc->ps_flags & PS_WAIT_FOR_BEACON) &&
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300412 ieee80211_is_beacon(hdr->frame_control))
Jouni Malinencc659652009-05-14 21:28:48 +0300413 ath_rx_ps_beacon(sc, skb);
Sujith1b04b932010-01-08 10:36:05 +0530414 else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
Jouni Malinencc659652009-05-14 21:28:48 +0300415 (ieee80211_is_data(hdr->frame_control) ||
416 ieee80211_is_action(hdr->frame_control)) &&
417 is_multicast_ether_addr(hdr->addr1) &&
418 !ieee80211_has_moredata(hdr->frame_control)) {
Jouni Malinencc659652009-05-14 21:28:48 +0300419 /*
420 * No more broadcast/multicast frames to be received at this
421 * point.
422 */
Sujith1b04b932010-01-08 10:36:05 +0530423 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700424 ath_print(common, ATH_DBG_PS,
425 "All PS CAB frames received, back to sleep\n");
Sujith1b04b932010-01-08 10:36:05 +0530426 } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300427 !is_multicast_ether_addr(hdr->addr1) &&
428 !ieee80211_has_morefrags(hdr->frame_control)) {
Sujith1b04b932010-01-08 10:36:05 +0530429 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700430 ath_print(common, ATH_DBG_PS,
431 "Going back to sleep after having received "
Pavel Roskinf643e512010-01-29 17:22:12 -0500432 "PS-Poll data (0x%lx)\n",
Sujith1b04b932010-01-08 10:36:05 +0530433 sc->ps_flags & (PS_WAIT_FOR_BEACON |
434 PS_WAIT_FOR_CAB |
435 PS_WAIT_FOR_PSPOLL_DATA |
436 PS_WAIT_FOR_TX_ACK));
Jouni Malinencc659652009-05-14 21:28:48 +0300437 }
438}
439
Luis R. Rodriguezb4afffc2009-11-02 11:36:08 -0800440static void ath_rx_send_to_mac80211(struct ieee80211_hw *hw,
441 struct ath_softc *sc, struct sk_buff *skb,
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -0800442 struct ieee80211_rx_status *rxs)
Jouni Malinen9d64a3c2009-05-14 21:28:47 +0300443{
444 struct ieee80211_hdr *hdr;
445
446 hdr = (struct ieee80211_hdr *)skb->data;
447
448 /* Send the frame to mac80211 */
449 if (is_multicast_ether_addr(hdr->addr1)) {
450 int i;
451 /*
452 * Deliver broadcast/multicast frames to all suitable
453 * virtual wiphys.
454 */
455 /* TODO: filter based on channel configuration */
456 for (i = 0; i < sc->num_sec_wiphy; i++) {
457 struct ath_wiphy *aphy = sc->sec_wiphy[i];
458 struct sk_buff *nskb;
459 if (aphy == NULL)
460 continue;
461 nskb = skb_copy(skb, GFP_ATOMIC);
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -0800462 if (!nskb)
463 continue;
464 ieee80211_rx(aphy->hw, nskb);
Jouni Malinen9d64a3c2009-05-14 21:28:47 +0300465 }
Johannes Bergf1d58c22009-06-17 13:13:00 +0200466 ieee80211_rx(sc->hw, skb);
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -0800467 } else
Jouni Malinen9d64a3c2009-05-14 21:28:47 +0300468 /* Deliver unicast frames based on receiver address */
Luis R. Rodriguezb4afffc2009-11-02 11:36:08 -0800469 ieee80211_rx(hw, skb);
Jouni Malinen9d64a3c2009-05-14 21:28:47 +0300470}
471
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700472int ath_rx_tasklet(struct ath_softc *sc, int flush)
473{
474#define PA2DESC(_sc, _pa) \
Sujithb77f4832008-12-07 21:44:03 +0530475 ((struct ath_desc *)((caddr_t)(_sc)->rx.rxdma.dd_desc + \
476 ((_pa) - (_sc)->rx.rxdma.dd_desc_paddr)))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700477
Sujithbe0418a2008-11-18 09:05:55 +0530478 struct ath_buf *bf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700479 struct ath_desc *ds;
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800480 struct sk_buff *skb = NULL, *requeue_skb;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -0800481 struct ieee80211_rx_status *rxs;
Sujithcbe61d82009-02-09 13:27:12 +0530482 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez27c51f12009-09-10 11:08:14 -0700483 struct ath_common *common = ath9k_hw_common(ah);
Luis R. Rodriguezb4afffc2009-11-02 11:36:08 -0800484 /*
485 * The hw can techncically differ from common->hw when using ath9k
486 * virtual wiphy so to account for that we iterate over the active
487 * wiphys and find the appropriate wiphy and therefore hw.
488 */
489 struct ieee80211_hw *hw = NULL;
Sujithbe0418a2008-11-18 09:05:55 +0530490 struct ieee80211_hdr *hdr;
Luis R. Rodriguezc9b14172009-11-04 16:47:22 -0800491 int retval;
Sujithbe0418a2008-11-18 09:05:55 +0530492 bool decrypt_error = false;
Felix Fietkau29bffa92010-03-29 20:14:23 -0700493 struct ath_rx_status rs;
Sujithbe0418a2008-11-18 09:05:55 +0530494
Sujithb77f4832008-12-07 21:44:03 +0530495 spin_lock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700496
497 do {
498 /* If handling rx interrupt and flush is in progress => exit */
Sujith98deeea2008-08-11 14:05:46 +0530499 if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700500 break;
501
Sujithb77f4832008-12-07 21:44:03 +0530502 if (list_empty(&sc->rx.rxbuf)) {
503 sc->rx.rxlink = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700504 break;
505 }
506
Sujithb77f4832008-12-07 21:44:03 +0530507 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700508 ds = bf->bf_desc;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700509
510 /*
511 * Must provide the virtual address of the current
512 * descriptor, the physical address, and the virtual
513 * address of the next descriptor in the h/w chain.
514 * This allows the HAL to look ahead to see if the
515 * hardware is done with a descriptor by checking the
516 * done bit in the following descriptor and the address
517 * of the current descriptor the DMA engine is working
518 * on. All this is necessary because of our use of
519 * a self-linked list to avoid rx overruns.
520 */
Felix Fietkau29bffa92010-03-29 20:14:23 -0700521 memset(&rs, 0, sizeof(rs));
522 retval = ath9k_hw_rxprocdesc(ah, ds, &rs, 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700523 if (retval == -EINPROGRESS) {
Felix Fietkau29bffa92010-03-29 20:14:23 -0700524 struct ath_rx_status trs;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700525 struct ath_buf *tbf;
526 struct ath_desc *tds;
527
Felix Fietkau29bffa92010-03-29 20:14:23 -0700528 memset(&trs, 0, sizeof(trs));
Sujithb77f4832008-12-07 21:44:03 +0530529 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
530 sc->rx.rxlink = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700531 break;
532 }
533
534 tbf = list_entry(bf->list.next, struct ath_buf, list);
535
536 /*
537 * On some hardware the descriptor status words could
538 * get corrupted, including the done bit. Because of
539 * this, check if the next descriptor's done bit is
540 * set or not.
541 *
542 * If the next descriptor's done bit is set, the current
543 * descriptor has been corrupted. Force s/w to discard
544 * this descriptor and continue...
545 */
546
547 tds = tbf->bf_desc;
Felix Fietkau29bffa92010-03-29 20:14:23 -0700548 retval = ath9k_hw_rxprocdesc(ah, tds, &trs, 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700549 if (retval == -EINPROGRESS) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700550 break;
551 }
552 }
553
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700554 skb = bf->bf_mpdu;
Sujithbe0418a2008-11-18 09:05:55 +0530555 if (!skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700556 continue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700557
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700558 /*
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +0530559 * Synchronize the DMA transfer with CPU before
560 * 1. accessing the frame
561 * 2. requeueing the same buffer to h/w
562 */
Gabor Juhos7da3c552009-01-14 20:17:03 +0100563 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800564 common->rx_bufsize,
Gabor Juhos7da3c552009-01-14 20:17:03 +0100565 DMA_FROM_DEVICE);
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +0530566
Luis R. Rodriguezb4afffc2009-11-02 11:36:08 -0800567 hdr = (struct ieee80211_hdr *) skb->data;
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -0800568 rxs = IEEE80211_SKB_RXCB(skb);
569
Luis R. Rodriguezb4afffc2009-11-02 11:36:08 -0800570 hw = ath_get_virt_hw(sc, hdr);
571
Felix Fietkau29bffa92010-03-29 20:14:23 -0700572 ath_debug_stat_rx(sc, &rs);
Sujith1395d3f2010-01-08 10:36:11 +0530573
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +0530574 /*
Sujithbe0418a2008-11-18 09:05:55 +0530575 * If we're asked to flush receive queue, directly
576 * chain it back at the queue without processing it.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700577 */
Sujithbe0418a2008-11-18 09:05:55 +0530578 if (flush)
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800579 goto requeue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700580
Felix Fietkau29bffa92010-03-29 20:14:23 -0700581 retval = ath9k_cmn_rx_skb_preprocess(common, hw, skb, &rs,
Luis R. Rodriguezdb86f072009-11-05 08:44:39 -0800582 rxs, &decrypt_error);
Luis R. Rodriguez1e875e92009-11-04 16:34:33 -0800583 if (retval)
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800584 goto requeue;
585
586 /* Ensure we always have an skb to requeue once we are done
587 * processing the current buffer's skb */
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800588 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800589
590 /* If there is no memory we ignore the current RX'd frame,
591 * tell hardware it can give us a new frame using the old
Sujithb77f4832008-12-07 21:44:03 +0530592 * skb and put it at the tail of the sc->rx.rxbuf list for
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800593 * processing. */
594 if (!requeue_skb)
595 goto requeue;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700596
Vasanthakumar Thiagarajan9bf9fca2008-12-15 20:40:46 +0530597 /* Unmap the frame */
Gabor Juhos7da3c552009-01-14 20:17:03 +0100598 dma_unmap_single(sc->dev, bf->bf_buf_addr,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800599 common->rx_bufsize,
Gabor Juhos7da3c552009-01-14 20:17:03 +0100600 DMA_FROM_DEVICE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700601
Felix Fietkau29bffa92010-03-29 20:14:23 -0700602 skb_put(skb, rs.rs_datalen);
Sujithbe0418a2008-11-18 09:05:55 +0530603
Felix Fietkau29bffa92010-03-29 20:14:23 -0700604 ath9k_cmn_rx_skb_postprocess(common, skb, &rs,
Luis R. Rodriguezdb86f072009-11-05 08:44:39 -0800605 rxs, decrypt_error);
Sujithbe0418a2008-11-18 09:05:55 +0530606
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800607 /* We will now give hardware our shiny new allocated skb */
608 bf->bf_mpdu = requeue_skb;
Gabor Juhos7da3c552009-01-14 20:17:03 +0100609 bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800610 common->rx_bufsize,
611 DMA_FROM_DEVICE);
Gabor Juhos7da3c552009-01-14 20:17:03 +0100612 if (unlikely(dma_mapping_error(sc->dev,
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -0800613 bf->bf_buf_addr))) {
614 dev_kfree_skb_any(requeue_skb);
615 bf->bf_mpdu = NULL;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700616 ath_print(common, ATH_DBG_FATAL,
617 "dma_mapping_error() on RX\n");
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -0800618 ath_rx_send_to_mac80211(hw, sc, skb, rxs);
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 */
Felix Fietkau29bffa92010-03-29 20:14:23 -0700627 if (sc->rx.defant != rs.rs_antenna) {
Sujithb77f4832008-12-07 21:44:03 +0530628 if (++sc->rx.rxotherant >= 3)
Felix Fietkau29bffa92010-03-29 20:14:23 -0700629 ath_setdefantenna(sc, rs.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
Sujith1b04b932010-01-08 10:36:05 +0530634 if (unlikely(sc->ps_flags & (PS_WAIT_FOR_BEACON |
635 PS_WAIT_FOR_CAB |
636 PS_WAIT_FOR_PSPOLL_DATA)))
Jouni Malinencc659652009-05-14 21:28:48 +0300637 ath_rx_ps(sc, skb);
638
Luis R. Rodriguez5ca42622009-11-04 08:20:42 -0800639 ath_rx_send_to_mac80211(hw, sc, skb, rxs);
Jouni Malinencc659652009-05-14 21:28:48 +0300640
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800641requeue:
Sujithb77f4832008-12-07 21:44:03 +0530642 list_move_tail(&bf->list, &sc->rx.rxbuf);
Luis R. Rodriguezcb71d9b2008-11-21 17:41:33 -0800643 ath_rx_buf_link(sc, bf);
Sujithbe0418a2008-11-18 09:05:55 +0530644 } while (1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700645
Sujithb77f4832008-12-07 21:44:03 +0530646 spin_unlock_bh(&sc->rx.rxbuflock);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700647
648 return 0;
649#undef PA2DESC
650}