2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting
5 * Copyright (c) 2007-2009 Intel Corporation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 * IEEE 802.11 TDMA mode support.
41 #ifdef IEEE80211_SUPPORT_TDMA
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/malloc.h>
47 #include <sys/kernel.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/endian.h>
52 #include <sys/errno.h>
54 #include <sys/sysctl.h>
57 #include <net/if_media.h>
58 #include <net/if_llc.h>
59 #include <net/ethernet.h>
63 #include <net80211/ieee80211_var.h>
64 #include <net80211/ieee80211_tdma.h>
65 #include <net80211/ieee80211_input.h>
67 #ifndef TDMA_SLOTLEN_DEFAULT
68 #define TDMA_SLOTLEN_DEFAULT 10*1000 /* 10ms */
70 #ifndef TDMA_SLOTCNT_DEFAULT
71 #define TDMA_SLOTCNT_DEFAULT 2 /* 2x (pt-to-pt) */
73 #ifndef TDMA_BINTVAL_DEFAULT
74 #define TDMA_BINTVAL_DEFAULT 5 /* 5x ~= 100TU beacon intvl */
76 #ifndef TDMA_TXRATE_11B_DEFAULT
77 #define TDMA_TXRATE_11B_DEFAULT 2*11
79 #ifndef TDMA_TXRATE_11G_DEFAULT
80 #define TDMA_TXRATE_11G_DEFAULT 2*24
82 #ifndef TDMA_TXRATE_11A_DEFAULT
83 #define TDMA_TXRATE_11A_DEFAULT 2*24
85 #ifndef TDMA_TXRATE_TURBO_DEFAULT
86 #define TDMA_TXRATE_TURBO_DEFAULT 2*24
88 #ifndef TDMA_TXRATE_HALF_DEFAULT
89 #define TDMA_TXRATE_HALF_DEFAULT 2*12
91 #ifndef TDMA_TXRATE_QUARTER_DEFAULT
92 #define TDMA_TXRATE_QUARTER_DEFAULT 2*6
94 #ifndef TDMA_TXRATE_11NA_DEFAULT
95 #define TDMA_TXRATE_11NA_DEFAULT (4 | IEEE80211_RATE_MCS)
97 #ifndef TDMA_TXRATE_11NG_DEFAULT
98 #define TDMA_TXRATE_11NG_DEFAULT (4 | IEEE80211_RATE_MCS)
101 #define TDMA_VERSION_VALID(_version) \
102 (TDMA_VERSION_V2 <= (_version) && (_version) <= TDMA_VERSION)
103 #define TDMA_SLOTCNT_VALID(_slotcnt) \
104 (2 <= (_slotcnt) && (_slotcnt) <= TDMA_MAXSLOTS)
105 /* XXX magic constants */
106 #define TDMA_SLOTLEN_VALID(_slotlen) \
107 (2*100 <= (_slotlen) && (unsigned)(_slotlen) <= 0xfffff)
108 /* XXX probably should set a max */
109 #define TDMA_BINTVAL_VALID(_bintval) (1 <= (_bintval))
112 * This code is not prepared to handle more than 2 slots.
114 CTASSERT(TDMA_MAXSLOTS == 2);
116 static void tdma_vdetach(struct ieee80211vap *vap);
117 static int tdma_newstate(struct ieee80211vap *, enum ieee80211_state, int);
118 static void tdma_beacon_miss(struct ieee80211vap *vap);
119 static void tdma_recv_mgmt(struct ieee80211_node *, struct mbuf *,
120 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf);
121 static int tdma_update(struct ieee80211vap *vap,
122 const struct ieee80211_tdma_param *tdma, struct ieee80211_node *ni,
124 static int tdma_process_params(struct ieee80211_node *ni,
125 const u_int8_t *ie, int rssi, int nf, const struct ieee80211_frame *wh);
128 settxparms(struct ieee80211vap *vap, enum ieee80211_phymode mode, int rate)
130 vap->iv_txparms[mode].ucastrate = rate;
131 vap->iv_txparms[mode].mcastrate = rate;
135 setackpolicy(struct ieee80211com *ic, int noack)
137 struct ieee80211_wme_state *wme = &ic->ic_wme;
140 for (ac = 0; ac < WME_NUM_AC; ac++) {
141 wme->wme_chanParams.cap_wmeParams[ac].wmep_noackPolicy = noack;
142 wme->wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy = noack;
147 ieee80211_tdma_vattach(struct ieee80211vap *vap)
149 struct ieee80211_tdma_state *ts;
151 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
152 ("not a tdma vap, caps 0x%x", vap->iv_caps));
154 ts = (struct ieee80211_tdma_state *) IEEE80211_MALLOC(
155 sizeof(struct ieee80211_tdma_state), M_80211_VAP,
156 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
158 printf("%s: cannot allocate TDMA state block\n", __func__);
159 /* NB: fall back to adhdemo mode */
160 vap->iv_caps &= ~IEEE80211_C_TDMA;
163 /* NB: default configuration is passive so no beacons */
164 ts->tdma_version = TDMA_VERSION;
165 ts->tdma_slotlen = TDMA_SLOTLEN_DEFAULT;
166 ts->tdma_slotcnt = TDMA_SLOTCNT_DEFAULT;
167 ts->tdma_bintval = TDMA_BINTVAL_DEFAULT;
168 ts->tdma_slot = 1; /* passive operation */
170 /* setup default fixed rates */
171 settxparms(vap, IEEE80211_MODE_11A, TDMA_TXRATE_11A_DEFAULT);
172 settxparms(vap, IEEE80211_MODE_11B, TDMA_TXRATE_11B_DEFAULT);
173 settxparms(vap, IEEE80211_MODE_11G, TDMA_TXRATE_11G_DEFAULT);
174 settxparms(vap, IEEE80211_MODE_TURBO_A, TDMA_TXRATE_TURBO_DEFAULT);
175 settxparms(vap, IEEE80211_MODE_TURBO_G, TDMA_TXRATE_TURBO_DEFAULT);
176 settxparms(vap, IEEE80211_MODE_STURBO_A, TDMA_TXRATE_TURBO_DEFAULT);
177 settxparms(vap, IEEE80211_MODE_11NA, TDMA_TXRATE_11NA_DEFAULT);
178 settxparms(vap, IEEE80211_MODE_11NG, TDMA_TXRATE_11NG_DEFAULT);
179 settxparms(vap, IEEE80211_MODE_HALF, TDMA_TXRATE_HALF_DEFAULT);
180 settxparms(vap, IEEE80211_MODE_QUARTER, TDMA_TXRATE_QUARTER_DEFAULT);
181 settxparms(vap, IEEE80211_MODE_VHT_2GHZ, TDMA_TXRATE_11NG_DEFAULT);
182 settxparms(vap, IEEE80211_MODE_VHT_5GHZ, TDMA_TXRATE_11NA_DEFAULT);
184 setackpolicy(vap->iv_ic, 1); /* disable ACK's */
186 ts->tdma_opdetach = vap->iv_opdetach;
187 vap->iv_opdetach = tdma_vdetach;
188 ts->tdma_newstate = vap->iv_newstate;
189 vap->iv_newstate = tdma_newstate;
190 vap->iv_bmiss = tdma_beacon_miss;
191 ts->tdma_recv_mgmt = vap->iv_recv_mgmt;
192 vap->iv_recv_mgmt = tdma_recv_mgmt;
198 tdma_vdetach(struct ieee80211vap *vap)
200 struct ieee80211_tdma_state *ts = vap->iv_tdma;
203 /* NB: should not have touched any ic state */
206 ts->tdma_opdetach(vap);
207 IEEE80211_FREE(vap->iv_tdma, M_80211_VAP);
210 setackpolicy(vap->iv_ic, 0); /* enable ACK's */
214 sta_leave(void *arg, struct ieee80211_node *ni)
216 struct ieee80211vap *vap = ni->ni_vap;
218 if (ni != vap->iv_bss)
219 ieee80211_node_leave(ni);
223 * TDMA state machine handler.
226 tdma_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
228 struct ieee80211_tdma_state *ts = vap->iv_tdma;
229 struct ieee80211com *ic = vap->iv_ic;
230 enum ieee80211_state ostate;
233 IEEE80211_LOCK_ASSERT(ic);
235 ostate = vap->iv_state;
236 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
237 __func__, ieee80211_state_name[ostate],
238 ieee80211_state_name[nstate], arg);
240 if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
241 callout_stop(&vap->iv_swbmiss);
242 if (nstate == IEEE80211_S_SCAN &&
243 (ostate == IEEE80211_S_INIT || ostate == IEEE80211_S_RUN) &&
244 ts->tdma_slot != 0) {
246 * Override adhoc behaviour when operating as a slave;
247 * we need to scan even if the channel is locked.
249 vap->iv_state = nstate; /* state transition */
250 ieee80211_cancel_scan(vap); /* background scan */
251 if (ostate == IEEE80211_S_RUN) {
252 /* purge station table; entries are stale */
253 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
256 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
257 ieee80211_check_scan(vap,
258 vap->iv_scanreq_flags,
259 vap->iv_scanreq_duration,
260 vap->iv_scanreq_mindwell,
261 vap->iv_scanreq_maxdwell,
262 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
263 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
265 ieee80211_check_scan_current(vap);
268 status = ts->tdma_newstate(vap, nstate, arg);
271 nstate == IEEE80211_S_RUN && ostate != IEEE80211_S_RUN &&
272 (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) &&
273 ts->tdma_slot != 0 &&
274 vap->iv_des_chan == IEEE80211_CHAN_ANYC) {
276 * Start s/w beacon miss timer for slave devices w/o
277 * hardware support. Note we do this only if we're
278 * not locked to a channel (i.e. roam to follow the
279 * master). The 2x is a fudge for our doing this in
282 vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
283 2 * vap->iv_bmissthreshold * ts->tdma_bintval *
284 ((ts->tdma_slotcnt * ts->tdma_slotlen) / 1024));
285 vap->iv_swbmiss_count = 0;
286 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
287 ieee80211_swbmiss, vap);
293 tdma_beacon_miss(struct ieee80211vap *vap)
295 struct ieee80211_tdma_state *ts = vap->iv_tdma;
297 IEEE80211_LOCK_ASSERT(vap->iv_ic);
299 KASSERT((vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
300 KASSERT(vap->iv_state == IEEE80211_S_RUN,
301 ("wrong state %d", vap->iv_state));
303 IEEE80211_DPRINTF(vap,
304 IEEE80211_MSG_STATE | IEEE80211_MSG_TDMA | IEEE80211_MSG_DEBUG,
305 "beacon miss, mode %u state %s\n",
306 vap->iv_opmode, ieee80211_state_name[vap->iv_state]);
308 callout_stop(&vap->iv_swbmiss);
310 if (ts->tdma_peer != NULL) { /* XXX? can this be null? */
311 ieee80211_notify_node_leave(vap->iv_bss);
312 ts->tdma_peer = NULL;
314 * Treat beacon miss like an associate failure wrt the
315 * scan policy; this forces the entry in the scan cache
316 * to be ignored after several tries.
318 ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr,
319 IEEE80211_STATUS_TIMEOUT);
322 ts->tdma_inuse = 0; /* clear slot usage */
324 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
328 tdma_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
329 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
331 struct ieee80211com *ic = ni->ni_ic;
332 struct ieee80211vap *vap = ni->ni_vap;
333 struct ieee80211_tdma_state *ts = vap->iv_tdma;
335 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON &&
336 (ic->ic_flags & IEEE80211_F_SCAN) == 0) {
337 struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
338 struct ieee80211_scanparams scan;
340 /* XXX TODO: use rxstatus to determine off-channel beacons */
341 if (ieee80211_parse_beacon(ni, m0, ic->ic_curchan, &scan) != 0)
343 if (scan.tdma == NULL) {
345 * TDMA stations must beacon a TDMA ie; ignore
347 * XXX detect overlapping bss and change channel
349 IEEE80211_DISCARD(vap,
350 IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
351 wh, ieee80211_mgt_subtype_name(subtype),
353 vap->iv_stats.is_rx_mgtdiscard++;
356 if (ni == vap->iv_bss &&
357 !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
359 * Fake up a node for this newly
360 * discovered member of the IBSS.
362 ni = ieee80211_add_neighbor(vap, wh, &scan);
364 /* NB: stat kept for alloc failure */
369 * Check for state updates.
371 if (IEEE80211_ADDR_EQ(wh->i_addr3, ni->ni_bssid)) {
373 * Count frame now that we know it's to be processed.
375 vap->iv_stats.is_rx_beacon++;
376 IEEE80211_NODE_STAT(ni, rx_beacons);
378 * Record tsf of last beacon. NB: this must be
379 * done before calling tdma_process_params
380 * as deeper routines reference it.
382 memcpy(&ni->ni_tstamp.data, scan.tstamp,
383 sizeof(ni->ni_tstamp.data));
385 * Count beacon frame for s/w bmiss handling.
387 vap->iv_swbmiss_count++;
389 * Process tdma ie. The contents are used to sync
390 * the slot timing, reconfigure the bss, etc.
392 (void) tdma_process_params(ni, scan.tdma, rssi, nf, wh);
396 * NB: defer remaining work to the adhoc code; this causes
397 * 2x parsing of the frame but should happen infrequently
400 ts->tdma_recv_mgmt(ni, m0, subtype, rxs, rssi, nf);
404 * Update TDMA state on receipt of a beacon frame with
405 * a TDMA information element. The sender's identity
406 * is provided so we can track who our peer is. If pickslot
407 * is non-zero we scan the slot allocation state in the ie
408 * to locate a free slot for our use.
411 tdma_update(struct ieee80211vap *vap, const struct ieee80211_tdma_param *tdma,
412 struct ieee80211_node *ni, int pickslot)
414 struct ieee80211_tdma_state *ts = vap->iv_tdma;
415 int slot, slotlen, update;
417 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
418 ("not a tdma vap, caps 0x%x", vap->iv_caps));
421 if (tdma->tdma_slotcnt != ts->tdma_slotcnt) {
422 if (!TDMA_SLOTCNT_VALID(tdma->tdma_slotcnt)) {
423 if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
424 printf("%s: bad slot cnt %u\n",
425 __func__, tdma->tdma_slotcnt);
428 update |= TDMA_UPDATE_SLOTCNT;
430 slotlen = le16toh(tdma->tdma_slotlen) * 100;
431 if (slotlen != ts->tdma_slotlen) {
432 if (!TDMA_SLOTLEN_VALID(slotlen)) {
433 if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
434 printf("%s: bad slot len %u\n",
438 update |= TDMA_UPDATE_SLOTLEN;
440 if (tdma->tdma_bintval != ts->tdma_bintval) {
441 if (!TDMA_BINTVAL_VALID(tdma->tdma_bintval)) {
442 if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
443 printf("%s: bad beacon interval %u\n",
444 __func__, tdma->tdma_bintval);
447 update |= TDMA_UPDATE_BINTVAL;
449 slot = ts->tdma_slot;
452 * Pick unoccupied slot. Note we never choose slot 0.
454 for (slot = tdma->tdma_slotcnt-1; slot > 0; slot--)
455 if (isclr(tdma->tdma_inuse, slot))
458 printf("%s: no free slot, slotcnt %u inuse: 0x%x\n",
459 __func__, tdma->tdma_slotcnt,
460 tdma->tdma_inuse[0]);
461 /* XXX need to do something better */
464 if (slot != ts->tdma_slot)
465 update |= TDMA_UPDATE_SLOT;
467 if (ni != ts->tdma_peer) {
468 /* update everything */
469 update = TDMA_UPDATE_SLOT
470 | TDMA_UPDATE_SLOTCNT
471 | TDMA_UPDATE_SLOTLEN
472 | TDMA_UPDATE_BINTVAL;
477 * New/changed parameters; update runtime state.
479 /* XXX overwrites user parameters */
480 if (update & TDMA_UPDATE_SLOTCNT)
481 ts->tdma_slotcnt = tdma->tdma_slotcnt;
482 if (update & TDMA_UPDATE_SLOTLEN)
483 ts->tdma_slotlen = slotlen;
484 if (update & TDMA_UPDATE_SLOT)
485 ts->tdma_slot = slot;
486 if (update & TDMA_UPDATE_BINTVAL)
487 ts->tdma_bintval = tdma->tdma_bintval;
488 /* mark beacon to be updated before next xmit */
489 ieee80211_beacon_notify(vap, IEEE80211_BEACON_TDMA);
491 IEEE80211_DPRINTF(vap, IEEE80211_MSG_TDMA,
492 "%s: slot %u slotcnt %u slotlen %u us bintval %u\n",
493 __func__, ts->tdma_slot, ts->tdma_slotcnt,
494 ts->tdma_slotlen, ts->tdma_bintval);
497 * Notify driver. Note we can be called before
498 * entering RUN state if we scanned and are
499 * joining an existing bss. In that case do not
500 * call the driver because not all necessary state
501 * has been setup. The next beacon will dtrt.
503 if (vap->iv_state == IEEE80211_S_RUN)
504 vap->iv_ic->ic_tdma_update(ni, tdma, update);
506 * Dispatch join event on first beacon from new master.
508 if (ts->tdma_peer != ni) {
509 if (ts->tdma_peer != NULL)
510 ieee80211_notify_node_leave(vap->iv_bss);
511 ieee80211_notify_node_join(ni, 1);
512 /* NB: no reference, we just use the address */
519 * Process received TDMA parameters.
522 tdma_process_params(struct ieee80211_node *ni, const u_int8_t *ie,
523 int rssi, int nf, const struct ieee80211_frame *wh)
525 struct ieee80211vap *vap = ni->ni_vap;
526 struct ieee80211_tdma_state *ts = vap->iv_tdma;
527 const struct ieee80211_tdma_param *tdma =
528 (const struct ieee80211_tdma_param *) ie;
531 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
532 ("not a tdma vap, caps 0x%x", vap->iv_caps));
534 if (len < sizeof(*tdma) - 2) {
535 IEEE80211_DISCARD_IE(vap,
536 IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
537 wh, "tdma", "too short, len %u", len);
538 return IEEE80211_REASON_IE_INVALID;
540 if (tdma->tdma_version != ts->tdma_version) {
541 IEEE80211_DISCARD_IE(vap,
542 IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
543 wh, "tdma", "bad version %u (ours %u)",
544 tdma->tdma_version, ts->tdma_version);
545 return IEEE80211_REASON_IE_INVALID;
548 * NB: ideally we'd check against tdma_slotcnt, but that
549 * would require extra effort so do this easy check that
550 * covers the work below; more stringent checks are done
551 * before we make more extensive use of the ie contents.
553 if (tdma->tdma_slot >= TDMA_MAXSLOTS) {
554 IEEE80211_DISCARD_IE(vap,
555 IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
556 wh, "tdma", "invalid slot %u", tdma->tdma_slot);
557 return IEEE80211_REASON_IE_INVALID;
560 * Can reach here while scanning, update
561 * operational state only in RUN state.
563 if (vap->iv_state == IEEE80211_S_RUN) {
564 if (tdma->tdma_slot != ts->tdma_slot &&
565 isclr(ts->tdma_inuse, tdma->tdma_slot)) {
566 IEEE80211_NOTE(vap, IEEE80211_MSG_TDMA, ni,
567 "discovered in slot %u", tdma->tdma_slot);
568 setbit(ts->tdma_inuse, tdma->tdma_slot);
569 /* XXX dispatch event only when operating as master */
570 if (ts->tdma_slot == 0)
571 ieee80211_notify_node_join(ni, 1);
573 setbit(ts->tdma_active, tdma->tdma_slot);
574 if (tdma->tdma_slot == ts->tdma_slot-1) {
576 * Slave tsf synchronization to station
577 * just before us in the schedule. The driver
578 * is responsible for copying the timestamp
579 * of the received beacon into our beacon
580 * frame so the sender can calculate round
581 * trip time. We cannot do that here because
582 * we don't know how to update our beacon frame.
584 (void) tdma_update(vap, tdma, ni, 0);
585 /* XXX reschedule swbmiss timer on parameter change */
586 } else if (tdma->tdma_slot == ts->tdma_slot+1) {
589 uint32_t rstamp = (uint32_t) le64toh(rs->tsf);
593 * Use returned timstamp to calculate the
596 memcpy(&tstamp, tdma->tdma_tstamp, 8);
598 /* XXX use only 15 bits of rstamp */
599 rtt = rstamp - (le64toh(tstamp) & 0x7fff);
602 /* XXX hack to quiet normal use */
603 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DOT1X,
604 "tdma rtt %5u [rstamp %5u tstamp %llu]\n",
606 (unsigned long long) le64toh(tstamp));
608 } else if (tdma->tdma_slot == ts->tdma_slot &&
609 le64toh(ni->ni_tstamp.tsf) > vap->iv_bss->ni_tstamp.tsf) {
611 * Station using the same slot as us and has
612 * been around longer than us; we must move.
613 * Note this can happen if stations do not
614 * see each other while scanning.
616 IEEE80211_DPRINTF(vap, IEEE80211_MSG_TDMA,
617 "slot %u collision rxtsf %llu tsf %llu\n",
619 (unsigned long long) le64toh(ni->ni_tstamp.tsf),
620 vap->iv_bss->ni_tstamp.tsf);
621 setbit(ts->tdma_inuse, tdma->tdma_slot);
623 (void) tdma_update(vap, tdma, ni, 1);
630 ieee80211_tdma_getslot(struct ieee80211vap *vap)
632 struct ieee80211_tdma_state *ts = vap->iv_tdma;
634 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
635 ("not a tdma vap, caps 0x%x", vap->iv_caps));
636 return ts->tdma_slot;
640 * Parse a TDMA ie on station join and use it to setup node state.
643 ieee80211_parse_tdma(struct ieee80211_node *ni, const uint8_t *ie)
645 struct ieee80211vap *vap = ni->ni_vap;
647 if (vap->iv_caps & IEEE80211_C_TDMA) {
648 const struct ieee80211_tdma_param *tdma =
649 (const struct ieee80211_tdma_param *)ie;
650 struct ieee80211_tdma_state *ts = vap->iv_tdma;
652 * Adopt TDMA configuration when joining an
655 setbit(ts->tdma_inuse, tdma->tdma_slot);
656 (void) tdma_update(vap, tdma, ni, 1);
658 * Propagate capabilities based on the local
659 * configuration and the remote station's advertised
660 * capabilities. In particular this permits us to
661 * enable use of QoS to disable ACK's.
663 if ((vap->iv_flags & IEEE80211_F_WME) &&
664 ni->ni_ies.wme_ie != NULL)
665 ni->ni_flags |= IEEE80211_NODE_QOS;
669 #define TDMA_OUI_BYTES 0x00, 0x03, 0x7f
671 * Add a TDMA parameters element to a frame.
674 ieee80211_add_tdma(uint8_t *frm, struct ieee80211vap *vap)
676 #define ADDSHORT(frm, v) do { \
677 frm[0] = (v) & 0xff; \
681 static const struct ieee80211_tdma_param param = {
682 .tdma_id = IEEE80211_ELEMID_VENDOR,
683 .tdma_len = sizeof(struct ieee80211_tdma_param) - 2,
684 .tdma_oui = { TDMA_OUI_BYTES },
685 .tdma_type = TDMA_OUI_TYPE,
686 .tdma_subtype = TDMA_SUBTYPE_PARAM,
687 .tdma_version = TDMA_VERSION,
689 const struct ieee80211_tdma_state *ts = vap->iv_tdma;
692 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
693 ("not a tdma vap, caps 0x%x", vap->iv_caps));
695 memcpy(frm, ¶m, sizeof(param));
696 frm += __offsetof(struct ieee80211_tdma_param, tdma_slot);
697 *frm++ = ts->tdma_slot;
698 *frm++ = ts->tdma_slotcnt;
699 /* NB: convert units to fit in 16-bits */
700 slotlen = ts->tdma_slotlen / 100; /* 100us units */
701 ADDSHORT(frm, slotlen);
702 *frm++ = ts->tdma_bintval;
703 *frm++ = ts->tdma_inuse[0];
704 frm += 10; /* pad+timestamp */
708 #undef TDMA_OUI_BYTES
711 * Update TDMA state at TBTT.
714 ieee80211_tdma_update_beacon(struct ieee80211vap *vap,
715 struct ieee80211_beacon_offsets *bo)
717 struct ieee80211_tdma_state *ts = vap->iv_tdma;
719 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
720 ("not a tdma vap, caps 0x%x", vap->iv_caps));
722 if (isset(bo->bo_flags, IEEE80211_BEACON_TDMA)) {
723 (void) ieee80211_add_tdma(bo->bo_tdma, vap);
724 clrbit(bo->bo_flags, IEEE80211_BEACON_TDMA);
726 if (ts->tdma_slot != 0) /* only on master */
728 if (ts->tdma_count <= 0) {
730 * Time to update the mask of active/inuse stations.
731 * We track stations that we've received a beacon
732 * frame from and update this mask periodically.
733 * This allows us to miss a few beacons before marking
734 * a slot free for re-use.
736 ts->tdma_inuse[0] = ts->tdma_active[0];
737 ts->tdma_active[0] = 0x01;
738 /* update next time 'round */
739 /* XXX use notify framework */
740 setbit(bo->bo_flags, IEEE80211_BEACON_TDMA);
741 /* NB: use s/w beacon miss threshold; may be too high */
742 ts->tdma_count = vap->iv_bmissthreshold-1;
748 tdma_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
750 struct ieee80211_tdma_state *ts = vap->iv_tdma;
752 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
755 switch (ireq->i_type) {
756 case IEEE80211_IOC_TDMA_SLOT:
757 ireq->i_val = ts->tdma_slot;
759 case IEEE80211_IOC_TDMA_SLOTCNT:
760 ireq->i_val = ts->tdma_slotcnt;
762 case IEEE80211_IOC_TDMA_SLOTLEN:
763 ireq->i_val = ts->tdma_slotlen;
765 case IEEE80211_IOC_TDMA_BINTERVAL:
766 ireq->i_val = ts->tdma_bintval;
773 IEEE80211_IOCTL_GET(tdma, tdma_ioctl_get80211);
776 tdma_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
778 struct ieee80211_tdma_state *ts = vap->iv_tdma;
780 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
783 switch (ireq->i_type) {
784 case IEEE80211_IOC_TDMA_SLOT:
785 if (!(0 <= ireq->i_val && ireq->i_val <= ts->tdma_slotcnt))
787 if (ireq->i_val != ts->tdma_slot) {
788 ts->tdma_slot = ireq->i_val;
792 case IEEE80211_IOC_TDMA_SLOTCNT:
793 if (!TDMA_SLOTCNT_VALID(ireq->i_val))
795 if (ireq->i_val != ts->tdma_slotcnt) {
796 ts->tdma_slotcnt = ireq->i_val;
800 case IEEE80211_IOC_TDMA_SLOTLEN:
803 * 150 insures at least 1/8 TU
804 * 0xfffff is the max duration for bursting
805 * (implict by way of 16-bit data type for i_val)
807 if (!TDMA_SLOTLEN_VALID(ireq->i_val))
809 if (ireq->i_val != ts->tdma_slotlen) {
810 ts->tdma_slotlen = ireq->i_val;
814 case IEEE80211_IOC_TDMA_BINTERVAL:
815 if (!TDMA_BINTVAL_VALID(ireq->i_val))
817 if (ireq->i_val != ts->tdma_bintval) {
818 ts->tdma_bintval = ireq->i_val;
827 ieee80211_beacon_notify(vap, IEEE80211_BEACON_TDMA);
830 IEEE80211_IOCTL_SET(tdma, tdma_ioctl_set80211);
832 #endif /* IEEE80211_SUPPORT_TDMA */