nl80211: Change max TX power to be in mBm instead of dBm
[linux-2.6-block.git] / drivers / net / wireless / ath9k / beacon.c
CommitLineData
f078f209
LR
1/*
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
17 /* Implementation of beacon processing. */
18
f078f209
LR
19#include "core.h"
20
21/*
22 * Configure parameters for the beacon queue
23 *
24 * This function will modify certain transmit queue properties depending on
25 * the operating mode of the station (AP or AdHoc). Parameters are AIFS
26 * settings and channel width min/max
27*/
f078f209
LR
28static int ath_beaconq_config(struct ath_softc *sc)
29{
30 struct ath_hal *ah = sc->sc_ah;
ea9880fb 31 struct ath9k_tx_queue_info qi;
f078f209 32
ea9880fb 33 ath9k_hw_get_txq_props(ah, sc->sc_bhalq, &qi);
b4696c8b 34 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) {
f078f209
LR
35 /* Always burst out beacon and CAB traffic. */
36 qi.tqi_aifs = 1;
37 qi.tqi_cwmin = 0;
38 qi.tqi_cwmax = 0;
39 } else {
40 /* Adhoc mode; important thing is to use 2x cwmin. */
41 qi.tqi_aifs = sc->sc_beacon_qi.tqi_aifs;
42 qi.tqi_cwmin = 2*sc->sc_beacon_qi.tqi_cwmin;
43 qi.tqi_cwmax = sc->sc_beacon_qi.tqi_cwmax;
44 }
45
ea9880fb 46 if (!ath9k_hw_set_txq_props(ah, sc->sc_bhalq, &qi)) {
f078f209
LR
47 DPRINTF(sc, ATH_DBG_FATAL,
48 "%s: unable to update h/w beacon queue parameters\n",
49 __func__);
50 return 0;
51 } else {
52 ath9k_hw_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */
53 return 1;
54 }
55}
56
57/*
58 * Setup the beacon frame for transmit.
59 *
60 * Associates the beacon frame buffer with a transmit descriptor. Will set
61 * up all required antenna switch parameters, rate codes, and channel flags.
62 * Beacons are always sent out at the lowest rate, and are not retried.
63*/
f078f209 64static void ath_beacon_setup(struct ath_softc *sc,
980b24da 65 struct ath_vap *avp, struct ath_buf *bf)
f078f209
LR
66{
67 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
68 struct ath_hal *ah = sc->sc_ah;
69 struct ath_desc *ds;
980b24da 70 struct ath9k_11n_rate_series series[4];
e63835b0 71 struct ath_rate_table *rt;
980b24da 72 int flags, antenna;
f078f209
LR
73 u8 rix, rate;
74 int ctsrate = 0;
75 int ctsduration = 0;
f078f209
LR
76
77 DPRINTF(sc, ATH_DBG_BEACON, "%s: m %p len %u\n",
78 __func__, skb, skb->len);
79
80 /* setup descriptors */
81 ds = bf->bf_desc;
82
83 flags = ATH9K_TXDESC_NOACK;
84
b4696c8b 85 if (sc->sc_ah->ah_opmode == ATH9K_M_IBSS &&
60b67f51 86 (ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
f078f209
LR
87 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 */
99 antenna = ((sc->ast_be_xmit / sc->sc_nbcnvaps) & 1 ? 2 : 1);
100 }
101
102 ds->ds_data = bf->bf_buf_addr;
103
104 /*
105 * Calculate rate code.
106 * XXX everything at min xmit rate
107 */
86b89eed 108 rix = 0;
e63835b0
S
109 rt = sc->hw_rate_table[sc->sc_curmode];
110 rate = rt->info[rix].ratecode;
672840ac 111 if (sc->sc_flags & SC_OP_PREAMBLE_SHORT)
e63835b0 112 rate |= rt->info[rix].short_preamble;
f078f209 113
ff9b662d 114 ath9k_hw_set11n_txdesc(ah, ds,
980b24da
S
115 skb->len + FCS_LEN, /* frame length */
116 ATH9K_PKT_TYPE_BEACON, /* Atheros packet type */
528f0c6b 117 MAX_RATE_POWER, /* FIXME */
980b24da
S
118 ATH9K_TXKEYIX_INVALID, /* no encryption */
119 ATH9K_KEY_TYPE_CLEAR, /* no encryption */
120 flags /* no ack,
121 veol for beacons */
f078f209
LR
122 );
123
124 /* NB: beacon's BufLen must be a multiple of 4 bytes */
ff9b662d
S
125 ath9k_hw_filltxdesc(ah, ds,
126 roundup(skb->len, 4), /* buffer length */
980b24da
S
127 true, /* first segment */
128 true, /* last segment */
129 ds /* first descriptor */
f078f209
LR
130 );
131
0345f37b 132 memset(series, 0, sizeof(struct ath9k_11n_rate_series) * 4);
f078f209
LR
133 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
f078f209
LR
141/*
142 * Generate beacon frame and queue cab data for a vap.
143 *
144 * Updates the contents of the beacon frame. It is assumed that the buffer for
145 * the beacon frame has been allocated in the ATH object, and simply needs to
146 * be filled for this cycle. Also, any CAB (crap after beacon?) traffic will
147 * be added to the beacon frame at this point.
148*/
149static struct ath_buf *ath_beacon_generate(struct ath_softc *sc, int if_id)
150{
f078f209
LR
151 struct ath_buf *bf;
152 struct ath_vap *avp;
153 struct sk_buff *skb;
f078f209 154 struct ath_txq *cabq;
5640b08e 155 struct ieee80211_vif *vif;
147583c0 156 struct ieee80211_tx_info *info;
980b24da
S
157 int cabq_depth;
158
5640b08e
S
159 vif = sc->sc_vaps[if_id];
160 ASSERT(vif);
f078f209 161
5640b08e 162 avp = (void *)vif->drv_priv;
f078f209
LR
163 cabq = sc->sc_cabq;
164
f078f209
LR
165 if (avp->av_bcbuf == NULL) {
166 DPRINTF(sc, ATH_DBG_BEACON, "%s: avp=%p av_bcbuf=%p\n",
167 __func__, avp, avp->av_bcbuf);
168 return NULL;
169 }
980b24da 170
f078f209 171 bf = avp->av_bcbuf;
980b24da 172 skb = (struct sk_buff *)bf->bf_mpdu;
a8fff50e
JM
173 if (skb) {
174 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
175 skb_end_pointer(skb) - skb->head,
176 PCI_DMA_TODEVICE);
177 }
f078f209 178
5640b08e 179 skb = ieee80211_beacon_get(sc->hw, vif);
a8fff50e
JM
180 bf->bf_mpdu = skb;
181 if (skb == NULL)
182 return NULL;
980b24da 183
147583c0
JM
184 info = IEEE80211_SKB_CB(skb);
185 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
186 /*
187 * TODO: make sure the seq# gets assigned properly (vs. other
188 * TX frames)
189 */
980b24da 190 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
147583c0
JM
191 sc->seq_no += 0x10;
192 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
193 hdr->seq_ctrl |= cpu_to_le16(sc->seq_no);
194 }
980b24da 195
a8fff50e
JM
196 bf->bf_buf_addr = bf->bf_dmacontext =
197 pci_map_single(sc->pdev, skb->data,
198 skb_end_pointer(skb) - skb->head,
199 PCI_DMA_TODEVICE);
f078f209 200
5640b08e 201 skb = ieee80211_get_buffered_bc(sc->hw, vif);
f078f209 202
f078f209
LR
203 /*
204 * if the CABQ traffic from previous DTIM is pending and the current
205 * beacon is also a DTIM.
206 * 1) if there is only one vap let the cab traffic continue.
207 * 2) if there are more than one vap and we are using staggered
208 * beacons, then drain the cabq by dropping all the frames in
209 * the cabq so that the current vaps cab traffic can be scheduled.
210 */
211 spin_lock_bh(&cabq->axq_lock);
212 cabq_depth = cabq->axq_depth;
213 spin_unlock_bh(&cabq->axq_lock);
214
e022edbd 215 if (skb && cabq_depth) {
f078f209
LR
216 /*
217 * Unlock the cabq lock as ath_tx_draintxq acquires
218 * the lock again which is a common function and that
219 * acquires txq lock inside.
220 */
221 if (sc->sc_nvaps > 1) {
222 ath_tx_draintxq(sc, cabq, false);
223 DPRINTF(sc, ATH_DBG_BEACON,
224 "%s: flush previous cabq traffic\n", __func__);
225 }
226 }
227
228 /* Construct tx descriptor. */
229 ath_beacon_setup(sc, avp, bf);
230
231 /*
232 * Enable the CAB queue before the beacon queue to
233 * insure cab frames are triggered by this beacon.
234 */
e022edbd
JM
235 while (skb) {
236 ath_tx_cabq(sc, skb);
5640b08e 237 skb = ieee80211_get_buffered_bc(sc->hw, vif);
e022edbd 238 }
f078f209 239
f078f209
LR
240 return bf;
241}
242
243/*
244 * Startup beacon transmission for adhoc mode when they are sent entirely
245 * by the hardware using the self-linked descriptor + veol trick.
246*/
f078f209
LR
247static void ath_beacon_start_adhoc(struct ath_softc *sc, int if_id)
248{
5640b08e 249 struct ieee80211_vif *vif;
f078f209
LR
250 struct ath_hal *ah = sc->sc_ah;
251 struct ath_buf *bf;
252 struct ath_vap *avp;
253 struct sk_buff *skb;
254
5640b08e
S
255 vif = sc->sc_vaps[if_id];
256 ASSERT(vif);
257
258 avp = (void *)vif->drv_priv;
f078f209
LR
259
260 if (avp->av_bcbuf == NULL) {
261 DPRINTF(sc, ATH_DBG_BEACON, "%s: avp=%p av_bcbuf=%p\n",
262 __func__, avp, avp != NULL ? avp->av_bcbuf : NULL);
263 return;
264 }
265 bf = avp->av_bcbuf;
266 skb = (struct sk_buff *) bf->bf_mpdu;
267
268 /* Construct tx descriptor. */
269 ath_beacon_setup(sc, avp, bf);
270
271 /* NB: caller is known to have already stopped tx dma */
272 ath9k_hw_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
273 ath9k_hw_txstart(ah, sc->sc_bhalq);
274 DPRINTF(sc, ATH_DBG_BEACON, "%s: TXDP%u = %llx (%p)\n", __func__,
275 sc->sc_bhalq, ito64(bf->bf_daddr), bf->bf_desc);
276}
277
278/*
279 * Setup a h/w transmit queue for beacons.
280 *
281 * This function allocates an information structure (struct ath9k_txq_info)
282 * on the stack, sets some specific parameters (zero out channel width
283 * min/max, and enable aifs). The info structure does not need to be
284 * persistant.
285*/
f078f209
LR
286int ath_beaconq_setup(struct ath_hal *ah)
287{
ea9880fb 288 struct ath9k_tx_queue_info qi;
f078f209 289
0345f37b 290 memset(&qi, 0, sizeof(qi));
f078f209
LR
291 qi.tqi_aifs = 1;
292 qi.tqi_cwmin = 0;
293 qi.tqi_cwmax = 0;
294 /* NB: don't enable any interrupts */
295 return ath9k_hw_setuptxqueue(ah, ATH9K_TX_QUEUE_BEACON, &qi);
296}
297
298
299/*
300 * Allocate and setup an initial beacon frame.
301 *
302 * Allocate a beacon state variable for a specific VAP instance created on
303 * the ATH interface. This routine also calculates the beacon "slot" for
304 * staggared beacons in the mBSSID case.
305*/
f078f209
LR
306int ath_beacon_alloc(struct ath_softc *sc, int if_id)
307{
5640b08e 308 struct ieee80211_vif *vif;
f078f209 309 struct ath_vap *avp;
980b24da 310 struct ieee80211_hdr *hdr;
f078f209
LR
311 struct ath_buf *bf;
312 struct sk_buff *skb;
459f5f90 313 __le64 tstamp;
f078f209 314
5640b08e
S
315 vif = sc->sc_vaps[if_id];
316 ASSERT(vif);
317
318 avp = (void *)vif->drv_priv;
f078f209
LR
319
320 /* Allocate a beacon descriptor if we haven't done so. */
321 if (!avp->av_bcbuf) {
980b24da
S
322 /* Allocate beacon state for hostap/ibss. We know
323 * a buffer is available. */
f078f209
LR
324
325 avp->av_bcbuf = list_first_entry(&sc->sc_bbuf,
980b24da 326 struct ath_buf, list);
f078f209
LR
327 list_del(&avp->av_bcbuf->list);
328
b4696c8b 329 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP ||
60b67f51 330 !(sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
f078f209
LR
331 int slot;
332 /*
333 * Assign the vap to a beacon xmit slot. As
334 * above, this cannot fail to find one.
335 */
336 avp->av_bslot = 0;
337 for (slot = 0; slot < ATH_BCBUF; slot++)
338 if (sc->sc_bslot[slot] == ATH_IF_ID_ANY) {
339 /*
340 * XXX hack, space out slots to better
341 * deal with misses
342 */
343 if (slot+1 < ATH_BCBUF &&
344 sc->sc_bslot[slot+1] ==
345 ATH_IF_ID_ANY) {
346 avp->av_bslot = slot+1;
347 break;
348 }
349 avp->av_bslot = slot;
350 /* NB: keep looking for a double slot */
351 }
352 BUG_ON(sc->sc_bslot[avp->av_bslot] != ATH_IF_ID_ANY);
353 sc->sc_bslot[avp->av_bslot] = if_id;
354 sc->sc_nbcnvaps++;
355 }
356 }
357
358 /* release the previous beacon frame , if it already exists. */
359 bf = avp->av_bcbuf;
360 if (bf->bf_mpdu != NULL) {
361 skb = (struct sk_buff *)bf->bf_mpdu;
a8fff50e
JM
362 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
363 skb_end_pointer(skb) - skb->head,
364 PCI_DMA_TODEVICE);
f078f209
LR
365 dev_kfree_skb_any(skb);
366 bf->bf_mpdu = NULL;
367 }
368
369 /*
980b24da 370 * NB: the beacon data buffer must be 32-bit aligned.
e022edbd 371 * FIXME: Fill avp->av_btxctl.txpower and
f078f209
LR
372 * avp->av_btxctl.shortPreamble
373 */
5640b08e 374 skb = ieee80211_beacon_get(sc->hw, vif);
f078f209
LR
375 if (skb == NULL) {
376 DPRINTF(sc, ATH_DBG_BEACON, "%s: cannot get skb\n",
377 __func__);
378 return -ENOMEM;
379 }
380
459f5f90
S
381 tstamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
382 sc->bc_tstamp = le64_to_cpu(tstamp);
383
f078f209
LR
384 /*
385 * Calculate a TSF adjustment factor required for
386 * staggered beacons. Note that we assume the format
387 * of the beacon frame leaves the tstamp field immediately
388 * following the header.
389 */
390 if (avp->av_bslot > 0) {
391 u64 tsfadjust;
392 __le64 val;
393 int intval;
394
a8fff50e
JM
395 intval = sc->hw->conf.beacon_int ?
396 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
f078f209
LR
397
398 /*
399 * The beacon interval is in TU's; the TSF in usecs.
400 * We figure out how many TU's to add to align the
401 * timestamp then convert to TSF units and handle
402 * byte swapping before writing it in the frame.
403 * The hardware will then add this each time a beacon
404 * frame is sent. Note that we align vap's 1..N
405 * and leave vap 0 untouched. This means vap 0
406 * has a timestamp in one beacon interval while the
407 * others get a timestamp aligned to the next interval.
408 */
409 tsfadjust = (intval * (ATH_BCBUF - avp->av_bslot)) / ATH_BCBUF;
410 val = cpu_to_le64(tsfadjust << 10); /* TU->TSF */
411
412 DPRINTF(sc, ATH_DBG_BEACON,
413 "%s: %s beacons, bslot %d intval %u tsfadjust %llu\n",
414 __func__, "stagger",
415 avp->av_bslot, intval, (unsigned long long)tsfadjust);
416
980b24da
S
417 hdr = (struct ieee80211_hdr *)skb->data;
418 memcpy(&hdr[1], &val, sizeof(val));
f078f209
LR
419 }
420
a8fff50e
JM
421 bf->bf_buf_addr = bf->bf_dmacontext =
422 pci_map_single(sc->pdev, skb->data,
423 skb_end_pointer(skb) - skb->head,
424 PCI_DMA_TODEVICE);
f078f209
LR
425 bf->bf_mpdu = skb;
426
427 return 0;
428}
429
430/*
431 * Reclaim beacon resources and return buffer to the pool.
432 *
433 * Checks the VAP to put the beacon frame buffer back to the ATH object
980b24da 434 * queue, and de-allocates any skbs that were sent as CAB traffic.
f078f209 435*/
f078f209
LR
436void ath_beacon_return(struct ath_softc *sc, struct ath_vap *avp)
437{
438 if (avp->av_bcbuf != NULL) {
439 struct ath_buf *bf;
440
441 if (avp->av_bslot != -1) {
442 sc->sc_bslot[avp->av_bslot] = ATH_IF_ID_ANY;
443 sc->sc_nbcnvaps--;
444 }
445
446 bf = avp->av_bcbuf;
447 if (bf->bf_mpdu != NULL) {
448 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
a8fff50e
JM
449 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
450 skb_end_pointer(skb) - skb->head,
451 PCI_DMA_TODEVICE);
f078f209
LR
452 dev_kfree_skb_any(skb);
453 bf->bf_mpdu = NULL;
454 }
455 list_add_tail(&bf->list, &sc->sc_bbuf);
456
457 avp->av_bcbuf = NULL;
458 }
459}
460
f078f209
LR
461/*
462 * Tasklet for Sending Beacons
463 *
464 * Transmit one or more beacon frames at SWBA. Dynamic updates to the frame
465 * contents are done as needed and the slot time is also adjusted based on
466 * current state.
f078f209 467*/
f078f209
LR
468void ath9k_beacon_tasklet(unsigned long data)
469{
f078f209
LR
470 struct ath_softc *sc = (struct ath_softc *)data;
471 struct ath_hal *ah = sc->sc_ah;
472 struct ath_buf *bf = NULL;
473 int slot, if_id;
474 u32 bfaddr;
475 u32 rx_clear = 0, rx_frame = 0, tx_frame = 0;
476 u32 show_cycles = 0;
477 u32 bc = 0; /* beacon count */
478 u64 tsf;
479 u32 tsftu;
480 u16 intval;
481
98deeea0 482 if (sc->sc_flags & SC_OP_NO_RESET) {
f078f209
LR
483 show_cycles = ath9k_hw_GetMibCycleCountsPct(ah,
484 &rx_clear,
485 &rx_frame,
486 &tx_frame);
487 }
488
489 /*
490 * Check if the previous beacon has gone out. If
491 * not don't try to post another, skip this period
492 * and wait for the next. Missed beacons indicate
493 * a problem and should not occur. If we miss too
494 * many consecutive beacons reset the device.
980b24da
S
495 *
496 * FIXME: Clean up this mess !!
f078f209
LR
497 */
498 if (ath9k_hw_numtxpending(ah, sc->sc_bhalq) != 0) {
499 sc->sc_bmisscount++;
500 /* XXX: doth needs the chanchange IE countdown decremented.
501 * We should consider adding a mac80211 call to indicate
502 * a beacon miss so appropriate action could be taken
503 * (in that layer).
504 */
505 if (sc->sc_bmisscount < BSTUCK_THRESH) {
98deeea0 506 if (sc->sc_flags & SC_OP_NO_RESET) {
f078f209
LR
507 DPRINTF(sc, ATH_DBG_BEACON,
508 "%s: missed %u consecutive beacons\n",
509 __func__, sc->sc_bmisscount);
510 if (show_cycles) {
511 /*
980b24da
S
512 * Display cycle counter stats from HW
513 * to aide in debug of stickiness.
f078f209 514 */
980b24da 515 DPRINTF(sc, ATH_DBG_BEACON,
f078f209
LR
516 "%s: busy times: rx_clear=%d, "
517 "rx_frame=%d, tx_frame=%d\n",
518 __func__, rx_clear, rx_frame,
519 tx_frame);
520 } else {
980b24da 521 DPRINTF(sc, ATH_DBG_BEACON,
f078f209
LR
522 "%s: unable to obtain "
523 "busy times\n", __func__);
524 }
525 } else {
526 DPRINTF(sc, ATH_DBG_BEACON,
527 "%s: missed %u consecutive beacons\n",
528 __func__, sc->sc_bmisscount);
529 }
530 } else if (sc->sc_bmisscount >= BSTUCK_THRESH) {
98deeea0 531 if (sc->sc_flags & SC_OP_NO_RESET) {
f078f209 532 if (sc->sc_bmisscount == BSTUCK_THRESH) {
980b24da 533 DPRINTF(sc, ATH_DBG_BEACON,
f078f209
LR
534 "%s: beacon is officially "
535 "stuck\n", __func__);
536 ath9k_hw_dmaRegDump(ah);
537 }
538 } else {
539 DPRINTF(sc, ATH_DBG_BEACON,
540 "%s: beacon is officially stuck\n",
541 __func__);
542 ath_bstuck_process(sc);
543 }
544 }
f078f209
LR
545 return;
546 }
980b24da 547
f078f209 548 if (sc->sc_bmisscount != 0) {
98deeea0 549 if (sc->sc_flags & SC_OP_NO_RESET) {
980b24da 550 DPRINTF(sc, ATH_DBG_BEACON,
f078f209
LR
551 "%s: resume beacon xmit after %u misses\n",
552 __func__, sc->sc_bmisscount);
553 } else {
554 DPRINTF(sc, ATH_DBG_BEACON,
555 "%s: resume beacon xmit after %u misses\n",
556 __func__, sc->sc_bmisscount);
557 }
558 sc->sc_bmisscount = 0;
559 }
560
561 /*
562 * Generate beacon frames. we are sending frames
563 * staggered so calculate the slot for this frame based
564 * on the tsf to safeguard against missing an swba.
565 */
566
a8fff50e
JM
567 intval = sc->hw->conf.beacon_int ?
568 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
f078f209
LR
569
570 tsf = ath9k_hw_gettsf64(ah);
571 tsftu = TSF_TO_TU(tsf>>32, tsf);
572 slot = ((tsftu % intval) * ATH_BCBUF) / intval;
573 if_id = sc->sc_bslot[(slot + 1) % ATH_BCBUF];
980b24da 574
f078f209 575 DPRINTF(sc, ATH_DBG_BEACON,
980b24da
S
576 "%s: slot %d [tsf %llu tsftu %u intval %u] if_id %d\n",
577 __func__, slot, (unsigned long long)tsf, tsftu,
578 intval, if_id);
579
f078f209
LR
580 bfaddr = 0;
581 if (if_id != ATH_IF_ID_ANY) {
582 bf = ath_beacon_generate(sc, if_id);
583 if (bf != NULL) {
584 bfaddr = bf->bf_daddr;
585 bc = 1;
586 }
587 }
588 /*
589 * Handle slot time change when a non-ERP station joins/leaves
590 * an 11g network. The 802.11 layer notifies us via callback,
591 * we mark updateslot, then wait one beacon before effecting
592 * the change. This gives associated stations at least one
593 * beacon interval to note the state change.
594 *
595 * NB: The slot time change state machine is clocked according
596 * to whether we are bursting or staggering beacons. We
597 * recognize the request to update and record the current
598 * slot then don't transition until that slot is reached
599 * again. If we miss a beacon for that slot then we'll be
600 * slow to transition but we'll be sure at least one beacon
601 * interval has passed. When bursting slot is always left
602 * set to ATH_BCBUF so this check is a noop.
603 */
604 /* XXX locking */
605 if (sc->sc_updateslot == UPDATE) {
606 sc->sc_updateslot = COMMIT; /* commit next beacon */
607 sc->sc_slotupdate = slot;
608 } else if (sc->sc_updateslot == COMMIT && sc->sc_slotupdate == slot)
609 ath_setslottime(sc); /* commit change to hardware */
610
611 if (bfaddr != 0) {
612 /*
613 * Stop any current dma and put the new frame(s) on the queue.
614 * This should never fail since we check above that no frames
615 * are still pending on the queue.
616 */
617 if (!ath9k_hw_stoptxdma(ah, sc->sc_bhalq)) {
618 DPRINTF(sc, ATH_DBG_FATAL,
619 "%s: beacon queue %u did not stop?\n",
620 __func__, sc->sc_bhalq);
621 /* NB: the HAL still stops DMA, so proceed */
622 }
623
624 /* NB: cabq traffic should already be queued and primed */
625 ath9k_hw_puttxbuf(ah, sc->sc_bhalq, bfaddr);
626 ath9k_hw_txstart(ah, sc->sc_bhalq);
627
628 sc->ast_be_xmit += bc; /* XXX per-vap? */
629 }
f078f209
LR
630}
631
632/*
633 * Tasklet for Beacon Stuck processing
634 *
635 * Processing for Beacon Stuck.
980b24da 636 * Basically resets the chip.
f078f209 637*/
f078f209
LR
638void ath_bstuck_process(struct ath_softc *sc)
639{
640 DPRINTF(sc, ATH_DBG_BEACON,
641 "%s: stuck beacon; resetting (bmiss count %u)\n",
642 __func__, sc->sc_bmisscount);
f45144ef 643 ath_reset(sc, false);
f078f209
LR
644}
645
646/*
647 * Configure the beacon and sleep timers.
648 *
649 * When operating as an AP this resets the TSF and sets
650 * up the hardware to notify us when we need to issue beacons.
651 *
652 * When operating in station mode this sets up the beacon
653 * timers according to the timestamp of the last received
654 * beacon and the current TSF, configures PCF and DTIM
655 * handling, programs the sleep registers so the hardware
656 * will wakeup in time to receive beacons, and configures
657 * the beacon miss handling so we'll receive a BMISS
658 * interrupt when we stop seeing beacons from the AP
659 * we've associated with.
660 */
f078f209
LR
661void ath_beacon_config(struct ath_softc *sc, int if_id)
662{
5640b08e 663 struct ieee80211_vif *vif;
f078f209 664 struct ath_hal *ah = sc->sc_ah;
f078f209 665 struct ath_beacon_config conf;
5640b08e 666 struct ath_vap *avp;
f078f209 667 enum ath9k_opmode av_opmode;
980b24da 668 u32 nexttbtt, intval;
f078f209 669
5640b08e
S
670 if (if_id != ATH_IF_ID_ANY) {
671 vif = sc->sc_vaps[if_id];
672 ASSERT(vif);
673 avp = (void *)vif->drv_priv;
674 av_opmode = avp->av_opmode;
675 } else {
b4696c8b 676 av_opmode = sc->sc_ah->ah_opmode;
5640b08e 677 }
f078f209 678
0345f37b 679 memset(&conf, 0, sizeof(struct ath_beacon_config));
f078f209 680
a8fff50e
JM
681 conf.beacon_interval = sc->hw->conf.beacon_int ?
682 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
f078f209
LR
683 conf.listen_interval = 1;
684 conf.dtim_period = conf.beacon_interval;
685 conf.dtim_count = 1;
686 conf.bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf.beacon_interval;
687
688 /* extract tstamp from last beacon and convert to TU */
459f5f90
S
689 nexttbtt = TSF_TO_TU(sc->bc_tstamp >> 32, sc->bc_tstamp);
690
f078f209 691 /* XXX conditionalize multi-bss support? */
b4696c8b 692 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) {
f078f209
LR
693 /*
694 * For multi-bss ap support beacons are either staggered
695 * evenly over N slots or burst together. For the former
696 * arrange for the SWBA to be delivered for each slot.
697 * Slots that are not occupied will generate nothing.
698 */
699 /* NB: the beacon interval is kept internally in TU's */
700 intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
701 intval /= ATH_BCBUF; /* for staggered beacons */
702 } else {
703 intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
704 }
705
980b24da 706 if (nexttbtt == 0) /* e.g. for ap mode */
f078f209 707 nexttbtt = intval;
980b24da 708 else if (intval) /* NB: can be 0 for monitor mode */
f078f209 709 nexttbtt = roundup(nexttbtt, intval);
980b24da 710
f078f209
LR
711 DPRINTF(sc, ATH_DBG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
712 __func__, nexttbtt, intval, conf.beacon_interval);
980b24da 713
f078f209 714 /* Check for ATH9K_M_HOSTAP and sc_nostabeacons for WDS client */
b4696c8b 715 if (sc->sc_ah->ah_opmode == ATH9K_M_STA) {
f078f209
LR
716 struct ath9k_beacon_state bs;
717 u64 tsf;
718 u32 tsftu;
719 int dtimperiod, dtimcount, sleepduration;
720 int cfpperiod, cfpcount;
721
722 /*
723 * Setup dtim and cfp parameters according to
724 * last beacon we received (which may be none).
725 */
726 dtimperiod = conf.dtim_period;
980b24da 727 if (dtimperiod <= 0) /* NB: 0 if not known */
f078f209
LR
728 dtimperiod = 1;
729 dtimcount = conf.dtim_count;
980b24da
S
730 if (dtimcount >= dtimperiod) /* NB: sanity check */
731 dtimcount = 0;
732 cfpperiod = 1; /* NB: no PCF support yet */
f078f209
LR
733 cfpcount = 0;
734
735 sleepduration = conf.listen_interval * intval;
736 if (sleepduration <= 0)
737 sleepduration = intval;
738
980b24da 739#define FUDGE 2
f078f209
LR
740 /*
741 * Pull nexttbtt forward to reflect the current
742 * TSF and calculate dtim+cfp state for the result.
743 */
744 tsf = ath9k_hw_gettsf64(ah);
745 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
746 do {
747 nexttbtt += intval;
748 if (--dtimcount < 0) {
749 dtimcount = dtimperiod - 1;
750 if (--cfpcount < 0)
751 cfpcount = cfpperiod - 1;
752 }
753 } while (nexttbtt < tsftu);
754#undef FUDGE
0345f37b 755 memset(&bs, 0, sizeof(bs));
f078f209
LR
756 bs.bs_intval = intval;
757 bs.bs_nexttbtt = nexttbtt;
758 bs.bs_dtimperiod = dtimperiod*intval;
759 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
760 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
761 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
762 bs.bs_cfpmaxduration = 0;
980b24da 763
f078f209
LR
764 /*
765 * Calculate the number of consecutive beacons to miss
766 * before taking a BMISS interrupt. The configuration
767 * is specified in TU so we only need calculate based
768 * on the beacon interval. Note that we clamp the
769 * result to at most 15 beacons.
770 */
771 if (sleepduration > intval) {
980b24da
S
772 bs.bs_bmissthreshold = conf.listen_interval *
773 ATH_DEFAULT_BMISS_LIMIT / 2;
f078f209
LR
774 } else {
775 bs.bs_bmissthreshold =
776 DIV_ROUND_UP(conf.bmiss_timeout, intval);
777 if (bs.bs_bmissthreshold > 15)
778 bs.bs_bmissthreshold = 15;
779 else if (bs.bs_bmissthreshold <= 0)
780 bs.bs_bmissthreshold = 1;
781 }
782
783 /*
784 * Calculate sleep duration. The configuration is
785 * given in ms. We insure a multiple of the beacon
786 * period is used. Also, if the sleep duration is
787 * greater than the DTIM period then it makes senses
788 * to make it a multiple of that.
789 *
790 * XXX fixed at 100ms
791 */
792
980b24da
S
793 bs.bs_sleepduration = roundup(IEEE80211_MS_TO_TU(100),
794 sleepduration);
f078f209
LR
795 if (bs.bs_sleepduration > bs.bs_dtimperiod)
796 bs.bs_sleepduration = bs.bs_dtimperiod;
797
798 DPRINTF(sc, ATH_DBG_BEACON,
799 "%s: tsf %llu "
800 "tsf:tu %u "
801 "intval %u "
802 "nexttbtt %u "
803 "dtim %u "
804 "nextdtim %u "
805 "bmiss %u "
806 "sleep %u "
807 "cfp:period %u "
808 "maxdur %u "
809 "next %u "
ff9b662d
S
810 "timoffset %u\n",
811 __func__,
812 (unsigned long long)tsf, tsftu,
813 bs.bs_intval,
814 bs.bs_nexttbtt,
815 bs.bs_dtimperiod,
816 bs.bs_nextdtim,
817 bs.bs_bmissthreshold,
818 bs.bs_sleepduration,
819 bs.bs_cfpperiod,
820 bs.bs_cfpmaxduration,
821 bs.bs_cfpnext,
822 bs.bs_timoffset
f078f209
LR
823 );
824
825 ath9k_hw_set_interrupts(ah, 0);
826 ath9k_hw_set_sta_beacon_timers(ah, &bs);
827 sc->sc_imask |= ATH9K_INT_BMISS;
828 ath9k_hw_set_interrupts(ah, sc->sc_imask);
829 } else {
830 u64 tsf;
831 u32 tsftu;
832 ath9k_hw_set_interrupts(ah, 0);
833 if (nexttbtt == intval)
834 intval |= ATH9K_BEACON_RESET_TSF;
b4696c8b 835 if (sc->sc_ah->ah_opmode == ATH9K_M_IBSS) {
f078f209
LR
836 /*
837 * Pull nexttbtt forward to reflect the current
980b24da 838 * TSF
f078f209 839 */
980b24da 840#define FUDGE 2
f078f209
LR
841 if (!(intval & ATH9K_BEACON_RESET_TSF)) {
842 tsf = ath9k_hw_gettsf64(ah);
843 tsftu = TSF_TO_TU((u32)(tsf>>32),
844 (u32)tsf) + FUDGE;
845 do {
846 nexttbtt += intval;
847 } while (nexttbtt < tsftu);
848 }
849#undef FUDGE
850 DPRINTF(sc, ATH_DBG_BEACON,
851 "%s: IBSS nexttbtt %u intval %u (%u)\n",
852 __func__, nexttbtt,
853 intval & ~ATH9K_BEACON_RESET_TSF,
854 conf.beacon_interval);
855
856 /*
857 * In IBSS mode enable the beacon timers but only
858 * enable SWBA interrupts if we need to manually
859 * prepare beacon frames. Otherwise we use a
860 * self-linked tx descriptor and let the hardware
861 * deal with things.
862 */
863 intval |= ATH9K_BEACON_ENA;
60b67f51 864 if (!(ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
f078f209
LR
865 sc->sc_imask |= ATH9K_INT_SWBA;
866 ath_beaconq_config(sc);
b4696c8b 867 } else if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) {
f078f209
LR
868 /*
869 * In AP mode we enable the beacon timers and
870 * SWBA interrupts to prepare beacon frames.
871 */
872 intval |= ATH9K_BEACON_ENA;
873 sc->sc_imask |= ATH9K_INT_SWBA; /* beacon prepare */
874 ath_beaconq_config(sc);
875 }
876 ath9k_hw_beaconinit(ah, nexttbtt, intval);
877 sc->sc_bmisscount = 0;
878 ath9k_hw_set_interrupts(ah, sc->sc_imask);
879 /*
880 * When using a self-linked beacon descriptor in
881 * ibss mode load it once here.
882 */
b4696c8b 883 if (sc->sc_ah->ah_opmode == ATH9K_M_IBSS &&
60b67f51 884 (ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
f078f209
LR
885 ath_beacon_start_adhoc(sc, 0);
886 }
f078f209
LR
887}
888
889/* Function to collect beacon rssi data and resync beacon if necessary */
890
891void ath_beacon_sync(struct ath_softc *sc, int if_id)
892{
893 /*
894 * Resync beacon timers using the tsf of the
895 * beacon frame we just received.
896 */
897 ath_beacon_config(sc, if_id);
672840ac 898 sc->sc_flags |= SC_OP_BEACONS;
f078f209 899}