2 * SPDX-License-Identifier: BSD-2-Clause
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>
34 * IEEE 802.11 TDMA mode support.
40 #ifdef IEEE80211_SUPPORT_TDMA
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/endian.h>
51 #include <sys/errno.h>
53 #include <sys/sysctl.h>
56 #include <net/if_media.h>
57 #include <net/if_llc.h>
58 #include <net/ethernet.h>
62 #include <net80211/ieee80211_var.h>
63 #include <net80211/ieee80211_tdma.h>
64 #include <net80211/ieee80211_input.h>
66 #ifndef TDMA_SLOTLEN_DEFAULT
67 #define TDMA_SLOTLEN_DEFAULT 10*1000 /* 10ms */
69 #ifndef TDMA_SLOTCNT_DEFAULT
70 #define TDMA_SLOTCNT_DEFAULT 2 /* 2x (pt-to-pt) */
72 #ifndef TDMA_BINTVAL_DEFAULT
73 #define TDMA_BINTVAL_DEFAULT 5 /* 5x ~= 100TU beacon intvl */
75 #ifndef TDMA_TXRATE_11B_DEFAULT
76 #define TDMA_TXRATE_11B_DEFAULT 2*11
78 #ifndef TDMA_TXRATE_11G_DEFAULT
79 #define TDMA_TXRATE_11G_DEFAULT 2*24
81 #ifndef TDMA_TXRATE_11A_DEFAULT
82 #define TDMA_TXRATE_11A_DEFAULT 2*24
84 #ifndef TDMA_TXRATE_TURBO_DEFAULT
85 #define TDMA_TXRATE_TURBO_DEFAULT 2*24
87 #ifndef TDMA_TXRATE_HALF_DEFAULT
88 #define TDMA_TXRATE_HALF_DEFAULT 2*12
90 #ifndef TDMA_TXRATE_QUARTER_DEFAULT
91 #define TDMA_TXRATE_QUARTER_DEFAULT 2*6
93 #ifndef TDMA_TXRATE_11NA_DEFAULT
94 #define TDMA_TXRATE_11NA_DEFAULT (4 | IEEE80211_RATE_MCS)
96 #ifndef TDMA_TXRATE_11NG_DEFAULT
97 #define TDMA_TXRATE_11NG_DEFAULT (4 | IEEE80211_RATE_MCS)
100 #define TDMA_VERSION_VALID(_version) \
101 (TDMA_VERSION_V2 <= (_version) && (_version) <= TDMA_VERSION)
102 #define TDMA_SLOTCNT_VALID(_slotcnt) \
103 (2 <= (_slotcnt) && (_slotcnt) <= TDMA_MAXSLOTS)
104 /* XXX magic constants */
105 #define TDMA_SLOTLEN_VALID(_slotlen) \
106 (2*100 <= (_slotlen) && (unsigned)(_slotlen) <= 0xfffff)
107 /* XXX probably should set a max */
108 #define TDMA_BINTVAL_VALID(_bintval) (1 <= (_bintval))
111 * This code is not prepared to handle more than 2 slots.
113 CTASSERT(TDMA_MAXSLOTS == 2);
115 static void tdma_vdetach(struct ieee80211vap *vap);
116 static int tdma_newstate(struct ieee80211vap *, enum ieee80211_state, int);
117 static void tdma_beacon_miss(struct ieee80211vap *vap);
118 static void tdma_recv_mgmt(struct ieee80211_node *, struct mbuf *,
119 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf);
120 static int tdma_update(struct ieee80211vap *vap,
121 const struct ieee80211_tdma_param *tdma, struct ieee80211_node *ni,
123 static int tdma_process_params(struct ieee80211_node *ni,
124 const u_int8_t *ie, int rssi, int nf, const struct ieee80211_frame *wh);
127 settxparms(struct ieee80211vap *vap, enum ieee80211_phymode mode, int rate)
129 if (isclr(vap->iv_ic->ic_modecaps, mode))
132 vap->iv_txparms[mode].ucastrate = rate;
133 vap->iv_txparms[mode].mcastrate = rate;
137 setackpolicy(struct ieee80211com *ic, int noack)
139 struct ieee80211_wme_state *wme = &ic->ic_wme;
142 for (ac = 0; ac < WME_NUM_AC; ac++) {
143 wme->wme_chanParams.cap_wmeParams[ac].wmep_noackPolicy = noack;
144 wme->wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy = noack;
149 ieee80211_tdma_vattach(struct ieee80211vap *vap)
151 struct ieee80211_tdma_state *ts;
153 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
154 ("not a tdma vap, caps 0x%x", vap->iv_caps));
156 ts = (struct ieee80211_tdma_state *) IEEE80211_MALLOC(
157 sizeof(struct ieee80211_tdma_state), M_80211_VAP,
158 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
160 printf("%s: cannot allocate TDMA state block\n", __func__);
161 /* NB: fall back to adhdemo mode */
162 vap->iv_caps &= ~IEEE80211_C_TDMA;
165 /* NB: default configuration is passive so no beacons */
166 ts->tdma_version = TDMA_VERSION;
167 ts->tdma_slotlen = TDMA_SLOTLEN_DEFAULT;
168 ts->tdma_slotcnt = TDMA_SLOTCNT_DEFAULT;
169 ts->tdma_bintval = TDMA_BINTVAL_DEFAULT;
170 ts->tdma_slot = 1; /* passive operation */
172 /* setup default fixed rates */
173 settxparms(vap, IEEE80211_MODE_11A, TDMA_TXRATE_11A_DEFAULT);
174 settxparms(vap, IEEE80211_MODE_11B, TDMA_TXRATE_11B_DEFAULT);
175 settxparms(vap, IEEE80211_MODE_11G, TDMA_TXRATE_11G_DEFAULT);
176 settxparms(vap, IEEE80211_MODE_TURBO_A, TDMA_TXRATE_TURBO_DEFAULT);
177 settxparms(vap, IEEE80211_MODE_TURBO_G, TDMA_TXRATE_TURBO_DEFAULT);
178 settxparms(vap, IEEE80211_MODE_STURBO_A, TDMA_TXRATE_TURBO_DEFAULT);
179 settxparms(vap, IEEE80211_MODE_11NA, TDMA_TXRATE_11NA_DEFAULT);
180 settxparms(vap, IEEE80211_MODE_11NG, TDMA_TXRATE_11NG_DEFAULT);
181 settxparms(vap, IEEE80211_MODE_HALF, TDMA_TXRATE_HALF_DEFAULT);
182 settxparms(vap, IEEE80211_MODE_QUARTER, TDMA_TXRATE_QUARTER_DEFAULT);
183 settxparms(vap, IEEE80211_MODE_VHT_2GHZ, TDMA_TXRATE_11NG_DEFAULT);
184 settxparms(vap, IEEE80211_MODE_VHT_5GHZ, TDMA_TXRATE_11NA_DEFAULT);
186 setackpolicy(vap->iv_ic, 1); /* disable ACK's */
188 ts->tdma_opdetach = vap->iv_opdetach;
189 vap->iv_opdetach = tdma_vdetach;
190 ts->tdma_newstate = vap->iv_newstate;
191 vap->iv_newstate = tdma_newstate;
192 vap->iv_bmiss = tdma_beacon_miss;
193 ts->tdma_recv_mgmt = vap->iv_recv_mgmt;
194 vap->iv_recv_mgmt = tdma_recv_mgmt;
200 tdma_vdetach(struct ieee80211vap *vap)
202 struct ieee80211_tdma_state *ts = vap->iv_tdma;
205 /* NB: should not have touched any ic state */
208 ts->tdma_opdetach(vap);
209 IEEE80211_FREE(vap->iv_tdma, M_80211_VAP);
212 setackpolicy(vap->iv_ic, 0); /* enable ACK's */
216 sta_leave(void *arg, struct ieee80211_node *ni)
218 struct ieee80211vap *vap = ni->ni_vap;
220 if (ni != vap->iv_bss)
221 ieee80211_node_leave(ni);
225 * TDMA state machine handler.
228 tdma_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
230 struct ieee80211_tdma_state *ts = vap->iv_tdma;
231 struct ieee80211com *ic = vap->iv_ic;
232 enum ieee80211_state ostate;
235 IEEE80211_LOCK_ASSERT(ic);
237 ostate = vap->iv_state;
238 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
239 __func__, ieee80211_state_name[ostate],
240 ieee80211_state_name[nstate], arg);
242 if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
243 callout_stop(&vap->iv_swbmiss);
244 if (nstate == IEEE80211_S_SCAN &&
245 (ostate == IEEE80211_S_INIT || ostate == IEEE80211_S_RUN) &&
246 ts->tdma_slot != 0) {
248 * Override adhoc behaviour when operating as a slave;
249 * we need to scan even if the channel is locked.
251 vap->iv_state = nstate; /* state transition */
252 ieee80211_cancel_scan(vap); /* background scan */
253 if (ostate == IEEE80211_S_RUN) {
254 /* purge station table; entries are stale */
255 ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
258 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
259 ieee80211_check_scan(vap,
260 vap->iv_scanreq_flags,
261 vap->iv_scanreq_duration,
262 vap->iv_scanreq_mindwell,
263 vap->iv_scanreq_maxdwell,
264 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
265 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
267 ieee80211_check_scan_current(vap);
270 status = ts->tdma_newstate(vap, nstate, arg);
273 nstate == IEEE80211_S_RUN && ostate != IEEE80211_S_RUN &&
274 (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) &&
275 ts->tdma_slot != 0 &&
276 vap->iv_des_chan == IEEE80211_CHAN_ANYC) {
278 * Start s/w beacon miss timer for slave devices w/o
279 * hardware support. Note we do this only if we're
280 * not locked to a channel (i.e. roam to follow the
281 * master). The 2x is a fudge for our doing this in
284 vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
285 2 * vap->iv_bmissthreshold * ts->tdma_bintval *
286 ((ts->tdma_slotcnt * ts->tdma_slotlen) / 1024));
287 vap->iv_swbmiss_count = 0;
288 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
289 ieee80211_swbmiss, vap);
295 tdma_beacon_miss(struct ieee80211vap *vap)
297 struct ieee80211_tdma_state *ts = vap->iv_tdma;
299 IEEE80211_LOCK_ASSERT(vap->iv_ic);
301 KASSERT((vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
302 KASSERT(vap->iv_state == IEEE80211_S_RUN,
303 ("wrong state %d", vap->iv_state));
305 IEEE80211_DPRINTF(vap,
306 IEEE80211_MSG_STATE | IEEE80211_MSG_TDMA | IEEE80211_MSG_DEBUG,
307 "beacon miss, mode %u state %s\n",
308 vap->iv_opmode, ieee80211_state_name[vap->iv_state]);
310 callout_stop(&vap->iv_swbmiss);
312 if (ts->tdma_peer != NULL) { /* XXX? can this be null? */
313 ieee80211_notify_node_leave(vap->iv_bss);
314 ts->tdma_peer = NULL;
316 * Treat beacon miss like an associate failure wrt the
317 * scan policy; this forces the entry in the scan cache
318 * to be ignored after several tries.
320 ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr,
321 IEEE80211_STATUS_TIMEOUT);
324 ts->tdma_inuse = 0; /* clear slot usage */
326 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
330 tdma_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
331 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
333 struct ieee80211com *ic = ni->ni_ic;
334 struct ieee80211vap *vap = ni->ni_vap;
335 struct ieee80211_tdma_state *ts = vap->iv_tdma;
337 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON &&
338 (ic->ic_flags & IEEE80211_F_SCAN) == 0) {
339 struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
340 struct ieee80211_scanparams scan;
342 /* XXX TODO: use rxstatus to determine off-channel beacons */
343 if (ieee80211_parse_beacon(ni, m0, ic->ic_curchan, &scan) != 0)
345 if (scan.tdma == NULL) {
347 * TDMA stations must beacon a TDMA ie; ignore
349 * XXX detect overlapping bss and change channel
351 IEEE80211_DISCARD(vap,
352 IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
353 wh, ieee80211_mgt_subtype_name(subtype),
355 vap->iv_stats.is_rx_mgtdiscard++;
358 if (ni == vap->iv_bss &&
359 !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
361 * Fake up a node for this newly
362 * discovered member of the IBSS.
364 ni = ieee80211_add_neighbor(vap, wh, &scan);
366 /* NB: stat kept for alloc failure */
371 * Check for state updates.
373 if (IEEE80211_ADDR_EQ(wh->i_addr3, ni->ni_bssid)) {
375 * Count frame now that we know it's to be processed.
377 vap->iv_stats.is_rx_beacon++;
378 IEEE80211_NODE_STAT(ni, rx_beacons);
380 * Record tsf of last beacon. NB: this must be
381 * done before calling tdma_process_params
382 * as deeper routines reference it.
384 memcpy(&ni->ni_tstamp.data, scan.tstamp,
385 sizeof(ni->ni_tstamp.data));
387 * Count beacon frame for s/w bmiss handling.
389 vap->iv_swbmiss_count++;
391 * Process tdma ie. The contents are used to sync
392 * the slot timing, reconfigure the bss, etc.
394 (void) tdma_process_params(ni, scan.tdma, rssi, nf, wh);
398 * NB: defer remaining work to the adhoc code; this causes
399 * 2x parsing of the frame but should happen infrequently
402 ts->tdma_recv_mgmt(ni, m0, subtype, rxs, rssi, nf);
406 * Update TDMA state on receipt of a beacon frame with
407 * a TDMA information element. The sender's identity
408 * is provided so we can track who our peer is. If pickslot
409 * is non-zero we scan the slot allocation state in the ie
410 * to locate a free slot for our use.
413 tdma_update(struct ieee80211vap *vap, const struct ieee80211_tdma_param *tdma,
414 struct ieee80211_node *ni, int pickslot)
416 struct ieee80211_tdma_state *ts = vap->iv_tdma;
417 int slot, slotlen, update;
419 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
420 ("not a tdma vap, caps 0x%x", vap->iv_caps));
423 if (tdma->tdma_slotcnt != ts->tdma_slotcnt) {
424 if (!TDMA_SLOTCNT_VALID(tdma->tdma_slotcnt)) {
425 if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
426 printf("%s: bad slot cnt %u\n",
427 __func__, tdma->tdma_slotcnt);
430 update |= TDMA_UPDATE_SLOTCNT;
432 slotlen = le16toh(tdma->tdma_slotlen) * 100;
433 if (slotlen != ts->tdma_slotlen) {
434 if (!TDMA_SLOTLEN_VALID(slotlen)) {
435 if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
436 printf("%s: bad slot len %u\n",
440 update |= TDMA_UPDATE_SLOTLEN;
442 if (tdma->tdma_bintval != ts->tdma_bintval) {
443 if (!TDMA_BINTVAL_VALID(tdma->tdma_bintval)) {
444 if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
445 printf("%s: bad beacon interval %u\n",
446 __func__, tdma->tdma_bintval);
449 update |= TDMA_UPDATE_BINTVAL;
451 slot = ts->tdma_slot;
454 * Pick unoccupied slot. Note we never choose slot 0.
456 for (slot = tdma->tdma_slotcnt-1; slot > 0; slot--)
457 if (isclr(tdma->tdma_inuse, slot))
460 printf("%s: no free slot, slotcnt %u inuse: 0x%x\n",
461 __func__, tdma->tdma_slotcnt,
462 tdma->tdma_inuse[0]);
463 /* XXX need to do something better */
466 if (slot != ts->tdma_slot)
467 update |= TDMA_UPDATE_SLOT;
469 if (ni != ts->tdma_peer) {
470 /* update everything */
471 update = TDMA_UPDATE_SLOT
472 | TDMA_UPDATE_SLOTCNT
473 | TDMA_UPDATE_SLOTLEN
474 | TDMA_UPDATE_BINTVAL;
479 * New/changed parameters; update runtime state.
481 /* XXX overwrites user parameters */
482 if (update & TDMA_UPDATE_SLOTCNT)
483 ts->tdma_slotcnt = tdma->tdma_slotcnt;
484 if (update & TDMA_UPDATE_SLOTLEN)
485 ts->tdma_slotlen = slotlen;
486 if (update & TDMA_UPDATE_SLOT)
487 ts->tdma_slot = slot;
488 if (update & TDMA_UPDATE_BINTVAL)
489 ts->tdma_bintval = tdma->tdma_bintval;
490 /* mark beacon to be updated before next xmit */
491 ieee80211_beacon_notify(vap, IEEE80211_BEACON_TDMA);
493 IEEE80211_DPRINTF(vap, IEEE80211_MSG_TDMA,
494 "%s: slot %u slotcnt %u slotlen %u us bintval %u\n",
495 __func__, ts->tdma_slot, ts->tdma_slotcnt,
496 ts->tdma_slotlen, ts->tdma_bintval);
499 * Notify driver. Note we can be called before
500 * entering RUN state if we scanned and are
501 * joining an existing bss. In that case do not
502 * call the driver because not all necessary state
503 * has been setup. The next beacon will dtrt.
505 if (vap->iv_state == IEEE80211_S_RUN)
506 vap->iv_ic->ic_tdma_update(ni, tdma, update);
508 * Dispatch join event on first beacon from new master.
510 if (ts->tdma_peer != ni) {
511 if (ts->tdma_peer != NULL)
512 ieee80211_notify_node_leave(vap->iv_bss);
513 ieee80211_notify_node_join(ni, 1);
514 /* NB: no reference, we just use the address */
521 * Process received TDMA parameters.
524 tdma_process_params(struct ieee80211_node *ni, const u_int8_t *ie,
525 int rssi, int nf, const struct ieee80211_frame *wh)
527 struct ieee80211vap *vap = ni->ni_vap;
528 struct ieee80211_tdma_state *ts = vap->iv_tdma;
529 const struct ieee80211_tdma_param *tdma =
530 (const struct ieee80211_tdma_param *) ie;
533 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
534 ("not a tdma vap, caps 0x%x", vap->iv_caps));
536 if (len < sizeof(*tdma) - 2) {
537 IEEE80211_DISCARD_IE(vap,
538 IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
539 wh, "tdma", "too short, len %u", len);
540 return IEEE80211_REASON_IE_INVALID;
542 if (tdma->tdma_version != ts->tdma_version) {
543 IEEE80211_DISCARD_IE(vap,
544 IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
545 wh, "tdma", "bad version %u (ours %u)",
546 tdma->tdma_version, ts->tdma_version);
547 return IEEE80211_REASON_IE_INVALID;
550 * NB: ideally we'd check against tdma_slotcnt, but that
551 * would require extra effort so do this easy check that
552 * covers the work below; more stringent checks are done
553 * before we make more extensive use of the ie contents.
555 if (tdma->tdma_slot >= TDMA_MAXSLOTS) {
556 IEEE80211_DISCARD_IE(vap,
557 IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
558 wh, "tdma", "invalid slot %u", tdma->tdma_slot);
559 return IEEE80211_REASON_IE_INVALID;
562 * Can reach here while scanning, update
563 * operational state only in RUN state.
565 if (vap->iv_state == IEEE80211_S_RUN) {
566 if (tdma->tdma_slot != ts->tdma_slot &&
567 isclr(ts->tdma_inuse, tdma->tdma_slot)) {
568 IEEE80211_NOTE(vap, IEEE80211_MSG_TDMA, ni,
569 "discovered in slot %u", tdma->tdma_slot);
570 setbit(ts->tdma_inuse, tdma->tdma_slot);
571 /* XXX dispatch event only when operating as master */
572 if (ts->tdma_slot == 0)
573 ieee80211_notify_node_join(ni, 1);
575 setbit(ts->tdma_active, tdma->tdma_slot);
576 if (tdma->tdma_slot == ts->tdma_slot-1) {
578 * Slave tsf synchronization to station
579 * just before us in the schedule. The driver
580 * is responsible for copying the timestamp
581 * of the received beacon into our beacon
582 * frame so the sender can calculate round
583 * trip time. We cannot do that here because
584 * we don't know how to update our beacon frame.
586 (void) tdma_update(vap, tdma, ni, 0);
587 /* XXX reschedule swbmiss timer on parameter change */
588 } else if (tdma->tdma_slot == ts->tdma_slot+1) {
591 uint32_t rstamp = (uint32_t) le64toh(rs->tsf);
595 * Use returned timstamp to calculate the
598 memcpy(&tstamp, tdma->tdma_tstamp, 8);
600 /* XXX use only 15 bits of rstamp */
601 rtt = rstamp - (le64toh(tstamp) & 0x7fff);
604 /* XXX hack to quiet normal use */
605 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DOT1X,
606 "tdma rtt %5u [rstamp %5u tstamp %llu]\n",
608 (unsigned long long) le64toh(tstamp));
610 } else if (tdma->tdma_slot == ts->tdma_slot &&
611 le64toh(ni->ni_tstamp.tsf) > vap->iv_bss->ni_tstamp.tsf) {
613 * Station using the same slot as us and has
614 * been around longer than us; we must move.
615 * Note this can happen if stations do not
616 * see each other while scanning.
618 IEEE80211_DPRINTF(vap, IEEE80211_MSG_TDMA,
619 "slot %u collision rxtsf %llu tsf %llu\n",
621 (unsigned long long) le64toh(ni->ni_tstamp.tsf),
622 vap->iv_bss->ni_tstamp.tsf);
623 setbit(ts->tdma_inuse, tdma->tdma_slot);
625 (void) tdma_update(vap, tdma, ni, 1);
632 ieee80211_tdma_getslot(struct ieee80211vap *vap)
634 struct ieee80211_tdma_state *ts = vap->iv_tdma;
636 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
637 ("not a tdma vap, caps 0x%x", vap->iv_caps));
638 return ts->tdma_slot;
642 * Parse a TDMA ie on station join and use it to setup node state.
645 ieee80211_parse_tdma(struct ieee80211_node *ni, const uint8_t *ie)
647 struct ieee80211vap *vap = ni->ni_vap;
649 if (vap->iv_caps & IEEE80211_C_TDMA) {
650 const struct ieee80211_tdma_param *tdma =
651 (const struct ieee80211_tdma_param *)ie;
652 struct ieee80211_tdma_state *ts = vap->iv_tdma;
654 * Adopt TDMA configuration when joining an
657 setbit(ts->tdma_inuse, tdma->tdma_slot);
658 (void) tdma_update(vap, tdma, ni, 1);
660 * Propagate capabilities based on the local
661 * configuration and the remote station's advertised
662 * capabilities. In particular this permits us to
663 * enable use of QoS to disable ACK's.
665 if ((vap->iv_flags & IEEE80211_F_WME) &&
666 ni->ni_ies.wme_ie != NULL)
667 ni->ni_flags |= IEEE80211_NODE_QOS;
671 #define TDMA_OUI_BYTES 0x00, 0x03, 0x7f
673 * Add a TDMA parameters element to a frame.
676 ieee80211_add_tdma(uint8_t *frm, struct ieee80211vap *vap)
678 #define ADDSHORT(frm, v) do { \
679 frm[0] = (v) & 0xff; \
683 static const struct ieee80211_tdma_param param = {
684 .tdma_id = IEEE80211_ELEMID_VENDOR,
685 .tdma_len = sizeof(struct ieee80211_tdma_param) - 2,
686 .tdma_oui = { TDMA_OUI_BYTES },
687 .tdma_type = TDMA_OUI_TYPE,
688 .tdma_subtype = TDMA_SUBTYPE_PARAM,
689 .tdma_version = TDMA_VERSION,
691 const struct ieee80211_tdma_state *ts = vap->iv_tdma;
694 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
695 ("not a tdma vap, caps 0x%x", vap->iv_caps));
697 memcpy(frm, ¶m, sizeof(param));
698 frm += __offsetof(struct ieee80211_tdma_param, tdma_slot);
699 *frm++ = ts->tdma_slot;
700 *frm++ = ts->tdma_slotcnt;
701 /* NB: convert units to fit in 16-bits */
702 slotlen = ts->tdma_slotlen / 100; /* 100us units */
703 ADDSHORT(frm, slotlen);
704 *frm++ = ts->tdma_bintval;
705 *frm++ = ts->tdma_inuse[0];
706 frm += 10; /* pad+timestamp */
710 #undef TDMA_OUI_BYTES
713 * Update TDMA state at TBTT.
716 ieee80211_tdma_update_beacon(struct ieee80211vap *vap,
717 struct ieee80211_beacon_offsets *bo)
719 struct ieee80211_tdma_state *ts = vap->iv_tdma;
721 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
722 ("not a tdma vap, caps 0x%x", vap->iv_caps));
724 if (isset(bo->bo_flags, IEEE80211_BEACON_TDMA)) {
725 (void) ieee80211_add_tdma(bo->bo_tdma, vap);
726 clrbit(bo->bo_flags, IEEE80211_BEACON_TDMA);
728 if (ts->tdma_slot != 0) /* only on master */
730 if (ts->tdma_count <= 0) {
732 * Time to update the mask of active/inuse stations.
733 * We track stations that we've received a beacon
734 * frame from and update this mask periodically.
735 * This allows us to miss a few beacons before marking
736 * a slot free for re-use.
738 ts->tdma_inuse[0] = ts->tdma_active[0];
739 ts->tdma_active[0] = 0x01;
740 /* update next time 'round */
741 /* XXX use notify framework */
742 setbit(bo->bo_flags, IEEE80211_BEACON_TDMA);
743 /* NB: use s/w beacon miss threshold; may be too high */
744 ts->tdma_count = vap->iv_bmissthreshold-1;
750 tdma_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
752 struct ieee80211_tdma_state *ts = vap->iv_tdma;
754 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
757 switch (ireq->i_type) {
758 case IEEE80211_IOC_TDMA_SLOT:
759 ireq->i_val = ts->tdma_slot;
761 case IEEE80211_IOC_TDMA_SLOTCNT:
762 ireq->i_val = ts->tdma_slotcnt;
764 case IEEE80211_IOC_TDMA_SLOTLEN:
765 ireq->i_val = ts->tdma_slotlen;
767 case IEEE80211_IOC_TDMA_BINTERVAL:
768 ireq->i_val = ts->tdma_bintval;
775 IEEE80211_IOCTL_GET(tdma, tdma_ioctl_get80211);
778 tdma_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
780 struct ieee80211_tdma_state *ts = vap->iv_tdma;
782 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
785 switch (ireq->i_type) {
786 case IEEE80211_IOC_TDMA_SLOT:
787 if (!(0 <= ireq->i_val && ireq->i_val <= ts->tdma_slotcnt))
789 if (ireq->i_val != ts->tdma_slot) {
790 ts->tdma_slot = ireq->i_val;
794 case IEEE80211_IOC_TDMA_SLOTCNT:
795 if (!TDMA_SLOTCNT_VALID(ireq->i_val))
797 if (ireq->i_val != ts->tdma_slotcnt) {
798 ts->tdma_slotcnt = ireq->i_val;
802 case IEEE80211_IOC_TDMA_SLOTLEN:
805 * 150 insures at least 1/8 TU
806 * 0xfffff is the max duration for bursting
807 * (implict by way of 16-bit data type for i_val)
809 if (!TDMA_SLOTLEN_VALID(ireq->i_val))
811 if (ireq->i_val != ts->tdma_slotlen) {
812 ts->tdma_slotlen = ireq->i_val;
816 case IEEE80211_IOC_TDMA_BINTERVAL:
817 if (!TDMA_BINTVAL_VALID(ireq->i_val))
819 if (ireq->i_val != ts->tdma_bintval) {
820 ts->tdma_bintval = ireq->i_val;
829 ieee80211_beacon_notify(vap, IEEE80211_BEACON_TDMA);
832 IEEE80211_IOCTL_SET(tdma, tdma_ioctl_set80211);
834 #endif /* IEEE80211_SUPPORT_TDMA */