blob: 3ab0b43aaf93f79c6b8af3859f7cf0ea9990e3db [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
Sujithb77f4832008-12-07 21:44:03 +053029 ath9k_hw_get_txq_props(ah, sc->beacon.beaconq, &qi);
Colin McCabed97809d2008-12-01 13:38:55 -080030 if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_AP) {
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. */
Sujithb77f4832008-12-07 21:44:03 +053037 qi.tqi_aifs = sc->beacon.beacon_qi.tqi_aifs;
38 qi.tqi_cwmin = 2*sc->beacon.beacon_qi.tqi_cwmin;
39 qi.tqi_cwmax = sc->beacon.beacon_qi.tqi_cwmax;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070040 }
41
Sujithb77f4832008-12-07 21:44:03 +053042 if (!ath9k_hw_set_txq_props(ah, sc->beacon.beaconq, &qi)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070043 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd46382008-11-28 22:18:05 +053044 "unable to update h/w beacon queue parameters\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070045 return 0;
46 } else {
Sujithb77f4832008-12-07 21:44:03 +053047 ath9k_hw_resettxqueue(ah, sc->beacon.beaconq); /* push to h/w */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070048 return 1;
49 }
50}
51
Sujithff37e332008-11-24 12:07:55 +053052static void ath_bstuck_process(struct ath_softc *sc)
53{
54 DPRINTF(sc, ATH_DBG_BEACON,
Sujith04bd46382008-11-28 22:18:05 +053055 "stuck beacon; resetting (bmiss count %u)\n",
Sujithb77f4832008-12-07 21:44:03 +053056 sc->beacon.bmisscnt);
Sujithff37e332008-11-24 12:07:55 +053057 ath_reset(sc, false);
58}
59
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070060/*
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070061 * Associates the beacon frame buffer with a transmit descriptor. Will set
62 * up all required antenna switch parameters, rate codes, and channel flags.
63 * Beacons are always sent out at the lowest rate, and are not retried.
64*/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070065static void ath_beacon_setup(struct ath_softc *sc,
Sujith980b24d2008-09-17 10:15:09 +053066 struct ath_vap *avp, struct ath_buf *bf)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070067{
68 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
69 struct ath_hal *ah = sc->sc_ah;
70 struct ath_desc *ds;
Sujith980b24d2008-09-17 10:15:09 +053071 struct ath9k_11n_rate_series series[4];
Sujithe63835b2008-11-18 09:07:53 +053072 struct ath_rate_table *rt;
Sujith980b24d2008-09-17 10:15:09 +053073 int flags, antenna;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070074 u8 rix, rate;
75 int ctsrate = 0;
76 int ctsduration = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070077
Sujith04bd46382008-11-28 22:18:05 +053078 DPRINTF(sc, ATH_DBG_BEACON, "m %p len %u\n", skb, skb->len);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070079
80 /* setup descriptors */
81 ds = bf->bf_desc;
82
83 flags = ATH9K_TXDESC_NOACK;
84
Colin McCabed97809d2008-12-01 13:38:55 -080085 if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_ADHOC &&
Sujith60b67f52008-08-07 10:52:38 +053086 (ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070087 ds->ds_link = bf->bf_daddr; /* self-linked */
88 flags |= ATH9K_TXDESC_VEOL;
89 /* Let hardware handle antenna switching. */
90 antenna = 0;
91 } else {
92 ds->ds_link = 0;
93 /*
94 * Switch antenna every beacon.
95 * Should only switch every beacon period, not for every
96 * SWBA's
97 * XXX assumes two antenna
98 */
Sujithb77f4832008-12-07 21:44:03 +053099 antenna = ((sc->beacon.ast_be_xmit / sc->sc_nbcnvaps) & 1 ? 2 : 1);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700100 }
101
102 ds->ds_data = bf->bf_buf_addr;
103
104 /*
105 * Calculate rate code.
106 * XXX everything at min xmit rate
107 */
Sujith86b89ee2008-08-07 10:54:57 +0530108 rix = 0;
Sujith3706de62008-12-07 21:42:10 +0530109 rt = sc->cur_rate_table;
Sujithe63835b2008-11-18 09:07:53 +0530110 rate = rt->info[rix].ratecode;
Sujith672840a2008-08-11 14:05:08 +0530111 if (sc->sc_flags & SC_OP_PREAMBLE_SHORT)
Sujithe63835b2008-11-18 09:07:53 +0530112 rate |= rt->info[rix].short_preamble;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700113
Sujithff9b6622008-08-14 13:27:16 +0530114 ath9k_hw_set11n_txdesc(ah, ds,
Sujith980b24d2008-09-17 10:15:09 +0530115 skb->len + FCS_LEN, /* frame length */
116 ATH9K_PKT_TYPE_BEACON, /* Atheros packet type */
Sujith528f0c62008-10-29 10:14:26 +0530117 MAX_RATE_POWER, /* FIXME */
Sujith980b24d2008-09-17 10:15:09 +0530118 ATH9K_TXKEYIX_INVALID, /* no encryption */
119 ATH9K_KEY_TYPE_CLEAR, /* no encryption */
120 flags /* no ack,
121 veol for beacons */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700122 );
123
124 /* NB: beacon's BufLen must be a multiple of 4 bytes */
Sujithff9b6622008-08-14 13:27:16 +0530125 ath9k_hw_filltxdesc(ah, ds,
126 roundup(skb->len, 4), /* buffer length */
Sujith980b24d2008-09-17 10:15:09 +0530127 true, /* first segment */
128 true, /* last segment */
129 ds /* first descriptor */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700130 );
131
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700132 memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700133 series[0].Tries = 1;
134 series[0].Rate = rate;
135 series[0].ChSel = sc->sc_tx_chainmask;
136 series[0].RateFlags = (ctsrate) ? ATH9K_RATESERIES_RTS_CTS : 0;
137 ath9k_hw_set11n_ratescenario(ah, ds, ds, 0,
138 ctsrate, ctsduration, series, 4, 0);
139}
140
Sujithff37e332008-11-24 12:07:55 +0530141/* Generate beacon frame and queue cab data for a vap */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700142static struct ath_buf *ath_beacon_generate(struct ath_softc *sc, int if_id)
143{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700144 struct ath_buf *bf;
145 struct ath_vap *avp;
146 struct sk_buff *skb;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700147 struct ath_txq *cabq;
Sujith5640b082008-10-29 10:16:06 +0530148 struct ieee80211_vif *vif;
Jouni Malinen147583c2008-08-11 14:01:50 +0300149 struct ieee80211_tx_info *info;
Sujith980b24d2008-09-17 10:15:09 +0530150 int cabq_depth;
151
Sujith5640b082008-10-29 10:16:06 +0530152 vif = sc->sc_vaps[if_id];
153 ASSERT(vif);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700154
Sujith5640b082008-10-29 10:16:06 +0530155 avp = (void *)vif->drv_priv;
Sujithb77f4832008-12-07 21:44:03 +0530156 cabq = sc->beacon.cabq;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700157
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700158 if (avp->av_bcbuf == NULL) {
Sujith04bd46382008-11-28 22:18:05 +0530159 DPRINTF(sc, ATH_DBG_BEACON, "avp=%p av_bcbuf=%p\n",
160 avp, avp->av_bcbuf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700161 return NULL;
162 }
Sujith980b24d2008-09-17 10:15:09 +0530163
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700164 bf = avp->av_bcbuf;
Sujith980b24d2008-09-17 10:15:09 +0530165 skb = (struct sk_buff *)bf->bf_mpdu;
Jouni Malinena8fff502008-08-11 14:01:48 +0300166 if (skb) {
167 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
Luis R. Rodriguezca0c7e52008-11-20 17:15:12 -0800168 skb->len,
Jouni Malinena8fff502008-08-11 14:01:48 +0300169 PCI_DMA_TODEVICE);
Jouni Malinen3fbb9d92008-12-05 20:42:45 +0200170 dev_kfree_skb_any(skb);
Jouni Malinena8fff502008-08-11 14:01:48 +0300171 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700172
Sujith5640b082008-10-29 10:16:06 +0530173 skb = ieee80211_beacon_get(sc->hw, vif);
Jouni Malinena8fff502008-08-11 14:01:48 +0300174 bf->bf_mpdu = skb;
175 if (skb == NULL)
176 return NULL;
Sujith980b24d2008-09-17 10:15:09 +0530177
Jouni Malinen147583c2008-08-11 14:01:50 +0300178 info = IEEE80211_SKB_CB(skb);
179 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
180 /*
181 * TODO: make sure the seq# gets assigned properly (vs. other
182 * TX frames)
183 */
Sujith980b24d2008-09-17 10:15:09 +0530184 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Sujithb77f4832008-12-07 21:44:03 +0530185 sc->tx.seq_no += 0x10;
Jouni Malinen147583c2008-08-11 14:01:50 +0300186 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Sujithb77f4832008-12-07 21:44:03 +0530187 hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
Jouni Malinen147583c2008-08-11 14:01:50 +0300188 }
Sujith980b24d2008-09-17 10:15:09 +0530189
Jouni Malinena8fff502008-08-11 14:01:48 +0300190 bf->bf_buf_addr = bf->bf_dmacontext =
191 pci_map_single(sc->pdev, skb->data,
Luis R. Rodriguezca0c7e52008-11-20 17:15:12 -0800192 skb->len,
Jouni Malinena8fff502008-08-11 14:01:48 +0300193 PCI_DMA_TODEVICE);
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -0800194 if (unlikely(pci_dma_mapping_error(sc->pdev, bf->bf_buf_addr))) {
195 dev_kfree_skb_any(skb);
196 bf->bf_mpdu = NULL;
197 DPRINTF(sc, ATH_DBG_CONFIG,
198 "pci_dma_mapping_error() on beaconing\n");
199 return NULL;
200 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700201
Sujith5640b082008-10-29 10:16:06 +0530202 skb = ieee80211_get_buffered_bc(sc->hw, vif);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700203
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700204 /*
205 * if the CABQ traffic from previous DTIM is pending and the current
206 * beacon is also a DTIM.
207 * 1) if there is only one vap let the cab traffic continue.
208 * 2) if there are more than one vap and we are using staggered
209 * beacons, then drain the cabq by dropping all the frames in
210 * the cabq so that the current vaps cab traffic can be scheduled.
211 */
212 spin_lock_bh(&cabq->axq_lock);
213 cabq_depth = cabq->axq_depth;
214 spin_unlock_bh(&cabq->axq_lock);
215
Jouni Malinene022edb2008-08-22 17:31:33 +0300216 if (skb && cabq_depth) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700217 /*
218 * Unlock the cabq lock as ath_tx_draintxq acquires
219 * the lock again which is a common function and that
220 * acquires txq lock inside.
221 */
222 if (sc->sc_nvaps > 1) {
223 ath_tx_draintxq(sc, cabq, false);
224 DPRINTF(sc, ATH_DBG_BEACON,
Sujith04bd46382008-11-28 22:18:05 +0530225 "flush previous cabq traffic\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700226 }
227 }
228
229 /* Construct tx descriptor. */
230 ath_beacon_setup(sc, avp, bf);
231
232 /*
233 * Enable the CAB queue before the beacon queue to
234 * insure cab frames are triggered by this beacon.
235 */
Jouni Malinene022edb2008-08-22 17:31:33 +0300236 while (skb) {
237 ath_tx_cabq(sc, skb);
Sujith5640b082008-10-29 10:16:06 +0530238 skb = ieee80211_get_buffered_bc(sc->hw, vif);
Jouni Malinene022edb2008-08-22 17:31:33 +0300239 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700240
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700241 return bf;
242}
243
244/*
245 * Startup beacon transmission for adhoc mode when they are sent entirely
246 * by the hardware using the self-linked descriptor + veol trick.
247*/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700248static void ath_beacon_start_adhoc(struct ath_softc *sc, int if_id)
249{
Sujith5640b082008-10-29 10:16:06 +0530250 struct ieee80211_vif *vif;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700251 struct ath_hal *ah = sc->sc_ah;
252 struct ath_buf *bf;
253 struct ath_vap *avp;
254 struct sk_buff *skb;
255
Sujith5640b082008-10-29 10:16:06 +0530256 vif = sc->sc_vaps[if_id];
257 ASSERT(vif);
258
259 avp = (void *)vif->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700260
261 if (avp->av_bcbuf == NULL) {
Sujith04bd46382008-11-28 22:18:05 +0530262 DPRINTF(sc, ATH_DBG_BEACON, "avp=%p av_bcbuf=%p\n",
263 avp, avp != NULL ? avp->av_bcbuf : NULL);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700264 return;
265 }
266 bf = avp->av_bcbuf;
267 skb = (struct sk_buff *) bf->bf_mpdu;
268
269 /* Construct tx descriptor. */
270 ath_beacon_setup(sc, avp, bf);
271
272 /* NB: caller is known to have already stopped tx dma */
Sujithb77f4832008-12-07 21:44:03 +0530273 ath9k_hw_puttxbuf(ah, sc->beacon.beaconq, bf->bf_daddr);
274 ath9k_hw_txstart(ah, sc->beacon.beaconq);
Sujith04bd46382008-11-28 22:18:05 +0530275 DPRINTF(sc, ATH_DBG_BEACON, "TXDP%u = %llx (%p)\n",
Sujithb77f4832008-12-07 21:44:03 +0530276 sc->beacon.beaconq, ito64(bf->bf_daddr), bf->bf_desc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700277}
278
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700279int ath_beaconq_setup(struct ath_hal *ah)
280{
Sujithea9880f2008-08-07 10:53:10 +0530281 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700282
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700283 memset(&qi, 0, sizeof(qi));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700284 qi.tqi_aifs = 1;
285 qi.tqi_cwmin = 0;
286 qi.tqi_cwmax = 0;
287 /* NB: don't enable any interrupts */
288 return ath9k_hw_setuptxqueue(ah, ATH9K_TX_QUEUE_BEACON, &qi);
289}
290
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700291int ath_beacon_alloc(struct ath_softc *sc, int if_id)
292{
Sujith5640b082008-10-29 10:16:06 +0530293 struct ieee80211_vif *vif;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700294 struct ath_vap *avp;
Sujith980b24d2008-09-17 10:15:09 +0530295 struct ieee80211_hdr *hdr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700296 struct ath_buf *bf;
297 struct sk_buff *skb;
Sujith459f5f92008-09-17 10:15:36 +0530298 __le64 tstamp;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700299
Sujith5640b082008-10-29 10:16:06 +0530300 vif = sc->sc_vaps[if_id];
301 ASSERT(vif);
302
303 avp = (void *)vif->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700304
305 /* Allocate a beacon descriptor if we haven't done so. */
306 if (!avp->av_bcbuf) {
Sujith980b24d2008-09-17 10:15:09 +0530307 /* Allocate beacon state for hostap/ibss. We know
308 * a buffer is available. */
Sujithb77f4832008-12-07 21:44:03 +0530309 avp->av_bcbuf = list_first_entry(&sc->beacon.bbuf,
Sujith980b24d2008-09-17 10:15:09 +0530310 struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700311 list_del(&avp->av_bcbuf->list);
312
Colin McCabed97809d2008-12-01 13:38:55 -0800313 if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_AP ||
Sujith60b67f52008-08-07 10:52:38 +0530314 !(sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700315 int slot;
316 /*
317 * Assign the vap to a beacon xmit slot. As
318 * above, this cannot fail to find one.
319 */
320 avp->av_bslot = 0;
321 for (slot = 0; slot < ATH_BCBUF; slot++)
Sujithb77f4832008-12-07 21:44:03 +0530322 if (sc->beacon.bslot[slot] == ATH_IF_ID_ANY) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700323 /*
324 * XXX hack, space out slots to better
325 * deal with misses
326 */
327 if (slot+1 < ATH_BCBUF &&
Sujithb77f4832008-12-07 21:44:03 +0530328 sc->beacon.bslot[slot+1] ==
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700329 ATH_IF_ID_ANY) {
330 avp->av_bslot = slot+1;
331 break;
332 }
333 avp->av_bslot = slot;
334 /* NB: keep looking for a double slot */
335 }
Sujithb77f4832008-12-07 21:44:03 +0530336 BUG_ON(sc->beacon.bslot[avp->av_bslot] != ATH_IF_ID_ANY);
337 sc->beacon.bslot[avp->av_bslot] = if_id;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700338 sc->sc_nbcnvaps++;
339 }
340 }
341
342 /* release the previous beacon frame , if it already exists. */
343 bf = avp->av_bcbuf;
344 if (bf->bf_mpdu != NULL) {
345 skb = (struct sk_buff *)bf->bf_mpdu;
Jouni Malinena8fff502008-08-11 14:01:48 +0300346 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
Luis R. Rodriguezca0c7e52008-11-20 17:15:12 -0800347 skb->len,
Jouni Malinena8fff502008-08-11 14:01:48 +0300348 PCI_DMA_TODEVICE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700349 dev_kfree_skb_any(skb);
350 bf->bf_mpdu = NULL;
351 }
352
353 /*
Sujith980b24d2008-09-17 10:15:09 +0530354 * NB: the beacon data buffer must be 32-bit aligned.
Jouni Malinene022edb2008-08-22 17:31:33 +0300355 * FIXME: Fill avp->av_btxctl.txpower and
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700356 * avp->av_btxctl.shortPreamble
357 */
Sujith5640b082008-10-29 10:16:06 +0530358 skb = ieee80211_beacon_get(sc->hw, vif);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700359 if (skb == NULL) {
Sujith04bd46382008-11-28 22:18:05 +0530360 DPRINTF(sc, ATH_DBG_BEACON, "cannot get skb\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700361 return -ENOMEM;
362 }
363
Sujith459f5f92008-09-17 10:15:36 +0530364 tstamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
Sujithb77f4832008-12-07 21:44:03 +0530365 sc->beacon.bc_tstamp = le64_to_cpu(tstamp);
Sujith459f5f92008-09-17 10:15:36 +0530366
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700367 /*
368 * Calculate a TSF adjustment factor required for
369 * staggered beacons. Note that we assume the format
370 * of the beacon frame leaves the tstamp field immediately
371 * following the header.
372 */
373 if (avp->av_bslot > 0) {
374 u64 tsfadjust;
375 __le64 val;
376 int intval;
377
Jouni Malinena8fff502008-08-11 14:01:48 +0300378 intval = sc->hw->conf.beacon_int ?
379 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700380
381 /*
382 * The beacon interval is in TU's; the TSF in usecs.
383 * We figure out how many TU's to add to align the
384 * timestamp then convert to TSF units and handle
385 * byte swapping before writing it in the frame.
386 * The hardware will then add this each time a beacon
387 * frame is sent. Note that we align vap's 1..N
388 * and leave vap 0 untouched. This means vap 0
389 * has a timestamp in one beacon interval while the
390 * others get a timestamp aligned to the next interval.
391 */
392 tsfadjust = (intval * (ATH_BCBUF - avp->av_bslot)) / ATH_BCBUF;
393 val = cpu_to_le64(tsfadjust << 10); /* TU->TSF */
394
395 DPRINTF(sc, ATH_DBG_BEACON,
Sujith04bd46382008-11-28 22:18:05 +0530396 "stagger beacons, bslot %d intval %u tsfadjust %llu\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700397 avp->av_bslot, intval, (unsigned long long)tsfadjust);
398
Sujith980b24d2008-09-17 10:15:09 +0530399 hdr = (struct ieee80211_hdr *)skb->data;
400 memcpy(&hdr[1], &val, sizeof(val));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700401 }
402
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -0800403 bf->bf_mpdu = skb;
Jouni Malinena8fff502008-08-11 14:01:48 +0300404 bf->bf_buf_addr = bf->bf_dmacontext =
405 pci_map_single(sc->pdev, skb->data,
Luis R. Rodriguezca0c7e52008-11-20 17:15:12 -0800406 skb->len,
Jouni Malinena8fff502008-08-11 14:01:48 +0300407 PCI_DMA_TODEVICE);
Luis R. Rodriguezf8316df2008-12-03 03:35:29 -0800408 if (unlikely(pci_dma_mapping_error(sc->pdev, bf->bf_buf_addr))) {
409 dev_kfree_skb_any(skb);
410 bf->bf_mpdu = NULL;
411 DPRINTF(sc, ATH_DBG_CONFIG,
412 "pci_dma_mapping_error() on beacon alloc\n");
413 return -ENOMEM;
414 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700415
416 return 0;
417}
418
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700419void ath_beacon_return(struct ath_softc *sc, struct ath_vap *avp)
420{
421 if (avp->av_bcbuf != NULL) {
422 struct ath_buf *bf;
423
424 if (avp->av_bslot != -1) {
Sujithb77f4832008-12-07 21:44:03 +0530425 sc->beacon.bslot[avp->av_bslot] = ATH_IF_ID_ANY;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700426 sc->sc_nbcnvaps--;
427 }
428
429 bf = avp->av_bcbuf;
430 if (bf->bf_mpdu != NULL) {
431 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
Jouni Malinena8fff502008-08-11 14:01:48 +0300432 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
Luis R. Rodriguezca0c7e52008-11-20 17:15:12 -0800433 skb->len,
Jouni Malinena8fff502008-08-11 14:01:48 +0300434 PCI_DMA_TODEVICE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700435 dev_kfree_skb_any(skb);
436 bf->bf_mpdu = NULL;
437 }
Sujithb77f4832008-12-07 21:44:03 +0530438 list_add_tail(&bf->list, &sc->beacon.bbuf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700439
440 avp->av_bcbuf = NULL;
441 }
442}
443
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700444void ath9k_beacon_tasklet(unsigned long data)
445{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700446 struct ath_softc *sc = (struct ath_softc *)data;
447 struct ath_hal *ah = sc->sc_ah;
448 struct ath_buf *bf = NULL;
449 int slot, if_id;
450 u32 bfaddr;
451 u32 rx_clear = 0, rx_frame = 0, tx_frame = 0;
452 u32 show_cycles = 0;
453 u32 bc = 0; /* beacon count */
454 u64 tsf;
455 u32 tsftu;
456 u16 intval;
457
Sujith98deeea2008-08-11 14:05:46 +0530458 if (sc->sc_flags & SC_OP_NO_RESET) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700459 show_cycles = ath9k_hw_GetMibCycleCountsPct(ah,
Sujithff37e332008-11-24 12:07:55 +0530460 &rx_clear, &rx_frame, &tx_frame);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700461 }
462
463 /*
464 * Check if the previous beacon has gone out. If
465 * not don't try to post another, skip this period
466 * and wait for the next. Missed beacons indicate
467 * a problem and should not occur. If we miss too
468 * many consecutive beacons reset the device.
Sujith980b24d2008-09-17 10:15:09 +0530469 *
470 * FIXME: Clean up this mess !!
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700471 */
Sujithb77f4832008-12-07 21:44:03 +0530472 if (ath9k_hw_numtxpending(ah, sc->beacon.beaconq) != 0) {
473 sc->beacon.bmisscnt++;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700474 /* XXX: doth needs the chanchange IE countdown decremented.
475 * We should consider adding a mac80211 call to indicate
476 * a beacon miss so appropriate action could be taken
477 * (in that layer).
478 */
Sujithb77f4832008-12-07 21:44:03 +0530479 if (sc->beacon.bmisscnt < BSTUCK_THRESH) {
Sujith98deeea2008-08-11 14:05:46 +0530480 if (sc->sc_flags & SC_OP_NO_RESET) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700481 DPRINTF(sc, ATH_DBG_BEACON,
Sujith04bd46382008-11-28 22:18:05 +0530482 "missed %u consecutive beacons\n",
Sujithb77f4832008-12-07 21:44:03 +0530483 sc->beacon.bmisscnt);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700484 if (show_cycles) {
485 /*
Sujith980b24d2008-09-17 10:15:09 +0530486 * Display cycle counter stats from HW
487 * to aide in debug of stickiness.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700488 */
Sujith980b24d2008-09-17 10:15:09 +0530489 DPRINTF(sc, ATH_DBG_BEACON,
Sujith04bd46382008-11-28 22:18:05 +0530490 "busy times: rx_clear=%d, "
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700491 "rx_frame=%d, tx_frame=%d\n",
Sujith04bd46382008-11-28 22:18:05 +0530492 rx_clear, rx_frame,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700493 tx_frame);
494 } else {
Sujith980b24d2008-09-17 10:15:09 +0530495 DPRINTF(sc, ATH_DBG_BEACON,
Sujith04bd46382008-11-28 22:18:05 +0530496 "unable to obtain "
497 "busy times\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700498 }
499 } else {
500 DPRINTF(sc, ATH_DBG_BEACON,
Sujith04bd46382008-11-28 22:18:05 +0530501 "missed %u consecutive beacons\n",
Sujithb77f4832008-12-07 21:44:03 +0530502 sc->beacon.bmisscnt);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700503 }
Sujithb77f4832008-12-07 21:44:03 +0530504 } else if (sc->beacon.bmisscnt >= BSTUCK_THRESH) {
Sujith98deeea2008-08-11 14:05:46 +0530505 if (sc->sc_flags & SC_OP_NO_RESET) {
Sujithb77f4832008-12-07 21:44:03 +0530506 if (sc->beacon.bmisscnt == BSTUCK_THRESH) {
Sujith980b24d2008-09-17 10:15:09 +0530507 DPRINTF(sc, ATH_DBG_BEACON,
Sujith04bd46382008-11-28 22:18:05 +0530508 "beacon is officially "
509 "stuck\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700510 }
511 } else {
512 DPRINTF(sc, ATH_DBG_BEACON,
Sujith04bd46382008-11-28 22:18:05 +0530513 "beacon is officially stuck\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700514 ath_bstuck_process(sc);
515 }
516 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700517 return;
518 }
Sujith980b24d2008-09-17 10:15:09 +0530519
Sujithb77f4832008-12-07 21:44:03 +0530520 if (sc->beacon.bmisscnt != 0) {
Sujith98deeea2008-08-11 14:05:46 +0530521 if (sc->sc_flags & SC_OP_NO_RESET) {
Sujith980b24d2008-09-17 10:15:09 +0530522 DPRINTF(sc, ATH_DBG_BEACON,
Sujith04bd46382008-11-28 22:18:05 +0530523 "resume beacon xmit after %u misses\n",
Sujithb77f4832008-12-07 21:44:03 +0530524 sc->beacon.bmisscnt);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700525 } else {
526 DPRINTF(sc, ATH_DBG_BEACON,
Sujith04bd46382008-11-28 22:18:05 +0530527 "resume beacon xmit after %u misses\n",
Sujithb77f4832008-12-07 21:44:03 +0530528 sc->beacon.bmisscnt);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700529 }
Sujithb77f4832008-12-07 21:44:03 +0530530 sc->beacon.bmisscnt = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700531 }
532
533 /*
534 * Generate beacon frames. we are sending frames
535 * staggered so calculate the slot for this frame based
536 * on the tsf to safeguard against missing an swba.
537 */
538
Jouni Malinena8fff502008-08-11 14:01:48 +0300539 intval = sc->hw->conf.beacon_int ?
540 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700541
542 tsf = ath9k_hw_gettsf64(ah);
543 tsftu = TSF_TO_TU(tsf>>32, tsf);
544 slot = ((tsftu % intval) * ATH_BCBUF) / intval;
Sujithb77f4832008-12-07 21:44:03 +0530545 if_id = sc->beacon.bslot[(slot + 1) % ATH_BCBUF];
Sujith980b24d2008-09-17 10:15:09 +0530546
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700547 DPRINTF(sc, ATH_DBG_BEACON,
Sujith04bd46382008-11-28 22:18:05 +0530548 "slot %d [tsf %llu tsftu %u intval %u] if_id %d\n",
549 slot, (unsigned long long)tsf, tsftu,
Sujith980b24d2008-09-17 10:15:09 +0530550 intval, if_id);
551
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700552 bfaddr = 0;
553 if (if_id != ATH_IF_ID_ANY) {
554 bf = ath_beacon_generate(sc, if_id);
555 if (bf != NULL) {
556 bfaddr = bf->bf_daddr;
557 bc = 1;
558 }
559 }
560 /*
561 * Handle slot time change when a non-ERP station joins/leaves
562 * an 11g network. The 802.11 layer notifies us via callback,
563 * we mark updateslot, then wait one beacon before effecting
564 * the change. This gives associated stations at least one
565 * beacon interval to note the state change.
566 *
567 * NB: The slot time change state machine is clocked according
568 * to whether we are bursting or staggering beacons. We
569 * recognize the request to update and record the current
570 * slot then don't transition until that slot is reached
571 * again. If we miss a beacon for that slot then we'll be
572 * slow to transition but we'll be sure at least one beacon
573 * interval has passed. When bursting slot is always left
574 * set to ATH_BCBUF so this check is a noop.
575 */
576 /* XXX locking */
Sujithb77f4832008-12-07 21:44:03 +0530577 if (sc->beacon.updateslot == UPDATE) {
578 sc->beacon.updateslot = COMMIT; /* commit next beacon */
579 sc->beacon.slotupdate = slot;
580 } else if (sc->beacon.updateslot == COMMIT && sc->beacon.slotupdate == slot) {
581 ath9k_hw_setslottime(sc->sc_ah, sc->beacon.slottime);
582 sc->beacon.updateslot = OK;
Sujithff37e332008-11-24 12:07:55 +0530583 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700584 if (bfaddr != 0) {
585 /*
586 * Stop any current dma and put the new frame(s) on the queue.
587 * This should never fail since we check above that no frames
588 * are still pending on the queue.
589 */
Sujithb77f4832008-12-07 21:44:03 +0530590 if (!ath9k_hw_stoptxdma(ah, sc->beacon.beaconq)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700591 DPRINTF(sc, ATH_DBG_FATAL,
Sujithb77f4832008-12-07 21:44:03 +0530592 "beacon queue %u did not stop?\n", sc->beacon.beaconq);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700593 /* NB: the HAL still stops DMA, so proceed */
594 }
595
596 /* NB: cabq traffic should already be queued and primed */
Sujithb77f4832008-12-07 21:44:03 +0530597 ath9k_hw_puttxbuf(ah, sc->beacon.beaconq, bfaddr);
598 ath9k_hw_txstart(ah, sc->beacon.beaconq);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700599
Sujithb77f4832008-12-07 21:44:03 +0530600 sc->beacon.ast_be_xmit += bc; /* XXX per-vap? */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700601 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700602}
603
604/*
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700605 * Configure the beacon and sleep timers.
606 *
607 * When operating as an AP this resets the TSF and sets
608 * up the hardware to notify us when we need to issue beacons.
609 *
610 * When operating in station mode this sets up the beacon
611 * timers according to the timestamp of the last received
612 * beacon and the current TSF, configures PCF and DTIM
613 * handling, programs the sleep registers so the hardware
614 * will wakeup in time to receive beacons, and configures
615 * the beacon miss handling so we'll receive a BMISS
616 * interrupt when we stop seeing beacons from the AP
617 * we've associated with.
618 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700619void ath_beacon_config(struct ath_softc *sc, int if_id)
620{
Sujith5640b082008-10-29 10:16:06 +0530621 struct ieee80211_vif *vif;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700622 struct ath_hal *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700623 struct ath_beacon_config conf;
Sujith5640b082008-10-29 10:16:06 +0530624 struct ath_vap *avp;
Colin McCabed97809d2008-12-01 13:38:55 -0800625 enum nl80211_iftype opmode;
Sujith980b24d2008-09-17 10:15:09 +0530626 u32 nexttbtt, intval;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700627
Sujith5640b082008-10-29 10:16:06 +0530628 if (if_id != ATH_IF_ID_ANY) {
629 vif = sc->sc_vaps[if_id];
630 ASSERT(vif);
631 avp = (void *)vif->drv_priv;
Colin McCabed97809d2008-12-01 13:38:55 -0800632 opmode = avp->av_opmode;
Sujith5640b082008-10-29 10:16:06 +0530633 } else {
Colin McCabed97809d2008-12-01 13:38:55 -0800634 opmode = sc->sc_ah->ah_opmode;
Sujith5640b082008-10-29 10:16:06 +0530635 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700636
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700637 memset(&conf, 0, sizeof(struct ath_beacon_config));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700638
Jouni Malinena8fff502008-08-11 14:01:48 +0300639 conf.beacon_interval = sc->hw->conf.beacon_int ?
640 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700641 conf.listen_interval = 1;
642 conf.dtim_period = conf.beacon_interval;
643 conf.dtim_count = 1;
644 conf.bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf.beacon_interval;
645
646 /* extract tstamp from last beacon and convert to TU */
Sujithb77f4832008-12-07 21:44:03 +0530647 nexttbtt = TSF_TO_TU(sc->beacon.bc_tstamp >> 32, sc->beacon.bc_tstamp);
Sujith459f5f92008-09-17 10:15:36 +0530648
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700649 /* XXX conditionalize multi-bss support? */
Colin McCabed97809d2008-12-01 13:38:55 -0800650 if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_AP) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700651 /*
652 * For multi-bss ap support beacons are either staggered
653 * evenly over N slots or burst together. For the former
654 * arrange for the SWBA to be delivered for each slot.
655 * Slots that are not occupied will generate nothing.
656 */
657 /* NB: the beacon interval is kept internally in TU's */
658 intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
659 intval /= ATH_BCBUF; /* for staggered beacons */
660 } else {
661 intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
662 }
663
Sujith980b24d2008-09-17 10:15:09 +0530664 if (nexttbtt == 0) /* e.g. for ap mode */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700665 nexttbtt = intval;
Sujith980b24d2008-09-17 10:15:09 +0530666 else if (intval) /* NB: can be 0 for monitor mode */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700667 nexttbtt = roundup(nexttbtt, intval);
Sujith980b24d2008-09-17 10:15:09 +0530668
Sujith04bd46382008-11-28 22:18:05 +0530669 DPRINTF(sc, ATH_DBG_BEACON, "nexttbtt %u intval %u (%u)\n",
670 nexttbtt, intval, conf.beacon_interval);
Sujith980b24d2008-09-17 10:15:09 +0530671
Colin McCabed97809d2008-12-01 13:38:55 -0800672 /* Check for NL80211_IFTYPE_AP and sc_nostabeacons for WDS client */
673 if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_STATION) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700674 struct ath9k_beacon_state bs;
675 u64 tsf;
676 u32 tsftu;
677 int dtimperiod, dtimcount, sleepduration;
678 int cfpperiod, cfpcount;
679
680 /*
681 * Setup dtim and cfp parameters according to
682 * last beacon we received (which may be none).
683 */
684 dtimperiod = conf.dtim_period;
Sujith980b24d2008-09-17 10:15:09 +0530685 if (dtimperiod <= 0) /* NB: 0 if not known */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700686 dtimperiod = 1;
687 dtimcount = conf.dtim_count;
Sujith980b24d2008-09-17 10:15:09 +0530688 if (dtimcount >= dtimperiod) /* NB: sanity check */
689 dtimcount = 0;
690 cfpperiod = 1; /* NB: no PCF support yet */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700691 cfpcount = 0;
692
693 sleepduration = conf.listen_interval * intval;
694 if (sleepduration <= 0)
695 sleepduration = intval;
696
Sujith980b24d2008-09-17 10:15:09 +0530697#define FUDGE 2
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700698 /*
699 * Pull nexttbtt forward to reflect the current
700 * TSF and calculate dtim+cfp state for the result.
701 */
702 tsf = ath9k_hw_gettsf64(ah);
703 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
704 do {
705 nexttbtt += intval;
706 if (--dtimcount < 0) {
707 dtimcount = dtimperiod - 1;
708 if (--cfpcount < 0)
709 cfpcount = cfpperiod - 1;
710 }
711 } while (nexttbtt < tsftu);
712#undef FUDGE
Luis R. Rodriguez0345f372008-10-03 15:45:25 -0700713 memset(&bs, 0, sizeof(bs));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700714 bs.bs_intval = intval;
715 bs.bs_nexttbtt = nexttbtt;
716 bs.bs_dtimperiod = dtimperiod*intval;
717 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
718 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
719 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
720 bs.bs_cfpmaxduration = 0;
Sujith980b24d2008-09-17 10:15:09 +0530721
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700722 /*
723 * Calculate the number of consecutive beacons to miss
724 * before taking a BMISS interrupt. The configuration
725 * is specified in TU so we only need calculate based
726 * on the beacon interval. Note that we clamp the
727 * result to at most 15 beacons.
728 */
729 if (sleepduration > intval) {
Sujith980b24d2008-09-17 10:15:09 +0530730 bs.bs_bmissthreshold = conf.listen_interval *
731 ATH_DEFAULT_BMISS_LIMIT / 2;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700732 } else {
733 bs.bs_bmissthreshold =
734 DIV_ROUND_UP(conf.bmiss_timeout, intval);
735 if (bs.bs_bmissthreshold > 15)
736 bs.bs_bmissthreshold = 15;
737 else if (bs.bs_bmissthreshold <= 0)
738 bs.bs_bmissthreshold = 1;
739 }
740
741 /*
742 * Calculate sleep duration. The configuration is
743 * given in ms. We insure a multiple of the beacon
744 * period is used. Also, if the sleep duration is
745 * greater than the DTIM period then it makes senses
746 * to make it a multiple of that.
747 *
748 * XXX fixed at 100ms
749 */
750
Sujith980b24d2008-09-17 10:15:09 +0530751 bs.bs_sleepduration = roundup(IEEE80211_MS_TO_TU(100),
752 sleepduration);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700753 if (bs.bs_sleepduration > bs.bs_dtimperiod)
754 bs.bs_sleepduration = bs.bs_dtimperiod;
755
756 DPRINTF(sc, ATH_DBG_BEACON,
Sujith04bd46382008-11-28 22:18:05 +0530757 "tsf %llu "
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700758 "tsf:tu %u "
759 "intval %u "
760 "nexttbtt %u "
761 "dtim %u "
762 "nextdtim %u "
763 "bmiss %u "
764 "sleep %u "
765 "cfp:period %u "
766 "maxdur %u "
767 "next %u "
Sujithff9b6622008-08-14 13:27:16 +0530768 "timoffset %u\n",
Sujithff9b6622008-08-14 13:27:16 +0530769 (unsigned long long)tsf, tsftu,
770 bs.bs_intval,
771 bs.bs_nexttbtt,
772 bs.bs_dtimperiod,
773 bs.bs_nextdtim,
774 bs.bs_bmissthreshold,
775 bs.bs_sleepduration,
776 bs.bs_cfpperiod,
777 bs.bs_cfpmaxduration,
778 bs.bs_cfpnext,
779 bs.bs_timoffset
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700780 );
781
782 ath9k_hw_set_interrupts(ah, 0);
783 ath9k_hw_set_sta_beacon_timers(ah, &bs);
784 sc->sc_imask |= ATH9K_INT_BMISS;
785 ath9k_hw_set_interrupts(ah, sc->sc_imask);
786 } else {
787 u64 tsf;
788 u32 tsftu;
789 ath9k_hw_set_interrupts(ah, 0);
790 if (nexttbtt == intval)
791 intval |= ATH9K_BEACON_RESET_TSF;
Colin McCabed97809d2008-12-01 13:38:55 -0800792 if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_ADHOC) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700793 /*
794 * Pull nexttbtt forward to reflect the current
Sujith980b24d2008-09-17 10:15:09 +0530795 * TSF
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700796 */
Sujith980b24d2008-09-17 10:15:09 +0530797#define FUDGE 2
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700798 if (!(intval & ATH9K_BEACON_RESET_TSF)) {
799 tsf = ath9k_hw_gettsf64(ah);
800 tsftu = TSF_TO_TU((u32)(tsf>>32),
801 (u32)tsf) + FUDGE;
802 do {
803 nexttbtt += intval;
804 } while (nexttbtt < tsftu);
805 }
806#undef FUDGE
807 DPRINTF(sc, ATH_DBG_BEACON,
Sujith04bd46382008-11-28 22:18:05 +0530808 "IBSS nexttbtt %u intval %u (%u)\n",
809 nexttbtt,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700810 intval & ~ATH9K_BEACON_RESET_TSF,
811 conf.beacon_interval);
812
813 /*
814 * In IBSS mode enable the beacon timers but only
815 * enable SWBA interrupts if we need to manually
816 * prepare beacon frames. Otherwise we use a
817 * self-linked tx descriptor and let the hardware
818 * deal with things.
819 */
820 intval |= ATH9K_BEACON_ENA;
Sujith60b67f52008-08-07 10:52:38 +0530821 if (!(ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700822 sc->sc_imask |= ATH9K_INT_SWBA;
823 ath_beaconq_config(sc);
Colin McCabed97809d2008-12-01 13:38:55 -0800824 } else if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_AP) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700825 /*
826 * In AP mode we enable the beacon timers and
827 * SWBA interrupts to prepare beacon frames.
828 */
829 intval |= ATH9K_BEACON_ENA;
830 sc->sc_imask |= ATH9K_INT_SWBA; /* beacon prepare */
831 ath_beaconq_config(sc);
832 }
833 ath9k_hw_beaconinit(ah, nexttbtt, intval);
Sujithb77f4832008-12-07 21:44:03 +0530834 sc->beacon.bmisscnt = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700835 ath9k_hw_set_interrupts(ah, sc->sc_imask);
836 /*
837 * When using a self-linked beacon descriptor in
838 * ibss mode load it once here.
839 */
Colin McCabed97809d2008-12-01 13:38:55 -0800840 if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_ADHOC &&
Sujith60b67f52008-08-07 10:52:38 +0530841 (ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700842 ath_beacon_start_adhoc(sc, 0);
843 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700844}
845
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700846void ath_beacon_sync(struct ath_softc *sc, int if_id)
847{
848 /*
849 * Resync beacon timers using the tsf of the
850 * beacon frame we just received.
851 */
852 ath_beacon_config(sc, if_id);
Sujith672840a2008-08-11 14:05:08 +0530853 sc->sc_flags |= SC_OP_BEACONS;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700854}