2 * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting
3 * Copyright (c) 2007-2009 Intel Corporation
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: head/sys/net80211/ieee80211_tdma.c 193114 2009-05-30 19:57:31Z sam $
31 * IEEE 802.11 TDMA mode support.
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/endian.h>
46 #include <sys/errno.h>
48 #include <sys/sysctl.h>
51 #include <net/if_media.h>
52 #include <net/if_llc.h>
53 #include <net/ethernet.h>
54 #include <net/route.h>
58 #include <netproto/802_11/ieee80211_var.h>
59 #include <netproto/802_11/ieee80211_tdma.h>
60 #include <netproto/802_11/ieee80211_input.h>
62 #ifndef TDMA_SLOTLEN_DEFAULT
63 #define TDMA_SLOTLEN_DEFAULT 10*1000 /* 10ms */
65 #ifndef TDMA_SLOTCNT_DEFAULT
66 #define TDMA_SLOTCNT_DEFAULT 2 /* 2x (pt-to-pt) */
68 #ifndef TDMA_BINTVAL_DEFAULT
69 #define TDMA_BINTVAL_DEFAULT 5 /* 5x ~= 100TU beacon intvl */
71 #ifndef TDMA_TXRATE_11B_DEFAULT
72 #define TDMA_TXRATE_11B_DEFAULT 2*11
74 #ifndef TDMA_TXRATE_11G_DEFAULT
75 #define TDMA_TXRATE_11G_DEFAULT 2*24
77 #ifndef TDMA_TXRATE_11A_DEFAULT
78 #define TDMA_TXRATE_11A_DEFAULT 2*24
80 #ifndef TDMA_TXRATE_TURBO_DEFAULT
81 #define TDMA_TXRATE_TURBO_DEFAULT 2*24
83 #ifndef TDMA_TXRATE_HALF_DEFAULT
84 #define TDMA_TXRATE_HALF_DEFAULT 2*12
86 #ifndef TDMA_TXRATE_QUARTER_DEFAULT
87 #define TDMA_TXRATE_QUARTER_DEFAULT 2*6
89 #ifndef TDMA_TXRATE_11NA_DEFAULT
90 #define TDMA_TXRATE_11NA_DEFAULT (4 | IEEE80211_RATE_MCS)
92 #ifndef TDMA_TXRATE_11NG_DEFAULT
93 #define TDMA_TXRATE_11NG_DEFAULT (4 | IEEE80211_RATE_MCS)
96 #define TDMA_VERSION_VALID(_version) \
97 (TDMA_VERSION_V2 <= (_version) && (_version) <= TDMA_VERSION)
98 #define TDMA_SLOTCNT_VALID(_slotcnt) \
99 (2 <= (_slotcnt) && (_slotcnt) <= TDMA_MAXSLOTS)
100 /* XXX magic constants */
101 #define TDMA_SLOTLEN_VALID(_slotlen) \
102 (2*100 <= (_slotlen) && (unsigned)(_slotlen) <= 0xfffff)
103 /* XXX probably should set a max */
104 #define TDMA_BINTVAL_VALID(_bintval) (1 <= (_bintval))
107 * This code is not prepared to handle more than 2 slots.
109 CTASSERT(TDMA_MAXSLOTS == 2);
111 static void tdma_vdetach(struct ieee80211vap *vap);
112 static int tdma_newstate(struct ieee80211vap *, enum ieee80211_state, int);
113 static void tdma_beacon_miss(struct ieee80211vap *vap);
114 static void tdma_recv_mgmt(struct ieee80211_node *, struct mbuf *,
115 int subtype, int rssi, int nf);
116 static int tdma_update(struct ieee80211vap *vap,
117 const struct ieee80211_tdma_param *tdma, struct ieee80211_node *ni,
119 static int tdma_process_params(struct ieee80211_node *ni,
120 const u_int8_t *ie, int rssi, int nf, const struct ieee80211_frame *wh);
123 settxparms(struct ieee80211vap *vap, enum ieee80211_phymode mode, int rate)
125 vap->iv_txparms[mode].ucastrate = rate;
126 vap->iv_txparms[mode].mcastrate = rate;
130 setackpolicy(struct ieee80211com *ic, int noack)
132 struct ieee80211_wme_state *wme = &ic->ic_wme;
135 for (ac = 0; ac < WME_NUM_AC; ac++) {
136 wme->wme_chanParams.cap_wmeParams[ac].wmep_noackPolicy = noack;
137 wme->wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy = noack;
142 ieee80211_tdma_vattach(struct ieee80211vap *vap)
144 struct ieee80211_tdma_state *ts;
146 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
147 ("not a tdma vap, caps 0x%x", vap->iv_caps));
149 ts = (struct ieee80211_tdma_state *) kmalloc(
150 sizeof(struct ieee80211_tdma_state), M_80211_VAP, M_NOWAIT | M_ZERO);
152 kprintf("%s: cannot allocate TDMA state block\n", __func__);
153 /* NB: fall back to adhdemo mode */
154 vap->iv_caps &= ~IEEE80211_C_TDMA;
157 /* NB: default configuration is passive so no beacons */
158 ts->tdma_version = TDMA_VERSION;
159 ts->tdma_slotlen = TDMA_SLOTLEN_DEFAULT;
160 ts->tdma_slotcnt = TDMA_SLOTCNT_DEFAULT;
161 ts->tdma_bintval = TDMA_BINTVAL_DEFAULT;
162 ts->tdma_slot = 1; /* passive operation */
164 /* setup default fixed rates */
165 settxparms(vap, IEEE80211_MODE_11A, TDMA_TXRATE_11A_DEFAULT);
166 settxparms(vap, IEEE80211_MODE_11B, TDMA_TXRATE_11B_DEFAULT);
167 settxparms(vap, IEEE80211_MODE_11G, TDMA_TXRATE_11G_DEFAULT);
168 settxparms(vap, IEEE80211_MODE_TURBO_A, TDMA_TXRATE_TURBO_DEFAULT);
169 settxparms(vap, IEEE80211_MODE_TURBO_G, TDMA_TXRATE_TURBO_DEFAULT);
170 settxparms(vap, IEEE80211_MODE_STURBO_A, TDMA_TXRATE_TURBO_DEFAULT);
171 settxparms(vap, IEEE80211_MODE_11NA, TDMA_TXRATE_11NA_DEFAULT);
172 settxparms(vap, IEEE80211_MODE_11NG, TDMA_TXRATE_11NG_DEFAULT);
173 settxparms(vap, IEEE80211_MODE_HALF, TDMA_TXRATE_HALF_DEFAULT);
174 settxparms(vap, IEEE80211_MODE_QUARTER, TDMA_TXRATE_QUARTER_DEFAULT);
176 setackpolicy(vap->iv_ic, 1); /* disable ACK's */
178 ts->tdma_opdetach = vap->iv_opdetach;
179 vap->iv_opdetach = tdma_vdetach;
180 ts->tdma_newstate = vap->iv_newstate;
181 vap->iv_newstate = tdma_newstate;
182 vap->iv_bmiss = tdma_beacon_miss;
183 ts->tdma_recv_mgmt = vap->iv_recv_mgmt;
184 vap->iv_recv_mgmt = tdma_recv_mgmt;
190 tdma_vdetach(struct ieee80211vap *vap)
192 struct ieee80211_tdma_state *ts = vap->iv_tdma;
195 /* NB: should not have touched any ic state */
198 ts->tdma_opdetach(vap);
199 kfree(vap->iv_tdma, M_80211_VAP);
202 setackpolicy(vap->iv_ic, 0); /* enable ACK's */
206 sta_leave(void *arg, struct ieee80211_node *ni)
208 struct ieee80211vap *vap = arg;
210 if (ni->ni_vap == vap && ni != vap->iv_bss)
211 ieee80211_node_leave(ni);
215 * TDMA state machine handler.
218 tdma_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
220 struct ieee80211_tdma_state *ts = vap->iv_tdma;
221 struct ieee80211com *ic = vap->iv_ic;
222 enum ieee80211_state ostate;
225 IEEE80211_LOCK_ASSERT(ic);
227 ostate = vap->iv_state;
228 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
229 __func__, ieee80211_state_name[ostate],
230 ieee80211_state_name[nstate], arg);
232 if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
233 callout_stop(&vap->iv_swbmiss);
234 if (nstate == IEEE80211_S_SCAN &&
235 (ostate == IEEE80211_S_INIT || ostate == IEEE80211_S_RUN) &&
236 ts->tdma_slot != 0) {
238 * Override adhoc behaviour when operating as a slave;
239 * we need to scan even if the channel is locked.
241 vap->iv_state = nstate; /* state transition */
242 ieee80211_cancel_scan(vap); /* background scan */
243 if (ostate == IEEE80211_S_RUN) {
244 /* purge station table; entries are stale */
245 ieee80211_iterate_nodes(&ic->ic_sta, sta_leave, vap);
247 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
248 ieee80211_check_scan(vap,
249 vap->iv_scanreq_flags,
250 vap->iv_scanreq_duration,
251 vap->iv_scanreq_mindwell,
252 vap->iv_scanreq_maxdwell,
253 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
254 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
256 ieee80211_check_scan_current(vap);
259 status = ts->tdma_newstate(vap, nstate, arg);
262 nstate == IEEE80211_S_RUN && ostate != IEEE80211_S_RUN &&
263 (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) &&
264 ts->tdma_slot != 0 &&
265 vap->iv_des_chan == IEEE80211_CHAN_ANYC) {
267 * Start s/w beacon miss timer for slave devices w/o
268 * hardware support. Note we do this only if we're
269 * not locked to a channel (i.e. roam to follow the
270 * master). The 2x is a fudge for our doing this in
273 vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
274 2 * vap->iv_bmissthreshold * ts->tdma_bintval *
275 ((ts->tdma_slotcnt * ts->tdma_slotlen) / 1024));
276 vap->iv_swbmiss_count = 0;
277 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
278 ieee80211_swbmiss, vap);
284 tdma_beacon_miss(struct ieee80211vap *vap)
286 struct ieee80211_tdma_state *ts = vap->iv_tdma;
288 KASSERT((vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
289 KASSERT(vap->iv_state == IEEE80211_S_RUN,
290 ("wrong state %d", vap->iv_state));
292 IEEE80211_DPRINTF(vap,
293 IEEE80211_MSG_STATE | IEEE80211_MSG_TDMA | IEEE80211_MSG_DEBUG,
294 "beacon miss, mode %u state %s\n",
295 vap->iv_opmode, ieee80211_state_name[vap->iv_state]);
297 if (ts->tdma_peer != NULL) { /* XXX? can this be null? */
298 ieee80211_notify_node_leave(vap->iv_bss);
299 ts->tdma_peer = NULL;
301 * Treat beacon miss like an associate failure wrt the
302 * scan policy; this forces the entry in the scan cache
303 * to be ignored after several tries.
305 ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr,
306 IEEE80211_STATUS_TIMEOUT);
309 ts->tdma_inuse = 0; /* clear slot usage */
311 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
315 tdma_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
316 int subtype, int rssi, int nf)
318 struct ieee80211com *ic = ni->ni_ic;
319 struct ieee80211vap *vap = ni->ni_vap;
320 struct ieee80211_tdma_state *ts = vap->iv_tdma;
322 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON &&
323 (ic->ic_flags & IEEE80211_F_SCAN) == 0) {
324 struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
325 struct ieee80211_scanparams scan;
327 if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
329 if (scan.tdma == NULL) {
331 * TDMA stations must beacon a TDMA ie; ignore
333 * XXX detect overlapping bss and change channel
335 IEEE80211_DISCARD(vap,
336 IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
337 wh, ieee80211_mgt_subtype_name[subtype >>
338 IEEE80211_FC0_SUBTYPE_SHIFT],
340 vap->iv_stats.is_rx_mgtdiscard++;
343 if (ni == vap->iv_bss &&
344 !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
346 * Fake up a node for this newly
347 * discovered member of the IBSS.
349 ni = ieee80211_add_neighbor(vap, wh, &scan);
351 /* NB: stat kept for alloc failure */
356 * Check for state updates.
358 if (IEEE80211_ADDR_EQ(wh->i_addr3, ni->ni_bssid)) {
360 * Count frame now that we know it's to be processed.
362 vap->iv_stats.is_rx_beacon++;
363 IEEE80211_NODE_STAT(ni, rx_beacons);
365 * Record tsf of last beacon. NB: this must be
366 * done before calling tdma_process_params
367 * as deeper routines reference it.
369 memcpy(&ni->ni_tstamp.data, scan.tstamp,
370 sizeof(ni->ni_tstamp.data));
372 * Count beacon frame for s/w bmiss handling.
374 vap->iv_swbmiss_count++;
376 * Process tdma ie. The contents are used to sync
377 * the slot timing, reconfigure the bss, etc.
379 (void) tdma_process_params(ni, scan.tdma, rssi, nf, wh);
383 * NB: defer remaining work to the adhoc code; this causes
384 * 2x parsing of the frame but should happen infrequently
387 ts->tdma_recv_mgmt(ni, m0, subtype, rssi, nf);
391 * Update TDMA state on receipt of a beacon frame with
392 * a TDMA information element. The sender's identity
393 * is provided so we can track who our peer is. If pickslot
394 * is non-zero we scan the slot allocation state in the ie
395 * to locate a free slot for our use.
398 tdma_update(struct ieee80211vap *vap, const struct ieee80211_tdma_param *tdma,
399 struct ieee80211_node *ni, int pickslot)
401 struct ieee80211_tdma_state *ts = vap->iv_tdma;
402 int slot, slotlen, update;
404 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
405 ("not a tdma vap, caps 0x%x", vap->iv_caps));
408 if (tdma->tdma_slotcnt != ts->tdma_slotcnt) {
409 if (!TDMA_SLOTCNT_VALID(tdma->tdma_slotcnt)) {
410 if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
411 kprintf("%s: bad slot cnt %u\n",
412 __func__, tdma->tdma_slotcnt);
415 update |= TDMA_UPDATE_SLOTCNT;
417 slotlen = le16toh(tdma->tdma_slotlen) * 100;
418 if (slotlen != ts->tdma_slotlen) {
419 if (!TDMA_SLOTLEN_VALID(slotlen)) {
420 if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
421 kprintf("%s: bad slot len %u\n",
425 update |= TDMA_UPDATE_SLOTLEN;
427 if (tdma->tdma_bintval != ts->tdma_bintval) {
428 if (!TDMA_BINTVAL_VALID(tdma->tdma_bintval)) {
429 if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
430 kprintf("%s: bad beacon interval %u\n",
431 __func__, tdma->tdma_bintval);
434 update |= TDMA_UPDATE_BINTVAL;
436 slot = ts->tdma_slot;
439 * Pick unoccupied slot. Note we never choose slot 0.
441 for (slot = tdma->tdma_slotcnt-1; slot > 0; slot--)
442 if (isclr(tdma->tdma_inuse, slot))
445 kprintf("%s: no free slot, slotcnt %u inuse: 0x%x\n",
446 __func__, tdma->tdma_slotcnt,
447 tdma->tdma_inuse[0]);
448 /* XXX need to do something better */
451 if (slot != ts->tdma_slot)
452 update |= TDMA_UPDATE_SLOT;
454 if (ni != ts->tdma_peer) {
455 /* update everything */
456 update = TDMA_UPDATE_SLOT
457 | TDMA_UPDATE_SLOTCNT
458 | TDMA_UPDATE_SLOTLEN
459 | TDMA_UPDATE_BINTVAL;
464 * New/changed parameters; update runtime state.
466 /* XXX overwrites user parameters */
467 if (update & TDMA_UPDATE_SLOTCNT)
468 ts->tdma_slotcnt = tdma->tdma_slotcnt;
469 if (update & TDMA_UPDATE_SLOTLEN)
470 ts->tdma_slotlen = slotlen;
471 if (update & TDMA_UPDATE_SLOT)
472 ts->tdma_slot = slot;
473 if (update & TDMA_UPDATE_BINTVAL)
474 ts->tdma_bintval = tdma->tdma_bintval;
475 /* mark beacon to be updated before next xmit */
476 ieee80211_beacon_notify(vap, IEEE80211_BEACON_TDMA);
478 IEEE80211_DPRINTF(vap, IEEE80211_MSG_TDMA,
479 "%s: slot %u slotcnt %u slotlen %u us bintval %u\n",
480 __func__, ts->tdma_slot, ts->tdma_slotcnt,
481 ts->tdma_slotlen, ts->tdma_bintval);
484 * Notify driver. Note we can be called before
485 * entering RUN state if we scanned and are
486 * joining an existing bss. In that case do not
487 * call the driver because not all necessary state
488 * has been setup. The next beacon will dtrt.
490 if (vap->iv_state == IEEE80211_S_RUN)
491 vap->iv_ic->ic_tdma_update(ni, tdma, update);
493 * Dispatch join event on first beacon from new master.
495 if (ts->tdma_peer != ni) {
496 if (ts->tdma_peer != NULL)
497 ieee80211_notify_node_leave(vap->iv_bss);
498 ieee80211_notify_node_join(ni, 1);
499 /* NB: no reference, we just use the address */
506 * Process received TDMA parameters.
509 tdma_process_params(struct ieee80211_node *ni, const u_int8_t *ie,
510 int rssi, int nf, const struct ieee80211_frame *wh)
512 struct ieee80211vap *vap = ni->ni_vap;
513 struct ieee80211_tdma_state *ts = vap->iv_tdma;
514 const struct ieee80211_tdma_param *tdma =
515 (const struct ieee80211_tdma_param *) ie;
518 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
519 ("not a tdma vap, caps 0x%x", vap->iv_caps));
521 if (len < sizeof(*tdma) - 2) {
522 IEEE80211_DISCARD_IE(vap,
523 IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
524 wh, "tdma", "too short, len %u", len);
525 return IEEE80211_REASON_IE_INVALID;
527 if (tdma->tdma_version != ts->tdma_version) {
528 IEEE80211_DISCARD_IE(vap,
529 IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
530 wh, "tdma", "bad version %u (ours %u)",
531 tdma->tdma_version, ts->tdma_version);
532 return IEEE80211_REASON_IE_INVALID;
535 * NB: ideally we'd check against tdma_slotcnt, but that
536 * would require extra effort so do this easy check that
537 * covers the work below; more stringent checks are done
538 * before we make more extensive use of the ie contents.
540 if (tdma->tdma_slot >= TDMA_MAXSLOTS) {
541 IEEE80211_DISCARD_IE(vap,
542 IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
543 wh, "tdma", "invalid slot %u", tdma->tdma_slot);
544 return IEEE80211_REASON_IE_INVALID;
547 * Can reach here while scanning, update
548 * operational state only in RUN state.
550 if (vap->iv_state == IEEE80211_S_RUN) {
551 if (tdma->tdma_slot != ts->tdma_slot &&
552 isclr(ts->tdma_inuse, tdma->tdma_slot)) {
553 IEEE80211_NOTE(vap, IEEE80211_MSG_TDMA, ni,
554 "discovered in slot %u", tdma->tdma_slot);
555 setbit(ts->tdma_inuse, tdma->tdma_slot);
556 /* XXX dispatch event only when operating as master */
557 if (ts->tdma_slot == 0)
558 ieee80211_notify_node_join(ni, 1);
560 setbit(ts->tdma_active, tdma->tdma_slot);
561 if (tdma->tdma_slot == ts->tdma_slot-1) {
563 * Slave tsf synchronization to station
564 * just before us in the schedule. The driver
565 * is responsible for copying the timestamp
566 * of the received beacon into our beacon
567 * frame so the sender can calculate round
568 * trip time. We cannot do that here because
569 * we don't know how to update our beacon frame.
571 (void) tdma_update(vap, tdma, ni, 0);
572 /* XXX reschedule swbmiss timer on parameter change */
573 } else if (tdma->tdma_slot == ts->tdma_slot+1) {
576 uint32_t rstamp = (uint32_t) le64toh(rs->tsf);
580 * Use returned timstamp to calculate the
583 memcpy(&tstamp, tdma->tdma_tstamp, 8);
585 /* XXX use only 15 bits of rstamp */
586 rtt = rstamp - (le64toh(tstamp) & 0x7fff);
589 /* XXX hack to quiet normal use */
590 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DOT1X,
591 "tdma rtt %5u [rstamp %5u tstamp %llu]\n",
593 (unsigned long long) le64toh(tstamp));
595 } else if (tdma->tdma_slot == ts->tdma_slot &&
596 le64toh(ni->ni_tstamp.tsf) > vap->iv_bss->ni_tstamp.tsf) {
598 * Station using the same slot as us and has
599 * been around longer than us; we must move.
600 * Note this can happen if stations do not
601 * see each other while scanning.
603 IEEE80211_DPRINTF(vap, IEEE80211_MSG_TDMA,
604 "slot %u collision rxtsf %llu tsf %llu\n",
606 (unsigned long long) le64toh(ni->ni_tstamp.tsf),
607 vap->iv_bss->ni_tstamp.tsf);
608 setbit(ts->tdma_inuse, tdma->tdma_slot);
610 (void) tdma_update(vap, tdma, ni, 1);
617 ieee80211_tdma_getslot(struct ieee80211vap *vap)
619 struct ieee80211_tdma_state *ts = vap->iv_tdma;
621 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
622 ("not a tdma vap, caps 0x%x", vap->iv_caps));
623 return ts->tdma_slot;
627 * Parse a TDMA ie on station join and use it to setup node state.
630 ieee80211_parse_tdma(struct ieee80211_node *ni, const uint8_t *ie)
632 struct ieee80211vap *vap = ni->ni_vap;
634 if (vap->iv_caps & IEEE80211_C_TDMA) {
635 const struct ieee80211_tdma_param *tdma =
636 (const struct ieee80211_tdma_param *)ie;
637 struct ieee80211_tdma_state *ts = vap->iv_tdma;
639 * Adopt TDMA configuration when joining an
642 setbit(ts->tdma_inuse, tdma->tdma_slot);
643 (void) tdma_update(vap, tdma, ni, 1);
645 * Propagate capabilities based on the local
646 * configuration and the remote station's advertised
647 * capabilities. In particular this permits us to
648 * enable use of QoS to disable ACK's.
650 if ((vap->iv_flags & IEEE80211_F_WME) &&
651 ni->ni_ies.wme_ie != NULL)
652 ni->ni_flags |= IEEE80211_NODE_QOS;
656 #define TDMA_OUI_BYTES 0x00, 0x03, 0x7f
658 * Add a TDMA parameters element to a frame.
661 ieee80211_add_tdma(uint8_t *frm, struct ieee80211vap *vap)
663 #define ADDSHORT(frm, v) do { \
664 frm[0] = (v) & 0xff; \
668 static const struct ieee80211_tdma_param param = {
669 .tdma_id = IEEE80211_ELEMID_VENDOR,
670 .tdma_len = sizeof(struct ieee80211_tdma_param) - 2,
671 .tdma_oui = { TDMA_OUI_BYTES },
672 .tdma_type = TDMA_OUI_TYPE,
673 .tdma_subtype = TDMA_SUBTYPE_PARAM,
674 .tdma_version = TDMA_VERSION,
676 const struct ieee80211_tdma_state *ts = vap->iv_tdma;
679 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
680 ("not a tdma vap, caps 0x%x", vap->iv_caps));
682 memcpy(frm, ¶m, sizeof(param));
683 frm += __offsetof(struct ieee80211_tdma_param, tdma_slot);
684 *frm++ = ts->tdma_slot;
685 *frm++ = ts->tdma_slotcnt;
686 /* NB: convert units to fit in 16-bits */
687 slotlen = ts->tdma_slotlen / 100; /* 100us units */
688 ADDSHORT(frm, slotlen);
689 *frm++ = ts->tdma_bintval;
690 *frm++ = ts->tdma_inuse[0];
691 frm += 10; /* pad+timestamp */
695 #undef TDMA_OUI_BYTES
698 * Update TDMA state at TBTT.
701 ieee80211_tdma_update_beacon(struct ieee80211vap *vap,
702 struct ieee80211_beacon_offsets *bo)
704 struct ieee80211_tdma_state *ts = vap->iv_tdma;
706 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
707 ("not a tdma vap, caps 0x%x", vap->iv_caps));
709 if (isset(bo->bo_flags, IEEE80211_BEACON_TDMA)) {
710 (void) ieee80211_add_tdma(bo->bo_tdma, vap);
711 clrbit(bo->bo_flags, IEEE80211_BEACON_TDMA);
713 if (ts->tdma_slot != 0) /* only on master */
715 if (ts->tdma_count <= 0) {
717 * Time to update the mask of active/inuse stations.
718 * We track stations that we've received a beacon
719 * frame from and update this mask periodically.
720 * This allows us to miss a few beacons before marking
721 * a slot free for re-use.
723 ts->tdma_inuse[0] = ts->tdma_active[0];
724 ts->tdma_active[0] = 0x01;
725 /* update next time 'round */
726 /* XXX use notify framework */
727 setbit(bo->bo_flags, IEEE80211_BEACON_TDMA);
728 /* NB: use s/w beacon miss threshold; may be too high */
729 ts->tdma_count = vap->iv_bmissthreshold-1;
735 tdma_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
737 struct ieee80211_tdma_state *ts = vap->iv_tdma;
739 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
742 switch (ireq->i_type) {
743 case IEEE80211_IOC_TDMA_SLOT:
744 ireq->i_val = ts->tdma_slot;
746 case IEEE80211_IOC_TDMA_SLOTCNT:
747 ireq->i_val = ts->tdma_slotcnt;
749 case IEEE80211_IOC_TDMA_SLOTLEN:
750 ireq->i_val = ts->tdma_slotlen;
752 case IEEE80211_IOC_TDMA_BINTERVAL:
753 ireq->i_val = ts->tdma_bintval;
760 IEEE80211_IOCTL_GET(tdma, tdma_ioctl_get80211);
763 tdma_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
765 struct ieee80211_tdma_state *ts = vap->iv_tdma;
767 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
770 switch (ireq->i_type) {
771 case IEEE80211_IOC_TDMA_SLOT:
772 if (!(0 <= ireq->i_val && ireq->i_val <= ts->tdma_slotcnt))
774 if (ireq->i_val != ts->tdma_slot) {
775 ts->tdma_slot = ireq->i_val;
779 case IEEE80211_IOC_TDMA_SLOTCNT:
780 if (!TDMA_SLOTCNT_VALID(ireq->i_val))
782 if (ireq->i_val != ts->tdma_slotcnt) {
783 ts->tdma_slotcnt = ireq->i_val;
787 case IEEE80211_IOC_TDMA_SLOTLEN:
790 * 150 insures at least 1/8 TU
791 * 0xfffff is the max duration for bursting
792 * (implict by way of 16-bit data type for i_val)
794 if (!TDMA_SLOTLEN_VALID(ireq->i_val))
796 if (ireq->i_val != ts->tdma_slotlen) {
797 ts->tdma_slotlen = ireq->i_val;
801 case IEEE80211_IOC_TDMA_BINTERVAL:
802 if (!TDMA_BINTVAL_VALID(ireq->i_val))
804 if (ireq->i_val != ts->tdma_bintval) {
805 ts->tdma_bintval = ireq->i_val;
814 ieee80211_beacon_notify(vap, IEEE80211_BEACON_TDMA);
817 IEEE80211_IOCTL_SET(tdma, tdma_ioctl_set80211);