blob: 377d2df05316d7bb56bb6cf94a9e95cac54a6612 [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
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070017#include "core.h"
18
19/*
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070020 * This function will modify certain transmit queue properties depending on
21 * the operating mode of the station (AP or AdHoc). Parameters are AIFS
22 * settings and channel width min/max
23*/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070024static int ath_beaconq_config(struct ath_softc *sc)
25{
26 struct ath_hal *ah = sc->sc_ah;
Sujithea9880f2008-08-07 10:53:10 +053027 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070028
Sujithea9880f2008-08-07 10:53:10 +053029 ath9k_hw_get_txq_props(ah, sc->sc_bhalq, &qi);
Sujithb4696c8b2008-08-11 14:04:52 +053030 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070031 /* Always burst out beacon and CAB traffic. */
32 qi.tqi_aifs = 1;
33 qi.tqi_cwmin = 0;
34 qi.tqi_cwmax = 0;
35 } else {
36 /* Adhoc mode; important thing is to use 2x cwmin. */
37 qi.tqi_aifs = sc->sc_beacon_qi.tqi_aifs;
38 qi.tqi_cwmin = 2*sc->sc_beacon_qi.tqi_cwmin;
39 qi.tqi_cwmax = sc->sc_beacon_qi.tqi_cwmax;
40 }
41
Sujithea9880f2008-08-07 10:53:10 +053042 if (!ath9k_hw_set_txq_props(ah, sc->sc_bhalq, &qi)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070043 DPRINTF(sc, ATH_DBG_FATAL,
44 "%s: unable to update h/w beacon queue parameters\n",
45 __func__);
46 return 0;
47 } else {
48 ath9k_hw_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */
49 return 1;
50 }
51}
52
Sujithff37e332008-11-24 12:07:55 +053053static void ath_bstuck_process(struct ath_softc *sc)
54{
55 DPRINTF(sc, ATH_DBG_BEACON,
56 "%s: stuck beacon; resetting (bmiss count %u)\n",
57 __func__, sc->sc_bmisscount);
58 ath_reset(sc, false);
59}
60
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070061/*
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070062 * Associates the beacon frame buffer with a transmit descriptor. Will set
63 * up all required antenna switch parameters, rate codes, and channel flags.
64 * Beacons are always sent out at the lowest rate, and are not retried.
65*/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070066static void ath_beacon_setup(struct ath_softc *sc,
Sujith980b24d2008-09-17 10:15:09 +053067 struct ath_vap *avp, struct ath_buf *bf)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070068{
69 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
70 struct ath_hal *ah = sc->sc_ah;
71 struct ath_desc *ds;
Sujith980b24d2008-09-17 10:15:09 +053072 struct ath9k_11n_rate_series series[4];
Sujithe63835b2008-11-18 09:07:53 +053073 struct ath_rate_table *rt;
Sujith980b24d2008-09-17 10:15:09 +053074 int flags, antenna;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070075 u8 rix, rate;
76 int ctsrate = 0;
77 int ctsduration = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070078
79 DPRINTF(sc, ATH_DBG_BEACON, "%s: m %p len %u\n",
80 __func__, skb, skb->len);
81
82 /* setup descriptors */
83 ds = bf->bf_desc;
84
85 flags = ATH9K_TXDESC_NOACK;
86
Sujithb4696c8b2008-08-11 14:04:52 +053087 if (sc->sc_ah->ah_opmode == ATH9K_M_IBSS &&
Sujith60b67f52008-08-07 10:52:38 +053088 (ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070089 ds->ds_link = bf->bf_daddr; /* self-linked */
90 flags |= ATH9K_TXDESC_VEOL;
91 /* Let hardware handle antenna switching. */
92 antenna = 0;
93 } else {
94 ds->ds_link = 0;
95 /*
96 * Switch antenna every beacon.
97 * Should only switch every beacon period, not for every
98 * SWBA's
99 * XXX assumes two antenna
100 */
101 antenna = ((sc->ast_be_xmit / sc->sc_nbcnvaps) & 1 ? 2 : 1);
102 }
103
104 ds->ds_data = bf->bf_buf_addr;
105
106 /*
107 * Calculate rate code.
108 * XXX everything at min xmit rate
109 */
Sujith86b89ee2008-08-07 10:54:57 +0530110 rix = 0;
Sujithe63835b2008-11-18 09:07:53 +0530111 rt = sc->hw_rate_table[sc->sc_curmode];
112 rate = rt->info[rix].ratecode;
Sujith672840a2008-08-11 14:05:08 +0530113 if (sc->sc_flags & SC_OP_PREAMBLE_SHORT)
Sujithe63835b2008-11-18 09:07:53 +0530114 rate |= rt->info[rix].short_preamble;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700115
Sujithff9b6622008-08-14 13:27:16 +0530116 ath9k_hw_set11n_txdesc(ah, ds,
Sujith980b24d2008-09-17 10:15:09 +0530117 skb->len + FCS_LEN, /* frame length */
118 ATH9K_PKT_TYPE_BEACON, /* Atheros packet type */
Sujith528f0c62008-10-29 10:14:26 +0530119 MAX_RATE_POWER, /* FIXME */
Sujith980b24d2008-09-17 10:15:09 +0530120 ATH9K_TXKEYIX_INVALID, /* no encryption */
121 ATH9K_KEY_TYPE_CLEAR, /* no encryption */
122 flags /* no ack,
123 veol for beacons */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700124 );
125
126 /* NB: beacon's BufLen must be a multiple of 4 bytes */
Sujithff9b6622008-08-14 13:27:16 +0530127 ath9k_hw_filltxdesc(ah, ds,
128 roundup(skb->len, 4), /* buffer length */
Sujith980b24d2008-09-17 10:15:09 +0530129 true, /* first segment */
130 true, /* last segment */
131 ds /* first descriptor */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700132 );
133
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700134 memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700135 series[0].Tries = 1;
136 series[0].Rate = rate;
137 series[0].ChSel = sc->sc_tx_chainmask;
138 series[0].RateFlags = (ctsrate) ? ATH9K_RATESERIES_RTS_CTS : 0;
139 ath9k_hw_set11n_ratescenario(ah, ds, ds, 0,
140 ctsrate, ctsduration, series, 4, 0);
141}
142
Sujithff37e332008-11-24 12:07:55 +0530143/* Generate beacon frame and queue cab data for a vap */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700144static struct ath_buf *ath_beacon_generate(struct ath_softc *sc, int if_id)
145{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700146 struct ath_buf *bf;
147 struct ath_vap *avp;
148 struct sk_buff *skb;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700149 struct ath_txq *cabq;
Sujith5640b082008-10-29 10:16:06 +0530150 struct ieee80211_vif *vif;
Jouni Malinen147583c2008-08-11 14:01:50 +0300151 struct ieee80211_tx_info *info;
Sujith980b24d2008-09-17 10:15:09 +0530152 int cabq_depth;
153
Sujith5640b082008-10-29 10:16:06 +0530154 vif = sc->sc_vaps[if_id];
155 ASSERT(vif);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700156
Sujith5640b082008-10-29 10:16:06 +0530157 avp = (void *)vif->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700158 cabq = sc->sc_cabq;
159
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700160 if (avp->av_bcbuf == NULL) {
161 DPRINTF(sc, ATH_DBG_BEACON, "%s: avp=%p av_bcbuf=%p\n",
162 __func__, avp, avp->av_bcbuf);
163 return NULL;
164 }
Sujith980b24d2008-09-17 10:15:09 +0530165
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700166 bf = avp->av_bcbuf;
Sujith980b24d2008-09-17 10:15:09 +0530167 skb = (struct sk_buff *)bf->bf_mpdu;
Jouni Malinena8fff502008-08-11 14:01:48 +0300168 if (skb) {
169 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
170 skb_end_pointer(skb) - skb->head,
171 PCI_DMA_TODEVICE);
172 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700173
Sujith5640b082008-10-29 10:16:06 +0530174 skb = ieee80211_beacon_get(sc->hw, vif);
Jouni Malinena8fff502008-08-11 14:01:48 +0300175 bf->bf_mpdu = skb;
176 if (skb == NULL)
177 return NULL;
Sujith980b24d2008-09-17 10:15:09 +0530178
Jouni Malinen147583c2008-08-11 14:01:50 +0300179 info = IEEE80211_SKB_CB(skb);
180 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
181 /*
182 * TODO: make sure the seq# gets assigned properly (vs. other
183 * TX frames)
184 */
Sujith980b24d2008-09-17 10:15:09 +0530185 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Jouni Malinen147583c2008-08-11 14:01:50 +0300186 sc->seq_no += 0x10;
187 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
188 hdr->seq_ctrl |= cpu_to_le16(sc->seq_no);
189 }
Sujith980b24d2008-09-17 10:15:09 +0530190
Jouni Malinena8fff502008-08-11 14:01:48 +0300191 bf->bf_buf_addr = bf->bf_dmacontext =
192 pci_map_single(sc->pdev, skb->data,
193 skb_end_pointer(skb) - skb->head,
194 PCI_DMA_TODEVICE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700195
Sujith5640b082008-10-29 10:16:06 +0530196 skb = ieee80211_get_buffered_bc(sc->hw, vif);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700197
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700198 /*
199 * if the CABQ traffic from previous DTIM is pending and the current
200 * beacon is also a DTIM.
201 * 1) if there is only one vap let the cab traffic continue.
202 * 2) if there are more than one vap and we are using staggered
203 * beacons, then drain the cabq by dropping all the frames in
204 * the cabq so that the current vaps cab traffic can be scheduled.
205 */
206 spin_lock_bh(&cabq->axq_lock);
207 cabq_depth = cabq->axq_depth;
208 spin_unlock_bh(&cabq->axq_lock);
209
Jouni Malinene022edb2008-08-22 17:31:33 +0300210 if (skb && cabq_depth) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700211 /*
212 * Unlock the cabq lock as ath_tx_draintxq acquires
213 * the lock again which is a common function and that
214 * acquires txq lock inside.
215 */
216 if (sc->sc_nvaps > 1) {
217 ath_tx_draintxq(sc, cabq, false);
218 DPRINTF(sc, ATH_DBG_BEACON,
219 "%s: flush previous cabq traffic\n", __func__);
220 }
221 }
222
223 /* Construct tx descriptor. */
224 ath_beacon_setup(sc, avp, bf);
225
226 /*
227 * Enable the CAB queue before the beacon queue to
228 * insure cab frames are triggered by this beacon.
229 */
Jouni Malinene022edb2008-08-22 17:31:33 +0300230 while (skb) {
231 ath_tx_cabq(sc, skb);
Sujith5640b082008-10-29 10:16:06 +0530232 skb = ieee80211_get_buffered_bc(sc->hw, vif);
Jouni Malinene022edb2008-08-22 17:31:33 +0300233 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700234
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700235 return bf;
236}
237
238/*
239 * Startup beacon transmission for adhoc mode when they are sent entirely
240 * by the hardware using the self-linked descriptor + veol trick.
241*/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700242static void ath_beacon_start_adhoc(struct ath_softc *sc, int if_id)
243{
Sujith5640b082008-10-29 10:16:06 +0530244 struct ieee80211_vif *vif;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700245 struct ath_hal *ah = sc->sc_ah;
246 struct ath_buf *bf;
247 struct ath_vap *avp;
248 struct sk_buff *skb;
249
Sujith5640b082008-10-29 10:16:06 +0530250 vif = sc->sc_vaps[if_id];
251 ASSERT(vif);
252
253 avp = (void *)vif->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700254
255 if (avp->av_bcbuf == NULL) {
256 DPRINTF(sc, ATH_DBG_BEACON, "%s: avp=%p av_bcbuf=%p\n",
257 __func__, avp, avp != NULL ? avp->av_bcbuf : NULL);
258 return;
259 }
260 bf = avp->av_bcbuf;
261 skb = (struct sk_buff *) bf->bf_mpdu;
262
263 /* Construct tx descriptor. */
264 ath_beacon_setup(sc, avp, bf);
265
266 /* NB: caller is known to have already stopped tx dma */
267 ath9k_hw_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
268 ath9k_hw_txstart(ah, sc->sc_bhalq);
269 DPRINTF(sc, ATH_DBG_BEACON, "%s: TXDP%u = %llx (%p)\n", __func__,
270 sc->sc_bhalq, ito64(bf->bf_daddr), bf->bf_desc);
271}
272
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700273int ath_beaconq_setup(struct ath_hal *ah)
274{
Sujithea9880f2008-08-07 10:53:10 +0530275 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700276
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700277 memset(&qi, 0, sizeof(qi));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700278 qi.tqi_aifs = 1;
279 qi.tqi_cwmin = 0;
280 qi.tqi_cwmax = 0;
281 /* NB: don't enable any interrupts */
282 return ath9k_hw_setuptxqueue(ah, ATH9K_TX_QUEUE_BEACON, &qi);
283}
284
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700285int ath_beacon_alloc(struct ath_softc *sc, int if_id)
286{
Sujith5640b082008-10-29 10:16:06 +0530287 struct ieee80211_vif *vif;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700288 struct ath_vap *avp;
Sujith980b24d2008-09-17 10:15:09 +0530289 struct ieee80211_hdr *hdr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700290 struct ath_buf *bf;
291 struct sk_buff *skb;
Sujith459f5f92008-09-17 10:15:36 +0530292 __le64 tstamp;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700293
Sujith5640b082008-10-29 10:16:06 +0530294 vif = sc->sc_vaps[if_id];
295 ASSERT(vif);
296
297 avp = (void *)vif->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700298
299 /* Allocate a beacon descriptor if we haven't done so. */
300 if (!avp->av_bcbuf) {
Sujith980b24d2008-09-17 10:15:09 +0530301 /* Allocate beacon state for hostap/ibss. We know
302 * a buffer is available. */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700303 avp->av_bcbuf = list_first_entry(&sc->sc_bbuf,
Sujith980b24d2008-09-17 10:15:09 +0530304 struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700305 list_del(&avp->av_bcbuf->list);
306
Sujithb4696c8b2008-08-11 14:04:52 +0530307 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP ||
Sujith60b67f52008-08-07 10:52:38 +0530308 !(sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700309 int slot;
310 /*
311 * Assign the vap to a beacon xmit slot. As
312 * above, this cannot fail to find one.
313 */
314 avp->av_bslot = 0;
315 for (slot = 0; slot < ATH_BCBUF; slot++)
316 if (sc->sc_bslot[slot] == ATH_IF_ID_ANY) {
317 /*
318 * XXX hack, space out slots to better
319 * deal with misses
320 */
321 if (slot+1 < ATH_BCBUF &&
322 sc->sc_bslot[slot+1] ==
323 ATH_IF_ID_ANY) {
324 avp->av_bslot = slot+1;
325 break;
326 }
327 avp->av_bslot = slot;
328 /* NB: keep looking for a double slot */
329 }
330 BUG_ON(sc->sc_bslot[avp->av_bslot] != ATH_IF_ID_ANY);
331 sc->sc_bslot[avp->av_bslot] = if_id;
332 sc->sc_nbcnvaps++;
333 }
334 }
335
336 /* release the previous beacon frame , if it already exists. */
337 bf = avp->av_bcbuf;
338 if (bf->bf_mpdu != NULL) {
339 skb = (struct sk_buff *)bf->bf_mpdu;
Jouni Malinena8fff502008-08-11 14:01:48 +0300340 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
341 skb_end_pointer(skb) - skb->head,
342 PCI_DMA_TODEVICE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700343 dev_kfree_skb_any(skb);
344 bf->bf_mpdu = NULL;
345 }
346
347 /*
Sujith980b24d2008-09-17 10:15:09 +0530348 * NB: the beacon data buffer must be 32-bit aligned.
Jouni Malinene022edb2008-08-22 17:31:33 +0300349 * FIXME: Fill avp->av_btxctl.txpower and
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700350 * avp->av_btxctl.shortPreamble
351 */
Sujith5640b082008-10-29 10:16:06 +0530352 skb = ieee80211_beacon_get(sc->hw, vif);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700353 if (skb == NULL) {
354 DPRINTF(sc, ATH_DBG_BEACON, "%s: cannot get skb\n",
355 __func__);
356 return -ENOMEM;
357 }
358
Sujith459f5f92008-09-17 10:15:36 +0530359 tstamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
360 sc->bc_tstamp = le64_to_cpu(tstamp);
361
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700362 /*
363 * Calculate a TSF adjustment factor required for
364 * staggered beacons. Note that we assume the format
365 * of the beacon frame leaves the tstamp field immediately
366 * following the header.
367 */
368 if (avp->av_bslot > 0) {
369 u64 tsfadjust;
370 __le64 val;
371 int intval;
372
Jouni Malinena8fff502008-08-11 14:01:48 +0300373 intval = sc->hw->conf.beacon_int ?
374 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700375
376 /*
377 * The beacon interval is in TU's; the TSF in usecs.
378 * We figure out how many TU's to add to align the
379 * timestamp then convert to TSF units and handle
380 * byte swapping before writing it in the frame.
381 * The hardware will then add this each time a beacon
382 * frame is sent. Note that we align vap's 1..N
383 * and leave vap 0 untouched. This means vap 0
384 * has a timestamp in one beacon interval while the
385 * others get a timestamp aligned to the next interval.
386 */
387 tsfadjust = (intval * (ATH_BCBUF - avp->av_bslot)) / ATH_BCBUF;
388 val = cpu_to_le64(tsfadjust << 10); /* TU->TSF */
389
390 DPRINTF(sc, ATH_DBG_BEACON,
391 "%s: %s beacons, bslot %d intval %u tsfadjust %llu\n",
392 __func__, "stagger",
393 avp->av_bslot, intval, (unsigned long long)tsfadjust);
394
Sujith980b24d2008-09-17 10:15:09 +0530395 hdr = (struct ieee80211_hdr *)skb->data;
396 memcpy(&hdr[1], &val, sizeof(val));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700397 }
398
Jouni Malinena8fff502008-08-11 14:01:48 +0300399 bf->bf_buf_addr = bf->bf_dmacontext =
400 pci_map_single(sc->pdev, skb->data,
401 skb_end_pointer(skb) - skb->head,
402 PCI_DMA_TODEVICE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700403 bf->bf_mpdu = skb;
404
405 return 0;
406}
407
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700408void ath_beacon_return(struct ath_softc *sc, struct ath_vap *avp)
409{
410 if (avp->av_bcbuf != NULL) {
411 struct ath_buf *bf;
412
413 if (avp->av_bslot != -1) {
414 sc->sc_bslot[avp->av_bslot] = ATH_IF_ID_ANY;
415 sc->sc_nbcnvaps--;
416 }
417
418 bf = avp->av_bcbuf;
419 if (bf->bf_mpdu != NULL) {
420 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
Jouni Malinena8fff502008-08-11 14:01:48 +0300421 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
422 skb_end_pointer(skb) - skb->head,
423 PCI_DMA_TODEVICE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700424 dev_kfree_skb_any(skb);
425 bf->bf_mpdu = NULL;
426 }
427 list_add_tail(&bf->list, &sc->sc_bbuf);
428
429 avp->av_bcbuf = NULL;
430 }
431}
432
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700433void ath9k_beacon_tasklet(unsigned long data)
434{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700435 struct ath_softc *sc = (struct ath_softc *)data;
436 struct ath_hal *ah = sc->sc_ah;
437 struct ath_buf *bf = NULL;
438 int slot, if_id;
439 u32 bfaddr;
440 u32 rx_clear = 0, rx_frame = 0, tx_frame = 0;
441 u32 show_cycles = 0;
442 u32 bc = 0; /* beacon count */
443 u64 tsf;
444 u32 tsftu;
445 u16 intval;
446
Sujith98deeea2008-08-11 14:05:46 +0530447 if (sc->sc_flags & SC_OP_NO_RESET) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700448 show_cycles = ath9k_hw_GetMibCycleCountsPct(ah,
Sujithff37e332008-11-24 12:07:55 +0530449 &rx_clear, &rx_frame, &tx_frame);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700450 }
451
452 /*
453 * Check if the previous beacon has gone out. If
454 * not don't try to post another, skip this period
455 * and wait for the next. Missed beacons indicate
456 * a problem and should not occur. If we miss too
457 * many consecutive beacons reset the device.
Sujith980b24d2008-09-17 10:15:09 +0530458 *
459 * FIXME: Clean up this mess !!
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700460 */
461 if (ath9k_hw_numtxpending(ah, sc->sc_bhalq) != 0) {
462 sc->sc_bmisscount++;
463 /* XXX: doth needs the chanchange IE countdown decremented.
464 * We should consider adding a mac80211 call to indicate
465 * a beacon miss so appropriate action could be taken
466 * (in that layer).
467 */
468 if (sc->sc_bmisscount < BSTUCK_THRESH) {
Sujith98deeea2008-08-11 14:05:46 +0530469 if (sc->sc_flags & SC_OP_NO_RESET) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700470 DPRINTF(sc, ATH_DBG_BEACON,
471 "%s: missed %u consecutive beacons\n",
472 __func__, sc->sc_bmisscount);
473 if (show_cycles) {
474 /*
Sujith980b24d2008-09-17 10:15:09 +0530475 * Display cycle counter stats from HW
476 * to aide in debug of stickiness.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700477 */
Sujith980b24d2008-09-17 10:15:09 +0530478 DPRINTF(sc, ATH_DBG_BEACON,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700479 "%s: busy times: rx_clear=%d, "
480 "rx_frame=%d, tx_frame=%d\n",
481 __func__, rx_clear, rx_frame,
482 tx_frame);
483 } else {
Sujith980b24d2008-09-17 10:15:09 +0530484 DPRINTF(sc, ATH_DBG_BEACON,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700485 "%s: unable to obtain "
486 "busy times\n", __func__);
487 }
488 } else {
489 DPRINTF(sc, ATH_DBG_BEACON,
490 "%s: missed %u consecutive beacons\n",
491 __func__, sc->sc_bmisscount);
492 }
493 } else if (sc->sc_bmisscount >= BSTUCK_THRESH) {
Sujith98deeea2008-08-11 14:05:46 +0530494 if (sc->sc_flags & SC_OP_NO_RESET) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700495 if (sc->sc_bmisscount == BSTUCK_THRESH) {
Sujith980b24d2008-09-17 10:15:09 +0530496 DPRINTF(sc, ATH_DBG_BEACON,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700497 "%s: beacon is officially "
498 "stuck\n", __func__);
499 ath9k_hw_dmaRegDump(ah);
500 }
501 } else {
502 DPRINTF(sc, ATH_DBG_BEACON,
503 "%s: beacon is officially stuck\n",
504 __func__);
505 ath_bstuck_process(sc);
506 }
507 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700508 return;
509 }
Sujith980b24d2008-09-17 10:15:09 +0530510
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700511 if (sc->sc_bmisscount != 0) {
Sujith98deeea2008-08-11 14:05:46 +0530512 if (sc->sc_flags & SC_OP_NO_RESET) {
Sujith980b24d2008-09-17 10:15:09 +0530513 DPRINTF(sc, ATH_DBG_BEACON,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700514 "%s: resume beacon xmit after %u misses\n",
515 __func__, sc->sc_bmisscount);
516 } else {
517 DPRINTF(sc, ATH_DBG_BEACON,
518 "%s: resume beacon xmit after %u misses\n",
519 __func__, sc->sc_bmisscount);
520 }
521 sc->sc_bmisscount = 0;
522 }
523
524 /*
525 * Generate beacon frames. we are sending frames
526 * staggered so calculate the slot for this frame based
527 * on the tsf to safeguard against missing an swba.
528 */
529
Jouni Malinena8fff502008-08-11 14:01:48 +0300530 intval = sc->hw->conf.beacon_int ?
531 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700532
533 tsf = ath9k_hw_gettsf64(ah);
534 tsftu = TSF_TO_TU(tsf>>32, tsf);
535 slot = ((tsftu % intval) * ATH_BCBUF) / intval;
536 if_id = sc->sc_bslot[(slot + 1) % ATH_BCBUF];
Sujith980b24d2008-09-17 10:15:09 +0530537
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700538 DPRINTF(sc, ATH_DBG_BEACON,
Sujith980b24d2008-09-17 10:15:09 +0530539 "%s: slot %d [tsf %llu tsftu %u intval %u] if_id %d\n",
540 __func__, slot, (unsigned long long)tsf, tsftu,
541 intval, if_id);
542
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700543 bfaddr = 0;
544 if (if_id != ATH_IF_ID_ANY) {
545 bf = ath_beacon_generate(sc, if_id);
546 if (bf != NULL) {
547 bfaddr = bf->bf_daddr;
548 bc = 1;
549 }
550 }
551 /*
552 * Handle slot time change when a non-ERP station joins/leaves
553 * an 11g network. The 802.11 layer notifies us via callback,
554 * we mark updateslot, then wait one beacon before effecting
555 * the change. This gives associated stations at least one
556 * beacon interval to note the state change.
557 *
558 * NB: The slot time change state machine is clocked according
559 * to whether we are bursting or staggering beacons. We
560 * recognize the request to update and record the current
561 * slot then don't transition until that slot is reached
562 * again. If we miss a beacon for that slot then we'll be
563 * slow to transition but we'll be sure at least one beacon
564 * interval has passed. When bursting slot is always left
565 * set to ATH_BCBUF so this check is a noop.
566 */
567 /* XXX locking */
568 if (sc->sc_updateslot == UPDATE) {
569 sc->sc_updateslot = COMMIT; /* commit next beacon */
570 sc->sc_slotupdate = slot;
Sujithff37e332008-11-24 12:07:55 +0530571 } else if (sc->sc_updateslot == COMMIT && sc->sc_slotupdate == slot) {
572 ath9k_hw_setslottime(sc->sc_ah, sc->sc_slottime);
573 sc->sc_updateslot = OK;
574 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700575 if (bfaddr != 0) {
576 /*
577 * Stop any current dma and put the new frame(s) on the queue.
578 * This should never fail since we check above that no frames
579 * are still pending on the queue.
580 */
581 if (!ath9k_hw_stoptxdma(ah, sc->sc_bhalq)) {
582 DPRINTF(sc, ATH_DBG_FATAL,
583 "%s: beacon queue %u did not stop?\n",
584 __func__, sc->sc_bhalq);
585 /* NB: the HAL still stops DMA, so proceed */
586 }
587
588 /* NB: cabq traffic should already be queued and primed */
589 ath9k_hw_puttxbuf(ah, sc->sc_bhalq, bfaddr);
590 ath9k_hw_txstart(ah, sc->sc_bhalq);
591
592 sc->ast_be_xmit += bc; /* XXX per-vap? */
593 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700594}
595
596/*
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700597 * Configure the beacon and sleep timers.
598 *
599 * When operating as an AP this resets the TSF and sets
600 * up the hardware to notify us when we need to issue beacons.
601 *
602 * When operating in station mode this sets up the beacon
603 * timers according to the timestamp of the last received
604 * beacon and the current TSF, configures PCF and DTIM
605 * handling, programs the sleep registers so the hardware
606 * will wakeup in time to receive beacons, and configures
607 * the beacon miss handling so we'll receive a BMISS
608 * interrupt when we stop seeing beacons from the AP
609 * we've associated with.
610 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700611void ath_beacon_config(struct ath_softc *sc, int if_id)
612{
Sujith5640b082008-10-29 10:16:06 +0530613 struct ieee80211_vif *vif;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700614 struct ath_hal *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700615 struct ath_beacon_config conf;
Sujith5640b082008-10-29 10:16:06 +0530616 struct ath_vap *avp;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700617 enum ath9k_opmode av_opmode;
Sujith980b24d2008-09-17 10:15:09 +0530618 u32 nexttbtt, intval;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700619
Sujith5640b082008-10-29 10:16:06 +0530620 if (if_id != ATH_IF_ID_ANY) {
621 vif = sc->sc_vaps[if_id];
622 ASSERT(vif);
623 avp = (void *)vif->drv_priv;
624 av_opmode = avp->av_opmode;
625 } else {
Sujithb4696c8b2008-08-11 14:04:52 +0530626 av_opmode = sc->sc_ah->ah_opmode;
Sujith5640b082008-10-29 10:16:06 +0530627 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700628
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700629 memset(&conf, 0, sizeof(struct ath_beacon_config));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700630
Jouni Malinena8fff502008-08-11 14:01:48 +0300631 conf.beacon_interval = sc->hw->conf.beacon_int ?
632 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700633 conf.listen_interval = 1;
634 conf.dtim_period = conf.beacon_interval;
635 conf.dtim_count = 1;
636 conf.bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf.beacon_interval;
637
638 /* extract tstamp from last beacon and convert to TU */
Sujith459f5f92008-09-17 10:15:36 +0530639 nexttbtt = TSF_TO_TU(sc->bc_tstamp >> 32, sc->bc_tstamp);
640
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700641 /* XXX conditionalize multi-bss support? */
Sujithb4696c8b2008-08-11 14:04:52 +0530642 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700643 /*
644 * For multi-bss ap support beacons are either staggered
645 * evenly over N slots or burst together. For the former
646 * arrange for the SWBA to be delivered for each slot.
647 * Slots that are not occupied will generate nothing.
648 */
649 /* NB: the beacon interval is kept internally in TU's */
650 intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
651 intval /= ATH_BCBUF; /* for staggered beacons */
652 } else {
653 intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
654 }
655
Sujith980b24d2008-09-17 10:15:09 +0530656 if (nexttbtt == 0) /* e.g. for ap mode */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700657 nexttbtt = intval;
Sujith980b24d2008-09-17 10:15:09 +0530658 else if (intval) /* NB: can be 0 for monitor mode */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700659 nexttbtt = roundup(nexttbtt, intval);
Sujith980b24d2008-09-17 10:15:09 +0530660
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700661 DPRINTF(sc, ATH_DBG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
662 __func__, nexttbtt, intval, conf.beacon_interval);
Sujith980b24d2008-09-17 10:15:09 +0530663
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700664 /* Check for ATH9K_M_HOSTAP and sc_nostabeacons for WDS client */
Sujithb4696c8b2008-08-11 14:04:52 +0530665 if (sc->sc_ah->ah_opmode == ATH9K_M_STA) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700666 struct ath9k_beacon_state bs;
667 u64 tsf;
668 u32 tsftu;
669 int dtimperiod, dtimcount, sleepduration;
670 int cfpperiod, cfpcount;
671
672 /*
673 * Setup dtim and cfp parameters according to
674 * last beacon we received (which may be none).
675 */
676 dtimperiod = conf.dtim_period;
Sujith980b24d2008-09-17 10:15:09 +0530677 if (dtimperiod <= 0) /* NB: 0 if not known */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700678 dtimperiod = 1;
679 dtimcount = conf.dtim_count;
Sujith980b24d2008-09-17 10:15:09 +0530680 if (dtimcount >= dtimperiod) /* NB: sanity check */
681 dtimcount = 0;
682 cfpperiod = 1; /* NB: no PCF support yet */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700683 cfpcount = 0;
684
685 sleepduration = conf.listen_interval * intval;
686 if (sleepduration <= 0)
687 sleepduration = intval;
688
Sujith980b24d2008-09-17 10:15:09 +0530689#define FUDGE 2
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700690 /*
691 * Pull nexttbtt forward to reflect the current
692 * TSF and calculate dtim+cfp state for the result.
693 */
694 tsf = ath9k_hw_gettsf64(ah);
695 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
696 do {
697 nexttbtt += intval;
698 if (--dtimcount < 0) {
699 dtimcount = dtimperiod - 1;
700 if (--cfpcount < 0)
701 cfpcount = cfpperiod - 1;
702 }
703 } while (nexttbtt < tsftu);
704#undef FUDGE
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700705 memset(&bs, 0, sizeof(bs));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700706 bs.bs_intval = intval;
707 bs.bs_nexttbtt = nexttbtt;
708 bs.bs_dtimperiod = dtimperiod*intval;
709 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
710 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
711 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
712 bs.bs_cfpmaxduration = 0;
Sujith980b24d2008-09-17 10:15:09 +0530713
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700714 /*
715 * Calculate the number of consecutive beacons to miss
716 * before taking a BMISS interrupt. The configuration
717 * is specified in TU so we only need calculate based
718 * on the beacon interval. Note that we clamp the
719 * result to at most 15 beacons.
720 */
721 if (sleepduration > intval) {
Sujith980b24d2008-09-17 10:15:09 +0530722 bs.bs_bmissthreshold = conf.listen_interval *
723 ATH_DEFAULT_BMISS_LIMIT / 2;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700724 } else {
725 bs.bs_bmissthreshold =
726 DIV_ROUND_UP(conf.bmiss_timeout, intval);
727 if (bs.bs_bmissthreshold > 15)
728 bs.bs_bmissthreshold = 15;
729 else if (bs.bs_bmissthreshold <= 0)
730 bs.bs_bmissthreshold = 1;
731 }
732
733 /*
734 * Calculate sleep duration. The configuration is
735 * given in ms. We insure a multiple of the beacon
736 * period is used. Also, if the sleep duration is
737 * greater than the DTIM period then it makes senses
738 * to make it a multiple of that.
739 *
740 * XXX fixed at 100ms
741 */
742
Sujith980b24d2008-09-17 10:15:09 +0530743 bs.bs_sleepduration = roundup(IEEE80211_MS_TO_TU(100),
744 sleepduration);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700745 if (bs.bs_sleepduration > bs.bs_dtimperiod)
746 bs.bs_sleepduration = bs.bs_dtimperiod;
747
748 DPRINTF(sc, ATH_DBG_BEACON,
749 "%s: tsf %llu "
750 "tsf:tu %u "
751 "intval %u "
752 "nexttbtt %u "
753 "dtim %u "
754 "nextdtim %u "
755 "bmiss %u "
756 "sleep %u "
757 "cfp:period %u "
758 "maxdur %u "
759 "next %u "
Sujithff9b6622008-08-14 13:27:16 +0530760 "timoffset %u\n",
761 __func__,
762 (unsigned long long)tsf, tsftu,
763 bs.bs_intval,
764 bs.bs_nexttbtt,
765 bs.bs_dtimperiod,
766 bs.bs_nextdtim,
767 bs.bs_bmissthreshold,
768 bs.bs_sleepduration,
769 bs.bs_cfpperiod,
770 bs.bs_cfpmaxduration,
771 bs.bs_cfpnext,
772 bs.bs_timoffset
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700773 );
774
775 ath9k_hw_set_interrupts(ah, 0);
776 ath9k_hw_set_sta_beacon_timers(ah, &bs);
777 sc->sc_imask |= ATH9K_INT_BMISS;
778 ath9k_hw_set_interrupts(ah, sc->sc_imask);
779 } else {
780 u64 tsf;
781 u32 tsftu;
782 ath9k_hw_set_interrupts(ah, 0);
783 if (nexttbtt == intval)
784 intval |= ATH9K_BEACON_RESET_TSF;
Sujithb4696c8b2008-08-11 14:04:52 +0530785 if (sc->sc_ah->ah_opmode == ATH9K_M_IBSS) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700786 /*
787 * Pull nexttbtt forward to reflect the current
Sujith980b24d2008-09-17 10:15:09 +0530788 * TSF
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700789 */
Sujith980b24d2008-09-17 10:15:09 +0530790#define FUDGE 2
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700791 if (!(intval & ATH9K_BEACON_RESET_TSF)) {
792 tsf = ath9k_hw_gettsf64(ah);
793 tsftu = TSF_TO_TU((u32)(tsf>>32),
794 (u32)tsf) + FUDGE;
795 do {
796 nexttbtt += intval;
797 } while (nexttbtt < tsftu);
798 }
799#undef FUDGE
800 DPRINTF(sc, ATH_DBG_BEACON,
801 "%s: IBSS nexttbtt %u intval %u (%u)\n",
802 __func__, nexttbtt,
803 intval & ~ATH9K_BEACON_RESET_TSF,
804 conf.beacon_interval);
805
806 /*
807 * In IBSS mode enable the beacon timers but only
808 * enable SWBA interrupts if we need to manually
809 * prepare beacon frames. Otherwise we use a
810 * self-linked tx descriptor and let the hardware
811 * deal with things.
812 */
813 intval |= ATH9K_BEACON_ENA;
Sujith60b67f52008-08-07 10:52:38 +0530814 if (!(ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700815 sc->sc_imask |= ATH9K_INT_SWBA;
816 ath_beaconq_config(sc);
Sujithb4696c8b2008-08-11 14:04:52 +0530817 } else if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700818 /*
819 * In AP mode we enable the beacon timers and
820 * SWBA interrupts to prepare beacon frames.
821 */
822 intval |= ATH9K_BEACON_ENA;
823 sc->sc_imask |= ATH9K_INT_SWBA; /* beacon prepare */
824 ath_beaconq_config(sc);
825 }
826 ath9k_hw_beaconinit(ah, nexttbtt, intval);
827 sc->sc_bmisscount = 0;
828 ath9k_hw_set_interrupts(ah, sc->sc_imask);
829 /*
830 * When using a self-linked beacon descriptor in
831 * ibss mode load it once here.
832 */
Sujithb4696c8b2008-08-11 14:04:52 +0530833 if (sc->sc_ah->ah_opmode == ATH9K_M_IBSS &&
Sujith60b67f52008-08-07 10:52:38 +0530834 (ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700835 ath_beacon_start_adhoc(sc, 0);
836 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700837}
838
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700839void ath_beacon_sync(struct ath_softc *sc, int if_id)
840{
841 /*
842 * Resync beacon timers using the tsf of the
843 * beacon frame we just received.
844 */
845 ath_beacon_config(sc, if_id);
Sujith672840a2008-08-11 14:05:08 +0530846 sc->sc_flags |= SC_OP_BEACONS;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700847}