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.
27 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
33 * IEEE 802.11 TDMA mode support.
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/endian.h>
48 #include <sys/errno.h>
50 #include <sys/sysctl.h>
53 #include <net/if_media.h>
54 #include <net/if_llc.h>
55 #include <net/ethernet.h>
59 #include <net80211/ieee80211_var.h>
60 #include <net80211/ieee80211_tdma.h>
61 #include <net80211/ieee80211_input.h>
63 #ifndef TDMA_SLOTLEN_DEFAULT
64 #define TDMA_SLOTLEN_DEFAULT 10*1000 /* 10ms */
66 #ifndef TDMA_SLOTCNT_DEFAULT
67 #define TDMA_SLOTCNT_DEFAULT 2 /* 2x (pt-to-pt) */
69 #ifndef TDMA_BINTVAL_DEFAULT
70 #define TDMA_BINTVAL_DEFAULT 5 /* 5x ~= 100TU beacon intvl */
72 #ifndef TDMA_TXRATE_11B_DEFAULT
73 #define TDMA_TXRATE_11B_DEFAULT 2*11
75 #ifndef TDMA_TXRATE_11G_DEFAULT
76 #define TDMA_TXRATE_11G_DEFAULT 2*24
78 #ifndef TDMA_TXRATE_11A_DEFAULT
79 #define TDMA_TXRATE_11A_DEFAULT 2*24
81 #ifndef TDMA_TXRATE_TURBO_DEFAULT
82 #define TDMA_TXRATE_TURBO_DEFAULT 2*24
84 #ifndef TDMA_TXRATE_HALF_DEFAULT
85 #define TDMA_TXRATE_HALF_DEFAULT 2*12
87 #ifndef TDMA_TXRATE_QUARTER_DEFAULT
88 #define TDMA_TXRATE_QUARTER_DEFAULT 2*6
90 #ifndef TDMA_TXRATE_11NA_DEFAULT
91 #define TDMA_TXRATE_11NA_DEFAULT (4 | IEEE80211_RATE_MCS)
93 #ifndef TDMA_TXRATE_11NG_DEFAULT
94 #define TDMA_TXRATE_11NG_DEFAULT (4 | IEEE80211_RATE_MCS)
97 #define TDMA_VERSION_VALID(_version) \
98 (TDMA_VERSION_V2 <= (_version) && (_version) <= TDMA_VERSION)
99 #define TDMA_SLOTCNT_VALID(_slotcnt) \
100 (2 <= (_slotcnt) && (_slotcnt) <= TDMA_MAXSLOTS)
101 /* XXX magic constants */
102 #define TDMA_SLOTLEN_VALID(_slotlen) \
103 (2*100 <= (_slotlen) && (unsigned)(_slotlen) <= 0xfffff)
104 /* XXX probably should set a max */
105 #define TDMA_BINTVAL_VALID(_bintval) (1 <= (_bintval))
107 static void tdma_vdetach(struct ieee80211vap *vap);
108 static int tdma_newstate(struct ieee80211vap *, enum ieee80211_state, int);
109 static void tdma_beacon_miss(struct ieee80211vap *vap);
110 static void tdma_recv_mgmt(struct ieee80211_node *, struct mbuf *,
111 int subtype, int rssi, int nf);
112 static int tdma_update(struct ieee80211vap *vap,
113 const struct ieee80211_tdma_param *tdma, struct ieee80211_node *ni,
115 static int tdma_process_params(struct ieee80211_node *ni,
116 const u_int8_t *ie, int rssi, int nf, const struct ieee80211_frame *wh);
119 settxparms(struct ieee80211vap *vap, enum ieee80211_phymode mode, int rate)
121 vap->iv_txparms[mode].ucastrate = rate;
122 vap->iv_txparms[mode].mcastrate = rate;
126 setackpolicy(struct ieee80211com *ic, int noack)
128 struct ieee80211_wme_state *wme = &ic->ic_wme;
131 for (ac = 0; ac < WME_NUM_AC; ac++) {
132 wme->wme_chanParams.cap_wmeParams[ac].wmep_noackPolicy = noack;
133 wme->wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy = noack;
138 ieee80211_tdma_vattach(struct ieee80211vap *vap)
140 struct ieee80211_tdma_state *ts;
142 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
143 ("not a tdma vap, caps 0x%x", vap->iv_caps));
145 ts = (struct ieee80211_tdma_state *) malloc(
146 sizeof(struct ieee80211_tdma_state), M_80211_VAP, M_NOWAIT | M_ZERO);
148 printf("%s: cannot allocate TDMA state block\n", __func__);
149 /* NB: fall back to adhdemo mode */
150 vap->iv_caps &= ~IEEE80211_C_TDMA;
153 /* NB: default configuration is passive so no beacons */
154 ts->tdma_version = TDMA_VERSION;
155 ts->tdma_slotlen = TDMA_SLOTLEN_DEFAULT;
156 ts->tdma_slotcnt = TDMA_SLOTCNT_DEFAULT;
157 ts->tdma_bintval = TDMA_BINTVAL_DEFAULT;
158 ts->tdma_slot = 1; /* passive operation */
160 /* setup default fixed rates */
161 settxparms(vap, IEEE80211_MODE_11A, TDMA_TXRATE_11A_DEFAULT);
162 settxparms(vap, IEEE80211_MODE_11B, TDMA_TXRATE_11B_DEFAULT);
163 settxparms(vap, IEEE80211_MODE_11G, TDMA_TXRATE_11G_DEFAULT);
164 settxparms(vap, IEEE80211_MODE_TURBO_A, TDMA_TXRATE_TURBO_DEFAULT);
165 settxparms(vap, IEEE80211_MODE_TURBO_G, TDMA_TXRATE_TURBO_DEFAULT);
166 settxparms(vap, IEEE80211_MODE_STURBO_A, TDMA_TXRATE_TURBO_DEFAULT);
167 settxparms(vap, IEEE80211_MODE_11NA, TDMA_TXRATE_11NA_DEFAULT);
168 settxparms(vap, IEEE80211_MODE_11NG, TDMA_TXRATE_11NG_DEFAULT);
169 settxparms(vap, IEEE80211_MODE_HALF, TDMA_TXRATE_HALF_DEFAULT);
170 settxparms(vap, IEEE80211_MODE_QUARTER, TDMA_TXRATE_QUARTER_DEFAULT);
172 setackpolicy(vap->iv_ic, 1); /* disable ACK's */
174 ts->tdma_opdetach = vap->iv_opdetach;
175 vap->iv_opdetach = tdma_vdetach;
176 ts->tdma_newstate = vap->iv_newstate;
177 vap->iv_newstate = tdma_newstate;
178 vap->iv_bmiss = tdma_beacon_miss;
179 ts->tdma_recv_mgmt = vap->iv_recv_mgmt;
180 vap->iv_recv_mgmt = tdma_recv_mgmt;
186 tdma_vdetach(struct ieee80211vap *vap)
188 struct ieee80211_tdma_state *ts = vap->iv_tdma;
190 ts->tdma_opdetach(vap);
191 free(vap->iv_tdma, M_80211_VAP);
193 setackpolicy(vap->iv_ic, 0); /* enable ACK's */
197 sta_leave(void *arg, struct ieee80211_node *ni)
199 struct ieee80211vap *vap = arg;
201 if (ni->ni_vap == vap && ni != vap->iv_bss)
202 ieee80211_node_leave(ni);
206 * TDMA state machine handler.
209 tdma_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
211 struct ieee80211_tdma_state *ts = vap->iv_tdma;
212 struct ieee80211com *ic = vap->iv_ic;
213 enum ieee80211_state ostate;
216 IEEE80211_LOCK_ASSERT(ic);
218 ostate = vap->iv_state;
219 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
220 __func__, ieee80211_state_name[ostate],
221 ieee80211_state_name[nstate], arg);
223 if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
224 callout_stop(&vap->iv_swbmiss);
225 if (nstate == IEEE80211_S_SCAN &&
226 (ostate == IEEE80211_S_INIT || ostate == IEEE80211_S_RUN) &&
227 ts->tdma_slot != 0) {
229 * Override adhoc behaviour when operating as a slave;
230 * we need to scan even if the channel is locked.
232 vap->iv_state = nstate; /* state transition */
233 ieee80211_cancel_scan(vap); /* background scan */
234 if (ostate == IEEE80211_S_RUN) {
235 /* purge station table; entries are stale */
236 ieee80211_iterate_nodes(&ic->ic_sta, sta_leave, vap);
238 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
239 ieee80211_check_scan(vap,
240 vap->iv_scanreq_flags,
241 vap->iv_scanreq_duration,
242 vap->iv_scanreq_mindwell,
243 vap->iv_scanreq_maxdwell,
244 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
245 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
247 ieee80211_check_scan_current(vap);
250 status = ts->tdma_newstate(vap, nstate, arg);
253 nstate == IEEE80211_S_RUN && ostate != IEEE80211_S_RUN &&
254 (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) &&
255 ts->tdma_slot != 0 &&
256 vap->iv_des_chan == IEEE80211_CHAN_ANYC) {
258 * Start s/w beacon miss timer for slave devices w/o
259 * hardware support. Note we do this only if we're
260 * not locked to a channel (i.e. roam to follow the
261 * master). The 2x is a fudge for our doing this in
264 vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
265 2 * vap->iv_bmissthreshold * ts->tdma_bintval *
266 ((ts->tdma_slotcnt * ts->tdma_slotlen) / 1024));
267 vap->iv_swbmiss_count = 0;
268 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
269 ieee80211_swbmiss, vap);
275 tdma_beacon_miss(struct ieee80211vap *vap)
277 struct ieee80211_tdma_state *ts = vap->iv_tdma;
279 KASSERT((vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
280 KASSERT(vap->iv_state == IEEE80211_S_RUN,
281 ("wrong state %d", vap->iv_state));
283 IEEE80211_DPRINTF(vap,
284 IEEE80211_MSG_STATE | IEEE80211_MSG_TDMA | IEEE80211_MSG_DEBUG,
285 "beacon miss, mode %u state %s\n",
286 vap->iv_opmode, ieee80211_state_name[vap->iv_state]);
288 if (ts->tdma_peer != NULL) { /* XXX? can this be null? */
289 ieee80211_notify_node_leave(vap->iv_bss);
290 ts->tdma_peer = NULL;
292 * Treat beacon miss like an associate failure wrt the
293 * scan policy; this forces the entry in the scan cache
294 * to be ignored after several tries.
296 ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr,
297 IEEE80211_STATUS_TIMEOUT);
300 ts->tdma_inuse = 0; /* clear slot usage */
302 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
306 tdma_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
307 int subtype, int rssi, int nf)
309 struct ieee80211com *ic = ni->ni_ic;
310 struct ieee80211vap *vap = ni->ni_vap;
311 struct ieee80211_tdma_state *ts = vap->iv_tdma;
313 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON &&
314 (ic->ic_flags & IEEE80211_F_SCAN) == 0) {
315 struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
316 struct ieee80211_scanparams scan;
318 if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
320 if (scan.tdma == NULL) {
322 * TDMA stations must beacon a TDMA ie; ignore
324 * XXX detect overlapping bss and change channel
326 IEEE80211_DISCARD(vap,
327 IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
328 wh, ieee80211_mgt_subtype_name[subtype >>
329 IEEE80211_FC0_SUBTYPE_SHIFT],
331 vap->iv_stats.is_rx_mgtdiscard++;
334 if (ni == vap->iv_bss &&
335 !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
337 * Fake up a node for this newly
338 * discovered member of the IBSS.
340 ni = ieee80211_add_neighbor(vap, wh, &scan);
342 /* NB: stat kept for alloc failure */
347 * Check for state updates.
349 if (IEEE80211_ADDR_EQ(wh->i_addr3, ni->ni_bssid)) {
351 * Count frame now that we know it's to be processed.
353 vap->iv_stats.is_rx_beacon++;
354 IEEE80211_NODE_STAT(ni, rx_beacons);
356 * Record tsf of last beacon. NB: this must be
357 * done before calling tdma_process_params
358 * as deeper routines reference it.
360 memcpy(&ni->ni_tstamp.data, scan.tstamp,
361 sizeof(ni->ni_tstamp.data));
363 * Count beacon frame for s/w bmiss handling.
365 vap->iv_swbmiss_count++;
367 * Process tdma ie. The contents are used to sync
368 * the slot timing, reconfigure the bss, etc.
370 (void) tdma_process_params(ni, scan.tdma, rssi, nf, wh);
374 * NB: defer remaining work to the adhoc code; this causes
375 * 2x parsing of the frame but should happen infrequently
378 ts->tdma_recv_mgmt(ni, m0, subtype, rssi, nf);
382 * Update TDMA state on receipt of a beacon frame with
383 * a TDMA information element. The sender's identity
384 * is provided so we can track who our peer is. If pickslot
385 * is non-zero we scan the slot allocation state in the ie
386 * to locate a free slot for our use.
389 tdma_update(struct ieee80211vap *vap, const struct ieee80211_tdma_param *tdma,
390 struct ieee80211_node *ni, int pickslot)
392 struct ieee80211_tdma_state *ts = vap->iv_tdma;
393 int slot, slotlen, update;
395 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
396 ("not a tdma vap, caps 0x%x", vap->iv_caps));
399 if (tdma->tdma_slotcnt != ts->tdma_slotcnt) {
400 if (!TDMA_SLOTCNT_VALID(tdma->tdma_slotcnt)) {
401 if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
402 printf("%s: bad slot cnt %u\n",
403 __func__, tdma->tdma_slotcnt);
406 update |= TDMA_UPDATE_SLOTCNT;
408 slotlen = le16toh(tdma->tdma_slotlen) * 100;
409 if (slotlen != ts->tdma_slotlen) {
410 if (!TDMA_SLOTLEN_VALID(slotlen)) {
411 if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
412 printf("%s: bad slot len %u\n",
416 update |= TDMA_UPDATE_SLOTLEN;
418 if (tdma->tdma_bintval != ts->tdma_bintval) {
419 if (!TDMA_BINTVAL_VALID(tdma->tdma_bintval)) {
420 if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
421 printf("%s: bad beacon interval %u\n",
422 __func__, tdma->tdma_bintval);
425 update |= TDMA_UPDATE_BINTVAL;
427 slot = ts->tdma_slot;
430 * Pick unoccupied slot. Note we never choose slot 0.
432 for (slot = tdma->tdma_slotcnt-1; slot > 0; slot--)
433 if (isclr(tdma->tdma_inuse, slot))
436 printf("%s: no free slot, slotcnt %u inuse: 0x%x\n",
437 __func__, tdma->tdma_slotcnt,
438 tdma->tdma_inuse[0]);
439 /* XXX need to do something better */
442 if (slot != ts->tdma_slot)
443 update |= TDMA_UPDATE_SLOT;
445 if (ni != ts->tdma_peer) {
446 /* update everything */
447 update = TDMA_UPDATE_SLOT
448 | TDMA_UPDATE_SLOTCNT
449 | TDMA_UPDATE_SLOTLEN
450 | TDMA_UPDATE_BINTVAL;
455 * New/changed parameters; update runtime state.
457 /* XXX overwrites user parameters */
458 if (update & TDMA_UPDATE_SLOTCNT)
459 ts->tdma_slotcnt = tdma->tdma_slotcnt;
460 if (update & TDMA_UPDATE_SLOTLEN)
461 ts->tdma_slotlen = slotlen;
462 if (update & TDMA_UPDATE_SLOT)
463 ts->tdma_slot = slot;
464 if (update & TDMA_UPDATE_BINTVAL)
465 ts->tdma_bintval = tdma->tdma_bintval;
466 /* mark beacon to be updated before next xmit */
467 ieee80211_beacon_notify(vap, IEEE80211_BEACON_TDMA);
469 IEEE80211_DPRINTF(vap, IEEE80211_MSG_TDMA,
470 "%s: slot %u slotcnt %u slotlen %u us bintval %u\n",
471 __func__, ts->tdma_slot, ts->tdma_slotcnt,
472 ts->tdma_slotlen, ts->tdma_bintval);
475 * Notify driver. Note we can be called before
476 * entering RUN state if we scanned and are
477 * joining an existing bss. In that case do not
478 * call the driver because not all necessary state
479 * has been setup. The next beacon will dtrt.
481 if (vap->iv_state == IEEE80211_S_RUN)
482 vap->iv_ic->ic_tdma_update(ni, tdma, update);
484 * Dispatch join event on first beacon from new master.
486 if (ts->tdma_peer != ni) {
487 if (ts->tdma_peer != NULL)
488 ieee80211_notify_node_leave(vap->iv_bss);
489 ieee80211_notify_node_join(ni, 1);
490 /* NB: no reference, we just use the address */
497 * Process received TDMA parameters.
500 tdma_process_params(struct ieee80211_node *ni, const u_int8_t *ie,
501 int rssi, int nf, const struct ieee80211_frame *wh)
503 struct ieee80211vap *vap = ni->ni_vap;
504 struct ieee80211_tdma_state *ts = vap->iv_tdma;
505 const struct ieee80211_tdma_param *tdma =
506 (const struct ieee80211_tdma_param *) ie;
509 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
510 ("not a tdma vap, caps 0x%x", vap->iv_caps));
512 if (len < sizeof(*tdma) - 2) {
513 IEEE80211_DISCARD_IE(vap,
514 IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
515 wh, "tdma", "too short, len %u", len);
516 return IEEE80211_REASON_IE_INVALID;
518 if (tdma->tdma_version != ts->tdma_version) {
519 IEEE80211_DISCARD_IE(vap,
520 IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
521 wh, "tdma", "bad version %u (ours %u)",
522 tdma->tdma_version, ts->tdma_version);
523 return IEEE80211_REASON_IE_INVALID;
526 * NB: ideally we'd check against tdma_slotcnt, but that
527 * would require extra effort so do this easy check that
528 * covers the work below; more stringent checks are done
529 * before we make more extensive use of the ie contents.
531 if (tdma->tdma_slot >= TDMA_MAXSLOTS) {
532 IEEE80211_DISCARD_IE(vap,
533 IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
534 wh, "tdma", "invalid slot %u", tdma->tdma_slot);
535 return IEEE80211_REASON_IE_INVALID;
538 * Can reach here while scanning, update
539 * operational state only in RUN state.
541 if (vap->iv_state == IEEE80211_S_RUN) {
542 if (tdma->tdma_slot != ts->tdma_slot &&
543 isclr(ts->tdma_inuse, tdma->tdma_slot)) {
544 IEEE80211_NOTE(vap, IEEE80211_MSG_TDMA, ni,
545 "discovered in slot %u", tdma->tdma_slot);
546 setbit(ts->tdma_inuse, tdma->tdma_slot);
547 /* XXX dispatch event only when operating as master */
548 if (ts->tdma_slot == 0)
549 ieee80211_notify_node_join(ni, 1);
551 setbit(ts->tdma_active, tdma->tdma_slot);
552 if (tdma->tdma_slot == ts->tdma_slot-1) {
554 * Slave tsf synchronization to station
555 * just before us in the schedule. The driver
556 * is responsible for copying the timestamp
557 * of the received beacon into our beacon
558 * frame so the sender can calculate round
559 * trip time. We cannot do that here because
560 * we don't know how to update our beacon frame.
562 (void) tdma_update(vap, tdma, ni, 0);
563 /* XXX reschedule swbmiss timer on parameter change */
564 } else if (tdma->tdma_slot == ts->tdma_slot+1) {
567 uint32_t rstamp = (uint32_t) le64toh(rs->tsf);
571 * Use returned timstamp to calculate the
574 memcpy(&tstamp, tdma->tdma_tstamp, 8);
576 /* XXX use only 15 bits of rstamp */
577 rtt = rstamp - (le64toh(tstamp) & 0x7fff);
580 /* XXX hack to quiet normal use */
581 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DOT1X,
582 "tdma rtt %5u [rstamp %5u tstamp %llu]\n",
584 (unsigned long long) le64toh(tstamp));
586 } else if (tdma->tdma_slot == ts->tdma_slot &&
587 le64toh(ni->ni_tstamp.tsf) > vap->iv_bss->ni_tstamp.tsf) {
589 * Station using the same slot as us and has
590 * been around longer than us; we must move.
591 * Note this can happen if stations do not
592 * see each other while scanning.
594 IEEE80211_DPRINTF(vap, IEEE80211_MSG_TDMA,
595 "slot %u collision rxtsf %llu tsf %llu\n",
597 (unsigned long long) le64toh(ni->ni_tstamp.tsf),
598 vap->iv_bss->ni_tstamp.tsf);
599 setbit(ts->tdma_inuse, tdma->tdma_slot);
601 (void) tdma_update(vap, tdma, ni, 1);
608 ieee80211_tdma_getslot(struct ieee80211vap *vap)
610 struct ieee80211_tdma_state *ts = vap->iv_tdma;
612 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
613 ("not a tdma vap, caps 0x%x", vap->iv_caps));
614 return ts->tdma_slot;
618 * Parse a TDMA ie on station join and use it to setup node state.
621 ieee80211_parse_tdma(struct ieee80211_node *ni, const uint8_t *ie)
623 struct ieee80211vap *vap = ni->ni_vap;
625 if (vap->iv_caps & IEEE80211_C_TDMA) {
626 const struct ieee80211_tdma_param *tdma =
627 (const struct ieee80211_tdma_param *)ie;
628 struct ieee80211_tdma_state *ts = vap->iv_tdma;
630 * Adopt TDMA configuration when joining an
633 setbit(ts->tdma_inuse, tdma->tdma_slot);
634 (void) tdma_update(vap, tdma, ni, 1);
636 * Propagate capabilities based on the local
637 * configuration and the remote station's advertised
638 * capabilities. In particular this permits us to
639 * enable use of QoS to disable ACK's.
641 if ((vap->iv_flags & IEEE80211_F_WME) &&
642 ni->ni_ies.wme_ie != NULL)
643 ni->ni_flags |= IEEE80211_NODE_QOS;
647 #define TDMA_OUI_BYTES 0x00, 0x03, 0x7f
649 * Add a TDMA parameters element to a frame.
652 ieee80211_add_tdma(uint8_t *frm, struct ieee80211vap *vap)
654 #define ADDSHORT(frm, v) do { \
655 frm[0] = (v) & 0xff; \
659 static const struct ieee80211_tdma_param param = {
660 .tdma_id = IEEE80211_ELEMID_VENDOR,
661 .tdma_len = sizeof(struct ieee80211_tdma_param) - 2,
662 .tdma_oui = { TDMA_OUI_BYTES },
663 .tdma_type = TDMA_OUI_TYPE,
664 .tdma_subtype = TDMA_SUBTYPE_PARAM,
665 .tdma_version = TDMA_VERSION,
667 const struct ieee80211_tdma_state *ts = vap->iv_tdma;
670 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
671 ("not a tdma vap, caps 0x%x", vap->iv_caps));
673 memcpy(frm, ¶m, sizeof(param));
674 frm += __offsetof(struct ieee80211_tdma_param, tdma_slot);
675 *frm++ = ts->tdma_slot;
676 *frm++ = ts->tdma_slotcnt;
677 /* NB: convert units to fit in 16-bits */
678 slotlen = ts->tdma_slotlen / 100; /* 100us units */
679 ADDSHORT(frm, slotlen);
680 *frm++ = ts->tdma_bintval;
681 *frm++ = ts->tdma_inuse[0];
682 frm += 10; /* pad+timestamp */
686 #undef TDMA_OUI_BYTES
689 * Update TDMA state at TBTT.
692 ieee80211_tdma_update_beacon(struct ieee80211vap *vap,
693 struct ieee80211_beacon_offsets *bo)
695 struct ieee80211_tdma_state *ts = vap->iv_tdma;
697 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
698 ("not a tdma vap, caps 0x%x", vap->iv_caps));
700 if (isset(bo->bo_flags, IEEE80211_BEACON_TDMA)) {
701 (void) ieee80211_add_tdma(bo->bo_tdma, vap);
702 clrbit(bo->bo_flags, IEEE80211_BEACON_TDMA);
704 if (ts->tdma_slot != 0) /* only on master */
706 if (ts->tdma_count <= 0) {
708 * Time to update the mask of active/inuse stations.
709 * We track stations that we've received a beacon
710 * frame from and update this mask periodically.
711 * This allows us to miss a few beacons before marking
712 * a slot free for re-use.
714 ts->tdma_inuse[0] = ts->tdma_active[0];
715 ts->tdma_active[0] = 0x01;
716 /* update next time 'round */
717 /* XXX use notify framework */
718 setbit(bo->bo_flags, IEEE80211_BEACON_TDMA);
719 /* NB: use s/w beacon miss threshold; may be too high */
720 ts->tdma_count = vap->iv_bmissthreshold-1;
726 tdma_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
728 struct ieee80211_tdma_state *ts = vap->iv_tdma;
730 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
733 switch (ireq->i_type) {
734 case IEEE80211_IOC_TDMA_SLOT:
735 ireq->i_val = ts->tdma_slot;
737 case IEEE80211_IOC_TDMA_SLOTCNT:
738 ireq->i_val = ts->tdma_slotcnt;
740 case IEEE80211_IOC_TDMA_SLOTLEN:
741 ireq->i_val = ts->tdma_slotlen;
743 case IEEE80211_IOC_TDMA_BINTERVAL:
744 ireq->i_val = ts->tdma_bintval;
751 IEEE80211_IOCTL_GET(tdma, tdma_ioctl_get80211);
754 tdma_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
756 struct ieee80211_tdma_state *ts = vap->iv_tdma;
758 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
761 switch (ireq->i_type) {
762 case IEEE80211_IOC_TDMA_SLOT:
763 if (!(0 <= ireq->i_val && ireq->i_val <= ts->tdma_slotcnt))
765 if (ireq->i_val != ts->tdma_slot) {
766 ts->tdma_slot = ireq->i_val;
770 case IEEE80211_IOC_TDMA_SLOTCNT:
771 if (!TDMA_SLOTCNT_VALID(ireq->i_val))
773 if (ireq->i_val != ts->tdma_slotcnt) {
774 ts->tdma_slotcnt = ireq->i_val;
778 case IEEE80211_IOC_TDMA_SLOTLEN:
781 * 150 insures at least 1/8 TU
782 * 0xfffff is the max duration for bursting
783 * (implict by way of 16-bit data type for i_val)
785 if (!TDMA_SLOTLEN_VALID(ireq->i_val))
787 if (ireq->i_val != ts->tdma_slotlen) {
788 ts->tdma_slotlen = ireq->i_val;
792 case IEEE80211_IOC_TDMA_BINTERVAL:
793 if (!TDMA_BINTVAL_VALID(ireq->i_val))
795 if (ireq->i_val != ts->tdma_bintval) {
796 ts->tdma_bintval = ireq->i_val;
805 IEEE80211_IOCTL_SET(tdma, tdma_ioctl_set80211);